@loaders.gl/compression 4.0.0-alpha.5 → 4.0.0-alpha.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (105) hide show
  1. package/dist/brotli/decode.js +2320 -2187
  2. package/dist/bundle.js +2 -2
  3. package/dist/compression-worker.d.ts +1 -1
  4. package/dist/compression-worker.d.ts.map +1 -1
  5. package/dist/compression-worker.js +2 -2
  6. package/dist/compression-worker.js.map +3 -3
  7. package/dist/dist.min.js +3 -3
  8. package/dist/dist.min.js.map +3 -3
  9. package/dist/es5/brotli/decode.js +1778 -0
  10. package/dist/es5/brotli/decode.js.map +1 -0
  11. package/dist/es5/bundle.js +6 -0
  12. package/dist/es5/bundle.js.map +1 -0
  13. package/dist/es5/compression-worker.js +23 -0
  14. package/dist/es5/compression-worker.js.map +1 -0
  15. package/dist/es5/index.js +82 -0
  16. package/dist/es5/index.js.map +1 -0
  17. package/dist/es5/lib/brotli-compression.js +170 -0
  18. package/dist/es5/lib/brotli-compression.js.map +1 -0
  19. package/dist/es5/lib/compression.js +160 -0
  20. package/dist/es5/lib/compression.js.map +1 -0
  21. package/dist/es5/lib/deflate-compression.js +318 -0
  22. package/dist/es5/lib/deflate-compression.js.map +1 -0
  23. package/dist/es5/lib/gzip-compression.js +40 -0
  24. package/dist/es5/lib/gzip-compression.js.map +1 -0
  25. package/dist/es5/lib/lz4-compression.js +119 -0
  26. package/dist/es5/lib/lz4-compression.js.map +1 -0
  27. package/dist/es5/lib/lzo-compression.js +116 -0
  28. package/dist/es5/lib/lzo-compression.js.map +1 -0
  29. package/dist/es5/lib/no-compression.js +87 -0
  30. package/dist/es5/lib/no-compression.js.map +1 -0
  31. package/dist/es5/lib/snappy-compression.js +48 -0
  32. package/dist/es5/lib/snappy-compression.js.map +1 -0
  33. package/dist/es5/lib/zstd-compression.js +90 -0
  34. package/dist/es5/lib/zstd-compression.js.map +1 -0
  35. package/dist/es5/types.js +2 -0
  36. package/dist/es5/types.js.map +1 -0
  37. package/dist/es5/workers/worker.js +91 -0
  38. package/dist/es5/workers/worker.js.map +1 -0
  39. package/dist/esm/brotli/decode.js +1771 -0
  40. package/dist/esm/brotli/decode.js.map +1 -0
  41. package/dist/esm/bundle.js +4 -0
  42. package/dist/esm/bundle.js.map +1 -0
  43. package/dist/esm/compression-worker.js +14 -0
  44. package/dist/esm/compression-worker.js.map +1 -0
  45. package/dist/esm/index.js +11 -0
  46. package/dist/esm/index.js.map +1 -0
  47. package/dist/esm/lib/brotli-compression.js +84 -0
  48. package/dist/esm/lib/brotli-compression.js.map +1 -0
  49. package/dist/esm/lib/compression.js +47 -0
  50. package/dist/esm/lib/compression.js.map +1 -0
  51. package/dist/esm/lib/deflate-compression.js +118 -0
  52. package/dist/esm/lib/deflate-compression.js.map +1 -0
  53. package/dist/esm/lib/gzip-compression.js +18 -0
  54. package/dist/esm/lib/gzip-compression.js.map +1 -0
  55. package/dist/esm/lib/lz4-compression.js +88 -0
  56. package/dist/esm/lib/lz4-compression.js.map +1 -0
  57. package/dist/esm/lib/lzo-compression.js +36 -0
  58. package/dist/esm/lib/lzo-compression.js.map +1 -0
  59. package/dist/esm/lib/no-compression.js +26 -0
  60. package/dist/esm/lib/no-compression.js.map +1 -0
  61. package/dist/esm/lib/snappy-compression.js +21 -0
  62. package/dist/esm/lib/snappy-compression.js.map +1 -0
  63. package/dist/esm/lib/zstd-compression.js +36 -0
  64. package/dist/esm/lib/zstd-compression.js.map +1 -0
  65. package/dist/esm/types.js +2 -0
  66. package/dist/esm/types.js.map +1 -0
  67. package/dist/esm/workers/worker.js +60 -0
  68. package/dist/esm/workers/worker.js.map +1 -0
  69. package/dist/index.js +24 -11
  70. package/dist/lib/brotli-compression.d.ts +1 -1
  71. package/dist/lib/brotli-compression.d.ts.map +1 -1
  72. package/dist/lib/brotli-compression.js +82 -98
  73. package/dist/lib/compression.d.ts +1 -1
  74. package/dist/lib/compression.d.ts.map +1 -1
  75. package/dist/lib/compression.js +56 -61
  76. package/dist/lib/deflate-compression.d.ts +1 -1
  77. package/dist/lib/deflate-compression.d.ts.map +1 -1
  78. package/dist/lib/deflate-compression.js +128 -150
  79. package/dist/lib/gzip-compression.d.ts +1 -1
  80. package/dist/lib/gzip-compression.d.ts.map +1 -1
  81. package/dist/lib/gzip-compression.js +16 -20
  82. package/dist/lib/lz4-compression.js +132 -108
  83. package/dist/lib/lzo-compression.js +54 -44
  84. package/dist/lib/no-compression.js +29 -36
  85. package/dist/lib/snappy-compression.js +26 -29
  86. package/dist/lib/zstd-compression.js +42 -43
  87. package/dist/types.js +2 -2
  88. package/dist/workers/worker.js +70 -61
  89. package/package.json +7 -9
  90. package/src/lib/brotli-compression.ts +3 -3
  91. package/src/lib/deflate-compression.ts +5 -5
  92. package/dist/brotli/decode.js.map +0 -1
  93. package/dist/bundle.js.map +0 -1
  94. package/dist/index.js.map +0 -1
  95. package/dist/lib/brotli-compression.js.map +0 -1
  96. package/dist/lib/compression.js.map +0 -1
  97. package/dist/lib/deflate-compression.js.map +0 -1
  98. package/dist/lib/gzip-compression.js.map +0 -1
  99. package/dist/lib/lz4-compression.js.map +0 -1
  100. package/dist/lib/lzo-compression.js.map +0 -1
  101. package/dist/lib/no-compression.js.map +0 -1
  102. package/dist/lib/snappy-compression.js.map +0 -1
  103. package/dist/lib/zstd-compression.js.map +0 -1
  104. package/dist/types.js.map +0 -1
  105. package/dist/workers/worker.js.map +0 -1
