hfs 0.56.0-alpha2 → 0.56.0-alpha3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,49 +3,32 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.DirentFromStats = exports.createDirStream = void 0;
6
+ exports.DirentFromStats = exports.walkDir = void 0;
7
7
  const makeQ_1 = require("./makeQ");
8
8
  const promises_1 = require("fs/promises");
9
9
  const const_1 = require("./const");
10
10
  const path_1 = require("path");
11
- const stream_1 = require("stream");
12
11
  const cross_1 = require("./cross");
13
12
  const node_fs_1 = require("node:fs");
14
13
  const fswin_1 = __importDefault(require("fswin"));
14
+ const util_files_1 = require("./util-files");
15
15
  const dirQ = (0, makeQ_1.makeQ)(3);
16
- function createDirStream(startPath, { depth = 0, hidden = true }) {
16
+ // cb returns void = just go on, null = stop, false = go on but don't recur (in case of depth)
17
+ function walkDir(path, { depth = 0, hidden = true }, cb) {
17
18
  let stopped = false;
18
- let started = false;
19
19
  const closingQ = [];
20
- const stream = new stream_1.Readable({
21
- objectMode: true,
22
- read() {
23
- if (started)
24
- return;
25
- started = true;
26
- dirQ.add(() => readDir('', depth)
27
- .then(res => {
28
- Promise.resolve(res === null || res === void 0 ? void 0 : res.branchDone).then(() => {
29
- stream.push(null);
30
- });
31
- }, e => {
32
- stream.emit('error', e);
33
- return stream.push(null);
34
- }));
35
- }
36
- });
37
- stream.on('close', () => stopped = true);
38
- return Object.assign(stream, {
39
- stop() {
40
- stopped = true;
41
- if (!dirQ.isWorking())
42
- stream.push(null);
43
- },
20
+ return new Promise(async (resolve, reject) => {
21
+ if (!await (0, util_files_1.isDirectory)(path))
22
+ throw Error('ENOTDIR');
23
+ dirQ.add(() => readDir('', depth)
24
+ .then(res => {
25
+ Promise.resolve(res === null || res === void 0 ? void 0 : res.branchDone).then(resolve);
26
+ }, reject));
44
27
  });
45
- async function readDir(path, depth) {
28
+ async function readDir(relativePath, depth) {
46
29
  if (stopped)
47
30
  return;
48
- const base = (0, path_1.join)(startPath, path);
31
+ const base = (0, path_1.join)(path, relativePath);
49
32
  const subDirsDone = [];
50
33
  let n = 0;
51
34
  let last;
@@ -63,7 +46,7 @@ function createDirStream(startPath, { depth = 0, hidden = true }) {
63
46
  break;
64
47
  if (!hidden && f.IS_HIDDEN)
65
48
  continue;
66
- work(Object.assign(Object.create(methods), {
49
+ await work(Object.assign(Object.create(methods), {
67
50
  isDir: f.IS_DIRECTORY,
68
51
  name: f.LONG_NAME,
69
52
  stats: { size: f.SIZE, birthtime: f.CREATION_TIME, mtime: f.LAST_WRITE_TIME }
@@ -84,38 +67,42 @@ function createDirStream(startPath, { depth = 0, hidden = true }) {
84
67
  const expanded = entry;
85
68
  if (stats)
86
69
  expanded.stats = stats;
87
- work(expanded);
70
+ await work(expanded);
88
71
  }
89
- function work(entry) {
90
- entry.path = (path && path + '/') + entry.name;
72
+ async function work(entry) {
73
+ entry.path = (relativePath && relativePath + '/') + entry.name;
91
74
  if (last && closingQ.length) // pending entries
92
75
  last.closingBranch = Promise.resolve(closingQ.shift());
93
76
  last = entry;
94
- if (depth > 0 && entry.isDirectory()) {
95
- const branchDone = (0, cross_1.pendingPromise)(); // per-job
96
- const job = () => readDir(entry.path, depth - 1) // recur
97
- .then(x => x, () => { }) // mute errors
98
- .then(res => {
99
- if (!(res === null || res === void 0 ? void 0 : res.n))
100
- closingQ.push(entry.path); // no children to tell i'm done
101
- Promise.resolve(res === null || res === void 0 ? void 0 : res.branchDone).then(() => branchDone.resolve());
102
- });
103
- dirQ.add(job); // this won't start until next tick
104
- subDirsDone.push(branchDone);
105
- }
106
- stream.push(entry);
77
+ const res = await cb(entry);
78
+ if (res === null)
79
+ return stopped = true;
80
+ if (res === false)
81
+ return;
107
82
  n++;
83
+ if (!depth || !entry.isDirectory())
84
+ return;
85
+ const branchDone = (0, cross_1.pendingPromise)(); // per-job
86
+ const job = () => readDir(entry.path, depth - 1) // recur
87
+ .then(x => x, () => { }) // mute errors
88
+ .then(res => {
89
+ if (!(res === null || res === void 0 ? void 0 : res.n))
90
+ closingQ.push(entry.path); // no children to tell i'm done
91
+ Promise.resolve(res === null || res === void 0 ? void 0 : res.branchDone).then(() => branchDone.resolve());
92
+ });
93
+ dirQ.add(job); // this won't start until next tick
94
+ subDirsDone.push(branchDone);
108
95
  }
109
96
  const branchDone = Promise.allSettled(subDirsDone).then(() => { });
110
97
  if (last) // using streams, we don't know when the entries are received, so we need to notify on last item
111
- last.closingBranch = branchDone.then(() => path);
98
+ last.closingBranch = branchDone.then(() => relativePath);
112
99
  else
113
- closingQ.push(path); // ok, we'll ask next one to carry this info
100
+ closingQ.push(relativePath); // ok, we'll ask next one to carry this info
114
101
  // don't return the promise directly, as this job ends here, but communicate to caller the promise for the whole branch
115
102
  return { branchDone, n };
116
103
  }
117
104
  }
118
- exports.createDirStream = createDirStream;
105
+ exports.walkDir = walkDir;
119
106
  const kStats = Symbol('stats');
120
107
  // Adapting an internal class in Node.js to mimic the behavior of `Dirent` when creating it manually from `Stats`.
121
108
  // https://github.com/nodejs/node/blob/a4cf6b204f0b160480153dc293ae748bf15225f9/lib/internal/fs/utils.js#L199C1-L213
package/src/zip.js CHANGED
@@ -14,6 +14,8 @@ const config_1 = require("./config");
14
14
  const path_1 = require("path");
15
15
  const serveFile_1 = require("./serveFile");
16
16
  const const_1 = require("./const");
17
+ const api_get_file_list_1 = require("./api.get_file_list");
18
+ const comments_1 = require("./comments");
17
19
  // expects 'node' to have had permissions checked by caller
18
20
  async function zipStreamFromFolder(node, ctx) {
19
21
  var _a;
@@ -25,7 +27,7 @@ async function zipStreamFromFolder(node, ctx) {
25
27
  // ctx.query.list is undefined | string | string[]
26
28
  const name = (list === null || list === void 0 ? void 0 : list.length) === 1 ? (0, misc_1.safeDecodeURIComponent)((0, path_1.basename)(list[0])) : (0, vfs_1.getNodeName)(node);
27
29
  (0, serveFile_1.forceDownload)(ctx, ((0, misc_1.isWindowsDrive)(name) ? name[0] : (name || 'archive')) + '.zip');
28
- const filter = (0, misc_1.pattern2filter)(String(ctx.query.search || ''));
30
+ const { filterName, filterComment } = (0, api_get_file_list_1.paramsToFilter)(ctx.query);
29
31
  const walker = !list ? (0, vfs_1.walkNode)(node, { ctx, requiredPerm: 'can_archive' })
30
32
  : (async function* () {
31
33
  for await (const uri of list) {
@@ -53,7 +55,8 @@ async function zipStreamFromFolder(node, ctx) {
53
55
  return; // the fact you see it doesn't mean you can get it
54
56
  const { source } = el;
55
57
  const name = (0, vfs_1.getNodeName)(el);
56
- if (!filter(name))
58
+ if (filterName && !filterName(name)
59
+ || filterComment && !filterComment(await (0, comments_1.getCommentFor)(source) || ''))
57
60
  return;
58
61
  try {
59
62
  if (el.isFolder)