@acryldev/plugin-share 0.1.3 → 0.1.4

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.
@@ -22,6 +22,7 @@ import {
22
22
  integrityOf,
23
23
  type PublishGitPort,
24
24
  packFolder,
25
+ type RegistryPort,
25
26
  type ReleasePort,
26
27
  type RepositoryHostPort,
27
28
  readTarball,
@@ -41,6 +42,7 @@ import {
41
42
  type SharedPluginInfo,
42
43
  type SharedRemoveResult,
43
44
  } from '../index.ts'
45
+ import { mergeTrees } from '../merge-edits.ts'
44
46
  import { type NativePorts, nativePorts, nativeStore } from '../native.ts'
45
47
  import { splitDigest } from '../pull-check.ts'
46
48
  import { parsePullList, pullListLocation } from '../pull-list.ts'
@@ -100,6 +102,8 @@ async function boot({
100
102
  publish?: PublishPorts
101
103
  download?: (url: string) => Promise<Uint8Array>
102
104
  native?: NativePorts
105
+ npm?: RegistryPort
106
+ registryIndex?: string
103
107
  root?: string
104
108
  } = {}) {
105
109
  const app = new Context()
@@ -291,8 +295,10 @@ test('a package with no entry file or a name no folder can hold is refused', asy
291
295
  })
292
296
 
293
297
  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/)
298
+ const { share, untouched } = await boot({
299
+ npm: { resolve: async () => undefined, download: async () => new Uint8Array() },
300
+ })
301
+ refused(await share.pull('npm:hello'), 'fetch', /npm has no hello/)
296
302
  refused(await share.pull('store:hello'), 'source', /store/)
297
303
  refused(await share.pull('hello'), 'source', /github:/)
298
304
  refused(await share.pull('git+https://github.com/someone/hello.git'), 'source', /no release/)
@@ -999,3 +1005,352 @@ test('at the next start a pulled plugin links its native package again, from the
999
1005
  assert.ok(existsSync(join(engine.link(root), 'package.json')))
1000
1006
  assert.equal(engine.downloads.length, 1)
1001
1007
  })
