@nmtjs/protocol 0.14.4 → 0.15.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 (47) hide show
  1. package/LICENSE.md +1 -1
  2. package/README.md +1 -1
  3. package/package.json +17 -22
  4. package/dist/client/events.d.ts +0 -15
  5. package/dist/client/events.js +0 -28
  6. package/dist/client/format.d.ts +0 -21
  7. package/dist/client/format.js +0 -2
  8. package/dist/client/index.d.ts +0 -4
  9. package/dist/client/index.js +0 -4
  10. package/dist/client/protocol.d.ts +0 -150
  11. package/dist/client/protocol.js +0 -354
  12. package/dist/client/stream.d.ts +0 -28
  13. package/dist/client/stream.js +0 -95
  14. package/dist/common/binary.d.ts +0 -19
  15. package/dist/common/binary.js +0 -27
  16. package/dist/common/blob.d.ts +0 -22
  17. package/dist/common/blob.js +0 -42
  18. package/dist/common/enums.d.ts +0 -41
  19. package/dist/common/enums.js +0 -45
  20. package/dist/common/index.d.ts +0 -4
  21. package/dist/common/index.js +0 -4
  22. package/dist/common/types.d.ts +0 -33
  23. package/dist/common/types.js +0 -1
  24. package/dist/server/api.d.ts +0 -33
  25. package/dist/server/api.js +0 -7
  26. package/dist/server/connection.d.ts +0 -25
  27. package/dist/server/connection.js +0 -22
  28. package/dist/server/constants.d.ts +0 -4
  29. package/dist/server/constants.js +0 -2
  30. package/dist/server/format.d.ts +0 -40
  31. package/dist/server/format.js +0 -60
  32. package/dist/server/index.d.ts +0 -11
  33. package/dist/server/index.js +0 -11
  34. package/dist/server/injectables.d.ts +0 -14
  35. package/dist/server/injectables.js +0 -22
  36. package/dist/server/protocol.d.ts +0 -119
  37. package/dist/server/protocol.js +0 -372
  38. package/dist/server/registry.d.ts +0 -3
  39. package/dist/server/registry.js +0 -3
  40. package/dist/server/stream.d.ts +0 -13
  41. package/dist/server/stream.js +0 -32
  42. package/dist/server/transport.d.ts +0 -23
  43. package/dist/server/transport.js +0 -3
  44. package/dist/server/types.d.ts +0 -13
  45. package/dist/server/types.js +0 -1
  46. package/dist/server/utils.d.ts +0 -15
  47. package/dist/server/utils.js +0 -15
