chore: Add BiDi serialization for RegExp and Date (#9623)

This commit is contained in:
Nikolay Vitkov 2023-02-03 13:32:26 +01:00 committed by GitHub
parent c8bb11adfc
commit 9b11b6a4e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 1 deletions

View File

@ -1,5 +1,5 @@
import * as Bidi from 'chromium-bidi/lib/cjs/protocol/protocol.js';
import {debugError, isPlainObject} from '../util.js';
import {debugError, isDate, isPlainObject, isRegExp} from '../util.js';
/**
* @internal
@ -58,6 +58,19 @@ export class BidiSerializer {
type: 'object',
value: parsedObject,
};
} else if (isRegExp(arg)) {
return {
type: 'regexp',
value: {
pattern: arg.source,
flags: arg.flags,
},
};
} else if (isDate(arg)) {
return {
type: 'date',
value: arg.toISOString(),
};
}
throw new UnserializableError(
@ -152,6 +165,11 @@ export class BidiSerializer {
}, new Map());
case 'promise':
return {};
case 'regexp':
return new RegExp(result.value.pattern, result.value.flags);
case 'date':
return new Date(result.value);
case 'undefined':
return undefined;
case 'null':

View File

@ -166,6 +166,20 @@ export const isPlainObject = (obj: unknown): obj is Record<any, unknown> => {
return typeof obj === 'object' && obj?.constructor === Object;
};
/**
* @internal
*/
export const isRegExp = (obj: unknown): obj is RegExp => {
return typeof obj === 'object' && obj?.constructor === RegExp;
};
/**
* @internal
*/
export const isDate = (obj: unknown): obj is Date => {
return typeof obj === 'object' && obj?.constructor === Date;
};
/**
* @internal
*/