@deepseek-ai/cordis-plugin-loader 1.1.0 → 1.2.0-alpha.2
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/README.md +9 -1
- package/lib/index.js +333 -49
- package/lib/types/config/entry.d.ts +21 -1
- package/lib/types/config/entry.d.ts.map +1 -1
- package/lib/types/config/group.d.ts +11 -0
- package/lib/types/config/group.d.ts.map +1 -1
- package/lib/types/config/tree.d.ts +24 -1
- package/lib/types/config/tree.d.ts.map +1 -1
- package/lib/types/config/utils.d.ts +58 -0
- package/lib/types/config/utils.d.ts.map +1 -1
- package/lib/types/index.d.ts +3 -3
- package/lib/types/index.d.ts.map +1 -1
- package/lib/types/resolve.d.ts.map +1 -1
- package/package.json +5 -5
- package/src/config/entry.ts +57 -19
- package/src/config/group.ts +40 -2
- package/src/config/tree.ts +87 -4
- package/src/config/utils.ts +131 -1
- package/src/index.ts +25 -4
- package/src/resolve.ts +32 -2
package/README.md
CHANGED
|
@@ -48,13 +48,21 @@ root.loader.update(id, { config: { enabled: false } })
|
|
|
48
48
|
|
|
49
49
|
For file-backed trees, use `@cordisjs/plugin-include`.
|
|
50
50
|
|
|
51
|
+
## Entry validation and source positions
|
|
52
|
+
|
|
53
|
+
`create()` and `update()` check the resulting options with `validateOptions()` before the tree changes and reject with `EntryValidationError`, whose `issues` list each rejected path. `id` and `name` must be strings, and `name` is required; `group` must be a boolean; `disabled` a boolean or `!!js` expression; an entry with `group: true` requires a `config` array, which may be empty; `inject` a string array or a mapping; `intercept` and `isolate` mappings, with each `isolate` value `true` or a string. `create()` and `update()` also check the entries of a group's `config`. A group checks each entry again as it creates it; an invalid entry is logged, any running entry with its id is disposed, and its siblings still start. Explicit ids may contain `:`.
|
|
54
|
+
|
|
55
|
+
`EntryTree.getLocation(id, path)` returns where an entry option is written; the base tree returns `undefined`, and Include implements it. Config validation errors and entry option errors of an entry with a source list each issue with its `file:line:column`, and their stack frames name the entry ids from the innermost tree outward. A config passed at runtime has no source position, so its validation errors keep their original message.
|
|
56
|
+
|
|
51
57
|
## Module resolution
|
|
52
58
|
|
|
53
59
|
Without Node module internals, Loader resolves bare specifiers, package imports and self-references in the nearest package scope containing the configuration file. Relative specifiers use the configuration URL directly, and `cordis:` builtins bypass filesystem resolution. Other specifiers, including `node:` builtins and absolute URLs, initialize an ignored `.cordis/resolve.mjs` helper in that scope to preserve native ESM import conditions.
|
|
54
60
|
|
|
55
61
|
If no scope exists or writing the helper files fails, Loader uses its native import fallback. This fallback emits no diagnostic of its own; project-only dependencies can remain unavailable, and the resulting import error can name the Loader module rather than the configuration project. Failure to import a written helper propagates to the caller without this fallback.
|
|
56
62
|
|
|
57
|
-
|
|
63
|
+
A successful initialization is cached for the lifetime of the resolver module, and so is a rejected helper import. A scope whose helper files cannot be written is not cached, so a later import retries the initialization. A helper file that already holds the expected content is not rewritten, and a rename blocked by a transient `EACCES`, `EPERM` or `EBUSY` lock is retried with a bounded backoff. Concurrent initialization through filesystem aliases of the same package can contend for the temporary helper files and cause a write failure.
|
|
64
|
+
|
|
65
|
+
On Windows, an entry `name` that is a drive-letter path such as `C:\app\plugin.js` is imported as its file URL. When the entry's own module cannot be found, the import rejects with `ModuleNotFoundError`, whose `specifier` is the entry `name` and whose message keeps Node's description and hints without the importer; an entry with a source position reports it as `(at name, file:line:column)`. A missing module imported by the plugin propagates Node's original error.
|
|
58
66
|
|
|
59
67
|
Node module internals are classified by their available APIs, including the v1 API used by Node 24.0–24.11.
|
|
60
68
|
|
package/lib/index.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { createRequire } from "node:module";
|
|
2
|
-
import { Context, Inject, Service, composeError } from "@deepseek-ai/cordis";
|
|
2
|
+
import { Context, Inject, Service, ValidationError, composeError } from "@deepseek-ai/cordis";
|
|
3
3
|
import { deepEqual, defineProperty, isNonNullable, isNullable, updateVolatile, valueMap, volatileEntries } from "@deepseek-ai/cosmokit";
|
|
4
|
+
import url from "node:url";
|
|
4
5
|
import fs from "node:fs";
|
|
5
6
|
import fsPromises from "node:fs/promises";
|
|
6
7
|
import path from "node:path";
|
|
7
|
-
import
|
|
8
|
+
import timers from "node:timers/promises";
|
|
8
9
|
//#region lib/types/internal.js
|
|
9
10
|
/** Helpers for locating the current Node internal module loader. */
|
|
10
11
|
var ModuleLoader;
|
|
@@ -43,6 +44,137 @@ var ModuleLoader;
|
|
|
43
44
|
ModuleLoader.fromInternal = fromInternal;
|
|
44
45
|
})(ModuleLoader || (ModuleLoader = {}));
|
|
45
46
|
//#endregion
|
|
47
|
+
//#region lib/types/config/utils.js
|
|
48
|
+
/** Evaluate a JavaScript expression against a loader context scope. */
|
|
49
|
+
const evaluate = new Function("ctx", "expr", `
|
|
50
|
+
with (ctx) {
|
|
51
|
+
return eval(expr)
|
|
52
|
+
}
|
|
53
|
+
`);
|
|
54
|
+
/** Recursively replace YAML `!!js` expression nodes with evaluated values. */
|
|
55
|
+
function interpolate(ctx, value) {
|
|
56
|
+
if (isJsExpr(value)) return evaluate(ctx, value.__jsExpr);
|
|
57
|
+
else if (!value || typeof value !== "object") return value;
|
|
58
|
+
else if (Array.isArray(value)) return value.map((item) => interpolate(ctx, item));
|
|
59
|
+
else return valueMap(value, (item) => interpolate(ctx, item));
|
|
60
|
+
}
|
|
61
|
+
/** Return true when a value is a serialized loader JavaScript expression. */
|
|
62
|
+
function isJsExpr(value) {
|
|
63
|
+
return value instanceof Object && typeof value.__jsExpr === "string";
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Format a source position as `file:line:column`.
|
|
67
|
+
* @param location - Position to format.
|
|
68
|
+
* @returns The formatted position.
|
|
69
|
+
*/
|
|
70
|
+
function formatLocation(location) {
|
|
71
|
+
return `${location.filename}:${location.line}:${location.column}`;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Format one validation issue as an indented list item.
|
|
75
|
+
* @param message - Issue description.
|
|
76
|
+
* @param location - Source position of the value, when known.
|
|
77
|
+
* @param path - Dotted option path of the value, when not the root.
|
|
78
|
+
* @returns The list item, with its path and position in parentheses.
|
|
79
|
+
*/
|
|
80
|
+
function formatIssue(message, location, path) {
|
|
81
|
+
const where = [path, location && formatLocation(location)].filter(Boolean).join(", ");
|
|
82
|
+
return where ? ` - ${message} (at ${where})` : ` - ${message}`;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Format a titled list of validation issues.
|
|
86
|
+
* @param title - First line of the message.
|
|
87
|
+
* @param issues - Issues to list in order.
|
|
88
|
+
* @param locate - Resolves each issue path to a source position.
|
|
89
|
+
* @returns The multi-line message.
|
|
90
|
+
*/
|
|
91
|
+
function formatIssues(title, issues, locate) {
|
|
92
|
+
return `${title}:\n${issues.map(({ message, path }) => {
|
|
93
|
+
return formatIssue(message, locate?.(path), path.length ? path.map(String).join(".") : void 0);
|
|
94
|
+
}).join("\n")}`;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Replace an error's message and stack frames, so the error points at the config source instead of the loader internals.
|
|
98
|
+
* @param error - Error to rewrite in place.
|
|
99
|
+
* @param message - New message.
|
|
100
|
+
* @param frames - Stack frames listed after the message line.
|
|
101
|
+
*/
|
|
102
|
+
function relocate(error, message, frames) {
|
|
103
|
+
error.message = message;
|
|
104
|
+
error.stack = [`${error.name}: ${message}`, ...frames].join("\n");
|
|
105
|
+
}
|
|
106
|
+
/** Entry options rejected by `validateOptions()`; `issues` lists every rejected option. */
|
|
107
|
+
var EntryValidationError = class extends TypeError {
|
|
108
|
+
issues;
|
|
109
|
+
name = "EntryValidationError";
|
|
110
|
+
constructor(issues) {
|
|
111
|
+
super(formatIssues("invalid entry options", issues));
|
|
112
|
+
this.issues = issues;
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
function isPlainObject(value) {
|
|
116
|
+
return !!value && typeof value === "object" && !Array.isArray(value) && !isJsExpr(value);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Check entry options before an entry is created or updated. Explicit ids may contain `sep`.
|
|
120
|
+
* @param options - Candidate entry options.
|
|
121
|
+
* @param sep - Tree id separator; kept for the upstream signature.
|
|
122
|
+
* @param deep - Also check the entries of a group's `config`.
|
|
123
|
+
* @returns Every issue found; empty when the options are valid.
|
|
124
|
+
*/
|
|
125
|
+
function validateOptions(options, sep, deep = false) {
|
|
126
|
+
if (!isPlainObject(options)) return [{
|
|
127
|
+
message: "entry must be an object",
|
|
128
|
+
path: []
|
|
129
|
+
}];
|
|
130
|
+
const issues = [];
|
|
131
|
+
const check = (key, expected, valid, required = false) => {
|
|
132
|
+
const value = options[key];
|
|
133
|
+
if (value === void 0 || value === null) {
|
|
134
|
+
if (required) issues.push({
|
|
135
|
+
message: `${key} is required`,
|
|
136
|
+
path: [key]
|
|
137
|
+
});
|
|
138
|
+
} else if (!valid(value)) issues.push({
|
|
139
|
+
message: `${key} must be ${expected}`,
|
|
140
|
+
path: [key]
|
|
141
|
+
});
|
|
142
|
+
};
|
|
143
|
+
check("id", "a string", (value) => typeof value === "string");
|
|
144
|
+
check("name", "a string", (value) => typeof value === "string", true);
|
|
145
|
+
check("group", "a boolean", (value) => typeof value === "boolean");
|
|
146
|
+
check("disabled", "a boolean", (value) => typeof value === "boolean" || isJsExpr(value));
|
|
147
|
+
if (options.group === true) check("config", "an array", Array.isArray, true);
|
|
148
|
+
check("inject", "an array or an object", (value) => Array.isArray(value) || isPlainObject(value));
|
|
149
|
+
if (Array.isArray(options.inject)) options.inject.forEach((name, index) => {
|
|
150
|
+
if (typeof name === "string") return;
|
|
151
|
+
issues.push({
|
|
152
|
+
message: `inject.${index} must be a string`,
|
|
153
|
+
path: ["inject", index]
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
check("intercept", "an object", isPlainObject);
|
|
157
|
+
check("isolate", "an object", isPlainObject);
|
|
158
|
+
if (isPlainObject(options.isolate)) for (const [name, label] of Object.entries(options.isolate)) {
|
|
159
|
+
if (label === true || typeof label === "string") continue;
|
|
160
|
+
issues.push({
|
|
161
|
+
message: `isolate.${name} must be true or a string`,
|
|
162
|
+
path: ["isolate", name]
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
if (deep && options.group === true && Array.isArray(options.config)) options.config.forEach((child, index) => {
|
|
166
|
+
for (const { message, path } of validateOptions(child, sep, true)) issues.push({
|
|
167
|
+
message,
|
|
168
|
+
path: [
|
|
169
|
+
"config",
|
|
170
|
+
index,
|
|
171
|
+
...path
|
|
172
|
+
]
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
return issues;
|
|
176
|
+
}
|
|
177
|
+
//#endregion
|
|
46
178
|
//#region lib/types/config/group.js
|
|
47
179
|
/** Runtime owner for a list of child loader entries. */
|
|
48
180
|
var EntryGroup = class {
|
|
@@ -59,13 +191,44 @@ var EntryGroup = class {
|
|
|
59
191
|
get context() {
|
|
60
192
|
return this.ctx;
|
|
61
193
|
}
|
|
194
|
+
/**
|
|
195
|
+
* Create or replace the entry for `options`. Invalid options reject with `EntryValidationError` and dispose any entry already registered under their id.
|
|
196
|
+
* @returns The full entry id.
|
|
197
|
+
*/
|
|
62
198
|
async create(options) {
|
|
199
|
+
const issues = validateOptions(options, EntryTree.sep);
|
|
200
|
+
if (issues.length) throw this._reject(options, issues);
|
|
63
201
|
const id = this.tree.ensureId(options);
|
|
64
202
|
const entry = this.tree.store[id] ??= new Entry(this.ctx.loader);
|
|
65
203
|
entry.parent = this;
|
|
66
204
|
await entry.update(options, true, true);
|
|
67
205
|
return entry.id;
|
|
68
206
|
}
|
|
207
|
+
/**
|
|
208
|
+
* Stack frames for an entry of this group, followed by those of the owning entries.
|
|
209
|
+
* @param id - Tree-local entry id.
|
|
210
|
+
* @returns Frames that point at the entries' source positions.
|
|
211
|
+
*/
|
|
212
|
+
getOuterStack(id) {
|
|
213
|
+
const location = this.tree.getLocation(id, []);
|
|
214
|
+
return [...location ? [` at ${id} (${formatLocation(location)})`] : [], ...this.ctx.fiber.entry?.getOuterStack() ?? []];
|
|
215
|
+
}
|
|
216
|
+
_reject(options, issues) {
|
|
217
|
+
const error = new EntryValidationError(issues);
|
|
218
|
+
const id = options?.id;
|
|
219
|
+
if (isNullable(id)) return error;
|
|
220
|
+
const entry = this.tree.store[id];
|
|
221
|
+
if (entry) {
|
|
222
|
+
delete this.tree.store[id];
|
|
223
|
+
entry.fiber?.dispose();
|
|
224
|
+
this.context.emit("loader/partial-dispose", entry, entry.options, false);
|
|
225
|
+
}
|
|
226
|
+
const frames = this.getOuterStack(id);
|
|
227
|
+
if (!frames.length) return error;
|
|
228
|
+
const locate = (path) => this.tree.getLocation(id, path);
|
|
229
|
+
relocate(error, formatIssues("invalid entry options", issues, locate), frames);
|
|
230
|
+
return error;
|
|
231
|
+
}
|
|
69
232
|
unlink(options) {
|
|
70
233
|
const config = this.data;
|
|
71
234
|
const index = config.indexOf(options);
|
|
@@ -82,8 +245,8 @@ var EntryGroup = class {
|
|
|
82
245
|
async update(config) {
|
|
83
246
|
const oldConfig = this.data;
|
|
84
247
|
this.data = config;
|
|
85
|
-
const oldMap = Object.fromEntries(oldConfig.map((options) => [options
|
|
86
|
-
const newMap = Object.fromEntries(config.map((options) => [options
|
|
248
|
+
const oldMap = Object.fromEntries(oldConfig.map((options) => [options?.id, options]));
|
|
249
|
+
const newMap = Object.fromEntries(config.map((options) => [options?.id ?? Symbol("anonymous"), options]));
|
|
87
250
|
const ids = Reflect.ownKeys({
|
|
88
251
|
...oldMap,
|
|
89
252
|
...newMap
|
|
@@ -129,6 +292,16 @@ const HELPER_FILES = {
|
|
|
129
292
|
"resolve.mjs": "export default (specifier) => import.meta.resolve(specifier)\n"
|
|
130
293
|
};
|
|
131
294
|
const cache = Object.create(null);
|
|
295
|
+
const RENAME_RETRIES = 10;
|
|
296
|
+
const RENAME_BACKOFF = 20;
|
|
297
|
+
/**
|
|
298
|
+
* On Windows a rename over a file that another process holds open fails with
|
|
299
|
+
* one of these; the lock is normally gone within milliseconds.
|
|
300
|
+
*/
|
|
301
|
+
function isTransientRenameError(error) {
|
|
302
|
+
const code = error?.code;
|
|
303
|
+
return code === "EACCES" || code === "EPERM" || code === "EBUSY";
|
|
304
|
+
}
|
|
132
305
|
/** Nearest ancestor of `dir` that owns a `package.json`, if any. */
|
|
133
306
|
function findScope(dir) {
|
|
134
307
|
while (true) {
|
|
@@ -149,9 +322,21 @@ function resolveScope(baseUrl) {
|
|
|
149
322
|
return findScope(path.resolve(dir));
|
|
150
323
|
}
|
|
151
324
|
async function write(filename, content) {
|
|
325
|
+
if (await fsPromises.readFile(filename, "utf8").catch(() => void 0) === content) return;
|
|
152
326
|
const temp = `${filename}.${process.pid}`;
|
|
153
327
|
await fsPromises.writeFile(temp, content);
|
|
154
|
-
|
|
328
|
+
for (let attempt = 0;; attempt++) {
|
|
329
|
+
try {
|
|
330
|
+
await fsPromises.rename(temp, filename);
|
|
331
|
+
return;
|
|
332
|
+
} catch (error) {
|
|
333
|
+
if (attempt >= RENAME_RETRIES || !isTransientRenameError(error)) {
|
|
334
|
+
await fsPromises.rm(temp, { force: true }).catch(() => {});
|
|
335
|
+
throw error;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
await timers.setTimeout(RENAME_BACKOFF * (attempt + 1));
|
|
339
|
+
}
|
|
155
340
|
}
|
|
156
341
|
async function _createResolve(scope) {
|
|
157
342
|
const dir = path.join(scope, ".cordis");
|
|
@@ -175,7 +360,9 @@ async function _createResolve(scope) {
|
|
|
175
360
|
async function createResolve(baseUrl) {
|
|
176
361
|
const scope = resolveScope(baseUrl);
|
|
177
362
|
if (!scope) return;
|
|
178
|
-
|
|
363
|
+
const resolve = await (cache[scope] ??= _createResolve(scope));
|
|
364
|
+
if (!resolve) delete cache[scope];
|
|
365
|
+
return resolve;
|
|
179
366
|
}
|
|
180
367
|
//#endregion
|
|
181
368
|
//#region lib/types/config/tree.js
|
|
@@ -185,6 +372,38 @@ var __rewriteRelativeImportExtension = function(path, preserveJsx) {
|
|
|
185
372
|
});
|
|
186
373
|
return path;
|
|
187
374
|
};
|
|
375
|
+
const driveLetter = /^[A-Za-z]:[\\/]/;
|
|
376
|
+
function parseNotFound(error) {
|
|
377
|
+
if (error?.code !== "ERR_MODULE_NOT_FOUND" || typeof error.message !== "string") return;
|
|
378
|
+
const [first, ...hints] = error.message.split("\n");
|
|
379
|
+
const match = first.match(/^(Cannot find (?:module|package) '.*') imported from (.*)$/);
|
|
380
|
+
if (match) return {
|
|
381
|
+
description: match[1],
|
|
382
|
+
parent: match[2],
|
|
383
|
+
hints
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
function toPath(href) {
|
|
387
|
+
try {
|
|
388
|
+
return href.startsWith("file:") ? url.fileURLToPath(href) : href;
|
|
389
|
+
} catch {
|
|
390
|
+
return href;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
/** A configured plugin module that cannot be found, as opposed to a missing module imported by that plugin. */
|
|
394
|
+
var ModuleNotFoundError = class extends Error {
|
|
395
|
+
specifier;
|
|
396
|
+
name = "ModuleNotFoundError";
|
|
397
|
+
code = "ERR_MODULE_NOT_FOUND";
|
|
398
|
+
/**
|
|
399
|
+
* @param specifier - Entry `name` that failed to resolve.
|
|
400
|
+
* @param message - Node's description and hints, without its importer.
|
|
401
|
+
*/
|
|
402
|
+
constructor(specifier, message) {
|
|
403
|
+
super(message);
|
|
404
|
+
this.specifier = specifier;
|
|
405
|
+
}
|
|
406
|
+
};
|
|
188
407
|
/** The entry and the same-tree members of its group, recursively. */
|
|
189
408
|
function members(entry) {
|
|
190
409
|
const { tree } = entry.parent;
|
|
@@ -268,6 +487,8 @@ var EntryTree = class EntryTree {
|
|
|
268
487
|
/** Create an entry in the root group or a nested group. */
|
|
269
488
|
async create(options, parent = null, position = Infinity) {
|
|
270
489
|
const group = this.resolveGroup(parent);
|
|
490
|
+
const issues = validateOptions(options, EntryTree.sep, true);
|
|
491
|
+
if (issues.length) throw new EntryValidationError(issues);
|
|
271
492
|
const id = group.tree.ensureId(options);
|
|
272
493
|
group.data.splice(position, 0, options);
|
|
273
494
|
group.tree.commit({
|
|
@@ -294,6 +515,8 @@ var EntryTree = class EntryTree {
|
|
|
294
515
|
const entry = this.resolve(id);
|
|
295
516
|
const source = entry.parent;
|
|
296
517
|
const legacy = { ...entry.options };
|
|
518
|
+
const issues = validateOptions(Entry.merge({ ...legacy }, options), EntryTree.sep, true);
|
|
519
|
+
if (issues.length) throw new EntryValidationError(issues);
|
|
297
520
|
if (parent !== void 0) {
|
|
298
521
|
const target = this.resolveGroup(parent);
|
|
299
522
|
const moved = target.tree === source.tree ? [] : members(entry);
|
|
@@ -334,46 +557,61 @@ var EntryTree = class EntryTree {
|
|
|
334
557
|
}
|
|
335
558
|
return task;
|
|
336
559
|
}
|
|
337
|
-
/**
|
|
560
|
+
/**
|
|
561
|
+
* Import a plugin module from a specifier or `cordis:` builtin. On Windows a drive-letter path is imported as a file URL.
|
|
562
|
+
* Rejects with `ModuleNotFoundError` when the specifier itself cannot be found; other import errors propagate unchanged.
|
|
563
|
+
*/
|
|
338
564
|
import(name, getOuterStack) {
|
|
339
565
|
if (name.startsWith("cordis:")) return this.ctx.loader.builtins[name.slice(7)];
|
|
566
|
+
if (process.platform === "win32" && driveLetter.test(name)) name = url.pathToFileURL(name).href;
|
|
567
|
+
let parent;
|
|
568
|
+
let unresolved;
|
|
340
569
|
return composeError(async (info) => {
|
|
341
570
|
info.offset += 3;
|
|
342
|
-
if (this.ctx.loader.internal)
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
571
|
+
if (this.ctx.loader.internal) {
|
|
572
|
+
parent = this.ctx.baseUrl;
|
|
573
|
+
return await this.ctx.loader.internal.import(name, parent, {});
|
|
574
|
+
} else if (name.startsWith(".")) {
|
|
575
|
+
parent = import.meta.url;
|
|
576
|
+
return await import(__rewriteRelativeImportExtension(
|
|
577
|
+
/* @vite-ignore */
|
|
578
|
+
new URL(name, this.ctx.baseUrl).href
|
|
579
|
+
));
|
|
580
|
+
} else {
|
|
348
581
|
const resolve = await createResolve(this.ctx.baseUrl);
|
|
582
|
+
let url = name;
|
|
583
|
+
if (resolve) try {
|
|
584
|
+
url = resolve(name);
|
|
585
|
+
} catch (error) {
|
|
586
|
+
unresolved = error;
|
|
587
|
+
throw error;
|
|
588
|
+
}
|
|
589
|
+
parent = import.meta.url;
|
|
349
590
|
return await import(__rewriteRelativeImportExtension(
|
|
350
591
|
/* @vite-ignore */
|
|
351
|
-
|
|
592
|
+
url
|
|
352
593
|
));
|
|
353
594
|
}
|
|
354
|
-
}, getOuterStack)
|
|
595
|
+
}, getOuterStack).catch((error) => {
|
|
596
|
+
const notFound = parseNotFound(error);
|
|
597
|
+
if (!notFound) throw error;
|
|
598
|
+
if (error !== unresolved && (!parent || notFound.parent !== toPath(parent))) throw error;
|
|
599
|
+
const result = new ModuleNotFoundError(name, [notFound.description, ...notFound.hints].join("\n"));
|
|
600
|
+
const frames = getOuterStack?.() ?? [];
|
|
601
|
+
if (frames.length) result.stack = [`${result.name}: ${result.message}`, ...frames].join("\n");
|
|
602
|
+
throw result;
|
|
603
|
+
});
|
|
355
604
|
}
|
|
605
|
+
/**
|
|
606
|
+
* Where a value of the entry options is written. A path that leaves the
|
|
607
|
+
* source falls back to its deepest written ancestor.
|
|
608
|
+
* @param _id - Tree-local entry id.
|
|
609
|
+
* @param _path - Path inside the entry options.
|
|
610
|
+
* @returns The source position, or undefined for trees without a source file.
|
|
611
|
+
*/
|
|
612
|
+
getLocation(_id, _path) {}
|
|
356
613
|
};
|
|
357
614
|
//#endregion
|
|
358
|
-
//#region lib/types/config/utils.js
|
|
359
|
-
/** Evaluate a JavaScript expression against a loader context scope. */
|
|
360
|
-
const evaluate = new Function("ctx", "expr", `
|
|
361
|
-
with (ctx) {
|
|
362
|
-
return eval(expr)
|
|
363
|
-
}
|
|
364
|
-
`);
|
|
365
|
-
/** Recursively replace YAML `!!js` expression nodes with evaluated values. */
|
|
366
|
-
function interpolate(ctx, value) {
|
|
367
|
-
if (isJsExpr(value)) return evaluate(ctx, value.__jsExpr);
|
|
368
|
-
else if (!value || typeof value !== "object") return value;
|
|
369
|
-
else if (Array.isArray(value)) return value.map((item) => interpolate(ctx, item));
|
|
370
|
-
else return valueMap(value, (item) => interpolate(ctx, item));
|
|
371
|
-
}
|
|
372
|
-
/** Return true when a value is a serialized loader JavaScript expression. */
|
|
373
|
-
function isJsExpr(value) {
|
|
374
|
-
return value instanceof Object && "__jsExpr" in value;
|
|
375
|
-
}
|
|
376
|
-
//#endregion
|
|
377
615
|
//#region lib/types/config/diff.js
|
|
378
616
|
function isSchemastery(schema) {
|
|
379
617
|
return schema?.["~standard"].vendor === "schemastery";
|
|
@@ -451,6 +689,17 @@ var Entry = class Entry {
|
|
|
451
689
|
this.ctx = loader.ctx.extend({ [Entry.key]: this });
|
|
452
690
|
this.context.emit("loader/entry-init", this);
|
|
453
691
|
}
|
|
692
|
+
/**
|
|
693
|
+
* Merge partial options into `target`; a nullish value deletes its key.
|
|
694
|
+
* @param target - Options to modify in place.
|
|
695
|
+
* @param options - Changes to apply.
|
|
696
|
+
* @returns `target`.
|
|
697
|
+
*/
|
|
698
|
+
static merge(target, options) {
|
|
699
|
+
for (const [key, value] of Object.entries(options)) if (isNullable(value)) delete target[key];
|
|
700
|
+
else target[key] = value;
|
|
701
|
+
return target;
|
|
702
|
+
}
|
|
454
703
|
get context() {
|
|
455
704
|
return this.ctx;
|
|
456
705
|
}
|
|
@@ -498,8 +747,7 @@ var Entry = class Entry {
|
|
|
498
747
|
async update(options, create = false, force = false) {
|
|
499
748
|
const legacy = { ...this.options };
|
|
500
749
|
if (create) this.options = options;
|
|
501
|
-
else
|
|
502
|
-
else this.options[key] = value;
|
|
750
|
+
else Entry.merge(this.options, options);
|
|
503
751
|
sortKeys(this.options);
|
|
504
752
|
if (this.disabled) {
|
|
505
753
|
await this.fiber?.dispose();
|
|
@@ -556,15 +804,40 @@ var Entry = class Entry {
|
|
|
556
804
|
}
|
|
557
805
|
return true;
|
|
558
806
|
}
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
807
|
+
/**
|
|
808
|
+
* Where a value of this entry's options is written.
|
|
809
|
+
* @param path - Path inside the entry options; empty for the entry itself.
|
|
810
|
+
* @returns The source position, or undefined when the tree has no source.
|
|
811
|
+
*/
|
|
812
|
+
getLocation(path = []) {
|
|
813
|
+
return this.parent.tree.getLocation(this.options.id, path);
|
|
814
|
+
}
|
|
815
|
+
/** Stack frames from this entry out through its owning entries, each with its source position when known. */
|
|
816
|
+
getOuterStack = () => this.parent.getOuterStack(this.options.id);
|
|
817
|
+
/**
|
|
818
|
+
* Rewrite a config validation error to list each issue with its source position, when this entry has a source.
|
|
819
|
+
* @param error - Error to rewrite in place.
|
|
820
|
+
*/
|
|
821
|
+
traceValidation(error) {
|
|
822
|
+
const frames = this.getOuterStack();
|
|
823
|
+
if (!frames.length) return;
|
|
824
|
+
const issues = error.issues.map(({ message, path }) => ({
|
|
825
|
+
message,
|
|
826
|
+
path: ValidationError.normalizePath(path)
|
|
827
|
+
}));
|
|
828
|
+
const locate = (path) => this.getLocation(["config", ...path]);
|
|
829
|
+
relocate(error, formatIssues("invalid config", issues, locate), frames);
|
|
830
|
+
}
|
|
831
|
+
_reportImport(error) {
|
|
832
|
+
if (error instanceof ModuleNotFoundError) {
|
|
833
|
+
const location = this.getLocation(["name"]);
|
|
834
|
+
if (location) {
|
|
835
|
+
const [first, ...hints] = error.message.split("\n");
|
|
836
|
+
relocate(error, [`${first} (at name, ${formatLocation(location)})`, ...hints].join("\n"), this.getOuterStack());
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
this.ctx.logger.error(error);
|
|
840
|
+
}
|
|
568
841
|
/** Import and start the configured plugin if it is not already running. */
|
|
569
842
|
async init() {
|
|
570
843
|
try {
|
|
@@ -582,7 +855,7 @@ var Entry = class Entry {
|
|
|
582
855
|
try {
|
|
583
856
|
moduleNamespace = await this.parent.tree.import(this.options.name, this.getOuterStack);
|
|
584
857
|
} catch (error) {
|
|
585
|
-
this.
|
|
858
|
+
this._reportImport(error);
|
|
586
859
|
return;
|
|
587
860
|
}
|
|
588
861
|
const plugin = this.loader.unwrapExports(moduleNamespace);
|
|
@@ -737,9 +1010,20 @@ var Loader = class extends EntryTree {
|
|
|
737
1010
|
noShadow: true
|
|
738
1011
|
});
|
|
739
1012
|
ctx.reflect.provide("loader", this, this[Service.check]);
|
|
1013
|
+
const sources = /* @__PURE__ */ new WeakMap();
|
|
1014
|
+
ctx.on("internal/config", (fiber, next) => {
|
|
1015
|
+
if (!fiber.entry || fiber.parent.fiber?.entry === fiber.entry) return next();
|
|
1016
|
+
try {
|
|
1017
|
+
return next();
|
|
1018
|
+
} catch (error) {
|
|
1019
|
+
if (ValidationError.is(error) && sources.get(fiber) === fiber.entry.options.config) fiber.entry.traceValidation(error);
|
|
1020
|
+
throw error;
|
|
1021
|
+
}
|
|
1022
|
+
}, { prepend: true });
|
|
740
1023
|
ctx.on("internal/config", (fiber, next) => {
|
|
741
1024
|
const config = next();
|
|
742
1025
|
if (!fiber.entry || fiber.parent.fiber?.entry === fiber.entry) return config;
|
|
1026
|
+
sources.set(fiber, config);
|
|
743
1027
|
if (fiber.runtime?.callback[EntryGroup.key]) return config;
|
|
744
1028
|
return interpolate(fiber.ctx, config);
|
|
745
1029
|
});
|
|
@@ -790,12 +1074,12 @@ var Loader = class extends EntryTree {
|
|
|
790
1074
|
}
|
|
791
1075
|
commit(_change) {}
|
|
792
1076
|
[Service.check]() {
|
|
793
|
-
if (Service.prototype[Service.
|
|
1077
|
+
if (Service.prototype[Service.resolveIntercept].call(this).await && this.getTasks().length) return false;
|
|
794
1078
|
return true;
|
|
795
1079
|
}
|
|
796
1080
|
showLog(entry, type) {
|
|
797
1081
|
if (entry.options.group || !entry.parent.tree.enableLogs) return;
|
|
798
|
-
this.ctx.root.logger?.("loader").info("%s plugin %C", type, entry.options.name);
|
|
1082
|
+
this.ctx[Context.root].logger?.("loader").info("%s plugin %C", type, entry.options.name);
|
|
799
1083
|
}
|
|
800
1084
|
/** Return the loader entry id that owns `fiber`, if any. */
|
|
801
1085
|
locate(fiber = this.ctx.fiber) {
|
|
@@ -817,4 +1101,4 @@ var Loader = class extends EntryTree {
|
|
|
817
1101
|
}
|
|
818
1102
|
};
|
|
819
1103
|
//#endregion
|
|
820
|
-
export { Entry, EntryGroup, EntryTree, GlobalRealm, Group, Loader, Loader as default, LocalRealm, ModuleLoader, Realm, evaluate, interpolate, isJsExpr };
|
|
1104
|
+
export { Entry, EntryGroup, EntryTree, EntryValidationError, GlobalRealm, Group, Loader, Loader as default, LocalRealm, ModuleLoader, ModuleNotFoundError, Realm, evaluate, formatIssue, formatIssues, formatLocation, interpolate, isJsExpr, relocate, validateOptions };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Context, Fiber, Inject } from '@deepseek-ai/cordis';
|
|
1
|
+
import { Context, Fiber, Inject, ValidationError } from '@deepseek-ai/cordis';
|
|
2
2
|
import { Loader } from '../index.ts';
|
|
3
3
|
import { EntryGroup } from './group.ts';
|
|
4
4
|
import { EntryTree } from './tree.ts';
|
|
@@ -31,6 +31,13 @@ export declare class Entry {
|
|
|
31
31
|
subtree?: EntryTree;
|
|
32
32
|
_initTask?: Promise<void>;
|
|
33
33
|
constructor(loader: Loader);
|
|
34
|
+
/**
|
|
35
|
+
* Merge partial options into `target`; a nullish value deletes its key.
|
|
36
|
+
* @param target - Options to modify in place.
|
|
37
|
+
* @param options - Changes to apply.
|
|
38
|
+
* @returns `target`.
|
|
39
|
+
*/
|
|
40
|
+
static merge(target: Partial<EntryOptions>, options: Partial<EntryOptions>): Partial<EntryOptions>;
|
|
34
41
|
get context(): Context;
|
|
35
42
|
get id(): string;
|
|
36
43
|
/** True when this entry or any owning parent entry is disabled. */
|
|
@@ -51,7 +58,20 @@ export declare class Entry {
|
|
|
51
58
|
* @returns `false` when an ordinary effective value changed, so the caller applies the ordinary update lifecycle.
|
|
52
59
|
*/
|
|
53
60
|
private _commitVolatile;
|
|
61
|
+
/**
|
|
62
|
+
* Where a value of this entry's options is written.
|
|
63
|
+
* @param path - Path inside the entry options; empty for the entry itself.
|
|
64
|
+
* @returns The source position, or undefined when the tree has no source.
|
|
65
|
+
*/
|
|
66
|
+
getLocation(path?: PropertyKey[]): import("./utils.ts").SourceLocation | undefined;
|
|
67
|
+
/** Stack frames from this entry out through its owning entries, each with its source position when known. */
|
|
54
68
|
getOuterStack: () => string[];
|
|
69
|
+
/**
|
|
70
|
+
* Rewrite a config validation error to list each issue with its source position, when this entry has a source.
|
|
71
|
+
* @param error - Error to rewrite in place.
|
|
72
|
+
*/
|
|
73
|
+
traceValidation(error: ValidationError): void;
|
|
74
|
+
private _reportImport;
|
|
55
75
|
/** Import and start the configured plugin if it is not already running. */
|
|
56
76
|
init(): Promise<void>;
|
|
57
77
|
private _init;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"entry.d.ts","sourceRoot":"","sources":["../../../src/config/entry.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,KAAK,EAAc,MAAM,EAAE,MAAM,qBAAqB,CAAA;
|
|
1
|
+
{"version":3,"file":"entry.d.ts","sourceRoot":"","sources":["../../../src/config/entry.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,KAAK,EAAc,MAAM,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAA;AAEzF,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACpC,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AACvC,OAAO,EAAE,SAAS,EAAuB,MAAM,WAAW,CAAA;AAI1D,qEAAqE;AACrE,MAAM,WAAW,YAAY;IAC3B,kDAAkD;IAClD,EAAE,EAAE,MAAM,CAAA;IACV,mDAAmD;IACnD,IAAI,EAAE,MAAM,CAAA;IACZ,mCAAmC;IACnC,MAAM,CAAC,EAAE,GAAG,CAAA;IACZ,0CAA0C;IAC1C,KAAK,CAAC,EAAE,OAAO,GAAG,IAAI,CAAA;IACtB,wDAAwD;IACxD,QAAQ,CAAC,EAAE,OAAO,GAAG,IAAI,CAAA;IACzB,oEAAoE;IACpE,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;CACvB;AAmBD,wDAAwD;AACxD,qBAAa,KAAK;IAeG,MAAM,EAAE,MAAM;IAdjC,MAAM,CAAC,QAAQ,CAAC,GAAG,gBAA6B;IAEzC,GAAG,EAAE,OAAO,CAAA;IACZ,KAAK,CAAC,EAAE,KAAK,CAAA;IACpB,+FAA+F;IACxF,eAAe,EAAE,OAAO,CAAA;IACxB,MAAM,EAAG,UAAU,CAAA;IAEnB,OAAO,EAAS,YAAY,CAAA;IAC5B,QAAQ,CAAC,EAAE,UAAU,CAAA;IACrB,OAAO,CAAC,EAAE,SAAS,CAAA;IAE1B,SAAS,CAAC,EAAE,OAAO,CAAC,IAAI,CAAC,CAAA;gBAEN,MAAM,EAAE,MAAM;IAKjC;;;;;OAKG;IACH,MAAM,CAAC,KAAK,CAAC,MAAM,EAAE,OAAO,CAAC,YAAY,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,YAAY,CAAC;IAW1E,IAAI,OAAO,IAAI,OAAO,CAErB;IAED,IAAI,EAAE,WAML;IAED,mEAAmE;IACnE,IAAI,QAAQ,YASX;IAED;;;OAGG;IACH,OAAO,CAAC,gBAAgB;IAMxB,QAAQ,CAAC,IAAI,EAAE,MAAM;YAIP,aAAa;IAarB,OAAO;IAMb,iFAAiF;IAC3E,MAAM,CAAC,OAAO,EAAE,OAAO,CAAC,YAAY,CAAC,EAAE,MAAM,UAAQ,EAAE,KAAK,UAAQ;IAmC1E;;;;OAIG;IACH,OAAO,CAAC,eAAe;IAmCvB;;;;OAIG;IACH,WAAW,CAAC,IAAI,GAAE,WAAW,EAAO;IAIpC,6GAA6G;IAC7G,aAAa,iBAAmD;IAEhE;;;OAGG;IACH,eAAe,CAAC,KAAK,EAAE,eAAe;IAStC,OAAO,CAAC,aAAa;IAYrB,2EAA2E;IACrE,IAAI;YAYI,KAAK;CAcpB"}
|
|
@@ -9,7 +9,18 @@ export declare class EntryGroup {
|
|
|
9
9
|
data: EntryOptions[];
|
|
10
10
|
constructor(ctx: Context, tree: EntryTree);
|
|
11
11
|
get context(): Context;
|
|
12
|
+
/**
|
|
13
|
+
* Create or replace the entry for `options`. Invalid options reject with `EntryValidationError` and dispose any entry already registered under their id.
|
|
14
|
+
* @returns The full entry id.
|
|
15
|
+
*/
|
|
12
16
|
create(options: Omit<EntryOptions, 'id'>): Promise<string>;
|
|
17
|
+
/**
|
|
18
|
+
* Stack frames for an entry of this group, followed by those of the owning entries.
|
|
19
|
+
* @param id - Tree-local entry id.
|
|
20
|
+
* @returns Frames that point at the entries' source positions.
|
|
21
|
+
*/
|
|
22
|
+
getOuterStack(id: string): string[];
|
|
23
|
+
private _reject;
|
|
13
24
|
unlink(options: EntryOptions): void;
|
|
14
25
|
remove(id: string, isDispose?: boolean): void;
|
|
15
26
|
update(config: EntryOptions[]): Promise<void>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"group.d.ts","sourceRoot":"","sources":["../../../src/config/group.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;
|
|
1
|
+
{"version":3,"file":"group.d.ts","sourceRoot":"","sources":["../../../src/config/group.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAEtD,OAAO,EAAS,KAAK,YAAY,EAAE,MAAM,YAAY,CAAA;AACrD,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAA;AAGrC,wDAAwD;AACxD,qBAAa,UAAU;IAKF,GAAG,EAAE,OAAO;IAAS,IAAI,EAAE,SAAS;IAJvD,MAAM,CAAC,QAAQ,CAAC,GAAG,gBAA6B;IAEzC,IAAI,EAAE,YAAY,EAAE,CAAK;gBAEb,GAAG,EAAE,OAAO,EAAS,IAAI,EAAE,SAAS;IAKvD,IAAI,OAAO,IAAI,OAAO,CAErB;IAED;;;OAGG;IACG,MAAM,CAAC,OAAO,EAAE,IAAI,CAAC,YAAY,EAAE,IAAI,CAAC;IAa9C;;;;OAIG;IACH,aAAa,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,EAAE;IAMnC,OAAO,CAAC,OAAO;IAkBf,MAAM,CAAC,OAAO,EAAE,YAAY;IAM5B,MAAM,CAAC,EAAE,EAAE,MAAM,EAAE,SAAS,UAAQ;IAY9B,MAAM,CAAC,MAAM,EAAE,YAAY,EAAE;IAoBnC,IAAI;CAKL;AAED,sDAAsD;AACtD,qBAAa,KAAM,SAAQ,UAAU;IAIhB,GAAG,EAAE,OAAO;IAAS,MAAM,EAAE,YAAY,EAAE;IAH9D,MAAM,CAAC,OAAO,EAAE,IAAI,CAAC,YAAY,EAAE,IAAI,CAAC,EAAE,CAAK;IAC/C,MAAM,CAAC,QAAQ,CAAC,CAAC,UAAU,CAAC,GAAG,CAAC,QAAO;gBAEpB,GAAG,EAAE,OAAO,EAAS,MAAM,EAAE,YAAY,EAAE;IAKvD,CAAC,OAAO,CAAC,IAAI,CAAC;CAItB"}
|
|
@@ -2,6 +2,18 @@ import { Context } from '@deepseek-ai/cordis';
|
|
|
2
2
|
import { type Dict } from '@deepseek-ai/cosmokit';
|
|
3
3
|
import { Entry, type EntryOptions } from './entry.ts';
|
|
4
4
|
import { EntryGroup } from './group.ts';
|
|
5
|
+
import { type SourceLocation } from './utils.ts';
|
|
6
|
+
/** A configured plugin module that cannot be found, as opposed to a missing module imported by that plugin. */
|
|
7
|
+
export declare class ModuleNotFoundError extends Error {
|
|
8
|
+
specifier: string;
|
|
9
|
+
name: string;
|
|
10
|
+
code: string;
|
|
11
|
+
/**
|
|
12
|
+
* @param specifier - Entry `name` that failed to resolve.
|
|
13
|
+
* @param message - Node's description and hints, without its importer.
|
|
14
|
+
*/
|
|
15
|
+
constructor(specifier: string, message: string);
|
|
16
|
+
}
|
|
5
17
|
/**
|
|
6
18
|
* One tree-side mutation, reported to `EntryTree.commit()` synchronously after
|
|
7
19
|
* it has been applied to `entry.options` / `group.data`.
|
|
@@ -56,9 +68,20 @@ export declare abstract class EntryTree {
|
|
|
56
68
|
remove(id: string): void;
|
|
57
69
|
/** Update an entry and optionally move it to another group. */
|
|
58
70
|
update(id: string, options: Omit<EntryOptions, 'id' | 'name'>, parent?: string | null, position?: number): Promise<void>;
|
|
59
|
-
/**
|
|
71
|
+
/**
|
|
72
|
+
* Import a plugin module from a specifier or `cordis:` builtin. On Windows a drive-letter path is imported as a file URL.
|
|
73
|
+
* Rejects with `ModuleNotFoundError` when the specifier itself cannot be found; other import errors propagate unchanged.
|
|
74
|
+
*/
|
|
60
75
|
import(name: string, getOuterStack?: () => string[]): any;
|
|
61
76
|
/** Report an applied mutation synchronously. In-memory trees may use a no-op. */
|
|
62
77
|
abstract commit(change: EntryChange): void;
|
|
78
|
+
/**
|
|
79
|
+
* Where a value of the entry options is written. A path that leaves the
|
|
80
|
+
* source falls back to its deepest written ancestor.
|
|
81
|
+
* @param _id - Tree-local entry id.
|
|
82
|
+
* @param _path - Path inside the entry options.
|
|
83
|
+
* @returns The source position, or undefined for trees without a source file.
|
|
84
|
+
*/
|
|
85
|
+
getLocation(_id: string, _path: PropertyKey[]): SourceLocation | undefined;
|
|
63
86
|
}
|
|
64
87
|
//# sourceMappingURL=tree.d.ts.map
|