summaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/tar/lib/replace.js
diff options
context:
space:
mode:
Diffstat (limited to 'deps/npm/node_modules/tar/lib/replace.js')
-rw-r--r--deps/npm/node_modules/tar/lib/replace.js40
1 files changed, 24 insertions, 16 deletions
diff --git a/deps/npm/node_modules/tar/lib/replace.js b/deps/npm/node_modules/tar/lib/replace.js
index aac6b57fa8..44126d1f85 100644
--- a/deps/npm/node_modules/tar/lib/replace.js
+++ b/deps/npm/node_modules/tar/lib/replace.js
@@ -5,6 +5,7 @@ const hlo = require('./high-level-opt.js')
const Pack = require('./pack.js')
const Parse = require('./parse.js')
const fs = require('fs')
+const fsm = require('fs-minipass')
const t = require('./list.js')
const path = require('path')
@@ -39,6 +40,8 @@ const replaceSync = (opt, files) => {
let threw = true
let fd
+ let position
+
try {
try {
fd = fs.openSync(opt.file, 'r+')
@@ -51,7 +54,6 @@ const replaceSync = (opt, files) => {
const st = fs.fstatSync(fd)
const headBuf = Buffer.alloc(512)
- let position
POSITION: for (position = 0; position < st.size; position += 512) {
for (let bufPos = 0, bytes = 0; bufPos < 512; bufPos += bytes) {
@@ -78,21 +80,24 @@ const replaceSync = (opt, files) => {
if (opt.mtimeCache)
opt.mtimeCache.set(h.path, h.mtime)
}
-
- p.on('data', c => {
- fs.writeSync(fd, c, 0, c.length, position)
- position += c.length
- })
- p.on('end', _ => fs.closeSync(fd))
-
- addFilesSync(p, files)
threw = false
+
+ streamSync(opt, p, position, fd, files)
} finally {
if (threw)
try { fs.closeSync(fd) } catch (er) {}
}
}
+const streamSync = (opt, p, position, fd, files) => {
+ const stream = new fsm.WriteStreamSync(opt.file, {
+ fd: fd,
+ start: position
+ })
+ p.pipe(stream)
+ addFilesSync(p, files)
+}
+
const replace = (opt, files, cb) => {
files = Array.from(files)
const p = new Pack(opt)
@@ -150,21 +155,24 @@ const replace = (opt, files, cb) => {
const promise = new Promise((resolve, reject) => {
p.on('error', reject)
+ let flag = 'r+'
const onopen = (er, fd) => {
- if (er) {
- if (er.code === 'ENOENT')
- return fs.open(opt.file, 'w+', onopen)
- return reject(er)
+ if (er && er.code === 'ENOENT' && flag === 'r+') {
+ flag = 'w+'
+ return fs.open(opt.file, flag, onopen)
}
+
+ if (er)
+ return reject(er)
+
fs.fstat(fd, (er, st) => {
if (er)
return reject(er)
getPos(fd, st.size, (er, position) => {
if (er)
return reject(er)
- const stream = fs.createWriteStream(opt.file, {
+ const stream = new fsm.WriteStream(opt.file, {
fd: fd,
- flags: 'r+',
start: position
})
p.pipe(stream)
@@ -174,7 +182,7 @@ const replace = (opt, files, cb) => {
})
})
}
- fs.open(opt.file, 'r+', onopen)
+ fs.open(opt.file, flag, onopen)
})
return cb ? promise.then(cb, cb) : promise