asar.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. (function () {
  2. const asar = process.binding('atom_common_asar')
  3. const {Buffer} = require('buffer')
  4. const childProcess = require('child_process')
  5. const path = require('path')
  6. const util = require('util')
  7. const hasProp = {}.hasOwnProperty
  8. // Cache asar archive objects.
  9. const cachedArchives = {}
  10. const envNoAsar = process.env.ELECTRON_NO_ASAR && process.type !== 'browser' && process.type !== 'renderer'
  11. const isAsarDisabled = function () {
  12. return process.noAsar || envNoAsar
  13. }
  14. const getOrCreateArchive = function (p) {
  15. let archive = cachedArchives[p]
  16. if (archive != null) {
  17. return archive
  18. }
  19. archive = asar.createArchive(p)
  20. if (!archive) {
  21. return false
  22. }
  23. cachedArchives[p] = archive
  24. return archive
  25. }
  26. // Clean cache on quit.
  27. process.on('exit', function () {
  28. for (let p in cachedArchives) {
  29. if (!hasProp.call(cachedArchives, p)) continue
  30. cachedArchives[p].destroy()
  31. }
  32. })
  33. // Separate asar package's path from full path.
  34. const splitPath = function (p) {
  35. // shortcut to disable asar.
  36. if (isAsarDisabled()) {
  37. return [false]
  38. }
  39. if (Buffer.isBuffer(p)) {
  40. p = p.toString()
  41. }
  42. if (typeof p !== 'string') {
  43. return [false]
  44. }
  45. if (p.substr(-5) === '.asar') {
  46. return [true, p, '']
  47. }
  48. p = path.normalize(p)
  49. const index = p.lastIndexOf('.asar' + path.sep)
  50. if (index === -1) {
  51. return [false]
  52. }
  53. return [true, p.substr(0, index + 5), p.substr(index + 6)]
  54. }
  55. // Convert asar archive's Stats object to fs's Stats object.
  56. let nextInode = 0
  57. const uid = process.getuid != null ? process.getuid() : 0
  58. const gid = process.getgid != null ? process.getgid() : 0
  59. const fakeTime = new Date()
  60. const msec = (date) => (date || fakeTime).getTime()
  61. const asarStatsToFsStats = function (stats) {
  62. const {Stats, constants} = require('fs')
  63. let mode = constants.S_IROTH ^ constants.S_IRGRP ^ constants.S_IRUSR ^ constants.S_IWUSR
  64. if (stats.isFile) {
  65. mode ^= constants.S_IFREG
  66. } else if (stats.isDirectory) {
  67. mode ^= constants.S_IFDIR
  68. } else if (stats.isLink) {
  69. mode ^= constants.S_IFLNK
  70. }
  71. return new Stats(
  72. 1, // dev
  73. mode, // mode
  74. 1, // nlink
  75. uid,
  76. gid,
  77. 0, // rdev
  78. undefined, // blksize
  79. ++nextInode, // ino
  80. stats.size,
  81. undefined, // blocks,
  82. msec(stats.atime), // atim_msec
  83. msec(stats.mtime), // mtim_msec
  84. msec(stats.ctime), // ctim_msec
  85. msec(stats.birthtime) // birthtim_msec
  86. )
  87. }
  88. // Create a ENOENT error.
  89. const notFoundError = function (asarPath, filePath, callback) {
  90. const error = new Error(`ENOENT, ${filePath} not found in ${asarPath}`)
  91. error.code = 'ENOENT'
  92. error.errno = -2
  93. if (typeof callback !== 'function') {
  94. throw error
  95. }
  96. process.nextTick(function () {
  97. callback(error)
  98. })
  99. }
  100. // Create a ENOTDIR error.
  101. const notDirError = function (callback) {
  102. const error = new Error('ENOTDIR, not a directory')
  103. error.code = 'ENOTDIR'
  104. error.errno = -20
  105. if (typeof callback !== 'function') {
  106. throw error
  107. }
  108. process.nextTick(function () {
  109. callback(error)
  110. })
  111. }
  112. // Create a EACCES error.
  113. const accessError = function (asarPath, filePath, callback) {
  114. const error = new Error(`EACCES: permission denied, access '${filePath}'`)
  115. error.code = 'EACCES'
  116. error.errno = -13
  117. if (typeof callback !== 'function') {
  118. throw error
  119. }
  120. process.nextTick(function () {
  121. callback(error)
  122. })
  123. }
  124. // Create invalid archive error.
  125. const invalidArchiveError = function (asarPath, callback) {
  126. const error = new Error(`Invalid package ${asarPath}`)
  127. if (typeof callback !== 'function') {
  128. throw error
  129. }
  130. process.nextTick(function () {
  131. callback(error)
  132. })
  133. }
  134. // Override APIs that rely on passing file path instead of content to C++.
  135. const overrideAPISync = function (module, name, arg) {
  136. if (arg == null) {
  137. arg = 0
  138. }
  139. const old = module[name]
  140. module[name] = function () {
  141. const p = arguments[arg]
  142. const [isAsar, asarPath, filePath] = splitPath(p)
  143. if (!isAsar) {
  144. return old.apply(this, arguments)
  145. }
  146. const archive = getOrCreateArchive(asarPath)
  147. if (!archive) {
  148. invalidArchiveError(asarPath)
  149. }
  150. const newPath = archive.copyFileOut(filePath)
  151. if (!newPath) {
  152. notFoundError(asarPath, filePath)
  153. }
  154. arguments[arg] = newPath
  155. return old.apply(this, arguments)
  156. }
  157. }
  158. const overrideAPI = function (module, name, arg) {
  159. if (arg == null) {
  160. arg = 0
  161. }
  162. const old = module[name]
  163. module[name] = function () {
  164. const p = arguments[arg]
  165. const [isAsar, asarPath, filePath] = splitPath(p)
  166. if (!isAsar) {
  167. return old.apply(this, arguments)
  168. }
  169. const callback = arguments[arguments.length - 1]
  170. if (typeof callback !== 'function') {
  171. return overrideAPISync(module, name, arg)
  172. }
  173. const archive = getOrCreateArchive(asarPath)
  174. if (!archive) {
  175. return invalidArchiveError(asarPath, callback)
  176. }
  177. const newPath = archive.copyFileOut(filePath)
  178. if (!newPath) {
  179. return notFoundError(asarPath, filePath, callback)
  180. }
  181. arguments[arg] = newPath
  182. return old.apply(this, arguments)
  183. }
  184. if (old[util.promisify.custom]) {
  185. module[name][util.promisify.custom] = function () {
  186. const p = arguments[arg]
  187. const [isAsar, asarPath, filePath] = splitPath(p)
  188. if (!isAsar) {
  189. return old[util.promisify.custom].apply(this, arguments)
  190. }
  191. const archive = getOrCreateArchive(asarPath)
  192. if (!archive) {
  193. return new Promise(() => invalidArchiveError(asarPath))
  194. }
  195. const newPath = archive.copyFileOut(filePath)
  196. if (!newPath) {
  197. return new Promise(() => notFoundError(asarPath, filePath))
  198. }
  199. arguments[arg] = newPath
  200. return old[util.promisify.custom].apply(this, arguments)
  201. }
  202. }
  203. }
  204. // Override fs APIs.
  205. exports.wrapFsWithAsar = function (fs) {
  206. const logFDs = {}
  207. const logASARAccess = function (asarPath, filePath, offset) {
  208. if (!process.env.ELECTRON_LOG_ASAR_READS) {
  209. return
  210. }
  211. if (!logFDs[asarPath]) {
  212. const path = require('path')
  213. const logFilename = path.basename(asarPath, '.asar') + '-access-log.txt'
  214. const logPath = path.join(require('os').tmpdir(), logFilename)
  215. logFDs[asarPath] = fs.openSync(logPath, 'a')
  216. console.log('Logging ' + asarPath + ' access to ' + logPath)
  217. }
  218. fs.writeSync(logFDs[asarPath], offset + ': ' + filePath + '\n')
  219. }
  220. const {lstatSync} = fs
  221. fs.lstatSync = function (p) {
  222. const [isAsar, asarPath, filePath] = splitPath(p)
  223. if (!isAsar) {
  224. return lstatSync(p)
  225. }
  226. const archive = getOrCreateArchive(asarPath)
  227. if (!archive) {
  228. invalidArchiveError(asarPath)
  229. }
  230. const stats = archive.stat(filePath)
  231. if (!stats) {
  232. notFoundError(asarPath, filePath)
  233. }
  234. return asarStatsToFsStats(stats)
  235. }
  236. const {lstat} = fs
  237. fs.lstat = function (p, callback) {
  238. const [isAsar, asarPath, filePath] = splitPath(p)
  239. if (!isAsar) {
  240. return lstat(p, callback)
  241. }
  242. const archive = getOrCreateArchive(asarPath)
  243. if (!archive) {
  244. return invalidArchiveError(asarPath, callback)
  245. }
  246. const stats = getOrCreateArchive(asarPath).stat(filePath)
  247. if (!stats) {
  248. return notFoundError(asarPath, filePath, callback)
  249. }
  250. process.nextTick(function () {
  251. callback(null, asarStatsToFsStats(stats))
  252. })
  253. }
  254. const {statSync} = fs
  255. fs.statSync = function (p) {
  256. const [isAsar] = splitPath(p)
  257. if (!isAsar) {
  258. return statSync(p)
  259. }
  260. // Do not distinguish links for now.
  261. return fs.lstatSync(p)
  262. }
  263. const {stat} = fs
  264. fs.stat = function (p, callback) {
  265. const [isAsar] = splitPath(p)
  266. if (!isAsar) {
  267. return stat(p, callback)
  268. }
  269. // Do not distinguish links for now.
  270. process.nextTick(function () {
  271. fs.lstat(p, callback)
  272. })
  273. }
  274. const {statSyncNoException} = fs
  275. fs.statSyncNoException = function (p) {
  276. const [isAsar, asarPath, filePath] = splitPath(p)
  277. if (!isAsar) {
  278. return statSyncNoException(p)
  279. }
  280. const archive = getOrCreateArchive(asarPath)
  281. if (!archive) {
  282. return false
  283. }
  284. const stats = archive.stat(filePath)
  285. if (!stats) {
  286. return false
  287. }
  288. return asarStatsToFsStats(stats)
  289. }
  290. const {realpathSync} = fs
  291. fs.realpathSync = function (p) {
  292. const [isAsar, asarPath, filePath] = splitPath(p)
  293. if (!isAsar) return realpathSync.apply(this, arguments)
  294. const archive = getOrCreateArchive(asarPath)
  295. if (!archive) return invalidArchiveError(asarPath)
  296. const real = archive.realpath(filePath)
  297. if (real === false) notFoundError(asarPath, filePath)
  298. return path.join(realpathSync(asarPath), real)
  299. }
  300. fs.realpathSync.native = function (p) {
  301. const [isAsar, asarPath, filePath] = splitPath(p)
  302. if (!isAsar) return realpathSync.native.apply(this, arguments)
  303. const archive = getOrCreateArchive(asarPath)
  304. if (!archive) return invalidArchiveError(asarPath)
  305. const real = archive.realpath(filePath)
  306. if (real === false) notFoundError(asarPath, filePath)
  307. return path.join(realpathSync.native(asarPath), real)
  308. }
  309. const {realpath} = fs
  310. fs.realpath = function (p, cache, callback) {
  311. const [isAsar, asarPath, filePath] = splitPath(p)
  312. if (!isAsar) return realpath.apply(this, arguments)
  313. if (typeof cache === 'function') {
  314. callback = cache
  315. cache = void 0
  316. }
  317. const archive = getOrCreateArchive(asarPath)
  318. if (!archive) return invalidArchiveError(asarPath, callback)
  319. const real = archive.realpath(filePath)
  320. if (real === false) return notFoundError(asarPath, filePath, callback)
  321. return realpath(asarPath, (err, p) => {
  322. return (err) ? callback(err) : callback(null, path.join(p, real))
  323. })
  324. }
  325. fs.realpath.native = function (p, cache, callback) {
  326. const [isAsar, asarPath, filePath] = splitPath(p)
  327. if (!isAsar) return realpath.native.apply(this, arguments)
  328. if (typeof cache === 'function') {
  329. callback = cache
  330. cache = void 0
  331. }
  332. const archive = getOrCreateArchive(asarPath)
  333. if (!archive) return invalidArchiveError(asarPath, callback)
  334. const real = archive.realpath(filePath)
  335. if (real === false) return notFoundError(asarPath, filePath, callback)
  336. return realpath.native(asarPath, (err, p) => {
  337. return (err) ? callback(err) : callback(null, path.join(p, real))
  338. })
  339. }
  340. const {exists} = fs
  341. fs.exists = function (p, callback) {
  342. const [isAsar, asarPath, filePath] = splitPath(p)
  343. if (!isAsar) {
  344. return exists(p, callback)
  345. }
  346. const archive = getOrCreateArchive(asarPath)
  347. if (!archive) {
  348. return invalidArchiveError(asarPath, callback)
  349. }
  350. process.nextTick(function () {
  351. // Disabled due to false positive in StandardJS
  352. // eslint-disable-next-line standard/no-callback-literal
  353. callback(archive.stat(filePath) !== false)
  354. })
  355. }
  356. fs.exists[util.promisify.custom] = function (p) {
  357. const [isAsar, asarPath, filePath] = splitPath(p)
  358. if (!isAsar) {
  359. return exists[util.promisify.custom](p)
  360. }
  361. const archive = getOrCreateArchive(asarPath)
  362. if (!archive) {
  363. return new Promise(() => invalidArchiveError(asarPath))
  364. }
  365. return Promise.resolve(archive.stat(filePath) !== false)
  366. }
  367. const {existsSync} = fs
  368. fs.existsSync = function (p) {
  369. const [isAsar, asarPath, filePath] = splitPath(p)
  370. if (!isAsar) {
  371. return existsSync(p)
  372. }
  373. const archive = getOrCreateArchive(asarPath)
  374. if (!archive) {
  375. return false
  376. }
  377. return archive.stat(filePath) !== false
  378. }
  379. const {access} = fs
  380. fs.access = function (p, mode, callback) {
  381. const [isAsar, asarPath, filePath] = splitPath(p)
  382. if (!isAsar) {
  383. return access.apply(this, arguments)
  384. }
  385. if (typeof mode === 'function') {
  386. callback = mode
  387. mode = fs.constants.F_OK
  388. }
  389. const archive = getOrCreateArchive(asarPath)
  390. if (!archive) {
  391. return invalidArchiveError(asarPath, callback)
  392. }
  393. const info = archive.getFileInfo(filePath)
  394. if (!info) {
  395. return notFoundError(asarPath, filePath, callback)
  396. }
  397. if (info.unpacked) {
  398. const realPath = archive.copyFileOut(filePath)
  399. return fs.access(realPath, mode, callback)
  400. }
  401. const stats = getOrCreateArchive(asarPath).stat(filePath)
  402. if (!stats) {
  403. return notFoundError(asarPath, filePath, callback)
  404. }
  405. if (mode & fs.constants.W_OK) {
  406. return accessError(asarPath, filePath, callback)
  407. }
  408. process.nextTick(function () {
  409. callback()
  410. })
  411. }
  412. const {accessSync} = fs
  413. fs.accessSync = function (p, mode) {
  414. const [isAsar, asarPath, filePath] = splitPath(p)
  415. if (!isAsar) {
  416. return accessSync.apply(this, arguments)
  417. }
  418. if (mode == null) {
  419. mode = fs.constants.F_OK
  420. }
  421. const archive = getOrCreateArchive(asarPath)
  422. if (!archive) {
  423. invalidArchiveError(asarPath)
  424. }
  425. const info = archive.getFileInfo(filePath)
  426. if (!info) {
  427. notFoundError(asarPath, filePath)
  428. }
  429. if (info.unpacked) {
  430. const realPath = archive.copyFileOut(filePath)
  431. return fs.accessSync(realPath, mode)
  432. }
  433. const stats = getOrCreateArchive(asarPath).stat(filePath)
  434. if (!stats) {
  435. notFoundError(asarPath, filePath)
  436. }
  437. if (mode & fs.constants.W_OK) {
  438. accessError(asarPath, filePath)
  439. }
  440. }
  441. const {readFile} = fs
  442. fs.readFile = function (p, options, callback) {
  443. const [isAsar, asarPath, filePath] = splitPath(p)
  444. if (!isAsar) {
  445. return readFile.apply(this, arguments)
  446. }
  447. if (typeof options === 'function') {
  448. callback = options
  449. options = {
  450. encoding: null
  451. }
  452. } else if (typeof options === 'string') {
  453. options = {
  454. encoding: options
  455. }
  456. } else if (options === null || options === undefined) {
  457. options = {
  458. encoding: null
  459. }
  460. } else if (typeof options !== 'object') {
  461. throw new TypeError('Bad arguments')
  462. }
  463. const {encoding} = options
  464. const archive = getOrCreateArchive(asarPath)
  465. if (!archive) {
  466. return invalidArchiveError(asarPath, callback)
  467. }
  468. const info = archive.getFileInfo(filePath)
  469. if (!info) {
  470. return notFoundError(asarPath, filePath, callback)
  471. }
  472. if (info.size === 0) {
  473. return process.nextTick(function () {
  474. callback(null, encoding ? '' : Buffer.alloc(0))
  475. })
  476. }
  477. if (info.unpacked) {
  478. const realPath = archive.copyFileOut(filePath)
  479. return fs.readFile(realPath, options, callback)
  480. }
  481. const buffer = Buffer.alloc(info.size)
  482. const fd = archive.getFd()
  483. if (!(fd >= 0)) {
  484. return notFoundError(asarPath, filePath, callback)
  485. }
  486. logASARAccess(asarPath, filePath, info.offset)
  487. fs.read(fd, buffer, 0, info.size, info.offset, function (error) {
  488. callback(error, encoding ? buffer.toString(encoding) : buffer)
  489. })
  490. }
  491. const {readFileSync} = fs
  492. fs.readFileSync = function (p, options) {
  493. const [isAsar, asarPath, filePath] = splitPath(p)
  494. if (!isAsar) {
  495. return readFileSync.apply(this, arguments)
  496. }
  497. const archive = getOrCreateArchive(asarPath)
  498. if (!archive) {
  499. invalidArchiveError(asarPath)
  500. }
  501. const info = archive.getFileInfo(filePath)
  502. if (!info) {
  503. notFoundError(asarPath, filePath)
  504. }
  505. if (info.size === 0) {
  506. if (options) {
  507. return ''
  508. } else {
  509. return Buffer.alloc(0)
  510. }
  511. }
  512. if (info.unpacked) {
  513. const realPath = archive.copyFileOut(filePath)
  514. return fs.readFileSync(realPath, options)
  515. }
  516. if (!options) {
  517. options = {
  518. encoding: null
  519. }
  520. } else if (typeof options === 'string') {
  521. options = {
  522. encoding: options
  523. }
  524. } else if (typeof options !== 'object') {
  525. throw new TypeError('Bad arguments')
  526. }
  527. const {encoding} = options
  528. const buffer = Buffer.alloc(info.size)
  529. const fd = archive.getFd()
  530. if (!(fd >= 0)) {
  531. notFoundError(asarPath, filePath)
  532. }
  533. logASARAccess(asarPath, filePath, info.offset)
  534. fs.readSync(fd, buffer, 0, info.size, info.offset)
  535. if (encoding) {
  536. return buffer.toString(encoding)
  537. } else {
  538. return buffer
  539. }
  540. }
  541. const {readdir} = fs
  542. fs.readdir = function (p, callback) {
  543. const [isAsar, asarPath, filePath] = splitPath(p)
  544. if (!isAsar) {
  545. return readdir.apply(this, arguments)
  546. }
  547. const archive = getOrCreateArchive(asarPath)
  548. if (!archive) {
  549. return invalidArchiveError(asarPath, callback)
  550. }
  551. const files = archive.readdir(filePath)
  552. if (!files) {
  553. return notFoundError(asarPath, filePath, callback)
  554. }
  555. process.nextTick(function () {
  556. callback(null, files)
  557. })
  558. }
  559. const {readdirSync} = fs
  560. fs.readdirSync = function (p) {
  561. const [isAsar, asarPath, filePath] = splitPath(p)
  562. if (!isAsar) {
  563. return readdirSync.apply(this, arguments)
  564. }
  565. const archive = getOrCreateArchive(asarPath)
  566. if (!archive) {
  567. invalidArchiveError(asarPath)
  568. }
  569. const files = archive.readdir(filePath)
  570. if (!files) {
  571. notFoundError(asarPath, filePath)
  572. }
  573. return files
  574. }
  575. const {internalModuleReadJSON} = process.binding('fs')
  576. process.binding('fs').internalModuleReadJSON = function (p) {
  577. const [isAsar, asarPath, filePath] = splitPath(p)
  578. if (!isAsar) {
  579. return internalModuleReadJSON(p)
  580. }
  581. const archive = getOrCreateArchive(asarPath)
  582. if (!archive) {
  583. return
  584. }
  585. const info = archive.getFileInfo(filePath)
  586. if (!info) {
  587. return
  588. }
  589. if (info.size === 0) {
  590. return ''
  591. }
  592. if (info.unpacked) {
  593. const realPath = archive.copyFileOut(filePath)
  594. return fs.readFileSync(realPath, {
  595. encoding: 'utf8'
  596. })
  597. }
  598. const buffer = Buffer.alloc(info.size)
  599. const fd = archive.getFd()
  600. if (!(fd >= 0)) {
  601. return
  602. }
  603. logASARAccess(asarPath, filePath, info.offset)
  604. fs.readSync(fd, buffer, 0, info.size, info.offset)
  605. return buffer.toString('utf8')
  606. }
  607. const {internalModuleStat} = process.binding('fs')
  608. process.binding('fs').internalModuleStat = function (p) {
  609. const [isAsar, asarPath, filePath] = splitPath(p)
  610. if (!isAsar) {
  611. return internalModuleStat(p)
  612. }
  613. const archive = getOrCreateArchive(asarPath)
  614. // -ENOENT
  615. if (!archive) {
  616. return -34
  617. }
  618. const stats = archive.stat(filePath)
  619. // -ENOENT
  620. if (!stats) {
  621. return -34
  622. }
  623. if (stats.isDirectory) {
  624. return 1
  625. } else {
  626. return 0
  627. }
  628. }
  629. // Calling mkdir for directory inside asar archive should throw ENOTDIR
  630. // error, but on Windows it throws ENOENT.
  631. // This is to work around the recursive looping bug of mkdirp since it is
  632. // widely used.
  633. if (process.platform === 'win32') {
  634. const {mkdir} = fs
  635. fs.mkdir = function (p, mode, callback) {
  636. if (typeof mode === 'function') {
  637. callback = mode
  638. }
  639. const [isAsar, , filePath] = splitPath(p)
  640. if (isAsar && filePath.length) {
  641. return notDirError(callback)
  642. }
  643. mkdir(p, mode, callback)
  644. }
  645. const {mkdirSync} = fs
  646. fs.mkdirSync = function (p, mode) {
  647. const [isAsar, , filePath] = splitPath(p)
  648. if (isAsar && filePath.length) {
  649. notDirError()
  650. }
  651. return mkdirSync(p, mode)
  652. }
  653. }
  654. // Executing a command string containing a path to an asar
  655. // archive confuses `childProcess.execFile`, which is internally
  656. // called by `childProcess.{exec,execSync}`, causing
  657. // Electron to consider the full command as a single path
  658. // to an archive.
  659. const {exec, execSync} = childProcess
  660. childProcess.exec = invokeWithNoAsar(exec)
  661. childProcess.exec[util.promisify.custom] = invokeWithNoAsar(exec[util.promisify.custom])
  662. childProcess.execSync = invokeWithNoAsar(execSync)
  663. function invokeWithNoAsar (func) {
  664. return function () {
  665. const processNoAsarOriginalValue = process.noAsar
  666. process.noAsar = true
  667. try {
  668. return func.apply(this, arguments)
  669. } finally {
  670. process.noAsar = processNoAsarOriginalValue
  671. }
  672. }
  673. }
  674. // Strictly implementing the flags of fs.copyFile is hard, just do a simple
  675. // implementation for now. Doing 2 copies won't spend much time more as OS
  676. // has filesystem caching.
  677. overrideAPI(fs, 'copyFile')
  678. overrideAPISync(fs, 'copyFileSync')
  679. overrideAPI(fs, 'open')
  680. overrideAPI(childProcess, 'execFile')
  681. overrideAPISync(process, 'dlopen', 1)
  682. overrideAPISync(require('module')._extensions, '.node', 1)
  683. overrideAPISync(fs, 'openSync')
  684. overrideAPISync(childProcess, 'execFileSync')
  685. }
  686. })()