@volter/world-core 2.0.37 → 3.0.1

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.
Files changed (205) hide show
  1. package/README.md +4 -5
  2. package/app-route.cjs +12 -6
  3. package/app-route.d.cts +1 -1
  4. package/dist/app-route.cjs +12 -6
  5. package/dist/app-route.d.cts +1 -1
  6. package/dist/generated/pack-facts.json +1410 -3069
  7. package/dist/inject.cjs +64 -9
  8. package/dist/pack-facts.cjs +44 -0
  9. package/dist/src/actions.d.ts +3 -3
  10. package/dist/src/actions.js +22 -16
  11. package/dist/src/ancestry.d.ts +14 -2
  12. package/dist/src/ancestry.js +92 -2
  13. package/dist/src/anthropic-wire.d.ts +39 -0
  14. package/dist/src/anthropic-wire.js +136 -0
  15. package/dist/src/bytes.d.ts +7 -0
  16. package/dist/src/bytes.js +35 -0
  17. package/dist/src/changeset.d.ts +1 -1
  18. package/dist/src/changeset.js +0 -0
  19. package/dist/src/clickhouse/index.d.ts +3 -0
  20. package/dist/src/clickhouse/index.js +6 -0
  21. package/dist/src/clickhouse/sql.d.ts +233 -0
  22. package/dist/src/clickhouse/sql.js +4329 -0
  23. package/dist/src/clickhouse/types.d.ts +18 -0
  24. package/dist/src/clickhouse/types.js +47 -0
  25. package/dist/src/clickhouse/values.d.ts +146 -0
  26. package/dist/src/clickhouse/values.js +858 -0
  27. package/dist/src/client-bundle.js +2 -3
  28. package/dist/src/cors.d.ts +15 -0
  29. package/dist/src/cors.js +31 -0
  30. package/dist/src/derived-core.d.ts +487 -24
  31. package/dist/src/derived-core.js +788 -144
  32. package/dist/src/derived-real.d.ts +13 -0
  33. package/dist/src/derived-real.js +518 -0
  34. package/dist/src/derived.d.ts +35 -1
  35. package/dist/src/derived.js +61 -9
  36. package/dist/src/emit.js +1 -2
  37. package/dist/src/events.d.ts +206 -0
  38. package/dist/src/events.js +341 -0
  39. package/dist/src/executor.d.ts +3 -0
  40. package/dist/src/executor.js +19 -2
  41. package/dist/src/file-response.d.ts +6 -0
  42. package/dist/src/file-response.js +30 -0
  43. package/dist/src/fork.js +3 -2
  44. package/dist/src/git/history.d.ts +7 -0
  45. package/dist/src/git/history.js +24 -0
  46. package/dist/src/git/index.d.ts +1 -0
  47. package/dist/src/git/index.js +1 -0
  48. package/dist/src/git/lfs.d.ts +28 -0
  49. package/dist/src/git/lfs.js +66 -0
  50. package/dist/src/git/objects.js +3 -8
  51. package/dist/src/git/smart-http.d.ts +3 -1
  52. package/dist/src/git/smart-http.js +67 -6
  53. package/dist/src/graphql-wire.d.ts +29 -0
  54. package/dist/src/graphql-wire.js +101 -0
  55. package/dist/src/grpc-wire.d.ts +67 -0
  56. package/dist/src/grpc-wire.js +170 -0
  57. package/dist/src/h2.d.ts +40 -0
  58. package/dist/src/h2.js +656 -0
  59. package/dist/src/head.d.ts +32 -3
  60. package/dist/src/head.js +161 -40
  61. package/dist/src/history.d.ts +1 -1
  62. package/dist/src/history.js +6 -6
  63. package/dist/src/hpack.json +1 -0
  64. package/dist/src/index.d.ts +64 -75
  65. package/dist/src/index.js +58 -101
  66. package/dist/src/log.js +28 -19
  67. package/dist/src/machines.d.ts +50 -0
  68. package/dist/src/machines.js +151 -0
  69. package/dist/src/managed-database.d.ts +86 -0
  70. package/dist/src/managed-database.js +283 -0
  71. package/dist/src/multipart.d.ts +11 -0
  72. package/dist/src/multipart.js +51 -0
  73. package/dist/src/observe.d.ts +15 -5
  74. package/dist/src/observe.js +23 -9
  75. package/dist/src/openai-wire.d.ts +108 -0
  76. package/dist/src/openai-wire.js +337 -0
  77. package/dist/src/pack-assets.d.ts +3 -4
  78. package/dist/src/pack-assets.js +15 -10
  79. package/dist/src/pack-fetch.d.ts +77 -0
  80. package/dist/src/pack-fetch.js +449 -0
  81. package/dist/src/pack-paths.d.ts +12 -0
  82. package/dist/src/pack-paths.js +86 -0
  83. package/dist/src/packRegistry.d.ts +69 -162
  84. package/dist/src/packRegistry.js +55 -20
  85. package/dist/src/people.d.ts +13 -0
  86. package/dist/src/people.js +18 -0
  87. package/dist/src/placeholder-image.d.ts +5 -0
  88. package/dist/src/placeholder-image.js +114 -0
  89. package/dist/src/protobuf.d.ts +28 -0
  90. package/dist/src/protobuf.js +332 -0
  91. package/dist/src/redis/engine.js +1 -1
  92. package/dist/src/request-scope.d.ts +1 -1
  93. package/dist/src/request-scope.js +6 -4
  94. package/dist/src/resource-blob.d.ts +5 -0
  95. package/dist/src/resource-blob.js +11 -0
  96. package/dist/src/runtime.d.ts +85 -0
  97. package/dist/src/runtime.js +104 -0
  98. package/dist/src/s3/wire.d.ts +60 -0
  99. package/dist/src/s3/wire.js +157 -0
  100. package/dist/src/scenario.d.ts +3 -0
  101. package/dist/src/scenario.js +2 -0
  102. package/dist/src/schema-sample.d.ts +1 -0
  103. package/dist/src/schema-sample.js +21 -0
  104. package/dist/src/sealed-box.d.ts +14 -0
  105. package/dist/src/sealed-box.js +225 -0
  106. package/dist/src/serve-http.d.ts +14 -0
  107. package/dist/src/serve-http.js +27 -3
  108. package/dist/src/serve.d.ts +6 -0
  109. package/dist/src/serve.js +69 -14
  110. package/dist/src/signing.d.ts +135 -0
  111. package/dist/src/signing.js +222 -0
  112. package/dist/src/sigv4.d.ts +48 -0
  113. package/dist/src/sigv4.js +167 -0
  114. package/dist/src/smtp.d.ts +16 -0
  115. package/dist/src/smtp.js +72 -0
  116. package/dist/src/sockets.d.ts +51 -0
  117. package/dist/src/sockets.js +90 -0
  118. package/dist/src/state-system.d.ts +1 -0
  119. package/dist/src/state-system.js +1 -1
  120. package/dist/src/storage.d.ts +1 -1
  121. package/dist/src/storage.js +3 -3
  122. package/dist/src/trace-context.js +1 -1
  123. package/dist/src/twin-fetch.d.ts +0 -7
  124. package/dist/src/twin-fetch.js +0 -14
  125. package/dist/src/vendor-call.d.ts +6 -0
  126. package/dist/src/vendor-call.js +41 -0
  127. package/dist/src/world-store.js +1 -1
  128. package/dist/vendor-hosts.cjs +36 -125
  129. package/dist/vendor-hosts.d.cts +8 -0
  130. package/generated/pack-facts.json +1410 -3069
  131. package/inject.cjs +64 -9
  132. package/pack-facts.cjs +44 -0
  133. package/package.json +17 -3
  134. package/src/actions.ts +23 -16
  135. package/src/ancestry.ts +74 -2
  136. package/src/anthropic-wire.ts +137 -0
  137. package/src/bytes.ts +42 -0
  138. package/src/changeset.ts +5 -5
  139. package/src/clickhouse/index.ts +6 -0
  140. package/src/clickhouse/sql.ts +3059 -0
  141. package/src/clickhouse/types.ts +44 -0
  142. package/src/clickhouse/values.ts +697 -0
  143. package/src/client-bundle.ts +2 -3
  144. package/src/cors.ts +34 -0
  145. package/src/derived-core.ts +1013 -146
  146. package/src/derived-real.ts +434 -0
  147. package/src/derived.ts +73 -3
  148. package/src/emit.ts +1 -2
  149. package/src/events.ts +449 -0
  150. package/src/executor.ts +24 -2
  151. package/src/file-response.ts +27 -0
  152. package/src/fork.ts +3 -2
  153. package/src/git/history.ts +19 -0
  154. package/src/git/index.ts +1 -0
  155. package/src/git/lfs.ts +67 -0
  156. package/src/git/objects.ts +3 -5
  157. package/src/git/smart-http.ts +56 -6
  158. package/src/graphql-wire.ts +106 -0
  159. package/src/grpc-wire.ts +159 -0
  160. package/src/h2.ts +627 -0
  161. package/src/head.ts +132 -41
  162. package/src/history.ts +6 -6
  163. package/src/hpack.json +1 -0
  164. package/src/index.ts +82 -329
  165. package/src/log.ts +27 -18
  166. package/src/machines.ts +151 -0
  167. package/src/managed-database.ts +299 -0
  168. package/src/multipart.ts +51 -0
  169. package/src/observe.ts +31 -15
  170. package/src/openai-wire.ts +371 -0
  171. package/src/pack-assets.ts +15 -11
  172. package/src/pack-fetch.ts +458 -0
  173. package/src/pack-paths.ts +72 -0
  174. package/src/packRegistry.ts +79 -167
  175. package/src/people.ts +31 -0
  176. package/src/placeholder-image.ts +88 -0
  177. package/src/protobuf.ts +251 -0
  178. package/src/redis/engine.ts +1 -1
  179. package/src/request-scope.ts +8 -4
  180. package/src/resource-blob.ts +13 -0
  181. package/src/runtime.ts +344 -0
  182. package/src/s3/wire.ts +172 -0
  183. package/src/scenario.ts +4 -0
  184. package/src/schema-sample.ts +24 -0
  185. package/src/sealed-box.ts +182 -0
  186. package/src/serve-http.ts +31 -3
  187. package/src/serve.ts +58 -14
  188. package/src/signing.ts +231 -0
  189. package/src/sigv4.ts +158 -0
  190. package/src/smtp.ts +76 -0
  191. package/src/sockets.ts +140 -0
  192. package/src/state-system.ts +2 -2
  193. package/src/storage.ts +3 -3
  194. package/src/trace-context.ts +1 -1
  195. package/src/twin-fetch.ts +0 -20
  196. package/src/vendor-call.ts +41 -0
  197. package/src/world-store.ts +1 -1
  198. package/vendor-hosts.cjs +36 -125
  199. package/vendor-hosts.d.cts +8 -0
  200. package/dist/src/mirror-shell.d.ts +0 -2
  201. package/dist/src/mirror-shell.js +0 -13
  202. package/dist/src/v1-removed.d.ts +0 -159
  203. package/dist/src/v1-removed.js +0 -124
  204. package/src/mirror-shell.ts +0 -15
  205. package/src/v1-removed.ts +0 -172