1008
+
1009
+ /** Releases of one plugin by tag, on a pretend GitHub: a source `@1.1.0` reads `v1.1.0`, no tag reads the newest. */
1010
+ const releasesByTag = (byTag: Record<string, Uint8Array>): ReleasePort => ({
1011
+ release: async (_owner, _repo, tag) => {
1012
+ const chosen = tag ?? Object.keys(byTag).at(-1)
1013
+ return chosen === undefined || byTag[chosen] === undefined
1014
+ ? undefined
1015
+ : {
1016
+ tag: chosen,
1017
+ assets: [{ name: `hello-${chosen.slice(1)}.tgz`, url: `fixture:${chosen}` }],
1018
+ }
1019
+ },
1020
+ download: async (url) => byTag[url.slice('fixture:'.length)] ?? new Uint8Array(),
1021
+ })
1022
+
1023
+ const NOTES_V1 = 'one\ntwo\nthree\nfour\nfive\nsix\nseven\n'
1024
+
1025
+ function twoReleases(v2: Record<string, string>) {
1026
+ const v1 = pack({ 'index.ts': ROUTE, 'notes.md': NOTES_V1 })
1027
+ const next = pack(
1028
+ { 'index.ts': routeSource('hello', '/hello', 'Hello 1.1'), ...v2 },
1029
+ { version: '1.1.0' },
1030
+ )
1031
+ return releasesByTag({ 'v1.0.0': v1.tarball, 'v1.1.0': next.tarball })
1032
+ }
1033
+
1034
+ test('a newer release of an untouched pulled plugin updates it in place, live', async () => {
1035
+ const root = appFolder()
1036
+ const { share, get } = await boot({ releases: twoReleases({ 'notes.md': NOTES_V1 }), root })
1037
+ assert.ok((await share.pull('github:someone/hello@1.0.0')).ok)
1038
+ assert.deepEqual((await get('/hello')).body, { text: 'Hello' })
1039
+
1040
+ const updated = await share.pull('github:someone/hello@1.1.0')
1041
+ assert.ok(updated.ok, JSON.stringify(updated))
1042
+ assert.equal(updated.action, 'updated')
1043
+ assert.equal(updated.kept, undefined)
1044
+ assert.deepEqual((await get('/hello')).body, { text: 'Hello 1.1' })
1045
+ assert.equal(recordsIn(root).hello?.source, 'github:someone/hello@1.1.0')
1046
+ })
1047
+
1048
+ test("a newer release keeps the person's own edits: a file only they changed stays, a file both changed is merged, a file of their own stays", async () => {
1049
+ const theirNotes = NOTES_V1.replace('seven\n', 'seven\neight\n')
1050
+ const root = appFolder()
1051
+ const { share, get } = await boot({ releases: twoReleases({ 'notes.md': theirNotes }), root })
1052
+ assert.ok((await share.pull('github:someone/hello@1.0.0')).ok)
1053
+ const folder = join(root, 'plugins', 'hello')
1054
+ writeFileSync(join(folder, 'notes.md'), NOTES_V1.replace('one\n', 'ONE\n'))
1055
+ writeFileSync(join(folder, 'mine.md'), 'a file of my own\n')
1056
+
1057
+ const updated = await share.pull('github:someone/hello@1.1.0')
1058
+ assert.ok(updated.ok, JSON.stringify(updated))
1059
+ assert.equal(updated.action, 'updated')
1060
+ assert.deepEqual(updated.kept, ['mine.md', 'notes.md'])
1061
+ assert.equal(
1062
+ readFileSync(join(folder, 'notes.md'), 'utf8'),
1063
+ 'ONE\ntwo\nthree\nfour\nfive\nsix\nseven\neight\n',
1064
+ )
1065
+ assert.equal(readFileSync(join(folder, 'mine.md'), 'utf8'), 'a file of my own\n')
1066
+ assert.deepEqual((await get('/hello')).body, { text: 'Hello 1.1' })
1067
+ // The record names the release, so the next update merges against this one and `plugin remove` keeps the folder as the person's.
1068
+ assert.equal(recordsIn(root).hello?.digest, share.pulledDigest('hello'))
1069
+ const removed = await share.remove('hello')
1070
+ assert.ok(removed.ok && removed.folder === 'kept', JSON.stringify(removed))
1071
+ })
1072
+
1073
+ test("a person and a release that changed the same lines are refused, and the person's folder is exactly as it was", async () => {
1074
+ const root = appFolder()
1075
+ const { share, get } = await boot({
1076
+ releases: twoReleases({ 'notes.md': NOTES_V1.replace('one\n', 'UNO\n') }),
1077
+ root,
1078
+ })
1079
+ assert.ok((await share.pull('github:someone/hello@1.0.0')).ok)
1080
+ const folder = join(root, 'plugins', 'hello')
1081
+ writeFileSync(join(folder, 'notes.md'), NOTES_V1.replace('one\n', 'ONE\n'))
1082
+
1083
+ const result = await share.pull('github:someone/hello@1.1.0')
1084
+ refused(result, 'folder-exists', /changes of your own to notes\.md/)
1085
+ assert.equal(readFileSync(join(folder, 'notes.md'), 'utf8'), NOTES_V1.replace('one\n', 'ONE\n'))
1086
+ assert.equal(readFileSync(join(folder, 'index.ts'), 'utf8'), ROUTE)
1087
+ assert.deepEqual((await get('/hello')).body, { text: 'Hello' })
1088
+ })
1089
+
1090
+ test('the merge of three trees: one side changed takes that change, both changed is merged or a conflict, a removal against an edit is a conflict', () => {
1091
+ const tree = (entries: Record<string, string>) =>
1092
+ new Map(Object.entries(entries).map(([path, text]) => [path, Buffer.from(text)]))
1093
+ const text = (bytes: Uint8Array | undefined) =>
1094
+ bytes === undefined ? undefined : Buffer.from(bytes).toString()
1095
+ const lineMerge = (_base: Uint8Array, mine: Uint8Array, theirs: Uint8Array) =>
1096
+ Buffer.from(`${Buffer.from(mine)}+${Buffer.from(theirs)}`)
1097
+ const result = mergeTrees(
1098
+ tree({ a: '1', b: '1', c: '1', d: '1', e: '1' }),
1099
+ tree({ a: '1', b: 'mine', c: 'mine', e: 'mine' }),
1100
+ tree({ a: 'theirs', b: '1', c: 'theirs', d: '1', f: 'new' }),
1101
+ lineMerge,
1102
+ )
1103
+ assert.deepEqual(
1104
+ Object.fromEntries([...result.files].map(([path, bytes]) => [path, text(bytes)])),
1105
+ { a: 'theirs', b: 'mine', c: 'mine+theirs', f: 'new' },
1106
+ )
1107
+ assert.deepEqual(result.kept, ['b', 'c', 'd'])
1108
+ assert.deepEqual(result.conflicts, ['e'])
1109
+ })
1110
+
1111
+ // registry:<id>: a plugin the public registry lists, pulled by its id; the integrity the registry lists is the check the release must pass.
1112
+ const REGISTRY = 'fixture:registry'
1113
+
1114
+ function registryOf(entries: unknown[]): (url: string) => Promise<Uint8Array> {
1115
+ return async (url) => {
1116
+ if (url !== REGISTRY) throw new Error(`not served: ${url}`)
1117
+ return Buffer.from(JSON.stringify({ formatVersion: 1, entries }))
1118
+ }
1119
+ }
1120
+
1121
+ test('registry:<id> pulls the release the registry lists, checked against the integrity it lists, and records the GitHub source', async () => {
1122
+ const release = pack({ 'index.ts': ROUTE })
1123
+ const root = appFolder()
1124
+ const { share, get } = await boot({
1125
+ releases: releasesByTag({ 'v1.0.0': release.tarball }),
1126
+ download: registryOf([
1127
+ { id: 'other', repo: 'https://github.com/x/other', version: '9.9.9', integrity: 'sha512-no' },
1128
+ {
1129
+ id: 'hello',
1130
+ name: '@someone/hello',
1131
+ repo: 'https://github.com/someone/hello',
1132
+ version: '1.0.0',
1133
+ integrity: release.integrity,
1134
+ },
1135
+ ]),
1136
+ registryIndex: REGISTRY,
1137
+ root,
1138
+ })
1139
+ const result = await share.pull('registry:hello')
1140
+ assert.ok(result.ok, JSON.stringify(result))
1141
+ assert.deepEqual((await get('/hello')).body, { text: 'Hello' })
1142
+ assert.equal(recordsIn(root).hello?.source, 'github:someone/hello@1.0.0')
1143
+ // The package name finds it too, and the same bytes are already there.
1144
+ const again = await share.pull('registry:@someone/hello')
1145
+ assert.ok(again.ok && again.action === 'unchanged', JSON.stringify(again))
1146
+ })
1147
+
1148
+ test('registry:<id> refuses an id the registry does not list, a registry that cannot be read, and a release the registry did not see', async () => {
1149
+ const release = pack({ 'index.ts': ROUTE })
1150
+ const listed = [
1151
+ {
1152
+ id: 'hello',
1153
+ repo: 'https://github.com/someone/hello',
1154
+ version: '1.0.0',
1155
+ integrity: 'sha512-other',
1156
+ },
1157
+ ]
1158
+ const { share, untouched } = await boot({
1159
+ releases: releasesByTag({ 'v1.0.0': release.tarball }),
1160
+ download: registryOf(listed),
1161
+ registryIndex: REGISTRY,
1162
+ })
1163
+ refused(await share.pull('registry:nothing'), 'source', /lists no plugin nothing/)
1164
+ // The registry listed other bytes than the release holds now: refused, nothing installed.
1165
+ const changed = await share.pull('registry:hello')
1166
+ assert.equal(changed.ok, false, JSON.stringify(changed))
1167
+ await untouched()
1168
+
1169
+ const unreachable = await boot({
1170
+ download: async () => {
1171
+ throw new Error('offline')
1172
+ },
1173
+ registryIndex: REGISTRY,
1174
+ })
1175
+ refused(
1176
+ await unreachable.share.pull('registry:hello'),
1177
+ 'fetch',
1178
+ /registry could not be read: offline/,
1179
+ )
1180
+ const notAnIndex = await boot({
1181
+ download: async () => Buffer.from('<html>'),
1182
+ registryIndex: REGISTRY,
1183
+ })
1184
+ refused(await notAnIndex.share.pull('registry:hello'), 'source', /does not understand/)
1185
+ })
1186
+
1187
+ // npm as a target: the person's own npm login (`npm whoami`), the same plan and yes, and a pull that checks the integrity npm lists.
1188
+ function fakeNpm(login: string | null = 'ana') {
1189
+ const registry = new Map<string, Buffer>()
1190
+ const sent: string[] = []
1191
+ const port: NonNullable<PublishPorts['npm']> = {
1192
+ login: () => {
1193
+ if (login === null) throw new Error('npm whoami failed: ENEEDAUTH (sign in with: npm login)')
1194
+ return login
1195
+ },
1196
+ hasVersion: (name, version) => registry.has(`${name}@${version}`),
1197
+ publish(tarballPath, registryAddress, access) {
1198
+ const bytes = readFileSync(tarballPath)
1199
+ const manifest = JSON.parse(
1200
+ Buffer.from(
1201
+ readTarball(bytes).find((file) => file.path === 'package.json')?.content ?? [],
1202
+ ).toString('utf8'),
1203
+ ) as { name: string; version: string }
1204
+ registry.set(`${manifest.name}@${manifest.version}`, bytes)
1205
+ sent.push(`publish ${manifest.name}@${manifest.version} ${access} ${registryAddress}`)
1206
+ },
1207
+ }
1208
+ // What an app pulling `npm:` reads: the version npm lists with the integrity it computed.
1209
+ const reads = {
1210
+ async resolve(name: string, version?: string) {
1211
+ const wanted =
1212
+ version ??
1213
+ [...registry.keys()]
1214
+ .filter((key) => key.startsWith(`${name}@`))
1215
+ .at(-1)
1216
+ ?.slice(name.length + 1)
1217
+ const bytes = wanted === undefined ? undefined : registry.get(`${name}@${wanted}`)
1218
+ return bytes === undefined || wanted === undefined
1219
+ ? undefined
1220
+ : { version: wanted, tarball: `npm:${name}@${wanted}`, integrity: integrityOf(bytes) }
1221
+ },
1222
+ async download(spec: string) {
1223
+ return registry.get(spec.replace(/^npm:/u, '')) ?? new Uint8Array()
1224
+ },
1225
+ }
1226
+ return { port, reads, registry, sent }
1227
+ }
1228
+
1229
+ async function bootForNpm(npm: ReturnType<typeof fakeNpm>) {
1230
+ const hub = fakeHost()
1231
+ const booted = await boot({ publish: { host: hub.host, git: hub.git, npm: npm.port } })
1232
+ mkdirSync(join(booted.root, 'plugins', 'hello'), { recursive: true })
1233
+ writeFileSync(join(booted.root, 'plugins', 'hello', 'index.ts'), ROUTE)
1234
+ return { ...booted, hub }
1235
+ }
1236
+
1237
+ test("an npm plan names the package under the account of the person's npm login, and sends nothing", async () => {
1238
+ const npm = fakeNpm('ana')
1239
+ const { share, hub } = await bootForNpm(npm)
1240
+ const plan = planned(await share.planPublish('hello', { to: 'npm' }))
1241
+ assert.deepEqual(
1242
+ [plan.target, plan.package, plan.owner, plan.version, plan.license, plan.registry],
1243
+ ['npm', '@ana/hello', 'ana', '0.1.0', 'MIT', 'https://registry.npmjs.org/'],
1244
+ )
1245
+ assert.equal(
1246
+ plan.pullLine,
1247
+ `npm run acryl -- plugin pull npm:@ana/hello@0.1.0@sha256:${plan.digest}`,
1248
+ )
1249
+ assert.equal(
1250
+ planned(await share.planPublish('hello', { to: 'npm', scope: '@acme' })).package,
1251
+ '@acme/hello',
1252
+ )
1253
+ refusedPublish(
1254
+ await share.planPublish('hello', { to: 'npm', scope: 'Not A Scope' }),
1255
+ /not an npm scope/,
1256
+ )
1257
+ assert.deepEqual([...npm.sent, ...hub.sent], [])
1258
+ })
1259
+
1260
+ test('without an npm login nothing is planned, and the refusal says how to sign in', async () => {
1261
+ const { share } = await bootForNpm(fakeNpm(null))
1262
+ refusedPublish(await share.planPublish('hello', { to: 'npm' }), /ENEEDAUTH/)
1263
+ })
1264
+
1265
+ test('publishing the confirmed npm plan sends exactly the planned bytes; the next publish is the next patch', async () => {
1266
+ const npm = fakeNpm('ana')
1267
+ const { share, root } = await bootForNpm(npm)
1268
+ const plan = planned(await share.planPublish('hello', { to: 'npm' }))
1269
+ const result = await share.publish('hello', plan, { to: 'npm' })
1270
+ assert.ok(result.ok, JSON.stringify(result))
1271
+ assert.equal(result.integrity, plan.integrity)
1272
+ assert.equal(result.url, 'https://www.npmjs.com/package/@ana/hello')
1273
+ assert.deepEqual(npm.sent, ['publish @ana/hello@0.1.0 public https://registry.npmjs.org/'])
1274
+ const bytes = npm.registry.get('@ana/hello@0.1.0')
1275
+ assert.ok(bytes)
1276
+ assert.equal(integrityOf(bytes), plan.integrity)
1277
+ const manifest = JSON.parse(
1278
+ Buffer.from(
1279
+ readTarball(bytes).find((file) => file.path === 'package.json')?.content ?? [],
1280
+ ).toString('utf8'),
1281
+ )
1282
+ assert.deepEqual([manifest.name, manifest.license], ['@ana/hello', 'MIT'])
1283
+ assert.ok(manifest.keywords.includes('cordis-plugin'))
1284
+
1285
+ writeFileSync(
1286
+ join(root, 'plugins', 'hello', 'index.ts'),
1287
+ routeSource('hello', '/hello', 'Hello again'),
1288
+ )
1289
+ const next = planned(await share.planPublish('hello', { to: 'npm' }))
1290
+ assert.deepEqual([next.version, next.released], ['0.1.1', '0.1.0'])
1291
+ assert.ok((await share.publish('hello', next, { to: 'npm' })).ok)
1292
+ assert.ok(npm.registry.has('@ana/hello@0.1.1'))
1293
+ })
1294
+
1295
+ test('a plan confirmed for npm is not published once the person asks for another scope', async () => {
1296
+ const npm = fakeNpm('ana')
1297
+ const { share } = await bootForNpm(npm)
1298
+ const plan = planned(await share.planPublish('hello', { to: 'npm' }))
1299
+ const refusedResult = await share.publish('hello', plan, { to: 'npm', scope: 'acme' })
1300
+ assert.equal(refusedResult.ok, false)
1301
+ assert.deepEqual(npm.sent, [])
1302
+ })
1303
+
1304
+ test('npm:<package> pulls what npm lists, checked against the integrity it lists, and a newer version updates it in place', async () => {
1305
+ const npm = fakeNpm('ana')
1306
+ const author = await bootForNpm(npm)
1307
+ const first = planned(await author.share.planPublish('hello', { to: 'npm' }))
1308
+ assert.ok((await author.share.publish('hello', first, { to: 'npm' })).ok)
1309
+
1310
+ const root = appFolder()
1311
+ const reader = await boot({ npm: npm.reads, root })
1312
+ const pulled = await reader.share.pull(`npm:@ana/hello@0.1.0@sha256:${first.digest}`)
1313
+ assert.ok(pulled.ok, JSON.stringify(pulled))
1314
+ assert.equal(pulled.name, 'hello')
1315
+ assert.deepEqual((await reader.get('/hello')).body, { text: 'Hello' })
1316
+ assert.equal(recordsIn(root).hello?.source, 'npm:@ana/hello@0.1.0')
1317
+
1318
+ writeFileSync(
1319
+ join(author.root, 'plugins', 'hello', 'index.ts'),
1320
+ routeSource('hello', '/hello', 'Hello 0.1.1'),
1321
+ )
1322
+ const next = planned(await author.share.planPublish('hello', { to: 'npm' }))
1323
+ assert.ok((await author.share.publish('hello', next, { to: 'npm' })).ok)
1324
+ const updated = await reader.share.pull('npm:@ana/hello')
1325
+ assert.ok(
1326
+ updated.ok && updated.action === 'updated' && updated.version === '0.1.1',
1327
+ JSON.stringify(updated),
1328
+ )
1329
+ assert.deepEqual((await reader.get('/hello')).body, { text: 'Hello 0.1.1' })
1330
+ })
1331
+
1332
+ test('npm:<package> refuses bytes whose checksum is not the one npm lists, and a package npm does not have', async () => {
1333
+ const npm = fakeNpm('ana')
1334
+ const author = await bootForNpm(npm)
1335
+ assert.ok(
1336
+ (
1337
+ await author.share.publish(
1338
+ 'hello',
1339
+ planned(await author.share.planPublish('hello', { to: 'npm' })),
1340
+ { to: 'npm' },
1341
+ )
1342
+ ).ok,
1343
+ )
1344
+ const tampered: typeof npm.reads = {
1345
+ ...npm.reads,
1346
+ resolve: async (name, version) => {
1347
+ const found = await npm.reads.resolve(name, version)
1348
+ return found === undefined ? undefined : { ...found, integrity: 'sha512-someoneelse' }
1349
+ },
1350
+ }
1351
+ const reader = await boot({ npm: tampered })
1352
+ refused(await reader.share.pull('npm:@ana/hello'), 'fetch', /checksum/)
1353
+ await reader.untouched()
1354
+ const empty = await boot({ npm: npm.reads })
1355
+ refused(await empty.share.pull('npm:@ana/nothing'), 'fetch', /npm has no @ana\/nothing/)
1356
+ })