@nmtjs/client 0.15.2 → 0.16.0-beta.1

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 (56) hide show
  1. package/dist/client.d.ts +64 -0
  2. package/dist/client.js +97 -0
  3. package/dist/client.js.map +1 -0
  4. package/dist/clients/runtime.d.ts +6 -12
  5. package/dist/clients/runtime.js +58 -57
  6. package/dist/clients/runtime.js.map +1 -1
  7. package/dist/clients/static.d.ts +4 -9
  8. package/dist/clients/static.js +20 -20
  9. package/dist/clients/static.js.map +1 -1
  10. package/dist/core.d.ts +33 -83
  11. package/dist/core.js +305 -690
  12. package/dist/core.js.map +1 -1
  13. package/dist/events.d.ts +0 -1
  14. package/dist/events.js +74 -11
  15. package/dist/events.js.map +1 -1
  16. package/dist/index.d.ts +4 -0
  17. package/dist/index.js +4 -0
  18. package/dist/index.js.map +1 -1
  19. package/dist/layers/ping.d.ts +6 -0
  20. package/dist/layers/ping.js +65 -0
  21. package/dist/layers/ping.js.map +1 -0
  22. package/dist/layers/rpc.d.ts +19 -0
  23. package/dist/layers/rpc.js +521 -0
  24. package/dist/layers/rpc.js.map +1 -0
  25. package/dist/layers/streams.d.ts +20 -0
  26. package/dist/layers/streams.js +194 -0
  27. package/dist/layers/streams.js.map +1 -0
  28. package/dist/plugins/browser.js +28 -9
  29. package/dist/plugins/browser.js.map +1 -1
  30. package/dist/plugins/heartbeat.js +10 -10
  31. package/dist/plugins/heartbeat.js.map +1 -1
  32. package/dist/plugins/index.d.ts +1 -1
  33. package/dist/plugins/index.js +0 -1
  34. package/dist/plugins/index.js.map +1 -1
  35. package/dist/plugins/reconnect.js +11 -94
  36. package/dist/plugins/reconnect.js.map +1 -1
  37. package/dist/plugins/types.d.ts +27 -11
  38. package/dist/transport.d.ts +49 -31
  39. package/dist/types.d.ts +21 -5
  40. package/package.json +10 -10
  41. package/src/client.ts +216 -0
  42. package/src/clients/runtime.ts +93 -79
  43. package/src/clients/static.ts +46 -38
  44. package/src/core.ts +394 -901
  45. package/src/events.ts +113 -14
  46. package/src/index.ts +4 -0
  47. package/src/layers/ping.ts +99 -0
  48. package/src/layers/rpc.ts +725 -0
  49. package/src/layers/streams.ts +277 -0
  50. package/src/plugins/browser.ts +39 -9
  51. package/src/plugins/heartbeat.ts +10 -10
  52. package/src/plugins/index.ts +8 -1
  53. package/src/plugins/reconnect.ts +12 -119
  54. package/src/plugins/types.ts +30 -13
  55. package/src/transport.ts +75 -46
  56. package/src/types.ts +33 -8
