pnpm 7.9.1 → 7.9.2

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 (108) hide show
  1. package/README.md +22 -7
  2. package/bin/pnpm.cjs +3 -0
  3. package/bin/pnpx.cjs +1 -15
  4. package/dist/node_modules/.modules.yaml +3 -3
  5. package/dist/node_modules/.pnpm/lock.yaml +76 -42
  6. package/dist/node_modules/@npmcli/fs/lib/common/owner-sync.js +96 -0
  7. package/dist/node_modules/@npmcli/fs/lib/common/owner.js +8 -4
  8. package/dist/node_modules/@npmcli/fs/lib/copy-file.js +3 -9
  9. package/dist/node_modules/@npmcli/fs/lib/fs.js +9 -3
  10. package/dist/node_modules/@npmcli/fs/lib/index.js +3 -1
  11. package/dist/node_modules/@npmcli/fs/lib/mkdir.js +19 -0
  12. package/dist/node_modules/@npmcli/fs/lib/mkdtemp.js +3 -8
  13. package/dist/node_modules/@npmcli/fs/lib/with-owner-sync.js +21 -0
  14. package/dist/node_modules/@npmcli/fs/lib/with-owner.js +21 -0
  15. package/dist/node_modules/@npmcli/fs/lib/with-temp-dir.js +4 -2
  16. package/dist/node_modules/@npmcli/fs/lib/write-file.js +3 -8
  17. package/dist/node_modules/@npmcli/fs/package.json +21 -9
  18. package/dist/node_modules/@npmcli/move-file/{index.js → lib/index.js} +50 -27
  19. package/dist/node_modules/@npmcli/move-file/package.json +20 -7
  20. package/dist/node_modules/@tootallnate/once/LICENSE +21 -0
  21. package/dist/node_modules/@tootallnate/once/dist/index.js +21 -36
  22. package/dist/node_modules/@tootallnate/once/dist/index.js.map +1 -1
  23. package/dist/node_modules/@tootallnate/once/dist/overloaded-parameters.js +3 -0
  24. package/dist/node_modules/@tootallnate/once/dist/overloaded-parameters.js.map +1 -0
  25. package/dist/node_modules/@tootallnate/once/dist/types.js +3 -0
  26. package/dist/node_modules/@tootallnate/once/dist/types.js.map +1 -0
  27. package/dist/node_modules/@tootallnate/once/package.json +22 -15
  28. package/dist/node_modules/cacache/lib/content/read.js +99 -102
  29. package/dist/node_modules/cacache/lib/content/rm.js +9 -8
  30. package/dist/node_modules/cacache/lib/content/write.js +67 -67
  31. package/dist/node_modules/cacache/lib/entry-index.js +128 -118
  32. package/dist/node_modules/cacache/{get.js → lib/get.js} +88 -100
  33. package/dist/node_modules/cacache/{index.js → lib/index.js} +5 -6
  34. package/dist/node_modules/cacache/lib/memoization.js +10 -11
  35. package/dist/node_modules/cacache/{put.js → lib/put.js} +23 -26
  36. package/dist/node_modules/cacache/{rm.js → lib/rm.js} +3 -3
  37. package/dist/node_modules/cacache/lib/util/fix-owner.js +41 -38
  38. package/dist/node_modules/cacache/lib/util/move-file.js +36 -47
  39. package/dist/node_modules/cacache/lib/util/tmp.js +5 -7
  40. package/dist/node_modules/cacache/lib/verify.js +160 -190
  41. package/dist/node_modules/cacache/node_modules/brace-expansion/.github/FUNDING.yml +2 -0
  42. package/dist/node_modules/cacache/node_modules/brace-expansion/LICENSE +21 -0
  43. package/dist/node_modules/cacache/node_modules/brace-expansion/index.js +203 -0
  44. package/dist/node_modules/cacache/node_modules/brace-expansion/package.json +46 -0
  45. package/dist/node_modules/cacache/node_modules/glob/LICENSE +15 -0
  46. package/dist/node_modules/cacache/node_modules/glob/common.js +240 -0
  47. package/dist/node_modules/cacache/node_modules/glob/glob.js +790 -0
  48. package/dist/node_modules/cacache/node_modules/glob/package.json +55 -0
  49. package/dist/node_modules/cacache/node_modules/glob/sync.js +486 -0
  50. package/dist/node_modules/cacache/node_modules/minimatch/LICENSE +15 -0
  51. package/dist/node_modules/cacache/node_modules/minimatch/lib/path.js +4 -0
  52. package/dist/node_modules/cacache/node_modules/minimatch/minimatch.js +906 -0
  53. package/dist/node_modules/cacache/node_modules/minimatch/package.json +32 -0
  54. package/dist/node_modules/cacache/package.json +34 -30
  55. package/dist/node_modules/http-proxy-agent/dist/agent.js +3 -3
  56. package/dist/node_modules/http-proxy-agent/dist/agent.js.map +1 -1
  57. package/dist/node_modules/http-proxy-agent/package.json +4 -4
  58. package/dist/node_modules/lru-cache/LICENSE +1 -1
  59. package/dist/node_modules/lru-cache/index.js +921 -247
  60. package/dist/node_modules/lru-cache/package.json +49 -9
  61. package/dist/node_modules/make-fetch-happen/LICENSE +1 -1
  62. package/dist/node_modules/make-fetch-happen/lib/agent.js +34 -14
  63. package/dist/node_modules/make-fetch-happen/lib/cache/entry.js +90 -106
  64. package/dist/node_modules/make-fetch-happen/lib/cache/errors.js +1 -0
  65. package/dist/node_modules/make-fetch-happen/lib/cache/index.js +10 -6
  66. package/dist/node_modules/make-fetch-happen/lib/cache/policy.js +21 -21
  67. package/dist/node_modules/make-fetch-happen/lib/dns.js +49 -0
  68. package/dist/node_modules/make-fetch-happen/lib/fetch.js +40 -22
  69. package/dist/node_modules/make-fetch-happen/lib/index.js +4 -3
  70. package/dist/node_modules/make-fetch-happen/lib/options.js +17 -9
  71. package/dist/node_modules/make-fetch-happen/lib/pipeline.js +41 -0
  72. package/dist/node_modules/make-fetch-happen/lib/remote.js +28 -9
  73. package/dist/node_modules/make-fetch-happen/package.json +36 -33
  74. package/dist/node_modules/minipass-fetch/lib/blob.js +4 -4
  75. package/dist/node_modules/minipass-fetch/lib/body.js +63 -49
  76. package/dist/node_modules/minipass-fetch/lib/fetch-error.js +2 -1
  77. package/dist/node_modules/minipass-fetch/lib/headers.js +38 -21
  78. package/dist/node_modules/minipass-fetch/lib/index.js +130 -106
  79. package/dist/node_modules/minipass-fetch/lib/request.js +46 -28
  80. package/dist/node_modules/minipass-fetch/lib/response.js +3 -2
  81. package/dist/node_modules/minipass-fetch/package.json +27 -14
  82. package/dist/node_modules/node-gyp/.github/workflows/release-please.yml +1 -1
  83. package/dist/node_modules/node-gyp/.github/workflows/tests.yml +16 -9
  84. package/dist/node_modules/node-gyp/.github/workflows/visual-studio.yml +16 -8
  85. package/dist/node_modules/node-gyp/lib/build.js +7 -0
  86. package/dist/node_modules/node-gyp/lib/configure.js +26 -1
  87. package/dist/node_modules/node-gyp/lib/create-config-gypi.js +2 -1
  88. package/dist/node_modules/node-gyp/lib/find-visualstudio.js +9 -8
  89. package/dist/node_modules/node-gyp/lib/node-gyp.js +4 -0
  90. package/dist/node_modules/node-gyp/package.json +4 -4
  91. package/dist/node_modules/semver/node_modules/lru-cache/LICENSE +15 -0
  92. package/dist/node_modules/semver/node_modules/lru-cache/index.js +334 -0
  93. package/dist/node_modules/semver/node_modules/lru-cache/package.json +34 -0
  94. package/dist/node_modules/socks-proxy-agent/dist/index.js +3 -3
  95. package/dist/node_modules/socks-proxy-agent/dist/index.js.map +1 -1
  96. package/dist/node_modules/socks-proxy-agent/package.json +2 -2
  97. package/dist/node_modules/ssri/{index.js → lib/index.js} +78 -24
  98. package/dist/node_modules/ssri/package.json +27 -16
  99. package/dist/pnpm.cjs +67042 -65886
  100. package/package.json +6 -6
  101. package/dist/node_modules/@npmcli/fs/lib/common/file-url-to-path/index.js +0 -17
  102. package/dist/node_modules/@npmcli/fs/lib/common/file-url-to-path/polyfill.js +0 -121
  103. package/dist/node_modules/@npmcli/fs/lib/mkdir/index.js +0 -32
  104. package/dist/node_modules/@npmcli/fs/lib/mkdir/polyfill.js +0 -81
  105. package/dist/node_modules/cacache/lib/util/disposer.js +0 -30
  106. package/dist/node_modules/cacache/ls.js +0 -6
  107. package/dist/node_modules/cacache/verify.js +0 -3
  108. package/dist/node_modules/minipass-fetch/index.js +0 -1
