hfs 0.49.0-alpha1 → 0.49.0-alpha2

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.
package/src/vfs.js CHANGED
@@ -4,7 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  return (mod && mod.__esModule) ? mod : { "default": mod };
5
5
  };
6
6
  Object.defineProperty(exports, "__esModule", { value: true });
7
- exports.parentMaskApplier = exports.masksCouldGivePermission = exports.walkNode = exports.statusCodeForMissingPerm = exports.hasPermission = exports.nodeIsDirectory = exports.getNodeName = exports.saveVfs = exports.vfs = exports.urlToNode = exports.applyParentToChild = exports.isSameFilenameAs = exports.MIME_AUTO = exports.PERM_KEYS = exports.defaultPerms = exports.WHO_ANY_ACCOUNT = exports.WHO_NO_ONE = exports.WHO_ANYONE = void 0;
7
+ exports.parentMaskApplier = exports.masksCouldGivePermission = exports.walkNode = exports.statusCodeForMissingPerm = exports.hasPermission = exports.nodeIsDirectory = exports.getNodeName = exports.saveVfs = exports.vfs = exports.urlToNode = exports.applyParentToChild = exports.isSameFilenameAs = exports.permsFromParent = exports.MIME_AUTO = void 0;
8
8
  const promises_1 = __importDefault(require("fs/promises"));
9
9
  const path_1 = require("path");
10
10
  const misc_1 = require("./misc");
@@ -13,46 +13,38 @@ const config_1 = require("./config");
13
13
  const const_1 = require("./const");
14
14
  const events_1 = __importDefault(require("./events"));
15
15
  const perm_1 = require("./perm");
16
- exports.WHO_ANYONE = true;
17
- exports.WHO_NO_ONE = false;
18
- exports.WHO_ANY_ACCOUNT = '*';
19
- exports.defaultPerms = {
20
- can_see: 'can_read',
21
- can_read: exports.WHO_ANYONE,
22
- can_list: 'can_read',
23
- can_upload: exports.WHO_NO_ONE,
24
- can_delete: exports.WHO_NO_ONE,
25
- };
26
- exports.PERM_KEYS = (0, misc_1.typedKeys)(exports.defaultPerms);
27
16
  exports.MIME_AUTO = 'auto';
