[ci] format
This commit is contained in:
parent
f0d22b9332
commit
cbd9e6222e
2 changed files with 269 additions and 225 deletions
|
@ -13,16 +13,20 @@ const GlobalCrypto = globalThis.crypto as Crypto;
|
||||||
|
|
||||||
export function randomBytes(size: number, cb?: (...args: any) => any) {
|
export function randomBytes(size: number, cb?: (...args: any) => any) {
|
||||||
if (!(GlobalCrypto && GlobalCrypto.getRandomValues)) {
|
if (!(GlobalCrypto && GlobalCrypto.getRandomValues)) {
|
||||||
throw new Error('Secure random number generation is not supported by this browser.\nUse Chrome, Firefox or Internet Explorer 11')
|
throw new Error(
|
||||||
|
'Secure random number generation is not supported by this browser.\nUse Chrome, Firefox or Internet Explorer 11'
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// phantomjs needs to throw
|
// phantomjs needs to throw
|
||||||
if (size > MAX_UINT32) throw new RangeError('requested too many random bytes');
|
if (size > MAX_UINT32) throw new RangeError('requested too many random bytes');
|
||||||
|
|
||||||
let bytes = new Uint32Array(size)
|
let bytes = new Uint32Array(size);
|
||||||
|
|
||||||
if (size > 0) { // getRandomValues fails on IE if size == 0
|
if (size > 0) {
|
||||||
if (size > MAX_BYTES) { // this is the max bytes crypto.getRandomValues
|
// getRandomValues fails on IE if size == 0
|
||||||
|
if (size > MAX_BYTES) {
|
||||||
|
// this is the max bytes crypto.getRandomValues
|
||||||
// can do at once see https://developer.mozilla.org/en-US/docs/Web/API/window.crypto.getRandomValues
|
// can do at once see https://developer.mozilla.org/en-US/docs/Web/API/window.crypto.getRandomValues
|
||||||
for (let generated = 0; generated < size; generated += MAX_BYTES) {
|
for (let generated = 0; generated < size; generated += MAX_BYTES) {
|
||||||
// buffer.slice automatically checks if the end is past the end of
|
// buffer.slice automatically checks if the end is past the end of
|
||||||
|
|
|
@ -7,12 +7,15 @@ Copyrights licensed under the New BSD License.
|
||||||
See the accompanying LICENSE file for terms.
|
See the accompanying LICENSE file for terms.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { randomBytes } from "./random-bytes";
|
import { randomBytes } from './random-bytes';
|
||||||
|
|
||||||
// Generate an internal UID to make the regexp pattern harder to guess.
|
// Generate an internal UID to make the regexp pattern harder to guess.
|
||||||
const UID_LENGTH = 16;
|
const UID_LENGTH = 16;
|
||||||
const UID = generateUID();
|
const UID = generateUID();
|
||||||
const PLACE_HOLDER_REGEXP = new RegExp('(\\\\)?"@__(F|R|D|M|S|A|U|I|B|L)-' + UID + '-(\\d+)__@"', 'g');
|
const PLACE_HOLDER_REGEXP = new RegExp(
|
||||||
|
'(\\\\)?"@__(F|R|D|M|S|A|U|I|B|L)-' + UID + '-(\\d+)__@"',
|
||||||
|
'g'
|
||||||
|
);
|
||||||
|
|
||||||
const IS_NATIVE_CODE_REGEXP = /\{\s*\[native code\]\s*\}/g;
|
const IS_NATIVE_CODE_REGEXP = /\{\s*\[native code\]\s*\}/g;
|
||||||
const IS_PURE_FUNCTION = /function.*?\(/;
|
const IS_PURE_FUNCTION = /function.*?\(/;
|
||||||
|
@ -28,10 +31,12 @@ const ESCAPED_CHARS = {
|
||||||
'>': '\\u003E',
|
'>': '\\u003E',
|
||||||
'/': '\\u002F',
|
'/': '\\u002F',
|
||||||
'\u2028': '\\u2028',
|
'\u2028': '\\u2028',
|
||||||
'\u2029': '\\u2029'
|
'\u2029': '\\u2029',
|
||||||
};
|
};
|
||||||
|
|
||||||
function escapeUnsafeChars(unsafeChar: keyof typeof ESCAPED_CHARS): typeof ESCAPED_CHARS[keyof typeof ESCAPED_CHARS] {
|
function escapeUnsafeChars(
|
||||||
|
unsafeChar: keyof typeof ESCAPED_CHARS
|
||||||
|
): typeof ESCAPED_CHARS[keyof typeof ESCAPED_CHARS] {
|
||||||
return ESCAPED_CHARS[unsafeChar];
|
return ESCAPED_CHARS[unsafeChar];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,7 +52,7 @@ function generateUID() {
|
||||||
function deleteFunctions(obj: Record<any, any>) {
|
function deleteFunctions(obj: Record<any, any>) {
|
||||||
let functionKeys = [];
|
let functionKeys = [];
|
||||||
for (let key in obj) {
|
for (let key in obj) {
|
||||||
if (typeof obj[key] === "function") {
|
if (typeof obj[key] === 'function') {
|
||||||
functionKeys.push(key);
|
functionKeys.push(key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -57,7 +62,10 @@ function deleteFunctions(obj: Record<any, any>) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export type TypeGenericFunction = (...args: any[]) => any;
|
export type TypeGenericFunction = (...args: any[]) => any;
|
||||||
export function serialize(obj: Record<any, any> | any, options?: number | string | Record<any, any>): string | any {
|
export function serialize(
|
||||||
|
obj: Record<any, any> | any,
|
||||||
|
options?: number | string | Record<any, any>
|
||||||
|
): string | any {
|
||||||
options || (options = {});
|
options || (options = {});
|
||||||
|
|
||||||
// Backwards-compatibility for `space` as the second argument.
|
// Backwards-compatibility for `space` as the second argument.
|
||||||
|
@ -72,14 +80,13 @@ export function serialize(obj: Record<any, any> | any, options?: number | string
|
||||||
let sets: Set<any>[] = [];
|
let sets: Set<any>[] = [];
|
||||||
let arrays: any[] = [];
|
let arrays: any[] = [];
|
||||||
let undefs: undefined[] = [];
|
let undefs: undefined[] = [];
|
||||||
let infinities: (typeof Infinity)[] = [];
|
let infinities: typeof Infinity[] = [];
|
||||||
let bigInts: BigInt[] = [];
|
let bigInts: BigInt[] = [];
|
||||||
let urls: URL[] = [];
|
let urls: URL[] = [];
|
||||||
|
|
||||||
// Returns placeholders for functions and regexps (identified by index)
|
// Returns placeholders for functions and regexps (identified by index)
|
||||||
// which are later replaced by their string representation.
|
// which are later replaced by their string representation.
|
||||||
function replacer(key: any, value: any) {
|
function replacer(key: any, value: any) {
|
||||||
|
|
||||||
// For nested function
|
// For nested function
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
if (options.ignoreFunction) {
|
if (options.ignoreFunction) {
|
||||||
|
@ -114,7 +121,10 @@ export function serialize(obj: Record<any, any> | any, options?: number | string
|
||||||
}
|
}
|
||||||
|
|
||||||
if (origValue instanceof Array) {
|
if (origValue instanceof Array) {
|
||||||
let isSparse = origValue.filter(function () { return true }).length !== origValue.length;
|
let isSparse =
|
||||||
|
origValue.filter(function () {
|
||||||
|
return true;
|
||||||
|
}).length !== origValue.length;
|
||||||
if (isSparse) {
|
if (isSparse) {
|
||||||
return '@__A-' + UID + '-' + (arrays.push(origValue) - 1) + '__@';
|
return '@__A-' + UID + '-' + (arrays.push(origValue) - 1) + '__@';
|
||||||
}
|
}
|
||||||
|
@ -161,20 +171,26 @@ export function serialize(obj: Record<any, any> | any, options?: number | string
|
||||||
}
|
}
|
||||||
|
|
||||||
let argsStartsAt = serializedFn.indexOf('(');
|
let argsStartsAt = serializedFn.indexOf('(');
|
||||||
let def = serializedFn.substr(0, argsStartsAt)
|
let def = serializedFn
|
||||||
|
.substr(0, argsStartsAt)
|
||||||
.trim()
|
.trim()
|
||||||
.split(' ')
|
.split(' ')
|
||||||
.filter(function (val: string) { return val.length > 0 });
|
.filter(function (val: string) {
|
||||||
|
return val.length > 0;
|
||||||
|
});
|
||||||
|
|
||||||
let nonReservedSymbols = def.filter(function (val: string) {
|
let nonReservedSymbols = def.filter(function (val: string) {
|
||||||
return RESERVED_SYMBOLS.indexOf(val) === -1
|
return RESERVED_SYMBOLS.indexOf(val) === -1;
|
||||||
});
|
});
|
||||||
|
|
||||||
// enhanced literal objects, example: {key() {}}
|
// enhanced literal objects, example: {key() {}}
|
||||||
if (nonReservedSymbols.length > 0) {
|
if (nonReservedSymbols.length > 0) {
|
||||||
return (def.indexOf('async') > -1 ? 'async ' : '') + 'function'
|
return (
|
||||||
+ (def.join('').indexOf('*') > -1 ? '*' : '')
|
(def.indexOf('async') > -1 ? 'async ' : '') +
|
||||||
+ serializedFn.substr(argsStartsAt);
|
'function' +
|
||||||
|
(def.join('').indexOf('*') > -1 ? '*' : '') +
|
||||||
|
serializedFn.substr(argsStartsAt)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// arrow functions
|
// arrow functions
|
||||||
|
@ -182,7 +198,7 @@ export function serialize(obj: Record<any, any> | any, options?: number | string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if the parameter is function
|
// Check if the parameter is function
|
||||||
if (options.ignoreFunction && typeof obj === "function") {
|
if (options.ignoreFunction && typeof obj === 'function') {
|
||||||
obj = undefined;
|
obj = undefined;
|
||||||
}
|
}
|
||||||
// Protects against `JSON.stringify()` returning `undefined`, by serializing
|
// Protects against `JSON.stringify()` returning `undefined`, by serializing
|
||||||
|
@ -216,7 +232,18 @@ export function serialize(obj: Record<any, any> | any, options?: number | string
|
||||||
str = str.replace(UNSAFE_CHARS_REGEXP, escapeUnsafeChars);
|
str = str.replace(UNSAFE_CHARS_REGEXP, escapeUnsafeChars);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (functions.length === 0 && regexps.length === 0 && dates.length === 0 && maps.length === 0 && sets.length === 0 && arrays.length === 0 && undefs.length === 0 && infinities.length === 0 && bigInts.length === 0 && urls.length === 0) {
|
if (
|
||||||
|
functions.length === 0 &&
|
||||||
|
regexps.length === 0 &&
|
||||||
|
dates.length === 0 &&
|
||||||
|
maps.length === 0 &&
|
||||||
|
sets.length === 0 &&
|
||||||
|
arrays.length === 0 &&
|
||||||
|
undefs.length === 0 &&
|
||||||
|
infinities.length === 0 &&
|
||||||
|
bigInts.length === 0 &&
|
||||||
|
urls.length === 0
|
||||||
|
) {
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -233,27 +260,40 @@ export function serialize(obj: Record<any, any> | any, options?: number | string
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type === 'D') {
|
if (type === 'D') {
|
||||||
return "new Date(\"" + dates[valueIndex].toISOString() + "\")";
|
return 'new Date("' + dates[valueIndex].toISOString() + '")';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type === 'R') {
|
if (type === 'R') {
|
||||||
return "new RegExp(" + serialize(regexps[valueIndex].source) + ", \"" + regexps[valueIndex].flags + "\")";
|
return (
|
||||||
|
'new RegExp(' +
|
||||||
|
serialize(regexps[valueIndex].source) +
|
||||||
|
', "' +
|
||||||
|
regexps[valueIndex].flags +
|
||||||
|
'")'
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type === 'M') {
|
if (type === 'M') {
|
||||||
return "new Map(" + serialize(Array.from(maps[valueIndex].entries()), options) + ")";
|
return 'new Map(' + serialize(Array.from(maps[valueIndex].entries()), options) + ')';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type === 'S') {
|
if (type === 'S') {
|
||||||
return "new Set(" + serialize(Array.from(sets[valueIndex].values()), options) + ")";
|
return 'new Set(' + serialize(Array.from(sets[valueIndex].values()), options) + ')';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type === 'A') {
|
if (type === 'A') {
|
||||||
return "Array.prototype.slice.call(" + serialize(Object.assign({ length: arrays[valueIndex].length }, arrays[valueIndex]), options) + ")";
|
return (
|
||||||
|
'Array.prototype.slice.call(' +
|
||||||
|
serialize(
|
||||||
|
Object.assign({ length: arrays[valueIndex].length }, arrays[valueIndex]),
|
||||||
|
options
|
||||||
|
) +
|
||||||
|
')'
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type === 'U') {
|
if (type === 'U') {
|
||||||
return 'undefined'
|
return 'undefined';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type === 'I') {
|
if (type === 'I') {
|
||||||
|
@ -261,11 +301,11 @@ export function serialize(obj: Record<any, any> | any, options?: number | string
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type === 'B') {
|
if (type === 'B') {
|
||||||
return "BigInt(\"" + bigInts[valueIndex] + "\")";
|
return 'BigInt("' + bigInts[valueIndex] + '")';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type === 'L') {
|
if (type === 'L') {
|
||||||
return "new URL(\"" + urls[valueIndex].toString() + "\")";
|
return 'new URL("' + urls[valueIndex].toString() + '")';
|
||||||
}
|
}
|
||||||
|
|
||||||
let fn = functions[valueIndex];
|
let fn = functions[valueIndex];
|
||||||
|
|
Loading…
Reference in a new issue