@acryldev/plugin-share 0.0.0-stage → 0.1.1
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
|
@@ -0,0 +1,1001 @@
|
|
|
1
|
+
import assert from 'node:assert/strict'
|
|
2
|
+
import { execFileSync } from 'node:child_process'
|
|
3
|
+
import { createHash } from 'node:crypto'
|
|
4
|
+
import {
|
|
5
|
+
existsSync,
|
|
6
|
+
mkdirSync,
|
|
7
|
+
mkdtempSync,
|
|
8
|
+
readdirSync,
|
|
9
|
+
readFileSync,
|
|
10
|
+
rmSync,
|
|
11
|
+
writeFileSync,
|
|
12
|
+
} from 'node:fs'
|
|
13
|
+
import { tmpdir } from 'node:os'
|
|
14
|
+
import { join } from 'node:path'
|
|
15
|
+
import { afterEach, test } from 'node:test'
|
|
16
|
+
import { digestFiles } from '@acryldev/blends-core'
|
|
17
|
+
import { routeSource } from '@acryldev/builder/script'
|
|
18
|
+
import { extensions } from '@acryldev/extensions'
|
|
19
|
+
import { instance, pinnedInstance } from '@acryldev/instance'
|
|
20
|
+
import { captureApp, verifyApp } from '@acryldev/manifest/snapshot'
|
|
21
|
+
import {
|
|
22
|
+
integrityOf,
|
|
23
|
+
type PublishGitPort,
|
|
24
|
+
packFolder,
|
|
25
|
+
type ReleasePort,
|
|
26
|
+
type RepositoryHostPort,
|
|
27
|
+
readTarball,
|
|
28
|
+
} from '@acryldev/plugin-hub-core'
|
|
29
|
+
import * as router from '@acryldev/router'
|
|
30
|
+
import { Context } from '@deepseek-ai/cordis'
|
|
31
|
+
import {
|
|
32
|
+
type PublishOptions,
|
|
33
|
+
type PublishPlan,
|
|
34
|
+
type PublishPorts,
|
|
35
|
+
type PublishRefused,
|
|
36
|
+
type PublishResult,
|
|
37
|
+
type PullListResult,
|
|
38
|
+
type PullResult,
|
|
39
|
+
pluginShare,
|
|
40
|
+
type RestoreOutcome,
|
|
41
|
+
type SharedPluginInfo,
|
|
42
|
+
type SharedRemoveResult,
|
|
43
|
+
} from '../index.ts'
|
|
44
|
+
import { type NativePorts, nativePorts, nativeStore } from '../native.ts'
|
|
45
|
+
import { splitDigest } from '../pull-check.ts'
|
|
46
|
+
import { parsePullList, pullListLocation } from '../pull-list.ts'
|
|
47
|
+
import { pulledRecords } from '../pulled-records.ts'
|
|
48
|
+
|
|
49
|
+
const cleanup: Array<() => Promise<void> | void> = []
|
|
50
|
+
afterEach(async () => {
|
|
51
|
+
for (const step of cleanup.splice(0)) await step()
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
const scratch = (prefix: string): string => {
|
|
55
|
+
const dir = mkdtempSync(join(tmpdir(), prefix))
|
|
56
|
+
cleanup.push(() => rmSync(dir, { recursive: true, force: true }))
|
|
57
|
+
return dir
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const ROUTE = routeSource('hello', '/hello', 'Hello')
|
|
61
|
+
|
|
62
|
+
/** A plugin as a person released it: the folder packed by the hub, the same way `package_plugin` does. */
|
|
63
|
+
function pack(files: Record<string, string>, manifest: Record<string, unknown> = {}) {
|
|
64
|
+
const dir = scratch('share-source-')
|
|
65
|
+
const all = {
|
|
66
|
+
'package.json': `${JSON.stringify({ name: 'hello', version: '1.0.0', type: 'module', keywords: ['cordis-plugin', 'acrylblends'], ...manifest }, null, 2)}\n`,
|
|
67
|
+
...files,
|
|
68
|
+
}
|
|
69
|
+
for (const [path, content] of Object.entries(all)) {
|
|
70
|
+
mkdirSync(join(dir, path, '..'), { recursive: true })
|
|
71
|
+
writeFileSync(join(dir, path), content)
|
|
72
|
+
}
|
|
73
|
+
const packed = packFolder(dir)
|
|
74
|
+
const file = join(scratch('share-release-'), packed.fileName)
|
|
75
|
+
writeFileSync(file, packed.tarball)
|
|
76
|
+
const digest = digestFiles(
|
|
77
|
+
readTarball(packed.tarball).map(({ path, content }) => ({
|
|
78
|
+
path,
|
|
79
|
+
bytes: content,
|
|
80
|
+
kind: 'file' as const,
|
|
81
|
+
})),
|
|
82
|
+
)
|
|
83
|
+
return { file, tarball: packed.tarball, integrity: packed.integrity, digest }
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** One release on a pretend GitHub, as the hub's port reads it. */
|
|
87
|
+
const releaseOf = (tarball: Uint8Array, tag = 'v1.0.0'): ReleasePort => ({
|
|
88
|
+
release: async () => ({
|
|
89
|
+
tag,
|
|
90
|
+
assets: [{ name: `hello-${tag.slice(1)}.tgz`, url: 'fixture:hello' }],
|
|
91
|
+
}),
|
|
92
|
+
download: async () => tarball,
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
async function boot({
|
|
96
|
+
root = scratch('share-root-'),
|
|
97
|
+
...config
|
|
98
|
+
}: {
|
|
99
|
+
releases?: ReleasePort
|
|
100
|
+
publish?: PublishPorts
|
|
101
|
+
download?: (url: string) => Promise<Uint8Array>
|
|
102
|
+
native?: NativePorts
|
|
103
|
+
root?: string
|
|
104
|
+
} = {}) {
|
|
105
|
+
const app = new Context()
|
|
106
|
+
const fibers = [
|
|
107
|
+
app.plugin(instance, pinnedInstance(join(root, '.acrylblends'), { root })),
|
|
108
|
+
app.plugin(router),
|
|
109
|
+
app.plugin(extensions, { sources: [{ dir: join(root, 'plugins'), trust: 'install' }] }),
|
|
110
|
+
app.plugin(pluginShare, config),
|
|
111
|
+
]
|
|
112
|
+
for (const fiber of fibers) await fiber.await()
|
|
113
|
+
const stop = async () => {
|
|
114
|
+
for (const fiber of [...fibers].reverse()) await fiber.dispose()
|
|
115
|
+
}
|
|
116
|
+
cleanup.push(stop)
|
|
117
|
+
const share = (
|
|
118
|
+
app as unknown as {
|
|
119
|
+
pluginShare: {
|
|
120
|
+
pull(source: string): Promise<PullResult>
|
|
121
|
+
pullList(from: string): Promise<PullListResult>
|
|
122
|
+
list(): SharedPluginInfo[]
|
|
123
|
+
pulledDigest(name: string): string | undefined
|
|
124
|
+
remove(name: string): Promise<SharedRemoveResult>
|
|
125
|
+
planPublish(name: string, options?: PublishOptions): Promise<PublishPlan | PublishRefused>
|
|
126
|
+
publish(
|
|
127
|
+
name: string,
|
|
128
|
+
confirmed: PublishPlan,
|
|
129
|
+
options?: PublishOptions,
|
|
130
|
+
): Promise<PublishResult>
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
).pluginShare
|
|
134
|
+
const get = async (path: string) => {
|
|
135
|
+
const response = await (
|
|
136
|
+
app as unknown as { router: { request(path: string): Promise<Response> } }
|
|
137
|
+
).router.request(path)
|
|
138
|
+
return {
|
|
139
|
+
status: response.status,
|
|
140
|
+
body: response.status === 200 ? await response.json() : undefined,
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
const installed = () =>
|
|
144
|
+
(app as unknown as { extensions: { list(): readonly unknown[] } }).extensions.list()
|
|
145
|
+
/** What a refusal must leave as it was: nothing in plugins/, nothing running, no scratch left in the home. */
|
|
146
|
+
const untouched = async () => {
|
|
147
|
+
assert.deepEqual(
|
|
148
|
+
existsSync(join(root, 'plugins')) ? readdirSync(join(root, 'plugins')) : [],
|
|
149
|
+
[],
|
|
150
|
+
)
|
|
151
|
+
assert.deepEqual(installed(), [])
|
|
152
|
+
assert.equal((await get('/hello')).status, 404)
|
|
153
|
+
assert.deepEqual(
|
|
154
|
+
readdirSync(join(root, '.acrylblends')).filter((entry) => entry.startsWith('plugin-pull')),
|
|
155
|
+
[],
|
|
156
|
+
)
|
|
157
|
+
}
|
|
158
|
+
const restored = () =>
|
|
159
|
+
(app as unknown as { pluginShare: { restored: Promise<readonly RestoreOutcome[]> } })
|
|
160
|
+
.pluginShare.restored
|
|
161
|
+
return { app, fibers, root, share, get, installed, untouched, stop, restored }
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const refused = (result: PullResult, refusal: string, reason: RegExp) => {
|
|
165
|
+
assert.equal(result.ok, false, JSON.stringify(result))
|
|
166
|
+
if (result.ok) return
|
|
167
|
+
assert.equal(result.refusal, refusal, JSON.stringify(result))
|
|
168
|
+
assert.match(result.reason, reason)
|
|
169
|
+
assert.ok(result.next.length > 0, 'a refusal says what to do next')
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
test('a released plugin is pulled into the running app and its route answers without a restart', async () => {
|
|
173
|
+
const release = pack({ 'index.ts': ROUTE })
|
|
174
|
+
const { share, get, root } = await boot({ releases: releaseOf(release.tarball) })
|
|
175
|
+
assert.equal((await get('/hello')).status, 404)
|
|
176
|
+
const result = await share.pull('github:someone/hello@1.0.0')
|
|
177
|
+
assert.ok(result.ok, JSON.stringify(result))
|
|
178
|
+
assert.deepEqual(
|
|
179
|
+
[result.name, result.version, result.integrity, result.digest, result.action, result.state],
|
|
180
|
+
['hello', '1.0.0', release.integrity, release.digest, 'installed', 'active'],
|
|
181
|
+
)
|
|
182
|
+
assert.deepEqual((await get('/hello')).body, { text: 'Hello' })
|
|
183
|
+
assert.ok(existsSync(join(root, 'plugins', 'hello', 'index.ts')))
|
|
184
|
+
})
|
|
185
|
+
|
|
186
|
+
test('a .tgz path is a source, read from the app folder when relative', async () => {
|
|
187
|
+
const release = pack({ 'index.ts': ROUTE })
|
|
188
|
+
const { share, get, root } = await boot()
|
|
189
|
+
writeFileSync(join(root, 'hello-1.0.0.tgz'), release.tarball)
|
|
190
|
+
const result = await share.pull('hello-1.0.0.tgz')
|
|
191
|
+
assert.ok(result.ok, JSON.stringify(result))
|
|
192
|
+
assert.deepEqual((await get('/hello')).body, { text: 'Hello' })
|
|
193
|
+
})
|
|
194
|
+
|
|
195
|
+
test('the digest the source names must be the digest of what arrived', async () => {
|
|
196
|
+
const release = pack({ 'index.ts': ROUTE })
|
|
197
|
+
const good = await boot({ releases: releaseOf(release.tarball) })
|
|
198
|
+
assert.ok((await good.share.pull(`github:someone/hello@1.0.0@sha256:${release.digest}`)).ok)
|
|
199
|
+
const bad = await boot({ releases: releaseOf(release.tarball) })
|
|
200
|
+
refused(
|
|
201
|
+
await bad.share.pull(`github:someone/hello@1.0.0@sha256:${'0'.repeat(64)}`),
|
|
202
|
+
'digest',
|
|
203
|
+
/is not the digest the source names/,
|
|
204
|
+
)
|
|
205
|
+
await bad.untouched()
|
|
206
|
+
})
|
|
207
|
+
|
|
208
|
+
test('a tampered archive is refused and changes nothing', async () => {
|
|
209
|
+
const release = pack({ 'index.ts': ROUTE })
|
|
210
|
+
const tampered = Buffer.from(release.tarball)
|
|
211
|
+
tampered[tampered.length - 20] = (tampered[tampered.length - 20] ?? 0) ^ 0xff
|
|
212
|
+
const { share, untouched } = await boot({ releases: releaseOf(tampered) })
|
|
213
|
+
refused(await share.pull('github:someone/hello@1.0.0'), 'fetch', /not a plugin tarball/)
|
|
214
|
+
await untouched()
|
|
215
|
+
})
|
|
216
|
+
|
|
217
|
+
test('a plugin with a carriage return is refused with its reason and changes nothing', async () => {
|
|
218
|
+
const release = pack({ 'index.ts': ROUTE.replaceAll('\n', '\r\n') })
|
|
219
|
+
const { share, untouched } = await boot({ releases: releaseOf(release.tarball) })
|
|
220
|
+
refused(
|
|
221
|
+
await share.pull('github:someone/hello@1.0.0'),
|
|
222
|
+
'portability',
|
|
223
|
+
/index\.ts.*carriage return/,
|
|
224
|
+
)
|
|
225
|
+
await untouched()
|
|
226
|
+
})
|
|
227
|
+
|
|
228
|
+
test('a plugin with a name Windows reserves is refused with its reason and changes nothing', async () => {
|
|
229
|
+
const release = pack({ 'index.ts': ROUTE, 'aux.txt': 'x\n' })
|
|
230
|
+
const { share, untouched } = await boot({ releases: releaseOf(release.tarball) })
|
|
231
|
+
refused(
|
|
232
|
+
await share.pull('github:someone/hello@1.0.0'),
|
|
233
|
+
'portability',
|
|
234
|
+
/aux\.txt.*reserved by Windows/,
|
|
235
|
+
)
|
|
236
|
+
await untouched()
|
|
237
|
+
})
|
|
238
|
+
|
|
239
|
+
test('a plugin needing a service this app lacks is refused by name, before any of its code runs', async () => {
|
|
240
|
+
const marker = join(scratch('share-marker-'), 'ran')
|
|
241
|
+
const source = `import { writeFileSync } from 'node:fs'\nwriteFileSync(${JSON.stringify(marker)}, 'x')\nexport const name = 'hello'\nexport const inject = ['router', 'noSuchService']\nexport function apply() {}\n`
|
|
242
|
+
const release = pack({ 'index.ts': source })
|
|
243
|
+
const { share, untouched } = await boot({ releases: releaseOf(release.tarball) })
|
|
244
|
+
refused(await share.pull('github:someone/hello@1.0.0'), 'missing-service', /noSuchService/)
|
|
245
|
+
assert.equal(existsSync(marker), false, 'the plugin ran')
|
|
246
|
+
await untouched()
|
|
247
|
+
})
|
|
248
|
+
|
|
249
|
+
test('a plugin that stays pending after a computed inject is taken back out', async () => {
|
|
250
|
+
const source = `const wanted = ['no', 'Such', 'Service'].join('')\nexport const name = 'hello'\nexport { inject }\nconst inject = [wanted]\nexport function apply() {}\n`
|
|
251
|
+
const release = pack({ 'index.ts': source })
|
|
252
|
+
const { share, untouched } = await boot({ releases: releaseOf(release.tarball) })
|
|
253
|
+
refused(await share.pull('github:someone/hello@1.0.0'), 'missing-service', /noSuchService/)
|
|
254
|
+
await untouched()
|
|
255
|
+
})
|
|
256
|
+
|
|
257
|
+
test('a plugin that fails to start is refused and leaves no folder', async () => {
|
|
258
|
+
const release = pack({
|
|
259
|
+
'index.ts': `export const name = 'hello'\nexport function apply() { throw new Error('refuses to start') }\n`,
|
|
260
|
+
})
|
|
261
|
+
const { share, untouched } = await boot({ releases: releaseOf(release.tarball) })
|
|
262
|
+
refused(await share.pull('github:someone/hello@1.0.0'), 'install', /refuses to start/)
|
|
263
|
+
await untouched()
|
|
264
|
+
})
|
|
265
|
+
|
|
266
|
+
test('an inject that cannot be read without running the plugin is refused', async () => {
|
|
267
|
+
const release = pack({
|
|
268
|
+
'index.ts': `const wanted = ['router']\nexport const name = 'hello'\nexport const inject = wanted\nexport function apply() {}\n`,
|
|
269
|
+
})
|
|
270
|
+
const { share, untouched } = await boot({ releases: releaseOf(release.tarball) })
|
|
271
|
+
refused(await share.pull('github:someone/hello@1.0.0'), 'inject', /without running it/)
|
|
272
|
+
await untouched()
|
|
273
|
+
})
|
|
274
|
+
|
|
275
|
+
test('dependencies are refused: nothing is installed for a pulled plugin', async () => {
|
|
276
|
+
const release = pack({ 'index.ts': ROUTE }, { dependencies: { 'left-pad': '1.3.0' } })
|
|
277
|
+
const { share, untouched } = await boot({ releases: releaseOf(release.tarball) })
|
|
278
|
+
refused(await share.pull('github:someone/hello@1.0.0'), 'dependencies', /left-pad/)
|
|
279
|
+
await untouched()
|
|
280
|
+
})
|
|
281
|
+
|
|
282
|
+
test('a package with no entry file or a name no folder can hold is refused', async () => {
|
|
283
|
+
const noEntry = await boot({ releases: releaseOf(pack({ 'lib.ts': 'export {}\n' }).tarball) })
|
|
284
|
+
refused(await noEntry.share.pull('github:someone/hello@1.0.0'), 'entry', /index\.ts/)
|
|
285
|
+
await noEntry.untouched()
|
|
286
|
+
const scoped = await boot({
|
|
287
|
+
releases: releaseOf(pack({ 'index.ts': ROUTE }, { name: '@someone/hello' }).tarball),
|
|
288
|
+
})
|
|
289
|
+
refused(await scoped.share.pull('github:someone/hello@1.0.0'), 'name', /@someone\/hello/)
|
|
290
|
+
await scoped.untouched()
|
|
291
|
+
})
|
|
292
|
+
|
|
293
|
+
test('sources other than a release or a .tgz are refused by name', async () => {
|
|
294
|
+
const { share, untouched } = await boot()
|
|
295
|
+
refused(await share.pull('npm:hello'), 'source', /npm/)
|
|
296
|
+
refused(await share.pull('store:hello'), 'source', /store/)
|
|
297
|
+
refused(await share.pull('hello'), 'source', /github:/)
|
|
298
|
+
refused(await share.pull('git+https://github.com/someone/hello.git'), 'source', /no release/)
|
|
299
|
+
refused(await share.pull('github:someone/hello@1.0.0@sha256:short'), 'source', /64/)
|
|
300
|
+
await untouched()
|
|
301
|
+
})
|
|
302
|
+
|
|
303
|
+
test('a release that does not exist is refused', async () => {
|
|
304
|
+
const { share, untouched } = await boot({
|
|
305
|
+
releases: { release: async () => undefined, download: async () => new Uint8Array() },
|
|
306
|
+
})
|
|
307
|
+
refused(await share.pull('github:someone/hello@9.9.9'), 'fetch', /no release v9\.9\.9/)
|
|
308
|
+
await untouched()
|
|
309
|
+
})
|
|
310
|
+
|
|
311
|
+
test('pulling the same bytes twice is already there; different bytes never overwrite the folder', async () => {
|
|
312
|
+
const first = pack({ 'index.ts': ROUTE })
|
|
313
|
+
const { share, get, root } = await boot({ releases: releaseOf(first.tarball) })
|
|
314
|
+
assert.ok((await share.pull('github:someone/hello@1.0.0')).ok)
|
|
315
|
+
const again = await share.pull('github:someone/hello@1.0.0')
|
|
316
|
+
assert.ok(again.ok, JSON.stringify(again))
|
|
317
|
+
assert.equal(again.action, 'unchanged')
|
|
318
|
+
|
|
319
|
+
writeFileSync(join(root, 'plugins', 'hello', 'mine.md'), 'an edit a person made\n')
|
|
320
|
+
refused(await share.pull('github:someone/hello@1.0.0'), 'folder-exists', /plugins\/hello/)
|
|
321
|
+
assert.ok(existsSync(join(root, 'plugins', 'hello', 'mine.md')))
|
|
322
|
+
assert.deepEqual((await get('/hello')).body, { text: 'Hello' })
|
|
323
|
+
})
|
|
324
|
+
|
|
325
|
+
test('the digest is split off the end of a source', () => {
|
|
326
|
+
const digest = 'a'.repeat(64)
|
|
327
|
+
assert.deepEqual(splitDigest(`github:o/r@1.0.0@sha256:${digest}`), {
|
|
328
|
+
source: 'github:o/r@1.0.0',
|
|
329
|
+
digest,
|
|
330
|
+
})
|
|
331
|
+
assert.deepEqual(splitDigest('github:o/r'), { source: 'github:o/r' })
|
|
332
|
+
assert.throws(() => splitDigest('github:o/r@sha256:ABC'), /64/)
|
|
333
|
+
})
|
|
334
|
+
|
|
335
|
+
test('the service leaves with its fiber', async () => {
|
|
336
|
+
const { app, fibers } = await boot()
|
|
337
|
+
assert.ok(app.get('pluginShare'))
|
|
338
|
+
await fibers.at(-1)?.dispose()
|
|
339
|
+
assert.equal(app.get('pluginShare'), undefined)
|
|
340
|
+
})
|
|
341
|
+
|
|
342
|
+
// Publishing. The host and git are stand-ins: no test here can reach GitHub.
|
|
343
|
+
function fakeHost(login = 'someone') {
|
|
344
|
+
const sent: string[] = []
|
|
345
|
+
const repos = new Set<string>()
|
|
346
|
+
const releases = new Map<string, Buffer>()
|
|
347
|
+
const host: RepositoryHostPort = {
|
|
348
|
+
login: () => login,
|
|
349
|
+
repository: (owner, repo) =>
|
|
350
|
+
repos.has(`${owner}/${repo}`)
|
|
351
|
+
? { visibility: 'public', url: `https://github.com/${owner}/${repo}` }
|
|
352
|
+
: undefined,
|
|
353
|
+
createRepository(owner, repo, visibility) {
|
|
354
|
+
sent.push(`create ${owner}/${repo} ${visibility}`)
|
|
355
|
+
repos.add(`${owner}/${repo}`)
|
|
356
|
+
},
|
|
357
|
+
setVisibility: () => {},
|
|
358
|
+
addTopics(owner, repo, topics) {
|
|
359
|
+
sent.push(`topics ${owner}/${repo} ${topics.join(',')}`)
|
|
360
|
+
},
|
|
361
|
+
releaseExists: (owner, repo, tag) => releases.has(`${owner}/${repo}@${tag}`),
|
|
362
|
+
createRelease(owner, repo, tag, _title, _notes, asset) {
|
|
363
|
+
sent.push(`release ${owner}/${repo} ${tag}`)
|
|
364
|
+
releases.set(`${owner}/${repo}@${tag}`, readFileSync(asset))
|
|
365
|
+
return `https://github.com/${owner}/${repo}/releases/tag/${tag}`
|
|
366
|
+
},
|
|
367
|
+
cloneUrl: (owner, repo) => `https://github.com/${owner}/${repo}.git`,
|
|
368
|
+
}
|
|
369
|
+
const git: PublishGitPort = {
|
|
370
|
+
clone: (_url, dir) => mkdirSync(dir, { recursive: true }),
|
|
371
|
+
commitAll: () => true,
|
|
372
|
+
tag: () => {},
|
|
373
|
+
push: () => sent.push('push'),
|
|
374
|
+
}
|
|
375
|
+
return { host, git, sent, releases }
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
async function bootWithPlugin(hub: ReturnType<typeof fakeHost>) {
|
|
379
|
+
const booted = await boot({ publish: { host: hub.host, git: hub.git } })
|
|
380
|
+
mkdirSync(join(booted.root, 'plugins', 'hello'), { recursive: true })
|
|
381
|
+
writeFileSync(join(booted.root, 'plugins', 'hello', 'index.ts'), ROUTE)
|
|
382
|
+
return booted
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
const refusedPublish = (answer: PublishPlan | PublishRefused, reason: RegExp) => {
|
|
386
|
+
assert.ok('ok' in answer && answer.ok === false, JSON.stringify(answer))
|
|
387
|
+
assert.match(answer.reason, reason)
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
const planned = (plan: PublishPlan | PublishRefused): PublishPlan => {
|
|
391
|
+
assert.ok('owner' in plan, JSON.stringify(plan))
|
|
392
|
+
return plan
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
test('a plan names the account, repository and version and sends nothing', async () => {
|
|
396
|
+
const hub = fakeHost()
|
|
397
|
+
const { share } = await bootWithPlugin(hub)
|
|
398
|
+
const plan = planned(await share.planPublish('hello'))
|
|
399
|
+
assert.deepEqual(
|
|
400
|
+
[plan.owner, plan.repo, plan.version, plan.visibility, plan.license, plan.createsRepository],
|
|
401
|
+
['someone', 'hello', '0.1.0', 'public', 'MIT', true],
|
|
402
|
+
)
|
|
403
|
+
assert.deepEqual(hub.sent, [])
|
|
404
|
+
})
|
|
405
|
+
|
|
406
|
+
test('the person can choose the license before the yes, and it is what is released', async () => {
|
|
407
|
+
const hub = fakeHost()
|
|
408
|
+
const { share } = await bootWithPlugin(hub)
|
|
409
|
+
refusedPublish(await share.planPublish('hello', { license: 'not a license' }), /not a license id/)
|
|
410
|
+
const plan = planned(await share.planPublish('hello', { license: 'Apache-2.0' }))
|
|
411
|
+
assert.equal(plan.license, 'Apache-2.0')
|
|
412
|
+
const result = await share.publish('hello', plan, { license: 'Apache-2.0' })
|
|
413
|
+
assert.ok(result.ok, JSON.stringify(result))
|
|
414
|
+
const bytes = hub.releases.get('someone/hello@v0.1.0')
|
|
415
|
+
assert.ok(bytes)
|
|
416
|
+
const manifest = readTarball(bytes).find((file) => file.path === 'package.json')
|
|
417
|
+
assert.equal(
|
|
418
|
+
JSON.parse(Buffer.from(manifest?.content ?? []).toString('utf8')).license,
|
|
419
|
+
'Apache-2.0',
|
|
420
|
+
)
|
|
421
|
+
})
|
|
422
|
+
|
|
423
|
+
test('publishing exactly the confirmed plan releases those bytes, and the pull line carries their digest', async () => {
|
|
424
|
+
const hub = fakeHost()
|
|
425
|
+
const { share } = await bootWithPlugin(hub)
|
|
426
|
+
const plan = planned(await share.planPublish('hello', { owner: 'inboxxobni' }))
|
|
427
|
+
const result = await share.publish('hello', plan, { owner: 'inboxxobni' })
|
|
428
|
+
assert.ok(result.ok, JSON.stringify(result))
|
|
429
|
+
assert.deepEqual(hub.sent, [
|
|
430
|
+
'create inboxxobni/hello public',
|
|
431
|
+
'push',
|
|
432
|
+
'release inboxxobni/hello v0.1.0',
|
|
433
|
+
'topics inboxxobni/hello cordis-plugin',
|
|
434
|
+
])
|
|
435
|
+
const bytes = hub.releases.get('inboxxobni/hello@v0.1.0')
|
|
436
|
+
assert.ok(bytes)
|
|
437
|
+
assert.equal(result.integrity, plan.integrity)
|
|
438
|
+
assert.equal(integrityOf(bytes), plan.integrity, 'the released bytes are the planned bytes')
|
|
439
|
+
assert.equal(
|
|
440
|
+
result.pullLine,
|
|
441
|
+
`npm run acryl -- plugin pull github:inboxxobni/hello@0.1.0@sha256:${plan.digest}`,
|
|
442
|
+
)
|
|
443
|
+
})
|
|
444
|
+
|
|
445
|
+
test('a plan confirmed through JSON in another field order publishes; one with another digest sends nothing', async () => {
|
|
446
|
+
const hub = fakeHost()
|
|
447
|
+
const { share } = await bootWithPlugin(hub)
|
|
448
|
+
const plan = planned(await share.planPublish('hello'))
|
|
449
|
+
// As the frame's confirm sends it back: parsed from JSON, fields in whatever order that surface wrote them.
|
|
450
|
+
const reordered = JSON.parse(
|
|
451
|
+
JSON.stringify(Object.fromEntries(Object.entries(plan).reverse())),
|
|
452
|
+
) as typeof plan
|
|
453
|
+
const changed = { ...reordered, digest: '0'.repeat(64) }
|
|
454
|
+
const refused = await share.publish('hello', changed)
|
|
455
|
+
assert.equal(refused.ok, false)
|
|
456
|
+
assert.deepEqual(hub.sent, [], 'a plan that is not the one shown sends nothing')
|
|
457
|
+
const result = await share.publish('hello', reordered)
|
|
458
|
+
assert.ok(result.ok, JSON.stringify(result))
|
|
459
|
+
})
|
|
460
|
+
|
|
461
|
+
test("what another person pulls is the plan's digest", async () => {
|
|
462
|
+
const hub = fakeHost()
|
|
463
|
+
const { share } = await bootWithPlugin(hub)
|
|
464
|
+
const plan = planned(await share.planPublish('hello'))
|
|
465
|
+
assert.ok((await share.publish('hello', plan)).ok)
|
|
466
|
+
const bytes = hub.releases.get('someone/hello@v0.1.0')
|
|
467
|
+
assert.ok(bytes)
|
|
468
|
+
const other = await boot({
|
|
469
|
+
releases: {
|
|
470
|
+
release: async () => ({
|
|
471
|
+
tag: 'v0.1.0',
|
|
472
|
+
assets: [{ name: 'hello-0.1.0.tgz', url: 'fixture:hello' }],
|
|
473
|
+
}),
|
|
474
|
+
download: async () => bytes,
|
|
475
|
+
},
|
|
476
|
+
})
|
|
477
|
+
const pulled = await other.share.pull(`github:someone/hello@0.1.0@sha256:${plan.digest}`)
|
|
478
|
+
assert.ok(pulled.ok, JSON.stringify(pulled))
|
|
479
|
+
assert.deepEqual((await other.get('/hello')).body, { text: 'Hello' })
|
|
480
|
+
})
|
|
481
|
+
|
|
482
|
+
test('nothing is sent when the plugin, the account or the version changed since the person said yes', async () => {
|
|
483
|
+
const hub = fakeHost()
|
|
484
|
+
const { share, root } = await bootWithPlugin(hub)
|
|
485
|
+
const plan = planned(await share.planPublish('hello'))
|
|
486
|
+
writeFileSync(join(root, 'plugins', 'hello', 'note.md'), 'one more file\n')
|
|
487
|
+
const edited = await share.publish('hello', plan)
|
|
488
|
+
assert.equal(edited.ok, false)
|
|
489
|
+
assert.match(edited.ok ? '' : edited.reason, /changed since you confirmed/)
|
|
490
|
+
const elsewhere = planned(await share.planPublish('hello'))
|
|
491
|
+
const other = await share.publish('hello', { ...elsewhere, owner: 'someone-else' })
|
|
492
|
+
assert.equal(other.ok, false)
|
|
493
|
+
assert.deepEqual(hub.sent, [])
|
|
494
|
+
})
|
|
495
|
+
|
|
496
|
+
test('a released version is not released again: the plan names the next patch', async () => {
|
|
497
|
+
const hub = fakeHost()
|
|
498
|
+
const { share, root } = await bootWithPlugin(hub)
|
|
499
|
+
assert.ok((await share.publish('hello', planned(await share.planPublish('hello')))).ok)
|
|
500
|
+
writeFileSync(join(root, 'plugins', 'hello', 'note.md'), 'a change\n')
|
|
501
|
+
const next = planned(await share.planPublish('hello'))
|
|
502
|
+
assert.deepEqual([next.version, next.released, next.createsRepository], ['0.1.1', '0.1.0', false])
|
|
503
|
+
const result = await share.publish('hello', next)
|
|
504
|
+
assert.ok(result.ok, JSON.stringify(result))
|
|
505
|
+
assert.ok(hub.releases.has('someone/hello@v0.1.1'))
|
|
506
|
+
})
|
|
507
|
+
|
|
508
|
+
test('a plugin holding a secret, or not on disk, is refused and nothing is sent', async () => {
|
|
509
|
+
const hub = fakeHost()
|
|
510
|
+
const { share, root } = await bootWithPlugin(hub)
|
|
511
|
+
refusedPublish(await share.planPublish('ghost'), /no plugin "ghost"/)
|
|
512
|
+
writeFileSync(
|
|
513
|
+
join(root, 'plugins', 'hello', 'config.ts'),
|
|
514
|
+
`export const key = 'ghp_${'a'.repeat(36)}'\n`,
|
|
515
|
+
)
|
|
516
|
+
const plan = await share.planPublish('hello')
|
|
517
|
+
refusedPublish(plan, /secret/)
|
|
518
|
+
assert.deepEqual(hub.sent, [])
|
|
519
|
+
})
|
|
520
|
+
|
|
521
|
+
// Remembering a pull: the source and digests stand in the app's own definition, so a copy of the app can fetch the same bytes.
|
|
522
|
+
const BLEND = `# What this app is.
|
|
523
|
+
apiVersion: blends.acryl.dev/v1alpha1
|
|
524
|
+
kind: Blueprint
|
|
525
|
+
metadata: { id: acme.board, name: board, version: 0.1.0 }
|
|
526
|
+
spec:
|
|
527
|
+
runtime: cordis
|
|
528
|
+
rows:
|
|
529
|
+
- id: router # the routes every plugin mounts on
|
|
530
|
+
name: '@acryldev/router'
|
|
531
|
+
- id: extensions
|
|
532
|
+
name: extensions
|
|
533
|
+
- id: plugin-share
|
|
534
|
+
name: plugin-share
|
|
535
|
+
`
|
|
536
|
+
|
|
537
|
+
function appFolder(): string {
|
|
538
|
+
const root = scratch('share-app-')
|
|
539
|
+
writeFileSync(join(root, 'blend.yaml'), BLEND)
|
|
540
|
+
return root
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
const recordsIn = (root: string) => pulledRecords(readFileSync(join(root, 'blend.yaml'), 'utf8'))
|
|
544
|
+
|
|
545
|
+
test('a pull is written into blend.yaml and the lock, the comments stay, and removing the plugin removes the record', async () => {
|
|
546
|
+
const release = pack({ 'index.ts': ROUTE })
|
|
547
|
+
const root = appFolder()
|
|
548
|
+
captureApp(root)
|
|
549
|
+
const { app, share } = await boot({ releases: releaseOf(release.tarball), root })
|
|
550
|
+
const result = await share.pull('github:someone/hello')
|
|
551
|
+
assert.ok(result.ok, JSON.stringify(result))
|
|
552
|
+
const record = {
|
|
553
|
+
source: 'github:someone/hello@1.0.0',
|
|
554
|
+
integrity: release.integrity,
|
|
555
|
+
digest: release.digest,
|
|
556
|
+
}
|
|
557
|
+
assert.deepEqual(recordsIn(root), { hello: record })
|
|
558
|
+
const text = readFileSync(join(root, 'blend.yaml'), 'utf8')
|
|
559
|
+
assert.match(text, /# the routes every plugin mounts on/)
|
|
560
|
+
const lock = JSON.parse(readFileSync(join(root, 'blend.lock.json'), 'utf8'))
|
|
561
|
+
assert.deepEqual(
|
|
562
|
+
lock.rows.find((row: { id: string }) => row.id === 'plugin-share').config.pulled.hello,
|
|
563
|
+
record,
|
|
564
|
+
)
|
|
565
|
+
assert.equal(verifyApp(root).ok, true, verifyApp(root).problems.join('; '))
|
|
566
|
+
|
|
567
|
+
await (
|
|
568
|
+
app as unknown as { extensions: { remove(name: string): Promise<unknown> } }
|
|
569
|
+
).extensions.remove('hello')
|
|
570
|
+
assert.deepEqual(recordsIn(root), {})
|
|
571
|
+
assert.equal(
|
|
572
|
+
verifyApp(join(root)).problems.filter((problem) => problem.includes('blend.yaml')).length,
|
|
573
|
+
0,
|
|
574
|
+
)
|
|
575
|
+
})
|
|
576
|
+
|
|
577
|
+
test('a local .tgz has no source another machine could fetch, so nothing is recorded, and the answer says so', async () => {
|
|
578
|
+
const release = pack({ 'index.ts': ROUTE })
|
|
579
|
+
const root = appFolder()
|
|
580
|
+
const { share } = await boot({ root })
|
|
581
|
+
writeFileSync(join(root, 'hello-1.0.0.tgz'), release.tarball)
|
|
582
|
+
const result = await share.pull('hello-1.0.0.tgz')
|
|
583
|
+
assert.ok(result.ok, JSON.stringify(result))
|
|
584
|
+
assert.equal(result.remembered, false)
|
|
585
|
+
assert.deepEqual(recordsIn(root), {})
|
|
586
|
+
})
|
|
587
|
+
|
|
588
|
+
test('a refused pull records nothing', async () => {
|
|
589
|
+
const release = pack({
|
|
590
|
+
'index.ts': `export const name = 'hello'\nexport function apply() { throw new Error('refuses to start') }\n`,
|
|
591
|
+
})
|
|
592
|
+
const root = appFolder()
|
|
593
|
+
const { share } = await boot({ releases: releaseOf(release.tarball), root })
|
|
594
|
+
assert.equal((await share.pull('github:someone/hello@1.0.0')).ok, false)
|
|
595
|
+
assert.deepEqual(recordsIn(root), {})
|
|
596
|
+
assert.equal(readFileSync(join(root, 'blend.yaml'), 'utf8'), BLEND)
|
|
597
|
+
})
|
|
598
|
+
|
|
599
|
+
test('a copy of the app without the folder fetches the recorded plugin, verified, and runs it', async () => {
|
|
600
|
+
const release = pack({ 'index.ts': ROUTE })
|
|
601
|
+
const root = appFolder()
|
|
602
|
+
const first = await boot({ releases: releaseOf(release.tarball), root })
|
|
603
|
+
assert.ok((await first.share.pull('github:someone/hello@1.0.0')).ok)
|
|
604
|
+
await first.stop()
|
|
605
|
+
rmSync(join(root, 'plugins', 'hello'), { recursive: true })
|
|
606
|
+
|
|
607
|
+
const copy = await boot({ releases: releaseOf(release.tarball), root })
|
|
608
|
+
assert.deepEqual(await copy.restored(), [{ name: 'hello', ok: true }])
|
|
609
|
+
assert.deepEqual((await copy.get('/hello')).body, { text: 'Hello' })
|
|
610
|
+
})
|
|
611
|
+
|
|
612
|
+
test('bytes changed since the record are refused, not silently replaced, and the record stays', async () => {
|
|
613
|
+
const release = pack({ 'index.ts': ROUTE })
|
|
614
|
+
const root = appFolder()
|
|
615
|
+
const first = await boot({ releases: releaseOf(release.tarball), root })
|
|
616
|
+
assert.ok((await first.share.pull('github:someone/hello@1.0.0')).ok)
|
|
617
|
+
await first.stop()
|
|
618
|
+
rmSync(join(root, 'plugins', 'hello'), { recursive: true })
|
|
619
|
+
|
|
620
|
+
const replaced = pack({ 'index.ts': `${ROUTE}// the same version, other bytes\n` })
|
|
621
|
+
const copy = await boot({ releases: releaseOf(replaced.tarball), root })
|
|
622
|
+
const [outcome] = await copy.restored()
|
|
623
|
+
assert.equal(outcome?.ok, false)
|
|
624
|
+
assert.match(outcome?.reason ?? '', /checksum/)
|
|
625
|
+
assert.equal(existsSync(join(root, 'plugins', 'hello')), false)
|
|
626
|
+
assert.equal(recordsIn(root).hello?.digest, release.digest)
|
|
627
|
+
})
|
|
628
|
+
|
|
629
|
+
test('a plugin whose folder is there is not fetched at the start', async () => {
|
|
630
|
+
const release = pack({ 'index.ts': ROUTE })
|
|
631
|
+
const root = appFolder()
|
|
632
|
+
const first = await boot({ releases: releaseOf(release.tarball), root })
|
|
633
|
+
assert.ok((await first.share.pull('github:someone/hello@1.0.0')).ok)
|
|
634
|
+
await first.stop()
|
|
635
|
+
let asked = 0
|
|
636
|
+
const counting: ReleasePort = {
|
|
637
|
+
release: async () => {
|
|
638
|
+
asked += 1
|
|
639
|
+
return undefined
|
|
640
|
+
},
|
|
641
|
+
download: async () => new Uint8Array(),
|
|
642
|
+
}
|
|
643
|
+
const again = await boot({ releases: counting, root })
|
|
644
|
+
assert.deepEqual(await again.restored(), [])
|
|
645
|
+
assert.equal(asked, 0)
|
|
646
|
+
})
|
|
647
|
+
|
|
648
|
+
test('the list says where each pulled plugin came from and who released it; one made in the app has neither', async () => {
|
|
649
|
+
const release = pack({ 'index.ts': ROUTE })
|
|
650
|
+
const root = appFolder()
|
|
651
|
+
const { app, share } = await boot({ releases: releaseOf(release.tarball), root })
|
|
652
|
+
assert.ok((await share.pull('github:someone/hello@1.0.0')).ok)
|
|
653
|
+
mkdirSync(join(root, 'plugins', 'mine'))
|
|
654
|
+
writeFileSync(join(root, 'plugins', 'mine', 'index.ts'), routeSource('mine', '/mine', 'Mine'))
|
|
655
|
+
const installed = await (
|
|
656
|
+
app as unknown as { extensions: { install(dir: string): Promise<{ ok: boolean }> } }
|
|
657
|
+
).extensions.install(join(root, 'plugins', 'mine'))
|
|
658
|
+
assert.ok(installed.ok)
|
|
659
|
+
assert.deepEqual(
|
|
660
|
+
share.list().map(({ name, state, pulledFrom, author, release }) => ({
|
|
661
|
+
name,
|
|
662
|
+
state,
|
|
663
|
+
pulledFrom,
|
|
664
|
+
author,
|
|
665
|
+
release,
|
|
666
|
+
})),
|
|
667
|
+
[
|
|
668
|
+
{
|
|
669
|
+
name: 'hello',
|
|
670
|
+
state: 'active',
|
|
671
|
+
pulledFrom: 'github:someone/hello@1.0.0',
|
|
672
|
+
author: 'someone',
|
|
673
|
+
release: '1.0.0',
|
|
674
|
+
},
|
|
675
|
+
// Its folder has no package.json, so no release version: extensions' `version` (the content's short digest) is all there is.
|
|
676
|
+
{
|
|
677
|
+
name: 'mine',
|
|
678
|
+
state: 'active',
|
|
679
|
+
pulledFrom: undefined,
|
|
680
|
+
author: undefined,
|
|
681
|
+
release: undefined,
|
|
682
|
+
},
|
|
683
|
+
],
|
|
684
|
+
)
|
|
685
|
+
// Everything extensions says is kept (a pending plugin's missing services among it): the source, author and release are added, nothing is left out.
|
|
686
|
+
assert.deepEqual(
|
|
687
|
+
share.list().map(({ pulledFrom: _from, author: _author, release: _release, ...info }) => info),
|
|
688
|
+
(app as unknown as { extensions: { list(): readonly unknown[] } }).extensions.list(),
|
|
689
|
+
)
|
|
690
|
+
})
|
|
691
|
+
|
|
692
|
+
test('removing a pulled plugin takes its folder and record when the folder is the release as pulled', async () => {
|
|
693
|
+
const release = pack({ 'index.ts': ROUTE })
|
|
694
|
+
const root = appFolder()
|
|
695
|
+
const { share, get } = await boot({ releases: releaseOf(release.tarball), root })
|
|
696
|
+
assert.ok((await share.pull('github:someone/hello@1.0.0')).ok)
|
|
697
|
+
assert.deepEqual(await share.remove('hello'), { ok: true, name: 'hello', folder: 'deleted' })
|
|
698
|
+
assert.equal(existsSync(join(root, 'plugins', 'hello')), false)
|
|
699
|
+
assert.deepEqual(recordsIn(root), {})
|
|
700
|
+
assert.equal((await get('/hello')).status, 404)
|
|
701
|
+
// The pull line brings it back.
|
|
702
|
+
assert.ok((await share.pull('github:someone/hello@1.0.0')).ok)
|
|
703
|
+
})
|
|
704
|
+
|
|
705
|
+
test('removing a pulled plugin changed since the pull keeps the folder and says why', async () => {
|
|
706
|
+
const release = pack({ 'index.ts': ROUTE })
|
|
707
|
+
const root = appFolder()
|
|
708
|
+
const { share } = await boot({ releases: releaseOf(release.tarball), root })
|
|
709
|
+
assert.ok((await share.pull('github:someone/hello@1.0.0')).ok)
|
|
710
|
+
writeFileSync(join(root, 'plugins', 'hello', 'mine.md'), 'an edit a person made\n')
|
|
711
|
+
const removed = await share.remove('hello')
|
|
712
|
+
assert.equal(removed.ok && removed.folder, 'kept')
|
|
713
|
+
assert.match(
|
|
714
|
+
removed.ok ? (removed.note ?? '') : '',
|
|
715
|
+
/plugins\/hello was changed after the pull, so it stays/,
|
|
716
|
+
)
|
|
717
|
+
assert.ok(existsSync(join(root, 'plugins', 'hello', 'mine.md')))
|
|
718
|
+
assert.deepEqual(recordsIn(root), {})
|
|
719
|
+
})
|
|
720
|
+
|
|
721
|
+
test('a different copy of the same name is refused, naming where the copy in the app came from', async () => {
|
|
722
|
+
const ana = pack({ 'index.ts': ROUTE })
|
|
723
|
+
const fay = pack({ 'index.ts': routeSource('hello', '/hello', 'Hi from fay') })
|
|
724
|
+
// Two people's releases of a plugin named hello: the port serves whichever is being pulled.
|
|
725
|
+
let serving = ana.tarball
|
|
726
|
+
const releases: ReleasePort = { ...releaseOf(ana.tarball), download: async () => serving }
|
|
727
|
+
const root = appFolder()
|
|
728
|
+
const { share } = await boot({ releases, root })
|
|
729
|
+
assert.ok((await share.pull('github:ana/hello@1.0.0')).ok)
|
|
730
|
+
serving = fay.tarball
|
|
731
|
+
const result = await share.pull('github:fay/hello@1.0.0')
|
|
732
|
+
refused(
|
|
733
|
+
result,
|
|
734
|
+
'folder-exists',
|
|
735
|
+
/^plugins\/hello holds a different copy of this plugin, pulled from github:ana\/hello@1\.0\.0$/,
|
|
736
|
+
)
|
|
737
|
+
assert.match(result.ok ? '' : result.next, /plugin remove hello/)
|
|
738
|
+
})
|
|
739
|
+
|
|
740
|
+
test("the running service knows the digest of every pull it installed, a local file's too, and forgets a refused or removed one", async () => {
|
|
741
|
+
const release = pack({ 'index.ts': ROUTE })
|
|
742
|
+
const broken = pack({
|
|
743
|
+
'index.ts': `export const name = 'broken'\nexport function apply() { throw new Error('refuses to start') }\n`,
|
|
744
|
+
})
|
|
745
|
+
const root = appFolder()
|
|
746
|
+
const { share } = await boot({ root })
|
|
747
|
+
writeFileSync(join(root, 'hello-1.0.0.tgz'), release.tarball)
|
|
748
|
+
writeFileSync(join(root, 'broken-1.0.0.tgz'), broken.tarball)
|
|
749
|
+
assert.ok((await share.pull('hello-1.0.0.tgz')).ok)
|
|
750
|
+
assert.equal(share.pulledDigest('hello'), release.digest)
|
|
751
|
+
assert.equal((await share.pull('broken-1.0.0.tgz')).ok, false)
|
|
752
|
+
assert.equal(share.pulledDigest('broken'), undefined)
|
|
753
|
+
assert.deepEqual(await share.remove('hello'), { ok: true, name: 'hello', folder: 'deleted' })
|
|
754
|
+
assert.equal(share.pulledDigest('hello'), undefined)
|
|
755
|
+
assert.equal(existsSync(join(root, 'plugins', 'hello')), false)
|
|
756
|
+
})
|
|
757
|
+
|
|
758
|
+
/** Three releases as local files of the app: two plugins with their own routes, and a third whose route is already the first one's. */
|
|
759
|
+
function groupReleases(root: string) {
|
|
760
|
+
const files = {
|
|
761
|
+
'hello-1.0.0.tgz': pack({ 'index.ts': ROUTE }),
|
|
762
|
+
'greet-1.0.0.tgz': pack(
|
|
763
|
+
{ 'index.ts': routeSource('greet', '/greet', 'Greet') },
|
|
764
|
+
{ name: 'greet' },
|
|
765
|
+
),
|
|
766
|
+
'echo-1.0.0.tgz': pack({ 'index.ts': routeSource('echo', '/hello', 'Echo') }, { name: 'echo' }),
|
|
767
|
+
}
|
|
768
|
+
for (const [name, release] of Object.entries(files))
|
|
769
|
+
writeFileSync(join(root, name), release.tarball)
|
|
770
|
+
return files
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
test('a list pulls each source on its own: a clashing one is refused by name and the others install and answer', async () => {
|
|
774
|
+
const { share, get, root, installed } = await boot()
|
|
775
|
+
groupReleases(root)
|
|
776
|
+
writeFileSync(
|
|
777
|
+
join(root, 'group.txt'),
|
|
778
|
+
[
|
|
779
|
+
'# the group, one line each',
|
|
780
|
+
'hello-1.0.0.tgz',
|
|
781
|
+
'npm run acryl -- plugin pull echo-1.0.0.tgz # pasted as plugin publish prints it',
|
|
782
|
+
'',
|
|
783
|
+
'plugin pull greet-1.0.0.tgz',
|
|
784
|
+
'hello-1.0.0.tgz',
|
|
785
|
+
].join('\r\n'),
|
|
786
|
+
)
|
|
787
|
+
const result = await share.pullList('group.txt')
|
|
788
|
+
assert.ok(result.ok, JSON.stringify(result))
|
|
789
|
+
if (!result.ok) return
|
|
790
|
+
assert.deepEqual(
|
|
791
|
+
result.entries.map((entry) => [entry.line, entry.source, entry.result.ok]),
|
|
792
|
+
[
|
|
793
|
+
[2, 'hello-1.0.0.tgz', true],
|
|
794
|
+
[3, 'echo-1.0.0.tgz', false],
|
|
795
|
+
[5, 'greet-1.0.0.tgz', true],
|
|
796
|
+
],
|
|
797
|
+
'each source once, in order, with its line',
|
|
798
|
+
)
|
|
799
|
+
const echo = result.entries[1]?.result
|
|
800
|
+
assert.ok(echo !== undefined && !echo.ok)
|
|
801
|
+
if (echo !== undefined && !echo.ok)
|
|
802
|
+
assert.match(echo.reason, /hello/u, 'the refusal names the plugin that has the route')
|
|
803
|
+
assert.deepEqual(
|
|
804
|
+
(await get('/hello')).body,
|
|
805
|
+
{ text: 'Hello' },
|
|
806
|
+
'the first plugin keeps its route',
|
|
807
|
+
)
|
|
808
|
+
assert.deepEqual(
|
|
809
|
+
(await get('/greet')).body,
|
|
810
|
+
{ text: 'Greet' },
|
|
811
|
+
'a source after the refused one still installs',
|
|
812
|
+
)
|
|
813
|
+
assert.deepEqual((installed() as Array<{ name: string }>).map((info) => info.name).sort(), [
|
|
814
|
+
'greet',
|
|
815
|
+
'hello',
|
|
816
|
+
])
|
|
817
|
+
assert.ok(!existsSync(join(root, 'plugins', 'echo')), 'the refused one leaves no folder')
|
|
818
|
+
})
|
|
819
|
+
|
|
820
|
+
test('a list at an https address is read through the download port', async () => {
|
|
821
|
+
const { root } = await boot()
|
|
822
|
+
groupReleases(root)
|
|
823
|
+
const asked: string[] = []
|
|
824
|
+
const { share, get } = await boot({
|
|
825
|
+
root,
|
|
826
|
+
download: async (url) => {
|
|
827
|
+
asked.push(url)
|
|
828
|
+
return new TextEncoder().encode('hello-1.0.0.tgz\ngreet-1.0.0.tgz\n')
|
|
829
|
+
},
|
|
830
|
+
})
|
|
831
|
+
const result = await share.pullList('https://gist.example/group.txt')
|
|
832
|
+
assert.ok(result.ok && result.entries.every((entry) => entry.result.ok), JSON.stringify(result))
|
|
833
|
+
assert.deepEqual(asked, ['https://gist.example/group.txt'])
|
|
834
|
+
assert.deepEqual((await get('/greet')).body, { text: 'Greet' })
|
|
835
|
+
})
|
|
836
|
+
|
|
837
|
+
test('a list that is not one, cannot be read or has a line that is not a source pulls nothing', async () => {
|
|
838
|
+
const { share, root, untouched } = await boot({
|
|
839
|
+
download: async () => {
|
|
840
|
+
throw new Error('https://gist.example/missing answered 404')
|
|
841
|
+
},
|
|
842
|
+
})
|
|
843
|
+
groupReleases(root)
|
|
844
|
+
writeFileSync(join(root, 'two-words.txt'), 'hello-1.0.0.tgz\nhello-1.0.0.tgz greet-1.0.0.tgz\n')
|
|
845
|
+
writeFileSync(join(root, 'empty.txt'), '# nobody yet\n\n')
|
|
846
|
+
writeFileSync(join(root, 'huge.txt'), 'x'.repeat(64 * 1024 + 1))
|
|
847
|
+
const list = async (from: string, reason: RegExp) => {
|
|
848
|
+
const result = await share.pullList(from)
|
|
849
|
+
assert.equal(result.ok, false, JSON.stringify(result))
|
|
850
|
+
if (result.ok) return
|
|
851
|
+
assert.equal(result.refusal, 'list')
|
|
852
|
+
assert.match(result.reason, reason)
|
|
853
|
+
assert.ok(result.next.length > 0)
|
|
854
|
+
}
|
|
855
|
+
await list('two-words.txt', /line 2 holds more than one word/u)
|
|
856
|
+
await list('empty.txt', /names no plugin/u)
|
|
857
|
+
await list('huge.txt', /larger than 65536 bytes/u)
|
|
858
|
+
await list('nowhere.txt', /there is no file nowhere\.txt/u)
|
|
859
|
+
await list('http://gist.example/group.txt', /is not a list/u)
|
|
860
|
+
await list('github:someone/hello@1.0.0', /is not a list/u)
|
|
861
|
+
await list('https://gist.example/missing', /could not be read: .*404/u)
|
|
862
|
+
for (const name of [
|
|
863
|
+
'hello-1.0.0.tgz',
|
|
864
|
+
'greet-1.0.0.tgz',
|
|
865
|
+
'echo-1.0.0.tgz',
|
|
866
|
+
'two-words.txt',
|
|
867
|
+
'empty.txt',
|
|
868
|
+
'huge.txt',
|
|
869
|
+
])
|
|
870
|
+
rmSync(join(root, name))
|
|
871
|
+
await untouched()
|
|
872
|
+
})
|
|
873
|
+
|
|
874
|
+
test('a list is read line by line: comments, a pasted pull command, repeats and Windows line ends', () => {
|
|
875
|
+
assert.deepEqual(
|
|
876
|
+
parsePullList(
|
|
877
|
+
'# group\r\ngithub:a/x@1.0.0 # ana\r\nnpm run acryl -- plugin pull github:b/y@0.2.0@sha256:abc\r\ngithub:a/x@1.0.0\r\n',
|
|
878
|
+
),
|
|
879
|
+
{
|
|
880
|
+
sources: [
|
|
881
|
+
{ line: 2, source: 'github:a/x@1.0.0' },
|
|
882
|
+
{ line: 3, source: 'github:b/y@0.2.0@sha256:abc' },
|
|
883
|
+
],
|
|
884
|
+
problems: [],
|
|
885
|
+
},
|
|
886
|
+
)
|
|
887
|
+
assert.deepEqual(pullListLocation('https://x/y.txt'), { kind: 'address', url: 'https://x/y.txt' })
|
|
888
|
+
assert.deepEqual(pullListLocation('lists/group.txt'), { kind: 'file', path: 'lists/group.txt' })
|
|
889
|
+
assert.deepEqual(pullListLocation('C:\\group.txt'), { kind: 'file', path: 'C:\\group.txt' })
|
|
890
|
+
assert.equal(pullListLocation('http://x/y.txt'), undefined)
|
|
891
|
+
assert.equal(pullListLocation('github:a/x'), undefined)
|
|
892
|
+
})
|
|
893
|
+
|
|
894
|
+
/** A native package for this computer as npm packs it, the entry a plugin declares for it, and ports that count downloads. */
|
|
895
|
+
function nativeEngine() {
|
|
896
|
+
const dir = scratch('share-native-')
|
|
897
|
+
mkdirSync(join(dir, 'package'), { recursive: true })
|
|
898
|
+
const name = `@scope/engine-${process.platform}-${process.arch}`
|
|
899
|
+
writeFileSync(join(dir, 'package', 'package.json'), JSON.stringify({ name, version: '1.0.0' }))
|
|
900
|
+
const file = join(dir, 'engine.tgz')
|
|
901
|
+
execFileSync('tar', ['-czf', file, '-C', dir, 'package'])
|
|
902
|
+
const declared = {
|
|
903
|
+
package: name,
|
|
904
|
+
version: '1.0.0',
|
|
905
|
+
os: process.platform,
|
|
906
|
+
cpu: process.arch,
|
|
907
|
+
integrity: `sha512-${createHash('sha512').update(readFileSync(file)).digest('base64')}`,
|
|
908
|
+
size: 150_000_000,
|
|
909
|
+
}
|
|
910
|
+
const downloads: string[] = []
|
|
911
|
+
const ports: NativePorts = {
|
|
912
|
+
download: async (url, to) => {
|
|
913
|
+
downloads.push(url)
|
|
914
|
+
writeFileSync(to, readFileSync(file))
|
|
915
|
+
},
|
|
916
|
+
unpack: nativePorts().unpack,
|
|
917
|
+
}
|
|
918
|
+
return {
|
|
919
|
+
declared,
|
|
920
|
+
ports,
|
|
921
|
+
downloads,
|
|
922
|
+
link: (root: string) => join(root, 'node_modules', ...name.split('/')),
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
|
|
926
|
+
test('a plugin brings its native package for this computer: installed before it starts, taken away with it', async () => {
|
|
927
|
+
const engine = nativeEngine()
|
|
928
|
+
const release = pack({ 'index.ts': ROUTE }, { acrylblends: { native: [engine.declared] } })
|
|
929
|
+
const { share, root, get } = await boot({
|
|
930
|
+
releases: releaseOf(release.tarball),
|
|
931
|
+
native: engine.ports,
|
|
932
|
+
})
|
|
933
|
+
const result = await share.pull('github:someone/hello@1.0.0')
|
|
934
|
+
assert.ok(result.ok, JSON.stringify(result))
|
|
935
|
+
assert.deepEqual(result.native, {
|
|
936
|
+
package: engine.declared.package,
|
|
937
|
+
version: '1.0.0',
|
|
938
|
+
size: 150_000_000,
|
|
939
|
+
action: 'downloaded',
|
|
940
|
+
})
|
|
941
|
+
assert.equal((await get('/hello')).status, 200)
|
|
942
|
+
assert.ok(existsSync(join(engine.link(root), 'package.json')))
|
|
943
|
+
const removed = await share.remove('hello')
|
|
944
|
+
assert.ok(removed.ok, JSON.stringify(removed))
|
|
945
|
+
assert.equal(existsSync(engine.link(root)), false)
|
|
946
|
+
assert.equal(existsSync(nativeStore(join(root, '.acrylblends'), engine.declared)), false)
|
|
947
|
+
})
|
|
948
|
+
|
|
949
|
+
test('a native package that cannot be downloaded refuses the plugin and leaves nothing', async () => {
|
|
950
|
+
const engine = nativeEngine()
|
|
951
|
+
const release = pack({ 'index.ts': ROUTE }, { acrylblends: { native: [engine.declared] } })
|
|
952
|
+
const failing: NativePorts = {
|
|
953
|
+
...engine.ports,
|
|
954
|
+
download: async () => Promise.reject(new Error('offline')),
|
|
955
|
+
}
|
|
956
|
+
const { share, root, untouched } = await boot({
|
|
957
|
+
releases: releaseOf(release.tarball),
|
|
958
|
+
native: failing,
|
|
959
|
+
})
|
|
960
|
+
refused(
|
|
961
|
+
await share.pull('github:someone/hello@1.0.0'),
|
|
962
|
+
'native',
|
|
963
|
+
/150 MB\) could not be installed: offline/u,
|
|
964
|
+
)
|
|
965
|
+
await untouched()
|
|
966
|
+
assert.equal(existsSync(engine.link(root)), false)
|
|
967
|
+
})
|
|
968
|
+
|
|
969
|
+
test('a native list that cannot be trusted is refused before anything is downloaded', async () => {
|
|
970
|
+
const engine = nativeEngine()
|
|
971
|
+
const release = pack(
|
|
972
|
+
{ 'index.ts': ROUTE },
|
|
973
|
+
{ acrylblends: { native: [{ ...engine.declared, integrity: 'sha1-x' }] } },
|
|
974
|
+
)
|
|
975
|
+
const { share, untouched } = await boot({
|
|
976
|
+
releases: releaseOf(release.tarball),
|
|
977
|
+
native: engine.ports,
|
|
978
|
+
})
|
|
979
|
+
refused(await share.pull('github:someone/hello@1.0.0'), 'native', /sha512/u)
|
|
980
|
+
await untouched()
|
|
981
|
+
assert.deepEqual(engine.downloads, [])
|
|
982
|
+
})
|
|
983
|
+
|
|
984
|
+
test('at the next start a pulled plugin links its native package again, from the download the app kept', async () => {
|
|
985
|
+
const engine = nativeEngine()
|
|
986
|
+
const release = pack({ 'index.ts': ROUTE }, { acrylblends: { native: [engine.declared] } })
|
|
987
|
+
const root = scratch('share-root-')
|
|
988
|
+
writeFileSync(
|
|
989
|
+
join(root, 'blend.yaml'),
|
|
990
|
+
'apiVersion: blends.acryl.dev/v1alpha1\nkind: Blueprint\nmetadata:\n id: t\nspec:\n rows:\n - id: plugin-share\n name: plugin-share\n',
|
|
991
|
+
)
|
|
992
|
+
const first = await boot({ root, releases: releaseOf(release.tarball), native: engine.ports })
|
|
993
|
+
assert.ok((await first.share.pull('github:someone/hello@1.0.0')).ok)
|
|
994
|
+
await first.stop()
|
|
995
|
+
// What an npm install does: the link goes, the placeholder comes back.
|
|
996
|
+
rmSync(engine.link(root), { recursive: true, force: true })
|
|
997
|
+
const second = await boot({ root, releases: releaseOf(release.tarball), native: engine.ports })
|
|
998
|
+
await second.restored()
|
|
999
|
+
assert.ok(existsSync(join(engine.link(root), 'package.json')))
|
|
1000
|
+
assert.equal(engine.downloads.length, 1)
|
|
1001
|
+
})
|