@nmtjs/protocol 0.15.0-beta.37 → 0.15.0-beta.39
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/format.d.ts +18 -13
- package/dist/client/format.js +1 -0
- package/dist/client/format.js.map +1 -0
- package/dist/client/index.d.ts +4 -1
- package/dist/client/index.js +7 -4
- package/dist/client/index.js.map +1 -0
- package/dist/client/protocol.d.ts +102 -138
- package/dist/client/protocol.js +7 -336
- package/dist/client/protocol.js.map +1 -0
- package/dist/client/stream.d.ts +34 -19
- package/dist/client/stream.js +138 -46
- package/dist/client/stream.js.map +1 -0
- package/dist/client/versions/v1.d.ts +108 -0
- package/dist/client/versions/v1.js +128 -0
- package/dist/client/versions/v1.js.map +1 -0
- package/dist/common/binary.d.ts +5 -6
- package/dist/common/binary.js +22 -9
- package/dist/common/binary.js.map +1 -0
- package/dist/common/blob.d.ts +14 -8
- package/dist/common/blob.js +55 -30
- package/dist/common/blob.js.map +1 -0
- package/dist/common/constants.d.ts +2 -0
- package/dist/common/constants.js +2 -0
- package/dist/common/constants.js.map +1 -0
- package/dist/common/enums.d.ts +17 -7
- package/dist/common/enums.js +34 -14
- package/dist/common/enums.js.map +1 -0
- package/dist/common/index.d.ts +2 -0
- package/dist/common/index.js +7 -4
- package/dist/common/index.js.map +1 -0
- package/dist/common/types.d.ts +5 -24
- 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 +7 -0
- package/dist/common/utils.js.map +1 -0
- package/dist/server/format.d.ts +17 -23
- package/dist/server/format.js +13 -8
- package/dist/server/format.js.map +1 -0
- package/dist/server/index.d.ts +4 -6
- package/dist/server/index.js +9 -11
- package/dist/server/index.js.map +1 -0
- package/dist/server/protocol.d.ts +88 -114
- package/dist/server/protocol.js +5 -354
- package/dist/server/protocol.js.map +1 -0
- package/dist/server/stream.d.ts +5 -0
- package/dist/server/stream.js +31 -2
- package/dist/server/stream.js.map +1 -0
- package/dist/server/types.d.ts +29 -8
- package/dist/server/types.js +1 -0
- package/dist/server/types.js.map +1 -0
- package/dist/server/utils.d.ts +3 -6
- package/dist/server/utils.js +5 -4
- package/dist/server/utils.js.map +1 -0
- package/dist/server/versions/v1.d.ts +77 -0
- package/dist/server/versions/v1.js +119 -0
- package/dist/server/versions/v1.js.map +1 -0
- package/package.json +18 -24
- 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 +94 -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 +117 -0
- package/src/server/index.ts +10 -0
- package/src/server/protocol.ts +97 -0
- package/src/server/stream.ts +72 -0
- package/src/server/types.ts +42 -0
- package/src/server/utils.ts +22 -0
- package/src/server/versions/v1.ts +198 -0
- package/dist/client/events.d.ts +0 -15
- package/dist/client/events.js +0 -28
- package/dist/server/api.d.ts +0 -33
- package/dist/server/api.js +0 -7
- package/dist/server/connection.d.ts +0 -25
- package/dist/server/connection.js +0 -22
- package/dist/server/constants.d.ts +0 -4
- package/dist/server/constants.js +0 -2
- package/dist/server/injectables.d.ts +0 -14
- package/dist/server/injectables.js +0 -22
- package/dist/server/registry.d.ts +0 -3
- package/dist/server/registry.js +0 -3
- package/dist/server/transport.d.ts +0 -23
- package/dist/server/transport.js +0 -3
|
@@ -0,0 +1,72 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
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
|
|
@@ -0,0 +1,22 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,198 @@
|
|
|
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
|
+
}
|
package/dist/client/events.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type { Callback } from '@nmtjs/common';
|
|
2
|
-
export type EventMap = {
|
|
3
|
-
[K: string]: any[];
|
|
4
|
-
};
|
|
5
|
-
/**
|
|
6
|
-
* Very simple node-like event emitter wrapper around EventTarget
|
|
7
|
-
*/
|
|
8
|
-
export declare class EventEmitter<Events extends EventMap = EventMap, EventName extends Extract<keyof Events, string> = Extract<keyof Events, string>> {
|
|
9
|
-
#private;
|
|
10
|
-
static once<T extends EventEmitter, E extends T extends EventEmitter<any, infer Event> ? Event : never>(ee: T, event: E): Promise<unknown>;
|
|
11
|
-
on<E extends EventName>(event: E | (Object & string), listener: (...args: Events[E]) => void, options?: AddEventListenerOptions): () => void;
|
|
12
|
-
once<E extends EventName>(event: E | (Object & string), listener: (...args: Events[E]) => void, options?: AddEventListenerOptions): () => void;
|
|
13
|
-
off(event: EventName | (Object & string), listener: Callback): void;
|
|
14
|
-
emit<E extends EventName | (Object & string)>(event: E, ...args: E extends EventName ? Events[E] : any[]): boolean;
|
|
15
|
-
}
|
package/dist/client/events.js
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
// TODO: add errors and promise rejections handling
|
|
2
|
-
/**
|
|
3
|
-
* Very simple node-like event emitter wrapper around EventTarget
|
|
4
|
-
*/
|
|
5
|
-
export class EventEmitter {
|
|
6
|
-
static once(ee, event) {
|
|
7
|
-
return new Promise((resolve) => ee.once(event, resolve));
|
|
8
|
-
}
|
|
9
|
-
#target = new EventTarget();
|
|
10
|
-
#listeners = new Map();
|
|
11
|
-
on(event, listener, options) {
|
|
12
|
-
const wrapper = (event) => listener(...event.detail);
|
|
13
|
-
this.#listeners.set(listener, wrapper);
|
|
14
|
-
this.#target.addEventListener(event, wrapper, { ...options, once: false });
|
|
15
|
-
return () => this.#target.removeEventListener(event, wrapper);
|
|
16
|
-
}
|
|
17
|
-
once(event, listener, options) {
|
|
18
|
-
return this.on(event, listener, { ...options, once: true });
|
|
19
|
-
}
|
|
20
|
-
off(event, listener) {
|
|
21
|
-
const wrapper = this.#listeners.get(listener);
|
|
22
|
-
if (wrapper)
|
|
23
|
-
this.#target.removeEventListener(event, wrapper);
|
|
24
|
-
}
|
|
25
|
-
emit(event, ...args) {
|
|
26
|
-
return this.#target.dispatchEvent(new CustomEvent(event, { detail: args }));
|
|
27
|
-
}
|
|
28
|
-
}
|
package/dist/server/api.d.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import type { Async } from '@nmtjs/common';
|
|
2
|
-
import type { Container, Hook, MetadataStore } from '@nmtjs/core';
|
|
3
|
-
import type { Connection } from './connection.ts';
|
|
4
|
-
import { kIterableResponse } from './constants.ts';
|
|
5
|
-
export type ProtocolApiCallOptions = {
|
|
6
|
-
connection: Connection;
|
|
7
|
-
procedure: string;
|
|
8
|
-
container: Container;
|
|
9
|
-
payload: any;
|
|
10
|
-
signal: AbortSignal;
|
|
11
|
-
validateMetadata?: (metadata: MetadataStore) => void;
|
|
12
|
-
};
|
|
13
|
-
export type ProtocolAnyIterable<T> = ((signal: AbortSignal) => Async<AsyncIterable<T>>) | AsyncIterable<T>;
|
|
14
|
-
export interface ProtocolApiCallBaseResult<T = unknown> {
|
|
15
|
-
output: T;
|
|
16
|
-
}
|
|
17
|
-
export interface ProtocolApiCallIterableResult<Y = unknown, O = unknown> extends ProtocolApiCallBaseResult<O> {
|
|
18
|
-
[kIterableResponse]: true;
|
|
19
|
-
iterable: ProtocolAnyIterable<Y>;
|
|
20
|
-
onFinish?: () => void;
|
|
21
|
-
}
|
|
22
|
-
export type ProtocolApiCallResult = ProtocolApiCallBaseResult | ProtocolApiCallIterableResult;
|
|
23
|
-
export interface ProtocolApi {
|
|
24
|
-
call(options: ProtocolApiCallOptions): Promise<ProtocolApiCallResult>;
|
|
25
|
-
}
|
|
26
|
-
export declare function isIterableResult(value: ProtocolApiCallResult): value is ProtocolApiCallIterableResult;
|
|
27
|
-
export declare function createStreamResponse<Y, O>(iterable: ProtocolAnyIterable<Y>, output?: O, onFinish?: () => void): ProtocolApiCallIterableResult<Y, O>;
|
|
28
|
-
declare module '@nmtjs/core' {
|
|
29
|
-
interface HookType {
|
|
30
|
-
[Hook.OnConnect]: (connection: Connection) => any;
|
|
31
|
-
[Hook.OnDisconnect]: (connection: Connection) => any;
|
|
32
|
-
}
|
|
33
|
-
}
|
package/dist/server/api.js
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { kIterableResponse } from "./constants.js";
|
|
2
|
-
export function isIterableResult(value) {
|
|
3
|
-
return value && value[kIterableResponse] === true;
|
|
4
|
-
}
|
|
5
|
-
export function createStreamResponse(iterable, output = undefined, onFinish) {
|
|
6
|
-
return { [kIterableResponse]: true, iterable, output, onFinish };
|
|
7
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import type { Container } from '@nmtjs/core';
|
|
2
|
-
import type { BaseServerDecoder, BaseServerEncoder } from './format.ts';
|
|
3
|
-
import type { ProtocolClientStream, ProtocolServerStream } from './stream.ts';
|
|
4
|
-
export type ConnectionOptions<Data = unknown> = {
|
|
5
|
-
id?: string;
|
|
6
|
-
data: Data;
|
|
7
|
-
};
|
|
8
|
-
export declare class Connection<Data = unknown> {
|
|
9
|
-
readonly id: string;
|
|
10
|
-
readonly data: Data;
|
|
11
|
-
constructor(options: ConnectionOptions<Data>);
|
|
12
|
-
}
|
|
13
|
-
export declare class ConnectionContext {
|
|
14
|
-
streamId: number;
|
|
15
|
-
rpcs: Map<number, AbortController>;
|
|
16
|
-
clientStreams: Map<number, ProtocolClientStream>;
|
|
17
|
-
serverStreams: Map<number, ProtocolServerStream>;
|
|
18
|
-
rpcStreams: Map<number, AbortController>;
|
|
19
|
-
container: Container;
|
|
20
|
-
format: {
|
|
21
|
-
encoder: BaseServerEncoder;
|
|
22
|
-
decoder: BaseServerDecoder;
|
|
23
|
-
};
|
|
24
|
-
constructor(container: ConnectionContext['container'], format: ConnectionContext['format']);
|
|
25
|
-
}
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { randomUUID } from 'node:crypto';
|
|
2
|
-
export class Connection {
|
|
3
|
-
id;
|
|
4
|
-
data;
|
|
5
|
-
constructor(options) {
|
|
6
|
-
this.id = options.id ?? randomUUID();
|
|
7
|
-
this.data = options.data;
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
export class ConnectionContext {
|
|
11
|
-
streamId = 1;
|
|
12
|
-
rpcs = new Map();
|
|
13
|
-
clientStreams = new Map();
|
|
14
|
-
serverStreams = new Map();
|
|
15
|
-
rpcStreams = new Map();
|
|
16
|
-
container;
|
|
17
|
-
format;
|
|
18
|
-
constructor(container, format) {
|
|
19
|
-
this.container = container;
|
|
20
|
-
this.format = format;
|
|
21
|
-
}
|
|
22
|
-
}
|
package/dist/server/constants.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { Scope } from '@nmtjs/core';
|
|
2
|
-
import type { Connection } from './connection.ts';
|
|
3
|
-
export declare const ProtocolInjectables: {
|
|
4
|
-
readonly connection: import("@nmtjs/core").LazyInjectable<Connection<unknown>, Scope.Connection>;
|
|
5
|
-
readonly connectionData: import("@nmtjs/core").LazyInjectable<any, Scope.Connection>;
|
|
6
|
-
readonly connectionAbortSignal: import("@nmtjs/core").LazyInjectable<AbortSignal, Scope.Connection>;
|
|
7
|
-
readonly rpcClientAbortSignal: import("@nmtjs/core").LazyInjectable<AbortSignal, Scope.Call>;
|
|
8
|
-
readonly rpcTimeoutSignal: import("@nmtjs/core").LazyInjectable<AbortSignal, Scope.Call>;
|
|
9
|
-
readonly rpcAbortSignal: import("@nmtjs/core").FactoryInjectable<AbortSignal, {
|
|
10
|
-
rpcTimeoutSignal: import("@nmtjs/core").LazyInjectable<AbortSignal, Scope.Call>;
|
|
11
|
-
rpcClientAbortSignal: import("@nmtjs/core").LazyInjectable<AbortSignal, Scope.Call>;
|
|
12
|
-
connectionAbortSignal: import("@nmtjs/core").LazyInjectable<AbortSignal, Scope.Connection>;
|
|
13
|
-
}, Scope.Global, AbortSignal>;
|
|
14
|
-
};
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { createFactoryInjectable, createLazyInjectable, Scope, } from '@nmtjs/core';
|
|
2
|
-
const connection = createLazyInjectable(Scope.Connection, 'RPC connection');
|
|
3
|
-
const connectionData = createLazyInjectable(Scope.Connection, "RPC connection's data");
|
|
4
|
-
const connectionAbortSignal = createLazyInjectable(Scope.Connection, 'Connection abort signal');
|
|
5
|
-
const rpcClientAbortSignal = createLazyInjectable(Scope.Call, 'RPC client abort signal');
|
|
6
|
-
const rpcTimeoutSignal = createLazyInjectable(Scope.Call, 'RPC timeout signal');
|
|
7
|
-
const rpcAbortSignal = createFactoryInjectable({
|
|
8
|
-
dependencies: {
|
|
9
|
-
rpcTimeoutSignal,
|
|
10
|
-
rpcClientAbortSignal,
|
|
11
|
-
connectionAbortSignal,
|
|
12
|
-
},
|
|
13
|
-
factory: (ctx) => AbortSignal.any(Object.values(ctx)),
|
|
14
|
-
}, 'Any RPC abort signal');
|
|
15
|
-
export const ProtocolInjectables = {
|
|
16
|
-
connection,
|
|
17
|
-
connectionData,
|
|
18
|
-
connectionAbortSignal,
|
|
19
|
-
rpcClientAbortSignal,
|
|
20
|
-
rpcTimeoutSignal,
|
|
21
|
-
rpcAbortSignal,
|
|
22
|
-
};
|
package/dist/server/registry.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import type { BasePlugin, PluginContext } from '@nmtjs/core';
|
|
2
|
-
import type { ServerMessageType } from '../common/enums.ts';
|
|
3
|
-
import type { Connection } from './connection.ts';
|
|
4
|
-
import type { ProtocolFormat } from './format.ts';
|
|
5
|
-
import type { Protocol } from './protocol.ts';
|
|
6
|
-
import type { ProtocolRegistry } from './registry.ts';
|
|
7
|
-
import type { ProtocolSendMetadata } from './types.ts';
|
|
8
|
-
import { kTransportPlugin } from './constants.ts';
|
|
9
|
-
export interface Transport<T = unknown> {
|
|
10
|
-
start: () => Promise<void>;
|
|
11
|
-
stop: () => Promise<void>;
|
|
12
|
-
send: (connection: Connection<T>, messageType: ServerMessageType, buffer: ArrayBuffer, metadata: ProtocolSendMetadata) => any;
|
|
13
|
-
}
|
|
14
|
-
export interface TransportPluginContext extends PluginContext {
|
|
15
|
-
protocol: Protocol;
|
|
16
|
-
registry: ProtocolRegistry;
|
|
17
|
-
format: ProtocolFormat;
|
|
18
|
-
}
|
|
19
|
-
export interface TransportPlugin<Type = unknown, Options = unknown> extends BasePlugin<Transport<Type>, Options, TransportPluginContext> {
|
|
20
|
-
[kTransportPlugin]: any;
|
|
21
|
-
}
|
|
22
|
-
export declare const createTransport: <Type = unknown, Options = unknown>(name: string, init: TransportPlugin<Type, Options>["init"]) => TransportPlugin<Type, Options>;
|
|
23
|
-
export declare const isTransportPlugin: (plugin: BasePlugin<any, any, any>) => plugin is TransportPlugin;
|
package/dist/server/transport.js
DELETED