Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
toyobayashi committed Jan 18, 2022
1 parent 8d1a28b commit afe0316
Show file tree
Hide file tree
Showing 11 changed files with 23 additions and 71 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/dist
/spec
/lib/asar/pickle.js
9 changes: 6 additions & 3 deletions lib/asar.js → lib/asar/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// Extract from asar 2.0.3
const path = require('path')
const fs = require('./_fs.js')
const fs = require('../fs/index.js')
const pickle = require('./pickle')
const openSync = fs.openSync
const readSync = fs.readSync
const closeSync = fs.closeSync

const kSeparators = process.platform === 'win32' ? /[\\/]/ : /\//

class Filesystem {
constructor (src) {
this.src = path.resolve(src)
Expand All @@ -16,7 +18,7 @@ class Filesystem {

searchNodeFromDirectory (p) {
let json = this.header
const dirs = p.split(path.sep)
const dirs = p.split(kSeparators)
for (const dir of dirs) {
if (dir !== '.') {
json = json.files[dir]
Expand Down Expand Up @@ -51,7 +53,7 @@ class Filesystem {
followLinks = typeof followLinks === 'undefined' ? true : followLinks
let json = this.header
if (!p) return json
const dirs = p.split(path.sep)
const dirs = p.split(kSeparators)
for (let i = 0; i < dirs.length; ++i) {
const dir = dirs[i]
if (dir !== '.') {
Expand Down Expand Up @@ -153,3 +155,4 @@ exports.disk = {
readFilesystemSync,
readFileSync
}
exports.Filesystem = Filesystem
File renamed without changes.
2 changes: 1 addition & 1 deletion lib/child-process.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const path = require('path')
const util = require('util')
const asar = require('./asar.js')
const asar = require('./asar/index.js')
const { isAsarDisabled, splitPath, createError, AsarError, copyFileOut, invokeWithNoAsar } = require('./util.js')
const childProcess = require('child_process')

Expand Down
70 changes: 8 additions & 62 deletions lib/fs.js → lib/fs-wrapper.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
const fs = require('./_fs.js')
const asar = require('./asar.js')
const fs = require('./fs/index.js')
const asar = require('./asar/index.js')
const path = require('path')
const util = require('util')
const pickle = require('./pickle')
const { splitPath, assertCallback, AsarError, createError, copyFileOut, readFileChunk } = require('./util.js')

const originalCreateReadStream = fs.createReadStream
const originalCreateWriteStream = fs.createWriteStream
const openSync = fs.openSync
const readSync = fs.readSync
const closeSync = fs.closeSync
const oldRead = fs.read
const { Stats, constants } = fs
Expand Down Expand Up @@ -182,55 +180,6 @@ function cancel () {

const bufferToString = (maybeBuffer) => Buffer.isBuffer(maybeBuffer) ? maybeBuffer.toString() : maybeBuffer

function getHeaderSize (fd) {
const sizeBuf = Buffer.alloc(8)
if (readSync(fd, sizeBuf, 0, 8, null) !== 8) {
throw new Error('Unable to read header size')
}

const sizePickle = pickle.createFromBuffer(sizeBuf)
const headerSize = sizePickle.createIterator().readUInt32()
return headerSize
}

/* function _createReadStream (asarPath, filePath, options, stats) {
const filesystem = asar.disk.readFilesystemSync(asarPath)
if (!stats) {
stats = filesystem.getFileEx(filePath)
}
const headerSize = filesystem.headerSize
const fd = openSync(asarPath, 'r')
const defaultOption = {
fd,
autoClose: true,
start: 8 + headerSize + parseInt(stats.offset, 10),
end: 8 + headerSize + parseInt(stats.offset, 10) + stats.size - 1
}
if (Object.prototype.toString.call(options) === '[object Object]') {
if (typeof options.end === 'number') {
defaultOption.end = defaultOption.start + options.end
delete options.end
}
if (typeof options.start === 'number') {
defaultOption.start += options.start
delete options.start
}
options = Object.assign({}, defaultOption, options)
} else {
options = defaultOption
}
try {
return originalCreateReadStream('', options)
} catch (err) {
closeSync(fd)
}
} */

function overwriteFs () {
if (registered) return fs

Expand Down Expand Up @@ -660,29 +609,26 @@ function overwriteFs () {
const { isAsar, asarPath, filePath } = splitPath(path.resolve(src))
if (!isAsar || filePath === '') return copyFileSync.apply(this, arguments)

const fd = openSync(asarPath, 'r')

let headerSize
let filesystem
try {
headerSize = getHeaderSize(fd)
filesystem = asar.disk.readFilesystemSync(asarPath)
} catch (err) {
closeSync(fd)
throw err
throw createError(AsarError.INVALID_ARCHIVE, { asarPath })
}

let stats
try {
stats = asar.statFile(asarPath, filePath)
stats = filesystem.getFileEx(filePath)
} catch (err) {
closeSync(fd)
throw createError(AsarError.NOT_FOUND, { asarPath, filePath })
}

if (stats.unpacked) {
closeSync(fd)
return copyFileSync.call(this, path.join(asarPath + '.unpacked', filePath), dest, mode)
}

const headerSize = filesystem.headerSize
const fd = openSync(asarPath, 'r')
dest = bufferToString(dest)
const wfd = openSync(dest, 'w')

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion lib/original-fs.js → lib/fs/original.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const fs = require('./_fs.js')
const fs = require('./index.js')

const originalFs = {}

Expand Down
2 changes: 2 additions & 0 deletions lib/require.js → lib/fs/require.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
function getNodeRequire () {
let nativeRequire
if (typeof __webpack_public_path__ !== 'undefined') {
// eslint-disable-next-line spaced-comment
nativeRequire = /*#__PURE__*/ (function () {
return typeof __non_webpack_require__ !== 'undefined' ? __non_webpack_require__ : undefined
})()
} else {
// eslint-disable-next-line spaced-comment
nativeRequire = /*#__PURE__*/ (function () {
return typeof __webpack_public_path__ !== 'undefined'
? (typeof __non_webpack_require__ !== 'undefined' ? __non_webpack_require__ : undefined)
Expand Down
2 changes: 1 addition & 1 deletion lib/module.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const path = require('path')
const toNamespacedPath = path.toNamespacedPath
const fs = require('./_fs.js')
const fs = require('./fs/index.js')
const { tryRedirectUnpacked } = require('./util.js')

const options = new Map()
Expand Down
4 changes: 2 additions & 2 deletions lib/register.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { isAsarDisabled, getModuleConstructor, initTempDir } = require('./util.js')
const originalFs = require('./original-fs.js')
const originalFs = require('./fs/original.js')
const { overwriteChildProcess, cancel: cancelCp } = require('./child-process.js')
const { overwriteFs, cancel: cancelFs } = require('./fs.js')
const { overwriteFs, cancel: cancelFs } = require('./fs-wrapper.js')

let registered = false

Expand Down
2 changes: 1 addition & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const {
readdirSync,
unlinkSync,
rmdirSync
} = require('./_fs.js')
} = require('./fs/index.js')
const path = require('path')
const { tmpdir } = require('os')
const { createHash } = require('crypto')
Expand Down

0 comments on commit afe0316

Please sign in to comment.