-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
42 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import * as WalkValue from '../WalkValue/WalkValue.ts' | ||
import * as IsTransferrable from '../IsTransferrable/IsTransferrable.ts' | ||
|
||
export const getTransferrables = (value: unknown): any => { | ||
const transferrables: Transferable[] = [] | ||
WalkValue.walkValue(value, transferrables) | ||
WalkValue.walkValue(value, transferrables, IsTransferrable.isTransferrable) | ||
return transferrables | ||
} |
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,9 @@ | ||
import * as GetTransferrables from '../GetTransferrables/GetTransferrables.ts' | ||
|
||
export const getTransferrablesNode = (value: unknown): any => { | ||
const transferrables = GetTransferrables.getTransferrables(value) | ||
if (transferrables.length === 0) { | ||
throw new Error(`no transferrables found`) | ||
} | ||
return transferrables[0] | ||
} |
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 |
---|---|---|
@@ -1,25 +1,21 @@ | ||
import * as IsTransferrable from '../IsTransferrable/IsTransferrable.ts' | ||
|
||
export const walkValue = (value: unknown, transferrables: unknown[]) => { | ||
export const walkValue = (value: unknown, transferrables: unknown[], isTransferrable: (value: any) => boolean) => { | ||
if (!value) { | ||
return value | ||
return | ||
} | ||
if (IsTransferrable.isTransferrable(value)) { | ||
if (isTransferrable(value)) { | ||
transferrables.push(value) | ||
return | ||
} | ||
if (Array.isArray(value)) { | ||
for (const item of value) { | ||
walkValue(item, transferrables) | ||
walkValue(item, transferrables, isTransferrable) | ||
} | ||
return | ||
} | ||
if (typeof value === 'object') { | ||
const newObject = Object.create(null) | ||
for (const property of Object.values(value)) { | ||
walkValue(property, transferrables) | ||
walkValue(property, transferrables, isTransferrable) | ||
} | ||
return newObject | ||
return | ||
} | ||
return value | ||
} |
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,19 @@ | ||
import { expect, test } from '@jest/globals' | ||
import * as GetTransferrablesNode from '../src/parts/GetTransferrablesNode/GetTransferrablesNode.js' | ||
|
||
test('socket inside object', () => { | ||
class Socket {} | ||
const socket = new Socket() | ||
const message = { | ||
jsonrpc: '2.0', | ||
method: 'HandleWebSocket.handleWebSocket', | ||
params: [socket], | ||
id: 1, | ||
} | ||
expect(GetTransferrablesNode.getTransferrablesNode(message)).toBe(socket) | ||
}) | ||
|
||
test('no transferrables', () => { | ||
const value: any[] = [] | ||
expect(() => GetTransferrablesNode.getTransferrablesNode(value)).toThrow(new Error('no transferrables found')) | ||
}) |