Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v1: Cut mksnapshot, update dependencies, promisify #165

Merged
merged 17 commits into from
Feb 19, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/asar.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ module.exports.createPackageFromFiles = function (src, dest, filenames, metadata
shouldUnpack = isUnpackedDir(dirName, options.unpackDir, unpackDirs)
}
files.push({ filename: filename, unpack: shouldUnpack })
filesystem.insertFile(filename, shouldUnpack, file, options, done)
return
return filesystem.insertFile(filename, shouldUnpack, file, options)
.then(done).catch(done)
felixrieseberg marked this conversation as resolved.
Show resolved Hide resolved
case 'link':
filesystem.insertLink(filename, file.stat)
break
Expand Down
50 changes: 29 additions & 21 deletions lib/filesystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const path = require('path')
const tmp = require('tmp-promise')
const UINT64 = require('cuint').UINT64

const UINT32_MAX = 4294967295

class Filesystem {
constructor (src) {
this.src = path.resolve(src)
Expand Down Expand Up @@ -48,22 +50,26 @@ class Filesystem {
return node.files
}

insertFile (p, shouldUnpack, file, options, callback) {
insertFile (p, shouldUnpack, file, options) {
const dirNode = this.searchNodeFromPath(path.dirname(p))
const node = this.searchNodeFromPath(p)
if (shouldUnpack || dirNode.unpacked) {
node.size = file.stat.size
node.unpacked = true
process.nextTick(callback)
return
return Promise.resolve()
}

const handler = () => {
const handler = (resolve, reject) => {
const size = file.transformed ? file.transformed.stat.size : file.stat.size

// JavaScript can not precisely present integers >= UINT32_MAX.
if (size > 4294967295) {
throw new Error(`${p}: file size can not be larger than 4.2GB`)
if (size > UINT32_MAX) {
const error = new Error(`${p}: file size can not be larger than 4.2GB`)
if (reject) {
return reject(error)
} else {
throw error
}
}

node.size = size
Expand All @@ -73,30 +79,32 @@ class Filesystem {
}
this.offset.add(UINT64(size))

return callback()
return resolve ? resolve() : Promise.resolve()
}

const transformed = options.transform && options.transform(p)
if (transformed) {
return tmp.file()
.then(tmpfile => {
const out = fs.createWriteStream(tmpfile.path)
const stream = fs.createReadStream(p)

stream.pipe(transformed).pipe(out)
return out.on('close', () => {
return fs.lstat(tmpfile.path)
.then(stat => {
file.transformed = {
path: tmpfile.path,
stat
}
handler()
})
return new Promise((resolve, reject) => {
const out = fs.createWriteStream(tmpfile.path)
const stream = fs.createReadStream(p)

stream.pipe(transformed).pipe(out)
return out.on('close', () => {
return fs.lstat(tmpfile.path)
.then(stat => {
file.transformed = {
path: tmpfile.path,
stat
}
return handler(resolve, reject)
})
})
})
})
} else {
return process.nextTick(handler)
return handler()
}
}

Expand Down