asar-spec.ts 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594
  1. import { expect } from 'chai';
  2. import * as path from 'path';
  3. import * as url from 'url';
  4. import { Worker } from '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 'fs';
  9. import { once } from 'events';
  10. const features = process._linkedBinding('electron_common_features');
  11. describe('asar package', () => {
  12. const fixtures = path.join(__dirname, 'fixtures');
  13. const asarDir = path.join(fixtures, 'test.asar');
  14. afterEach(closeAllWindows);
  15. describe('asar protocol', () => {
  16. it('sets __dirname correctly', async function () {
  17. after(function () {
  18. ipcMain.removeAllListeners('dirname');
  19. });
  20. const w = new BrowserWindow({
  21. show: false,
  22. width: 400,
  23. height: 400,
  24. webPreferences: {
  25. nodeIntegration: true,
  26. contextIsolation: false
  27. }
  28. });
  29. const p = path.resolve(asarDir, 'web.asar', 'index.html');
  30. const dirnameEvent = once(ipcMain, 'dirname');
  31. w.loadFile(p);
  32. const [, dirname] = await dirnameEvent;
  33. expect(dirname).to.equal(path.dirname(p));
  34. });
  35. it('loads script tag in html', async function () {
  36. after(function () {
  37. ipcMain.removeAllListeners('ping');
  38. });
  39. const w = new BrowserWindow({
  40. show: false,
  41. width: 400,
  42. height: 400,
  43. webPreferences: {
  44. nodeIntegration: true,
  45. contextIsolation: false
  46. }
  47. });
  48. const p = path.resolve(asarDir, 'script.asar', 'index.html');
  49. const ping = once(ipcMain, 'ping');
  50. w.loadFile(p);
  51. const [, message] = await ping;
  52. expect(message).to.equal('pong');
  53. });
  54. it('loads video tag in html', async function () {
  55. this.timeout(60000);
  56. after(function () {
  57. ipcMain.removeAllListeners('asar-video');
  58. });
  59. const w = new BrowserWindow({
  60. show: false,
  61. width: 400,
  62. height: 400,
  63. webPreferences: {
  64. nodeIntegration: true,
  65. contextIsolation: false
  66. }
  67. });
  68. const p = path.resolve(asarDir, 'video.asar', 'index.html');
  69. w.loadFile(p);
  70. const [, message, error] = await once(ipcMain, 'asar-video');
  71. if (message === 'ended') {
  72. expect(error).to.be.null();
  73. } else if (message === 'error') {
  74. throw new Error(error);
  75. }
  76. });
  77. });
  78. describe('worker', () => {
  79. it('Worker can load asar file', async () => {
  80. const w = new BrowserWindow({ show: false });
  81. await w.loadFile(path.join(fixtures, 'workers', 'load_worker.html'));
  82. const workerUrl = url.format({
  83. pathname: path.resolve(fixtures, 'workers', 'workers.asar', 'worker.js').replace(/\\/g, '/'),
  84. protocol: 'file',
  85. slashes: true
  86. });
  87. const result = await w.webContents.executeJavaScript(`loadWorker('${workerUrl}')`);
  88. expect(result).to.equal('success');
  89. });
  90. it('SharedWorker can load asar file', async () => {
  91. const w = new BrowserWindow({ show: false });
  92. await w.loadFile(path.join(fixtures, 'workers', 'load_shared_worker.html'));
  93. const workerUrl = url.format({
  94. pathname: path.resolve(fixtures, 'workers', 'workers.asar', 'shared_worker.js').replace(/\\/g, '/'),
  95. protocol: 'file',
  96. slashes: true
  97. });
  98. const result = await w.webContents.executeJavaScript(`loadSharedWorker('${workerUrl}')`);
  99. expect(result).to.equal('success');
  100. });
  101. });
  102. describe('worker threads', function () {
  103. // DISABLED-FIXME(#38192): only disabled for ASan.
  104. ifit(!process.env.IS_ASAN)('should start worker thread from asar file', function (callback) {
  105. const p = path.join(asarDir, 'worker_threads.asar', 'worker.js');
  106. const w = new Worker(p);
  107. w.on('error', (err) => callback(err));
  108. w.on('message', (message) => {
  109. expect(message).to.equal('ping');
  110. w.terminate();
  111. callback(null);
  112. });
  113. });
  114. });
  115. });
  116. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  117. async function expectToThrowErrorWithCode (_func: Function, _code: string) {
  118. /* dummy for typescript */
  119. }
  120. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  121. function promisify (_f: Function): any {
  122. /* dummy for typescript */
  123. }
  124. describe('asar package', function () {
  125. const fixtures = path.join(__dirname, 'fixtures');
  126. const asarDir = path.join(fixtures, 'test.asar');
  127. const fs = require('fs') as typeof importedFs; // dummy, to fool typescript
  128. useRemoteContext({
  129. url: url.pathToFileURL(path.join(fixtures, 'pages', 'blank.html')),
  130. setup: `
  131. async function expectToThrowErrorWithCode (func, code) {
  132. let error;
  133. try {
  134. await func();
  135. } catch (e) {
  136. error = e;
  137. }
  138. const chai = require('chai')
  139. chai.expect(error).to.have.property('code').which.equals(code);
  140. }
  141. fs = require('fs')
  142. path = require('path')
  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. });
  435. describe('fs.lstat', function () {
  436. itremote('handles path with trailing slash correctly', async function () {
  437. const p = path.join(asarDir, 'a.asar', 'link2', 'link2', 'file1');
  438. await promisify(fs.lstat)(p + '/');
  439. });
  440. itremote('returns information of root', async function () {
  441. const p = path.join(asarDir, 'a.asar');
  442. const stats = await promisify(fs.lstat)(p);
  443. expect(stats.isFile()).to.be.false();
  444. expect(stats.isDirectory()).to.be.true();
  445. expect(stats.isSymbolicLink()).to.be.false();
  446. expect(stats.size).to.equal(0);
  447. });
  448. itremote('returns information of root with stats as bigint', async function () {
  449. const p = path.join(asarDir, 'a.asar');
  450. const stats = await promisify(fs.lstat)(p, { bigint: false });
  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 a normal file', async function () {
  457. const p = path.join(asarDir, 'a.asar', 'link2', 'file1');
  458. const stats = await promisify(fs.lstat)(p);
  459. expect(stats.isFile()).to.be.true();
  460. expect(stats.isDirectory()).to.be.false();
  461. expect(stats.isSymbolicLink()).to.be.false();
  462. expect(stats.size).to.equal(6);
  463. });
  464. itremote('returns information of a normal directory', async function () {
  465. const p = path.join(asarDir, 'a.asar', 'dir1');
  466. const stats = await promisify(fs.lstat)(p);
  467. expect(stats.isFile()).to.be.false();
  468. expect(stats.isDirectory()).to.be.true();
  469. expect(stats.isSymbolicLink()).to.be.false();
  470. expect(stats.size).to.equal(0);
  471. });
  472. itremote('returns information of a linked file', async function () {
  473. const p = path.join(asarDir, 'a.asar', 'link2', 'link1');
  474. const stats = await promisify(fs.lstat)(p);
  475. expect(stats.isFile()).to.be.false();
  476. expect(stats.isDirectory()).to.be.false();
  477. expect(stats.isSymbolicLink()).to.be.true();
  478. expect(stats.size).to.equal(0);
  479. });
  480. itremote('returns information of a linked directory', async function () {
  481. const p = path.join(asarDir, 'a.asar', 'link2', 'link2');
  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('throws ENOENT error when can not find file', async function () {
  489. const p = path.join(asarDir, 'a.asar', 'file4');
  490. const err = await new Promise<any>(resolve => fs.lstat(p, resolve));
  491. expect(err.code).to.equal('ENOENT');
  492. });
  493. });
  494. describe('fs.promises.lstat', function () {
  495. itremote('handles path with trailing slash correctly', async function () {
  496. const p = path.join(asarDir, 'a.asar', 'link2', 'link2', 'file1');
  497. await fs.promises.lstat(p + '/');
  498. });
  499. itremote('returns information of root', async function () {
  500. const p = path.join(asarDir, 'a.asar');
  501. const stats = await fs.promises.lstat(p);
  502. expect(stats.isFile()).to.be.false();
  503. expect(stats.isDirectory()).to.be.true();
  504. expect(stats.isSymbolicLink()).to.be.false();
  505. expect(stats.size).to.equal(0);
  506. });
  507. itremote('returns information of root with stats as bigint', async function () {
  508. const p = path.join(asarDir, 'a.asar');
  509. const stats = await fs.promises.lstat(p, { bigint: false });
  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 a normal file', async function () {
  516. const p = path.join(asarDir, 'a.asar', 'link2', 'file1');
  517. const stats = await fs.promises.lstat(p);
  518. expect(stats.isFile()).to.be.true();
  519. expect(stats.isDirectory()).to.be.false();
  520. expect(stats.isSymbolicLink()).to.be.false();
  521. expect(stats.size).to.equal(6);
  522. });
  523. itremote('returns information of a normal directory', async function () {
  524. const p = path.join(asarDir, 'a.asar', 'dir1');
  525. const stats = await fs.promises.lstat(p);
  526. expect(stats.isFile()).to.be.false();
  527. expect(stats.isDirectory()).to.be.true();
  528. expect(stats.isSymbolicLink()).to.be.false();
  529. expect(stats.size).to.equal(0);
  530. });
  531. itremote('returns information of a linked file', async function () {
  532. const p = path.join(asarDir, 'a.asar', 'link2', 'link1');
  533. const stats = await fs.promises.lstat(p);
  534. expect(stats.isFile()).to.be.false();
  535. expect(stats.isDirectory()).to.be.false();
  536. expect(stats.isSymbolicLink()).to.be.true();
  537. expect(stats.size).to.equal(0);
  538. });
  539. itremote('returns information of a linked directory', async function () {
  540. const p = path.join(asarDir, 'a.asar', 'link2', 'link2');
  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('throws ENOENT error when can not find file', async function () {
  548. const p = path.join(asarDir, 'a.asar', 'file4');
  549. await expectToThrowErrorWithCode(() => fs.promises.lstat(p), 'ENOENT');
  550. });
  551. });
  552. describe('fs.realpathSync', () => {
  553. itremote('returns real path root', () => {
  554. const parent = fs.realpathSync(asarDir);
  555. const p = 'a.asar';
  556. const r = fs.realpathSync(path.join(parent, p));
  557. expect(r).to.equal(path.join(parent, p));
  558. });
  559. itremote('returns real path of a normal file', () => {
  560. const parent = fs.realpathSync(asarDir);
  561. const p = path.join('a.asar', 'file1');
  562. const r = fs.realpathSync(path.join(parent, p));
  563. expect(r).to.equal(path.join(parent, p));
  564. });
  565. itremote('returns real path of a normal directory', () => {
  566. const parent = fs.realpathSync(asarDir);
  567. const p = path.join('a.asar', 'dir1');
  568. const r = fs.realpathSync(path.join(parent, p));
  569. expect(r).to.equal(path.join(parent, p));
  570. });
  571. itremote('returns real path of a linked file', () => {
  572. const parent = fs.realpathSync(asarDir);
  573. const p = path.join('a.asar', 'link2', 'link1');
  574. const r = fs.realpathSync(path.join(parent, p));
  575. expect(r).to.equal(path.join(parent, 'a.asar', 'file1'));
  576. });
  577. itremote('returns real path of a linked directory', () => {
  578. const parent = fs.realpathSync(asarDir);
  579. const p = path.join('a.asar', 'link2', 'link2');
  580. const r = fs.realpathSync(path.join(parent, p));
  581. expect(r).to.equal(path.join(parent, 'a.asar', 'dir1'));
  582. });
  583. itremote('returns real path of an unpacked file', () => {
  584. const parent = fs.realpathSync(asarDir);
  585. const p = path.join('unpack.asar', 'a.txt');
  586. const r = fs.realpathSync(path.join(parent, p));
  587. expect(r).to.equal(path.join(parent, p));
  588. });
  589. itremote('throws ENOENT error when can not find file', () => {
  590. const parent = fs.realpathSync(asarDir);
  591. const p = path.join('a.asar', 'not-exist');
  592. expect(() => {
  593. fs.realpathSync(path.join(parent, p));
  594. }).to.throw(/ENOENT/);
  595. });
  596. });
  597. describe('fs.realpathSync.native', () => {
  598. itremote('returns real path root', () => {
  599. const parent = fs.realpathSync.native(asarDir);
  600. const p = 'a.asar';
  601. const r = fs.realpathSync.native(path.join(parent, p));
  602. expect(r).to.equal(path.join(parent, p));
  603. });
  604. itremote('returns real path of a normal file', () => {
  605. const parent = fs.realpathSync.native(asarDir);
  606. const p = path.join('a.asar', 'file1');
  607. const r = fs.realpathSync.native(path.join(parent, p));
  608. expect(r).to.equal(path.join(parent, p));
  609. });
  610. itremote('returns real path of a normal directory', () => {
  611. const parent = fs.realpathSync.native(asarDir);
  612. const p = path.join('a.asar', 'dir1');
  613. const r = fs.realpathSync.native(path.join(parent, p));
  614. expect(r).to.equal(path.join(parent, p));
  615. });
  616. itremote('returns real path of a linked file', () => {
  617. const parent = fs.realpathSync.native(asarDir);
  618. const p = path.join('a.asar', 'link2', 'link1');
  619. const r = fs.realpathSync.native(path.join(parent, p));
  620. expect(r).to.equal(path.join(parent, 'a.asar', 'file1'));
  621. });
  622. itremote('returns real path of a linked directory', () => {
  623. const parent = fs.realpathSync.native(asarDir);
  624. const p = path.join('a.asar', 'link2', 'link2');
  625. const r = fs.realpathSync.native(path.join(parent, p));
  626. expect(r).to.equal(path.join(parent, 'a.asar', 'dir1'));
  627. });
  628. itremote('returns real path of an unpacked file', () => {
  629. const parent = fs.realpathSync.native(asarDir);
  630. const p = path.join('unpack.asar', 'a.txt');
  631. const r = fs.realpathSync.native(path.join(parent, p));
  632. expect(r).to.equal(path.join(parent, p));
  633. });
  634. itremote('throws ENOENT error when can not find file', () => {
  635. const parent = fs.realpathSync.native(asarDir);
  636. const p = path.join('a.asar', 'not-exist');
  637. expect(() => {
  638. fs.realpathSync.native(path.join(parent, p));
  639. }).to.throw(/ENOENT/);
  640. });
  641. });
  642. describe('fs.realpath', () => {
  643. itremote('returns real path root', async () => {
  644. const parent = fs.realpathSync(asarDir);
  645. const p = 'a.asar';
  646. const r = await promisify(fs.realpath)(path.join(parent, p));
  647. expect(r).to.equal(path.join(parent, p));
  648. });
  649. itremote('returns real path of a normal file', async () => {
  650. const parent = fs.realpathSync(asarDir);
  651. const p = path.join('a.asar', 'file1');
  652. const r = await promisify(fs.realpath)(path.join(parent, p));
  653. expect(r).to.equal(path.join(parent, p));
  654. });
  655. itremote('returns real path of a normal directory', async () => {
  656. const parent = fs.realpathSync(asarDir);
  657. const p = path.join('a.asar', 'dir1');
  658. const r = await promisify(fs.realpath)(path.join(parent, p));
  659. expect(r).to.equal(path.join(parent, p));
  660. });
  661. itremote('returns real path of a linked file', async () => {
  662. const parent = fs.realpathSync(asarDir);
  663. const p = path.join('a.asar', 'link2', 'link1');
  664. const r = await promisify(fs.realpath)(path.join(parent, p));
  665. expect(r).to.equal(path.join(parent, 'a.asar', 'file1'));
  666. });
  667. itremote('returns real path of a linked directory', async () => {
  668. const parent = fs.realpathSync(asarDir);
  669. const p = path.join('a.asar', 'link2', 'link2');
  670. const r = await promisify(fs.realpath)(path.join(parent, p));
  671. expect(r).to.equal(path.join(parent, 'a.asar', 'dir1'));
  672. });
  673. itremote('returns real path of an unpacked file', async () => {
  674. const parent = fs.realpathSync(asarDir);
  675. const p = path.join('unpack.asar', 'a.txt');
  676. const r = await promisify(fs.realpath)(path.join(parent, p));
  677. expect(r).to.equal(path.join(parent, p));
  678. });
  679. itremote('throws ENOENT error when can not find file', async () => {
  680. const parent = fs.realpathSync(asarDir);
  681. const p = path.join('a.asar', 'not-exist');
  682. const err = await new Promise<any>(resolve => fs.realpath(path.join(parent, p), resolve));
  683. expect(err.code).to.equal('ENOENT');
  684. });
  685. });
  686. describe('fs.promises.realpath', () => {
  687. itremote('returns real path root', async () => {
  688. const parent = fs.realpathSync(asarDir);
  689. const p = 'a.asar';
  690. const r = await fs.promises.realpath(path.join(parent, p));
  691. expect(r).to.equal(path.join(parent, p));
  692. });
  693. itremote('returns real path of a normal file', async () => {
  694. const parent = fs.realpathSync(asarDir);
  695. const p = path.join('a.asar', 'file1');
  696. const r = await fs.promises.realpath(path.join(parent, p));
  697. expect(r).to.equal(path.join(parent, p));
  698. });
  699. itremote('returns real path of a normal directory', async () => {
  700. const parent = fs.realpathSync(asarDir);
  701. const p = path.join('a.asar', 'dir1');
  702. const r = await fs.promises.realpath(path.join(parent, p));
  703. expect(r).to.equal(path.join(parent, p));
  704. });
  705. itremote('returns real path of a linked file', async () => {
  706. const parent = fs.realpathSync(asarDir);
  707. const p = path.join('a.asar', 'link2', 'link1');
  708. const r = await fs.promises.realpath(path.join(parent, p));
  709. expect(r).to.equal(path.join(parent, 'a.asar', 'file1'));
  710. });
  711. itremote('returns real path of a linked directory', async () => {
  712. const parent = fs.realpathSync(asarDir);
  713. const p = path.join('a.asar', 'link2', 'link2');
  714. const r = await fs.promises.realpath(path.join(parent, p));
  715. expect(r).to.equal(path.join(parent, 'a.asar', 'dir1'));
  716. });
  717. itremote('returns real path of an unpacked file', async () => {
  718. const parent = fs.realpathSync(asarDir);
  719. const p = path.join('unpack.asar', 'a.txt');
  720. const r = await fs.promises.realpath(path.join(parent, p));
  721. expect(r).to.equal(path.join(parent, p));
  722. });
  723. itremote('throws ENOENT error when can not find file', async () => {
  724. const parent = fs.realpathSync(asarDir);
  725. const p = path.join('a.asar', 'not-exist');
  726. await expectToThrowErrorWithCode(() => fs.promises.realpath(path.join(parent, p)), 'ENOENT');
  727. });
  728. });
  729. describe('fs.realpath.native', () => {
  730. itremote('returns real path root', async () => {
  731. const parent = fs.realpathSync.native(asarDir);
  732. const p = 'a.asar';
  733. const r = await promisify(fs.realpath.native)(path.join(parent, p));
  734. expect(r).to.equal(path.join(parent, p));
  735. });
  736. itremote('returns real path of a normal file', async () => {
  737. const parent = fs.realpathSync.native(asarDir);
  738. const p = path.join('a.asar', 'file1');
  739. const r = await promisify(fs.realpath.native)(path.join(parent, p));
  740. expect(r).to.equal(path.join(parent, p));
  741. });
  742. itremote('returns real path of a normal directory', async () => {
  743. const parent = fs.realpathSync.native(asarDir);
  744. const p = path.join('a.asar', 'dir1');
  745. const r = await promisify(fs.realpath.native)(path.join(parent, p));
  746. expect(r).to.equal(path.join(parent, p));
  747. });
  748. itremote('returns real path of a linked file', async () => {
  749. const parent = fs.realpathSync.native(asarDir);
  750. const p = path.join('a.asar', 'link2', 'link1');
  751. const r = await promisify(fs.realpath.native)(path.join(parent, p));
  752. expect(r).to.equal(path.join(parent, 'a.asar', 'file1'));
  753. });
  754. itremote('returns real path of a linked directory', async () => {
  755. const parent = fs.realpathSync.native(asarDir);
  756. const p = path.join('a.asar', 'link2', 'link2');
  757. const r = await promisify(fs.realpath.native)(path.join(parent, p));
  758. expect(r).to.equal(path.join(parent, 'a.asar', 'dir1'));
  759. });
  760. itremote('returns real path of an unpacked file', async () => {
  761. const parent = fs.realpathSync.native(asarDir);
  762. const p = path.join('unpack.asar', 'a.txt');
  763. const r = await promisify(fs.realpath.native)(path.join(parent, p));
  764. expect(r).to.equal(path.join(parent, p));
  765. });
  766. itremote('throws ENOENT error when can not find file', async () => {
  767. const parent = fs.realpathSync.native(asarDir);
  768. const p = path.join('a.asar', 'not-exist');
  769. const err = await new Promise<any>(resolve => fs.realpath.native(path.join(parent, p), resolve));
  770. expect(err.code).to.equal('ENOENT');
  771. });
  772. });
  773. describe('fs.readdirSync', function () {
  774. itremote('reads dirs from root', function () {
  775. const p = path.join(asarDir, 'a.asar');
  776. const dirs = fs.readdirSync(p);
  777. expect(dirs).to.deep.equal(['dir1', 'dir2', 'dir3', 'file1', 'file2', 'file3', 'link1', 'link2', 'ping.js']);
  778. });
  779. itremote('reads dirs from a normal dir', function () {
  780. const p = path.join(asarDir, 'a.asar', 'dir1');
  781. const dirs = fs.readdirSync(p);
  782. expect(dirs).to.deep.equal(['file1', 'file2', 'file3', 'link1', 'link2']);
  783. });
  784. itremote('supports withFileTypes', function () {
  785. const p = path.join(asarDir, 'a.asar');
  786. const dirs = fs.readdirSync(p, { withFileTypes: true });
  787. for (const dir of dirs) {
  788. expect(dir).to.be.an.instanceof(fs.Dirent);
  789. }
  790. const names = dirs.map(a => a.name);
  791. expect(names).to.deep.equal(['dir1', 'dir2', 'dir3', 'file1', 'file2', 'file3', 'link1', 'link2', 'ping.js']);
  792. });
  793. itremote('supports withFileTypes for a deep directory', function () {
  794. const p = path.join(asarDir, 'a.asar', 'dir3');
  795. const dirs = fs.readdirSync(p, { withFileTypes: true });
  796. for (const dir of dirs) {
  797. expect(dir).to.be.an.instanceof(fs.Dirent);
  798. }
  799. const names = dirs.map(a => a.name);
  800. expect(names).to.deep.equal(['file1', 'file2', 'file3']);
  801. });
  802. itremote('reads dirs from a linked dir', function () {
  803. const p = path.join(asarDir, 'a.asar', 'link2', 'link2');
  804. const dirs = fs.readdirSync(p);
  805. expect(dirs).to.deep.equal(['file1', 'file2', 'file3', 'link1', 'link2']);
  806. });
  807. itremote('throws ENOENT error when can not find file', function () {
  808. const p = path.join(asarDir, 'a.asar', 'not-exist');
  809. expect(() => {
  810. fs.readdirSync(p);
  811. }).to.throw(/ENOENT/);
  812. });
  813. });
  814. describe('fs.readdir', function () {
  815. itremote('reads dirs from root', async () => {
  816. const p = path.join(asarDir, 'a.asar');
  817. const dirs = await promisify(fs.readdir)(p);
  818. expect(dirs).to.deep.equal(['dir1', 'dir2', 'dir3', 'file1', 'file2', 'file3', 'link1', 'link2', 'ping.js']);
  819. });
  820. itremote('supports withFileTypes', async () => {
  821. const p = path.join(asarDir, 'a.asar');
  822. const dirs = await promisify(fs.readdir)(p, { withFileTypes: true });
  823. for (const dir of dirs) {
  824. expect(dir).to.be.an.instanceof(fs.Dirent);
  825. }
  826. const names = dirs.map((a: any) => a.name);
  827. expect(names).to.deep.equal(['dir1', 'dir2', 'dir3', 'file1', 'file2', 'file3', 'link1', 'link2', 'ping.js']);
  828. });
  829. itremote('reads dirs from a normal dir', async () => {
  830. const p = path.join(asarDir, 'a.asar', 'dir1');
  831. const dirs = await promisify(fs.readdir)(p);
  832. expect(dirs).to.deep.equal(['file1', 'file2', 'file3', 'link1', 'link2']);
  833. });
  834. itremote('reads dirs from a linked dir', async () => {
  835. const p = path.join(asarDir, 'a.asar', 'link2', 'link2');
  836. const dirs = await promisify(fs.readdir)(p);
  837. expect(dirs).to.deep.equal(['file1', 'file2', 'file3', 'link1', 'link2']);
  838. });
  839. itremote('throws ENOENT error when can not find file', async () => {
  840. const p = path.join(asarDir, 'a.asar', 'not-exist');
  841. const err = await new Promise<any>(resolve => fs.readdir(p, resolve));
  842. expect(err.code).to.equal('ENOENT');
  843. });
  844. it('handles null for options', function (done) {
  845. const p = path.join(asarDir, 'a.asar', 'dir1');
  846. fs.readdir(p, null, function (err, dirs) {
  847. try {
  848. expect(err).to.be.null();
  849. expect(dirs).to.deep.equal(['file1', 'file2', 'file3', 'link1', 'link2']);
  850. done();
  851. } catch (e) {
  852. done(e);
  853. }
  854. });
  855. });
  856. it('handles undefined for options', function (done) {
  857. const p = path.join(asarDir, 'a.asar', 'dir1');
  858. fs.readdir(p, undefined, function (err, dirs) {
  859. try {
  860. expect(err).to.be.null();
  861. expect(dirs).to.deep.equal(['file1', 'file2', 'file3', 'link1', 'link2']);
  862. done();
  863. } catch (e) {
  864. done(e);
  865. }
  866. });
  867. });
  868. });
  869. describe('fs.promises.readdir', function () {
  870. itremote('reads dirs from root', async function () {
  871. const p = path.join(asarDir, 'a.asar');
  872. const dirs = await fs.promises.readdir(p);
  873. expect(dirs).to.deep.equal(['dir1', 'dir2', 'dir3', 'file1', 'file2', 'file3', 'link1', 'link2', 'ping.js']);
  874. });
  875. itremote('supports withFileTypes', async function () {
  876. const p = path.join(asarDir, 'a.asar');
  877. const dirs = await fs.promises.readdir(p, { withFileTypes: true });
  878. for (const dir of dirs) {
  879. expect(dir).to.be.an.instanceof(fs.Dirent);
  880. }
  881. const names = dirs.map(a => a.name);
  882. expect(names).to.deep.equal(['dir1', 'dir2', 'dir3', 'file1', 'file2', 'file3', 'link1', 'link2', 'ping.js']);
  883. });
  884. itremote('reads dirs from a normal dir', async function () {
  885. const p = path.join(asarDir, 'a.asar', 'dir1');
  886. const dirs = await fs.promises.readdir(p);
  887. expect(dirs).to.deep.equal(['file1', 'file2', 'file3', 'link1', 'link2']);
  888. });
  889. itremote('reads dirs from a linked dir', async function () {
  890. const p = path.join(asarDir, 'a.asar', 'link2', 'link2');
  891. const dirs = await fs.promises.readdir(p);
  892. expect(dirs).to.deep.equal(['file1', 'file2', 'file3', 'link1', 'link2']);
  893. });
  894. itremote('throws ENOENT error when can not find file', async function () {
  895. const p = path.join(asarDir, 'a.asar', 'not-exist');
  896. await expectToThrowErrorWithCode(() => fs.promises.readdir(p), 'ENOENT');
  897. });
  898. });
  899. describe('fs.openSync', function () {
  900. itremote('opens a normal/linked/under-linked-directory file', function () {
  901. const ref2 = ['file1', 'link1', path.join('link2', 'file1')];
  902. for (let j = 0, len = ref2.length; j < len; j++) {
  903. const file = ref2[j];
  904. const p = path.join(asarDir, 'a.asar', file);
  905. const fd = fs.openSync(p, 'r');
  906. const buffer = Buffer.alloc(6);
  907. fs.readSync(fd, buffer, 0, 6, 0);
  908. expect(String(buffer).trim()).to.equal('file1');
  909. fs.closeSync(fd);
  910. }
  911. });
  912. itremote('throws ENOENT error when can not find file', function () {
  913. const p = path.join(asarDir, 'a.asar', 'not-exist');
  914. expect(() => {
  915. (fs.openSync as any)(p);
  916. }).to.throw(/ENOENT/);
  917. });
  918. });
  919. describe('fs.open', function () {
  920. itremote('opens a normal file', async function () {
  921. const p = path.join(asarDir, 'a.asar', 'file1');
  922. const fd = await promisify(fs.open)(p, 'r');
  923. const buffer = Buffer.alloc(6);
  924. await promisify(fs.read)(fd, buffer, 0, 6, 0);
  925. expect(String(buffer).trim()).to.equal('file1');
  926. await promisify(fs.close)(fd);
  927. });
  928. itremote('throws ENOENT error when can not find file', async function () {
  929. const p = path.join(asarDir, 'a.asar', 'not-exist');
  930. const err = await new Promise<any>(resolve => fs.open(p, 'r', resolve));
  931. expect(err.code).to.equal('ENOENT');
  932. });
  933. });
  934. describe('fs.promises.open', function () {
  935. itremote('opens a normal file', async function () {
  936. const p = path.join(asarDir, 'a.asar', 'file1');
  937. const fh = await fs.promises.open(p, 'r');
  938. const buffer = Buffer.alloc(6);
  939. await fh.read(buffer, 0, 6, 0);
  940. expect(String(buffer).trim()).to.equal('file1');
  941. await fh.close();
  942. });
  943. itremote('throws ENOENT error when can not find file', async function () {
  944. const p = path.join(asarDir, 'a.asar', 'not-exist');
  945. await expectToThrowErrorWithCode(() => fs.promises.open(p, 'r'), 'ENOENT');
  946. });
  947. });
  948. describe('fs.mkdir', function () {
  949. itremote('throws error when calling inside asar archive', async function () {
  950. const p = path.join(asarDir, 'a.asar', 'not-exist');
  951. const err = await new Promise<any>(resolve => fs.mkdir(p, resolve));
  952. expect(err.code).to.equal('ENOTDIR');
  953. });
  954. });
  955. describe('fs.promises.mkdir', function () {
  956. itremote('throws error when calling inside asar archive', async function () {
  957. const p = path.join(asarDir, 'a.asar', 'not-exist');
  958. await expectToThrowErrorWithCode(() => fs.promises.mkdir(p), 'ENOTDIR');
  959. });
  960. });
  961. describe('fs.mkdirSync', function () {
  962. itremote('throws error when calling inside asar archive', function () {
  963. const p = path.join(asarDir, 'a.asar', 'not-exist');
  964. expect(() => {
  965. fs.mkdirSync(p);
  966. }).to.throw(/ENOTDIR/);
  967. });
  968. });
  969. describe('fs.exists', function () {
  970. itremote('handles an existing file', async function () {
  971. const p = path.join(asarDir, 'a.asar', 'file1');
  972. // eslint-disable-next-line
  973. const exists = await new Promise(resolve => fs.exists(p, resolve))
  974. expect(exists).to.be.true();
  975. });
  976. itremote('handles a non-existent file', async function () {
  977. const p = path.join(asarDir, 'a.asar', 'not-exist');
  978. // eslint-disable-next-line
  979. const exists = await new Promise(resolve => fs.exists(p, resolve))
  980. expect(exists).to.be.false();
  981. });
  982. itremote('promisified version handles an existing file', async () => {
  983. const p = path.join(asarDir, 'a.asar', 'file1');
  984. // eslint-disable-next-line
  985. const exists = await require('util').promisify(fs.exists)(p)
  986. expect(exists).to.be.true();
  987. });
  988. itremote('promisified version handles a non-existent file', async function () {
  989. const p = path.join(asarDir, 'a.asar', 'not-exist');
  990. // eslint-disable-next-line
  991. const exists = await require('util').promisify(fs.exists)(p)
  992. expect(exists).to.be.false();
  993. });
  994. });
  995. describe('fs.existsSync', function () {
  996. itremote('handles an existing file', function () {
  997. const p = path.join(asarDir, 'a.asar', 'file1');
  998. expect(fs.existsSync(p)).to.be.true();
  999. });
  1000. itremote('handles a non-existent file', function () {
  1001. const p = path.join(asarDir, 'a.asar', 'not-exist');
  1002. expect(fs.existsSync(p)).to.be.false();
  1003. });
  1004. });
  1005. describe('fs.access', function () {
  1006. itremote('accesses a normal file', async function () {
  1007. const p = path.join(asarDir, 'a.asar', 'file1');
  1008. await promisify(fs.access)(p);
  1009. });
  1010. itremote('throws an error when called with write mode', async function () {
  1011. const p = path.join(asarDir, 'a.asar', 'file1');
  1012. const err = await new Promise<any>(resolve => fs.access(p, fs.constants.R_OK | fs.constants.W_OK, resolve));
  1013. expect(err.code).to.equal('EACCES');
  1014. });
  1015. itremote('throws an error when called on non-existent file', async function () {
  1016. const p = path.join(asarDir, 'a.asar', 'not-exist');
  1017. const err = await new Promise<any>(resolve => fs.access(p, fs.constants.R_OK | fs.constants.W_OK, resolve));
  1018. expect(err.code).to.equal('ENOENT');
  1019. });
  1020. itremote('allows write mode for unpacked files', async function () {
  1021. const p = path.join(asarDir, 'unpack.asar', 'a.txt');
  1022. await promisify(fs.access)(p, fs.constants.R_OK | fs.constants.W_OK);
  1023. });
  1024. });
  1025. describe('fs.promises.access', function () {
  1026. itremote('accesses a normal file', async function () {
  1027. const p = path.join(asarDir, 'a.asar', 'file1');
  1028. await fs.promises.access(p);
  1029. });
  1030. itremote('throws an error when called with write mode', async function () {
  1031. const p = path.join(asarDir, 'a.asar', 'file1');
  1032. await expectToThrowErrorWithCode(() => fs.promises.access(p, fs.constants.R_OK | fs.constants.W_OK), 'EACCES');
  1033. });
  1034. itremote('throws an error when called on non-existent file', async function () {
  1035. const p = path.join(asarDir, 'a.asar', 'not-exist');
  1036. await expectToThrowErrorWithCode(() => fs.promises.access(p), 'ENOENT');
  1037. });
  1038. itremote('allows write mode for unpacked files', async function () {
  1039. const p = path.join(asarDir, 'unpack.asar', 'a.txt');
  1040. await fs.promises.access(p, fs.constants.R_OK | fs.constants.W_OK);
  1041. });
  1042. });
  1043. describe('fs.accessSync', function () {
  1044. itremote('accesses a normal file', function () {
  1045. const p = path.join(asarDir, 'a.asar', 'file1');
  1046. expect(() => {
  1047. fs.accessSync(p);
  1048. }).to.not.throw();
  1049. });
  1050. itremote('throws an error when called with write mode', function () {
  1051. const p = path.join(asarDir, 'a.asar', 'file1');
  1052. expect(() => {
  1053. fs.accessSync(p, fs.constants.R_OK | fs.constants.W_OK);
  1054. }).to.throw(/EACCES/);
  1055. });
  1056. itremote('throws an error when called on non-existent file', function () {
  1057. const p = path.join(asarDir, 'a.asar', 'not-exist');
  1058. expect(() => {
  1059. fs.accessSync(p);
  1060. }).to.throw(/ENOENT/);
  1061. });
  1062. itremote('allows write mode for unpacked files', function () {
  1063. const p = path.join(asarDir, 'unpack.asar', 'a.txt');
  1064. expect(() => {
  1065. fs.accessSync(p, fs.constants.R_OK | fs.constants.W_OK);
  1066. }).to.not.throw();
  1067. });
  1068. });
  1069. function generateSpecs (childProcess: string) {
  1070. ifdescribe(features.isRunAsNodeEnabled())(`${childProcess}.fork`, function () {
  1071. itremote('opens a normal js file', async function (childProcess: string) {
  1072. const child = require(childProcess).fork(path.join(asarDir, 'a.asar', 'ping.js'));
  1073. child.send('message');
  1074. const msg = await new Promise(resolve => child.once('message', resolve));
  1075. expect(msg).to.equal('message');
  1076. }, [childProcess]);
  1077. itremote('supports asar in the forked js', async function (childProcess: string, fixtures: string) {
  1078. const file = path.join(asarDir, 'a.asar', 'file1');
  1079. const child = require(childProcess).fork(path.join(fixtures, 'module', 'asar.js'));
  1080. child.send(file);
  1081. const content = await new Promise(resolve => child.once('message', resolve));
  1082. expect(content).to.equal(fs.readFileSync(file).toString());
  1083. }, [childProcess, fixtures]);
  1084. });
  1085. describe(`${childProcess}.exec`, function () {
  1086. itremote('should not try to extract the command if there is a reference to a file inside an .asar', async function (childProcess: string) {
  1087. const echo = path.join(asarDir, 'echo.asar', 'echo');
  1088. const stdout = await promisify(require(childProcess).exec)('echo ' + echo + ' foo bar');
  1089. expect(stdout.toString().replace(/\r/g, '')).to.equal(echo + ' foo bar\n');
  1090. }, [childProcess]);
  1091. });
  1092. describe(`${childProcess}.execSync`, function () {
  1093. itremote('should not try to extract the command if there is a reference to a file inside an .asar', async function (childProcess: string) {
  1094. const echo = path.join(asarDir, 'echo.asar', 'echo');
  1095. const stdout = require(childProcess).execSync('echo ' + echo + ' foo bar');
  1096. expect(stdout.toString().replace(/\r/g, '')).to.equal(echo + ' foo bar\n');
  1097. }, [childProcess]);
  1098. });
  1099. ifdescribe(process.platform === 'darwin' && process.arch !== 'arm64')(`${childProcess}.execFile`, function () {
  1100. itremote('executes binaries', async function (childProcess: string) {
  1101. const echo = path.join(asarDir, 'echo.asar', 'echo');
  1102. const stdout = await promisify(require(childProcess).execFile)(echo, ['test']);
  1103. expect(stdout).to.equal('test\n');
  1104. }, [childProcess]);
  1105. itremote('executes binaries without callback', async function (childProcess: string) {
  1106. const echo = path.join(asarDir, 'echo.asar', 'echo');
  1107. const process = require(childProcess).execFile(echo, ['test']);
  1108. const code = await new Promise(resolve => process.once('close', resolve));
  1109. expect(code).to.equal(0);
  1110. process.on('error', function () {
  1111. throw new Error('error');
  1112. });
  1113. }, [childProcess]);
  1114. itremote('execFileSync executes binaries', function (childProcess: string) {
  1115. const echo = path.join(asarDir, 'echo.asar', 'echo');
  1116. const output = require(childProcess).execFileSync(echo, ['test']);
  1117. expect(String(output)).to.equal('test\n');
  1118. }, [childProcess]);
  1119. });
  1120. }
  1121. generateSpecs('child_process');
  1122. generateSpecs('node:child_process');
  1123. describe('internalModuleReadJSON', function () {
  1124. itremote('reads a normal file', function () {
  1125. const { internalModuleReadJSON } = (process as any).binding('fs');
  1126. const file1 = path.join(asarDir, 'a.asar', 'file1');
  1127. const [s1, c1] = internalModuleReadJSON(file1);
  1128. expect([s1.toString().trim(), c1]).to.eql(['file1', true]);
  1129. const file2 = path.join(asarDir, 'a.asar', 'file2');
  1130. const [s2, c2] = internalModuleReadJSON(file2);
  1131. expect([s2.toString().trim(), c2]).to.eql(['file2', true]);
  1132. const file3 = path.join(asarDir, 'a.asar', 'file3');
  1133. const [s3, c3] = internalModuleReadJSON(file3);
  1134. expect([s3.toString().trim(), c3]).to.eql(['file3', true]);
  1135. });
  1136. itremote('reads a normal file with unpacked files', function () {
  1137. const { internalModuleReadJSON } = (process as any).binding('fs');
  1138. const p = path.join(asarDir, 'unpack.asar', 'a.txt');
  1139. const [s, c] = internalModuleReadJSON(p);
  1140. expect([s.toString().trim(), c]).to.eql(['a', true]);
  1141. });
  1142. });
  1143. describe('util.promisify', function () {
  1144. itremote('can promisify all fs functions', function () {
  1145. const originalFs = require('original-fs');
  1146. const util = require('util');
  1147. const { hasOwnProperty } = Object.prototype;
  1148. for (const [propertyName, originalValue] of Object.entries(originalFs)) {
  1149. // Some properties exist but have a value of `undefined` on some platforms.
  1150. // E.g. `fs.lchmod`, which in only available on MacOS, see
  1151. // https://nodejs.org/docs/latest-v10.x/api/fs.html#fs_fs_lchmod_path_mode_callback
  1152. // Also check for `null`s, `hasOwnProperty()` can't handle them.
  1153. if (typeof originalValue === 'undefined' || originalValue === null) continue;
  1154. if (hasOwnProperty.call(originalValue, util.promisify.custom)) {
  1155. expect(fs).to.have.own.property(propertyName)
  1156. .that.has.own.property(util.promisify.custom);
  1157. }
  1158. }
  1159. });
  1160. });
  1161. describe('process.noAsar', function () {
  1162. const errorName = process.platform === 'win32' ? 'ENOENT' : 'ENOTDIR';
  1163. beforeEach(async function () {
  1164. return (await getRemoteContext()).webContents.executeJavaScript(`
  1165. process.noAsar = true;
  1166. `);
  1167. });
  1168. afterEach(async function () {
  1169. return (await getRemoteContext()).webContents.executeJavaScript(`
  1170. process.noAsar = false;
  1171. `);
  1172. });
  1173. itremote('disables asar support in sync API', function (errorName: string) {
  1174. const file = path.join(asarDir, 'a.asar', 'file1');
  1175. const dir = path.join(asarDir, 'a.asar', 'dir1');
  1176. console.log(1);
  1177. expect(() => {
  1178. fs.readFileSync(file);
  1179. }).to.throw(new RegExp(errorName));
  1180. expect(() => {
  1181. fs.lstatSync(file);
  1182. }).to.throw(new RegExp(errorName));
  1183. expect(() => {
  1184. fs.realpathSync(file);
  1185. }).to.throw(new RegExp(errorName));
  1186. expect(() => {
  1187. fs.readdirSync(dir);
  1188. }).to.throw(new RegExp(errorName));
  1189. }, [errorName]);
  1190. itremote('disables asar support in async API', async function (errorName: string) {
  1191. const file = path.join(asarDir, 'a.asar', 'file1');
  1192. const dir = path.join(asarDir, 'a.asar', 'dir1');
  1193. await new Promise<void>(resolve => {
  1194. fs.readFile(file, function (error) {
  1195. expect(error?.code).to.equal(errorName);
  1196. fs.lstat(file, function (error) {
  1197. expect(error?.code).to.equal(errorName);
  1198. fs.realpath(file, function (error) {
  1199. expect(error?.code).to.equal(errorName);
  1200. fs.readdir(dir, function (error) {
  1201. expect(error?.code).to.equal(errorName);
  1202. resolve();
  1203. });
  1204. });
  1205. });
  1206. });
  1207. });
  1208. }, [errorName]);
  1209. itremote('disables asar support in promises API', async function (errorName: string) {
  1210. const file = path.join(asarDir, 'a.asar', 'file1');
  1211. const dir = path.join(asarDir, 'a.asar', 'dir1');
  1212. await expect(fs.promises.readFile(file)).to.be.eventually.rejectedWith(Error, new RegExp(errorName));
  1213. await expect(fs.promises.lstat(file)).to.be.eventually.rejectedWith(Error, new RegExp(errorName));
  1214. await expect(fs.promises.realpath(file)).to.be.eventually.rejectedWith(Error, new RegExp(errorName));
  1215. await expect(fs.promises.readdir(dir)).to.be.eventually.rejectedWith(Error, new RegExp(errorName));
  1216. }, [errorName]);
  1217. itremote('treats *.asar as normal file', function () {
  1218. const originalFs = require('original-fs');
  1219. const asar = path.join(asarDir, 'a.asar');
  1220. const content1 = fs.readFileSync(asar);
  1221. const content2 = originalFs.readFileSync(asar);
  1222. expect(content1.compare(content2)).to.equal(0);
  1223. expect(() => {
  1224. fs.readdirSync(asar);
  1225. }).to.throw(/ENOTDIR/);
  1226. });
  1227. itremote('is reset to its original value when execSync throws an error', function () {
  1228. process.noAsar = false;
  1229. expect(() => {
  1230. require('child_process').execSync(path.join(__dirname, 'does-not-exist.txt'));
  1231. }).to.throw();
  1232. expect(process.noAsar).to.be.false();
  1233. });
  1234. });
  1235. /*
  1236. describe('process.env.ELECTRON_NO_ASAR', function () {
  1237. before(function () {
  1238. if (!features.isRunAsNodeEnabled()) {
  1239. this.skip();
  1240. }
  1241. });
  1242. itremote('disables asar support in forked processes', function (done) {
  1243. const forked = ChildProcess.fork(path.join(__dirname, 'fixtures', 'module', 'no-asar.js'), [], {
  1244. env: {
  1245. ELECTRON_NO_ASAR: true
  1246. }
  1247. });
  1248. forked.on('message', function (stats) {
  1249. try {
  1250. expect(stats.isFile).to.be.true();
  1251. expect(stats.size).to.equal(3458);
  1252. done();
  1253. } catch (e) {
  1254. done(e);
  1255. }
  1256. });
  1257. });
  1258. itremote('disables asar support in spawned processes', function (done) {
  1259. const spawned = ChildProcess.spawn(process.execPath, [path.join(__dirname, 'fixtures', 'module', 'no-asar.js')], {
  1260. env: {
  1261. ELECTRON_NO_ASAR: true,
  1262. ELECTRON_RUN_AS_NODE: true
  1263. }
  1264. });
  1265. let output = '';
  1266. spawned.stdout.on('data', function (data) {
  1267. output += data;
  1268. });
  1269. spawned.stdout.on('close', function () {
  1270. try {
  1271. const stats = JSON.parse(output);
  1272. expect(stats.isFile).to.be.true();
  1273. expect(stats.size).to.equal(3458);
  1274. done();
  1275. } catch (e) {
  1276. done(e);
  1277. }
  1278. });
  1279. });
  1280. });
  1281. */
  1282. });
  1283. describe('asar protocol', function () {
  1284. itremote('can request a file in package', async function () {
  1285. const p = path.resolve(asarDir, 'a.asar', 'file1');
  1286. const response = await fetch('file://' + p);
  1287. const data = await response.text();
  1288. expect(data.trim()).to.equal('file1');
  1289. });
  1290. itremote('can request a file in package with unpacked files', async function () {
  1291. const p = path.resolve(asarDir, 'unpack.asar', 'a.txt');
  1292. const response = await fetch('file://' + p);
  1293. const data = await response.text();
  1294. expect(data.trim()).to.equal('a');
  1295. });
  1296. itremote('can request a linked file in package', async function () {
  1297. const p = path.resolve(asarDir, 'a.asar', 'link2', 'link1');
  1298. const response = await fetch('file://' + p);
  1299. const data = await response.text();
  1300. expect(data.trim()).to.equal('file1');
  1301. });
  1302. itremote('can request a file in filesystem', async function () {
  1303. const p = path.resolve(asarDir, 'file');
  1304. const response = await fetch('file://' + p);
  1305. const data = await response.text();
  1306. expect(data.trim()).to.equal('file');
  1307. });
  1308. itremote('gets error when file is not found', async function () {
  1309. const p = path.resolve(asarDir, 'a.asar', 'no-exist');
  1310. try {
  1311. const response = await fetch('file://' + p);
  1312. expect(response.status).to.equal(404);
  1313. } catch (error: any) {
  1314. expect(error.message).to.equal('Failed to fetch');
  1315. }
  1316. });
  1317. });
  1318. describe('original-fs module', function () {
  1319. itremote('treats .asar as file', function () {
  1320. const file = path.join(asarDir, 'a.asar');
  1321. const originalFs = require('original-fs');
  1322. const stats = originalFs.statSync(file);
  1323. expect(stats.isFile()).to.be.true();
  1324. });
  1325. /*
  1326. ifit(features.isRunAsNodeEnabled())('is available in forked scripts', async function () {
  1327. const child = ChildProcess.fork(path.join(fixtures, 'module', 'original-fs.js'));
  1328. const message = once(child, 'message');
  1329. child.send('message');
  1330. const [msg] = await message;
  1331. expect(msg).to.equal('object');
  1332. });
  1333. */
  1334. itremote('can be used with streams', () => {
  1335. const originalFs = require('original-fs');
  1336. originalFs.createReadStream(path.join(asarDir, 'a.asar'));
  1337. });
  1338. itremote('can recursively delete a directory with an asar file in itremote', () => {
  1339. const deleteDir = path.join(asarDir, 'deleteme');
  1340. fs.mkdirSync(deleteDir);
  1341. const originalFs = require('original-fs');
  1342. originalFs.rmdirSync(deleteDir, { recursive: true });
  1343. expect(fs.existsSync(deleteDir)).to.be.false();
  1344. });
  1345. itremote('has the same APIs as fs', function () {
  1346. expect(Object.keys(require('fs'))).to.deep.equal(Object.keys(require('original-fs')));
  1347. expect(Object.keys(require('fs').promises)).to.deep.equal(Object.keys(require('original-fs').promises));
  1348. });
  1349. });
  1350. describe('graceful-fs module', function () {
  1351. itremote('recognize asar archives', function () {
  1352. const gfs = require('graceful-fs');
  1353. const p = path.join(asarDir, 'a.asar', 'link1');
  1354. expect(gfs.readFileSync(p).toString().trim()).to.equal('file1');
  1355. });
  1356. itremote('does not touch global fs object', function () {
  1357. const gfs = require('graceful-fs');
  1358. expect(fs.readdir).to.not.equal(gfs.readdir);
  1359. });
  1360. });
  1361. describe('mkdirp module', function () {
  1362. itremote('throws error when calling inside asar archive', function () {
  1363. const mkdirp = require('mkdirp');
  1364. const p = path.join(asarDir, 'a.asar', 'not-exist');
  1365. expect(() => {
  1366. mkdirp.sync(p);
  1367. }).to.throw(/ENOTDIR/);
  1368. });
  1369. });
  1370. describe('native-image', function () {
  1371. itremote('reads image from asar archive', function () {
  1372. const p = path.join(asarDir, 'logo.asar', 'logo.png');
  1373. const logo = require('electron').nativeImage.createFromPath(p);
  1374. expect(logo.getSize()).to.deep.equal({
  1375. width: 55,
  1376. height: 55
  1377. });
  1378. });
  1379. itremote('reads image from asar archive with unpacked files', function () {
  1380. const p = path.join(asarDir, 'unpack.asar', 'atom.png');
  1381. const logo = require('electron').nativeImage.createFromPath(p);
  1382. expect(logo.getSize()).to.deep.equal({
  1383. width: 1024,
  1384. height: 1024
  1385. });
  1386. });
  1387. });
  1388. });