@joe-broadhead/opencode-mem 0.9.14 → 0.11.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/CHANGELOG.md +1805 -0
- package/README.md +14 -7
- package/SECURITY.md +3 -1
- package/docs/agent-instructions.md +1 -1
- package/docs/agent-protocol.md +7 -7
- package/docs/permissions.md +16 -0
- package/package.json +7688 -339
- package/shared/page-paths.js +44 -0
- package/skills/mem.pages/SKILL.md +3 -1
- package/skills/mem.usage/SKILL.md +15 -15
- package/src/action-grant-keys.js +159 -0
- package/src/action-grant.js +439 -0
- package/src/actions.js +1690 -0
- package/src/backend/records.js +25 -2
- package/src/backend/v3.js +76 -22
- package/src/call-shapes.js +17 -16
- package/src/collector-lock.js +533 -62
- package/src/collector-service.js +153 -26
- package/src/collector.js +623 -30
- package/src/config.js +79 -6
- package/src/connect.js +106 -8
- package/src/credentials.js +123 -25
- package/src/events.js +174 -3
- package/src/forget-turn.js +141 -0
- package/src/host-refusal.js +78 -0
- package/src/index.js +209 -22
- package/src/jobs/host.js +557 -0
- package/src/ops/codes.js +110 -0
- package/src/ops/copy-lineage.js +42 -0
- package/src/ops/index.js +28 -0
- package/src/ops/space-ops.js +1936 -0
- package/src/outlinks.js +103 -0
- package/src/p2-reservation.js +151 -0
- package/src/permissions.js +30 -0
- package/src/plugins/descriptor.js +62 -0
- package/src/plugins/discovery.js +318 -0
- package/src/plugins/grants.js +178 -0
- package/src/plugins/locate.js +319 -0
- package/src/plugins/manifest.js +1162 -0
- package/src/plugins/source-contract/index.js +34 -0
- package/src/plugins/source-contract/policy.js +102 -0
- package/src/plugins/source-contract/vendor/PROVENANCE.json +17 -0
- package/src/plugins/source-contract/vendor/RELEASE-SHA256SUMS +3 -0
- package/src/plugins/source-contract/vendor/RELEASE.json +17 -0
- package/src/plugins/source-contract/vendor/SHA256SUMS +41 -0
- package/src/plugins/source-contract/vendor/THIRD-PARTY-NOTICES.md +35 -0
- package/src/plugins/source-contract/vendor/broker-client.js +93 -0
- package/src/plugins/source-contract/vendor/chunks/chunk-3M5PAKSS.js +84 -0
- package/src/plugins/source-contract/vendor/chunks/chunk-4ERCA4TK.js +185 -0
- package/src/plugins/source-contract/vendor/chunks/chunk-6HDIB2SA.js +183 -0
- package/src/plugins/source-contract/vendor/chunks/chunk-ELPJD3ND.js +10 -0
- package/src/plugins/source-contract/vendor/chunks/chunk-HLVAKAYG.js +10 -0
- package/src/plugins/source-contract/vendor/chunks/chunk-I2RXJJJM.js +81 -0
- package/src/plugins/source-contract/vendor/chunks/chunk-KWEWSZ4V.js +505 -0
- package/src/plugins/source-contract/vendor/chunks/chunk-KYWBTAF4.js +182 -0
- package/src/plugins/source-contract/vendor/chunks/chunk-Q3RQA5J2.js +249 -0
- package/src/plugins/source-contract/vendor/chunks/chunk-RRUAIQ4I.js +156 -0
- package/src/plugins/source-contract/vendor/chunks/chunk-RYNKNCPP.js +165 -0
- package/src/plugins/source-contract/vendor/chunks/chunk-W5SHR7DQ.js +9477 -0
- package/src/plugins/source-contract/vendor/chunks/chunk-ZUKF7P6U.js +653 -0
- package/src/plugins/source-contract/vendor/cursor.js +26 -0
- package/src/plugins/source-contract/vendor/descriptor.js +34 -0
- package/src/plugins/source-contract/vendor/events.js +257 -0
- package/src/plugins/source-contract/vendor/index.js +10 -0
- package/src/plugins/source-contract/vendor/params.js +22 -0
- package/src/plugins/source-contract/vendor/permission.js +28 -0
- package/src/plugins/source-contract/vendor/pick.js +22 -0
- package/src/plugins/source-contract/vendor/read.js +29 -0
- package/src/plugins/source-contract/vendor/registry.js +23 -0
- package/src/plugins/source-contract/vendor/schema.js +17 -0
- package/src/plugins/source-contract/vendor/schemas/output.agenda.json +96 -0
- package/src/plugins/source-contract/vendor/schemas/output.countdown.json +28 -0
- package/src/plugins/source-contract/vendor/schemas/output.image.json +28 -0
- package/src/plugins/source-contract/vendor/schemas/output.inbox.json +84 -0
- package/src/plugins/source-contract/vendor/schemas/output.links.json +46 -0
- package/src/plugins/source-contract/vendor/schemas/output.list.json +57 -0
- package/src/plugins/source-contract/vendor/schemas/output.metric.json +61 -0
- package/src/plugins/source-contract/vendor/schemas/output.note.json +14 -0
- package/src/plugins/source-contract/vendor/schemas/output.progress.json +50 -0
- package/src/plugins/source-contract/vendor/schemas/output.record.json +38 -0
- package/src/plugins/source-contract/vendor/schemas/output.table.json +61 -0
- package/src/plugins/source-contract/vendor/schemas/output.thread.json +62 -0
- package/src/plugins/source-contract/vendor/schemas/sources.json +819 -0
- package/src/plugins/source-contract/vendor/schemas.js +19 -0
- package/src/plugins/source-contract/vendor/tools-rpc.js +42 -0
- package/src/plugins/verify.js +56 -0
- package/src/protocol.js +11 -6
- package/src/recall.js +92 -5
- package/src/records.js +61 -2
- package/src/requests.js +216 -0
- package/src/runtime.js +89 -7
- package/src/setup-time.js +68 -0
- package/src/skills.js +230 -8
- package/src/source-ids.js +25 -0
- package/src/sources/declaration.js +365 -0
- package/src/sources/reads.js +772 -0
- package/src/sources/rpc.js +151 -0
- package/src/sources.js +389 -92
- package/src/spaces.js +283 -11
- package/src/spool.js +61 -4
- package/src/stated.js +10 -5
- package/src/storable.js +139 -0
- package/src/tools/commitments.js +37 -4
- package/src/tools/held-write.js +31 -12
- package/src/tools/index.js +27 -0
- package/src/tools/ingest.js +27 -2
- package/src/tools/memory.js +30 -7
- package/src/tools/page.js +95 -9
- package/src/tools/recall.js +208 -4
- package/src/tools/share.js +42 -3
- package/src/tools/shared.js +44 -4
- package/src/tools/space-lifecycle.js +278 -0
- package/src/tools/spaces.js +373 -29
- package/src/tools/status.js +9 -2
- package/src/tools/wiki.js +34 -4
- package/src/util.js +110 -6
- package/src/vendor/source-contract/README.md +24 -0
- package/src/vendor/source-contract/THIRD-PARTY-NOTICES.md +35 -0
- package/src/vendor/source-contract/VENDOR.json +81 -0
- package/src/vendor/source-contract/chunks/chunk-3M5PAKSS.js +84 -0
- package/src/vendor/source-contract/chunks/chunk-6HDIB2SA.js +183 -0
- package/src/vendor/source-contract/chunks/chunk-HLVAKAYG.js +10 -0
- package/src/vendor/source-contract/chunks/chunk-I2RXJJJM.js +81 -0
- package/src/vendor/source-contract/chunks/chunk-KWEWSZ4V.js +505 -0
- package/src/vendor/source-contract/chunks/chunk-W5SHR7DQ.js +9477 -0
- package/src/vendor/source-contract/chunks/chunk-ZUKF7P6U.js +653 -0
- package/src/vendor/source-contract/cursor.js +26 -0
- package/src/vendor/source-contract/events.js +257 -0
- package/src/vendor/source-contract/registry.js +23 -0
- package/src/vendor/source-contract/tools-rpc.js +42 -0
- package/src/views/access-reads.js +256 -0
- package/src/views/communities.js +77 -0
- package/src/views/contracts.js +930 -21
- package/src/views/inbox-bytes.js +103 -0
- package/src/views/profile.js +31 -0
- package/src/views/records-reads.js +355 -38
- package/src/views/rpc.js +118 -8
- package/src/views/shape.js +8 -5
- package/views/pages.html +2 -2
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The locator (DESIGN-PLUGIN-MEMORY §2.3 step 2): finds the installed
|
|
3
|
+
* `package.json` of a plugin `/api/plugin` lists, the only place Memory reads
|
|
4
|
+
* a declaration from.
|
|
5
|
+
*
|
|
6
|
+
* - **local:** `<source.path>/package.json`.
|
|
7
|
+
* - **package, git target:** `<root>/git-*-<sha256(target)[:12]>/<stamp>/`,
|
|
8
|
+
* every stamp whose `package.json` maps a dependency to the target and whose
|
|
9
|
+
* `package-lock.json` resolves it at `#<source.version>` (the commit SHA).
|
|
10
|
+
* - **package, registry target:** `<root>/<name>@<spec or latest>/<stamp>/`,
|
|
11
|
+
* every stamp whose dependency and lockfile `version` equal
|
|
12
|
+
* `source.version` (the npm version).
|
|
13
|
+
* - Several matching stamps are accepted when their `package.json#memory`
|
|
14
|
+
* digests match; when they differ the plugin is unverified.
|
|
15
|
+
* - **Confinement:** every real path stays under the cache root (or the local
|
|
16
|
+
* directory): no symlink out, and a `package.json` of at most 256 KB.
|
|
17
|
+
* - **Fail closed:** anything else is `{ ok: false }` with the reason. The
|
|
18
|
+
* layout is OpenCode's internal one (2.2.6, S0 Q3); when it changes, the
|
|
19
|
+
* discovery reports `LAYOUT_CHANGED`.
|
|
20
|
+
*
|
|
21
|
+
* Synchronous and read-only; it never writes, follows no link out, and never
|
|
22
|
+
* reads a second cache root.
|
|
23
|
+
*/
|
|
24
|
+
import { createHash } from "node:crypto"
|
|
25
|
+
import fs from "node:fs"
|
|
26
|
+
import os from "node:os"
|
|
27
|
+
import path from "node:path"
|
|
28
|
+
import { descriptorHash } from "./source-contract/vendor/descriptor.js"
|
|
29
|
+
import { manifestDigest } from "./manifest.js"
|
|
30
|
+
|
|
31
|
+
export const LAYOUT_CHANGED = "OpenCode changed its plugin store; update Memory"
|
|
32
|
+
export const NOT_FOUND = "Memory can't find this plugin's installed package"
|
|
33
|
+
export const NOT_AT_COMMIT = "this plugin's installed copy is not at the commit OpenCode reports"
|
|
34
|
+
export const STAMPS_DIFFER = "the installed copies of this plugin at the same version differ"
|
|
35
|
+
export const MAX_PACKAGE_JSON = 256 * 1024
|
|
36
|
+
/** Stamps read per target at most: a cache directory holds a handful. */
|
|
37
|
+
const MAX_STAMPS = 64
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The root of OpenCode's plugin installs: OpenCode's own `Global.Path.cache`
|
|
41
|
+
* with `/npm`, read from the host the collector runs in. Only when that
|
|
42
|
+
* module cannot be loaded: `$XDG_CACHE_HOME/opencode` when the variable is
|
|
43
|
+
* set (even if the directory does not exist yet), else
|
|
44
|
+
* `$HOME/.cache/opencode` (S0 Q2). One root; nothing is probed.
|
|
45
|
+
*
|
|
46
|
+
* @param {{ load?: () => Promise<any>, env?: Record<string, string | undefined> }} [options]
|
|
47
|
+
*/
|
|
48
|
+
export async function cacheRoot({ load = () => import("@opencode/util/global"), env = process.env } = {}) {
|
|
49
|
+
try {
|
|
50
|
+
const module = await load()
|
|
51
|
+
const cache = module?.Global?.Path?.cache ?? module?.Path?.cache
|
|
52
|
+
if (typeof cache === "string" && path.isAbsolute(cache)) return path.join(cache, "npm")
|
|
53
|
+
} catch {
|
|
54
|
+
/* not inside an OpenCode host: the fallback */
|
|
55
|
+
}
|
|
56
|
+
const xdg = env.XDG_CACHE_HOME
|
|
57
|
+
const base = xdg ? path.join(xdg, "opencode") : path.join(env.HOME || os.homedir(), ".cache", "opencode")
|
|
58
|
+
return path.join(base, "npm")
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const missing = (detail) => ({ ok: false, cause: "missing", reason: NOT_FOUND, detail })
|
|
62
|
+
const escape = (detail) => ({ ok: false, cause: "escape", reason: `${NOT_FOUND}: a file links outside the plugin store`, detail })
|
|
63
|
+
const unreadable = (detail) => ({ ok: false, cause: "unreadable", reason: `${NOT_FOUND}: its package.json could not be read`, detail })
|
|
64
|
+
|
|
65
|
+
function realpath(file) {
|
|
66
|
+
try {
|
|
67
|
+
return fs.realpathSync(file)
|
|
68
|
+
} catch {
|
|
69
|
+
return null
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function within(real, root) {
|
|
74
|
+
return real === root || real.startsWith(root.endsWith(path.sep) ? root : root + path.sep)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Read a small JSON file through one descriptor: opened without following a
|
|
79
|
+
* link (`O_NOFOLLOW` where the platform has it), checked with `fstat` (a
|
|
80
|
+
* regular file, at most `cap` bytes, and, unless `links`, not hard-linked),
|
|
81
|
+
* and read from that descriptor up to the cap, so a file swapped or grown
|
|
82
|
+
* after the check is not read past it.
|
|
83
|
+
*
|
|
84
|
+
* @param {string} file
|
|
85
|
+
* @param {{ cap: number, links?: boolean }} options `links`: hard links allowed (a local plugin's own files)
|
|
86
|
+
* @returns {{ value: unknown } | { missing: true } | { escape: true } | { unreadable: true }}
|
|
87
|
+
*/
|
|
88
|
+
export function readCapped(file, { cap, links = false }) {
|
|
89
|
+
let fd
|
|
90
|
+
try {
|
|
91
|
+
fd = fs.openSync(file, fs.constants.O_RDONLY | (fs.constants.O_NOFOLLOW ?? 0))
|
|
92
|
+
} catch (error) {
|
|
93
|
+
if (error?.code === "ENOENT" || error?.code === "ENOTDIR") return { missing: true }
|
|
94
|
+
if (error?.code === "ELOOP" || error?.code === "EMLINK") return { escape: true }
|
|
95
|
+
return { unreadable: true }
|
|
96
|
+
}
|
|
97
|
+
try {
|
|
98
|
+
const stat = fs.fstatSync(fd)
|
|
99
|
+
if (!stat.isFile()) return { unreadable: true }
|
|
100
|
+
if (!links && stat.nlink > 1) return { escape: true }
|
|
101
|
+
if (stat.size > cap) return { unreadable: true }
|
|
102
|
+
const buffer = Buffer.alloc(cap + 1)
|
|
103
|
+
let total = 0
|
|
104
|
+
while (total < buffer.length) {
|
|
105
|
+
const read = fs.readSync(fd, buffer, total, buffer.length - total, total)
|
|
106
|
+
if (read === 0) break
|
|
107
|
+
total += read
|
|
108
|
+
}
|
|
109
|
+
if (total > cap) return { unreadable: true }
|
|
110
|
+
return { value: JSON.parse(buffer.subarray(0, total).toString("utf8")) }
|
|
111
|
+
} catch {
|
|
112
|
+
return { unreadable: true }
|
|
113
|
+
} finally {
|
|
114
|
+
fs.closeSync(fd)
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** A small JSON file under `root`, by real path, read once (readCapped); `{ escape }` when it is outside the root. */
|
|
119
|
+
function readJson(file, root, max = MAX_PACKAGE_JSON, { links = false } = {}) {
|
|
120
|
+
const real = realpath(file)
|
|
121
|
+
if (!real) return { missing: true }
|
|
122
|
+
if (!within(real, root)) return { escape: true }
|
|
123
|
+
return readCapped(real, { cap: max, links })
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function isObject(value) {
|
|
127
|
+
return value !== null && typeof value === "object" && !Array.isArray(value)
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** The stamps of one cache directory, by real path, each confined to the root. */
|
|
131
|
+
function stampsOf(dir, root) {
|
|
132
|
+
let names
|
|
133
|
+
try {
|
|
134
|
+
names = fs.readdirSync(dir).sort()
|
|
135
|
+
} catch {
|
|
136
|
+
return { stamps: [], escaped: false }
|
|
137
|
+
}
|
|
138
|
+
const stamps = []
|
|
139
|
+
let escaped = false
|
|
140
|
+
for (const name of names.slice(0, MAX_STAMPS)) {
|
|
141
|
+
const real = realpath(path.join(dir, name))
|
|
142
|
+
if (!real) continue
|
|
143
|
+
if (!within(real, root)) {
|
|
144
|
+
escaped = true
|
|
145
|
+
continue
|
|
146
|
+
}
|
|
147
|
+
try {
|
|
148
|
+
if (fs.statSync(real).isDirectory()) stamps.push(real)
|
|
149
|
+
} catch {
|
|
150
|
+
/* gone */
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return { stamps, escaped }
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** The installed package inside a matching stamp: its package.json, confined. */
|
|
157
|
+
function packageIn(stamp, name, root) {
|
|
158
|
+
const dir = path.join(stamp, "node_modules", ...name.split("/"))
|
|
159
|
+
const real = realpath(dir)
|
|
160
|
+
if (!real) return missing(`no node_modules/${name}`)
|
|
161
|
+
if (!within(real, root)) return escape(`node_modules/${name}`)
|
|
162
|
+
const read = readJson(path.join(real, "package.json"), root)
|
|
163
|
+
if (read.escape) return escape(`node_modules/${name}/package.json`)
|
|
164
|
+
if (read.missing) return missing(`no node_modules/${name}/package.json`)
|
|
165
|
+
if (read.unreadable || !isObject(read.value)) return unreadable(`node_modules/${name}/package.json`)
|
|
166
|
+
return { ok: true, dir: real, pkg: read.value }
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** Every matching stamp's package; accepted only when their declarations agree. */
|
|
170
|
+
function choose(found, extra) {
|
|
171
|
+
const okay = found.filter((item) => item.ok)
|
|
172
|
+
if (okay.length === 0) return found.find((item) => item.cause === "escape") ?? found.find((item) => item.cause === "unreadable") ?? missing("no stamp at this version")
|
|
173
|
+
const digests = new Set(okay.map((item) => digestOf(item.pkg)))
|
|
174
|
+
if (digests.size > 1) return { ok: false, cause: "differ", reason: STAMPS_DIFFER }
|
|
175
|
+
const first = okay[0]
|
|
176
|
+
return { ok: true, ...extra(first), dir: first.dir, packageName: typeof first.pkg.name === "string" ? first.pkg.name : null, packageVersion: typeof first.pkg.version === "string" ? first.pkg.version : null, memory: first.pkg.memory, sources: first.pkg.sources, digest: shown(digestOf(first.pkg)), stamps: okay.length }
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** A digest as reported: a sha256 digest, or null (no declaration, or one too deep to digest). */
|
|
180
|
+
function shown(digest) {
|
|
181
|
+
return /^sha256:[0-9a-f]{64}$/.test(digest) ? digest : null
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* A stamp's declaration digest, for comparing stamps: `sources`' own hash when
|
|
186
|
+
* the package declares sources (it is the hash the person consents to),
|
|
187
|
+
* the `memory` manifest's when it declares that, "none" without either, and
|
|
188
|
+
* "unreadable" when it cannot be digested.
|
|
189
|
+
*/
|
|
190
|
+
function digestOf(pkg) {
|
|
191
|
+
if (pkg.sources !== undefined) {
|
|
192
|
+
try {
|
|
193
|
+
return descriptorHash(pkg.sources)
|
|
194
|
+
} catch {
|
|
195
|
+
return "unreadable"
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
if (pkg.memory === undefined) return "none"
|
|
199
|
+
try {
|
|
200
|
+
return manifestDigest(pkg.memory)
|
|
201
|
+
} catch {
|
|
202
|
+
return "unreadable"
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** A git-style target: github:, gitlab:, bitbucket:, git+…, git://, git@…, a .git URL, or owner/repo. */
|
|
207
|
+
export function isGitTarget(target) {
|
|
208
|
+
return /^(?:github:|gitlab:|bitbucket:|git\+|git:\/\/|git@)/.test(target) || /^https?:\/\/\S+\.git(?:#.*)?$/.test(target) || /^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+(?:#.*)?$/.test(target)
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** `@scope/name@spec` → `{ name, spec }`, with `latest` for no spec; null when it is not an npm name. */
|
|
212
|
+
export function parseRegistryTarget(target) {
|
|
213
|
+
const match = /^((?:@[a-z0-9][a-z0-9._~-]*\/)?[a-z0-9][a-z0-9._~-]*)(?:@(.+))?$/.exec(target)
|
|
214
|
+
if (!match) return null
|
|
215
|
+
return { name: match[1], spec: match[2] ?? "latest" }
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function locateGit(target, version, root) {
|
|
219
|
+
if (typeof version !== "string" || !/^[0-9a-f]{7,64}$/.test(version)) return missing("no commit reported for a git plugin")
|
|
220
|
+
const suffix = `-${createHash("sha256").update(target).digest("hex").slice(0, 12)}`
|
|
221
|
+
let dirs
|
|
222
|
+
try {
|
|
223
|
+
dirs = fs.readdirSync(root).filter((name) => name.startsWith("git-") && name.endsWith(suffix))
|
|
224
|
+
} catch {
|
|
225
|
+
return missing("no plugin store")
|
|
226
|
+
}
|
|
227
|
+
const found = []
|
|
228
|
+
for (const name of dirs) {
|
|
229
|
+
const dir = realpath(path.join(root, name))
|
|
230
|
+
if (!dir) continue
|
|
231
|
+
if (!within(dir, root)) {
|
|
232
|
+
found.push(escape(name))
|
|
233
|
+
continue
|
|
234
|
+
}
|
|
235
|
+
const { stamps, escaped } = stampsOf(dir, root)
|
|
236
|
+
if (escaped) found.push(escape(`${name}/<stamp>`))
|
|
237
|
+
for (const stamp of stamps) {
|
|
238
|
+
const manifest = readJson(path.join(stamp, "package.json"), root)
|
|
239
|
+
const lock = readJson(path.join(stamp, "package-lock.json"), root, 16 * MAX_PACKAGE_JSON)
|
|
240
|
+
if (manifest.escape || lock.escape) {
|
|
241
|
+
found.push(escape(stamp))
|
|
242
|
+
continue
|
|
243
|
+
}
|
|
244
|
+
if (!isObject(manifest.value?.dependencies) || !isObject(lock.value?.packages)) continue
|
|
245
|
+
const dependency = Object.entries(manifest.value.dependencies).find(([, spec]) => spec === target)?.[0]
|
|
246
|
+
if (!dependency) continue
|
|
247
|
+
const locked = lock.value.packages[`node_modules/${dependency}`]
|
|
248
|
+
if (typeof locked?.resolved !== "string" || !locked.resolved.endsWith(`#${version}`)) continue
|
|
249
|
+
found.push({ ...packageIn(stamp, dependency, root), dependency })
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
// The store holds the target, at another commit: the install is older or
|
|
253
|
+
// newer than `/api/plugin` says, and it is never read as this version's.
|
|
254
|
+
if (found.length === 0 && dirs.length > 0) return { ok: false, cause: "stale", reason: NOT_AT_COMMIT }
|
|
255
|
+
return choose(found, () => ({ kind: "git" }))
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function locateRegistry(target, version, root) {
|
|
259
|
+
const parsed = parseRegistryTarget(target)
|
|
260
|
+
if (!parsed) return { ok: false, cause: "unsupported", reason: `${NOT_FOUND}: Memory does not know this kind of plugin source` }
|
|
261
|
+
if (typeof version !== "string" || version === "") return missing("no version reported")
|
|
262
|
+
const dir = path.join(root, ...`${parsed.name}@${parsed.spec}`.split("/"))
|
|
263
|
+
const real = realpath(dir)
|
|
264
|
+
if (!real) return missing(`no ${parsed.name}@${parsed.spec}`)
|
|
265
|
+
if (!within(real, root)) return escape(`${parsed.name}@${parsed.spec}`)
|
|
266
|
+
const { stamps, escaped } = stampsOf(real, root)
|
|
267
|
+
const found = escaped ? [escape(`${parsed.name}@${parsed.spec}/<stamp>`)] : []
|
|
268
|
+
for (const stamp of stamps) {
|
|
269
|
+
const manifest = readJson(path.join(stamp, "package.json"), root)
|
|
270
|
+
const lock = readJson(path.join(stamp, "package-lock.json"), root, 16 * MAX_PACKAGE_JSON)
|
|
271
|
+
if (manifest.escape || lock.escape) {
|
|
272
|
+
found.push(escape(stamp))
|
|
273
|
+
continue
|
|
274
|
+
}
|
|
275
|
+
const locked = isObject(lock.value?.packages) ? lock.value.packages[`node_modules/${parsed.name}`] : null
|
|
276
|
+
if (manifest.value?.dependencies?.[parsed.name] !== version || locked?.version !== version) continue
|
|
277
|
+
found.push({ ...packageIn(stamp, parsed.name, root), integrity: typeof locked.integrity === "string" ? locked.integrity : null })
|
|
278
|
+
}
|
|
279
|
+
return choose(found, (first) => ({ kind: "registry", integrity: first.integrity ?? null }))
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function locateLocal(dir) {
|
|
283
|
+
if (typeof dir !== "string" || !path.isAbsolute(dir)) return missing("a local plugin's path is not absolute")
|
|
284
|
+
const real = realpath(dir)
|
|
285
|
+
if (!real) return missing("the local directory does not exist")
|
|
286
|
+
const read = readJson(path.join(real, "package.json"), real, MAX_PACKAGE_JSON, { links: true })
|
|
287
|
+
if (read.escape) return escape("package.json")
|
|
288
|
+
if (read.missing) return missing("no package.json")
|
|
289
|
+
if (read.unreadable || !isObject(read.value)) return unreadable("package.json")
|
|
290
|
+
const pkg = read.value
|
|
291
|
+
return {
|
|
292
|
+
ok: true,
|
|
293
|
+
kind: "local",
|
|
294
|
+
dir: real,
|
|
295
|
+
packageName: typeof pkg.name === "string" ? pkg.name : null,
|
|
296
|
+
packageVersion: typeof pkg.version === "string" ? pkg.version : null,
|
|
297
|
+
memory: pkg.memory,
|
|
298
|
+
sources: pkg.sources,
|
|
299
|
+
digest: shown(digestOf(pkg)),
|
|
300
|
+
stamps: 1,
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Find one plugin's installed package and read its `package.json`.
|
|
306
|
+
*
|
|
307
|
+
* @param {{ source?: { type?: string, target?: string, version?: string, path?: string } }} info a `/api/plugin` entry
|
|
308
|
+
* @param {{ root: string }} options the cache root (`cacheRoot()`)
|
|
309
|
+
* @returns {{ ok: true, kind: "local" | "git" | "registry", dir: string, packageName: string | null, packageVersion: string | null, memory: unknown, digest: string | null, stamps: number, integrity?: string | null }
|
|
310
|
+
* | { ok: false, cause: "missing" | "differ" | "escape" | "unreadable" | "unsupported", reason: string, detail?: string }}
|
|
311
|
+
*/
|
|
312
|
+
export function locate(info, { root }) {
|
|
313
|
+
const source = info?.source ?? {}
|
|
314
|
+
if (source.type === "local") return locateLocal(source.path)
|
|
315
|
+
if (source.type !== "package" || typeof source.target !== "string" || source.target === "") return { ok: false, cause: "unsupported", reason: `${NOT_FOUND}: Memory does not know this kind of plugin source` }
|
|
316
|
+
const realRoot = realpath(root)
|
|
317
|
+
if (!realRoot) return missing("no plugin store")
|
|
318
|
+
return isGitTarget(source.target) ? locateGit(source.target, source.version, realRoot) : locateRegistry(source.target, source.version, realRoot)
|
|
319
|
+
}
|