-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
57 lines (46 loc) · 1.61 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const fsp = require('fs').promises
const fs = require('fs')
const os = require('os')
const path = require('path')
const { Readable } = require('stream')
const yazl = require('yazl')
/**
* @description Creates a .zip that contains given filecontents, within given filenames. All at the zip root
* @param {{}} filesobj For instance { 'file.txt': 'abc' }
* @returns {Promise<string>} Path to the created .zip
*/
async function zip(filesobj) {
const uid = `${Math.ceil(Math.random() * 10000)}`
const zipfile = new yazl.ZipFile()
console.time(`zip-${uid}`)
// create tmp dir
const outdir = await fsp.mkdtemp(path.join(os.tmpdir(), 'zipped-'))
const outpath = path.join(outdir, 'deploypackage.zip')
zipfile.outputStream.pipe(fs.createWriteStream(outpath))
// filesobj is like { 'file.txt': 'abc', 'file2.txt': '123' }
// for each such destination file,...
const fnames = Object.keys(filesobj)
for (let i = 0; i < fnames.length; i += 1) {
const fname = fnames[i]
const fcontent = filesobj[fname]
// set up stream
const s = new Readable();
s._read = () => {};
s.push(fcontent);
s.push(null);
// In zip, set last-modified header to 01-01-2020
// this way, rezipping identical files is deterministic (gives the same codesha256)
// that way we can skip uploading zips that haven't changed
const options = {
mtime: new Date(1577836),
}
zipfile.addReadStream(s, fname, options); // place code in index.js inside zip
// console.log(`created ${fname} in zip`)
}
zipfile.end()
console.timeEnd(`zip-${uid}`)
return outpath
}
module.exports = {
zip,
}