@acryldev/plugin-share 0.1.3 → 0.1.5

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