hfs 0.55.0-alpha1 → 0.55.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/plugins.js CHANGED
@@ -27,7 +27,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
27
27
  return (mod && mod.__esModule) ? mod : { "default": mod };
28
28
  };
29
29
  Object.defineProperty(exports, "__esModule", { value: true });
30
- exports.getMissingDependencies = exports.parsePluginSource = exports.rescan = exports.pluginsConfig = exports.enablePlugins = exports.pluginsWatcher = exports.getAvailablePlugins = exports.firstPlugin = exports.mapPlugins = exports.SERVER_CODE_ID = exports.Plugin = exports.pluginsMiddleware = exports.getPluginConfigFields = exports.findPluginByRepo = exports.getPluginInfo = exports.setPluginConfig = exports.startPlugin = exports.stopPlugin = exports.enablePlugin = exports.isPluginEnabled = exports.isPluginRunning = exports.STORAGE_FOLDER = exports.DISABLING_SUFFIX = exports.PATH = void 0;
30
+ exports.getMissingDependencies = exports.parsePluginSource = exports.rescan = exports.pluginsConfig = exports.suspendPlugins = exports.enablePlugins = exports.pluginsWatcher = exports.getAvailablePlugins = exports.firstPlugin = exports.mapPlugins = exports.SERVER_CODE_ID = exports.Plugin = exports.pluginsMiddleware = exports.getPluginConfigFields = exports.findPluginByRepo = exports.getPluginInfo = exports.setPluginConfig = exports.startPlugin = exports.stopPlugin = exports.enablePlugin = exports.isPluginEnabled = exports.isPluginRunning = exports.STORAGE_FOLDER = exports.DISABLING_SUFFIX = exports.PATH = void 0;
31
31
  const fast_glob_1 = __importDefault(require("fast-glob"));
32
32
  const watchLoad_1 = require("./watchLoad");
33
33
  const lodash_1 = __importDefault(require("lodash"));
@@ -57,8 +57,8 @@ function isPluginRunning(id) {
57
57
  return Boolean((_a = plugins.get(id)) === null || _a === void 0 ? void 0 : _a.started);
58
58
  }
59
59
  exports.isPluginRunning = isPluginRunning;