@@ -5,7 +5,7 @@ const util = require('util')
5
5
  const pMap = require('p-map')
6
6
  const contentPath = require('./content/path')
7
7
  const fixOwner = require('./util/fix-owner')
8
- const fs = require('fs')
8
+ const fs = require('@npmcli/fs')
9
9
  const fsm = require('fs-minipass')
10
10
  const glob = util.promisify(require('glob'))
11
11
  const index = require('./entry-index')
@@ -13,14 +13,11 @@ const path = require('path')
13
13
  const rimraf = util.promisify(require('rimraf'))
14
14
  const ssri = require('ssri')
15
15
 
16
+ const globify = pattern => pattern.split('\\').join('/')
17
+
16
18
  const hasOwnProperty = (obj, key) =>
17
19
  Object.prototype.hasOwnProperty.call(obj, key)
18
20
 
19
- const stat = util.promisify(fs.stat)
20
- const truncate = util.promisify(fs.truncate)
21
- const writeFile = util.promisify(fs.writeFile)
22
- const readFile = util.promisify(fs.readFile)
23
-
24
21
  const verifyOpts = (opts) => ({
25
22
  concurrency: 20,
26
23
  log: { silly () {} },
@@ -29,7 +26,7 @@ const verifyOpts = (opts) => ({
29
26
 
30
27
  module.exports = verify
31
28
 
32
- function verify (cache, opts) {
29
+ async function verify (cache, opts) {
33
30
  opts = verifyOpts(opts)
34
31
  opts.log.silly('verify', 'verifying cache at', cache)
35
32
 
@@ -43,55 +40,47 @@ function verify (cache, opts) {
43
40
  markEndTime,
44
41
  ]
45
42
 
46
- return steps
47
- .reduce((promise, step, i) => {
48
- const label = step.name
49
- const start = new Date()
50
- return promise.then((stats) => {
51
- return step(cache, opts).then((s) => {
52
- s &&
53
- Object.keys(s).forEach((k) => {
54
- stats[k] = s[k]
55
- })
56
- const end = new Date()
57
- if (!stats.runTime)
58
- stats.runTime = {}
59
-
60
- stats.runTime[label] = end - start
61
- return Promise.resolve(stats)
62
- })
43
+ const stats = {}
44
+ for (const step of steps) {
45
+ const label = step.name
46
+ const start = new Date()
47
+ const s = await step(cache, opts)
48
+ if (s) {
49
+ Object.keys(s).forEach((k) => {
50
+ stats[k] = s[k]
63
51
  })
64
- }, Promise.resolve({}))
65
- .then((stats) => {
66
- stats.runTime.total = stats.endTime - stats.startTime
67
- opts.log.silly(
68
- 'verify',
69
- 'verification finished for',
70
- cache,
71
- 'in',
72
- `${stats.runTime.total}ms`
73
- )
74
- return stats
75
- })
52
+ }
53
+ const end = new Date()
54
+ if (!stats.runTime) {
55
+ stats.runTime = {}
56
+ }
57
+ stats.runTime[label] = end - start
58
+ }
59
+ stats.runTime.total = stats.endTime - stats.startTime
60
+ opts.log.silly(
61
+ 'verify',
62
+ 'verification finished for',
63
+ cache,
64
+ 'in',
65
+ `${stats.runTime.total}ms`
66
+ )
67
+ return stats
76
68
  }
77
69
 
78
- function markStartTime (cache, opts) {
79
- return Promise.resolve({ startTime: new Date() })
70
+ async function markStartTime (cache, opts) {
71
+ return { startTime: new Date() }
80
72
  }
81
73
 
82
- function markEndTime (cache, opts) {
83
- return Promise.resolve({ endTime: new Date() })
74
+ async function markEndTime (cache, opts) {
75
+ return { endTime: new Date() }
84
76
  }
85
77
 
86
- function fixPerms (cache, opts) {
78
+ async function fixPerms (cache, opts) {
87
79
  opts.log.silly('verify', 'fixing cache permissions')
88
- return fixOwner
89
- .mkdirfix(cache, cache)
90
- .then(() => {
91
- // TODO - fix file permissions too
92
- return fixOwner.chownr(cache, cache)
93
- })
94
- .then(() => null)
80
+ await fixOwner.mkdirfix(cache, cache)
81
+ // TODO - fix file permissions too
82
+ await fixOwner.chownr(cache, cache)
83
+ return null
95
84
  }
96
85
 
97
86
  // Implements a naive mark-and-sweep tracing garbage collector.
@@ -103,164 +92,146 @@ function fixPerms (cache, opts) {
103
92
  // 4. If content is live, verify its checksum and delete it if it fails
104
93
  // 5. If content is not marked as live, rimraf it.
105
94
  //
106
- function garbageCollect (cache, opts) {
95
+ async function garbageCollect (cache, opts) {
107
96
  opts.log.silly('verify', 'garbage collecting content')
108
97
  const indexStream = index.lsStream(cache)
109
98
  const liveContent = new Set()
110
99
  indexStream.on('data', (entry) => {
111
- if (opts.filter && !opts.filter(entry))
100
+ if (opts.filter && !opts.filter(entry)) {
112
101
  return
102
+ }
113
103
 
114
104
  liveContent.add(entry.integrity.toString())
115
105
  })
116
- return new Promise((resolve, reject) => {
106
+ await new Promise((resolve, reject) => {
117
107
  indexStream.on('end', resolve).on('error', reject)
118
- }).then(() => {
119
- const contentDir = contentPath.contentDir(cache)
120
- return glob(path.join(contentDir, '**'), {
121
- follow: false,
122
- nodir: true,
123
- nosort: true,
124
- }).then((files) => {
125
- return Promise.resolve({
126
- verifiedContent: 0,
127
- reclaimedCount: 0,
128
- reclaimedSize: 0,
129
- badContentCount: 0,
130
- keptSize: 0,
131
- }).then((stats) =>
132
- pMap(
133
- files,
134
- (f) => {
135
- const split = f.split(/[/\\]/)
136
- const digest = split.slice(split.length - 3).join('')
137
- const algo = split[split.length - 4]
138
- const integrity = ssri.fromHex(digest, algo)
139
- if (liveContent.has(integrity.toString())) {
140
- return verifyContent(f, integrity).then((info) => {
141
- if (!info.valid) {
142
- stats.reclaimedCount++
143
- stats.badContentCount++
144
- stats.reclaimedSize += info.size
145
- } else {
146
- stats.verifiedContent++
147
- stats.keptSize += info.size
148
- }
149
- return stats
150
- })
151
- } else {
152
- // No entries refer to this content. We can delete.
153
- stats.reclaimedCount++
154
- return stat(f).then((s) => {
155
- return rimraf(f).then(() => {
156
- stats.reclaimedSize += s.size
157
- return stats
158
- })
159
- })
160
- }
161
- },
162
- { concurrency: opts.concurrency }
163
- ).then(() => stats)
164
- )
165
- })
166
108
  })
167
- }
168
-
169
- function verifyContent (filepath, sri) {
170
- return stat(filepath)
171
- .then((s) => {
172
- const contentInfo = {
173
- size: s.size,
174
- valid: true,
109
+ const contentDir = contentPath.contentDir(cache)
110
+ const files = await glob(globify(path.join(contentDir, '**')), {
111
+ follow: false,
112
+ nodir: true,
113
+ nosort: true,
114
+ })
115
+ const stats = {
116
+ verifiedContent: 0,
117
+ reclaimedCount: 0,
118
+ reclaimedSize: 0,
119
+ badContentCount: 0,
120
+ keptSize: 0,
121
+ }
122
+ await pMap(
123
+ files,
124
+ async (f) => {
125
+ const split = f.split(/[/\\]/)
126
+ const digest = split.slice(split.length - 3).join('')
127
+ const algo = split[split.length - 4]
128
+ const integrity = ssri.fromHex(digest, algo)
129
+ if (liveContent.has(integrity.toString())) {
130
+ const info = await verifyContent(f, integrity)
131
+ if (!info.valid) {
132
+ stats.reclaimedCount++
133
+ stats.badContentCount++
134
+ stats.reclaimedSize += info.size
135
+ } else {
136
+ stats.verifiedContent++
137
+ stats.keptSize += info.size
138
+ }
139
+ } else {
140
+ // No entries refer to this content. We can delete.
141
+ stats.reclaimedCount++
142
+ const s = await fs.stat(f)
143
+ await rimraf(f)
144
+ stats.reclaimedSize += s.size
175
145
  }
176
- return ssri
177
- .checkStream(new fsm.ReadStream(filepath), sri)
178
- .catch((err) => {
179
- if (err.code !== 'EINTEGRITY')
180
- throw err
181
-
182
- return rimraf(filepath).then(() => {
183
- contentInfo.valid = false
184
- })
185
- })
186
- .then(() => contentInfo)
187
- })
188
- .catch((err) => {
189
- if (err.code === 'ENOENT')
190
- return { size: 0, valid: false }
146
+ return stats
147
+ },
148
+ { concurrency: opts.concurrency }
149
+ )
150
+ return stats
151
+ }
191
152
 
153
+ async function verifyContent (filepath, sri) {
154
+ const contentInfo = {}
155
+ try {
156
+ const { size } = await fs.stat(filepath)
157
+ contentInfo.size = size
158
+ contentInfo.valid = true
159
+ await ssri.checkStream(new fsm.ReadStream(filepath), sri)
160
+ } catch (err) {
161
+ if (err.code === 'ENOENT') {
162
+ return { size: 0, valid: false }
163
+ }
164
+ if (err.code !== 'EINTEGRITY') {
192
165
  throw err
193
- })
166
+ }
167
+
168
+ await rimraf(filepath)
169
+ contentInfo.valid = false
170
+ }
171
+ return contentInfo
194
172
  }
195
173
 
196
- function rebuildIndex (cache, opts) {
174
+ async function rebuildIndex (cache, opts) {
197
175
  opts.log.silly('verify', 'rebuilding index')
198
- return index.ls(cache).then((entries) => {
199
- const stats = {
200
- missingContent: 0,
201
- rejectedEntries: 0,
202
- totalEntries: 0,
203
- }
204
- const buckets = {}
205
- for (const k in entries) {
206
- /* istanbul ignore else */
207
- if (hasOwnProperty(entries, k)) {
208
- const hashed = index.hashKey(k)
209
- const entry = entries[k]
210
- const excluded = opts.filter && !opts.filter(entry)
211
- excluded && stats.rejectedEntries++
212
- if (buckets[hashed] && !excluded)
213
- buckets[hashed].push(entry)
214
- else if (buckets[hashed] && excluded) {
215
- // skip
216
- } else if (excluded) {
217
- buckets[hashed] = []
218
- buckets[hashed]._path = index.bucketPath(cache, k)
219
- } else {
220
- buckets[hashed] = [entry]
221
- buckets[hashed]._path = index.bucketPath(cache, k)
222
- }
176
+ const entries = await index.ls(cache)
177
+ const stats = {
178
+ missingContent: 0,
179
+ rejectedEntries: 0,
180
+ totalEntries: 0,
181
+ }
182
+ const buckets = {}
183
+ for (const k in entries) {
184
+ /* istanbul ignore else */
185
+ if (hasOwnProperty(entries, k)) {
186
+ const hashed = index.hashKey(k)
187
+ const entry = entries[k]
188
+ const excluded = opts.filter && !opts.filter(entry)
189
+ excluded && stats.rejectedEntries++
190
+ if (buckets[hashed] && !excluded) {
191
+ buckets[hashed].push(entry)
192
+ } else if (buckets[hashed] && excluded) {
193
+ // skip
194
+ } else if (excluded) {
195
+ buckets[hashed] = []
196
+ buckets[hashed]._path = index.bucketPath(cache, k)
197
+ } else {
198
+ buckets[hashed] = [entry]
199
+ buckets[hashed]._path = index.bucketPath(cache, k)
223
200
  }
224
201
  }
225
- return pMap(
226
- Object.keys(buckets),
227
- (key) => {
228
- return rebuildBucket(cache, buckets[key], stats, opts)
229
- },
230
- { concurrency: opts.concurrency }
231
- ).then(() => stats)
232
- })
202
+ }
203
+ await pMap(
204
+ Object.keys(buckets),
205
+ (key) => {
206
+ return rebuildBucket(cache, buckets[key], stats, opts)
207
+ },
208
+ { concurrency: opts.concurrency }
209
+ )
210
+ return stats
233
211
  }
234
212
 
235
- function rebuildBucket (cache, bucket, stats, opts) {
236
- return truncate(bucket._path).then(() => {
237
- // This needs to be serialized because cacache explicitly
238
- // lets very racy bucket conflicts clobber each other.
239
- return bucket.reduce((promise, entry) => {
240
- return promise.then(() => {
241
- const content = contentPath(cache, entry.integrity)
242
- return stat(content)
243
- .then(() => {
244
- return index
245
- .insert(cache, entry.key, entry.integrity, {
246
- metadata: entry.metadata,
247
- size: entry.size,
248
- })
249
- .then(() => {
250
- stats.totalEntries++
251
- })
252
- })
253
- .catch((err) => {
254
- if (err.code === 'ENOENT') {
255
- stats.rejectedEntries++
256
- stats.missingContent++
257
- return
258
- }
259
- throw err
260
- })
213
+ async function rebuildBucket (cache, bucket, stats, opts) {
214
+ await fs.truncate(bucket._path)
215
+ // This needs to be serialized because cacache explicitly
216
+ // lets very racy bucket conflicts clobber each other.
217
+ for (const entry of bucket) {
218
+ const content = contentPath(cache, entry.integrity)
219
+ try {
220
+ await fs.stat(content)
221
+ await index.insert(cache, entry.key, entry.integrity, {
222
+ metadata: entry.metadata,
223
+ size: entry.size,
261
224
  })
262
- }, Promise.resolve())
263
- })
225
+ stats.totalEntries++
226
+ } catch (err) {
227
+ if (err.code === 'ENOENT') {
228
+ stats.rejectedEntries++
229
+ stats.missingContent++
230
+ } else {
231
+ throw err
232
+ }
233
+ }
234
+ }
264
235
  }
265
236
 
266
237
  function cleanTmp (cache, opts) {
@@ -272,7 +243,7 @@ function writeVerifile (cache, opts) {
272
243
  const verifile = path.join(cache, '_lastverified')
273
244
  opts.log.silly('verify', 'writing verifile to ' + verifile)
274
245
  try {
275
- return writeFile(verifile, '' + +new Date())
246
+ return fs.writeFile(verifile, `${Date.now()}`)
276
247
  } finally {
277
248
  fixOwner.chownr.sync(cache, verifile)
278
249
  }
@@ -280,8 +251,7 @@ function writeVerifile (cache, opts) {
280
251
 
281
252
  module.exports.lastRun = lastRun
282
253
 
283
- function lastRun (cache) {
284
- return readFile(path.join(cache, '_lastverified'), 'utf8').then(
285
- (data) => new Date(+data)
286
- )
254
+ async function lastRun (cache) {
255
+ const data = await fs.readFile(path.join(cache, '_lastverified'), { encoding: 'utf8' })
256
+ return new Date(+data)
287
257
  }
@@ -0,0 +1,2 @@
1
+ tidelift: "npm/brace-expansion"
2
+ patreon: juliangruber
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2013 Julian Gruber <julian@juliangruber.com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,203 @@
1
+ var balanced = require('balanced-match');
2
+
3
+ module.exports = expandTop;
4
+
5
+ var escSlash = '\0SLASH'+Math.random()+'\0';
6
+ var escOpen = '\0OPEN'+Math.random()+'\0';
7
+ var escClose = '\0CLOSE'+Math.random()+'\0';
8
+ var escComma = '\0COMMA'+Math.random()+'\0';
9
+ var escPeriod = '\0PERIOD'+Math.random()+'\0';
10
+
11
+ function numeric(str) {
12
+ return parseInt(str, 10) == str
13
+ ? parseInt(str, 10)
14
+ : str.charCodeAt(0);
15
+ }
16
+
17
+ function escapeBraces(str) {
18
+ return str.split('\\\\').join(escSlash)
19
+ .split('\\{').join(escOpen)
20
+ .split('\\}').join(escClose)
21
+ .split('\\,').join(escComma)
22
+ .split('\\.').join(escPeriod);
23
+ }
24
+
25
+ function unescapeBraces(str) {
26
+ return str.split(escSlash).join('\\')
27
+ .split(escOpen).join('{')
28
+ .split(escClose).join('}')
29
+ .split(escComma).join(',')
30
+ .split(escPeriod).join('.');
31
+ }
32
+
33
+
34
+ // Basically just str.split(","), but handling cases
35
+ // where we have nested braced sections, which should be
36
+ // treated as individual members, like {a,{b,c},d}
37
+ function parseCommaParts(str) {
38
+ if (!str)
39
+ return [''];
40
+
41
+ var parts = [];
42
+ var m = balanced('{', '}', str);
43
+
44
+ if (!m)
45
+ return str.split(',');
46
+
47
+ var pre = m.pre;
48
+ var body = m.body;
49
+ var post = m.post;
50
+ var p = pre.split(',');
51
+
52
+ p[p.length-1] += '{' + body + '}';
53
+ var postParts = parseCommaParts(post);
54
+ if (post.length) {
55
+ p[p.length-1] += postParts.shift();
56
+ p.push.apply(p, postParts);
57
+ }
58
+
59
+ parts.push.apply(parts, p);
60
+
61
+ return parts;
62
+ }
63
+
64
+ function expandTop(str) {
65
+ if (!str)
66
+ return [];
67
+
68
+ // I don't know why Bash 4.3 does this, but it does.
69
+ // Anything starting with {} will have the first two bytes preserved
70
+ // but *only* at the top level, so {},a}b will not expand to anything,
71
+ // but a{},b}c will be expanded to [a}c,abc].
72
+ // One could argue that this is a bug in Bash, but since the goal of
73
+ // this module is to match Bash's rules, we escape a leading {}
74
+ if (str.substr(0, 2) === '{}') {
75
+ str = '\\{\\}' + str.substr(2);
76
+ }
77
+
78
+ return expand(escapeBraces(str), true).map(unescapeBraces);
79
+ }
80
+
81
+ function embrace(str) {
82
+ return '{' + str + '}';
83
+ }
84
+ function isPadded(el) {
85
+ return /^-?0\d/.test(el);
86
+ }
87
+
88
+ function lte(i, y) {
89
+ return i <= y;
90
+ }
91
+ function gte(i, y) {
92
+ return i >= y;
93
+ }
94
+
95
+ function expand(str, isTop) {
96
+ var expansions = [];
97
+
98
+ var m = balanced('{', '}', str);
99
+ if (!m) return [str];
100
+
101
+ // no need to expand pre, since it is guaranteed to be free of brace-sets
102
+ var pre = m.pre;
103
+ var post = m.post.length
104
+ ? expand(m.post, false)
105
+ : [''];
106
+
107
+ if (/\$$/.test(m.pre)) {
108
+ for (var k = 0; k < post.length; k++) {
109
+ var expansion = pre+ '{' + m.body + '}' + post[k];
110
+ expansions.push(expansion);
111
+ }
112
+ } else {
113
+ var isNumericSequence = /^-?\d+\.\.-?\d+(?:\.\.-?\d+)?$/.test(m.body);
114
+ var isAlphaSequence = /^[a-zA-Z]\.\.[a-zA-Z](?:\.\.-?\d+)?$/.test(m.body);
115
+ var isSequence = isNumericSequence || isAlphaSequence;
116
+ var isOptions = m.body.indexOf(',') >= 0;
117
+ if (!isSequence && !isOptions) {
118
+ // {a},b}
119
+ if (m.post.match(/,.*\}/)) {
120
+ str = m.pre + '{' + m.body + escClose + m.post;
121
+ return expand(str);
122
+ }
123
+ return [str];
124
+ }
125
+
126
+ var n;
127
+ if (isSequence) {
128
+ n = m.body.split(/\.\./);
129
+ } else {
130
+ n = parseCommaParts(m.body);
131
+ if (n.length === 1) {
132
+ // x{{a,b}}y ==> x{a}y x{b}y
133
+ n = expand(n[0], false).map(embrace);
134
+ if (n.length === 1) {
135
+ return post.map(function(p) {
136
+ return m.pre + n[0] + p;
137
+ });
138
+ }
139
+ }
140
+ }
141
+
142
+ // at this point, n is the parts, and we know it's not a comma set
143
+ // with a single entry.
144
+ var N;
145
+
146
+ if (isSequence) {
147
+ var x = numeric(n[0]);
148
+ var y = numeric(n[1]);
149
+ var width = Math.max(n[0].length, n[1].length)
150
+ var incr = n.length == 3
151
+ ? Math.abs(numeric(n[2]))
152
+ : 1;
153
+ var test = lte;
154
+ var reverse = y < x;
155
+ if (reverse) {
156
+ incr *= -1;
157
+ test = gte;
158
+ }
159
+ var pad = n.some(isPadded);
160
+
161
+ N = [];
162
+
163
+ for (var i = x; test(i, y); i += incr) {
164
+ var c;
165
+ if (isAlphaSequence) {
166
+ c = String.fromCharCode(i);
167
+ if (c === '\\')
168
+ c = '';
169
+ } else {
170
+ c = String(i);
171
+ if (pad) {
172
+ var need = width - c.length;
173
+ if (need > 0) {
174
+ var z = new Array(need + 1).join('0');
175
+ if (i < 0)
176
+ c = '-' + z + c.slice(1);
177
+ else
178
+ c = z + c;
179
+ }
180
+ }
181
+ }
182
+ N.push(c);
183
+ }
184
+ } else {
185
+ N = [];
186
+
187
+ for (var j = 0; j < n.length; j++) {
188
+ N.push.apply(N, expand(n[j], false));
189
+ }
190
+ }
191
+
192
+ for (var j = 0; j < N.length; j++) {
193
+ for (var k = 0; k < post.length; k++) {
194
+ var expansion = pre + N[j] + post[k];
195
+ if (!isTop || isSequence || expansion)
196
+ expansions.push(expansion);
197
+ }
198
+ }
199
+ }
200
+
201
+ return expansions;
202
+ }
203
+