ZJONSSON / node-unzipper

node.js cross-platform unzip using streams
Other
435 stars 116 forks source link

How to use Open.url with native "fetch"? #292

Open lucasrmendonca opened 8 months ago

lucasrmendonca commented 8 months ago

How do I use Open.url with native "fetch" on Node.js v20.9.0 ?

const directory = await unzipper.Open.url(fetch, 'https://www.dwsamplefiles.com/?dl_id=559');
TypeError: Failed to parse URL from [object Object]
    at Object.fetch (node:internal/deps/undici/undici:11372:11) {
  [cause]: TypeError: Invalid URL
      at new URL (node:internal/url:775:36)
      at new _Request (node:internal/deps/undici/undici:5055:25)
      at fetch2 (node:internal/deps/undici/undici:9195:25)
      at Object.fetch (node:internal/deps/undici/undici:11370:18)
      at fetch (node:internal/process/pre_execution:282:25)
      at /home/<user>/<project>/node_modules/unzipper/lib/Open/index.js:59:21
      at Promise._execute (/home/<user>/<project>/node_modules/bluebird/js/release/debuggability.js:300:9)
      at Promise._resolveFromExecutor (/home/<user>/<project>/node_modules/bluebird/js/release/promise.js:481:18)
      at new Promise (/home/<user>/<project>/node_modules/bluebird/js/release/promise.js:77:14)
      at Object.size (/home/<user>/<project>/node_modules/unzipper/lib/Open/index.js:58:16)
      at centralDirectory (/home/<user>/<project>/node_modules/unzipper/lib/Open/directory.js:93:17)
      at Object.url (/home/<user>/<project>/node_modules/unzipper/lib/Open/index.js:71:12)
      at start (file:///home/<user>/<project>/index.js:120:45) {
    code: 'ERR_INVALID_URL',
    input: '[object Object]'
  }
}
sethtjf commented 6 months ago

did you ever get this working?

lucasrmendonca commented 6 months ago

Nope, I went with Open.file instead of Open.url since the latter seems to only work with the "requests" library and I didn't want to install an extra dependency just for that

ZJONSSON commented 5 months ago

It would make sense to move to fetch (ideally with backward compatibility for request)

rdsedmundo commented 5 months ago

I was also looking for that because request is now deprecated and also has an open CVE.

ZJONSSON commented 5 months ago

Open to PRs, if not I will find time to do this at some point, hopefully soon!

jpambrun commented 5 months ago

I just stumbled on this issue.

It's not a complete solution, but I have a start of an implementation with fetch in this issue https://github.com/ZJONSSON/node-unzipper/issues/309#issue-2267169008. I needs better error handling which I am struggling with.

sethtjf commented 5 months ago

I was able to hack something together using got but found it unreliable for my use case bc of the location of the central directory at the end of the zip file.

ecofi commented 2 months ago

May I ask whether the .url method would have any benefits compared to .buffer()?

Am I write that the received directory variable would be of same size regardless whether .url() or .buffer() is used? In this case, the only benefit for .url() would be to save memory in avoiding the zip file fully loaded to a buffer first?