zx-bulk-release 3.2.0 → 3.3.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.
Files changed (44) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/README.md +58 -22
  3. package/package.json +7 -6
  4. package/src/main/js/cli.js +3 -3
  5. package/src/main/js/config.js +2 -1
  6. package/src/main/js/post/api/git.js +96 -47
  7. package/src/main/js/post/api/npm.js +24 -15
  8. package/src/main/js/post/courier/channels/changelog.js +5 -10
  9. package/src/main/js/post/courier/channels/cmd.js +5 -1
  10. package/src/main/js/post/courier/channels/gh-pages.js +4 -4
  11. package/src/main/js/post/courier/channels/gh-release.js +3 -3
  12. package/src/main/js/post/courier/channels/git-tag.js +6 -26
  13. package/src/main/js/post/courier/channels/index.js +19 -0
  14. package/src/main/js/post/courier/channels/meta.js +4 -4
  15. package/src/main/js/post/courier/channels/npm.js +20 -17
  16. package/src/main/js/post/courier/execute.js +88 -0
  17. package/src/main/js/post/courier/index.js +115 -228
  18. package/src/main/js/post/courier/plan.js +149 -0
  19. package/src/main/js/post/courier/semaphore.js +14 -24
  20. package/src/main/js/post/courier/seniority.js +7 -17
  21. package/src/main/js/post/depot/context.js +14 -1
  22. package/src/main/js/post/depot/deps.js +13 -1
  23. package/src/main/js/post/depot/exec.js +2 -2
  24. package/src/main/js/post/depot/reconcile.js +15 -23
  25. package/src/main/js/post/depot/steps/clean.js +1 -2
  26. package/src/main/js/post/depot/steps/contextify.js +9 -0
  27. package/src/main/js/post/depot/steps/pack.js +6 -4
  28. package/src/main/js/post/depot/steps/publish.js +2 -1
  29. package/src/main/js/post/log.js +10 -1
  30. package/src/main/js/post/modes/deliver.js +14 -30
  31. package/src/main/js/post/modes/pack.js +8 -16
  32. package/src/main/js/post/modes/receive.js +10 -14
  33. package/src/main/js/post/modes/verify.js +18 -2
  34. package/src/main/js/post/parcel/build.js +24 -3
  35. package/src/main/js/post/parcel/directive.js +10 -24
  36. package/src/main/js/post/parcel/index.js +7 -1
  37. package/src/main/js/post/parcel/populate.js +136 -0
  38. package/src/main/js/post/parcel/verify-directive.js +81 -0
  39. package/src/main/js/post/parcel/verify.js +93 -36
  40. package/src/main/js/post/release.js +5 -4
  41. package/src/main/js/post/tar.js +83 -20
  42. package/src/main/js/util.js +32 -5
  43. package/src/test/js/utils/mock.js +0 -3
  44. package/src/test/js/utils/repo.js +10 -4
@@ -1,8 +1,18 @@
1
1
  import tar from 'tar-stream'
2
2
  import crypto from 'node:crypto'
3
+ import zlib from 'node:zlib'
3
4
  import {pipeline} from 'node:stream/promises'
4
5
  import {fs, path} from 'zx-extra'
5
6
 
7
+ // A handled parcel is replaced by a marker: released, conflict, skip (3.x, held for credentials) or orphan.
8
+ const MARKERS = new Set(['released', 'skip', 'conflict', 'orphan'])
9
+
10
+ export const readMarker = async (tarPath) => {
11
+ if ((await fs.stat(tarPath)).size > 16) return null
12
+ const content = await fs.readFile(tarPath, 'utf8')
13
+ return MARKERS.has(content) ? content : null
14
+ }
15
+
6
16
  export const packTar = async (tarPath, manifest, files = []) => {
7
17
  await fs.ensureDir(path.dirname(tarPath))
8
18
  const pack = tar.pack()
@@ -41,33 +51,86 @@ const addDir = async (pack, prefix, dirPath) => {
41
51
  }
42
52
  }
43
53
 
