Repository navigation
Expand file tree
/
Copy pathconformance.ts
More file actions
156 lines (150 loc) · 6.76 KB
/
Copy pathconformance.ts
File metadata and controls
156 lines (150 loc) · 6.76 KB
1
2
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// © Copyright 2025-2026, Query.Farm LLC - https://query.farm
// SPDX-License-Identifier: Apache-2.0
/**
* Conformance worker — 90-method reference RPC service exercising all framework
* capabilities. Used by the Python CLI to verify wire-protocol compatibility.
*
* Flags:
* --access-log <path> Append JSONL access-log records to <path>.
* --access-log-sample R Log only fraction R (0.0-1.0) of successful calls.
* Errors are always logged and every kept record
* carries `sample_rate`. An out-of-range R fails here,
* at startup, not at the first request.
* --access-log-async Drain records through a bounded queue instead of
* writing them inline. Trades durability for latency.
* --access-log-debug Accepted for CLI parity with the other ports. Records
* are identical at every level: no payload value or
* state token is ever logged (request_fields /
* request_rows and *_state_bytes describe them).
* --tcp [HOST:]PORT Serve over a raw TCP socket instead of stdin/stdout.
* HOST defaults to 127.0.0.1 (loopback only); PORT may
* be 0 to let the OS auto-select. Prints
* `TCP:<host>:<port>` once bound, then nothing more on
* stdout. No auth/TLS — trusted networks only.
* --unix <path> Serve over an AF_UNIX socket instead of stdin/stdout.
* Prints `UNIX:<path>` once bound. The pipe and socket
* writers are different code (writeSync vs
* socket.write + drain), and AF_UNIX is where Bun's
* large-write behaviour diverges from TCP's, so
* `vgi-rpc-test --unix` is not redundant with --tcp.
* --fake-storage <url> Externalize response batches to the conformance fake
* object store at <url>. Byte-stream transports resolve
* pointers through the same code path HTTP does, and
* nothing exercised it until `TestExternalByteStream`;
* this flag is what gives that group a server.
* --externalize-threshold <n>
* Byte size above which a batch is externalized.
* Defaults to 1 — every data batch goes through
* storage, which is what makes the group's upload
* assertions mean something.
*
* Run: bun run examples/conformance.ts
*/
import { openSync } from "node:fs";
import type { ExternalLocationConfig } from "../src/external.js";
import { AccessLogHook, FdSink, serveTcp, serveUnix } from "../src/index.js";
import { conformanceHost, protocol, setConformanceExternalStorage } from "./conformance-protocol.js";
import { FakeStorage } from "./fake-storage.js";
const args = process.argv.slice(2);
let accessLogPath: string | undefined;
let accessLogSample = 1;
let accessLogAsync = false;
let accessLogDebug = false;
let tcpArg: string | undefined;
let unixArg: string | undefined;
let fakeStorageUrl: string | undefined;
let externalizeThreshold = 1;
for (let i = 0; i < args.length; i++) {
if (args[i] === "--access-log" && i + 1 < args.length) {
accessLogPath = args[++i];
} else if (args[i] === "--access-log-sample" && i + 1 < args.length) {
accessLogSample = Number.parseFloat(args[++i]);
} else if (args[i] === "--access-log-async") {
accessLogAsync = true;
} else if (args[i] === "--access-log-debug") {
accessLogDebug = true;
} else if (args[i] === "--tcp" && i + 1 < args.length) {
tcpArg = args[++i];
} else if (args[i] === "--unix" && i + 1 < args.length) {
unixArg = args[++i];
} else if (args[i] === "--fake-storage" && i + 1 < args.length) {
fakeStorageUrl = args[++i];
} else if (args[i] === "--externalize-threshold" && i + 1 < args.length) {
externalizeThreshold = Number.parseInt(args[++i], 10);
}
}
let externalLocation: ExternalLocationConfig | undefined;
if (fakeStorageUrl) {
if (!Number.isSafeInteger(externalizeThreshold) || externalizeThreshold < 1) {
process.stderr.write(`--externalize-threshold expects a positive integer, got '${externalizeThreshold}'\n`);
process.exit(2);
}
externalLocation = {
storage: new FakeStorage(fakeStorageUrl),
externalizeThresholdBytes: externalizeThreshold,
// The fake store vends http://127.0.0.1 URLs; the default validator is
// HTTPS-only and would (correctly) refuse them.
urlValidator: null,
};
// published_string publishes through the worker's own storage.
setConformanceExternalStorage(externalLocation.storage, externalLocation.compression);
}
let dispatchHook: AccessLogHook | undefined;
if (accessLogPath) {
const fd = openSync(accessLogPath, "a");
dispatchHook = new AccessLogHook(new FdSink(fd), {
serverVersion: "vgi-rpc-typescript-conformance",
sampleRate: accessLogSample,
async: accessLogAsync,
level: accessLogDebug ? "DEBUG" : "INFO",
});
// Queued records are only durable once drained; a worker that exits with a
// full queue would otherwise lose the tail of its own audit trail.
if (accessLogAsync) {
const hook = dispatchHook;
process.on("exit", () => hook.flush());
}
}
// One protocol host for whichever transport was selected: ConformanceService
// plus conformance.Secondary.v1, registered through the public hosting API.
const conformance = conformanceHost({
enableDescribe: true,
dispatchHook,
externalLocation,
protocolVersion: protocol.protocolVersion,
});
if (unixArg !== undefined) {
const handle = await serveUnix(conformance, {
unixPath: unixArg,
idleTimeout: 0,
});
await handle.done;
} else if (tcpArg !== undefined) {
// Parse `[HOST:]PORT`, mirroring the Python `--tcp` parsing. Host defaults
// to loopback. `serveTcp` prints `TCP:<host>:<port>` once bound.
let host = "127.0.0.1";
let portPart = tcpArg;
if (tcpArg.includes(":")) {
const idx = tcpArg.lastIndexOf(":");
const hostPart = tcpArg.slice(0, idx);
if (hostPart) host = hostPart;
portPart = tcpArg.slice(idx + 1);
}
const port = Number.parseInt(portPart, 10);
if (Number.isNaN(port)) {
process.stderr.write(`--tcp expects [HOST:]PORT, got '${tcpArg}'\n`);
process.exit(2);
}
const handle = await serveTcp(conformance, {
host,
port,
idleTimeout: 0,
});
await handle.done;
} else {
// Introspection is vgi_rpc.Reflection.v1, an ordinary co-hosted protocol,
// rather than a reserved method name; `enableDescribe` (the default) is what
// hosts it, registered after the application protocol so it appears in its
// own output without being special-cased.
conformance.run();
}