asar-spec.ts 62 KB

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