asar-spec.js 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466
  1. const { expect } = require('chai')
  2. const ChildProcess = require('child_process')
  3. const fs = require('fs')
  4. const path = require('path')
  5. const temp = require('temp').track()
  6. const util = require('util')
  7. const nativeImage = require('electron').nativeImage
  8. const features = process.electronBinding('features')
  9. async function expectToThrowErrorWithCode (func, code) {
  10. let error
  11. try {
  12. await func()
  13. } catch (e) {
  14. error = e
  15. }
  16. expect(error).is.an('Error')
  17. expect(error).to.have.property('code').which.equals(code)
  18. }
  19. describe('asar package', function () {
  20. const fixtures = path.join(__dirname, 'fixtures')
  21. const asarDir = path.join(fixtures, 'test.asar')
  22. describe('node api', function () {
  23. it('supports paths specified as a Buffer', function () {
  24. const file = Buffer.from(path.join(asarDir, 'a.asar', 'file1'))
  25. expect(fs.existsSync(file)).to.be.true()
  26. })
  27. describe('fs.readFileSync', function () {
  28. it('does not leak fd', function () {
  29. let readCalls = 1
  30. while (readCalls <= 10000) {
  31. fs.readFileSync(path.join(process.resourcesPath, 'default_app.asar', 'main.js'))
  32. readCalls++
  33. }
  34. })
  35. it('reads a normal file', function () {
  36. const file1 = path.join(asarDir, 'a.asar', 'file1')
  37. expect(fs.readFileSync(file1).toString().trim()).to.equal('file1')
  38. const file2 = path.join(asarDir, 'a.asar', 'file2')
  39. expect(fs.readFileSync(file2).toString().trim()).to.equal('file2')
  40. const file3 = path.join(asarDir, 'a.asar', 'file3')
  41. expect(fs.readFileSync(file3).toString().trim()).to.equal('file3')
  42. })
  43. it('reads from a empty file', function () {
  44. const file = path.join(asarDir, 'empty.asar', 'file1')
  45. const buffer = fs.readFileSync(file)
  46. expect(buffer).to.be.empty()
  47. expect(buffer.toString()).to.equal('')
  48. })
  49. it('reads a linked file', function () {
  50. const p = path.join(asarDir, 'a.asar', 'link1')
  51. expect(fs.readFileSync(p).toString().trim()).to.equal('file1')
  52. })
  53. it('reads a file from linked directory', function () {
  54. const p1 = path.join(asarDir, 'a.asar', 'link2', 'file1')
  55. expect(fs.readFileSync(p1).toString().trim()).to.equal('file1')
  56. const p2 = path.join(asarDir, 'a.asar', 'link2', 'link2', 'file1')
  57. expect(fs.readFileSync(p2).toString().trim()).to.equal('file1')
  58. })
  59. it('throws ENOENT error when can not find file', function () {
  60. const p = path.join(asarDir, 'a.asar', 'not-exist')
  61. expect(() => {
  62. fs.readFileSync(p)
  63. }).to.throw(/ENOENT/)
  64. })
  65. it('passes ENOENT error to callback when can not find file', function () {
  66. const p = path.join(asarDir, 'a.asar', 'not-exist')
  67. let async = false
  68. fs.readFile(p, function (error) {
  69. expect(async).to.be.true()
  70. expect(error).to.match(/ENOENT/)
  71. })
  72. async = true
  73. })
  74. it('reads a normal file with unpacked files', function () {
  75. const p = path.join(asarDir, 'unpack.asar', 'a.txt')
  76. expect(fs.readFileSync(p).toString().trim()).to.equal('a')
  77. })
  78. it('reads a file in filesystem', function () {
  79. const p = path.resolve(asarDir, 'file')
  80. expect(fs.readFileSync(p).toString().trim()).to.equal('file')
  81. })
  82. })
  83. describe('fs.readFile', function () {
  84. it('reads a normal file', function (done) {
  85. const p = path.join(asarDir, 'a.asar', 'file1')
  86. fs.readFile(p, function (err, content) {
  87. expect(err).to.be.null()
  88. expect(String(content).trim()).to.equal('file1')
  89. done()
  90. })
  91. })
  92. it('reads from a empty file', function (done) {
  93. const p = path.join(asarDir, 'empty.asar', 'file1')
  94. fs.readFile(p, function (err, content) {
  95. expect(err).to.be.null()
  96. expect(String(content)).to.equal('')
  97. done()
  98. })
  99. })
  100. it('reads from a empty file with encoding', function (done) {
  101. const p = path.join(asarDir, 'empty.asar', 'file1')
  102. fs.readFile(p, 'utf8', function (err, content) {
  103. expect(err).to.be.null()
  104. expect(content).to.equal('')
  105. done()
  106. })
  107. })
  108. it('reads a linked file', function (done) {
  109. const p = path.join(asarDir, 'a.asar', 'link1')
  110. fs.readFile(p, function (err, content) {
  111. expect(err).to.be.null()
  112. expect(String(content).trim()).to.equal('file1')
  113. done()
  114. })
  115. })
  116. it('reads a file from linked directory', function (done) {
  117. const p = path.join(asarDir, 'a.asar', 'link2', 'link2', 'file1')
  118. fs.readFile(p, function (err, content) {
  119. expect(err).to.be.null()
  120. expect(String(content).trim()).to.equal('file1')
  121. done()
  122. })
  123. })
  124. it('throws ENOENT error when can not find file', function (done) {
  125. const p = path.join(asarDir, 'a.asar', 'not-exist')
  126. fs.readFile(p, function (err) {
  127. expect(err.code).to.equal('ENOENT')
  128. done()
  129. })
  130. })
  131. })
  132. describe('fs.promises.readFile', function () {
  133. it('reads a normal file', async function () {
  134. const p = path.join(asarDir, 'a.asar', 'file1')
  135. const content = await fs.promises.readFile(p)
  136. expect(String(content).trim()).to.equal('file1')
  137. })
  138. it('reads from a empty file', async function () {
  139. const p = path.join(asarDir, 'empty.asar', 'file1')
  140. const content = await fs.promises.readFile(p)
  141. expect(String(content)).to.equal('')
  142. })
  143. it('reads from a empty file with encoding', async function () {
  144. const p = path.join(asarDir, 'empty.asar', 'file1')
  145. const content = await fs.promises.readFile(p, 'utf8')
  146. expect(content).to.equal('')
  147. })
  148. it('reads a linked file', async function () {
  149. const p = path.join(asarDir, 'a.asar', 'link1')
  150. const content = await fs.promises.readFile(p)
  151. expect(String(content).trim()).to.equal('file1')
  152. })
  153. it('reads a file from linked directory', async function () {
  154. const p = path.join(asarDir, 'a.asar', 'link2', 'link2', 'file1')
  155. const content = await fs.promises.readFile(p)
  156. expect(String(content).trim()).to.equal('file1')
  157. })
  158. it('throws ENOENT error when can not find file', async function () {
  159. const p = path.join(asarDir, 'a.asar', 'not-exist')
  160. await expectToThrowErrorWithCode(() => fs.promises.readFile(p), 'ENOENT')
  161. })
  162. })
  163. describe('fs.copyFile', function () {
  164. it('copies a normal file', function (done) {
  165. const p = path.join(asarDir, 'a.asar', 'file1')
  166. const dest = temp.path()
  167. fs.copyFile(p, dest, function (err) {
  168. expect(err).to.be.null()
  169. expect(fs.readFileSync(p).equals(fs.readFileSync(dest))).to.be.true()
  170. done()
  171. })
  172. })
  173. it('copies a unpacked file', function (done) {
  174. const p = path.join(asarDir, 'unpack.asar', 'a.txt')
  175. const dest = temp.path()
  176. fs.copyFile(p, dest, function (err) {
  177. expect(err).to.be.null()
  178. expect(fs.readFileSync(p).equals(fs.readFileSync(dest))).to.be.true()
  179. done()
  180. })
  181. })
  182. })
  183. describe('fs.promises.copyFile', function () {
  184. it('copies a normal file', async function () {
  185. const p = path.join(asarDir, 'a.asar', 'file1')
  186. const dest = temp.path()
  187. await fs.promises.copyFile(p, dest)
  188. expect(fs.readFileSync(p).equals(fs.readFileSync(dest))).to.be.true()
  189. })
  190. it('copies a unpacked file', async function () {
  191. const p = path.join(asarDir, 'unpack.asar', 'a.txt')
  192. const dest = temp.path()
  193. await fs.promises.copyFile(p, dest)
  194. expect(fs.readFileSync(p).equals(fs.readFileSync(dest))).to.be.true()
  195. })
  196. })
  197. describe('fs.copyFileSync', function () {
  198. it('copies a normal file', function () {
  199. const p = path.join(asarDir, 'a.asar', 'file1')
  200. const dest = temp.path()
  201. fs.copyFileSync(p, dest)
  202. expect(fs.readFileSync(p).equals(fs.readFileSync(dest))).to.be.true()
  203. })
  204. it('copies a unpacked file', function () {
  205. const p = path.join(asarDir, 'unpack.asar', 'a.txt')
  206. const dest = temp.path()
  207. fs.copyFileSync(p, dest)
  208. expect(fs.readFileSync(p).equals(fs.readFileSync(dest))).to.be.true()
  209. })
  210. })
  211. describe('fs.lstatSync', function () {
  212. it('handles path with trailing slash correctly', function () {
  213. const p = path.join(asarDir, 'a.asar', 'link2', 'link2', 'file1')
  214. fs.lstatSync(p)
  215. fs.lstatSync(p + '/')
  216. })
  217. it('returns information of root', function () {
  218. const p = path.join(asarDir, 'a.asar')
  219. const stats = fs.lstatSync(p)
  220. expect(stats.isFile()).to.be.false()
  221. expect(stats.isDirectory()).to.be.true()
  222. expect(stats.isSymbolicLink()).to.be.false()
  223. expect(stats.size).to.equal(0)
  224. })
  225. it('returns information of root with stats as bigint', function () {
  226. const p = path.join(asarDir, 'a.asar')
  227. const stats = fs.lstatSync(p, { bigint: false })
  228. expect(stats.isFile()).to.be.false()
  229. expect(stats.isDirectory()).to.be.true()
  230. expect(stats.isSymbolicLink()).to.be.false()
  231. expect(stats.size).to.equal(0)
  232. })
  233. it('returns information of a normal file', function () {
  234. const ref2 = ['file1', 'file2', 'file3', path.join('dir1', 'file1'), path.join('link2', 'file1')]
  235. for (let j = 0, len = ref2.length; j < len; j++) {
  236. const file = ref2[j]
  237. const p = path.join(asarDir, 'a.asar', file)
  238. const stats = fs.lstatSync(p)
  239. expect(stats.isFile()).to.be.true()
  240. expect(stats.isDirectory()).to.be.false()
  241. expect(stats.isSymbolicLink()).to.be.false()
  242. expect(stats.size).to.equal(6)
  243. }
  244. })
  245. it('returns information of a normal directory', function () {
  246. const ref2 = ['dir1', 'dir2', 'dir3']
  247. for (let j = 0, len = ref2.length; j < len; j++) {
  248. const file = ref2[j]
  249. const p = path.join(asarDir, 'a.asar', file)
  250. const stats = fs.lstatSync(p)
  251. expect(stats.isFile()).to.be.false()
  252. expect(stats.isDirectory()).to.be.true()
  253. expect(stats.isSymbolicLink()).to.be.false()
  254. expect(stats.size).to.equal(0)
  255. }
  256. })
  257. it('returns information of a linked file', function () {
  258. const ref2 = ['link1', path.join('dir1', 'link1'), path.join('link2', 'link2')]
  259. for (let j = 0, len = ref2.length; j < len; j++) {
  260. const file = ref2[j]
  261. const p = path.join(asarDir, 'a.asar', file)
  262. const stats = fs.lstatSync(p)
  263. expect(stats.isFile()).to.be.false()
  264. expect(stats.isDirectory()).to.be.false()
  265. expect(stats.isSymbolicLink()).to.be.true()
  266. expect(stats.size).to.equal(0)
  267. }
  268. })
  269. it('returns information of a linked directory', function () {
  270. const ref2 = ['link2', path.join('dir1', 'link2'), path.join('link2', 'link2')]
  271. for (let j = 0, len = ref2.length; j < len; j++) {
  272. const file = ref2[j]
  273. const p = path.join(asarDir, 'a.asar', file)
  274. const stats = fs.lstatSync(p)
  275. expect(stats.isFile()).to.be.false()
  276. expect(stats.isDirectory()).to.be.false()
  277. expect(stats.isSymbolicLink()).to.be.true()
  278. expect(stats.size).to.equal(0)
  279. }
  280. })
  281. it('throws ENOENT error when can not find file', function () {
  282. const ref2 = ['file4', 'file5', path.join('dir1', 'file4')]
  283. for (let j = 0, len = ref2.length; j < len; j++) {
  284. const file = ref2[j]
  285. const p = path.join(asarDir, 'a.asar', file)
  286. expect(() => {
  287. fs.lstatSync(p)
  288. }).to.throw(/ENOENT/)
  289. }
  290. })
  291. })
  292. describe('fs.lstat', function () {
  293. it('handles path with trailing slash correctly', function (done) {
  294. const p = path.join(asarDir, 'a.asar', 'link2', 'link2', 'file1')
  295. fs.lstat(p + '/', done)
  296. })
  297. it('returns information of root', function (done) {
  298. const p = path.join(asarDir, 'a.asar')
  299. fs.lstat(p, function (err, stats) {
  300. expect(err).to.be.null()
  301. expect(stats.isFile()).to.be.false()
  302. expect(stats.isDirectory()).to.be.true()
  303. expect(stats.isSymbolicLink()).to.be.false()
  304. expect(stats.size).to.equal(0)
  305. done()
  306. })
  307. })
  308. it('returns information of root with stats as bigint', function (done) {
  309. const p = path.join(asarDir, 'a.asar')
  310. fs.lstat(p, { bigint: false }, function (err, stats) {
  311. expect(err).to.be.null()
  312. expect(stats.isFile()).to.be.false()
  313. expect(stats.isDirectory()).to.be.true()
  314. expect(stats.isSymbolicLink()).to.be.false()
  315. expect(stats.size).to.equal(0)
  316. done()
  317. })
  318. })
  319. it('returns information of a normal file', function (done) {
  320. const p = path.join(asarDir, 'a.asar', 'link2', 'file1')
  321. fs.lstat(p, function (err, stats) {
  322. expect(err).to.be.null()
  323. expect(stats.isFile()).to.be.true()
  324. expect(stats.isDirectory()).to.be.false()
  325. expect(stats.isSymbolicLink()).to.be.false()
  326. expect(stats.size).to.equal(6)
  327. done()
  328. })
  329. })
  330. it('returns information of a normal directory', function (done) {
  331. const p = path.join(asarDir, 'a.asar', 'dir1')
  332. fs.lstat(p, function (err, stats) {
  333. expect(err).to.be.null()
  334. expect(stats.isFile()).to.be.false()
  335. expect(stats.isDirectory()).to.be.true()
  336. expect(stats.isSymbolicLink()).to.be.false()
  337. expect(stats.size).to.equal(0)
  338. done()
  339. })
  340. })
  341. it('returns information of a linked file', function (done) {
  342. const p = path.join(asarDir, 'a.asar', 'link2', 'link1')
  343. fs.lstat(p, function (err, stats) {
  344. expect(err).to.be.null()
  345. expect(stats.isFile()).to.be.false()
  346. expect(stats.isDirectory()).to.be.false()
  347. expect(stats.isSymbolicLink()).to.be.true()
  348. expect(stats.size).to.equal(0)
  349. done()
  350. })
  351. })
  352. it('returns information of a linked directory', function (done) {
  353. const p = path.join(asarDir, 'a.asar', 'link2', 'link2')
  354. fs.lstat(p, function (err, stats) {
  355. expect(err).to.be.null()
  356. expect(stats.isFile()).to.be.false()
  357. expect(stats.isDirectory()).to.be.false()
  358. expect(stats.isSymbolicLink()).to.be.true()
  359. expect(stats.size).to.equal(0)
  360. done()
  361. })
  362. })
  363. it('throws ENOENT error when can not find file', function (done) {
  364. const p = path.join(asarDir, 'a.asar', 'file4')
  365. fs.lstat(p, function (err) {
  366. expect(err.code).to.equal('ENOENT')
  367. done()
  368. })
  369. })
  370. })
  371. describe('fs.promises.lstat', function () {
  372. it('handles path with trailing slash correctly', async function () {
  373. const p = path.join(asarDir, 'a.asar', 'link2', 'link2', 'file1')
  374. await fs.promises.lstat(p + '/')
  375. })
  376. it('returns information of root', async function () {
  377. const p = path.join(asarDir, 'a.asar')
  378. const stats = await fs.promises.lstat(p)
  379. expect(stats.isFile()).to.be.false()
  380. expect(stats.isDirectory()).to.be.true()
  381. expect(stats.isSymbolicLink()).to.be.false()
  382. expect(stats.size).to.equal(0)
  383. })
  384. it('returns information of root with stats as bigint', async function () {
  385. const p = path.join(asarDir, 'a.asar')
  386. const stats = await fs.promises.lstat(p, { bigint: false })
  387. expect(stats.isFile()).to.be.false()
  388. expect(stats.isDirectory()).to.be.true()
  389. expect(stats.isSymbolicLink()).to.be.false()
  390. expect(stats.size).to.equal(0)
  391. })
  392. it('returns information of a normal file', async function () {
  393. const p = path.join(asarDir, 'a.asar', 'link2', 'file1')
  394. const stats = await fs.promises.lstat(p)
  395. expect(stats.isFile()).to.be.true()
  396. expect(stats.isDirectory()).to.be.false()
  397. expect(stats.isSymbolicLink()).to.be.false()
  398. expect(stats.size).to.equal(6)
  399. })
  400. it('returns information of a normal directory', async function () {
  401. const p = path.join(asarDir, 'a.asar', 'dir1')
  402. const stats = await fs.promises.lstat(p)
  403. expect(stats.isFile()).to.be.false()
  404. expect(stats.isDirectory()).to.be.true()
  405. expect(stats.isSymbolicLink()).to.be.false()
  406. expect(stats.size).to.equal(0)
  407. })
  408. it('returns information of a linked file', async function () {
  409. const p = path.join(asarDir, 'a.asar', 'link2', 'link1')
  410. const stats = await fs.promises.lstat(p)
  411. expect(stats.isFile()).to.be.false()
  412. expect(stats.isDirectory()).to.be.false()
  413. expect(stats.isSymbolicLink()).to.be.true()
  414. expect(stats.size).to.equal(0)
  415. })
  416. it('returns information of a linked directory', async function () {
  417. const p = path.join(asarDir, 'a.asar', 'link2', 'link2')
  418. const stats = await fs.promises.lstat(p)
  419. expect(stats.isFile()).to.be.false()
  420. expect(stats.isDirectory()).to.be.false()
  421. expect(stats.isSymbolicLink()).to.be.true()
  422. expect(stats.size).to.equal(0)
  423. })
  424. it('throws ENOENT error when can not find file', async function () {
  425. const p = path.join(asarDir, 'a.asar', 'file4')
  426. await expectToThrowErrorWithCode(() => fs.promises.lstat(p), 'ENOENT')
  427. })
  428. })
  429. describe('fs.realpathSync', () => {
  430. it('returns real path root', () => {
  431. const parent = fs.realpathSync(asarDir)
  432. const p = 'a.asar'
  433. const r = fs.realpathSync(path.join(parent, p))
  434. expect(r).to.equal(path.join(parent, p))
  435. })
  436. it('returns real path of a normal file', () => {
  437. const parent = fs.realpathSync(asarDir)
  438. const p = path.join('a.asar', 'file1')
  439. const r = fs.realpathSync(path.join(parent, p))
  440. expect(r).to.equal(path.join(parent, p))
  441. })
  442. it('returns real path of a normal directory', () => {
  443. const parent = fs.realpathSync(asarDir)
  444. const p = path.join('a.asar', 'dir1')
  445. const r = fs.realpathSync(path.join(parent, p))
  446. expect(r).to.equal(path.join(parent, p))
  447. })
  448. it('returns real path of a linked file', () => {
  449. const parent = fs.realpathSync(asarDir)
  450. const p = path.join('a.asar', 'link2', 'link1')
  451. const r = fs.realpathSync(path.join(parent, p))
  452. expect(r).to.equal(path.join(parent, 'a.asar', 'file1'))
  453. })
  454. it('returns real path of a linked directory', () => {
  455. const parent = fs.realpathSync(asarDir)
  456. const p = path.join('a.asar', 'link2', 'link2')
  457. const r = fs.realpathSync(path.join(parent, p))
  458. expect(r).to.equal(path.join(parent, 'a.asar', 'dir1'))
  459. })
  460. it('returns real path of an unpacked file', () => {
  461. const parent = fs.realpathSync(asarDir)
  462. const p = path.join('unpack.asar', 'a.txt')
  463. const r = fs.realpathSync(path.join(parent, p))
  464. expect(r).to.equal(path.join(parent, p))
  465. })
  466. it('throws ENOENT error when can not find file', () => {
  467. const parent = fs.realpathSync(asarDir)
  468. const p = path.join('a.asar', 'not-exist')
  469. expect(() => {
  470. fs.realpathSync(path.join(parent, p))
  471. }).to.throw(/ENOENT/)
  472. })
  473. })
  474. describe('fs.realpathSync.native', () => {
  475. it('returns real path root', () => {
  476. const parent = fs.realpathSync.native(asarDir)
  477. const p = 'a.asar'
  478. const r = fs.realpathSync.native(path.join(parent, p))
  479. expect(r).to.equal(path.join(parent, p))
  480. })
  481. it('returns real path of a normal file', () => {
  482. const parent = fs.realpathSync.native(asarDir)
  483. const p = path.join('a.asar', 'file1')
  484. const r = fs.realpathSync.native(path.join(parent, p))
  485. expect(r).to.equal(path.join(parent, p))
  486. })
  487. it('returns real path of a normal directory', () => {
  488. const parent = fs.realpathSync.native(asarDir)
  489. const p = path.join('a.asar', 'dir1')
  490. const r = fs.realpathSync.native(path.join(parent, p))
  491. expect(r).to.equal(path.join(parent, p))
  492. })
  493. it('returns real path of a linked file', () => {
  494. const parent = fs.realpathSync.native(asarDir)
  495. const p = path.join('a.asar', 'link2', 'link1')
  496. const r = fs.realpathSync.native(path.join(parent, p))
  497. expect(r).to.equal(path.join(parent, 'a.asar', 'file1'))
  498. })
  499. it('returns real path of a linked directory', () => {
  500. const parent = fs.realpathSync.native(asarDir)
  501. const p = path.join('a.asar', 'link2', 'link2')
  502. const r = fs.realpathSync.native(path.join(parent, p))
  503. expect(r).to.equal(path.join(parent, 'a.asar', 'dir1'))
  504. })
  505. it('returns real path of an unpacked file', () => {
  506. const parent = fs.realpathSync.native(asarDir)
  507. const p = path.join('unpack.asar', 'a.txt')
  508. const r = fs.realpathSync.native(path.join(parent, p))
  509. expect(r).to.equal(path.join(parent, p))
  510. })
  511. it('throws ENOENT error when can not find file', () => {
  512. const parent = fs.realpathSync.native(asarDir)
  513. const p = path.join('a.asar', 'not-exist')
  514. expect(() => {
  515. fs.realpathSync.native(path.join(parent, p))
  516. }).to.throw(/ENOENT/)
  517. })
  518. })
  519. describe('fs.realpath', () => {
  520. it('returns real path root', done => {
  521. const parent = fs.realpathSync(asarDir)
  522. const p = 'a.asar'
  523. fs.realpath(path.join(parent, p), (err, r) => {
  524. expect(err).to.be.null()
  525. expect(r).to.equal(path.join(parent, p))
  526. done()
  527. })
  528. })
  529. it('returns real path of a normal file', done => {
  530. const parent = fs.realpathSync(asarDir)
  531. const p = path.join('a.asar', 'file1')
  532. fs.realpath(path.join(parent, p), (err, r) => {
  533. expect(err).to.be.null()
  534. expect(r).to.equal(path.join(parent, p))
  535. done()
  536. })
  537. })
  538. it('returns real path of a normal directory', done => {
  539. const parent = fs.realpathSync(asarDir)
  540. const p = path.join('a.asar', 'dir1')
  541. fs.realpath(path.join(parent, p), (err, r) => {
  542. expect(err).to.be.null()
  543. expect(r).to.equal(path.join(parent, p))
  544. done()
  545. })
  546. })
  547. it('returns real path of a linked file', done => {
  548. const parent = fs.realpathSync(asarDir)
  549. const p = path.join('a.asar', 'link2', 'link1')
  550. fs.realpath(path.join(parent, p), (err, r) => {
  551. expect(err).to.be.null()
  552. expect(r).to.equal(path.join(parent, 'a.asar', 'file1'))
  553. done()
  554. })
  555. })
  556. it('returns real path of a linked directory', done => {
  557. const parent = fs.realpathSync(asarDir)
  558. const p = path.join('a.asar', 'link2', 'link2')
  559. fs.realpath(path.join(parent, p), (err, r) => {
  560. expect(err).to.be.null()
  561. expect(r).to.equal(path.join(parent, 'a.asar', 'dir1'))
  562. done()
  563. })
  564. })
  565. it('returns real path of an unpacked file', done => {
  566. const parent = fs.realpathSync(asarDir)
  567. const p = path.join('unpack.asar', 'a.txt')
  568. fs.realpath(path.join(parent, p), (err, r) => {
  569. expect(err).to.be.null()
  570. expect(r).to.equal(path.join(parent, p))
  571. done()
  572. })
  573. })
  574. it('throws ENOENT error when can not find file', done => {
  575. const parent = fs.realpathSync(asarDir)
  576. const p = path.join('a.asar', 'not-exist')
  577. fs.realpath(path.join(parent, p), err => {
  578. expect(err.code).to.equal('ENOENT')
  579. done()
  580. })
  581. })
  582. })
  583. describe('fs.promises.realpath', () => {
  584. it('returns real path root', async () => {
  585. const parent = fs.realpathSync(asarDir)
  586. const p = 'a.asar'
  587. const r = await fs.promises.realpath(path.join(parent, p))
  588. expect(r).to.equal(path.join(parent, p))
  589. })
  590. it('returns real path of a normal file', async () => {
  591. const parent = fs.realpathSync(asarDir)
  592. const p = path.join('a.asar', 'file1')
  593. const r = await fs.promises.realpath(path.join(parent, p))
  594. expect(r).to.equal(path.join(parent, p))
  595. })
  596. it('returns real path of a normal directory', async () => {
  597. const parent = fs.realpathSync(asarDir)
  598. const p = path.join('a.asar', 'dir1')
  599. const r = await fs.promises.realpath(path.join(parent, p))
  600. expect(r).to.equal(path.join(parent, p))
  601. })
  602. it('returns real path of a linked file', async () => {
  603. const parent = fs.realpathSync(asarDir)
  604. const p = path.join('a.asar', 'link2', 'link1')
  605. const r = await fs.promises.realpath(path.join(parent, p))
  606. expect(r).to.equal(path.join(parent, 'a.asar', 'file1'))
  607. })
  608. it('returns real path of a linked directory', async () => {
  609. const parent = fs.realpathSync(asarDir)
  610. const p = path.join('a.asar', 'link2', 'link2')
  611. const r = await fs.promises.realpath(path.join(parent, p))
  612. expect(r).to.equal(path.join(parent, 'a.asar', 'dir1'))
  613. })
  614. it('returns real path of an unpacked file', async () => {
  615. const parent = fs.realpathSync(asarDir)
  616. const p = path.join('unpack.asar', 'a.txt')
  617. const r = await fs.promises.realpath(path.join(parent, p))
  618. expect(r).to.equal(path.join(parent, p))
  619. })
  620. it('throws ENOENT error when can not find file', async () => {
  621. const parent = fs.realpathSync(asarDir)
  622. const p = path.join('a.asar', 'not-exist')
  623. await expectToThrowErrorWithCode(() => fs.promises.realpath(path.join(parent, p)), 'ENOENT')
  624. })
  625. })
  626. describe('fs.realpath.native', () => {
  627. it('returns real path root', done => {
  628. const parent = fs.realpathSync.native(asarDir)
  629. const p = 'a.asar'
  630. fs.realpath.native(path.join(parent, p), (err, r) => {
  631. expect(err).to.be.null()
  632. expect(r).to.equal(path.join(parent, p))
  633. done()
  634. })
  635. })
  636. it('returns real path of a normal file', done => {
  637. const parent = fs.realpathSync.native(asarDir)
  638. const p = path.join('a.asar', 'file1')
  639. fs.realpath.native(path.join(parent, p), (err, r) => {
  640. expect(err).to.be.null()
  641. expect(r).to.equal(path.join(parent, p))
  642. done()
  643. })
  644. })
  645. it('returns real path of a normal directory', done => {
  646. const parent = fs.realpathSync.native(asarDir)
  647. const p = path.join('a.asar', 'dir1')
  648. fs.realpath.native(path.join(parent, p), (err, r) => {
  649. expect(err).to.be.null()
  650. expect(r).to.equal(path.join(parent, p))
  651. done()
  652. })
  653. })
  654. it('returns real path of a linked file', done => {
  655. const parent = fs.realpathSync.native(asarDir)
  656. const p = path.join('a.asar', 'link2', 'link1')
  657. fs.realpath.native(path.join(parent, p), (err, r) => {
  658. expect(err).to.be.null()
  659. expect(r).to.equal(path.join(parent, 'a.asar', 'file1'))
  660. done()
  661. })
  662. })
  663. it('returns real path of a linked directory', done => {
  664. const parent = fs.realpathSync.native(asarDir)
  665. const p = path.join('a.asar', 'link2', 'link2')
  666. fs.realpath.native(path.join(parent, p), (err, r) => {
  667. expect(err).to.be.null()
  668. expect(r).to.equal(path.join(parent, 'a.asar', 'dir1'))
  669. done()
  670. })
  671. })
  672. it('returns real path of an unpacked file', done => {
  673. const parent = fs.realpathSync.native(asarDir)
  674. const p = path.join('unpack.asar', 'a.txt')
  675. fs.realpath.native(path.join(parent, p), (err, r) => {
  676. expect(err).to.be.null()
  677. expect(r).to.equal(path.join(parent, p))
  678. done()
  679. })
  680. })
  681. it('throws ENOENT error when can not find file', done => {
  682. const parent = fs.realpathSync.native(asarDir)
  683. const p = path.join('a.asar', 'not-exist')
  684. fs.realpath.native(path.join(parent, p), err => {
  685. expect(err.code).to.equal('ENOENT')
  686. done()
  687. })
  688. })
  689. })
  690. describe('fs.readdirSync', function () {
  691. it('reads dirs from root', function () {
  692. const p = path.join(asarDir, 'a.asar')
  693. const dirs = fs.readdirSync(p)
  694. expect(dirs).to.deep.equal(['dir1', 'dir2', 'dir3', 'file1', 'file2', 'file3', 'link1', 'link2', 'ping.js'])
  695. })
  696. it('reads dirs from a normal dir', function () {
  697. const p = path.join(asarDir, 'a.asar', 'dir1')
  698. const dirs = fs.readdirSync(p)
  699. expect(dirs).to.deep.equal(['file1', 'file2', 'file3', 'link1', 'link2'])
  700. })
  701. it('reads dirs from a linked dir', function () {
  702. const p = path.join(asarDir, 'a.asar', 'link2', 'link2')
  703. const dirs = fs.readdirSync(p)
  704. expect(dirs).to.deep.equal(['file1', 'file2', 'file3', 'link1', 'link2'])
  705. })
  706. it('throws ENOENT error when can not find file', function () {
  707. const p = path.join(asarDir, 'a.asar', 'not-exist')
  708. expect(() => {
  709. fs.readdirSync(p)
  710. }).to.throw(/ENOENT/)
  711. })
  712. })
  713. describe('fs.readdir', function () {
  714. it('reads dirs from root', function (done) {
  715. const p = path.join(asarDir, 'a.asar')
  716. fs.readdir(p, function (err, dirs) {
  717. expect(err).to.be.null()
  718. expect(dirs).to.deep.equal(['dir1', 'dir2', 'dir3', 'file1', 'file2', 'file3', 'link1', 'link2', 'ping.js'])
  719. done()
  720. })
  721. })
  722. it('reads dirs from a normal dir', function (done) {
  723. const p = path.join(asarDir, 'a.asar', 'dir1')
  724. fs.readdir(p, function (err, dirs) {
  725. expect(err).to.be.null()
  726. expect(dirs).to.deep.equal(['file1', 'file2', 'file3', 'link1', 'link2'])
  727. done()
  728. })
  729. })
  730. it('reads dirs from a linked dir', function (done) {
  731. const p = path.join(asarDir, 'a.asar', 'link2', 'link2')
  732. fs.readdir(p, function (err, dirs) {
  733. expect(err).to.be.null()
  734. expect(dirs).to.deep.equal(['file1', 'file2', 'file3', 'link1', 'link2'])
  735. done()
  736. })
  737. })
  738. it('throws ENOENT error when can not find file', function (done) {
  739. const p = path.join(asarDir, 'a.asar', 'not-exist')
  740. fs.readdir(p, function (err) {
  741. expect(err.code).to.equal('ENOENT')
  742. done()
  743. })
  744. })
  745. })
  746. describe('fs.promises.readdir', function () {
  747. it('reads dirs from root', async function () {
  748. const p = path.join(asarDir, 'a.asar')
  749. const dirs = await fs.promises.readdir(p)
  750. expect(dirs).to.deep.equal(['dir1', 'dir2', 'dir3', 'file1', 'file2', 'file3', 'link1', 'link2', 'ping.js'])
  751. })
  752. it('reads dirs from a normal dir', async function () {
  753. const p = path.join(asarDir, 'a.asar', 'dir1')
  754. const dirs = await fs.promises.readdir(p)
  755. expect(dirs).to.deep.equal(['file1', 'file2', 'file3', 'link1', 'link2'])
  756. })
  757. it('reads dirs from a linked dir', async function () {
  758. const p = path.join(asarDir, 'a.asar', 'link2', 'link2')
  759. const dirs = await fs.promises.readdir(p)
  760. expect(dirs).to.deep.equal(['file1', 'file2', 'file3', 'link1', 'link2'])
  761. })
  762. it('throws ENOENT error when can not find file', async function () {
  763. const p = path.join(asarDir, 'a.asar', 'not-exist')
  764. await expectToThrowErrorWithCode(() => fs.promises.readdir(p), 'ENOENT')
  765. })
  766. })
  767. describe('fs.openSync', function () {
  768. it('opens a normal/linked/under-linked-directory file', function () {
  769. const ref2 = ['file1', 'link1', path.join('link2', 'file1')]
  770. for (let j = 0, len = ref2.length; j < len; j++) {
  771. const file = ref2[j]
  772. const p = path.join(asarDir, 'a.asar', file)
  773. const fd = fs.openSync(p, 'r')
  774. const buffer = Buffer.alloc(6)
  775. fs.readSync(fd, buffer, 0, 6, 0)
  776. expect(String(buffer).trim()).to.equal('file1')
  777. fs.closeSync(fd)
  778. }
  779. })
  780. it('throws ENOENT error when can not find file', function () {
  781. const p = path.join(asarDir, 'a.asar', 'not-exist')
  782. expect(() => {
  783. fs.openSync(p)
  784. }).to.throw(/ENOENT/)
  785. })
  786. })
  787. describe('fs.open', function () {
  788. it('opens a normal file', function (done) {
  789. const p = path.join(asarDir, 'a.asar', 'file1')
  790. fs.open(p, 'r', function (err, fd) {
  791. expect(err).to.be.null()
  792. const buffer = Buffer.alloc(6)
  793. fs.read(fd, buffer, 0, 6, 0, function (err) {
  794. expect(err).to.be.null()
  795. expect(String(buffer).trim()).to.equal('file1')
  796. fs.close(fd, done)
  797. })
  798. })
  799. })
  800. it('throws ENOENT error when can not find file', function (done) {
  801. const p = path.join(asarDir, 'a.asar', 'not-exist')
  802. fs.open(p, 'r', function (err) {
  803. expect(err.code).to.equal('ENOENT')
  804. done()
  805. })
  806. })
  807. })
  808. describe('fs.promises.open', function () {
  809. it('opens a normal file', async function () {
  810. const p = path.join(asarDir, 'a.asar', 'file1')
  811. const fh = await fs.promises.open(p, 'r')
  812. const buffer = Buffer.alloc(6)
  813. await fh.read(buffer, 0, 6, 0)
  814. expect(String(buffer).trim()).to.equal('file1')
  815. await fh.close()
  816. })
  817. it('throws ENOENT error when can not find file', async function () {
  818. const p = path.join(asarDir, 'a.asar', 'not-exist')
  819. await expectToThrowErrorWithCode(() => fs.promises.open(p, 'r'), 'ENOENT')
  820. })
  821. })
  822. describe('fs.mkdir', function () {
  823. it('throws error when calling inside asar archive', function (done) {
  824. const p = path.join(asarDir, 'a.asar', 'not-exist')
  825. fs.mkdir(p, function (err) {
  826. expect(err.code).to.equal('ENOTDIR')
  827. done()
  828. })
  829. })
  830. })
  831. describe('fs.promises.mkdir', function () {
  832. it('throws error when calling inside asar archive', async function () {
  833. const p = path.join(asarDir, 'a.asar', 'not-exist')
  834. await expectToThrowErrorWithCode(() => fs.promises.mkdir(p), 'ENOTDIR')
  835. })
  836. })
  837. describe('fs.mkdirSync', function () {
  838. it('throws error when calling inside asar archive', function () {
  839. const p = path.join(asarDir, 'a.asar', 'not-exist')
  840. expect(() => {
  841. fs.mkdirSync(p)
  842. }).to.throw(/ENOTDIR/)
  843. })
  844. })
  845. describe('fs.exists', function () {
  846. it('handles an existing file', function (done) {
  847. const p = path.join(asarDir, 'a.asar', 'file1')
  848. // eslint-disable-next-line
  849. fs.exists(p, function (exists) {
  850. expect(exists).to.be.true()
  851. done()
  852. })
  853. })
  854. it('handles a non-existent file', function (done) {
  855. const p = path.join(asarDir, 'a.asar', 'not-exist')
  856. // eslint-disable-next-line
  857. fs.exists(p, function (exists) {
  858. expect(exists).to.be.false()
  859. done()
  860. })
  861. })
  862. it('promisified version handles an existing file', (done) => {
  863. const p = path.join(asarDir, 'a.asar', 'file1')
  864. // eslint-disable-next-line
  865. util.promisify(fs.exists)(p).then(exists => {
  866. expect(exists).to.be.true()
  867. done()
  868. })
  869. })
  870. it('promisified version handles a non-existent file', function (done) {
  871. const p = path.join(asarDir, 'a.asar', 'not-exist')
  872. // eslint-disable-next-line
  873. util.promisify(fs.exists)(p).then(exists => {
  874. expect(exists).to.be.false()
  875. done()
  876. })
  877. })
  878. })
  879. describe('fs.existsSync', function () {
  880. it('handles an existing file', function () {
  881. const p = path.join(asarDir, 'a.asar', 'file1')
  882. expect(fs.existsSync(p)).to.be.true()
  883. })
  884. it('handles a non-existent file', function () {
  885. const p = path.join(asarDir, 'a.asar', 'not-exist')
  886. expect(fs.existsSync(p)).to.be.false()
  887. })
  888. })
  889. describe('fs.access', function () {
  890. it('accesses a normal file', function (done) {
  891. const p = path.join(asarDir, 'a.asar', 'file1')
  892. fs.access(p, function (err) {
  893. expect(err).to.be.undefined()
  894. done()
  895. })
  896. })
  897. it('throws an error when called with write mode', function (done) {
  898. const p = path.join(asarDir, 'a.asar', 'file1')
  899. fs.access(p, fs.constants.R_OK | fs.constants.W_OK, function (err) {
  900. expect(err.code).to.equal('EACCES')
  901. done()
  902. })
  903. })
  904. it('throws an error when called on non-existent file', function (done) {
  905. const p = path.join(asarDir, 'a.asar', 'not-exist')
  906. fs.access(p, function (err) {
  907. expect(err.code).to.equal('ENOENT')
  908. done()
  909. })
  910. })
  911. it('allows write mode for unpacked files', function (done) {
  912. const p = path.join(asarDir, 'unpack.asar', 'a.txt')
  913. fs.access(p, fs.constants.R_OK | fs.constants.W_OK, function (err) {
  914. expect(err).to.be.null()
  915. done()
  916. })
  917. })
  918. })
  919. describe('fs.promises.access', function () {
  920. it('accesses a normal file', async function () {
  921. const p = path.join(asarDir, 'a.asar', 'file1')
  922. await fs.promises.access(p)
  923. })
  924. it('throws an error when called with write mode', async function () {
  925. const p = path.join(asarDir, 'a.asar', 'file1')
  926. await expectToThrowErrorWithCode(() => fs.promises.access(p, fs.constants.R_OK | fs.constants.W_OK), 'EACCES')
  927. })
  928. it('throws an error when called on non-existent file', async function () {
  929. const p = path.join(asarDir, 'a.asar', 'not-exist')
  930. await expectToThrowErrorWithCode(() => fs.promises.access(p), 'ENOENT')
  931. })
  932. it('allows write mode for unpacked files', async function () {
  933. const p = path.join(asarDir, 'unpack.asar', 'a.txt')
  934. await fs.promises.access(p, fs.constants.R_OK | fs.constants.W_OK)
  935. })
  936. })
  937. describe('fs.accessSync', function () {
  938. it('accesses a normal file', function () {
  939. const p = path.join(asarDir, 'a.asar', 'file1')
  940. expect(() => {
  941. fs.accessSync(p)
  942. }).to.not.throw()
  943. })
  944. it('throws an error when called with write mode', function () {
  945. const p = path.join(asarDir, 'a.asar', 'file1')
  946. expect(() => {
  947. fs.accessSync(p, fs.constants.R_OK | fs.constants.W_OK)
  948. }).to.throw(/EACCES/)
  949. })
  950. it('throws an error when called on non-existent file', function () {
  951. const p = path.join(asarDir, 'a.asar', 'not-exist')
  952. expect(() => {
  953. fs.accessSync(p)
  954. }).to.throw(/ENOENT/)
  955. })
  956. it('allows write mode for unpacked files', function () {
  957. const p = path.join(asarDir, 'unpack.asar', 'a.txt')
  958. expect(() => {
  959. fs.accessSync(p, fs.constants.R_OK | fs.constants.W_OK)
  960. }).to.not.throw()
  961. })
  962. })
  963. describe('child_process.fork', function () {
  964. before(function () {
  965. if (!features.isRunAsNodeEnabled()) {
  966. this.skip()
  967. }
  968. })
  969. it('opens a normal js file', function (done) {
  970. const child = ChildProcess.fork(path.join(asarDir, 'a.asar', 'ping.js'))
  971. child.on('message', function (msg) {
  972. expect(msg).to.equal('message')
  973. done()
  974. })
  975. child.send('message')
  976. })
  977. it('supports asar in the forked js', function (done) {
  978. const file = path.join(asarDir, 'a.asar', 'file1')
  979. const child = ChildProcess.fork(path.join(fixtures, 'module', 'asar.js'))
  980. child.on('message', function (content) {
  981. expect(content).to.equal(fs.readFileSync(file).toString())
  982. done()
  983. })
  984. child.send(file)
  985. })
  986. })
  987. describe('child_process.exec', function () {
  988. const echo = path.join(asarDir, 'echo.asar', 'echo')
  989. it('should not try to extract the command if there is a reference to a file inside an .asar', function (done) {
  990. ChildProcess.exec('echo ' + echo + ' foo bar', function (error, stdout) {
  991. expect(error).to.be.null()
  992. expect(stdout.toString().replace(/\r/g, '')).to.equal(echo + ' foo bar\n')
  993. done()
  994. })
  995. })
  996. it('can be promisified', () => {
  997. return util.promisify(ChildProcess.exec)('echo ' + echo + ' foo bar').then(({ stdout }) => {
  998. expect(stdout.toString().replace(/\r/g, '')).to.equal(echo + ' foo bar\n')
  999. })
  1000. })
  1001. })
  1002. describe('child_process.execSync', function () {
  1003. const echo = path.join(asarDir, 'echo.asar', 'echo')
  1004. it('should not try to extract the command if there is a reference to a file inside an .asar', function (done) {
  1005. const stdout = ChildProcess.execSync('echo ' + echo + ' foo bar')
  1006. expect(stdout.toString().replace(/\r/g, '')).to.equal(echo + ' foo bar\n')
  1007. done()
  1008. })
  1009. })
  1010. describe('child_process.execFile', function () {
  1011. const execFile = ChildProcess.execFile
  1012. const execFileSync = ChildProcess.execFileSync
  1013. const echo = path.join(asarDir, 'echo.asar', 'echo')
  1014. before(function () {
  1015. if (process.platform !== 'darwin') {
  1016. this.skip()
  1017. }
  1018. })
  1019. it('executes binaries', function (done) {
  1020. execFile(echo, ['test'], function (error, stdout) {
  1021. expect(error).to.be.null()
  1022. expect(stdout).to.equal('test\n')
  1023. done()
  1024. })
  1025. })
  1026. it('executes binaries without callback', function (done) {
  1027. const process = execFile(echo, ['test'])
  1028. process.on('close', function (code) {
  1029. expect(code).to.equal(0)
  1030. done()
  1031. })
  1032. process.on('error', function () {
  1033. expect.fail()
  1034. done()
  1035. })
  1036. })
  1037. it('execFileSync executes binaries', function () {
  1038. const output = execFileSync(echo, ['test'])
  1039. expect(String(output)).to.equal('test\n')
  1040. })
  1041. it('can be promisified', () => {
  1042. return util.promisify(ChildProcess.execFile)(echo, ['test']).then(({ stdout }) => {
  1043. expect(stdout).to.equal('test\n')
  1044. })
  1045. })
  1046. })
  1047. describe('internalModuleReadJSON', function () {
  1048. const internalModuleReadJSON = process.binding('fs').internalModuleReadJSON
  1049. it('read a normal file', function () {
  1050. const file1 = path.join(asarDir, 'a.asar', 'file1')
  1051. expect(internalModuleReadJSON(file1).toString().trim()).to.equal('file1')
  1052. const file2 = path.join(asarDir, 'a.asar', 'file2')
  1053. expect(internalModuleReadJSON(file2).toString().trim()).to.equal('file2')
  1054. const file3 = path.join(asarDir, 'a.asar', 'file3')
  1055. expect(internalModuleReadJSON(file3).toString().trim()).to.equal('file3')
  1056. })
  1057. it('reads a normal file with unpacked files', function () {
  1058. const p = path.join(asarDir, 'unpack.asar', 'a.txt')
  1059. expect(internalModuleReadJSON(p).toString().trim()).to.equal('a')
  1060. })
  1061. })
  1062. describe('util.promisify', function () {
  1063. it('can promisify all fs functions', function () {
  1064. const originalFs = require('original-fs')
  1065. const { hasOwnProperty } = Object.prototype
  1066. for (const [propertyName, originalValue] of Object.entries(originalFs)) {
  1067. // Some properties exist but have a value of `undefined` on some platforms.
  1068. // E.g. `fs.lchmod`, which in only available on MacOS, see
  1069. // https://nodejs.org/docs/latest-v10.x/api/fs.html#fs_fs_lchmod_path_mode_callback
  1070. // Also check for `null`s, `hasOwnProperty()` can't handle them.
  1071. if (typeof originalValue === 'undefined' || originalValue === null) continue
  1072. if (hasOwnProperty.call(originalValue, util.promisify.custom)) {
  1073. expect(fs).to.have.own.property(propertyName)
  1074. .that.has.own.property(util.promisify.custom)
  1075. }
  1076. }
  1077. })
  1078. })
  1079. describe('process.noAsar', function () {
  1080. const errorName = process.platform === 'win32' ? 'ENOENT' : 'ENOTDIR'
  1081. beforeEach(function () {
  1082. process.noAsar = true
  1083. })
  1084. afterEach(function () {
  1085. process.noAsar = false
  1086. })
  1087. it('disables asar support in sync API', function () {
  1088. const file = path.join(asarDir, 'a.asar', 'file1')
  1089. const dir = path.join(asarDir, 'a.asar', 'dir1')
  1090. expect(() => {
  1091. fs.readFileSync(file)
  1092. }).to.throw(new RegExp(errorName))
  1093. expect(() => {
  1094. fs.lstatSync(file)
  1095. }).to.throw(new RegExp(errorName))
  1096. expect(() => {
  1097. fs.realpathSync(file)
  1098. }).to.throw(new RegExp(errorName))
  1099. expect(() => {
  1100. fs.readdirSync(dir)
  1101. }).to.throw(new RegExp(errorName))
  1102. })
  1103. it('disables asar support in async API', function (done) {
  1104. const file = path.join(asarDir, 'a.asar', 'file1')
  1105. const dir = path.join(asarDir, 'a.asar', 'dir1')
  1106. fs.readFile(file, function (error) {
  1107. expect(error.code).to.equal(errorName)
  1108. fs.lstat(file, function (error) {
  1109. expect(error.code).to.equal(errorName)
  1110. fs.realpath(file, function (error) {
  1111. expect(error.code).to.equal(errorName)
  1112. fs.readdir(dir, function (error) {
  1113. expect(error.code).to.equal(errorName)
  1114. done()
  1115. })
  1116. })
  1117. })
  1118. })
  1119. })
  1120. it('disables asar support in promises API', async function () {
  1121. const file = path.join(asarDir, 'a.asar', 'file1')
  1122. const dir = path.join(asarDir, 'a.asar', 'dir1')
  1123. await expect(fs.promises.readFile(file)).to.be.eventually.rejectedWith(Error, new RegExp(errorName))
  1124. await expect(fs.promises.lstat(file)).to.be.eventually.rejectedWith(Error, new RegExp(errorName))
  1125. await expect(fs.promises.realpath(file)).to.be.eventually.rejectedWith(Error, new RegExp(errorName))
  1126. await expect(fs.promises.readdir(dir)).to.be.eventually.rejectedWith(Error, new RegExp(errorName))
  1127. })
  1128. it('treats *.asar as normal file', function () {
  1129. const originalFs = require('original-fs')
  1130. const asar = path.join(asarDir, 'a.asar')
  1131. const content1 = fs.readFileSync(asar)
  1132. const content2 = originalFs.readFileSync(asar)
  1133. expect(content1.compare(content2)).to.equal(0)
  1134. expect(() => {
  1135. fs.readdirSync(asar)
  1136. }).to.throw(/ENOTDIR/)
  1137. })
  1138. it('is reset to its original value when execSync throws an error', function () {
  1139. process.noAsar = false
  1140. expect(() => {
  1141. ChildProcess.execSync(path.join(__dirname, 'does-not-exist.txt'))
  1142. }).to.throw()
  1143. expect(process.noAsar).to.be.false()
  1144. })
  1145. })
  1146. describe('process.env.ELECTRON_NO_ASAR', function () {
  1147. before(function () {
  1148. if (!features.isRunAsNodeEnabled()) {
  1149. this.skip()
  1150. }
  1151. })
  1152. it('disables asar support in forked processes', function (done) {
  1153. const forked = ChildProcess.fork(path.join(__dirname, 'fixtures', 'module', 'no-asar.js'), [], {
  1154. env: {
  1155. ELECTRON_NO_ASAR: true
  1156. }
  1157. })
  1158. forked.on('message', function (stats) {
  1159. expect(stats.isFile).to.be.true()
  1160. expect(stats.size).to.equal(778)
  1161. done()
  1162. })
  1163. })
  1164. it('disables asar support in spawned processes', function (done) {
  1165. const spawned = ChildProcess.spawn(process.execPath, [path.join(__dirname, 'fixtures', 'module', 'no-asar.js')], {
  1166. env: {
  1167. ELECTRON_NO_ASAR: true,
  1168. ELECTRON_RUN_AS_NODE: true
  1169. }
  1170. })
  1171. let output = ''
  1172. spawned.stdout.on('data', function (data) {
  1173. output += data
  1174. })
  1175. spawned.stdout.on('close', function () {
  1176. const stats = JSON.parse(output)
  1177. expect(stats.isFile).to.be.true()
  1178. expect(stats.size).to.equal(778)
  1179. done()
  1180. })
  1181. })
  1182. })
  1183. })
  1184. describe('asar protocol', function () {
  1185. it('can request a file in package', function (done) {
  1186. const p = path.resolve(asarDir, 'a.asar', 'file1')
  1187. $.get('file://' + p, function (data) {
  1188. expect(data.trim()).to.equal('file1')
  1189. done()
  1190. })
  1191. })
  1192. it('can request a file in package with unpacked files', function (done) {
  1193. const p = path.resolve(asarDir, 'unpack.asar', 'a.txt')
  1194. $.get('file://' + p, function (data) {
  1195. expect(data.trim()).to.equal('a')
  1196. done()
  1197. })
  1198. })
  1199. it('can request a linked file in package', function (done) {
  1200. const p = path.resolve(asarDir, 'a.asar', 'link2', 'link1')
  1201. $.get('file://' + p, function (data) {
  1202. expect(data.trim()).to.equal('file1')
  1203. done()
  1204. })
  1205. })
  1206. it('can request a file in filesystem', function (done) {
  1207. const p = path.resolve(asarDir, 'file')
  1208. $.get('file://' + p, function (data) {
  1209. expect(data.trim()).to.equal('file')
  1210. done()
  1211. })
  1212. })
  1213. it('gets 404 when file is not found', function (done) {
  1214. const p = path.resolve(asarDir, 'a.asar', 'no-exist')
  1215. $.ajax({
  1216. url: 'file://' + p,
  1217. error: function (err) {
  1218. expect(err.status).to.equal(404)
  1219. done()
  1220. }
  1221. })
  1222. })
  1223. })
  1224. describe('original-fs module', function () {
  1225. const originalFs = require('original-fs')
  1226. it('treats .asar as file', function () {
  1227. const file = path.join(asarDir, 'a.asar')
  1228. const stats = originalFs.statSync(file)
  1229. expect(stats.isFile()).to.be.true()
  1230. })
  1231. it('is available in forked scripts', function (done) {
  1232. if (!features.isRunAsNodeEnabled()) {
  1233. this.skip()
  1234. done()
  1235. }
  1236. const child = ChildProcess.fork(path.join(fixtures, 'module', 'original-fs.js'))
  1237. child.on('message', function (msg) {
  1238. expect(msg).to.equal('object')
  1239. done()
  1240. })
  1241. child.send('message')
  1242. })
  1243. it('can be used with streams', () => {
  1244. originalFs.createReadStream(path.join(asarDir, 'a.asar'))
  1245. })
  1246. it('has the same APIs as fs', function () {
  1247. expect(Object.keys(require('fs'))).to.deep.equal(Object.keys(require('original-fs')))
  1248. expect(Object.keys(require('fs').promises)).to.deep.equal(Object.keys(require('original-fs').promises))
  1249. })
  1250. })
  1251. describe('graceful-fs module', function () {
  1252. const gfs = require('graceful-fs')
  1253. it('recognize asar archvies', function () {
  1254. const p = path.join(asarDir, 'a.asar', 'link1')
  1255. expect(gfs.readFileSync(p).toString().trim()).to.equal('file1')
  1256. })
  1257. it('does not touch global fs object', function () {
  1258. expect(fs.readdir).to.not.equal(gfs.readdir)
  1259. })
  1260. })
  1261. describe('mkdirp module', function () {
  1262. const mkdirp = require('mkdirp')
  1263. it('throws error when calling inside asar archive', function () {
  1264. const p = path.join(asarDir, 'a.asar', 'not-exist')
  1265. expect(() => {
  1266. mkdirp.sync(p)
  1267. }).to.throw(/ENOTDIR/)
  1268. })
  1269. })
  1270. describe('native-image', function () {
  1271. it('reads image from asar archive', function () {
  1272. const p = path.join(asarDir, 'logo.asar', 'logo.png')
  1273. const logo = nativeImage.createFromPath(p)
  1274. expect(logo.getSize()).to.deep.equal({
  1275. width: 55,
  1276. height: 55
  1277. })
  1278. })
  1279. it('reads image from asar archive with unpacked files', function () {
  1280. const p = path.join(asarDir, 'unpack.asar', 'atom.png')
  1281. const logo = nativeImage.createFromPath(p)
  1282. expect(logo.getSize()).to.deep.equal({
  1283. width: 1024,
  1284. height: 1024
  1285. })
  1286. })
  1287. })
  1288. })