node-unzipper icon indicating copy to clipboard operation
node-unzipper copied to clipboard

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

Open lucasrmendonca opened this issue 1 year ago • 9 comments

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]'
  }
}

lucasrmendonca avatar Feb 04 '24 20:02 lucasrmendonca

did you ever get this working?

sethtjf avatar Mar 17 '24 19:03 sethtjf

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

lucasrmendonca avatar Mar 19 '24 02:03 lucasrmendonca

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

ZJONSSON avatar Apr 14 '24 18:04 ZJONSSON

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

rdsedmundo avatar Apr 18 '24 20:04 rdsedmundo

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

ZJONSSON avatar Apr 22 '24 21:04 ZJONSSON

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.

jpambrun avatar Apr 27 '24 20:04 jpambrun

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.

sethtjf avatar Apr 28 '24 02:04 sethtjf

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?

ecofi avatar Jul 22 '24 18:07 ecofi