@@ -0,0 +1,64 @@
1
+ import type { TypeProvider } from '@nmtjs/common';
2
+ import type { TAnyRouterContract } from '@nmtjs/contract';
3
+ import type { ProtocolBlobMetadata, ProtocolVersion } from '@nmtjs/protocol';
4
+ import type { BaseClientFormat } from '@nmtjs/protocol/client';
5
+ import { ProtocolBlob } from '@nmtjs/protocol';
6
+ import type { ConnectionState } from './core.ts';
7
+ import type { PingLayerApi } from './layers/ping.ts';
8
+ import type { RpcLayerApi } from './layers/rpc.ts';
9
+ import type { StreamLayerApi } from './layers/streams.ts';
10
+ import type { ClientPlugin } from './plugins/types.ts';
11
+ import type { BaseClientTransformer } from './transformers.ts';
12
+ import type { ClientTransportFactory } from './transport.ts';
13
+ import type { AnyResolvedContractRouter, ClientCallers, ResolveAPIRouterRoutes } from './types.ts';
14
+ import { ClientCore } from './core.ts';
15
+ export interface ClientOptions<RouterContract extends TAnyRouterContract = TAnyRouterContract, SafeCall extends boolean = false> {
16
+ contract: RouterContract;
17
+ protocol: ProtocolVersion;
18
+ format: BaseClientFormat;
19
+ application?: string;
20
+ timeout?: number;
21
+ plugins?: ClientPlugin[];
22
+ safe?: SafeCall;
23
+ }
24
+ export type BaseClientOptions<RouterContract extends TAnyRouterContract = TAnyRouterContract, SafeCall extends boolean = false> = ClientOptions<RouterContract, SafeCall>;
25
+ export interface ClientCallersFactory<Routes extends AnyResolvedContractRouter, SafeCall extends boolean> {
26
+ call: ClientCallers<Routes, SafeCall, false>;
27
+ stream: ClientCallers<Routes, SafeCall, true>;
28
+ }
29
+ type ClientRoutes<RouterContract extends TAnyRouterContract, InputTypeProvider extends TypeProvider, OutputTypeProvider extends TypeProvider> = ResolveAPIRouterRoutes<RouterContract, InputTypeProvider, OutputTypeProvider>;
30
+ export declare class Client<TransportFactory extends ClientTransportFactory<any, any> = ClientTransportFactory<any, any>, RouterContract extends TAnyRouterContract = TAnyRouterContract, SafeCall extends boolean = false, InputTypeProvider extends TypeProvider = TypeProvider, OutputTypeProvider extends TypeProvider = TypeProvider> {
31
+ readonly options: ClientOptions<RouterContract, SafeCall>;
32
+ readonly transportFactory: TransportFactory;
33
+ readonly transportOptions: TransportFactory extends ClientTransportFactory<any, infer Options> ? Options : never;
34
+ _: {
35
+ routes: ResolveAPIRouterRoutes<RouterContract, InputTypeProvider, OutputTypeProvider>;
36
+ safe: SafeCall;
37
+ };
38
+ readonly core: ClientCore;
39
+ protected readonly rpcLayer: RpcLayerApi;
40
+ protected readonly streamLayer: StreamLayerApi;
41
+ protected readonly pingLayer: PingLayerApi;
42
+ protected readonly transformer: BaseClientTransformer;
43
+ readonly call: ClientCallers<ClientRoutes<RouterContract, InputTypeProvider, OutputTypeProvider>, SafeCall, false>;
44
+ readonly stream: ClientCallers<ClientRoutes<RouterContract, InputTypeProvider, OutputTypeProvider>, SafeCall, true>;
45
+ readonly on: ClientCore['on'];
46
+ readonly once: ClientCore['once'];
47
+ readonly off: ClientCore['off'];
48
+ constructor(options: ClientOptions<RouterContract, SafeCall>, transportFactory: TransportFactory, transportOptions: TransportFactory extends ClientTransportFactory<any, infer Options> ? Options : never, transformer: BaseClientTransformer, buildCallers: (rpc: RpcLayerApi) => {
49
+ call: unknown;
50
+ stream: unknown;
51
+ });
52
+ get state(): ConnectionState;
53
+ get transportType(): import("@nmtjs/protocol").ConnectionType;
54
+ get lastDisconnectReason(): import("./transport.ts").ClientDisconnectReason;
55
+ get auth(): any;
56
+ set auth(value: any);
57
+ isDisposed(): boolean;
58
+ connect(): Promise<void>;
59
+ disconnect(): Promise<void>;
60
+ ping(timeout: number, signal?: AbortSignal): Promise<void>;
61
+ blob(source: Blob | ReadableStream | string | AsyncIterable<Uint8Array>, metadata?: ProtocolBlobMetadata): ProtocolBlob;
62
+ dispose(): void;
63
+ }
64
+ export {};
package/dist/client.js ADDED
@@ -0,0 +1,97 @@
1
+ import { noopFn } from '@nmtjs/common';
2
+ import { ProtocolBlob } from '@nmtjs/protocol';
3
+ import { ClientCore } from './core.js';
4
+ import { createPingLayer } from './layers/ping.js';
5
+ import { createRpcLayer } from './layers/rpc.js';
6
+ import { createStreamLayer } from './layers/streams.js';
7
+ export class Client {
8
+ options;
9
+ transportFactory;
10
+ transportOptions;
11
+ _;
12
+ core;
13
+ rpcLayer;
14
+ streamLayer;
15
+ pingLayer;
16
+ transformer;
17
+ call;
18
+ stream;
19
+ on;
20
+ once;
21
+ off;
22
+ constructor(options, transportFactory, transportOptions, transformer, buildCallers) {
23
+ this.options = options;
24
+ this.transportFactory = transportFactory;
25
+ this.transportOptions = transportOptions;
26
+ this.transformer = transformer;
27
+ const transport = this.transportFactory({ protocol: this.options.protocol, format: this.options.format }, this.transportOptions);
28
+ const coreOptions = {
29
+ protocol: this.options.protocol,
30
+ format: this.options.format,
31
+ application: this.options.application,
32
+ };
33
+ this.core = new ClientCore(coreOptions, transport);
34
+ this.streamLayer = createStreamLayer(this.core);
35
+ this.rpcLayer = createRpcLayer(this.core, this.streamLayer, transformer, {
36
+ timeout: this.options.timeout,
37
+ safe: this.options.safe,
38
+ });
39
+ this.pingLayer = createPingLayer(this.core);
40
+ this.core.setMessageContextFactory(() => ({
41
+ encoder: this.core.format,
42
+ decoder: this.core.format,
43
+ transport: {
44
+ send: (buffer) => {
45
+ this.core.send(buffer).catch(noopFn);
46
+ },
47
+ },
48
+ streamId: () => this.streamLayer.getStreamId(),
49
+ addClientStream: (blob) => this.streamLayer.addClientStream(blob),
50
+ addServerStream: (streamId, metadata) => this.streamLayer.createServerBlobStream(streamId, metadata),
51
+ }));
52
+ this.core.initPlugins(this.options.plugins, {
53
+ core: this.core,
54
+ ping: this.pingLayer,
55
+ });
56
+ const callers = buildCallers(this.rpcLayer);
57
+ this.call = callers.call;
58
+ this.stream = callers.stream;
59
+ this.on = this.core.on.bind(this.core);
60
+ this.once = this.core.once.bind(this.core);
61
+ this.off = this.core.off.bind(this.core);
62
+ }
63
+ get state() {
64
+ return this.core.state;
65
+ }
66
+ get transportType() {
67
+ return this.core.transportType;
68
+ }
69
+ get lastDisconnectReason() {
70
+ return this.core.lastDisconnectReason;
71
+ }
72
+ get auth() {
73
+ return this.core.auth;
74
+ }
75
+ set auth(value) {
76
+ this.core.auth = value;
77
+ }
78
+ isDisposed() {
79
+ return this.core.isDisposed();
80
+ }
81
+ connect() {
82
+ return this.core.connect();
83
+ }
84
+ disconnect() {
85
+ return this.core.disconnect();
86
+ }
87
+ ping(timeout, signal) {
88
+ return this.pingLayer.ping(timeout, signal);
89
+ }
90
+ blob(source, metadata) {
91
+ return ProtocolBlob.from(source, metadata);
92
+ }
93
+ dispose() {
94
+ this.core.dispose();
95
+ }
96
+ }
97
+ //# sourceMappingURL=client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,MAAM,EAAE,MAAM,eAAe,CAAA;AACtC,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AAc9C,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAA;AACtC,OAAO,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAA;AAClD,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;AAChD,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAA;AAsCvD,MAAM,OAAO,MAAM;IAyCN,OAAO;IACP,gBAAgB;IAChB,gBAAgB;IAjC3B,CAAC,CAOA;IAEQ,IAAI,CAAY;IACN,QAAQ,CAAa;IACrB,W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AA;IAAA,CACzC;IAED,IAAI,KAAK,GAAoB;QAC3B,OAAO,IAAI,CAAC,IAAI,CAAC,KAAK,CAAA;IAAA,CACvB;IAED,IAAI,aAAa,GAAG;QAClB,OAAO,IAAI,CAAC,IAAI,CAAC,aAAa,CAAA;IAAA,CAC/B;IAED,IAAI,oBAAoB,GAAG;QACzB,OAAO,IAAI,CAAC,IAAI,CAAC,oBAAoB,CAAA;IAAA,CACtC;IAED,IAAI,IAAI,GAAG;QACT,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAA;IAAA,CACtB;IAED,IAAI,IAAI,CAAC,KAAU,EAAE;QACnB,IAAI,CAAC,IAAI,CAAC,IAAI,GAAG,KAAK,CAAA;IAAA,CACvB;IAED,UAAU,GAAG;QACX,OAAO,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE,CAAA;IAAA,CAC9B;IAED,OAAO,GAAG;QACR,OAAO,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,CAAA;IAAA,CAC3B;IAED,UAAU,GAAG;QACX,OAAO,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE,CAAA;IAAA,CAC9B;IAED,IAAI,CAAC,OAAe,EAAE,MAAoB,EAAE;QAC1C,OAAO,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,OAAO,EAAE,MAAM,CAAC,CAAA;IAAA,CAC5C;IAED,IAAI,CACF,MAAkE,EAClE,QAA+B,EAC/B;QACA,OAAO,YAAY,CAAC,IAAI,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAA;IAAA,CAC3C;IAED,OAAO,GAAG;QACR,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,CAAA;IAAA,CACpB;CACF"}
@@ -1,20 +1,14 @@
1
1
  import type { TAnyRouterContract } from '@nmtjs/contract';