@@ -0,0 +1,170 @@
1
+ // THE gRPC WIRE (architecture, "Other wires": gRPC): unary gRPC calls over the kernel's HTTP/2 server (./h2.ts), each
2
+ // request's one length-prefixed message decoded by the proto unit's schema (./protobuf.ts) and handed to the pack's
3
+ // handler for its operation, whose JSON answer is encoded back with the status trailers
4
+ // (https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md). After the older catalog's temporal pack's
5
+ // `temporal-grpc.ts`, over this repository's codec rather than protobufjs. Everything here is framing: which rpcs exist
6
+ // and what they answer is the pack's. Importing it does nothing.
7
+ import { acceptH2Connection } from "./h2.js";
8
+ import { nodeBuiltin, WORLD_BOOT_PATH } from "./serve-http.js";
9
+ import { decodeMessage, encodeMessage } from "./protobuf.js";
10
+ /** gRPC's status codes (https://grpc.github.io/grpc/core/md_doc_statuscodes.html). */
11
+ export const GrpcStatus = {
12
+ OK: 0, CANCELLED: 1, UNKNOWN: 2, INVALID_ARGUMENT: 3, DEADLINE_EXCEEDED: 4, NOT_FOUND: 5, ALREADY_EXISTS: 6,
13
+ PERMISSION_DENIED: 7, RESOURCE_EXHAUSTED: 8, FAILED_PRECONDITION: 9, ABORTED: 10, OUT_OF_RANGE: 11, UNIMPLEMENTED: 12,
14
+ INTERNAL: 13, UNAVAILABLE: 14, DATA_LOSS: 15, UNAUTHENTICATED: 16,
15
+ };
16
+ /** A call answered with a gRPC status other than OK: its code and its message. */
17
+ export class GrpcError extends Error {
18
+ code;
19
+ constructor(code, message) {
20
+ super(message);
21
+ this.code = code;
22
+ }
23
+ }
24
+ /** `grpc-timeout`: an integer and a unit (H M S m u n), PROTOCOL-HTTP2.md "Timeout". */
25
+ export function parseGrpcTimeout(value) {
26
+ const match = /^(\d{1,8})([HMSmun])$/.exec(value ?? '');
27
+ if (!match)
28
+ return undefined;
29
+ return Number(match[1]) * { H: 3_600_000, M: 60_000, S: 1000, m: 1, u: 0.001, n: 0.000001 }[match[2]];
30
+ }
31
+ /** Length-prefixed messages out of a request body: [compressed flag][u32 length][bytes]… */
32
+ export function splitGrpcFrames(body) {
33
+ const frames = [];
34
+ let offset = 0;
35
+ while (offset < body.length) {
36
+ if (body.length - offset < 5)
37
+ throw new GrpcError(GrpcStatus.INTERNAL, 'truncated gRPC frame header');
38
+ const length = new DataView(body.buffer, body.byteOffset + offset, 5).getUint32(1);
39
+ if (body.length - offset - 5 < length)
40
+ throw new GrpcError(GrpcStatus.INTERNAL, 'truncated gRPC message');
41
+ frames.push({ compressed: body[offset] === 1, message: body.subarray(offset + 5, offset + 5 + length) });
42
+ offset += 5 + length;
43
+ }
44
+ return frames;
45
+ }
46
+ export function grpcFrame(message) {
47
+ const out = new Uint8Array(5 + message.length);
48
+ new DataView(out.buffer).setUint32(1, message.length);
49
+ out.set(message, 5);
50
+ return out;
51
+ }
52
+ /** grpc-message is percent-encoded (PROTOCOL-HTTP2.md "Status-Message"). */
53
+ export function encodeGrpcMessage(text) {
54
+ return Array.from(new TextEncoder().encode(text), (b) => (b >= 0x20 && b <= 0x7e && b !== 0x25 ? String.fromCharCode(b) : `%${b.toString(16).toUpperCase().padStart(2, '0')}`)).join('');
55
+ }
56
+ /** The HTTP/2 stream handler that answers the unary rpcs of a proto unit's schema through `handler`. */
57
+ export function grpcStreamHandler(schema, handler) {
58
+ return (request, response) => {
59
+ const headers = request.headers;
60
+ const fail = (code, message) => {
61
+ if (response.closed)
62
+ return;
63
+ response.headers([[':status', '200'], ['content-type', 'application/grpc'], ['grpc-status', String(code)], ['grpc-message', encodeGrpcMessage(message)]], true);
64
+ };
65
+ if (headers[':method'] !== 'POST' || !(headers['content-type'] ?? '').startsWith('application/grpc')) {
66
+ response.headers([[':status', '415'], ['content-type', 'text/plain']], true);
67
+ return;
68
+ }
69
+ const path = headers[':path'] ?? '';
70
+ const rpc = schema.rpcs[path];
71
+ if (!rpc) {
72
+ fail(GrpcStatus.UNIMPLEMENTED, `unknown method ${path}`);
73
+ return;
74
+ }
75
+ if (rpc.clientStreams || rpc.serverStreams) {
76
+ fail(GrpcStatus.UNIMPLEMENTED, `${path} streams; this wire answers unary calls`);
77
+ return;
78
+ }
79
+ const timeout = parseGrpcTimeout(headers['grpc-timeout']);
80
+ const controller = new AbortController();
81
+ // a call ends once: its deadline passing answers DEADLINE_EXCEEDED, and the client going away (its stream reset)
82
+ // is CANCELLED, which no one hears; the handler's later answer, either way, is dropped
83
+ // (https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md "Timeout", statuscodes.md "CANCELLED")
84
+ let ended;
85
+ const end = (code) => { if (ended !== undefined)
86
+ return; ended = code; controller.abort(); if (code === GrpcStatus.DEADLINE_EXCEEDED)
87
+ fail(code, 'Deadline Exceeded'); };
88
+ const timer = timeout !== undefined ? setTimeout(() => end(GrpcStatus.DEADLINE_EXCEEDED), timeout) : undefined;
89
+ request.signal.addEventListener('abort', () => end(GrpcStatus.CANCELLED), { once: true });
90
+ const metadata = Object.fromEntries(Object.entries(headers).filter(([k]) => !k.startsWith(':')));
91
+ let decoded;
92
+ try {
93
+ const frames = splitGrpcFrames(request.body);
94
+ if (frames.length !== 1)
95
+ throw new GrpcError(GrpcStatus.INTERNAL, `a unary call carries exactly one message (got ${frames.length})`);
96
+ if (frames[0].compressed)
97
+ throw new GrpcError(GrpcStatus.UNIMPLEMENTED, 'compressed messages are not accepted (no grpc-encoding is advertised)');
98
+ decoded = decodeMessage(schema, rpc.request, frames[0].message);
99
+ }
100
+ catch (error) {
101
+ if (timer)
102
+ clearTimeout(timer);
103
+ fail(error instanceof GrpcError ? error.code : GrpcStatus.INTERNAL, error.message);
104
+ return;
105
+ }
106
+ void (async () => {
107
+ try {
108
+ const answer = await handler({ operation: rpc.operation, path, request: decoded, metadata, signal: controller.signal });
109
+ if (ended !== undefined || response.closed)
110
+ return;
111
+ response.headers([[':status', '200'], ['content-type', 'application/grpc']]);
112
+ response.data(grpcFrame(encodeMessage(schema, rpc.response, answer)));
113
+ response.trailers([['grpc-status', '0']]);
114
+ }
115
+ catch (error) {
116
+ if (ended !== undefined)
117
+ return;
118
+ if (error instanceof GrpcError)
119
+ fail(error.code, error.message);
120
+ else
121
+ fail(GrpcStatus.INTERNAL, `internal error: ${error?.message ?? String(error)}`);
122
+ }
123
+ finally {
124
+ if (timer)
125
+ clearTimeout(timer);
126
+ }
127
+ })();
128
+ };
129
+ }
130
+ /** A gRPC code for the HTTP status a pack's fetch answered: Google's REST error names it (`error.status`), else the
131
+ * status's customary code (https://github.com/googleapis/googleapis/blob/master/google/rpc/code.proto, "HTTP Mapping"). */
132
+ function codeOf(status, body) {
133
+ const named = body?.error?.status;
134
+ if (typeof named === 'string' && named in GrpcStatus)
135
+ return GrpcStatus[named];
136
+ return { 400: GrpcStatus.INVALID_ARGUMENT, 401: GrpcStatus.UNAUTHENTICATED, 403: GrpcStatus.PERMISSION_DENIED, 404: GrpcStatus.NOT_FOUND, 409: GrpcStatus.ABORTED, 429: GrpcStatus.RESOURCE_EXHAUSTED, 499: GrpcStatus.CANCELLED, 501: GrpcStatus.UNIMPLEMENTED, 503: GrpcStatus.UNAVAILABLE, 504: GrpcStatus.DEADLINE_EXCEEDED }[status] ?? (status >= 500 ? GrpcStatus.INTERNAL : GrpcStatus.UNKNOWN);
137
+ }
138
+ /** A gRPC call answered by the pack's own fetch, as the derive serves a proto unit's rpc (`POST /<package>.<Service>/
139
+ * <Method>`, the request message's JSON form the body): its JSON answer is the response message; any other status its
140
+ * gRPC code, the error's message as the status message. */
141
+ export function grpcOverFetch(fetch) {
142
+ return async (call) => {
143
+ const res = await fetch(new Request(`http://twin${call.path}`, { method: 'POST', headers: { ...call.metadata, 'content-type': 'application/json' }, body: JSON.stringify(call.request), signal: call.signal }));
144
+ const text = await res.text();
145
+ let body;
146
+ try {
147
+ body = text ? JSON.parse(text) : {};
148
+ }
149
+ catch {
150
+ body = undefined;
151
+ }
152
+ if (res.ok)
153
+ return (body ?? {});
154
+ const message = body?.error?.message ?? body?.message ?? text;
155
+ throw new GrpcError(codeOf(res.status, body), String(message).slice(0, 500));
156
+ };
157
+ }
158
+ /** A pack that speaks gRPC, served on one port: HTTP/2 (gRPC, by the schema through the pack's fetch) and HTTP/1 (the
159
+ * pack's fetch: its doors, its REST) on the same socket, as the kernel's HTTP/2 server tells them apart. Inside a World
160
+ * it answers the World's boot probe as `serveHttp` does. */
161
+ export async function serveGrpc(options) {
162
+ const net = nodeBuiltin('node:net');
163
+ const worldBoot = globalThis.process?.env?.VOLTER_WORLD_BOOT_ID;
164
+ const fetch = worldBoot ? (request) => (new URL(request.url).pathname === WORLD_BOOT_PATH ? Promise.resolve(Response.json({ boot: worldBoot })) : options.fetch(request)) : options.fetch;
165
+ const handler = grpcStreamHandler(options.schema, grpcOverFetch(fetch));
166
+ const server = net.createServer((socket) => { acceptH2Connection(socket, handler, fetch); });
167
+ await new Promise((resolve, reject) => { server.once('error', reject); server.listen(options.port ?? 0, '127.0.0.1', () => resolve()); });
168
+ const address = server.address();
169
+ return { port: typeof address === 'object' && address ? address.port : 0, stop: () => { server.close(); } };
170
+ }
@@ -0,0 +1,40 @@
1
+ import type { Socket } from 'node:net';
2
+ export declare class HpackError extends Error {
3
+ }
4
+ export declare function huffmanDecode(bytes: Uint8Array): string;
5
+ export declare class HpackDecoder {
6
+ private table;
7
+ private size;
8
+ private maxSize;
9
+ /** the SETTINGS_HEADER_TABLE_SIZE this endpoint advertised (the decoder's ceiling; 4096 is the
10
+ * protocol default, which this server never changes) */
11
+ private readonly limit;
12
+ constructor(headerTableSize?: number);
13
+ decode(block: Uint8Array): Array<[string, string]>;
14
+ private insert;
15
+ private evict;
16
+ }
17
+ /** Every header as a literal without indexing, new name, no huffman (RFC 7541 §6.2.2). */
18
+ export declare function hpackEncode(headers: Array<[string, string]>): Uint8Array;
19
+ export type H2Request = {
20
+ headers: Record<string, string>;
21
+ body: Uint8Array;
22
+ /** aborted when the peer resets the stream or the connection goes away */
23
+ signal: AbortSignal;
24
+ };
25
+ export type H2Response = {
26
+ /** send response headers; `end` closes the stream with them (a trailers-only answer) */
27
+ headers(headers: Array<[string, string]>, end?: boolean): void;
28
+ data(bytes: Uint8Array): void;
29
+ /** close the stream with trailing headers */
30
+ trailers(headers: Array<[string, string]>): void;
31
+ readonly closed: boolean;
32
+ };
33
+ export type H2Handler = (request: H2Request, response: H2Response) => void;
34
+ /** An HTTP/1 request arriving on the same port, answered by a fetch handler. */
35
+ export type Http1Handler = (request: Request) => Response | Promise<Response>;
36
+ /** Speak HTTP/2 (h2c with prior knowledge) on an accepted socket; an HTTP/1 request line is handed to
37
+ * `http1` when given. The listener that accepts sockets is the pack's server module's. */
38
+ export declare function acceptH2Connection(socket: Socket, handler: H2Handler, http1?: Http1Handler): {
39
+ teardown(): void;
40
+ };