async.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.fromSnapshot = exports.toSnapshot = void 0;
  4. const shared_1 = require("./shared");
  5. const toSnapshot = async ({ fs, path = '/', separator = '/' }) => {
  6. const stats = await fs.lstat(path);
  7. if (stats.isDirectory()) {
  8. const list = await fs.readdir(path);
  9. const entries = {};
  10. const dir = path.endsWith(separator) ? path : path + separator;
  11. const snapshots = await Promise.all(list.map(child => (0, exports.toSnapshot)({ fs, path: `${dir}${child}`, separator })));
  12. for (let i = 0; i < list.length; i++) {
  13. if (snapshots[i])
  14. entries['' + list[i]] = snapshots[i];
  15. }
  16. return [0 /* SnapshotNodeType.Folder */, {}, entries];
  17. }
  18. else if (stats.isFile()) {
  19. const buf = (await fs.readFile(path));
  20. const uint8 = new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
  21. return [1 /* SnapshotNodeType.File */, {}, uint8];
  22. }
  23. else if (stats.isSymbolicLink()) {
  24. return [
  25. 2 /* SnapshotNodeType.Symlink */,
  26. {
  27. target: (await fs.readlink(path, { encoding: 'utf8' })),
  28. },
  29. ];
  30. }
  31. return null;
  32. };
  33. exports.toSnapshot = toSnapshot;
  34. const fromSnapshot = async (snapshot, { fs, path = '/', separator = '/' }) => {
  35. if (!snapshot)
  36. return;
  37. switch (snapshot[0]) {
  38. case 0 /* SnapshotNodeType.Folder */: {
  39. if (!path.endsWith(separator))
  40. path = path + separator;
  41. const [, , entries] = snapshot;
  42. await fs.mkdir(path, { recursive: true });
  43. for (const [name, child] of Object.entries(entries)) {
  44. (0, shared_1.validateEntryName)(name);
  45. await (0, exports.fromSnapshot)(child, { fs, path: `${path}${name}`, separator });
  46. }
  47. break;
  48. }
  49. case 1 /* SnapshotNodeType.File */: {
  50. const [, , data] = snapshot;
  51. await fs.writeFile(path, data);
  52. break;
  53. }
  54. case 2 /* SnapshotNodeType.Symlink */: {
  55. const [, { target }] = snapshot;
  56. await fs.symlink(target, path);
  57. break;
  58. }
  59. }
  60. };
  61. exports.fromSnapshot = fromSnapshot;
  62. //# sourceMappingURL=async.js.map