2
- import type { BaseClientOptions } from '../core.ts';
2
+ import type { BaseClientOptions } from '../client.ts';
3
3
  import type { ClientTransportFactory } from '../transport.ts';
4
- import type { ClientCallers, RuntimeInputContractTypeProvider, RuntimeOutputContractTypeProvider } from '../types.ts';
5
- import { BaseClient } from '../core.ts';
4
+ import type { RuntimeInputContractTypeProvider, RuntimeOutputContractTypeProvider } from '../types.ts';
5
+ import { Client } from '../client.ts';
6
6
  export declare class RuntimeContractTransformer {
7
7
  #private;
8
8
  constructor(router: TAnyRouterContract);
9
- encode(_procedure: string, payload: any): unknown;
10
- decode(_procedure: string, payload: any): unknown;
9
+ encode(procedure: string, payload: any): unknown;
10
+ decode(procedure: string, payload: any): unknown;
11
11
  }
12
- export declare class RuntimeClient<Transport extends ClientTransportFactory<any, any> = ClientTransportFactory<any, any>, RouterContract extends TAnyRouterContract = TAnyRouterContract, SafeCall extends boolean = false> extends BaseClient<Transport, RouterContract, SafeCall, RuntimeInputContractTypeProvider, RuntimeOutputContractTypeProvider> {
13
- #private;
14
- protected readonly transformer: RuntimeContractTransformer;
12
+ export declare class RuntimeClient<Transport extends ClientTransportFactory<any, any> = ClientTransportFactory<any, any>, RouterContract extends TAnyRouterContract = TAnyRouterContract, SafeCall extends boolean = false> extends Client<Transport, RouterContract, SafeCall, RuntimeInputContractTypeProvider, RuntimeOutputContractTypeProvider> {
15
13
  constructor(options: BaseClientOptions<RouterContract, SafeCall>, transport: Transport, transportOptions: Transport extends ClientTransportFactory<any, infer Options> ? Options : never);
16
- get call(): ClientCallers<this['_']['routes'], SafeCall, false>;
17
- get stream(): ClientCallers<this['_']['routes'], SafeCall, true>;
18
- protected resolveProcedures(router: TAnyRouterContract, path?: string[]): void;
19
- protected buildCallers(): ClientCallers<this['_']['routes'], SafeCall, boolean>;
20
14
  }
@@ -1,81 +1,82 @@
1
1
  import { IsProcedureContract, IsRouterContract } from '@nmtjs/contract';
2
- import { BaseClient } from '../core.js';
2
+ import { Client } from '../client.js';
3
3
  export class RuntimeContractTransformer {
4
4
  #procedures = new Map();
5
5
  constructor(router) {
6
- const registerProcedures = (r, path = []) => {
7
- if (IsRouterContract(r)) {
8
- for (const [key, route] of Object.entries(r.routes)) {
9
- registerProcedures(route, [...path, key]);
6
+ const registerProcedures = (route, path = []) => {
7
+ if (IsRouterContract(route)) {
8
+ for (const [key, child] of Object.entries(route.routes)) {
9
+ registerProcedures(child, [...path, key]);
10
10
  }
11
+ return;
11
12
  }
12
- else if (IsProcedureContract(r)) {
13
- const fullName = [...path].join('/');
14
- this.#procedures.set(fullName, r);
13
+ if (IsProcedureContract(route)) {
14
+ this.#procedures.set(path.join('/'), route);
15
15
  }
16
16
  };
17
17
  registerProcedures(router);
18
18
  }
19
- encode(_procedure, payload) {
20
- const procedure = this.#procedures.get(_procedure);
21
- if (!procedure)
22
- throw new Error(`Procedure not found: ${_procedure}`);
23
- return procedure.input.encode(payload);
19
+ encode(procedure, payload) {
20
+ const contract = this.#procedures.get(procedure);
21
+ if (!contract)
22
+ throw new Error(`Procedure not found: ${procedure}`);
23
+ return contract.input.encode(payload);
24
24
  }
25
- decode(_procedure, payload) {
26
- const procedure = this.#procedures.get(_procedure);
27
- if (!procedure)
28
- throw new Error(`Procedure not found: ${_procedure}`);
29
- return procedure.output.decode(payload);
25
+ decode(procedure, payload) {
26
+ const contract = this.#procedures.get(procedure);
27
+ if (!contract)
28
+ throw new Error(`Procedure not found: ${procedure}`);
29
+ return contract.output.decode(payload);
30
30
  }
31
31
  }
32
- export class RuntimeClient extends BaseClient {
33
- transformer;
34
- #procedures = new Map();
35
- #callers;
36
- constructor(options, transport, transportOptions) {
37
- super(options, transport, transportOptions);
38
- this.resolveProcedures(this.options.contract);
39
- this.transformer = new RuntimeContractTransformer(this.options.contract);
40
- this.#callers = this.buildCallers();
41
- }
42
- get call() {
43
- return this.#callers;
44
- }
45
- get stream() {
46
- return this.#callers;
32
+ const assignNested = (root, name, value) => {
33
+ const parts = name.split('/');
34
+ let current = root;
35
+ for (let i = 0; i < parts.length; i++) {
36
+ const part = parts[i];
37
+ if (i === parts.length - 1) {
38
+ current[part] = value;
39
+ }
40
+ else {
41
+ current[part] = current[part] ?? Object.create(null);
42
+ current = current[part];
43
+ }
47
44
  }
48
- resolveProcedures(router, path = []) {
45
+ };
46
+ const buildRuntimeCallers = (rpc, contract) => {
47
+ const procedures = new Map();
48
+ const resolveProcedures = (router, path = []) => {
49
49
  for (const [key, route] of Object.entries(router.routes)) {
50
50
  if (IsRouterContract(route)) {
51
- this.resolveProcedures(route, [...path, key]);
51
+ resolveProcedures(route, [...path, key]);
52
52
  }
53
53
  else if (IsProcedureContract(route)) {
54
- const fullName = [...path, key].join('/');
55
- this.#procedures.set(fullName, route);
54
+ procedures.set([...path, key].join('/'), route);
56
55
  }
57
56
  }
58
- }
59
- buildCallers() {
60
- const callers = Object.create(null);
61
- for (const [name, { stream }] of this.#procedures) {
62
- const parts = name.split('/');
63
- let current = callers;
64
- for (let i = 0; i < parts.length; i++) {
65
- const part = parts[i];
66
- if (i === parts.length - 1) {
67
- current[part] = (payload, options) => this._call(name, payload, {
68
- ...options,
69
- _stream_response: !!stream,
70
- });
71
- }
72
- else {
73
- current[part] = current[part] ?? Object.create(null);
74
- current = current[part];
75
- }
76
- }
57
+ };
58
+ resolveProcedures(contract);
59
+ const callers = Object.create(null);
60
+ const streams = Object.create(null);
61
+ for (const [name, procedure] of procedures) {
62
+ const invoke = (payload, options) => {
63
+ return rpc.call(name, payload, {
64
+ ...options,
65
+ _stream_response: !!procedure.stream,
66
+ });
67
+ };
68
+ if (procedure.stream) {
69
+ assignNested(streams, name, invoke);
77
70
  }
78
- return callers;
71
+ else {
72
+ assignNested(callers, name, invoke);
73
+ }
74
+ }
75
+ return { call: callers, stream: streams };
76
+ };
77
+ export class RuntimeClient extends Client {
78
+ constructor(options, transport, transportOptions) {
79
+ super(options, transport, transportOptions, new RuntimeContractTransformer(options.contract), (rpc) => buildRuntimeCallers(rpc, options.contract));
79
80
  }
80
81
  }
81
82
  //# sourceMappingURL=runtime.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"runtime.js","sourceRoot":"","sources":["../../src/clients/runtime.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,mBA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,GAAG,EAAE,EAAE,CAAC,mBAAmB,CAAC,GAAG,EAAE,OAAO,CAAC,QAAQ,CAAC,CACpD,CAAA;IAAA,CACF;CACF"}
@@ -1,13 +1,8 @@
1
1
  import type { TAnyRouterContract } from '@nmtjs/contract';
2
- import type { BaseClientOptions } from '../core.ts';
2
+ import type { BaseClientOptions } from '../client.ts';
3
3
  import type { ClientTransportFactory } from '../transport.ts';
4
- import type { ClientCallers, StaticInputContractTypeProvider, StaticOutputContractTypeProvider } from '../types.ts';
5
- import { BaseClient } from '../core.ts';
6
- import { BaseClientTransformer } from '../transformers.ts';
7
- export declare class StaticClient<Transport extends ClientTransportFactory<any, any> = ClientTransportFactory<any, any>, RouterContract extends TAnyRouterContract = TAnyRouterContract, SafeCall extends boolean = false> extends BaseClient<Transport, RouterContract, SafeCall, StaticInputContractTypeProvider, StaticOutputContractTypeProvider> {
8
- protected readonly transformer: BaseClientTransformer;
4
+ import type { StaticInputContractTypeProvider, StaticOutputContractTypeProvider } from '../types.ts';
5
+ import { Client } from '../client.ts';
6
+ export declare class StaticClient<Transport extends ClientTransportFactory<any, any> = ClientTransportFactory<any, any>, RouterContract extends TAnyRouterContract = TAnyRouterContract, SafeCall extends boolean = false> extends Client<Transport, RouterContract, SafeCall, StaticInputContractTypeProvider, StaticOutputContractTypeProvider> {
9
7
  constructor(options: BaseClientOptions<RouterContract, SafeCall>, transport: Transport, transportOptions: Transport extends ClientTransportFactory<any, infer Options> ? Options : never);
10
- get call(): ClientCallers<this['_']['routes'], SafeCall, false>;
11
- get stream(): ClientCallers<this['_']['routes'], SafeCall, true>;
12
- protected createProxy<T>(target: Record<string, unknown>, isStream: boolean, path?: string[]): T;
13
8
  }
@@ -1,30 +1,30 @@
1
- import { BaseClient } from '../core.js';
1
+ import { Client } from '../client.js';
2
2
  import { BaseClientTransformer } from '../transformers.js';
3
- export class StaticClient extends BaseClient {
4
- transformer;
5
- constructor(options, transport, transportOptions) {
6
- super(options, transport, transportOptions);
7
- this.transformer = new BaseClientTransformer();
8
- }
9
- get call() {
10
- return this.createProxy(Object.create(null), false);
11
- }
12
- get stream() {
13
- return this.createProxy(Object.create(null), true);
14
- }
15
- createProxy(target, isStream, path = []) {
3
+ const buildStaticCallers = (rpc, isStream, path = []) => {
4
+ const createProxy = (target, current) => {
16
5
  return new Proxy(target, {
17
6
  get: (obj, prop) => {
18
7
  if (prop === 'then')
19
8
  return obj;
20
- const newPath = [...path, String(prop)];
21
- const caller = (payload, options) => this._call(newPath.join('/'), payload, {
22
- ...options,
23
- _stream_response: isStream || options?._stream_response,
24
- });
25
- return this.createProxy(caller, isStream, newPath);
9
+ const nextPath = [...current, String(prop)];
10
+ const caller = (payload, options) => {
11
+ return rpc.call(nextPath.join('/'), payload, {
12
+ ...options,
13
+ _stream_response: isStream || options?._stream_response,
14
+ });
15
+ };
16
+ return createProxy(caller, nextPath);
26
17
  },
27
18
  });
19
+ };
20
+ return createProxy(Object.create(null), path);
21
+ };
22
+ export class StaticClient extends Client {
23
+ constructor(options, transport, transportOptions) {
24
+ super(options, transport, transportOptions, new BaseClientTransformer(), (rpc) => ({
25
+ call: buildStaticCallers(rpc, false),
26
+ stream: buildStaticCallers(rpc, true),
27
+ }));
28
28
  }
29
29
  }
30
30
  //# sourceMappingURL=static.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"static.js","sourceRoot":"","sources":["../../src/clients/static.ts"],"names":[],"mappings":"AAUA,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AACvC,OAAO,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAA;AAE1D,MAAM,OAAO,YAOX,SAAQ,UAMT;IACoB,WAAW,CAAuB;IAErD,YACE,OAAoD,EACpD,SAAoB,EACpB,gBAKS,EACT;QACA,KAAK,CAAC,OAAO,EAAE,SAAS,EAAE,gBAAgB,CAAC,CAAA;QAC3C,IAAI,CAAC,WAAW,GAAG,IAAI,qBAAqB,EAAE,CAAA;IAAA,CAC/C;IAED,IAAa,IAAI,GA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,OAAiB,EACjB,EAAE,CAAC;QACH,OAAO,IAAI,KAAK,CAAC,MAAM,EAAE;YACvB,GAAG,EAAE,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE,CAAC;gBAClB,IAAI,IAAI,KAAK,MAAM;oBAAE,OAAO,GAAG,CAAA;gBAE/B,MAAM,QAAQ,GAAG,CAAC,GAAG,OAAO,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC,CAAA;gBAC3C,MAAM,MAAM,GAAG,CACb,OAAiB,EACjB,OAAoC,EACpC,EAAE,CAAC;oBACH,OAAO,GAAG,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,OAAO,EAAE;wBAC3C,GAAG,OAAO;wBACV,gBAAgB,EAAE,QAAQ,IAAI,OAAO,EAAE,gBAAgB;qBACxD,CAAC,CAAA;gBAAA,CACH,CAAA;gBAED,OAAO,WAAW,CAAC,MAAa,EAAE,QAAQ,CAAC,CAAA;YAAA,CAC5C;SACF,CAAM,CAAA;IAAA,CACR,CAAA;IAED,OAAO,WAAW,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,IAAI,CAAC,CAAA;AAAA,CAC9C,CAAA;AAED,MAAM,OAAO,YAOX,SAAQ,MAMT;IACC,YACE,OAAoD,EACpD,SAAoB,EACpB,gBAKS,EACT;QACA,KAAK,CACH,OAAO,EACP,SAAS,EACT,gBAAgB,EAChB,IAAI,qBAAqB,EAAE,EAC3B,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC;YACR,IAAI,EAAE,kBAAkB,CAAC,GAAG,EAAE,KAAK,CAAC;YACpC,MAAM,EAAE,kBAAkB,CAAC,GAAG,EAAE,IAAI,CAAC;SACtC,CAAC,CACH,CAAA;IAAA,CACF;CACF"}
package/dist/core.d.ts CHANGED
@@ -1,101 +1,51 @@
1
- import type { Future, TypeProvider } from '@nmtjs/common';
2
- import type { TAnyRouterContract } from '@nmtjs/contract';
3
- import type { ProtocolBlobMetadata, ProtocolVersion } from '@nmtjs/protocol';
1
+ import type { ProtocolVersion } from '@nmtjs/protocol';
4
2
  import type { BaseClientFormat, MessageContext, ProtocolVersionInterface } from '@nmtjs/protocol/client';
5
- import { ConnectionType, ProtocolBlob } from '@nmtjs/protocol';
3
+ import { ConnectionType } from '@nmtjs/protocol';
6
4
  import { ProtocolError } from '@nmtjs/protocol/client';
7
- import type { ClientDisconnectReason, ClientPlugin, ClientPluginEvent, ClientPluginInstance } from './plugins/types.ts';
8
- import type { BaseClientTransformer } from './transformers.ts';
9
- import type { ClientTransportFactory } from './transport.ts';
10
- import type { ClientCallers, ClientCallOptions, ResolveAPIRouterRoutes } from './types.ts';
5
+ import type { ClientPlugin, ClientPluginContext, ClientPluginEvent, ReconnectConfig, StreamEvent } from './plugins/types.ts';
6
+ import type { ClientDisconnectReason, ClientTransport, TransportCallContext, TransportCallOptions, TransportCallResponse, TransportRpcParams } from './transport.ts';
11
7
  import { EventEmitter } from './events.ts';
12
- import { ClientStreams, ServerStreams } from './streams.ts';
13
8
  export { ErrorCode, ProtocolBlob, type ProtocolBlobMetadata, } from '@nmtjs/protocol';
14
- export * from './types.ts';
15
- export declare class ClientError extends ProtocolError {
16
- }
17
- export type ProtocolClientCall = Future<any> & {
18
- procedure: string;
19
- signal?: AbortSignal;
20
- };
21
- export interface BaseClientOptions<RouterContract extends TAnyRouterContract = TAnyRouterContract, SafeCall extends boolean = false> {
22
- contract: RouterContract;
9
+ export type ConnectionState = 'idle' | 'connecting' | 'connected' | 'disconnecting' | 'disconnected';
10
+ export interface ClientCoreOptions {
23
11
  protocol: ProtocolVersion;
24
12
  format: BaseClientFormat;
25
13
  application?: string;
26
- timeout?: number;
27
14
  plugins?: ClientPlugin[];
28
- safe?: SafeCall;
29
15
  }
30
- /**
31
- * @todo Add error logging in ClientStreamPull rejection handler for easier debugging
32
- * @todo Consider edge case where callId/streamId overflow at MAX_UINT32 with existing entries
33
- */
34
- export declare abstract class BaseClient<TransportFactory extends ClientTransportFactory<any, any> = ClientTransportFactory<any, any>, RouterContract extends TAnyRouterContract = TAnyRouterContract, SafeCall extends boolean = false, InputTypeProvider extends TypeProvider = TypeProvider, OutputTypeProvider extends TypeProvider = TypeProvider> extends EventEmitter<{
16
+ export declare class ClientError extends ProtocolError {
17
+ }
18
+ export declare class ClientCore extends EventEmitter<{
19
+ message: [message: unknown, raw: ArrayBufferView];
35
20
  connected: [];
36
21
  disconnected: [reason: ClientDisconnectReason];
22
+ state_changed: [state: ConnectionState, previous: ConnectionState];
37
23
  pong: [nonce: number];
38
24
  }> {
39
- readonly options: BaseClientOptions<RouterContract, SafeCall>;
40
- readonly transportFactory: TransportFactory;
41
- readonly transportOptions: TransportFactory extends ClientTransportFactory<any, infer U> ? U : never;
42
- _: {
43
- routes: ResolveAPIRouterRoutes<RouterContract, InputTypeProvider, OutputTypeProvider>;
44
- safe: SafeCall;
45
- };
46
- protected abstract readonly transformer: BaseClientTransformer;
47
- abstract call: ClientCallers<this['_']['routes'], SafeCall, false>;
48
- abstract stream: ClientCallers<this['_']['routes'], SafeCall, true>;
49
- protected calls: Map<number, ProtocolClientCall>;
50
- protected transport: TransportFactory extends ClientTransportFactory<any, any, infer T> ? T : never;
51
- protected protocol: ProtocolVersionInterface;
52
- protected messageContext: MessageContext | null;
53
- protected clientStreams: ClientStreams;
54
- protected serverStreams: ServerStreams<import("@nmtjs/protocol/client").ProtocolServerStreamInterface<unknown>>;
55
- protected rpcStreams: ServerStreams<import("@nmtjs/protocol/client").ProtocolServerStreamInterface<unknown>>;
56
- protected callId: number;
57
- protected streamId: number;
58
- protected cab: AbortController | null;
59
- protected connecting: Promise<void> | null;
60
- protected _state: 'connected' | 'disconnected';
61
- protected _lastDisconnectReason: ClientDisconnectReason;
62
- protected _disposed: boolean;
63
- protected pingNonce: number;
64
- protected pendingPings: Map<number, Future<void>>;
65
- protected plugins: ClientPluginInstance[];
66
- private clientDisconnectAsReconnect;
67
- private clientDisconnectOverrideReason;
68
- private authValue;
69
- constructor(options: BaseClientOptions<RouterContract, SafeCall>, transportFactory: TransportFactory, transportOptions: TransportFactory extends ClientTransportFactory<any, infer U> ? U : never);
70
- dispose(): void;
71
- get state(): "connected" | "disconnected";
25
+ #private;
26
+ readonly transport: ClientTransport;
27
+ readonly protocol: ProtocolVersionInterface;
28
+ readonly format: BaseClientFormat;
29
+ readonly application?: string;
30
+ auth: any;
31
+ messageContext: MessageContext | null;
32
+ constructor(options: ClientCoreOptions, transport: ClientTransport);
33
+ get state(): ConnectionState;
72
34
  get lastDisconnectReason(): ClientDisconnectReason;
73
35
  get transportType(): ConnectionType;
36
+ get connectionSignal(): AbortSignal | undefined;
74
37
  isDisposed(): boolean;
75
- requestReconnect(reason?: string): Promise<void>;
76
- get auth(): any;
77
- set auth(value: any);
38
+ initPlugins(plugins: ClientPlugin[] | undefined, context: ClientPluginContext): void;
39
+ setMessageContextFactory(factory: () => MessageContext): void;
40
+ configureReconnect(config: ReconnectConfig | null): void;
41
+ setReconnectPauseReason(reason: string, active: boolean): void;
42
+ triggerReconnect(): void;
78
43
  connect(): Promise<void>;
79
- disconnect(options?: {
80
- reconnect?: boolean;
81
- reason?: string;
82
- }): Promise<void>;
83
- blob(source: Blob | ReadableStream | string | AsyncIterable<Uint8Array>, metadata?: ProtocolBlobMetadata): ProtocolBlob;
84
- protected _call(procedure: string, payload: any, options?: ClientCallOptions): Promise<any>;
85
- protected onConnect(): Promise<void>;
86
- protected onDisconnect(reason: ClientDisconnectReason): Promise<void>;
87
- protected nextPingNonce(): number;
88
- ping(timeout: number, signal?: AbortSignal): Promise<any>;
89
- protected stopAllPendingPings(reason?: any): void;
90
- protected onMessage(buffer: ArrayBufferView): Promise<void>;
91
- private handleRPCResponseMessage;
92
- private handleRPCStreamResponseMessage;
93
- private handleCallResponse;
94
- protected send(buffer: ArrayBufferView, signal?: AbortSignal): Promise<void>;
95
- protected emitStreamEvent(event: Omit<Extract<ClientPluginEvent, {
96
- kind: 'stream_event';
97
- }>, 'kind' | 'timestamp'>): void;
98
- protected getStreamId(): number;
99
- protected getCallId(): number;
100
- protected emitClientEvent(event: ClientPluginEvent): void;
44
+ disconnect(reason?: ClientDisconnectReason): Promise<void>;
45
+ requestReconnect(reason?: ClientDisconnectReason): Promise<void>;
46
+ dispose(): void;
47
+ send(buffer: ArrayBufferView, signal?: AbortSignal): Promise<void>;
48
+ transportCall(context: TransportCallContext, rpc: TransportRpcParams, options: TransportCallOptions): Promise<TransportCallResponse>;
49
+ emitClientEvent(event: ClientPluginEvent): void;
50
+ emitStreamEvent(event: StreamEvent): void;
101
51
  }