@hop-top/kit 0.4.0-alpha.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.
- package/CHANGELOG.md +86 -0
- package/LICENSE +25 -0
- package/README.md +189 -0
- package/dist/aim.d.ts +116 -0
- package/dist/aim.js +417 -0
- package/dist/alias.d.ts +73 -0
- package/dist/alias.js +207 -0
- package/dist/api.d.ts +62 -0
- package/dist/api.js +165 -0
- package/dist/auth.d.ts +26 -0
- package/dist/auth.js +41 -0
- package/dist/cli.d.ts +309 -0
- package/dist/cli.js +885 -0
- package/dist/config.d.ts +97 -0
- package/dist/config.js +79 -0
- package/dist/errcorrect.d.ts +37 -0
- package/dist/errcorrect.js +69 -0
- package/dist/llm.d.ts +188 -0
- package/dist/llm.js +356 -0
- package/dist/output.d.ts +213 -0
- package/dist/output.js +447 -0
- package/dist/progress.d.ts +30 -0
- package/dist/progress.js +56 -0
- package/dist/provenance.d.ts +24 -0
- package/dist/provenance.js +32 -0
- package/dist/routellm.d.ts +64 -0
- package/dist/routellm.js +258 -0
- package/dist/rpc.d.ts +25 -0
- package/dist/rpc.js +101 -0
- package/dist/safety.d.ts +24 -0
- package/dist/safety.js +44 -0
- package/dist/scope.d.ts +121 -0
- package/dist/scope.js +380 -0
- package/dist/sqlstore.d.ts +84 -0
- package/dist/sqlstore.js +85 -0
- package/dist/stream.d.ts +35 -0
- package/dist/stream.js +52 -0
- package/dist/tui/index.d.ts +300 -0
- package/dist/tui/index.js +332 -0
- package/dist/upgrade.d.ts +95 -0
- package/dist/upgrade.js +115 -0
- package/dist/xdg.d.ts +169 -0
- package/dist/xdg.js +129 -0
- package/eslint.config.mjs +17 -0
- package/package.json +133 -0
- package/src/README.md +3 -0
- package/src/aim.test.ts +331 -0
- package/src/aim.ts +388 -0
- package/src/alias.test.ts +423 -0
- package/src/alias.ts +256 -0
- package/src/api.test.ts +190 -0
- package/src/api.ts +214 -0
- package/src/auth.test.ts +24 -0
- package/src/auth.ts +37 -0
- package/src/bus.test.ts +178 -0
- package/src/bus.ts +114 -0
- package/src/cli.test.ts +690 -0
- package/src/cli.ts +741 -0
- package/src/completion-values.test.ts +202 -0
- package/src/completion-values.ts +173 -0
- package/src/completion.test.ts +212 -0
- package/src/completion.ts +158 -0
- package/src/compliance.test.ts +134 -0
- package/src/compliance.ts +669 -0
- package/src/config.test.ts +110 -0
- package/src/config.ts +164 -0
- package/src/errcorrect.test.ts +134 -0
- package/src/errcorrect.ts +84 -0
- package/src/flagregister.test.ts +103 -0
- package/src/flagregister.ts +78 -0
- package/src/gen/crud_connect.ts +76 -0
- package/src/gen/crud_pb.ts +269 -0
- package/src/hint.test.ts +315 -0
- package/src/hint.ts +178 -0
- package/src/llm.test.ts +612 -0
- package/src/llm.ts +577 -0
- package/src/log.test.ts +145 -0
- package/src/log.ts +155 -0
- package/src/output/builtins.ts +34 -0
- package/src/output/dispatch.ts +195 -0
- package/src/output/flags.ts +151 -0
- package/src/output/format_help.ts +80 -0
- package/src/output/formatter.ts +167 -0
- package/src/output/formatters/csv.ts +72 -0
- package/src/output/formatters/json.ts +44 -0
- package/src/output/formatters/table.ts +44 -0
- package/src/output/formatters/text.ts +109 -0
- package/src/output/formatters/yaml.ts +47 -0
- package/src/output/projection.ts +126 -0
- package/src/output/registry.ts +88 -0
- package/src/output/template.ts +63 -0
- package/src/output.test.ts +117 -0
- package/src/output.ts +90 -0
- package/src/progress.test.ts +93 -0
- package/src/progress.ts +58 -0
- package/src/provenance.test.ts +59 -0
- package/src/provenance.ts +26 -0
- package/src/routellm.test.ts +179 -0
- package/src/routellm.ts +365 -0
- package/src/router/bert.test.ts +180 -0
- package/src/router/bert.ts +145 -0
- package/src/router/controller.test.ts +183 -0
- package/src/router/controller.ts +165 -0
- package/src/router/integration.test.ts +309 -0
- package/src/router/intent.test.ts +187 -0
- package/src/router/intent.ts +208 -0
- package/src/router/mf.test.ts +66 -0
- package/src/router/mf.ts +52 -0
- package/src/router/random.test.ts +23 -0
- package/src/router/random.ts +13 -0
- package/src/router/router.test.ts +31 -0
- package/src/router/router.ts +62 -0
- package/src/router/server.test.ts +193 -0
- package/src/router/server.ts +134 -0
- package/src/router/sw.test.ts +199 -0
- package/src/router/sw.ts +232 -0
- package/src/rpc.test.ts +47 -0
- package/src/rpc.ts +91 -0
- package/src/safety.test.ts +53 -0
- package/src/safety.ts +42 -0
- package/src/scope-defaults.json +72 -0
- package/src/scope.test.ts +177 -0
- package/src/scope.ts +382 -0
- package/src/setflag.test.ts +110 -0
- package/src/setflag.ts +102 -0
- package/src/sqlstore.test.ts +110 -0
- package/src/sqlstore.ts +144 -0
- package/src/stream.test.ts +69 -0
- package/src/stream.ts +47 -0
- package/src/textflag.test.ts +105 -0
- package/src/textflag.ts +68 -0
- package/src/toolspec.test.ts +187 -0
- package/src/toolspec.ts +279 -0
- package/src/triton/client.test.ts +205 -0
- package/src/triton/client.ts +261 -0
- package/src/tui/anim.test.ts +146 -0
- package/src/tui/anim.ts +134 -0
- package/src/tui/badge.test.ts +41 -0
- package/src/tui/badge.ts +55 -0
- package/src/tui/confirm.test.ts +68 -0
- package/src/tui/confirm.ts +49 -0
- package/src/tui/index.ts +29 -0
- package/src/tui/list.test.ts +84 -0
- package/src/tui/list.ts +72 -0
- package/src/tui/parity.ts +41 -0
- package/src/tui/pills.test.ts +40 -0
- package/src/tui/pills.ts +43 -0
- package/src/tui/progress.test.ts +164 -0
- package/src/tui/progress.ts +120 -0
- package/src/tui/spinner.test.ts +111 -0
- package/src/tui/spinner.ts +96 -0
- package/src/tui/status.test.ts +68 -0
- package/src/tui/status.ts +66 -0
- package/src/upgrade.test.ts +298 -0
- package/src/upgrade.ts +205 -0
- package/src/uri.ts +199 -0
- package/src/wizard.test.ts +441 -0
- package/src/wizard.ts +662 -0
- package/src/xdg.test.ts +202 -0
- package/src/xdg.ts +280 -0
- package/test/output/cols.test.ts +148 -0
- package/test/output/csv.test.ts +129 -0
- package/test/output/dispatch.test.ts +194 -0
- package/test/output/format_help.test.ts +131 -0
- package/test/output/formatter.test.ts +137 -0
- package/test/output/output_flag.test.ts +124 -0
- package/test/output/registry.test.ts +127 -0
- package/test/output/smoke-capture.ts +83 -0
- package/test/output/smoke.test.ts +142 -0
- package/test/output/template.test.ts +95 -0
- package/test/output/text.test.ts +75 -0
- package/test/testdata/api-fixture.json +115 -0
- package/test/testdata/query-vectors.json +187 -0
- package/tsconfig.json +12 -0
package/dist/scope.js
ADDED
|
@@ -0,0 +1,380 @@
|
|
|
1
|
+
"use strict";
|
|
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 __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/scope.ts
|
|
31
|
+
var scope_exports = {};
|
|
32
|
+
__export(scope_exports, {
|
|
33
|
+
Decision: () => Decision,
|
|
34
|
+
Default: () => Default,
|
|
35
|
+
ErrDenied: () => ErrDenied,
|
|
36
|
+
Mode: () => Mode,
|
|
37
|
+
New: () => New,
|
|
38
|
+
Op: () => Op,
|
|
39
|
+
Policy: () => Policy,
|
|
40
|
+
SecretPaths: () => SecretPaths,
|
|
41
|
+
setDefault: () => setDefault
|
|
42
|
+
});
|
|
43
|
+
module.exports = __toCommonJS(scope_exports);
|
|
44
|
+
var fs = __toESM(require("fs"));
|
|
45
|
+
var os = __toESM(require("os"));
|
|
46
|
+
var path = __toESM(require("path"));
|
|
47
|
+
var import_picomatch = __toESM(require("picomatch"));
|
|
48
|
+
|
|
49
|
+
// src/scope-defaults.json
|
|
50
|
+
var scope_defaults_default = {
|
|
51
|
+
$schema: "https://json-schema.org/draft/2020-12/schema",
|
|
52
|
+
$id: "https://hop.top/kit/contracts/parity/scope-defaults.json",
|
|
53
|
+
description: "Cross-language default deny patterns for kit/scope. Go, TypeScript, and Python ports MUST load values from here. Patterns use bmatcuk/doublestar/v4 syntax; '~' expands to the user home directory; environment macros (%APPDATA%, %LOCALAPPDATA%, %USERPROFILE%) expand to the corresponding os.UserConfigDir / os.UserHomeDir results so test overrides work.",
|
|
54
|
+
version: 1,
|
|
55
|
+
deny: {
|
|
56
|
+
common: [
|
|
57
|
+
"~/.ssh/**",
|
|
58
|
+
"~/.aws/**",
|
|
59
|
+
"~/.azure/**",
|
|
60
|
+
"~/.config/gcloud/**",
|
|
61
|
+
"~/.gnupg/**",
|
|
62
|
+
"~/.pki/**",
|
|
63
|
+
"~/.kube/**",
|
|
64
|
+
"~/.netrc",
|
|
65
|
+
"~/.pgpass",
|
|
66
|
+
"~/.my.cnf",
|
|
67
|
+
"~/.npmrc",
|
|
68
|
+
"~/.pypirc",
|
|
69
|
+
"**/.env",
|
|
70
|
+
"**/.env.*",
|
|
71
|
+
"**/credentials*",
|
|
72
|
+
"**/secrets*",
|
|
73
|
+
"**/*.pem",
|
|
74
|
+
"**/*.key",
|
|
75
|
+
"**/id_rsa*",
|
|
76
|
+
"**/id_ed25519*",
|
|
77
|
+
"**/.git/config"
|
|
78
|
+
],
|
|
79
|
+
darwin: [
|
|
80
|
+
"~/Library/Keychains/**",
|
|
81
|
+
"~/Library/Cookies/**",
|
|
82
|
+
"~/Library/Application Support/Google/Chrome/**/Cookies*",
|
|
83
|
+
"~/Library/Application Support/Google/Chrome/**/Login Data*",
|
|
84
|
+
"~/Library/Application Support/Arc/User Data/**/Cookies*",
|
|
85
|
+
"~/Library/Application Support/BraveSoftware/Brave-Browser/**/Cookies*",
|
|
86
|
+
"~/Library/Application Support/Microsoft Edge/**/Cookies*",
|
|
87
|
+
"~/Library/Application Support/Firefox/Profiles/**/cookies.sqlite",
|
|
88
|
+
"~/Library/Application Support/Firefox/Profiles/**/key*.db",
|
|
89
|
+
"~/Library/Application Support/Firefox/Profiles/**/logins.json"
|
|
90
|
+
],
|
|
91
|
+
linux: [
|
|
92
|
+
"~/.config/google-chrome/**/Cookies*",
|
|
93
|
+
"~/.config/google-chrome/**/Login Data*",
|
|
94
|
+
"~/.config/BraveSoftware/Brave-Browser/**/Cookies*",
|
|
95
|
+
"~/.config/microsoft-edge/**/Cookies*",
|
|
96
|
+
"~/.mozilla/firefox/**/cookies.sqlite",
|
|
97
|
+
"~/.mozilla/firefox/**/key*.db",
|
|
98
|
+
"~/.mozilla/firefox/**/logins.json"
|
|
99
|
+
],
|
|
100
|
+
windows: [
|
|
101
|
+
"%USERPROFILE%/.aws/**",
|
|
102
|
+
"%USERPROFILE%/.azure/**",
|
|
103
|
+
"%APPDATA%/gcloud/**",
|
|
104
|
+
"%USERPROFILE%/.kube/**",
|
|
105
|
+
"%USERPROFILE%/.ssh/**",
|
|
106
|
+
"%APPDATA%/Microsoft/Credentials/**",
|
|
107
|
+
"%LOCALAPPDATA%/Microsoft/Credentials/**",
|
|
108
|
+
"%APPDATA%/Microsoft/Crypto/**",
|
|
109
|
+
"%APPDATA%/Microsoft/Protect/**",
|
|
110
|
+
"%APPDATA%/Microsoft/SystemCertificates/**",
|
|
111
|
+
"%APPDATA%/PuTTY/sshhostkeys",
|
|
112
|
+
"%APPDATA%/PuTTY/sessions/**",
|
|
113
|
+
"%LOCALAPPDATA%/Google/Chrome/User Data/**/Cookies*",
|
|
114
|
+
"%LOCALAPPDATA%/Google/Chrome/User Data/**/Login Data*",
|
|
115
|
+
"%LOCALAPPDATA%/Microsoft/Edge/User Data/**/Cookies*",
|
|
116
|
+
"%APPDATA%/Mozilla/Firefox/Profiles/**/cookies.sqlite",
|
|
117
|
+
"%APPDATA%/Mozilla/Firefox/Profiles/**/key*.db",
|
|
118
|
+
"%APPDATA%/Mozilla/Firefox/Profiles/**/logins.json"
|
|
119
|
+
]
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
// src/scope.ts
|
|
124
|
+
var Mode = /* @__PURE__ */ ((Mode2) => {
|
|
125
|
+
Mode2["Strict"] = "strict";
|
|
126
|
+
Mode2["Warn"] = "warn";
|
|
127
|
+
Mode2["Prompt"] = "prompt";
|
|
128
|
+
return Mode2;
|
|
129
|
+
})(Mode || {});
|
|
130
|
+
var Op = /* @__PURE__ */ ((Op2) => {
|
|
131
|
+
Op2[Op2["Read"] = 1] = "Read";
|
|
132
|
+
Op2[Op2["Write"] = 2] = "Write";
|
|
133
|
+
Op2[Op2["Exec"] = 4] = "Exec";
|
|
134
|
+
return Op2;
|
|
135
|
+
})(Op || {});
|
|
136
|
+
var Decision = /* @__PURE__ */ ((Decision2) => {
|
|
137
|
+
Decision2["Unknown"] = "unknown";
|
|
138
|
+
Decision2["Allowed"] = "allowed";
|
|
139
|
+
Decision2["Denied"] = "denied";
|
|
140
|
+
return Decision2;
|
|
141
|
+
})(Decision || {});
|
|
142
|
+
var ErrDenied = class extends Error {
|
|
143
|
+
constructor(p, op) {
|
|
144
|
+
super(`scope: path denied: ${p} (op=${opString(op)})`);
|
|
145
|
+
this.name = "ErrDenied";
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
var defaults = scope_defaults_default;
|
|
149
|
+
var Policy = class _Policy {
|
|
150
|
+
constructor() {
|
|
151
|
+
this.rules = [];
|
|
152
|
+
this.mode = "strict" /* Strict */;
|
|
153
|
+
}
|
|
154
|
+
/** Register an allow rule covering Read|Write|Exec. */
|
|
155
|
+
allow(...patterns) {
|
|
156
|
+
return this.allowOp(1 /* Read */ | 2 /* Write */ | 4 /* Exec */, ...patterns);
|
|
157
|
+
}
|
|
158
|
+
/** Register an allow rule for the given operations. */
|
|
159
|
+
allowOp(op, ...patterns) {
|
|
160
|
+
if (patterns.length === 0) return this;
|
|
161
|
+
this.rules.push({ patterns: [...patterns], ops: op, allow: true });
|
|
162
|
+
return this;
|
|
163
|
+
}
|
|
164
|
+
/** Register a deny rule covering Read|Write|Exec. */
|
|
165
|
+
deny(...patterns) {
|
|
166
|
+
return this.denyOp(1 /* Read */ | 2 /* Write */ | 4 /* Exec */, ...patterns);
|
|
167
|
+
}
|
|
168
|
+
/** Register a deny rule for the given operations. */
|
|
169
|
+
denyOp(op, ...patterns) {
|
|
170
|
+
if (patterns.length === 0) return this;
|
|
171
|
+
this.rules.push({ patterns: [...patterns], ops: op, allow: false });
|
|
172
|
+
return this;
|
|
173
|
+
}
|
|
174
|
+
/** Set the enforcement mode. */
|
|
175
|
+
setMode(m) {
|
|
176
|
+
this.mode = m;
|
|
177
|
+
return this;
|
|
178
|
+
}
|
|
179
|
+
/** Get the current enforcement mode. */
|
|
180
|
+
getMode() {
|
|
181
|
+
return this.mode;
|
|
182
|
+
}
|
|
183
|
+
/** Set the prompt callback used in `Mode.Prompt`. */
|
|
184
|
+
setPromptFunc(fn) {
|
|
185
|
+
this.prompt = fn;
|
|
186
|
+
return this;
|
|
187
|
+
}
|
|
188
|
+
/** Defensive copy of the policy's rules in registration order. */
|
|
189
|
+
getRules() {
|
|
190
|
+
return this.rules.map((r) => ({ patterns: [...r.patterns], ops: r.ops, allow: r.allow }));
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Evaluate `(p, op)` against the policy. Pure — no prompt, no mutation.
|
|
194
|
+
* Resolves symlinks before matching to defeat symlink escapes; on ENOENT
|
|
195
|
+
* the cleaned path is used so "intent to write" still matches deny rules.
|
|
196
|
+
*/
|
|
197
|
+
check(p, op) {
|
|
198
|
+
const resolved = resolvePath(p);
|
|
199
|
+
let sawAllow = false;
|
|
200
|
+
for (const r of this.rules) {
|
|
201
|
+
if ((r.ops & op) === 0) continue;
|
|
202
|
+
if (!matchAny(r.patterns, resolved)) continue;
|
|
203
|
+
if (!r.allow) return "denied" /* Denied */;
|
|
204
|
+
sawAllow = true;
|
|
205
|
+
}
|
|
206
|
+
return sawAllow ? "allowed" /* Allowed */ : "unknown" /* Unknown */;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Enforce calls `check` and translates the decision according to mode:
|
|
210
|
+
* Strict + (Denied|Unknown) throws; Warn + Denied logs; Prompt + Denied
|
|
211
|
+
* invokes the prompt callback.
|
|
212
|
+
*/
|
|
213
|
+
enforce(p, op) {
|
|
214
|
+
const dec = this.check(p, op);
|
|
215
|
+
if (dec === "allowed" /* Allowed */) return;
|
|
216
|
+
if (dec === "unknown" /* Unknown */ && this.mode !== "strict" /* Strict */) return;
|
|
217
|
+
this.handleDeny(p, op);
|
|
218
|
+
}
|
|
219
|
+
handleDeny(p, op) {
|
|
220
|
+
switch (this.mode) {
|
|
221
|
+
case "warn" /* Warn */:
|
|
222
|
+
console.warn(`scope: path denied (warn mode, allowing): ${p} op=${opString(op)}`);
|
|
223
|
+
return;
|
|
224
|
+
case "prompt" /* Prompt */:
|
|
225
|
+
if (this.prompt && this.prompt(p, op)) return;
|
|
226
|
+
throw new ErrDenied(p, op);
|
|
227
|
+
default:
|
|
228
|
+
throw new ErrDenied(p, op);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
/** Deep copy. Useful for per-test isolation paired with `setDefault`. */
|
|
232
|
+
snapshot() {
|
|
233
|
+
const cp = new _Policy();
|
|
234
|
+
cp.mode = this.mode;
|
|
235
|
+
cp.prompt = this.prompt;
|
|
236
|
+
cp.rules = this.rules.map((r) => ({ patterns: [...r.patterns], ops: r.ops, allow: r.allow }));
|
|
237
|
+
return cp;
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
var defaultP = null;
|
|
241
|
+
function New() {
|
|
242
|
+
return new Policy();
|
|
243
|
+
}
|
|
244
|
+
function Default() {
|
|
245
|
+
if (!defaultP) {
|
|
246
|
+
defaultP = New().deny(...SecretPaths());
|
|
247
|
+
}
|
|
248
|
+
return defaultP;
|
|
249
|
+
}
|
|
250
|
+
function setDefault(p) {
|
|
251
|
+
const prev = Default();
|
|
252
|
+
defaultP = p;
|
|
253
|
+
return () => {
|
|
254
|
+
defaultP = prev;
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
function SecretPaths() {
|
|
258
|
+
const common = defaults.deny["common"] ?? [];
|
|
259
|
+
const platName = platformKey();
|
|
260
|
+
const platSpecific = defaults.deny[platName] ?? [];
|
|
261
|
+
return [...common, ...platSpecific].map(expandWindowsEnv);
|
|
262
|
+
}
|
|
263
|
+
function platformKey() {
|
|
264
|
+
switch (process.platform) {
|
|
265
|
+
case "darwin":
|
|
266
|
+
return "darwin";
|
|
267
|
+
case "win32":
|
|
268
|
+
return "windows";
|
|
269
|
+
default:
|
|
270
|
+
return "linux";
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
function expandWindowsEnv(p) {
|
|
274
|
+
for (const key of ["APPDATA", "LOCALAPPDATA", "USERPROFILE"]) {
|
|
275
|
+
const token = `%${key}%`;
|
|
276
|
+
if (!p.includes(token)) continue;
|
|
277
|
+
const v = process.env[key];
|
|
278
|
+
if (!v) continue;
|
|
279
|
+
p = p.split(token).join(v);
|
|
280
|
+
}
|
|
281
|
+
return p;
|
|
282
|
+
}
|
|
283
|
+
function expandHome(s) {
|
|
284
|
+
if (s === "~" || s.startsWith("~/")) {
|
|
285
|
+
let home = os.homedir();
|
|
286
|
+
try {
|
|
287
|
+
home = fs.realpathSync(home);
|
|
288
|
+
} catch {
|
|
289
|
+
}
|
|
290
|
+
return s === "~" ? home : path.join(home, s.slice(2));
|
|
291
|
+
}
|
|
292
|
+
return s;
|
|
293
|
+
}
|
|
294
|
+
function resolvePath(s) {
|
|
295
|
+
const expanded = expandHome(s);
|
|
296
|
+
const cleaned = path.normalize(expanded);
|
|
297
|
+
try {
|
|
298
|
+
return fs.realpathSync(cleaned);
|
|
299
|
+
} catch (err) {
|
|
300
|
+
if (!isENOENT(err)) return cleaned;
|
|
301
|
+
}
|
|
302
|
+
let dir = path.dirname(cleaned);
|
|
303
|
+
let tail = path.basename(cleaned);
|
|
304
|
+
while (dir && dir !== path.parse(dir).root) {
|
|
305
|
+
try {
|
|
306
|
+
const resolved = fs.realpathSync(dir);
|
|
307
|
+
return path.join(resolved, tail);
|
|
308
|
+
} catch (err) {
|
|
309
|
+
if (!isENOENT(err)) return cleaned;
|
|
310
|
+
}
|
|
311
|
+
tail = path.join(path.basename(dir), tail);
|
|
312
|
+
dir = path.dirname(dir);
|
|
313
|
+
}
|
|
314
|
+
return cleaned;
|
|
315
|
+
}
|
|
316
|
+
function isENOENT(err) {
|
|
317
|
+
return typeof err === "object" && err !== null && err.code === "ENOENT";
|
|
318
|
+
}
|
|
319
|
+
function matchAny(patterns, abs) {
|
|
320
|
+
for (const p of patterns) {
|
|
321
|
+
const expanded = expandHome(p);
|
|
322
|
+
const canon = canonicalisePattern(path.normalize(expanded));
|
|
323
|
+
if (import_picomatch.default.isMatch(abs, canon, { dot: true })) return true;
|
|
324
|
+
}
|
|
325
|
+
return false;
|
|
326
|
+
}
|
|
327
|
+
function canonicalisePattern(pat) {
|
|
328
|
+
const sep2 = path.sep;
|
|
329
|
+
const parts = pat.split(sep2);
|
|
330
|
+
let cut = 0;
|
|
331
|
+
for (let i = 0; i < parts.length; i++) {
|
|
332
|
+
if (containsGlobMeta(parts[i] ?? "")) break;
|
|
333
|
+
cut = i + 1;
|
|
334
|
+
}
|
|
335
|
+
if (cut === 0) return pat;
|
|
336
|
+
const literalParts = parts.slice(0, cut);
|
|
337
|
+
const prefix = literalParts.join(sep2);
|
|
338
|
+
if (!prefix) return pat;
|
|
339
|
+
try {
|
|
340
|
+
const resolved = fs.realpathSync(prefix);
|
|
341
|
+
return joinPattern(resolved, parts.slice(cut));
|
|
342
|
+
} catch {
|
|
343
|
+
for (let i = literalParts.length - 1; i >= 1; i--) {
|
|
344
|
+
const ancestor = literalParts.slice(0, i).join(sep2);
|
|
345
|
+
if (!ancestor) continue;
|
|
346
|
+
try {
|
|
347
|
+
const resolved = fs.realpathSync(ancestor);
|
|
348
|
+
const tail = [...literalParts.slice(i), ...parts.slice(cut)];
|
|
349
|
+
return joinPattern(resolved, tail);
|
|
350
|
+
} catch {
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
return pat;
|
|
355
|
+
}
|
|
356
|
+
function joinPattern(resolved, rest) {
|
|
357
|
+
return rest.length === 0 ? resolved : resolved + path.sep + rest.join(path.sep);
|
|
358
|
+
}
|
|
359
|
+
function containsGlobMeta(s) {
|
|
360
|
+
return /[*?[\]{}]/.test(s);
|
|
361
|
+
}
|
|
362
|
+
function opString(op) {
|
|
363
|
+
const parts = [];
|
|
364
|
+
if (op & 1 /* Read */) parts.push("read");
|
|
365
|
+
if (op & 2 /* Write */) parts.push("write");
|
|
366
|
+
if (op & 4 /* Exec */) parts.push("exec");
|
|
367
|
+
return parts.length === 0 ? "none" : parts.join("|");
|
|
368
|
+
}
|
|
369
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
370
|
+
0 && (module.exports = {
|
|
371
|
+
Decision,
|
|
372
|
+
Default,
|
|
373
|
+
ErrDenied,
|
|
374
|
+
Mode,
|
|
375
|
+
New,
|
|
376
|
+
Op,
|
|
377
|
+
Policy,
|
|
378
|
+
SecretPaths,
|
|
379
|
+
setDefault
|
|
380
|
+
});
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import Database from 'better-sqlite3';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @module sqlstore
|
|
5
|
+
*
|
|
6
|
+
* SQLite-backed key-value store with optional TTL and custom migration support.
|
|
7
|
+
*
|
|
8
|
+
* ## Schema
|
|
9
|
+
* ```sql
|
|
10
|
+
* CREATE TABLE IF NOT EXISTS kv (
|
|
11
|
+
* key TEXT PRIMARY KEY,
|
|
12
|
+
* value TEXT NOT NULL,
|
|
13
|
+
* expires_at INTEGER -- NULL means no expiry; epoch-ms when set
|
|
14
|
+
* )
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* ## TTL semantics
|
|
18
|
+
* - `opts.ttl` is in **milliseconds**.
|
|
19
|
+
* - On `put`, `expires_at` is set to `Date.now() + ttl` when `ttl > 0`.
|
|
20
|
+
* - On `get`, if `expires_at !== null && expires_at <= Date.now()`, `null` is returned.
|
|
21
|
+
* Expired rows are NOT automatically deleted; prune them with:
|
|
22
|
+
* ```sql
|
|
23
|
+
* DELETE FROM kv WHERE expires_at IS NOT NULL AND expires_at <= <now>
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* ## migrateSql
|
|
27
|
+
* If `opts.migrateSql` is provided it is executed once inside `open()`, after
|
|
28
|
+
* the `kv` table is created. Use it to add application-specific tables,
|
|
29
|
+
* indexes, or seed data. Multiple statements must be separated by semicolons.
|
|
30
|
+
*
|
|
31
|
+
* ## Example
|
|
32
|
+
* ```ts
|
|
33
|
+
* import { open } from '@hop-top/kit/sqlstore'
|
|
34
|
+
*
|
|
35
|
+
* const store = open('/tmp/myapp.db', { ttl: 60_000 })
|
|
36
|
+
* store.put('session:abc', { userId: 1 })
|
|
37
|
+
* const session = store.get<{ userId: number }>('session:abc')
|
|
38
|
+
* store.close()
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
|
|
42
|
+
/** Options accepted by {@link open}. */
|
|
43
|
+
interface Options {
|
|
44
|
+
/**
|
|
45
|
+
* Time-to-live in **milliseconds**. When positive, values older than this
|
|
46
|
+
* are treated as absent on `get`. Zero or omitted disables expiry.
|
|
47
|
+
*/
|
|
48
|
+
ttl?: number;
|
|
49
|
+
/**
|
|
50
|
+
* SQL executed once during `open()` after the `kv` table is created.
|
|
51
|
+
* Useful for adding custom tables, indexes, or initial data.
|
|
52
|
+
* Multiple statements must be semicolon-separated.
|
|
53
|
+
*/
|
|
54
|
+
migrateSql?: string;
|
|
55
|
+
}
|
|
56
|
+
/** A SQLite-backed key-value store. */
|
|
57
|
+
interface Store {
|
|
58
|
+
/**
|
|
59
|
+
* Serialises `value` as JSON and upserts it under `key`.
|
|
60
|
+
* If `key` already exists its value (and expiry) are overwritten.
|
|
61
|
+
*/
|
|
62
|
+
put(key: string, value: unknown): void;
|
|
63
|
+
/**
|
|
64
|
+
* Returns the JSON-deserialised value for `key`, or `null` if the key
|
|
65
|
+
* does not exist or has expired.
|
|
66
|
+
*/
|
|
67
|
+
get<T>(key: string): T | null;
|
|
68
|
+
/** Returns the underlying `better-sqlite3` Database instance. */
|
|
69
|
+
db(): Database.Database;
|
|
70
|
+
/** Closes the underlying database connection. */
|
|
71
|
+
close(): void;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Opens (or creates) a SQLite database at `path`, ensures the `kv` table
|
|
75
|
+
* exists, and runs `opts.migrateSql` if supplied.
|
|
76
|
+
*
|
|
77
|
+
* Pass `':memory:'` as `path` for an in-memory database (useful in tests).
|
|
78
|
+
*
|
|
79
|
+
* @param path Filesystem path or `':memory:'`.
|
|
80
|
+
* @param opts Optional TTL and migration SQL.
|
|
81
|
+
*/
|
|
82
|
+
declare function open(path: string, opts?: Options): Store;
|
|
83
|
+
|
|
84
|
+
export { type Options, type Store, open };
|
package/dist/sqlstore.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
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 __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/sqlstore.ts
|
|
31
|
+
var sqlstore_exports = {};
|
|
32
|
+
__export(sqlstore_exports, {
|
|
33
|
+
open: () => open
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(sqlstore_exports);
|
|
36
|
+
var import_better_sqlite3 = __toESM(require("better-sqlite3"));
|
|
37
|
+
var CREATE_KV = `
|
|
38
|
+
CREATE TABLE IF NOT EXISTS kv (
|
|
39
|
+
key TEXT PRIMARY KEY,
|
|
40
|
+
value TEXT NOT NULL,
|
|
41
|
+
expires_at INTEGER
|
|
42
|
+
)
|
|
43
|
+
`;
|
|
44
|
+
function open(path, opts = {}) {
|
|
45
|
+
const database = new import_better_sqlite3.default(path);
|
|
46
|
+
database.exec(CREATE_KV);
|
|
47
|
+
if (opts.migrateSql) {
|
|
48
|
+
database.exec(opts.migrateSql);
|
|
49
|
+
}
|
|
50
|
+
const stmtPutNoExpiry = database.prepare(
|
|
51
|
+
`INSERT INTO kv (key, value, expires_at) VALUES (?, ?, NULL)
|
|
52
|
+
ON CONFLICT(key) DO UPDATE SET value = excluded.value, expires_at = NULL`
|
|
53
|
+
);
|
|
54
|
+
const stmtPutWithExpiry = database.prepare(
|
|
55
|
+
`INSERT INTO kv (key, value, expires_at) VALUES (?, ?, ?)
|
|
56
|
+
ON CONFLICT(key) DO UPDATE SET value = excluded.value, expires_at = excluded.expires_at`
|
|
57
|
+
);
|
|
58
|
+
const stmtGet = database.prepare(`SELECT value, expires_at FROM kv WHERE key = ?`);
|
|
59
|
+
return {
|
|
60
|
+
put(key, value) {
|
|
61
|
+
const json = JSON.stringify(value);
|
|
62
|
+
if (opts.ttl && opts.ttl > 0) {
|
|
63
|
+
stmtPutWithExpiry.run(key, json, Date.now() + opts.ttl);
|
|
64
|
+
} else {
|
|
65
|
+
stmtPutNoExpiry.run(key, json);
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
get(key) {
|
|
69
|
+
const row = stmtGet.get(key);
|
|
70
|
+
if (row == null) return null;
|
|
71
|
+
if (row.expires_at !== null && row.expires_at <= Date.now()) return null;
|
|
72
|
+
return JSON.parse(row.value);
|
|
73
|
+
},
|
|
74
|
+
db() {
|
|
75
|
+
return database;
|
|
76
|
+
},
|
|
77
|
+
close() {
|
|
78
|
+
database.close();
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
83
|
+
0 && (module.exports = {
|
|
84
|
+
open
|
|
85
|
+
});
|
package/dist/stream.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module stream
|
|
3
|
+
* @package @hop-top/kit
|
|
4
|
+
*
|
|
5
|
+
* Factor 3 — Stream and Exit Discipline.
|
|
6
|
+
*
|
|
7
|
+
* Structured data goes to stdout; human-oriented messages
|
|
8
|
+
* (logs, progress, prompts) go to stderr. Exit codes are
|
|
9
|
+
* semantic, not just 0/1.
|
|
10
|
+
*/
|
|
11
|
+
declare enum ExitCode {
|
|
12
|
+
OK = 0,
|
|
13
|
+
Error = 1,
|
|
14
|
+
Usage = 2,
|
|
15
|
+
NotFound = 3,
|
|
16
|
+
Conflict = 4,
|
|
17
|
+
Auth = 5,
|
|
18
|
+
Permission = 6,
|
|
19
|
+
Timeout = 7,
|
|
20
|
+
Cancelled = 8
|
|
21
|
+
}
|
|
22
|
+
interface StreamWriter {
|
|
23
|
+
/** stdout — structured/parseable output */
|
|
24
|
+
data: NodeJS.WritableStream;
|
|
25
|
+
/** stderr — logs, progress, human messages */
|
|
26
|
+
human: NodeJS.WritableStream;
|
|
27
|
+
isTTY: boolean;
|
|
28
|
+
}
|
|
29
|
+
interface StreamWriterOptions {
|
|
30
|
+
data?: NodeJS.WritableStream;
|
|
31
|
+
human?: NodeJS.WritableStream;
|
|
32
|
+
}
|
|
33
|
+
declare function createStreamWriter(opts?: StreamWriterOptions): StreamWriter;
|
|
34
|
+
|
|
35
|
+
export { ExitCode, type StreamWriter, type StreamWriterOptions, createStreamWriter };
|
package/dist/stream.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/stream.ts
|
|
21
|
+
var stream_exports = {};
|
|
22
|
+
__export(stream_exports, {
|
|
23
|
+
ExitCode: () => ExitCode,
|
|
24
|
+
createStreamWriter: () => createStreamWriter
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(stream_exports);
|
|
27
|
+
var ExitCode = /* @__PURE__ */ ((ExitCode2) => {
|
|
28
|
+
ExitCode2[ExitCode2["OK"] = 0] = "OK";
|
|
29
|
+
ExitCode2[ExitCode2["Error"] = 1] = "Error";
|
|
30
|
+
ExitCode2[ExitCode2["Usage"] = 2] = "Usage";
|
|
31
|
+
ExitCode2[ExitCode2["NotFound"] = 3] = "NotFound";
|
|
32
|
+
ExitCode2[ExitCode2["Conflict"] = 4] = "Conflict";
|
|
33
|
+
ExitCode2[ExitCode2["Auth"] = 5] = "Auth";
|
|
34
|
+
ExitCode2[ExitCode2["Permission"] = 6] = "Permission";
|
|
35
|
+
ExitCode2[ExitCode2["Timeout"] = 7] = "Timeout";
|
|
36
|
+
ExitCode2[ExitCode2["Cancelled"] = 8] = "Cancelled";
|
|
37
|
+
return ExitCode2;
|
|
38
|
+
})(ExitCode || {});
|
|
39
|
+
function createStreamWriter(opts) {
|
|
40
|
+
const data = opts?.data ?? process.stdout;
|
|
41
|
+
const human = opts?.human ?? process.stderr;
|
|
42
|
+
return {
|
|
43
|
+
data,
|
|
44
|
+
human,
|
|
45
|
+
isTTY: !!process.stdout.isTTY
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
49
|
+
0 && (module.exports = {
|
|
50
|
+
ExitCode,
|
|
51
|
+
createStreamWriter
|
|
52
|
+
});
|