@@ -1,28 +0,0 @@
1
- import type { ProtocolBlobMetadata } from '../common/blob.ts';
2
- export declare class ProtocolClientBlobStream extends TransformStream<any, ArrayBuffer> {
3
- #private;
4
- readonly source: ReadableStream;
5
- readonly id: number;
6
- readonly metadata: ProtocolBlobMetadata;
7
- constructor(source: ReadableStream, id: number, metadata: ProtocolBlobMetadata);
8
- read(size: number): Promise<ArrayBuffer | null>;
9
- abort(error?: Error): void;
10
- end(): Promise<void>;
11
- }
12
- export interface ProtocolServerStreamInterface<T = any> {
13
- [Symbol.asyncIterator](): AsyncGenerator<T>;
14
- abort(error?: Error): void;
15
- }
16
- export declare class ProtocolServerStream<T = any> extends TransformStream<any, T> implements ProtocolServerStreamInterface<T> {
17
- #private;
18
- constructor(options?: Transformer<any, T>);
19
- [Symbol.asyncIterator](): AsyncGenerator<Awaited<T>, void, unknown>;
20
- push(chunk: T): Promise<void>;
21
- end(): Promise<void>;
22
- abort(error?: Error): Promise<void>;
23
- }
24
- export declare class ProtocolServerBlobStream extends ProtocolServerStream<ArrayBuffer> {
25
- readonly id: number;
26
- readonly metadata: ProtocolBlobMetadata;
27
- constructor(id: number, metadata: ProtocolBlobMetadata, options?: Transformer<any, ArrayBuffer>);
28
- }
@@ -1,95 +0,0 @@
1
- import { defer } from '@nmtjs/common';
2
- import { concat, encodeText } from "../common/binary.js";
3
- export class ProtocolClientBlobStream extends TransformStream {
4
- source;
5
- id;
6
- metadata;
7
- #queue;
8
- #reader;
9
- constructor(source, id, metadata) {
10
- super({
11
- start: () => {
12
- defer(() => source.pipeThrough(this));
13
- },
14
- transform: (chunk, controller) => {
15
- if (chunk instanceof ArrayBuffer) {
16
- controller.enqueue(chunk);
17
- }
18
- else if (chunk instanceof Uint8Array) {
19
- controller.enqueue(chunk.buffer);
20
- }
21
- else if (typeof chunk === 'string') {
22
- controller.enqueue(encodeText(chunk));
23
- }
24
- else {
25
- throw new Error('Invalid chunk data type. Expected ArrayBuffer, Uint8Array, or string.');
26
- }
27
- },
28
- });
29
- this.source = source;
30
- this.id = id;
31
- this.metadata = metadata;
32
- this.#queue = new ArrayBuffer(0);
33
- this.#reader = this.readable.getReader();
34
- }
35
- async read(size) {
36
- while (this.#queue.byteLength < size) {
37
- const { done, value } = await this.#reader.read();
38
- if (done) {
39
- if (this.#queue.byteLength > 0) {
40
- const chunk = this.#queue;
41
- this.#queue = new ArrayBuffer(0);
42
- return chunk;
43
- }
44
- return null;
45
- }
46
- const buffer = value;
47
- this.#queue = concat(this.#queue, buffer);
48
- }
49
- const chunk = this.#queue.slice(0, size);
50
- this.#queue = this.#queue.slice(size);
51
- return chunk;
52
- }
53
- abort(error = new Error('Stream aborted')) {
54
- this.#reader.cancel(error);
55
- this.source.cancel(error);
56
- }
57
- end() {
58
- return this.source.cancel('Stream ended');
59
- }
60
- }
61
- export class ProtocolServerStream extends TransformStream {
62
- #writer;
63
- constructor(options) {
64
- super(options);
65
- this.#writer = this.writable.getWriter();
66
- }
67
- async *[Symbol.asyncIterator]() {
68
- const reader = this.readable.getReader();
69
- while (true) {
70
- const { done, value } = await reader.read();
71
- if (!done)
72
- yield value;
73
- else
74
- break;
75
- }
76
- }
77
- async push(chunk) {
78
- await this.#writer.write(chunk);
79
- }
80
- async end() {
81
- await this.#writer.close();
82
- }
83
- async abort(error = new Error('Stream aborted')) {
84
- await this.#writer.abort(error);
85
- }
86
- }
87
- export class ProtocolServerBlobStream extends ProtocolServerStream {
88
- id;
89
- metadata;
90
- constructor(id, metadata, options) {
91
- super(options);
92
- this.id = id;
93
- this.metadata = metadata;
94
- }
95
- }
@@ -1,19 +0,0 @@
1
- declare const utf8decoder: TextDecoder;
2
- export type BinaryTypes = {
3
- Int8: number;
4
- Int16: number;
5
- Int32: number;
6
- Uint8: number;
7
- Uint16: number;
8
- Uint32: number;
9
- Float32: number;
10
- Float64: number;
11
- BigInt64: bigint;
12
- BigUint64: bigint;
13
- };
14
- export declare const encodeNumber: <T extends keyof BinaryTypes>(value: BinaryTypes[T], type: T, littleEndian?: boolean) => ArrayBuffer;
15
- export declare const decodeNumber: <T extends keyof BinaryTypes>(buffer: ArrayBuffer, type: T, offset?: number, littleEndian?: boolean) => BinaryTypes[T];
16
- export declare const encodeText: (text: string) => ArrayBuffer;
17
- export declare const decodeText: (buffer: Parameters<typeof utf8decoder.decode>[0]) => string;
18
- export declare const concat: (...buffers: ArrayBuffer[]) => ArrayBuffer;
19
- export {};
@@ -1,27 +0,0 @@
1
- // TODO: get rid of lib DOM somehow...
2
- /// <reference lib="dom" />
3
- const utf8decoder = new TextDecoder();
4
- const utf8encoder = new TextEncoder();
5
- export const encodeNumber = (value, type, littleEndian = false) => {
6
- const bytesNeeded = globalThis[`${type}Array`].BYTES_PER_ELEMENT;
7
- const ab = new ArrayBuffer(bytesNeeded);
8
- const dv = new DataView(ab);
9
- dv[`set${type}`](0, value, littleEndian);
10
- return ab;
11
- };
12
- export const decodeNumber = (buffer, type, offset = 0, littleEndian = false) => {
13
- const view = new DataView(buffer);
14
- return view[`get${type}`](offset, littleEndian);
15
- };
16
- export const encodeText = (text) => new Uint8Array(utf8encoder.encode(text)).buffer;
17
- export const decodeText = (buffer) => utf8decoder.decode(buffer);
18
- export const concat = (...buffers) => {
19
- const totalLength = buffers.reduce((acc, buffer) => acc + buffer.byteLength, 0);
20
- const view = new Uint8Array(totalLength);
21
- let offset = 0;
22
- for (const buffer of buffers) {
23
- view.set(new Uint8Array(buffer), offset);
24
- offset += buffer.byteLength;
25
- }
26
- return view.buffer;
27
- };
@@ -1,22 +0,0 @@
1
- export declare const BlobKey: unique symbol;
2
- export type BlobKey = typeof BlobKey;
3
- export type ProtocolBlobMetadata = {
4
- type: string;
5
- size?: number;
6
- filename?: string;
7
- };
8
- export interface ProtocolBlobInterface {
9
- readonly metadata: ProtocolBlobMetadata;
10
- readonly [BlobKey]: true;
11
- }
12
- export declare class ProtocolBlob implements ProtocolBlobInterface {
13
- readonly [BlobKey] = true;
14
- readonly metadata: ProtocolBlobMetadata;
15
- readonly source: any;
16
- constructor(source: any, size?: number, type?: string, filename?: string);
17
- static from(source: any, metadata?: {
18
- size?: number;
19
- type?: string;
20
- filename?: string;
21
- }): ProtocolBlob;
22
- }
@@ -1,42 +0,0 @@
1
- export const BlobKey = Symbol.for('neemata:BlobKey');
2
- export class ProtocolBlob {
3
- [BlobKey] = true;
4
- metadata;
5
- source;
6
- constructor(source, size, type = 'application/octet-stream', filename) {
7
- if (typeof size !== 'undefined' && size <= 0)
8
- throw new Error('Blob size is invalid');
9
- this.source = source;
10
- this.metadata = { size, type, filename };
11
- }
12
- static from(source, metadata = {}) {
13
- let _source;
14
- if (source instanceof globalThis.ReadableStream) {
15
- _source = source;
16
- }
17
- else if ('File' in globalThis && source instanceof globalThis.File) {
18
- _source = source.stream();
19
- metadata.size = source.size;
20
- metadata.filename = source.name;
21
- }
22
- else if (source instanceof globalThis.Blob) {
23
- _source = source.stream();
24
- metadata.size = source.size;
25
- }
26
- else if (typeof source === 'string') {
27
- const blob = new Blob([source]);
28
- _source = blob.stream();
29
- metadata.size = blob.size;
30
- metadata.type = metadata.type || 'text/plain';
31
- }
32
- else if (source instanceof globalThis.ArrayBuffer) {
33
- const blob = new Blob([source]);
34
- _source = blob.stream();
35
- metadata.size = blob.size;
36
- }
37
- else {
38
- _source = source;
39
- }
40
- return new ProtocolBlob(_source, metadata.size, metadata.type, metadata.filename);
41
- }
42
- }
@@ -1,41 +0,0 @@
1
- export declare enum ClientMessageType {
2
- Rpc = 10,
3
- RpcAbort = 11,
4
- RpcStreamAbort = 12,
5
- ClientStreamPush = 20,
6
- ClientStreamEnd = 21,
7
- ClientStreamAbort = 22,
8
- ServerStreamAbort = 23,
9
- ServerStreamPull = 24
10
- }
11
- export declare enum ServerMessageType {
12
- Event = 1,
13
- RpcResponse = 10,
14
- RpcStreamResponse = 11,
15
- RpcStreamChunk = 12,
16
- RpcStreamEnd = 13,
17
- RpcStreamAbort = 14,
18
- ServerStreamPush = 20,
19
- ServerStreamEnd = 21,
20
- ServerStreamAbort = 22,
21
- ClientStreamAbort = 23,
22
- ClientStreamPull = 24
23
- }
24
- export declare enum TransportType {
25
- Bidirectional = "Bidirectional",
26
- Unidirectional = "Unidirectional"
27
- }
28
- export declare enum ErrorCode {
29
- ValidationError = "ValidationError",
30
- BadRequest = "BadRequest",
31
- NotFound = "NotFound",
32
- Forbidden = "Forbidden",
33
- Unauthorized = "Unauthorized",
34
- InternalServerError = "InternalServerError",
35
- NotAcceptable = "NotAcceptable",
36
- RequestTimeout = "RequestTimeout",
37
- GatewayTimeout = "GatewayTimeout",
38
- ServiceUnavailable = "ServiceUnavailable",
39
- ClientRequestError = "ClientRequestError",
40
- ConnectionError = "ConnectionError"
41
- }
@@ -1,45 +0,0 @@
1
- export var ClientMessageType;
2
- (function (ClientMessageType) {
3
- ClientMessageType[ClientMessageType["Rpc"] = 10] = "Rpc";
4
- ClientMessageType[ClientMessageType["RpcAbort"] = 11] = "RpcAbort";
5
- ClientMessageType[ClientMessageType["RpcStreamAbort"] = 12] = "RpcStreamAbort";
6
- ClientMessageType[ClientMessageType["ClientStreamPush"] = 20] = "ClientStreamPush";
7
- ClientMessageType[ClientMessageType["ClientStreamEnd"] = 21] = "ClientStreamEnd";
8
- ClientMessageType[ClientMessageType["ClientStreamAbort"] = 22] = "ClientStreamAbort";
9
- ClientMessageType[ClientMessageType["ServerStreamAbort"] = 23] = "ServerStreamAbort";
10
- ClientMessageType[ClientMessageType["ServerStreamPull"] = 24] = "ServerStreamPull";
11
- })(ClientMessageType || (ClientMessageType = {}));
12
- export var ServerMessageType;
13
- (function (ServerMessageType) {
14
- ServerMessageType[ServerMessageType["Event"] = 1] = "Event";
15
- ServerMessageType[ServerMessageType["RpcResponse"] = 10] = "RpcResponse";
16
- ServerMessageType[ServerMessageType["RpcStreamResponse"] = 11] = "RpcStreamResponse";
17
- ServerMessageType[ServerMessageType["RpcStreamChunk"] = 12] = "RpcStreamChunk";
18
- ServerMessageType[ServerMessageType["RpcStreamEnd"] = 13] = "RpcStreamEnd";
19
- ServerMessageType[ServerMessageType["RpcStreamAbort"] = 14] = "RpcStreamAbort";
20
- ServerMessageType[ServerMessageType["ServerStreamPush"] = 20] = "ServerStreamPush";
21
- ServerMessageType[ServerMessageType["ServerStreamEnd"] = 21] = "ServerStreamEnd";
22
- ServerMessageType[ServerMessageType["ServerStreamAbort"] = 22] = "ServerStreamAbort";
23
- ServerMessageType[ServerMessageType["ClientStreamAbort"] = 23] = "ClientStreamAbort";
24
- ServerMessageType[ServerMessageType["ClientStreamPull"] = 24] = "ClientStreamPull";
25
- })(ServerMessageType || (ServerMessageType = {}));
26
- export var TransportType;
27
- (function (TransportType) {
28
- TransportType["Bidirectional"] = "Bidirectional";
29
- TransportType["Unidirectional"] = "Unidirectional";
30
- })(TransportType || (TransportType = {}));
31
- export var ErrorCode;
32
- (function (ErrorCode) {
33
- ErrorCode["ValidationError"] = "ValidationError";
34
- ErrorCode["BadRequest"] = "BadRequest";
35
- ErrorCode["NotFound"] = "NotFound";
36
- ErrorCode["Forbidden"] = "Forbidden";
37
- ErrorCode["Unauthorized"] = "Unauthorized";
38
- ErrorCode["InternalServerError"] = "InternalServerError";
39
- ErrorCode["NotAcceptable"] = "NotAcceptable";
40
- ErrorCode["RequestTimeout"] = "RequestTimeout";
41
- ErrorCode["GatewayTimeout"] = "GatewayTimeout";
42
- ErrorCode["ServiceUnavailable"] = "ServiceUnavailable";
43
- ErrorCode["ClientRequestError"] = "ClientRequestError";
44
- ErrorCode["ConnectionError"] = "ConnectionError";
45
- })(ErrorCode || (ErrorCode = {}));
@@ -1,4 +0,0 @@
1
- export * from './binary.ts';
2
- export * from './blob.ts';
3
- export * from './enums.ts';
4
- export * from './types.ts';
@@ -1,4 +0,0 @@
1
- export * from "./binary.js";
2
- export * from "./blob.js";
3
- export * from "./enums.js";
4
- export * from "./types.js";
@@ -1,33 +0,0 @@
1
- import type { OneOf } from '@nmtjs/common';
2
- import type { ProtocolBlob, ProtocolBlobMetadata } from './blob.ts';
3
- type Stream = any;
4
- export interface BaseProtocolError {
5
- code: string;
6
- message: string;
7
- data?: any;
8
- }
9
- export type ProtocolRPC = {
10
- callId: number;
11
- procedure: string;
12
- payload: any;
13
- };
14
- export type ProtocolRPCResponse<T = Stream> = OneOf<[
15
- {
16
- callId: number;
17
- error: BaseProtocolError;
18
- },
19
- {
20
- callId: number;
21
- result: any;
22
- streams: Record<number, T>;
23
- }
24
- ]>;
25
- export interface EncodeRPCContext<T = Stream> {
26
- getStream: (id: number) => T;
27
- addStream: (blob: ProtocolBlob) => T;
28
- }
29
- export interface DecodeRPCContext<T = Stream> {
30
- getStream: (id: number, callId: number) => T;
31
- addStream: (id: number, callId: number, metadata: ProtocolBlobMetadata) => T;
32
- }
33
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -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
- }
@@ -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
- }
@@ -1,4 +0,0 @@
1
- export declare const kTransportPlugin: unique symbol;
2
- export type kTransportPlugin = typeof kTransportPlugin;
3
- export declare const kIterableResponse: unique symbol;
4
- export type kIterableResponse = typeof kIterableResponse;
@@ -1,2 +0,0 @@
1
- export const kTransportPlugin = Symbol.for('neemata:TransportPluginKey');
2
- export const kIterableResponse = Symbol.for('neemata:IterableResponseKey');
@@ -1,40 +0,0 @@
1
- import type { OneOf } from '@nmtjs/common';
2
- import type { Pattern } from '@nmtjs/core';
3
- import type { DecodeRPCContext, EncodeRPCContext, ProtocolRPC, ProtocolRPCResponse } from '../common/types.ts';
4
- import type { ProtocolClientStream, ProtocolServerStream } from './stream.ts';
5
- export interface BaseServerDecoder {
6
- accept: Pattern[];
7
- decode(buffer: ArrayBuffer): any;
8
- decodeRPC(buffer: ArrayBuffer, context: DecodeRPCContext<ProtocolClientStream>): ProtocolRPC;
9
- }
10
- export interface BaseServerEncoder {
11
- contentType: string;
12
- encode(data: any): ArrayBuffer;
13
- encodeRPC(rpc: OneOf<[
14
- {
15
- callId: number;
16
- error: any;
17
- },
18
- {
19
- callId: number;
20
- result: any;
21
- }
22
- ]>, context: EncodeRPCContext<ProtocolServerStream>): ArrayBuffer;
23
- }
24
- export declare abstract class BaseServerFormat implements BaseServerDecoder, BaseServerEncoder {
25
- abstract accept: Pattern[];
26
- abstract contentType: string;
27
- abstract encode(data: any): ArrayBuffer;
28
- abstract encodeRPC(rpc: ProtocolRPCResponse, context: EncodeRPCContext<ProtocolServerStream>): ArrayBuffer;
29
- abstract decode(buffer: ArrayBuffer): any;
30
- abstract decodeRPC(buffer: ArrayBuffer, context: DecodeRPCContext<ProtocolClientStream>): ProtocolRPC;
31
- }
32
- export declare const parseContentTypes: (types: string) => string[];
33
- export declare class Format {
34
- decoders: Map<Pattern, BaseServerDecoder>;
35
- encoders: Map<Pattern, BaseServerEncoder>;
36
- constructor(formats: BaseServerFormat[]);
37
- supportsDecoder(contentType: string, throwIfUnsupported?: boolean): BaseServerDecoder | null;
38
- supportsEncoder(contentType: string, throwIfUnsupported?: boolean): BaseServerEncoder | null;
39
- private supports;
40
- }
@@ -1,60 +0,0 @@
1
- import { match } from '@nmtjs/core';
2
- export class BaseServerFormat {
3
- }
4
- export const parseContentTypes = (types) => {
5
- if (types === '*/*')
6
- return ['*/*'];
7
- return types
8
- .split(',')
9
- .map((t) => {
10
- const [type, ...rest] = t.split(';');
11
- const params = new Map(rest.map((p) => p
12
- .trim()
13
- .split('=')
14
- .slice(0, 2)
15
- .map((p) => p.trim())));
16
- return {
17
- type,
18
- q: params.has('q') ? Number.parseFloat(params.get('q')) : 1,
19
- };
20
- })
21
- .sort((a, b) => {
22
- if (a.type === '*/*')
23
- return 1;
24
- if (b.type === '*/*')
25
- return -1;
26
- return b.q - a.q ? -1 : 1;
27
- })
28
- .map((t) => t.type);
29
- };
30
- export class Format {
31
- decoders = new Map();
32
- encoders = new Map();
33
- constructor(formats) {
34
- for (const format of formats) {
35
- this.encoders.set(format.contentType, format);
36
- for (const acceptType of format.accept) {
37
- this.decoders.set(acceptType, format);
38
- }
39
- }
40
- }
41
- supportsDecoder(contentType, throwIfUnsupported = false) {
42
- return this.supports(this.decoders, contentType, throwIfUnsupported);
43
- }
44
- supportsEncoder(contentType, throwIfUnsupported = false) {
45
- return this.supports(this.encoders, contentType, throwIfUnsupported);
46
- }
47
- supports(formats, contentType, throwIfUnsupported = false) {
48
- // TODO: Use node:utils.MIMEType (not implemented yet in Deno and Bun yet)
49
- const types = parseContentTypes(contentType);
50
- for (const type of types) {
51
- for (const [pattern, format] of formats) {
52
- if (type === '*/*' || match(type, pattern))
53
- return format;
54
- }
55
- }
56
- if (throwIfUnsupported)
57
- throw new Error(`No supported format found: ${contentType}`);
58
- return null;
59
- }
60
- }
@@ -1,11 +0,0 @@
1
- export * from './api.ts';
2
- export * from './connection.ts';
3
- export * from './constants.ts';
4
- export * from './format.ts';
5
- export * from './injectables.ts';
6
- export * from './protocol.ts';
7
- export * from './registry.ts';
8
- export * from './stream.ts';
9
- export * from './transport.ts';
10
- export * from './types.ts';
11
- export * from './utils.ts';
@@ -1,11 +0,0 @@
1
- export * from "./api.js";
2
- export * from "./connection.js";
3
- export * from "./constants.js";
4
- export * from "./format.js";
5
- export * from "./injectables.js";
6
- export * from "./protocol.js";
7
- export * from "./registry.js";
8
- export * from "./stream.js";
9
- export * from "./transport.js";
10
- export * from "./types.js";
11
- export * from "./utils.js";
@@ -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
- };