Code cleanup, fix race condition, return promise on unpack
This commit is contained in:
parent
05b855a40a
commit
59cc470e1e
1 changed files with 41 additions and 34 deletions
75
index.js
75
index.js
|
@ -126,7 +126,7 @@ async function packDirectory(directory, options = {}) {
|
||||||
});
|
});
|
||||||
|
|
||||||
await writeStream(entry, contents);
|
await writeStream(entry, contents);
|
||||||
//console.log(contents)
|
|
||||||
entry.end();
|
entry.end();
|
||||||
}catch (e){console.log(e)}
|
}catch (e){console.log(e)}
|
||||||
}, directory, packRoot);
|
}, directory, packRoot);
|
||||||
|
@ -154,42 +154,49 @@ function streamToBuffer(stream) {
|
||||||
|
|
||||||
|
|
||||||
async function unpackDirectory(directory, options = {}) {
|
async function unpackDirectory(directory, options = {}) {
|
||||||
if(!fs.existsSync(directory)) {
|
return new Promise(async (resolve) => {
|
||||||
fs.mkdirSync(directory);
|
if(!fs.existsSync(directory)) {
|
||||||
}
|
fs.mkdirSync(directory);
|
||||||
|
|
||||||
const fileReadStream = getFileReadStream(options);
|
|
||||||
const zstd = await getZstd();
|
|
||||||
|
|
||||||
const extract = tar.extract();
|
|
||||||
|
|
||||||
extract.on('entry', async (header, fileStream, next) => {
|
|
||||||
let contents = await streamToBuffer(fileStream);
|
|
||||||
contents = new Uint8Array(contents);
|
|
||||||
|
|
||||||
contents = zstd.decompress(contents);
|
|
||||||
|
|
||||||
if(!/^\./.test(header.name)) {
|
|
||||||
if(header.name == 'index.html') {
|
|
||||||
console.log(String.fromCharCode.apply(null, contents))
|
|
||||||
}
|
|
||||||
const writePath = path.join(directory, header.name);
|
|
||||||
fs.promises.mkdir(path.dirname(writePath), { recursive: true });
|
|
||||||
var fileWriteStream = fs.createWriteStream(writePath);
|
|
||||||
fileWriteStream.write(contents);
|
|
||||||
fileWriteStream.end();
|
|
||||||
next();
|
|
||||||
} else {
|
|
||||||
fileStream.resume();
|
|
||||||
next();
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
extract.on('finish', () => {
|
const fileReadStream = getFileReadStream(options);
|
||||||
// all entries read
|
const zstd = await getZstd();
|
||||||
});
|
|
||||||
|
|
||||||
fileReadStream.pipe(extract);
|
const extract = tar.extract();
|
||||||
|
|
||||||
|
extract.on('entry', async (header, fileStream, next) => {
|
||||||
|
let contents = await streamToBuffer(fileStream);
|
||||||
|
contents = new Uint8Array(contents);
|
||||||
|
|
||||||
|
contents = zstd.decompress(contents);
|
||||||
|
|
||||||
|
if(!/^\./.test(header.name)) {
|
||||||
|
const writePath = path.join(directory, header.name);
|
||||||
|
|
||||||
|
var fileWriteStream;
|
||||||
|
try {
|
||||||
|
fileWriteStream = fs.createWriteStream(writePath);
|
||||||
|
} catch (e) {
|
||||||
|
// Assume directory may not exist if an error is thrown
|
||||||
|
fs.mkdirSync(path.dirname(writePath), { recursive: true });
|
||||||
|
fileWriteStream = fs.createWriteStream(writePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
fileWriteStream.write(contents);
|
||||||
|
fileWriteStream.end();
|
||||||
|
next();
|
||||||
|
} else {
|
||||||
|
fileStream.resume();
|
||||||
|
next();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
extract.on('finish', () => {
|
||||||
|
resolve(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
fileReadStream.pipe(extract);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
Loading…
Add table
Reference in a new issue