ZipOpenFS.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.ZipOpenFS = exports.getArchivePart = void 0;
  4. const tslib_1 = require("tslib");
  5. const fs_1 = require("fs");
  6. const FakeFS_1 = require("./FakeFS");
  7. const NodeFS_1 = require("./NodeFS");
  8. const ZipFS_1 = require("./ZipFS");
  9. const watchFile_1 = require("./algorithms/watchFile");
  10. const errors = tslib_1.__importStar(require("./errors"));
  11. const path_1 = require("./path");
  12. // Only file descriptors prefixed by those values will be forwarded to the ZipFS
  13. // instances. Note that the highest ZIP_MAGIC bit MUST NOT be set, otherwise the
  14. // resulting fd becomes a negative integer, which isn't supposed to happen per
  15. // the unix rules (caused problems w/ Go).
  16. //
  17. // Those values must be synced with packages/yarnpkg-pnp/sources/esm-loader/fspatch.ts
  18. //
  19. const ZIP_MASK = 0xff000000;
  20. const ZIP_MAGIC = 0x2a000000;
  21. /**
  22. * Extracts the archive part (ending in the first instance of `extension`) from a path.
  23. *
  24. * The indexOf-based implementation is ~3.7x faster than a RegExp-based implementation.
  25. */
  26. const getArchivePart = (path, extension) => {
  27. let idx = path.indexOf(extension);
  28. if (idx <= 0)
  29. return null;
  30. let nextCharIdx = idx;
  31. while (idx >= 0) {
  32. nextCharIdx = idx + extension.length;
  33. if (path[nextCharIdx] === path_1.ppath.sep)
  34. break;
  35. // Disallow files named ".zip"
  36. if (path[idx - 1] === path_1.ppath.sep)
  37. return null;
  38. idx = path.indexOf(extension, nextCharIdx);
  39. }
  40. // The path either has to end in ".zip" or contain an archive subpath (".zip/...")
  41. if (path.length > nextCharIdx && path[nextCharIdx] !== path_1.ppath.sep)
  42. return null;
  43. return path.slice(0, nextCharIdx);
  44. };
  45. exports.getArchivePart = getArchivePart;
  46. class ZipOpenFS extends FakeFS_1.BasePortableFakeFS {
  47. static async openPromise(fn, opts) {
  48. const zipOpenFs = new ZipOpenFS(opts);
  49. try {
  50. return await fn(zipOpenFs);
  51. }
  52. finally {
  53. zipOpenFs.saveAndClose();
  54. }
  55. }
  56. get libzip() {
  57. if (typeof this.libzipInstance === `undefined`)
  58. this.libzipInstance = this.libzipFactory();
  59. return this.libzipInstance;
  60. }
  61. constructor({ libzip, baseFs = new NodeFS_1.NodeFS(), filter = null, maxOpenFiles = Infinity, readOnlyArchives = false, useCache = true, maxAge = 5000, fileExtensions = null }) {
  62. super();
  63. this.fdMap = new Map();
  64. this.nextFd = 3;
  65. this.isZip = new Set();
  66. this.notZip = new Set();
  67. this.realPaths = new Map();
  68. this.limitOpenFilesTimeout = null;
  69. this.libzipFactory = typeof libzip !== `function`
  70. ? () => libzip
  71. : libzip;
  72. this.baseFs = baseFs;
  73. this.zipInstances = useCache ? new Map() : null;
  74. this.filter = filter;
  75. this.maxOpenFiles = maxOpenFiles;
  76. this.readOnlyArchives = readOnlyArchives;
  77. this.maxAge = maxAge;
  78. this.fileExtensions = fileExtensions;
  79. }
  80. getExtractHint(hints) {
  81. return this.baseFs.getExtractHint(hints);
  82. }
  83. getRealPath() {
  84. return this.baseFs.getRealPath();
  85. }
  86. saveAndClose() {
  87. (0, watchFile_1.unwatchAllFiles)(this);
  88. if (this.zipInstances) {
  89. for (const [path, { zipFs }] of this.zipInstances.entries()) {
  90. zipFs.saveAndClose();
  91. this.zipInstances.delete(path);
  92. }
  93. }
  94. }
  95. discardAndClose() {
  96. (0, watchFile_1.unwatchAllFiles)(this);
  97. if (this.zipInstances) {
  98. for (const [path, { zipFs }] of this.zipInstances.entries()) {
  99. zipFs.discardAndClose();
  100. this.zipInstances.delete(path);
  101. }
  102. }
  103. }
  104. resolve(p) {
  105. return this.baseFs.resolve(p);
  106. }
  107. remapFd(zipFs, fd) {
  108. const remappedFd = this.nextFd++ | ZIP_MAGIC;
  109. this.fdMap.set(remappedFd, [zipFs, fd]);
  110. return remappedFd;
  111. }
  112. async openPromise(p, flags, mode) {
  113. return await this.makeCallPromise(p, async () => {
  114. return await this.baseFs.openPromise(p, flags, mode);
  115. }, async (zipFs, { subPath }) => {
  116. return this.remapFd(zipFs, await zipFs.openPromise(subPath, flags, mode));
  117. });
  118. }
  119. openSync(p, flags, mode) {
  120. return this.makeCallSync(p, () => {
  121. return this.baseFs.openSync(p, flags, mode);
  122. }, (zipFs, { subPath }) => {
  123. return this.remapFd(zipFs, zipFs.openSync(subPath, flags, mode));
  124. });
  125. }
  126. async opendirPromise(p, opts) {
  127. return await this.makeCallPromise(p, async () => {
  128. return await this.baseFs.opendirPromise(p, opts);
  129. }, async (zipFs, { subPath }) => {
  130. return await zipFs.opendirPromise(subPath, opts);
  131. }, {
  132. requireSubpath: false,
  133. });
  134. }
  135. opendirSync(p, opts) {
  136. return this.makeCallSync(p, () => {
  137. return this.baseFs.opendirSync(p, opts);
  138. }, (zipFs, { subPath }) => {
  139. return zipFs.opendirSync(subPath, opts);
  140. }, {
  141. requireSubpath: false,
  142. });
  143. }
  144. async readPromise(fd, buffer, offset, length, position) {
  145. if ((fd & ZIP_MASK) !== ZIP_MAGIC)
  146. return await this.baseFs.readPromise(fd, buffer, offset, length, position);
  147. const entry = this.fdMap.get(fd);
  148. if (typeof entry === `undefined`)
  149. throw errors.EBADF(`read`);
  150. const [zipFs, realFd] = entry;
  151. return await zipFs.readPromise(realFd, buffer, offset, length, position);
  152. }
  153. readSync(fd, buffer, offset, length, position) {
  154. if ((fd & ZIP_MASK) !== ZIP_MAGIC)
  155. return this.baseFs.readSync(fd, buffer, offset, length, position);
  156. const entry = this.fdMap.get(fd);
  157. if (typeof entry === `undefined`)
  158. throw errors.EBADF(`readSync`);
  159. const [zipFs, realFd] = entry;
  160. return zipFs.readSync(realFd, buffer, offset, length, position);
  161. }
  162. async writePromise(fd, buffer, offset, length, position) {
  163. if ((fd & ZIP_MASK) !== ZIP_MAGIC) {
  164. if (typeof buffer === `string`) {
  165. return await this.baseFs.writePromise(fd, buffer, offset);
  166. }
  167. else {
  168. return await this.baseFs.writePromise(fd, buffer, offset, length, position);
  169. }
  170. }
  171. const entry = this.fdMap.get(fd);
  172. if (typeof entry === `undefined`)
  173. throw errors.EBADF(`write`);
  174. const [zipFs, realFd] = entry;
  175. if (typeof buffer === `string`) {
  176. return await zipFs.writePromise(realFd, buffer, offset);
  177. }
  178. else {
  179. return await zipFs.writePromise(realFd, buffer, offset, length, position);
  180. }
  181. }
  182. writeSync(fd, buffer, offset, length, position) {
  183. if ((fd & ZIP_MASK) !== ZIP_MAGIC) {
  184. if (typeof buffer === `string`) {
  185. return this.baseFs.writeSync(fd, buffer, offset);
  186. }
  187. else {
  188. return this.baseFs.writeSync(fd, buffer, offset, length, position);
  189. }
  190. }
  191. const entry = this.fdMap.get(fd);
  192. if (typeof entry === `undefined`)
  193. throw errors.EBADF(`writeSync`);
  194. const [zipFs, realFd] = entry;
  195. if (typeof buffer === `string`) {
  196. return zipFs.writeSync(realFd, buffer, offset);
  197. }
  198. else {
  199. return zipFs.writeSync(realFd, buffer, offset, length, position);
  200. }
  201. }
  202. async closePromise(fd) {
  203. if ((fd & ZIP_MASK) !== ZIP_MAGIC)
  204. return await this.baseFs.closePromise(fd);
  205. const entry = this.fdMap.get(fd);
  206. if (typeof entry === `undefined`)
  207. throw errors.EBADF(`close`);
  208. this.fdMap.delete(fd);
  209. const [zipFs, realFd] = entry;
  210. return await zipFs.closePromise(realFd);
  211. }
  212. closeSync(fd) {
  213. if ((fd & ZIP_MASK) !== ZIP_MAGIC)
  214. return this.baseFs.closeSync(fd);
  215. const entry = this.fdMap.get(fd);
  216. if (typeof entry === `undefined`)
  217. throw errors.EBADF(`closeSync`);
  218. this.fdMap.delete(fd);
  219. const [zipFs, realFd] = entry;
  220. return zipFs.closeSync(realFd);
  221. }
  222. createReadStream(p, opts) {
  223. if (p === null)
  224. return this.baseFs.createReadStream(p, opts);
  225. return this.makeCallSync(p, () => {
  226. return this.baseFs.createReadStream(p, opts);
  227. }, (zipFs, { archivePath, subPath }) => {
  228. const stream = zipFs.createReadStream(subPath, opts);
  229. // This is a very hacky workaround. `ZipOpenFS` shouldn't have to work with `NativePath`s.
  230. // Ref: https://github.com/yarnpkg/berry/pull/3774
  231. // TODO: think of a better solution
  232. stream.path = path_1.npath.fromPortablePath(this.pathUtils.join(archivePath, subPath));
  233. return stream;
  234. });
  235. }
  236. createWriteStream(p, opts) {
  237. if (p === null)
  238. return this.baseFs.createWriteStream(p, opts);
  239. return this.makeCallSync(p, () => {
  240. return this.baseFs.createWriteStream(p, opts);
  241. }, (zipFs, { subPath }) => {
  242. return zipFs.createWriteStream(subPath, opts);
  243. });
  244. }
  245. async realpathPromise(p) {
  246. return await this.makeCallPromise(p, async () => {
  247. return await this.baseFs.realpathPromise(p);
  248. }, async (zipFs, { archivePath, subPath }) => {
  249. let realArchivePath = this.realPaths.get(archivePath);
  250. if (typeof realArchivePath === `undefined`) {
  251. realArchivePath = await this.baseFs.realpathPromise(archivePath);
  252. this.realPaths.set(archivePath, realArchivePath);
  253. }
  254. return this.pathUtils.join(realArchivePath, this.pathUtils.relative(path_1.PortablePath.root, await zipFs.realpathPromise(subPath)));
  255. });
  256. }
  257. realpathSync(p) {
  258. return this.makeCallSync(p, () => {
  259. return this.baseFs.realpathSync(p);
  260. }, (zipFs, { archivePath, subPath }) => {
  261. let realArchivePath = this.realPaths.get(archivePath);
  262. if (typeof realArchivePath === `undefined`) {
  263. realArchivePath = this.baseFs.realpathSync(archivePath);
  264. this.realPaths.set(archivePath, realArchivePath);
  265. }
  266. return this.pathUtils.join(realArchivePath, this.pathUtils.relative(path_1.PortablePath.root, zipFs.realpathSync(subPath)));
  267. });
  268. }
  269. async existsPromise(p) {
  270. return await this.makeCallPromise(p, async () => {
  271. return await this.baseFs.existsPromise(p);
  272. }, async (zipFs, { subPath }) => {
  273. return await zipFs.existsPromise(subPath);
  274. });
  275. }
  276. existsSync(p) {
  277. return this.makeCallSync(p, () => {
  278. return this.baseFs.existsSync(p);
  279. }, (zipFs, { subPath }) => {
  280. return zipFs.existsSync(subPath);
  281. });
  282. }
  283. async accessPromise(p, mode) {
  284. return await this.makeCallPromise(p, async () => {
  285. return await this.baseFs.accessPromise(p, mode);
  286. }, async (zipFs, { subPath }) => {
  287. return await zipFs.accessPromise(subPath, mode);
  288. });
  289. }
  290. accessSync(p, mode) {
  291. return this.makeCallSync(p, () => {
  292. return this.baseFs.accessSync(p, mode);
  293. }, (zipFs, { subPath }) => {
  294. return zipFs.accessSync(subPath, mode);
  295. });
  296. }
  297. async statPromise(p, opts) {
  298. return await this.makeCallPromise(p, async () => {
  299. return await this.baseFs.statPromise(p, opts);
  300. }, async (zipFs, { subPath }) => {
  301. return await zipFs.statPromise(subPath, opts);
  302. });
  303. }
  304. statSync(p, opts) {
  305. return this.makeCallSync(p, () => {
  306. return this.baseFs.statSync(p, opts);
  307. }, (zipFs, { subPath }) => {
  308. return zipFs.statSync(subPath, opts);
  309. });
  310. }
  311. async fstatPromise(fd, opts) {
  312. if ((fd & ZIP_MASK) !== ZIP_MAGIC)
  313. return this.baseFs.fstatPromise(fd, opts);
  314. const entry = this.fdMap.get(fd);
  315. if (typeof entry === `undefined`)
  316. throw errors.EBADF(`fstat`);
  317. const [zipFs, realFd] = entry;
  318. return zipFs.fstatPromise(realFd, opts);
  319. }
  320. fstatSync(fd, opts) {
  321. if ((fd & ZIP_MASK) !== ZIP_MAGIC)
  322. return this.baseFs.fstatSync(fd, opts);
  323. const entry = this.fdMap.get(fd);
  324. if (typeof entry === `undefined`)
  325. throw errors.EBADF(`fstatSync`);
  326. const [zipFs, realFd] = entry;
  327. return zipFs.fstatSync(realFd, opts);
  328. }
  329. async lstatPromise(p, opts) {
  330. return await this.makeCallPromise(p, async () => {
  331. return await this.baseFs.lstatPromise(p, opts);
  332. }, async (zipFs, { subPath }) => {
  333. return await zipFs.lstatPromise(subPath, opts);
  334. });
  335. }
  336. lstatSync(p, opts) {
  337. return this.makeCallSync(p, () => {
  338. return this.baseFs.lstatSync(p, opts);
  339. }, (zipFs, { subPath }) => {
  340. return zipFs.lstatSync(subPath, opts);
  341. });
  342. }
  343. async fchmodPromise(fd, mask) {
  344. if ((fd & ZIP_MASK) !== ZIP_MAGIC)
  345. return this.baseFs.fchmodPromise(fd, mask);
  346. const entry = this.fdMap.get(fd);
  347. if (typeof entry === `undefined`)
  348. throw errors.EBADF(`fchmod`);
  349. const [zipFs, realFd] = entry;
  350. return zipFs.fchmodPromise(realFd, mask);
  351. }
  352. fchmodSync(fd, mask) {
  353. if ((fd & ZIP_MASK) !== ZIP_MAGIC)
  354. return this.baseFs.fchmodSync(fd, mask);
  355. const entry = this.fdMap.get(fd);
  356. if (typeof entry === `undefined`)
  357. throw errors.EBADF(`fchmodSync`);
  358. const [zipFs, realFd] = entry;
  359. return zipFs.fchmodSync(realFd, mask);
  360. }
  361. async chmodPromise(p, mask) {
  362. return await this.makeCallPromise(p, async () => {
  363. return await this.baseFs.chmodPromise(p, mask);
  364. }, async (zipFs, { subPath }) => {
  365. return await zipFs.chmodPromise(subPath, mask);
  366. });
  367. }
  368. chmodSync(p, mask) {
  369. return this.makeCallSync(p, () => {
  370. return this.baseFs.chmodSync(p, mask);
  371. }, (zipFs, { subPath }) => {
  372. return zipFs.chmodSync(subPath, mask);
  373. });
  374. }
  375. async fchownPromise(fd, uid, gid) {
  376. if ((fd & ZIP_MASK) !== ZIP_MAGIC)
  377. return this.baseFs.fchownPromise(fd, uid, gid);
  378. const entry = this.fdMap.get(fd);
  379. if (typeof entry === `undefined`)
  380. throw errors.EBADF(`fchown`);
  381. const [zipFs, realFd] = entry;
  382. return zipFs.fchownPromise(realFd, uid, gid);
  383. }
  384. fchownSync(fd, uid, gid) {
  385. if ((fd & ZIP_MASK) !== ZIP_MAGIC)
  386. return this.baseFs.fchownSync(fd, uid, gid);
  387. const entry = this.fdMap.get(fd);
  388. if (typeof entry === `undefined`)
  389. throw errors.EBADF(`fchownSync`);
  390. const [zipFs, realFd] = entry;
  391. return zipFs.fchownSync(realFd, uid, gid);
  392. }
  393. async chownPromise(p, uid, gid) {
  394. return await this.makeCallPromise(p, async () => {
  395. return await this.baseFs.chownPromise(p, uid, gid);
  396. }, async (zipFs, { subPath }) => {
  397. return await zipFs.chownPromise(subPath, uid, gid);
  398. });
  399. }
  400. chownSync(p, uid, gid) {
  401. return this.makeCallSync(p, () => {
  402. return this.baseFs.chownSync(p, uid, gid);
  403. }, (zipFs, { subPath }) => {
  404. return zipFs.chownSync(subPath, uid, gid);
  405. });
  406. }
  407. async renamePromise(oldP, newP) {
  408. return await this.makeCallPromise(oldP, async () => {
  409. return await this.makeCallPromise(newP, async () => {
  410. return await this.baseFs.renamePromise(oldP, newP);
  411. }, async () => {
  412. throw Object.assign(new Error(`EEXDEV: cross-device link not permitted`), { code: `EEXDEV` });
  413. });
  414. }, async (zipFsO, { subPath: subPathO }) => {
  415. return await this.makeCallPromise(newP, async () => {
  416. throw Object.assign(new Error(`EEXDEV: cross-device link not permitted`), { code: `EEXDEV` });
  417. }, async (zipFsN, { subPath: subPathN }) => {
  418. if (zipFsO !== zipFsN) {
  419. throw Object.assign(new Error(`EEXDEV: cross-device link not permitted`), { code: `EEXDEV` });
  420. }
  421. else {
  422. return await zipFsO.renamePromise(subPathO, subPathN);
  423. }
  424. });
  425. });
  426. }
  427. renameSync(oldP, newP) {
  428. return this.makeCallSync(oldP, () => {
  429. return this.makeCallSync(newP, () => {
  430. return this.baseFs.renameSync(oldP, newP);
  431. }, () => {
  432. throw Object.assign(new Error(`EEXDEV: cross-device link not permitted`), { code: `EEXDEV` });
  433. });
  434. }, (zipFsO, { subPath: subPathO }) => {
  435. return this.makeCallSync(newP, () => {
  436. throw Object.assign(new Error(`EEXDEV: cross-device link not permitted`), { code: `EEXDEV` });
  437. }, (zipFsN, { subPath: subPathN }) => {
  438. if (zipFsO !== zipFsN) {
  439. throw Object.assign(new Error(`EEXDEV: cross-device link not permitted`), { code: `EEXDEV` });
  440. }
  441. else {
  442. return zipFsO.renameSync(subPathO, subPathN);
  443. }
  444. });
  445. });
  446. }
  447. async copyFilePromise(sourceP, destP, flags = 0) {
  448. const fallback = async (sourceFs, sourceP, destFs, destP) => {
  449. if ((flags & fs_1.constants.COPYFILE_FICLONE_FORCE) !== 0)
  450. throw Object.assign(new Error(`EXDEV: cross-device clone not permitted, copyfile '${sourceP}' -> ${destP}'`), { code: `EXDEV` });
  451. if ((flags & fs_1.constants.COPYFILE_EXCL) && await this.existsPromise(sourceP))
  452. throw Object.assign(new Error(`EEXIST: file already exists, copyfile '${sourceP}' -> '${destP}'`), { code: `EEXIST` });
  453. let content;
  454. try {
  455. content = await sourceFs.readFilePromise(sourceP);
  456. }
  457. catch (error) {
  458. throw Object.assign(new Error(`EINVAL: invalid argument, copyfile '${sourceP}' -> '${destP}'`), { code: `EINVAL` });
  459. }
  460. await destFs.writeFilePromise(destP, content);
  461. };
  462. return await this.makeCallPromise(sourceP, async () => {
  463. return await this.makeCallPromise(destP, async () => {
  464. return await this.baseFs.copyFilePromise(sourceP, destP, flags);
  465. }, async (zipFsD, { subPath: subPathD }) => {
  466. return await fallback(this.baseFs, sourceP, zipFsD, subPathD);
  467. });
  468. }, async (zipFsS, { subPath: subPathS }) => {
  469. return await this.makeCallPromise(destP, async () => {
  470. return await fallback(zipFsS, subPathS, this.baseFs, destP);
  471. }, async (zipFsD, { subPath: subPathD }) => {
  472. if (zipFsS !== zipFsD) {
  473. return await fallback(zipFsS, subPathS, zipFsD, subPathD);
  474. }
  475. else {
  476. return await zipFsS.copyFilePromise(subPathS, subPathD, flags);
  477. }
  478. });
  479. });
  480. }
  481. copyFileSync(sourceP, destP, flags = 0) {
  482. const fallback = (sourceFs, sourceP, destFs, destP) => {
  483. if ((flags & fs_1.constants.COPYFILE_FICLONE_FORCE) !== 0)
  484. throw Object.assign(new Error(`EXDEV: cross-device clone not permitted, copyfile '${sourceP}' -> ${destP}'`), { code: `EXDEV` });
  485. if ((flags & fs_1.constants.COPYFILE_EXCL) && this.existsSync(sourceP))
  486. throw Object.assign(new Error(`EEXIST: file already exists, copyfile '${sourceP}' -> '${destP}'`), { code: `EEXIST` });
  487. let content;
  488. try {
  489. content = sourceFs.readFileSync(sourceP);
  490. }
  491. catch (error) {
  492. throw Object.assign(new Error(`EINVAL: invalid argument, copyfile '${sourceP}' -> '${destP}'`), { code: `EINVAL` });
  493. }
  494. destFs.writeFileSync(destP, content);
  495. };
  496. return this.makeCallSync(sourceP, () => {
  497. return this.makeCallSync(destP, () => {
  498. return this.baseFs.copyFileSync(sourceP, destP, flags);
  499. }, (zipFsD, { subPath: subPathD }) => {
  500. return fallback(this.baseFs, sourceP, zipFsD, subPathD);
  501. });
  502. }, (zipFsS, { subPath: subPathS }) => {
  503. return this.makeCallSync(destP, () => {
  504. return fallback(zipFsS, subPathS, this.baseFs, destP);
  505. }, (zipFsD, { subPath: subPathD }) => {
  506. if (zipFsS !== zipFsD) {
  507. return fallback(zipFsS, subPathS, zipFsD, subPathD);
  508. }
  509. else {
  510. return zipFsS.copyFileSync(subPathS, subPathD, flags);
  511. }
  512. });
  513. });
  514. }
  515. async appendFilePromise(p, content, opts) {
  516. return await this.makeCallPromise(p, async () => {
  517. return await this.baseFs.appendFilePromise(p, content, opts);
  518. }, async (zipFs, { subPath }) => {
  519. return await zipFs.appendFilePromise(subPath, content, opts);
  520. });
  521. }
  522. appendFileSync(p, content, opts) {
  523. return this.makeCallSync(p, () => {
  524. return this.baseFs.appendFileSync(p, content, opts);
  525. }, (zipFs, { subPath }) => {
  526. return zipFs.appendFileSync(subPath, content, opts);
  527. });
  528. }
  529. async writeFilePromise(p, content, opts) {
  530. return await this.makeCallPromise(p, async () => {
  531. return await this.baseFs.writeFilePromise(p, content, opts);
  532. }, async (zipFs, { subPath }) => {
  533. return await zipFs.writeFilePromise(subPath, content, opts);
  534. });
  535. }
  536. writeFileSync(p, content, opts) {
  537. return this.makeCallSync(p, () => {
  538. return this.baseFs.writeFileSync(p, content, opts);
  539. }, (zipFs, { subPath }) => {
  540. return zipFs.writeFileSync(subPath, content, opts);
  541. });
  542. }
  543. async unlinkPromise(p) {
  544. return await this.makeCallPromise(p, async () => {
  545. return await this.baseFs.unlinkPromise(p);
  546. }, async (zipFs, { subPath }) => {
  547. return await zipFs.unlinkPromise(subPath);
  548. });
  549. }
  550. unlinkSync(p) {
  551. return this.makeCallSync(p, () => {
  552. return this.baseFs.unlinkSync(p);
  553. }, (zipFs, { subPath }) => {
  554. return zipFs.unlinkSync(subPath);
  555. });
  556. }
  557. async utimesPromise(p, atime, mtime) {
  558. return await this.makeCallPromise(p, async () => {
  559. return await this.baseFs.utimesPromise(p, atime, mtime);
  560. }, async (zipFs, { subPath }) => {
  561. return await zipFs.utimesPromise(subPath, atime, mtime);
  562. });
  563. }
  564. utimesSync(p, atime, mtime) {
  565. return this.makeCallSync(p, () => {
  566. return this.baseFs.utimesSync(p, atime, mtime);
  567. }, (zipFs, { subPath }) => {
  568. return zipFs.utimesSync(subPath, atime, mtime);
  569. });
  570. }
  571. async mkdirPromise(p, opts) {
  572. return await this.makeCallPromise(p, async () => {
  573. return await this.baseFs.mkdirPromise(p, opts);
  574. }, async (zipFs, { subPath }) => {
  575. return await zipFs.mkdirPromise(subPath, opts);
  576. });
  577. }
  578. mkdirSync(p, opts) {
  579. return this.makeCallSync(p, () => {
  580. return this.baseFs.mkdirSync(p, opts);
  581. }, (zipFs, { subPath }) => {
  582. return zipFs.mkdirSync(subPath, opts);
  583. });
  584. }
  585. async rmdirPromise(p, opts) {
  586. return await this.makeCallPromise(p, async () => {
  587. return await this.baseFs.rmdirPromise(p, opts);
  588. }, async (zipFs, { subPath }) => {
  589. return await zipFs.rmdirPromise(subPath, opts);
  590. });
  591. }
  592. rmdirSync(p, opts) {
  593. return this.makeCallSync(p, () => {
  594. return this.baseFs.rmdirSync(p, opts);
  595. }, (zipFs, { subPath }) => {
  596. return zipFs.rmdirSync(subPath, opts);
  597. });
  598. }
  599. async linkPromise(existingP, newP) {
  600. return await this.makeCallPromise(newP, async () => {
  601. return await this.baseFs.linkPromise(existingP, newP);
  602. }, async (zipFs, { subPath }) => {
  603. return await zipFs.linkPromise(existingP, subPath);
  604. });
  605. }
  606. linkSync(existingP, newP) {
  607. return this.makeCallSync(newP, () => {
  608. return this.baseFs.linkSync(existingP, newP);
  609. }, (zipFs, { subPath }) => {
  610. return zipFs.linkSync(existingP, subPath);
  611. });
  612. }
  613. async symlinkPromise(target, p, type) {
  614. return await this.makeCallPromise(p, async () => {
  615. return await this.baseFs.symlinkPromise(target, p, type);
  616. }, async (zipFs, { subPath }) => {
  617. return await zipFs.symlinkPromise(target, subPath);
  618. });
  619. }
  620. symlinkSync(target, p, type) {
  621. return this.makeCallSync(p, () => {
  622. return this.baseFs.symlinkSync(target, p, type);
  623. }, (zipFs, { subPath }) => {
  624. return zipFs.symlinkSync(target, subPath);
  625. });
  626. }
  627. async readFilePromise(p, encoding) {
  628. return this.makeCallPromise(p, async () => {
  629. // This weird switch is required to tell TypeScript that the signatures are proper (otherwise it thinks that only the generic one is covered)
  630. switch (encoding) {
  631. case `utf8`:
  632. return await this.baseFs.readFilePromise(p, encoding);
  633. default:
  634. return await this.baseFs.readFilePromise(p, encoding);
  635. }
  636. }, async (zipFs, { subPath }) => {
  637. return await zipFs.readFilePromise(subPath, encoding);
  638. });
  639. }
  640. readFileSync(p, encoding) {
  641. return this.makeCallSync(p, () => {
  642. // This weird switch is required to tell TypeScript that the signatures are proper (otherwise it thinks that only the generic one is covered)
  643. switch (encoding) {
  644. case `utf8`:
  645. return this.baseFs.readFileSync(p, encoding);
  646. default:
  647. return this.baseFs.readFileSync(p, encoding);
  648. }
  649. }, (zipFs, { subPath }) => {
  650. return zipFs.readFileSync(subPath, encoding);
  651. });
  652. }
  653. async readdirPromise(p, opts) {
  654. return await this.makeCallPromise(p, async () => {
  655. return await this.baseFs.readdirPromise(p, opts);
  656. }, async (zipFs, { subPath }) => {
  657. return await zipFs.readdirPromise(subPath, opts);
  658. }, {
  659. requireSubpath: false,
  660. });
  661. }
  662. readdirSync(p, opts) {
  663. return this.makeCallSync(p, () => {
  664. return this.baseFs.readdirSync(p, opts);
  665. }, (zipFs, { subPath }) => {
  666. return zipFs.readdirSync(subPath, opts);
  667. }, {
  668. requireSubpath: false,
  669. });
  670. }
  671. async readlinkPromise(p) {
  672. return await this.makeCallPromise(p, async () => {
  673. return await this.baseFs.readlinkPromise(p);
  674. }, async (zipFs, { subPath }) => {
  675. return await zipFs.readlinkPromise(subPath);
  676. });
  677. }
  678. readlinkSync(p) {
  679. return this.makeCallSync(p, () => {
  680. return this.baseFs.readlinkSync(p);
  681. }, (zipFs, { subPath }) => {
  682. return zipFs.readlinkSync(subPath);
  683. });
  684. }
  685. async truncatePromise(p, len) {
  686. return await this.makeCallPromise(p, async () => {
  687. return await this.baseFs.truncatePromise(p, len);
  688. }, async (zipFs, { subPath }) => {
  689. return await zipFs.truncatePromise(subPath, len);
  690. });
  691. }
  692. truncateSync(p, len) {
  693. return this.makeCallSync(p, () => {
  694. return this.baseFs.truncateSync(p, len);
  695. }, (zipFs, { subPath }) => {
  696. return zipFs.truncateSync(subPath, len);
  697. });
  698. }
  699. async ftruncatePromise(fd, len) {
  700. if ((fd & ZIP_MASK) !== ZIP_MAGIC)
  701. return this.baseFs.ftruncatePromise(fd, len);
  702. const entry = this.fdMap.get(fd);
  703. if (typeof entry === `undefined`)
  704. throw errors.EBADF(`ftruncate`);
  705. const [zipFs, realFd] = entry;
  706. return zipFs.ftruncatePromise(realFd, len);
  707. }
  708. ftruncateSync(fd, len) {
  709. if ((fd & ZIP_MASK) !== ZIP_MAGIC)
  710. return this.baseFs.ftruncateSync(fd, len);
  711. const entry = this.fdMap.get(fd);
  712. if (typeof entry === `undefined`)
  713. throw errors.EBADF(`ftruncateSync`);
  714. const [zipFs, realFd] = entry;
  715. return zipFs.ftruncateSync(realFd, len);
  716. }
  717. watch(p, a, b) {
  718. return this.makeCallSync(p, () => {
  719. return this.baseFs.watch(p,
  720. // @ts-expect-error
  721. a, b);
  722. }, (zipFs, { subPath }) => {
  723. return zipFs.watch(subPath,
  724. // @ts-expect-error
  725. a, b);
  726. });
  727. }
  728. watchFile(p, a, b) {
  729. return this.makeCallSync(p, () => {
  730. return this.baseFs.watchFile(p,
  731. // @ts-expect-error
  732. a, b);
  733. }, () => {
  734. return (0, watchFile_1.watchFile)(this, p, a, b);
  735. });
  736. }
  737. unwatchFile(p, cb) {
  738. return this.makeCallSync(p, () => {
  739. return this.baseFs.unwatchFile(p, cb);
  740. }, () => {
  741. return (0, watchFile_1.unwatchFile)(this, p, cb);
  742. });
  743. }
  744. async makeCallPromise(p, discard, accept, { requireSubpath = true } = {}) {
  745. if (typeof p !== `string`)
  746. return await discard();
  747. const normalizedP = this.resolve(p);
  748. const zipInfo = this.findZip(normalizedP);
  749. if (!zipInfo)
  750. return await discard();
  751. if (requireSubpath && zipInfo.subPath === `/`)
  752. return await discard();
  753. return await this.getZipPromise(zipInfo.archivePath, async (zipFs) => await accept(zipFs, zipInfo));
  754. }
  755. makeCallSync(p, discard, accept, { requireSubpath = true } = {}) {
  756. if (typeof p !== `string`)
  757. return discard();
  758. const normalizedP = this.resolve(p);
  759. const zipInfo = this.findZip(normalizedP);
  760. if (!zipInfo)
  761. return discard();
  762. if (requireSubpath && zipInfo.subPath === `/`)
  763. return discard();
  764. return this.getZipSync(zipInfo.archivePath, zipFs => accept(zipFs, zipInfo));
  765. }
  766. findZip(p) {
  767. if (this.filter && !this.filter.test(p))
  768. return null;
  769. let filePath = ``;
  770. while (true) {
  771. const pathPartWithArchive = p.substring(filePath.length);
  772. let archivePart;
  773. if (!this.fileExtensions) {
  774. archivePart = (0, exports.getArchivePart)(pathPartWithArchive, `.zip`);
  775. }
  776. else {
  777. for (const ext of this.fileExtensions) {
  778. archivePart = (0, exports.getArchivePart)(pathPartWithArchive, ext);
  779. if (archivePart) {
  780. break;
  781. }
  782. }
  783. }
  784. if (!archivePart)
  785. return null;
  786. filePath = this.pathUtils.join(filePath, archivePart);
  787. if (this.isZip.has(filePath) === false) {
  788. if (this.notZip.has(filePath))
  789. continue;
  790. try {
  791. if (!this.baseFs.lstatSync(filePath).isFile()) {
  792. this.notZip.add(filePath);
  793. continue;
  794. }
  795. }
  796. catch {
  797. return null;
  798. }
  799. this.isZip.add(filePath);
  800. }
  801. return {
  802. archivePath: filePath,
  803. subPath: this.pathUtils.join(path_1.PortablePath.root, p.substring(filePath.length)),
  804. };
  805. }
  806. }
  807. limitOpenFiles(max) {
  808. if (this.zipInstances === null)
  809. return;
  810. const now = Date.now();
  811. let nextExpiresAt = now + this.maxAge;
  812. let closeCount = max === null ? 0 : this.zipInstances.size - max;
  813. for (const [path, { zipFs, expiresAt, refCount }] of this.zipInstances.entries()) {
  814. if (refCount !== 0 || zipFs.hasOpenFileHandles()) {
  815. continue;
  816. }
  817. else if (now >= expiresAt) {
  818. zipFs.saveAndClose();
  819. this.zipInstances.delete(path);
  820. closeCount -= 1;
  821. continue;
  822. }
  823. else if (max === null || closeCount <= 0) {
  824. nextExpiresAt = expiresAt;
  825. break;
  826. }
  827. zipFs.saveAndClose();
  828. this.zipInstances.delete(path);
  829. closeCount -= 1;
  830. }
  831. if (this.limitOpenFilesTimeout === null && ((max === null && this.zipInstances.size > 0) || max !== null)) {
  832. this.limitOpenFilesTimeout = setTimeout(() => {
  833. this.limitOpenFilesTimeout = null;
  834. this.limitOpenFiles(null);
  835. }, nextExpiresAt - now).unref();
  836. }
  837. }
  838. async getZipPromise(p, accept) {
  839. const getZipOptions = async () => ({
  840. baseFs: this.baseFs,
  841. libzip: this.libzip,
  842. readOnly: this.readOnlyArchives,
  843. stats: await this.baseFs.statPromise(p),
  844. });
  845. if (this.zipInstances) {
  846. let cachedZipFs = this.zipInstances.get(p);
  847. if (!cachedZipFs) {
  848. const zipOptions = await getZipOptions();
  849. // We need to recheck because concurrent getZipPromise calls may
  850. // have instantiated the zip archive while we were waiting
  851. cachedZipFs = this.zipInstances.get(p);
  852. if (!cachedZipFs) {
  853. cachedZipFs = {
  854. zipFs: new ZipFS_1.ZipFS(p, zipOptions),
  855. expiresAt: 0,
  856. refCount: 0,
  857. };
  858. }
  859. }
  860. // Removing then re-adding the field allows us to easily implement
  861. // a basic LRU garbage collection strategy
  862. this.zipInstances.delete(p);
  863. this.limitOpenFiles(this.maxOpenFiles - 1);
  864. this.zipInstances.set(p, cachedZipFs);
  865. cachedZipFs.expiresAt = Date.now() + this.maxAge;
  866. cachedZipFs.refCount += 1;
  867. try {
  868. return await accept(cachedZipFs.zipFs);
  869. }
  870. finally {
  871. cachedZipFs.refCount -= 1;
  872. }
  873. }
  874. else {
  875. const zipFs = new ZipFS_1.ZipFS(p, await getZipOptions());
  876. try {
  877. return await accept(zipFs);
  878. }
  879. finally {
  880. zipFs.saveAndClose();
  881. }
  882. }
  883. }
  884. getZipSync(p, accept) {
  885. const getZipOptions = () => ({
  886. baseFs: this.baseFs,
  887. libzip: this.libzip,
  888. readOnly: this.readOnlyArchives,
  889. stats: this.baseFs.statSync(p),
  890. });
  891. if (this.zipInstances) {
  892. let cachedZipFs = this.zipInstances.get(p);
  893. if (!cachedZipFs) {
  894. cachedZipFs = {
  895. zipFs: new ZipFS_1.ZipFS(p, getZipOptions()),
  896. expiresAt: 0,
  897. refCount: 0,
  898. };
  899. }
  900. // Removing then re-adding the field allows us to easily implement
  901. // a basic LRU garbage collection strategy
  902. this.zipInstances.delete(p);
  903. this.limitOpenFiles(this.maxOpenFiles - 1);
  904. this.zipInstances.set(p, cachedZipFs);
  905. cachedZipFs.expiresAt = Date.now() + this.maxAge;
  906. return accept(cachedZipFs.zipFs);
  907. }
  908. else {
  909. const zipFs = new ZipFS_1.ZipFS(p, getZipOptions());
  910. try {
  911. return accept(zipFs);
  912. }
  913. finally {
  914. zipFs.saveAndClose();
  915. }
  916. }
  917. }
  918. }
  919. exports.ZipOpenFS = ZipOpenFS;