@acryldev/plugin-share 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +85 -2
- package/index.ts +791 -0
- package/lib/index.js +880 -0
- package/lib/index.js.map +1 -0
- package/lib/package.js +138 -0
- package/lib/package.js.map +1 -0
- package/lib/types/index.d.ts +170 -0
- package/lib/types/native.d.ts +55 -0
- package/lib/types/package.d.ts +30 -0
- package/lib/types/publish.d.ts +55 -0
- package/lib/types/pull-check.d.ts +34 -0
- package/lib/types/pull-list.d.ts +32 -0
- package/lib/types/pulled-records.d.ts +16 -0
- package/lib/types/tsdown.config.d.ts +2 -0
- package/native.ts +290 -0
- package/package.json +51 -3
- package/package.ts +186 -0
- package/plugin.json +19 -0
- package/publish.ts +79 -0
- package/pull-check.ts +142 -0
- package/pull-list.ts +71 -0
- package/pulled-records.ts +70 -0
- package/test/native.test.ts +213 -0
- package/test/plugin-share.test.ts +1001 -0
- package/tsconfig.json +21 -0
- package/tsdown.config.ts +23 -0
package/plugin.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "plugin-share",
|
|
3
|
+
"description": "Share plugins between people. Pull one someone released into the running app: fetched without running anything, checked by digest and for every OS, then installed live. Publish one, only as a plan a person confirmed.",
|
|
4
|
+
"requires": ["instance", "extensions", "manifest"],
|
|
5
|
+
"inject": ["extensions", "appInstance"],
|
|
6
|
+
"provides": ["pluginShare"],
|
|
7
|
+
"dependencies": {
|
|
8
|
+
"@deepseek-ai/cordis": "4.0.4",
|
|
9
|
+
"@acryldev/app-persistence": "0.1.2",
|
|
10
|
+
"@acryldev/blends-core": "0.6.0",
|
|
11
|
+
"@acryldev/plugin-hub-core": "0.2.2",
|
|
12
|
+
"yaml": "2.9.0"
|
|
13
|
+
},
|
|
14
|
+
"mount": "mount it after extensions, in development builds: root.plugin(pluginShare, { dir: join(root, 'plugins') }); dir must be an extensions source",
|
|
15
|
+
"entries": {
|
|
16
|
+
"": "index.ts",
|
|
17
|
+
"/package": "package.ts"
|
|
18
|
+
}
|
|
19
|
+
}
|
package/publish.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Publishing a plugin: what a person is asked to say yes to, and the comparison that makes the yes mean exactly that. Pure; the service does the I/O.
|
|
3
|
+
*/
|
|
4
|
+
import type { PublishGitPort, RepositoryHostPort } from '@acryldev/plugin-hub-core'
|
|
5
|
+
|
|
6
|
+
/** Everything that leaves the machine, named before anything does. */
|
|
7
|
+
export interface PublishPlan {
|
|
8
|
+
readonly owner: string
|
|
9
|
+
readonly repo: string
|
|
10
|
+
readonly version: string
|
|
11
|
+
readonly visibility: 'public'
|
|
12
|
+
readonly license: string
|
|
13
|
+
/** The repository does not exist yet: publishing creates it. */
|
|
14
|
+
readonly createsRepository: boolean
|
|
15
|
+
/** The version already released there; the plan then releases the next patch. */
|
|
16
|
+
readonly released?: string
|
|
17
|
+
readonly files: readonly string[]
|
|
18
|
+
/** The sha512 of the release file. */
|
|
19
|
+
readonly integrity: string
|
|
20
|
+
/** The sha256 of the plugin's content. */
|
|
21
|
+
readonly digest: string
|
|
22
|
+
/** What a friend runs to get exactly these bytes. */
|
|
23
|
+
readonly pullLine: string
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface PublishRefused {
|
|
27
|
+
readonly ok: false
|
|
28
|
+
readonly reason: string
|
|
29
|
+
readonly next: string
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export type PublishResult =
|
|
33
|
+
| {
|
|
34
|
+
readonly ok: true
|
|
35
|
+
readonly url: string
|
|
36
|
+
readonly releaseUrl: string
|
|
37
|
+
readonly version: string
|
|
38
|
+
readonly integrity: string
|
|
39
|
+
readonly digest: string
|
|
40
|
+
readonly pullLine: string
|
|
41
|
+
}
|
|
42
|
+
| PublishRefused
|
|
43
|
+
|
|
44
|
+
export interface PublishOptions {
|
|
45
|
+
/** The account or organization; the signed-in one when absent. */
|
|
46
|
+
readonly owner?: string
|
|
47
|
+
/** The license the plugin is offered under, as an SPDX id; MIT when absent. The plan shows it before the person says yes. */
|
|
48
|
+
readonly license?: string
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Where a publish goes through: the person's own `gh` and `git`. A test passes stand-ins, so no test publishes. */
|
|
52
|
+
export interface PublishPorts {
|
|
53
|
+
readonly host: RepositoryHostPort
|
|
54
|
+
readonly git: PublishGitPort
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** The line a friend copies: it names the bytes, so the check comes for free. */
|
|
58
|
+
export const pullLineOf = (owner: string, repo: string, version: string, digest: string): string =>
|
|
59
|
+
`npm run acryl -- plugin pull github:${owner}/${repo}@${version}@sha256:${digest}`
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The same plan: what the person confirmed is what would be published now. `released` only explains the version, and is gone once the next one is packaged.
|
|
63
|
+
* Field by field, so a plan that came back through JSON from another surface (the frame's confirm) is the same plan whatever order its fields arrived in.
|
|
64
|
+
*/
|
|
65
|
+
export function samePlan(a: PublishPlan, b: PublishPlan): boolean {
|
|
66
|
+
return (
|
|
67
|
+
a.owner === b.owner &&
|
|
68
|
+
a.repo === b.repo &&
|
|
69
|
+
a.version === b.version &&
|
|
70
|
+
a.visibility === b.visibility &&
|
|
71
|
+
a.license === b.license &&
|
|
72
|
+
a.createsRepository === b.createsRepository &&
|
|
73
|
+
a.integrity === b.integrity &&
|
|
74
|
+
a.digest === b.digest &&
|
|
75
|
+
a.pullLine === b.pullLine &&
|
|
76
|
+
a.files.length === b.files.length &&
|
|
77
|
+
a.files.every((file, index) => file === b.files[index])
|
|
78
|
+
)
|
|
79
|
+
}
|
package/pull-check.ts
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What decides whether a pulled plugin may enter this app. Pure, over the files of the package: nothing here touches a disk or runs the plugin,
|
|
3
|
+
* so a refusal has nothing to undo (Clean Architecture: policy free of I/O).
|
|
4
|
+
*/
|
|
5
|
+
import { digestFiles, type PluginFile, portabilityIssues } from '@acryldev/blends-core'
|
|
6
|
+
import { hostInjectFromSource } from '@acryldev/plugin-hub-core'
|
|
7
|
+
import { nativesOf } from './native.ts'
|
|
8
|
+
|
|
9
|
+
export type Refusal =
|
|
10
|
+
| 'source'
|
|
11
|
+
| 'fetch'
|
|
12
|
+
| 'name'
|
|
13
|
+
| 'digest'
|
|
14
|
+
| 'portability'
|
|
15
|
+
| 'dependencies'
|
|
16
|
+
/** Its `acrylblends.native` list (native.ts) cannot be used. */
|
|
17
|
+
| 'native'
|
|
18
|
+
| 'entry'
|
|
19
|
+
| 'inject'
|
|
20
|
+
| 'missing-service'
|
|
21
|
+
| 'folder-exists'
|
|
22
|
+
| 'install'
|
|
23
|
+
/** A list of sources that cannot be read, or holds a line that is not one source. */
|
|
24
|
+
| 'list'
|
|
25
|
+
|
|
26
|
+
export interface PullRefused {
|
|
27
|
+
readonly ok: false
|
|
28
|
+
readonly refusal: Refusal
|
|
29
|
+
readonly reason: string
|
|
30
|
+
/** What the person can do about it. */
|
|
31
|
+
readonly next: string
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export const refuse = (refusal: Refusal, reason: string, next: string): PullRefused => ({
|
|
35
|
+
ok: false,
|
|
36
|
+
refusal,
|
|
37
|
+
reason,
|
|
38
|
+
next,
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
const SHA256 = /^[0-9a-f]{64}$/
|
|
42
|
+
const DIGEST_MARK = '@sha256:'
|
|
43
|
+
|
|
44
|
+
/** The source a person wrote, split into where it comes from and the content digest it names. @throws when the digest is malformed. */
|
|
45
|
+
export function splitDigest(input: string): { readonly source: string; readonly digest?: string } {
|
|
46
|
+
const at = input.lastIndexOf(DIGEST_MARK)
|
|
47
|
+
if (at === -1) return { source: input }
|
|
48
|
+
const digest = input.slice(at + DIGEST_MARK.length)
|
|
49
|
+
if (!SHA256.test(digest))
|
|
50
|
+
throw new Error(`"${digest}" is not a content digest: it has 64 lowercase hex characters`)
|
|
51
|
+
return { source: input.slice(0, at), digest }
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** The entry files the extensions folder loads, in its order. */
|
|
55
|
+
const ENTRIES = ['index.ts', 'index.js', 'index.mjs']
|
|
56
|
+
|
|
57
|
+
const text = (file: PluginFile): string => Buffer.from(file.bytes).toString('utf8')
|
|
58
|
+
|
|
59
|
+
export interface PullContext {
|
|
60
|
+
/** The content digest the source names. */
|
|
61
|
+
readonly digest?: string | undefined
|
|
62
|
+
/** Whether the extensions folder can hold a plugin of this name. */
|
|
63
|
+
readonly nameFits: (name: string) => boolean
|
|
64
|
+
/** Whether this app has the service right now. */
|
|
65
|
+
readonly hasService: (name: string) => boolean
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** The first reason this package may not enter the app, or undefined. */
|
|
69
|
+
export function pullRefusal(
|
|
70
|
+
manifest: Readonly<Record<string, unknown>>,
|
|
71
|
+
files: readonly PluginFile[],
|
|
72
|
+
context: PullContext,
|
|
73
|
+
): PullRefused | undefined {
|
|
74
|
+
const name = String(manifest.name)
|
|
75
|
+
if (!context.nameFits(name)) {
|
|
76
|
+
return refuse(
|
|
77
|
+
'name',
|
|
78
|
+
`the package is called "${name}", which a folder in plugins/ cannot hold`,
|
|
79
|
+
'ask its author to release it under lowercase letters, digits and dashes',
|
|
80
|
+
)
|
|
81
|
+
}
|
|
82
|
+
const digest = digestFiles(files)
|
|
83
|
+
if (context.digest !== undefined && context.digest !== digest) {
|
|
84
|
+
return refuse(
|
|
85
|
+
'digest',
|
|
86
|
+
`${digest} is not the digest the source names (${context.digest}); these are different bytes`,
|
|
87
|
+
'ask the person who gave you the digest which release they meant',
|
|
88
|
+
)
|
|
89
|
+
}
|
|
90
|
+
const issues = portabilityIssues(files)
|
|
91
|
+
if (issues.length > 0) {
|
|
92
|
+
return refuse(
|
|
93
|
+
'portability',
|
|
94
|
+
issues.map((issue) => `${issue.path}: ${issue.reason}`).join('; '),
|
|
95
|
+
'ask its author to fix this and release again, so it works on every OS',
|
|
96
|
+
)
|
|
97
|
+
}
|
|
98
|
+
const dependencies = Object.keys(
|
|
99
|
+
(manifest.dependencies as Record<string, unknown> | undefined) ?? {},
|
|
100
|
+
)
|
|
101
|
+
if (dependencies.length > 0) {
|
|
102
|
+
return refuse(
|
|
103
|
+
'dependencies',
|
|
104
|
+
`it needs ${dependencies.join(', ')} installed, and a pull installs nothing`,
|
|
105
|
+
'ask its author to bundle them or name them as peers',
|
|
106
|
+
)
|
|
107
|
+
}
|
|
108
|
+
const native = nativesOf(manifest)
|
|
109
|
+
if ('error' in native) {
|
|
110
|
+
return refuse(
|
|
111
|
+
'native',
|
|
112
|
+
native.error,
|
|
113
|
+
'ask its author to fix "acrylblends.native" in its package.json and release again',
|
|
114
|
+
)
|
|
115
|
+
}
|
|
116
|
+
const entry = ENTRIES.flatMap((path) => files.filter((file) => file.path === path))[0]
|
|
117
|
+
if (entry === undefined) {
|
|
118
|
+
return refuse(
|
|
119
|
+
'entry',
|
|
120
|
+
`it has none of ${ENTRIES.join(', ')} at its top level`,
|
|
121
|
+
'this package is not a plugin an app can load; ask its author',
|
|
122
|
+
)
|
|
123
|
+
}
|
|
124
|
+
const inject = hostInjectFromSource(text(entry))
|
|
125
|
+
if (inject !== undefined && 'error' in inject) {
|
|
126
|
+
return refuse(
|
|
127
|
+
'inject',
|
|
128
|
+
`${inject.error}. A pull reads what a plugin needs without running it`,
|
|
129
|
+
'ask its author to list the services as plain names',
|
|
130
|
+
)
|
|
131
|
+
}
|
|
132
|
+
const missing = (inject?.names ?? []).filter((service) => !context.hasService(service))
|
|
133
|
+
if (missing.length > 0) return missingServices(missing)
|
|
134
|
+
return undefined
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export const missingServices = (missing: readonly string[]): PullRefused =>
|
|
138
|
+
refuse(
|
|
139
|
+
'missing-service',
|
|
140
|
+
`it needs ${missing.join(', ')}, which this app does not have`,
|
|
141
|
+
'add the plugin that provides it, then pull again',
|
|
142
|
+
)
|
package/pull-list.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A group's list of plugins: one source per line, as each person sends it (`github:<owner>/<repo>@<version>@sha256:<digest>`, or the whole line
|
|
3
|
+
* `plugin publish` prints). The list is text a person or a group keeps (a file in the app, a gist, a file in a repository), so reading it is
|
|
4
|
+
* forgiving about comments, blank lines and the pasted command, and strict about anything else: a line that is not one source is named, not guessed.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/** A list is a few lines of text: anything larger is not a list of plugins. */
|
|
8
|
+
export const LIST_MAX_BYTES = 64 * 1024
|
|
9
|
+
|
|
10
|
+
/** More sources than a group has people is a mistake, not a workshop. */
|
|
11
|
+
export const LIST_MAX_SOURCES = 100
|
|
12
|
+
|
|
13
|
+
/** The command a person pastes from `plugin publish`: only its source is kept. */
|
|
14
|
+
const PULL_COMMAND = /^(?:npm run acryl --\s+)?plugin pull\s+/u
|
|
15
|
+
|
|
16
|
+
/** A `#` at the start of a line or after a space begins a comment; a source never holds one there. */
|
|
17
|
+
const COMMENT = /(?:^|\s)#.*$/u
|
|
18
|
+
|
|
19
|
+
export interface PullListLine {
|
|
20
|
+
/** Its line in the list, from 1, so a problem is easy to find. */
|
|
21
|
+
readonly line: number
|
|
22
|
+
readonly source: string
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface ParsedPullList {
|
|
26
|
+
/** The sources in their order, each once. */
|
|
27
|
+
readonly sources: readonly PullListLine[]
|
|
28
|
+
/** Lines that are not one source, each with what is wrong. */
|
|
29
|
+
readonly problems: readonly string[]
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** The sources of a list, in order and without repeats, and the lines that are not a source. */
|
|
33
|
+
export function parsePullList(text: string): ParsedPullList {
|
|
34
|
+
const sources: PullListLine[] = []
|
|
35
|
+
const problems: string[] = []
|
|
36
|
+
const seen = new Set<string>()
|
|
37
|
+
text.split(/\r?\n/u).forEach((raw, index) => {
|
|
38
|
+
const line = index + 1
|
|
39
|
+
const value = raw.replace(COMMENT, '').trim().replace(PULL_COMMAND, '').trim()
|
|
40
|
+
if (value === '') return
|
|
41
|
+
if (/\s/u.test(value)) {
|
|
42
|
+
problems.push(
|
|
43
|
+
`line ${String(line)} holds more than one word ("${value}"); write one source per line`,
|
|
44
|
+
)
|
|
45
|
+
return
|
|
46
|
+
}
|
|
47
|
+
if (seen.has(value)) return
|
|
48
|
+
seen.add(value)
|
|
49
|
+
sources.push({ line, source: value })
|
|
50
|
+
})
|
|
51
|
+
if (sources.length > LIST_MAX_SOURCES)
|
|
52
|
+
problems.push(
|
|
53
|
+
`the list has ${String(sources.length)} sources; a list holds at most ${String(LIST_MAX_SOURCES)}`,
|
|
54
|
+
)
|
|
55
|
+
return { sources, problems }
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Where a list is read from: an https address, or a file (relative to the app folder). */
|
|
59
|
+
export type PullListLocation =
|
|
60
|
+
| { readonly kind: 'address'; readonly url: string }
|
|
61
|
+
| { readonly kind: 'file'; readonly path: string }
|
|
62
|
+
|
|
63
|
+
/** @returns undefined for something that is neither (an http: address, a github: source): the caller names what a list may be. */
|
|
64
|
+
export function pullListLocation(from: string): PullListLocation | undefined {
|
|
65
|
+
const value = from.trim()
|
|
66
|
+
if (value === '') return undefined
|
|
67
|
+
if (/^https:\/\//iu.test(value)) return { kind: 'address', url: value }
|
|
68
|
+
// A drive letter (C:\...) is a path; any other scheme is not a place a list is read from.
|
|
69
|
+
if (/^[a-z][a-z0-9+.-]+:/iu.test(value) && !/^[a-z]:[\\/]/iu.test(value)) return undefined
|
|
70
|
+
return { kind: 'file', path: value }
|
|
71
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What an app remembers about the plugins it pulled: their source and digests, in the `plugin-share` row's config of `blend.yaml`. A row's config is
|
|
3
|
+
* resolved into `blend.lock.json` too, so the lock records them with no new format, and a copy of the app can fetch exactly the same bytes. Pure text
|
|
4
|
+
* to text; comments and the person's formatting stay.
|
|
5
|
+
*/
|
|
6
|
+
import { isMap, isSeq, parseDocument } from 'yaml'
|
|
7
|
+
|
|
8
|
+
export const ROW = 'plugin-share'
|
|
9
|
+
|
|
10
|
+
export interface PulledRecord {
|
|
11
|
+
/** `github:<owner>/<repo>@<version>` or an https address: something another machine can fetch. */
|
|
12
|
+
readonly source: string
|
|
13
|
+
/** The sha512 of the release file. */
|
|
14
|
+
readonly integrity: string
|
|
15
|
+
/** The sha256 of the plugin's content. */
|
|
16
|
+
readonly digest: string
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const isRecord = (value: unknown): value is PulledRecord => {
|
|
20
|
+
const record = value as Partial<Record<keyof PulledRecord, unknown>> | null
|
|
21
|
+
return (
|
|
22
|
+
typeof record?.source === 'string' &&
|
|
23
|
+
typeof record.integrity === 'string' &&
|
|
24
|
+
typeof record.digest === 'string'
|
|
25
|
+
)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** The plugins this definition remembers pulling. */
|
|
29
|
+
export function pulledRecords(text: string): Readonly<Record<string, PulledRecord>> {
|
|
30
|
+
const rows = (parseDocument(text).toJS() as { spec?: { rows?: unknown } } | null)?.spec?.rows
|
|
31
|
+
if (!Array.isArray(rows)) return {}
|
|
32
|
+
const pulled = (
|
|
33
|
+
rows.find((row: { name?: unknown }) => row?.name === ROW) as
|
|
34
|
+
| { config?: { pulled?: unknown } }
|
|
35
|
+
| undefined
|
|
36
|
+
)?.config?.pulled
|
|
37
|
+
if (typeof pulled !== 'object' || pulled === null) return {}
|
|
38
|
+
return Object.fromEntries(
|
|
39
|
+
Object.entries(pulled).filter(([, record]) => isRecord(record)),
|
|
40
|
+
) as Record<string, PulledRecord>
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The definition with one plugin's record written, or taken out when `record` is undefined. Unchanged when it has no `plugin-share` row: an app
|
|
45
|
+
* composed without the row has nowhere to remember.
|
|
46
|
+
*/
|
|
47
|
+
export function withPulledRecord(
|
|
48
|
+
text: string,
|
|
49
|
+
name: string,
|
|
50
|
+
record: PulledRecord | undefined,
|
|
51
|
+
): string {
|
|
52
|
+
const document = parseDocument(text)
|
|
53
|
+
const rows = document.getIn(['spec', 'rows'])
|
|
54
|
+
if (!isSeq(rows)) return text
|
|
55
|
+
const row = rows.items.find((item) => isMap(item) && item.get('name') === ROW)
|
|
56
|
+
if (!isMap(row)) return text
|
|
57
|
+
if (record === undefined) {
|
|
58
|
+
if (!row.hasIn(['config', 'pulled', name])) return text
|
|
59
|
+
row.deleteIn(['config', 'pulled', name])
|
|
60
|
+
const left = row.getIn(['config', 'pulled'])
|
|
61
|
+
if (isMap(left) && left.items.length === 0) row.deleteIn(['config', 'pulled'])
|
|
62
|
+
const config = row.get('config')
|
|
63
|
+
if (isMap(config) && config.items.length === 0) row.delete('config')
|
|
64
|
+
} else {
|
|
65
|
+
row.setIn(['config'], row.get('config') ?? document.createNode({}))
|
|
66
|
+
if (!row.hasIn(['config', 'pulled'])) row.setIn(['config', 'pulled'], document.createNode({}))
|
|
67
|
+
row.setIn(['config', 'pulled', name], document.createNode(record))
|
|
68
|
+
}
|
|
69
|
+
return String(document)
|
|
70
|
+
}
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import assert from 'node:assert/strict'
|
|
2
|
+
import { execFileSync } from 'node:child_process'
|
|
3
|
+
import { createHash } from 'node:crypto'
|
|
4
|
+
import {
|
|
5
|
+
copyFileSync,
|
|
6
|
+
existsSync,
|
|
7
|
+
lstatSync,
|
|
8
|
+
mkdirSync,
|
|
9
|
+
mkdtempSync,
|
|
10
|
+
readdirSync,
|
|
11
|
+
readFileSync,
|
|
12
|
+
rmSync,
|
|
13
|
+
statSync,
|
|
14
|
+
writeFileSync,
|
|
15
|
+
} from 'node:fs'
|
|
16
|
+
import { tmpdir } from 'node:os'
|
|
17
|
+
import { join } from 'node:path'
|
|
18
|
+
import { afterEach, test } from 'node:test'
|
|
19
|
+
import {
|
|
20
|
+
ensureNative,
|
|
21
|
+
type NativePackage,
|
|
22
|
+
type NativePorts,
|
|
23
|
+
nativeFor,
|
|
24
|
+
nativePorts,
|
|
25
|
+
nativeStore,
|
|
26
|
+
nativesOf,
|
|
27
|
+
PLACEHOLDER,
|
|
28
|
+
releaseNative,
|
|
29
|
+
tarballUrl,
|
|
30
|
+
} from '../native.ts'
|
|
31
|
+
|
|
32
|
+
const cleanup: Array<() => void> = []
|
|
33
|
+
afterEach(() => {
|
|
34
|
+
for (const step of cleanup.splice(0)) step()
|
|
35
|
+
})
|
|
36
|
+
const scratch = (prefix: string) => {
|
|
37
|
+
const dir = mkdtempSync(join(tmpdir(), prefix))
|
|
38
|
+
cleanup.push(() => rmSync(dir, { recursive: true, force: true }))
|
|
39
|
+
return dir
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const INTEGRITY = `sha512-${'A'.repeat(86)}==`
|
|
43
|
+
const entry = (fields: Record<string, unknown> = {}) => ({
|
|
44
|
+
package: '@scope/engine-darwin-arm64',
|
|
45
|
+
version: '1.2.3',
|
|
46
|
+
os: 'darwin',
|
|
47
|
+
cpu: 'arm64',
|
|
48
|
+
integrity: INTEGRITY,
|
|
49
|
+
size: 1000,
|
|
50
|
+
...fields,
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
test('a plugin without the field brings nothing; a valid list is read as it is', () => {
|
|
54
|
+
assert.deepEqual(nativesOf({ name: 'x' }), { natives: [] })
|
|
55
|
+
assert.deepEqual(nativesOf({ acrylblends: {} }), { natives: [] })
|
|
56
|
+
// The second entry has no CPU: a build for any CPU of that OS (a WebAssembly engine), read without the key.
|
|
57
|
+
const { cpu: _anyCpu, ...wasm } = entry({ os: 'linux' })
|
|
58
|
+
assert.deepEqual(nativesOf({ acrylblends: { native: [entry(), wasm] } }), {
|
|
59
|
+
natives: [entry(), wasm],
|
|
60
|
+
})
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
test('a declaration that cannot be trusted is refused with the field that is wrong', () => {
|
|
64
|
+
const error = (native: unknown) => {
|
|
65
|
+
const read = nativesOf({ acrylblends: { native } })
|
|
66
|
+
return 'error' in read ? read.error : ''
|
|
67
|
+
}
|
|
68
|
+
assert.match(error({}), /not a list/u)
|
|
69
|
+
assert.match(error([entry({ package: '../../etc' })]), /\.package/u)
|
|
70
|
+
assert.match(error([entry({ version: '^1.2.3' })]), /exact version/u)
|
|
71
|
+
assert.match(error([entry({ os: 'aix' })]), /\.os/u)
|
|
72
|
+
assert.match(error([entry({ cpu: 'ia32' })]), /\.cpu/u)
|
|
73
|
+
assert.match(error([entry({ integrity: 'sha1-abc' })]), /sha512/u)
|
|
74
|
+
assert.match(error([entry({ size: -1 })]), /\.size/u)
|
|
75
|
+
assert.match(error([entry({ size: 2_000_000_000 })]), /more than a plugin may bring/u)
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
test("this computer's entry: its OS and CPU first, then its OS for any CPU", () => {
|
|
79
|
+
const natives = [
|
|
80
|
+
entry(),
|
|
81
|
+
{ ...entry({ package: '@scope/engine-wasm' }), os: 'linux' as const, cpu: undefined },
|
|
82
|
+
entry({ package: '@scope/engine-win32-x64', os: 'win32', cpu: 'x64' }),
|
|
83
|
+
] as NativePackage[]
|
|
84
|
+
assert.equal(nativeFor(natives, 'darwin', 'arm64')?.package, '@scope/engine-darwin-arm64')
|
|
85
|
+
assert.equal(nativeFor(natives, 'linux', 'arm64')?.package, '@scope/engine-wasm')
|
|
86
|
+
assert.equal(nativeFor(natives, 'win32', 'x64')?.package, '@scope/engine-win32-x64')
|
|
87
|
+
assert.equal(nativeFor(natives, 'darwin', 'x64'), undefined)
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
test("the tarball's address is npm's", () => {
|
|
91
|
+
assert.equal(
|
|
92
|
+
tarballUrl({ package: '@deepseek-ai/libreoffice-kit-darwin-arm64', version: '0.1.5' }),
|
|
93
|
+
'https://registry.npmjs.org/@deepseek-ai/libreoffice-kit-darwin-arm64/-/libreoffice-kit-darwin-arm64-0.1.5.tgz',
|
|
94
|
+
)
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
/** A native package as npm packs it (`package/` at the top), with an executable, and the entry that names it. */
|
|
98
|
+
function engine(name = '@scope/engine-darwin-arm64', version = '1.2.3') {
|
|
99
|
+
const dir = scratch('native-pack-')
|
|
100
|
+
mkdirSync(join(dir, 'package', 'bin'), { recursive: true })
|
|
101
|
+
writeFileSync(join(dir, 'package', 'package.json'), JSON.stringify({ name, version }))
|
|
102
|
+
writeFileSync(join(dir, 'package', 'bin', 'engine'), '#!/bin/sh\necho engine\n', { mode: 0o755 })
|
|
103
|
+
const file = join(dir, 'engine.tgz')
|
|
104
|
+
execFileSync('tar', ['-czf', file, '-C', dir, 'package'])
|
|
105
|
+
const integrity = `sha512-${createHash('sha512').update(readFileSync(file)).digest('base64')}`
|
|
106
|
+
const native: NativePackage = {
|
|
107
|
+
package: name,
|
|
108
|
+
version,
|
|
109
|
+
os: 'darwin',
|
|
110
|
+
cpu: 'arm64',
|
|
111
|
+
integrity,
|
|
112
|
+
size: 100,
|
|
113
|
+
}
|
|
114
|
+
const downloads: string[] = []
|
|
115
|
+
const ports: NativePorts = {
|
|
116
|
+
download: async (url, to) => {
|
|
117
|
+
downloads.push(url)
|
|
118
|
+
copyFileSync(file, to)
|
|
119
|
+
},
|
|
120
|
+
unpack: nativePorts().unpack,
|
|
121
|
+
}
|
|
122
|
+
return { native, ports, downloads }
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const app = () => ({ root: scratch('native-root-'), home: scratch('native-home-') })
|
|
126
|
+
const linkIn = (root: string) => join(root, 'node_modules', '@scope', 'engine-darwin-arm64')
|
|
127
|
+
|
|
128
|
+
test('installs from npm: checked, unpacked with its execute bits into the home, linked where the package resolves it', async () => {
|
|
129
|
+
const { native, ports, downloads } = engine()
|
|
130
|
+
const places = app()
|
|
131
|
+
assert.equal(await ensureNative(native, places, ports), 'downloaded')
|
|
132
|
+
assert.deepEqual(downloads, [tarballUrl(native)])
|
|
133
|
+
const link = linkIn(places.root)
|
|
134
|
+
assert.ok(lstatSync(link).isSymbolicLink())
|
|
135
|
+
assert.equal(JSON.parse(readFileSync(join(link, 'package.json'), 'utf8')).name, native.package)
|
|
136
|
+
if (process.platform !== 'win32') assert.ok(statSync(join(link, 'bin', 'engine')).mode & 0o100)
|
|
137
|
+
assert.ok(existsSync(nativeStore(places.home, native)))
|
|
138
|
+
assert.equal(await ensureNative(native, places, ports), 'unchanged')
|
|
139
|
+
assert.equal(downloads.length, 1)
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
test('an npm install that put the placeholder back is linked again from the home, without a second download', async () => {
|
|
143
|
+
const { native, ports, downloads } = engine()
|
|
144
|
+
const places = app()
|
|
145
|
+
await ensureNative(native, places, ports)
|
|
146
|
+
const link = linkIn(places.root)
|
|
147
|
+
rmSync(link, { recursive: true, force: true })
|
|
148
|
+
mkdirSync(link, { recursive: true })
|
|
149
|
+
writeFileSync(join(link, 'package.json'), JSON.stringify({ name: PLACEHOLDER, version: '1.0.0' }))
|
|
150
|
+
assert.equal(await ensureNative(native, places, ports), 'linked')
|
|
151
|
+
assert.equal(downloads.length, 1)
|
|
152
|
+
assert.ok(lstatSync(link).isSymbolicLink())
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
test('a real copy npm installed is left alone, and so is its removal', async () => {
|
|
156
|
+
const { native, ports, downloads } = engine()
|
|
157
|
+
const places = app()
|
|
158
|
+
const link = linkIn(places.root)
|
|
159
|
+
mkdirSync(link, { recursive: true })
|
|
160
|
+
writeFileSync(
|
|
161
|
+
join(link, 'package.json'),
|
|
162
|
+
JSON.stringify({ name: native.package, version: '1.2.3' }),
|
|
163
|
+
)
|
|
164
|
+
assert.equal(await ensureNative(native, places, ports), 'present')
|
|
165
|
+
assert.deepEqual(downloads, [])
|
|
166
|
+
releaseNative(native, places)
|
|
167
|
+
assert.ok(existsSync(join(link, 'package.json')))
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
test('a download that is not the file the plugin names is refused and leaves nothing behind', async () => {
|
|
171
|
+
const { native, ports } = engine()
|
|
172
|
+
const places = app()
|
|
173
|
+
await assert.rejects(
|
|
174
|
+
ensureNative({ ...native, integrity: INTEGRITY }, places, ports),
|
|
175
|
+
/sha512 differs/u,
|
|
176
|
+
)
|
|
177
|
+
assert.equal(existsSync(linkIn(places.root)), false)
|
|
178
|
+
assert.deepEqual(readdirSync(join(places.home, 'native')), [])
|
|
179
|
+
})
|
|
180
|
+
|
|
181
|
+
test('a tarball holding another package is refused', async () => {
|
|
182
|
+
const { native, ports } = engine('@scope/other', '1.2.3')
|
|
183
|
+
const places = app()
|
|
184
|
+
await assert.rejects(
|
|
185
|
+
ensureNative({ ...native, package: '@scope/engine-darwin-arm64' }, places, ports),
|
|
186
|
+
/holds @scope\/other@1\.2\.3/u,
|
|
187
|
+
)
|
|
188
|
+
})
|
|
189
|
+
|
|
190
|
+
test('removal takes the link and the download; keeping the download keeps only that', async () => {
|
|
191
|
+
const { native, ports } = engine()
|
|
192
|
+
const places = app()
|
|
193
|
+
await ensureNative(native, places, ports)
|
|
194
|
+
releaseNative(native, places, true)
|
|
195
|
+
assert.equal(existsSync(linkIn(places.root)), false)
|
|
196
|
+
assert.ok(existsSync(nativeStore(places.home, native)))
|
|
197
|
+
await ensureNative(native, places, ports)
|
|
198
|
+
releaseNative(native, places)
|
|
199
|
+
assert.equal(existsSync(linkIn(places.root)), false)
|
|
200
|
+
assert.equal(existsSync(nativeStore(places.home, native)), false)
|
|
201
|
+
})
|
|
202
|
+
|
|
203
|
+
test('an update of the native package replaces the link to the older download', async () => {
|
|
204
|
+
const older = engine('@scope/engine-darwin-arm64', '1.2.3')
|
|
205
|
+
const newer = engine('@scope/engine-darwin-arm64', '1.3.0')
|
|
206
|
+
const places = app()
|
|
207
|
+
await ensureNative(older.native, places, older.ports)
|
|
208
|
+
assert.equal(await ensureNative(newer.native, places, newer.ports), 'downloaded')
|
|
209
|
+
assert.equal(
|
|
210
|
+
JSON.parse(readFileSync(join(linkIn(places.root), 'package.json'), 'utf8')).version,
|
|
211
|
+
'1.3.0',
|
|
212
|
+
)
|
|
213
|
+
})
|