@namzu/sdk 64.0.0 → 64.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/CHANGELOG.md +43 -0
- package/dist/agents/file-definitions.d.ts +10 -0
- package/dist/agents/file-definitions.d.ts.map +1 -1
- package/dist/agents/file-definitions.js +10 -2
- package/dist/agents/file-definitions.js.map +1 -1
- package/dist/plugin/file-change-hooks.d.ts +53 -0
- package/dist/plugin/file-change-hooks.d.ts.map +1 -0
- package/dist/plugin/file-change-hooks.js +111 -0
- package/dist/plugin/file-change-hooks.js.map +1 -0
- package/dist/plugin/file-change-watch.d.ts +111 -0
- package/dist/plugin/file-change-watch.d.ts.map +1 -0
- package/dist/plugin/file-change-watch.js +324 -0
- package/dist/plugin/file-change-watch.js.map +1 -0
- package/dist/plugin/index.d.ts +10 -1
- package/dist/plugin/index.d.ts.map +1 -1
- package/dist/plugin/index.js +7 -1
- package/dist/plugin/index.js.map +1 -1
- package/dist/plugin/lifecycle.d.ts +60 -1
- package/dist/plugin/lifecycle.d.ts.map +1 -1
- package/dist/plugin/lifecycle.js +118 -7
- package/dist/plugin/lifecycle.js.map +1 -1
- package/dist/plugin/loader.d.ts.map +1 -1
- package/dist/plugin/loader.js +24 -1
- package/dist/plugin/loader.js.map +1 -1
- package/dist/plugin/session-hook-env.d.ts +140 -0
- package/dist/plugin/session-hook-env.d.ts.map +1 -0
- package/dist/plugin/session-hook-env.js +458 -0
- package/dist/plugin/session-hook-env.js.map +1 -0
- package/dist/plugin/shell-hook-env.d.ts +107 -0
- package/dist/plugin/shell-hook-env.d.ts.map +1 -0
- package/dist/plugin/shell-hook-env.js +272 -0
- package/dist/plugin/shell-hook-env.js.map +1 -0
- package/dist/plugin/shell-hook.d.ts +105 -2
- package/dist/plugin/shell-hook.d.ts.map +1 -1
- package/dist/plugin/shell-hook.js +282 -24
- package/dist/plugin/shell-hook.js.map +1 -1
- package/dist/plugin/worktree-hook-answer.d.ts +26 -0
- package/dist/plugin/worktree-hook-answer.d.ts.map +1 -0
- package/dist/plugin/worktree-hook-answer.js +132 -0
- package/dist/plugin/worktree-hook-answer.js.map +1 -0
- package/dist/public-runtime.d.ts +3 -3
- package/dist/public-runtime.d.ts.map +1 -1
- package/dist/public-runtime.js +3 -3
- package/dist/public-runtime.js.map +1 -1
- package/dist/public-types.d.ts +7 -1
- package/dist/public-types.d.ts.map +1 -1
- package/dist/runtime/query/executor.d.ts +5 -0
- package/dist/runtime/query/executor.d.ts.map +1 -1
- package/dist/runtime/query/executor.js +3 -0
- package/dist/runtime/query/executor.js.map +1 -1
- package/dist/runtime/query/index.d.ts +9 -0
- package/dist/runtime/query/index.d.ts.map +1 -1
- package/dist/runtime/query/index.js +3 -0
- package/dist/runtime/query/index.js.map +1 -1
- package/dist/runtime/query/tooling.d.ts +5 -0
- package/dist/runtime/query/tooling.d.ts.map +1 -1
- package/dist/runtime/query/tooling.js +3 -0
- package/dist/runtime/query/tooling.js.map +1 -1
- package/dist/session/workspace/hooked-driver.d.ts +72 -0
- package/dist/session/workspace/hooked-driver.d.ts.map +1 -0
- package/dist/session/workspace/hooked-driver.js +204 -0
- package/dist/session/workspace/hooked-driver.js.map +1 -0
- package/dist/session/workspace/index.d.ts +2 -0
- package/dist/session/workspace/index.d.ts.map +1 -1
- package/dist/session/workspace/index.js +1 -0
- package/dist/session/workspace/index.js.map +1 -1
- package/dist/skills/index.d.ts +1 -1
- package/dist/skills/index.d.ts.map +1 -1
- package/dist/skills/index.js +1 -1
- package/dist/skills/index.js.map +1 -1
- package/dist/skills/loader.d.ts +22 -0
- package/dist/skills/loader.d.ts.map +1 -1
- package/dist/skills/loader.js +41 -0
- package/dist/skills/loader.js.map +1 -1
- package/dist/tools/builtins/bash-cwd.d.ts +6 -1
- package/dist/tools/builtins/bash-cwd.d.ts.map +1 -1
- package/dist/tools/builtins/bash-cwd.js +27 -2
- package/dist/tools/builtins/bash-cwd.js.map +1 -1
- package/dist/tools/builtins/bash.js +1 -1
- package/dist/tools/builtins/bash.js.map +1 -1
- package/dist/tools/builtins/skill.d.ts.map +1 -1
- package/dist/tools/builtins/skill.js +93 -16
- package/dist/tools/builtins/skill.js.map +1 -1
- package/dist/types/plugin/index.d.ts +75 -2
- package/dist/types/plugin/index.d.ts.map +1 -1
- package/dist/types/plugin/index.js +5 -0
- package/dist/types/plugin/index.js.map +1 -1
- package/dist/types/skills/index.d.ts +8 -0
- package/dist/types/skills/index.d.ts.map +1 -1
- package/dist/types/skills/index.js.map +1 -1
- package/dist/types/tool/index.d.ts +68 -0
- package/dist/types/tool/index.d.ts.map +1 -1
- package/dist/types/tool/index.js.map +1 -1
- package/dist/types/workspace/ref.d.ts +6 -0
- package/dist/types/workspace/ref.d.ts.map +1 -1
- package/dist/utils/frontmatter.d.ts +16 -0
- package/dist/utils/frontmatter.d.ts.map +1 -1
- package/dist/utils/frontmatter.js +31 -1
- package/dist/utils/frontmatter.js.map +1 -1
- package/package.json +1 -1
- package/src/agents/file-definitions.ts +17 -2
- package/src/plugin/file-change-hooks.ts +131 -0
- package/src/plugin/file-change-watch.ts +367 -0
- package/src/plugin/index.ts +39 -1
- package/src/plugin/lifecycle.ts +162 -8
- package/src/plugin/loader.ts +22 -1
- package/src/plugin/session-hook-env.ts +475 -0
- package/src/plugin/shell-hook-env.ts +335 -0
- package/src/plugin/shell-hook.ts +416 -24
- package/src/plugin/worktree-hook-answer.ts +142 -0
- package/src/public-runtime.ts +32 -0
- package/src/public-types.ts +20 -1
- package/src/runtime/query/executor.ts +8 -0
- package/src/runtime/query/index.ts +12 -0
- package/src/runtime/query/tooling.ts +8 -0
- package/src/session/workspace/hooked-driver.ts +272 -0
- package/src/session/workspace/index.ts +7 -0
- package/src/skills/index.ts +3 -0
- package/src/skills/loader.ts +47 -0
- package/src/tools/builtins/bash-cwd.ts +25 -1
- package/src/tools/builtins/bash.ts +1 -1
- package/src/tools/builtins/skill.ts +115 -26
- package/src/types/plugin/index.ts +84 -1
- package/src/types/skills/index.ts +9 -0
- package/src/types/tool/index.ts +74 -0
- package/src/types/workspace/ref.ts +6 -0
- package/src/utils/frontmatter.ts +52 -1
package/src/plugin/loader.ts
CHANGED
|
@@ -114,7 +114,28 @@ export async function loadPluginManifest(
|
|
|
114
114
|
*/
|
|
115
115
|
export function parsePluginManifestText(raw: string): PluginManifest {
|
|
116
116
|
const parsed: unknown = JSON.parse(raw)
|
|
117
|
-
return PluginManifestSchema.parse(parsed)
|
|
117
|
+
return PluginManifestSchema.parse(normalizeManifestHooks(parsed))
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* The two ways a plugin written for the common hook contract names its hook
|
|
122
|
+
* entries, read as the keys this manifest has: `"hooks": "./hooks/hooks.json"`
|
|
123
|
+
* (one file, as a text) is a list of one path, and `"hooks": { <events> }`
|
|
124
|
+
* (the events object written inline) is `hookEntries`. A manifest that already
|
|
125
|
+
* has `hookEntries` next to an inline `hooks` object is left as written, so the
|
|
126
|
+
* schema says what is wrong rather than one of the two being dropped.
|
|
127
|
+
*/
|
|
128
|
+
function normalizeManifestHooks(parsed: unknown): unknown {
|
|
129
|
+
if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) return parsed
|
|
130
|
+
const manifest = parsed as Record<string, unknown>
|
|
131
|
+
const hooks = manifest.hooks
|
|
132
|
+
if (typeof hooks === 'string') return { ...manifest, hooks: [hooks] }
|
|
133
|
+
if (hooks !== null && typeof hooks === 'object' && !Array.isArray(hooks)) {
|
|
134
|
+
if (manifest.hookEntries !== undefined) return parsed
|
|
135
|
+
const { hooks: inline, ...rest } = manifest
|
|
136
|
+
return { ...rest, hookEntries: inline }
|
|
137
|
+
}
|
|
138
|
+
return parsed
|
|
118
139
|
}
|
|
119
140
|
|
|
120
141
|
async function discoverScopedPlugins(
|
|
@@ -0,0 +1,475 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Variables a hook sets for the commands that run after it.
|
|
3
|
+
*
|
|
4
|
+
* A person's shell picks up a project's environment when they enter its
|
|
5
|
+
* folder. A hook on `session_start`, `cwd_changed` or `file_changed` can do
|
|
6
|
+
* the same for the session's shell commands: it is started with
|
|
7
|
+
* `NAMZU_ENV_FILE`, the path of a file kept for the session outside the
|
|
8
|
+
* project, and appends lines of the form `export NAME=value` to it. The
|
|
9
|
+
* commands the shell tool runs afterwards start with those variables.
|
|
10
|
+
*
|
|
11
|
+
* ## What is read
|
|
12
|
+
*
|
|
13
|
+
* The file is read as data and never run. A line is, after leading white
|
|
14
|
+
* space, `export NAME=value` or `NAME=value`; blank lines and lines starting
|
|
15
|
+
* with `#` are skipped. A value is a run of text with no white space or shell
|
|
16
|
+
* punctuation, a single-quoted string (taken literally), or a double-quoted
|
|
17
|
+
* string (`\"`, `\\`, `\$` and a backquote escaped are read as the character
|
|
18
|
+
* itself); the three may be joined (`"a b"'c'd`). `$NAME` and `${NAME}` are
|
|
19
|
+
* replaced by the value the name has in the environment the session was
|
|
20
|
+
* started with or was set earlier (in this file or by an earlier kind of hook
|
|
21
|
+
* below), and by nothing when it has none. Nothing else is expanded: a line
|
|
22
|
+
* with `$(...)`, a backquote, `$((...))`, `${NAME:-x}` or any other
|
|
23
|
+
* parameter form is not read at all, and is reported instead, so a command is
|
|
24
|
+
* never run by us on a hook's behalf. A trailing ` # comment` is ignored.
|
|
25
|
+
*
|
|
26
|
+
* ## Which line wins
|
|
27
|
+
*
|
|
28
|
+
* A later line for the same name replaces an earlier one, and when two hooks
|
|
29
|
+
* of one kind set the same name the one that comes later in the configuration
|
|
30
|
+
* wins, however long each took (each hook has a file of its own, read in
|
|
31
|
+
* configuration order). There is no unset:
|
|
32
|
+
* to remove a variable, set it empty. The three kinds of hook have a file
|
|
33
|
+
* each, and a later kind outranks an earlier one: `session_start`, then
|
|
34
|
+
* `cwd_changed`, then `file_changed`. A change of the working folder clears
|
|
35
|
+
* what `cwd_changed` and `file_changed` hooks set (they describe the folder
|
|
36
|
+
* that was left) before the `cwd_changed` hooks run again, and keeps what
|
|
37
|
+
* `session_start` hooks set.
|
|
38
|
+
*
|
|
39
|
+
* ## Bounds
|
|
40
|
+
*
|
|
41
|
+
* Only the first 64 KiB of a file is read. A value longer than 8 KiB, a name
|
|
42
|
+
* longer than 128 characters and a variable beyond the 128th are left out
|
|
43
|
+
* and reported.
|
|
44
|
+
*
|
|
45
|
+
* ## Which names are refused
|
|
46
|
+
*
|
|
47
|
+
* `PATH` is allowed: it is the point of the feature. Refused, and reported
|
|
48
|
+
* once, are the names that decide what program a command runs without being
|
|
49
|
+
* a program named in it, and the ones that say where Namzu keeps its state:
|
|
50
|
+
* every `NAMZU_*` name, the dynamic loader's (`LD_*`, `DYLD_*`), the shell's
|
|
51
|
+
* start-up and prompt hooks (`BASH_ENV`, `ENV`, `SHELLOPTS`, `BASHOPTS`,
|
|
52
|
+
* `PROMPT_COMMAND`, `PS4`, `IFS`, `CDPATH`, `BASH_FUNC_*`, `SHELL`), and the
|
|
53
|
+
* interpreters' own injection variables (`NODE_OPTIONS`, `PYTHONSTARTUP`,
|
|
54
|
+
* `PYTHONPATH`, `PYTHONHOME`, `PERL5OPT`, `PERL5LIB`, `RUBYOPT`, `RUBYLIB`,
|
|
55
|
+
* `JAVA_TOOL_OPTIONS`), the programs Git runs on its own (`GIT_EXEC_PATH`,
|
|
56
|
+
* `GIT_SSH`, `GIT_SSH_COMMAND`, `GIT_ASKPASS`, `GIT_PROXY_COMMAND`,
|
|
57
|
+
* `GIT_EXTERNAL_DIFF`), the name `__proto__`, plus the places the
|
|
58
|
+
* home of the account and the user-level application folders live
|
|
59
|
+
* (`HOME`, `USERPROFILE`, `XDG_CONFIG_HOME`, `XDG_DATA_HOME`, `XDG_STATE_HOME`,
|
|
60
|
+
* `XDG_CACHE_HOME`).
|
|
61
|
+
*/
|
|
62
|
+
|
|
63
|
+
import { closeSync, mkdirSync, openSync, readSync, rmSync, writeFileSync } from 'node:fs'
|
|
64
|
+
import { join } from 'node:path'
|
|
65
|
+
|
|
66
|
+
import { createTempDirSync } from '../utils/temp.js'
|
|
67
|
+
|
|
68
|
+
/** The events whose hooks may set variables, in the order a later one outranks an earlier one. */
|
|
69
|
+
export const HOOK_ENV_EVENTS = ['session_start', 'cwd_changed', 'file_changed'] as const
|
|
70
|
+
export type HookEnvEvent = (typeof HOOK_ENV_EVENTS)[number]
|
|
71
|
+
|
|
72
|
+
/** The most bytes of a hook's file that are read. */
|
|
73
|
+
export const MAX_HOOK_ENV_FILE_BYTES = 64 * 1024
|
|
74
|
+
/** The longest value kept, in characters. */
|
|
75
|
+
export const MAX_HOOK_ENV_VALUE_CHARS = 8 * 1024
|
|
76
|
+
/** The most variables hooks may have set at once. */
|
|
77
|
+
export const MAX_HOOK_ENV_VARIABLES = 128
|
|
78
|
+
const MAX_NAME_CHARS = 128
|
|
79
|
+
|
|
80
|
+
const NAME = /^[A-Za-z_][A-Za-z0-9_]*$/
|
|
81
|
+
|
|
82
|
+
const REFUSED_NAMES: ReadonlySet<string> = new Set([
|
|
83
|
+
'BASH_ENV',
|
|
84
|
+
'ENV',
|
|
85
|
+
'SHELLOPTS',
|
|
86
|
+
'BASHOPTS',
|
|
87
|
+
'PROMPT_COMMAND',
|
|
88
|
+
'PS4',
|
|
89
|
+
'IFS',
|
|
90
|
+
'CDPATH',
|
|
91
|
+
'NODE_OPTIONS',
|
|
92
|
+
'PYTHONSTARTUP',
|
|
93
|
+
'PERL5OPT',
|
|
94
|
+
'PERL5LIB',
|
|
95
|
+
'RUBYOPT',
|
|
96
|
+
'JAVA_TOOL_OPTIONS',
|
|
97
|
+
'PYTHONPATH',
|
|
98
|
+
'PYTHONHOME',
|
|
99
|
+
'RUBYLIB',
|
|
100
|
+
'SHELL',
|
|
101
|
+
'GIT_EXEC_PATH',
|
|
102
|
+
'GIT_SSH',
|
|
103
|
+
'GIT_SSH_COMMAND',
|
|
104
|
+
'GIT_ASKPASS',
|
|
105
|
+
'GIT_PROXY_COMMAND',
|
|
106
|
+
'GIT_EXTERNAL_DIFF',
|
|
107
|
+
'GCONV_PATH',
|
|
108
|
+
'LOCPATH',
|
|
109
|
+
'NLSPATH',
|
|
110
|
+
'HOSTALIASES',
|
|
111
|
+
'HOME',
|
|
112
|
+
'USERPROFILE',
|
|
113
|
+
'XDG_CONFIG_HOME',
|
|
114
|
+
'XDG_DATA_HOME',
|
|
115
|
+
'XDG_STATE_HOME',
|
|
116
|
+
'XDG_CACHE_HOME',
|
|
117
|
+
])
|
|
118
|
+
|
|
119
|
+
/** Why a hook may not set `name`, in a sentence, or `undefined` when it may. */
|
|
120
|
+
export function refusedHookEnvName(name: string): string | undefined {
|
|
121
|
+
if (name === '__proto__') {
|
|
122
|
+
return `${name} is not a name an environment can hold`
|
|
123
|
+
}
|
|
124
|
+
if (name.startsWith('NAMZU_')) {
|
|
125
|
+
return `${name} is one of Namzu's own settings, and a hook may not change those`
|
|
126
|
+
}
|
|
127
|
+
if (name.startsWith('LD_') || name.startsWith('DYLD_') || name === 'GCONV_PATH') {
|
|
128
|
+
return `${name} is a dynamic loader variable: it changes what code every command loads`
|
|
129
|
+
}
|
|
130
|
+
if (name.startsWith('BASH_FUNC_')) {
|
|
131
|
+
return `${name} defines a shell function that every command would run`
|
|
132
|
+
}
|
|
133
|
+
if (REFUSED_NAMES.has(name)) {
|
|
134
|
+
return `${name} decides what program runs, or where Namzu and the account keep their state`
|
|
135
|
+
}
|
|
136
|
+
return undefined
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const envFileName = (event: HookEnvEvent, slot: number): string =>
|
|
140
|
+
slot === 0 ? `${event}.env` : `${event}.${slot}.env`
|
|
141
|
+
|
|
142
|
+
interface Reading {
|
|
143
|
+
readonly name: string
|
|
144
|
+
readonly value: string
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** What went wrong with one line, or the pair it set. */
|
|
148
|
+
type Line = { readonly ok: Reading } | { readonly skip: true } | { readonly problem: string }
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* One value: quoted and unquoted pieces joined, then only white space or a
|
|
152
|
+
* ` #` comment. Returns the text, or why it cannot be read.
|
|
153
|
+
*/
|
|
154
|
+
function readValue(
|
|
155
|
+
text: string,
|
|
156
|
+
lookup: (name: string) => string,
|
|
157
|
+
): { readonly value: string } | { readonly problem: string } {
|
|
158
|
+
let out = ''
|
|
159
|
+
let i = 0
|
|
160
|
+
const expand = (source: string, at: number): { next: number } | { problem: string } => {
|
|
161
|
+
// `source[at]` is `$`.
|
|
162
|
+
const after = source[at + 1]
|
|
163
|
+
if (after === '(' || after === '`') {
|
|
164
|
+
return { problem: 'it contains a command substitution, which is never run' }
|
|
165
|
+
}
|
|
166
|
+
if (after === '{') {
|
|
167
|
+
const close = source.indexOf('}', at + 2)
|
|
168
|
+
const inner = close < 0 ? '' : source.slice(at + 2, close)
|
|
169
|
+
if (!NAME.test(inner)) {
|
|
170
|
+
return { problem: 'it uses a form of `${...}` other than `${NAME}`, which is not read' }
|
|
171
|
+
}
|
|
172
|
+
out += lookup(inner)
|
|
173
|
+
return { next: close + 1 }
|
|
174
|
+
}
|
|
175
|
+
if (after !== undefined && /[A-Za-z_]/.test(after)) {
|
|
176
|
+
let end = at + 2
|
|
177
|
+
while (end < source.length && /[A-Za-z0-9_]/.test(source[end] as string)) end += 1
|
|
178
|
+
out += lookup(source.slice(at + 1, end))
|
|
179
|
+
return { next: end }
|
|
180
|
+
}
|
|
181
|
+
out += '$'
|
|
182
|
+
return { next: at + 1 }
|
|
183
|
+
}
|
|
184
|
+
while (i < text.length) {
|
|
185
|
+
const ch = text[i] as string
|
|
186
|
+
if (ch === "'") {
|
|
187
|
+
const close = text.indexOf("'", i + 1)
|
|
188
|
+
if (close < 0) return { problem: 'a single quote is never closed' }
|
|
189
|
+
out += text.slice(i + 1, close)
|
|
190
|
+
i = close + 1
|
|
191
|
+
} else if (ch === '"') {
|
|
192
|
+
i += 1
|
|
193
|
+
let closed = false
|
|
194
|
+
while (i < text.length) {
|
|
195
|
+
const c = text[i] as string
|
|
196
|
+
if (c === '"') {
|
|
197
|
+
closed = true
|
|
198
|
+
i += 1
|
|
199
|
+
break
|
|
200
|
+
}
|
|
201
|
+
if (c === '\\' && i + 1 < text.length) {
|
|
202
|
+
const nextChar = text[i + 1] as string
|
|
203
|
+
if (nextChar === '"' || nextChar === '\\' || nextChar === '$' || nextChar === '`') {
|
|
204
|
+
out += nextChar
|
|
205
|
+
i += 2
|
|
206
|
+
continue
|
|
207
|
+
}
|
|
208
|
+
out += c
|
|
209
|
+
i += 1
|
|
210
|
+
continue
|
|
211
|
+
}
|
|
212
|
+
if (c === '`') return { problem: 'it contains a command substitution, which is never run' }
|
|
213
|
+
if (c === '$') {
|
|
214
|
+
const step = expand(text, i)
|
|
215
|
+
if ('problem' in step) return step
|
|
216
|
+
i = step.next
|
|
217
|
+
continue
|
|
218
|
+
}
|
|
219
|
+
out += c
|
|
220
|
+
i += 1
|
|
221
|
+
}
|
|
222
|
+
if (!closed) return { problem: 'a double quote is never closed' }
|
|
223
|
+
} else if (/\s/.test(ch)) {
|
|
224
|
+
const rest = text.slice(i).trim()
|
|
225
|
+
if (rest === '' || rest.startsWith('#')) break
|
|
226
|
+
return { problem: 'it has text after the value that a shell would read as another word' }
|
|
227
|
+
} else if (ch === '$') {
|
|
228
|
+
const step = expand(text, i)
|
|
229
|
+
if ('problem' in step) return step
|
|
230
|
+
i = step.next
|
|
231
|
+
} else if (ch === '`' || ';&|<>()'.includes(ch)) {
|
|
232
|
+
return { problem: `it has "${ch}" outside quotes, which a shell would act on` }
|
|
233
|
+
} else if (ch === '\\' && i + 1 < text.length) {
|
|
234
|
+
out += text[i + 1] as string
|
|
235
|
+
i += 2
|
|
236
|
+
} else {
|
|
237
|
+
out += ch
|
|
238
|
+
i += 1
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
return { value: out }
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/** One line of a hook's file. */
|
|
245
|
+
export function readHookEnvLine(raw: string, lookup: (name: string) => string): Line {
|
|
246
|
+
const line = raw.trim()
|
|
247
|
+
if (line === '' || line.startsWith('#')) return { skip: true }
|
|
248
|
+
const body = /^export\s+/.test(line) ? line.replace(/^export\s+/, '') : line
|
|
249
|
+
const eq = body.indexOf('=')
|
|
250
|
+
if (eq <= 0) {
|
|
251
|
+
// `export NAME` re-exports what is already there: nothing to carry.
|
|
252
|
+
return NAME.test(body) && body !== line ? { skip: true } : { problem: 'it is not NAME=value' }
|
|
253
|
+
}
|
|
254
|
+
const name = body.slice(0, eq)
|
|
255
|
+
if (!NAME.test(name) || name.length > MAX_NAME_CHARS) {
|
|
256
|
+
return { problem: `"${name.slice(0, 40)}" is not a variable name` }
|
|
257
|
+
}
|
|
258
|
+
const refusal = refusedHookEnvName(name)
|
|
259
|
+
if (refusal !== undefined) return { problem: refusal }
|
|
260
|
+
const value = readValue(body.slice(eq + 1), lookup)
|
|
261
|
+
if ('problem' in value) return { problem: value.problem }
|
|
262
|
+
if (value.value.length > MAX_HOOK_ENV_VALUE_CHARS) {
|
|
263
|
+
return { problem: `its value is longer than ${MAX_HOOK_ENV_VALUE_CHARS} characters` }
|
|
264
|
+
}
|
|
265
|
+
if (value.value.includes('\0')) return { problem: 'its value has a NUL character' }
|
|
266
|
+
return { ok: { name, value: value.value } }
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
export interface SessionHookEnvOptions {
|
|
270
|
+
/** The environment `$NAME` is read from besides what hooks set. Default `process.env`. */
|
|
271
|
+
readonly inherited?: () => NodeJS.ProcessEnv
|
|
272
|
+
/** Called once for each distinct thing that was not read or not allowed. */
|
|
273
|
+
readonly onProblem?: (message: string) => void
|
|
274
|
+
/** Where the files go; default a private folder under Namzu's own scratch space, made on first use. */
|
|
275
|
+
readonly directory?: () => string
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* The variables hooks have set in one session. `variables` is one live
|
|
280
|
+
* object, changed in place: hand it to the shell tool as its environment and
|
|
281
|
+
* every command starts with what hooks have set so far.
|
|
282
|
+
*/
|
|
283
|
+
export class SessionHookEnv {
|
|
284
|
+
/** What hooks have set, live. Do not write to it. */
|
|
285
|
+
// No prototype, so a variable called `__proto__` is a variable like any other.
|
|
286
|
+
readonly variables: Record<string, string> = Object.create(null)
|
|
287
|
+
/** Per kind of hook, one layer for each hook that has a file, by the hook's place in the configuration. */
|
|
288
|
+
private readonly layers = new Map<HookEnvEvent, Map<number, Map<string, string>>>()
|
|
289
|
+
private readonly slots = new Map<HookEnvEvent, Set<number>>()
|
|
290
|
+
private readonly reported = new Set<string>()
|
|
291
|
+
private directory: string | undefined
|
|
292
|
+
private disposed = false
|
|
293
|
+
|
|
294
|
+
constructor(private readonly options: SessionHookEnvOptions = {}) {}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* The file a hook of `event` is told to append to; made on first use. Each
|
|
298
|
+
* hook has a file of its own (`slot` is its place in the configuration), so
|
|
299
|
+
* hooks that run side by side never interleave their lines and a variable
|
|
300
|
+
* two of them set takes the value of the one that comes later in the file.
|
|
301
|
+
*/
|
|
302
|
+
fileFor(event: HookEnvEvent, slot = 0): string | undefined {
|
|
303
|
+
if (this.disposed) return undefined
|
|
304
|
+
try {
|
|
305
|
+
if (this.directory === undefined) {
|
|
306
|
+
this.directory = this.options.directory?.() ?? createTempDirSync('hook-env')
|
|
307
|
+
mkdirSync(this.directory, { recursive: true, mode: 0o700 })
|
|
308
|
+
}
|
|
309
|
+
const file = join(this.directory, envFileName(event, slot))
|
|
310
|
+
const known = this.slots.get(event) ?? new Set<number>()
|
|
311
|
+
known.add(slot)
|
|
312
|
+
this.slots.set(event, known)
|
|
313
|
+
// Created empty, owner-only, so a hook that appends finds it there.
|
|
314
|
+
writeFileSync(file, '', { flag: 'a', mode: 0o600 })
|
|
315
|
+
return file
|
|
316
|
+
} catch (error) {
|
|
317
|
+
this.report(
|
|
318
|
+
`Namzu could not make the file hooks set variables in (${error instanceof Error ? error.message : String(error)}), so no hook can set a variable for this session.`,
|
|
319
|
+
)
|
|
320
|
+
return undefined
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Read `event`'s file again and recompute what is set. Call after a hook of
|
|
326
|
+
* that event ends: with its `slot` for that hook's file, without one for
|
|
327
|
+
* every file the event has.
|
|
328
|
+
*/
|
|
329
|
+
absorb(event: HookEnvEvent, slot?: number): void {
|
|
330
|
+
if (this.disposed || this.directory === undefined) return
|
|
331
|
+
const wanted =
|
|
332
|
+
slot === undefined ? [...(this.slots.get(event) ?? [])].sort((a, b) => a - b) : [slot]
|
|
333
|
+
for (const one of wanted) {
|
|
334
|
+
const text = this.readFile(join(this.directory, envFileName(event, one)))
|
|
335
|
+
if (text === undefined) continue
|
|
336
|
+
const bySlot = this.layers.get(event) ?? new Map<number, Map<string, string>>()
|
|
337
|
+
this.layers.set(event, bySlot)
|
|
338
|
+
bySlot.set(one, this.parse(event, one, text))
|
|
339
|
+
}
|
|
340
|
+
this.merge()
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/** Forget what `cwd_changed` and `file_changed` hooks set: it described the folder that was left. */
|
|
344
|
+
clearFolderLayers(): void {
|
|
345
|
+
if (this.disposed) return
|
|
346
|
+
for (const event of ['cwd_changed', 'file_changed'] as const) {
|
|
347
|
+
this.layers.delete(event)
|
|
348
|
+
if (this.directory !== undefined) {
|
|
349
|
+
for (const slot of this.slots.get(event) ?? []) {
|
|
350
|
+
try {
|
|
351
|
+
writeFileSync(join(this.directory, envFileName(event, slot)), '', { mode: 0o600 })
|
|
352
|
+
} catch {
|
|
353
|
+
// A file that cannot be emptied is read again at the next absorb.
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
this.merge()
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/** The names set now and the kind of hook that set each, never the values. */
|
|
362
|
+
describe(): readonly { readonly name: string; readonly event: HookEnvEvent }[] {
|
|
363
|
+
const out = new Map<string, HookEnvEvent>()
|
|
364
|
+
for (const event of HOOK_ENV_EVENTS) {
|
|
365
|
+
for (const layer of this.orderedLayers(event)) {
|
|
366
|
+
for (const name of layer.keys()) out.set(name, event)
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
return [...out].map(([name, event]) => ({ name, event }))
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/** Remove the files. The live object is emptied too. */
|
|
373
|
+
dispose(): void {
|
|
374
|
+
this.disposed = true
|
|
375
|
+
this.layers.clear()
|
|
376
|
+
this.merge()
|
|
377
|
+
if (this.directory !== undefined) {
|
|
378
|
+
try {
|
|
379
|
+
rmSync(this.directory, { recursive: true, force: true })
|
|
380
|
+
} catch {
|
|
381
|
+
// Scratch space is cleaned up by the operating system in the end.
|
|
382
|
+
}
|
|
383
|
+
this.directory = undefined
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
private report(message: string): void {
|
|
388
|
+
if (this.reported.has(message)) return
|
|
389
|
+
this.reported.add(message)
|
|
390
|
+
this.options.onProblem?.(message)
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
private readFile(path: string): string | undefined {
|
|
394
|
+
let fd: number | undefined
|
|
395
|
+
try {
|
|
396
|
+
fd = openSync(path, 'r')
|
|
397
|
+
const buffer = Buffer.alloc(MAX_HOOK_ENV_FILE_BYTES + 1)
|
|
398
|
+
const read = readSync(fd, buffer, 0, buffer.length, 0)
|
|
399
|
+
if (read > MAX_HOOK_ENV_FILE_BYTES) {
|
|
400
|
+
this.report(
|
|
401
|
+
`A hook's variables file is longer than ${MAX_HOOK_ENV_FILE_BYTES / 1024} KiB; only the first ${MAX_HOOK_ENV_FILE_BYTES / 1024} KiB is read.`,
|
|
402
|
+
)
|
|
403
|
+
}
|
|
404
|
+
return buffer.subarray(0, Math.min(read, MAX_HOOK_ENV_FILE_BYTES)).toString('utf8')
|
|
405
|
+
} catch {
|
|
406
|
+
return undefined
|
|
407
|
+
} finally {
|
|
408
|
+
if (fd !== undefined) {
|
|
409
|
+
try {
|
|
410
|
+
closeSync(fd)
|
|
411
|
+
} catch {
|
|
412
|
+
// Nothing more to do with a descriptor that will not close.
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
private orderedLayers(event: HookEnvEvent): Map<string, string>[] {
|
|
419
|
+
const bySlot = this.layers.get(event)
|
|
420
|
+
if (bySlot === undefined) return []
|
|
421
|
+
return [...bySlot].sort(([a], [b]) => a - b).map(([, layer]) => layer)
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
private parse(event: HookEnvEvent, slot: number, text: string): Map<string, string> {
|
|
425
|
+
const layer = new Map<string, string>()
|
|
426
|
+
const inherited = this.options.inherited?.() ?? process.env
|
|
427
|
+
// An earlier kind of hook's values are visible to `$NAME`, then this file's own so far.
|
|
428
|
+
const lower = new Map<string, string>()
|
|
429
|
+
for (const earlier of HOOK_ENV_EVENTS) {
|
|
430
|
+
for (const [earlierSlot, entries] of [...(this.layers.get(earlier) ?? [])].sort(
|
|
431
|
+
([a], [b]) => a - b,
|
|
432
|
+
)) {
|
|
433
|
+
if (earlier === event && earlierSlot >= slot) break
|
|
434
|
+
for (const [name, value] of entries) lower.set(name, value)
|
|
435
|
+
}
|
|
436
|
+
if (earlier === event) break
|
|
437
|
+
}
|
|
438
|
+
const lookup = (name: string): string =>
|
|
439
|
+
layer.get(name) ?? lower.get(name) ?? inherited[name] ?? ''
|
|
440
|
+
const lines = text.split(/\r?\n/)
|
|
441
|
+
for (const [index, raw] of lines.entries()) {
|
|
442
|
+
const read = readHookEnvLine(raw, lookup)
|
|
443
|
+
if ('skip' in read) continue
|
|
444
|
+
if ('problem' in read) {
|
|
445
|
+
this.report(
|
|
446
|
+
`A ${event} hook set a variable that was not read (line ${index + 1}): ${read.problem}.`,
|
|
447
|
+
)
|
|
448
|
+
continue
|
|
449
|
+
}
|
|
450
|
+
layer.set(read.ok.name, read.ok.value)
|
|
451
|
+
}
|
|
452
|
+
return layer
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
private merge(): void {
|
|
456
|
+
for (const key of Object.keys(this.variables)) delete this.variables[key]
|
|
457
|
+
let count = 0
|
|
458
|
+
for (const event of HOOK_ENV_EVENTS) {
|
|
459
|
+
for (const layer of this.orderedLayers(event)) {
|
|
460
|
+
for (const [name, value] of layer) {
|
|
461
|
+
if (!(name in this.variables)) {
|
|
462
|
+
if (count >= MAX_HOOK_ENV_VARIABLES) {
|
|
463
|
+
this.report(
|
|
464
|
+
`Hooks have set more than ${MAX_HOOK_ENV_VARIABLES} variables; the ones past that are left out.`,
|
|
465
|
+
)
|
|
466
|
+
continue
|
|
467
|
+
}
|
|
468
|
+
count += 1
|
|
469
|
+
}
|
|
470
|
+
this.variables[name] = value
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
}
|