@nmtjs/protocol 0.15.0-beta.2 → 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/dist/client/format.d.ts +26 -0
- package/dist/client/format.js +1 -0
- package/dist/client/format.js.map +1 -0
- package/dist/client/index.d.ts +7 -0
- package/dist/client/index.js +1 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/protocol.d.ts +114 -0
- package/dist/client/protocol.js +1 -0
- package/dist/client/protocol.js.map +1 -0
- package/dist/client/stream.d.ts +49 -0
- package/dist/client/stream.js +1 -0
- package/dist/client/stream.js.map +1 -0
- package/dist/client/versions/v1.d.ts +108 -0
- package/dist/client/versions/v1.js +1 -0
- package/dist/client/versions/v1.js.map +1 -0
- package/dist/common/binary.d.ts +20 -0
- package/dist/common/binary.js +1 -0
- package/dist/common/binary.js.map +1 -0
- package/dist/common/blob.d.ts +29 -0
- package/dist/common/blob.js +1 -0
- package/dist/common/blob.js.map +1 -0
- package/dist/common/constants.d.ts +2 -0
- package/dist/common/constants.js +1 -0
- package/dist/common/constants.js.map +1 -0
- package/dist/common/enums.d.ts +51 -0
- package/dist/common/enums.js +1 -0
- package/dist/common/enums.js.map +1 -0
- package/dist/common/index.d.ts +6 -0
- package/dist/common/index.js +1 -0
- package/dist/common/index.js.map +1 -0
- package/dist/common/types.d.ts +14 -0
- package/dist/common/types.js +1 -0
- package/dist/common/types.js.map +1 -0
- package/dist/common/utils.d.ts +2 -0
- package/dist/common/utils.js +1 -0
- package/dist/common/utils.js.map +1 -0
- package/dist/server/format.d.ts +32 -0
- package/dist/server/format.js +1 -0
- package/dist/server/format.js.map +1 -0
- package/dist/server/index.d.ts +9 -0
- package/dist/server/index.js +1 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/protocol.d.ts +102 -0
- package/dist/server/protocol.js +1 -0
- package/dist/server/protocol.js.map +1 -0
- package/dist/server/stream.d.ts +15 -0
- package/dist/server/stream.js +1 -0
- package/dist/server/stream.js.map +1 -0
- package/dist/server/types.d.ts +34 -0
- package/dist/server/types.js +1 -0
- package/dist/server/types.js.map +1 -0
- package/dist/server/utils.d.ts +12 -0
- package/dist/server/utils.js +1 -0
- package/dist/server/utils.js.map +1 -0
- package/dist/server/versions/v1.d.ts +77 -0
- package/dist/server/versions/v1.js +1 -0
- package/dist/server/versions/v1.js.map +1 -0
- package/package.json +11 -10
- 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
|
@@ -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
|
+
})
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { kBlobKey } from './constants.ts'
|
|
2
|
+
|
|
3
|
+
export type ProtocolBlobMetadata = {
|
|
4
|
+
type: string
|
|
5
|
+
size?: number | undefined
|
|
6
|
+
filename?: string | undefined
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface ProtocolBlobInterface {
|
|
10
|
+
readonly metadata: ProtocolBlobMetadata
|
|
11
|
+
readonly [kBlobKey]: any
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export class ProtocolBlob implements ProtocolBlobInterface {
|
|
15
|
+
[kBlobKey]: true = true
|
|
16
|
+
|
|
17
|
+
public readonly source: any
|
|
18
|
+
public readonly metadata: ProtocolBlobMetadata
|
|
19
|
+
public readonly encode?: () => unknown
|
|
20
|
+
public readonly toJSON?: () => unknown
|
|
21
|
+
|
|
22
|
+
constructor({
|
|
23
|
+
source,
|
|
24
|
+
encode,
|
|
25
|
+
size,
|
|
26
|
+
type = 'application/octet-stream',
|
|
27
|
+
filename,
|
|
28
|
+
}: {
|
|
29
|
+
source: any
|
|
30
|
+
encode?: () => unknown
|
|
31
|
+
size?: number
|
|
32
|
+
type?: string
|
|
33
|
+
filename?: string
|
|
34
|
+
}) {
|
|
35
|
+
if (typeof size !== 'undefined' && size <= 0)
|
|
36
|
+
throw new Error('Blob size is invalid')
|
|
37
|
+
|
|
38
|
+
this.encode = encode
|
|
39
|
+
this.source = source
|
|
40
|
+
this.metadata = { size, type, filename }
|
|
41
|
+
if (encode) {
|
|
42
|
+
Object.defineProperty(this, 'toJSON', {
|
|
43
|
+
configurable: false,
|
|
44
|
+
enumerable: false,
|
|
45
|
+
writable: false,
|
|
46
|
+
value: encode,
|
|
47
|
+
})
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
static from(
|
|
52
|
+
_source: any,
|
|
53
|
+
_metadata: { size?: number; type?: string; filename?: string } = {},
|
|
54
|
+
_encode?: () => unknown,
|
|
55
|
+
) {
|
|
56
|
+
let source: any
|
|
57
|
+
const metadata = { ..._metadata }
|
|
58
|
+
|
|
59
|
+
if (_source instanceof globalThis.ReadableStream) {
|
|
60
|
+
source = _source
|
|
61
|
+
} else if ('File' in globalThis && _source instanceof globalThis.File) {
|
|
62
|
+
source = _source.stream()
|
|
63
|
+
metadata.size ??= _source.size
|
|
64
|
+
metadata.filename ??= _source.name
|
|
65
|
+
} else if (_source instanceof globalThis.Blob) {
|
|
66
|
+
source = _source.stream()
|
|
67
|
+
metadata.size ??= _source.size
|
|
68
|
+
metadata.type ??= _source.type
|
|
69
|
+
} else if (typeof _source === 'string') {
|
|
70
|
+
const blob = new Blob([_source])
|
|
71
|
+
source = blob.stream()
|
|
72
|
+
metadata.size ??= blob.size
|
|
73
|
+
metadata.type ??= 'text/plain'
|
|
74
|
+
} else if (globalThis.ArrayBuffer.isView(_source)) {
|
|
75
|
+
const blob = new Blob([_source as ArrayBufferView<ArrayBuffer>])
|
|
76
|
+
source = blob.stream()
|
|
77
|
+
metadata.size ??= blob.size
|
|
78
|
+
} else if (_source instanceof globalThis.ArrayBuffer) {
|
|
79
|
+
const blob = new Blob([_source])
|
|
80
|
+
source = blob.stream()
|
|
81
|
+
metadata.size ??= blob.size
|
|
82
|
+
} else {
|
|
83
|
+
source = _source
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return new ProtocolBlob({
|
|
87
|
+
source,
|
|
88
|
+
encode: _encode,
|
|
89
|
+
size: metadata.size,
|
|
90
|
+
type: metadata.type,
|
|
91
|
+
filename: metadata.filename,
|
|
92
|
+
})
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// toJSON() {
|
|
96
|
+
// if (!this.encode) throw new Error('Blob format encoder is not defined')
|
|
97
|
+
// return this.encode()
|
|
98
|
+
// }
|
|
99
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
export enum ProtocolVersion {
|
|
2
|
+
v1 = 1,
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export enum ClientMessageType {
|
|
6
|
+
Rpc = 10,
|
|
7
|
+
RpcAbort = 11,
|
|
8
|
+
RpcPull = 12,
|
|
9
|
+
|
|
10
|
+
ClientStreamPush = 20,
|
|
11
|
+
ClientStreamEnd = 21,
|
|
12
|
+
ClientStreamAbort = 22,
|
|
13
|
+
|
|
14
|
+
ServerStreamAbort = 33,
|
|
15
|
+
ServerStreamPull = 34,
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export enum ServerMessageType {
|
|
19
|
+
// Event = 1,
|
|
20
|
+
|
|
21
|
+
RpcResponse = 10,
|
|
22
|
+
RpcStreamResponse = 11,
|
|
23
|
+
RpcStreamChunk = 12,
|
|
24
|
+
RpcStreamEnd = 13,
|
|
25
|
+
RpcStreamAbort = 14,
|
|
26
|
+
|
|
27
|
+
ServerStreamPush = 20,
|
|
28
|
+
ServerStreamEnd = 21,
|
|
29
|
+
ServerStreamAbort = 22,
|
|
30
|
+
|
|
31
|
+
ClientStreamAbort = 33,
|
|
32
|
+
ClientStreamPull = 34,
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export enum ConnectionType {
|
|
36
|
+
Bidirectional = 'Bidirectional',
|
|
37
|
+
Unidirectional = 'Unidirectional',
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export enum ErrorCode {
|
|
41
|
+
ValidationError = 'ValidationError',
|
|
42
|
+
BadRequest = 'BadRequest',
|
|
43
|
+
NotFound = 'NotFound',
|
|
44
|
+
Forbidden = 'Forbidden',
|
|
45
|
+
Unauthorized = 'Unauthorized',
|
|
46
|
+
InternalServerError = 'InternalServerError',
|
|
47
|
+
NotAcceptable = 'NotAcceptable',
|
|
48
|
+
RequestTimeout = 'RequestTimeout',
|
|
49
|
+
GatewayTimeout = 'GatewayTimeout',
|
|
50
|
+
ServiceUnavailable = 'ServiceUnavailable',
|
|
51
|
+
ClientRequestError = 'ClientRequestError',
|
|
52
|
+
ConnectionError = 'ConnectionError',
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export enum MessageByteLength {
|
|
56
|
+
MessageType = 1,
|
|
57
|
+
MessageError = 1,
|
|
58
|
+
ProcedureLength = 2,
|
|
59
|
+
CallId = 4,
|
|
60
|
+
StreamId = 4,
|
|
61
|
+
ChunkSize = 4,
|
|
62
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { ProtocolBlobMetadata } from './blob.ts'
|
|
2
|
+
|
|
3
|
+
type Stream = any
|
|
4
|
+
|
|
5
|
+
export interface BaseProtocolError {
|
|
6
|
+
code: string
|
|
7
|
+
message: string
|
|
8
|
+
data?: any
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export type ProtocolRPCPayload = unknown
|
|
12
|
+
export type ProtocolRPCResponse = unknown
|
|
13
|
+
|
|
14
|
+
export type EncodeRPCStreams = Record<number, ProtocolBlobMetadata>
|
|
15
|
+
|
|
16
|
+
export interface DecodeRPCContext<T = Stream> {
|
|
17
|
+
addStream: (id: number, metadata: ProtocolBlobMetadata) => T
|
|
18
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ProtocolBlobInterface } from './blob.ts'
|
|
2
|
+
import { kBlobKey } from './constants.ts'
|
|
3
|
+
|
|
4
|
+
export const isBlobInterface = <T extends ProtocolBlobInterface>(
|
|
5
|
+
value: any,
|
|
6
|
+
): value is T => {
|
|
7
|
+
return (
|
|
8
|
+
value &&
|
|
9
|
+
(typeof value === 'object' || typeof value === 'function') &&
|
|
10
|
+
kBlobKey in value
|
|
11
|
+
)
|
|
12
|
+
}
|