@celobus/sdk 0.0.0-stage → 0.1.0
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 +91 -0
- package/README.md +38 -2
- package/dist/access.d.ts +141 -0
- package/dist/access.js +232 -0
- package/dist/codec.d.ts +29 -0
- package/dist/codec.js +361 -0
- package/dist/cron.d.ts +2 -0
- package/dist/cron.js +113 -0
- package/dist/ctx.d.ts +644 -0
- package/dist/ctx.js +657 -0
- package/dist/db.d.ts +234 -0
- package/dist/db.js +495 -0
- package/dist/decimal.d.ts +39 -0
- package/dist/decimal.js +158 -0
- package/dist/declare.d.ts +311 -0
- package/dist/declare.js +78 -0
- package/dist/descriptor.d.ts +229 -0
- package/dist/descriptor.js +899 -0
- package/dist/errors.d.ts +65 -0
- package/dist/errors.js +114 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +34 -0
- package/dist/log.d.ts +39 -0
- package/dist/log.js +101 -0
- package/dist/names.d.ts +12 -0
- package/dist/names.js +41 -0
- package/dist/notify.d.ts +185 -0
- package/dist/notify.js +234 -0
- package/dist/platform.d.ts +61 -0
- package/dist/platform.js +47 -0
- package/dist/plugin.d.ts +43 -0
- package/dist/plugin.js +117 -0
- package/dist/query.d.ts +326 -0
- package/dist/query.js +794 -0
- package/dist/remote.d.ts +8 -0
- package/dist/remote.js +19 -0
- package/dist/schema.d.ts +487 -0
- package/dist/schema.js +405 -0
- package/dist/serve.d.ts +63 -0
- package/dist/serve.js +411 -0
- package/dist/sql.d.ts +13 -0
- package/dist/sql.js +33 -0
- package/dist/ui.d.ts +1867 -0
- package/dist/ui.js +3964 -0
- package/dist/viz-kinds.d.ts +12 -0
- package/dist/viz-kinds.js +14 -0
- package/package.json +33 -4
- package/proto/plugin.proto +994 -0
package/dist/serve.js
ADDED
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
// The plugin session (`Celld.Session` of proto/plugin.proto) and request dispatch.
|
|
2
|
+
//
|
|
3
|
+
// The plugin connects to the platform at $CELOBUS_ADDR, sends `Register`, then serves `Request`s concurrently
|
|
4
|
+
// (at most `maxConcurrency` handlers at once, the rest wait in order) and sends `Syscall`s for its context. When
|
|
5
|
+
// the session drops, pending syscalls fail with `unavailable`, in-flight handlers are aborted (their answers
|
|
6
|
+
// could not be delivered), and the plugin reconnects with backoff. SIGTERM (and SIGINT) drain: new requests get
|
|
7
|
+
// 503 `unavailable`, in-flight ones finish (up to `drainMs`), then the session closes and the process exits.
|
|
8
|
+
//
|
|
9
|
+
// Background jobs: the plugin announces its job pool (`jobSlots`) when it registers, and runs each `JobRun` on that
|
|
10
|
+
// pool, apart from the requests; the platform never hands it more runs than it has slots. A run's handler gets a
|
|
11
|
+
// context whose `job` is the run and whose `signal` aborts on `JobAbort` (and when the session ends); the attempt is
|
|
12
|
+
// answered with `JobDone` (at once on an abort). A drain waits for the runs too, and a run that arrives while
|
|
13
|
+
// draining is refused as `shutdown`.
|
|
14
|
+
import * as fs from "node:fs";
|
|
15
|
+
import * as path from "node:path";
|
|
16
|
+
import { fileURLToPath } from "node:url";
|
|
17
|
+
import * as grpc from "@grpc/grpc-js";
|
|
18
|
+
import * as loader from "@grpc/proto-loader";
|
|
19
|
+
import { decodeMessage, encodeMessage, OutputError } from "./codec.js";
|
|
20
|
+
import { Caller, Ctx, SyscallError } from "./ctx.js";
|
|
21
|
+
import { DomainError, InvalidError, RpcError, toRpcError, unavailable } from "./errors.js";
|
|
22
|
+
import { cutUtf8, runLogger } from "./log.js";
|
|
23
|
+
import { endpointPath } from "./names.js";
|
|
24
|
+
function protoPath() {
|
|
25
|
+
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
26
|
+
const candidates = [
|
|
27
|
+
process.env.CELOBUS_PROTO,
|
|
28
|
+
path.join(here, "..", "proto", "plugin.proto"), // the package (copied at build)
|
|
29
|
+
path.join(here, "..", "..", "..", "proto", "plugin.proto"), // the platform checkout
|
|
30
|
+
].filter((p) => !!p);
|
|
31
|
+
const found = candidates.find((p) => fs.existsSync(p));
|
|
32
|
+
if (!found)
|
|
33
|
+
throw new Error("plugin.proto not found; set CELOBUS_PROTO");
|
|
34
|
+
return found;
|
|
35
|
+
}
|
|
36
|
+
let celldService;
|
|
37
|
+
/** @internal The `celobus.v1.Celld` service, loaded from plugin.proto. */
|
|
38
|
+
export function celldDefinition() {
|
|
39
|
+
if (!celldService) {
|
|
40
|
+
const def = loader.loadSync(protoPath(), { keepCase: false, longs: String, enums: String, defaults: true, oneofs: true });
|
|
41
|
+
const pkg = grpc.loadPackageDefinition(def);
|
|
42
|
+
celldService = pkg.celobus.v1.Celld;
|
|
43
|
+
}
|
|
44
|
+
return celldService;
|
|
45
|
+
}
|
|
46
|
+
function json(status, body) {
|
|
47
|
+
return {
|
|
48
|
+
status,
|
|
49
|
+
headers: [{ name: "content-type", value: "application/json" }],
|
|
50
|
+
body: Buffer.from(JSON.stringify(body)),
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
function errorResponse(e) {
|
|
54
|
+
return json(e.status, e.toJSON());
|
|
55
|
+
}
|
|
56
|
+
function header(req, name) {
|
|
57
|
+
return req.headers?.find((h) => h.name.toLowerCase() === name)?.value;
|
|
58
|
+
}
|
|
59
|
+
/** @internal Serves one request: route, decode, run the handler under its deadline, encode, map errors. */
|
|
60
|
+
export async function dispatch(model, routes, session, req, sessionSignal) {
|
|
61
|
+
const found = routes.get(req.path);
|
|
62
|
+
// the platform's own requests (the run-change hook) carry no caller: one with a caller came from outside
|
|
63
|
+
const ep = found?.system && req.principal ? undefined : found;
|
|
64
|
+
if (!ep)
|
|
65
|
+
return errorResponse(new RpcError(404, "unknown_endpoint", `no endpoint ${req.path}`));
|
|
66
|
+
if ((req.method || "POST").toUpperCase() !== "POST") {
|
|
67
|
+
return errorResponse(new RpcError(405, "method_not_allowed", `${req.method} is not allowed; use POST`));
|
|
68
|
+
}
|
|
69
|
+
const contentType = header(req, "content-type");
|
|
70
|
+
if (contentType && !/^application\/json\s*(;|$)/i.test(contentType)) {
|
|
71
|
+
return errorResponse(new RpcError(415, "unsupported_media_type", `${contentType} is not supported; send application/json`));
|
|
72
|
+
}
|
|
73
|
+
const log = model.log.with({ endpoint: ep.name, request_id: req.id });
|
|
74
|
+
let body;
|
|
75
|
+
try {
|
|
76
|
+
const text = Buffer.from(req.body ?? []).toString("utf8");
|
|
77
|
+
body = text.trim() ? JSON.parse(text) : {};
|
|
78
|
+
}
|
|
79
|
+
catch {
|
|
80
|
+
return errorResponse(new InvalidError("the body is not JSON"));
|
|
81
|
+
}
|
|
82
|
+
if (ep.decode) {
|
|
83
|
+
try {
|
|
84
|
+
body = ep.decode(body);
|
|
85
|
+
}
|
|
86
|
+
catch (e) {
|
|
87
|
+
return errorResponse(toRpcError(e));
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
const fieldErrors = {};
|
|
91
|
+
const input = ep.decode ? body : decodeMessage(ep.input, body, "", fieldErrors);
|
|
92
|
+
if (Object.keys(fieldErrors).length > 0) {
|
|
93
|
+
const e = new InvalidError("the request is invalid");
|
|
94
|
+
for (const [p, m] of Object.entries(fieldErrors))
|
|
95
|
+
e.field(p, m);
|
|
96
|
+
return errorResponse(e);
|
|
97
|
+
}
|
|
98
|
+
const controller = new AbortController();
|
|
99
|
+
const deadline = new Date(Date.now() + ep.timeoutMs);
|
|
100
|
+
const timer = setTimeout(() => controller.abort(new RpcError(504, "deadline_exceeded", `${ep.name} did not answer within ${ep.timeoutMs} ms`)), ep.timeoutMs);
|
|
101
|
+
const onSessionEnd = () => controller.abort(unavailable("the session to the platform ended"));
|
|
102
|
+
if (sessionSignal?.aborted)
|
|
103
|
+
onSessionEnd();
|
|
104
|
+
else
|
|
105
|
+
sessionSignal?.addEventListener("abort", onSessionEnd, { once: true });
|
|
106
|
+
const aborted = new Promise((_, reject) => {
|
|
107
|
+
if (controller.signal.aborted)
|
|
108
|
+
reject(controller.signal.reason);
|
|
109
|
+
controller.signal.addEventListener("abort", () => reject(controller.signal.reason), { once: true });
|
|
110
|
+
});
|
|
111
|
+
aborted.catch(() => { });
|
|
112
|
+
const ctx = new Ctx(session, new Caller(req.principal, model.id, model.resolved.roleName), { id: String(req.id), deadline, signal: controller.signal }, { id: model.id, version: model.version }, log);
|
|
113
|
+
try {
|
|
114
|
+
const output = await Promise.race([Promise.resolve().then(() => ep.handler(ctx, input)), aborted]);
|
|
115
|
+
return json(200, encodeMessage(ep.output, output));
|
|
116
|
+
}
|
|
117
|
+
catch (e) {
|
|
118
|
+
const err = toRpcError(e);
|
|
119
|
+
if (err.status >= 500) {
|
|
120
|
+
const cause = e instanceof RpcError ? e.cause : e;
|
|
121
|
+
log.error(e instanceof OutputError ? "the handler's output does not fit its type" : "the request failed", {
|
|
122
|
+
status: err.status,
|
|
123
|
+
code: err.code,
|
|
124
|
+
error: cause ?? err,
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
return errorResponse(err);
|
|
128
|
+
}
|
|
129
|
+
finally {
|
|
130
|
+
clearTimeout(timer);
|
|
131
|
+
sessionSignal?.removeEventListener("abort", onSessionEnd);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
/** The error code of an abort's reason (an unknown reason is a cancel). */
|
|
135
|
+
const ABORT_CODES = {
|
|
136
|
+
TIMEOUT: "timeout",
|
|
137
|
+
CANCELLED: "cancelled",
|
|
138
|
+
SHUTDOWN: "shutdown",
|
|
139
|
+
LEASE_LOST: "lease_lost",
|
|
140
|
+
AUTH_CHANGED: "auth_changed",
|
|
141
|
+
REMOVED: "job_removed",
|
|
142
|
+
};
|
|
143
|
+
/** The longest error message a `JobDone` carries, in UTF-8 bytes. */
|
|
144
|
+
const MAX_JOB_ERROR = 2048;
|
|
145
|
+
/** A run's attempt stopped: `code` is what its `JobDone` says. */
|
|
146
|
+
class RunStopped extends RpcError {
|
|
147
|
+
constructor(code) {
|
|
148
|
+
super(503, code, `stopped: ${code}`);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/** The `JobError` of what a run's handler threw. */
|
|
152
|
+
function jobError(e) {
|
|
153
|
+
const err = e instanceof DomainError ? e.error() : e;
|
|
154
|
+
if (err instanceof RpcError)
|
|
155
|
+
return { code: err.code, message: cutUtf8(err.message, MAX_JOB_ERROR), permanent: err.permanent };
|
|
156
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
157
|
+
return { code: "internal", message: cutUtf8(message, MAX_JOB_ERROR), permanent: false };
|
|
158
|
+
}
|
|
159
|
+
const LEVELS = { debug: "DEBUG", info: "INFO", warn: "WARN", error: "ERROR" };
|
|
160
|
+
/** @internal Runs the session until stopped. */
|
|
161
|
+
export function runSession(model, options = {}) {
|
|
162
|
+
const addr = options.addr ?? process.env.CELOBUS_ADDR;
|
|
163
|
+
if (!addr)
|
|
164
|
+
throw new Error("CELOBUS_ADDR is not set (the platform sets it)");
|
|
165
|
+
const maxConcurrency = Math.max(1, options.maxConcurrency ?? 64);
|
|
166
|
+
const drainMs = options.drainMs ?? 10_000;
|
|
167
|
+
const routes = new Map(model.resolved.endpoints.map((ep) => [endpointPath(model.id, ep.name), ep]));
|
|
168
|
+
const log = model.log;
|
|
169
|
+
const Celld = celldDefinition();
|
|
170
|
+
const client = new Celld(addr, grpc.credentials.createInsecure(), {
|
|
171
|
+
"grpc.max_receive_message_length": 64 << 20,
|
|
172
|
+
"grpc.max_send_message_length": 64 << 20,
|
|
173
|
+
});
|
|
174
|
+
let link;
|
|
175
|
+
let nextSyscall = 1;
|
|
176
|
+
let backoff = 250;
|
|
177
|
+
let stopping = false;
|
|
178
|
+
let stopped = false;
|
|
179
|
+
let active = 0;
|
|
180
|
+
const waiting = [];
|
|
181
|
+
let idle;
|
|
182
|
+
let finish;
|
|
183
|
+
const done = new Promise((resolve) => (finish = resolve));
|
|
184
|
+
const jobs = model.resolved.jobs ?? new Map();
|
|
185
|
+
const jobSlots = jobs.size > 0 ? model.resolved.jobSlots : 0;
|
|
186
|
+
/** The runs in flight, by run id: what stops each and the session it came on. */
|
|
187
|
+
const runs = new Map();
|
|
188
|
+
const session = {
|
|
189
|
+
jobs: new Map([...jobs.values()].map((j) => [j.def, j])),
|
|
190
|
+
syscall(call) {
|
|
191
|
+
const current = link;
|
|
192
|
+
if (!current || current.closed)
|
|
193
|
+
return Promise.reject(unavailable("the session to the platform is down"));
|
|
194
|
+
const id = String(nextSyscall++);
|
|
195
|
+
return new Promise((resolve, reject) => {
|
|
196
|
+
current.pending.set(id, { resolve, reject });
|
|
197
|
+
current.stream.write({ syscall: { id, ...call } });
|
|
198
|
+
});
|
|
199
|
+
},
|
|
200
|
+
invalidate(topic, key) {
|
|
201
|
+
const current = link;
|
|
202
|
+
if (current && !current.closed)
|
|
203
|
+
current.stream.write({ invalidate: { topic, key } });
|
|
204
|
+
},
|
|
205
|
+
};
|
|
206
|
+
const acquire = () => {
|
|
207
|
+
if (active < maxConcurrency) {
|
|
208
|
+
active++;
|
|
209
|
+
return Promise.resolve();
|
|
210
|
+
}
|
|
211
|
+
return new Promise((resolve) => waiting.push(() => (active++, resolve())));
|
|
212
|
+
};
|
|
213
|
+
const busy = () => active > 0 || waiting.length > 0 || runs.size > 0;
|
|
214
|
+
const release = () => {
|
|
215
|
+
active--;
|
|
216
|
+
waiting.shift()?.();
|
|
217
|
+
if (!busy())
|
|
218
|
+
idle?.();
|
|
219
|
+
};
|
|
220
|
+
const onRequest = async (current, req) => {
|
|
221
|
+
let resp;
|
|
222
|
+
if (stopping) {
|
|
223
|
+
resp = errorResponse(unavailable("the plugin is stopping"));
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
await acquire();
|
|
227
|
+
try {
|
|
228
|
+
resp = await dispatch(model, routes, session, req, current.ended.signal);
|
|
229
|
+
}
|
|
230
|
+
finally {
|
|
231
|
+
release();
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
if (!current.closed)
|
|
235
|
+
current.stream.write({ response: { id: req.id, ...resp } });
|
|
236
|
+
};
|
|
237
|
+
const send = (current, msg) => {
|
|
238
|
+
if (!current.closed)
|
|
239
|
+
current.stream.write(msg);
|
|
240
|
+
};
|
|
241
|
+
const onJobRun = async (current, run) => {
|
|
242
|
+
const runId = String(run.runId);
|
|
243
|
+
const attempt = Number(run.attempt ?? 0);
|
|
244
|
+
const done = (outcome) => send(current, { jobDone: { runId, attempt, ...outcome } });
|
|
245
|
+
const refuse = (code, message) => done({ error: { code, message, permanent: false } });
|
|
246
|
+
if (stopping)
|
|
247
|
+
return refuse("shutdown", "the plugin is stopping");
|
|
248
|
+
const job = jobs.get(run.job);
|
|
249
|
+
if (!job)
|
|
250
|
+
return refuse("unavailable", `this version has no job ${run.job}`);
|
|
251
|
+
if (runs.size >= jobSlots || runs.has(runId))
|
|
252
|
+
return refuse("unavailable", `all ${jobSlots} job slots are taken`);
|
|
253
|
+
const stop = new AbortController();
|
|
254
|
+
runs.set(runId, { stop, link: current });
|
|
255
|
+
const requestId = String(run.requestId ?? "0");
|
|
256
|
+
const timeoutMs = Number(run.timeoutMs ?? 0);
|
|
257
|
+
const aborted = new Promise((_, reject) => {
|
|
258
|
+
stop.signal.addEventListener("abort", () => reject(stop.signal.reason), { once: true });
|
|
259
|
+
});
|
|
260
|
+
aborted.catch(() => { });
|
|
261
|
+
const stderr = log.with({ job: job.name, run_id: runId, attempt });
|
|
262
|
+
const ctxLog = runLogger(stderr, (level, message) => send(current, { pluginLog: { requestId, level: LEVELS[level], message, atUnixMs: String(Date.now()) } }));
|
|
263
|
+
const maxAttempts = Number(run.maxAttempts ?? 0);
|
|
264
|
+
const ctx = new Ctx(session, new Caller(run.principal, model.id, model.resolved.roleName), { id: requestId, deadline: new Date(Date.now() + timeoutMs), signal: stop.signal }, { id: model.id, version: model.version }, ctxLog, "0", "", Object.freeze({
|
|
265
|
+
job: job.name,
|
|
266
|
+
runId,
|
|
267
|
+
attempt,
|
|
268
|
+
maxAttempts,
|
|
269
|
+
scheduledAt: new Date(Number(run.scheduledAtUnixMs ?? 0)),
|
|
270
|
+
isRun: true,
|
|
271
|
+
isLastAttempt: attempt >= maxAttempts,
|
|
272
|
+
}));
|
|
273
|
+
const started = Date.now();
|
|
274
|
+
let outcome;
|
|
275
|
+
try {
|
|
276
|
+
let json;
|
|
277
|
+
try {
|
|
278
|
+
json = run.payload ? JSON.parse(run.payload) : {};
|
|
279
|
+
}
|
|
280
|
+
catch (e) {
|
|
281
|
+
throw new RpcError(400, "payload_invalid", `the payload is not JSON: ${e.message}`);
|
|
282
|
+
}
|
|
283
|
+
const errors = {};
|
|
284
|
+
const payload = decodeMessage(job.payload, json, "", errors);
|
|
285
|
+
const problems = Object.entries(errors).map(([p, m]) => `${p}: ${m}`);
|
|
286
|
+
if (problems.length > 0) {
|
|
287
|
+
throw new RpcError(400, "payload_invalid", `the payload is not a ${job.payload.name}: ${problems.join("; ")}`);
|
|
288
|
+
}
|
|
289
|
+
await Promise.race([Promise.resolve().then(() => job.handler(ctx, payload)), aborted]);
|
|
290
|
+
outcome = { ok: {} };
|
|
291
|
+
log.info("run done", { job: job.name, run_id: runId, attempt, elapsed_ms: Date.now() - started });
|
|
292
|
+
}
|
|
293
|
+
catch (e) {
|
|
294
|
+
const error = stop.signal.aborted && stop.signal.reason instanceof RunStopped ? jobError(stop.signal.reason) : jobError(e);
|
|
295
|
+
outcome = { error };
|
|
296
|
+
log.warn("run failed", { job: job.name, run_id: runId, attempt, elapsed_ms: Date.now() - started, ...error });
|
|
297
|
+
}
|
|
298
|
+
finally {
|
|
299
|
+
runs.delete(runId);
|
|
300
|
+
}
|
|
301
|
+
done(outcome);
|
|
302
|
+
if (!busy())
|
|
303
|
+
idle?.();
|
|
304
|
+
};
|
|
305
|
+
const close = (current, why) => {
|
|
306
|
+
if (current.closed)
|
|
307
|
+
return;
|
|
308
|
+
current.closed = true;
|
|
309
|
+
for (const p of current.pending.values())
|
|
310
|
+
p.reject(new SyscallError("the session to the platform ended"));
|
|
311
|
+
current.pending.clear();
|
|
312
|
+
current.ended.abort();
|
|
313
|
+
// the runs of this session stop: their answers could not be delivered
|
|
314
|
+
for (const run of runs.values())
|
|
315
|
+
if (run.link === current)
|
|
316
|
+
run.stop.abort(new RunStopped("cancelled"));
|
|
317
|
+
if (link === current)
|
|
318
|
+
link = undefined;
|
|
319
|
+
if (stopped)
|
|
320
|
+
return;
|
|
321
|
+
log.warn("session ended; reconnecting", { reason: why, retry_ms: backoff });
|
|
322
|
+
setTimeout(start, backoff);
|
|
323
|
+
backoff = Math.min(backoff * 2, 5_000);
|
|
324
|
+
};
|
|
325
|
+
function start() {
|
|
326
|
+
if (stopped)
|
|
327
|
+
return;
|
|
328
|
+
const stream = client.session();
|
|
329
|
+
const current = { stream, pending: new Map(), closed: false, ended: new AbortController() };
|
|
330
|
+
link = current;
|
|
331
|
+
stream.on("data", (msg) => {
|
|
332
|
+
switch (msg.kind) {
|
|
333
|
+
case "registered":
|
|
334
|
+
backoff = 250;
|
|
335
|
+
log.info("registered", { instance_id: msg.registered.instanceId, version: model.version });
|
|
336
|
+
break;
|
|
337
|
+
case "request":
|
|
338
|
+
void onRequest(current, msg.request);
|
|
339
|
+
break;
|
|
340
|
+
case "jobRun":
|
|
341
|
+
void onJobRun(current, msg.jobRun);
|
|
342
|
+
break;
|
|
343
|
+
case "jobAbort": {
|
|
344
|
+
const reason = String(msg.jobAbort.reason ?? "");
|
|
345
|
+
runs.get(String(msg.jobAbort.runId))?.stop.abort(new RunStopped(ABORT_CODES[reason] ?? "cancelled"));
|
|
346
|
+
break;
|
|
347
|
+
}
|
|
348
|
+
case "syscallResult": {
|
|
349
|
+
const r = msg.syscallResult;
|
|
350
|
+
const p = current.pending.get(String(r.id));
|
|
351
|
+
if (!p)
|
|
352
|
+
break;
|
|
353
|
+
current.pending.delete(String(r.id));
|
|
354
|
+
if (r.error)
|
|
355
|
+
p.reject(new SyscallError(r.error, r.errorDetails));
|
|
356
|
+
else
|
|
357
|
+
p.resolve(r);
|
|
358
|
+
break;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
});
|
|
362
|
+
stream.on("error", (e) => close(current, e.message));
|
|
363
|
+
stream.on("end", () => close(current, "end"));
|
|
364
|
+
stream.write({
|
|
365
|
+
register: {
|
|
366
|
+
plugin: model.id,
|
|
367
|
+
version: model.version,
|
|
368
|
+
runtime: "node",
|
|
369
|
+
depends: model.depends,
|
|
370
|
+
manifestDigest: "",
|
|
371
|
+
token: process.env.CELOBUS_PLUGIN_TOKEN ?? "",
|
|
372
|
+
jobSlots,
|
|
373
|
+
},
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
const stop = async (exit) => {
|
|
377
|
+
if (stopping)
|
|
378
|
+
return;
|
|
379
|
+
stopping = true;
|
|
380
|
+
log.info("stopping", { in_flight: active + waiting.length, runs: runs.size });
|
|
381
|
+
if (busy()) {
|
|
382
|
+
await Promise.race([
|
|
383
|
+
new Promise((resolve) => (idle = resolve)),
|
|
384
|
+
new Promise((resolve) => setTimeout(resolve, drainMs).unref()),
|
|
385
|
+
]);
|
|
386
|
+
}
|
|
387
|
+
stopped = true;
|
|
388
|
+
if (link) {
|
|
389
|
+
const current = link;
|
|
390
|
+
current.stream.end();
|
|
391
|
+
close(current, "stopped");
|
|
392
|
+
}
|
|
393
|
+
client.close();
|
|
394
|
+
finish();
|
|
395
|
+
if (exit)
|
|
396
|
+
process.exit(0);
|
|
397
|
+
};
|
|
398
|
+
if (options.signals ?? true) {
|
|
399
|
+
process.once("SIGTERM", () => void stop(true));
|
|
400
|
+
process.once("SIGINT", () => void stop(true));
|
|
401
|
+
}
|
|
402
|
+
if (options.stop) {
|
|
403
|
+
if (options.stop.aborted)
|
|
404
|
+
void stop(false);
|
|
405
|
+
else
|
|
406
|
+
options.stop.addEventListener("abort", () => void stop(false), { once: true });
|
|
407
|
+
}
|
|
408
|
+
log.info("connecting", { addr, plugin: model.id, version: model.version });
|
|
409
|
+
start();
|
|
410
|
+
return done;
|
|
411
|
+
}
|
package/dist/sql.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One row of `ctx.sql`: its columns by the names SQL gives them (`owner_id`, `notes` for
|
|
3
|
+
* `select owner_id, count(*) as notes ..`). A bigint is a JSON number (exact up to 2^53), a timestamp its text.
|
|
4
|
+
*/
|
|
5
|
+
export type SqlRow = Record<string, unknown>;
|
|
6
|
+
/**
|
|
7
|
+
* The values of `$1`, `$2`, … as the JSON array the platform reads: a `bigint` is its decimal text and a `Date` its ISO
|
|
8
|
+
* text (a value carries no type of its own, so `'42'` and `42` are the same),
|
|
9
|
+
* `undefined` is null, an object or array its JSON.
|
|
10
|
+
*/
|
|
11
|
+
export declare function sqlParams(params: readonly unknown[]): Buffer;
|
|
12
|
+
/** The rows of a `DbRows` answer, each a JSON object. */
|
|
13
|
+
export declare function sqlRows(rows: readonly (Buffer | Uint8Array)[]): SqlRow[];
|
package/dist/sql.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// SQL sent as text (`ctx.sql`, `ctx.unsafeSql`): the query with positional parameters
|
|
2
|
+
// (`$1` is the first value), its values as a JSON array, the rows back as JSON objects keyed by column name.
|
|
3
|
+
import { internal } from "./errors.js";
|
|
4
|
+
/**
|
|
5
|
+
* The values of `$1`, `$2`, … as the JSON array the platform reads: a `bigint` is its decimal text and a `Date` its ISO
|
|
6
|
+
* text (a value carries no type of its own, so `'42'` and `42` are the same),
|
|
7
|
+
* `undefined` is null, an object or array its JSON.
|
|
8
|
+
*/
|
|
9
|
+
export function sqlParams(params) {
|
|
10
|
+
try {
|
|
11
|
+
const text = JSON.stringify(params.map((v) => (v === undefined ? null : v)), (_key, v) => typeof v === "bigint" ? v.toString() : v);
|
|
12
|
+
return Buffer.from(text, "utf8");
|
|
13
|
+
}
|
|
14
|
+
catch (e) {
|
|
15
|
+
throw internal(new Error(`ctx.sql(): a parameter is not JSON: ${e.message}`));
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
/** The rows of a `DbRows` answer, each a JSON object. */
|
|
19
|
+
export function sqlRows(rows) {
|
|
20
|
+
return rows.map((raw) => {
|
|
21
|
+
let row;
|
|
22
|
+
try {
|
|
23
|
+
row = JSON.parse(Buffer.from(raw).toString("utf8"));
|
|
24
|
+
}
|
|
25
|
+
catch (e) {
|
|
26
|
+
throw internal(new Error(`the platform sent a row that is not JSON: ${e.message}`));
|
|
27
|
+
}
|
|
28
|
+
if (row === null || typeof row !== "object" || Array.isArray(row)) {
|
|
29
|
+
throw internal(new Error("the platform sent a row that is not a JSON object"));
|
|
30
|
+
}
|
|
31
|
+
return row;
|
|
32
|
+
});
|
|
33
|
+
}
|