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

fix: don't overwrite global constructor names in remote #20637

Merged
merged 2 commits into from Oct 21, 2019
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
16 changes: 14 additions & 2 deletions lib/browser/remote/server.ts
Expand Up @@ -245,6 +245,17 @@ type MetaTypeFromRenderer = {
length: number
}

const fakeConstructor = (constructor: Function, name: string) =>
new Proxy(Object, {
get (target, prop, receiver) {
if (prop === 'name') {
return name
} else {
return Reflect.get(target, prop, receiver)
}
}
})

// Convert array of meta data from renderer into array of real values.
const unwrapArgs = function (sender: electron.WebContents, frameId: number, contextId: string, args: any[]) {
const metaToValue = function (meta: MetaTypeFromRenderer): any {
Expand All @@ -262,8 +273,9 @@ const unwrapArgs = function (sender: electron.WebContents, frameId: number, cont
then: metaToValue(meta.then)
})
case 'object': {
const ret: any = {}
Object.defineProperty(ret.constructor, 'name', { value: meta.name })
const ret: any = meta.name !== 'Object' ? Object.create({
constructor: fakeConstructor(Object, meta.name)
}) : {}

for (const { name, value } of meta.members) {
ret[name] = metaToValue(value)
Expand Down
5 changes: 5 additions & 0 deletions lib/renderer/api/remote.js
Expand Up @@ -64,6 +64,11 @@ function wrapArgs (args, visited = new Set()) {
type: 'remote-object',
id: v8Util.getHiddenValue(value, 'atomId')
}
} else if (value instanceof Error) {
return {
type: 'value',
value
}
}

const meta = {
Expand Down
15 changes: 7 additions & 8 deletions spec/api-remote-spec.js
Expand Up @@ -498,17 +498,16 @@ ifdescribe(features.isRemoteModuleEnabled())('remote module', () => {
it('throws errors from the main process', () => {
expect(() => {
throwFunction()
}).to.throw()
}).to.throw(/undefined/)
})

it('throws custom errors from the main process', () => {
const err = new Error('error')
err.cause = new Error('cause')
err.prop = 'error prop'
it('tracks error cause', () => {
try {
throwFunction(err)
} catch (error) {
expect(error.cause).to.deep.equal(...resolveGetters(err))
throwFunction(new Error('error from main'))
expect.fail()
} catch (e) {
expect(e.message).to.match(/Could not call remote function/)
expect(e.cause.message).to.equal('error from main')
}
})
})
Expand Down