browser.dom.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. "use strict";
  2. var ZenFS_DOM = (() => {
  3. var __defProp = Object.defineProperty;
  4. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  5. var __getOwnPropNames = Object.getOwnPropertyNames;
  6. var __hasOwnProp = Object.prototype.hasOwnProperty;
  7. var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
  8. var __export = (target, all) => {
  9. for (var name in all)
  10. __defProp(target, name, { get: all[name], enumerable: true });
  11. };
  12. var __copyProps = (to, from, except, desc) => {
  13. if (from && typeof from === "object" || typeof from === "function") {
  14. for (let key of __getOwnPropNames(from))
  15. if (!__hasOwnProp.call(to, key) && key !== except)
  16. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  17. }
  18. return to;
  19. };
  20. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  21. // src/index.ts
  22. var src_exports = {};
  23. __export(src_exports, {
  24. IndexedDB: () => IndexedDB,
  25. IndexedDBStore: () => IndexedDBStore,
  26. IndexedDBTransaction: () => IndexedDBTransaction,
  27. WebAccess: () => WebAccess,
  28. WebAccessFS: () => WebAccessFS,
  29. WebStorage: () => WebStorage,
  30. WebStorageStore: () => WebStorageStore
  31. });
  32. // global-externals:@zenfs/core
  33. var core_default = ZenFS;
  34. var { ActionType, Async, AsyncIndexFS, AsyncTransaction, BigIntStats, BigIntStatsFs, Dir, Dirent, Errno, ErrnoError, Fetch, FetchFS, File, FileIndex, FileSystem, FileType, InMemory, InMemoryStore, IndexDirInode, IndexFS, IndexFileInode, IndexInode, Inode, LockedFS, Mutex, NoSyncFile, Overlay, OverlayFS, Port, PortFS, PortFile, PreloadFile, ReadStream, Readonly, SimpleAsyncStore, SimpleTransaction, Stats, StatsCommon, StatsFs, StoreFS, Sync, SyncIndexFS, SyncTransaction, Transaction, UnlockedOverlayFS, WriteStream, _toUnixTimestamp, access, accessSync, appendFile, appendFileSync, attachFS, checkOptions, chmod, chmodSync, chown, chownSync, close, closeSync, configure, constants, copyFile, copyFileSync, cp, cpSync, createReadStream, createWriteStream, decode, decodeDirListing, detachFS, encode, encodeDirListing, errorMessages, exists, existsSync, fchmod, fchmodSync, fchown, fchownSync, fdatasync, fdatasyncSync, flagToMode, flagToNumber, flagToString, fs, fstat, fstatSync, fsync, fsyncSync, ftruncate, ftruncateSync, futimes, futimesSync, isAppendable, isBackend, isBackendConfig, isExclusive, isReadable, isSynchronous, isTruncating, isWriteable, lchmod, lchmodSync, lchown, lchownSync, levenshtein, link, linkSync, lopenSync, lstat, lstatSync, lutimes, lutimesSync, mkdir, mkdirSync, mkdirpSync, mkdtemp, mkdtempSync, mount, mountObject, mounts, nop, normalizeMode, normalizeOptions, normalizePath, normalizeTime, open, openAsBlob, openSync, opendir, opendirSync, parseFlag, pathExistsAction, pathNotExistsAction, promises, randomIno, read, readFile, readFileSync, readSync, readdir, readdirSync, readlink, readlinkSync, readv, readvSync, realpath, realpathSync, rename, renameSync, resolveMountConfig, rm, rmSync, rmdir, rmdirSync, rootCred, rootIno, setImmediate, size_max, stat, statSync, statfs, statfsSync, symlink, symlinkSync, truncate, truncateSync, umount, unlink, unlinkSync, unwatchFile, utimes, utimesSync, watch, watchFile, write, writeFile, writeFileSync, writeSync, writev, writevSync } = ZenFS;
  35. // node_modules/@zenfs/core/dist/emulation/path.js
  36. function normalizeString(path, allowAboveRoot) {
  37. let res = "";
  38. let lastSegmentLength = 0;
  39. let lastSlash = -1;
  40. let dots = 0;
  41. let char = "\0";
  42. for (let i = 0; i <= path.length; ++i) {
  43. if (i < path.length) {
  44. char = path[i];
  45. } else if (char == "/") {
  46. break;
  47. } else {
  48. char = "/";
  49. }
  50. if (char == "/") {
  51. if (lastSlash === i - 1 || dots === 1) {
  52. } else if (dots === 2) {
  53. if (res.length < 2 || lastSegmentLength !== 2 || res.at(-1) !== "." || res.at(-2) !== ".") {
  54. if (res.length > 2) {
  55. const lastSlashIndex = res.lastIndexOf("/");
  56. if (lastSlashIndex === -1) {
  57. res = "";
  58. lastSegmentLength = 0;
  59. } else {
  60. res = res.slice(0, lastSlashIndex);
  61. lastSegmentLength = res.length - 1 - res.lastIndexOf("/");
  62. }
  63. lastSlash = i;
  64. dots = 0;
  65. continue;
  66. } else if (res.length !== 0) {
  67. res = "";
  68. lastSegmentLength = 0;
  69. lastSlash = i;
  70. dots = 0;
  71. continue;
  72. }
  73. }
  74. if (allowAboveRoot) {
  75. res += res.length > 0 ? "/.." : "..";
  76. lastSegmentLength = 2;
  77. }
  78. } else {
  79. if (res.length > 0)
  80. res += "/" + path.slice(lastSlash + 1, i);
  81. else
  82. res = path.slice(lastSlash + 1, i);
  83. lastSegmentLength = i - lastSlash - 1;
  84. }
  85. lastSlash = i;
  86. dots = 0;
  87. } else if (char === "." && dots !== -1) {
  88. ++dots;
  89. } else {
  90. dots = -1;
  91. }
  92. }
  93. return res;
  94. }
  95. __name(normalizeString, "normalizeString");
  96. function normalize(path) {
  97. if (!path.length)
  98. return ".";
  99. const isAbsolute = path.startsWith("/");
  100. const trailingSeparator = path.endsWith("/");
  101. path = normalizeString(path, !isAbsolute);
  102. if (!path.length) {
  103. if (isAbsolute)
  104. return "/";
  105. return trailingSeparator ? "./" : ".";
  106. }
  107. if (trailingSeparator)
  108. path += "/";
  109. return isAbsolute ? `/${path}` : path;
  110. }
  111. __name(normalize, "normalize");
  112. function join(...parts) {
  113. if (!parts.length)
  114. return ".";
  115. const joined = parts.join("/");
  116. if (!joined?.length)
  117. return ".";
  118. return normalize(joined);
  119. }
  120. __name(join, "join");
  121. function dirname(path) {
  122. if (path.length === 0)
  123. return ".";
  124. const hasRoot = path[0] === "/";
  125. let end = -1;
  126. let matchedSlash = true;
  127. for (let i = path.length - 1; i >= 1; --i) {
  128. if (path[i] === "/") {
  129. if (!matchedSlash) {
  130. end = i;
  131. break;
  132. }
  133. } else {
  134. matchedSlash = false;
  135. }
  136. }
  137. if (end === -1)
  138. return hasRoot ? "/" : ".";
  139. if (hasRoot && end === 1)
  140. return "//";
  141. return path.slice(0, end);
  142. }
  143. __name(dirname, "dirname");
  144. function basename(path, suffix) {
  145. let start = 0;
  146. let end = -1;
  147. let matchedSlash = true;
  148. if (suffix !== void 0 && suffix.length > 0 && suffix.length <= path.length) {
  149. if (suffix === path)
  150. return "";
  151. let extIdx = suffix.length - 1;
  152. let firstNonSlashEnd = -1;
  153. for (let i = path.length - 1; i >= 0; --i) {
  154. if (path[i] === "/") {
  155. if (!matchedSlash) {
  156. start = i + 1;
  157. break;
  158. }
  159. } else {
  160. if (firstNonSlashEnd === -1) {
  161. matchedSlash = false;
  162. firstNonSlashEnd = i + 1;
  163. }
  164. if (extIdx >= 0) {
  165. if (path[i] === suffix[extIdx]) {
  166. if (--extIdx === -1) {
  167. end = i;
  168. }
  169. } else {
  170. extIdx = -1;
  171. end = firstNonSlashEnd;
  172. }
  173. }
  174. }
  175. }
  176. if (start === end)
  177. end = firstNonSlashEnd;
  178. else if (end === -1)
  179. end = path.length;
  180. return path.slice(start, end);
  181. }
  182. for (let i = path.length - 1; i >= 0; --i) {
  183. if (path[i] === "/") {
  184. if (!matchedSlash) {
  185. start = i + 1;
  186. break;
  187. }
  188. } else if (end === -1) {
  189. matchedSlash = false;
  190. end = i + 1;
  191. }
  192. }
  193. if (end === -1)
  194. return "";
  195. return path.slice(start, end);
  196. }
  197. __name(basename, "basename");
  198. // src/utils.ts
  199. function errnoForDOMException(ex) {
  200. switch (ex.name) {
  201. case "IndexSizeError":
  202. case "HierarchyRequestError":
  203. case "InvalidCharacterError":
  204. case "InvalidStateError":
  205. case "SyntaxError":
  206. case "NamespaceError":
  207. case "TypeMismatchError":
  208. case "ConstraintError":
  209. case "VersionError":
  210. case "URLMismatchError":
  211. case "InvalidNodeTypeError":
  212. return "EINVAL";
  213. case "WrongDocumentError":
  214. return "EXDEV";
  215. case "NoModificationAllowedError":
  216. case "InvalidModificationError":
  217. case "InvalidAccessError":
  218. case "SecurityError":
  219. case "NotAllowedError":
  220. return "EACCES";
  221. case "NotFoundError":
  222. return "ENOENT";
  223. case "NotSupportedError":
  224. return "ENOTSUP";
  225. case "InUseAttributeError":
  226. return "EBUSY";
  227. case "NetworkError":
  228. return "ENETDOWN";
  229. case "AbortError":
  230. return "EINTR";
  231. case "QuotaExceededError":
  232. return "ENOSPC";
  233. case "TimeoutError":
  234. return "ETIMEDOUT";
  235. case "ReadOnlyError":
  236. return "EROFS";
  237. case "DataCloneError":
  238. case "EncodingError":
  239. case "NotReadableError":
  240. case "DataError":
  241. case "TransactionInactiveError":
  242. case "OperationError":
  243. case "UnknownError":
  244. default:
  245. return "EIO";
  246. }
  247. }
  248. __name(errnoForDOMException, "errnoForDOMException");
  249. function convertException(ex, path, syscall) {
  250. if (ex instanceof ErrnoError) {
  251. return ex;
  252. }
  253. const code = ex instanceof DOMException ? Errno[errnoForDOMException(ex)] : Errno.EIO;
  254. const error = new ErrnoError(code, ex.message, path, syscall);
  255. error.stack = ex.stack;
  256. error.cause = ex.cause;
  257. return error;
  258. }
  259. __name(convertException, "convertException");
  260. // src/access.ts
  261. var WebAccessFS = class extends Async(FileSystem) {
  262. _handles = /* @__PURE__ */ new Map();
  263. /**
  264. * @hidden
  265. */
  266. _sync;
  267. constructor({ handle }) {
  268. super();
  269. this._handles.set("/", handle);
  270. this._sync = InMemory.create({ name: "accessfs-cache" });
  271. }
  272. metadata() {
  273. return {
  274. ...super.metadata(),
  275. name: "WebAccess"
  276. };
  277. }
  278. async sync(path, data, stats) {
  279. const currentStats = await this.stat(path);
  280. if (stats.mtime !== currentStats.mtime) {
  281. await this.writeFile(path, data);
  282. }
  283. }
  284. async rename(oldPath, newPath) {
  285. try {
  286. const handle = await this.getHandle(oldPath);
  287. if (handle instanceof FileSystemDirectoryHandle) {
  288. const files = await this.readdir(oldPath);
  289. await this.mkdir(newPath);
  290. if (files.length == 0) {
  291. await this.unlink(oldPath);
  292. } else {
  293. for (const file of files) {
  294. await this.rename(join(oldPath, file), join(newPath, file));
  295. await this.unlink(oldPath);
  296. }
  297. }
  298. }
  299. if (!(handle instanceof FileSystemFileHandle)) {
  300. return;
  301. }
  302. const oldFile = await handle.getFile(), destFolder = await this.getHandle(dirname(newPath));
  303. if (!(destFolder instanceof FileSystemDirectoryHandle)) {
  304. return;
  305. }
  306. const newFile = await destFolder.getFileHandle(basename(newPath), { create: true });
  307. const writable = await newFile.createWritable();
  308. await writable.write(await oldFile.arrayBuffer());
  309. writable.close();
  310. await this.unlink(oldPath);
  311. } catch (ex) {
  312. throw convertException(ex, oldPath, "rename");
  313. }
  314. }
  315. async writeFile(fname, data) {
  316. const handle = await this.getHandle(dirname(fname));
  317. if (!(handle instanceof FileSystemDirectoryHandle)) {
  318. return;
  319. }
  320. const file = await handle.getFileHandle(basename(fname), { create: true });
  321. const writable = await file.createWritable();
  322. await writable.write(data);
  323. await writable.close();
  324. }
  325. async createFile(path, flag) {
  326. await this.writeFile(path, new Uint8Array());
  327. return this.openFile(path, flag);
  328. }
  329. async stat(path) {
  330. const handle = await this.getHandle(path);
  331. if (!handle) {
  332. throw ErrnoError.With("ENOENT", path, "stat");
  333. }
  334. if (handle instanceof FileSystemDirectoryHandle) {
  335. return new Stats({ mode: 511 | FileType.DIRECTORY, size: 4096 });
  336. }
  337. if (handle instanceof FileSystemFileHandle) {
  338. const { lastModified, size } = await handle.getFile();
  339. return new Stats({ mode: 511 | FileType.FILE, size, mtimeMs: lastModified });
  340. }
  341. throw new ErrnoError(Errno.EBADE, "Handle is not a directory or file", path, "stat");
  342. }
  343. async openFile(path, flag) {
  344. const handle = await this.getHandle(path);
  345. if (!(handle instanceof FileSystemFileHandle)) {
  346. throw ErrnoError.With("EISDIR", path, "openFile");
  347. }
  348. try {
  349. const file = await handle.getFile();
  350. const data = new Uint8Array(await file.arrayBuffer());
  351. const stats = new Stats({ mode: 511 | FileType.FILE, size: file.size, mtimeMs: file.lastModified });
  352. return new PreloadFile(this, path, flag, stats, data);
  353. } catch (ex) {
  354. throw convertException(ex, path, "openFile");
  355. }
  356. }
  357. async unlink(path) {
  358. const handle = await this.getHandle(dirname(path));
  359. if (handle instanceof FileSystemDirectoryHandle) {
  360. try {
  361. await handle.removeEntry(basename(path), { recursive: true });
  362. } catch (ex) {
  363. throw convertException(ex, path, "unlink");
  364. }
  365. }
  366. }
  367. async link(srcpath) {
  368. throw ErrnoError.With("ENOSYS", srcpath, "WebAccessFS.link");
  369. }
  370. async rmdir(path) {
  371. return this.unlink(path);
  372. }
  373. async mkdir(path) {
  374. const existingHandle = await this.getHandle(path);
  375. if (existingHandle) {
  376. throw ErrnoError.With("EEXIST", path, "mkdir");
  377. }
  378. const handle = await this.getHandle(dirname(path));
  379. if (!(handle instanceof FileSystemDirectoryHandle)) {
  380. throw ErrnoError.With("ENOTDIR", path, "mkdir");
  381. }
  382. await handle.getDirectoryHandle(basename(path), { create: true });
  383. }
  384. async readdir(path) {
  385. const handle = await this.getHandle(path);
  386. if (!(handle instanceof FileSystemDirectoryHandle)) {
  387. throw ErrnoError.With("ENOTDIR", path, "readdir");
  388. }
  389. const _keys = [];
  390. for await (const key of handle.keys()) {
  391. _keys.push(join(path, key));
  392. }
  393. return _keys;
  394. }
  395. async getHandle(path) {
  396. if (this._handles.has(path)) {
  397. return this._handles.get(path);
  398. }
  399. let walked = "/";
  400. for (const part of path.split("/").slice(1)) {
  401. const handle = this._handles.get(walked);
  402. if (!(handle instanceof FileSystemDirectoryHandle)) {
  403. throw ErrnoError.With("ENOTDIR", walked, "getHandle");
  404. }
  405. walked = join(walked, part);
  406. try {
  407. const dirHandle = await handle.getDirectoryHandle(part);
  408. this._handles.set(walked, dirHandle);
  409. } catch (_ex) {
  410. const ex = _ex;
  411. if (ex.name == "TypeMismatchError") {
  412. try {
  413. const fileHandle = await handle.getFileHandle(part);
  414. this._handles.set(walked, fileHandle);
  415. } catch (ex2) {
  416. convertException(ex2, walked, "getHandle");
  417. }
  418. }
  419. if (ex.name === "TypeError") {
  420. throw new ErrnoError(Errno.ENOENT, ex.message, walked, "getHandle");
  421. }
  422. convertException(ex, walked, "getHandle");
  423. }
  424. }
  425. return this._handles.get(path);
  426. }
  427. };
  428. __name(WebAccessFS, "WebAccessFS");
  429. var WebAccess = {
  430. name: "WebAccess",
  431. options: {
  432. handle: {
  433. type: "object",
  434. required: true,
  435. description: "The directory handle to use for the root"
  436. }
  437. },
  438. isAvailable() {
  439. return typeof FileSystemHandle == "function";
  440. },
  441. create(options) {
  442. return new WebAccessFS(options);
  443. }
  444. };
  445. // node_modules/@zenfs/core/dist/error.js
  446. var Errno2;
  447. (function(Errno3) {
  448. Errno3[Errno3["EPERM"] = 1] = "EPERM";
  449. Errno3[Errno3["ENOENT"] = 2] = "ENOENT";
  450. Errno3[Errno3["EINTR"] = 4] = "EINTR";
  451. Errno3[Errno3["EIO"] = 5] = "EIO";
  452. Errno3[Errno3["ENXIO"] = 6] = "ENXIO";
  453. Errno3[Errno3["EBADF"] = 9] = "EBADF";
  454. Errno3[Errno3["EAGAIN"] = 11] = "EAGAIN";
  455. Errno3[Errno3["ENOMEM"] = 12] = "ENOMEM";
  456. Errno3[Errno3["EACCES"] = 13] = "EACCES";
  457. Errno3[Errno3["EFAULT"] = 14] = "EFAULT";
  458. Errno3[Errno3["ENOTBLK"] = 15] = "ENOTBLK";
  459. Errno3[Errno3["EBUSY"] = 16] = "EBUSY";
  460. Errno3[Errno3["EEXIST"] = 17] = "EEXIST";
  461. Errno3[Errno3["EXDEV"] = 18] = "EXDEV";
  462. Errno3[Errno3["ENODEV"] = 19] = "ENODEV";
  463. Errno3[Errno3["ENOTDIR"] = 20] = "ENOTDIR";
  464. Errno3[Errno3["EISDIR"] = 21] = "EISDIR";
  465. Errno3[Errno3["EINVAL"] = 22] = "EINVAL";
  466. Errno3[Errno3["ENFILE"] = 23] = "ENFILE";
  467. Errno3[Errno3["EMFILE"] = 24] = "EMFILE";
  468. Errno3[Errno3["ETXTBSY"] = 26] = "ETXTBSY";
  469. Errno3[Errno3["EFBIG"] = 27] = "EFBIG";
  470. Errno3[Errno3["ENOSPC"] = 28] = "ENOSPC";
  471. Errno3[Errno3["ESPIPE"] = 29] = "ESPIPE";
  472. Errno3[Errno3["EROFS"] = 30] = "EROFS";
  473. Errno3[Errno3["EMLINK"] = 31] = "EMLINK";
  474. Errno3[Errno3["EPIPE"] = 32] = "EPIPE";
  475. Errno3[Errno3["EDOM"] = 33] = "EDOM";
  476. Errno3[Errno3["ERANGE"] = 34] = "ERANGE";
  477. Errno3[Errno3["EDEADLK"] = 35] = "EDEADLK";
  478. Errno3[Errno3["ENAMETOOLONG"] = 36] = "ENAMETOOLONG";
  479. Errno3[Errno3["ENOLCK"] = 37] = "ENOLCK";
  480. Errno3[Errno3["ENOSYS"] = 38] = "ENOSYS";
  481. Errno3[Errno3["ENOTEMPTY"] = 39] = "ENOTEMPTY";
  482. Errno3[Errno3["ELOOP"] = 40] = "ELOOP";
  483. Errno3[Errno3["ENOMSG"] = 42] = "ENOMSG";
  484. Errno3[Errno3["EBADE"] = 52] = "EBADE";
  485. Errno3[Errno3["EBADR"] = 53] = "EBADR";
  486. Errno3[Errno3["EXFULL"] = 54] = "EXFULL";
  487. Errno3[Errno3["ENOANO"] = 55] = "ENOANO";
  488. Errno3[Errno3["EBADRQC"] = 56] = "EBADRQC";
  489. Errno3[Errno3["ENOSTR"] = 60] = "ENOSTR";
  490. Errno3[Errno3["ENODATA"] = 61] = "ENODATA";
  491. Errno3[Errno3["ETIME"] = 62] = "ETIME";
  492. Errno3[Errno3["ENOSR"] = 63] = "ENOSR";
  493. Errno3[Errno3["ENONET"] = 64] = "ENONET";
  494. Errno3[Errno3["EREMOTE"] = 66] = "EREMOTE";
  495. Errno3[Errno3["ENOLINK"] = 67] = "ENOLINK";
  496. Errno3[Errno3["ECOMM"] = 70] = "ECOMM";
  497. Errno3[Errno3["EPROTO"] = 71] = "EPROTO";
  498. Errno3[Errno3["EBADMSG"] = 74] = "EBADMSG";
  499. Errno3[Errno3["EOVERFLOW"] = 75] = "EOVERFLOW";
  500. Errno3[Errno3["EBADFD"] = 77] = "EBADFD";
  501. Errno3[Errno3["ESTRPIPE"] = 86] = "ESTRPIPE";
  502. Errno3[Errno3["ENOTSOCK"] = 88] = "ENOTSOCK";
  503. Errno3[Errno3["EDESTADDRREQ"] = 89] = "EDESTADDRREQ";
  504. Errno3[Errno3["EMSGSIZE"] = 90] = "EMSGSIZE";
  505. Errno3[Errno3["EPROTOTYPE"] = 91] = "EPROTOTYPE";
  506. Errno3[Errno3["ENOPROTOOPT"] = 92] = "ENOPROTOOPT";
  507. Errno3[Errno3["EPROTONOSUPPORT"] = 93] = "EPROTONOSUPPORT";
  508. Errno3[Errno3["ESOCKTNOSUPPORT"] = 94] = "ESOCKTNOSUPPORT";
  509. Errno3[Errno3["ENOTSUP"] = 95] = "ENOTSUP";
  510. Errno3[Errno3["ENETDOWN"] = 100] = "ENETDOWN";
  511. Errno3[Errno3["ENETUNREACH"] = 101] = "ENETUNREACH";
  512. Errno3[Errno3["ENETRESET"] = 102] = "ENETRESET";
  513. Errno3[Errno3["ETIMEDOUT"] = 110] = "ETIMEDOUT";
  514. Errno3[Errno3["ECONNREFUSED"] = 111] = "ECONNREFUSED";
  515. Errno3[Errno3["EHOSTDOWN"] = 112] = "EHOSTDOWN";
  516. Errno3[Errno3["EHOSTUNREACH"] = 113] = "EHOSTUNREACH";
  517. Errno3[Errno3["EALREADY"] = 114] = "EALREADY";
  518. Errno3[Errno3["EINPROGRESS"] = 115] = "EINPROGRESS";
  519. Errno3[Errno3["ESTALE"] = 116] = "ESTALE";
  520. Errno3[Errno3["EREMOTEIO"] = 121] = "EREMOTEIO";
  521. Errno3[Errno3["EDQUOT"] = 122] = "EDQUOT";
  522. })(Errno2 || (Errno2 = {}));
  523. var errorMessages2 = {
  524. [Errno2.EPERM]: "Operation not permitted",
  525. [Errno2.ENOENT]: "No such file or directory",
  526. [Errno2.EINTR]: "Interrupted system call",
  527. [Errno2.EIO]: "Input/output error",
  528. [Errno2.ENXIO]: "No such device or address",
  529. [Errno2.EBADF]: "Bad file descriptor",
  530. [Errno2.EAGAIN]: "Resource temporarily unavailable",
  531. [Errno2.ENOMEM]: "Cannot allocate memory",
  532. [Errno2.EACCES]: "Permission denied",
  533. [Errno2.EFAULT]: "Bad address",
  534. [Errno2.ENOTBLK]: "Block device required",
  535. [Errno2.EBUSY]: "Resource busy or locked",
  536. [Errno2.EEXIST]: "File exists",
  537. [Errno2.EXDEV]: "Invalid cross-device link",
  538. [Errno2.ENODEV]: "No such device",
  539. [Errno2.ENOTDIR]: "File is not a directory",
  540. [Errno2.EISDIR]: "File is a directory",
  541. [Errno2.EINVAL]: "Invalid argument",
  542. [Errno2.ENFILE]: "Too many open files in system",
  543. [Errno2.EMFILE]: "Too many open files",
  544. [Errno2.ETXTBSY]: "Text file busy",
  545. [Errno2.EFBIG]: "File is too big",
  546. [Errno2.ENOSPC]: "No space left on disk",
  547. [Errno2.ESPIPE]: "Illegal seek",
  548. [Errno2.EROFS]: "Cannot modify a read-only file system",
  549. [Errno2.EMLINK]: "Too many links",
  550. [Errno2.EPIPE]: "Broken pipe",
  551. [Errno2.EDOM]: "Numerical argument out of domain",
  552. [Errno2.ERANGE]: "Numerical result out of range",
  553. [Errno2.EDEADLK]: "Resource deadlock would occur",
  554. [Errno2.ENAMETOOLONG]: "File name too long",
  555. [Errno2.ENOLCK]: "No locks available",
  556. [Errno2.ENOSYS]: "Function not implemented",
  557. [Errno2.ENOTEMPTY]: "Directory is not empty",
  558. [Errno2.ELOOP]: "Too many levels of symbolic links",
  559. [Errno2.ENOMSG]: "No message of desired type",
  560. [Errno2.EBADE]: "Invalid exchange",
  561. [Errno2.EBADR]: "Invalid request descriptor",
  562. [Errno2.EXFULL]: "Exchange full",
  563. [Errno2.ENOANO]: "No anode",
  564. [Errno2.EBADRQC]: "Invalid request code",
  565. [Errno2.ENOSTR]: "Device not a stream",
  566. [Errno2.ENODATA]: "No data available",
  567. [Errno2.ETIME]: "Timer expired",
  568. [Errno2.ENOSR]: "Out of streams resources",
  569. [Errno2.ENONET]: "Machine is not on the network",
  570. [Errno2.EREMOTE]: "Object is remote",
  571. [Errno2.ENOLINK]: "Link has been severed",
  572. [Errno2.ECOMM]: "Communication error on send",
  573. [Errno2.EPROTO]: "Protocol error",
  574. [Errno2.EBADMSG]: "Bad message",
  575. [Errno2.EOVERFLOW]: "Value too large for defined data type",
  576. [Errno2.EBADFD]: "File descriptor in bad state",
  577. [Errno2.ESTRPIPE]: "Streams pipe error",
  578. [Errno2.ENOTSOCK]: "Socket operation on non-socket",
  579. [Errno2.EDESTADDRREQ]: "Destination address required",
  580. [Errno2.EMSGSIZE]: "Message too long",
  581. [Errno2.EPROTOTYPE]: "Protocol wrong type for socket",
  582. [Errno2.ENOPROTOOPT]: "Protocol not available",
  583. [Errno2.EPROTONOSUPPORT]: "Protocol not supported",
  584. [Errno2.ESOCKTNOSUPPORT]: "Socket type not supported",
  585. [Errno2.ENOTSUP]: "Operation is not supported",
  586. [Errno2.ENETDOWN]: "Network is down",
  587. [Errno2.ENETUNREACH]: "Network is unreachable",
  588. [Errno2.ENETRESET]: "Network dropped connection on reset",
  589. [Errno2.ETIMEDOUT]: "Connection timed out",
  590. [Errno2.ECONNREFUSED]: "Connection refused",
  591. [Errno2.EHOSTDOWN]: "Host is down",
  592. [Errno2.EHOSTUNREACH]: "No route to host",
  593. [Errno2.EALREADY]: "Operation already in progress",
  594. [Errno2.EINPROGRESS]: "Operation now in progress",
  595. [Errno2.ESTALE]: "Stale file handle",
  596. [Errno2.EREMOTEIO]: "Remote I/O error",
  597. [Errno2.EDQUOT]: "Disk quota exceeded"
  598. };
  599. var ErrnoError2 = class extends Error {
  600. static fromJSON(json) {
  601. const err = new ErrnoError2(json.errno, json.message, json.path, json.syscall);
  602. err.code = json.code;
  603. err.stack = json.stack;
  604. return err;
  605. }
  606. static With(code, path, syscall) {
  607. return new ErrnoError2(Errno2[code], errorMessages2[Errno2[code]], path, syscall);
  608. }
  609. /**
  610. * Represents a ZenFS error. Passed back to applications after a failed
  611. * call to the ZenFS API.
  612. *
  613. * Error codes mirror those returned by regular Unix file operations, which is
  614. * what Node returns.
  615. * @param type The type of the error.
  616. * @param message A descriptive error message.
  617. */
  618. constructor(errno, message = errorMessages2[errno], path, syscall = "") {
  619. super(message);
  620. this.errno = errno;
  621. this.path = path;
  622. this.syscall = syscall;
  623. this.code = Errno2[errno];
  624. this.message = `${this.code}: ${message}${this.path ? `, '${this.path}'` : ""}`;
  625. }
  626. /**
  627. * @return A friendly error message.
  628. */
  629. toString() {
  630. return this.message;
  631. }
  632. toJSON() {
  633. return {
  634. errno: this.errno,
  635. code: this.code,
  636. path: this.path,
  637. stack: this.stack,
  638. message: this.message,
  639. syscall: this.syscall
  640. };
  641. }
  642. /**
  643. * The size of the API error in buffer-form in bytes.
  644. */
  645. bufferSize() {
  646. return 4 + JSON.stringify(this.toJSON()).length;
  647. }
  648. };
  649. __name(ErrnoError2, "ErrnoError");
  650. // node_modules/@zenfs/core/dist/backends/store/store.js
  651. var Transaction2 = class {
  652. constructor() {
  653. this.aborted = false;
  654. }
  655. async [Symbol.asyncDispose]() {
  656. if (this.aborted) {
  657. return;
  658. }
  659. await this.commit();
  660. }
  661. [Symbol.dispose]() {
  662. if (this.aborted) {
  663. return;
  664. }
  665. this.commitSync();
  666. }
  667. };
  668. __name(Transaction2, "Transaction");
  669. var AsyncTransaction2 = class extends Transaction2 {
  670. getSync(ino) {
  671. throw ErrnoError2.With("ENOSYS", void 0, "AsyncTransaction.getSync");
  672. }
  673. setSync(ino, data) {
  674. throw ErrnoError2.With("ENOSYS", void 0, "AsyncTransaction.setSync");
  675. }
  676. removeSync(ino) {
  677. throw ErrnoError2.With("ENOSYS", void 0, "AsyncTransaction.removeSync");
  678. }
  679. commitSync() {
  680. throw ErrnoError2.With("ENOSYS", void 0, "AsyncTransaction.commitSync");
  681. }
  682. abortSync() {
  683. throw ErrnoError2.With("ENOSYS", void 0, "AsyncTransaction.abortSync");
  684. }
  685. };
  686. __name(AsyncTransaction2, "AsyncTransaction");
  687. // src/IndexedDB.ts
  688. function wrap(request) {
  689. return new Promise((resolve, reject) => {
  690. request.onsuccess = () => resolve(request.result);
  691. request.onerror = (e) => {
  692. e.preventDefault();
  693. reject(convertException(request.error));
  694. };
  695. });
  696. }
  697. __name(wrap, "wrap");
  698. var IndexedDBTransaction = class extends AsyncTransaction2 {
  699. constructor(tx, store) {
  700. super();
  701. this.tx = tx;
  702. this.store = store;
  703. }
  704. get(key) {
  705. return wrap(this.store.get(key.toString()));
  706. }
  707. async set(key, data) {
  708. await wrap(this.store.put(data, key.toString()));
  709. }
  710. remove(key) {
  711. return wrap(this.store.delete(key.toString()));
  712. }
  713. async commit() {
  714. this.tx.commit();
  715. }
  716. async abort() {
  717. try {
  718. this.tx.abort();
  719. } catch (e) {
  720. throw convertException(e);
  721. }
  722. }
  723. };
  724. __name(IndexedDBTransaction, "IndexedDBTransaction");
  725. async function createDB(name, indexedDB = globalThis.indexedDB) {
  726. const req = indexedDB.open(name);
  727. req.onupgradeneeded = () => {
  728. const db = req.result;
  729. if (db.objectStoreNames.contains(name)) {
  730. db.deleteObjectStore(name);
  731. }
  732. db.createObjectStore(name);
  733. };
  734. const result = await wrap(req);
  735. return result;
  736. }
  737. __name(createDB, "createDB");
  738. var IndexedDBStore = class {
  739. constructor(db) {
  740. this.db = db;
  741. }
  742. sync() {
  743. throw new Error("Method not implemented.");
  744. }
  745. get name() {
  746. return IndexedDB.name + ":" + this.db.name;
  747. }
  748. clear() {
  749. return wrap(this.db.transaction(this.db.name, "readwrite").objectStore(this.db.name).clear());
  750. }
  751. clearSync() {
  752. throw ErrnoError.With("ENOSYS", void 0, "IndexedDBStore.clearSync");
  753. }
  754. transaction() {
  755. const tx = this.db.transaction(this.db.name, "readwrite");
  756. return new IndexedDBTransaction(tx, tx.objectStore(this.db.name));
  757. }
  758. };
  759. __name(IndexedDBStore, "IndexedDBStore");
  760. var IndexedDB = {
  761. name: "IndexedDB",
  762. options: {
  763. storeName: {
  764. type: "string",
  765. required: false,
  766. description: "The name of this file system. You can have multiple IndexedDB file systems operating at once, but each must have a different name."
  767. },
  768. idbFactory: {
  769. type: "object",
  770. required: false,
  771. description: "The IDBFactory to use. Defaults to globalThis.indexedDB."
  772. }
  773. },
  774. async isAvailable(idbFactory = globalThis.indexedDB) {
  775. try {
  776. if (!(idbFactory instanceof IDBFactory)) {
  777. return false;
  778. }
  779. const req = idbFactory.open("__zenfs_test");
  780. await wrap(req);
  781. idbFactory.deleteDatabase("__zenfs_test");
  782. return true;
  783. } catch (e) {
  784. idbFactory.deleteDatabase("__zenfs_test");
  785. return false;
  786. }
  787. },
  788. async create(options) {
  789. const db = await createDB(options.storeName || "zenfs", options.idbFactory);
  790. const store = new IndexedDBStore(db);
  791. const fs2 = new StoreFS(store);
  792. return fs2;
  793. }
  794. };
  795. // src/Storage.ts
  796. var WebStorageStore = class {
  797. constructor(_storage) {
  798. this._storage = _storage;
  799. }
  800. get name() {
  801. return WebStorage.name;
  802. }
  803. clear() {
  804. this._storage.clear();
  805. }
  806. clearSync() {
  807. this._storage.clear();
  808. }
  809. async sync() {
  810. }
  811. transaction() {
  812. return new SimpleTransaction(this);
  813. }
  814. get(key) {
  815. const data = this._storage.getItem(key.toString());
  816. if (typeof data != "string") {
  817. return;
  818. }
  819. return encode(data);
  820. }
  821. set(key, data) {
  822. try {
  823. this._storage.setItem(key.toString(), decode(data));
  824. } catch (e) {
  825. throw new ErrnoError(Errno.ENOSPC, "Storage is full.");
  826. }
  827. }
  828. delete(key) {
  829. try {
  830. this._storage.removeItem(key.toString());
  831. } catch (e) {
  832. throw new ErrnoError(Errno.EIO, "Unable to delete key " + key + ": " + e);
  833. }
  834. }
  835. };
  836. __name(WebStorageStore, "WebStorageStore");
  837. var WebStorage = {
  838. name: "WebStorage",
  839. options: {
  840. storage: {
  841. type: "object",
  842. required: false,
  843. description: "The Storage to use. Defaults to globalThis.localStorage."
  844. }
  845. },
  846. isAvailable(storage = globalThis.localStorage) {
  847. return storage instanceof globalThis.Storage;
  848. },
  849. create({ storage = globalThis.localStorage }) {
  850. return new StoreFS(new WebStorageStore(storage));
  851. }
  852. };
  853. return __toCommonJS(src_exports);
  854. })();
  855. //# sourceMappingURL=browser.js.map