1 | ;
|
2 |
|
3 | /**
|
4 | * Module dependencies.
|
5 | */
|
6 |
|
7 | const _require = require('node:string_decoder'),
|
8 | StringDecoder = _require.StringDecoder;
|
9 | const Stream = require('node:stream');
|
10 | const zlib = require('node:zlib');
|
11 |
|
12 | /**
|
13 | * Buffers response data events and re-emits when they're unzipped.
|
14 | *
|
15 | * @param {Request} req
|
16 | * @param {Response} res
|
17 | * @api private
|
18 | */
|
19 |
|
20 | exports.unzip = (request, res) => {
|
21 | const unzip = zlib.createUnzip();
|
22 | const stream = new Stream();
|
23 | let decoder;
|
24 |
|
25 | // make node responseOnEnd() happy
|
26 | stream.req = request;
|
27 | unzip.on('error', error => {
|
28 | if (error && error.code === 'Z_BUF_ERROR') {
|
29 | // unexpected end of file is ignored by browsers and curl
|
30 | stream.emit('end');
|
31 | return;
|
32 | }
|
33 | stream.emit('error', error);
|
34 | });
|
35 |
|
36 | // pipe to unzip
|
37 | res.pipe(unzip);
|
38 |
|
39 | // override `setEncoding` to capture encoding
|
40 | res.setEncoding = type => {
|
41 | decoder = new StringDecoder(type);
|
42 | };
|
43 |
|
44 | // decode upon decompressing with captured encoding
|
45 | unzip.on('data', buf => {
|
46 | if (decoder) {
|
47 | const string_ = decoder.write(buf);
|
48 | if (string_.length > 0) stream.emit('data', string_);
|
49 | } else {
|
50 | stream.emit('data', buf);
|
51 | }
|
52 | });
|
53 | unzip.on('end', () => {
|
54 | stream.emit('end');
|
55 | });
|
56 |
|
57 | // override `on` to capture data listeners
|
58 | const _on = res.on;
|
59 | res.on = function (type, fn) {
|
60 | if (type === 'data' || type === 'end') {
|
61 | stream.on(type, fn.bind(res));
|
62 | } else if (type === 'error') {
|
63 | stream.on(type, fn.bind(res));
|
64 | _on.call(res, type, fn);
|
65 | } else {
|
66 | _on.call(res, type, fn);
|
67 | }
|
68 | return this;
|
69 | };
|
70 | };
|
71 | //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJfc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