@zintrust/redis-rpc 2.4.6 → 2.4.7
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/client.js +26 -35
- package/package.json +1 -1
package/dist/client.js
CHANGED
|
@@ -1,39 +1,30 @@
|
|
|
1
1
|
import { ErrorFactory, isUndefinedOrNull } from '@zintrust/core/runtime';
|
|
2
|
-
import { randomUUID } from 'node:crypto';
|
|
3
|
-
import http from 'node:http';
|
|
4
|
-
import https from 'node:https';
|
|
5
2
|
import { rpcClientHeaders, rpcServerOptions } from './env.js';
|
|
6
|
-
const requestJson = (url, body, headers) => {
|
|
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
|
-
request.on('error', (error) => reject(ErrorFactory.createConnectionError('Redis RPC request failed', { error })));
|
|
35
|
-
request.end(body);
|
|
36
|
-
});
|
|
3
|
+
const requestJson = async (url, body, headers) => {
|
|
4
|
+
let response;
|
|
5
|
+
try {
|
|
6
|
+
response = await fetch(url, { method: 'POST', headers, body });
|
|
7
|
+
}
|
|
8
|
+
catch (error) {
|
|
9
|
+
throw ErrorFactory.createConnectionError('Redis RPC request failed', { error });
|
|
10
|
+
}
|
|
11
|
+
let text;
|
|
12
|
+
try {
|
|
13
|
+
text = await response.text();
|
|
14
|
+
}
|
|
15
|
+
catch (error) {
|
|
16
|
+
throw ErrorFactory.createTryCatchError('Redis RPC response read failed', { error });
|
|
17
|
+
}
|
|
18
|
+
try {
|
|
19
|
+
return {
|
|
20
|
+
statusCode: response.status,
|
|
21
|
+
ok: response.ok,
|
|
22
|
+
body: isUndefinedOrNull(text.trim()) ? {} : JSON.parse(text),
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
throw ErrorFactory.createTryCatchError('Redis RPC response parse failed', { error });
|
|
27
|
+
}
|
|
37
28
|
};
|
|
38
29
|
const createServiceProxy = (client, service, target) => {
|
|
39
30
|
return new Proxy(Object.create(null), {
|
|
@@ -54,7 +45,7 @@ export const createRedisRpcClient = (options = {}) => {
|
|
|
54
45
|
call: async (service, method, payload = {}) => {
|
|
55
46
|
const url = new URL('/rpc', baseUrl);
|
|
56
47
|
const body = JSON.stringify({
|
|
57
|
-
requestId: randomUUID(),
|
|
48
|
+
requestId: globalThis.crypto.randomUUID(),
|
|
58
49
|
service,
|
|
59
50
|
method,
|
|
60
51
|
payload,
|