60
- function isPluginEnabled(id) {
61
- return exports.enablePlugins.get().includes(id);
60
+ function isPluginEnabled(id, considerSuspension = false) {
61
+ return (!considerSuspension || !exports.suspendPlugins.get()) && exports.enablePlugins.get().includes(id);
62
62
  }
63
63
  exports.isPluginEnabled = isPluginEnabled;
64
64
  function enablePlugin(id, state = true) {
@@ -356,6 +356,7 @@ if (!(0, fs_1.existsSync)(exports.PATH))
356
356
  exports.pluginsWatcher = (0, misc_1.watchDir)(exports.PATH, rescanAsap);
357
357
  exports.enablePlugins = (0, config_1.defineConfig)('enable_plugins', ['antibrute']);
358
358
  exports.enablePlugins.sub(rescanAsap);
359
+ exports.suspendPlugins = (0, config_1.defineConfig)(misc_1.CFG.suspend_plugins, false);
359
360
  exports.pluginsConfig = (0, config_1.defineConfig)('plugins_config', {});
360
361
  const pluginWatchers = new Map();
361
362
  async function rescan() {
@@ -369,40 +370,38 @@ async function rescan() {
369
370
  continue;
370
371
  const id = path.split('/').slice(-1)[0];
371
372
  met.push(id);
372
- const w = pluginWatchers.get(id);
373
- if (w)
374
- continue;
375
- console.debug('plugin watch', id);
376
- pluginWatchers.set(id, watchPlugin(id, (0, path_1.join)(path, 'plugin.js')));
373
+ if (!pluginWatchers.has(id))
374
+ pluginWatchers.set(id, watchPlugin(id, (0, path_1.join)(path, 'plugin.js')));
377
375
  }
378
376
  for (const [id, cancelWatcher] of pluginWatchers.entries())
379
377
  if (!met.includes(id)) {
380
378
  enablePlugin(id, false);
381
- console.debug('plugin unwatch', id);
382
379
  cancelWatcher();
383
380
  pluginWatchers.delete(id);
384
381
  }
385
382
  }
386
383
  exports.rescan = rescan;
387
384
  function watchPlugin(id, path) {
385
+ console.debug('plugin watch', id);
388
386
  const module = (0, path_1.resolve)(path);
389
387
  let starting;
390
- const unsub = exports.enablePlugins.sub(() => {
391
- if (!getPluginInfo(id))
392
- return; // not loaded yet
393
- const enabled = isPluginEnabled(id);
394
- if (enabled === isPluginRunning(id))
388
+ const unsub = exports.enablePlugins.sub(() => getPluginInfo(id) && considerStart()); // only after it has been loaded
389
+ const unsub2 = exports.suspendPlugins.sub(() => getPluginInfo(id) && considerStart());
390
+ function considerStart() {
391
+ const should = isPluginEnabled(id, true);
392
+ if (should === isPluginRunning(id))
395
393
  return;
396
- if (enabled)
394
+ if (should) {
397
395
  start();
398
- else
399
- stop();
400
- });
396
+ return true;
397
+ }
398
+ stop();
399
+ }
401
400
  const { unwatch } = (0, watchLoad_1.watchLoad)(module, async (source) => {
402
401
  const notRunning = availablePlugins[id];
403
402
  if (!source)
404
403
  return onUninstalled();
405
- if (isPluginEnabled(id))
404
+ if (isPluginEnabled(id, true))
406
405
  return start();
407
406
  const p = parsePluginSource(id, source);
408
407
  if ((0, misc_1.same)(notRunning, p))
@@ -411,7 +410,9 @@ function watchPlugin(id, path) {
411
410
  events_1.default.emit(notRunning ? 'pluginUpdated' : 'pluginInstalled', p);
412
411
  });
413
412
  return () => {
413
+ console.debug('plugin unwatch', id);
414
414
  unsub();
415
+ unsub2();
415
416
  unwatch();
416
417
  return onUninstalled();
417
418
  };
@@ -43,18 +43,19 @@ const serveGuiAndSharedFiles = async (ctx, next) => {
43
43
  if (ctx.method === 'PUT') { // curl -T file url/
44
44
  const decPath = decodeURIComponent(path);
45
45
  let rest = (0, path_1.basename)(decPath);
46
- const folder = await (0, vfs_1.urlToNode)((0, path_1.dirname)(path), ctx, vfs_1.vfs, v => rest = v + '/' + rest);
46
+ const folderUri = (0, path_1.dirname)(path);
47
+ const folder = await (0, vfs_1.urlToNode)(folderUri, ctx, vfs_1.vfs, v => rest = v + '/' + rest);
47
48
  if (!folder)
48
49
  return (0, errorPages_1.sendErrorPage)(ctx, cross_const_1.HTTP_NOT_FOUND);
49
50
  ctx.state.uploadPath = decPath;
50
- const dest = (0, upload_1.uploadWriter)(folder, rest, ctx);
51
+ const dest = (0, upload_1.uploadWriter)(folder, folderUri, rest, ctx);
51
52
  if (dest) {
52
53
  ctx.req.pipe(dest).on('error', err => {
53
54
  ctx.status = cross_const_1.HTTP_SERVER_ERROR;
54
55
  ctx.body = err.message || String(err);
55
56
  });
56
- await dest.lockMiddleware; // we need to wait more than just the stream
57
- ctx.body = {};
57
+ const uri = await dest.lockMiddleware; // we need to wait more than just the stream
58
+ ctx.body = { uri };
58
59
  }
59
60
  return;
60
61
  }
@@ -66,7 +67,6 @@ const serveGuiAndSharedFiles = async (ctx, next) => {
66
67
  if (ctx.method === 'POST') { // curl -F upload=@file url/
67
68
  if (ctx.request.type !== 'multipart/form-data')
68
69
  return ctx.status = cross_const_1.HTTP_BAD_REQUEST;
69
- ctx.body = {};
70
70
  ctx.state.uploads = [];
71
71
  let locks = [];
72
72
  const form = (0, formidable_1.default)({
@@ -76,18 +76,20 @@ const serveGuiAndSharedFiles = async (ctx, next) => {
76
76
  const fn = f.originalFilename;
77
77
  ctx.state.uploadPath = decodeURI(ctx.path) + fn;
78
78
  ctx.state.uploads.push(fn);
79
- const ret = (0, upload_1.uploadWriter)(node, fn, ctx);
79
+ const ret = (0, upload_1.uploadWriter)(node, path, fn, ctx);
80
80
  if (!ret)
81
81
  return new stream_1.Writable({ write(data, enc, cb) { cb(); } }); // just discard data
82
82
  locks.push(ret.lockMiddleware);
83
83
  return ret;
84
84
  }
85
85
  });
86
- return new Promise(res => form.parse(ctx.req, err => {
86
+ const uris = await new Promise(res => form.parse(ctx.req, async (err) => {
87
87
  if (err)
88
88
  console.error(String(err));
89
89
  res(Promise.all(locks));
90
90
  }));
91
+ ctx.body = { uris };
92
+ return;
91
93
  }
92
94
  if (ctx.method === 'DELETE') {
93
95
  const res = await (0, misc_1.deleteNode)(ctx, node, ctx.path);
package/src/upload.js CHANGED
@@ -37,7 +37,7 @@ function setUploadMeta(path, ctx) {
37
37
  // stay sync because we use this function with formidable()
38
38
  const diskSpaceCache = {};
39
39
  const openFiles = new Set();
40
- function uploadWriter(base, path, ctx) {
40
+ function uploadWriter(base, baseUri, path, ctx) {
41
41
  let fullPath = '';
42
42
  if ((0, misc_1.dirTraversal)(path))
43
43
  return fail(const_1.HTTP_FOOL);
@@ -129,7 +129,7 @@ function uploadWriter(base, path, ctx) {
129
129
  cancelDeletion(tempName);
130
130
  ctx.state.uploadDestinationPath = tempName;
131
131
  // allow plugins to mess with the write-stream, because the read-stream can be complicated in case of multipart
132
- const obj = { ctx, writeStream };
132
+ const obj = { ctx, writeStream, uri: '' };
133
133
  const resEvent = events_1.default.emit('uploadStart', obj);
134
134
  if (resEvent === null || resEvent === void 0 ? void 0 : resEvent.isDefaultPrevented())
135
135
  return;
@@ -176,6 +176,7 @@ function uploadWriter(base, path, ctx) {
176
176
  void (0, comments_1.setCommentFor)(dest, String(ctx.query.comment));
177
177
  if (resumable && !resuming) // this happens if user decided to not resume and the new upload finished before delayedDelete
178
178
  (0, promises_1.rm)(resumable).catch(console.warn);
179
+ obj.uri = (0, misc_1.enforceFinal)('/', baseUri) + (0, misc_1.pathEncode)((0, path_1.basename)(dest));
179
180
  events_1.default.emit('uploadFinished', obj);
180
181
  if (resEvent)
181
182
  for (const cb of resEvent)
@@ -189,12 +190,10 @@ function uploadWriter(base, path, ctx) {
189
190
  }
190
191
  finally {
191
192
  releaseFile();
192
- lockMiddleware.resolve();
193
+ lockMiddleware.resolve(obj.uri);
193
194
  }
194
195
  });
195
- return Object.assign(obj.writeStream, {
196
- lockMiddleware
197
- });
196
+ return Object.assign(obj.writeStream, { lockMiddleware });
198
197
  function trackProgress() {
199
198
  let lastGot = 0;
200
199
  let lastGotTime = 0;
package/src/vfs.js CHANGED
@@ -54,7 +54,7 @@ function isSameFilenameAs(name) {
54
54
  }
55
55
  exports.isSameFilenameAs = isSameFilenameAs;
56
56
  function normalizeFilename(x) {
57
- return const_1.IS_WINDOWS || const_1.IS_MAC ? x.toLocaleLowerCase() : x;
57
+ return (const_1.IS_WINDOWS || const_1.IS_MAC ? x.toLocaleLowerCase() : x).normalize();
58
58
  }
59
59
  async function applyParentToChild(child, parent, name) {
60
60
  var _a, _b;
@@ -238,7 +238,7 @@ function statusCodeForMissingPerm(node, perm, ctx, assign = true) {
238
238
  }
239
239
  exports.statusCodeForMissingPerm = statusCodeForMissingPerm;
240
240
  // it's responsibility of the caller to verify you have list permission on parent, as callers have different needs.
241
- async function* walkNode(parent, { ctx, depth = Infinity, prefixPath = '', requiredPerm, onlyFolders = false } = {}) {
241
+ async function* walkNode(parent, { ctx, depth = Infinity, prefixPath = '', requiredPerm, onlyFolders = false, onlyFiles = false, } = {}) {
242
242
  var _a, _b;
243
243
  const { children, source } = parent;
244
244
  const took = prefixPath ? undefined : new Set();
@@ -246,7 +246,7 @@ async function* walkNode(parent, { ctx, depth = Infinity, prefixPath = '', requi
246
246
  const parentsCache = new Map(); // we use this only if depth > 0
247
247
  if (children)
248
248
  for (const child of children) {
249
- if (onlyFolders && !await nodeIsDirectory(child))
249
+ if (await nodeIsDirectory(child) ? onlyFiles : onlyFolders)
250
250
  continue;
251
251
  const nodeName = getNodeName(child);
252
252
  const name = prefixPath + nodeName;
@@ -275,11 +275,10 @@ async function* walkNode(parent, { ctx, depth = Infinity, prefixPath = '', requi
275
275
  && !hasPermission(parent, requiredPerm, ctx)
276
276
  && !masksCouldGivePermission(parent.masks, requiredPerm))
277
277
  return;
278
- let n = 0;
279
278
  try {
280
279
  let lastDir = prefixPath.slice(0, -1) || '.';
281
280
  parentsCache.set(lastDir, parent);
282
- for await (const entry of (0, misc_1.dirStream)(source, { depth, onlyFolders, hidden: showHiddenFiles.get() })) {
281
+ for await (const entry of (0, misc_1.dirStream)(source, { depth, onlyFolders, onlyFiles, hidden: showHiddenFiles.get() })) {
283
282
  if (ctx === null || ctx === void 0 ? void 0 : ctx.req.aborted)
284
283
  return;
285
284
  const { path } = entry;