@nmtjs/http-client 0.12.5 → 0.12.7

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,29 +3,29 @@
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/client": "0.12.7",
13
+ "@nmtjs/protocol": "0.12.7",
14
+ "@nmtjs/common": "0.12.7"
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/common": "0.12.7",
18
+ "@nmtjs/protocol": "0.12.7",
19
+ "@nmtjs/client": "0.12.7"
19
20
  },
20
21
  "files": [
21
- "src",
22
22
  "dist",
23
23
  "LICENSE.md",
24
24
  "README.md"
25
25
  ],
26
- "version": "0.12.5",
26
+ "version": "0.12.7",
27
27
  "scripts": {
28
- "build": "neemata-build --root=./src './*.ts'",
28
+ "build": "tsc",
29
29
  "type-check": "tsc --noEmit"
30
30
  }
31
31
  }
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,aAEV;CAC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;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"}
package/src/index.ts DELETED
@@ -1,144 +0,0 @@
1
- import { ClientError } from '@nmtjs/client'
2
- import {
3
- type ClientMessageType,
4
- ErrorCode,
5
- ProtocolBlob,
6
- } from '@nmtjs/protocol'
7
- import {
8
- type BaseProtocol,
9
- EventEmitter,
10
- type ProtocolBaseClientCallOptions,
11
- type ProtocolBaseTransformer,
12
- type ProtocolClientCall,
13
- type ProtocolSendMetadata,
14
- ProtocolServerBlobStream,
15
- type ProtocolTransport,
16
- type ProtocolTransportEventMap,
17
- ProtocolTransportStatus,
18
- } from '@nmtjs/protocol/client'
19
-
20
- export type HttpClientTransportOptions = {
21
- /**
22
- * The origin of the server
23
- * @example 'http://localhost:3000'
24
- */
25
- origin: string
26
- debug?: boolean
27
- }
28
-
29
- export class HttpClientTransport
30
- extends EventEmitter<ProtocolTransportEventMap>
31
- implements ProtocolTransport
32
- {
33
- #auth: string | null = null
34
- status: ProtocolTransportStatus = ProtocolTransportStatus.CONNECTED
35
-
36
- constructor(
37
- protected readonly protocol: BaseProtocol,
38
- private readonly options: HttpClientTransportOptions,
39
- ) {
40
- super()
41
- }
42
-
43
- async call(
44
- namespace: string,
45
- procedure: string,
46
- payload: any,
47
- options: ProtocolBaseClientCallOptions,
48
- transformer: ProtocolBaseTransformer,
49
- ): Promise<ProtocolClientCall> {
50
- const call = this.protocol.createCall(namespace, procedure, options)
51
-
52
- const headers = new Headers()
53
-
54
- headers.set('Content-Type', this.protocol.contentType)
55
- headers.set('Accept', this.protocol.contentType)
56
-
57
- if (this.#auth) headers.set('Authorization', this.#auth)
58
-
59
- const isBlob = payload instanceof ProtocolBlob
60
- if (isBlob) headers.set('x-neemata-blob', 'true')
61
-
62
- const response = fetch(
63
- `${this.options.origin}/api/${namespace}/${procedure}`,
64
- {
65
- method: 'POST',
66
- headers,
67
- credentials: 'include',
68
- body: isBlob
69
- ? payload.source
70
- : transformer.encodeRPC(namespace, procedure, payload),
71
- signal: call.signal,
72
- // @ts-expect-error
73
- duplex: 'half',
74
- },
75
- )
76
-
77
- response
78
- .catch((error) =>
79
- Promise.reject(new Error('Network error', { cause: error })),
80
- )
81
- .then((response) => {
82
- const isBlob = response.headers.get('x-neemata-blob') === 'true'
83
- if (isBlob) {
84
- const contentLength = response.headers.get('content-length')
85
- const size = contentLength
86
- ? Number.parseInt(contentLength) || undefined
87
- : undefined
88
- const type =
89
- response.headers.get('content-type') || 'application/octet-stream'
90
- const stream = new ProtocolServerBlobStream(-1, {
91
- size,
92
- type,
93
- })
94
- response.body?.pipeThrough(stream)
95
- return stream
96
- } else {
97
- const body = response.arrayBuffer()
98
- return body.then((buffer) => {
99
- if (response.ok) {
100
- const decoded = this.protocol.format.decode(buffer)
101
- return transformer.decodeRPC(namespace, procedure, decoded)
102
- } else {
103
- if (buffer.byteLength === 0) {
104
- const error = new ClientError(
105
- ErrorCode.InternalServerError,
106
- `Empty response with ${response.status} status code`,
107
- )
108
- return Promise.reject(error)
109
- } else {
110
- const payload = this.protocol.format.decode(buffer)
111
- const error = new ClientError(
112
- payload.code,
113
- payload.message,
114
- payload.data,
115
- )
116
- return Promise.reject(error)
117
- }
118
- }
119
- })
120
- }
121
- })
122
- .then(call.resolve)
123
- .catch(call.reject)
124
-
125
- return call
126
- }
127
-
128
- async connect(auth: any) {
129
- this.#auth = auth
130
- this.emit('connected')
131
- }
132
-
133
- async disconnect() {
134
- this.emit('disconnected')
135
- }
136
-
137
- async send(
138
- messageType: ClientMessageType,
139
- buffer: ArrayBuffer,
140
- metadata: ProtocolSendMetadata,
141
- ) {
142
- throw new Error('Not supported')
143
- }
144
- }