@nmtjs/http-client 0.14.5 → 0.15.0-beta.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/LICENSE.md +1 -1
- package/README.md +1 -1
- package/dist/index.js +116 -74
- package/package.json +9 -7
- package/dist/index.d.ts +0 -22
package/LICENSE.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
Copyright (c)
|
|
1
|
+
Copyright (c) 2025 Denys Ilchyshyn
|
|
2
2
|
|
|
3
3
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
4
|
|
package/README.md
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,84 +1,126 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { createFuture } from '@nmtjs/common';
|
|
2
|
+
import { ConnectionType, ProtocolBlob } from '@nmtjs/protocol';
|
|
3
|
+
const createDecodeBase64 = (customFn) => {
|
|
4
|
+
return (string) => {
|
|
5
|
+
if ('fromBase64' in Uint8Array &&
|
|
6
|
+
typeof Uint8Array.fromBase64 === 'function') {
|
|
7
|
+
return Uint8Array.fromBase64(string);
|
|
8
|
+
}
|
|
9
|
+
else if (typeof atob === 'function') {
|
|
10
|
+
return Uint8Array.from(atob(string), (c) => c.charCodeAt(0));
|
|
11
|
+
}
|
|
12
|
+
else if (customFn) {
|
|
13
|
+
return customFn(string);
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
throw new Error('No base64 decoding function available');
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
const NEEMATA_BLOB_HEADER = 'X-Neemata-Blob';
|
|
21
|
+
export class HttpTransportClient {
|
|
22
|
+
format;
|
|
5
23
|
protocol;
|
|
6
24
|
options;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
constructor(protocol, options) {
|
|
10
|
-
|
|
25
|
+
type = ConnectionType.Unidirectional;
|
|
26
|
+
decodeBase64;
|
|
27
|
+
constructor(format, protocol, options) {
|
|
28
|
+
this.format = format;
|
|
11
29
|
this.protocol = protocol;
|
|
12
30
|
this.options = options;
|
|
31
|
+
this.options = { debug: false, ...options };
|
|
32
|
+
this.decodeBase64 = createDecodeBase64(options.decodeBase64);
|
|
13
33
|
}
|
|
14
|
-
|
|
15
|
-
const
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
34
|
+
url({ procedure, application, payload, }) {
|
|
35
|
+
const base = application ? `/${application}/${procedure}` : `/${procedure}`;
|
|
36
|
+
const url = new URL(base, this.options.url);
|
|
37
|
+
if (payload)
|
|
38
|
+
url.searchParams.set('payload', JSON.stringify(payload));
|
|
39
|
+
return url;
|
|
40
|
+
}
|
|
41
|
+
async call(client, rpc, options) {
|
|
42
|
+
const { procedure, payload } = rpc;
|
|
43
|
+
const requestHeaders = new Headers();
|
|
44
|
+
const url = this.url({ application: client.application, procedure });
|
|
45
|
+
if (client.auth)
|
|
46
|
+
requestHeaders.set('Authorization', client.auth);
|
|
47
|
+
let body;
|
|
48
|
+
if (payload instanceof ProtocolBlob) {
|
|
49
|
+
requestHeaders.set('Content-Type', payload.metadata.type);
|
|
50
|
+
requestHeaders.set(NEEMATA_BLOB_HEADER, 'true');
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
requestHeaders.set('Content-Type', client.format.contentType);
|
|
54
|
+
const buffer = client.format.encode(payload);
|
|
55
|
+
body = buffer;
|
|
56
|
+
}
|
|
57
|
+
if (options._stream_response) {
|
|
58
|
+
const _constructor = this.options.EventSource
|
|
59
|
+
? this.options.EventSource
|
|
60
|
+
: EventSource;
|
|
61
|
+
const source = new _constructor(url.toString(), { withCredentials: true });
|
|
62
|
+
const future = createFuture();
|
|
63
|
+
const { readable, writable } = new TransformStream();
|
|
64
|
+
const writer = writable.getWriter();
|
|
65
|
+
source.addEventListener('open', () => future.resolve({ type: 'rpc_stream', stream: readable }));
|
|
66
|
+
source.addEventListener('close', () => writable.close());
|
|
67
|
+
source.addEventListener('error', (event) => {
|
|
68
|
+
const error = new Error('Stream error', { cause: event });
|
|
69
|
+
future.reject(error);
|
|
70
|
+
writable.abort(error);
|
|
71
|
+
});
|
|
72
|
+
source.addEventListener('message', (event) => {
|
|
73
|
+
try {
|
|
74
|
+
const buffer = this.decodeBase64(event.data);
|
|
75
|
+
writer.write(buffer);
|
|
76
|
+
}
|
|
77
|
+
catch (cause) {
|
|
78
|
+
const error = new Error('Failed to decode stream message', { cause });
|
|
79
|
+
writable.abort(error);
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
return future.promise;
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
const response = await fetch(url.toString(), {
|
|
86
|
+
body,
|
|
87
|
+
method: 'POST',
|
|
88
|
+
headers: requestHeaders,
|
|
89
|
+
signal: options.signal,
|
|
90
|
+
credentials: 'include',
|
|
91
|
+
keepalive: true,
|
|
92
|
+
});
|
|
93
|
+
if (response.ok) {
|
|
94
|
+
const isBlob = !!response.headers.get(NEEMATA_BLOB_HEADER);
|
|
95
|
+
if (isBlob) {
|
|
96
|
+
const contentLength = response.headers.get('content-length');
|
|
97
|
+
const size = (contentLength && Number.parseInt(contentLength, 10)) || undefined;
|
|
98
|
+
const type = response.headers.get('content-type') || 'application/octet-stream';
|
|
99
|
+
const disposition = response.headers.get('content-disposition');
|
|
100
|
+
let filename;
|
|
101
|
+
if (disposition) {
|
|
102
|
+
const match = disposition.match(/filename="?([^"]+)"?/);
|
|
103
|
+
if (match)
|
|
104
|
+
filename = match[1];
|
|
105
|
+
}
|
|
106
|
+
return {
|
|
107
|
+
type: 'blob',
|
|
108
|
+
metadata: { type, size, filename },
|
|
109
|
+
source: body,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
return { type: 'rpc', result: await response.bytes() };
|
|
114
|
+
}
|
|
48
115
|
}
|
|
49
116
|
else {
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
const decoded = this.protocol.format.decode(buffer);
|
|
54
|
-
return transformer.decodeRPC(procedure, decoded);
|
|
55
|
-
}
|
|
56
|
-
else {
|
|
57
|
-
if (buffer.byteLength === 0) {
|
|
58
|
-
const error = new ClientError(ErrorCode.InternalServerError, `Empty response with ${response.status} status code`);
|
|
59
|
-
return Promise.reject(error);
|
|
60
|
-
}
|
|
61
|
-
else {
|
|
62
|
-
const payload = this.protocol.format.decode(buffer);
|
|
63
|
-
const error = new ClientError(payload.code, payload.message, payload.data);
|
|
64
|
-
return Promise.reject(error);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
});
|
|
117
|
+
const decoded = await response.text();
|
|
118
|
+
// throw new ProtocolError()
|
|
119
|
+
throw new Error();
|
|
68
120
|
}
|
|
69
|
-
}
|
|
70
|
-
.then(call.resolve)
|
|
71
|
-
.catch(call.reject);
|
|
72
|
-
return call;
|
|
73
|
-
}
|
|
74
|
-
async connect(auth) {
|
|
75
|
-
this.#auth = auth;
|
|
76
|
-
this.emit('connected');
|
|
77
|
-
}
|
|
78
|
-
async disconnect() {
|
|
79
|
-
this.emit('disconnected', 'client');
|
|
80
|
-
}
|
|
81
|
-
async send(_messageType, _buffer, _metadata) {
|
|
82
|
-
throw new Error('Not supported');
|
|
121
|
+
}
|
|
83
122
|
}
|
|
84
123
|
}
|
|
124
|
+
export const HttpTransportFactory = (params, options) => {
|
|
125
|
+
return new HttpTransportClient(params.format, params.protocol, options);
|
|
126
|
+
};
|
package/package.json
CHANGED
|
@@ -9,23 +9,25 @@
|
|
|
9
9
|
}
|
|
10
10
|
},
|
|
11
11
|
"dependencies": {
|
|
12
|
-
"@nmtjs/client": "0.
|
|
13
|
-
"@nmtjs/common": "0.
|
|
14
|
-
"@nmtjs/protocol": "0.
|
|
12
|
+
"@nmtjs/client": "0.15.0-beta.2",
|
|
13
|
+
"@nmtjs/common": "0.15.0-beta.2",
|
|
14
|
+
"@nmtjs/protocol": "0.15.0-beta.2"
|
|
15
15
|
},
|
|
16
16
|
"peerDependencies": {
|
|
17
|
-
"@nmtjs/
|
|
18
|
-
"@nmtjs/
|
|
19
|
-
"@nmtjs/
|
|
17
|
+
"@nmtjs/client": "0.15.0-beta.2",
|
|
18
|
+
"@nmtjs/common": "0.15.0-beta.2",
|
|
19
|
+
"@nmtjs/protocol": "0.15.0-beta.2"
|
|
20
20
|
},
|
|
21
21
|
"files": [
|
|
22
22
|
"dist",
|
|
23
23
|
"LICENSE.md",
|
|
24
24
|
"README.md"
|
|
25
25
|
],
|
|
26
|
-
"version": "0.
|
|
26
|
+
"version": "0.15.0-beta.2",
|
|
27
27
|
"scripts": {
|
|
28
|
+
"clean-build": "rm -rf ./dist",
|
|
28
29
|
"build": "tsc",
|
|
30
|
+
"dev": "tsc --watch",
|
|
29
31
|
"type-check": "tsc --noEmit"
|
|
30
32
|
}
|
|
31
33
|
}
|
package/dist/index.d.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import type { ClientMessageType } from '@nmtjs/protocol';
|
|
2
|
-
import type { BaseProtocol, ProtocolBaseClientCallOptions, ProtocolBaseTransformer, ProtocolClientCall, ProtocolSendMetadata } from '@nmtjs/protocol/client';
|
|
3
|
-
import { ProtocolTransport, ProtocolTransportStatus } from '@nmtjs/protocol/client';
|
|
4
|
-
export type HttpClientTransportOptions = {
|
|
5
|
-
/**
|
|
6
|
-
* The origin of the server
|
|
7
|
-
* @example 'http://localhost:3000'
|
|
8
|
-
*/
|
|
9
|
-
origin: string;
|
|
10
|
-
debug?: boolean;
|
|
11
|
-
};
|
|
12
|
-
export declare class HttpClientTransport extends ProtocolTransport<HttpClientTransportOptions> {
|
|
13
|
-
#private;
|
|
14
|
-
protected readonly protocol: BaseProtocol;
|
|
15
|
-
protected readonly options: HttpClientTransportOptions;
|
|
16
|
-
status: ProtocolTransportStatus;
|
|
17
|
-
constructor(protocol: BaseProtocol, options: HttpClientTransportOptions);
|
|
18
|
-
call(procedure: string, payload: any, options: ProtocolBaseClientCallOptions, transformer: ProtocolBaseTransformer): Promise<ProtocolClientCall>;
|
|
19
|
-
connect(auth: any): Promise<void>;
|
|
20
|
-
disconnect(): Promise<void>;
|
|
21
|
-
send(_messageType: ClientMessageType, _buffer: ArrayBuffer, _metadata: ProtocolSendMetadata): Promise<void>;
|
|
22
|
-
}
|