54
+ // Parcels come from an untrusted phase: only plain files are taken, and only inside destDir.
55
+ // A bad entry fails the unpacking, it never escapes as an unhandled rejection.
44
56
  export const unpackTar = async (tarPath, destDir) => {
45
- await fs.ensureDir(destDir)
57
+ const root = path.resolve(destDir)
46
58
  const extract = tar.extract()
47
59
  let manifest = null
48
60
 
49
- const done = new Promise((resolve, reject) => {
50
- extract.on('entry', (header, stream, cb) => {
61
+ const take = async ({name, type}, buf) => {
62
+ if (name === 'manifest.json') {
63
+ manifest = JSON.parse(buf.toString('utf8'))
64
+ return
65
+ }
66
+ const dest = path.resolve(root, name)
67
+ if (type !== 'file' || !dest.startsWith(root + path.sep)) throw new Error(`unsafe entry in ${path.basename(tarPath)}: ${name}`)
68
+ await fs.outputFile(dest, buf)
69
+ }
70
+
71
+ extract.on('entry', (header, stream, cb) => {
72
+ const chunks = []
73
+ stream.on('data', d => chunks.push(d))
74
+ stream.on('end', () => take(header, Buffer.concat(chunks)).then(() => cb(), cb))
75
+ stream.resume()
76
+ })
77
+
78
+ await fs.ensureDir(root)
79
+ await pipeline(fs.createReadStream(tarPath), extract)
80
+ return {manifest, dir: destDir}
81
+ }
82
+
83
+ // Reads manifest.json only, other entries are skipped without touching the disk.
84
+ export const readManifest = async (tarPath) => {
85
+ const extract = tar.extract()
86
+ let raw = null
87
+
88
+ extract.on('entry', (header, stream, cb) => {
89
+ const chunks = []
90
+ stream.on('data', d => header.name === 'manifest.json' && chunks.push(d))
91
+ stream.on('end', () => {
92
+ if (header.name === 'manifest.json') raw = Buffer.concat(chunks).toString('utf8')
93
+ cb()
94
+ })
95
+ stream.resume()
96
+ })
97
+
98
+ await pipeline(fs.createReadStream(tarPath), extract)
99
+ return raw === null ? null : JSON.parse(raw)
100
+ }
101
+
102
+ // Reads package.json from the npm tarball (package.tgz) inside a parcel — npm publishes
103
+ // under the name and version found there. Null if there is no tarball or it is ambiguous.
104
+ export const readNpmManifest = async (tarPath) => {
105
+ const outer = tar.extract()
106
+ const found = []
107
+
108
+ outer.on('entry', (header, stream, next) => {
109
+ if (header.name !== 'package.tgz') {
110
+ stream.on('end', next)
111
+ return stream.resume()
112
+ }
113
+
114
+ const inner = tar.extract()
115
+ inner.on('entry', (h, s, n) => {
116
+ const match = isNpmManifest(h.name)
51
117
  const chunks = []
52
- stream.on('data', d => chunks.push(d))
53
- stream.on('end', async () => {
54
- const buf = Buffer.concat(chunks)
55
- if (header.name === 'manifest.json') {
56
- manifest = JSON.parse(buf.toString('utf8'))
57
- } else {
58
- const dest = path.join(destDir, header.name)
59
- await fs.ensureDir(path.dirname(dest))
60
- await fs.writeFile(dest, buf)
61
- }
62
- cb()
118
+ s.on('data', d => match && chunks.push(d))
119
+ s.on('end', () => {
120
+ if (match) found.push({type: h.type, json: Buffer.concat(chunks).toString('utf8')})
121
+ n()
63
122
  })
64
- stream.resume()
123
+ s.resume()
65
124
  })
66
- extract.on('finish', resolve)
67
- extract.on('error', reject)
125
+ pipeline(stream, zlib.createGunzip(), inner).then(() => next(), next)
68
126
  })
69
127
 
70
- await pipeline(fs.createReadStream(tarPath), extract)
71
- await done
72
- return {manifest, dir: destDir}
128
+ await pipeline(fs.createReadStream(tarPath), outer)
129
+ if (found.length !== 1 || found[0].type !== 'file') return null
130
+ try { return JSON.parse(found[0].json) } catch { return null }
73
131
  }
132
+
133
+ // npm drops the first path segment as it is, whatever it is called, then normalizes the rest and takes
134
+ // package.json from the top: every entry that lands there counts, so a second spelling can't hide behind the first.
135
+ const isNpmManifest = (name) =>
136
+ path.posix.normalize(name.split('/').slice(1).join('/')).replace(/^\/+/, '') === 'package.json'
@@ -1,3 +1,4 @@
1
+ import {AsyncResource} from 'node:async_hooks'
1
2
  import {$} from 'zx-extra'
2
3
 
3
4
  export const tpl = (str, context) =>
@@ -79,17 +80,43 @@ export const queuefy = (fn, concurrency = 1) => {
79
80
  const next = () => {
80
81
  if (active >= concurrency || queue.length === 0) return
81
82
  active++
82
- const {args, resolve, reject} = queue.shift()
83
- Promise.resolve().then(() => fn(...args)).then(
83
+ const {task, resolve, reject} = queue.shift()
84
+ Promise.resolve().then(task).then(
84
85
  v => { active--; resolve(v); next() },
85
86
  e => { active--; reject(e); next() }
86
87
  )
87
88
  }
89
+ // A call runs in its caller's async context (zx `within`, `$.scope`), not in the one that freed the slot.
88
90
  return (...args) => new Promise((resolve, reject) => {
89
- queue.push({args, resolve, reject})
91
+ queue.push({task: AsyncResource.bind(() => fn(...args)), resolve, reject})
90
92
  next()
91
93
  })
92
94
  }
93
95
 
94
- export const pool = async (tasks, concurrency, fn) =>
95
- Promise.all(tasks.map(queuefy(fn, concurrency)))
96
+ // Settled work reports its errors together: one as is (its exit code too), several as one AggregateError.
97
+ export const throwAll = (errors, what) => {
98
+ if (errors.length > 1) throw new AggregateError(errors, `${errors.length} ${what} failed`)
99
+ if (errors.length) throw errors[0]
100
+ }
101
+
102
+ // Keys ordered so that each comes after what it depends on (only keys among `keys` count); throws on a cycle.
103
+ export const topoOrder = (keys, deps) => {
104
+ const order = []
105
+ const visit = (key, path) => {
106
+ if (order.includes(key)) return
107
+ if (path.includes(key)) throw new Error(`dependency cycle: ${[...path, key].join(' → ')}`)
108
+ for (const dep of deps.get(key) || []) if (keys.includes(dep)) visit(dep, [...path, key])
109
+ order.push(key)
110
+ }
111
+ for (const key of keys) visit(key, [])
112
+ return order
113
+ }
114
+
115
+ // Runs tasks one at a time per key: lock(key, task).
116
+ export const keyedQueue = () => {
117
+ const queues = new Map()
118
+ return (key, task) => {
119
+ if (!queues.has(key)) queues.set(key, queuefy(task => task()))
120
+ return queues.get(key)(task)
121
+ }
122
+ }
@@ -42,8 +42,6 @@ export const gitResponses = (overrides = {}) => [
42
42
  ['git config --get remote.origin.url', overrides.origin ?? 'https://github.com/test-org/test-repo.git'],
43
43
  ['git tag -l', overrides.tags ?? ''],
44
44
  [/git log .+--format/, overrides.log ?? ''],
45
- ['git config user.name', ''],
46
- ['git config user.email', ''],
47
45
  [/git tag -m/, ''],
48
46
  ['git push', ''],
49
47
  ['git fetch', ''],
@@ -52,7 +50,6 @@ export const gitResponses = (overrides = {}) => [
52
50
  ['git add', ''],
53
51
  ['git commit', ''],
54
52
  [/git remote/, ''],
55
- [/git config --unset/, ''],
56
53
  ]
57
54
 
58
55
  export const npmResponses = (overrides = {}) => [
@@ -5,19 +5,25 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url))
5
5
  export const fixtures = path.resolve(__dirname, '../../fixtures')
6
6
 
7
7
  export const createNpmRegistry = () => {
8
- let p
8
+ let p, exited
9
9
 
10
10
  return {
11
11
  address: $.env.NPM_REGISTRY || 'http://localhost:4873',
12
12
  async start() {
13
13
  fs.removeSync(path.resolve(__dirname, '../../../../storage'))
14
14
  const config = path.resolve(__dirname, '../../../../verdaccio.config.yaml')
15
- p = $({preferLocal: true})`verdaccio --config ${config}`
15
+ exited = false
16
+ p = $({preferLocal: true, nothrow: true})`verdaccio --config ${config}`
17
+ p.then(() => { exited = true })
16
18
 
17
- return sleep(1000)
19
+ await sleep(1000)
20
+ // e.g. the port is still taken by a registry from another run
21
+ if (exited) throw new Error(`verdaccio exited: ${await p}`)
18
22
  },
19
23
  async stop() {
20
- p.nothrow().kill()
24
+ if (exited) return
25
+ await p.kill() // signals the whole process tree, verdaccio included
26
+ await p
21
27
  }
22
28
  }
23
29
  }