Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for TypedArrays in IPC. #6572

Merged
merged 4 commits into from Jul 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions filenames.gypi
Expand Up @@ -44,6 +44,7 @@
'lib/common/api/deprecate.js',
'lib/common/api/deprecations.js',
'lib/common/api/is-promise.js',
'lib/common/api/is-typed-array.js',
'lib/common/api/exports/electron.js',
'lib/common/api/native-image.js',
'lib/common/api/shell.js',
Expand Down
6 changes: 5 additions & 1 deletion lib/browser/rpc-server.js
Expand Up @@ -2,7 +2,7 @@

const electron = require('electron')
const v8Util = process.atomBinding('v8_util')
const {ipcMain, isPromise, webContents} = electron
const {ipcMain, isPromise, isTypedArray, webContents} = electron

const objectsRegistry = require('./objects-registry')

Expand Down Expand Up @@ -63,6 +63,8 @@ let valueToMeta = function (sender, value, optimizeSimpleObject = false) {
meta.type = 'buffer'
} else if (Array.isArray(value)) {
meta.type = 'array'
} else if (isTypedArray(value)) {
meta.type = 'typed-array'
} else if (value instanceof Error) {
meta.type = 'error'
} else if (value instanceof Date) {
Expand Down Expand Up @@ -149,6 +151,8 @@ const unwrapArgs = function (sender, args) {
return unwrapArgs(sender, meta.value)
case 'buffer':
return new Buffer(meta.value)
case 'typed-array':
return Buffer.from(meta.value)
case 'date':
return new Date(meta.value)
case 'promise':
Expand Down
5 changes: 5 additions & 0 deletions lib/common/api/exports/electron.js
Expand Up @@ -48,6 +48,11 @@ exports.defineProperties = function (exports) {
get: function () {
return require('../is-promise')
}
},
isTypedArray: {
get: function () {
return require('../is-typed-array')
}
}
})
}
17 changes: 17 additions & 0 deletions lib/common/api/is-typed-array.js
@@ -0,0 +1,17 @@
'use strict'

module.exports = function isTypedArray (val) {
return (
val &&
(val instanceof Int8Array ||
val instanceof Int16Array ||
val instanceof Int32Array ||
val instanceof Uint8Array ||
val instanceof Uint8ClampedArray ||
val instanceof Uint16Array ||
val instanceof Uint32Array ||
val instanceof Float32Array ||
val instanceof Float64Array) ||
(Object.prototype.toString.call(val).substr(-6, 5) === 'Array')
)
}
9 changes: 8 additions & 1 deletion lib/renderer/api/remote.js
@@ -1,7 +1,7 @@
'use strict'

const v8Util = process.atomBinding('v8_util')
const {ipcRenderer, isPromise, CallbacksRegistry} = require('electron')
const {ipcRenderer, isPromise, isTypedArray, CallbacksRegistry} = require('electron')

const callbacksRegistry = new CallbacksRegistry()

Expand Down Expand Up @@ -35,6 +35,11 @@ const wrapArgs = function (args, visited) {
type: 'buffer',
value: Array.prototype.slice.call(value, 0)
}
} else if (isTypedArray(value)) {
return {
type: 'typed-array',
value: Array.prototype.slice.call(value)
}
} else if (value instanceof Date) {
return {
type: 'date',
Expand Down Expand Up @@ -164,6 +169,8 @@ const metaToValue = function (meta) {
return results
case 'buffer':
return new Buffer(meta.value)
case 'typed-array':
return Buffer.from(meta.value)
case 'promise':
return Promise.resolve({
then: metaToValue(meta.then)
Expand Down