asar-spec.ts 61 KB

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