Skip to content

Commit

Permalink
feature: improve error message when no transferrables are transferred (
Browse files Browse the repository at this point in the history
  • Loading branch information
levivilet committed Sep 7, 2024
1 parent a7ed52b commit a5992e4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/parts/FixNodeParameters/FixNodeParameters.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as GetTransferrables from '../GetTransferrables/GetTransferrables.ts'
import { IpcError } from '../IpcError/IpcError.ts'
import * as RemoveValues from '../RemoveValues/RemoveValues.ts'

// workaround for node not supporting transferrable objects
Expand All @@ -7,7 +8,7 @@ import * as RemoveValues from '../RemoveValues/RemoveValues.ts'
export const fixNodeParameters = (value: unknown) => {
const transfer = GetTransferrables.getTransferrables(value)
if (transfer.length === 0) {
throw new Error('no transferrables found')
throw new IpcError('no transferrables found')
}
const newValue = RemoveValues.removeValues(value, transfer)
return {
Expand Down
27 changes: 27 additions & 0 deletions test/FixNodeParameters.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { expect, test } from '@jest/globals'
import * as FixNodeParameters from '../src/parts/FixNodeParameters/FixNodeParameters.ts'

test('error - no transferrables found', () => {
const value = {
jsonrpc: '2.0',
method: 'CreateMessagePort.createMessagePort',
params: [],
}
expect(() => FixNodeParameters.fixNodeParameters(value)).toThrow(new Error('no transferrables found'))
})

test('transfer messagePort', () => {
const { port1 } = new MessageChannel()
const value = {
jsonrpc: '2.0',
method: 'CreateMessagePort.createMessagePort',
params: [, port1],
}
const { newValue, transfer } = FixNodeParameters.fixNodeParameters(value)
expect(newValue).toEqual({
jsonrpc: '2.0',
method: 'CreateMessagePort.createMessagePort',
params: [],
})
expect(transfer).toBe(port1)
})

0 comments on commit a5992e4

Please sign in to comment.