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

Is there another HTTP library I can use?

Open skytreader opened this issue 4 years ago • 2 comments

The docs make use of request, a library deprecated a year ago. Is there another library I can use?

FWIW, I tried using got and I get:

node_modules/unzipper/lib/Open/index.js:61
            req.abort();
                ^

TypeError: req.abort is not a function
    at EventEmitter.<anonymous> (/home/ad/cestioco/language-service/node_modules/unzipper/lib/Open/index.js:61:17)
    at EventEmitter.emit (node:events:378:20)
    at EventEmitter.<anonymous> (/home/ad/cestioco/language-service/node_modules/got/source/as-promise.js:20:10)
    at EventEmitter.emit (node:events:378:20)
    at module.exports (/home/ad/cestioco/language-service/node_modules/got/source/get-response.js:22:10)
    at ClientRequest.handleResponse (/home/ad/cestioco/language-service/node_modules/got/source/request-as-event-emitter.js:155:5)
    at Object.onceWrapper (node:events:485:26)
    at ClientRequest.emit (node:events:390:22)
    at ClientRequest.origin.emit (/home/ad/cestioco/language-service/node_modules/@szmarczak/http-timer/source/index.js:37:11)
    at HTTPParser.parserOnIncomingClient [as onIncoming] (node:_http_client:636:27)
    at HTTPParser.parserOnHeadersComplete (node:_http_common:129:17)
    at TLSSocket.socketOnData (node:_http_client:502:22)
    at TLSSocket.emit (node:events:378:20)
    at addChunk (node:internal/streams/readable:313:12)
    at readableAddChunk (node:internal/streams/readable:288:9)
    at TLSSocket.Readable.push (node:internal/streams/readable:227:10)

skytreader avatar Feb 10 '21 13:02 skytreader

Actually the current version (v0.10.11) of this library requires an http lib which have specific methods. The got does have all the methods but one is missing. i.e. abort()

You can still use got or any library of your own as long as you pass the object which have desired method what this lib is looking for.

A very minimalist version of code is here demonstrating how to use got library

const got = require('got');
const unzipper = require('unzipper');

const zipRequestProxy = (options) => {
    const { url, headers } = options;
    const stream = got.stream(url, { headers });
    var proxy = Object.assign(stream, { abort: stream.destroy });
    return proxy;
}

const directory = await unzipper.Open.url(zipRequestProxy, q);


Note- if you are using typescript you might have to cast the proxy object to unknown and then cast it back to ClientRequest to avoid compile error. Or perhaps casting to any would work.

mnsrulz avatar Jan 19 '22 03:01 mnsrulz

And of course you can find the full code implementation and demo in https://github.com/mnsrulz/zipservice-netlify

mnsrulz avatar Jan 19 '22 03:01 mnsrulz