-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
worker: make MessageEvent class more Web-compatible
PR-URL: #35496 Fixes: #35495 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Myles Borins <myles.borins@gmail.com> Reviewed-By: Shelley Vohr <codebytere@gmail.com> Reviewed-By: Michael Dawson <midawson@redhat.com> Reviewed-By: Khaidi Chu <i@2333.moe>
- Loading branch information
1 parent
828518c
commit 705d888
Showing
4 changed files
with
154 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Flags: --expose-internals | ||
'use strict'; | ||
require('../common'); | ||
const assert = require('assert'); | ||
const { MessageEvent, MessageChannel } = require('internal/worker/io'); | ||
|
||
const dummyPort = new MessageChannel().port1; | ||
|
||
{ | ||
for (const [ args, expected ] of [ | ||
[ | ||
['message'], | ||
{ | ||
type: 'message', data: null, origin: '', | ||
lastEventId: '', source: null, ports: [] | ||
} | ||
], | ||
[ | ||
['message', { data: undefined, origin: 'foo' }], | ||
{ | ||
type: 'message', data: null, origin: 'foo', | ||
lastEventId: '', source: null, ports: [] | ||
} | ||
], | ||
[ | ||
['message', { data: 2, origin: 1, lastEventId: 0 }], | ||
{ | ||
type: 'message', data: 2, origin: '1', | ||
lastEventId: '0', source: null, ports: [] | ||
} | ||
], | ||
[ | ||
['message', { lastEventId: 'foo' }], | ||
{ | ||
type: 'message', data: null, origin: '', | ||
lastEventId: 'foo', source: null, ports: [] | ||
} | ||
], | ||
[ | ||
['messageerror', { lastEventId: 'foo', source: dummyPort }], | ||
{ | ||
type: 'messageerror', data: null, origin: '', | ||
lastEventId: 'foo', source: dummyPort, ports: [] | ||
} | ||
], | ||
[ | ||
['message', { ports: [dummyPort], source: null }], | ||
{ | ||
type: 'message', data: null, origin: '', | ||
lastEventId: '', source: null, ports: [dummyPort] | ||
} | ||
], | ||
]) { | ||
const ev = new MessageEvent(...args); | ||
const { type, data, origin, lastEventId, source, ports } = ev; | ||
assert.deepStrictEqual(expected, { | ||
type, data, origin, lastEventId, source, ports | ||
}); | ||
} | ||
} | ||
|
||
{ | ||
assert.throws(() => { | ||
new MessageEvent('message', { source: 1 }); | ||
}, { | ||
code: 'ERR_INVALID_ARG_TYPE', | ||
message: /The "init\.source" property must be an instance of MessagePort/, | ||
}); | ||
assert.throws(() => { | ||
new MessageEvent('message', { source: {} }); | ||
}, { | ||
code: 'ERR_INVALID_ARG_TYPE', | ||
message: /The "init\.source" property must be an instance of MessagePort/, | ||
}); | ||
assert.throws(() => { | ||
new MessageEvent('message', { ports: 0 }); | ||
}, { | ||
message: /ports is not iterable/, | ||
}); | ||
assert.throws(() => { | ||
new MessageEvent('message', { ports: [ null ] }); | ||
}, { | ||
code: 'ERR_INVALID_ARG_TYPE', | ||
message: /The "init\.ports\[0\]" property must be an instance of MessagePort/, | ||
}); | ||
assert.throws(() => { | ||
new MessageEvent('message', { ports: [ {} ] }); | ||
}, { | ||
code: 'ERR_INVALID_ARG_TYPE', | ||
message: /The "init\.ports\[0\]" property must be an instance of MessagePort/, | ||
}); | ||
} |