mirror of
https://github.com/electron/electron.git
synced 2026-01-28 08:48:14 -05:00
* refactor: correctly serialize nativeImage/buffer with typeUtils * test: add serialization specs * fix: construct from dataURL * test: test for dataURL specificity
81 lines
2.4 KiB
TypeScript
81 lines
2.4 KiB
TypeScript
const { nativeImage, NativeImage } = process.electronBinding('native_image');
|
|
|
|
export function isPromise (val: any) {
|
|
return (
|
|
val &&
|
|
val.then &&
|
|
val.then instanceof Function &&
|
|
val.constructor &&
|
|
val.constructor.reject &&
|
|
val.constructor.reject instanceof Function &&
|
|
val.constructor.resolve &&
|
|
val.constructor.resolve instanceof Function
|
|
);
|
|
}
|
|
|
|
const serializableTypes = [
|
|
Boolean,
|
|
Number,
|
|
String,
|
|
Date,
|
|
Error,
|
|
RegExp,
|
|
ArrayBuffer,
|
|
NativeImage
|
|
];
|
|
|
|
export function isSerializableObject (value: any) {
|
|
return value === null || ArrayBuffer.isView(value) || serializableTypes.some(type => value instanceof type);
|
|
}
|
|
|
|
const objectMap = function (source: Object, mapper: (value: any) => any) {
|
|
const sourceEntries = Object.entries(source);
|
|
const targetEntries = sourceEntries.map(([key, val]) => [key, mapper(val)]);
|
|
return Object.fromEntries(targetEntries);
|
|
};
|
|
|
|
export function serialize (value: any): any {
|
|
if (value instanceof NativeImage) {
|
|
const representations = [];
|
|
for (const scaleFactor of value.getScaleFactors()) {
|
|
const size = value.getSize(scaleFactor);
|
|
const dataURL = value.toDataURL({ scaleFactor });
|
|
representations.push({ scaleFactor, size, dataURL });
|
|
}
|
|
return { __ELECTRON_SERIALIZED_NativeImage__: true, representations };
|
|
} else if (value instanceof Buffer) {
|
|
return { __ELECTRON_SERIALIZED_Buffer__: true, data: value };
|
|
} else if (Array.isArray(value)) {
|
|
return value.map(serialize);
|
|
} else if (isSerializableObject(value)) {
|
|
return value;
|
|
} else if (value instanceof Object) {
|
|
return objectMap(value, serialize);
|
|
} else {
|
|
return value;
|
|
}
|
|
}
|
|
|
|
export function deserialize (value: any): any {
|
|
if (value && value.__ELECTRON_SERIALIZED_NativeImage__) {
|
|
const image = nativeImage.createEmpty();
|
|
for (const rep of value.representations) {
|
|
const { size, scaleFactor, dataURL } = rep;
|
|
const { width, height } = size;
|
|
image.addRepresentation({ dataURL, scaleFactor, width, height });
|
|
}
|
|
return image;
|
|
} else if (value && value.__ELECTRON_SERIALIZED_Buffer__) {
|
|
const { buffer, byteOffset, byteLength } = value.data;
|
|
return Buffer.from(buffer, byteOffset, byteLength);
|
|
} else if (Array.isArray(value)) {
|
|
return value.map(deserialize);
|
|
} else if (isSerializableObject(value)) {
|
|
return value;
|
|
} else if (value instanceof Object) {
|
|
return objectMap(value, deserialize);
|
|
} else {
|
|
return value;
|
|
}
|
|
}
|