@milaboratories/pl-client 2.16.26 → 2.16.28
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 +2 -1
- package/dist/core/PromiseTracker.cjs +1 -3
- package/dist/core/PromiseTracker.cjs.map +1 -1
- package/dist/core/PromiseTracker.d.ts.map +1 -1
- package/dist/core/PromiseTracker.js +1 -3
- package/dist/core/PromiseTracker.js.map +1 -1
- package/dist/core/StatefulPromise.cjs +4 -4
- package/dist/core/StatefulPromise.cjs.map +1 -1
- package/dist/core/StatefulPromise.d.ts +1 -1
- package/dist/core/StatefulPromise.d.ts.map +1 -1
- package/dist/core/StatefulPromise.js +4 -4
- package/dist/core/StatefulPromise.js.map +1 -1
- package/dist/core/advisory_locks.cjs +1 -1
- package/dist/core/advisory_locks.cjs.map +1 -1
- package/dist/core/advisory_locks.js +1 -1
- package/dist/core/advisory_locks.js.map +1 -1
- package/dist/core/auth.cjs.map +1 -1
- package/dist/core/auth.d.ts +1 -1
- package/dist/core/auth.js.map +1 -1
- package/dist/core/cache.d.ts +1 -1
- package/dist/core/client.cjs +14 -14
- package/dist/core/client.cjs.map +1 -1
- package/dist/core/client.d.ts +11 -11
- package/dist/core/client.d.ts.map +1 -1
- package/dist/core/client.js +14 -14
- package/dist/core/client.js.map +1 -1
- package/dist/core/config.cjs +39 -39
- package/dist/core/config.cjs.map +1 -1
- package/dist/core/config.d.ts +5 -5
- package/dist/core/config.js +39 -39
- package/dist/core/config.js.map +1 -1
- package/dist/core/default_client.cjs +23 -23
- package/dist/core/default_client.cjs.map +1 -1
- package/dist/core/default_client.d.ts +3 -3
- package/dist/core/default_client.js +23 -23
- package/dist/core/default_client.js.map +1 -1
- package/dist/core/driver.cjs +1 -1
- package/dist/core/driver.cjs.map +1 -1
- package/dist/core/driver.d.ts +5 -5
- package/dist/core/driver.js +1 -1
- package/dist/core/driver.js.map +1 -1
- package/dist/core/error_resource.cjs +2 -2
- package/dist/core/error_resource.cjs.map +1 -1
- package/dist/core/error_resource.d.ts +1 -1
- package/dist/core/error_resource.js +2 -2
- package/dist/core/error_resource.js.map +1 -1
- package/dist/core/errors.cjs +24 -24
- package/dist/core/errors.cjs.map +1 -1
- package/dist/core/errors.d.ts +1 -1
- package/dist/core/errors.d.ts.map +1 -1
- package/dist/core/errors.js +24 -24
- package/dist/core/errors.js.map +1 -1
- package/dist/core/final.cjs +43 -43
- package/dist/core/final.cjs.map +1 -1
- package/dist/core/final.d.ts +3 -3
- package/dist/core/final.d.ts.map +1 -1
- package/dist/core/final.js +43 -43
- package/dist/core/final.js.map +1 -1
- package/dist/core/ll_client.cjs +50 -43
- package/dist/core/ll_client.cjs.map +1 -1
- package/dist/core/ll_client.d.ts +9 -9
- package/dist/core/ll_client.d.ts.map +1 -1
- package/dist/core/ll_client.js +50 -43
- package/dist/core/ll_client.js.map +1 -1
- package/dist/core/ll_transaction.cjs +9 -9
- package/dist/core/ll_transaction.cjs.map +1 -1
- package/dist/core/ll_transaction.d.ts +7 -7
- package/dist/core/ll_transaction.d.ts.map +1 -1
- package/dist/core/ll_transaction.js +9 -9
- package/dist/core/ll_transaction.js.map +1 -1
- package/dist/core/stat.cjs.map +1 -1
- package/dist/core/stat.d.ts +1 -1
- package/dist/core/stat.js.map +1 -1
- package/dist/core/transaction.cjs +46 -46
- package/dist/core/transaction.cjs.map +1 -1
- package/dist/core/transaction.d.ts +7 -7
- package/dist/core/transaction.d.ts.map +1 -1
- package/dist/core/transaction.js +46 -46
- package/dist/core/transaction.js.map +1 -1
- package/dist/core/type_conversion.cjs +22 -22
- package/dist/core/type_conversion.cjs.map +1 -1
- package/dist/core/type_conversion.d.ts +3 -3
- package/dist/core/type_conversion.d.ts.map +1 -1
- package/dist/core/type_conversion.js +22 -22
- package/dist/core/type_conversion.js.map +1 -1
- package/dist/core/types.cjs +25 -25
- package/dist/core/types.cjs.map +1 -1
- package/dist/core/types.d.ts +7 -7
- package/dist/core/types.js +25 -25
- package/dist/core/types.js.map +1 -1
- package/dist/core/unauth_client.cjs +6 -4
- package/dist/core/unauth_client.cjs.map +1 -1
- package/dist/core/unauth_client.d.ts +4 -4
- package/dist/core/unauth_client.d.ts.map +1 -1
- package/dist/core/unauth_client.js +6 -4
- package/dist/core/unauth_client.js.map +1 -1
- package/dist/core/websocket_stream.cjs +22 -20
- package/dist/core/websocket_stream.cjs.map +1 -1
- package/dist/core/websocket_stream.d.ts +3 -3
- package/dist/core/websocket_stream.d.ts.map +1 -1
- package/dist/core/websocket_stream.js +22 -20
- package/dist/core/websocket_stream.js.map +1 -1
- package/dist/core/wire.d.ts +6 -6
- package/dist/core/wire.d.ts.map +1 -1
- package/dist/helpers/pl.cjs +19 -19
- package/dist/helpers/pl.cjs.map +1 -1
- package/dist/helpers/pl.d.ts +2 -2
- package/dist/helpers/pl.js +19 -19
- package/dist/helpers/pl.js.map +1 -1
- package/dist/helpers/poll.cjs +6 -6
- package/dist/helpers/poll.cjs.map +1 -1
- package/dist/helpers/poll.d.ts +4 -4
- package/dist/helpers/poll.d.ts.map +1 -1
- package/dist/helpers/poll.js +6 -6
- package/dist/helpers/poll.js.map +1 -1
- package/dist/helpers/retry_strategy.cjs +1 -1
- package/dist/helpers/retry_strategy.cjs.map +1 -1
- package/dist/helpers/retry_strategy.d.ts.map +1 -1
- package/dist/helpers/retry_strategy.js +1 -1
- package/dist/helpers/retry_strategy.js.map +1 -1
- package/dist/helpers/state_helpers.d.ts +2 -2
- package/dist/helpers/tx_helpers.cjs +2 -2
- package/dist/helpers/tx_helpers.cjs.map +1 -1
- package/dist/helpers/tx_helpers.d.ts +2 -2
- package/dist/helpers/tx_helpers.d.ts.map +1 -1
- package/dist/helpers/tx_helpers.js +2 -2
- package/dist/helpers/tx_helpers.js.map +1 -1
- package/dist/index.d.ts +16 -16
- package/dist/proto-grpc/google/protobuf/struct.d.ts +1 -1
- package/dist/proto-grpc/google/protobuf/struct.d.ts.map +1 -1
- package/dist/proto-rest/index.cjs +4 -5
- package/dist/proto-rest/index.cjs.map +1 -1
- package/dist/proto-rest/index.d.ts +4 -4
- package/dist/proto-rest/index.d.ts.map +1 -1
- package/dist/proto-rest/index.js +4 -5
- package/dist/proto-rest/index.js.map +1 -1
- package/dist/proto-rest/plapi.d.ts.map +1 -1
- package/dist/test/tcp-proxy.cjs +11 -10
- package/dist/test/tcp-proxy.cjs.map +1 -1
- package/dist/test/tcp-proxy.d.ts +1 -1
- package/dist/test/tcp-proxy.d.ts.map +1 -1
- package/dist/test/tcp-proxy.js +11 -10
- package/dist/test/tcp-proxy.js.map +1 -1
- package/dist/test/test_config.cjs +21 -17
- package/dist/test/test_config.cjs.map +1 -1
- package/dist/test/test_config.d.ts +6 -6
- package/dist/test/test_config.d.ts.map +1 -1
- package/dist/test/test_config.js +21 -17
- package/dist/test/test_config.js.map +1 -1
- package/dist/util/pl.cjs +1 -1
- package/dist/util/pl.cjs.map +1 -1
- package/dist/util/pl.js +1 -1
- package/dist/util/pl.js.map +1 -1
- package/dist/util/util.cjs +1 -1
- package/dist/util/util.cjs.map +1 -1
- package/dist/util/util.js +1 -1
- package/dist/util/util.js.map +1 -1
- package/package.json +23 -23
- package/src/core/PromiseTracker.ts +3 -4
- package/src/core/StatefulPromise.ts +17 -8
- package/src/core/abstract_stream.ts +3 -4
- package/src/core/advisory_locks.ts +1 -1
- package/src/core/auth.ts +2 -2
- package/src/core/cache.ts +1 -1
- package/src/core/client.test.ts +25 -21
- package/src/core/client.ts +54 -45
- package/src/core/config.test.ts +44 -44
- package/src/core/config.ts +49 -49
- package/src/core/connectivity.test.ts +69 -63
- package/src/core/default_client.ts +46 -46
- package/src/core/driver.ts +6 -6
- package/src/core/error.test.ts +5 -5
- package/src/core/error_resource.ts +3 -3
- package/src/core/errors.ts +39 -31
- package/src/core/final.ts +48 -55
- package/src/core/ll_client.test.ts +53 -36
- package/src/core/ll_client.ts +125 -81
- package/src/core/ll_transaction.test.ts +75 -49
- package/src/core/ll_transaction.ts +37 -35
- package/src/core/stat.ts +1 -1
- package/src/core/transaction.test.ts +65 -65
- package/src/core/transaction.ts +91 -84
- package/src/core/type_conversion.ts +30 -31
- package/src/core/types.test.ts +6 -6
- package/src/core/types.ts +35 -35
- package/src/core/unauth_client.test.ts +18 -14
- package/src/core/unauth_client.ts +14 -12
- package/src/core/websocket_stream.test.ts +52 -52
- package/src/core/websocket_stream.ts +41 -37
- package/src/core/wire.ts +10 -8
- package/src/helpers/pl.ts +22 -22
- package/src/helpers/poll.ts +13 -27
- package/src/helpers/retry_strategy.ts +2 -4
- package/src/helpers/rich_resource_types.test.ts +2 -2
- package/src/helpers/state_helpers.ts +3 -3
- package/src/helpers/tx_helpers.ts +9 -7
- package/src/index.ts +16 -16
- package/src/proto-grpc/google/protobuf/struct.ts +1 -1
- package/src/proto-rest/index.ts +17 -18
- package/src/proto-rest/plapi.ts +1472 -1472
- package/src/test/tcp-proxy.ts +55 -54
- package/src/test/test_config.test.ts +3 -3
- package/src/test/test_config.ts +51 -46
- package/src/util/pl.ts +1 -1
- package/src/util/util.test.ts +5 -5
- package/src/util/util.ts +1 -1
- package/dist/helpers/rich_resource_types.d.ts +0 -2
- package/dist/helpers/rich_resource_types.d.ts.map +0 -1
- package/dist/helpers/smart_accessors.d.ts +0 -2
- package/dist/helpers/smart_accessors.d.ts.map +0 -1
- package/src/helpers/rich_resource_types.ts +0 -84
- package/src/helpers/smart_accessors.ts +0 -146
package/src/test/tcp-proxy.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import * as net from
|
|
2
|
-
import type { AddressInfo } from
|
|
3
|
-
import { Transform } from
|
|
4
|
-
import type { TransformCallback } from
|
|
5
|
-
import { pipeline } from
|
|
6
|
-
import * as timers from
|
|
1
|
+
import * as net from "node:net";
|
|
2
|
+
import type { AddressInfo } from "node:net";
|
|
3
|
+
import { Transform } from "node:stream";
|
|
4
|
+
import type { TransformCallback } from "node:stream";
|
|
5
|
+
import { pipeline } from "node:stream/promises";
|
|
6
|
+
import * as timers from "node:timers/promises";
|
|
7
7
|
|
|
8
8
|
export type TcpProxyOptions = {
|
|
9
9
|
port?: number;
|
|
@@ -39,79 +39,80 @@ export async function startTcpProxy(options: TcpProxyOptions) {
|
|
|
39
39
|
};
|
|
40
40
|
if (delayMs !== undefined) await timers.setTimeout(delayMs);
|
|
41
41
|
kill();
|
|
42
|
-
}
|
|
42
|
+
}
|
|
43
43
|
|
|
44
|
-
const server = net
|
|
45
|
-
.
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
44
|
+
const server = net.createServer((socket: net.Socket) => {
|
|
45
|
+
const client = net.createConnection({ port: targetPort }, () => {
|
|
46
|
+
if (options.verbose) console.log(`connected to ${targetPort}`);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
const pair = { socket, client };
|
|
50
|
+
connections.add(pair);
|
|
51
|
+
const onClose = () => connections.delete(pair);
|
|
52
|
+
socket.on("close", onClose);
|
|
53
|
+
client.on("close", onClose);
|
|
49
54
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
super();
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
_transform(chunk: Buffer, _enc: BufferEncoding, callback: TransformCallback) {
|
|
63
|
-
// Backpressure is respected by delaying the callback until after push
|
|
64
|
-
this.pendingTimer = setTimeout(() => {
|
|
65
|
-
this.pendingTimer = undefined;
|
|
66
|
-
this.push(chunk);
|
|
67
|
-
callback();
|
|
68
|
-
}, state.latency);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
_destroy(err: Error | null, cb: (error?: Error | null) => void) {
|
|
72
|
-
if (this.pendingTimer) clearTimeout(this.pendingTimer);
|
|
55
|
+
class LatencyTransform extends Transform {
|
|
56
|
+
private pendingTimer?: NodeJS.Timeout;
|
|
57
|
+
constructor() {
|
|
58
|
+
super();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
_transform(chunk: Buffer, _enc: BufferEncoding, callback: TransformCallback) {
|
|
62
|
+
// Backpressure is respected by delaying the callback until after push
|
|
63
|
+
this.pendingTimer = setTimeout(() => {
|
|
73
64
|
this.pendingTimer = undefined;
|
|
74
|
-
|
|
75
|
-
|
|
65
|
+
this.push(chunk);
|
|
66
|
+
callback();
|
|
67
|
+
}, state.latency);
|
|
76
68
|
}
|
|
77
69
|
|
|
78
|
-
|
|
79
|
-
|
|
70
|
+
_destroy(err: Error | null, cb: (error?: Error | null) => void) {
|
|
71
|
+
if (this.pendingTimer) clearTimeout(this.pendingTimer);
|
|
72
|
+
this.pendingTimer = undefined;
|
|
73
|
+
cb(err);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
80
76
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
socket.destroy(err);
|
|
84
|
-
client.destroy(err);
|
|
85
|
-
});
|
|
77
|
+
const toClientLatency = new LatencyTransform();
|
|
78
|
+
const toTargetLatency = new LatencyTransform();
|
|
86
79
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
80
|
+
// Bidirectional pipelines with latency and error propagation
|
|
81
|
+
pipeline(socket, toTargetLatency, client).catch((err) => {
|
|
82
|
+
socket.destroy(err);
|
|
83
|
+
client.destroy(err);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
pipeline(client, toClientLatency, socket).catch((err) => {
|
|
87
|
+
socket.destroy(err);
|
|
88
|
+
client.destroy(err);
|
|
91
89
|
});
|
|
90
|
+
});
|
|
92
91
|
|
|
93
92
|
server.listen({ port: port ?? 0 }, () => {
|
|
94
|
-
console.log(
|
|
93
|
+
console.log("Test TCP proxy server started on", server.address());
|
|
95
94
|
});
|
|
96
95
|
|
|
97
96
|
// Wait for proxy to be ready
|
|
98
97
|
await new Promise<void>((resolve, reject) => {
|
|
99
98
|
if (server.listening) return resolve();
|
|
100
99
|
const onError = (err: Error) => {
|
|
101
|
-
server.off(
|
|
100
|
+
server.off("listening", onListening);
|
|
102
101
|
reject(err);
|
|
103
102
|
};
|
|
104
103
|
const onListening = () => {
|
|
105
|
-
server.off(
|
|
104
|
+
server.off("error", onError);
|
|
106
105
|
resolve();
|
|
107
106
|
};
|
|
108
|
-
server.once(
|
|
109
|
-
server.once(
|
|
107
|
+
server.once("error", onError);
|
|
108
|
+
server.once("listening", onListening);
|
|
110
109
|
});
|
|
111
110
|
|
|
112
111
|
return {
|
|
113
112
|
server,
|
|
114
|
-
get port() {
|
|
113
|
+
get port() {
|
|
114
|
+
return (server.address() as AddressInfo)?.port;
|
|
115
|
+
},
|
|
115
116
|
setLatency,
|
|
116
117
|
getLatency,
|
|
117
118
|
disconnectAll,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { getTestClientConf } from
|
|
2
|
-
import { test, expect } from
|
|
1
|
+
import { getTestClientConf } from "./test_config";
|
|
2
|
+
import { test, expect } from "vitest";
|
|
3
3
|
|
|
4
|
-
test(
|
|
4
|
+
test("test that test config have no alternative root set", async () => {
|
|
5
5
|
const { conf } = await getTestClientConf();
|
|
6
6
|
expect(conf.alternativeRoot).toBeUndefined();
|
|
7
7
|
});
|
package/src/test/test_config.ts
CHANGED
|
@@ -1,20 +1,18 @@
|
|
|
1
|
-
import * as fs from
|
|
2
|
-
import { LLPlClient } from
|
|
3
|
-
import type { AuthInformation, AuthOps, PlClientConfig } from
|
|
4
|
-
import { plAddressToConfig } from
|
|
5
|
-
import { UnauthenticatedPlClient } from
|
|
6
|
-
import { PlClient } from
|
|
7
|
-
import { randomUUID } from
|
|
8
|
-
import type { OptionalResourceId } from
|
|
9
|
-
import { NullResourceId, resourceIdToString } from
|
|
10
|
-
import { inferAuthRefreshTime } from
|
|
11
|
-
import * as path from
|
|
12
|
-
import type { TestTcpProxy } from
|
|
13
|
-
import { startTcpProxy } from
|
|
14
|
-
|
|
15
|
-
export {
|
|
16
|
-
TestTcpProxy,
|
|
17
|
-
};
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import { LLPlClient } from "../core/ll_client";
|
|
3
|
+
import type { AuthInformation, AuthOps, PlClientConfig } from "../core/config";
|
|
4
|
+
import { plAddressToConfig } from "../core/config";
|
|
5
|
+
import { UnauthenticatedPlClient } from "../core/unauth_client";
|
|
6
|
+
import { PlClient } from "../core/client";
|
|
7
|
+
import { randomUUID } from "node:crypto";
|
|
8
|
+
import type { OptionalResourceId } from "../core/types";
|
|
9
|
+
import { NullResourceId, resourceIdToString } from "../core/types";
|
|
10
|
+
import { inferAuthRefreshTime } from "../core/auth";
|
|
11
|
+
import * as path from "node:path";
|
|
12
|
+
import type { TestTcpProxy } from "./tcp-proxy";
|
|
13
|
+
import { startTcpProxy } from "./tcp-proxy";
|
|
14
|
+
|
|
15
|
+
export { TestTcpProxy };
|
|
18
16
|
|
|
19
17
|
export interface TestConfig {
|
|
20
18
|
address: string;
|
|
@@ -23,20 +21,20 @@ export interface TestConfig {
|
|
|
23
21
|
test_password?: string;
|
|
24
22
|
}
|
|
25
23
|
|
|
26
|
-
const CONFIG_FILE =
|
|
24
|
+
const CONFIG_FILE = "test_config.json";
|
|
27
25
|
// const AUTH_DATA_FILE = '.test_auth.json';
|
|
28
26
|
|
|
29
27
|
let authDataFilePath: string | undefined;
|
|
30
28
|
|
|
31
29
|
function getFullAuthDataFilePath() {
|
|
32
|
-
if (authDataFilePath === undefined) authDataFilePath = path.resolve(
|
|
30
|
+
if (authDataFilePath === undefined) authDataFilePath = path.resolve(".test_auth.json");
|
|
33
31
|
return authDataFilePath;
|
|
34
32
|
}
|
|
35
33
|
|
|
36
34
|
export function getTestConfig(): TestConfig {
|
|
37
35
|
let conf: Partial<TestConfig> = {};
|
|
38
36
|
if (fs.existsSync(CONFIG_FILE))
|
|
39
|
-
conf = JSON.parse(fs.readFileSync(CONFIG_FILE, { encoding:
|
|
37
|
+
conf = JSON.parse(fs.readFileSync(CONFIG_FILE, { encoding: "utf-8" })) as TestConfig;
|
|
40
38
|
|
|
41
39
|
if (process.env.PL_ADDRESS !== undefined) conf.address = process.env.PL_ADDRESS;
|
|
42
40
|
|
|
@@ -84,7 +82,7 @@ function saveAuthInfoCallback(tConf: TestConfig): (authInformation: AuthInformat
|
|
|
84
82
|
expiration: inferAuthRefreshTime(authInformation, 24 * 60 * 60),
|
|
85
83
|
} as AuthCache),
|
|
86
84
|
),
|
|
87
|
-
|
|
85
|
+
"utf8",
|
|
88
86
|
);
|
|
89
87
|
fs.renameSync(tmpDst, dst);
|
|
90
88
|
};
|
|
@@ -104,16 +102,16 @@ export async function getTestClientConf(): Promise<{ conf: PlClientConfig; auth:
|
|
|
104
102
|
if (fs.existsSync(getFullAuthDataFilePath())) {
|
|
105
103
|
try {
|
|
106
104
|
const cache: AuthCache = JSON.parse(
|
|
107
|
-
fs.readFileSync(getFullAuthDataFilePath(), { encoding:
|
|
105
|
+
fs.readFileSync(getFullAuthDataFilePath(), { encoding: "utf-8" }),
|
|
108
106
|
) as AuthCache; // TODO runtime validation
|
|
109
107
|
if (
|
|
110
|
-
cache.conf.address === tConf.address
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
108
|
+
cache.conf.address === tConf.address &&
|
|
109
|
+
cache.conf.test_user === tConf.test_user &&
|
|
110
|
+
cache.conf.test_password === tConf.test_password &&
|
|
111
|
+
cache.expiration > Date.now()
|
|
114
112
|
)
|
|
115
113
|
authInformation = cache.authInformation;
|
|
116
|
-
} catch
|
|
114
|
+
} catch {
|
|
117
115
|
// removing cache file on any error
|
|
118
116
|
fs.rmSync(getFullAuthDataFilePath());
|
|
119
117
|
}
|
|
@@ -165,23 +163,23 @@ export async function getTestClient(
|
|
|
165
163
|
) {
|
|
166
164
|
const { conf, auth } = await getTestClientConf();
|
|
167
165
|
if (alternativeRoot !== undefined && conf.alternativeRoot !== undefined)
|
|
168
|
-
throw new Error(
|
|
166
|
+
throw new Error("test pl address configured with alternative root");
|
|
169
167
|
return await PlClient.init({ ...conf, ...confOverrides, alternativeRoot }, auth);
|
|
170
168
|
}
|
|
171
169
|
|
|
172
|
-
export type WithTempRootOptions =
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
170
|
+
export type WithTempRootOptions =
|
|
171
|
+
| {
|
|
172
|
+
/** If true and PL_ADDRESS is http://localhost or http://127.0.0.1:<port>,
|
|
173
|
+
* a TCP proxy will be started and PL client will connect through it. */
|
|
174
|
+
viaTcpProxy: true;
|
|
175
|
+
/** Artificial latency for proxy (ms). Default 0 */
|
|
176
|
+
proxyLatencyMs?: number;
|
|
177
|
+
}
|
|
178
|
+
| {
|
|
179
|
+
viaTcpProxy?: undefined;
|
|
180
|
+
};
|
|
181
181
|
|
|
182
|
-
export async function withTempRoot<T>(
|
|
183
|
-
body: (pl: PlClient) => Promise<T>
|
|
184
|
-
): Promise<T | void>;
|
|
182
|
+
export async function withTempRoot<T>(body: (pl: PlClient) => Promise<T>): Promise<T | void>;
|
|
185
183
|
|
|
186
184
|
export async function withTempRoot<T>(
|
|
187
185
|
body: (pl: PlClient, proxy: Awaited<ReturnType<typeof startTcpProxy>>) => Promise<T>,
|
|
@@ -205,18 +203,21 @@ export async function withTempRoot<T>(
|
|
|
205
203
|
if (options.viaTcpProxy === true && process.env.PL_ADDRESS) {
|
|
206
204
|
try {
|
|
207
205
|
const url = new URL(process.env.PL_ADDRESS);
|
|
208
|
-
const isHttp = url.protocol ===
|
|
209
|
-
const isLocal = url.hostname ===
|
|
206
|
+
const isHttp = url.protocol === "http:";
|
|
207
|
+
const isLocal = url.hostname === "127.0.0.1" || url.hostname === "localhost";
|
|
210
208
|
const port = parseInt(url.port);
|
|
211
209
|
if (isHttp && isLocal && Number.isFinite(port)) {
|
|
212
210
|
proxy = await startTcpProxy({ targetPort: port, latency: options.proxyLatencyMs ?? 0 });
|
|
213
211
|
// Override client connection host:port to proxy
|
|
214
212
|
confOverrides = { hostAndPort: `127.0.0.1:${proxy.port}` } as Partial<PlClientConfig>;
|
|
215
213
|
} else {
|
|
216
|
-
console.warn(
|
|
214
|
+
console.warn(
|
|
215
|
+
"*** skipping proxy-based test, PL_ADDRESS is not localhost",
|
|
216
|
+
process.env.PL_ADDRESS,
|
|
217
|
+
);
|
|
217
218
|
return;
|
|
218
219
|
}
|
|
219
|
-
} catch
|
|
220
|
+
} catch {
|
|
220
221
|
// ignore proxy setup errors; tests will run against original address
|
|
221
222
|
}
|
|
222
223
|
}
|
|
@@ -250,10 +251,14 @@ export async function withTempRoot<T>(
|
|
|
250
251
|
if (proxy) {
|
|
251
252
|
try {
|
|
252
253
|
await proxy.disconnectAll();
|
|
253
|
-
} catch
|
|
254
|
+
} catch {
|
|
255
|
+
/* ignore */
|
|
256
|
+
}
|
|
254
257
|
try {
|
|
255
258
|
await new Promise<void>((resolve) => proxy!.server.close(() => resolve()));
|
|
256
|
-
} catch
|
|
259
|
+
} catch {
|
|
260
|
+
/* ignore */
|
|
261
|
+
}
|
|
257
262
|
}
|
|
258
263
|
}
|
|
259
264
|
}
|
package/src/util/pl.ts
CHANGED
package/src/util/util.test.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { toBytes } from
|
|
2
|
-
import { test, expect } from
|
|
1
|
+
import { toBytes } from "./util";
|
|
2
|
+
import { test, expect } from "vitest";
|
|
3
3
|
|
|
4
|
-
test(
|
|
4
|
+
test("test toBytes 1", () => {
|
|
5
5
|
const arr = new Uint8Array([1, 2, 3]);
|
|
6
6
|
expect(toBytes(arr)).toEqual(arr);
|
|
7
7
|
});
|
|
8
8
|
|
|
9
|
-
test(
|
|
10
|
-
expect(toBytes(
|
|
9
|
+
test("test toBytes 2", () => {
|
|
10
|
+
expect(toBytes("\x01\x02\x03")).toEqual(Buffer.from(new Uint8Array([1, 2, 3])));
|
|
11
11
|
});
|
package/src/util/util.ts
CHANGED
|
@@ -3,7 +3,7 @@ function isArrayBufferOrView(value: unknown): value is ArrayBufferLike {
|
|
|
3
3
|
}
|
|
4
4
|
|
|
5
5
|
export function toBytes(value: string | Uint8Array): Uint8Array {
|
|
6
|
-
if (typeof value ===
|
|
6
|
+
if (typeof value === "string") return Buffer.from(value);
|
|
7
7
|
else if (isArrayBufferOrView(value)) return value;
|
|
8
8
|
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
9
9
|
else throw new Error(`Unexpected type: ${value}`);
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"rich_resource_types.d.ts","sourceRoot":"","sources":["../../src/helpers/rich_resource_types.ts"],"names":[],"mappings":""}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"smart_accessors.d.ts","sourceRoot":"","sources":["../../src/helpers/smart_accessors.ts"],"names":[],"mappings":""}
|
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
// import { ResourceType } from './types';
|
|
2
|
-
//
|
|
3
|
-
// //
|
|
4
|
-
// // NOTE: This code is not used.
|
|
5
|
-
// // It shows a raw idea on how to implement typed Tx API
|
|
6
|
-
// //
|
|
7
|
-
//
|
|
8
|
-
// export type AnyStructuralResourceKind = 'structural' | 'ephemeral' | 'singleton'
|
|
9
|
-
// export type ResourceKind = AnyStructuralResourceKind | 'value';
|
|
10
|
-
//
|
|
11
|
-
// declare const __resource_type: unique symbol;
|
|
12
|
-
// export type WithRichType<T, B extends ResourceType> = T & { [__resource_type]: B }
|
|
13
|
-
//
|
|
14
|
-
// export type Serde<V> = {
|
|
15
|
-
// ser: (obj: V) => Uint8Array
|
|
16
|
-
// des: (data: Uint8Array) => V
|
|
17
|
-
// }
|
|
18
|
-
//
|
|
19
|
-
// export type RichResourceType<Kind extends ResourceKind,
|
|
20
|
-
// Name extends string, Version extends string, Value = never> =
|
|
21
|
-
// {
|
|
22
|
-
// kind: Kind,
|
|
23
|
-
// name: Name,
|
|
24
|
-
// version: Version,
|
|
25
|
-
// serde: Serde<Value>
|
|
26
|
-
// }
|
|
27
|
-
//
|
|
28
|
-
// export type AnyStructuralRichResourceType = RichResourceType<AnyStructuralResourceKind, string, string, unknown>
|
|
29
|
-
//
|
|
30
|
-
// const DummySerde: Serde<never> = {
|
|
31
|
-
// ser: (t) => {
|
|
32
|
-
// throw new Error();
|
|
33
|
-
// },
|
|
34
|
-
// des: (t) => {
|
|
35
|
-
// throw new Error();
|
|
36
|
-
// }
|
|
37
|
-
// };
|
|
38
|
-
//
|
|
39
|
-
// const NoSerde = { serde: DummySerde };
|
|
40
|
-
//
|
|
41
|
-
// export function richStructuralType<
|
|
42
|
-
// Name extends string,
|
|
43
|
-
// Version extends string,
|
|
44
|
-
// Kind extends AnyStructuralResourceKind>(
|
|
45
|
-
// kind: Kind,
|
|
46
|
-
// name: Name,
|
|
47
|
-
// version: Version
|
|
48
|
-
// ): RichResourceType<Kind, Name, Version> {
|
|
49
|
-
// return { kind, name, version, ...NoSerde };
|
|
50
|
-
// }
|
|
51
|
-
//
|
|
52
|
-
// export function richValueType<
|
|
53
|
-
// Name extends string,
|
|
54
|
-
// Version extends string,
|
|
55
|
-
// Value>(
|
|
56
|
-
// name: Name,
|
|
57
|
-
// version: Version,
|
|
58
|
-
// serde: Serde<Value>
|
|
59
|
-
// ): RichResourceType<'value', Name, Version, Value> {
|
|
60
|
-
// return { kind: 'value', name, version, serde };
|
|
61
|
-
// }
|
|
62
|
-
//
|
|
63
|
-
// export type RichFieldType<
|
|
64
|
-
// Parent extends AnyStructuralRichResourceType,
|
|
65
|
-
// Name extends string,
|
|
66
|
-
// Ref extends AnyStructuralRichResourceType> =
|
|
67
|
-
// {
|
|
68
|
-
// parent: Parent,
|
|
69
|
-
// name: Name,
|
|
70
|
-
// referenced: Ref
|
|
71
|
-
// }
|
|
72
|
-
//
|
|
73
|
-
// export function richFieldType<
|
|
74
|
-
// Parent extends AnyStructuralRichResourceType,
|
|
75
|
-
// Name extends string,
|
|
76
|
-
// Ref extends AnyStructuralRichResourceType>(
|
|
77
|
-
// parent: Parent,
|
|
78
|
-
// name: Name,
|
|
79
|
-
// referenced: Ref
|
|
80
|
-
// ): RichFieldType<Parent, Name, Ref> {
|
|
81
|
-
// return { parent, name, referenced };
|
|
82
|
-
// }
|
|
83
|
-
//
|
|
84
|
-
// const BContextEnd = richStructuralType('structural', 'BContextEnd', '1');
|
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
// import { FieldData, isNotNullResourceId, ResourceData, ResourceKind } from '../core/types';
|
|
2
|
-
// import { AnyFieldRef, AnyResourceRef, PlTransaction, ResourceRef } from '../core/transaction';
|
|
3
|
-
// import { notEmpty } from '../util/util';
|
|
4
|
-
//
|
|
5
|
-
// export type Serde<V> = {
|
|
6
|
-
// ser: (obj: V) => Uint8Array
|
|
7
|
-
// des: (data: Uint8Array) => V
|
|
8
|
-
// }
|
|
9
|
-
//
|
|
10
|
-
// const DummySerde: Serde<never> = {
|
|
11
|
-
// ser: () => {
|
|
12
|
-
// throw new Error();
|
|
13
|
-
// },
|
|
14
|
-
// des: () => {
|
|
15
|
-
// throw new Error();
|
|
16
|
-
// }
|
|
17
|
-
// };
|
|
18
|
-
//
|
|
19
|
-
// const NoSerde = { serde: DummySerde };
|
|
20
|
-
//
|
|
21
|
-
// export type StructuralSmartResourceKind = 'structural' | 'ephemeral'
|
|
22
|
-
// export type SmartResourceKind = StructuralSmartResourceKind | 'value'
|
|
23
|
-
//
|
|
24
|
-
// export type SmartResourceType<Kind extends ResourceKind, Name extends string,
|
|
25
|
-
// Version extends string, Value = never> =
|
|
26
|
-
// {
|
|
27
|
-
// kind: Kind,
|
|
28
|
-
// smartKind: SmartResourceKind,
|
|
29
|
-
// name: Name,
|
|
30
|
-
// version: Version,
|
|
31
|
-
// serde: Serde<Value>
|
|
32
|
-
// }
|
|
33
|
-
//
|
|
34
|
-
// export type AnySmartResourceType = SmartResourceType<ResourceKind, string, string, unknown>
|
|
35
|
-
// export type AnyStructuralSmartResourceType = SmartResourceType<'Structural', string, string, unknown>
|
|
36
|
-
//
|
|
37
|
-
// export type SmartFieldType<
|
|
38
|
-
// Parent extends AnyStructuralSmartResourceType,
|
|
39
|
-
// Ref extends AnyStructuralSmartResourceType> =
|
|
40
|
-
// {
|
|
41
|
-
// parent: Parent,
|
|
42
|
-
// name: string,
|
|
43
|
-
// referenced: Ref
|
|
44
|
-
// }
|
|
45
|
-
//
|
|
46
|
-
// export type AnySmartFieldType = SmartFieldType<AnyStructuralSmartResourceType, AnyStructuralSmartResourceType>;
|
|
47
|
-
//
|
|
48
|
-
// export function structuralValueSRT<Name extends string, Version extends string, Value>(
|
|
49
|
-
// smartKind: SmartResourceKind,
|
|
50
|
-
// name: Name,
|
|
51
|
-
// version: Version,
|
|
52
|
-
// serde: Serde<Value>
|
|
53
|
-
// ): SmartResourceType<'Structural', Name, Version, Value> {
|
|
54
|
-
// return { kind: 'Structural', smartKind, name, version, serde };
|
|
55
|
-
// }
|
|
56
|
-
//
|
|
57
|
-
// export function structuralSRT<Name extends string, Version extends string>(
|
|
58
|
-
// smartKind: SmartResourceKind,
|
|
59
|
-
// name: Name,
|
|
60
|
-
// version: Version
|
|
61
|
-
// ): SmartResourceType<'Structural', Name, Version, never> {
|
|
62
|
-
// return { kind: 'Structural', smartKind, name, version, serde: DummySerde };
|
|
63
|
-
// }
|
|
64
|
-
//
|
|
65
|
-
// export function valueSRT<Name extends string, Version extends string, Value>(
|
|
66
|
-
// name: Name,
|
|
67
|
-
// version: Version,
|
|
68
|
-
// serde: Serde<Value>
|
|
69
|
-
// ): SmartResourceType<'Value', Name, Version, Value> {
|
|
70
|
-
// return { kind: 'Value', smartKind: 'value', name, version, serde };
|
|
71
|
-
// }
|
|
72
|
-
//
|
|
73
|
-
// export function smartFields<
|
|
74
|
-
// Parent extends AnyStructuralSmartResourceType,
|
|
75
|
-
// Ref extends AnyStructuralSmartResourceType>(
|
|
76
|
-
// parent: Parent,
|
|
77
|
-
// name: string,
|
|
78
|
-
// referenced: Ref
|
|
79
|
-
// ): SmartFieldType<Parent, Ref> {
|
|
80
|
-
// return { parent, name, referenced };
|
|
81
|
-
// }
|
|
82
|
-
//
|
|
83
|
-
// export type GetValueType<ST extends AnySmartResourceType> =
|
|
84
|
-
// ST extends SmartResourceType<ResourceKind, string, string, infer X> ? X : never
|
|
85
|
-
//
|
|
86
|
-
// export type GetRef<SF extends AnySmartFieldType> =
|
|
87
|
-
// SF extends SmartFieldType<AnyStructuralSmartResourceType, infer X> ? X : never
|
|
88
|
-
//
|
|
89
|
-
// export type GetParent<SF extends AnySmartFieldType> =
|
|
90
|
-
// SF extends SmartFieldType<infer X, AnyStructuralSmartResourceType> ? X : never
|
|
91
|
-
//
|
|
92
|
-
// export class SmartStructuralResourceAccessor<ST extends AnyStructuralSmartResourceType> {
|
|
93
|
-
// constructor(public readonly type: ST,
|
|
94
|
-
// public readonly ref: AnyResourceRef,
|
|
95
|
-
// public readonly tx: PlTransaction) {
|
|
96
|
-
// }
|
|
97
|
-
//
|
|
98
|
-
// private _resourceData?: Promise<ResourceData>;
|
|
99
|
-
//
|
|
100
|
-
// public async getResourceData(): Promise<ResourceData> {
|
|
101
|
-
// if (this._resourceData === undefined)
|
|
102
|
-
// this._resourceData = this.tx.getResourceData(this.ref, true);
|
|
103
|
-
// return await this._resourceData;
|
|
104
|
-
// }
|
|
105
|
-
//
|
|
106
|
-
// public async getValue(): Promise<GetValueType<ST>> {
|
|
107
|
-
// return this.type.serde.des(notEmpty((await this.getResourceData()).data)) as GetValueType<ST>;
|
|
108
|
-
// }
|
|
109
|
-
//
|
|
110
|
-
// public getField<SF extends SmartFieldType<ST, AnyStructuralSmartResourceType>>(field: SF): Promise<GetValueType<ST>> {
|
|
111
|
-
// return new SmartFieldAccessor(field, { resourceId: this.ref, fieldName: field.name }, this.tx);
|
|
112
|
-
// }
|
|
113
|
-
//
|
|
114
|
-
// }
|
|
115
|
-
//
|
|
116
|
-
// export class SmartFieldAccessor<SF extends AnySmartFieldType> {
|
|
117
|
-
// constructor(public readonly descriptor: SF,
|
|
118
|
-
// public readonly ref: AnyFieldRef,
|
|
119
|
-
// public readonly tx: PlTransaction) {
|
|
120
|
-
// }
|
|
121
|
-
//
|
|
122
|
-
// public parent(): SmartStructuralResourceAccessor<GetParent<SF>> {
|
|
123
|
-
// return new SmartStructuralResourceAccessor(
|
|
124
|
-
// this.descriptor.parent as GetParent<SF>,
|
|
125
|
-
// this.ref.resourceId, this.tx);
|
|
126
|
-
// }
|
|
127
|
-
//
|
|
128
|
-
// private _fieldData?: Promise<FieldData>;
|
|
129
|
-
//
|
|
130
|
-
// public async getFieldData(): Promise<FieldData> {
|
|
131
|
-
// if (this._fieldData === undefined)
|
|
132
|
-
// this._fieldData = this.tx.getField(this.ref);
|
|
133
|
-
// return await this._fieldData;
|
|
134
|
-
// }
|
|
135
|
-
//
|
|
136
|
-
//
|
|
137
|
-
// public async get(): SmartStructuralResourceAccessor<GetRef<SF>> {
|
|
138
|
-
// const fieldData = await this.getFieldData();
|
|
139
|
-
// if (isNotNullResourceId(fieldData.error))
|
|
140
|
-
// throw new Error('Error in field.');
|
|
141
|
-
// return new SmartStructuralResourceAccessor(
|
|
142
|
-
// this.descriptor.referenced as GetRef<SF>,
|
|
143
|
-
// fieldData.value,
|
|
144
|
-
// this.tx);
|
|
145
|
-
// }
|
|
146
|
-
// }
|