@nmtjs/protocol 0.15.0-beta.3 → 0.15.0-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +10 -9
- package/src/client/format.ts +49 -0
- package/src/client/index.ts +8 -0
- package/src/client/protocol.ts +107 -0
- package/src/client/stream.ts +222 -0
- package/src/client/versions/v1.ts +205 -0
- package/src/common/binary.ts +70 -0
- package/src/common/blob.ts +99 -0
- package/src/common/constants.ts +2 -0
- package/src/common/enums.ts +62 -0
- package/src/common/index.ts +6 -0
- package/src/common/types.ts +18 -0
- package/src/common/utils.ts +12 -0
- package/src/server/format.ts +113 -0
- package/src/server/index.ts +10 -0
- package/src/server/protocol.ts +97 -0
- package/src/server/stream.ts +51 -0
- package/src/server/types.ts +42 -0
- package/src/server/utils.ts +22 -0
- package/src/server/versions/v1.ts +198 -0
package/package.json
CHANGED
|
@@ -8,25 +8,26 @@
|
|
|
8
8
|
},
|
|
9
9
|
"dependencies": {
|
|
10
10
|
"hookable": "6.0.0-rc.1",
|
|
11
|
-
"@nmtjs/common": "0.15.0-beta.
|
|
12
|
-
"@nmtjs/type": "0.15.0-beta.
|
|
13
|
-
"@nmtjs/contract": "0.15.0-beta.
|
|
11
|
+
"@nmtjs/common": "0.15.0-beta.4",
|
|
12
|
+
"@nmtjs/type": "0.15.0-beta.4",
|
|
13
|
+
"@nmtjs/contract": "0.15.0-beta.4"
|
|
14
14
|
},
|
|
15
15
|
"devDependencies": {
|
|
16
|
-
"@nmtjs/core": "0.15.0-beta.
|
|
16
|
+
"@nmtjs/core": "0.15.0-beta.4"
|
|
17
17
|
},
|
|
18
18
|
"peerDependencies": {
|
|
19
|
-
"@nmtjs/common": "0.15.0-beta.
|
|
20
|
-
"@nmtjs/
|
|
21
|
-
"@nmtjs/
|
|
22
|
-
"@nmtjs/
|
|
19
|
+
"@nmtjs/common": "0.15.0-beta.4",
|
|
20
|
+
"@nmtjs/contract": "0.15.0-beta.4",
|
|
21
|
+
"@nmtjs/type": "0.15.0-beta.4",
|
|
22
|
+
"@nmtjs/core": "0.15.0-beta.4"
|
|
23
23
|
},
|
|
24
24
|
"files": [
|
|
25
25
|
"dist",
|
|
26
|
+
"src",
|
|
26
27
|
"LICENSE.md",
|
|
27
28
|
"README.md"
|
|
28
29
|
],
|
|
29
|
-
"version": "0.15.0-beta.
|
|
30
|
+
"version": "0.15.0-beta.4",
|
|
30
31
|
"scripts": {
|
|
31
32
|
"clean-build": "rm -rf ./dist",
|
|
32
33
|
"build": "tsc --declaration --sourcemap",
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { ProtocolBlob } from '../common/blob.ts'
|
|
2
|
+
import type { DecodeRPCContext } from '../common/types.ts'
|
|
3
|
+
import type {
|
|
4
|
+
ProtocolClientBlobStream,
|
|
5
|
+
ProtocolServerBlobStream,
|
|
6
|
+
} from './stream.ts'
|
|
7
|
+
|
|
8
|
+
export interface EncodeRPCContext<T = any> {
|
|
9
|
+
addStream: (blob: ProtocolBlob) => T
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export type ProtocolRPCEncode = ArrayBufferView
|
|
13
|
+
|
|
14
|
+
export interface BaseClientDecoder {
|
|
15
|
+
decode(buffer: ArrayBufferView): unknown
|
|
16
|
+
decodeRPC(
|
|
17
|
+
buffer: ArrayBufferView,
|
|
18
|
+
context: DecodeRPCContext<
|
|
19
|
+
(options?: { signal?: AbortSignal }) => ProtocolServerBlobStream
|
|
20
|
+
>,
|
|
21
|
+
): unknown
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface BaseClientEncoder {
|
|
25
|
+
encode(data: unknown): ArrayBufferView
|
|
26
|
+
encodeRPC(
|
|
27
|
+
data: unknown,
|
|
28
|
+
context: EncodeRPCContext<ProtocolClientBlobStream>,
|
|
29
|
+
): ProtocolRPCEncode
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export abstract class BaseClientFormat
|
|
33
|
+
implements BaseClientDecoder, BaseClientEncoder
|
|
34
|
+
{
|
|
35
|
+
abstract contentType: string
|
|
36
|
+
|
|
37
|
+
abstract encode(data: unknown): ArrayBufferView
|
|
38
|
+
abstract encodeRPC(
|
|
39
|
+
data: unknown,
|
|
40
|
+
context: EncodeRPCContext,
|
|
41
|
+
): ProtocolRPCEncode
|
|
42
|
+
abstract decode(buffer: ArrayBufferView): unknown
|
|
43
|
+
abstract decodeRPC(
|
|
44
|
+
buffer: ArrayBufferView,
|
|
45
|
+
context: DecodeRPCContext<
|
|
46
|
+
(options?: { signal?: AbortSignal }) => ProtocolServerBlobStream
|
|
47
|
+
>,
|
|
48
|
+
): unknown
|
|
49
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from './format.ts'
|
|
2
|
+
export * from './protocol.ts'
|
|
3
|
+
export * from './stream.ts'
|
|
4
|
+
|
|
5
|
+
import { ProtocolVersion } from '../common/enums.ts'
|
|
6
|
+
import { ProtocolVersion1 } from './versions/v1.ts'
|
|
7
|
+
|
|
8
|
+
export const versions = { [ProtocolVersion.v1]: new ProtocolVersion1() }
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import type { ProtocolBlob, ProtocolBlobMetadata } from '../common/blob.ts'
|
|
2
|
+
import type {
|
|
3
|
+
ClientMessageType,
|
|
4
|
+
ProtocolVersion,
|
|
5
|
+
ServerMessageType,
|
|
6
|
+
} from '../common/enums.ts'
|
|
7
|
+
import type { BaseProtocolError, EncodeRPCStreams } from '../common/types.ts'
|
|
8
|
+
import type { BaseClientDecoder, BaseClientEncoder } from './format.ts'
|
|
9
|
+
import type {
|
|
10
|
+
ProtocolClientBlobStream,
|
|
11
|
+
ProtocolServerBlobStream,
|
|
12
|
+
} from './stream.ts'
|
|
13
|
+
import { concat } from '../common/binary.ts'
|
|
14
|
+
|
|
15
|
+
export type MessageContext = {
|
|
16
|
+
decoder: BaseClientDecoder
|
|
17
|
+
encoder: BaseClientEncoder
|
|
18
|
+
addClientStream: (blob: ProtocolBlob) => ProtocolClientBlobStream
|
|
19
|
+
addServerStream: (
|
|
20
|
+
streamId: number,
|
|
21
|
+
metadata: ProtocolBlobMetadata,
|
|
22
|
+
) => (options?: { signal?: AbortSignal }) => ProtocolServerBlobStream
|
|
23
|
+
transport: { send: (buffer: ArrayBufferView) => void }
|
|
24
|
+
streamId: () => number
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export type ClientMessageTypePayload = {
|
|
28
|
+
[ClientMessageType.Rpc]: { callId: number; procedure: string; payload: any }
|
|
29
|
+
[ClientMessageType.RpcAbort]: { callId: number; reason?: string }
|
|
30
|
+
[ClientMessageType.RpcPull]: { callId: number }
|
|
31
|
+
[ClientMessageType.ClientStreamPush]: {
|
|
32
|
+
streamId: number
|
|
33
|
+
chunk: ArrayBufferView
|
|
34
|
+
}
|
|
35
|
+
[ClientMessageType.ClientStreamEnd]: { streamId: number }
|
|
36
|
+
[ClientMessageType.ClientStreamAbort]: { streamId: number; reason?: string }
|
|
37
|
+
[ClientMessageType.ServerStreamPull]: { streamId: number; size: number }
|
|
38
|
+
[ClientMessageType.ServerStreamAbort]: { streamId: number; reason?: string }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export type ServerMessageTypePayload = {
|
|
42
|
+
[ServerMessageType.RpcResponse]: {
|
|
43
|
+
callId: number
|
|
44
|
+
result?: any
|
|
45
|
+
error?: BaseProtocolError
|
|
46
|
+
streams?: EncodeRPCStreams
|
|
47
|
+
}
|
|
48
|
+
[ServerMessageType.RpcStreamResponse]: {
|
|
49
|
+
callId: number
|
|
50
|
+
error?: BaseProtocolError
|
|
51
|
+
}
|
|
52
|
+
[ServerMessageType.RpcStreamChunk]: { callId: number; chunk: ArrayBufferView }
|
|
53
|
+
[ServerMessageType.RpcStreamEnd]: { callId: number }
|
|
54
|
+
[ServerMessageType.RpcStreamAbort]: { callId: number; reason?: string }
|
|
55
|
+
[ServerMessageType.ServerStreamAbort]: { streamId: number; reason?: string }
|
|
56
|
+
[ServerMessageType.ServerStreamEnd]: { streamId: number }
|
|
57
|
+
[ServerMessageType.ServerStreamPush]: {
|
|
58
|
+
streamId: number
|
|
59
|
+
chunk: ArrayBufferView
|
|
60
|
+
}
|
|
61
|
+
[ServerMessageType.ClientStreamAbort]: { streamId: number; reason?: string }
|
|
62
|
+
[ServerMessageType.ClientStreamPull]: { streamId: number; size: number }
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export abstract class ProtocolVersionInterface {
|
|
66
|
+
abstract version: ProtocolVersion
|
|
67
|
+
abstract decodeMessage(
|
|
68
|
+
context: MessageContext,
|
|
69
|
+
buffer: ArrayBufferView,
|
|
70
|
+
): {
|
|
71
|
+
[K in keyof ServerMessageTypePayload]: {
|
|
72
|
+
type: K
|
|
73
|
+
} & ServerMessageTypePayload[K]
|
|
74
|
+
}[keyof ServerMessageTypePayload]
|
|
75
|
+
abstract encodeMessage<T extends ClientMessageType>(
|
|
76
|
+
context: MessageContext,
|
|
77
|
+
messageType: T,
|
|
78
|
+
payload: ClientMessageTypePayload[T],
|
|
79
|
+
): any
|
|
80
|
+
|
|
81
|
+
protected encode(...chunks: (ArrayBuffer | ArrayBufferView)[]) {
|
|
82
|
+
return concat(...chunks)
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export class ProtocolError extends Error implements BaseProtocolError {
|
|
87
|
+
code: string
|
|
88
|
+
data?: any
|
|
89
|
+
|
|
90
|
+
constructor(code: string, message?: string, data?: any) {
|
|
91
|
+
super(message)
|
|
92
|
+
this.code = code
|
|
93
|
+
this.data = data
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
get message() {
|
|
97
|
+
return `${this.code} ${super.message}`
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
toString() {
|
|
101
|
+
return `${this.code} ${this.message}`
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
toJSON() {
|
|
105
|
+
return { code: this.code, message: this.message, data: this.data }
|
|
106
|
+
}
|
|
107
|
+
}
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import type { Callback, DuplexStreamOptions } from '@nmtjs/common'
|
|
2
|
+
import { DuplexStream } from '@nmtjs/common'
|
|
3
|
+
|
|
4
|
+
import type {
|
|
5
|
+
ProtocolBlobInterface,
|
|
6
|
+
ProtocolBlobMetadata,
|
|
7
|
+
} from '../common/blob.ts'
|
|
8
|
+
import { concat, decodeText, encodeText } from '../common/binary.ts'
|
|
9
|
+
import { kBlobKey } from '../common/constants.ts'
|
|
10
|
+
|
|
11
|
+
export class ProtocolClientBlobStream
|
|
12
|
+
extends DuplexStream<any, ArrayBufferView>
|
|
13
|
+
implements ProtocolBlobInterface
|
|
14
|
+
{
|
|
15
|
+
readonly [kBlobKey] = true
|
|
16
|
+
|
|
17
|
+
#queue: Uint8Array
|
|
18
|
+
#reader: ReadableStreamDefaultReader
|
|
19
|
+
#sourceReader: ReadableStreamDefaultReader | null = null
|
|
20
|
+
|
|
21
|
+
constructor(
|
|
22
|
+
readonly source: ReadableStream,
|
|
23
|
+
readonly id: number,
|
|
24
|
+
readonly metadata: ProtocolBlobMetadata,
|
|
25
|
+
) {
|
|
26
|
+
let sourceReader: ReadableStreamDefaultReader | null = null
|
|
27
|
+
super({
|
|
28
|
+
start: () => {
|
|
29
|
+
sourceReader = source.getReader()
|
|
30
|
+
},
|
|
31
|
+
pull: async (controller) => {
|
|
32
|
+
const { done, value } = await sourceReader!.read()
|
|
33
|
+
if (done) {
|
|
34
|
+
controller.close()
|
|
35
|
+
return
|
|
36
|
+
}
|
|
37
|
+
const chunk = value
|
|
38
|
+
controller.enqueue(
|
|
39
|
+
chunk instanceof Uint8Array
|
|
40
|
+
? chunk
|
|
41
|
+
: new Uint8Array(chunk.buffer, chunk.byteOffset, chunk.byteLength),
|
|
42
|
+
)
|
|
43
|
+
},
|
|
44
|
+
transform: (chunk) => {
|
|
45
|
+
if (chunk instanceof ArrayBuffer) {
|
|
46
|
+
return new Uint8Array(chunk)
|
|
47
|
+
} else if (chunk instanceof Uint8Array) {
|
|
48
|
+
return chunk
|
|
49
|
+
} else if (typeof chunk === 'string') {
|
|
50
|
+
return encodeText(chunk)
|
|
51
|
+
} else {
|
|
52
|
+
throw new Error(
|
|
53
|
+
'Invalid chunk data type. Expected ArrayBuffer, Uint8Array, or string.',
|
|
54
|
+
)
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
cancel: (reason) => {
|
|
58
|
+
// Use reader.cancel() if reader exists (stream is locked), otherwise source.cancel()
|
|
59
|
+
if (sourceReader) {
|
|
60
|
+
sourceReader.cancel(reason)
|
|
61
|
+
} else {
|
|
62
|
+
source.cancel(reason)
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
this.#queue = new Uint8Array(0)
|
|
68
|
+
this.#reader = this.readable.getReader()
|
|
69
|
+
this.#sourceReader = sourceReader
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async abort(reason = 'Stream aborted') {
|
|
73
|
+
await this.#reader.cancel(reason)
|
|
74
|
+
this.#reader.releaseLock()
|
|
75
|
+
this.#sourceReader?.releaseLock()
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async end() {
|
|
79
|
+
// Release the reader lock when the stream is finished
|
|
80
|
+
this.#reader.releaseLock()
|
|
81
|
+
this.#sourceReader?.releaseLock()
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async read(size: number) {
|
|
85
|
+
while (this.#queue.byteLength < size) {
|
|
86
|
+
const { done, value } = await this.#reader.read()
|
|
87
|
+
if (done) {
|
|
88
|
+
if (this.#queue.byteLength > 0) {
|
|
89
|
+
const chunk = this.#queue
|
|
90
|
+
this.#queue = new Uint8Array(0)
|
|
91
|
+
return chunk
|
|
92
|
+
}
|
|
93
|
+
return null
|
|
94
|
+
}
|
|
95
|
+
const buffer = value
|
|
96
|
+
this.#queue = concat(this.#queue, buffer)
|
|
97
|
+
}
|
|
98
|
+
const chunk = this.#queue.subarray(0, size)
|
|
99
|
+
this.#queue = this.#queue.subarray(size)
|
|
100
|
+
return chunk
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export abstract class ProtocolServerStreamInterface<
|
|
105
|
+
O = unknown,
|
|
106
|
+
> extends DuplexStream<O, ArrayBufferView> {
|
|
107
|
+
async *[Symbol.asyncIterator]() {
|
|
108
|
+
const reader = this.readable.getReader()
|
|
109
|
+
try {
|
|
110
|
+
while (true) {
|
|
111
|
+
const { done, value } = await reader.read()
|
|
112
|
+
if (!done) yield value
|
|
113
|
+
else break
|
|
114
|
+
}
|
|
115
|
+
} finally {
|
|
116
|
+
reader.releaseLock()
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export class ProtocolServerStream<T = unknown>
|
|
122
|
+
extends ProtocolServerStreamInterface<T>
|
|
123
|
+
implements ProtocolServerStreamInterface<T> {}
|
|
124
|
+
|
|
125
|
+
export class ProtocolServerRPCStream<
|
|
126
|
+
T = unknown,
|
|
127
|
+
> extends ProtocolServerStream<T> {
|
|
128
|
+
createAsyncIterable(onDone: Callback) {
|
|
129
|
+
return {
|
|
130
|
+
[Symbol.asyncIterator]: () => {
|
|
131
|
+
const iterator = this[Symbol.asyncIterator]()
|
|
132
|
+
return {
|
|
133
|
+
async next() {
|
|
134
|
+
const result = await iterator.next()
|
|
135
|
+
if (result.done) onDone()
|
|
136
|
+
return result
|
|
137
|
+
},
|
|
138
|
+
async return(value) {
|
|
139
|
+
onDone()
|
|
140
|
+
return iterator.return?.(value) ?? { done: true, value }
|
|
141
|
+
},
|
|
142
|
+
async throw(error) {
|
|
143
|
+
onDone()
|
|
144
|
+
return iterator.throw?.(error) ?? Promise.reject(error)
|
|
145
|
+
},
|
|
146
|
+
}
|
|
147
|
+
},
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export class ProtocolServerBlobStream
|
|
153
|
+
extends ProtocolServerStreamInterface<ArrayBufferView>
|
|
154
|
+
implements ProtocolBlobInterface, Blob
|
|
155
|
+
{
|
|
156
|
+
readonly [kBlobKey] = true
|
|
157
|
+
|
|
158
|
+
constructor(
|
|
159
|
+
readonly metadata: ProtocolBlobMetadata,
|
|
160
|
+
options?: DuplexStreamOptions<ArrayBufferView, ArrayBufferView>,
|
|
161
|
+
) {
|
|
162
|
+
super(options)
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
get size() {
|
|
166
|
+
return this.metadata.size || Number.NaN
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
get type() {
|
|
170
|
+
return this.metadata.type || 'application/octet-stream'
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
async text() {
|
|
174
|
+
const chunks: ArrayBufferView[] = []
|
|
175
|
+
for await (const chunk of this) chunks.push(chunk)
|
|
176
|
+
return decodeText(concat(...chunks))
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
async bytes() {
|
|
180
|
+
const chunks: ArrayBufferView[] = []
|
|
181
|
+
for await (const chunk of this) chunks.push(chunk)
|
|
182
|
+
return concat(...chunks)
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
async arrayBuffer() {
|
|
186
|
+
const bytes = await this.bytes()
|
|
187
|
+
return bytes.buffer
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
async json<T = unknown>() {
|
|
191
|
+
const text = await this.text()
|
|
192
|
+
return JSON.parse(text) as T
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
stream() {
|
|
196
|
+
const transform = new TransformStream<ArrayBufferView, Uint8Array>({
|
|
197
|
+
transform: (chunk, controller) => {
|
|
198
|
+
controller.enqueue(
|
|
199
|
+
chunk instanceof Uint8Array
|
|
200
|
+
? chunk
|
|
201
|
+
: new Uint8Array(chunk.buffer, chunk.byteOffset, chunk.byteLength),
|
|
202
|
+
)
|
|
203
|
+
},
|
|
204
|
+
})
|
|
205
|
+
this.readable.pipeThrough(transform)
|
|
206
|
+
return transform.readable as ReadableStream<Uint8Array<ArrayBuffer>>
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Throws an error
|
|
211
|
+
*/
|
|
212
|
+
async formData(): Promise<FormData> {
|
|
213
|
+
throw new Error('Method not implemented.')
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Throws an error
|
|
218
|
+
*/
|
|
219
|
+
slice(): Blob {
|
|
220
|
+
throw new Error('Unable to slice')
|
|
221
|
+
}
|
|
222
|
+
}
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import type { BaseProtocolError } from '../../common/types.ts'
|
|
2
|
+
import type { ClientMessageTypePayload, MessageContext } from '../protocol.ts'
|
|
3
|
+
import {
|
|
4
|
+
decodeNumber,
|
|
5
|
+
decodeText,
|
|
6
|
+
encodeNumber,
|
|
7
|
+
encodeText,
|
|
8
|
+
} from '../../common/binary.ts'
|
|
9
|
+
import {
|
|
10
|
+
ClientMessageType,
|
|
11
|
+
MessageByteLength,
|
|
12
|
+
ProtocolVersion,
|
|
13
|
+
ServerMessageType,
|
|
14
|
+
} from '../../common/enums.ts'
|
|
15
|
+
import { ProtocolVersionInterface } from '../protocol.ts'
|
|
16
|
+
|
|
17
|
+
export class ProtocolVersion1 extends ProtocolVersionInterface {
|
|
18
|
+
version = ProtocolVersion.v1
|
|
19
|
+
|
|
20
|
+
decodeMessage(context: MessageContext, buffer: Uint8Array) {
|
|
21
|
+
const messageType = decodeNumber(buffer, 'Uint8')
|
|
22
|
+
const payload = buffer.subarray(MessageByteLength.MessageType)
|
|
23
|
+
switch (messageType) {
|
|
24
|
+
// case ServerMessageType.Event: {
|
|
25
|
+
// const { event, data } = context.decoder.decode(payload)
|
|
26
|
+
// return { type: messageType, event, data }
|
|
27
|
+
// }
|
|
28
|
+
case ServerMessageType.RpcResponse: {
|
|
29
|
+
const callId = decodeNumber(payload, 'Uint32')
|
|
30
|
+
const isError = decodeNumber(payload, 'Uint8', MessageByteLength.CallId)
|
|
31
|
+
const dataPayload = payload.subarray(
|
|
32
|
+
MessageByteLength.CallId + MessageByteLength.MessageError,
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
if (isError) {
|
|
36
|
+
const error = context.decoder.decode(dataPayload) as BaseProtocolError
|
|
37
|
+
return { type: messageType, callId, error }
|
|
38
|
+
} else {
|
|
39
|
+
const result = context.decoder.decodeRPC(dataPayload, {
|
|
40
|
+
addStream: (streamId, metadata) => {
|
|
41
|
+
return context.addServerStream(streamId, metadata)
|
|
42
|
+
},
|
|
43
|
+
})
|
|
44
|
+
return { type: messageType, callId, result }
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
case ServerMessageType.RpcStreamResponse: {
|
|
48
|
+
const callId = decodeNumber(payload, 'Uint32')
|
|
49
|
+
const errorPayload = payload.subarray(MessageByteLength.CallId)
|
|
50
|
+
const error =
|
|
51
|
+
errorPayload.byteLength > 0
|
|
52
|
+
? (context.decoder.decode(errorPayload) as
|
|
53
|
+
| BaseProtocolError
|
|
54
|
+
| undefined)
|
|
55
|
+
: undefined
|
|
56
|
+
return { type: messageType, callId, error }
|
|
57
|
+
}
|
|
58
|
+
case ServerMessageType.RpcStreamChunk: {
|
|
59
|
+
const callId = decodeNumber(payload, 'Uint32')
|
|
60
|
+
const chunk = payload.subarray(MessageByteLength.CallId)
|
|
61
|
+
return { type: messageType, callId, chunk }
|
|
62
|
+
}
|
|
63
|
+
case ServerMessageType.RpcStreamEnd: {
|
|
64
|
+
const callId = decodeNumber(payload, 'Uint32')
|
|
65
|
+
return { type: messageType, callId }
|
|
66
|
+
}
|
|
67
|
+
case ServerMessageType.RpcStreamAbort: {
|
|
68
|
+
const callId = decodeNumber(payload, 'Uint32')
|
|
69
|
+
const reasonPayload = payload.subarray(MessageByteLength.CallId)
|
|
70
|
+
const reason =
|
|
71
|
+
reasonPayload.byteLength > 0 ? decodeText(reasonPayload) : undefined
|
|
72
|
+
return { type: messageType, callId, reason }
|
|
73
|
+
}
|
|
74
|
+
case ServerMessageType.ClientStreamPull: {
|
|
75
|
+
const streamId = decodeNumber(payload, 'Uint32')
|
|
76
|
+
const size = decodeNumber(payload, 'Uint32', MessageByteLength.StreamId)
|
|
77
|
+
return { type: messageType, streamId, size }
|
|
78
|
+
}
|
|
79
|
+
case ServerMessageType.ClientStreamAbort: {
|
|
80
|
+
const streamId = decodeNumber(payload, 'Uint32')
|
|
81
|
+
const reasonPayload = payload.subarray(MessageByteLength.StreamId)
|
|
82
|
+
const reason =
|
|
83
|
+
reasonPayload.byteLength > 0 ? decodeText(reasonPayload) : undefined
|
|
84
|
+
|
|
85
|
+
return { type: messageType, streamId, reason }
|
|
86
|
+
}
|
|
87
|
+
case ServerMessageType.ServerStreamPush: {
|
|
88
|
+
const streamId = decodeNumber(payload, 'Uint32')
|
|
89
|
+
const chunk = payload.subarray(MessageByteLength.StreamId)
|
|
90
|
+
return { type: messageType, streamId, chunk }
|
|
91
|
+
}
|
|
92
|
+
case ServerMessageType.ServerStreamEnd: {
|
|
93
|
+
const streamId = decodeNumber(payload, 'Uint32')
|
|
94
|
+
const reasonPayload = payload.subarray(MessageByteLength.StreamId)
|
|
95
|
+
const reason =
|
|
96
|
+
reasonPayload.byteLength > 0 ? decodeText(reasonPayload) : undefined
|
|
97
|
+
|
|
98
|
+
return { type: messageType, streamId, reason }
|
|
99
|
+
}
|
|
100
|
+
case ServerMessageType.ServerStreamAbort: {
|
|
101
|
+
const streamId = decodeNumber(payload, 'Uint32')
|
|
102
|
+
const reasonPayload = payload.subarray(MessageByteLength.StreamId)
|
|
103
|
+
const reason =
|
|
104
|
+
reasonPayload.byteLength > 0 ? decodeText(reasonPayload) : undefined
|
|
105
|
+
|
|
106
|
+
return { type: messageType, streamId, reason }
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
default:
|
|
110
|
+
throw new Error(`Unsupported message type: ${messageType}`)
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
encodeMessage<T extends ClientMessageType>(
|
|
115
|
+
context: MessageContext,
|
|
116
|
+
messageType: T,
|
|
117
|
+
payload: ClientMessageTypePayload[T],
|
|
118
|
+
) {
|
|
119
|
+
switch (messageType) {
|
|
120
|
+
case ClientMessageType.Rpc: {
|
|
121
|
+
const {
|
|
122
|
+
callId,
|
|
123
|
+
procedure,
|
|
124
|
+
payload: rpcPayload,
|
|
125
|
+
} = payload as ClientMessageTypePayload[ClientMessageType.Rpc]
|
|
126
|
+
const procedureBuffer = encodeText(procedure)
|
|
127
|
+
return this.encode(
|
|
128
|
+
encodeNumber(messageType, 'Uint8'),
|
|
129
|
+
encodeNumber(callId, 'Uint32'),
|
|
130
|
+
encodeNumber(procedureBuffer.byteLength, 'Uint16'),
|
|
131
|
+
procedureBuffer,
|
|
132
|
+
context.encoder.encodeRPC(rpcPayload, {
|
|
133
|
+
addStream: (blob) => context.addClientStream(blob),
|
|
134
|
+
}),
|
|
135
|
+
)
|
|
136
|
+
}
|
|
137
|
+
case ClientMessageType.RpcAbort: {
|
|
138
|
+
const { callId, reason } =
|
|
139
|
+
payload as ClientMessageTypePayload[ClientMessageType.RpcAbort]
|
|
140
|
+
|
|
141
|
+
return this.encode(
|
|
142
|
+
encodeNumber(messageType, 'Uint8'),
|
|
143
|
+
encodeNumber(callId, 'Uint32'),
|
|
144
|
+
reason ? encodeText(reason) : new Uint8Array(0),
|
|
145
|
+
)
|
|
146
|
+
}
|
|
147
|
+
case ClientMessageType.RpcPull: {
|
|
148
|
+
const { callId } =
|
|
149
|
+
payload as ClientMessageTypePayload[ClientMessageType.RpcPull]
|
|
150
|
+
|
|
151
|
+
return this.encode(
|
|
152
|
+
encodeNumber(messageType, 'Uint8'),
|
|
153
|
+
encodeNumber(callId, 'Uint32'),
|
|
154
|
+
)
|
|
155
|
+
}
|
|
156
|
+
case ClientMessageType.ClientStreamPush: {
|
|
157
|
+
const { streamId, chunk } =
|
|
158
|
+
payload as ClientMessageTypePayload[ClientMessageType.ClientStreamPush]
|
|
159
|
+
return this.encode(
|
|
160
|
+
encodeNumber(messageType, 'Uint8'),
|
|
161
|
+
encodeNumber(streamId, 'Uint32'),
|
|
162
|
+
chunk,
|
|
163
|
+
)
|
|
164
|
+
}
|
|
165
|
+
case ClientMessageType.ClientStreamEnd: {
|
|
166
|
+
const { streamId } =
|
|
167
|
+
payload as ClientMessageTypePayload[ClientMessageType.ClientStreamEnd]
|
|
168
|
+
return this.encode(
|
|
169
|
+
encodeNumber(messageType, 'Uint8'),
|
|
170
|
+
encodeNumber(streamId, 'Uint32'),
|
|
171
|
+
)
|
|
172
|
+
}
|
|
173
|
+
case ClientMessageType.ClientStreamAbort: {
|
|
174
|
+
const { streamId, reason } =
|
|
175
|
+
payload as ClientMessageTypePayload[ClientMessageType.ClientStreamAbort]
|
|
176
|
+
return this.encode(
|
|
177
|
+
encodeNumber(messageType, 'Uint8'),
|
|
178
|
+
encodeNumber(streamId, 'Uint32'),
|
|
179
|
+
reason ? encodeText(reason) : new Uint8Array(0),
|
|
180
|
+
)
|
|
181
|
+
}
|
|
182
|
+
case ClientMessageType.ServerStreamPull: {
|
|
183
|
+
const { streamId, size } =
|
|
184
|
+
payload as ClientMessageTypePayload[ClientMessageType.ServerStreamPull]
|
|
185
|
+
return this.encode(
|
|
186
|
+
encodeNumber(messageType, 'Uint8'),
|
|
187
|
+
encodeNumber(streamId, 'Uint32'),
|
|
188
|
+
encodeNumber(size, 'Uint32'),
|
|
189
|
+
)
|
|
190
|
+
}
|
|
191
|
+
case ClientMessageType.ServerStreamAbort: {
|
|
192
|
+
const { streamId, reason } =
|
|
193
|
+
payload as ClientMessageTypePayload[ClientMessageType.ServerStreamAbort]
|
|
194
|
+
return this.encode(
|
|
195
|
+
encodeNumber(messageType, 'Uint8'),
|
|
196
|
+
encodeNumber(streamId, 'Uint32'),
|
|
197
|
+
reason ? encodeText(reason) : new Uint8Array(0),
|
|
198
|
+
)
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
default:
|
|
202
|
+
throw new Error(`Unsupported message type: ${messageType}`)
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// TODO: get rid of lib DOM somehow...
|
|
2
|
+
/// <reference lib="dom" />
|
|
3
|
+
|
|
4
|
+
const utf8decoder = new TextDecoder()
|
|
5
|
+
const utf8encoder = new TextEncoder()
|
|
6
|
+
|
|
7
|
+
export type BinaryTypes = {
|
|
8
|
+
Int8: number
|
|
9
|
+
Int16: number
|
|
10
|
+
Int32: number
|
|
11
|
+
Uint8: number
|
|
12
|
+
Uint16: number
|
|
13
|
+
Uint32: number
|
|
14
|
+
Float32: number
|
|
15
|
+
Float64: number
|
|
16
|
+
BigInt64: bigint
|
|
17
|
+
BigUint64: bigint
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const encodeNumber = <T extends keyof BinaryTypes>(
|
|
21
|
+
value: BinaryTypes[T],
|
|
22
|
+
type: T,
|
|
23
|
+
littleEndian = true,
|
|
24
|
+
) => {
|
|
25
|
+
const bytesNeeded = globalThis[`${type}Array`].BYTES_PER_ELEMENT
|
|
26
|
+
const ab = new ArrayBuffer(bytesNeeded)
|
|
27
|
+
const dv = new DataView(ab)
|
|
28
|
+
dv[`set${type}`](0, value as never, littleEndian)
|
|
29
|
+
return ab
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export const decodeNumber = <T extends keyof BinaryTypes>(
|
|
33
|
+
buffer: ArrayBuffer | ArrayBufferView,
|
|
34
|
+
type: T,
|
|
35
|
+
offset = 0,
|
|
36
|
+
littleEndian = true,
|
|
37
|
+
): BinaryTypes[T] => {
|
|
38
|
+
const ab = buffer instanceof ArrayBuffer ? buffer : buffer.buffer
|
|
39
|
+
const baseOffset = buffer instanceof ArrayBuffer ? 0 : buffer.byteOffset
|
|
40
|
+
const view = new DataView(ab)
|
|
41
|
+
return view[`get${type}`](baseOffset + offset, littleEndian) as BinaryTypes[T]
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export const encodeText = (text: string) => utf8encoder.encode(text)
|
|
45
|
+
|
|
46
|
+
export const decodeText = (buffer: Parameters<typeof utf8decoder.decode>[0]) =>
|
|
47
|
+
utf8decoder.decode(buffer)
|
|
48
|
+
|
|
49
|
+
export const concat = (...buffers: (ArrayBuffer | ArrayBufferView)[]) => {
|
|
50
|
+
let totalLength = 0
|
|
51
|
+
for (const buffer of buffers) totalLength += buffer.byteLength
|
|
52
|
+
const view = new Uint8Array(totalLength)
|
|
53
|
+
let offset = 0
|
|
54
|
+
for (const buffer of buffers) {
|
|
55
|
+
const chunk =
|
|
56
|
+
buffer instanceof ArrayBuffer
|
|
57
|
+
? new Uint8Array(buffer)
|
|
58
|
+
: new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength)
|
|
59
|
+
view.set(chunk, offset)
|
|
60
|
+
offset += chunk.byteLength
|
|
61
|
+
}
|
|
62
|
+
return view
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export const UTF8Transform = () =>
|
|
66
|
+
new TransformStream<string, Uint8Array>({
|
|
67
|
+
transform(chunk, controller) {
|
|
68
|
+
controller.enqueue(encodeText(chunk))
|
|
69
|
+
},
|
|
70
|
+
})
|