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

expose MessageEvent in fetch bundle #2770

Merged
merged 2 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
expose MessageEvent
  • Loading branch information
KhafraDev committed Feb 16, 2024
commit 352c44b2e1250bc78dfcc15189cddc3e13a66f2e
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
131 changes: 131 additions & 0 deletions test/websocket/messageevent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
'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)
})
Loading