@nmtjs/http-client 0.12.2 → 0.12.4

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.
package/dist/index.js CHANGED
@@ -1,8 +1,9 @@
1
1
  import { ClientError } from "@nmtjs/client";
2
2
  import { ErrorCode, ProtocolBlob } from "@nmtjs/protocol";
3
- import { EventEmitter, ProtocolServerBlobStream } from "@nmtjs/protocol/client";
3
+ import { EventEmitter, ProtocolServerBlobStream, ProtocolTransportStatus } from "@nmtjs/protocol/client";
4
4
  export class HttpClientTransport extends EventEmitter {
5
5
  #auth = null;
6
+ status = ProtocolTransportStatus.CONNECTED;
6
7
  constructor(protocol, options) {
7
8
  super();
8
9
  this.protocol = protocol;
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"mappings":"AAAA,SAAS,mBAAmB,eAAe;AAC3C,SAEE,WACA,oBACK,iBAAiB;AACxB,SAEE,cAKA,gCAGK,wBAAwB;AAW/B,OAAO,MAAM,4BACH,aAEV;CACE,QAAuB;CAEvB,YACqBA,UACFC,SACjB;AACA,SAAO;OAHY;OACF;CAGlB;CAED,MAAM,KACJC,WACAC,WACAC,SACAC,SACAC,aAC6B;EAC7B,MAAM,OAAO,KAAK,SAAS,WAAW,WAAW,WAAW,QAAQ;EAEpE,MAAM,UAAU,IAAI;AAEpB,UAAQ,IAAI,gBAAgB,KAAK,SAAS,YAAY;AACtD,UAAQ,IAAI,UAAU,KAAK,SAAS,YAAY;AAEhD,MAAI,KAAKC,MAAO,SAAQ,IAAI,iBAAiB,KAAKA,MAAM;EAExD,MAAM,SAAS,mBAAmB;AAClC,MAAI,OAAQ,SAAQ,IAAI,kBAAkB,OAAO;EAEjD,MAAM,WAAW,OACd,EAAE,KAAK,QAAQ,OAAO,OAAO,UAAU,GAAG,UAAU,GACrD;GACE,QAAQ;GACR;GACA,aAAa;GACb,MAAM,SACF,QAAQ,SACR,YAAY,UAAU,WAAW,WAAW,QAAQ;GACxD,QAAQ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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} 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\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"}
1
+ {"mappings":"AAAA,SAAS,mBAAmB,eAAe;AAC3C,SAEE,WACA,oBACK,iBAAiB;AACxB,SAEE,cAKA,0BAGA,+BACK,wBAAwB;AAW/B,OAAO,MAAM,4BACH,aAEV;CACE,QAAuB;CACvB,SAAkC,wBAAwB;CAE1D,YACqBA,UACFC,SACjB;AACA,SAAO;OAHY;OACF;CAGlB;CAED,MAAM,KACJC,WACAC,WACAC,SACAC,SACAC,aAC6B;EAC7B,MAAM,OAAO,KAAK,SAAS,WAAW,WAAW,WAAW,QAAQ;EAEpE,MAAM,UAAU,IAAI;AAEpB,UAAQ,IAAI,gBAAgB,KAAK,SAAS,YAAY;AACtD,UAAQ,IAAI,UAAU,KAAK,SAAS,YAAY;AAEhD,MAAI,KAAKC,M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AO,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/package.json CHANGED
@@ -8,14 +8,14 @@
8
8
  }
9
9
  },
10
10
  "dependencies": {
11
- "@nmtjs/client": "0.12.2",
12
- "@nmtjs/common": "0.12.2",
13
- "@nmtjs/protocol": "0.12.2"
11
+ "@nmtjs/client": "0.12.4",
12
+ "@nmtjs/common": "0.12.4",
13
+ "@nmtjs/protocol": "0.12.4"
14
14
  },
15
15
  "peerDependencies": {
16
- "@nmtjs/common": "0.12.2",
17
- "@nmtjs/client": "0.12.2",
18
- "@nmtjs/protocol": "0.12.2"
16
+ "@nmtjs/common": "0.12.4",
17
+ "@nmtjs/client": "0.12.4",
18
+ "@nmtjs/protocol": "0.12.4"
19
19
  },
20
20
  "files": [
21
21
  "src",
@@ -23,7 +23,7 @@
23
23
  "LICENSE.md",
24
24
  "README.md"
25
25
  ],
26
- "version": "0.12.2",
26
+ "version": "0.12.4",
27
27
  "scripts": {
28
28
  "build": "neemata-build --root=./src './*.ts'",
29
29
  "type-check": "tsc --noEmit"
package/src/index.ts CHANGED
@@ -14,6 +14,7 @@ import {
14
14
  ProtocolServerBlobStream,
15
15
  type ProtocolTransport,
16
16
  type ProtocolTransportEventMap,
17
+ ProtocolTransportStatus,
17
18
  } from '@nmtjs/protocol/client'
18
19
 
19
20
  export type HttpClientTransportOptions = {
@@ -30,6 +31,7 @@ export class HttpClientTransport
30
31
  implements ProtocolTransport
31
32
  {
32
33
  #auth: string | null = null
34
+ status: ProtocolTransportStatus = ProtocolTransportStatus.CONNECTED
33
35
 
34
36
  constructor(
35
37
  protected readonly protocol: BaseProtocol,