@azlib/scheduler 1.0.4 → 1.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/Dockerfile +14 -0
- package/README.md +40 -0
- package/compose.yaml +28 -0
- package/dist/cli-config-7F2U8qPM.mjs +462 -0
- package/dist/cli-config-7F2U8qPM.mjs.map +1 -0
- package/dist/cli-config-Cz8QP_4I.cjs +512 -0
- package/dist/dashboard/cli.cjs +30 -0
- package/dist/dashboard/cli.d.cts +6 -0
- package/dist/dashboard/cli.d.cts.map +1 -0
- package/dist/dashboard/cli.d.mts +6 -0
- package/dist/dashboard/cli.d.mts.map +1 -0
- package/dist/dashboard/cli.mjs +31 -0
- package/dist/dashboard/cli.mjs.map +1 -0
- package/dist/dashboard/public/assets/index-CbmpG6_w.css +2 -0
- package/dist/dashboard/public/assets/index-eWhp41cY.js +12 -0
- package/dist/dashboard/public/index.html +13 -0
- package/dist/dashboard-types-BHjRLcxJ.d.cts +127 -0
- package/dist/dashboard-types-BHjRLcxJ.d.cts.map +1 -0
- package/dist/dashboard-types-BHjRLcxJ.d.mts +127 -0
- package/dist/dashboard-types-BHjRLcxJ.d.mts.map +1 -0
- package/dist/dashboard.cjs +9 -0
- package/dist/dashboard.d.cts +20 -0
- package/dist/dashboard.d.cts.map +1 -0
- package/dist/dashboard.d.mts +20 -0
- package/dist/dashboard.d.mts.map +1 -0
- package/dist/dashboard.mjs +4 -0
- package/dist/http-server-DN6Lg464.d.cts +19 -0
- package/dist/http-server-DN6Lg464.d.cts.map +1 -0
- package/dist/http-server-DPQ9HIK-.d.mts +19 -0
- package/dist/http-server-DPQ9HIK-.d.mts.map +1 -0
- package/dist/index.cjs +49 -442
- package/dist/index.d.cts +3 -158
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +3 -158
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +44 -439
- package/dist/index.mjs.map +1 -1
- package/dist/queue-dashboard-service-BFXcnfUr.cjs +55 -0
- package/dist/queue-dashboard-service-C0QapgOg.mjs +52 -0
- package/dist/queue-dashboard-service-C0QapgOg.mjs.map +1 -0
- package/dist/store-dashboard-C44d7sdh.d.cts +59 -0
- package/dist/store-dashboard-C44d7sdh.d.cts.map +1 -0
- package/dist/store-dashboard-Cyf_JILa.mjs +515 -0
- package/dist/store-dashboard-Cyf_JILa.mjs.map +1 -0
- package/dist/store-dashboard-D-yIxXxL.d.mts +59 -0
- package/dist/store-dashboard-D-yIxXxL.d.mts.map +1 -0
- package/dist/store-dashboard-Sg4BBWiJ.cjs +554 -0
- package/package.json +56 -8
|
@@ -0,0 +1,512 @@
|
|
|
1
|
+
//#region \0rolldown/runtime.js
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
10
|
+
key = keys[i];
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
12
|
+
get: ((k) => from[k]).bind(null, key),
|
|
13
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
+
value: mod,
|
|
20
|
+
enumerable: true
|
|
21
|
+
}) : target, mod));
|
|
22
|
+
//#endregion
|
|
23
|
+
const require_store_dashboard = require("./store-dashboard-Sg4BBWiJ.cjs");
|
|
24
|
+
let _azlib_std = require("@azlib/std");
|
|
25
|
+
let node_fs = require("node:fs");
|
|
26
|
+
let node_http = require("node:http");
|
|
27
|
+
node_http = __toESM(node_http, 1);
|
|
28
|
+
let node_path = require("node:path");
|
|
29
|
+
let node_url = require("node:url");
|
|
30
|
+
let node_async_hooks = require("node:async_hooks");
|
|
31
|
+
let _azlib_persistence = require("@azlib/persistence");
|
|
32
|
+
//#region src/dashboard/http-auth.ts
|
|
33
|
+
var DashboardHttpError = class extends Error {
|
|
34
|
+
status;
|
|
35
|
+
constructor(status, message) {
|
|
36
|
+
super(message);
|
|
37
|
+
this.name = "DashboardHttpError";
|
|
38
|
+
this.status = status;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
function isLoopbackHost(host) {
|
|
42
|
+
const normalized = host.trim().toLowerCase();
|
|
43
|
+
return normalized === "127.0.0.1" || normalized === "localhost" || normalized === "::1" || normalized === "[::1]";
|
|
44
|
+
}
|
|
45
|
+
function authorizeDashboardRequest(input) {
|
|
46
|
+
if (!input.token) return;
|
|
47
|
+
if ((Array.isArray(input.authorization) ? input.authorization[0] : input.authorization) !== `Bearer ${input.token}`) throw new DashboardHttpError(401, "Unauthorized");
|
|
48
|
+
}
|
|
49
|
+
function assertDashboardBindSafety(input) {
|
|
50
|
+
if (!isLoopbackHost(input.host) && !input.token) throw new Error("DASHBOARD_TOKEN is required when HOST is not a loopback address");
|
|
51
|
+
}
|
|
52
|
+
//#endregion
|
|
53
|
+
//#region src/dashboard/http-parse.ts
|
|
54
|
+
const DEFAULT_TIMEZONE = "UTC";
|
|
55
|
+
function requiredText(value, field) {
|
|
56
|
+
const text = (0, _azlib_std.asString)(value)?.trim();
|
|
57
|
+
if (!text) throw new DashboardHttpError(400, `${field} is required.`);
|
|
58
|
+
return text;
|
|
59
|
+
}
|
|
60
|
+
function parseConfig(value) {
|
|
61
|
+
if (value === void 0 || value === null || value === "") return {};
|
|
62
|
+
if (typeof value === "string") try {
|
|
63
|
+
return JSON.parse(value);
|
|
64
|
+
} catch {
|
|
65
|
+
throw new DashboardHttpError(400, "config must be valid JSON.");
|
|
66
|
+
}
|
|
67
|
+
return value;
|
|
68
|
+
}
|
|
69
|
+
function parseScheduleFromRecord(record) {
|
|
70
|
+
const nested = (0, _azlib_std.asRecord)(record.schedule);
|
|
71
|
+
const scheduleType = (0, _azlib_std.asString)(record.scheduleType)?.trim() || (0, _azlib_std.asString)(nested?.scheduleType)?.trim() || "cron";
|
|
72
|
+
if (scheduleType !== "cron" && scheduleType !== "once") throw new DashboardHttpError(400, "scheduleType must be cron or once.");
|
|
73
|
+
const expression = requiredText(record.expression ?? nested?.expression, "expression");
|
|
74
|
+
const timezone = (0, _azlib_std.asString)(record.timezone)?.trim() || (0, _azlib_std.asString)(nested?.timezone)?.trim() || DEFAULT_TIMEZONE;
|
|
75
|
+
require_store_dashboard.parseSchedule(scheduleType, expression, timezone);
|
|
76
|
+
return {
|
|
77
|
+
scheduleType,
|
|
78
|
+
expression,
|
|
79
|
+
timezone
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
function parseCreateJobBody(body) {
|
|
83
|
+
const record = (0, _azlib_std.asRecord)(body);
|
|
84
|
+
if (!record) throw new DashboardHttpError(400, "Request body must be a JSON object.");
|
|
85
|
+
return {
|
|
86
|
+
name: requiredText(record.name, "name"),
|
|
87
|
+
handlerKey: requiredText(record.handlerKey, "handlerKey"),
|
|
88
|
+
schedule: parseScheduleFromRecord(record),
|
|
89
|
+
config: parseConfig(record.config),
|
|
90
|
+
enabled: (0, _azlib_std.asBoolean)(record.enabled) ?? true
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
function parseUpdateJobBody(body) {
|
|
94
|
+
const record = (0, _azlib_std.asRecord)(body);
|
|
95
|
+
if (!record) throw new DashboardHttpError(400, "Request body must be a JSON object.");
|
|
96
|
+
const patch = {};
|
|
97
|
+
if (record.name !== void 0) patch.name = requiredText(record.name, "name");
|
|
98
|
+
if (record.handlerKey !== void 0) patch.handlerKey = requiredText(record.handlerKey, "handlerKey");
|
|
99
|
+
if (record.expression !== void 0 || record.timezone !== void 0 || record.scheduleType !== void 0 || record.schedule !== void 0) patch.schedule = parseScheduleFromRecord(record);
|
|
100
|
+
if (record.config !== void 0) patch.config = parseConfig(record.config);
|
|
101
|
+
if (record.enabled !== void 0) patch.enabled = (0, _azlib_std.asBoolean)(record.enabled) ?? true;
|
|
102
|
+
return patch;
|
|
103
|
+
}
|
|
104
|
+
//#endregion
|
|
105
|
+
//#region src/dashboard/public-dir.ts
|
|
106
|
+
function resolveDashboardPublicDir(fromFileUrl) {
|
|
107
|
+
const dir = (0, node_path.dirname)((0, node_url.fileURLToPath)(fromFileUrl));
|
|
108
|
+
const nested = (0, node_path.join)(dir, "public");
|
|
109
|
+
const sibling = (0, node_path.join)(dir, "dashboard", "public");
|
|
110
|
+
if ((0, node_fs.existsSync)((0, node_path.join)(nested, "index.html"))) return nested;
|
|
111
|
+
if ((0, node_fs.existsSync)((0, node_path.join)(sibling, "index.html"))) return sibling;
|
|
112
|
+
return nested;
|
|
113
|
+
}
|
|
114
|
+
//#endregion
|
|
115
|
+
//#region src/dashboard/http-server.ts
|
|
116
|
+
const MIME_TYPES = {
|
|
117
|
+
".css": "text/css; charset=utf-8",
|
|
118
|
+
".html": "text/html; charset=utf-8",
|
|
119
|
+
".js": "text/javascript; charset=utf-8",
|
|
120
|
+
".json": "application/json; charset=utf-8",
|
|
121
|
+
".map": "application/json; charset=utf-8",
|
|
122
|
+
".svg": "image/svg+xml",
|
|
123
|
+
".woff2": "font/woff2"
|
|
124
|
+
};
|
|
125
|
+
function sendJson(res, status, body) {
|
|
126
|
+
res.writeHead(status, { "Content-Type": "application/json; charset=utf-8" });
|
|
127
|
+
res.end(JSON.stringify(body));
|
|
128
|
+
}
|
|
129
|
+
function sendError(res, error) {
|
|
130
|
+
if (error instanceof DashboardHttpError) {
|
|
131
|
+
sendJson(res, error.status, { error: error.message });
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
const message = error instanceof Error ? error.message : "Unknown error";
|
|
135
|
+
sendJson(res, message.includes("not found") ? 404 : message.includes("Only failed") ? 400 : message.includes("Unknown handlerKey") ? 400 : 500, { error: message });
|
|
136
|
+
}
|
|
137
|
+
async function readJsonBody(req) {
|
|
138
|
+
const chunks = [];
|
|
139
|
+
for await (const chunk of req) chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
140
|
+
if (chunks.length === 0) return {};
|
|
141
|
+
const raw = Buffer.concat(chunks).toString("utf8");
|
|
142
|
+
if (!raw.trim()) return {};
|
|
143
|
+
try {
|
|
144
|
+
return JSON.parse(raw);
|
|
145
|
+
} catch {
|
|
146
|
+
throw new DashboardHttpError(400, "Request body must be valid JSON.");
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
function matchPath(pathname, pattern) {
|
|
150
|
+
const pathParts = pathname.split("/").filter(Boolean);
|
|
151
|
+
const patternParts = pattern.split("/").filter(Boolean);
|
|
152
|
+
if (pathParts.length !== patternParts.length) return null;
|
|
153
|
+
const params = {};
|
|
154
|
+
for (let i = 0; i < patternParts.length; i += 1) {
|
|
155
|
+
const expected = patternParts[i] ?? "";
|
|
156
|
+
const actual = pathParts[i] ?? "";
|
|
157
|
+
if (expected.startsWith(":")) {
|
|
158
|
+
params[expected.slice(1)] = decodeURIComponent(actual);
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
if (expected !== actual) return null;
|
|
162
|
+
}
|
|
163
|
+
return params;
|
|
164
|
+
}
|
|
165
|
+
function servePublicFile(publicDir, urlPath, res) {
|
|
166
|
+
if (!(0, node_fs.existsSync)(publicDir)) return false;
|
|
167
|
+
const pathname = decodeURIComponent(urlPath.split("?")[0] ?? "/");
|
|
168
|
+
const abs = (0, node_path.resolve)(publicDir, `.${pathname === "/" ? "/index.html" : pathname}`);
|
|
169
|
+
const rel = (0, node_path.relative)((0, node_path.resolve)(publicDir), abs);
|
|
170
|
+
if (rel.startsWith("..") || rel.includes(`..${node_path.sep}`)) return false;
|
|
171
|
+
const filePath = (0, node_fs.existsSync)(abs) && (0, node_fs.statSync)(abs).isFile() ? abs : (0, node_path.join)(publicDir, "index.html");
|
|
172
|
+
if (!(0, node_fs.existsSync)(filePath) || !(0, node_fs.statSync)(filePath).isFile()) return false;
|
|
173
|
+
const type = MIME_TYPES[(0, node_path.extname)(filePath)] ?? "application/octet-stream";
|
|
174
|
+
res.writeHead(200, { "Content-Type": type });
|
|
175
|
+
(0, node_fs.createReadStream)(filePath).pipe(res);
|
|
176
|
+
return true;
|
|
177
|
+
}
|
|
178
|
+
function createSchedulerDashboardServer(options) {
|
|
179
|
+
const port = options.port ?? 9100;
|
|
180
|
+
const host = options.host ?? "127.0.0.1";
|
|
181
|
+
const publicDir = options.publicDir ?? resolveDashboardPublicDir(require("url").pathToFileURL(__filename).href);
|
|
182
|
+
const dashboard = options.dashboard;
|
|
183
|
+
const server = node_http.createServer((req, res) => {
|
|
184
|
+
handleRequest(req, res).catch((error) => {
|
|
185
|
+
if (!res.headersSent) sendError(res, error);
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
async function handleRequest(req, res) {
|
|
189
|
+
const method = req.method ?? "GET";
|
|
190
|
+
const pathname = new URL(req.url ?? "/", `http://${host}:${port}`).pathname;
|
|
191
|
+
if (method === "GET" && pathname === "/health") {
|
|
192
|
+
res.writeHead(200, { "Content-Type": "text/plain; charset=utf-8" });
|
|
193
|
+
res.end("ok");
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
if (pathname === "/api" || pathname.startsWith("/api/")) {
|
|
197
|
+
authorizeDashboardRequest({
|
|
198
|
+
token: options.token,
|
|
199
|
+
authorization: req.headers.authorization
|
|
200
|
+
});
|
|
201
|
+
await handleApi(method, pathname, req, res);
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
if (method === "GET" && servePublicFile(publicDir, pathname, res)) return;
|
|
205
|
+
res.writeHead(404, { "Content-Type": "text/plain; charset=utf-8" });
|
|
206
|
+
res.end("Not Found");
|
|
207
|
+
}
|
|
208
|
+
async function handleApi(method, pathname, req, res) {
|
|
209
|
+
try {
|
|
210
|
+
if (method === "GET" && pathname === "/api/health") {
|
|
211
|
+
sendJson(res, 200, await dashboard.getHealth());
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
if (method === "GET" && pathname === "/api/handlers") {
|
|
215
|
+
sendJson(res, 200, { handlers: dashboard.listHandlerKeys() });
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
if (method === "GET" && pathname === "/api/jobs") {
|
|
219
|
+
sendJson(res, 200, { items: await dashboard.listItems() });
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
if (method === "POST" && pathname === "/api/jobs") {
|
|
223
|
+
sendJson(res, 201, await dashboard.createJob(parseCreateJobBody(await readJsonBody(req))));
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
const jobExec = matchPath(pathname, "/api/jobs/:jobId/executions");
|
|
227
|
+
if (jobExec && method === "GET") {
|
|
228
|
+
sendJson(res, 200, { executions: await dashboard.listExecutions(jobExec.jobId ?? "") });
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
const jobAction = matchPath(pathname, "/api/jobs/:jobId/:action");
|
|
232
|
+
if (jobAction && method === "POST") {
|
|
233
|
+
const jobId = jobAction.jobId ?? "";
|
|
234
|
+
if (jobAction.action === "pause") {
|
|
235
|
+
await dashboard.pauseJob(jobId);
|
|
236
|
+
res.writeHead(204);
|
|
237
|
+
res.end();
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
if (jobAction.action === "resume") {
|
|
241
|
+
await dashboard.resumeJob(jobId);
|
|
242
|
+
res.writeHead(204);
|
|
243
|
+
res.end();
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
if (jobAction.action === "run") {
|
|
247
|
+
await dashboard.runJob(jobId);
|
|
248
|
+
res.writeHead(204);
|
|
249
|
+
res.end();
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
const retry = matchPath(pathname, "/api/executions/:executionId/retry");
|
|
254
|
+
if (retry && method === "POST") {
|
|
255
|
+
await dashboard.retryExecution(retry.executionId ?? "");
|
|
256
|
+
res.writeHead(204);
|
|
257
|
+
res.end();
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
const job = matchPath(pathname, "/api/jobs/:jobId");
|
|
261
|
+
if (job) {
|
|
262
|
+
const jobId = job.jobId ?? "";
|
|
263
|
+
if (method === "GET") {
|
|
264
|
+
const item = await dashboard.getJob(jobId);
|
|
265
|
+
if (!item) throw new DashboardHttpError(404, `Job not found: ${jobId}`);
|
|
266
|
+
sendJson(res, 200, item);
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
if (method === "PATCH") {
|
|
270
|
+
await dashboard.updateJob(jobId, parseUpdateJobBody(await readJsonBody(req)));
|
|
271
|
+
res.writeHead(204);
|
|
272
|
+
res.end();
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
if (method === "DELETE") {
|
|
276
|
+
await dashboard.deleteJob(jobId);
|
|
277
|
+
res.writeHead(204);
|
|
278
|
+
res.end();
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
sendJson(res, 404, { error: "Not Found" });
|
|
283
|
+
} catch (error) {
|
|
284
|
+
sendError(res, error);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
return {
|
|
288
|
+
get port() {
|
|
289
|
+
return port;
|
|
290
|
+
},
|
|
291
|
+
get host() {
|
|
292
|
+
return host;
|
|
293
|
+
},
|
|
294
|
+
listen() {
|
|
295
|
+
return new Promise((resolveListen, reject) => {
|
|
296
|
+
server.once("error", reject);
|
|
297
|
+
server.listen(port, host, () => {
|
|
298
|
+
server.off("error", reject);
|
|
299
|
+
resolveListen();
|
|
300
|
+
});
|
|
301
|
+
});
|
|
302
|
+
},
|
|
303
|
+
close() {
|
|
304
|
+
return new Promise((resolveClose, reject) => {
|
|
305
|
+
server.close((err) => err ? reject(err) : resolveClose());
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
//#endregion
|
|
311
|
+
//#region src/dashboard/sql-drivers.ts
|
|
312
|
+
function rewriteQuestionParams(sql, style) {
|
|
313
|
+
if (style === "?") return sql;
|
|
314
|
+
let index = 0;
|
|
315
|
+
return sql.replaceAll("?", () => {
|
|
316
|
+
index += 1;
|
|
317
|
+
return style === "$n" ? `$${index}` : `@p${index}`;
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
function inferDashboardDialect(databaseUrl) {
|
|
321
|
+
const scheme = databaseUrl.split(":")[0]?.toLowerCase() ?? "";
|
|
322
|
+
if (scheme === "postgres" || scheme === "postgresql") return "postgres";
|
|
323
|
+
if (scheme === "mysql" || scheme === "mariadb") return "mysql";
|
|
324
|
+
if (scheme === "mssql" || scheme === "sqlserver") return "mssql";
|
|
325
|
+
if (scheme === "sqlite" || scheme === "file") return "sqlite";
|
|
326
|
+
return "postgres";
|
|
327
|
+
}
|
|
328
|
+
function dialectAdapterFor(dialect) {
|
|
329
|
+
if (dialect === "mysql") return (0, _azlib_persistence.createMySqlDialectAdapter)();
|
|
330
|
+
if (dialect === "sqlite") return (0, _azlib_persistence.createSqliteDialectAdapter)();
|
|
331
|
+
if (dialect === "mssql") return (0, _azlib_persistence.createSqlServerDialectAdapter)();
|
|
332
|
+
return (0, _azlib_persistence.createPostgresDialectAdapter)();
|
|
333
|
+
}
|
|
334
|
+
function sqlitePathFromUrl(databaseUrl) {
|
|
335
|
+
if (databaseUrl.startsWith("file:")) return databaseUrl.slice(5);
|
|
336
|
+
if (databaseUrl.startsWith("sqlite:")) return databaseUrl.slice(7);
|
|
337
|
+
return databaseUrl;
|
|
338
|
+
}
|
|
339
|
+
function bindMssqlParams(request, params) {
|
|
340
|
+
(params ?? []).forEach((value, index) => {
|
|
341
|
+
request.input(`p${index + 1}`, value);
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
function createPgDriver(pool) {
|
|
345
|
+
const txStore = new node_async_hooks.AsyncLocalStorage();
|
|
346
|
+
const active = () => txStore.getStore() ?? pool;
|
|
347
|
+
const runQuery = async (sql, params) => active().query(rewriteQuestionParams(sql, "$n"), [...params ?? []]);
|
|
348
|
+
return {
|
|
349
|
+
query: async (sql, params) => {
|
|
350
|
+
return (await runQuery(sql, params)).rows;
|
|
351
|
+
},
|
|
352
|
+
execute: async (sql, params) => {
|
|
353
|
+
await runQuery(sql, params);
|
|
354
|
+
},
|
|
355
|
+
transaction: async (run) => {
|
|
356
|
+
if (txStore.getStore()) return run();
|
|
357
|
+
const conn = await pool.connect();
|
|
358
|
+
try {
|
|
359
|
+
await conn.query("BEGIN");
|
|
360
|
+
const result = await txStore.run(conn, run);
|
|
361
|
+
await conn.query("COMMIT");
|
|
362
|
+
return result;
|
|
363
|
+
} catch (error) {
|
|
364
|
+
await conn.query("ROLLBACK");
|
|
365
|
+
throw error;
|
|
366
|
+
} finally {
|
|
367
|
+
conn.release();
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
function createMysqlDriver(pool) {
|
|
373
|
+
const txStore = new node_async_hooks.AsyncLocalStorage();
|
|
374
|
+
const active = () => txStore.getStore() ?? pool;
|
|
375
|
+
return {
|
|
376
|
+
query: async (sql, params) => {
|
|
377
|
+
const [rows] = await active().execute(sql, [...params ?? []]);
|
|
378
|
+
return rows;
|
|
379
|
+
},
|
|
380
|
+
execute: async (sql, params) => {
|
|
381
|
+
await active().execute(sql, [...params ?? []]);
|
|
382
|
+
},
|
|
383
|
+
transaction: async (run) => {
|
|
384
|
+
if (txStore.getStore()) return run();
|
|
385
|
+
const conn = await pool.getConnection();
|
|
386
|
+
try {
|
|
387
|
+
await conn.beginTransaction();
|
|
388
|
+
const result = await txStore.run(conn, run);
|
|
389
|
+
await conn.commit();
|
|
390
|
+
return result;
|
|
391
|
+
} catch (error) {
|
|
392
|
+
await conn.rollback();
|
|
393
|
+
throw error;
|
|
394
|
+
} finally {
|
|
395
|
+
conn.release();
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
async function createDashboardSqlDriver(databaseUrl, dialect) {
|
|
401
|
+
if (dialect === "postgres") {
|
|
402
|
+
const pg = await import("pg");
|
|
403
|
+
const Pool = pg.Pool ?? pg.default?.Pool;
|
|
404
|
+
if (!Pool) throw new Error("The pg package is required for postgres dashboards.");
|
|
405
|
+
return createPgDriver(new Pool({ connectionString: databaseUrl }));
|
|
406
|
+
}
|
|
407
|
+
if (dialect === "mysql") {
|
|
408
|
+
const mysql = await import("mysql2/promise");
|
|
409
|
+
const createPool = mysql.createPool ?? mysql.default?.createPool;
|
|
410
|
+
if (!createPool) throw new Error("The mysql2 package is required for mysql dashboards.");
|
|
411
|
+
return createMysqlDriver(createPool(databaseUrl));
|
|
412
|
+
}
|
|
413
|
+
if (dialect === "sqlite") {
|
|
414
|
+
const Database = (await import("better-sqlite3")).default;
|
|
415
|
+
if (!Database) throw new Error("The better-sqlite3 package is required for sqlite dashboards.");
|
|
416
|
+
const db = new Database(sqlitePathFromUrl(databaseUrl));
|
|
417
|
+
return {
|
|
418
|
+
query: async (sql, params) => db.prepare(sql).all(...params ?? []),
|
|
419
|
+
execute: async (sql, params) => {
|
|
420
|
+
db.prepare(sql).run(...params ?? []);
|
|
421
|
+
},
|
|
422
|
+
transaction: async (run) => run()
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
const mssql = await import("mssql");
|
|
426
|
+
const connect = mssql.connect ?? mssql.default?.connect;
|
|
427
|
+
if (!connect) throw new Error("The mssql package is required for mssql dashboards.");
|
|
428
|
+
const pool = await connect(databaseUrl);
|
|
429
|
+
return {
|
|
430
|
+
query: async (sql, params) => {
|
|
431
|
+
const request = pool.request();
|
|
432
|
+
bindMssqlParams(request, params);
|
|
433
|
+
return (await request.query(rewriteQuestionParams(sql, "@pn"))).recordset;
|
|
434
|
+
},
|
|
435
|
+
execute: async (sql, params) => {
|
|
436
|
+
const request = pool.request();
|
|
437
|
+
bindMssqlParams(request, params);
|
|
438
|
+
await request.query(rewriteQuestionParams(sql, "@pn"));
|
|
439
|
+
},
|
|
440
|
+
transaction: async (run) => run()
|
|
441
|
+
};
|
|
442
|
+
}
|
|
443
|
+
function createDashboardPersistenceConfig(input) {
|
|
444
|
+
return {
|
|
445
|
+
client: (0, _azlib_persistence.createSqlClientAdapter)(input.driver),
|
|
446
|
+
dialect: dialectAdapterFor(input.dialect),
|
|
447
|
+
namespace: input.namespace
|
|
448
|
+
};
|
|
449
|
+
}
|
|
450
|
+
//#endregion
|
|
451
|
+
//#region src/dashboard/cli-config.ts
|
|
452
|
+
function parsePort(value, fallback) {
|
|
453
|
+
if (!value) return fallback;
|
|
454
|
+
const port = Number(value);
|
|
455
|
+
if (!Number.isInteger(port) || port <= 0 || port > 65535) throw new Error(`Invalid PORT: ${value}`);
|
|
456
|
+
return port;
|
|
457
|
+
}
|
|
458
|
+
function parseDialect(value, databaseUrl) {
|
|
459
|
+
const explicit = value?.trim().toLowerCase();
|
|
460
|
+
if (!explicit) return inferDashboardDialect(databaseUrl);
|
|
461
|
+
if (explicit === "postgres" || explicit === "mysql" || explicit === "sqlite" || explicit === "mssql") return explicit;
|
|
462
|
+
throw new Error(`Invalid SCHEDULER_DIALECT: ${value}. Use postgres, mysql, sqlite, or mssql.`);
|
|
463
|
+
}
|
|
464
|
+
function parseDashboardCliEnv(env) {
|
|
465
|
+
const databaseUrl = (0, _azlib_std.asString)(env.DATABASE_URL)?.trim();
|
|
466
|
+
if (!databaseUrl) throw new Error("DATABASE_URL is required");
|
|
467
|
+
const host = (0, _azlib_std.asString)(env.HOST)?.trim() || "127.0.0.1";
|
|
468
|
+
const token = (0, _azlib_std.asString)(env.DASHBOARD_TOKEN)?.trim() || void 0;
|
|
469
|
+
assertDashboardBindSafety({
|
|
470
|
+
host,
|
|
471
|
+
token
|
|
472
|
+
});
|
|
473
|
+
return {
|
|
474
|
+
databaseUrl,
|
|
475
|
+
dialect: parseDialect(env.SCHEDULER_DIALECT, databaseUrl),
|
|
476
|
+
namespace: (0, _azlib_std.asString)(env.SCHEDULER_NAMESPACE)?.trim() || "azlib",
|
|
477
|
+
port: parsePort(env.PORT, 9100),
|
|
478
|
+
host,
|
|
479
|
+
token
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
//#endregion
|
|
483
|
+
Object.defineProperty(exports, "createDashboardPersistenceConfig", {
|
|
484
|
+
enumerable: true,
|
|
485
|
+
get: function() {
|
|
486
|
+
return createDashboardPersistenceConfig;
|
|
487
|
+
}
|
|
488
|
+
});
|
|
489
|
+
Object.defineProperty(exports, "createDashboardSqlDriver", {
|
|
490
|
+
enumerable: true,
|
|
491
|
+
get: function() {
|
|
492
|
+
return createDashboardSqlDriver;
|
|
493
|
+
}
|
|
494
|
+
});
|
|
495
|
+
Object.defineProperty(exports, "createSchedulerDashboardServer", {
|
|
496
|
+
enumerable: true,
|
|
497
|
+
get: function() {
|
|
498
|
+
return createSchedulerDashboardServer;
|
|
499
|
+
}
|
|
500
|
+
});
|
|
501
|
+
Object.defineProperty(exports, "parseDashboardCliEnv", {
|
|
502
|
+
enumerable: true,
|
|
503
|
+
get: function() {
|
|
504
|
+
return parseDashboardCliEnv;
|
|
505
|
+
}
|
|
506
|
+
});
|
|
507
|
+
Object.defineProperty(exports, "resolveDashboardPublicDir", {
|
|
508
|
+
enumerable: true,
|
|
509
|
+
get: function() {
|
|
510
|
+
return resolveDashboardPublicDir;
|
|
511
|
+
}
|
|
512
|
+
});
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const require_cli_config = require("../cli-config-Cz8QP_4I.cjs");
|
|
4
|
+
const require_store_dashboard = require("../store-dashboard-Sg4BBWiJ.cjs");
|
|
5
|
+
let node_url = require("node:url");
|
|
6
|
+
//#region src/dashboard/cli.ts
|
|
7
|
+
async function startSchedulerDashboardCli(env = process.env) {
|
|
8
|
+
const config = require_cli_config.parseDashboardCliEnv(env);
|
|
9
|
+
const server = require_cli_config.createSchedulerDashboardServer({
|
|
10
|
+
dashboard: require_store_dashboard.createSchedulerDashboardFromPersistence(require_cli_config.createDashboardPersistenceConfig({
|
|
11
|
+
driver: await require_cli_config.createDashboardSqlDriver(config.databaseUrl, config.dialect),
|
|
12
|
+
dialect: config.dialect,
|
|
13
|
+
namespace: config.namespace
|
|
14
|
+
})),
|
|
15
|
+
port: config.port,
|
|
16
|
+
host: config.host,
|
|
17
|
+
token: config.token,
|
|
18
|
+
publicDir: require_cli_config.resolveDashboardPublicDir(require("url").pathToFileURL(__filename).href)
|
|
19
|
+
});
|
|
20
|
+
await server.listen();
|
|
21
|
+
process.stderr.write(`@azlib/scheduler dashboard listening on http://${config.host}:${config.port}\n`);
|
|
22
|
+
return server;
|
|
23
|
+
}
|
|
24
|
+
if (Boolean(process.argv[1]) && require("url").pathToFileURL(__filename).href === (0, node_url.pathToFileURL)(process.argv[1]).href) startSchedulerDashboardCli().catch((error) => {
|
|
25
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
26
|
+
process.stderr.write(`${message}\n`);
|
|
27
|
+
process.exitCode = 1;
|
|
28
|
+
});
|
|
29
|
+
//#endregion
|
|
30
|
+
exports.startSchedulerDashboardCli = startSchedulerDashboardCli;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { t as SchedulerDashboardServer } from "../http-server-DN6Lg464.cjs";
|
|
2
|
+
//#region src/dashboard/cli.d.ts
|
|
3
|
+
declare function startSchedulerDashboardCli(env?: Record<string, string | undefined>): Promise<SchedulerDashboardServer>;
|
|
4
|
+
//#endregion
|
|
5
|
+
export { startSchedulerDashboardCli };
|
|
6
|
+
//# sourceMappingURL=cli.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.d.cts","names":[],"sources":["../../src/dashboard/cli.ts"],"mappings":";;iBAasB,2BACpB,MAAK,qCAAgD,QAAhD"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { t as SchedulerDashboardServer } from "../http-server-DPQ9HIK-.mjs";
|
|
2
|
+
//#region src/dashboard/cli.d.ts
|
|
3
|
+
declare function startSchedulerDashboardCli(env?: Record<string, string | undefined>): Promise<SchedulerDashboardServer>;
|
|
4
|
+
//#endregion
|
|
5
|
+
export { startSchedulerDashboardCli };
|
|
6
|
+
//# sourceMappingURL=cli.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.d.mts","names":[],"sources":["../../src/dashboard/cli.ts"],"mappings":";;iBAasB,2BACpB,MAAK,qCAAgD,QAAhD"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { t as createSchedulerDashboardFromPersistence } from "../store-dashboard-Cyf_JILa.mjs";
|
|
3
|
+
import { a as resolveDashboardPublicDir, i as createSchedulerDashboardServer, n as createDashboardPersistenceConfig, r as createDashboardSqlDriver, t as parseDashboardCliEnv } from "../cli-config-7F2U8qPM.mjs";
|
|
4
|
+
import { pathToFileURL } from "node:url";
|
|
5
|
+
//#region src/dashboard/cli.ts
|
|
6
|
+
async function startSchedulerDashboardCli(env = process.env) {
|
|
7
|
+
const config = parseDashboardCliEnv(env);
|
|
8
|
+
const server = createSchedulerDashboardServer({
|
|
9
|
+
dashboard: createSchedulerDashboardFromPersistence(createDashboardPersistenceConfig({
|
|
10
|
+
driver: await createDashboardSqlDriver(config.databaseUrl, config.dialect),
|
|
11
|
+
dialect: config.dialect,
|
|
12
|
+
namespace: config.namespace
|
|
13
|
+
})),
|
|
14
|
+
port: config.port,
|
|
15
|
+
host: config.host,
|
|
16
|
+
token: config.token,
|
|
17
|
+
publicDir: resolveDashboardPublicDir(import.meta.url)
|
|
18
|
+
});
|
|
19
|
+
await server.listen();
|
|
20
|
+
process.stderr.write(`@azlib/scheduler dashboard listening on http://${config.host}:${config.port}\n`);
|
|
21
|
+
return server;
|
|
22
|
+
}
|
|
23
|
+
if (Boolean(process.argv[1]) && import.meta.url === pathToFileURL(process.argv[1]).href) startSchedulerDashboardCli().catch((error) => {
|
|
24
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
25
|
+
process.stderr.write(`${message}\n`);
|
|
26
|
+
process.exitCode = 1;
|
|
27
|
+
});
|
|
28
|
+
//#endregion
|
|
29
|
+
export { startSchedulerDashboardCli };
|
|
30
|
+
|
|
31
|
+
//# sourceMappingURL=cli.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.mjs","names":[],"sources":["../../src/dashboard/cli.ts"],"sourcesContent":["#!/usr/bin/env node\n\nimport { pathToFileURL } from \"node:url\";\n\nimport { parseDashboardCliEnv } from \"./cli-config.js\";\nimport { createSchedulerDashboardServer } from \"./http-server.js\";\nimport { resolveDashboardPublicDir } from \"./public-dir.js\";\nimport {\n createDashboardPersistenceConfig,\n createDashboardSqlDriver,\n} from \"./sql-drivers.js\";\nimport { createSchedulerDashboardFromPersistence } from \"./store-dashboard.js\";\n\nexport async function startSchedulerDashboardCli(\n env: Record<string, string | undefined> = process.env,\n) {\n const config = parseDashboardCliEnv(env);\n const driver = await createDashboardSqlDriver(\n config.databaseUrl,\n config.dialect,\n );\n const persistence = createDashboardPersistenceConfig({\n driver,\n dialect: config.dialect,\n namespace: config.namespace,\n });\n const dashboard = createSchedulerDashboardFromPersistence(persistence);\n const server = createSchedulerDashboardServer({\n dashboard,\n port: config.port,\n host: config.host,\n token: config.token,\n publicDir: resolveDashboardPublicDir(import.meta.url),\n });\n await server.listen();\n process.stderr.write(\n `@azlib/scheduler dashboard listening on http://${config.host}:${config.port}\\n`,\n );\n return server;\n}\n\nconst isDirectRun =\n Boolean(process.argv[1]) &&\n import.meta.url === pathToFileURL(process.argv[1]).href;\nif (isDirectRun) {\n void startSchedulerDashboardCli().catch((error: unknown) => {\n const message = error instanceof Error ? error.message : String(error);\n process.stderr.write(`${message}\\n`);\n process.exitCode = 1;\n });\n}\n"],"mappings":";;;;;AAaA,eAAsB,2BACpB,MAA0C,QAAQ,KAClD;CACA,MAAM,SAAS,qBAAqB,GAAG;CAWvC,MAAM,SAAS,+BAA+B;EAC5C,WAFgB,wCALE,iCAAiC;GACnD,QAAA,MALmB,yBACnB,OAAO,aACP,OAAO,OACT;GAGE,SAAS,OAAO;GAChB,WAAW,OAAO;EACpB,CACoE,CAE1D;EACR,MAAM,OAAO;EACb,MAAM,OAAO;EACb,OAAO,OAAO;EACd,WAAW,0BAA0B,OAAO,KAAK,GAAG;CACtD,CAAC;CACD,MAAM,OAAO,OAAO;CACpB,QAAQ,OAAO,MACb,kDAAkD,OAAO,KAAK,GAAG,OAAO,KAAK,GAC/E;CACA,OAAO;AACT;AAKA,IAFE,QAAQ,QAAQ,KAAK,EAAE,KACvB,OAAO,KAAK,QAAQ,cAAc,QAAQ,KAAK,EAAE,CAAC,CAAC,MAEnD,2BAAgC,CAAC,CAAC,OAAO,UAAmB;CAC1D,MAAM,UAAU,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK;CACrE,QAAQ,OAAO,MAAM,GAAG,QAAQ,GAAG;CACnC,QAAQ,WAAW;AACrB,CAAC"}
|