@@ -1,157 +1,135 @@
1
- import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
2
- import { Compression } from './compression';
3
- import { isBrowser, toArrayBuffer } from '@loaders.gl/loader-utils';
4
- import pako from 'pako';
5
- import zlib from 'zlib';
6
- import { promisify } from '@loaders.gl/loader-utils';
7
- export class DeflateCompression extends Compression {
8
- constructor(options = {}) {
9
- super(options);
10
-
11
- _defineProperty(this, "name", 'deflate');
12
-
13
- _defineProperty(this, "extensions", []);
14
-
15
- _defineProperty(this, "contentEncodings", ['deflate']);
16
-
17
- _defineProperty(this, "isSupported", true);
18
-
19
- _defineProperty(this, "options", void 0);
20
-
21
- _defineProperty(this, "_chunks", []);
22
-
23
- this.options = options;
24
- }
25
-
26
- async compress(input) {
27
- var _this$options$deflate;
28
-
29
- if (!isBrowser && (_this$options$deflate = this.options.deflate) !== null && _this$options$deflate !== void 0 && _this$options$deflate.useZlib) {
30
- var _this$options$deflate2;
31
-
32
- const buffer = (_this$options$deflate2 = this.options.deflate) !== null && _this$options$deflate2 !== void 0 && _this$options$deflate2.gzip ? await promisify(zlib.gzip)(input) : await promisify(zlib.deflate)(input);
33
- return toArrayBuffer(buffer);
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.DeflateCompression = void 0;
7
+ const compression_1 = require("./compression");
8
+ const loader_utils_1 = require("@loaders.gl/loader-utils");
9
+ const pako_1 = __importDefault(require("pako")); // https://bundlephobia.com/package/pako
10
+ const zlib_1 = __importDefault(require("zlib"));
11
+ const loader_utils_2 = require("@loaders.gl/loader-utils");
12
+ /**
13
+ * DEFLATE compression / decompression
14
+ */
15
+ class DeflateCompression extends compression_1.Compression {
16
+ constructor(options = {}) {
17
+ super(options);
18
+ this.name = 'deflate';
19
+ this.extensions = [];
20
+ this.contentEncodings = ['deflate'];
21
+ this.isSupported = true;
22
+ this._chunks = [];
23
+ this.options = options;
34
24
  }
35
-
36
- return this.compressSync(input);
37
- }
38
-
39
- async decompress(input) {
40
- var _this$options$deflate3;
41
-
42
- if (!isBrowser && (_this$options$deflate3 = this.options.deflate) !== null && _this$options$deflate3 !== void 0 && _this$options$deflate3.useZlib) {
43
- var _this$options$deflate4;
44
-
45
- const buffer = (_this$options$deflate4 = this.options.deflate) !== null && _this$options$deflate4 !== void 0 && _this$options$deflate4.gzip ? await promisify(zlib.gunzip)(input) : await promisify(zlib.inflate)(input);
46
- return toArrayBuffer(buffer);
25
+ async compress(input) {
26
+ // On Node.js we can use built-in zlib
27
+ if (!loader_utils_1.isBrowser && this.options.deflate?.useZlib) {
28
+ const buffer = this.options.deflate?.gzip
29
+ ? await (0, loader_utils_2.promisify1)(zlib_1.default.gzip)(input)
30
+ : await (0, loader_utils_2.promisify1)(zlib_1.default.deflate)(input);
31
+ return (0, loader_utils_1.toArrayBuffer)(buffer);
32
+ }
33
+ return this.compressSync(input);
47
34
  }
48
-
49
- return this.decompressSync(input);
50
- }
51
-
52
- compressSync(input) {
53
- var _this$options$deflate5, _this$options;
54
-
55
- if (!isBrowser && (_this$options$deflate5 = this.options.deflate) !== null && _this$options$deflate5 !== void 0 && _this$options$deflate5.useZlib) {
56
- var _this$options$deflate6;
57
-
58
- const buffer = (_this$options$deflate6 = this.options.deflate) !== null && _this$options$deflate6 !== void 0 && _this$options$deflate6.gzip ? zlib.gzipSync(input) : zlib.deflateSync(input);
59
- return toArrayBuffer(buffer);
35
+ async decompress(input) {
36
+ // On Node.js we can use built-in zlib
37
+ if (!loader_utils_1.isBrowser && this.options.deflate?.useZlib) {
38
+ const buffer = this.options.deflate?.gzip
39
+ ? await (0, loader_utils_2.promisify1)(zlib_1.default.gunzip)(input)
40
+ : await (0, loader_utils_2.promisify1)(zlib_1.default.inflate)(input);
41
+ return (0, loader_utils_1.toArrayBuffer)(buffer);
42
+ }
43
+ return this.decompressSync(input);
60
44
  }
61
-
62
- const pakoOptions = ((_this$options = this.options) === null || _this$options === void 0 ? void 0 : _this$options.deflate) || {};
63
- const inputArray = new Uint8Array(input);
64
- return pako.deflate(inputArray, pakoOptions).buffer;
65
- }
66
-
67
- decompressSync(input) {
68
- var _this$options$deflate7, _this$options2;
69
-
70
- if (!isBrowser && (_this$options$deflate7 = this.options.deflate) !== null && _this$options$deflate7 !== void 0 && _this$options$deflate7.useZlib) {
71
- var _this$options$deflate8;
72
-
73
- const buffer = (_this$options$deflate8 = this.options.deflate) !== null && _this$options$deflate8 !== void 0 && _this$options$deflate8.gzip ? zlib.gunzipSync(input) : zlib.inflateSync(input);
74
- return toArrayBuffer(buffer);
45
+ compressSync(input) {
46
+ // On Node.js we can use built-in zlib
47
+ if (!loader_utils_1.isBrowser && this.options.deflate?.useZlib) {
48
+ const buffer = this.options.deflate?.gzip ? zlib_1.default.gzipSync(input) : zlib_1.default.deflateSync(input);
49
+ return (0, loader_utils_1.toArrayBuffer)(buffer);
50
+ }
51
+ const pakoOptions = this.options?.deflate || {};
52
+ const inputArray = new Uint8Array(input);
53
+ return pako_1.default.deflate(inputArray, pakoOptions).buffer;
75
54
  }
76
-
77
- const pakoOptions = ((_this$options2 = this.options) === null || _this$options2 === void 0 ? void 0 : _this$options2.deflate) || {};
78
- const inputArray = new Uint8Array(input);
79
- return pako.inflate(inputArray, pakoOptions).buffer;
80
- }
81
-
82
- async *compressBatches(asyncIterator) {
83
- var _this$options3;
84
-
85
- const pakoOptions = ((_this$options3 = this.options) === null || _this$options3 === void 0 ? void 0 : _this$options3.deflate) || {};
86
- const pakoProcessor = new pako.Deflate(pakoOptions);
87
- yield* this.transformBatches(pakoProcessor, asyncIterator);
88
- }
89
-
90
- async *decompressBatches(asyncIterator) {
91
- var _this$options4;
92
-
93
- const pakoOptions = ((_this$options4 = this.options) === null || _this$options4 === void 0 ? void 0 : _this$options4.deflate) || {};
94
- const pakoProcessor = new pako.Inflate(pakoOptions);
95
- yield* this.transformBatches(pakoProcessor, asyncIterator);
96
- }
97
-
98
- async *transformBatches(pakoProcessor, asyncIterator) {
99
- pakoProcessor.onData = this._onData.bind(this);
100
- pakoProcessor.onEnd = this._onEnd.bind(this);
101
-
102
- for await (const chunk of asyncIterator) {
103
- const uint8Array = new Uint8Array(chunk);
104
- const ok = pakoProcessor.push(uint8Array, false);
105
-
106
- if (!ok) {
107
- throw new Error("".concat(this._getError(), "write"));
108
- }
109
-
110
- const chunks = this._getChunks();
111
-
112
- yield* chunks;
55
+ decompressSync(input) {
56
+ // On Node.js we can use built-in zlib
57
+ if (!loader_utils_1.isBrowser && this.options.deflate?.useZlib) {
58
+ const buffer = this.options.deflate?.gzip ? zlib_1.default.gunzipSync(input) : zlib_1.default.inflateSync(input);
59
+ return (0, loader_utils_1.toArrayBuffer)(buffer);
60
+ }
61
+ const pakoOptions = this.options?.deflate || {};
62
+ const inputArray = new Uint8Array(input);
63
+ return pako_1.default.inflate(inputArray, pakoOptions).buffer;
113
64
  }
114
-
115
- const emptyChunk = new Uint8Array(0);
116
- const ok = pakoProcessor.push(emptyChunk, true);
117
-
118
- if (!ok) {}
119
-
120
- const chunks = this._getChunks();
121
-
122
- yield* chunks;
123
- }
124
-
125
- _onData(chunk) {
126
- this._chunks.push(chunk);
127
- }
128
-
129
- _onEnd(status) {
130
- if (status !== 0) {
131
- throw new Error(this._getError(status) + this._chunks.length);
65
+ async *compressBatches(asyncIterator) {
66
+ const pakoOptions = this.options?.deflate || {};
67
+ const pakoProcessor = new pako_1.default.Deflate(pakoOptions);
68
+ yield* this.transformBatches(pakoProcessor, asyncIterator);
69
+ }
70
+ async *decompressBatches(asyncIterator) {
71
+ const pakoOptions = this.options?.deflate || {};
72
+ const pakoProcessor = new pako_1.default.Inflate(pakoOptions);
73
+ yield* this.transformBatches(pakoProcessor, asyncIterator);
74
+ }
75
+ async *transformBatches(pakoProcessor, asyncIterator) {
76
+ pakoProcessor.onData = this._onData.bind(this);
77
+ pakoProcessor.onEnd = this._onEnd.bind(this);
78
+ for await (const chunk of asyncIterator) {
79
+ const uint8Array = new Uint8Array(chunk);
80
+ const ok = pakoProcessor.push(uint8Array, false); // false -> not last chunk
81
+ if (!ok) {
82
+ throw new Error(`${this._getError()}write`);
83
+ }
84
+ const chunks = this._getChunks();
85
+ yield* chunks;
86
+ }
87
+ // End
88
+ const emptyChunk = new Uint8Array(0);
89
+ const ok = pakoProcessor.push(emptyChunk, true); // true -> last chunk
90
+ if (!ok) {
91
+ // For some reason we get error but it still works???
92
+ // throw new Error(this._getError() + 'end');
93
+ }
94
+ const chunks = this._getChunks();
95
+ yield* chunks;
96
+ }
97
+ _onData(chunk) {
98
+ this._chunks.push(chunk);
99
+ }
100
+ _onEnd(status) {
101
+ if (status !== 0) {
102
+ throw new Error(this._getError(status) + this._chunks.length);
103
+ }
104
+ }
105
+ _getChunks() {
106
+ const chunks = this._chunks;
107
+ this._chunks = [];
108
+ return chunks;
109
+ }
110
+ // TODO - For some reason we don't get the error message from pako in _onEnd?
111
+ _getError(code = 0) {
112
+ const MESSAGES = {
113
+ /* Z_NEED_DICT 2 */
114
+ 2: 'need dictionary',
115
+ /* Z_STREAM_END 1 */
116
+ 1: 'stream end',
117
+ /* Z_OK 0 */
118
+ 0: '',
119
+ /* Z_ERRNO (-1) */
120
+ '-1': 'file error',
121
+ /* Z_STREAM_ERROR (-2) */
122
+ '-2': 'stream error',
123
+ /* Z_DATA_ERROR (-3) */
124
+ '-3': 'data error',
125
+ /* Z_MEM_ERROR (-4) */
126
+ '-4': 'insufficient memory',
127
+ /* Z_BUF_ERROR (-5) */
128
+ '-5': 'buffer error',
129
+ /* Z_VERSION_ERROR (-6) */
130
+ '-6': 'incompatible version'
131
+ };
132
+ return `${this.name}: ${MESSAGES[code]}`;
132
133
  }
133
- }
134
-
135
- _getChunks() {
136
- const chunks = this._chunks;
137
- this._chunks = [];
138
- return chunks;
139
- }
140
-
141
- _getError(code = 0) {
142
- const MESSAGES = {
143
- 2: 'need dictionary',
144
- 1: 'stream end',
145
- 0: '',
146
- '-1': 'file error',
147
- '-2': 'stream error',
148
- '-3': 'data error',
149
- '-4': 'insufficient memory',
150
- '-5': 'buffer error',
151
- '-6': 'incompatible version'
152
- };
153
- return "".concat(this.name, ": ").concat(MESSAGES[code]);
154
- }
155
-
156
134
  }
157
- //# sourceMappingURL=deflate-compression.js.map
135
+ exports.DeflateCompression = DeflateCompression;
@@ -1,7 +1,7 @@
1
1
  import type { CompressionOptions } from './compression';
2
2
  import { DeflateCompression } from './deflate-compression';
3
3
  import pako from 'pako';
4
- export declare type GZipCompressionOptions = CompressionOptions & {
4
+ export type GZipCompressionOptions = CompressionOptions & {
5
5
  gzip?: pako.InflateOptions & pako.DeflateOptions;
6
6
  };
7
7
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"gzip-compression.d.ts","sourceRoot":"","sources":["../../src/lib/gzip-compression.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAC,kBAAkB,EAAC,MAAM,eAAe,CAAC;AACtD,OAAO,EAAC,kBAAkB,EAAC,MAAM,uBAAuB,CAAC;AACzD,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,oBAAY,sBAAsB,GAAG,kBAAkB,GAAG;IACxD,IAAI,CAAC,EAAE,IAAI,CAAC,cAAc,GAAG,IAAI,CAAC,cAAc,CAAC;CAClD,CAAC;AAEF;;GAEG;AACH,qBAAa,eAAgB,SAAQ,kBAAkB;IACrD,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAU;IAC/B,QAAQ,CAAC,UAAU,WAAkB;IACrC,QAAQ,CAAC,gBAAgB,WAAsB;IAC/C,QAAQ,CAAC,WAAW,QAAQ;gBAEhB,OAAO,CAAC,EAAE,sBAAsB;CAG7C"}
1
+ {"version":3,"file":"gzip-compression.d.ts","sourceRoot":"","sources":["../../src/lib/gzip-compression.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAC,kBAAkB,EAAC,MAAM,eAAe,CAAC;AACtD,OAAO,EAAC,kBAAkB,EAAC,MAAM,uBAAuB,CAAC;AACzD,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,MAAM,MAAM,sBAAsB,GAAG,kBAAkB,GAAG;IACxD,IAAI,CAAC,EAAE,IAAI,CAAC,cAAc,GAAG,IAAI,CAAC,cAAc,CAAC;CAClD,CAAC;AAEF;;GAEG;AACH,qBAAa,eAAgB,SAAQ,kBAAkB;IACrD,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAU;IAC/B,QAAQ,CAAC,UAAU,WAAkB;IACrC,QAAQ,CAAC,gBAAgB,WAAsB;IAC/C,QAAQ,CAAC,WAAW,QAAQ;gBAEhB,OAAO,CAAC,EAAE,sBAAsB;CAG7C"}
@@ -1,21 +1,17 @@
1
- import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
2
- import { DeflateCompression } from './deflate-compression';
3
- export class GZipCompression extends DeflateCompression {
4
- constructor(options) {
5
- super({ ...options,
6
- deflate: { ...(options === null || options === void 0 ? void 0 : options.gzip),
7
- gzip: true
8
- }
9
- });
10
-
11
- _defineProperty(this, "name", 'gzip');
12
-
13
- _defineProperty(this, "extensions", ['gz', 'gzip']);
14
-
15
- _defineProperty(this, "contentEncodings", ['gzip', 'x-gzip']);
16
-
17
- _defineProperty(this, "isSupported", true);
18
- }
19
-
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GZipCompression = void 0;
4
+ const deflate_compression_1 = require("./deflate-compression");
5
+ /**
6
+ * GZIP compression / decompression
7
+ */
8
+ class GZipCompression extends deflate_compression_1.DeflateCompression {
9
+ constructor(options) {
10
+ super({ ...options, deflate: { ...options?.gzip, gzip: true } });
11
+ this.name = 'gzip';
12
+ this.extensions = ['gz', 'gzip'];
13
+ this.contentEncodings = ['gzip', 'x-gzip'];
14
+ this.isSupported = true;
15
+ }
20
16
  }
21
- //# sourceMappingURL=gzip-compression.js.map
17
+ exports.GZipCompression = GZipCompression;
@@ -1,117 +1,141 @@
1
- import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
2
- import { toArrayBuffer } from '@loaders.gl/loader-utils';
3
- import { Compression } from './compression';
1
+ "use strict";
2
+ // Copyright (c) 2012 Pierre Curto
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.LZ4Compression = void 0;
5
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ // of this software and associated documentation files (the "Software"), to deal
7
+ // in the Software without restriction, including without limitation the rights
8
+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ // copies of the Software, and to permit persons to whom the Software is
10
+ // furnished to do so, subject to the following conditions:
11
+ // The above copyright notice and this permission notice shall be included in
12
+ // all copies or substantial portions of the Software.
13
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ // THE SOFTWARE.
20
+ /* eslint-disable complexity */
21
+ /* eslint-disable max-statements */
22
+ // LZ4
23
+ const loader_utils_1 = require("@loaders.gl/loader-utils");
24
+ const compression_1 = require("./compression");
25
+ // import lz4js from 'lz4js'; // https://bundlephobia.com/package/lz4
4
26
  const LZ4_MAGIC_NUMBER = 0x184d2204;
5
27
  let lz4js;
6
- export class LZ4Compression extends Compression {
7
- constructor(options) {
8
- var _this$options, _this$options$modules;
9
-
10
- super(options);
11
-
12
- _defineProperty(this, "name", 'lz4');
13
-
14
- _defineProperty(this, "extensions", ['lz4']);
15
-
16
- _defineProperty(this, "contentEncodings", ['x-lz4']);
17
-
18
- _defineProperty(this, "isSupported", true);
19
-
20
- _defineProperty(this, "options", void 0);
21
-
22
- this.options = options;
23
- lz4js = lz4js || ((_this$options = this.options) === null || _this$options === void 0 ? void 0 : (_this$options$modules = _this$options.modules) === null || _this$options$modules === void 0 ? void 0 : _this$options$modules.lz4js);
24
-
25
- if (!lz4js) {
26
- throw new Error(this.name);
28
+ /**
29
+ * LZ4 compression / decompression
30
+ */
31
+ class LZ4Compression extends compression_1.Compression {
32
+ constructor(options) {
33
+ super(options);
34
+ this.name = 'lz4';
35
+ this.extensions = ['lz4'];
36
+ this.contentEncodings = ['x-lz4'];
37
+ this.isSupported = true;
38
+ this.options = options;
39
+ lz4js = lz4js || this.options?.modules?.lz4js;
40
+ if (!lz4js) {
41
+ throw new Error(this.name);
42
+ }
27
43
  }
28
- }
29
-
30
- compressSync(input) {
31
- const inputArray = new Uint8Array(input);
32
- return lz4js.compress(inputArray).buffer;
33
- }
34
-
35
- decompressSync(data, maxSize) {
36
- try {
37
- const isMagicNumberExists = this.checkMagicNumber(data);
38
- const inputArray = new Uint8Array(data);
39
-
40
- if (isMagicNumberExists) {
41
- return lz4js.decompress(inputArray, maxSize).buffer;
42
- }
43
-
44
- if (!maxSize) {
45
- const error = new Error('Need to provide maxSize');
46
- throw this.improveError(error);
47
- }
48
-
49
- let uncompressed = new Uint8Array(maxSize);
50
- const uncompressedSize = this.decodeBlock(inputArray, uncompressed);
51
- uncompressed = uncompressed.slice(0, uncompressedSize);
52
- return toArrayBuffer(uncompressed);
53
- } catch (error) {
54
- throw this.improveError(error);
44
+ compressSync(input) {
45
+ const inputArray = new Uint8Array(input);
46
+ return lz4js.compress(inputArray).buffer;
55
47
  }
56
- }
57
-
58
- decodeBlock(data, output, startIndex, endIndex) {
59
- startIndex = startIndex || 0;
60
- endIndex = endIndex || data.length - startIndex;
61
- let uncompressedSize = 0;
62
-
63
- for (let index = startIndex; index < endIndex;) {
64
- const token = data[index++];
65
- let literalsLength = token >> 4;
66
-
67
- if (literalsLength > 0) {
68
- let length = literalsLength + 240;
69
-
70
- while (length === 255) {
71
- length = data[index++];
72
- literalsLength += length;
48
+ /**
49
+ * Decompresses an ArrayBuffer containing an Lz4 frame. maxSize is optional; if not
50
+ * provided, a maximum size will be determined by examining the data. The
51
+ * returned ArrayBuffer will always be perfectly sized.
52
+ * If data provided without magic number we will parse it as block
53
+ */
54
+ decompressSync(data, maxSize) {
55
+ try {
56
+ const isMagicNumberExists = this.checkMagicNumber(data);
57
+ const inputArray = new Uint8Array(data);
58
+ if (isMagicNumberExists) {
59
+ return lz4js.decompress(inputArray, maxSize).buffer;
60
+ }
61
+ if (!maxSize) {
62
+ const error = new Error('Need to provide maxSize');
63
+ throw this.improveError(error);
64
+ }
65
+ let uncompressed = new Uint8Array(maxSize);
66
+ const uncompressedSize = this.decodeBlock(inputArray, uncompressed);
67
+ uncompressed = uncompressed.slice(0, uncompressedSize);
68
+ return (0, loader_utils_1.toArrayBuffer)(uncompressed);
73
69
  }
74
-
75
- const end = index + literalsLength;
76
-
77
- while (index < end) {
78
- output[uncompressedSize++] = data[index++];
70
+ catch (error) {
71
+ throw this.improveError(error);
79
72
  }
80
-
81
- if (index === endIndex) {
82
- return uncompressedSize;
73
+ }
74
+ /**
75
+ * Decode lz4 file as block
76
+ * Solution taken from here
77
+ * https://github.com/pierrec/node-lz4/blob/0dac687262403fd34f905b963da7220692f2a4a1/lib/binding.js#L25
78
+ * @param input
79
+ * @param output
80
+ * @param startIndex
81
+ * @param endIndex
82
+ */
83
+ decodeBlock(data, output, startIndex, endIndex) {
84
+ startIndex = startIndex || 0;
85
+ endIndex = endIndex || data.length - startIndex;
86
+ let uncompressedSize = 0;
87
+ // Process each sequence in the incoming data
88
+ for (let index = startIndex; index < endIndex;) {
89
+ const token = data[index++];
90
+ // Literals
91
+ let literalsLength = token >> 4;
92
+ if (literalsLength > 0) {
93
+ // length of literals
94
+ let length = literalsLength + 240;
95
+ while (length === 255) {
96
+ length = data[index++];
97
+ literalsLength += length;
98
+ }
99
+ // Copy the literals
100
+ const end = index + literalsLength;
101
+ while (index < end) {
102
+ output[uncompressedSize++] = data[index++];
103
+ }
104
+ // End of buffer?
105
+ if (index === endIndex) {
106
+ return uncompressedSize;
107
+ }
108
+ }
109
+ // Match copy
110
+ // 2 bytes offset (little endian)
111
+ const offset = data[index++] | (data[index++] << 8);
112
+ // 0 is an invalid offset value
113
+ if (offset === 0 || offset > uncompressedSize) {
114
+ return -(index - 2);
115
+ }
116
+ // length of match copy
117
+ let matchLength = token & 0xf;
118
+ let length = matchLength + 240;
119
+ while (length === 255) {
120
+ length = data[index++];
121
+ matchLength += length;
122
+ }
123
+ // Copy the match
124
+ let pos = uncompressedSize - offset; // position of the match copy in the current output
125
+ const end = uncompressedSize + matchLength + 4; // minmatch = 4
126
+ while (uncompressedSize < end) {
127
+ output[uncompressedSize++] = output[pos++];
128
+ }
83
129
  }
84
- }
85
-
86
- const offset = data[index++] | data[index++] << 8;
87
-
88
- if (offset === 0 || offset > uncompressedSize) {
89
- return -(index - 2);
90
- }
91
-
92
- let matchLength = token & 0xf;
93
- let length = matchLength + 240;
94
-
95
- while (length === 255) {
96
- length = data[index++];
97
- matchLength += length;
98
- }
99
-
100
- let pos = uncompressedSize - offset;
101
- const end = uncompressedSize + matchLength + 4;
102
-
103
- while (uncompressedSize < end) {
104
- output[uncompressedSize++] = output[pos++];
105
- }
130
+ return uncompressedSize;
131
+ }
132
+ /**
133
+ * Compare file magic with lz4 magic number
134
+ * @param input
135
+ */
136
+ checkMagicNumber(data) {
137
+ const magic = new Uint32Array(data.slice(0, 4));
138
+ return magic[0] === LZ4_MAGIC_NUMBER;
106
139
  }
107
-
108
- return uncompressedSize;
109
- }
110
-
111
- checkMagicNumber(data) {
112
- const magic = new Uint32Array(data.slice(0, 4));
113
- return magic[0] === LZ4_MAGIC_NUMBER;
114
- }
115
-
116
140
  }
117
- //# sourceMappingURL=lz4-compression.js.map
141
+ exports.LZ4Compression = LZ4Compression;