Skip to content

Commit

Permalink
expose MessageEvent in fetch bundle (nodejs#2770)
Browse files Browse the repository at this point in the history
* expose MessageEvent

* fixup
  • Loading branch information
KhafraDev authored and crysmags committed Feb 27, 2024
1 parent c4dc93e commit 751ca56
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 0 deletions.
1 change: 1 addition & 0 deletions index-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ module.exports.Response = require('./lib/fetch/response').Response
module.exports.Request = require('./lib/fetch/request').Request

module.exports.WebSocket = require('./lib/websocket/websocket').WebSocket
module.exports.MessageEvent = require('./lib/websocket/events').MessageEvent

module.exports.EventSource = require('./lib/eventsource/eventsource').EventSource
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,11 @@ const { parseMIMEType, serializeAMimeType } = require('./lib/fetch/dataURL')
module.exports.parseMIMEType = parseMIMEType
module.exports.serializeAMimeType = serializeAMimeType

const { CloseEvent, ErrorEvent, MessageEvent } = require('./lib/websocket/events')
module.exports.WebSocket = require('./lib/websocket/websocket').WebSocket
module.exports.CloseEvent = CloseEvent
module.exports.ErrorEvent = ErrorEvent
module.exports.MessageEvent = MessageEvent

module.exports.request = makeDispatcher(api.request)
module.exports.stream = makeDispatcher(api.stream)
Expand Down
136 changes: 136 additions & 0 deletions test/websocket/messageevent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
'use strict'

const { test } = require('node:test')
const assert = require('assert')
const { MessageEvent } = require('../..')

test('test/parallel/test-messageevent-brandcheck.js', () => {
[
'data',
'origin',
'lastEventId',
'source',
'ports'
].forEach((i) => {
assert.throws(() => Reflect.get(MessageEvent.prototype, i, {}), {
constructor: TypeError,
message: 'Illegal invocation'
})
})
})

test('test/parallel/test-worker-message-port.js', () => {
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 }), {
constructor: TypeError,
message: 'MessagePort: Expected 1 to be an instance of MessagePort.'
})
assert.throws(() => new MessageEvent('message', { source: {} }), {
constructor: TypeError,
message: 'MessagePort: Expected [object Object] to be an instance of MessagePort.'
})
assert.throws(() => new MessageEvent('message', { ports: 0 }), {
constructor: TypeError,
message: 'Sequence: Value of type Number is not an Object.'
})
assert.throws(() => new MessageEvent('message', { ports: [null] }), {
constructor: TypeError,
message: 'MessagePort: Expected null to be an instance of MessagePort.'
})
assert.throws(() =>
new MessageEvent('message', { ports: [{}] })
, {
constructor: TypeError,
message: 'MessagePort: Expected [object Object] to be an instance of MessagePort.'
})

assert(new MessageEvent('message') instanceof Event)

// https://github.com/nodejs/node/issues/51767
const event = new MessageEvent('type', { cancelable: true })
event.preventDefault()

assert(event.cancelable)
assert(event.defaultPrevented)
})

test('bug in node core', () => {
// In node core, this will throw an error.
new MessageEvent('', null) // eslint-disable-line no-new
})

0 comments on commit 751ca56

Please sign in to comment.