refactor: take advantage of structured clone algorithm in the remote module (#20427)

This commit is contained in:
Milan Burda 2019-10-10 15:59:08 +02:00 committed by John Kleinschmidt
parent c2e77e4429
commit b92163d226
13 changed files with 87 additions and 210 deletions

View file

@ -1,37 +0,0 @@
const constructors = new Map([
[Error.name, Error],
[EvalError.name, EvalError],
[RangeError.name, RangeError],
[ReferenceError.name, ReferenceError],
[SyntaxError.name, SyntaxError],
[TypeError.name, TypeError],
[URIError.name, URIError]
])
export function deserialize (error: Electron.SerializedError): Electron.ErrorWithCause {
if (error && error.__ELECTRON_SERIALIZED_ERROR__ && constructors.has(error.name)) {
const constructor = constructors.get(error.name)
const deserializedError = new constructor!(error.message) as Electron.ErrorWithCause
deserializedError.stack = error.stack
deserializedError.from = error.from
deserializedError.cause = exports.deserialize(error.cause)
return deserializedError
}
return error
}
export function serialize (error: Electron.ErrorWithCause): Electron.SerializedError {
if (error instanceof Error) {
// Errors get lost, because: JSON.stringify(new Error('Message')) === {}
// Take the serializable properties and construct a generic object
return {
message: error.message,
stack: error.stack,
name: error.name,
from: process.type as Electron.ProcessType,
cause: exports.serialize(error.cause),
__ELECTRON_SERIALIZED_ERROR__: true
}
}
return error
}

View file

@ -1,71 +0,0 @@
import { Buffer } from 'buffer'
const typedArrays: Record<string, Function> = {
Buffer,
ArrayBuffer,
Int8Array,
Uint8Array,
Uint8ClampedArray,
Int16Array,
Uint16Array,
Int32Array,
Uint32Array,
Float32Array,
Float64Array
}
type BufferLike = Buffer | ArrayBuffer | ArrayBufferView
function getType (value: BufferLike) {
for (const type of Object.keys(typedArrays)) {
if (value instanceof typedArrays[type]) {
return type
}
}
throw new Error('Invalid buffer')
}
function getBuffer (value: BufferLike) {
if (value instanceof Buffer) {
return value
} else if (value instanceof ArrayBuffer) {
return Buffer.from(value)
} else {
return Buffer.from(value.buffer, value.byteOffset, value.byteLength)
}
}
export function isBuffer (value: BufferLike) {
return ArrayBuffer.isView(value) || value instanceof ArrayBuffer
}
export interface BufferMeta {
type: keyof typeof typedArrays;
data: Buffer;
length: number | undefined;
}
export function bufferToMeta (value: BufferLike): BufferMeta {
return {
type: getType(value),
data: getBuffer(value),
// NB. We only use length when decoding Int8Array and friends.
// For other buffer-like types this is expected to be undefined.
length: (value as Buffer).length
}
}
export function metaToBuffer (value: BufferMeta) {
const constructor = typedArrays[value.type]
const data = getBuffer(value.data)
if (constructor === Buffer) {
return data
} else if (constructor === ArrayBuffer) {
return data.buffer
} else if (constructor) {
return new (constructor as any)(data.buffer, data.byteOffset, value.length)
} else {
return data
}
}

View file

@ -10,3 +10,16 @@ export function isPromise (val: any) {
val.constructor.resolve instanceof Function
)
}
const serializableTypes = [
Boolean,
Number,
String,
Date,
RegExp,
ArrayBuffer
]
export function isSerializableObject (value: any) {
return value === null || ArrayBuffer.isView(value) || serializableTypes.some(type => value instanceof type)
}