@loaders.gl/netcdf 4.2.0-alpha.4 → 4.2.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.
@@ -1,269 +1,448 @@
1
1
  const DEFAULT_BYTE_LENGTH = 1024 * 8;
2
2
  export class IOBuffer {
3
- constructor() {
4
- let data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : DEFAULT_BYTE_LENGTH;
5
- let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
6
- this.buffer = void 0;
7
- this.byteLength = void 0;
8
- this.byteOffset = void 0;
9
- this.length = void 0;
10
- this.offset = void 0;
11
- this.lastWrittenByte = void 0;
12
- this.littleEndian = void 0;
13
- this._data = void 0;
14
- this._mark = void 0;
15
- this._marks = void 0;
16
- this.textDecoder = new TextDecoder();
17
- this.textEncoder = new TextEncoder();
18
- let dataIsGiven = false;
19
- if (typeof data === 'number') {
20
- data = new ArrayBuffer(data);
21
- } else {
22
- dataIsGiven = true;
23
- this.lastWrittenByte = data.byteLength;
24
- }
25
- const offset = options.offset ? options.offset >>> 0 : 0;
26
- const byteLength = data.byteLength - offset;
27
- let dvOffset = offset;
28
- if (ArrayBuffer.isView(data) || data instanceof IOBuffer) {
29
- if (data.byteLength !== data.buffer.byteLength) {
30
- dvOffset = data.byteOffset + offset;
31
- }
32
- data = data.buffer;
33
- }
34
- if (dataIsGiven) {
35
- this.lastWrittenByte = byteLength;
36
- } else {
37
- this.lastWrittenByte = 0;
38
- }
39
- this.buffer = data;
40
- this.length = byteLength;
41
- this.byteLength = byteLength;
42
- this.byteOffset = dvOffset;
43
- this.offset = 0;
44
- this.littleEndian = true;
45
- this._data = new DataView(this.buffer, dvOffset, byteLength);
46
- this._mark = 0;
47
- this._marks = [];
48
- }
49
- available() {
50
- let byteLength = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
51
- return this.offset + byteLength <= this.length;
52
- }
53
- isLittleEndian() {
54
- return this.littleEndian;
55
- }
56
- setLittleEndian() {
57
- this.littleEndian = true;
58
- return this;
59
- }
60
- isBigEndian() {
61
- return !this.littleEndian;
62
- }
63
- setBigEndian() {
64
- this.littleEndian = false;
65
- return this;
66
- }
67
- skip() {
68
- let n = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
69
- this.offset += n;
70
- return this;
71
- }
72
- seek(offset) {
73
- this.offset = offset;
74
- return this;
75
- }
76
- mark() {
77
- this._mark = this.offset;
78
- return this;
79
- }
80
- reset() {
81
- this.offset = this._mark;
82
- return this;
83
- }
84
- pushMark() {
85
- this._marks.push(this.offset);
86
- return this;
87
- }
88
- popMark() {
89
- const offset = this._marks.pop();
90
- if (offset === undefined) {
91
- throw new Error('Mark stack empty');
92
- }
93
- this.seek(offset);
94
- return this;
95
- }
96
- rewind() {
97
- this.offset = 0;
98
- return this;
99
- }
100
- ensureAvailable() {
101
- let byteLength = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
102
- if (!this.available(byteLength)) {
103
- const lengthNeeded = this.offset + byteLength;
104
- const newLength = lengthNeeded * 2;
105
- const newArray = new Uint8Array(newLength);
106
- newArray.set(new Uint8Array(this.buffer));
107
- this.buffer = newArray.buffer;
108
- this.length = this.byteLength = newLength;
109
- this._data = new DataView(this.buffer);
110
- }
111
- return this;
112
- }
113
- readBoolean() {
114
- return this.readUint8() !== 0;
115
- }
116
- readInt8() {
117
- return this._data.getInt8(this.offset++);
118
- }
119
- readUint8() {
120
- return this._data.getUint8(this.offset++);
121
- }
122
- readByte() {
123
- return this.readUint8();
124
- }
125
- readBytes() {
126
- let n = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
127
- const bytes = new Uint8Array(n);
128
- for (let i = 0; i < n; i++) {
129
- bytes[i] = this.readByte();
130
- }
131
- return bytes;
132
- }
133
- readInt16() {
134
- const value = this._data.getInt16(this.offset, this.littleEndian);
135
- this.offset += 2;
136
- return value;
137
- }
138
- readUint16() {
139
- const value = this._data.getUint16(this.offset, this.littleEndian);
140
- this.offset += 2;
141
- return value;
142
- }
143
- readInt32() {
144
- const value = this._data.getInt32(this.offset, this.littleEndian);
145
- this.offset += 4;
146
- return value;
147
- }
148
- readUint32() {
149
- const value = this._data.getUint32(this.offset, this.littleEndian);
150
- this.offset += 4;
151
- return value;
152
- }
153
- readFloat32() {
154
- const value = this._data.getFloat32(this.offset, this.littleEndian);
155
- this.offset += 4;
156
- return value;
157
- }
158
- readFloat64() {
159
- const value = this._data.getFloat64(this.offset, this.littleEndian);
160
- this.offset += 8;
161
- return value;
162
- }
163
- readChar() {
164
- return String.fromCharCode(this.readInt8());
165
- }
166
- readChars() {
167
- let n = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
168
- let result = '';
169
- for (let i = 0; i < n; i++) {
170
- result += this.readChar();
171
- }
172
- return result;
173
- }
174
- readUtf8() {
175
- let n = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
176
- return this.textDecoder.decode(this.readBytes(n));
177
- }
178
- writeBoolean(value) {
179
- this.writeUint8(value ? 0xff : 0x00);
180
- return this;
181
- }
182
- writeInt8(value) {
183
- this.ensureAvailable(1);
184
- this._data.setInt8(this.offset++, value);
185
- this._updateLastWrittenByte();
186
- return this;
187
- }
188
- writeUint8(value) {
189
- this.ensureAvailable(1);
190
- this._data.setUint8(this.offset++, value);
191
- this._updateLastWrittenByte();
192
- return this;
193
- }
194
- writeByte(value) {
195
- return this.writeUint8(value);
196
- }
197
- writeBytes(bytes) {
198
- this.ensureAvailable(bytes.length);
199
- for (let i = 0; i < bytes.length; i++) {
200
- this._data.setUint8(this.offset++, bytes[i]);
201
- }
202
- this._updateLastWrittenByte();
203
- return this;
204
- }
205
- writeInt16(value) {
206
- this.ensureAvailable(2);
207
- this._data.setInt16(this.offset, value, this.littleEndian);
208
- this.offset += 2;
209
- this._updateLastWrittenByte();
210
- return this;
211
- }
212
- writeUint16(value) {
213
- this.ensureAvailable(2);
214
- this._data.setUint16(this.offset, value, this.littleEndian);
215
- this.offset += 2;
216
- this._updateLastWrittenByte();
217
- return this;
218
- }
219
- writeInt32(value) {
220
- this.ensureAvailable(4);
221
- this._data.setInt32(this.offset, value, this.littleEndian);
222
- this.offset += 4;
223
- this._updateLastWrittenByte();
224
- return this;
225
- }
226
- writeUint32(value) {
227
- this.ensureAvailable(4);
228
- this._data.setUint32(this.offset, value, this.littleEndian);
229
- this.offset += 4;
230
- this._updateLastWrittenByte();
231
- return this;
232
- }
233
- writeFloat32(value) {
234
- this.ensureAvailable(4);
235
- this._data.setFloat32(this.offset, value, this.littleEndian);
236
- this.offset += 4;
237
- this._updateLastWrittenByte();
238
- return this;
239
- }
240
- writeFloat64(value) {
241
- this.ensureAvailable(8);
242
- this._data.setFloat64(this.offset, value, this.littleEndian);
243
- this.offset += 8;
244
- this._updateLastWrittenByte();
245
- return this;
246
- }
247
- writeChar(str) {
248
- return this.writeUint8(str.charCodeAt(0));
249
- }
250
- writeChars(str) {
251
- for (let i = 0; i < str.length; i++) {
252
- this.writeUint8(str.charCodeAt(i));
253
- }
254
- return this;
255
- }
256
- writeUtf8(str) {
257
- const bytes = this.textEncoder.encode(str);
258
- return this.writeBytes(bytes);
259
- }
260
- toArray() {
261
- return new Uint8Array(this.buffer, this.byteOffset, this.lastWrittenByte);
262
- }
263
- _updateLastWrittenByte() {
264
- if (this.offset > this.lastWrittenByte) {
265
- this.lastWrittenByte = this.offset;
266
- }
267
- }
3
+ /**
4
+ * Reference to the internal ArrayBuffer object.
5
+ */
6
+ buffer;
7
+ /**
8
+ * Byte length of the internal ArrayBuffer.
9
+ */
10
+ byteLength;
11
+ /**
12
+ * Byte offset of the internal ArrayBuffer.
13
+ */
14
+ byteOffset;
15
+ /**
16
+ * Byte length of the internal ArrayBuffer.
17
+ */
18
+ length;
19
+ /**
20
+ * The current offset of the buffer's pointer.
21
+ */
22
+ offset;
23
+ lastWrittenByte;
24
+ littleEndian;
25
+ _data;
26
+ _mark;
27
+ _marks;
28
+ textDecoder = new TextDecoder();
29
+ textEncoder = new TextEncoder();
30
+ /**
31
+ * @param data - The data to construct the IOBuffer with.
32
+ * If data is a number, it will be the new buffer's length<br>
33
+ * If data is `undefined`, the buffer will be initialized with a default length of 8Kb<br>
34
+ * If data is an ArrayBuffer, SharedArrayBuffer, an ArrayBufferView (Typed Array), an IOBuffer instance,
35
+ * or a Node.js Buffer, a view will be created over the underlying ArrayBuffer.
36
+ * @param options
37
+ */
38
+ constructor(data = DEFAULT_BYTE_LENGTH, options = {}) {
39
+ let dataIsGiven = false;
40
+ if (typeof data === 'number') {
41
+ data = new ArrayBuffer(data);
42
+ }
43
+ else {
44
+ dataIsGiven = true;
45
+ this.lastWrittenByte = data.byteLength;
46
+ }
47
+ const offset = options.offset ? options.offset >>> 0 : 0;
48
+ const byteLength = data.byteLength - offset;
49
+ let dvOffset = offset;
50
+ if (ArrayBuffer.isView(data) || data instanceof IOBuffer) {
51
+ if (data.byteLength !== data.buffer.byteLength) {
52
+ dvOffset = data.byteOffset + offset;
53
+ }
54
+ data = data.buffer;
55
+ }
56
+ if (dataIsGiven) {
57
+ this.lastWrittenByte = byteLength;
58
+ }
59
+ else {
60
+ this.lastWrittenByte = 0;
61
+ }
62
+ this.buffer = data;
63
+ this.length = byteLength;
64
+ this.byteLength = byteLength;
65
+ this.byteOffset = dvOffset;
66
+ this.offset = 0;
67
+ this.littleEndian = true;
68
+ this._data = new DataView(this.buffer, dvOffset, byteLength);
69
+ this._mark = 0;
70
+ this._marks = [];
71
+ }
72
+ /**
73
+ * Checks if the memory allocated to the buffer is sufficient to store more
74
+ * bytes after the offset.
75
+ * @param byteLength - The needed memory in bytes.
76
+ * @returns `true` if there is sufficient space and `false` otherwise.
77
+ */
78
+ available(byteLength = 1) {
79
+ return this.offset + byteLength <= this.length;
80
+ }
81
+ /**
82
+ * Check if little-endian mode is used for reading and writing multi-byte
83
+ * values.
84
+ * @returns `true` if little-endian mode is used, `false` otherwise.
85
+ */
86
+ isLittleEndian() {
87
+ return this.littleEndian;
88
+ }
89
+ /**
90
+ * Set little-endian mode for reading and writing multi-byte values.
91
+ */
92
+ setLittleEndian() {
93
+ this.littleEndian = true;
94
+ return this;
95
+ }
96
+ /**
97
+ * Check if big-endian mode is used for reading and writing multi-byte values.
98
+ * @returns `true` if big-endian mode is used, `false` otherwise.
99
+ */
100
+ isBigEndian() {
101
+ return !this.littleEndian;
102
+ }
103
+ /**
104
+ * Switches to big-endian mode for reading and writing multi-byte values.
105
+ */
106
+ setBigEndian() {
107
+ this.littleEndian = false;
108
+ return this;
109
+ }
110
+ /**
111
+ * Move the pointer n bytes forward.
112
+ * @param n - Number of bytes to skip.
113
+ */
114
+ skip(n = 1) {
115
+ this.offset += n;
116
+ return this;
117
+ }
118
+ /**
119
+ * Move the pointer to the given offset.
120
+ * @param offset
121
+ */
122
+ seek(offset) {
123
+ this.offset = offset;
124
+ return this;
125
+ }
126
+ /**
127
+ * Store the current pointer offset.
128
+ * @see {@link IOBuffer#reset}
129
+ */
130
+ mark() {
131
+ this._mark = this.offset;
132
+ return this;
133
+ }
134
+ /**
135
+ * Move the pointer back to the last pointer offset set by mark.
136
+ * @see {@link IOBuffer#mark}
137
+ */
138
+ reset() {
139
+ this.offset = this._mark;
140
+ return this;
141
+ }
142
+ /**
143
+ * Push the current pointer offset to the mark stack.
144
+ * @see {@link IOBuffer#popMark}
145
+ */
146
+ pushMark() {
147
+ this._marks.push(this.offset);
148
+ return this;
149
+ }
150
+ /**
151
+ * Pop the last pointer offset from the mark stack, and set the current
152
+ * pointer offset to the popped value.
153
+ * @see {@link IOBuffer#pushMark}
154
+ */
155
+ popMark() {
156
+ const offset = this._marks.pop();
157
+ if (offset === undefined) {
158
+ throw new Error('Mark stack empty');
159
+ }
160
+ this.seek(offset);
161
+ return this;
162
+ }
163
+ /**
164
+ * Move the pointer offset back to 0.
165
+ */
166
+ rewind() {
167
+ this.offset = 0;
168
+ return this;
169
+ }
170
+ /**
171
+ * Make sure the buffer has sufficient memory to write a given byteLength at
172
+ * the current pointer offset.
173
+ * If the buffer's memory is insufficient, this method will create a new
174
+ * buffer (a copy) with a length that is twice (byteLength + current offset).
175
+ * @param byteLength
176
+ */
177
+ ensureAvailable(byteLength = 1) {
178
+ if (!this.available(byteLength)) {
179
+ const lengthNeeded = this.offset + byteLength;
180
+ const newLength = lengthNeeded * 2;
181
+ const newArray = new Uint8Array(newLength);
182
+ newArray.set(new Uint8Array(this.buffer));
183
+ this.buffer = newArray.buffer;
184
+ this.length = this.byteLength = newLength;
185
+ this._data = new DataView(this.buffer);
186
+ }
187
+ return this;
188
+ }
189
+ /**
190
+ * Read a byte and return false if the byte's value is 0, or true otherwise.
191
+ * Moves pointer forward by one byte.
192
+ */
193
+ readBoolean() {
194
+ return this.readUint8() !== 0;
195
+ }
196
+ /**
197
+ * Read a signed 8-bit integer and move pointer forward by 1 byte.
198
+ */
199
+ readInt8() {
200
+ return this._data.getInt8(this.offset++);
201
+ }
202
+ /**
203
+ * Read an unsigned 8-bit integer and move pointer forward by 1 byte.
204
+ */
205
+ readUint8() {
206
+ return this._data.getUint8(this.offset++);
207
+ }
208
+ /**
209
+ * Alias for {@link IOBuffer#readUint8}.
210
+ */
211
+ readByte() {
212
+ return this.readUint8();
213
+ }
214
+ /**
215
+ * Read `n` bytes and move pointer forward by `n` bytes.
216
+ */
217
+ readBytes(n = 1) {
218
+ const bytes = new Uint8Array(n);
219
+ for (let i = 0; i < n; i++) {
220
+ bytes[i] = this.readByte();
221
+ }
222
+ return bytes;
223
+ }
224
+ /**
225
+ * Read a 16-bit signed integer and move pointer forward by 2 bytes.
226
+ */
227
+ readInt16() {
228
+ const value = this._data.getInt16(this.offset, this.littleEndian);
229
+ this.offset += 2;
230
+ return value;
231
+ }
232
+ /**
233
+ * Read a 16-bit unsigned integer and move pointer forward by 2 bytes.
234
+ */
235
+ readUint16() {
236
+ const value = this._data.getUint16(this.offset, this.littleEndian);
237
+ this.offset += 2;
238
+ return value;
239
+ }
240
+ /**
241
+ * Read a 32-bit signed integer and move pointer forward by 4 bytes.
242
+ */
243
+ readInt32() {
244
+ const value = this._data.getInt32(this.offset, this.littleEndian);
245
+ this.offset += 4;
246
+ return value;
247
+ }
248
+ /**
249
+ * Read a 32-bit unsigned integer and move pointer forward by 4 bytes.
250
+ */
251
+ readUint32() {
252
+ const value = this._data.getUint32(this.offset, this.littleEndian);
253
+ this.offset += 4;
254
+ return value;
255
+ }
256
+ /**
257
+ * Read a 32-bit floating number and move pointer forward by 4 bytes.
258
+ */
259
+ readFloat32() {
260
+ const value = this._data.getFloat32(this.offset, this.littleEndian);
261
+ this.offset += 4;
262
+ return value;
263
+ }
264
+ /**
265
+ * Read a 64-bit floating number and move pointer forward by 8 bytes.
266
+ */
267
+ readFloat64() {
268
+ const value = this._data.getFloat64(this.offset, this.littleEndian);
269
+ this.offset += 8;
270
+ return value;
271
+ }
272
+ /**
273
+ * Read a 1-byte ASCII character and move pointer forward by 1 byte.
274
+ */
275
+ readChar() {
276
+ return String.fromCharCode(this.readInt8());
277
+ }
278
+ /**
279
+ * Read `n` 1-byte ASCII characters and move pointer forward by `n` bytes.
280
+ */
281
+ readChars(n = 1) {
282
+ let result = '';
283
+ for (let i = 0; i < n; i++) {
284
+ result += this.readChar();
285
+ }
286
+ return result;
287
+ }
288
+ /**
289
+ * Read the next `n` bytes, return a UTF-8 decoded string and move pointer
290
+ * forward by `n` bytes.
291
+ */
292
+ readUtf8(n = 1) {
293
+ return this.textDecoder.decode(this.readBytes(n));
294
+ }
295
+ /**
296
+ * Write 0xff if the passed value is truthy, 0x00 otherwise and move pointer
297
+ * forward by 1 byte.
298
+ */
299
+ writeBoolean(value) {
300
+ this.writeUint8(value ? 0xff : 0x00);
301
+ return this;
302
+ }
303
+ /**
304
+ * Write `value` as an 8-bit signed integer and move pointer forward by 1 byte.
305
+ */
306
+ writeInt8(value) {
307
+ this.ensureAvailable(1);
308
+ this._data.setInt8(this.offset++, value);
309
+ this._updateLastWrittenByte();
310
+ return this;
311
+ }
312
+ /**
313
+ * Write `value` as an 8-bit unsigned integer and move pointer forward by 1
314
+ * byte.
315
+ */
316
+ writeUint8(value) {
317
+ this.ensureAvailable(1);
318
+ this._data.setUint8(this.offset++, value);
319
+ this._updateLastWrittenByte();
320
+ return this;
321
+ }
322
+ /**
323
+ * An alias for {@link IOBuffer#writeUint8}.
324
+ */
325
+ writeByte(value) {
326
+ return this.writeUint8(value);
327
+ }
328
+ /**
329
+ * Write all elements of `bytes` as uint8 values and move pointer forward by
330
+ * `bytes.length` bytes.
331
+ */
332
+ writeBytes(bytes) {
333
+ this.ensureAvailable(bytes.length);
334
+ for (let i = 0; i < bytes.length; i++) {
335
+ this._data.setUint8(this.offset++, bytes[i]);
336
+ }
337
+ this._updateLastWrittenByte();
338
+ return this;
339
+ }
340
+ /**
341
+ * Write `value` as a 16-bit signed integer and move pointer forward by 2
342
+ * bytes.
343
+ */
344
+ writeInt16(value) {
345
+ this.ensureAvailable(2);
346
+ this._data.setInt16(this.offset, value, this.littleEndian);
347
+ this.offset += 2;
348
+ this._updateLastWrittenByte();
349
+ return this;
350
+ }
351
+ /**
352
+ * Write `value` as a 16-bit unsigned integer and move pointer forward by 2
353
+ * bytes.
354
+ */
355
+ writeUint16(value) {
356
+ this.ensureAvailable(2);
357
+ this._data.setUint16(this.offset, value, this.littleEndian);
358
+ this.offset += 2;
359
+ this._updateLastWrittenByte();
360
+ return this;
361
+ }
362
+ /**
363
+ * Write `value` as a 32-bit signed integer and move pointer forward by 4
364
+ * bytes.
365
+ */
366
+ writeInt32(value) {
367
+ this.ensureAvailable(4);
368
+ this._data.setInt32(this.offset, value, this.littleEndian);
369
+ this.offset += 4;
370
+ this._updateLastWrittenByte();
371
+ return this;
372
+ }
373
+ /**
374
+ * Write `value` as a 32-bit unsigned integer and move pointer forward by 4
375
+ * bytes.
376
+ */
377
+ writeUint32(value) {
378
+ this.ensureAvailable(4);
379
+ this._data.setUint32(this.offset, value, this.littleEndian);
380
+ this.offset += 4;
381
+ this._updateLastWrittenByte();
382
+ return this;
383
+ }
384
+ /**
385
+ * Write `value` as a 32-bit floating number and move pointer forward by 4
386
+ * bytes.
387
+ */
388
+ writeFloat32(value) {
389
+ this.ensureAvailable(4);
390
+ this._data.setFloat32(this.offset, value, this.littleEndian);
391
+ this.offset += 4;
392
+ this._updateLastWrittenByte();
393
+ return this;
394
+ }
395
+ /**
396
+ * Write `value` as a 64-bit floating number and move pointer forward by 8
397
+ * bytes.
398
+ */
399
+ writeFloat64(value) {
400
+ this.ensureAvailable(8);
401
+ this._data.setFloat64(this.offset, value, this.littleEndian);
402
+ this.offset += 8;
403
+ this._updateLastWrittenByte();
404
+ return this;
405
+ }
406
+ /**
407
+ * Write the charCode of `str`'s first character as an 8-bit unsigned integer
408
+ * and move pointer forward by 1 byte.
409
+ */
410
+ writeChar(str) {
411
+ return this.writeUint8(str.charCodeAt(0));
412
+ }
413
+ /**
414
+ * Write the charCodes of all `str`'s characters as 8-bit unsigned integers
415
+ * and move pointer forward by `str.length` bytes.
416
+ */
417
+ writeChars(str) {
418
+ for (let i = 0; i < str.length; i++) {
419
+ this.writeUint8(str.charCodeAt(i));
420
+ }
421
+ return this;
422
+ }
423
+ /**
424
+ * UTF-8 encode and write `str` to the current pointer offset and move pointer
425
+ * forward according to the encoded length.
426
+ */
427
+ writeUtf8(str) {
428
+ const bytes = this.textEncoder.encode(str);
429
+ return this.writeBytes(bytes);
430
+ }
431
+ /**
432
+ * Export a Uint8Array view of the internal buffer.
433
+ * The view starts at the byte offset and its length
434
+ * is calculated to stop at the last written byte or the original length.
435
+ */
436
+ toArray() {
437
+ return new Uint8Array(this.buffer, this.byteOffset, this.lastWrittenByte);
438
+ }
439
+ /**
440
+ * Update the last written byte offset
441
+ * @private
442
+ */
443
+ _updateLastWrittenByte() {
444
+ if (this.offset > this.lastWrittenByte) {
445
+ this.lastWrittenByte = this.offset;
446
+ }
447
+ }
268
448
  }
269
- //# sourceMappingURL=iobuffer.js.map