@gera2ld/common-node 0.0.1 → 0.0.3
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/dist/index.js +362 -404
- package/dist/node/index.js +45 -46
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1,415 +1,373 @@
|
|
|
1
|
-
import { spawn as
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
5
|
-
import { Readable as
|
|
6
|
-
import { buffer as
|
|
7
|
-
import { isatty as
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
import { spawn as e } from "node:child_process";
|
|
2
|
+
import { access as t, mkdir as n, readFile as r, readdir as i, rename as a, rm as o, stat as s, writeFile as c } from "node:fs/promises";
|
|
3
|
+
import { dirname as l, extname as u, join as d, normalize as f, resolve as p } from "node:path";
|
|
4
|
+
import m from "node:process";
|
|
5
|
+
import { Readable as h } from "node:stream";
|
|
6
|
+
import { buffer as g } from "node:stream/consumers";
|
|
7
|
+
import { isatty as _ } from "node:tty";
|
|
8
|
+
//#region ../../node_modules/.pnpm/@gera2ld+common@file+packages+common/node_modules/@gera2ld/common/dist/index.js
|
|
9
|
+
function v(e) {
|
|
10
|
+
return new TextEncoder().encode(e);
|
|
10
11
|
}
|
|
11
|
-
function
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
},
|
|
21
|
-
() => {
|
|
22
|
-
r.status = "rejected";
|
|
23
|
-
}
|
|
24
|
-
), r;
|
|
12
|
+
function y() {
|
|
13
|
+
let e = { status: "pending" };
|
|
14
|
+
return e.promise = new Promise((t, n) => {
|
|
15
|
+
e.resolve = t, e.reject = n;
|
|
16
|
+
}), e.promise.then(() => {
|
|
17
|
+
e.status = "resolved";
|
|
18
|
+
}, () => {
|
|
19
|
+
e.status = "rejected";
|
|
20
|
+
}), e;
|
|
25
21
|
}
|
|
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
|
-
this.requests.shift();
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
function V(r, t) {
|
|
84
|
-
const e = new Uint8Array(r.length + t.length);
|
|
85
|
-
return e.set(r, 0), e.set(t, r.length), e;
|
|
86
|
-
}
|
|
87
|
-
function G(r, t) {
|
|
88
|
-
if (!t.length) return 0;
|
|
89
|
-
for (let e = 0; e <= r.length - t.length; e += 1) {
|
|
90
|
-
let s;
|
|
91
|
-
for (s = 0; s < t.length && r[e + s] === t[s]; s += 1)
|
|
92
|
-
;
|
|
93
|
-
if (s === t.length) return e;
|
|
94
|
-
}
|
|
95
|
-
return -1;
|
|
96
|
-
}
|
|
97
|
-
const Q = {
|
|
98
|
-
cgiDir: R.env.CGI_DIR || "cgi",
|
|
99
|
-
runnerMap: {
|
|
100
|
-
".js": ["node"],
|
|
101
|
-
".sh": ["bash"]
|
|
102
|
-
},
|
|
103
|
-
env: {},
|
|
104
|
-
timeout: 1e4,
|
|
105
|
-
// Do not allow directories or files starting with `.` or `_`
|
|
106
|
-
disallowedPattern: /\/[._]/
|
|
22
|
+
var b = new Uint8Array([13, 10]), x = class {
|
|
23
|
+
buffer = new Uint8Array();
|
|
24
|
+
requests = [];
|
|
25
|
+
isClosed = !1;
|
|
26
|
+
read(e = 8192) {
|
|
27
|
+
let t = y();
|
|
28
|
+
return this.requests.push({
|
|
29
|
+
deferred: t,
|
|
30
|
+
handle: () => {
|
|
31
|
+
let n = Math.min(e, this.buffer.length), r = this.buffer.subarray(0, n);
|
|
32
|
+
return this.buffer = this.buffer.subarray(n), t.resolve(r), !0;
|
|
33
|
+
}
|
|
34
|
+
}), this.processBuffer(), t.promise;
|
|
35
|
+
}
|
|
36
|
+
readExact(e) {
|
|
37
|
+
let t = y();
|
|
38
|
+
return this.requests.push({
|
|
39
|
+
deferred: t,
|
|
40
|
+
handle: () => {
|
|
41
|
+
if (e > this.buffer.length) return !1;
|
|
42
|
+
let n = this.buffer.subarray(0, e);
|
|
43
|
+
return this.buffer = this.buffer.subarray(e), t.resolve(n), !0;
|
|
44
|
+
}
|
|
45
|
+
}), this.processBuffer(), t.promise;
|
|
46
|
+
}
|
|
47
|
+
readUntil(e = b) {
|
|
48
|
+
typeof e == "string" && (e = v(e));
|
|
49
|
+
let t = y();
|
|
50
|
+
return this.requests.push({
|
|
51
|
+
deferred: t,
|
|
52
|
+
handle: () => {
|
|
53
|
+
let n = C(this.buffer, e);
|
|
54
|
+
if (n < 0) return !1;
|
|
55
|
+
let r = n + e.length, i = this.buffer.subarray(0, r);
|
|
56
|
+
return this.buffer = this.buffer.subarray(r), t.resolve(i), !0;
|
|
57
|
+
}
|
|
58
|
+
}), this.processBuffer(), t.promise;
|
|
59
|
+
}
|
|
60
|
+
feed(e) {
|
|
61
|
+
if (this.isClosed) throw Error("Buffer is closed");
|
|
62
|
+
this.buffer = S(this.buffer, e), this.processBuffer();
|
|
63
|
+
}
|
|
64
|
+
close() {
|
|
65
|
+
this.isClosed = !0, this.processBuffer();
|
|
66
|
+
}
|
|
67
|
+
processBuffer() {
|
|
68
|
+
for (; this.requests.length;) {
|
|
69
|
+
let e = this.requests[0];
|
|
70
|
+
if (!e.handle()) if (this.isClosed) e.deferred.reject(/* @__PURE__ */ Error("Buffer is closed"));
|
|
71
|
+
else break;
|
|
72
|
+
this.requests.shift();
|
|
73
|
+
}
|
|
74
|
+
}
|
|
107
75
|
};
|
|
108
|
-
function
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
...Q,
|
|
112
|
-
...r
|
|
113
|
-
};
|
|
114
|
-
if (i.disallowedPattern.test(n))
|
|
115
|
-
return new Response(null, { status: 403 });
|
|
116
|
-
const o = C(i.cgiDir, n);
|
|
117
|
-
try {
|
|
118
|
-
await A(o);
|
|
119
|
-
} catch {
|
|
120
|
-
return new Response(null, { status: 404 });
|
|
121
|
-
}
|
|
122
|
-
const u = i.runnerMap[z(o)];
|
|
123
|
-
try {
|
|
124
|
-
if (!u) throw new Error("Unsupported command");
|
|
125
|
-
t?.log?.(`Run hook: ${n}`);
|
|
126
|
-
const [f, ...w] = [...u, o], c = D(f, w, {
|
|
127
|
-
cwd: g(o),
|
|
128
|
-
env: {
|
|
129
|
-
...R.env,
|
|
130
|
-
...i.env,
|
|
131
|
-
HTTP_METHOD: s.method,
|
|
132
|
-
HTTP_URL: s.url,
|
|
133
|
-
HTTP_HEADERS: [...s.headers.entries()].map((l) => `${l[0]}: ${l[1]}`).join(`
|
|
134
|
-
`)
|
|
135
|
-
},
|
|
136
|
-
stdio: ["pipe", "pipe", "inherit"],
|
|
137
|
-
timeout: i.timeout
|
|
138
|
-
});
|
|
139
|
-
s.body ? E.fromWeb(s.body).pipe(c.stdin) : c.stdin.end();
|
|
140
|
-
let y = !1;
|
|
141
|
-
const h = new J(), N = h.readUntil(`
|
|
142
|
-
|
|
143
|
-
`).finally(() => {
|
|
144
|
-
y = !0;
|
|
145
|
-
});
|
|
146
|
-
for await (const l of c.stdout)
|
|
147
|
-
if (h.feed(l), y) break;
|
|
148
|
-
y || h.close();
|
|
149
|
-
const B = await N, M = JSON.parse(new TextDecoder().decode(B)), { status: m, ...j } = M;
|
|
150
|
-
let p = m ? +m : 200;
|
|
151
|
-
if (isNaN(p) || p < 100 || p > 999)
|
|
152
|
-
throw new Error(`Invalid status code: ${m}`);
|
|
153
|
-
c.exitCode && (console.error("Script exit code:", c.exitCode), p = 500);
|
|
154
|
-
const F = E.toWeb(
|
|
155
|
-
E.from(
|
|
156
|
-
(async function* () {
|
|
157
|
-
for (; ; ) {
|
|
158
|
-
const l = await h.read();
|
|
159
|
-
if (!l.length) break;
|
|
160
|
-
yield l;
|
|
161
|
-
}
|
|
162
|
-
for await (const l of c.stdout)
|
|
163
|
-
yield l;
|
|
164
|
-
})()
|
|
165
|
-
)
|
|
166
|
-
);
|
|
167
|
-
return new Response(F, {
|
|
168
|
-
status: p,
|
|
169
|
-
headers: j
|
|
170
|
-
});
|
|
171
|
-
} catch (f) {
|
|
172
|
-
return t?.log?.(`Error: ${f}`), new Response("Internal server error", {
|
|
173
|
-
status: 500
|
|
174
|
-
});
|
|
175
|
-
}
|
|
176
|
-
};
|
|
177
|
-
}
|
|
178
|
-
class X extends Error {
|
|
179
|
-
constructor(t) {
|
|
180
|
-
super(`Command exit code: ${t.code}`), this.output = t;
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
async function ot(r, t, e) {
|
|
184
|
-
const s = e?.ensureSuccess ?? !0, a = [
|
|
185
|
-
...t?.stdio || ["ignore", "inherit", "inherit"]
|
|
186
|
-
];
|
|
187
|
-
e?.stdin && (a[0] = "pipe"), e?.buffer && (e.buffer !== "stderr" && (a[1] = "pipe"), e.buffer !== "stdout" && (a[2] = "pipe"));
|
|
188
|
-
const n = D(r[0], r.slice(1), {
|
|
189
|
-
...t,
|
|
190
|
-
stdio: a
|
|
191
|
-
});
|
|
192
|
-
if (e?.stdin && n.stdin) {
|
|
193
|
-
const c = typeof e.stdin == "string" ? new TextEncoder().encode(e.stdin) : e.stdin instanceof ArrayBuffer ? new Uint8Array(e.stdin) : e.stdin;
|
|
194
|
-
n.stdin.write(c), n.stdin.end();
|
|
195
|
-
}
|
|
196
|
-
const i = n.stdout && k(n.stdout), o = n.stderr && k(n.stderr), u = await new Promise(
|
|
197
|
-
(c) => n.on("exit", (y, h) => {
|
|
198
|
-
c({ code: y, signal: h });
|
|
199
|
-
})
|
|
200
|
-
), f = !u.code && !u.signal, w = {
|
|
201
|
-
success: f,
|
|
202
|
-
code: u.code,
|
|
203
|
-
signal: u.signal,
|
|
204
|
-
async stdout() {
|
|
205
|
-
if (!i) throw new Error("stdout is not available");
|
|
206
|
-
return i;
|
|
207
|
-
},
|
|
208
|
-
async stderr() {
|
|
209
|
-
if (!o) throw new Error("stderr is not available");
|
|
210
|
-
return o;
|
|
211
|
-
}
|
|
212
|
-
};
|
|
213
|
-
if (s && !f)
|
|
214
|
-
throw new X(w);
|
|
215
|
-
return w;
|
|
76
|
+
function S(e, t) {
|
|
77
|
+
let n = new Uint8Array(e.length + t.length);
|
|
78
|
+
return n.set(e, 0), n.set(t, e.length), n;
|
|
216
79
|
}
|
|
217
|
-
|
|
218
|
-
|
|
80
|
+
function C(e, t) {
|
|
81
|
+
if (!t.length) return 0;
|
|
82
|
+
for (let n = 0; n <= e.length - t.length; n += 1) {
|
|
83
|
+
let r;
|
|
84
|
+
for (r = 0; r < t.length && e[n + r] === t[r]; r += 1);
|
|
85
|
+
if (r === t.length) return n;
|
|
86
|
+
}
|
|
87
|
+
return -1;
|
|
219
88
|
}
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
89
|
+
//#endregion
|
|
90
|
+
//#region src/cgi.ts
|
|
91
|
+
var w = {
|
|
92
|
+
cgiDir: m.env.CGI_DIR || "cgi",
|
|
93
|
+
runnerMap: {
|
|
94
|
+
".js": ["node"],
|
|
95
|
+
".sh": ["bash"]
|
|
96
|
+
},
|
|
97
|
+
env: {},
|
|
98
|
+
timeout: 1e4,
|
|
99
|
+
disallowedPattern: /\/[._]/
|
|
100
|
+
};
|
|
101
|
+
function T(t, n) {
|
|
102
|
+
return async function(r) {
|
|
103
|
+
let i = "." + p(new URL(r.url).pathname), a = {
|
|
104
|
+
...w,
|
|
105
|
+
...t
|
|
106
|
+
};
|
|
107
|
+
if (a.disallowedPattern.test(i)) return new Response(null, { status: 403 });
|
|
108
|
+
let o = p(a.cgiDir, i);
|
|
109
|
+
try {
|
|
110
|
+
await s(o);
|
|
111
|
+
} catch {
|
|
112
|
+
return new Response(null, { status: 404 });
|
|
113
|
+
}
|
|
114
|
+
let c = a.runnerMap[u(o)];
|
|
115
|
+
try {
|
|
116
|
+
if (!c) throw Error("Unsupported command");
|
|
117
|
+
n?.log?.(`Run hook: ${i}`);
|
|
118
|
+
let [t, ...s] = [...c, o], u = e(t, s, {
|
|
119
|
+
cwd: l(o),
|
|
120
|
+
env: {
|
|
121
|
+
...m.env,
|
|
122
|
+
...a.env,
|
|
123
|
+
HTTP_METHOD: r.method,
|
|
124
|
+
HTTP_URL: r.url,
|
|
125
|
+
HTTP_HEADERS: [...r.headers.entries()].map((e) => `${e[0]}: ${e[1]}`).join("\n")
|
|
126
|
+
},
|
|
127
|
+
stdio: [
|
|
128
|
+
"pipe",
|
|
129
|
+
"pipe",
|
|
130
|
+
"inherit"
|
|
131
|
+
],
|
|
132
|
+
timeout: a.timeout
|
|
133
|
+
});
|
|
134
|
+
r.body ? h.fromWeb(r.body).pipe(u.stdin) : u.stdin.end();
|
|
135
|
+
let d = !1, f = new x(), p = f.readUntil("\n\n").finally(() => {
|
|
136
|
+
d = !0;
|
|
137
|
+
});
|
|
138
|
+
for await (let e of u.stdout) if (f.feed(e), d) break;
|
|
139
|
+
d || f.close();
|
|
140
|
+
let g = await p, { status: _, ...v } = JSON.parse(new TextDecoder().decode(g)), y = Number(_ ? +_ : 200);
|
|
141
|
+
if (isNaN(y) || y < 100 || y > 999) throw Error(`Invalid status code: ${_}`);
|
|
142
|
+
u.exitCode && (console.error("Script exit code:", u.exitCode), y = 500);
|
|
143
|
+
let b = h.toWeb(h.from((async function* () {
|
|
144
|
+
for (;;) {
|
|
145
|
+
let e = await f.read();
|
|
146
|
+
if (!e.length) break;
|
|
147
|
+
yield e;
|
|
148
|
+
}
|
|
149
|
+
for await (let e of u.stdout) yield e;
|
|
150
|
+
})()));
|
|
151
|
+
return new Response(b, {
|
|
152
|
+
status: y,
|
|
153
|
+
headers: v
|
|
154
|
+
});
|
|
155
|
+
} catch (e) {
|
|
156
|
+
return n?.log?.(`Error: ${e}`), new Response("Internal server error", { status: 500 });
|
|
157
|
+
}
|
|
158
|
+
};
|
|
238
159
|
}
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
}
|
|
282
|
-
async keys() {
|
|
283
|
-
const t = [];
|
|
284
|
-
for await (const e of this.loadDir(""))
|
|
285
|
-
t.push(e);
|
|
286
|
-
return t;
|
|
287
|
-
}
|
|
288
|
-
async removeEmptyDirs(t) {
|
|
289
|
-
const e = [this.root, ...t.split("/")];
|
|
290
|
-
for (e.pop(); e.length > 1; ) {
|
|
291
|
-
const s = d(...e);
|
|
292
|
-
if ((await S(s)).length) break;
|
|
293
|
-
await P(s);
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
async del(t) {
|
|
297
|
-
const e = this.normalizeKey(t), s = d(this.root, e);
|
|
298
|
-
await P(s), await this.removeEmptyDirs(e);
|
|
299
|
-
}
|
|
300
|
-
async rename(t, e) {
|
|
301
|
-
const s = this.normalizeKey(t), a = this.normalizeKey(e), n = d(this.root, s), i = d(this.root, a);
|
|
302
|
-
await L(n), await v(g(i), { recursive: !0 }), await $(n, i);
|
|
303
|
-
}
|
|
304
|
-
async all() {
|
|
305
|
-
const t = [];
|
|
306
|
-
for await (const e of this.loadDir(""))
|
|
307
|
-
t.push({
|
|
308
|
-
key: e,
|
|
309
|
-
value: await this.get(e) || ""
|
|
310
|
-
});
|
|
311
|
-
return t;
|
|
312
|
-
}
|
|
160
|
+
//#endregion
|
|
161
|
+
//#region src/cli.ts
|
|
162
|
+
var E = class extends Error {
|
|
163
|
+
constructor(e) {
|
|
164
|
+
super(`Command exit code: ${e.code}`), this.output = e;
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
async function D(t, n, r) {
|
|
168
|
+
let i = r?.ensureSuccess ?? !0, a = [...n?.stdio || [
|
|
169
|
+
"ignore",
|
|
170
|
+
"inherit",
|
|
171
|
+
"inherit"
|
|
172
|
+
]];
|
|
173
|
+
r?.stdin && (a[0] = "pipe"), r?.buffer && (r.buffer !== "stderr" && (a[1] = "pipe"), r.buffer !== "stdout" && (a[2] = "pipe"));
|
|
174
|
+
let o = e(t[0], t.slice(1), {
|
|
175
|
+
...n,
|
|
176
|
+
stdio: a
|
|
177
|
+
});
|
|
178
|
+
if (r?.stdin && o.stdin) {
|
|
179
|
+
let e = typeof r.stdin == "string" ? new TextEncoder().encode(r.stdin) : r.stdin instanceof ArrayBuffer ? new Uint8Array(r.stdin) : r.stdin;
|
|
180
|
+
o.stdin.write(e), o.stdin.end();
|
|
181
|
+
}
|
|
182
|
+
let s = o.stdout && g(o.stdout), c = o.stderr && g(o.stderr), l = await new Promise((e) => o.on("exit", (t, n) => {
|
|
183
|
+
e({
|
|
184
|
+
code: t,
|
|
185
|
+
signal: n
|
|
186
|
+
});
|
|
187
|
+
})), u = !l.code && !l.signal, d = {
|
|
188
|
+
success: u,
|
|
189
|
+
code: l.code,
|
|
190
|
+
signal: l.signal,
|
|
191
|
+
async stdout() {
|
|
192
|
+
if (!s) throw Error("stdout is not available");
|
|
193
|
+
return s;
|
|
194
|
+
},
|
|
195
|
+
async stderr() {
|
|
196
|
+
if (!c) throw Error("stderr is not available");
|
|
197
|
+
return c;
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
if (i && !u) throw new E(d);
|
|
201
|
+
return d;
|
|
313
202
|
}
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
this.path = t, this.data = e;
|
|
317
|
-
}
|
|
318
|
-
static async open(t = "kv.json") {
|
|
319
|
-
const e = new I(t);
|
|
320
|
-
return await e.load(), e;
|
|
321
|
-
}
|
|
322
|
-
async load() {
|
|
323
|
-
let t;
|
|
324
|
-
try {
|
|
325
|
-
t = JSON.parse(await q(this.path, "utf8"));
|
|
326
|
-
} catch {
|
|
327
|
-
}
|
|
328
|
-
t ||= {}, this.data = t;
|
|
329
|
-
}
|
|
330
|
-
async dump() {
|
|
331
|
-
await U(this.path, JSON.stringify(this.data));
|
|
332
|
-
}
|
|
333
|
-
async get(t) {
|
|
334
|
-
return this.data[t];
|
|
335
|
-
}
|
|
336
|
-
async set(t, e) {
|
|
337
|
-
this.data[t] = e, await this.dump();
|
|
338
|
-
}
|
|
339
|
-
async keys() {
|
|
340
|
-
return Object.keys(this.data);
|
|
341
|
-
}
|
|
342
|
-
async del(t) {
|
|
343
|
-
delete this.data[t], await this.dump();
|
|
344
|
-
}
|
|
345
|
-
async rename(t, e) {
|
|
346
|
-
this.data[e] = this.data[t], delete this.data[t], await this.dump();
|
|
347
|
-
}
|
|
348
|
-
async all() {
|
|
349
|
-
return Object.entries(this.data).map(([t, e]) => ({ key: t, value: e }));
|
|
350
|
-
}
|
|
203
|
+
async function O() {
|
|
204
|
+
if (!_(process.stdin.fd)) return await g(process.stdin);
|
|
351
205
|
}
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
);
|
|
357
|
-
|
|
358
|
-
class H {
|
|
359
|
-
constructor(t) {
|
|
360
|
-
this.adapter = t, t.exec(Y);
|
|
361
|
-
}
|
|
362
|
-
static async open(t) {
|
|
363
|
-
return new H(t);
|
|
364
|
-
}
|
|
365
|
-
async get(t) {
|
|
366
|
-
return this.adapter.queryRow(
|
|
367
|
-
"SELECT value FROM kv WHERE key=?1",
|
|
368
|
-
t
|
|
369
|
-
)?.value;
|
|
370
|
-
}
|
|
371
|
-
async set(t, e) {
|
|
372
|
-
this.adapter.queryRow(
|
|
373
|
-
"INSERT INTO kv(key, value) VALUES(?1, ?2) ON CONFLICT(key) DO UPDATE SET value=?2",
|
|
374
|
-
t,
|
|
375
|
-
e
|
|
376
|
-
);
|
|
377
|
-
}
|
|
378
|
-
async keys() {
|
|
379
|
-
return this.adapter.queryRows("SELECT key FROM kv").map((e) => e.key);
|
|
380
|
-
}
|
|
381
|
-
async del(t) {
|
|
382
|
-
this.adapter.queryRow("DELETE FROM kv WHERE key=?", t);
|
|
383
|
-
}
|
|
384
|
-
async rename(t, e) {
|
|
385
|
-
this.adapter.queryRow("UPDATE kv SET key=?1 WHERE key=?2", e, t);
|
|
386
|
-
}
|
|
387
|
-
async all() {
|
|
388
|
-
return this.adapter.queryRows(
|
|
389
|
-
"SELECT key, value FROM kv"
|
|
390
|
-
);
|
|
391
|
-
}
|
|
206
|
+
//#endregion
|
|
207
|
+
//#region src/env.ts
|
|
208
|
+
function k(e) {
|
|
209
|
+
let t = m.env[e];
|
|
210
|
+
if (!t) throw Error(`Missing environment variable: ${e}`);
|
|
211
|
+
return t;
|
|
392
212
|
}
|
|
393
|
-
function
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
213
|
+
function A(e) {
|
|
214
|
+
let t = {}, n = [];
|
|
215
|
+
for (let r of e) try {
|
|
216
|
+
t[r] = k(r);
|
|
217
|
+
} catch {
|
|
218
|
+
n.push(r);
|
|
219
|
+
}
|
|
220
|
+
if (n.length) throw Error(`Missing environment variables: ${n.join(", ")}`);
|
|
221
|
+
return t;
|
|
402
222
|
}
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
223
|
+
//#endregion
|
|
224
|
+
//#region src/kv/file.ts
|
|
225
|
+
var j = class e {
|
|
226
|
+
static defaultOptions = {
|
|
227
|
+
pathToKey: (e) => e.endsWith(".txt") ? e.slice(0, -4) : void 0,
|
|
228
|
+
keyToPath: (e) => e + ".txt"
|
|
229
|
+
};
|
|
230
|
+
static async open(t = ".", r) {
|
|
231
|
+
return await n(t, { recursive: !0 }), new e(t, r);
|
|
232
|
+
}
|
|
233
|
+
options;
|
|
234
|
+
constructor(t, n) {
|
|
235
|
+
this.root = t, this.options = {
|
|
236
|
+
...e.defaultOptions,
|
|
237
|
+
...n
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
normalizeKey(e) {
|
|
241
|
+
let t = f("./" + this.options.keyToPath(e)).replaceAll("\\", "/");
|
|
242
|
+
if (t.startsWith(".")) throw Error("Invalid key");
|
|
243
|
+
return t;
|
|
244
|
+
}
|
|
245
|
+
async get(e) {
|
|
246
|
+
let t = d(this.root, this.normalizeKey(e));
|
|
247
|
+
try {
|
|
248
|
+
return await r(t, "utf8");
|
|
249
|
+
} catch {}
|
|
250
|
+
}
|
|
251
|
+
async set(e, t) {
|
|
252
|
+
let r = d(this.root, this.normalizeKey(e));
|
|
253
|
+
await n(l(r), { recursive: !0 }), await c(r, t);
|
|
254
|
+
}
|
|
255
|
+
async *loadDir(e) {
|
|
256
|
+
let t = d(this.root, e);
|
|
257
|
+
for (let n of await i(t)) {
|
|
258
|
+
let r = await s(d(t, n)), i = d(e, n);
|
|
259
|
+
if (r.isDirectory()) yield* this.loadDir(i);
|
|
260
|
+
else {
|
|
261
|
+
let e = this.options.pathToKey(i);
|
|
262
|
+
e && (yield e);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
async keys() {
|
|
267
|
+
let e = [];
|
|
268
|
+
for await (let t of this.loadDir("")) e.push(t);
|
|
269
|
+
return e;
|
|
270
|
+
}
|
|
271
|
+
async removeEmptyDirs(e) {
|
|
272
|
+
let t = [this.root, ...e.split("/")];
|
|
273
|
+
for (t.pop(); t.length > 1;) {
|
|
274
|
+
let e = d(...t);
|
|
275
|
+
if ((await i(e)).length) break;
|
|
276
|
+
await o(e);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
async del(e) {
|
|
280
|
+
let t = this.normalizeKey(e);
|
|
281
|
+
await o(d(this.root, t)), await this.removeEmptyDirs(t);
|
|
282
|
+
}
|
|
283
|
+
async rename(e, r) {
|
|
284
|
+
let i = this.normalizeKey(e), o = this.normalizeKey(r), s = d(this.root, i), c = d(this.root, o);
|
|
285
|
+
await t(s), await n(l(c), { recursive: !0 }), await a(s, c);
|
|
286
|
+
}
|
|
287
|
+
async all() {
|
|
288
|
+
let e = [];
|
|
289
|
+
for await (let t of this.loadDir("")) e.push({
|
|
290
|
+
key: t,
|
|
291
|
+
value: await this.get(t) || ""
|
|
292
|
+
});
|
|
293
|
+
return e;
|
|
294
|
+
}
|
|
295
|
+
}, M = class e {
|
|
296
|
+
static async open(t = "kv.json") {
|
|
297
|
+
let n = new e(t);
|
|
298
|
+
return await n.load(), n;
|
|
299
|
+
}
|
|
300
|
+
constructor(e, t = {}) {
|
|
301
|
+
this.path = e, this.data = t;
|
|
302
|
+
}
|
|
303
|
+
async load() {
|
|
304
|
+
let e;
|
|
305
|
+
try {
|
|
306
|
+
e = JSON.parse(await r(this.path, "utf8"));
|
|
307
|
+
} catch {}
|
|
308
|
+
e ||= {}, this.data = e;
|
|
309
|
+
}
|
|
310
|
+
async dump() {
|
|
311
|
+
await c(this.path, JSON.stringify(this.data));
|
|
312
|
+
}
|
|
313
|
+
async get(e) {
|
|
314
|
+
return this.data[e];
|
|
315
|
+
}
|
|
316
|
+
async set(e, t) {
|
|
317
|
+
this.data[e] = t, await this.dump();
|
|
318
|
+
}
|
|
319
|
+
async keys() {
|
|
320
|
+
return Object.keys(this.data);
|
|
321
|
+
}
|
|
322
|
+
async del(e) {
|
|
323
|
+
delete this.data[e], await this.dump();
|
|
324
|
+
}
|
|
325
|
+
async rename(e, t) {
|
|
326
|
+
this.data[t] = this.data[e], delete this.data[e], await this.dump();
|
|
327
|
+
}
|
|
328
|
+
async all() {
|
|
329
|
+
return Object.entries(this.data).map(([e, t]) => ({
|
|
330
|
+
key: e,
|
|
331
|
+
value: t
|
|
332
|
+
}));
|
|
333
|
+
}
|
|
334
|
+
}, N = "\nCREATE TABLE IF NOT EXISTS kv (\n key VARCHAR UNIQUE,\n value VARCHAR\n);\n", P = class e {
|
|
335
|
+
static async open(t) {
|
|
336
|
+
return new e(t);
|
|
337
|
+
}
|
|
338
|
+
constructor(e) {
|
|
339
|
+
this.adapter = e, e.exec(N);
|
|
340
|
+
}
|
|
341
|
+
async get(e) {
|
|
342
|
+
return this.adapter.queryRow("SELECT value FROM kv WHERE key=?1", e)?.value;
|
|
343
|
+
}
|
|
344
|
+
async set(e, t) {
|
|
345
|
+
this.adapter.queryRow("INSERT INTO kv(key, value) VALUES(?1, ?2) ON CONFLICT(key) DO UPDATE SET value=?2", e, t);
|
|
346
|
+
}
|
|
347
|
+
async keys() {
|
|
348
|
+
return this.adapter.queryRows("SELECT key FROM kv").map((e) => e.key);
|
|
349
|
+
}
|
|
350
|
+
async del(e) {
|
|
351
|
+
this.adapter.queryRow("DELETE FROM kv WHERE key=?", e);
|
|
352
|
+
}
|
|
353
|
+
async rename(e, t) {
|
|
354
|
+
this.adapter.queryRow("UPDATE kv SET key=?1 WHERE key=?2", t, e);
|
|
355
|
+
}
|
|
356
|
+
async all() {
|
|
357
|
+
return this.adapter.queryRows("SELECT key, value FROM kv");
|
|
358
|
+
}
|
|
415
359
|
};
|
|
360
|
+
//#endregion
|
|
361
|
+
//#region src/sqlite-common/handler.ts
|
|
362
|
+
function F(e) {
|
|
363
|
+
return function(t) {
|
|
364
|
+
let { sql: n, params: r, type: i } = {
|
|
365
|
+
sql: t.sql,
|
|
366
|
+
params: t.params ?? [],
|
|
367
|
+
type: t.type ?? "values"
|
|
368
|
+
}, a = e(), o = a.prepare(n)[i === "values" ? "values" : "all"](...r);
|
|
369
|
+
return a.destroy(), o;
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
//#endregion
|
|
373
|
+
export { E as CommandError, j as KvFile, M as KvJson, P as KvSqlite, T as createCGIHandler, w as defaultConfig, k as ensureEnv, A as ensureEnvs, F as getSqlHandler, O as readStdIn, D as runCommand };
|
package/dist/node/index.js
CHANGED
|
@@ -1,47 +1,46 @@
|
|
|
1
|
-
import { DatabaseSync as
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
class
|
|
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
|
-
export {
|
|
46
|
-
i as SqliteAdapter
|
|
1
|
+
import { DatabaseSync as e } from "node:sqlite";
|
|
2
|
+
//#region src/node/sqlite/adapter.ts
|
|
3
|
+
var t = class {
|
|
4
|
+
constructor(e) {
|
|
5
|
+
this.statement = e;
|
|
6
|
+
}
|
|
7
|
+
get(...e) {
|
|
8
|
+
return this.statement.get(...e) || void 0;
|
|
9
|
+
}
|
|
10
|
+
all(...e) {
|
|
11
|
+
return this.statement.all(...e);
|
|
12
|
+
}
|
|
13
|
+
values(...e) {
|
|
14
|
+
return this.all(...e).map((e) => Object.values(e));
|
|
15
|
+
}
|
|
16
|
+
}, n = class {
|
|
17
|
+
db;
|
|
18
|
+
constructor(t, n) {
|
|
19
|
+
this.db = new e(t, n);
|
|
20
|
+
}
|
|
21
|
+
exec(e) {
|
|
22
|
+
if (!this.db) throw Error("Database is closed");
|
|
23
|
+
this.db.exec(e);
|
|
24
|
+
}
|
|
25
|
+
transaction(e) {
|
|
26
|
+
throw Error("Not supported in Node.js");
|
|
27
|
+
}
|
|
28
|
+
prepare(e) {
|
|
29
|
+
if (!this.db) throw Error("Database is closed");
|
|
30
|
+
return new t(this.db.prepare(e));
|
|
31
|
+
}
|
|
32
|
+
queryRow(e, ...t) {
|
|
33
|
+
return this.prepare(e).get(...t);
|
|
34
|
+
}
|
|
35
|
+
queryRows(e, ...t) {
|
|
36
|
+
return this.prepare(e).all(...t);
|
|
37
|
+
}
|
|
38
|
+
queryValues(e, ...t) {
|
|
39
|
+
return this.prepare(e).values(...t);
|
|
40
|
+
}
|
|
41
|
+
destroy() {
|
|
42
|
+
this.db?.close();
|
|
43
|
+
}
|
|
47
44
|
};
|
|
45
|
+
//#endregion
|
|
46
|
+
export { n as SqliteAdapter };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gera2ld/common-node",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"registry": "https://registry.npmjs.org/"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
|
-
"@gera2ld/common": "^0.0.
|
|
23
|
+
"@gera2ld/common": "^0.0.3"
|
|
24
24
|
},
|
|
25
25
|
"scripts": {
|
|
26
26
|
"clean": "del-cli dist tsconfig.tsbuildinfo",
|