@nmtjs/http-client 0.15.0-beta.3 → 0.15.0-beta.31
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.d.ts +5 -5
- package/dist/index.js +15 -5
- package/dist/index.js.map +1 -1
- package/package.json +9 -11
- package/src/index.ts +197 -0
package/dist/index.d.ts
CHANGED
|
@@ -20,7 +20,7 @@ export declare class HttpTransportClient implements ClientTransport<ConnectionTy
|
|
|
20
20
|
type: ConnectionType.Unidirectional;
|
|
21
21
|
decodeBase64: DecodeBase64Function;
|
|
22
22
|
constructor(format: BaseClientFormat, protocol: ProtocolVersion, options: HttpClientTransportOptions);
|
|
23
|
-
url({ procedure, application, payload
|
|
23
|
+
url({ procedure, application, payload }: {
|
|
24
24
|
procedure: string;
|
|
25
25
|
application?: string;
|
|
26
26
|
payload?: unknown;
|
|
@@ -31,7 +31,7 @@ export declare class HttpTransportClient implements ClientTransport<ConnectionTy
|
|
|
31
31
|
payload: unknown;
|
|
32
32
|
}, options: ClientTransportMessageOptions): Promise<{
|
|
33
33
|
type: "rpc_stream";
|
|
34
|
-
stream: ReadableStream<ArrayBufferView
|
|
34
|
+
stream: ReadableStream<ArrayBufferView<ArrayBufferLike>>;
|
|
35
35
|
} | {
|
|
36
36
|
type: "blob";
|
|
37
37
|
metadata: {
|
|
@@ -39,13 +39,13 @@ export declare class HttpTransportClient implements ClientTransport<ConnectionTy
|
|
|
39
39
|
size: number | undefined;
|
|
40
40
|
filename: string | undefined;
|
|
41
41
|
};
|
|
42
|
-
source:
|
|
42
|
+
source: ReadableStream<Uint8Array<ArrayBuffer>>;
|
|
43
43
|
result?: undefined;
|
|
44
44
|
} | {
|
|
45
|
-
type: "rpc";
|
|
46
|
-
result: Uint8Array<ArrayBuffer>;
|
|
47
45
|
metadata?: undefined;
|
|
48
46
|
source?: undefined;
|
|
47
|
+
type: "rpc";
|
|
48
|
+
result: Uint8Array<ArrayBuffer>;
|
|
49
49
|
}>;
|
|
50
50
|
}
|
|
51
51
|
export type HttpTransportFactory = ClientTransportFactory<ConnectionType.Unidirectional, HttpClientTransportOptions, HttpTransportClient>;
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { createFuture } from '@nmtjs/common';
|
|
2
|
-
import { ConnectionType, ProtocolBlob } from '@nmtjs/protocol';
|
|
2
|
+
import { ConnectionType, ErrorCode, ProtocolBlob } from '@nmtjs/protocol';
|
|
3
|
+
import { ProtocolError } from '@nmtjs/protocol/client';
|
|
3
4
|
const createDecodeBase64 = (customFn) => {
|
|
4
5
|
return (string) => {
|
|
5
6
|
if ('fromBase64' in Uint8Array &&
|
|
@@ -77,6 +78,7 @@ export class HttpTransportClient {
|
|
|
77
78
|
catch (cause) {
|
|
78
79
|
const error = new Error('Failed to decode stream message', { cause });
|
|
79
80
|
writable.abort(error);
|
|
81
|
+
source.close();
|
|
80
82
|
}
|
|
81
83
|
});
|
|
82
84
|
return future.promise;
|
|
@@ -106,7 +108,7 @@ export class HttpTransportClient {
|
|
|
106
108
|
return {
|
|
107
109
|
type: 'blob',
|
|
108
110
|
metadata: { type, size, filename },
|
|
109
|
-
source: body,
|
|
111
|
+
source: response.body,
|
|
110
112
|
};
|
|
111
113
|
}
|
|
112
114
|
else {
|
|
@@ -114,9 +116,17 @@ export class HttpTransportClient {
|
|
|
114
116
|
}
|
|
115
117
|
}
|
|
116
118
|
else {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
119
|
+
try {
|
|
120
|
+
const buffer = await response.bytes();
|
|
121
|
+
const error = client.format.decode(buffer);
|
|
122
|
+
throw new ProtocolError(error.code || ErrorCode.ClientRequestError, error.message || response.statusText, error.data);
|
|
123
|
+
}
|
|
124
|
+
catch (cause) {
|
|
125
|
+
if (cause instanceof ProtocolError)
|
|
126
|
+
throw cause;
|
|
127
|
+
// If decoding fails, throw generic error with status info
|
|
128
|
+
throw new ProtocolError(ErrorCode.ClientRequestError, `HTTP ${response.status}: ${response.statusText}`);
|
|
129
|
+
}
|
|
120
130
|
}
|
|
121
131
|
}
|
|
122
132
|
}
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAA;AAC5C,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAA;AAC5C,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AACzE,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAA;AAItD,MAAM,kBAAkB,GAAG,CACzB,QAA+B,EACT,EAAE,CAAC;IACzB,OAAO,CAAC,MAAc,EAAE,EAAE,CAAC;QACzB,IACE,YAAY,IAAI,UAAU;YAC1B,OAAO,UAAU,CAAC,UAAU,KAAK,UAAU,EAC3C,CAAC;YACD,OAAO,UAAU,CAAC,UAAU,CAAC,MAAM,CAAC,CAAA;QACtC,CAAC;aAAM,IAAI,OAAO,IAAI,KAAK,UAAU,EAAE,CAAC;YACtC,OAAO,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAA;QAC9D,CAAC;aAAM,IAAI,QAAQ,EAAE,CAAC;YACpB,OAAO,QAAQ,CAAC,MAAM,CAAC,CAAA;QACzB,CAAC;aAAM,CAAC;YACN,MAAM,IAAI,KAAK,CAAC,uCAAuC,CAAC,CAAA;QAC1D,CAAC;IAAA,CACF,CAAA;AAAA,CACF,CAAA;AAED,MAAM,mBAAmB,GAAG,gBAAgB,CA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YAAY,aAAa;wBAAE,MAAM,KAAK,CAAA;oBAC/C,0DAA0D;oBAC1D,MAAM,IAAI,aAAa,CACrB,SAAS,CAAC,kBAAkB,EAC5B,QAAQ,QAAQ,CAAC,MAAM,KAAK,QAAQ,CAAC,UAAU,EAAE,CAClD,CAAA;gBACH,CAAC;YACH,CAAC;QACH,CAAC;IAAA,CACF;CACF;AAQD,MAAM,CAAC,MAAM,oBAAoB,GAAyB,CAAC,MAAM,EAAE,OAAO,EAAE,EAAE,CAAC;IAC7E,OAAO,IAAI,mBAAmB,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAA;AAAA,CACxE,CAAA"}
|
package/package.json
CHANGED
|
@@ -9,25 +9,23 @@
|
|
|
9
9
|
}
|
|
10
10
|
},
|
|
11
11
|
"dependencies": {
|
|
12
|
-
"@nmtjs/client": "0.15.0-beta.
|
|
13
|
-
"@nmtjs/common": "0.15.0-beta.
|
|
14
|
-
"@nmtjs/protocol": "0.15.0-beta.
|
|
12
|
+
"@nmtjs/client": "0.15.0-beta.31",
|
|
13
|
+
"@nmtjs/common": "0.15.0-beta.31",
|
|
14
|
+
"@nmtjs/protocol": "0.15.0-beta.31"
|
|
15
15
|
},
|
|
16
16
|
"peerDependencies": {
|
|
17
|
-
"@nmtjs/
|
|
18
|
-
"@nmtjs/
|
|
19
|
-
"@nmtjs/common": "0.15.0-beta.
|
|
17
|
+
"@nmtjs/protocol": "0.15.0-beta.31",
|
|
18
|
+
"@nmtjs/client": "0.15.0-beta.31",
|
|
19
|
+
"@nmtjs/common": "0.15.0-beta.31"
|
|
20
20
|
},
|
|
21
21
|
"files": [
|
|
22
22
|
"dist",
|
|
23
|
+
"src",
|
|
23
24
|
"LICENSE.md",
|
|
24
25
|
"README.md"
|
|
25
26
|
],
|
|
26
|
-
"version": "0.15.0-beta.
|
|
27
|
+
"version": "0.15.0-beta.31",
|
|
27
28
|
"scripts": {
|
|
28
|
-
"clean-build": "rm -rf ./dist"
|
|
29
|
-
"build": "tsc --declaration --sourcemap",
|
|
30
|
-
"dev": "tsc --watch",
|
|
31
|
-
"type-check": "tsc --noEmit"
|
|
29
|
+
"clean-build": "rm -rf ./dist"
|
|
32
30
|
}
|
|
33
31
|
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ClientTransport,
|
|
3
|
+
ClientTransportFactory,
|
|
4
|
+
ClientTransportMessageOptions,
|
|
5
|
+
ClientTransportRpcParams,
|
|
6
|
+
} from '@nmtjs/client'
|
|
7
|
+
import type { ProtocolVersion } from '@nmtjs/protocol'
|
|
8
|
+
import type { BaseClientFormat } from '@nmtjs/protocol/client'
|
|
9
|
+
import { createFuture } from '@nmtjs/common'
|
|
10
|
+
import { ConnectionType, ErrorCode, ProtocolBlob } from '@nmtjs/protocol'
|
|
11
|
+
import { ProtocolError } from '@nmtjs/protocol/client'
|
|
12
|
+
|
|
13
|
+
type DecodeBase64Function = (data: string) => ArrayBufferView
|
|
14
|
+
|
|
15
|
+
const createDecodeBase64 = (
|
|
16
|
+
customFn?: DecodeBase64Function,
|
|
17
|
+
): DecodeBase64Function => {
|
|
18
|
+
return (string: string) => {
|
|
19
|
+
if (
|
|
20
|
+
'fromBase64' in Uint8Array &&
|
|
21
|
+
typeof Uint8Array.fromBase64 === 'function'
|
|
22
|
+
) {
|
|
23
|
+
return Uint8Array.fromBase64(string)
|
|
24
|
+
} else if (typeof atob === 'function') {
|
|
25
|
+
return Uint8Array.from(atob(string), (c) => c.charCodeAt(0))
|
|
26
|
+
} else if (customFn) {
|
|
27
|
+
return customFn(string)
|
|
28
|
+
} else {
|
|
29
|
+
throw new Error('No base64 decoding function available')
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const NEEMATA_BLOB_HEADER = 'X-Neemata-Blob'
|
|
35
|
+
|
|
36
|
+
export type HttpClientTransportOptions = {
|
|
37
|
+
/**
|
|
38
|
+
* The origin of the server
|
|
39
|
+
* @example 'http://localhost:3000'
|
|
40
|
+
*/
|
|
41
|
+
url: string
|
|
42
|
+
debug?: boolean
|
|
43
|
+
EventSource?: typeof EventSource
|
|
44
|
+
decodeBase64?: DecodeBase64Function
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export class HttpTransportClient
|
|
48
|
+
implements ClientTransport<ConnectionType.Unidirectional>
|
|
49
|
+
{
|
|
50
|
+
type: ConnectionType.Unidirectional = ConnectionType.Unidirectional
|
|
51
|
+
decodeBase64: DecodeBase64Function
|
|
52
|
+
|
|
53
|
+
constructor(
|
|
54
|
+
protected readonly format: BaseClientFormat,
|
|
55
|
+
protected readonly protocol: ProtocolVersion,
|
|
56
|
+
protected options: HttpClientTransportOptions,
|
|
57
|
+
) {
|
|
58
|
+
this.options = { debug: false, ...options }
|
|
59
|
+
this.decodeBase64 = createDecodeBase64(options.decodeBase64)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
url({
|
|
63
|
+
procedure,
|
|
64
|
+
application,
|
|
65
|
+
payload,
|
|
66
|
+
}: {
|
|
67
|
+
procedure: string
|
|
68
|
+
application?: string
|
|
69
|
+
payload?: unknown
|
|
70
|
+
}) {
|
|
71
|
+
const base = application ? `/${application}/${procedure}` : `/${procedure}`
|
|
72
|
+
const url = new URL(base, this.options.url)
|
|
73
|
+
if (payload) url.searchParams.set('payload', JSON.stringify(payload))
|
|
74
|
+
return url
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async call(
|
|
78
|
+
client: ClientTransportRpcParams,
|
|
79
|
+
rpc: { callId: number; procedure: string; payload: unknown },
|
|
80
|
+
options: ClientTransportMessageOptions,
|
|
81
|
+
) {
|
|
82
|
+
const { procedure, payload } = rpc
|
|
83
|
+
const requestHeaders = new Headers()
|
|
84
|
+
|
|
85
|
+
const url = this.url({ application: client.application, procedure })
|
|
86
|
+
|
|
87
|
+
if (client.auth) requestHeaders.set('Authorization', client.auth)
|
|
88
|
+
|
|
89
|
+
let body: any
|
|
90
|
+
|
|
91
|
+
if (payload instanceof ProtocolBlob) {
|
|
92
|
+
requestHeaders.set('Content-Type', payload.metadata.type)
|
|
93
|
+
requestHeaders.set(NEEMATA_BLOB_HEADER, 'true')
|
|
94
|
+
} else {
|
|
95
|
+
requestHeaders.set('Content-Type', client.format.contentType)
|
|
96
|
+
const buffer = client.format.encode(payload)
|
|
97
|
+
body = buffer
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (options._stream_response) {
|
|
101
|
+
const _constructor = this.options.EventSource
|
|
102
|
+
? this.options.EventSource
|
|
103
|
+
: EventSource
|
|
104
|
+
const source = new _constructor(url.toString(), { withCredentials: true })
|
|
105
|
+
const future = createFuture<{
|
|
106
|
+
type: 'rpc_stream'
|
|
107
|
+
stream: ReadableStream<ArrayBufferView>
|
|
108
|
+
}>()
|
|
109
|
+
const { readable, writable } = new TransformStream()
|
|
110
|
+
const writer = writable.getWriter()
|
|
111
|
+
source.addEventListener('open', () =>
|
|
112
|
+
future.resolve({ type: 'rpc_stream', stream: readable }),
|
|
113
|
+
)
|
|
114
|
+
source.addEventListener('close', () => writable.close())
|
|
115
|
+
source.addEventListener('error', (event) => {
|
|
116
|
+
const error = new Error('Stream error', { cause: event })
|
|
117
|
+
future.reject(error)
|
|
118
|
+
writable.abort(error)
|
|
119
|
+
})
|
|
120
|
+
source.addEventListener('message', (event) => {
|
|
121
|
+
try {
|
|
122
|
+
const buffer = this.decodeBase64(event.data)
|
|
123
|
+
writer.write(buffer)
|
|
124
|
+
} catch (cause) {
|
|
125
|
+
const error = new Error('Failed to decode stream message', { cause })
|
|
126
|
+
writable.abort(error)
|
|
127
|
+
source.close()
|
|
128
|
+
}
|
|
129
|
+
})
|
|
130
|
+
return future.promise
|
|
131
|
+
} else {
|
|
132
|
+
const response = await fetch(url.toString(), {
|
|
133
|
+
body,
|
|
134
|
+
method: 'POST',
|
|
135
|
+
headers: requestHeaders,
|
|
136
|
+
signal: options.signal,
|
|
137
|
+
credentials: 'include',
|
|
138
|
+
keepalive: true,
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
if (response.ok) {
|
|
142
|
+
const isBlob = !!response.headers.get(NEEMATA_BLOB_HEADER)
|
|
143
|
+
if (isBlob) {
|
|
144
|
+
const contentLength = response.headers.get('content-length')
|
|
145
|
+
const size =
|
|
146
|
+
(contentLength && Number.parseInt(contentLength, 10)) || undefined
|
|
147
|
+
const type =
|
|
148
|
+
response.headers.get('content-type') || 'application/octet-stream'
|
|
149
|
+
const disposition = response.headers.get('content-disposition')
|
|
150
|
+
let filename: string | undefined
|
|
151
|
+
if (disposition) {
|
|
152
|
+
const match = disposition.match(/filename="?([^"]+)"?/)
|
|
153
|
+
if (match) filename = match[1]
|
|
154
|
+
}
|
|
155
|
+
return {
|
|
156
|
+
type: 'blob' as const,
|
|
157
|
+
metadata: { type, size, filename },
|
|
158
|
+
source: response.body!,
|
|
159
|
+
}
|
|
160
|
+
} else {
|
|
161
|
+
return { type: 'rpc' as const, result: await response.bytes() }
|
|
162
|
+
}
|
|
163
|
+
} else {
|
|
164
|
+
try {
|
|
165
|
+
const buffer = await response.bytes()
|
|
166
|
+
const error = client.format.decode(buffer) as {
|
|
167
|
+
code?: string
|
|
168
|
+
message?: string
|
|
169
|
+
data?: unknown
|
|
170
|
+
}
|
|
171
|
+
throw new ProtocolError(
|
|
172
|
+
error.code || ErrorCode.ClientRequestError,
|
|
173
|
+
error.message || response.statusText,
|
|
174
|
+
error.data,
|
|
175
|
+
)
|
|
176
|
+
} catch (cause) {
|
|
177
|
+
if (cause instanceof ProtocolError) throw cause
|
|
178
|
+
// If decoding fails, throw generic error with status info
|
|
179
|
+
throw new ProtocolError(
|
|
180
|
+
ErrorCode.ClientRequestError,
|
|
181
|
+
`HTTP ${response.status}: ${response.statusText}`,
|
|
182
|
+
)
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export type HttpTransportFactory = ClientTransportFactory<
|
|
190
|
+
ConnectionType.Unidirectional,
|
|
191
|
+
HttpClientTransportOptions,
|
|
192
|
+
HttpTransportClient
|
|
193
|
+
>
|
|
194
|
+
|
|
195
|
+
export const HttpTransportFactory: HttpTransportFactory = (params, options) => {
|
|
196
|
+
return new HttpTransportClient(params.format, params.protocol, options)
|
|
197
|
+
}
|