@nmtjs/protocol 0.15.0-beta.35 → 0.15.0-beta.37
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/LICENSE.md +1 -1
- package/README.md +1 -1
- package/dist/client/events.d.ts +15 -0
- package/dist/client/events.js +28 -0
- package/dist/client/format.d.ts +13 -18
- package/dist/client/format.js +0 -1
- package/dist/client/index.d.ts +1 -4
- package/dist/client/index.js +4 -7
- package/dist/client/protocol.d.ts +138 -102
- package/dist/client/protocol.js +336 -7
- package/dist/client/stream.d.ts +19 -37
- package/dist/client/stream.js +46 -138
- package/dist/common/binary.d.ts +6 -5
- package/dist/common/binary.js +9 -22
- package/dist/common/blob.d.ts +8 -15
- package/dist/common/blob.js +30 -55
- package/dist/common/enums.d.ts +7 -17
- package/dist/common/enums.js +14 -34
- package/dist/common/index.d.ts +0 -2
- package/dist/common/index.js +4 -7
- package/dist/common/types.d.ts +24 -5
- package/dist/common/types.js +0 -1
- package/dist/server/api.d.ts +33 -0
- package/dist/server/api.js +7 -0
- package/dist/server/connection.d.ts +25 -0
- package/dist/server/connection.js +22 -0
- package/dist/server/constants.d.ts +4 -0
- package/dist/server/constants.js +2 -0
- package/dist/server/format.d.ts +23 -17
- package/dist/server/format.js +8 -13
- package/dist/server/index.d.ts +6 -4
- package/dist/server/index.js +11 -9
- package/dist/server/injectables.d.ts +14 -0
- package/dist/server/injectables.js +22 -0
- package/dist/server/protocol.d.ts +114 -88
- package/dist/server/protocol.js +354 -5
- package/dist/server/registry.d.ts +3 -0
- package/dist/server/registry.js +3 -0
- package/dist/server/stream.d.ts +0 -5
- package/dist/server/stream.js +2 -31
- package/dist/server/transport.d.ts +23 -0
- package/dist/server/transport.js +3 -0
- package/dist/server/types.d.ts +8 -29
- package/dist/server/types.js +0 -1
- package/dist/server/utils.d.ts +6 -3
- package/dist/server/utils.js +4 -5
- package/package.json +24 -17
- package/dist/client/format.js.map +0 -1
- package/dist/client/index.js.map +0 -1
- package/dist/client/protocol.js.map +0 -1
- package/dist/client/stream.js.map +0 -1
- package/dist/client/versions/v1.d.ts +0 -108
- package/dist/client/versions/v1.js +0 -128
- package/dist/client/versions/v1.js.map +0 -1
- package/dist/common/binary.js.map +0 -1
- package/dist/common/blob.js.map +0 -1
- package/dist/common/constants.d.ts +0 -2
- package/dist/common/constants.js +0 -2
- package/dist/common/constants.js.map +0 -1
- package/dist/common/enums.js.map +0 -1
- package/dist/common/index.js.map +0 -1
- package/dist/common/types.js.map +0 -1
- package/dist/common/utils.d.ts +0 -2
- package/dist/common/utils.js +0 -7
- package/dist/common/utils.js.map +0 -1
- package/dist/server/format.js.map +0 -1
- package/dist/server/index.js.map +0 -1
- package/dist/server/protocol.js.map +0 -1
- package/dist/server/stream.js.map +0 -1
- package/dist/server/types.js.map +0 -1
- package/dist/server/utils.js.map +0 -1
- package/dist/server/versions/v1.d.ts +0 -77
- package/dist/server/versions/v1.js +0 -119
- package/dist/server/versions/v1.js.map +0 -1
- package/src/client/format.ts +0 -49
- package/src/client/index.ts +0 -8
- package/src/client/protocol.ts +0 -107
- package/src/client/stream.ts +0 -222
- package/src/client/versions/v1.ts +0 -205
- package/src/common/binary.ts +0 -70
- package/src/common/blob.ts +0 -94
- package/src/common/constants.ts +0 -2
- package/src/common/enums.ts +0 -62
- package/src/common/index.ts +0 -6
- package/src/common/types.ts +0 -18
- package/src/common/utils.ts +0 -12
- package/src/server/format.ts +0 -117
- package/src/server/index.ts +0 -10
- package/src/server/protocol.ts +0 -97
- package/src/server/stream.ts +0 -72
- package/src/server/types.ts +0 -42
- package/src/server/utils.ts +0 -22
- package/src/server/versions/v1.ts +0 -198
package/src/server/stream.ts
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import type { ReadableOptions } from 'node:stream'
|
|
2
|
-
import { PassThrough, Readable } from 'node:stream'
|
|
3
|
-
import { ReadableStream } from 'node:stream/web'
|
|
4
|
-
|
|
5
|
-
import type { ProtocolBlob, ProtocolBlobMetadata } from '../common/blob.ts'
|
|
6
|
-
|
|
7
|
-
export class ProtocolClientStream extends PassThrough {
|
|
8
|
-
readonly #read?: ReadableOptions['read']
|
|
9
|
-
|
|
10
|
-
constructor(
|
|
11
|
-
public readonly id: number,
|
|
12
|
-
public readonly metadata: ProtocolBlobMetadata,
|
|
13
|
-
options?: ReadableOptions,
|
|
14
|
-
) {
|
|
15
|
-
const { read, ...rest } = options ?? {}
|
|
16
|
-
super(rest)
|
|
17
|
-
this.#read = read
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
override _read(size: number): void {
|
|
21
|
-
if (this.#read) {
|
|
22
|
-
this.#read.call(this, size)
|
|
23
|
-
}
|
|
24
|
-
super._read(size)
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export class ProtocolServerStream extends PassThrough {
|
|
29
|
-
public readonly id: number
|
|
30
|
-
public readonly metadata: ProtocolBlobMetadata
|
|
31
|
-
readonly #source: Readable
|
|
32
|
-
#piped = false
|
|
33
|
-
|
|
34
|
-
constructor(id: number, blob: ProtocolBlob) {
|
|
35
|
-
let readable: Readable
|
|
36
|
-
|
|
37
|
-
if (blob.source instanceof Readable) {
|
|
38
|
-
readable = blob.source
|
|
39
|
-
} else if (blob.source instanceof ReadableStream) {
|
|
40
|
-
readable = Readable.fromWeb(blob.source as ReadableStream)
|
|
41
|
-
} else {
|
|
42
|
-
throw new Error('Invalid source type')
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
super()
|
|
46
|
-
|
|
47
|
-
this.pause()
|
|
48
|
-
this.#source = readable
|
|
49
|
-
this.#source.on('error', (error) => {
|
|
50
|
-
this.destroy(error)
|
|
51
|
-
})
|
|
52
|
-
|
|
53
|
-
this.id = id
|
|
54
|
-
this.metadata = blob.metadata
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
override resume(): this {
|
|
58
|
-
if (!this.#piped) {
|
|
59
|
-
this.#piped = true
|
|
60
|
-
this.#source.pipe(this)
|
|
61
|
-
}
|
|
62
|
-
return super.resume()
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
override destroy(error?: Error | null) {
|
|
66
|
-
if (!this.#piped) {
|
|
67
|
-
this.#piped = true
|
|
68
|
-
}
|
|
69
|
-
this.#source.destroy?.(error ?? undefined)
|
|
70
|
-
return super.destroy(error ?? undefined)
|
|
71
|
-
}
|
|
72
|
-
}
|
package/src/server/types.ts
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import type { PlainType } from '@nmtjs/type'
|
|
2
|
-
|
|
3
|
-
import type {
|
|
4
|
-
ProtocolBlobInterface,
|
|
5
|
-
ProtocolBlobMetadata,
|
|
6
|
-
} from '../common/blob.ts'
|
|
7
|
-
import type { kBlobKey } from '../common/constants.ts'
|
|
8
|
-
import type { BaseServerDecoder, BaseServerEncoder } from './format.ts'
|
|
9
|
-
import type { ProtocolVersionInterface } from './protocol.ts'
|
|
10
|
-
import type { ProtocolClientStream } from './stream.ts'
|
|
11
|
-
|
|
12
|
-
export type ClientStreamConsumer = (() => ProtocolClientStream) & {
|
|
13
|
-
readonly [kBlobKey]: any
|
|
14
|
-
readonly metadata: ProtocolBlobMetadata
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export type MessageContext = {
|
|
18
|
-
protocol: ProtocolVersionInterface
|
|
19
|
-
connectionId: string
|
|
20
|
-
streamId: () => number
|
|
21
|
-
decoder: BaseServerDecoder
|
|
22
|
-
encoder: BaseServerEncoder
|
|
23
|
-
addClientStream: (options: {
|
|
24
|
-
streamId: number
|
|
25
|
-
metadata: ProtocolBlobMetadata
|
|
26
|
-
callId: number
|
|
27
|
-
}) => ClientStreamConsumer
|
|
28
|
-
transport: {
|
|
29
|
-
send?: (connectionId: string, buffer: ArrayBufferView) => boolean | null
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export type ResolveFormatParams = {
|
|
34
|
-
contentType?: string | null
|
|
35
|
-
accept?: string | null
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export type InputType<T> = T extends ProtocolBlobInterface
|
|
39
|
-
? ClientStreamConsumer
|
|
40
|
-
: T extends { [PlainType]?: true }
|
|
41
|
-
? { [K in keyof Omit<T, PlainType>]: InputType<T[K]> }
|
|
42
|
-
: T
|
package/src/server/utils.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import type { ProtocolFormats } from './format.ts'
|
|
2
|
-
import type { ResolveFormatParams } from './types.ts'
|
|
3
|
-
|
|
4
|
-
export class UnsupportedFormatError extends Error {}
|
|
5
|
-
|
|
6
|
-
export class UnsupportedContentTypeError extends UnsupportedFormatError {}
|
|
7
|
-
|
|
8
|
-
export class UnsupportedAcceptTypeError extends UnsupportedFormatError {}
|
|
9
|
-
|
|
10
|
-
export const getFormat = (
|
|
11
|
-
format: ProtocolFormats,
|
|
12
|
-
{ accept, contentType }: ResolveFormatParams,
|
|
13
|
-
) => {
|
|
14
|
-
const encoder = contentType ? format.supportsEncoder(contentType) : undefined
|
|
15
|
-
if (!encoder)
|
|
16
|
-
throw new UnsupportedContentTypeError('Unsupported Content type')
|
|
17
|
-
|
|
18
|
-
const decoder = accept ? format.supportsDecoder(accept) : undefined
|
|
19
|
-
if (!decoder) throw new UnsupportedAcceptTypeError('Unsupported Accept type')
|
|
20
|
-
|
|
21
|
-
return { encoder, decoder }
|
|
22
|
-
}
|
|
@@ -1,198 +0,0 @@
|
|
|
1
|
-
import type { ServerMessageTypePayload } from '../protocol.ts'
|
|
2
|
-
import type { MessageContext } from '../types.ts'
|
|
3
|
-
import { decodeText, encodeNumber, encodeText } from '../../common/binary.ts'
|
|
4
|
-
import {
|
|
5
|
-
ClientMessageType,
|
|
6
|
-
MessageByteLength,
|
|
7
|
-
ProtocolVersion,
|
|
8
|
-
ServerMessageType,
|
|
9
|
-
} from '../../common/enums.ts'
|
|
10
|
-
import { ProtocolVersionInterface } from '../protocol.ts'
|
|
11
|
-
|
|
12
|
-
export class ProtocolVersion1 extends ProtocolVersionInterface {
|
|
13
|
-
version = ProtocolVersion.v1
|
|
14
|
-
decodeMessage(context: MessageContext, buffer: Buffer) {
|
|
15
|
-
const messageType = buffer.readUint8(0)
|
|
16
|
-
const messagePayload = buffer.subarray(MessageByteLength.MessageType)
|
|
17
|
-
switch (messageType) {
|
|
18
|
-
case ClientMessageType.Rpc: {
|
|
19
|
-
const callId = messagePayload.readUint32LE(0)
|
|
20
|
-
const procedureLength = messagePayload.readUInt16LE(
|
|
21
|
-
MessageByteLength.CallId,
|
|
22
|
-
)
|
|
23
|
-
const procedureOffset =
|
|
24
|
-
MessageByteLength.CallId + MessageByteLength.ProcedureLength
|
|
25
|
-
const procedure = messagePayload.toString(
|
|
26
|
-
'utf-8',
|
|
27
|
-
procedureOffset,
|
|
28
|
-
procedureOffset + procedureLength,
|
|
29
|
-
)
|
|
30
|
-
const formatPayload = messagePayload.subarray(
|
|
31
|
-
procedureOffset + procedureLength,
|
|
32
|
-
)
|
|
33
|
-
const payload = context.decoder.decodeRPC(formatPayload, {
|
|
34
|
-
addStream: (streamId, metadata) => {
|
|
35
|
-
return context.addClientStream({ callId, streamId, metadata })
|
|
36
|
-
},
|
|
37
|
-
})
|
|
38
|
-
|
|
39
|
-
return { type: messageType, rpc: { callId, procedure, payload } }
|
|
40
|
-
}
|
|
41
|
-
case ClientMessageType.RpcPull: {
|
|
42
|
-
const callId = messagePayload.readUInt32LE(0)
|
|
43
|
-
return { type: messageType, callId }
|
|
44
|
-
}
|
|
45
|
-
case ClientMessageType.RpcAbort: {
|
|
46
|
-
const callId = messagePayload.readUInt32LE(0)
|
|
47
|
-
const reasonPayload = messagePayload.subarray(MessageByteLength.CallId)
|
|
48
|
-
const reason =
|
|
49
|
-
reasonPayload.byteLength > 0 ? decodeText(reasonPayload) : undefined
|
|
50
|
-
return { type: messageType, callId, reason }
|
|
51
|
-
}
|
|
52
|
-
case ClientMessageType.ServerStreamAbort: {
|
|
53
|
-
const streamId = messagePayload.readUInt32LE(0)
|
|
54
|
-
const reasonPayload = messagePayload.subarray(
|
|
55
|
-
MessageByteLength.StreamId,
|
|
56
|
-
)
|
|
57
|
-
const reason =
|
|
58
|
-
reasonPayload.byteLength > 0 ? decodeText(reasonPayload) : undefined
|
|
59
|
-
return { type: messageType, streamId, reason }
|
|
60
|
-
}
|
|
61
|
-
case ClientMessageType.ServerStreamPull: {
|
|
62
|
-
const streamId = messagePayload.readUInt32LE(0)
|
|
63
|
-
const size = messagePayload.readUInt32LE(MessageByteLength.StreamId)
|
|
64
|
-
return { type: messageType, streamId, size }
|
|
65
|
-
}
|
|
66
|
-
case ClientMessageType.ClientStreamAbort: {
|
|
67
|
-
const streamId = messagePayload.readUInt32LE(0)
|
|
68
|
-
const reasonPayload = messagePayload.subarray(
|
|
69
|
-
MessageByteLength.StreamId,
|
|
70
|
-
)
|
|
71
|
-
const reason =
|
|
72
|
-
reasonPayload.byteLength > 0 ? decodeText(reasonPayload) : undefined
|
|
73
|
-
return { type: messageType, streamId, reason }
|
|
74
|
-
}
|
|
75
|
-
case ClientMessageType.ClientStreamEnd: {
|
|
76
|
-
return { type: messageType, streamId: messagePayload.readUInt32LE(0) }
|
|
77
|
-
}
|
|
78
|
-
case ClientMessageType.ClientStreamPush: {
|
|
79
|
-
const streamId = messagePayload.readUInt32LE(0)
|
|
80
|
-
const chunk = messagePayload.subarray(MessageByteLength.StreamId)
|
|
81
|
-
return { type: messageType, streamId, chunk }
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
default:
|
|
85
|
-
throw new Error(`Unsupported message type: ${messageType}`)
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
encodeMessage<T extends ServerMessageType>(
|
|
90
|
-
context: MessageContext,
|
|
91
|
-
messageType: T,
|
|
92
|
-
payload: ServerMessageTypePayload[T],
|
|
93
|
-
) {
|
|
94
|
-
switch (messageType) {
|
|
95
|
-
// case ServerMessageType.Event: {
|
|
96
|
-
// const { event, data } =
|
|
97
|
-
// payload as ServerMessageTypePayload[ServerMessageType.Event]
|
|
98
|
-
// return this.encode(
|
|
99
|
-
// encodeNumber(messageType, 'Uint8'),
|
|
100
|
-
// context.encoder.encode({ event, data }),
|
|
101
|
-
// )
|
|
102
|
-
// }
|
|
103
|
-
case ServerMessageType.RpcResponse: {
|
|
104
|
-
const { callId, result, streams, error } =
|
|
105
|
-
payload as ServerMessageTypePayload[ServerMessageType.RpcResponse]
|
|
106
|
-
return this.encode(
|
|
107
|
-
encodeNumber(messageType, 'Uint8'),
|
|
108
|
-
encodeNumber(callId, 'Uint32'),
|
|
109
|
-
encodeNumber(error ? 1 : 0, 'Uint8'),
|
|
110
|
-
error
|
|
111
|
-
? context.encoder.encode(error)
|
|
112
|
-
: context.encoder.encodeRPC(result, streams),
|
|
113
|
-
)
|
|
114
|
-
}
|
|
115
|
-
case ServerMessageType.RpcStreamResponse: {
|
|
116
|
-
const { callId } =
|
|
117
|
-
payload as ServerMessageTypePayload[ServerMessageType.RpcStreamResponse]
|
|
118
|
-
return this.encode(
|
|
119
|
-
encodeNumber(messageType, 'Uint8'),
|
|
120
|
-
encodeNumber(callId, 'Uint32'),
|
|
121
|
-
)
|
|
122
|
-
}
|
|
123
|
-
case ServerMessageType.RpcStreamChunk: {
|
|
124
|
-
const { callId, chunk } =
|
|
125
|
-
payload as ServerMessageTypePayload[ServerMessageType.RpcStreamChunk]
|
|
126
|
-
return this.encode(
|
|
127
|
-
encodeNumber(messageType, 'Uint8'),
|
|
128
|
-
encodeNumber(callId, 'Uint32'),
|
|
129
|
-
chunk,
|
|
130
|
-
)
|
|
131
|
-
}
|
|
132
|
-
case ServerMessageType.RpcStreamEnd: {
|
|
133
|
-
const { callId } =
|
|
134
|
-
payload as ServerMessageTypePayload[ServerMessageType.RpcStreamEnd]
|
|
135
|
-
return this.encode(
|
|
136
|
-
encodeNumber(messageType, 'Uint8'),
|
|
137
|
-
encodeNumber(callId, 'Uint32'),
|
|
138
|
-
)
|
|
139
|
-
}
|
|
140
|
-
case ServerMessageType.RpcStreamAbort: {
|
|
141
|
-
const { callId, reason } =
|
|
142
|
-
payload as ServerMessageTypePayload[ServerMessageType.RpcStreamAbort]
|
|
143
|
-
return this.encode(
|
|
144
|
-
encodeNumber(messageType, 'Uint8'),
|
|
145
|
-
encodeNumber(callId, 'Uint32'),
|
|
146
|
-
reason ? encodeText(reason) : Buffer.alloc(0),
|
|
147
|
-
)
|
|
148
|
-
}
|
|
149
|
-
case ServerMessageType.ClientStreamPull: {
|
|
150
|
-
const { size, streamId } =
|
|
151
|
-
payload as ServerMessageTypePayload[ServerMessageType.ClientStreamPull]
|
|
152
|
-
return this.encode(
|
|
153
|
-
encodeNumber(messageType, 'Uint8'),
|
|
154
|
-
encodeNumber(streamId, 'Uint32'),
|
|
155
|
-
encodeNumber(size, 'Uint32'),
|
|
156
|
-
)
|
|
157
|
-
}
|
|
158
|
-
case ServerMessageType.ClientStreamAbort: {
|
|
159
|
-
const { streamId, reason } =
|
|
160
|
-
payload as ServerMessageTypePayload[ServerMessageType.ClientStreamAbort]
|
|
161
|
-
return this.encode(
|
|
162
|
-
encodeNumber(messageType, 'Uint8'),
|
|
163
|
-
encodeNumber(streamId, 'Uint32'),
|
|
164
|
-
reason ? encodeText(reason) : Buffer.alloc(0),
|
|
165
|
-
)
|
|
166
|
-
}
|
|
167
|
-
case ServerMessageType.ServerStreamPush: {
|
|
168
|
-
const { streamId, chunk } =
|
|
169
|
-
payload as ServerMessageTypePayload[ServerMessageType.ServerStreamPush]
|
|
170
|
-
return this.encode(
|
|
171
|
-
encodeNumber(messageType, 'Uint8'),
|
|
172
|
-
encodeNumber(streamId, 'Uint32'),
|
|
173
|
-
chunk,
|
|
174
|
-
)
|
|
175
|
-
}
|
|
176
|
-
case ServerMessageType.ServerStreamEnd: {
|
|
177
|
-
const { streamId } =
|
|
178
|
-
payload as ServerMessageTypePayload[ServerMessageType.ServerStreamEnd]
|
|
179
|
-
return this.encode(
|
|
180
|
-
encodeNumber(messageType, 'Uint8'),
|
|
181
|
-
encodeNumber(streamId, 'Uint32'),
|
|
182
|
-
)
|
|
183
|
-
}
|
|
184
|
-
case ServerMessageType.ServerStreamAbort: {
|
|
185
|
-
const { streamId, reason } =
|
|
186
|
-
payload as ServerMessageTypePayload[ServerMessageType.ServerStreamAbort]
|
|
187
|
-
return this.encode(
|
|
188
|
-
encodeNumber(messageType, 'Uint8'),
|
|
189
|
-
encodeNumber(streamId, 'Uint32'),
|
|
190
|
-
reason ? encodeText(reason) : Buffer.alloc(0),
|
|
191
|
-
)
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
default:
|
|
195
|
-
throw new Error(`Unsupported message type: ${messageType}`)
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
}
|