-
-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathrpc-link.test.ts
More file actions
182 lines (136 loc) Β· 5.16 KB
/
Copy pathrpc-link.test.ts
File metadata and controls
182 lines (136 loc) Β· 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { sleep } from '@orpc/shared'
import { decodePeerMessage, encodePeerMessage } from '@standardserver/peer'
import { createORPCClient } from '../../client'
import { RPCLink } from './rpc-link'
describe('rpcLink', () => {
beforeEach(() => {
vi.clearAllMocks()
})
let onMessage: any
let onClose: any
const createPort = () => {
const port = {
addEventListener: vi.fn((event: string, callback: any) => {
if (event === 'message')
onMessage = callback
if (event === 'close')
onClose = callback
}),
postMessage: vi.fn(),
}
return port
}
const createResponseMessage = async ({
id,
body = { json: 'pong' },
status = 200,
prefix,
}: { id: string, body?: unknown, status?: number, prefix?: string }) => {
return encodePeerMessage({
id,
kind: 'response',
json: { body, status, headers: {} },
}, prefix ? { prefix } : undefined)
}
const decodeRequest = (sent: any, prefix?: string) => {
return decodePeerMessage(sent, prefix ? { prefix } : undefined) as {
matched: true
message: { id: string, kind: string, json: any }
}
}
it.each([
['string', async (encoded: string | Uint8Array) => encoded],
['bytes', async (encoded: string | Uint8Array) => new TextEncoder().encode(encoded as string)],
])('handles %s response', async (_type, transform) => {
const port = createPort()
const orpc = createORPCClient(new RPCLink({ port })) as any
const promise = expect(orpc.ping('input')).resolves.toEqual('pong')
await vi.waitFor(() => expect(port.postMessage).toHaveBeenCalledTimes(1))
const decoded = decodeRequest(port.postMessage.mock.calls[0]![0])
expect(decoded.matched).toBe(true)
expect(decoded.message.kind).toBe('request')
expect(decoded.message.id).toBeTypeOf('string')
expect(decoded.message.json).toEqual({
url: '/ping',
body: { json: 'input' },
})
const raw = await createResponseMessage({ id: decoded.message.id })
onMessage({ data: await transform(raw) })
await promise
})
it('aborts pending requests on close', async () => {
const port = createPort()
const orpc = createORPCClient(new RPCLink({ port })) as any
const promise = expect(orpc.ping('input')).rejects.toThrow()
await sleep(0)
onClose()
await promise
})
it('can encode messages with prefix', async () => {
const port = createPort()
const orpc = createORPCClient(new RPCLink({
port,
encodePeerMessage: { prefix: 'orpc:' },
})) as any
const promise = expect(orpc.ping('input')).resolves.toEqual('pong')
await vi.waitFor(() => expect(port.postMessage).toHaveBeenCalledTimes(1))
const decoded = decodeRequest(port.postMessage.mock.calls[0]![0], 'orpc:')
expect(decoded.matched).toBe(true)
expect(decoded.message.kind).toBe('request')
onMessage({ data: await createResponseMessage({ id: decoded.message.id }) })
await promise
})
it('can decode messages with prefix and ignore messages with mismatched prefix', async () => {
const port = createPort()
const orpc = createORPCClient(new RPCLink({
port,
decodePeerMessage: { prefix: 'orpc:' },
})) as any
const promise = expect(orpc.ping('input')).resolves.toEqual('pong')
await vi.waitFor(() => expect(port.postMessage).toHaveBeenCalledTimes(1))
const decoded = decodeRequest(port.postMessage.mock.calls[0]![0])
const id = decoded.message.id
// Message with wrong prefix β should be ignored
onMessage({ data: await createResponseMessage({ id, prefix: 'wrong:' }) })
// Correct message β should be processed
onMessage({ data: await createResponseMessage({ id, prefix: 'orpc:' }) })
await promise
})
it('can receive and send un-encoded messages with transfer option (structured clone)', async () => {
const port = createPort()
const transferable = new Uint8Array([1, 2, 3]).buffer
const transfer = vi.fn(async () => [transferable])
const orpc = createORPCClient(new RPCLink({
port,
experimental_transfer: transfer,
})) as any
const promise = expect(orpc.ping('input')).resolves.toEqual('pong')
await vi.waitFor(() => expect(port.postMessage).toHaveBeenCalledTimes(1))
const message = port.postMessage.mock.calls[0]![0]
expect(port.postMessage).toHaveBeenCalledWith(
message,
[transferable],
)
onMessage({
data: {
id: message.id,
kind: 'response',
json: { body: { json: 'pong' }, status: 200, headers: {} },
},
})
await promise
})
it('ignore invalid messages', async () => {
const port = createPort()
const orpc = createORPCClient(new RPCLink({ port })) as any
const promise = expect(orpc.ping('input')).resolves.toEqual('pong')
await vi.waitFor(() => expect(port.postMessage).toHaveBeenCalledTimes(1))
const decoded = decodeRequest(port.postMessage.mock.calls[0]![0])
const id = decoded.message.id
// Invalid message β should be ignored
onMessage({ data: { invalid: true } })
// Correct message β should be processed
onMessage({ data: await createResponseMessage({ id }) })
await promise
})
})