@nmtjs/http-client 0.11.0 → 0.11.2

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
@@ -59,8 +59,11 @@ export class HttpClientTransport extends EventEmitter {
59
59
  }
60
60
  async connect(auth) {
61
61
  this.#auth = auth;
62
+ this.emit("connected");
63
+ }
64
+ async disconnect() {
65
+ this.emit("disconnected");
62
66
  }
63
- async disconnect() {}
64
67
  async send(messageType, buffer, metadata) {
65
68
  throw new Error("Not supported");
66
69
  }
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,KAAK;GAEb,QAAQ;EACT,EACF;AAED,WACG,MAAM,CAAC,UACN,QAAQ,OAAO,IAAI,MAAM,iBAAiB,EAAE,OAAO,MAAO,GAAE,CAC7D,CACA,KAAK,CAAC,aAAa;GAClB,MAAM,SAAS,SAAS,QAAQ,IAAI,iBAAiB,KAAK;AAC1D,OAAI,QAAQ;IACV,MAAM,gBAAgB,SAAS,QAAQ,IAAI,iBAAiB;IAC5D,MAAM,OAAO,gBACT,OAAO,SAAS,cAAc,IAAI,YAClC;IACJ,MAAM,OACJ,SAAS,QAAQ,IAAI,eAAe,IAAI;IAC1C,MAAM,SAAS,IAAI,0BAA0B,GAAG;KAC9C;KACA;IACD;AACD,aAAS,MAAM,YAAY,OAAO;AAClC,WAAO;GACR,OAAM;IACL,MAAM,OAAO,SAAS,aAAa;AACnC,WAAO,KAAK,KAAK,CAAC,WAAW;AAC3B,SAAI,SAAS,IAAI;MACf,MAAM,UAAU,KAAK,SAAS,OAAO,OAAO,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;CACd;CAED,MAAM,aAAa,CAAE;CAErB,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} 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 }\n\n async disconnect() {}\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,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,aA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AAK,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} 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"}
package/package.json CHANGED
@@ -8,14 +8,14 @@
8
8
  }
9
9
  },
10
10
  "dependencies": {
11
- "@nmtjs/common": "0.11.0",
12
- "@nmtjs/protocol": "0.11.0",
13
- "@nmtjs/client": "0.11.0"
11
+ "@nmtjs/common": "0.11.2",
12
+ "@nmtjs/protocol": "0.11.2",
13
+ "@nmtjs/client": "0.11.2"
14
14
  },
15
15
  "peerDependencies": {
16
- "@nmtjs/client": "0.11.0",
17
- "@nmtjs/common": "0.11.0",
18
- "@nmtjs/protocol": "0.11.0"
16
+ "@nmtjs/common": "0.11.2",
17
+ "@nmtjs/protocol": "0.11.2",
18
+ "@nmtjs/client": "0.11.2"
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.11.0",
26
+ "version": "0.11.2",
27
27
  "scripts": {
28
28
  "build": "neemata-build --root=./src './*.ts'",
29
29
  "type-check": "tsc --noEmit"
package/src/index.ts CHANGED
@@ -125,9 +125,12 @@ export class HttpClientTransport
125
125
 
126
126
  async connect(auth: any) {
127
127
  this.#auth = auth
128
+ this.emit('connected')
128
129
  }
129
130
 
130
- async disconnect() {}
131
+ async disconnect() {
132
+ this.emit('disconnected')
133
+ }
131
134
 
132
135
  async send(
133
136
  messageType: ClientMessageType,