mcbe-leveldb 1.0.0

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/utils/JSONB.ts ADDED
@@ -0,0 +1,566 @@
1
+ /**
2
+ * JSONB.ts
3
+ * An improved version of {@link JSON}
4
+ * @module
5
+ * @description This file contains the `JSONB` class.
6
+ * @author 8Crafter
7
+ */
8
+
9
+ // This version of the JSONB class is modified to not be on globalThis.
10
+
11
+ /**
12
+ * An intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.
13
+ *
14
+ * This is an improved version of {@link JSON}.
15
+ *
16
+ * @author 8Crafter
17
+ */
18
+ export const JSONB: JSONB = {} as any;
19
+
20
+ /**
21
+ * An intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.
22
+ *
23
+ * This is an improved version of {@link JSON}.
24
+ *
25
+ * @author 8Crafter
26
+ *
27
+ * @ignore
28
+ */
29
+ export interface JSONB {
30
+ /**
31
+ * Converts a JavaScript Object Notation B (JSONB) string into an object.
32
+ * @param text A valid JSON string.
33
+ * @param reviver A function that transforms the results. This function is called for each member of the object.
34
+ * @param options The options for parsing the JSON string.
35
+ * @param options.bigint A value that indicates whether to parse bigints (ex. 57126n).
36
+ * @param options.undefined A value that indicates whether to parse undefined (ex. undefined).
37
+ * @param options.Infinity A value that indicates whether to parse Infinity (ex. Infinity).
38
+ * @param options.NegativeInfinity A value that indicates whether to parse -Infinity (ex. -Infinity).
39
+ * @param options.NaN A value that indicates whether to parse NaN (ex. NaN).
40
+ * @param options.get A value that indicates whether to parse getter functions (ex. get).
41
+ *
42
+ * Warning: This option is currently ignored as it is non-functional.
43
+ * @param options.set A value that indicates whether to parse setter functions (ex. set).
44
+ *
45
+ * Warning: This option is currently ignored as it is non-functional.
46
+ * @param options.function A value that indicates whether to parse functions (ex. function).
47
+ *
48
+ * Warning: This option is currently ignored as it is non-functional.
49
+ * @param options.class A value that indicates whether to parse classes (ex. class).
50
+ *
51
+ * Warning: This option is currently ignored as it is non-functional.
52
+ *
53
+ * @returns A JavaScript value, usually an object or array, that represents the JSONB that was parsed from the specified string.
54
+ *
55
+ * @author 8Crafter
56
+ */
57
+ parse(
58
+ text: string,
59
+ reviver?: (this: any, key: string, value: any) => any,
60
+ options?: {
61
+ bigint?: boolean;
62
+ undefined?: boolean;
63
+ Infinity?: boolean;
64
+ NegativeInfinity?: boolean;
65
+ NaN?: boolean;
66
+ get?: false;
67
+ set?: false;
68
+ function?: false;
69
+ class?: false;
70
+ }
71
+ ): any;
72
+ /**
73
+ * Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
74
+ * @param value A JavaScript value, usually an object or array, to be converted.
75
+ * @param replacer A function that transforms the results.
76
+ * @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
77
+ * @param options The options for stringifying the provided value.
78
+ * @param options.bigint A value that indicates whether to stringify bigints (ex. 57126n).
79
+ * @param options.undefined A value that indicates whether to stringify undefined (ex. undefined).
80
+ * @param options.Infinity A value that indicates whether to stringify Infinity (ex. Infinity).
81
+ * @param options.NegativeInfinity A value that indicates whether to stringify -Infinity (ex. -Infinity).
82
+ * @param options.NaN A value that indicates whether to stringify NaN (ex. NaN).
83
+ * @param options.get A value that indicates whether to stringify getter functions (ex. get).
84
+ * @param options.set A value that indicates whether to stringify setter functions (ex. set).
85
+ * @param options.function A value that indicates whether to stringify functions (ex. function).
86
+ * @param options.class A value that indicates whether to stringify classes (ex. class).\
87
+ *
88
+ * Warning: This option is currently ignored as it is non-functional.
89
+ * @returns A JavaScript Object Notation B (JSONB) string that represents the value passed in.
90
+ *
91
+ * @author 8Crafter
92
+ */
93
+ stringify(
94
+ value: any,
95
+ replacer?: (this: any, key: string, value: any) => any,
96
+ space?: string | number,
97
+ options?: {
98
+ bigint?: boolean;
99
+ undefined?: boolean;
100
+ Infinity?: boolean;
101
+ NegativeInfinity?: boolean;
102
+ NaN?: boolean;
103
+ get?: boolean;
104
+ set?: boolean;
105
+ function?: boolean;
106
+ class?: false;
107
+ }
108
+ ): string;
109
+ /**
110
+ * Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
111
+ * @param value A JavaScript value, usually an object or array, to be converted.
112
+ * @param replacer An array of strings and numbers that acts as an approved list for selecting the object properties that will be stringified.
113
+ * @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
114
+ * @param options The options for stringifying the provided value.
115
+ * @param options.bigint A value that indicates whether to stringify bigints (ex. 57126n).
116
+ * @param options.undefined A value that indicates whether to stringify undefined (ex. undefined).
117
+ * @param options.Infinity A value that indicates whether to stringify Infinity (ex. Infinity).
118
+ * @param options.NegativeInfinity A value that indicates whether to stringify -Infinity (ex. -Infinity).
119
+ * @param options.NaN A value that indicates whether to stringify NaN (ex. NaN).
120
+ * @param options.get A value that indicates whether to stringify getter functions (ex. get).
121
+ * @param options.set A value that indicates whether to stringify setter functions (ex. set).
122
+ * @param options.function A value that indicates whether to stringify functions (ex. function).
123
+ * @param options.class A value that indicates whether to stringify classes (ex. class).\
124
+ *
125
+ * Warning: This option is currently ignored as it is non-functional.
126
+ * @returns A JavaScript Object Notation B (JSONB) string that represents the value passed in.
127
+ *
128
+ * @author 8Crafter
129
+ */
130
+ stringify(
131
+ value: any,
132
+ replacer?: (number | string)[] | null,
133
+ space?: string | number,
134
+ options?: {
135
+ bigint?: boolean;
136
+ undefined?: boolean;
137
+ Infinity?: boolean;
138
+ NegativeInfinity?: boolean;
139
+ NaN?: boolean;
140
+ get?: boolean;
141
+ set?: boolean;
142
+ function?: boolean;
143
+ class?: false;
144
+ }
145
+ ): string;
146
+ }
147
+
148
+ (function () {
149
+ "use strict";
150
+
151
+ /**
152
+ * Asserts that a value is not `undefined` or `null`.
153
+ *
154
+ * @template T The type of the value to check.
155
+ * @param {T} value The value to check.
156
+ * @returns {asserts value is NonNullable<T>} Asserts that the value is not `undefined` or `null`.
157
+ *
158
+ * @throws {Error} If the value is `undefined` or `null`.
159
+ */
160
+ function assertIsDefined<T>(value: T): asserts value is NonNullable<T> {
161
+ if (value === undefined || value === null) {
162
+ throw new Error(`${value} is not defined`);
163
+ }
164
+ }
165
+
166
+ var rx_one = /^[\],:{}\s]*$/;
167
+ var rx_two = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g;
168
+ var rx_three = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g;
169
+ var rx_four = /(?:^|:|,)(?:\s*\[)+/g;
170
+ var rx_escapable = /[\\"\u0000-\u001f\u007f-\u009f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;
171
+ var rx_dangerous = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;
172
+
173
+ function f(n: number) {
174
+ // Format integers to have at least two digits.
175
+ return n < 10 ? "0" + n : n;
176
+ }
177
+
178
+ function this_value(this: any) {
179
+ return this.valueOf();
180
+ }
181
+
182
+ if (typeof Date.prototype.toJSON !== "function") {
183
+ Date.prototype.toJSON = function () {
184
+ return isFinite(this.valueOf())
185
+ ? this.getUTCFullYear() +
186
+ "-" +
187
+ f(this.getUTCMonth() + 1) +
188
+ "-" +
189
+ f(this.getUTCDate()) +
190
+ "T" +
191
+ f(this.getUTCHours()) +
192
+ ":" +
193
+ f(this.getUTCMinutes()) +
194
+ ":" +
195
+ f(this.getUTCSeconds()) +
196
+ "Z"
197
+ : (null as never);
198
+ }; /*
199
+
200
+ Boolean.prototype.toJSON = this_value;
201
+ Number.prototype.toJSON = this_value;
202
+ String.prototype.toJSON = this_value;*/
203
+ }
204
+
205
+ var gap: string;
206
+ var indent: string;
207
+ var meta: { [key: string]: string };
208
+ var rep: (string | number)[] | ((this: any, key: string, value: any) => any) | null | undefined;
209
+
210
+ function quote(string: string) {
211
+ // If the string contains no control characters, no quote characters, and no
212
+ // backslash characters, then we can safely slap some quotes around it.
213
+ // Otherwise we must also replace the offending characters with safe escape
214
+ // sequences.
215
+
216
+ rx_escapable.lastIndex = 0;
217
+ return rx_escapable.test(string)
218
+ ? '"' +
219
+ string.replace(rx_escapable, function (a) {
220
+ var c = meta[a];
221
+ return typeof c === "string" ? c : "\\u" + ("0000" + a.charCodeAt(0).toString(16)).slice(-4);
222
+ }) +
223
+ '"'
224
+ : '"' + string + '"';
225
+ }
226
+
227
+ function str(key: string | number, holder: any, options: Parameters<typeof JSONB.stringify>[3]): string | undefined {
228
+ // Produce a string from holder[key].
229
+
230
+ var i: number; // The loop counter.
231
+ var k; // The member key.
232
+ var v; // The member value.
233
+ var length;
234
+ var mind = gap;
235
+ var partial;
236
+ var value = holder[key] as any | unknown | null;
237
+ if (options?.get) {
238
+ if (Object.hasOwn(holder, "__lookupGetter__") ? !!holder?.__lookupGetter__(key) : false) {
239
+ if (options.set) {
240
+ if (!!holder.__lookupSetter__(key)) {
241
+ value = { get: holder.__lookupGetter__(key), set: holder.__lookupSetter__(key) };
242
+ } else {
243
+ value = { get: holder.__lookupGetter__(key) };
244
+ }
245
+ } else {
246
+ value = { get: holder.__lookupGetter__(key) };
247
+ }
248
+ } else if (options.set) {
249
+ if (Object.hasOwn(holder, "__lookupSetter__") ? !!holder.__lookupSetter__(key) : false) {
250
+ value = { set: holder.__lookupSetter__(key) };
251
+ }
252
+ }
253
+ } else if (options?.set) {
254
+ if (Object.hasOwn(holder, "__lookupSetter__") ? !!holder.__lookupSetter__(key) : false) {
255
+ value = { set: holder.__lookupSetter__(key) };
256
+ }
257
+ } /*
258
+
259
+ // If the value is an instance of the Decimal or Decimal2 class, convert it to decimal type.
260
+
261
+ if (
262
+ value
263
+ && typeof value === "object"
264
+ && typeof value.toJSONB === "function"
265
+ ) {
266
+ value = value.toJSONB(key);
267
+ } */
268
+
269
+ // If the value has a toJSONB or toJSON method, call it to obtain a replacement value.
270
+
271
+ if (value && typeof value === "object" && typeof value.toJSONB === "function") {
272
+ value = value.toJSONB(key);
273
+ } else if (value && typeof value === "object" && typeof value.toJSON === "function") {
274
+ value = value.toJSON(key);
275
+ }
276
+
277
+ // If we were called with a replacer function, then call the replacer to
278
+ // obtain a replacement value.
279
+
280
+ if (typeof rep === "function") {
281
+ value = rep.call(holder, key.toString(), value);
282
+ }
283
+
284
+ // What happens next depends on the value's type.
285
+
286
+ switch (typeof value) {
287
+ case "string":
288
+ return quote(value);
289
+
290
+ case "number":
291
+ // JSONB numbers must be finite. Encode non-finite numbers as null.
292
+
293
+ return isFinite(value)
294
+ ? String(value)
295
+ : value == Infinity
296
+ ? options?.Infinity ?? true
297
+ ? "Infinity"
298
+ : "null"
299
+ : value == -Infinity
300
+ ? options?.NegativeInfinity ?? true
301
+ ? "-Infinity"
302
+ : "null"
303
+ : Number.isNaN(value)
304
+ ? options?.NaN ?? true
305
+ ? "NaN"
306
+ : "null"
307
+ : "null";
308
+
309
+ case "bigint":
310
+ return options?.bigint ?? true ? String(value) + "n" : "null";
311
+ case "undefined":
312
+ return options?.undefined ?? true ? "undefined" : undefined;
313
+ case "function":
314
+ return options?.function ?? false ? value.toString() : undefined;
315
+ case "boolean":
316
+ // @ts-ignore
317
+ case "null":
318
+ // If the value is a boolean or null, convert it to a string. Note:
319
+ // typeof null does not produce "null". The case is included here in
320
+ // the remote chance that this gets fixed someday.
321
+
322
+ return String(value);
323
+
324
+ // If the type is "object", we might be dealing with an object or an array or
325
+ // null.
326
+
327
+ case "object":
328
+ // Due to a specification blunder in ECMAScript, typeof null is "object",
329
+ // so watch out for that case.
330
+
331
+ if (!value) {
332
+ return "null";
333
+ }
334
+
335
+ // Make an array to hold the partial results of stringifying this object value.
336
+
337
+ gap += indent;
338
+ partial = [];
339
+
340
+ // Is the value an array?
341
+
342
+ if (Object.prototype.toString.apply(value) === "[object Array]") {
343
+ // The value is an array. Stringify every element. Use null as a placeholder
344
+ // for non-JSONB values.
345
+
346
+ length = value.length;
347
+ for (i = 0; i < length; i += 1) {
348
+ partial[i] = str(i, value, options) || "null";
349
+ }
350
+
351
+ // Join all of the elements together, separated with commas, and wrap them in
352
+ // brackets.
353
+
354
+ v = partial.length === 0 ? "[]" : gap ? "[\n" + gap + partial.join(",\n" + gap) + "\n" + mind + "]" : "[" + partial.join(",") + "]";
355
+ gap = mind;
356
+ return v;
357
+ }
358
+
359
+ // If the replacer is an array, use it to select the members to be stringified.
360
+
361
+ if (rep && typeof rep === "object") {
362
+ length = rep.length;
363
+ for (i = 0; i < length; i += 1) {
364
+ if (typeof rep[i] === "string") {
365
+ k = rep[i]!;
366
+ v = str(k, value, options);
367
+ if (v) {
368
+ partial.push(quote(k.toString()) + (gap ? ": " : ":") + v);
369
+ }
370
+ }
371
+ }
372
+ } else {
373
+ // Otherwise, iterate through all of the keys in the object.
374
+
375
+ for (k in value) {
376
+ if (Object.prototype.hasOwnProperty.call(value, k)) {
377
+ v = str(k, value, options);
378
+ if (v) {
379
+ partial.push(quote(k) + (gap ? ": " : ":") + v);
380
+ }
381
+ }
382
+ }
383
+ }
384
+
385
+ // Join all of the member texts together, separated with commas,
386
+ // and wrap them in braces.
387
+
388
+ v = partial.length === 0 ? "{}" : gap ? "{\n" + gap + partial.join(",\n" + gap) + "\n" + mind + "}" : "{" + partial.join(",") + "}";
389
+ gap = mind;
390
+ return v;
391
+ }
392
+ }
393
+
394
+ // If the JSONB object does not yet have a stringify method, give it one.
395
+
396
+ if (typeof JSONB.stringify !== "function") {
397
+ meta = {
398
+ // table of character substitutions
399
+ "\b": "\\b",
400
+ "\t": "\\t",
401
+ "\n": "\\n",
402
+ "\f": "\\f",
403
+ "\r": "\\r",
404
+ '"': '\\"',
405
+ "\\": "\\\\",
406
+ };
407
+ JSONB.stringify = function (
408
+ value: any,
409
+ replacer?: ((this: any, key: string, value: any) => any) | (number | string)[] | null,
410
+ space?: string | number,
411
+ options: {
412
+ bigint?: boolean;
413
+ undefined?: boolean;
414
+ Infinity?: boolean;
415
+ NegativeInfinity?: boolean;
416
+ NaN?: boolean;
417
+ get?: false;
418
+ set?: false;
419
+ function?: boolean;
420
+ class?: false;
421
+ } = { bigint: true, undefined: true, Infinity: true, NegativeInfinity: true, NaN: true, get: false, set: false, function: false, class: false }
422
+ ): string {
423
+ // The stringify method takes a value and an optional replacer, and an optional
424
+ // space parameter, and returns a JSONB text. The replacer can be a function
425
+ // that can replace values, or an array of strings that will select the keys.
426
+ // A default replacer method can be provided. Use of the space parameter can
427
+ // produce text that is more easily readable.
428
+
429
+ var i;
430
+ gap = "";
431
+ indent = "";
432
+
433
+ // If the space parameter is a number, make an indent string containing that
434
+ // many spaces.
435
+
436
+ if (typeof space === "number") {
437
+ for (i = 0; i < space; i += 1) {
438
+ indent += " ";
439
+ }
440
+
441
+ // If the space parameter is a string, it will be used as the indent string.
442
+ } else if (typeof space === "string") {
443
+ indent = space;
444
+ }
445
+
446
+ // If there is a replacer, it must be a function or an array.
447
+ // Otherwise, throw an error.
448
+
449
+ rep = replacer;
450
+ if (replacer && typeof replacer !== "function" && (typeof replacer !== "object" || typeof replacer.length !== "number")) {
451
+ throw new SyntaxError("Invalid Replacer");
452
+ }
453
+
454
+ // Make a fake root object containing our value under the key of "".
455
+ // Return the result of stringifying the value.
456
+
457
+ return str("", { "": value }, options) as string;
458
+ };
459
+ }
460
+
461
+ // If the JSONB object does not yet have a parse method, give it one.
462
+
463
+ if (typeof JSONB.parse !== "function") {
464
+ JSONB.parse = function (
465
+ text: string,
466
+ reviver?: (this: any, key: string, value: any) => any,
467
+ options: {
468
+ bigint?: boolean;
469
+ undefined?: boolean;
470
+ Infinity?: boolean;
471
+ NegativeInfinity?: boolean;
472
+ NaN?: boolean;
473
+ get?: false;
474
+ set?: false;
475
+ function?: false;
476
+ class?: false;
477
+ } = { bigint: true, undefined: true, Infinity: true, NegativeInfinity: true, NaN: true, get: false, set: false, function: false, class: false }
478
+ ) {
479
+ // The parse method takes a text and an optional reviver function, and returns
480
+ // a JavaScript value if the text is a valid JSONB text.
481
+
482
+ var j;
483
+ var rx_three_b = RegExp(
484
+ `"[^"\\\\\\n\\r]*"|true|false|null|${options.undefined ?? true ? "undefined|" : ""}${options.Infinity ?? true ? "Infinity|" : ""}${
485
+ options.NegativeInfinity ?? true ? "-Infinity|" : ""
486
+ }${options.NaN ?? true ? "NaN|" : ""}-?\\d+${
487
+ options.bigint ?? true ? `(?:n|(?:\\.\\d*)?(?:[eE][+\\-]?\\d+)?)` : `(?:\\.\\d*)?(?:[eE][+\\-]?\\d+)?`
488
+ }`,
489
+ "g"
490
+ );
491
+
492
+ function walk(holder: any, key: string) {
493
+ assertIsDefined(reviver);
494
+
495
+ // The walk method is used to recursively walk the resulting structure so
496
+ // that modifications can be made.
497
+
498
+ var k;
499
+ var v;
500
+ var value = holder[key];
501
+ if (value && typeof value === "object") {
502
+ for (k in value) {
503
+ if (Object.prototype.hasOwnProperty.call(value, k)) {
504
+ v = walk(value, k);
505
+ if (v !== undefined) {
506
+ value[k] = v;
507
+ } else {
508
+ delete value[k];
509
+ }
510
+ }
511
+ }
512
+ }
513
+ return reviver.call(holder, key, value);
514
+ }
515
+
516
+ // Parsing happens in four stages. In the first stage, we replace certain
517
+ // Unicode characters with escape sequences. JavaScript handles many characters
518
+ // incorrectly, either silently deleting them, or treating them as line endings.
519
+
520
+ text = String(text);
521
+ rx_dangerous.lastIndex = 0;
522
+ if (rx_dangerous.test(text)) {
523
+ text = text.replace(rx_dangerous, function (a) {
524
+ return "\\u" + ("0000" + a.charCodeAt(0).toString(16)).slice(-4);
525
+ });
526
+ }
527
+
528
+ // In the second stage, we run the text against regular expressions that look
529
+ // for non-JSONB patterns. We are especially concerned with "()" and "new"
530
+ // because they can cause invocation, and "=" because it can cause mutation.
531
+ // But just to be safe, we want to reject all unexpected forms.
532
+
533
+ // We split the second stage into 4 regexp operations in order to work around
534
+ // crippling inefficiencies in IE's and Safari's regexp engines. First we
535
+ // replace the JSONB backslash pairs with "@" (a non-JSONB character). Second, we
536
+ // replace all simple value tokens with "]" characters. Third, we delete all
537
+ // open brackets that follow a colon or comma or that begin the text. Finally,
538
+ // we look to see that the remaining characters are only whitespace or "]" or
539
+ // "," or ":" or "{" or "}". If that is so, then the text is safe for eval.
540
+ /* console.log(
541
+ text
542
+ .replace(rx_two, "@")
543
+ .replace(rx_three_b, "]")
544
+ .replace(rx_four, "")
545
+ ) */
546
+ if (rx_one.test(text.replace(rx_two, "@").replace(rx_three_b, "]").replace(rx_four, ""))) {
547
+ // In the third stage we use the eval function to compile the text into a
548
+ // JavaScript structure. The "{" operator is subject to a syntactic ambiguity
549
+ // in JavaScript: it can begin a block or an object literal. We wrap the text
550
+ // in parens to eliminate the ambiguity.
551
+
552
+ j = eval("(" + text + ")");
553
+
554
+ // In the optional fourth stage, we recursively walk the new structure, passing
555
+ // each name/value pair to a reviver function for possible transformation.
556
+
557
+ return typeof reviver === "function" ? walk({ "": j }, "") : j;
558
+ }
559
+
560
+ // If the text is not JSONB parseable, then a SyntaxError is thrown.
561
+
562
+ throw new SyntaxError("JSONB.parse");
563
+ };
564
+ }
565
+ })();
566
+ // globalThis.JSONB = JSONB;