asar-spec.ts 66 KB

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