1- import { AsarFileInfo , listPackage , statFile } from "asar"
21import BluebirdPromise from "bluebird-lst"
32import { debug } from "electron-builder-util"
43import { deepAssign } from "electron-builder-util/out/deepAssign"
54import { CONCURRENCY , FileCopier , Filter , MAX_FILE_REQUESTS , statOrNull , walk } from "electron-builder-util/out/fs"
65import { log } from "electron-builder-util/out/log"
76import { createReadStream , createWriteStream , ensureDir , readFile , readJson , readlink , stat , Stats , writeFile } from "fs-extra-p"
87import * as path from "path"
8+ import { AsarFilesystem , Node , readAsar } from "./asar"
99import { AsarOptions } from "./metadata"
1010
1111const isBinaryFile : any = BluebirdPromise . promisify ( require ( "isbinaryfile" ) )
1212const pickle = require ( "chromium-pickle-js" )
13- const Filesystem = require ( "asar/lib/filesystem" )
14- const UINT64 = require ( "cuint" ) . UINT64
1513
1614const NODE_MODULES_PATTERN = `${ path . sep } node_modules${ path . sep } `
1715
@@ -51,7 +49,7 @@ function writeUnpackedFiles(filesToUnpack: Array<UnpackedFileTask>, fileCopier:
5149
5250class AsarPackager {
5351 private readonly toPack : Array < string > = [ ]
54- private readonly fs = new Filesystem ( this . src )
52+ private readonly fs = new AsarFilesystem ( this . src )
5553 private readonly changedFiles = new Map < string , string > ( )
5654 private readonly outFile : string
5755
@@ -188,7 +186,7 @@ class AsarPackager {
188186 const stat = metadata . get ( file ) !
189187 if ( stat . isFile ( ) ) {
190188 const fileParent = path . dirname ( file )
191- const dirNode = this . fs . searchNodeFromPath ( fileParent )
189+ const dirNode = this . fs . getOrCreateNode ( fileParent )
192190 const packageDataPromise = fileIndexToModulePackageData . get ( i )
193191 let newData : string | null = null
194192 if ( packageDataPromise == null ) {
@@ -213,7 +211,7 @@ class AsarPackager {
213211 }
214212
215213 const fileSize = newData == null ? stat . size : Buffer . byteLength ( newData )
216- const node = this . fs . searchNodeFromPath ( file )
214+ const node = this . fs . getOrCreateNode ( file )
217215 node . size = fileSize
218216 if ( dirNode . unpacked || ( this . unpackPattern != null && this . unpackPattern ( file , stat ) ) ) {
219217 node . unpacked = true
@@ -234,18 +232,8 @@ class AsarPackager {
234232 if ( newData != null ) {
235233 this . changedFiles . set ( file , newData )
236234 }
237-
238- if ( fileSize > 4294967295 ) {
239- throw new Error ( `${ file } : file size can not be larger than 4.2GB` )
240- }
241-
242- node . offset = this . fs . offset . toString ( )
243- //noinspection JSBitwiseOperatorUsage
244- if ( process . platform !== "win32" && stat . mode & 0x40 ) {
245- node . executable = true
246- }
235+ this . fs . insertFileNode ( node , stat , file )
247236 this . toPack . push ( file )
248- this . fs . offset . add ( UINT64 ( fileSize ) )
249237 }
250238 }
251239 else if ( stat . isDirectory ( ) ) {
@@ -268,7 +256,7 @@ class AsarPackager {
268256 this . fs . insertDirectory ( file , unpacked )
269257 }
270258 else if ( stat . isSymbolicLink ( ) ) {
271- this . fs . searchNodeFromPath ( file ) . link = ( < any > stat ) . relativeLink
259+ this . fs . getOrCreateNode ( file ) . link = ( < any > stat ) . relativeLink
272260 }
273261 }
274262
@@ -289,7 +277,7 @@ class AsarPackager {
289277 const writeStream = createWriteStream ( this . outFile )
290278 return new BluebirdPromise ( ( resolve , reject ) => {
291279 writeStream . on ( "error" , reject )
292- writeStream . once ( "finish ", resolve )
280+ writeStream . on ( "close ", resolve )
293281 writeStream . write ( sizeBuf )
294282
295283 let w : ( list : Array < any > , index : number ) => void
@@ -386,23 +374,24 @@ export async function checkFileInArchive(asarFile: string, relativeFile: string,
386374 return new Error ( `${ messagePrefix } "${ relativeFile } " in the "${ asarFile } " ${ text } ` )
387375 }
388376
389- let stat : AsarFileInfo | null
377+ let fs
378+ try {
379+ fs = await readAsar ( asarFile )
380+ }
381+ catch ( e ) {
382+ throw error ( `is corrupted: ${ e } ` )
383+ }
384+
385+ let stat : Node | null
390386 try {
391- stat = statFile ( asarFile , relativeFile )
387+ stat = fs . getFile ( relativeFile )
392388 }
393389 catch ( e ) {
394390 const fileStat = await statOrNull ( asarFile )
395391 if ( fileStat == null ) {
396392 throw error ( `does not exist. Seems like a wrong configuration.` )
397393 }
398394
399- try {
400- listPackage ( asarFile )
401- }
402- catch ( e ) {
403- throw error ( `is corrupted: ${ e } ` )
404- }
405-
406395 // asar throws error on access to undefined object (info.link)
407396 stat = null
408397 }
0 commit comments