-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
712 lines (551 loc) · 26.1 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
import { NJSONError, parse as parser } from "./parser";
/** A function which can be used as `replacer` or `reviver`. */
export type NjsonFunction = (this: unknown, key: number | string, value: unknown) => unknown;
/** A function that transforms the results or the `Array` of _keys_ to be serialized. */
export type NjsonReplacer = NjsonFunction | (number | string)[];
/** NJSON parsing options. */
export interface NjsonParseOptions {
/** If `true` the `key` argument passed to `reviver` is of type `number` when reviving an `Array`. */
numberKey?: boolean;
/** A function that transforms the results. This function is called for each member of the object. If a member contains nested objects, the nested objects are transformed before the parent object is. */
reviver?: NjsonFunction;
}
/** NJSON serializing options. */
export interface NjsonStringifyOptions {
/** Specifies the method to be used to serialize `Date` objects. */
date?: "iso" | "string" | "time" | "utc";
/** If `true` the `key` argument passed to `replacer` is of type `number` when replacing an `Array`. */
numberKey?: boolean;
/** If `true` the `stack` is omitted when serializing `Error` objects. */
omitStack?: boolean;
/** A function that transforms the results or the `Array` of _keys_ to be serialized. */
replacer?: NjsonReplacer;
/** If `true` the _keys_ of objects are alphabetically sorted before being serialized. */
sortKeys?: boolean;
/** Adds indentation, white space, and line break characters to the return-value NJSON text to make it easier to read. */
space?: number | string;
/** When a `string` is longer than `stringLength` character it is considered a reference. */
stringLength?: number;
/** If `false`, `undefined` values ar skipped (as JSON.stringify does). */
undef?: boolean;
}
/**
* Converts a Next JavaScript Object Notation (NJSON) string into an object.
*
* @param text A valid NJSON string.
* @param options A `NjsonParseOptions` specifying the parsing options.
*/
function parse<T = unknown>(text: string, options?: NjsonParseOptions): T;
/**
* Converts a Next JavaScript Object Notation (NJSON) string into an object.
*
* @param text A valid NJSON string.
* @param reviver A function that transforms the results. This function is called for each member of the object. If a member contains nested objects, the nested objects are transformed before the parent object is.
*/
function parse<T = unknown>(text: string, reviver?: NjsonFunction): T;
function parse<T = unknown>(text: string, options?: NjsonParseOptions | NjsonFunction) {
try {
const result = parser(text, { grammarSource: "", offset: 0, ...options });
if(typeof options === "function") options = { reviver: options };
if(! options) options = {};
if(typeof options.reviver !== "function") return result;
const { numberKey, reviver } = options;
const references: unknown[] = [];
function revive(context: unknown, key: number | string, value: unknown, skip?: boolean, skipNext?: boolean): unknown {
if(value && typeof value === "object" && ! (value instanceof Date) && ! (value instanceof RegExp) && ! (value instanceof URL) && ! ArrayBuffer.isView(value) && references.indexOf(value) === -1) {
references.push(value);
if(value instanceof Array) for(const [i, _] of value.entries()) value[i] = revive(value, numberKey ? i : i.toString(), _, skipNext);
else if(value instanceof Error) {
const revived = revive(value, "message", value.message);
Object.defineProperty(value, "message", { configurable: true, value: revived, writable: true });
} else if(value instanceof Map) {
const elements = Array.from(value);
const { length } = elements;
for(const [i, _] of elements.entries()) {
const revived = revive(value, i, _, false, true);
let remove = false;
let replace = false;
let key: unknown;
let val: unknown;
if(revived instanceof Array && revived.length === 2) {
[key, val] = revived as [unknown, unknown];
if(key !== _[0]) remove = replace = true;
else value.set(key, val);
} else remove = true;
if(remove) {
for(let l = i; l < length; ++l) value.delete(elements[l][0]);
if(replace) value.set(key, val);
for(let l = i + i; l < length; ++l) value.set(...elements[l]);
}
}
} else if(value instanceof Set) {
const elements = Array.from(value);
const { length } = elements;
for(const [i, _] of elements.entries()) {
const revived = revive(value, i, _);
if(revived !== _) {
for(let l = i; l < length; ++l) value.delete(elements[l]);
value.add(revived);
for(let l = i + i; l < length; ++l) value.add(elements[l]);
}
}
} else for(const [key, val] of Object.entries(value)) (value as Record<string, unknown>)[key] = revive(value, key, val);
}
return skip ? value : reviver.call(context, key, value);
}
return revive({ "": result }, "", result) as T;
} catch(e) {
if(e instanceof NJSONError) {
const err = new SyntaxError(e.format([]).substring(7).replace(".\n at :", " at "));
const [first, , ...rest] = err.stack!.split("\n");
Object.defineProperty(err, "stack", { value: [first, ...rest].join("\n") });
throw err;
}
throw e;
}
}
interface ReplacerRef {
already?: boolean;
argument: () => boolean;
elements?: unknown;
exclude?: boolean;
found?: boolean;
identifier?: string;
statement?: () => string;
stringified?: string;
stringify: (currIndent: string, body?: boolean, first?: boolean) => string;
}
function argument(this: ReplacerRef): boolean {
if(this.identifier) return false;
if((this.elements as ReplacerRef[]).some(_ => ! _.argument())) return false;
return true;
}
function excludeRef(): ReplacerRef {
function argument(): boolean {
return true;
}
function stringify() {
return "null";
}
return { argument, exclude: true, stringify };
}
function nativeRef(stringified: string): ReplacerRef {
function argument(this: ReplacerRef): boolean {
return ! this.identifier;
}
function stringify(this: ReplacerRef, _: string, body?: boolean) {
return this.identifier && body ? this.identifier : this.stringified!;
}
return { argument, stringified, stringify };
}
const errors = [EvalError, RangeError, ReferenceError, SyntaxError, TypeError, URIError];
const typedArrays = [BigInt64Array, BigUint64Array, Float32Array, Float64Array, Int8Array, Int16Array, Int32Array, Uint8Array, Uint8ClampedArray, Uint16Array, Uint32Array];
/**
* Converts a JavaScript value to a Next JavaScript Object Notation (NJSON) string.
*
* @param value A JavaScript value, any value, to be converted.
* @param options A `NjsonStringifyOptions` specifying the serializing options.
*/
function stringify(value: unknown, options?: NjsonStringifyOptions): string;
/**
* Converts a JavaScript value to a Next JavaScript Object Notation (NJSON) string.
*
* @param value A JavaScript value, any value, to be converted.
* @param replacer A function that transforms the results or the `Array` of _keys_ to be serialized.
* @param space Adds indentation, white space, and line break characters to the return-value NJSON text to make it easier to read.
*/
function stringify(value: unknown, replacer?: NjsonReplacer | null, space?: number | string): string;
function stringify(value: unknown, options?: NjsonStringifyOptions | NjsonReplacer | null, space?: number | string) {
let newLine = "";
let numberKey = false;
let omitStack = false;
let replacer: NjsonFunction | undefined;
let separator = "";
let sortKeys = false;
let stringLength: number | undefined = undefined;
let stringifyDate: (value: Date) => string = value => value.getTime().toString();
let undef = true;
if(options) {
let arrayReplacer: (number | string)[] | undefined = undefined;
if(typeof options === "object") {
if(options instanceof Array) arrayReplacer = options;
else {
if(options.date === "iso") stringifyDate = value => `"${value.toISOString()}"`;
else if(options.date === "string") stringifyDate = value => `"${value.toString()}"`;
else if(options.date === "utc") stringifyDate = value => `"${value.toUTCString()}"`;
if(options.numberKey) numberKey = true;
if(typeof options.stringLength === "number") stringLength = options.stringLength;
if(options.omitStack === true) omitStack = true;
if(options.sortKeys === true) sortKeys = true;
if(options.undef === false) undef = false;
if(typeof options.replacer === "function") replacer = options.replacer;
else if(options.replacer instanceof Array) arrayReplacer = options.replacer;
space = options.space;
}
}
if(typeof options === "function") replacer = options;
if(arrayReplacer) {
const keys = ["", ...arrayReplacer.map(_ => (typeof _ === "number" && ! numberKey ? _.toString() : _)).filter(_ => typeof _ === "string" || (numberKey && typeof _ === "number"))];
replacer = function(key, value) {
return this instanceof Array ? value : keys.includes(key) ? value : parse;
};
}
}
if(typeof space === "string" || (typeof space === "number" && space > 0)) {
if(typeof space === "number") {
let spc = "";
for(let i = 1; i <= 10 && i <= space; ++i) spc += " ";
space = spc;
} else if(typeof space === "string") space = space.replace(/[^ \n\r\t]/g, "").substring(0, 10);
newLine = "\n";
separator = " ";
} else space = "";
const letters: number[] = [];
const references = new Map<unknown, ReplacerRef>();
const indent = space;
const twoIndent = indent + indent;
function getIdentifier() {
const { length } = letters;
for(let i = 0; i <= length; ++i) {
if(i === length) for(let l = 0; l <= length; ++l) letters[l] = 0;
else if(letters[i] === 25) letters[i] = 0;
else {
letters[i]++;
break;
}
}
return String.fromCharCode(...letters.map(_ => _ + 65).reverse());
}
function isReference(value: unknown) {
switch(typeof value) {
case "function":
case "symbol":
return true;
case "object":
return value;
case "string":
return stringLength !== undefined && value.length >= stringLength;
}
return false;
}
function replace(context: unknown, key: number | string, value: unknown, skip?: boolean, skipNext?: boolean): ReplacerRef {
if(replacer && ! skip) value = replacer.call(context, key, value);
if(skipNext && (! (value instanceof Array) || value.length !== 2)) return { exclude: true } as ReplacerRef;
if(isReference(value)) {
let ref = references.get(value);
if(! ref) {
let rest: ReplacerRef;
references.set(value, (ref = {} as ReplacerRef));
switch(typeof value) {
case "function":
case "symbol":
rest = excludeRef();
break;
case "string":
rest = nativeRef(JSON.stringify(value));
break;
default:
if(value instanceof Array) {
const elements: ReplacerRef[] = [];
let elems = elements;
for(const [i, _] of value.entries()) elements.push(replace(value, numberKey ? i : i.toString(), _, skipNext));
function stringify(this: ReplacerRef, currIndent: string, body?: boolean, first?: boolean) {
if(this.identifier && body) return this.identifier;
if(first) {
const push = elements.findIndex(_ => ! _.argument());
if(push !== -1) {
elems = elements.slice(0, push);
const args = elements.slice(push, elements.length);
this.statement = function(this: ReplacerRef) {
return `${indent}${this.identifier}.push(${newLine}${args.map(_ => twoIndent + _.stringify(twoIndent, true)).join("," + newLine)}${newLine}${indent})`;
};
}
}
const nextIndent = currIndent + indent;
return elems.length ? `[${newLine}${elems.map(_ => nextIndent + _.stringify(nextIndent, body)).join("," + newLine)}${newLine}${currIndent}]` : "[]";
}
rest = { argument, elements, stringify };
break;
}
if(value instanceof Date) {
rest = nativeRef(isNaN(value.getTime()) ? "new Date(NaN)" : `new Date(${stringifyDate(value)})`);
break;
}
if(value instanceof Map) {
const elements: ReplacerRef[] = [];
let args: ReplacerRef[] = [];
let elems = elements;
for(const [i, _] of Array.from(value).entries()) {
const replaced = replace(value, i, _, false, true);
if(! replaced.exclude) elements.push(replaced);
}
function stringify(this: ReplacerRef, currIndent: string, body?: boolean, first?: boolean) {
if(this.already && this.identifier && body) return this.identifier;
if(first) {
const set = elements.findIndex(_ => ! _.argument());
if(set !== -1) {
elems = elements.slice(0, set);
args = elements.slice(set, elements.length);
}
}
const nextIndent = currIndent + indent;
if(! this.already && this.identifier && body) {
this.already = true;
return args.length
? `${this.identifier}${newLine}${args
.map(_ => {
const stringified = _.stringify(nextIndent, true);
return `${nextIndent}.set(${stringified.substring(1, stringified.length - 1)})`;
})
.join(newLine)}`
: this.identifier;
}
return elems.length ? `new Map([${newLine}${elems.map(_ => nextIndent + _.stringify(nextIndent, body)).join("," + newLine)}${newLine}${currIndent}])` : "new Map()";
}
rest = { argument, elements, stringify };
break;
}
if(value instanceof RegExp) {
const [, exp, flags] = value.toString().match("/(.*)/(.*)")!;
rest = nativeRef(`new RegExp(${JSON.stringify(exp)}${flags ? `,"${flags}"` : ""})`);
break;
}
if(value instanceof Set) {
const elements: ReplacerRef[] = [];
let args: ReplacerRef[] = [];
let elems = elements;
for(const [i, _] of Array.from(value).entries()) elements.push(replace(value, i, _));
function stringify(this: ReplacerRef, currIndent: string, body?: boolean, first?: boolean) {
if(this.already && this.identifier && body) return this.identifier;
if(first) {
const add = elements.findIndex(_ => ! _.argument());
if(add !== -1) {
elems = elements.slice(0, add);
args = elements.slice(add, elements.length);
}
}
const nextIndent = currIndent + indent;
if(! this.already && this.identifier && body) {
this.already = true;
return args.length ? `${this.identifier}${newLine}${args.map(_ => `${nextIndent}.add(${_.stringify(nextIndent, true)})`).join(newLine)}` : this.identifier;
}
return elems.length ? `new Set([${newLine}${elems.map(_ => nextIndent + _.stringify(nextIndent, body)).join("," + newLine)}${newLine}${currIndent}])` : "new Set()";
}
rest = { argument, elements, stringify };
break;
}
if(value instanceof URL) {
rest = nativeRef(`new URL(${JSON.stringify(value.toString())})`);
break;
}
const id = typedArrays.findIndex(_ => value instanceof _);
if(id !== -1) {
const {
name,
prototype: { toString }
} = typedArrays[id];
const bigint = value instanceof BigInt64Array || value instanceof BigUint64Array;
const stringified = toString.call(value);
rest = nativeRef(stringified.length ? `new ${name}([${bigint ? stringified.replace(/,/g, "n,") + "n" : stringified}])` : `new ${name}()`);
} else {
const args: [string, ReplacerRef][] = [];
const elements: [string, ReplacerRef][] = [];
let elems = elements;
const entries = Object.entries(value as object);
if(sortKeys) entries.sort((a, b) => (a[0] < b[0] ? -1 : 1));
function argument(this: ReplacerRef): boolean {
if(this.identifier) return false;
if((this.elements as [string, ReplacerRef][]).some(_ => ! _[1].argument())) return false;
return true;
}
if(value instanceof Error) {
const id = errors.findIndex(_ => value instanceof _);
const error = id === -1 ? Error : errors[id];
const cause = value.cause ? replace(value, "cause", value.cause) : undefined;
const message = replace(value, "message", value.message);
const name = value.name !== error.name ? replace(value, "name", value.name) : undefined;
const stack = omitStack ? undefined : replace(value, "stack", value.stack);
let anyway = false;
if(cause && ! cause.exclude) elements.push(["cause", cause]);
if(name && ! name.exclude) elements.push(["name", name]);
if(stack && ! stack.exclude) elements.push(["stack", stack]);
for(const [key, val] of entries) {
if(["cause", "message", "name", "stack"].indexOf(key) === -1) {
const replaced = replace(value, key, val);
if(! replaced.exclude) elements.push([key, replaced]);
}
}
function stringify(this: ReplacerRef, currIndent: string, body?: boolean, first?: boolean) {
if(this.already && this.identifier && body) return this.identifier;
if(first) {
const notArgument = message && ! message.argument();
anyway = elements.findIndex(_ => ! _[1].argument()) !== -1 || notArgument;
if(anyway && notArgument) {
const cause = elements.length && elements[0][0] === "cause" ? elements.shift() : undefined;
elements.unshift(["message", message]);
if(cause) elements.unshift(cause);
}
}
const nextIndent = currIndent + indent;
const nextNextIndent = nextIndent + indent;
const construct = (indent: string) => `new ${error.name}(${! message || message.exclude || (first && ! message.argument()) ? '""' : message.stringify(indent, body)})`;
const assign = () =>
`Object.assign(${newLine}${nextIndent}${this.identifier && body ? this.identifier : construct(nextNextIndent)},${newLine}${nextIndent}{${newLine}${elements
.map(_ => `${nextNextIndent}${JSON.stringify(_[0])}:${separator}${_[1].stringify(nextNextIndent, body)}`)
.join("," + newLine)}${newLine}${nextIndent}}${newLine}${currIndent})`;
if(! this.already && this.identifier && body) {
this.already = true;
return anyway ? assign() : this.identifier;
}
return elements.length === 0 || anyway ? construct(nextIndent) : assign();
}
rest = { argument, elements, stringify };
break;
}
for(const [key, val] of entries) {
const replaced = replace(value, key, val);
if(! replaced.exclude) elements.push([key, replaced]);
}
function stringify(this: ReplacerRef, currIndent: string, body?: boolean, first?: boolean) {
if(this.already && this.identifier && body) return this.identifier;
if(first) {
elems = [];
for(const _ of elements) (_[1].argument() ? elems : args).push(_);
}
const nextIndent = currIndent + indent;
if(! this.already && this.identifier && body) {
this.already = true;
if(args.length) {
const nextNextIndent = nextIndent + indent;
return `Object.assign(${newLine}${nextIndent}${this.identifier},${newLine}${nextIndent}{${newLine}${args
.map(_ => `${nextNextIndent}${JSON.stringify(_[0])}:${separator}${_[1].stringify(nextNextIndent, true)}`)
.join("," + newLine)}${newLine}${nextIndent}}${newLine}${currIndent})`;
}
return this.identifier;
}
return elems.length
? `{${newLine}${elems.map(_ => `${nextIndent}${JSON.stringify(_[0])}:${separator}${_[1].stringify(nextIndent, body)}`).join("," + newLine)}${newLine}${currIndent}}`
: "{}";
}
rest = { argument, elements, stringify };
}
}
Object.assign(ref, rest);
}
if(ref.found) {
if(! (ref.exclude || ref.identifier)) ref.identifier = getIdentifier();
} else ref.found = true;
return ref;
}
switch(typeof value) {
case "bigint":
return nativeRef(value.toString() + "n");
case "boolean":
case "number":
return nativeRef(Object.is(value, -0) ? "-0" : value.toString());
case "object":
return nativeRef("null");
case "string":
return nativeRef(JSON.stringify(value));
case "undefined":
if(undef) return nativeRef("undefined");
}
return excludeRef();
}
const replaced = replace({ "": value }, "", value);
if(replaced.exclude) return undefined;
const identifiers = Array.from(references.values())
.filter(_ => _.identifier)
.sort((a, b) => (a.identifier!.length < b.identifier!.length ? -1 : a.identifier!.length > b.identifier!.length ? 1 : a.identifier! < b.identifier! ? -1 : 1));
if(! identifiers.length) return replaced.stringify("");
const args = identifiers.map(_ => _.stringify(indent, false, true));
const statements = identifiers.filter(_ => _.statement).map(_ => _.statement!());
statements.push(`${indent}return ${replaced.stringify(indent, true)}`);
return (
`((${identifiers.map(_ => _.identifier).join("," + separator)})${separator}=>${separator}{${newLine}${statements.join(";" + newLine)}${newLine}})` +
`(${newLine}${indent}${args.join(`,${newLine}${indent}`)}${newLine})`
);
}
/** Next JavaScript Object Notation. */
export const NJSON = { parse, stringify } as const;
declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace Express {
interface Response {
/**
* Sends NJSON response.
*
* @param body A value sent as body.
* @param options A `NjsonStringifyOptions` specifying the body serializing options; defaults to `ExpressNjsonOptions.stringify` passed to `expressNJSON`.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
njson(body?: any, options?: NjsonStringifyOptions): Promise<unknown>;
}
}
interface Response {
/**
* Parses the response body with `NJSON.parse`.
*
* @param options A `NjsonParseOptions` specifying the body parsing options; defaults to `options` passed to `fetchNJSON`.
*/
readonly njson: (options?: NjsonParseOptions) => Promise<unknown>;
}
}
/** `expressNJSON` middleware options. */
export interface ExpressNjsonOptions {
/** A `NjsonParseOptions` specifying the body parsing options. */
parse?: NjsonParseOptions;
/** A `NjsonStringifyOptions` specifying the body serializing options; can be overridden while calling `res.njson()`. */
stringify?: NjsonStringifyOptions;
}
/**
* An Express middleware which works as NJSON body parser and installs the `Express.Response.njson` method.
*
* @param options A `ExpressNjsonOptions` specifying the options to be used while serializing and parsing bodies.
*/
export function expressNJSON(options: ExpressNjsonOptions = {}) {
const { parse, stringify } = options;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return function(req: any, res: any, next: any) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
res.njson = function njson(this: any, body?: unknown, options?: NjsonStringifyOptions) {
this.set({ "Content-Type": "application/njson" });
this.end(NJSON.stringify(body, options || stringify));
};
if(req._body || req.headers["content-type"] !== "application/njson") return next();
const chunks: Buffer[] = [];
req.on("data", (chunk: Buffer) => chunks.push(chunk));
req.on("end", () => {
try {
req.body = NJSON.parse(Buffer.concat(chunks).toString(req.headers["content-encoding"] || "utf8"), parse);
req._body = true;
next();
} catch(error) {
next(error);
}
});
};
}
/**
* Installs the `Response.njson` method.
*
* @param options The default `NjsonParseOptions` specifying the body parsing options.
*/
export function fetchNJSON(options?: NjsonParseOptions) {
const _options = options;
if(Response && Response.prototype) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(Response.prototype as any).njson = async function njson(this: Response, options?: NjsonParseOptions) {
try {
return NJSON.parse(await this.text(), options || _options);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch(error: any) {
const { stack } = error;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [first, _, ...rest] = stack.split("\n");
Object.defineProperty(error, "stack", { configurable: true, enumerable: false, value: [first, ...rest].join("\n") });
throw error;
}
};
}
}