@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.
Files changed (64) hide show
  1. package/dist/client/events.js +2 -0
  2. package/dist/client/events.js.map +1 -1
  3. package/dist/client/format.js +2 -0
  4. package/dist/client/format.js.map +1 -1
  5. package/dist/client/index.js +3 -0
  6. package/dist/client/index.js.map +1 -1
  7. package/dist/client/protocol.js +205 -199
  8. package/dist/client/protocol.js.map +1 -1
  9. package/dist/client/stream.js +9 -12
  10. package/dist/client/stream.js.map +1 -1
  11. package/dist/client/types.js +3 -0
  12. package/dist/client/types.js.map +1 -0
  13. package/dist/common/binary.js +2 -0
  14. package/dist/common/binary.js.map +1 -1
  15. package/dist/common/blob.js +6 -2
  16. package/dist/common/blob.js.map +1 -1
  17. package/dist/common/enums.js +4 -1
  18. package/dist/common/enums.js.map +1 -1
  19. package/dist/common/index.js +2 -0
  20. package/dist/common/index.js.map +1 -1
  21. package/dist/common/types.js +2 -0
  22. package/dist/common/types.js.map +1 -1
  23. package/dist/server/api.js +4 -1
  24. package/dist/server/api.js.map +1 -1
  25. package/dist/server/connection.js +2 -0
  26. package/dist/server/connection.js.map +1 -1
  27. package/dist/server/constants.js +2 -0
  28. package/dist/server/constants.js.map +1 -1
  29. package/dist/server/format.js +2 -0
  30. package/dist/server/format.js.map +1 -1
  31. package/dist/server/index.js +3 -0
  32. package/dist/server/index.js.map +1 -1
  33. package/dist/server/injectables.js +2 -0
  34. package/dist/server/injectables.js.map +1 -1
  35. package/dist/server/protocol.js +154 -55
  36. package/dist/server/protocol.js.map +1 -1
  37. package/dist/server/registry.js +2 -0
  38. package/dist/server/registry.js.map +1 -1
  39. package/dist/server/stream.js +3 -1
  40. package/dist/server/stream.js.map +1 -1
  41. package/dist/server/transport.js +2 -0
  42. package/dist/server/transport.js.map +1 -1
  43. package/dist/server/types.js +3 -0
  44. package/dist/server/types.js.map +1 -0
  45. package/dist/server/utils.js +7 -2
  46. package/dist/server/utils.js.map +1 -1
  47. package/package.json +7 -7
  48. package/src/client/format.ts +34 -5
  49. package/src/client/index.ts +2 -0
  50. package/src/client/protocol.ts +381 -274
  51. package/src/client/stream.ts +7 -14
  52. package/src/client/types.ts +14 -0
  53. package/src/common/blob.ts +10 -3
  54. package/src/common/enums.ts +3 -1
  55. package/src/common/types.ts +28 -47
  56. package/src/server/api.ts +15 -1
  57. package/src/server/connection.ts +1 -5
  58. package/src/server/format.ts +14 -4
  59. package/src/server/index.ts +1 -0
  60. package/src/server/protocol.ts +208 -66
  61. package/src/server/stream.ts +2 -7
  62. package/src/server/transport.ts +5 -1
  63. package/src/server/types.ts +21 -0
  64. package/src/server/utils.ts +9 -2
@@ -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 return void 0
83
+ else break
91
84
  }
92
85
  }
93
86
 
94
87
  async push(chunk: T) {
95
- await this.#writer.write(chunk)
88
+ await this.writable.getWriter().write(chunk)
96
89
  }
97
90
 
98
91
  async end() {
99
- await this.#writer.close()
92
+ await this.writable.getWriter().close()
100
93
  }
101
94
 
102
95
  abort(error = new Error('Stream aborted')) {
103
- this.#writer.abort(error)
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
- start: Callback,
104
+ options?: Transformer<any, ArrayBuffer>,
112
105
  ) {
113
- super(start)
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
@@ -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: number
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 = -1,
23
+ size?: number,
18
24
  type = 'application/octet-stream',
19
25
  filename?: string,
20
26
  ) {
21
- if (size < -1 || size === 0) throw new Error('Blob size is invalid')
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 = {
@@ -16,11 +16,13 @@ export enum ServerMessageType {
16
16
  RpcResponse = 10,
17
17
  RpcStreamResponse = 11,
18
18
  RpcStreamChunk = 12,
19
- RpcStreamAbort = 13,
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
  }
@@ -1,9 +1,13 @@
1
- import type { ProtocolServerBlobStream } from '../client/stream.ts'
2
- import type {
3
- ProtocolBlob,
4
- ProtocolBlobInterface,
5
- ProtocolBlobMetadata,
6
- } from './blob.ts'
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: any
19
- payload?: never
20
- }
21
- | {
23
+ error: BaseProtocolError
24
+ },
25
+ {
22
26
  callId: number
23
- payload: any
24
- error?: never
25
- }
26
-
27
- export interface EncodeRPCContext {
28
- getStream: (id: number) => any
29
- addStream: (blob: ProtocolBlob) => {
30
- id: number
31
- metadata: ProtocolBlobMetadata
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 BaseClientEncoder {
46
- encode(data: any): ArrayBuffer
47
- encodeRPC(rpc: ProtocolRPC, context: EncodeRPCContext): ArrayBuffer
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 { Container } from '@nmtjs/core'
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
  }
@@ -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, ConnectionCall<ProtocolApiCallResult>>()
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>()
@@ -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(buffer: ArrayBuffer, context: DecodeRPCContext): ProtocolRPC
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(rpc: ProtocolRPCResponse, context: EncodeRPCContext): ArrayBuffer
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
 
@@ -7,4 +7,5 @@ export * from './protocol.ts'
7
7
  export * from './registry.ts'
8
8
  export * from './stream.ts'
9
9
  export * from './transport.ts'
10
+ export * from './types.ts'
10
11
  export * from './utils.ts'