summaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/cacache/lib/content/write.js
diff options
context:
space:
mode:
Diffstat (limited to 'deps/npm/node_modules/cacache/lib/content/write.js')
-rw-r--r--deps/npm/node_modules/cacache/lib/content/write.js36
1 files changed, 21 insertions, 15 deletions
diff --git a/deps/npm/node_modules/cacache/lib/content/write.js b/deps/npm/node_modules/cacache/lib/content/write.js
index d799ae8837..b6f5c5623b 100644
--- a/deps/npm/node_modules/cacache/lib/content/write.js
+++ b/deps/npm/node_modules/cacache/lib/content/write.js
@@ -4,8 +4,8 @@ const events = require('events')
const contentPath = require('./path')
const fs = require('fs/promises')
-const moveFile = require('../util/move-file')
-const Minipass = require('minipass')
+const { moveFile } = require('@npmcli/fs')
+const { Minipass } = require('minipass')
const Pipeline = require('minipass-pipeline')
const Flush = require('minipass-flush')
const path = require('path')
@@ -17,9 +17,6 @@ module.exports = write
async function write (cache, data, opts = {}) {
const { algorithms, size, integrity } = opts
- if (algorithms && algorithms.length > 1) {
- throw new Error('opts.algorithms only supports a single algorithm for now')
- }
if (typeof size === 'number' && data.length !== size) {
throw sizeError(size, data.length)
@@ -30,16 +27,19 @@ async function write (cache, data, opts = {}) {
throw checksumError(integrity, sri)
}
- const tmp = await makeTmp(cache, opts)
- try {
- await fs.writeFile(tmp.target, data, { flag: 'wx' })
- await moveToDestination(tmp, cache, sri, opts)
- return { integrity: sri, size: data.length }
- } finally {
- if (!tmp.moved) {
- await fs.rm(tmp.target, { recursive: true, force: true })
+ for (const algo in sri) {
+ const tmp = await makeTmp(cache, opts)
+ const hash = sri[algo].toString()
+ try {
+ await fs.writeFile(tmp.target, data, { flag: 'wx' })
+ await moveToDestination(tmp, cache, hash, opts)
+ } finally {
+ if (!tmp.moved) {
+ await fs.rm(tmp.target, { recursive: true, force: true })
+ }
}
}
+ return { integrity: sri, size: data.length }
}
module.exports.stream = writeStream
@@ -161,8 +161,14 @@ async function moveToDestination (tmp, cache, sri, opts) {
const destDir = path.dirname(destination)
await fs.mkdir(destDir, { recursive: true })
- await moveFile(tmp.target, destination)
- tmp.moved = true
+ try {
+ await moveFile(tmp.target, destination, { overwrite: false })
+ tmp.moved = true
+ } catch (err) {
+ if (!err.message.startsWith('The destination file exists')) {
+ throw Object.assign(err, { code: 'EEXIST' })
+ }
+ }
}
function sizeError (expected, found) {