add proper support for typed arrays in remote (#13055)
This commit is contained in:
parent
a798a40026
commit
4cfe5ecaa4
6 changed files with 174 additions and 22 deletions
58
lib/common/buffer-utils.js
Normal file
58
lib/common/buffer-utils.js
Normal file
|
@ -0,0 +1,58 @@
|
|||
// Note: Don't use destructuring assignment for `Buffer`, or we'll hit a
|
||||
// browserify bug that makes the statement invalid, throwing an error in
|
||||
// sandboxed renderer.
|
||||
const Buffer = require('buffer').Buffer
|
||||
|
||||
const typedArrays = {
|
||||
Buffer,
|
||||
ArrayBuffer,
|
||||
Int8Array,
|
||||
Uint8Array,
|
||||
Uint8ClampedArray,
|
||||
Int16Array,
|
||||
Uint16Array,
|
||||
Int32Array,
|
||||
Uint32Array,
|
||||
Float32Array,
|
||||
Float64Array
|
||||
}
|
||||
|
||||
function getType (value) {
|
||||
for (const type of Object.keys(typedArrays)) {
|
||||
if (value instanceof typedArrays[type]) {
|
||||
return type
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
exports.isBuffer = function (value) {
|
||||
return ArrayBuffer.isView(value) || value instanceof ArrayBuffer
|
||||
}
|
||||
|
||||
exports.bufferToMeta = function (value) {
|
||||
const buffer = (value instanceof ArrayBuffer)
|
||||
? Buffer.from(value)
|
||||
: Buffer.from(value.buffer, value.byteOffset, value.byteLength)
|
||||
|
||||
return {
|
||||
type: getType(value),
|
||||
data: buffer.toString('base64'),
|
||||
length: value.length
|
||||
}
|
||||
}
|
||||
|
||||
exports.metaToBuffer = function (value) {
|
||||
const constructor = typedArrays[value.type]
|
||||
const data = Buffer.from(value.data, 'base64')
|
||||
|
||||
if (constructor === Buffer) {
|
||||
return data
|
||||
} else if (constructor === ArrayBuffer) {
|
||||
return data.buffer
|
||||
} else if (constructor) {
|
||||
return new constructor(data.buffer, data.byteOffset, value.length)
|
||||
} else {
|
||||
return data
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue