@nmtjs/protocol 0.8.1 → 0.9.0
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/events.js +2 -0
- package/dist/client/events.js.map +1 -1
- package/dist/client/format.js +2 -0
- package/dist/client/format.js.map +1 -1
- package/dist/client/index.js +3 -0
- package/dist/client/index.js.map +1 -1
- package/dist/client/protocol.js +205 -199
- package/dist/client/protocol.js.map +1 -1
- package/dist/client/stream.js +9 -12
- package/dist/client/stream.js.map +1 -1
- package/dist/client/types.js +3 -0
- package/dist/client/types.js.map +1 -0
- package/dist/common/binary.js +2 -0
- package/dist/common/binary.js.map +1 -1
- package/dist/common/blob.js +6 -2
- package/dist/common/blob.js.map +1 -1
- package/dist/common/enums.js +4 -1
- package/dist/common/enums.js.map +1 -1
- package/dist/common/index.js +2 -0
- package/dist/common/index.js.map +1 -1
- package/dist/common/types.js +2 -0
- package/dist/common/types.js.map +1 -1
- package/dist/server/api.js +4 -1
- package/dist/server/api.js.map +1 -1
- package/dist/server/connection.js +2 -0
- package/dist/server/connection.js.map +1 -1
- package/dist/server/constants.js +2 -0
- package/dist/server/constants.js.map +1 -1
- package/dist/server/format.js +2 -0
- package/dist/server/format.js.map +1 -1
- package/dist/server/index.js +3 -0
- package/dist/server/index.js.map +1 -1
- package/dist/server/injectables.js +2 -0
- package/dist/server/injectables.js.map +1 -1
- package/dist/server/protocol.js +154 -55
- package/dist/server/protocol.js.map +1 -1
- package/dist/server/registry.js +2 -0
- package/dist/server/registry.js.map +1 -1
- package/dist/server/stream.js +3 -1
- package/dist/server/stream.js.map +1 -1
- package/dist/server/transport.js +2 -0
- package/dist/server/transport.js.map +1 -1
- package/dist/server/types.js +3 -0
- package/dist/server/types.js.map +1 -0
- package/dist/server/utils.js +7 -2
- package/dist/server/utils.js.map +1 -1
- package/package.json +7 -7
- package/src/client/format.ts +34 -5
- package/src/client/index.ts +2 -0
- package/src/client/protocol.ts +381 -274
- package/src/client/stream.ts +7 -14
- package/src/client/types.ts +14 -0
- package/src/common/blob.ts +10 -3
- package/src/common/enums.ts +3 -1
- package/src/common/types.ts +28 -47
- package/src/server/api.ts +15 -1
- package/src/server/connection.ts +1 -5
- package/src/server/format.ts +14 -4
- package/src/server/index.ts +1 -0
- package/src/server/protocol.ts +208 -66
- package/src/server/stream.ts +2 -7
- package/src/server/transport.ts +5 -1
- package/src/server/types.ts +21 -0
- package/src/server/utils.ts +9 -2
package/src/client/stream.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type Callback, defer } from '@nmtjs/common'
|
|
1
|
+
import { type Callback, defer, noopFn } from '@nmtjs/common'
|
|
2
2
|
import { concat, encodeText } from '../common/binary.ts'
|
|
3
3
|
import type { ProtocolBlobMetadata } from '../common/blob.ts'
|
|
4
4
|
|
|
@@ -75,32 +75,25 @@ export class ProtocolServerStream<T = any>
|
|
|
75
75
|
extends TransformStream<any, T>
|
|
76
76
|
implements ProtocolServerStreamInterface<T>
|
|
77
77
|
{
|
|
78
|
-
#writer: WritableStreamDefaultWriter
|
|
79
|
-
|
|
80
|
-
constructor(start?: Callback) {
|
|
81
|
-
super({ start })
|
|
82
|
-
this.#writer = this.writable.getWriter()
|
|
83
|
-
}
|
|
84
|
-
|
|
85
78
|
async *[Symbol.asyncIterator]() {
|
|
86
79
|
const reader = this.readable.getReader()
|
|
87
80
|
while (true) {
|
|
88
81
|
const { done, value } = await reader.read()
|
|
89
82
|
if (!done) yield value
|
|
90
|
-
else
|
|
83
|
+
else break
|
|
91
84
|
}
|
|
92
85
|
}
|
|
93
86
|
|
|
94
87
|
async push(chunk: T) {
|
|
95
|
-
await this
|
|
88
|
+
await this.writable.getWriter().write(chunk)
|
|
96
89
|
}
|
|
97
90
|
|
|
98
91
|
async end() {
|
|
99
|
-
await this
|
|
92
|
+
await this.writable.getWriter().close()
|
|
100
93
|
}
|
|
101
94
|
|
|
102
95
|
abort(error = new Error('Stream aborted')) {
|
|
103
|
-
this
|
|
96
|
+
this.writable.getWriter().abort(error)
|
|
104
97
|
}
|
|
105
98
|
}
|
|
106
99
|
|
|
@@ -108,8 +101,8 @@ export class ProtocolServerBlobStream extends ProtocolServerStream<ArrayBuffer>
|
|
|
108
101
|
constructor(
|
|
109
102
|
readonly id: number,
|
|
110
103
|
readonly metadata: ProtocolBlobMetadata,
|
|
111
|
-
|
|
104
|
+
options?: Transformer<any, ArrayBuffer>,
|
|
112
105
|
) {
|
|
113
|
-
super(
|
|
106
|
+
super(options)
|
|
114
107
|
}
|
|
115
108
|
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ProtocolBlob, ProtocolBlobInterface } from '../common/blob.ts'
|
|
2
|
+
import type { ProtocolServerBlobStream } from './stream.ts'
|
|
3
|
+
|
|
4
|
+
export type InputType<T> = T extends ProtocolBlobInterface
|
|
5
|
+
? ProtocolBlob
|
|
6
|
+
: T extends object
|
|
7
|
+
? { [K in keyof T]: InputType<T[K]> }
|
|
8
|
+
: T
|
|
9
|
+
|
|
10
|
+
export type OutputType<T> = T extends ProtocolBlobInterface
|
|
11
|
+
? ProtocolServerBlobStream
|
|
12
|
+
: T extends object
|
|
13
|
+
? { [K in keyof T]: OutputType<T[K]> }
|
|
14
|
+
: T
|
package/src/common/blob.ts
CHANGED
|
@@ -1,24 +1,31 @@
|
|
|
1
|
+
export const BlobKey: unique symbol = Symbol.for('neemata:BlobKey')
|
|
2
|
+
export type BlobKey = typeof BlobKey
|
|
3
|
+
|
|
1
4
|
export type ProtocolBlobMetadata = {
|
|
2
5
|
type: string
|
|
3
|
-
size
|
|
6
|
+
size?: number
|
|
4
7
|
filename?: string
|
|
5
8
|
}
|
|
6
9
|
|
|
7
10
|
export interface ProtocolBlobInterface {
|
|
8
11
|
readonly metadata: ProtocolBlobMetadata
|
|
12
|
+
readonly [BlobKey]: true
|
|
9
13
|
}
|
|
10
14
|
|
|
11
15
|
export class ProtocolBlob implements ProtocolBlobInterface {
|
|
16
|
+
readonly [BlobKey] = true
|
|
17
|
+
|
|
12
18
|
public readonly metadata: ProtocolBlobMetadata
|
|
13
19
|
public readonly source: any
|
|
14
20
|
|
|
15
21
|
constructor(
|
|
16
22
|
source: any,
|
|
17
|
-
size
|
|
23
|
+
size?: number,
|
|
18
24
|
type = 'application/octet-stream',
|
|
19
25
|
filename?: string,
|
|
20
26
|
) {
|
|
21
|
-
if (size
|
|
27
|
+
if (typeof size !== 'undefined' && size <= 0)
|
|
28
|
+
throw new Error('Blob size is invalid')
|
|
22
29
|
|
|
23
30
|
this.source = source
|
|
24
31
|
this.metadata = {
|
package/src/common/enums.ts
CHANGED
|
@@ -16,11 +16,13 @@ export enum ServerMessageType {
|
|
|
16
16
|
RpcResponse = 10,
|
|
17
17
|
RpcStreamResponse = 11,
|
|
18
18
|
RpcStreamChunk = 12,
|
|
19
|
-
|
|
19
|
+
RpcStreamEnd = 13,
|
|
20
|
+
RpcStreamAbort = 14,
|
|
20
21
|
|
|
21
22
|
ServerStreamPush = 20,
|
|
22
23
|
ServerStreamEnd = 21,
|
|
23
24
|
ServerStreamAbort = 22,
|
|
25
|
+
|
|
24
26
|
ClientStreamAbort = 23,
|
|
25
27
|
ClientStreamPull = 24,
|
|
26
28
|
}
|
package/src/common/types.ts
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import type { OneOf } from '@nmtjs/common'
|
|
2
|
+
import type { ProtocolBlob, ProtocolBlobMetadata } from './blob.ts'
|
|
3
|
+
|
|
4
|
+
type Stream = any
|
|
5
|
+
|
|
6
|
+
export interface BaseProtocolError {
|
|
7
|
+
code: string
|
|
8
|
+
message: string
|
|
9
|
+
data?: any
|
|
10
|
+
}
|
|
7
11
|
|
|
8
12
|
export type ProtocolRPC = {
|
|
9
13
|
callId: number
|
|
@@ -12,49 +16,26 @@ export type ProtocolRPC = {
|
|
|
12
16
|
payload: any
|
|
13
17
|
}
|
|
14
18
|
|
|
15
|
-
export type ProtocolRPCResponse =
|
|
16
|
-
|
|
19
|
+
export type ProtocolRPCResponse<T = Stream> = OneOf<
|
|
20
|
+
[
|
|
21
|
+
{
|
|
17
22
|
callId: number
|
|
18
|
-
error:
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
| {
|
|
23
|
+
error: BaseProtocolError
|
|
24
|
+
},
|
|
25
|
+
{
|
|
22
26
|
callId: number
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export interface DecodeRPCContext {
|
|
36
|
-
getStream: (id: number) => any
|
|
37
|
-
addStream: (id: number, metadata: ProtocolBlobMetadata) => any
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export interface BaseClientDecoder {
|
|
41
|
-
decode(buffer: ArrayBuffer): any
|
|
42
|
-
decodeRPC(buffer: ArrayBuffer, context: DecodeRPCContext): ProtocolRPCResponse
|
|
27
|
+
result: any
|
|
28
|
+
streams: Record<number, T>
|
|
29
|
+
},
|
|
30
|
+
]
|
|
31
|
+
>
|
|
32
|
+
|
|
33
|
+
export interface EncodeRPCContext<T = Stream> {
|
|
34
|
+
getStream: (id: number) => T
|
|
35
|
+
addStream: (blob: ProtocolBlob) => T
|
|
43
36
|
}
|
|
44
37
|
|
|
45
|
-
export interface
|
|
46
|
-
|
|
47
|
-
|
|
38
|
+
export interface DecodeRPCContext<T = Stream> {
|
|
39
|
+
getStream: (id: number, callId: number) => T
|
|
40
|
+
addStream: (id: number, callId: number, metadata: ProtocolBlobMetadata) => T
|
|
48
41
|
}
|
|
49
|
-
|
|
50
|
-
export type InputType<T> = T extends ProtocolBlobInterface
|
|
51
|
-
? ProtocolBlob
|
|
52
|
-
: T extends object
|
|
53
|
-
? { [K in keyof T]: InputType<T[K]> }
|
|
54
|
-
: T
|
|
55
|
-
|
|
56
|
-
export type OutputType<T> = T extends ProtocolBlobInterface
|
|
57
|
-
? ProtocolServerBlobStream
|
|
58
|
-
: T extends object
|
|
59
|
-
? { [K in keyof T]: OutputType<T[K]> }
|
|
60
|
-
: T
|
package/src/server/api.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
Container,
|
|
3
|
+
Metadata,
|
|
4
|
+
MetadataKey,
|
|
5
|
+
MetadataStore,
|
|
6
|
+
} from '@nmtjs/core'
|
|
2
7
|
import type { Hook } from '@nmtjs/core'
|
|
3
8
|
import type { Connection } from './connection.ts'
|
|
4
9
|
|
|
@@ -9,6 +14,7 @@ export type ProtocolApiCallOptions = {
|
|
|
9
14
|
container: Container
|
|
10
15
|
payload: any
|
|
11
16
|
signal: AbortSignal
|
|
17
|
+
metadata?: (metadata: MetadataStore) => void
|
|
12
18
|
}
|
|
13
19
|
|
|
14
20
|
export type ProtocolAnyIterable<T> =
|
|
@@ -34,6 +40,14 @@ export type ProtocolApiCallResult =
|
|
|
34
40
|
| ProtocolApiCallSubscriptionResult
|
|
35
41
|
| ProtocolApiCallIterableResult
|
|
36
42
|
|
|
43
|
+
export const isIterableResult = (
|
|
44
|
+
result: ProtocolApiCallResult,
|
|
45
|
+
): result is ProtocolApiCallIterableResult => 'iterable' in result
|
|
46
|
+
|
|
47
|
+
export const isSubscriptionResult = (
|
|
48
|
+
result: ProtocolApiCallResult,
|
|
49
|
+
): result is ProtocolApiCallSubscriptionResult => 'subscription' in result
|
|
50
|
+
|
|
37
51
|
export interface ProtocolApi {
|
|
38
52
|
call(options: ProtocolApiCallOptions): Promise<ProtocolApiCallResult>
|
|
39
53
|
}
|
package/src/server/connection.ts
CHANGED
|
@@ -20,13 +20,9 @@ export class Connection<Data = unknown> {
|
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
export type ConnectionCall<T = unknown> = InteractivePromise<T> & {
|
|
24
|
-
abort: AbortController['abort']
|
|
25
|
-
}
|
|
26
|
-
|
|
27
23
|
export class ConnectionContext {
|
|
28
24
|
streamId = 1
|
|
29
|
-
calls = new Map<number,
|
|
25
|
+
calls = new Map<number, AbortController>()
|
|
30
26
|
clientStreams = new Map<number, ProtocolClientStream>()
|
|
31
27
|
serverStreams = new Map<number, ProtocolServerStream>()
|
|
32
28
|
rpcStreams = new Map<number, AbortController>()
|
package/src/server/format.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { OneOf } from '@nmtjs/common'
|
|
1
2
|
import { match, type Pattern } from '@nmtjs/core'
|
|
2
3
|
import type {
|
|
3
4
|
DecodeRPCContext,
|
|
@@ -5,17 +6,26 @@ import type {
|
|
|
5
6
|
ProtocolRPC,
|
|
6
7
|
ProtocolRPCResponse,
|
|
7
8
|
} from '../common/types.ts'
|
|
9
|
+
import type { ProtocolClientStream, ProtocolServerStream } from './stream.ts'
|
|
8
10
|
|
|
9
11
|
export interface BaseServerDecoder {
|
|
10
12
|
accept: Pattern[]
|
|
11
13
|
decode(buffer: ArrayBuffer): any
|
|
12
|
-
decodeRPC(
|
|
14
|
+
decodeRPC(
|
|
15
|
+
buffer: ArrayBuffer,
|
|
16
|
+
context: DecodeRPCContext<ProtocolClientStream>,
|
|
17
|
+
): ProtocolRPC
|
|
13
18
|
}
|
|
14
19
|
|
|
15
20
|
export interface BaseServerEncoder {
|
|
16
21
|
contentType: string
|
|
17
22
|
encode(data: any): ArrayBuffer
|
|
18
|
-
encodeRPC(
|
|
23
|
+
encodeRPC(
|
|
24
|
+
rpc: OneOf<
|
|
25
|
+
[{ callId: number; error: any }, { callId: number; result: any }]
|
|
26
|
+
>,
|
|
27
|
+
context: EncodeRPCContext<ProtocolServerStream>,
|
|
28
|
+
): ArrayBuffer
|
|
19
29
|
}
|
|
20
30
|
|
|
21
31
|
export abstract class BaseServerFormat
|
|
@@ -27,12 +37,12 @@ export abstract class BaseServerFormat
|
|
|
27
37
|
abstract encode(data: any): ArrayBuffer
|
|
28
38
|
abstract encodeRPC(
|
|
29
39
|
rpc: ProtocolRPCResponse,
|
|
30
|
-
context: EncodeRPCContext
|
|
40
|
+
context: EncodeRPCContext<ProtocolServerStream>,
|
|
31
41
|
): ArrayBuffer
|
|
32
42
|
abstract decode(buffer: ArrayBuffer): any
|
|
33
43
|
abstract decodeRPC(
|
|
34
44
|
buffer: ArrayBuffer,
|
|
35
|
-
context: DecodeRPCContext
|
|
45
|
+
context: DecodeRPCContext<ProtocolClientStream>,
|
|
36
46
|
): ProtocolRPC
|
|
37
47
|
}
|
|
38
48
|
|