@nmtjs/http-client 0.12.5 → 0.12.6

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.
@@ -0,0 +1,21 @@
1
+ import { type ClientMessageType } from '@nmtjs/protocol';
2
+ import { type BaseProtocol, EventEmitter, type ProtocolBaseClientCallOptions, type ProtocolBaseTransformer, type ProtocolClientCall, type ProtocolSendMetadata, type ProtocolTransport, type ProtocolTransportEventMap, ProtocolTransportStatus } from '@nmtjs/protocol/client';
3
+ export type HttpClientTransportOptions = {
4
+ /**
5
+ * The origin of the server
6
+ * @example 'http://localhost:3000'
7
+ */
8
+ origin: string;
9
+ debug?: boolean;
10
+ };
11
+ export declare class HttpClientTransport extends EventEmitter<ProtocolTransportEventMap> implements ProtocolTransport {
12
+ #private;
13
+ protected readonly protocol: BaseProtocol;
14
+ private readonly options;
15
+ status: ProtocolTransportStatus;
16
+ constructor(protocol: BaseProtocol, options: HttpClientTransportOptions);
17
+ call(namespace: string, procedure: string, payload: any, options: ProtocolBaseClientCallOptions, transformer: ProtocolBaseTransformer): Promise<ProtocolClientCall>;
18
+ connect(auth: any): Promise<void>;
19
+ disconnect(): Promise<void>;
20
+ send(messageType: ClientMessageType, buffer: ArrayBuffer, metadata: ProtocolSendMetadata): Promise<void>;
21
+ }
package/dist/index.js CHANGED
@@ -1,73 +1,87 @@
1
- import { ClientError } from "@nmtjs/client";
2
- import { ErrorCode, ProtocolBlob } from "@nmtjs/protocol";
3
- import { EventEmitter, ProtocolServerBlobStream, ProtocolTransportStatus } from "@nmtjs/protocol/client";
1
+ import { ClientError } from '@nmtjs/client';
2
+ import { ErrorCode, ProtocolBlob, } from '@nmtjs/protocol';
3
+ import { EventEmitter, ProtocolServerBlobStream, ProtocolTransportStatus, } from '@nmtjs/protocol/client';
4
4
  export class HttpClientTransport extends EventEmitter {
5
- #auth = null;
6
- status = ProtocolTransportStatus.CONNECTED;
7
- constructor(protocol, options) {
8
- super();
9
- this.protocol = protocol;
10
- this.options = options;
11
- }
12
- async call(namespace, procedure, payload, options, transformer) {
13
- const call = this.protocol.createCall(namespace, procedure, options);
14
- const headers = new Headers();
15
- headers.set("Content-Type", this.protocol.contentType);
16
- headers.set("Accept", this.protocol.contentType);
17
- if (this.#auth) headers.set("Authorization", this.#auth);
18
- const isBlob = payload instanceof ProtocolBlob;
19
- if (isBlob) headers.set("x-neemata-blob", "true");
20
- const response = fetch(`${this.options.origin}/api/${namespace}/${procedure}`, {
21
- method: "POST",
22
- headers,
23
- credentials: "include",
24
- body: isBlob ? payload.source : transformer.encodeRPC(namespace, procedure, payload),
25
- signal: call.signal,
26
- duplex: "half"
27
- });
28
- response.catch((error) => Promise.reject(new Error("Network error", { cause: error }))).then((response) => {
29
- const isBlob = response.headers.get("x-neemata-blob") === "true";
30
- if (isBlob) {
31
- const contentLength = response.headers.get("content-length");
32
- const size = contentLength ? Number.parseInt(contentLength) || undefined : undefined;
33
- const type = response.headers.get("content-type") || "application/octet-stream";
34
- const stream = new ProtocolServerBlobStream(-1, {
35
- size,
36
- type
37
- });
38
- response.body?.pipeThrough(stream);
39
- return stream;
40
- } else {
41
- const body = response.arrayBuffer();
42
- return body.then((buffer) => {
43
- if (response.ok) {
44
- const decoded = this.protocol.format.decode(buffer);
45
- return transformer.decodeRPC(namespace, procedure, decoded);
46
- } else {
47
- if (buffer.byteLength === 0) {
48
- const error = new ClientError(ErrorCode.InternalServerError, `Empty response with ${response.status} status code`);
49
- return Promise.reject(error);
50
- } else {
51
- const payload = this.protocol.format.decode(buffer);
52
- const error = new ClientError(payload.code, payload.message, payload.data);
53
- return Promise.reject(error);
54
- }
55
- }
56
- });
57
- }
58
- }).then(call.resolve).catch(call.reject);
59
- return call;
60
- }
61
- async connect(auth) {
62
- this.#auth = auth;
63
- this.emit("connected");
64
- }
65
- async disconnect() {
66
- this.emit("disconnected");
67
- }
68
- async send(messageType, buffer, metadata) {
69
- throw new Error("Not supported");
70
- }
5
+ protocol;
6
+ options;
7
+ #auth = null;
8
+ status = ProtocolTransportStatus.CONNECTED;
9
+ constructor(protocol, options) {
10
+ super();
11
+ this.protocol = protocol;
12
+ this.options = options;
13
+ }
14
+ async call(namespace, procedure, payload, options, transformer) {
15
+ const call = this.protocol.createCall(namespace, procedure, options);
16
+ const headers = new Headers();
17
+ headers.set('Content-Type', this.protocol.contentType);
18
+ headers.set('Accept', this.protocol.contentType);
19
+ if (this.#auth)
20
+ headers.set('Authorization', this.#auth);
21
+ const isBlob = payload instanceof ProtocolBlob;
22
+ if (isBlob)
23
+ headers.set('x-neemata-blob', 'true');
24
+ const response = fetch(`${this.options.origin}/api/${namespace}/${procedure}`, {
25
+ method: 'POST',
26
+ headers,
27
+ credentials: 'include',
28
+ body: isBlob
29
+ ? payload.source
30
+ : transformer.encodeRPC(namespace, procedure, payload),
31
+ signal: call.signal,
32
+ // @ts-expect-error
33
+ duplex: 'half',
34
+ });
35
+ response
36
+ .catch((error) => Promise.reject(new Error('Network error', { cause: error })))
37
+ .then((response) => {
38
+ const isBlob = response.headers.get('x-neemata-blob') === 'true';
39
+ if (isBlob) {
40
+ const contentLength = response.headers.get('content-length');
41
+ const size = contentLength
42
+ ? Number.parseInt(contentLength) || undefined
43
+ : undefined;
44
+ const type = response.headers.get('content-type') || 'application/octet-stream';
45
+ const stream = new ProtocolServerBlobStream(-1, {
46
+ size,
47
+ type,
48
+ });
49
+ response.body?.pipeThrough(stream);
50
+ return stream;
51
+ }
52
+ else {
53
+ const body = response.arrayBuffer();
54
+ return body.then((buffer) => {
55
+ if (response.ok) {
56
+ const decoded = this.protocol.format.decode(buffer);
57
+ return transformer.decodeRPC(namespace, procedure, decoded);
58
+ }
59
+ else {
60
+ if (buffer.byteLength === 0) {
61
+ const error = new ClientError(ErrorCode.InternalServerError, `Empty response with ${response.status} status code`);
62
+ return Promise.reject(error);
63
+ }
64
+ else {
65
+ const payload = this.protocol.format.decode(buffer);
66
+ const error = new ClientError(payload.code, payload.message, payload.data);
67
+ return Promise.reject(error);
68
+ }
69
+ }
70
+ });
71
+ }
72
+ })
73
+ .then(call.resolve)
74
+ .catch(call.reject);
75
+ return call;
76
+ }
77
+ async connect(auth) {
78
+ this.#auth = auth;
79
+ this.emit('connected');
80
+ }
81
+ async disconnect() {
82
+ this.emit('disconnected');
83
+ }
84
+ async send(messageType, buffer, metadata) {
85
+ throw new Error('Not supported');
86
+ }
71
87
  }
72
-
73
- //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -3,19 +3,20 @@
3
3
  "type": "module",
4
4
  "exports": {
5
5
  ".": {
6
- "types": "./src/index.ts",
7
- "import": "./dist/index.js"
6
+ "types": "./dist/index.d.ts",
7
+ "import": "./dist/index.js",
8
+ "module-sync": "./dist/index.js"
8
9
  }
9
10
  },
10
11
  "dependencies": {
11
- "@nmtjs/client": "0.12.5",
12
- "@nmtjs/protocol": "0.12.5",
13
- "@nmtjs/common": "0.12.5"
12
+ "@nmtjs/common": "0.12.6",
13
+ "@nmtjs/protocol": "0.12.6",
14
+ "@nmtjs/client": "0.12.6"
14
15
  },
15
16
  "peerDependencies": {
16
- "@nmtjs/client": "0.12.5",
17
- "@nmtjs/common": "0.12.5",
18
- "@nmtjs/protocol": "0.12.5"
17
+ "@nmtjs/client": "0.12.6",
18
+ "@nmtjs/common": "0.12.6",
19
+ "@nmtjs/protocol": "0.12.6"
19
20
  },
20
21
  "files": [
21
22
  "src",
@@ -23,9 +24,9 @@
23
24
  "LICENSE.md",
24
25
  "README.md"
25
26
  ],
26
- "version": "0.12.5",
27
+ "version": "0.12.6",
27
28
  "scripts": {
28
- "build": "neemata-build --root=./src './*.ts'",
29
+ "build": "tsc",
29
30
  "type-check": "tsc --noEmit"
30
31
  }
31
32
  }
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"mappings":"AAAA,SAAS,mBAAmB,eAAe;AAC3C,SAEE,WACA,oBACK,iBAAiB;AACxB,SAEE,cAKA,0BAGA,+BACK,wBAAwB;AAW/B,OAAO,MAAM,4BACH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AAO,OAAO;AACnD,aAAO,YAAY,UAAU,WAAW,WAAW,QAAQ;KAC5D,OAAM;AACL,UAAI,OAAO,eAAe,GAAG;OAC3B,MAAM,QAAQ,IAAI,YAChB,UAAU,sBACT,sBAAsB,SAAS,OAAO;AAEzC,cAAO,QAAQ,OAAO,MAAM;MAC7B,OAAM;OACL,MAAM,UAAU,KAAK,SAAS,OAAO,OAAO,OAAO;OACnD,MAAM,QAAQ,IAAI,YAChB,QAAQ,MACR,QAAQ,SACR,QAAQ;AAEV,cAAO,QAAQ,OAAO,MAAM;MAC7B;KACF;IACF,EAAC;GACH;EACF,EAAC,CACD,KAAK,KAAK,QAAQ,CAClB,MAAM,KAAK,OAAO;AAErB,SAAO;CACR;CAED,MAAM,QAAQC,MAAW;AACvB,OAAKD,QAAQ;AACb,OAAK,KAAK,YAAY;CACvB;CAED,MAAM,aAAa;AACjB,OAAK,KAAK,eAAe;CAC1B;CAED,MAAM,KACJE,aACAC,QACAC,UACA;AACA,QAAM,IAAI,MAAM;CACjB;AACF","names":["protocol: BaseProtocol","options: HttpClientTransportOptions","namespace: string","procedure: string","payload: any","options: ProtocolBaseClientCallOptions","transformer: ProtocolBaseTransformer","#auth","auth: any","messageType: ClientMessageType","buffer: ArrayBuffer","metadata: ProtocolSendMetadata"],"sources":["../src/index.ts"],"sourcesContent":["import { ClientError } from '@nmtjs/client'\nimport {\n type ClientMessageType,\n ErrorCode,\n ProtocolBlob,\n} from '@nmtjs/protocol'\nimport {\n type BaseProtocol,\n EventEmitter,\n type ProtocolBaseClientCallOptions,\n type ProtocolBaseTransformer,\n type ProtocolClientCall,\n type ProtocolSendMetadata,\n ProtocolServerBlobStream,\n type ProtocolTransport,\n type ProtocolTransportEventMap,\n ProtocolTransportStatus,\n} from '@nmtjs/protocol/client'\n\nexport type HttpClientTransportOptions = {\n /**\n * The origin of the server\n * @example 'http://localhost:3000'\n */\n origin: string\n debug?: boolean\n}\n\nexport class HttpClientTransport\n extends EventEmitter<ProtocolTransportEventMap>\n implements ProtocolTransport\n{\n #auth: string | null = null\n status: ProtocolTransportStatus = ProtocolTransportStatus.CONNECTED\n\n constructor(\n protected readonly protocol: BaseProtocol,\n private readonly options: HttpClientTransportOptions,\n ) {\n super()\n }\n\n async call(\n namespace: string,\n procedure: string,\n payload: any,\n options: ProtocolBaseClientCallOptions,\n transformer: ProtocolBaseTransformer,\n ): Promise<ProtocolClientCall> {\n const call = this.protocol.createCall(namespace, procedure, options)\n\n const headers = new Headers()\n\n headers.set('Content-Type', this.protocol.contentType)\n headers.set('Accept', this.protocol.contentType)\n\n if (this.#auth) headers.set('Authorization', this.#auth)\n\n const isBlob = payload instanceof ProtocolBlob\n if (isBlob) headers.set('x-neemata-blob', 'true')\n\n const response = fetch(\n `${this.options.origin}/api/${namespace}/${procedure}`,\n {\n method: 'POST',\n headers,\n credentials: 'include',\n body: isBlob\n ? payload.source\n : transformer.encodeRPC(namespace, procedure, payload),\n signal: call.signal,\n // @ts-expect-error\n duplex: 'half',\n },\n )\n\n response\n .catch((error) =>\n Promise.reject(new Error('Network error', { cause: error })),\n )\n .then((response) => {\n const isBlob = response.headers.get('x-neemata-blob') === 'true'\n if (isBlob) {\n const contentLength = response.headers.get('content-length')\n const size = contentLength\n ? Number.parseInt(contentLength) || undefined\n : undefined\n const type =\n response.headers.get('content-type') || 'application/octet-stream'\n const stream = new ProtocolServerBlobStream(-1, {\n size,\n type,\n })\n response.body?.pipeThrough(stream)\n return stream\n } else {\n const body = response.arrayBuffer()\n return body.then((buffer) => {\n if (response.ok) {\n const decoded = this.protocol.format.decode(buffer)\n return transformer.decodeRPC(namespace, procedure, decoded)\n } else {\n if (buffer.byteLength === 0) {\n const error = new ClientError(\n ErrorCode.InternalServerError,\n `Empty response with ${response.status} status code`,\n )\n return Promise.reject(error)\n } else {\n const payload = this.protocol.format.decode(buffer)\n const error = new ClientError(\n payload.code,\n payload.message,\n payload.data,\n )\n return Promise.reject(error)\n }\n }\n })\n }\n })\n .then(call.resolve)\n .catch(call.reject)\n\n return call\n }\n\n async connect(auth: any) {\n this.#auth = auth\n this.emit('connected')\n }\n\n async disconnect() {\n this.emit('disconnected')\n }\n\n async send(\n messageType: ClientMessageType,\n buffer: ArrayBuffer,\n metadata: ProtocolSendMetadata,\n ) {\n throw new Error('Not supported')\n }\n}\n"],"version":3,"file":"index.js"}