@dr-co/dsh-secret-form 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +264 -2
- package/index.mjs +442 -0
- package/lib/forms.mjs +318 -0
- package/lib/i18n.mjs +458 -0
- package/lib/page.mjs +318 -0
- package/lib/targets.mjs +222 -0
- package/lib/yaml.mjs +252 -0
- package/package.json +41 -4
package/lib/yaml.mjs
ADDED
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
// A block-style YAML reader and writer, limited to what a secret form needs.
|
|
2
|
+
//
|
|
3
|
+
// The form sets exactly one dotted path inside a document someone already keeps
|
|
4
|
+
// by hand. A real YAML implementation would be a dependency, and a partial one
|
|
5
|
+
// that silently mangles a document is worse than a refusal: everything this
|
|
6
|
+
// reader does not understand (sequences, flow collections, block scalars,
|
|
7
|
+
// anchors, tags, multiple documents) throws instead of being re-serialised
|
|
8
|
+
// losslessly-but-wrongly.
|
|
9
|
+
//
|
|
10
|
+
// Leaf values keep their original scalar spelling, so setting one path never
|
|
11
|
+
// turns `port: 8080` into `port: "8080"`. Comments are the one thing the rewrite
|
|
12
|
+
// drops; the README says so.
|
|
13
|
+
|
|
14
|
+
const RESERVED_WORD = /^(?:true|false|yes|no|on|off|y|n|null|~)$/i;
|
|
15
|
+
const NUMERIC = /^[-+]?(?:\d[\d_]*)?(?:\.\d*)?(?:[eE][-+]?\d+)?$/;
|
|
16
|
+
const PLAIN_KEY = /^[A-Za-z_][A-Za-z0-9_.-]*$/;
|
|
17
|
+
const PLAIN_VALUE = /^[A-Za-z_][A-Za-z0-9_./@+-]*$/;
|
|
18
|
+
|
|
19
|
+
function unsupported(source, reason) {
|
|
20
|
+
const error = new TypeError(`${source} is not supported as a YAML target: ${reason}`);
|
|
21
|
+
error.code = 'secret-form-yaml-unsupported';
|
|
22
|
+
return error;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function isPlain(text) {
|
|
26
|
+
return PLAIN_VALUE.test(text) && !RESERVED_WORD.test(text) && !NUMERIC.test(text);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function quoted(text) {
|
|
30
|
+
// JSON strings are valid YAML double-quoted scalars and carry every escape a
|
|
31
|
+
// secret can need (newlines, tabs, backslashes, quotes).
|
|
32
|
+
return JSON.stringify(text);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Serialise a new string value so a YAML reader gets the same string back. */
|
|
36
|
+
export function yamlScalar(value) {
|
|
37
|
+
if (value === '') return '""';
|
|
38
|
+
return isPlain(value) ? value : quoted(value);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function emitKey(key) {
|
|
42
|
+
return PLAIN_KEY.test(key) && !RESERVED_WORD.test(key) && !NUMERIC.test(key) ? key : quoted(key);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function stripComment(text) {
|
|
46
|
+
let single = false;
|
|
47
|
+
let double = false;
|
|
48
|
+
for (let index = 0; index < text.length; index += 1) {
|
|
49
|
+
const char = text[index];
|
|
50
|
+
if (double) {
|
|
51
|
+
if (char === '\\') index += 1;
|
|
52
|
+
else if (char === '"') double = false;
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
if (single) {
|
|
56
|
+
if (char === "'") {
|
|
57
|
+
if (text[index + 1] === "'") index += 1;
|
|
58
|
+
else single = false;
|
|
59
|
+
}
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
if (char === '"') {
|
|
63
|
+
double = true;
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
if (char === "'") {
|
|
67
|
+
single = true;
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
if (char === '#' && (index === 0 || text[index - 1] === ' ' || text[index - 1] === '\t')) {
|
|
71
|
+
return text.slice(0, index).trimEnd();
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return text.trimEnd();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function unquote(text) {
|
|
78
|
+
const trimmed = text.trim();
|
|
79
|
+
if (trimmed.length >= 2 && trimmed.startsWith('"') && trimmed.endsWith('"')) {
|
|
80
|
+
try {
|
|
81
|
+
return JSON.parse(trimmed);
|
|
82
|
+
} catch {
|
|
83
|
+
return trimmed.slice(1, -1);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (trimmed.length >= 2 && trimmed.startsWith("'") && trimmed.endsWith("'")) {
|
|
87
|
+
return trimmed.slice(1, -1).replaceAll("''", "'");
|
|
88
|
+
}
|
|
89
|
+
return trimmed;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function splitEntry(body, source, lineNumber) {
|
|
93
|
+
let single = false;
|
|
94
|
+
let double = false;
|
|
95
|
+
for (let index = 0; index < body.length; index += 1) {
|
|
96
|
+
const char = body[index];
|
|
97
|
+
if (double) {
|
|
98
|
+
if (char === '\\') index += 1;
|
|
99
|
+
else if (char === '"') double = false;
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
if (single) {
|
|
103
|
+
if (char === "'") {
|
|
104
|
+
if (body[index + 1] === "'") index += 1;
|
|
105
|
+
else single = false;
|
|
106
|
+
}
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
if (char === '"') {
|
|
110
|
+
double = true;
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
if (char === "'") {
|
|
114
|
+
single = true;
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
if (char === ':' && (index + 1 === body.length || body[index + 1] === ' ' || body[index + 1] === '\t')) {
|
|
118
|
+
const key = unquote(body.slice(0, index));
|
|
119
|
+
if (key === '') throw unsupported(source, `line ${lineNumber} has an empty key`);
|
|
120
|
+
const rest = stripComment(body.slice(index + 1)).trim();
|
|
121
|
+
return { key, raw: rest };
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
throw unsupported(source, `line ${lineNumber} is not a "key: value" entry`);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function collectLines(text, source) {
|
|
128
|
+
const raw = text.split(/\r?\n/);
|
|
129
|
+
const lines = [];
|
|
130
|
+
for (let index = 0; index < raw.length; index += 1) {
|
|
131
|
+
const line = raw[index];
|
|
132
|
+
const lineNumber = index + 1;
|
|
133
|
+
if (line.trim() === '') continue;
|
|
134
|
+
if (line.trimStart().startsWith('#')) continue;
|
|
135
|
+
if (/^[ \t]*\t/.test(line)) throw unsupported(source, `line ${lineNumber} is indented with a tab`);
|
|
136
|
+
if (/^(?:---|\.\.\.)(?:\s|$)/.test(line.trim())) throw unsupported(source, 'multi-document markers are not supported');
|
|
137
|
+
const indent = /^ */.exec(line)[0].length;
|
|
138
|
+
const body = line.slice(indent);
|
|
139
|
+
if (/^-(\s|$)/.test(body)) throw unsupported(source, `line ${lineNumber} opens a sequence`);
|
|
140
|
+
if (/^[?&*!|>%@`]/.test(body)) throw unsupported(source, `line ${lineNumber} uses YAML syntax this writer does not keep`);
|
|
141
|
+
const entry = splitEntry(body, source, lineNumber);
|
|
142
|
+
if (/^[?&*!|>%@`{]/.test(entry.raw) || entry.raw.startsWith('[')) {
|
|
143
|
+
throw unsupported(source, `line ${lineNumber} has a value this writer does not keep`);
|
|
144
|
+
}
|
|
145
|
+
lines.push({ indent, lineNumber, key: entry.key, raw: entry.raw });
|
|
146
|
+
}
|
|
147
|
+
return lines;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function buildMap(lines, start, indent, source) {
|
|
151
|
+
const node = { type: 'map', entries: [] };
|
|
152
|
+
const keys = new Set();
|
|
153
|
+
let index = start;
|
|
154
|
+
while (index < lines.length) {
|
|
155
|
+
const line = lines[index];
|
|
156
|
+
if (line.indent < indent) break;
|
|
157
|
+
if (line.indent > indent) throw unsupported(source, `line ${line.lineNumber} is indented without a parent key`);
|
|
158
|
+
if (keys.has(line.key)) throw unsupported(source, `line ${line.lineNumber} repeats the key "${line.key}"`);
|
|
159
|
+
keys.add(line.key);
|
|
160
|
+
index += 1;
|
|
161
|
+
if (line.raw === '') {
|
|
162
|
+
let child = { type: 'map', entries: [] };
|
|
163
|
+
if (index < lines.length && lines[index].indent > indent) {
|
|
164
|
+
const nested = buildMap(lines, index, lines[index].indent, source);
|
|
165
|
+
child = nested.node;
|
|
166
|
+
index = nested.next;
|
|
167
|
+
}
|
|
168
|
+
node.entries.push({ key: line.key, node: child });
|
|
169
|
+
} else {
|
|
170
|
+
node.entries.push({ key: line.key, node: { type: 'scalar', raw: line.raw } });
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return { node, next: index };
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Read a block-style YAML mapping.
|
|
178
|
+
* @param text - document text; an empty or comment-only document reads as an empty mapping.
|
|
179
|
+
* @param source - file name used in error messages.
|
|
180
|
+
* @returns mapping node: `{ type: 'map', entries: [{ key, node }] }`.
|
|
181
|
+
*/
|
|
182
|
+
export function parseYamlMapping(text, source = 'the YAML file') {
|
|
183
|
+
if (typeof text !== 'string') throw unsupported(source, 'the document is not text');
|
|
184
|
+
const lines = collectLines(text, source);
|
|
185
|
+
if (lines.length === 0) return { type: 'map', entries: [] };
|
|
186
|
+
const parsed = buildMap(lines, 0, lines[0].indent, source);
|
|
187
|
+
if (parsed.next !== lines.length) {
|
|
188
|
+
throw unsupported(source, `line ${lines[parsed.next].lineNumber} is indented less than the first key`);
|
|
189
|
+
}
|
|
190
|
+
return parsed.node;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** Serialise a mapping node back to block-style YAML text. */
|
|
194
|
+
export function stringifyYamlMapping(root) {
|
|
195
|
+
const lines = [];
|
|
196
|
+
const emit = (node, indent) => {
|
|
197
|
+
const pad = ' '.repeat(indent);
|
|
198
|
+
for (const entry of node.entries) {
|
|
199
|
+
if (entry.node.type === 'map') {
|
|
200
|
+
if (entry.node.entries.length === 0) {
|
|
201
|
+
lines.push(`${pad}${emitKey(entry.key)}:`);
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
lines.push(`${pad}${emitKey(entry.key)}:`);
|
|
205
|
+
emit(entry.node, indent + 2);
|
|
206
|
+
} else if (entry.node.raw === '') {
|
|
207
|
+
lines.push(`${pad}${emitKey(entry.key)}:`);
|
|
208
|
+
} else {
|
|
209
|
+
lines.push(`${pad}${emitKey(entry.key)}: ${entry.node.raw}`);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
emit(root, 0);
|
|
214
|
+
return lines.length === 0 ? '' : `${lines.join('\n')}\n`;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Set one dotted path inside a mapping node, creating nested mappings.
|
|
219
|
+
* @param root - mapping node from {@link parseYamlMapping}.
|
|
220
|
+
* @param segments - dotted path already split; each segment becomes one key.
|
|
221
|
+
* @param value - string leaf to store.
|
|
222
|
+
* @param source - file name used in error messages.
|
|
223
|
+
*/
|
|
224
|
+
export function setYamlPath(root, segments, value, source = 'the YAML file') {
|
|
225
|
+
let node = root;
|
|
226
|
+
const walk = segments.slice(0, -1);
|
|
227
|
+
for (const segment of walk) {
|
|
228
|
+
let entry = node.entries.find((candidate) => candidate.key === segment);
|
|
229
|
+
if (entry === undefined) {
|
|
230
|
+
const created = { type: 'map', entries: [] };
|
|
231
|
+
entry = { key: segment, node: created };
|
|
232
|
+
node.entries.push(entry);
|
|
233
|
+
node = created;
|
|
234
|
+
continue;
|
|
235
|
+
}
|
|
236
|
+
if (entry.node.type === 'scalar') {
|
|
237
|
+
if (entry.node.raw !== '') {
|
|
238
|
+
throw unsupported(source, `"${segment}" already holds a value, not a mapping`);
|
|
239
|
+
}
|
|
240
|
+
// An empty `key:` reads as null, which is the natural place to grow a mapping.
|
|
241
|
+
entry.node = { type: 'map', entries: [] };
|
|
242
|
+
}
|
|
243
|
+
node = entry.node;
|
|
244
|
+
}
|
|
245
|
+
const last = segments[segments.length - 1];
|
|
246
|
+
const leaf = node.entries.find((candidate) => candidate.key === last);
|
|
247
|
+
const scalar = { type: 'scalar', raw: yamlScalar(value) };
|
|
248
|
+
if (leaf === undefined) node.entries.push({ key: last, node: scalar });
|
|
249
|
+
else if (leaf.node.type === 'map' && leaf.node.entries.length > 0) {
|
|
250
|
+
throw unsupported(source, `"${last}" already holds a mapping; refusing to replace it`);
|
|
251
|
+
} else leaf.node = scalar;
|
|
252
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,43 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dr-co/dsh-secret-form",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "One-time secret links for DeepSeek Harness: the model asks, the human types into a web form, the value lands in the credential store or a file.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./index.mjs",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./index.mjs",
|
|
9
|
+
"./package.json": "./package.json"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"index.mjs",
|
|
13
|
+
"lib/",
|
|
14
|
+
"README.md",
|
|
15
|
+
"LICENSE"
|
|
16
|
+
],
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"scripts": {
|
|
19
|
+
"test": "node --test test/*.test.mjs"
|
|
20
|
+
},
|
|
21
|
+
"keywords": [
|
|
22
|
+
"deepseek-harness",
|
|
23
|
+
"dsh",
|
|
24
|
+
"dsh-plugin",
|
|
25
|
+
"secret",
|
|
26
|
+
"credentials"
|
|
27
|
+
],
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": ">=22"
|
|
30
|
+
},
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public"
|
|
33
|
+
},
|
|
34
|
+
"repository": {
|
|
35
|
+
"type": "git",
|
|
36
|
+
"url": "git+https://github.com/unera/dsh-secret-form.git"
|
|
37
|
+
},
|
|
38
|
+
"homepage": "https://github.com/unera/dsh-secret-form#readme",
|
|
39
|
+
"bugs": {
|
|
40
|
+
"url": "https://github.com/unera/dsh-secret-form/issues"
|
|
41
|
+
},
|
|
42
|
+
"author": "Dmitry Oboukhov"
|
|
43
|
+
}
|