| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | /** |
| b69ab31 | | | 9 | * Types that are compatible with JSON.stringify and can be sent over the transport, |
| b69ab31 | | | 10 | * Plus Map, Set, Date, Error are supported (they are converted to objects before serializing) |
| b69ab31 | | | 11 | */ |
| b69ab31 | | | 12 | export type Serializable = |
| b69ab31 | | | 13 | | string |
| b69ab31 | | | 14 | | number |
| b69ab31 | | | 15 | | boolean |
| b69ab31 | | | 16 | | null |
| b69ab31 | | | 17 | | undefined |
| b69ab31 | | | 18 | | Map<Serializable, Serializable> |
| b69ab31 | | | 19 | | Set<Serializable> |
| b69ab31 | | | 20 | | Error |
| b69ab31 | | | 21 | | Date |
| b69ab31 | | | 22 | | {[key: string]: Serializable} |
| b69ab31 | | | 23 | | ReadonlyArray<Serializable>; |
| b69ab31 | | | 24 | |
| b69ab31 | | | 25 | export type Serialized = |
| b69ab31 | | | 26 | | string |
| b69ab31 | | | 27 | | number |
| b69ab31 | | | 28 | | boolean |
| b69ab31 | | | 29 | | null |
| b69ab31 | | | 30 | | undefined |
| b69ab31 | | | 31 | | Array<Serialized> |
| b69ab31 | | | 32 | | CustomSerialized; |
| b69ab31 | | | 33 | |
| b69ab31 | | | 34 | export type CustomSerialized = |
| b69ab31 | | | 35 | | {__rpcType: 'undefined'} |
| b69ab31 | | | 36 | | {__rpcType: 'object'; [key: string]: Serialized} |
| b69ab31 | | | 37 | | {__rpcType: 'Error'; data: {message: string; stack?: string}} |
| b69ab31 | | | 38 | | {__rpcType: 'Map'; data: Array<[Serialized, Serialized]>} |
| b69ab31 | | | 39 | | {__rpcType: 'Set'; data: Array<Serialized>} |
| b69ab31 | | | 40 | | {__rpcType: 'Date'; data: number}; |
| b69ab31 | | | 41 | |
| b69ab31 | | | 42 | const UNDEFINED_SERIALIZED = {__rpcType: 'undefined' as const}; |
| b69ab31 | | | 43 | |
| b69ab31 | | | 44 | /** |
| b69ab31 | | | 45 | * Prepare function arguments/return value to be serialized. This lets you pass Map/Set/RegExp to rpc functions. |
| b69ab31 | | | 46 | * Note that we need to do this recursively for arguments to Map/Set, since you can have complex nesting like Map<Set<>, Map<>> |
| b69ab31 | | | 47 | */ |
| b69ab31 | | | 48 | export function serialize(arg: Serializable): Serialized { |
| b69ab31 | | | 49 | // 'undefined' is not valid JSON, so it will be converted to 'null' when serialized |
| b69ab31 | | | 50 | // Therefore, we must serialize it ourselves |
| b69ab31 | | | 51 | if (arg === undefined) { |
| b69ab31 | | | 52 | return UNDEFINED_SERIALIZED; |
| b69ab31 | | | 53 | } |
| b69ab31 | | | 54 | |
| b69ab31 | | | 55 | if ( |
| b69ab31 | | | 56 | typeof arg === 'number' || |
| b69ab31 | | | 57 | typeof arg === 'boolean' || |
| b69ab31 | | | 58 | typeof arg === 'string' || |
| b69ab31 | | | 59 | arg === null |
| b69ab31 | | | 60 | ) { |
| b69ab31 | | | 61 | return arg; |
| b69ab31 | | | 62 | } |
| b69ab31 | | | 63 | |
| b69ab31 | | | 64 | if (arg instanceof Map) { |
| b69ab31 | | | 65 | return { |
| b69ab31 | | | 66 | __rpcType: 'Map', |
| b69ab31 | | | 67 | data: Array.from(arg.entries()).map(([key, val]) => [serialize(key), serialize(val)]), |
| b69ab31 | | | 68 | } as CustomSerialized; |
| b69ab31 | | | 69 | } else if (arg instanceof Set) { |
| b69ab31 | | | 70 | return {__rpcType: 'Set', data: Array.from(arg.values()).map(serialize)} as CustomSerialized; |
| b69ab31 | | | 71 | } else if (arg instanceof Error) { |
| b69ab31 | | | 72 | return {__rpcType: 'Error', data: {message: arg.message, stack: arg.stack}} as CustomSerialized; |
| b69ab31 | | | 73 | } else if (arg instanceof Date) { |
| b69ab31 | | | 74 | return {__rpcType: 'Date', data: arg.valueOf()} as CustomSerialized; |
| b69ab31 | | | 75 | } else if (Array.isArray(arg)) { |
| b69ab31 | | | 76 | return arg.map(a => serialize(a)); |
| b69ab31 | | | 77 | } else if (typeof arg === 'object') { |
| b69ab31 | | | 78 | const newObj: CustomSerialized & {__rpcType: 'object'} = {__rpcType: 'object'}; |
| b69ab31 | | | 79 | for (const [propertyName, propertyValue] of Object.entries(arg)) { |
| b69ab31 | | | 80 | newObj[propertyName] = serialize(propertyValue); |
| b69ab31 | | | 81 | } |
| b69ab31 | | | 82 | |
| b69ab31 | | | 83 | return newObj; |
| b69ab31 | | | 84 | } |
| b69ab31 | | | 85 | |
| b69ab31 | | | 86 | throw new Error(`cannot serialize argument ${arg}`); |
| b69ab31 | | | 87 | } |
| b69ab31 | | | 88 | |
| b69ab31 | | | 89 | export function serializeToString(data: Serializable): string { |
| b69ab31 | | | 90 | return JSON.stringify(serialize(data)); |
| b69ab31 | | | 91 | } |
| b69ab31 | | | 92 | |
| b69ab31 | | | 93 | /** |
| b69ab31 | | | 94 | * Restore function arguments/return value after deserializing. This lets you recover passed Map/Set/Date/Error during remote transport. |
| b69ab31 | | | 95 | */ |
| b69ab31 | | | 96 | export function deserialize(arg: Serialized): Serializable { |
| b69ab31 | | | 97 | if (typeof arg !== 'object' || arg == null) { |
| b69ab31 | | | 98 | return arg; |
| b69ab31 | | | 99 | } |
| b69ab31 | | | 100 | |
| b69ab31 | | | 101 | if (Array.isArray(arg)) { |
| b69ab31 | | | 102 | return arg.map(a => deserialize(a)); |
| b69ab31 | | | 103 | } |
| b69ab31 | | | 104 | |
| b69ab31 | | | 105 | const specific = arg as CustomSerialized; |
| b69ab31 | | | 106 | switch (specific.__rpcType) { |
| b69ab31 | | | 107 | case 'undefined': |
| b69ab31 | | | 108 | return undefined; |
| b69ab31 | | | 109 | case 'Map': |
| b69ab31 | | | 110 | return new Map(specific.data.map(([key, value]) => [deserialize(key), deserialize(value)])); |
| b69ab31 | | | 111 | case 'Set': |
| b69ab31 | | | 112 | return new Set(specific.data.map(deserialize)); |
| b69ab31 | | | 113 | case 'Error': { |
| b69ab31 | | | 114 | const e = new Error(); |
| b69ab31 | | | 115 | e.stack = specific.data.stack; |
| b69ab31 | | | 116 | e.message = specific.data.message; |
| b69ab31 | | | 117 | return e; |
| b69ab31 | | | 118 | } |
| b69ab31 | | | 119 | case 'Date': |
| b69ab31 | | | 120 | return new Date(specific.data); |
| b69ab31 | | | 121 | case 'object': { |
| b69ab31 | | | 122 | const standardObject = arg as {[key: string]: Serialized}; |
| b69ab31 | | | 123 | const newObj: {[key: string]: Serializable} = {}; |
| b69ab31 | | | 124 | for (const [propertyName, propertyValue] of Object.entries(standardObject)) { |
| b69ab31 | | | 125 | if (propertyName !== '__rpcType') { |
| b69ab31 | | | 126 | newObj[propertyName] = deserialize(propertyValue); |
| b69ab31 | | | 127 | } |
| b69ab31 | | | 128 | } |
| b69ab31 | | | 129 | return newObj; |
| b69ab31 | | | 130 | } |
| b69ab31 | | | 131 | default: { |
| b69ab31 | | | 132 | throw new Error(`cannot deserialize unknown type ${specific}`); |
| b69ab31 | | | 133 | } |
| b69ab31 | | | 134 | } |
| b69ab31 | | | 135 | } |
| b69ab31 | | | 136 | |
| b69ab31 | | | 137 | export function deserializeFromString(data: string): Serializable { |
| b69ab31 | | | 138 | return deserialize(JSON.parse(data)); |
| b69ab31 | | | 139 | } |