index.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || (function () {
  19. var ownKeys = function(o) {
  20. ownKeys = Object.getOwnPropertyNames || function (o) {
  21. var ar = [];
  22. for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
  23. return ar;
  24. };
  25. return ownKeys(o);
  26. };
  27. return function (mod) {
  28. if (mod && mod.__esModule) return mod;
  29. var result = {};
  30. if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
  31. __setModuleDefault(result, mod);
  32. return result;
  33. };
  34. })();
  35. var __importDefault = (this && this.__importDefault) || function (mod) {
  36. return (mod && mod.__esModule) ? mod : { "default": mod };
  37. };
  38. Object.defineProperty(exports, "__esModule", { value: true });
  39. exports.BrotliDecompress = exports.BrotliCompress = exports.Brotli = exports.Unzip = exports.InflateRaw = exports.DeflateRaw = exports.Gunzip = exports.Gzip = exports.Inflate = exports.Deflate = exports.Zlib = exports.ZlibError = exports.constants = void 0;
  40. const assert_1 = __importDefault(require("assert"));
  41. const buffer_1 = require("buffer");
  42. const minipass_1 = require("minipass");
  43. const realZlib = __importStar(require("zlib"));
  44. const constants_js_1 = require("./constants.js");
  45. var constants_js_2 = require("./constants.js");
  46. Object.defineProperty(exports, "constants", { enumerable: true, get: function () { return constants_js_2.constants; } });
  47. const OriginalBufferConcat = buffer_1.Buffer.concat;
  48. const desc = Object.getOwnPropertyDescriptor(buffer_1.Buffer, 'concat');
  49. const noop = (args) => args;
  50. const passthroughBufferConcat = desc?.writable === true || desc?.set !== undefined
  51. ? (makeNoOp) => {
  52. buffer_1.Buffer.concat = makeNoOp ? noop : OriginalBufferConcat;
  53. }
  54. : (_) => { };
  55. const _superWrite = Symbol('_superWrite');
  56. class ZlibError extends Error {
  57. code;
  58. errno;
  59. constructor(err) {
  60. super('zlib: ' + err.message);
  61. this.code = err.code;
  62. this.errno = err.errno;
  63. /* c8 ignore next */
  64. if (!this.code)
  65. this.code = 'ZLIB_ERROR';
  66. this.message = 'zlib: ' + err.message;
  67. Error.captureStackTrace(this, this.constructor);
  68. }
  69. get name() {
  70. return 'ZlibError';
  71. }
  72. }
  73. exports.ZlibError = ZlibError;
  74. // the Zlib class they all inherit from
  75. // This thing manages the queue of requests, and returns
  76. // true or false if there is anything in the queue when
  77. // you call the .write() method.
  78. const _flushFlag = Symbol('flushFlag');
  79. class ZlibBase extends minipass_1.Minipass {
  80. #sawError = false;
  81. #ended = false;
  82. #flushFlag;
  83. #finishFlushFlag;
  84. #fullFlushFlag;
  85. #handle;
  86. #onError;
  87. get sawError() {
  88. return this.#sawError;
  89. }
  90. get handle() {
  91. return this.#handle;
  92. }
  93. /* c8 ignore start */
  94. get flushFlag() {
  95. return this.#flushFlag;
  96. }
  97. /* c8 ignore stop */
  98. constructor(opts, mode) {
  99. if (!opts || typeof opts !== 'object')
  100. throw new TypeError('invalid options for ZlibBase constructor');
  101. //@ts-ignore
  102. super(opts);
  103. /* c8 ignore start */
  104. this.#flushFlag = opts.flush ?? 0;
  105. this.#finishFlushFlag = opts.finishFlush ?? 0;
  106. this.#fullFlushFlag = opts.fullFlushFlag ?? 0;
  107. /* c8 ignore stop */
  108. // this will throw if any options are invalid for the class selected
  109. try {
  110. // @types/node doesn't know that it exports the classes, but they're there
  111. //@ts-ignore
  112. this.#handle = new realZlib[mode](opts);
  113. }
  114. catch (er) {
  115. // make sure that all errors get decorated properly
  116. throw new ZlibError(er);
  117. }
  118. this.#onError = err => {
  119. // no sense raising multiple errors, since we abort on the first one.
  120. if (this.#sawError)
  121. return;
  122. this.#sawError = true;
  123. // there is no way to cleanly recover.
  124. // continuing only obscures problems.
  125. this.close();
  126. this.emit('error', err);
  127. };
  128. this.#handle?.on('error', er => this.#onError(new ZlibError(er)));
  129. this.once('end', () => this.close);
  130. }
  131. close() {
  132. if (this.#handle) {
  133. this.#handle.close();
  134. this.#handle = undefined;
  135. this.emit('close');
  136. }
  137. }
  138. reset() {
  139. if (!this.#sawError) {
  140. (0, assert_1.default)(this.#handle, 'zlib binding closed');
  141. //@ts-ignore
  142. return this.#handle.reset?.();
  143. }
  144. }
  145. flush(flushFlag) {
  146. if (this.ended)
  147. return;
  148. if (typeof flushFlag !== 'number')
  149. flushFlag = this.#fullFlushFlag;
  150. this.write(Object.assign(buffer_1.Buffer.alloc(0), { [_flushFlag]: flushFlag }));
  151. }
  152. end(chunk, encoding, cb) {
  153. /* c8 ignore start */
  154. if (typeof chunk === 'function') {
  155. cb = chunk;
  156. encoding = undefined;
  157. chunk = undefined;
  158. }
  159. if (typeof encoding === 'function') {
  160. cb = encoding;
  161. encoding = undefined;
  162. }
  163. /* c8 ignore stop */
  164. if (chunk) {
  165. if (encoding)
  166. this.write(chunk, encoding);
  167. else
  168. this.write(chunk);
  169. }
  170. this.flush(this.#finishFlushFlag);
  171. this.#ended = true;
  172. return super.end(cb);
  173. }
  174. get ended() {
  175. return this.#ended;
  176. }
  177. // overridden in the gzip classes to do portable writes
  178. [_superWrite](data) {
  179. return super.write(data);
  180. }
  181. write(chunk, encoding, cb) {
  182. // process the chunk using the sync process
  183. // then super.write() all the outputted chunks
  184. if (typeof encoding === 'function')
  185. (cb = encoding), (encoding = 'utf8');
  186. if (typeof chunk === 'string')
  187. chunk = buffer_1.Buffer.from(chunk, encoding);
  188. if (this.#sawError)
  189. return;
  190. (0, assert_1.default)(this.#handle, 'zlib binding closed');
  191. // _processChunk tries to .close() the native handle after it's done, so we
  192. // intercept that by temporarily making it a no-op.
  193. // diving into the node:zlib internals a bit here
  194. const nativeHandle = this.#handle
  195. ._handle;
  196. const originalNativeClose = nativeHandle.close;
  197. nativeHandle.close = () => { };
  198. const originalClose = this.#handle.close;
  199. this.#handle.close = () => { };
  200. // It also calls `Buffer.concat()` at the end, which may be convenient
  201. // for some, but which we are not interested in as it slows us down.
  202. passthroughBufferConcat(true);
  203. let result = undefined;
  204. try {
  205. const flushFlag = typeof chunk[_flushFlag] === 'number'
  206. ? chunk[_flushFlag]
  207. : this.#flushFlag;
  208. result = this.#handle._processChunk(chunk, flushFlag);
  209. // if we don't throw, reset it back how it was
  210. passthroughBufferConcat(false);
  211. }
  212. catch (err) {
  213. // or if we do, put Buffer.concat() back before we emit error
  214. // Error events call into user code, which may call Buffer.concat()
  215. passthroughBufferConcat(false);
  216. this.#onError(new ZlibError(err));
  217. }
  218. finally {
  219. if (this.#handle) {
  220. // Core zlib resets `_handle` to null after attempting to close the
  221. // native handle. Our no-op handler prevented actual closure, but we
  222. // need to restore the `._handle` property.
  223. ;
  224. this.#handle._handle =
  225. nativeHandle;
  226. nativeHandle.close = originalNativeClose;
  227. this.#handle.close = originalClose;
  228. // `_processChunk()` adds an 'error' listener. If we don't remove it
  229. // after each call, these handlers start piling up.
  230. this.#handle.removeAllListeners('error');
  231. // make sure OUR error listener is still attached tho
  232. }
  233. }
  234. if (this.#handle)
  235. this.#handle.on('error', er => this.#onError(new ZlibError(er)));
  236. let writeReturn;
  237. if (result) {
  238. if (Array.isArray(result) && result.length > 0) {
  239. const r = result[0];
  240. // The first buffer is always `handle._outBuffer`, which would be
  241. // re-used for later invocations; so, we always have to copy that one.
  242. writeReturn = this[_superWrite](buffer_1.Buffer.from(r));
  243. for (let i = 1; i < result.length; i++) {
  244. writeReturn = this[_superWrite](result[i]);
  245. }
  246. }
  247. else {
  248. // either a single Buffer or an empty array
  249. writeReturn = this[_superWrite](buffer_1.Buffer.from(result));
  250. }
  251. }
  252. if (cb)
  253. cb();
  254. return writeReturn;
  255. }
  256. }
  257. class Zlib extends ZlibBase {
  258. #level;
  259. #strategy;
  260. constructor(opts, mode) {
  261. opts = opts || {};
  262. opts.flush = opts.flush || constants_js_1.constants.Z_NO_FLUSH;
  263. opts.finishFlush = opts.finishFlush || constants_js_1.constants.Z_FINISH;
  264. opts.fullFlushFlag = constants_js_1.constants.Z_FULL_FLUSH;
  265. super(opts, mode);
  266. this.#level = opts.level;
  267. this.#strategy = opts.strategy;
  268. }
  269. params(level, strategy) {
  270. if (this.sawError)
  271. return;
  272. if (!this.handle)
  273. throw new Error('cannot switch params when binding is closed');
  274. // no way to test this without also not supporting params at all
  275. /* c8 ignore start */
  276. if (!this.handle.params)
  277. throw new Error('not supported in this implementation');
  278. /* c8 ignore stop */
  279. if (this.#level !== level || this.#strategy !== strategy) {
  280. this.flush(constants_js_1.constants.Z_SYNC_FLUSH);
  281. (0, assert_1.default)(this.handle, 'zlib binding closed');
  282. // .params() calls .flush(), but the latter is always async in the
  283. // core zlib. We override .flush() temporarily to intercept that and
  284. // flush synchronously.
  285. const origFlush = this.handle.flush;
  286. this.handle.flush = (flushFlag, cb) => {
  287. /* c8 ignore start */
  288. if (typeof flushFlag === 'function') {
  289. cb = flushFlag;
  290. flushFlag = this.flushFlag;
  291. }
  292. /* c8 ignore stop */
  293. this.flush(flushFlag);
  294. cb?.();
  295. };
  296. try {
  297. ;
  298. this.handle.params(level, strategy);
  299. }
  300. finally {
  301. this.handle.flush = origFlush;
  302. }
  303. /* c8 ignore start */
  304. if (this.handle) {
  305. this.#level = level;
  306. this.#strategy = strategy;
  307. }
  308. /* c8 ignore stop */
  309. }
  310. }
  311. }
  312. exports.Zlib = Zlib;
  313. // minimal 2-byte header
  314. class Deflate extends Zlib {
  315. constructor(opts) {
  316. super(opts, 'Deflate');
  317. }
  318. }
  319. exports.Deflate = Deflate;
  320. class Inflate extends Zlib {
  321. constructor(opts) {
  322. super(opts, 'Inflate');
  323. }
  324. }
  325. exports.Inflate = Inflate;
  326. class Gzip extends Zlib {
  327. #portable;
  328. constructor(opts) {
  329. super(opts, 'Gzip');
  330. this.#portable = opts && !!opts.portable;
  331. }
  332. [_superWrite](data) {
  333. if (!this.#portable)
  334. return super[_superWrite](data);
  335. // we'll always get the header emitted in one first chunk
  336. // overwrite the OS indicator byte with 0xFF
  337. this.#portable = false;
  338. data[9] = 255;
  339. return super[_superWrite](data);
  340. }
  341. }
  342. exports.Gzip = Gzip;
  343. class Gunzip extends Zlib {
  344. constructor(opts) {
  345. super(opts, 'Gunzip');
  346. }
  347. }
  348. exports.Gunzip = Gunzip;
  349. // raw - no header
  350. class DeflateRaw extends Zlib {
  351. constructor(opts) {
  352. super(opts, 'DeflateRaw');
  353. }
  354. }
  355. exports.DeflateRaw = DeflateRaw;
  356. class InflateRaw extends Zlib {
  357. constructor(opts) {
  358. super(opts, 'InflateRaw');
  359. }
  360. }
  361. exports.InflateRaw = InflateRaw;
  362. // auto-detect header.
  363. class Unzip extends Zlib {
  364. constructor(opts) {
  365. super(opts, 'Unzip');
  366. }
  367. }
  368. exports.Unzip = Unzip;
  369. class Brotli extends ZlibBase {
  370. constructor(opts, mode) {
  371. opts = opts || {};
  372. opts.flush = opts.flush || constants_js_1.constants.BROTLI_OPERATION_PROCESS;
  373. opts.finishFlush =
  374. opts.finishFlush || constants_js_1.constants.BROTLI_OPERATION_FINISH;
  375. opts.fullFlushFlag = constants_js_1.constants.BROTLI_OPERATION_FLUSH;
  376. super(opts, mode);
  377. }
  378. }
  379. exports.Brotli = Brotli;
  380. class BrotliCompress extends Brotli {
  381. constructor(opts) {
  382. super(opts, 'BrotliCompress');
  383. }
  384. }
  385. exports.BrotliCompress = BrotliCompress;
  386. class BrotliDecompress extends Brotli {
  387. constructor(opts) {
  388. super(opts, 'BrotliDecompress');
  389. }
  390. }
  391. exports.BrotliDecompress = BrotliDecompress;
  392. //# sourceMappingURL=index.js.map