28
- function inheritFromParent(parent, child) {
29
- var _a, _b, _c;
30
- for (const k of (0, misc_1.typedKeys)(exports.defaultPerms)) {
17
+ function permsFromParent(parent, child) {
18
+ const ret = {};
19
+ for (const k of misc_1.PERM_KEYS) {
31
20
  let p = parent;
32
21
  let inheritedPerm;
33
22
  while (p) {
34
23
  inheritedPerm = p[k];
35
- // // in case of object without children, parent is skipped in favor of the parent's parent
36
- if (!isWhoObject(inheritedPerm))
24
+ // in case of object without children, parent is skipped in favor of the parent's parent
25
+ if (!(0, misc_1.isWhoObject)(inheritedPerm))
37
26
  break;
38
27
  inheritedPerm = inheritedPerm.children;
39
28
  if (inheritedPerm !== undefined)
40
29
  break;
41
30
  p = p.parent;
42
31
  }
43
- if (inheritedPerm !== undefined) // small optimization: don't expand the object
44
- (_a = child[k]) !== null && _a !== void 0 ? _a : (child[k] = inheritedPerm);
32
+ if (inheritedPerm !== undefined && child[k] === undefined) // small optimization: don't expand the object
33
+ ret[k] = inheritedPerm;
45
34
  }
35
+ return lodash_1.default.isEmpty(ret) ? undefined : ret;
36
+ }
37
+ exports.permsFromParent = permsFromParent;
38
+ function inheritFromParent(parent, child) {
39
+ var _a, _b;
40
+ Object.assign(child, permsFromParent(parent, child));
46
41
  if (typeof parent.mime === 'object' && typeof child.mime === 'object')
47
42
  lodash_1.default.defaults(child.mime, parent.mime);
48
43
  else
49
- (_b = child.mime) !== null && _b !== void 0 ? _b : (child.mime = parent.mime);
50
- (_c = child.accept) !== null && _c !== void 0 ? _c : (child.accept = parent.accept);
44
+ (_a = child.mime) !== null && _a !== void 0 ? _a : (child.mime = parent.mime);
45
+ (_b = child.accept) !== null && _b !== void 0 ? _b : (child.accept = parent.accept);
51
46
  return child;
52
47
  }
53
- function isWhoObject(v) {
54
- return v !== null && typeof v === 'object' && !Array.isArray(v);
55
- }
56
48
  function isSameFilenameAs(name) {
57
49
  const lc = name.toLowerCase();
58
50
  return (other) => lc === (typeof other === 'string' ? other : getNodeName(other)).toLowerCase();
@@ -183,25 +175,25 @@ function statusCodeForMissingPerm(node, perm, ctx, assign = true) {
183
175
  return const_1.HTTP_FORBIDDEN;
184
176
  // calculate value of permission resolving references to other permissions, avoiding infinite loop
185
177
  let who;
186
- let max = exports.PERM_KEYS.length;
178
+ let max = misc_1.PERM_KEYS.length;
187
179
  do {
188
180
  who = node[perm];
189
- if (isWhoObject(who))
181
+ if ((0, misc_1.isWhoObject)(who))
190
182
  who = who.this;
191
- who !== null && who !== void 0 ? who : (who = exports.defaultPerms[perm]);
192
- if (!max-- || typeof who !== 'string' || who === exports.WHO_ANY_ACCOUNT)
183
+ who !== null && who !== void 0 ? who : (who = misc_1.defaultPerms[perm]);
184
+ if (!max-- || typeof who !== 'string' || who === misc_1.WHO_ANY_ACCOUNT)
193
185
  break;
194
186
  perm = who;
195
187
  } while (1);
196
188
  if (Array.isArray(who)) {
197
189
  const arr = who; // shut up ts
198
190
  // check if I or any ancestor match `who`, but cache ancestors' usernames inside context state
199
- const some = (0, misc_1.getOrSet)(ctx.state, 'usernames', () => (0, perm_1.getCurrentUsernameExpanded)(ctx))
191
+ const some = (0, misc_1.getOrSet)(ctx.state, 'usernames', () => (0, perm_1.expandUsername)((0, perm_1.getCurrentUsername)(ctx)))
200
192
  .some((u) => arr.includes(u));
201
193
  return some ? 0 : const_1.HTTP_UNAUTHORIZED;
202
194
  }
203
195
  return typeof who === 'boolean' ? (who ? 0 : const_1.HTTP_FORBIDDEN)
204
- : who === exports.WHO_ANY_ACCOUNT ? (ctx.state.account ? 0 : const_1.HTTP_UNAUTHORIZED)
196
+ : who === misc_1.WHO_ANY_ACCOUNT ? (ctx.state.account ? 0 : const_1.HTTP_UNAUTHORIZED)
205
197
  : (0, misc_1.throw_)(Error('invalid permission: ' + JSON.stringify(who)));
206
198
  }
207
199
  }
@@ -235,7 +227,7 @@ async function* walkNode(parent, ctx, depth = 0, prefixPath = '', requiredPerm)
235
227
  parentsCache.set(name, item);
236
228
  inheritMasks(item, parent, nodeName);
237
229
  if (!ctx || hasPermission(item, 'can_list', ctx)) // check perm before recursion
238
- yield* walkNode(item, ctx, depth - 1, name + '/');
230
+ yield* walkNode(item, ctx, depth - 1, name + '/', requiredPerm);
239
231
  }
240
232
  if (!source)
241
233
  return;
@@ -338,7 +330,7 @@ events_1.default.on('accountRenamed', (from, to) => {
338
330
  ;
339
331
  (function renameInNode(n) {
340
332
  var _a;
341
- for (const k of exports.PERM_KEYS)
333
+ for (const k of misc_1.PERM_KEYS)
342
334
  renameInPerm(n[k]);
343
335
  if (n.masks)
344
336
  Object.values(n.masks).forEach(renameInNode);
package/src/watchLoad.js CHANGED
@@ -13,21 +13,14 @@ function watchLoad(path, parser, { failedOnFirstAttempt, immediateFirst } = {})
13
13
  let watcher;
14
14
  const debounced = (0, misc_1.debounceAsync)(load, 500, { maxWait: 1000 });
15
15
  let retry;
16
- let saving;
17
16
  let last;
18
17
  install(true);
19
- return {
20
- unwatch: uninstall,
21
- save: (data) => Promise.resolve(saving).catch(() => { }).then(() => {
22
- console.debug('writing', path);
23
- return saving = promises_1.default.writeFile(path, data, 'utf8').finally(() => // save but also keep track of the current operation
24
- saving = undefined);
25
- }) // clear
26
- };
18
+ const save = (0, misc_1.debounceAsync)((data) => promises_1.default.writeFile(path, data, 'utf8'));
19
+ return { unwatch, save };
27
20
  function install(first = false) {
28
21
  try {
29
22
  watcher = (0, fs_1.watch)(path, () => {
30
- if (!saving)
23
+ if (!save.isWorking())
31
24
  debounced().then();
32
25
  });
33
26
  debounced().catch(x => x);
@@ -40,7 +33,7 @@ function watchLoad(path, parser, { failedOnFirstAttempt, immediateFirst } = {})
40
33
  failedOnFirstAttempt === null || failedOnFirstAttempt === void 0 ? void 0 : failedOnFirstAttempt();
41
34
  }
42
35
  }
43
- function uninstall() {
36
+ function unwatch() {
44
37
  watcher === null || watcher === void 0 ? void 0 : watcher.close();
45
38
  clearTimeout(retry);
46
39
  watcher = undefined;
@@ -59,7 +52,7 @@ function watchLoad(path, parser, { failedOnFirstAttempt, immediateFirst } = {})
59
52
  return;
60
53
  last = text;
61
54
  console.debug('loaded', path);
62
- uninstall();
55
+ unwatch();
63
56
  install(); // reinstall, as the original file could have been renamed. We watch by the name.
64
57
  await parser(text);
65
58
  }
package/src/zip.js CHANGED
@@ -24,7 +24,7 @@ async function zipStreamFromFolder(node, ctx) {
24
24
  const name = (list === null || list === void 0 ? void 0 : list.length) === 1 ? decodeURIComponent((0, path_1.basename)(list[0])) : (0, vfs_1.getNodeName)(node);
25
25
  ctx.attachment(((0, misc_1.isWindowsDrive)(name) ? name[0] : (name || 'archive')) + '.zip');
26
26
  const filter = (0, misc_1.pattern2filter)(String(ctx.query.search || ''));
27
- const walker = !list ? (0, vfs_1.walkNode)(node, ctx, Infinity, '', 'can_read')
27
+ const walker = !list ? (0, vfs_1.walkNode)(node, ctx, Infinity, '', 'can_archive')
28
28
  : (async function* () {
29
29
  for await (const uri of list) {
30
30
  const subNode = await (0, vfs_1.urlToNode)(uri, ctx, node);
@@ -33,7 +33,7 @@ async function zipStreamFromFolder(node, ctx) {
33
33
  if (await (0, vfs_1.nodeIsDirectory)(subNode)) { // a directory needs to walked
34
34
  if ((0, vfs_1.hasPermission)(subNode, 'can_list', ctx)) {
35
35
  yield subNode; // it could be empty
36
- yield* (0, vfs_1.walkNode)(subNode, ctx, Infinity, decodeURI(uri) + '/', 'can_read');
36
+ yield* (0, vfs_1.walkNode)(subNode, ctx, Infinity, decodeURI(uri) + '/', 'can_archive');
37
37
  }
38
38
  continue;
39
39
  }
@@ -43,8 +43,8 @@ async function zipStreamFromFolder(node, ctx) {
43
43
  }
44
44
  })();
45
45
  const mappedWalker = (0, misc_1.filterMapGenerator)(walker, async (el) => {
46
- if (!(0, vfs_1.hasPermission)(el, 'can_read', ctx))
47
- return; // the fact you see it doesn't mean you can read it
46
+ if (!(0, vfs_1.hasPermission)(el, 'can_archive', ctx))
47
+ return; // the fact you see it doesn't mean you can get it
48
48
  const { source } = el;
49
49
  const name = (0, vfs_1.getNodeName)(el);
50
50
  if (ctx.req.aborted || !filter(name))