mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
chore: Add BiDi serialization for RegExp and Date (#9623)
This commit is contained in:
parent
c8bb11adfc
commit
9b11b6a4e0
@ -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':
|
||||
|
@ -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
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user