fix: two windows fs patch bugs (#692)

* fix: two windows fs patch bugs

- winPath was not upcasing drive letters of namespace-prefixed keys
- internalModuleReadFile and internalModuleStat were not normalizing keys before checking the manifest for existing entries
This commit is contained in:
Roy Razon
2020-01-14 16:29:25 +02:00
committed by Caleb Boyd
parent e49d72db88
commit ec7f7f318e
+10 -10
View File
@@ -15,6 +15,12 @@ export interface NexeBinary {
let originalFsMethods: any = null
let lazyRestoreFs = () => {}
// optional Win32 file namespace prefix followed by drive letter and colon
const windowsFullPathRegex = /^(\\{2}\?\\)?([a-zA-Z]):/
const upcaseDriveLetter = (s: string): string =>
s.replace(windowsFullPathRegex, (_match, ns, drive) => `${ns || ''}${drive.toUpperCase()}:`)
function shimFs(binary: NexeBinary, fs: any = require('fs')) {
if (originalFsMethods !== null) {
return
@@ -28,7 +34,8 @@ function shimFs(binary: NexeBinary, fs: any = require('fs')) {
isString = (x: any): x is string => typeof x === 'string' || x instanceof String,
noop = () => {},
path = require('path'),
baseDir = path.dirname(process.execPath)
winPath: (key: string) => string = isWin ? upcaseDriveLetter : s => s,
baseDir = winPath(path.dirname(process.execPath))
let log = (_: string) => true
let loggedManifest = false
@@ -44,13 +51,6 @@ function shimFs(binary: NexeBinary, fs: any = require('fs')) {
}
}
const winPath = (key: string) => {
if (isWin && key.substr(1, 2) === ':\\') {
key = key[0].toUpperCase() + key.substr(1)
}
return key
}
const getKey = function getKey(filepath: string | Buffer | null): string {
if (Buffer.isBuffer(filepath)) {
filepath = filepath.toString()
@@ -308,8 +308,8 @@ function shimFs(binary: NexeBinary, fs: any = require('fs')) {
const patches = (process as any).nexe.patches || {}
delete (process as any).nexe
patches.internalModuleReadFile = function(this: any, original: any, ...args: any[]) {
const [filepath] = args
setupManifest()
const filepath = getKey(args[0])
if (manifest[filepath]) {
log('read (hit) ' + filepath)
return nfs.readFileSync(filepath, 'utf-8')
@@ -320,7 +320,7 @@ function shimFs(binary: NexeBinary, fs: any = require('fs')) {
patches.internalModuleReadJSON = patches.internalModuleReadFile
patches.internalModuleStat = function(this: any, original: any, ...args: any[]) {
setupManifest()
const [filepath] = args
const filepath = getKey(args[0])
if (manifest[filepath]) {
log('stat (hit) ' + filepath + ' ' + 0)
return 0