@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/src/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Context, FiberState, Inject, Service } from '@deepseek-ai/cordis'
|
|
1
|
+
import { Context, FiberState, Inject, Service, ValidationError, type Fiber } from '@deepseek-ai/cordis'
|
|
2
2
|
import { defineProperty, isNullable, type Awaitable, type Dict } from '@deepseek-ai/cosmokit'
|
|
3
3
|
import { ModuleLoader } from './internal.ts'
|
|
4
4
|
import { Entry, type EntryOptions } from './config/entry.ts'
|
|
@@ -75,7 +75,7 @@ export namespace Loader {
|
|
|
75
75
|
* Subclasses provide persistence by implementing `commit(change)` on `EntryTree`.
|
|
76
76
|
*/
|
|
77
77
|
export class Loader extends EntryTree {
|
|
78
|
-
declare [Service.
|
|
78
|
+
declare [Service.metadata]: Loader.Intercept
|
|
79
79
|
|
|
80
80
|
public envData = process.env.CORDIS_SHARED
|
|
81
81
|
? JSON.parse(process.env.CORDIS_SHARED)
|
|
@@ -101,9 +101,30 @@ export class Loader extends EntryTree {
|
|
|
101
101
|
|
|
102
102
|
ctx.reflect.provide('loader', this, this[Service.check])
|
|
103
103
|
|
|
104
|
+
// the source config each entry fiber last resolved
|
|
105
|
+
const sources = new WeakMap<Fiber, any>()
|
|
106
|
+
|
|
107
|
+
// Registered before the built-in validation listener, so validation
|
|
108
|
+
// failures can be traced to the config source. A config passed in at
|
|
109
|
+
// runtime is not written anywhere yet, so its failures are left as is.
|
|
110
|
+
ctx.on('internal/config', (fiber, next) => {
|
|
111
|
+
if (!fiber.entry || fiber.parent.fiber?.entry === fiber.entry) return next()
|
|
112
|
+
try {
|
|
113
|
+
return next()
|
|
114
|
+
} catch (error) {
|
|
115
|
+
if (ValidationError.is(error) && sources.get(fiber) === fiber.entry.options.config) {
|
|
116
|
+
fiber.entry.traceValidation(error)
|
|
117
|
+
}
|
|
118
|
+
throw error
|
|
119
|
+
}
|
|
120
|
+
}, { prepend: true })
|
|
121
|
+
|
|
122
|
+
// Registered after the built-in validation listener, so interpolation runs
|
|
123
|
+
// on the source config and validation sees the interpolated result.
|
|
104
124
|
ctx.on('internal/config', (fiber, next) => {
|
|
105
125
|
const config = next()
|
|
106
126
|
if (!fiber.entry || fiber.parent.fiber?.entry === fiber.entry) return config
|
|
127
|
+
sources.set(fiber, config)
|
|
107
128
|
// Tree carriers preserve the expressions owned by their child entries.
|
|
108
129
|
if (fiber.runtime?.callback[EntryGroup.key]) return config
|
|
109
130
|
return interpolate(fiber.ctx, config)
|
|
@@ -181,14 +202,14 @@ export class Loader extends EntryTree {
|
|
|
181
202
|
}
|
|
182
203
|
|
|
183
204
|
[Service.check]() {
|
|
184
|
-
const config: Loader.Intercept = Service.prototype[Service.
|
|
205
|
+
const config: Loader.Intercept = Service.prototype[Service.resolveIntercept].call(this)
|
|
185
206
|
if (config.await && this.getTasks().length) return false
|
|
186
207
|
return true
|
|
187
208
|
}
|
|
188
209
|
|
|
189
210
|
showLog(entry: Entry, type: string) {
|
|
190
211
|
if (entry.options.group || !entry.parent.tree.enableLogs) return
|
|
191
|
-
this.ctx.root.logger?.('loader').info('%s plugin %C', type, entry.options.name)
|
|
212
|
+
this.ctx[Context.root].logger?.('loader').info('%s plugin %C', type, entry.options.name)
|
|
192
213
|
}
|
|
193
214
|
|
|
194
215
|
/** Return the loader entry id that owns `fiber`, if any. */
|
package/src/resolve.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import fs from 'node:fs'
|
|
2
2
|
import fsPromises from 'node:fs/promises'
|
|
3
3
|
import path from 'node:path'
|
|
4
|
+
import timers from 'node:timers/promises'
|
|
4
5
|
import url from 'node:url'
|
|
5
6
|
import type { Dict } from '@deepseek-ai/cosmokit'
|
|
6
7
|
|
|
@@ -19,6 +20,18 @@ const HELPER_FILES = {
|
|
|
19
20
|
|
|
20
21
|
const cache: Dict<Promise<Resolve | undefined>> = Object.create(null)
|
|
21
22
|
|
|
23
|
+
const RENAME_RETRIES = 10
|
|
24
|
+
const RENAME_BACKOFF = 20
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* On Windows a rename over a file that another process holds open fails with
|
|
28
|
+
* one of these; the lock is normally gone within milliseconds.
|
|
29
|
+
*/
|
|
30
|
+
function isTransientRenameError(error: unknown) {
|
|
31
|
+
const code = (error as NodeJS.ErrnoException | undefined)?.code
|
|
32
|
+
return code === 'EACCES' || code === 'EPERM' || code === 'EBUSY'
|
|
33
|
+
}
|
|
34
|
+
|
|
22
35
|
/** Nearest ancestor of `dir` that owns a `package.json`, if any. */
|
|
23
36
|
function findScope(dir: string) {
|
|
24
37
|
while (true) {
|
|
@@ -41,11 +54,25 @@ function resolveScope(baseUrl: string | undefined) {
|
|
|
41
54
|
}
|
|
42
55
|
|
|
43
56
|
async function write(filename: string, content: string) {
|
|
57
|
+
// The helper content is constant; skipping an identical rewrite avoids
|
|
58
|
+
// repeating the rename that another process may have blocked.
|
|
59
|
+
if (await fsPromises.readFile(filename, 'utf8').catch(() => undefined) === content) return
|
|
44
60
|
// Another process may be writing the same path; `rename` is atomic, so a
|
|
45
61
|
// concurrent reader only ever observes the complete helper.
|
|
46
62
|
const temp = `${filename}.${process.pid}`
|
|
47
63
|
await fsPromises.writeFile(temp, content)
|
|
48
|
-
|
|
64
|
+
for (let attempt = 0; ; attempt++) {
|
|
65
|
+
try {
|
|
66
|
+
await fsPromises.rename(temp, filename)
|
|
67
|
+
return
|
|
68
|
+
} catch (error) {
|
|
69
|
+
if (attempt >= RENAME_RETRIES || !isTransientRenameError(error)) {
|
|
70
|
+
await fsPromises.rm(temp, { force: true }).catch(() => {})
|
|
71
|
+
throw error
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
await timers.setTimeout(RENAME_BACKOFF * (attempt + 1))
|
|
75
|
+
}
|
|
49
76
|
}
|
|
50
77
|
|
|
51
78
|
async function _createResolve(scope: string): Promise<Resolve | undefined> {
|
|
@@ -71,5 +98,8 @@ async function _createResolve(scope: string): Promise<Resolve | undefined> {
|
|
|
71
98
|
export async function createResolve(baseUrl: string | undefined) {
|
|
72
99
|
const scope = resolveScope(baseUrl)
|
|
73
100
|
if (!scope) return
|
|
74
|
-
|
|
101
|
+
const resolve = await (cache[scope] ??= _createResolve(scope))
|
|
102
|
+
// A scope that cannot be prepared may succeed on a later attempt.
|
|
103
|
+
if (!resolve) delete cache[scope]
|
|
104
|
+
return resolve
|
|
75
105
|
}
|