asar-spec.ts 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590
  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', () => {
  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('has the same APIs as fs', function () {
  1343. expect(Object.keys(require('node:fs'))).to.deep.equal(Object.keys(require('original-fs')));
  1344. expect(Object.keys(require('node:fs').promises)).to.deep.equal(Object.keys(require('original-fs').promises));
  1345. });
  1346. });
  1347. describe('graceful-fs module', function () {
  1348. itremote('recognize asar archives', function () {
  1349. const gfs = require('graceful-fs');
  1350. const p = path.join(asarDir, 'a.asar', 'link1');
  1351. expect(gfs.readFileSync(p).toString().trim()).to.equal('file1');
  1352. });
  1353. itremote('does not touch global fs object', function () {
  1354. const gfs = require('graceful-fs');
  1355. expect(fs.readdir).to.not.equal(gfs.readdir);
  1356. });
  1357. });
  1358. describe('mkdirp module', function () {
  1359. itremote('throws error when calling inside asar archive', function () {
  1360. const mkdirp = require('mkdirp');
  1361. const p = path.join(asarDir, 'a.asar', 'not-exist');
  1362. expect(() => {
  1363. mkdirp.sync(p);
  1364. }).to.throw(/ENOTDIR/);
  1365. });
  1366. });
  1367. describe('native-image', function () {
  1368. itremote('reads image from asar archive', function () {
  1369. const p = path.join(asarDir, 'logo.asar', 'logo.png');
  1370. const logo = require('electron').nativeImage.createFromPath(p);
  1371. expect(logo.getSize()).to.deep.equal({
  1372. width: 55,
  1373. height: 55
  1374. });
  1375. });
  1376. itremote('reads image from asar archive with unpacked files', function () {
  1377. const p = path.join(asarDir, 'unpack.asar', 'atom.png');
  1378. const logo = require('electron').nativeImage.createFromPath(p);
  1379. expect(logo.getSize()).to.deep.equal({
  1380. width: 1024,
  1381. height: 1024
  1382. });
  1383. });
  1384. });
  1385. });