@nmtjs/http-client 0.1.8 → 0.10.0
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/README.md +9 -1
- package/dist/index.js +68 -78
- package/dist/index.js.map +1 -1
- package/package.json +13 -12
- package/src/index.ts +139 -0
- package/index.ts +0 -92
- package/tsconfig.json +0 -6
package/README.md
CHANGED
|
@@ -1 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
# NeemataJS - RPC application server for real-time applications (proof of concept)
|
|
2
|
+
|
|
3
|
+
### Built with following in mind:
|
|
4
|
+
- transport-agnostic (like WebSockets, WebTransport, .etc)
|
|
5
|
+
- format-agnostic (like JSON, MessagePack, BSON, .etc)
|
|
6
|
+
- binary data streaming and event subscriptions
|
|
7
|
+
- contract-based API
|
|
8
|
+
- end-to-end type safety
|
|
9
|
+
- CPU-intensive task execution on separate workers
|
package/dist/index.js
CHANGED
|
@@ -1,79 +1,69 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
return {
|
|
68
|
-
success: true,
|
|
69
|
-
value: result
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
getURL(path = '', params = '') {
|
|
75
|
-
const url = new URL(path, this.options.origin);
|
|
76
|
-
url.search = params;
|
|
77
|
-
return url;
|
|
78
|
-
}
|
|
1
|
+
import { ClientError } from "@nmtjs/client";
|
|
2
|
+
import { EventEmitter, ProtocolServerBlobStream } from "@nmtjs/protocol/client";
|
|
3
|
+
import { ErrorCode, ProtocolBlob } from "@nmtjs/protocol";
|
|
4
|
+
export class HttpClientTransport extends EventEmitter {
|
|
5
|
+
#auth = null;
|
|
6
|
+
constructor(protocol, options) {
|
|
7
|
+
super();
|
|
8
|
+
this.protocol = protocol;
|
|
9
|
+
this.options = options;
|
|
10
|
+
}
|
|
11
|
+
async call(namespace, procedure, payload, options, transformer) {
|
|
12
|
+
const call = this.protocol.createCall(namespace, procedure, options);
|
|
13
|
+
const headers = new Headers();
|
|
14
|
+
headers.set("Content-Type", this.protocol.contentType);
|
|
15
|
+
headers.set("Accept", this.protocol.contentType);
|
|
16
|
+
if (this.#auth) headers.set("Authorization", this.#auth);
|
|
17
|
+
const isBlob = payload instanceof ProtocolBlob;
|
|
18
|
+
if (isBlob) headers.set("x-neemata-blob", "true");
|
|
19
|
+
const response = fetch(`${this.options.origin}/api/${namespace}/${procedure}`, {
|
|
20
|
+
method: "POST",
|
|
21
|
+
headers,
|
|
22
|
+
credentials: "include",
|
|
23
|
+
body: isBlob ? payload.source : transformer.encodeRPC(namespace, procedure, payload),
|
|
24
|
+
signal: call.signal,
|
|
25
|
+
duplex: "half"
|
|
26
|
+
});
|
|
27
|
+
response.catch((error) => Promise.reject(new Error("Network error", { cause: error }))).then((response) => {
|
|
28
|
+
const isBlob = response.headers.get("x-neemata-blob") === "true";
|
|
29
|
+
if (isBlob) {
|
|
30
|
+
const contentLength = response.headers.get("content-length");
|
|
31
|
+
const size = contentLength ? Number.parseInt(contentLength) || undefined : undefined;
|
|
32
|
+
const type = response.headers.get("content-type") || "application/octet-stream";
|
|
33
|
+
const stream = new ProtocolServerBlobStream(-1, {
|
|
34
|
+
size,
|
|
35
|
+
type
|
|
36
|
+
});
|
|
37
|
+
response.body?.pipeThrough(stream);
|
|
38
|
+
return stream;
|
|
39
|
+
} else {
|
|
40
|
+
const body = response.arrayBuffer();
|
|
41
|
+
return body.then((buffer) => {
|
|
42
|
+
if (response.ok) {
|
|
43
|
+
const decoded = this.protocol.format.decode(buffer);
|
|
44
|
+
return transformer.decodeRPC(namespace, procedure, decoded);
|
|
45
|
+
} else {
|
|
46
|
+
if (buffer.byteLength === 0) {
|
|
47
|
+
const error = new ClientError(ErrorCode.InternalServerError, `Empty response with ${response.status} status code`);
|
|
48
|
+
return Promise.reject(error);
|
|
49
|
+
} else {
|
|
50
|
+
const payload = this.protocol.format.decode(buffer);
|
|
51
|
+
const error = new ClientError(payload.code, payload.message, payload.data);
|
|
52
|
+
return Promise.reject(error);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
}).then(call.resolve).catch(call.reject);
|
|
58
|
+
return call;
|
|
59
|
+
}
|
|
60
|
+
async connect(auth) {
|
|
61
|
+
this.#auth = auth;
|
|
62
|
+
}
|
|
63
|
+
async disconnect() {}
|
|
64
|
+
async send(messageType, buffer, metadata) {
|
|
65
|
+
throw new Error("Not supported");
|
|
66
|
+
}
|
|
79
67
|
}
|
|
68
|
+
|
|
69
|
+
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"
|
|
1
|
+
{"mappings":"AAAA,SAAS,mBAAmB,eAAe;AAC3C,SAEE,cAKA,gCAGK,wBAAwB;AAC/B,SAEE,WACA,oBACK,iBAAiB;AAWxB,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 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'\nimport {\n type ClientMessageType,\n ErrorCode,\n ProtocolBlob,\n} from '@nmtjs/protocol'\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"}
|
package/package.json
CHANGED
|
@@ -3,28 +3,29 @@
|
|
|
3
3
|
"type": "module",
|
|
4
4
|
"exports": {
|
|
5
5
|
".": {
|
|
6
|
-
"
|
|
7
|
-
"
|
|
6
|
+
"types": "./src/index.ts",
|
|
7
|
+
"import": "./dist/index.js"
|
|
8
8
|
}
|
|
9
9
|
},
|
|
10
|
-
"
|
|
11
|
-
"@nmtjs/
|
|
12
|
-
"@nmtjs/
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"@nmtjs/client": "0.10.0",
|
|
12
|
+
"@nmtjs/protocol": "0.10.0",
|
|
13
|
+
"@nmtjs/common": "0.10.0"
|
|
13
14
|
},
|
|
14
|
-
"
|
|
15
|
-
"@nmtjs/
|
|
16
|
-
"@nmtjs/
|
|
15
|
+
"peerDependencies": {
|
|
16
|
+
"@nmtjs/client": "0.10.0",
|
|
17
|
+
"@nmtjs/protocol": "0.10.0",
|
|
18
|
+
"@nmtjs/common": "0.10.0"
|
|
17
19
|
},
|
|
18
20
|
"files": [
|
|
19
|
-
"
|
|
21
|
+
"src",
|
|
20
22
|
"dist",
|
|
21
|
-
"tsconfig.json",
|
|
22
23
|
"LICENSE.md",
|
|
23
24
|
"README.md"
|
|
24
25
|
],
|
|
25
|
-
"version": "0.
|
|
26
|
+
"version": "0.10.0",
|
|
26
27
|
"scripts": {
|
|
27
|
-
"build": "neemata-build
|
|
28
|
+
"build": "neemata-build --root=./src './*.ts'",
|
|
28
29
|
"type-check": "tsc --noEmit"
|
|
29
30
|
}
|
|
30
31
|
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { ClientError } from '@nmtjs/client'
|
|
2
|
+
import {
|
|
3
|
+
type BaseProtocol,
|
|
4
|
+
EventEmitter,
|
|
5
|
+
type ProtocolBaseClientCallOptions,
|
|
6
|
+
type ProtocolBaseTransformer,
|
|
7
|
+
type ProtocolClientCall,
|
|
8
|
+
type ProtocolSendMetadata,
|
|
9
|
+
ProtocolServerBlobStream,
|
|
10
|
+
type ProtocolTransport,
|
|
11
|
+
type ProtocolTransportEventMap,
|
|
12
|
+
} from '@nmtjs/protocol/client'
|
|
13
|
+
import {
|
|
14
|
+
type ClientMessageType,
|
|
15
|
+
ErrorCode,
|
|
16
|
+
ProtocolBlob,
|
|
17
|
+
} from '@nmtjs/protocol'
|
|
18
|
+
|
|
19
|
+
export type HttpClientTransportOptions = {
|
|
20
|
+
/**
|
|
21
|
+
* The origin of the server
|
|
22
|
+
* @example 'http://localhost:3000'
|
|
23
|
+
*/
|
|
24
|
+
origin: string
|
|
25
|
+
debug?: boolean
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export class HttpClientTransport
|
|
29
|
+
extends EventEmitter<ProtocolTransportEventMap>
|
|
30
|
+
implements ProtocolTransport
|
|
31
|
+
{
|
|
32
|
+
#auth: string | null = null
|
|
33
|
+
|
|
34
|
+
constructor(
|
|
35
|
+
protected readonly protocol: BaseProtocol,
|
|
36
|
+
private readonly options: HttpClientTransportOptions,
|
|
37
|
+
) {
|
|
38
|
+
super()
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async call(
|
|
42
|
+
namespace: string,
|
|
43
|
+
procedure: string,
|
|
44
|
+
payload: any,
|
|
45
|
+
options: ProtocolBaseClientCallOptions,
|
|
46
|
+
transformer: ProtocolBaseTransformer,
|
|
47
|
+
): Promise<ProtocolClientCall> {
|
|
48
|
+
const call = this.protocol.createCall(namespace, procedure, options)
|
|
49
|
+
|
|
50
|
+
const headers = new Headers()
|
|
51
|
+
|
|
52
|
+
headers.set('Content-Type', this.protocol.contentType)
|
|
53
|
+
headers.set('Accept', this.protocol.contentType)
|
|
54
|
+
|
|
55
|
+
if (this.#auth) headers.set('Authorization', this.#auth)
|
|
56
|
+
|
|
57
|
+
const isBlob = payload instanceof ProtocolBlob
|
|
58
|
+
if (isBlob) headers.set('x-neemata-blob', 'true')
|
|
59
|
+
|
|
60
|
+
const response = fetch(
|
|
61
|
+
`${this.options.origin}/api/${namespace}/${procedure}`,
|
|
62
|
+
{
|
|
63
|
+
method: 'POST',
|
|
64
|
+
headers,
|
|
65
|
+
credentials: 'include',
|
|
66
|
+
body: isBlob
|
|
67
|
+
? payload.source
|
|
68
|
+
: transformer.encodeRPC(namespace, procedure, payload),
|
|
69
|
+
signal: call.signal,
|
|
70
|
+
// @ts-expect-error
|
|
71
|
+
duplex: 'half',
|
|
72
|
+
},
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
response
|
|
76
|
+
.catch((error) =>
|
|
77
|
+
Promise.reject(new Error('Network error', { cause: error })),
|
|
78
|
+
)
|
|
79
|
+
.then((response) => {
|
|
80
|
+
const isBlob = response.headers.get('x-neemata-blob') === 'true'
|
|
81
|
+
if (isBlob) {
|
|
82
|
+
const contentLength = response.headers.get('content-length')
|
|
83
|
+
const size = contentLength
|
|
84
|
+
? Number.parseInt(contentLength) || undefined
|
|
85
|
+
: undefined
|
|
86
|
+
const type =
|
|
87
|
+
response.headers.get('content-type') || 'application/octet-stream'
|
|
88
|
+
const stream = new ProtocolServerBlobStream(-1, {
|
|
89
|
+
size,
|
|
90
|
+
type,
|
|
91
|
+
})
|
|
92
|
+
response.body?.pipeThrough(stream)
|
|
93
|
+
return stream
|
|
94
|
+
} else {
|
|
95
|
+
const body = response.arrayBuffer()
|
|
96
|
+
return body.then((buffer) => {
|
|
97
|
+
if (response.ok) {
|
|
98
|
+
const decoded = this.protocol.format.decode(buffer)
|
|
99
|
+
return transformer.decodeRPC(namespace, procedure, decoded)
|
|
100
|
+
} else {
|
|
101
|
+
if (buffer.byteLength === 0) {
|
|
102
|
+
const error = new ClientError(
|
|
103
|
+
ErrorCode.InternalServerError,
|
|
104
|
+
`Empty response with ${response.status} status code`,
|
|
105
|
+
)
|
|
106
|
+
return Promise.reject(error)
|
|
107
|
+
} else {
|
|
108
|
+
const payload = this.protocol.format.decode(buffer)
|
|
109
|
+
const error = new ClientError(
|
|
110
|
+
payload.code,
|
|
111
|
+
payload.message,
|
|
112
|
+
payload.data,
|
|
113
|
+
)
|
|
114
|
+
return Promise.reject(error)
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
})
|
|
118
|
+
}
|
|
119
|
+
})
|
|
120
|
+
.then(call.resolve)
|
|
121
|
+
.catch(call.reject)
|
|
122
|
+
|
|
123
|
+
return call
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async connect(auth: any) {
|
|
127
|
+
this.#auth = auth
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
async disconnect() {}
|
|
131
|
+
|
|
132
|
+
async send(
|
|
133
|
+
messageType: ClientMessageType,
|
|
134
|
+
buffer: ArrayBuffer,
|
|
135
|
+
metadata: ProtocolSendMetadata,
|
|
136
|
+
) {
|
|
137
|
+
throw new Error('Not supported')
|
|
138
|
+
}
|
|
139
|
+
}
|
package/index.ts
DELETED
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ClientTransport,
|
|
3
|
-
type ClientTransportRpcCall,
|
|
4
|
-
type ClientTransportRpcResult,
|
|
5
|
-
} from '@nmtjs/client'
|
|
6
|
-
import { ErrorCode, TransportType } from '@nmtjs/common'
|
|
7
|
-
|
|
8
|
-
export type ClientOptions = {
|
|
9
|
-
/**
|
|
10
|
-
* The origin of the server
|
|
11
|
-
* @example 'http://localhost:3000'
|
|
12
|
-
*/
|
|
13
|
-
origin: string
|
|
14
|
-
debug?: boolean
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export type HttpRpcOptions = {
|
|
18
|
-
timeout?: number
|
|
19
|
-
headers?: Record<string, string>
|
|
20
|
-
signal?: AbortSignal
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export class HttpClient extends ClientTransport {
|
|
24
|
-
type = TransportType.HTTP
|
|
25
|
-
private attempts = 0
|
|
26
|
-
|
|
27
|
-
constructor(private readonly options: ClientOptions) {
|
|
28
|
-
super()
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
async healthCheck() {
|
|
32
|
-
while (true) {
|
|
33
|
-
try {
|
|
34
|
-
const signal = AbortSignal.timeout(10000)
|
|
35
|
-
const url = this.getURL('healthy')
|
|
36
|
-
const { ok } = await fetch(url, { signal })
|
|
37
|
-
if (ok) break
|
|
38
|
-
} catch (e) {}
|
|
39
|
-
this.attempts++
|
|
40
|
-
const seconds = Math.min(this.attempts, 15)
|
|
41
|
-
await new Promise((r) => setTimeout(r, seconds * 1000))
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
async connect() {}
|
|
46
|
-
async disconnect() {}
|
|
47
|
-
|
|
48
|
-
async rpc(params: ClientTransportRpcCall): Promise<ClientTransportRpcResult> {
|
|
49
|
-
const { service, procedure, payload, signal } = params
|
|
50
|
-
const url = this.getURL(`api/${service}/${procedure}`)
|
|
51
|
-
const body = this.client.format.encode(payload)
|
|
52
|
-
|
|
53
|
-
const response = await fetch(url, {
|
|
54
|
-
method: 'POST',
|
|
55
|
-
body,
|
|
56
|
-
signal,
|
|
57
|
-
keepalive: true,
|
|
58
|
-
credentials: 'include',
|
|
59
|
-
cache: 'no-cache',
|
|
60
|
-
headers: {
|
|
61
|
-
'Content-Type': this.client.format.contentType,
|
|
62
|
-
Accept: this.client.format.contentType,
|
|
63
|
-
...(this.client.auth ? { Authorization: this.client.auth } : {}),
|
|
64
|
-
},
|
|
65
|
-
})
|
|
66
|
-
|
|
67
|
-
if (!response.ok) {
|
|
68
|
-
return {
|
|
69
|
-
success: false,
|
|
70
|
-
error: {
|
|
71
|
-
code: ErrorCode.InternalServerError,
|
|
72
|
-
data: { status: response.status, statusText: response.statusText },
|
|
73
|
-
message: await response.text(),
|
|
74
|
-
},
|
|
75
|
-
}
|
|
76
|
-
} else {
|
|
77
|
-
const buf = await response.arrayBuffer()
|
|
78
|
-
const { error, result } = this.client.format.decode(buf)
|
|
79
|
-
if (error) {
|
|
80
|
-
return { success: false, error }
|
|
81
|
-
} else {
|
|
82
|
-
return { success: true, value: result }
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
private getURL(path = '', params = '') {
|
|
88
|
-
const url = new URL(path, this.options.origin)
|
|
89
|
-
url.search = params
|
|
90
|
-
return url
|
|
91
|
-
}
|
|
92
|
-
}
|