dcp-client 4.4.23 → 4.4.24

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.
@@ -0,0 +1,519 @@
1
+ /**
2
+ * @file polyfills.js
3
+ *
4
+ * Applies polyfills to the global object.
5
+ *
6
+ * @author Sam Cantor, sam@kingsds.network
7
+ * Ryan Saweczko, ryansaweczko@distributive.network
8
+ * @date Sept 2020, Mar 2025
9
+ */
10
+
11
+ self.wrapScriptLoading({ scriptName: 'polyfills' }, function accessLists$$fn(protectedStorage)
12
+ {
13
+ // Origin time for performance polyfill
14
+ const pt0 = new Date().getTime();
15
+
16
+ // Add polyfills for any non-allowed symbols
17
+ const polyfills = {
18
+ location: {
19
+ search: "",
20
+ href: 'DCP Worker',
21
+ },
22
+ // Assumption that if performance exists, performance.now must exist
23
+ performance: typeof performance !== 'undefined' ? performance : {
24
+ now: ()=>{
25
+ const res = new Date().getTime() - pt0;
26
+ return res;
27
+ },
28
+ timeOrigin: pt0,
29
+ toJSON: () => {return { timeOrigin: performance.timeOrigin } },
30
+ },
31
+ importScripts: function () {
32
+ throw new Error('importScripts is not supported on DCP');
33
+ },
34
+ globalThis: typeof globalThis === 'undefined' ? self : globalThis,
35
+ WorkerGlobalScope: typeof globalThis === 'undefined' ? self : globalThis,
36
+ // For browsers/SA-workers that don't support btoa/atob, modified from https://github.com/MaxArt2501/base64-js/blob/master/base64.js
37
+ btoa: function (string) {
38
+ var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
39
+
40
+ string = String(string);
41
+ var bitmap, a, b, c,
42
+ result = "", i = 0,
43
+ rest = string.length % 3;
44
+
45
+ for (; i < string.length;) {
46
+ if ((a = string.charCodeAt(i++)) > 255
47
+ || (b = string.charCodeAt(i++)) > 255
48
+ || (c = string.charCodeAt(i++)) > 255)
49
+ throw new TypeError("Failed to execute 'btoa': The string to be encoded contains characters outside of the Latin1 range.");
50
+
51
+ bitmap = (a << 16) | (b << 8) | c;
52
+ result += b64.charAt(bitmap >> 18 & 63) + b64.charAt(bitmap >> 12 & 63)
53
+ + b64.charAt(bitmap >> 6 & 63) + b64.charAt(bitmap & 63);
54
+ }
55
+
56
+ // If there's need of padding, replace the last 'A's with equal signs
57
+ return rest ? result.slice(0, rest - 3) + "===".substring(rest) : result;
58
+ },
59
+ atob: function (string) {
60
+ var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
61
+ string = String(string).replace(/[\t\n\f\r ]+/g, "");
62
+
63
+ // Adding the padding if missing, for semplicity
64
+ string += "==".slice(2 - (string.length & 3));
65
+ var bitmap, result = "", r1, r2, i = 0;
66
+ for (; i < string.length;) {
67
+ bitmap = b64.indexOf(string.charAt(i++)) << 18 | b64.indexOf(string.charAt(i++)) << 12
68
+ | (r1 = b64.indexOf(string.charAt(i++))) << 6 | (r2 = b64.indexOf(string.charAt(i++)));
69
+
70
+ result += r1 === 64 ? String.fromCharCode(bitmap >> 16 & 255)
71
+ : r2 === 64 ? String.fromCharCode(bitmap >> 16 & 255, bitmap >> 8 & 255)
72
+ : String.fromCharCode(bitmap >> 16 & 255, bitmap >> 8 & 255, bitmap & 255);
73
+ }
74
+ return result;
75
+ },
76
+ // Polyfill for Blob
77
+ Blob: class Blob {
78
+ /** @type {Array.<(Blob|Uint8Array)>} */
79
+ #parts = [];
80
+ #type = '';
81
+ #size = 0;
82
+ #endings = 'transparent';
83
+
84
+ /**
85
+ * The Blob() constructor returns a new Blob object. The content
86
+ * of the blob consists of the concatenation of the values given
87
+ * in the parameter array.
88
+ *
89
+ * @param {*} blobParts
90
+ * @param {{ type?: string, endings?: string }} [options]
91
+ */
92
+ constructor (blobParts = [], options = {}) {
93
+ if (typeof blobParts !== 'object' || blobParts === null) {
94
+ throw new TypeError('Failed to construct \'Blob\': The provided value cannot be converted to a sequence.');
95
+ }
96
+
97
+ if (typeof blobParts[Symbol.iterator] !== 'function') {
98
+ throw new TypeError('Failed to construct \'Blob\': The object must have a callable @@iterator property.');
99
+ }
100
+
101
+ if (typeof options !== 'object' && typeof options !== 'function') {
102
+ throw new TypeError('Failed to construct \'Blob\': parameter 2 cannot convert to dictionary.');
103
+ }
104
+
105
+ if (options === null) options = {};
106
+
107
+ const encoder = new TextEncoder();
108
+
109
+ for (const element of blobParts) {
110
+ let part;
111
+
112
+ if (ArrayBuffer.isView(element)) {
113
+ part = new Uint8Array(element.buffer.slice(element.byteOffset, element.byteOffset + element.byteLength));
114
+ } else if (element instanceof ArrayBuffer) {
115
+ part = new Uint8Array(element.slice(0));
116
+ } else if (element instanceof Blob) {
117
+ part = element;
118
+ } else {
119
+ part = encoder.encode(`${element}`);
120
+ }
121
+
122
+ const size = ArrayBuffer.isView(part) ? part.byteLength : part.size;
123
+ // Avoid pushing empty parts into the array to better GC them
124
+ if (size) {
125
+ this.#size += size;
126
+ this.#parts.push(part);
127
+ }
128
+ }
129
+
130
+ this.#endings = `${options.endings === undefined ? 'transparent' : options.endings}`;
131
+ const type = options.type === undefined ? '' : String(options.type);
132
+ this.#type = /^[\x20-\x7E]*$/.test(type) ? type : '';
133
+ }
134
+
135
+ /**
136
+ * The Blob interface's size property returns the
137
+ * size of the Blob in bytes.
138
+ */
139
+ get size() {
140
+ return this.#size;
141
+ }
142
+
143
+ /**
144
+ * The type property of a Blob object returns the MIME type of the file.
145
+ */
146
+ get type() {
147
+ return this.#type;
148
+ }
149
+
150
+ /**
151
+ * The text() method in the Blob interface returns a Promise
152
+ * that resolves with a string containing the contents of
153
+ * the blob, interpreted as UTF-8.
154
+ *
155
+ * @return {Promise<string>}
156
+ */
157
+ async text() {
158
+ debugger;
159
+ // More optimized than using this.arrayBuffer()
160
+ // that requires twice as much ram
161
+ const decoder = new TextDecoder();
162
+ let str = '';
163
+
164
+ for await (const part of toIterator(this.#parts, false)) {
165
+ str += decoder.decode(part, { stream: true });
166
+ }
167
+
168
+ // Remaining
169
+ str += decoder.decode();
170
+ return str;
171
+ }
172
+
173
+ /**
174
+ * The arrayBuffer() method in the Blob interface returns a
175
+ * Promise that resolves with the contents of the blob as
176
+ * binary data contained in an ArrayBuffer.
177
+ *
178
+ * @return {Promise<ArrayBuffer>}
179
+ */
180
+ async arrayBuffer() {
181
+ // Easier way... Just a unnecessary overhead
182
+ // const view = new Uint8Array(this.size);
183
+ // await this.stream().getReader({mode: 'byob'}).read(view);
184
+ // return view.buffer;
185
+
186
+ const data = new Uint8Array(this.size);
187
+ let offset = 0;
188
+
189
+ for await (const chunk of toIterator(this.#parts, false)) {
190
+ data.set(chunk, offset);
191
+ offset += chunk.length;
192
+ }
193
+
194
+ return data.buffer;
195
+ }
196
+
197
+ /**
198
+ * stream() requires a polyfill for "ReadableStream" so leave it NYI for
199
+ * now, in case of feature testing
200
+ */
201
+ // stream() {
202
+ // }
203
+
204
+ /**
205
+ * The Blob interface's slice() method creates and returns a
206
+ * new Blob object which contains data from a subset of the
207
+ * blob on which it's called.
208
+ *
209
+ * @param {number} [start]
210
+ * @param {number} [end]
211
+ * @param {string} [type]
212
+ */
213
+ slice(start = 0, end = this.size, type = '') {
214
+ const { size } = this;
215
+
216
+ let relativeStart = start < 0 ? Math.max(size + start, 0) : Math.min(start, size);
217
+ let relativeEnd = end < 0 ? Math.max(size + end, 0) : Math.min(end, size);
218
+
219
+ const span = Math.max(relativeEnd - relativeStart, 0);
220
+ const parts = this.#parts;
221
+ const blobParts = [];
222
+ let added = 0;
223
+
224
+ for (const part of parts) {
225
+ // don't add the overflow to new blobParts
226
+ if (added >= span) {
227
+ break;
228
+ }
229
+
230
+ const size = ArrayBuffer.isView(part) ? part.byteLength : part.size;
231
+ if (relativeStart && size <= relativeStart) {
232
+ // Skip the beginning and change the relative
233
+ // start & end position as we skip the unwanted parts
234
+ relativeStart -= size;
235
+ relativeEnd -= size;
236
+ } else {
237
+ let chunk;
238
+
239
+ if (ArrayBuffer.isView(part)) {
240
+ chunk = part.subarray(relativeStart, Math.min(size, relativeEnd));
241
+ added += chunk.byteLength;
242
+ } else {
243
+ chunk = part.slice(relativeStart, Math.min(size, relativeEnd));
244
+ added += chunk.size;
245
+ }
246
+
247
+ relativeEnd -= size;
248
+ blobParts.push(chunk);
249
+ relativeStart = 0; // All next sequential parts should start at 0
250
+ }
251
+ }
252
+
253
+ const blob = new Blob([], { type: String(type).toLowerCase() });
254
+ blob.#size = span;
255
+ blob.#parts = blobParts;
256
+
257
+ return blob;
258
+ }
259
+
260
+ get[Symbol.toStringTag]() {
261
+ return 'Blob';
262
+ }
263
+
264
+ static[Symbol.hasInstance](object) {
265
+ return (
266
+ object &&
267
+ typeof object === 'object' &&
268
+ typeof object.constructor === 'function' &&
269
+ (
270
+ typeof object.stream === 'function' ||
271
+ typeof object.arrayBuffer === 'function'
272
+ ) &&
273
+ /^(Blob|File)$/.test(object[Symbol.toStringTag])
274
+ );
275
+ }
276
+ },
277
+ };
278
+
279
+ /** @param {(Blob | Uint8Array)[]} parts */
280
+ async function * toIterator (parts, clone = true) {
281
+ for (const part of parts) {
282
+ if ('stream' in part) {
283
+ yield * (/** @type {AsyncIterableIterator<Uint8Array>} */ (part.stream()))
284
+ } else if (ArrayBuffer.isView(part)) {
285
+ if (clone) {
286
+ let position = part.byteOffset
287
+ const end = part.byteOffset + part.byteLength
288
+ while (position !== end) {
289
+ const size = Math.min(end - position, POOL_SIZE)
290
+ const chunk = part.buffer.slice(position, position + size)
291
+ position += chunk.byteLength
292
+ yield new Uint8Array(chunk)
293
+ }
294
+ } else {
295
+ yield part
296
+ }
297
+ /* c8 ignore next 10 */
298
+ } else {
299
+ // For blobs that have arrayBuffer but no stream method (nodes buffer.Blob)
300
+ let position = 0, b = (/** @type {Blob} */ (part))
301
+ while (position !== b.size) {
302
+ const chunk = b.slice(position, Math.min(b.size, position + POOL_SIZE))
303
+ const buffer = await chunk.arrayBuffer()
304
+ position += buffer.byteLength
305
+ yield new Uint8Array(buffer)
306
+ }
307
+ }
308
+ }
309
+ }
310
+
311
+ // Polyfill for TextEncoder/Decoder
312
+ var fromCharCode = String.fromCharCode;
313
+ var Object_prototype_toString = ({}).toString;
314
+ var sharedArrayBufferString = Object_prototype_toString.call(self["SharedArrayBuffer"]);
315
+ var undefinedObjectString = '[object Undefined]';
316
+ var NativeUint8Array = self.Uint8Array;
317
+ var patchedU8Array = NativeUint8Array || Array;
318
+ var nativeArrayBuffer = NativeUint8Array ? ArrayBuffer : patchedU8Array;
319
+ var arrayBuffer_isView = nativeArrayBuffer.isView || function(x) {return x && "length" in x};
320
+ var arrayBufferString = Object_prototype_toString.call(nativeArrayBuffer.prototype);
321
+ var tmpBufferU16 = new (NativeUint8Array ? Uint16Array : patchedU8Array)(32);
322
+
323
+ if (typeof TextEncoder === "undefined") {
324
+ polyfills.TextEncoder = function TextEncoder(){};
325
+ var TextEncoderPrototype = polyfills.TextEncoder["prototype"];
326
+ TextEncoderPrototype["encode"] = function(inputString){
327
+ // 0xc0 => 0b11000000; 0xff => 0b11111111; 0xc0-0xff => 0b11xxxxxx
328
+ // 0x80 => 0b10000000; 0xbf => 0b10111111; 0x80-0xbf => 0b10xxxxxx
329
+ var encodedString = inputString === void 0 ? "" : ("" + inputString), len=encodedString.length|0;
330
+ var result=new patchedU8Array((len << 1) + 8|0), tmpResult;
331
+ var i=0, pos=0, point=0, nextcode=0;
332
+ var upgradededArraySize=!NativeUint8Array; // normal arrays are auto-expanding
333
+ for (i=0; i<len; i=i+1|0, pos=pos+1|0) {
334
+ point = encodedString.charCodeAt(i)|0;
335
+ if (point <= 0x007f) {
336
+ result[pos] = point;
337
+ } else if (point <= 0x07ff) {
338
+ result[pos] = (0x6<<5)|(point>>6);
339
+ result[pos=pos+1|0] = (0x2<<6)|(point&0x3f);
340
+ } else {
341
+ widenCheck: {
342
+ if (0xD800 <= point) {
343
+ if (point <= 0xDBFF) {
344
+ nextcode = encodedString.charCodeAt(i=i+1|0)|0; // defaults to 0 when NaN, causing null replacement character
345
+
346
+ if (0xDC00 <= nextcode && nextcode <= 0xDFFF) {
347
+ //point = ((point - 0xD800)<<10) + nextcode - 0xDC00 + 0x10000|0;
348
+ point = (point<<10) + nextcode - 0x35fdc00|0;
349
+ if (point > 0xffff) {
350
+ result[pos] = (0x1e/*0b11110*/<<3) | (point>>18);
351
+ result[pos=pos+1|0] = (0x2/*0b10*/<<6) | ((point>>12)&0x3f/*0b00111111*/);
352
+ result[pos=pos+1|0] = (0x2/*0b10*/<<6) | ((point>>6)&0x3f/*0b00111111*/);
353
+ result[pos=pos+1|0] = (0x2/*0b10*/<<6) | (point&0x3f/*0b00111111*/);
354
+ continue;
355
+ }
356
+ break widenCheck;
357
+ }
358
+ point = 65533/*0b1111111111111101*/;//return '\xEF\xBF\xBD';//fromCharCode(0xef, 0xbf, 0xbd);
359
+ } else if (point <= 0xDFFF) {
360
+ point = 65533/*0b1111111111111101*/;//return '\xEF\xBF\xBD';//fromCharCode(0xef, 0xbf, 0xbd);
361
+ }
362
+ }
363
+ if (!upgradededArraySize && (i << 1) < pos && (i << 1) < (pos - 7|0)) {
364
+ upgradededArraySize = true;
365
+ tmpResult = new patchedU8Array(len * 3);
366
+ tmpResult.set( result );
367
+ result = tmpResult;
368
+ }
369
+ }
370
+ result[pos] = (0xe/*0b1110*/<<4) | (point>>12);
371
+ result[pos=pos+1|0] =(0x2/*0b10*/<<6) | ((point>>6)&0x3f/*0b00111111*/);
372
+ result[pos=pos+1|0] =(0x2/*0b10*/<<6) | (point&0x3f/*0b00111111*/);
373
+ }
374
+ }
375
+ return NativeUint8Array ? result.subarray(0, pos) : result.slice(0, pos);
376
+ };
377
+ }
378
+
379
+ if (typeof TextDecoder === "undefined") {
380
+ polyfills.TextDecoder = function TextDecoder(){};
381
+ polyfills.TextDecoder["prototype"]["decode"] = function(inputArrayOrBuffer){
382
+ var inputAs8 = inputArrayOrBuffer, asObjectString;
383
+ if (!arrayBuffer_isView(inputAs8)) {
384
+ asObjectString = Object_prototype_toString.call(inputAs8);
385
+ if (asObjectString !== arrayBufferString && asObjectString !== sharedArrayBufferString && asObjectString !== undefinedObjectString)
386
+ throw TypeError("Failed to execute 'decode' on 'TextDecoder': The provided value is not of type '(ArrayBuffer or ArrayBufferView)'");
387
+ inputAs8 = NativeUint8Array ? new patchedU8Array(inputAs8) : inputAs8 || [];
388
+ }
389
+
390
+ var resultingString = "", tmpStr = "", index = 0, len = inputAs8.length | 0, lenMinus32 = len - 32 | 0, nextEnd = 0, nextStop = 0, cp0 = 0, codePoint = 0, minBits = 0, cp1 = 0, pos = 0, tmp = -1;
391
+ // Note that tmp represents the 2nd half of a surrogate pair incase a surrogate gets divided between blocks
392
+ for (; index < len;) {
393
+ nextEnd = index <= lenMinus32 ? 32 : len - index | 0;
394
+ for (; pos < nextEnd; index = index + 1 | 0, pos = pos + 1 | 0) {
395
+ cp0 = inputAs8[index] & 0xff;
396
+ switch (cp0 >> 4) {
397
+ case 15:
398
+ cp1 = inputAs8[index = index + 1 | 0] & 0xff;
399
+ if ((cp1 >> 6) !== 2 || 247 < cp0) {
400
+ index = index - 1 | 0;
401
+ break;
402
+ }
403
+ codePoint = ((cp0 & 7) << 6) | (cp1 & 63);
404
+ minBits = 5; // 20 ensures it never passes -> all invalid replacements
405
+ cp0 = 0x100; // keep track of th bit size
406
+ case 14:
407
+ cp1 = inputAs8[index = index + 1 | 0] & 0xff;
408
+ codePoint <<= 6;
409
+ codePoint |= ((cp0 & 15) << 6) | (cp1 & 63);
410
+ minBits = (cp1 >> 6) === 2 ? minBits + 4 | 0 : 24; // 24 ensures it never passes -> all invalid replacements
411
+ cp0 = (cp0 + 0x100) & 0x300; // keep track of th bit size
412
+ case 13:
413
+ case 12:
414
+ cp1 = inputAs8[index = index + 1 | 0] & 0xff;
415
+ codePoint <<= 6;
416
+ codePoint |= ((cp0 & 31) << 6) | cp1 & 63;
417
+ minBits = minBits + 7 | 0;
418
+
419
+ // Now, process the code point
420
+ if (index < len && (cp1 >> 6) === 2 && (codePoint >> minBits) && codePoint < 0x110000) {
421
+ cp0 = codePoint;
422
+ codePoint = codePoint - 0x10000 | 0;
423
+ if (0 <= codePoint/*0xffff < codePoint*/) { // BMP code point
424
+ //nextEnd = nextEnd - 1|0;
425
+
426
+ tmp = (codePoint >> 10) + 0xD800 | 0; // highSurrogate
427
+ cp0 = (codePoint & 0x3ff) + 0xDC00 | 0; // lowSurrogate (will be inserted later in the switch-statement)
428
+
429
+ if (pos < 31) { // notice 31 instead of 32
430
+ tmpBufferU16[pos] = tmp;
431
+ pos = pos + 1 | 0;
432
+ tmp = -1;
433
+ } else {// else, we are at the end of the inputAs8 and let tmp0 be filled in later on
434
+ // NOTE that cp1 is being used as a temporary variable for the swapping of tmp with cp0
435
+ cp1 = tmp;
436
+ tmp = cp0;
437
+ cp0 = cp1;
438
+ }
439
+ } else nextEnd = nextEnd + 1 | 0; // because we are advancing i without advancing pos
440
+ } else {
441
+ // invalid code point means replacing the whole thing with null replacement characters
442
+ cp0 >>= 8;
443
+ index = index - cp0 - 1 | 0; // reset index back to what it was before
444
+ cp0 = 0xfffd;
445
+ }
446
+ // Finally, reset the variables for the next go-around
447
+ minBits = 0;
448
+ codePoint = 0;
449
+ nextEnd = index <= lenMinus32 ? 32 : len - index | 0;
450
+ /*case 11:
451
+ case 10:
452
+ case 9:
453
+ case 8:
454
+ codePoint ? codePoint = 0 : cp0 = 0xfffd; // fill with invalid replacement character
455
+ case 7:
456
+ case 6:
457
+ case 5:
458
+ case 4:
459
+ case 3:
460
+ case 2:
461
+ case 1:
462
+ case 0:
463
+ tmpBufferU16[pos] = cp0;
464
+ continue;*/
465
+ default:
466
+ tmpBufferU16[pos] = cp0; // fill with invalid replacement character
467
+ continue;
468
+ case 11:
469
+ case 10:
470
+ case 9:
471
+ case 8:
472
+ }
473
+ tmpBufferU16[pos] = 0xfffd; // fill with invalid replacement character
474
+ }
475
+ tmpStr += fromCharCode(
476
+ tmpBufferU16[0], tmpBufferU16[1], tmpBufferU16[2], tmpBufferU16[3], tmpBufferU16[4], tmpBufferU16[5], tmpBufferU16[6], tmpBufferU16[7],
477
+ tmpBufferU16[8], tmpBufferU16[9], tmpBufferU16[10], tmpBufferU16[11], tmpBufferU16[12], tmpBufferU16[13], tmpBufferU16[14], tmpBufferU16[15],
478
+ tmpBufferU16[16], tmpBufferU16[17], tmpBufferU16[18], tmpBufferU16[19], tmpBufferU16[20], tmpBufferU16[21], tmpBufferU16[22], tmpBufferU16[23],
479
+ tmpBufferU16[24], tmpBufferU16[25], tmpBufferU16[26], tmpBufferU16[27], tmpBufferU16[28], tmpBufferU16[29], tmpBufferU16[30], tmpBufferU16[31]
480
+ );
481
+ if (pos < 32) tmpStr = tmpStr.slice(0, pos - 32 | 0);//-(32-pos));
482
+ if (index < len) {
483
+ //fromCharCode.apply(0, tmpBufferU16 : NativeUint8Array ? tmpBufferU16.subarray(0,pos) : tmpBufferU16.slice(0,pos));
484
+ tmpBufferU16[0] = tmp;
485
+ pos = (~tmp) >>> 31;//tmp !== -1 ? 1 : 0;
486
+ tmp = -1;
487
+
488
+ if (tmpStr.length < resultingString.length) continue;
489
+ } else if (tmp !== -1) {
490
+ tmpStr += fromCharCode(tmp);
491
+ }
492
+
493
+ resultingString += tmpStr;
494
+ tmpStr = "";
495
+ }
496
+
497
+ return resultingString;
498
+ }
499
+ }
500
+
501
+ /* Polyfill section of workerBootstrap */
502
+
503
+ // Apply symbols from polyfill object
504
+ for (let prop in polyfills)
505
+ {
506
+ if (globalThis.hasOwnProperty(prop))
507
+ continue;
508
+ let propValue = polyfills[prop];
509
+ Object.defineProperty(self, prop, {
510
+ get: function getPolyfill() {
511
+ return propValue;
512
+ },
513
+ set: function setPolyfill(value) {
514
+ propValue = value;
515
+ },
516
+ configurable: false
517
+ });
518
+ }
519
+ });