diff --git a/src/patches/boot-nexe.ts b/src/patches/boot-nexe.ts index f3ae18ce..565c27ba 100644 --- a/src/patches/boot-nexe.ts +++ b/src/patches/boot-nexe.ts @@ -1,24 +1,42 @@ const fs = require('fs'), fd = fs.openSync(process.execPath, 'r'), stat = fs.fstatSync(fd), - defaultTailSize = 16000, - tailSize = Math.min( - stat.size, - require('os').platform() === 'darwin' ? defaultTailSize * 100 : defaultTailSize - ), - tailWindow = Buffer.from(Array(tailSize)) + tailSize = Math.min(stat.size, 16000), + tailWindow = Buffer.alloc(tailSize), + match = '', + matchLength = match.length, + lastBuffer = Buffer.alloc(matchLength + 32) -fs.readSync(fd, tailWindow, 0, tailSize, stat.size - tailSize) +let offset = stat.size, + footerPosition = -1, + footerPositionOffset = 0, + footer: Buffer + +while (true) { + const bytesRead = fs.readSync(fd, tailWindow, 0, tailSize, offset - tailSize) + if (bytesRead === 0) break + + const combinedBuffers = Buffer.concat([tailWindow, lastBuffer]) + footerPosition = combinedBuffers.indexOf(match) + + if (footerPosition > -1) { + footer = combinedBuffers.slice(footerPosition, footerPosition + 32) + break + } + + if (offset < 0) break + + tailWindow.copy(lastBuffer) + offset = offset - bytesRead +} -const footerPosition = tailWindow.indexOf('') if (footerPosition == -1) { throw 'Invalid Nexe binary' } -const footer = tailWindow.slice(footerPosition, footerPosition + 32), - contentSize = footer.readDoubleLE(16), - resourceSize = footer.readDoubleLE(24), - contentStart = stat.size - tailSize + footerPosition - resourceSize - contentSize, +const contentSize = footer!.readDoubleLE(16), + resourceSize = footer!.readDoubleLE(24), + contentStart = offset - tailSize + footerPosition - resourceSize - contentSize, resourceStart = contentStart + contentSize Object.defineProperty( @@ -52,10 +70,10 @@ Object.defineProperty( })() ) -const contentBuffer = Buffer.from(Array(contentSize)), +const contentBuffer = Buffer.alloc(contentSize), Module = require('module') fs.readSync(fd, contentBuffer, 0, contentSize, contentStart) fs.closeSync(fd) -new Module(process.execPath, null)._compile(contentBuffer.toString(), process.execPath) +new Module(process.execPath, null)._compile(contentBuffer.slice(1).toString(), process.execPath)