@gjsify/buffer 0.0.4 → 0.1.1
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.
- package/README.md +25 -1
- package/lib/esm/blob.js +66 -0
- package/lib/esm/buffer.js +661 -0
- package/lib/esm/index.js +31 -4
- package/lib/types/blob.d.ts +17 -0
- package/lib/types/buffer.d.ts +89 -0
- package/lib/types/index.d.ts +37 -3
- package/package.json +14 -20
- package/src/blob.ts +77 -0
- package/src/buffer.ts +786 -0
- package/src/index.spec.ts +959 -403
- package/src/index.ts +39 -3
- package/src/test.mts +1 -1
- package/tsconfig.json +21 -9
- package/tsconfig.tsbuildinfo +1 -0
- package/lib/cjs/index.js +0 -6
- package/test.gjs.js +0 -34758
- package/test.gjs.mjs +0 -34820
- package/test.gjs.mjs.meta.json +0 -1
- package/test.node.js +0 -1226
- package/test.node.mjs +0 -439
- package/tsconfig.types.json +0 -7
- package/tsconfig.types.tsbuildinfo +0 -1
|
@@ -0,0 +1,661 @@
|
|
|
1
|
+
import { normalizeEncoding, checkEncoding, btoaPolyfill as _btoa, base64Decode } from "@gjsify/utils";
|
|
2
|
+
const textEncoder = new TextEncoder();
|
|
3
|
+
const textDecoder = new TextDecoder();
|
|
4
|
+
const hasSharedArrayBuffer = typeof SharedArrayBuffer !== "undefined";
|
|
5
|
+
function encodeString(str, encoding) {
|
|
6
|
+
switch (encoding) {
|
|
7
|
+
case "utf8":
|
|
8
|
+
return textEncoder.encode(str);
|
|
9
|
+
case "ascii": {
|
|
10
|
+
const buf = new Uint8Array(str.length);
|
|
11
|
+
for (let i = 0; i < str.length; i++) {
|
|
12
|
+
buf[i] = str.charCodeAt(i) & 127;
|
|
13
|
+
}
|
|
14
|
+
return buf;
|
|
15
|
+
}
|
|
16
|
+
case "latin1": {
|
|
17
|
+
const buf = new Uint8Array(str.length);
|
|
18
|
+
for (let i = 0; i < str.length; i++) {
|
|
19
|
+
buf[i] = str.charCodeAt(i) & 255;
|
|
20
|
+
}
|
|
21
|
+
return buf;
|
|
22
|
+
}
|
|
23
|
+
case "base64": {
|
|
24
|
+
const standard = str.replace(/-/g, "+").replace(/_/g, "/");
|
|
25
|
+
return base64Decode(standard);
|
|
26
|
+
}
|
|
27
|
+
case "base64url": {
|
|
28
|
+
const base64 = str.replace(/-/g, "+").replace(/_/g, "/");
|
|
29
|
+
return encodeString(base64, "base64");
|
|
30
|
+
}
|
|
31
|
+
case "hex": {
|
|
32
|
+
const bytes = str.length >>> 1;
|
|
33
|
+
const buf = new Uint8Array(bytes);
|
|
34
|
+
for (let i = 0; i < bytes; i++) {
|
|
35
|
+
const hi = parseInt(str[i * 2], 16);
|
|
36
|
+
const lo = parseInt(str[i * 2 + 1], 16);
|
|
37
|
+
if (Number.isNaN(hi) || Number.isNaN(lo)) break;
|
|
38
|
+
buf[i] = hi << 4 | lo;
|
|
39
|
+
}
|
|
40
|
+
return buf;
|
|
41
|
+
}
|
|
42
|
+
case "utf16le": {
|
|
43
|
+
const buf = new Uint8Array(str.length * 2);
|
|
44
|
+
for (let i = 0; i < str.length; i++) {
|
|
45
|
+
const code = str.charCodeAt(i);
|
|
46
|
+
buf[i * 2] = code & 255;
|
|
47
|
+
buf[i * 2 + 1] = code >> 8 & 255;
|
|
48
|
+
}
|
|
49
|
+
return buf;
|
|
50
|
+
}
|
|
51
|
+
default:
|
|
52
|
+
return textEncoder.encode(str);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
function decodeString(buf, encoding, start, end) {
|
|
56
|
+
const slice = start !== void 0 || end !== void 0 ? buf.subarray(start ?? 0, end ?? buf.length) : buf;
|
|
57
|
+
switch (encoding) {
|
|
58
|
+
case "utf8":
|
|
59
|
+
return textDecoder.decode(slice);
|
|
60
|
+
case "ascii": {
|
|
61
|
+
let result = "";
|
|
62
|
+
for (let i = 0; i < slice.length; i++) {
|
|
63
|
+
result += String.fromCharCode(slice[i] & 127);
|
|
64
|
+
}
|
|
65
|
+
return result;
|
|
66
|
+
}
|
|
67
|
+
case "latin1": {
|
|
68
|
+
let result = "";
|
|
69
|
+
for (let i = 0; i < slice.length; i++) {
|
|
70
|
+
result += String.fromCharCode(slice[i]);
|
|
71
|
+
}
|
|
72
|
+
return result;
|
|
73
|
+
}
|
|
74
|
+
case "base64": {
|
|
75
|
+
let binary = "";
|
|
76
|
+
for (let i = 0; i < slice.length; i++) {
|
|
77
|
+
binary += String.fromCharCode(slice[i]);
|
|
78
|
+
}
|
|
79
|
+
return _btoa(binary);
|
|
80
|
+
}
|
|
81
|
+
case "base64url": {
|
|
82
|
+
const base64 = decodeString(slice, "base64");
|
|
83
|
+
return base64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
84
|
+
}
|
|
85
|
+
case "hex": {
|
|
86
|
+
let result = "";
|
|
87
|
+
for (let i = 0; i < slice.length; i++) {
|
|
88
|
+
result += slice[i].toString(16).padStart(2, "0");
|
|
89
|
+
}
|
|
90
|
+
return result;
|
|
91
|
+
}
|
|
92
|
+
case "utf16le": {
|
|
93
|
+
let result = "";
|
|
94
|
+
for (let i = 0; i + 1 < slice.length; i += 2) {
|
|
95
|
+
result += String.fromCharCode(slice[i] | slice[i + 1] << 8);
|
|
96
|
+
}
|
|
97
|
+
return result;
|
|
98
|
+
}
|
|
99
|
+
default:
|
|
100
|
+
return textDecoder.decode(slice);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
function checkOffset(offset, ext, length) {
|
|
104
|
+
if (offset + ext > length) {
|
|
105
|
+
throw new RangeError("Attempt to access memory outside buffer bounds");
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
const INSPECT_MAX_BYTES = 50;
|
|
109
|
+
class Buffer extends Uint8Array {
|
|
110
|
+
// Marker to identify Buffer instances
|
|
111
|
+
static _isBuffer = true;
|
|
112
|
+
// ---- Static constructors ----
|
|
113
|
+
static alloc(size, fill, encoding) {
|
|
114
|
+
if (typeof size !== "number") {
|
|
115
|
+
throw new TypeError(`The "size" argument must be of type number. Received type ${typeof size}`);
|
|
116
|
+
}
|
|
117
|
+
if (size < 0) {
|
|
118
|
+
throw new RangeError(`The value "${size}" is invalid for option "size"`);
|
|
119
|
+
}
|
|
120
|
+
const buf = new Buffer(size);
|
|
121
|
+
if (fill !== void 0 && fill !== 0) {
|
|
122
|
+
buf.fill(fill, 0, size, encoding);
|
|
123
|
+
}
|
|
124
|
+
return buf;
|
|
125
|
+
}
|
|
126
|
+
static allocUnsafe(size) {
|
|
127
|
+
if (typeof size !== "number") {
|
|
128
|
+
throw new TypeError(`The "size" argument must be of type number. Received type ${typeof size}`);
|
|
129
|
+
}
|
|
130
|
+
return new Buffer(size);
|
|
131
|
+
}
|
|
132
|
+
static allocUnsafeSlow(size) {
|
|
133
|
+
return Buffer.allocUnsafe(size);
|
|
134
|
+
}
|
|
135
|
+
static from(value, encodingOrOffset, length) {
|
|
136
|
+
if (typeof value === "string") {
|
|
137
|
+
const encoding = normalizeEncoding(encodingOrOffset);
|
|
138
|
+
if (encodingOrOffset && typeof encodingOrOffset === "string") {
|
|
139
|
+
const lower = ("" + encodingOrOffset).toLowerCase().replace(/-/g, "");
|
|
140
|
+
const valid = ["utf8", "ascii", "latin1", "binary", "base64", "base64url", "hex", "ucs2", "utf16le", ""];
|
|
141
|
+
if (!valid.includes(lower)) {
|
|
142
|
+
checkEncoding(encodingOrOffset);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
const encoded = encodeString(value, encoding);
|
|
146
|
+
const buf = new Buffer(encoded.buffer, encoded.byteOffset, encoded.byteLength);
|
|
147
|
+
return buf;
|
|
148
|
+
}
|
|
149
|
+
if (ArrayBuffer.isView(value)) {
|
|
150
|
+
const buf = new Buffer(value.buffer, value.byteOffset, value.byteLength);
|
|
151
|
+
const copy = new Buffer(buf.length);
|
|
152
|
+
copy.set(buf);
|
|
153
|
+
return copy;
|
|
154
|
+
}
|
|
155
|
+
if (value instanceof ArrayBuffer) {
|
|
156
|
+
const offset = encodingOrOffset || 0;
|
|
157
|
+
const len = length !== void 0 ? length : value.byteLength - offset;
|
|
158
|
+
return new Buffer(value, offset, len);
|
|
159
|
+
}
|
|
160
|
+
if (hasSharedArrayBuffer && value instanceof SharedArrayBuffer) {
|
|
161
|
+
const offset = encodingOrOffset || 0;
|
|
162
|
+
const len = length !== void 0 ? length : value.byteLength - offset;
|
|
163
|
+
return new Buffer(new Uint8Array(value, offset, len));
|
|
164
|
+
}
|
|
165
|
+
if (Array.isArray(value)) {
|
|
166
|
+
const buf = new Buffer(value.length);
|
|
167
|
+
for (let i = 0; i < value.length; i++) {
|
|
168
|
+
buf[i] = value[i] & 255;
|
|
169
|
+
}
|
|
170
|
+
return buf;
|
|
171
|
+
}
|
|
172
|
+
throw new TypeError("The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array");
|
|
173
|
+
}
|
|
174
|
+
// ---- Static methods ----
|
|
175
|
+
static isBuffer(obj) {
|
|
176
|
+
return obj instanceof Buffer;
|
|
177
|
+
}
|
|
178
|
+
static isEncoding(encoding) {
|
|
179
|
+
if (typeof encoding !== "string") return false;
|
|
180
|
+
const lower = encoding.toLowerCase().replace(/-/g, "");
|
|
181
|
+
return ["utf8", "ascii", "latin1", "binary", "base64", "base64url", "hex", "ucs2", "utf16le"].includes(lower);
|
|
182
|
+
}
|
|
183
|
+
static byteLength(string, encoding) {
|
|
184
|
+
if (typeof string !== "string") {
|
|
185
|
+
if (ArrayBuffer.isView(string)) return string.byteLength;
|
|
186
|
+
if (string instanceof ArrayBuffer) return string.byteLength;
|
|
187
|
+
if (hasSharedArrayBuffer && string instanceof SharedArrayBuffer) return string.byteLength;
|
|
188
|
+
throw new TypeError('The "string" argument must be one of type string, Buffer, or ArrayBuffer');
|
|
189
|
+
}
|
|
190
|
+
const enc = normalizeEncoding(encoding);
|
|
191
|
+
switch (enc) {
|
|
192
|
+
case "utf8":
|
|
193
|
+
return textEncoder.encode(string).byteLength;
|
|
194
|
+
case "ascii":
|
|
195
|
+
case "latin1":
|
|
196
|
+
return string.length;
|
|
197
|
+
case "base64":
|
|
198
|
+
case "base64url": {
|
|
199
|
+
let len = string.length;
|
|
200
|
+
while (len > 0 && (string[len - 1] === "=" || string[len - 1] === " ")) len--;
|
|
201
|
+
return len * 3 >>> 2;
|
|
202
|
+
}
|
|
203
|
+
case "hex":
|
|
204
|
+
return string.length >>> 1;
|
|
205
|
+
case "utf16le":
|
|
206
|
+
return string.length * 2;
|
|
207
|
+
default:
|
|
208
|
+
return textEncoder.encode(string).byteLength;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
static compare(buf1, buf2) {
|
|
212
|
+
if (!(buf1 instanceof Uint8Array) || !(buf2 instanceof Uint8Array)) {
|
|
213
|
+
throw new TypeError("Arguments must be Buffers or Uint8Arrays");
|
|
214
|
+
}
|
|
215
|
+
const len = Math.min(buf1.length, buf2.length);
|
|
216
|
+
for (let i = 0; i < len; i++) {
|
|
217
|
+
if (buf1[i] < buf2[i]) return -1;
|
|
218
|
+
if (buf1[i] > buf2[i]) return 1;
|
|
219
|
+
}
|
|
220
|
+
if (buf1.length < buf2.length) return -1;
|
|
221
|
+
if (buf1.length > buf2.length) return 1;
|
|
222
|
+
return 0;
|
|
223
|
+
}
|
|
224
|
+
static concat(list, totalLength) {
|
|
225
|
+
if (list.length === 0) return Buffer.alloc(0);
|
|
226
|
+
if (totalLength === void 0) {
|
|
227
|
+
totalLength = 0;
|
|
228
|
+
for (let i = 0; i < list.length; i++) {
|
|
229
|
+
totalLength += list[i].length;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
const result = Buffer.alloc(totalLength);
|
|
233
|
+
let offset = 0;
|
|
234
|
+
for (let i = 0; i < list.length; i++) {
|
|
235
|
+
const buf = list[i];
|
|
236
|
+
const toCopy = Math.min(buf.length, totalLength - offset);
|
|
237
|
+
if (toCopy <= 0) continue;
|
|
238
|
+
result.set(buf.subarray(0, toCopy), offset);
|
|
239
|
+
offset += toCopy;
|
|
240
|
+
}
|
|
241
|
+
return result;
|
|
242
|
+
}
|
|
243
|
+
static poolSize = 8192;
|
|
244
|
+
// ---- Instance methods ----
|
|
245
|
+
toString(encoding, start, end) {
|
|
246
|
+
const enc = normalizeEncoding(encoding);
|
|
247
|
+
return decodeString(this, enc, start, end);
|
|
248
|
+
}
|
|
249
|
+
toJSON() {
|
|
250
|
+
return {
|
|
251
|
+
type: "Buffer",
|
|
252
|
+
data: Array.from(this)
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
equals(otherBuffer) {
|
|
256
|
+
if (!(otherBuffer instanceof Uint8Array)) {
|
|
257
|
+
throw new TypeError("Argument must be a Buffer or Uint8Array");
|
|
258
|
+
}
|
|
259
|
+
if (this.length !== otherBuffer.length) return false;
|
|
260
|
+
for (let i = 0; i < this.length; i++) {
|
|
261
|
+
if (this[i] !== otherBuffer[i]) return false;
|
|
262
|
+
}
|
|
263
|
+
return true;
|
|
264
|
+
}
|
|
265
|
+
compare(target, targetStart, targetEnd, sourceStart, sourceEnd) {
|
|
266
|
+
if (!(target instanceof Uint8Array)) {
|
|
267
|
+
throw new TypeError("Argument must be a Buffer or Uint8Array");
|
|
268
|
+
}
|
|
269
|
+
const src = sourceStart !== void 0 || sourceEnd !== void 0 ? this.subarray(sourceStart ?? 0, sourceEnd ?? this.length) : this;
|
|
270
|
+
const tgt = targetStart !== void 0 || targetEnd !== void 0 ? target.subarray(targetStart ?? 0, targetEnd ?? target.length) : target;
|
|
271
|
+
return Buffer.compare(src, tgt);
|
|
272
|
+
}
|
|
273
|
+
copy(target, targetStart = 0, sourceStart = 0, sourceEnd) {
|
|
274
|
+
const end = sourceEnd ?? this.length;
|
|
275
|
+
const toCopy = Math.min(end - sourceStart, target.length - targetStart);
|
|
276
|
+
if (toCopy <= 0) return 0;
|
|
277
|
+
target.set(this.subarray(sourceStart, sourceStart + toCopy), targetStart);
|
|
278
|
+
return toCopy;
|
|
279
|
+
}
|
|
280
|
+
// slice returns a Buffer (not Uint8Array) that shares memory
|
|
281
|
+
slice(start, end) {
|
|
282
|
+
const s = start ?? 0;
|
|
283
|
+
const e = end ?? this.length;
|
|
284
|
+
const sub = super.subarray(s, e);
|
|
285
|
+
return new Buffer(sub.buffer, sub.byteOffset, sub.byteLength);
|
|
286
|
+
}
|
|
287
|
+
// subarray also returns a Buffer
|
|
288
|
+
subarray(start, end) {
|
|
289
|
+
const sub = super.subarray(start, end);
|
|
290
|
+
return new Buffer(sub.buffer, sub.byteOffset, sub.byteLength);
|
|
291
|
+
}
|
|
292
|
+
write(string, offset, length, encoding) {
|
|
293
|
+
offset = offset ?? 0;
|
|
294
|
+
const enc = normalizeEncoding(encoding || (typeof length === "string" ? length : void 0));
|
|
295
|
+
const encoded = encodeString(string, enc);
|
|
296
|
+
const maxLen = length !== void 0 && typeof length === "number" ? Math.min(length, this.length - offset) : this.length - offset;
|
|
297
|
+
const toCopy = Math.min(encoded.length, maxLen);
|
|
298
|
+
this.set(encoded.subarray(0, toCopy), offset);
|
|
299
|
+
return toCopy;
|
|
300
|
+
}
|
|
301
|
+
fill(value, offset, end, encoding) {
|
|
302
|
+
const start = offset ?? 0;
|
|
303
|
+
const stop = end ?? this.length;
|
|
304
|
+
if (typeof value === "number") {
|
|
305
|
+
super.fill(value & 255, start, stop);
|
|
306
|
+
} else if (typeof value === "string") {
|
|
307
|
+
const enc = normalizeEncoding(encoding);
|
|
308
|
+
const encoded = encodeString(value, enc);
|
|
309
|
+
if (encoded.length === 0) {
|
|
310
|
+
super.fill(0, start, stop);
|
|
311
|
+
} else if (encoded.length === 1) {
|
|
312
|
+
super.fill(encoded[0], start, stop);
|
|
313
|
+
} else {
|
|
314
|
+
for (let i = start; i < stop; i++) {
|
|
315
|
+
this[i] = encoded[(i - start) % encoded.length];
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
} else if (value instanceof Uint8Array) {
|
|
319
|
+
if (value.length === 0) {
|
|
320
|
+
super.fill(0, start, stop);
|
|
321
|
+
} else {
|
|
322
|
+
for (let i = start; i < stop; i++) {
|
|
323
|
+
this[i] = value[(i - start) % value.length];
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
return this;
|
|
328
|
+
}
|
|
329
|
+
indexOf(value, byteOffset, encoding) {
|
|
330
|
+
if (typeof value === "number") {
|
|
331
|
+
return super.indexOf(value & 255, byteOffset);
|
|
332
|
+
}
|
|
333
|
+
const needle = typeof value === "string" ? encodeString(value, normalizeEncoding(encoding)) : value;
|
|
334
|
+
const start = byteOffset ?? 0;
|
|
335
|
+
outer:
|
|
336
|
+
for (let i = start; i <= this.length - needle.length; i++) {
|
|
337
|
+
for (let j = 0; j < needle.length; j++) {
|
|
338
|
+
if (this[i + j] !== needle[j]) continue outer;
|
|
339
|
+
}
|
|
340
|
+
return i;
|
|
341
|
+
}
|
|
342
|
+
return -1;
|
|
343
|
+
}
|
|
344
|
+
lastIndexOf(value, byteOffset, encoding) {
|
|
345
|
+
if (typeof value === "number") {
|
|
346
|
+
return byteOffset !== void 0 ? super.lastIndexOf(value & 255, byteOffset) : super.lastIndexOf(value & 255);
|
|
347
|
+
}
|
|
348
|
+
const needle = typeof value === "string" ? encodeString(value, normalizeEncoding(encoding)) : value;
|
|
349
|
+
const start = byteOffset !== void 0 ? Math.min(byteOffset, this.length - needle.length) : this.length - needle.length;
|
|
350
|
+
outer:
|
|
351
|
+
for (let i = start; i >= 0; i--) {
|
|
352
|
+
for (let j = 0; j < needle.length; j++) {
|
|
353
|
+
if (this[i + j] !== needle[j]) continue outer;
|
|
354
|
+
}
|
|
355
|
+
return i;
|
|
356
|
+
}
|
|
357
|
+
return -1;
|
|
358
|
+
}
|
|
359
|
+
includes(value, byteOffset, encoding) {
|
|
360
|
+
return this.indexOf(value, byteOffset, encoding) !== -1;
|
|
361
|
+
}
|
|
362
|
+
// ---- Read methods ----
|
|
363
|
+
readUInt8(offset = 0) {
|
|
364
|
+
checkOffset(offset, 1, this.length);
|
|
365
|
+
return this[offset];
|
|
366
|
+
}
|
|
367
|
+
readUInt16BE(offset = 0) {
|
|
368
|
+
checkOffset(offset, 2, this.length);
|
|
369
|
+
return this[offset] << 8 | this[offset + 1];
|
|
370
|
+
}
|
|
371
|
+
readUInt16LE(offset = 0) {
|
|
372
|
+
checkOffset(offset, 2, this.length);
|
|
373
|
+
return this[offset] | this[offset + 1] << 8;
|
|
374
|
+
}
|
|
375
|
+
readUInt32BE(offset = 0) {
|
|
376
|
+
checkOffset(offset, 4, this.length);
|
|
377
|
+
return this[offset] * 16777216 + (this[offset + 1] << 16 | this[offset + 2] << 8 | this[offset + 3]);
|
|
378
|
+
}
|
|
379
|
+
readUInt32LE(offset = 0) {
|
|
380
|
+
checkOffset(offset, 4, this.length);
|
|
381
|
+
return this[offset + 3] * 16777216 + (this[offset + 2] << 16 | this[offset + 1] << 8 | this[offset]) >>> 0;
|
|
382
|
+
}
|
|
383
|
+
readInt8(offset = 0) {
|
|
384
|
+
checkOffset(offset, 1, this.length);
|
|
385
|
+
return this[offset] | (this[offset] & 128 ? 4294967040 : 0);
|
|
386
|
+
}
|
|
387
|
+
readInt16BE(offset = 0) {
|
|
388
|
+
checkOffset(offset, 2, this.length);
|
|
389
|
+
const val = this[offset] << 8 | this[offset + 1];
|
|
390
|
+
return val & 32768 ? val | 4294901760 : val;
|
|
391
|
+
}
|
|
392
|
+
readInt16LE(offset = 0) {
|
|
393
|
+
checkOffset(offset, 2, this.length);
|
|
394
|
+
const val = this[offset] | this[offset + 1] << 8;
|
|
395
|
+
return val & 32768 ? val | 4294901760 : val;
|
|
396
|
+
}
|
|
397
|
+
readInt32BE(offset = 0) {
|
|
398
|
+
checkOffset(offset, 4, this.length);
|
|
399
|
+
return this[offset] << 24 | this[offset + 1] << 16 | this[offset + 2] << 8 | this[offset + 3];
|
|
400
|
+
}
|
|
401
|
+
readInt32LE(offset = 0) {
|
|
402
|
+
checkOffset(offset, 4, this.length);
|
|
403
|
+
return this[offset] | this[offset + 1] << 8 | this[offset + 2] << 16 | this[offset + 3] << 24;
|
|
404
|
+
}
|
|
405
|
+
readFloatBE(offset = 0) {
|
|
406
|
+
checkOffset(offset, 4, this.length);
|
|
407
|
+
const view = new DataView(this.buffer, this.byteOffset + offset, 4);
|
|
408
|
+
return view.getFloat32(0, false);
|
|
409
|
+
}
|
|
410
|
+
readFloatLE(offset = 0) {
|
|
411
|
+
checkOffset(offset, 4, this.length);
|
|
412
|
+
const view = new DataView(this.buffer, this.byteOffset + offset, 4);
|
|
413
|
+
return view.getFloat32(0, true);
|
|
414
|
+
}
|
|
415
|
+
readDoubleBE(offset = 0) {
|
|
416
|
+
checkOffset(offset, 8, this.length);
|
|
417
|
+
const view = new DataView(this.buffer, this.byteOffset + offset, 8);
|
|
418
|
+
return view.getFloat64(0, false);
|
|
419
|
+
}
|
|
420
|
+
readDoubleLE(offset = 0) {
|
|
421
|
+
checkOffset(offset, 8, this.length);
|
|
422
|
+
const view = new DataView(this.buffer, this.byteOffset + offset, 8);
|
|
423
|
+
return view.getFloat64(0, true);
|
|
424
|
+
}
|
|
425
|
+
readBigInt64BE(offset = 0) {
|
|
426
|
+
checkOffset(offset, 8, this.length);
|
|
427
|
+
const view = new DataView(this.buffer, this.byteOffset + offset, 8);
|
|
428
|
+
return view.getBigInt64(0, false);
|
|
429
|
+
}
|
|
430
|
+
readBigInt64LE(offset = 0) {
|
|
431
|
+
checkOffset(offset, 8, this.length);
|
|
432
|
+
const view = new DataView(this.buffer, this.byteOffset + offset, 8);
|
|
433
|
+
return view.getBigInt64(0, true);
|
|
434
|
+
}
|
|
435
|
+
readBigUInt64BE(offset = 0) {
|
|
436
|
+
checkOffset(offset, 8, this.length);
|
|
437
|
+
const view = new DataView(this.buffer, this.byteOffset + offset, 8);
|
|
438
|
+
return view.getBigUint64(0, false);
|
|
439
|
+
}
|
|
440
|
+
readBigUInt64LE(offset = 0) {
|
|
441
|
+
checkOffset(offset, 8, this.length);
|
|
442
|
+
const view = new DataView(this.buffer, this.byteOffset + offset, 8);
|
|
443
|
+
return view.getBigUint64(0, true);
|
|
444
|
+
}
|
|
445
|
+
readUIntBE(offset, byteLength) {
|
|
446
|
+
checkOffset(offset, byteLength, this.length);
|
|
447
|
+
let val = 0;
|
|
448
|
+
for (let i = 0; i < byteLength; i++) {
|
|
449
|
+
val = val * 256 + this[offset + i];
|
|
450
|
+
}
|
|
451
|
+
return val;
|
|
452
|
+
}
|
|
453
|
+
readUIntLE(offset, byteLength) {
|
|
454
|
+
checkOffset(offset, byteLength, this.length);
|
|
455
|
+
let val = 0;
|
|
456
|
+
let mul = 1;
|
|
457
|
+
for (let i = 0; i < byteLength; i++) {
|
|
458
|
+
val += this[offset + i] * mul;
|
|
459
|
+
mul *= 256;
|
|
460
|
+
}
|
|
461
|
+
return val;
|
|
462
|
+
}
|
|
463
|
+
readIntBE(offset, byteLength) {
|
|
464
|
+
checkOffset(offset, byteLength, this.length);
|
|
465
|
+
let val = 0;
|
|
466
|
+
for (let i = 0; i < byteLength; i++) {
|
|
467
|
+
val = val * 256 + this[offset + i];
|
|
468
|
+
}
|
|
469
|
+
if (val >= Math.pow(2, 8 * byteLength - 1)) {
|
|
470
|
+
val -= Math.pow(2, 8 * byteLength);
|
|
471
|
+
}
|
|
472
|
+
return val;
|
|
473
|
+
}
|
|
474
|
+
readIntLE(offset, byteLength) {
|
|
475
|
+
checkOffset(offset, byteLength, this.length);
|
|
476
|
+
let val = 0;
|
|
477
|
+
let mul = 1;
|
|
478
|
+
for (let i = 0; i < byteLength; i++) {
|
|
479
|
+
val += this[offset + i] * mul;
|
|
480
|
+
mul *= 256;
|
|
481
|
+
}
|
|
482
|
+
if (val >= Math.pow(2, 8 * byteLength - 1)) {
|
|
483
|
+
val -= Math.pow(2, 8 * byteLength);
|
|
484
|
+
}
|
|
485
|
+
return val;
|
|
486
|
+
}
|
|
487
|
+
// ---- Write methods ----
|
|
488
|
+
writeUInt8(value, offset = 0) {
|
|
489
|
+
checkOffset(offset, 1, this.length);
|
|
490
|
+
this[offset] = value & 255;
|
|
491
|
+
return offset + 1;
|
|
492
|
+
}
|
|
493
|
+
writeUInt16BE(value, offset = 0) {
|
|
494
|
+
checkOffset(offset, 2, this.length);
|
|
495
|
+
this[offset] = value >>> 8 & 255;
|
|
496
|
+
this[offset + 1] = value & 255;
|
|
497
|
+
return offset + 2;
|
|
498
|
+
}
|
|
499
|
+
writeUInt16LE(value, offset = 0) {
|
|
500
|
+
checkOffset(offset, 2, this.length);
|
|
501
|
+
this[offset] = value & 255;
|
|
502
|
+
this[offset + 1] = value >>> 8 & 255;
|
|
503
|
+
return offset + 2;
|
|
504
|
+
}
|
|
505
|
+
writeUInt32BE(value, offset = 0) {
|
|
506
|
+
checkOffset(offset, 4, this.length);
|
|
507
|
+
this[offset] = value >>> 24 & 255;
|
|
508
|
+
this[offset + 1] = value >>> 16 & 255;
|
|
509
|
+
this[offset + 2] = value >>> 8 & 255;
|
|
510
|
+
this[offset + 3] = value & 255;
|
|
511
|
+
return offset + 4;
|
|
512
|
+
}
|
|
513
|
+
writeUInt32LE(value, offset = 0) {
|
|
514
|
+
checkOffset(offset, 4, this.length);
|
|
515
|
+
this[offset] = value & 255;
|
|
516
|
+
this[offset + 1] = value >>> 8 & 255;
|
|
517
|
+
this[offset + 2] = value >>> 16 & 255;
|
|
518
|
+
this[offset + 3] = value >>> 24 & 255;
|
|
519
|
+
return offset + 4;
|
|
520
|
+
}
|
|
521
|
+
writeInt8(value, offset = 0) {
|
|
522
|
+
checkOffset(offset, 1, this.length);
|
|
523
|
+
if (value < 0) value = 255 + value + 1;
|
|
524
|
+
this[offset] = value & 255;
|
|
525
|
+
return offset + 1;
|
|
526
|
+
}
|
|
527
|
+
writeInt16BE(value, offset = 0) {
|
|
528
|
+
checkOffset(offset, 2, this.length);
|
|
529
|
+
this[offset] = value >>> 8 & 255;
|
|
530
|
+
this[offset + 1] = value & 255;
|
|
531
|
+
return offset + 2;
|
|
532
|
+
}
|
|
533
|
+
writeInt16LE(value, offset = 0) {
|
|
534
|
+
checkOffset(offset, 2, this.length);
|
|
535
|
+
this[offset] = value & 255;
|
|
536
|
+
this[offset + 1] = value >>> 8 & 255;
|
|
537
|
+
return offset + 2;
|
|
538
|
+
}
|
|
539
|
+
writeInt32BE(value, offset = 0) {
|
|
540
|
+
checkOffset(offset, 4, this.length);
|
|
541
|
+
this[offset] = value >>> 24 & 255;
|
|
542
|
+
this[offset + 1] = value >>> 16 & 255;
|
|
543
|
+
this[offset + 2] = value >>> 8 & 255;
|
|
544
|
+
this[offset + 3] = value & 255;
|
|
545
|
+
return offset + 4;
|
|
546
|
+
}
|
|
547
|
+
writeInt32LE(value, offset = 0) {
|
|
548
|
+
checkOffset(offset, 4, this.length);
|
|
549
|
+
this[offset] = value & 255;
|
|
550
|
+
this[offset + 1] = value >>> 8 & 255;
|
|
551
|
+
this[offset + 2] = value >>> 16 & 255;
|
|
552
|
+
this[offset + 3] = value >>> 24 & 255;
|
|
553
|
+
return offset + 4;
|
|
554
|
+
}
|
|
555
|
+
writeFloatBE(value, offset = 0) {
|
|
556
|
+
checkOffset(offset, 4, this.length);
|
|
557
|
+
const view = new DataView(this.buffer, this.byteOffset + offset, 4);
|
|
558
|
+
view.setFloat32(0, value, false);
|
|
559
|
+
return offset + 4;
|
|
560
|
+
}
|
|
561
|
+
writeFloatLE(value, offset = 0) {
|
|
562
|
+
checkOffset(offset, 4, this.length);
|
|
563
|
+
const view = new DataView(this.buffer, this.byteOffset + offset, 4);
|
|
564
|
+
view.setFloat32(0, value, true);
|
|
565
|
+
return offset + 4;
|
|
566
|
+
}
|
|
567
|
+
writeDoubleBE(value, offset = 0) {
|
|
568
|
+
checkOffset(offset, 8, this.length);
|
|
569
|
+
const view = new DataView(this.buffer, this.byteOffset + offset, 8);
|
|
570
|
+
view.setFloat64(0, value, false);
|
|
571
|
+
return offset + 8;
|
|
572
|
+
}
|
|
573
|
+
writeDoubleLE(value, offset = 0) {
|
|
574
|
+
checkOffset(offset, 8, this.length);
|
|
575
|
+
const view = new DataView(this.buffer, this.byteOffset + offset, 8);
|
|
576
|
+
view.setFloat64(0, value, true);
|
|
577
|
+
return offset + 8;
|
|
578
|
+
}
|
|
579
|
+
writeBigInt64BE(value, offset = 0) {
|
|
580
|
+
checkOffset(offset, 8, this.length);
|
|
581
|
+
const view = new DataView(this.buffer, this.byteOffset + offset, 8);
|
|
582
|
+
view.setBigInt64(0, value, false);
|
|
583
|
+
return offset + 8;
|
|
584
|
+
}
|
|
585
|
+
writeBigInt64LE(value, offset = 0) {
|
|
586
|
+
checkOffset(offset, 8, this.length);
|
|
587
|
+
const view = new DataView(this.buffer, this.byteOffset + offset, 8);
|
|
588
|
+
view.setBigInt64(0, value, true);
|
|
589
|
+
return offset + 8;
|
|
590
|
+
}
|
|
591
|
+
writeBigUInt64BE(value, offset = 0) {
|
|
592
|
+
checkOffset(offset, 8, this.length);
|
|
593
|
+
const view = new DataView(this.buffer, this.byteOffset + offset, 8);
|
|
594
|
+
view.setBigUint64(0, value, false);
|
|
595
|
+
return offset + 8;
|
|
596
|
+
}
|
|
597
|
+
writeBigUInt64LE(value, offset = 0) {
|
|
598
|
+
checkOffset(offset, 8, this.length);
|
|
599
|
+
const view = new DataView(this.buffer, this.byteOffset + offset, 8);
|
|
600
|
+
view.setBigUint64(0, value, true);
|
|
601
|
+
return offset + 8;
|
|
602
|
+
}
|
|
603
|
+
// ---- Swap methods ----
|
|
604
|
+
swap16() {
|
|
605
|
+
const len = this.length;
|
|
606
|
+
if (len % 2 !== 0) throw new RangeError("Buffer size must be a multiple of 16-bits");
|
|
607
|
+
for (let i = 0; i < len; i += 2) {
|
|
608
|
+
const a = this[i];
|
|
609
|
+
this[i] = this[i + 1];
|
|
610
|
+
this[i + 1] = a;
|
|
611
|
+
}
|
|
612
|
+
return this;
|
|
613
|
+
}
|
|
614
|
+
swap32() {
|
|
615
|
+
const len = this.length;
|
|
616
|
+
if (len % 4 !== 0) throw new RangeError("Buffer size must be a multiple of 32-bits");
|
|
617
|
+
for (let i = 0; i < len; i += 4) {
|
|
618
|
+
const a = this[i];
|
|
619
|
+
const b = this[i + 1];
|
|
620
|
+
this[i] = this[i + 3];
|
|
621
|
+
this[i + 1] = this[i + 2];
|
|
622
|
+
this[i + 2] = b;
|
|
623
|
+
this[i + 3] = a;
|
|
624
|
+
}
|
|
625
|
+
return this;
|
|
626
|
+
}
|
|
627
|
+
swap64() {
|
|
628
|
+
const len = this.length;
|
|
629
|
+
if (len % 8 !== 0) throw new RangeError("Buffer size must be a multiple of 64-bits");
|
|
630
|
+
for (let i = 0; i < len; i += 8) {
|
|
631
|
+
const a = this[i];
|
|
632
|
+
const b = this[i + 1];
|
|
633
|
+
const c = this[i + 2];
|
|
634
|
+
const d = this[i + 3];
|
|
635
|
+
this[i] = this[i + 7];
|
|
636
|
+
this[i + 1] = this[i + 6];
|
|
637
|
+
this[i + 2] = this[i + 5];
|
|
638
|
+
this[i + 3] = this[i + 4];
|
|
639
|
+
this[i + 4] = d;
|
|
640
|
+
this[i + 5] = c;
|
|
641
|
+
this[i + 6] = b;
|
|
642
|
+
this[i + 7] = a;
|
|
643
|
+
}
|
|
644
|
+
return this;
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
const kMaxLength = 2 ** 31 - 1;
|
|
648
|
+
const kStringMaxLength = 2 ** 28 - 16;
|
|
649
|
+
const constants = { MAX_LENGTH: kMaxLength, MAX_STRING_LENGTH: kStringMaxLength };
|
|
650
|
+
class SlowBuffer extends Buffer {
|
|
651
|
+
constructor(size) {
|
|
652
|
+
super(size);
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
export {
|
|
656
|
+
Buffer,
|
|
657
|
+
SlowBuffer,
|
|
658
|
+
constants,
|
|
659
|
+
kMaxLength,
|
|
660
|
+
kStringMaxLength
|
|
661
|
+
};
|
package/lib/esm/index.js
CHANGED
|
@@ -1,6 +1,33 @@
|
|
|
1
|
-
|
|
2
|
-
import
|
|
3
|
-
|
|
1
|
+
import { Buffer, SlowBuffer, kMaxLength, kStringMaxLength, constants } from "./buffer.js";
|
|
2
|
+
import { Blob as BlobImpl, File as FileImpl } from "./blob.js";
|
|
3
|
+
import { registerGlobal } from "@gjsify/utils";
|
|
4
|
+
registerGlobal("Blob", BlobImpl);
|
|
5
|
+
registerGlobal("File", FileImpl);
|
|
6
|
+
const atob = globalThis.atob;
|
|
7
|
+
const btoa = globalThis.btoa;
|
|
8
|
+
const INSPECT_MAX_BYTES = 50;
|
|
9
|
+
var index_default = {
|
|
10
|
+
Buffer,
|
|
11
|
+
SlowBuffer,
|
|
12
|
+
Blob: BlobImpl,
|
|
13
|
+
File: FileImpl,
|
|
14
|
+
atob,
|
|
15
|
+
btoa,
|
|
16
|
+
kMaxLength,
|
|
17
|
+
kStringMaxLength,
|
|
18
|
+
constants,
|
|
19
|
+
INSPECT_MAX_BYTES
|
|
20
|
+
};
|
|
4
21
|
export {
|
|
5
|
-
|
|
22
|
+
BlobImpl as Blob,
|
|
23
|
+
Buffer,
|
|
24
|
+
FileImpl as File,
|
|
25
|
+
INSPECT_MAX_BYTES,
|
|
26
|
+
SlowBuffer,
|
|
27
|
+
atob,
|
|
28
|
+
btoa,
|
|
29
|
+
constants,
|
|
30
|
+
index_default as default,
|
|
31
|
+
kMaxLength,
|
|
32
|
+
kStringMaxLength
|
|
6
33
|
};
|