hfs 0.53.0 → 0.54.0-alpha1

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
@@ -34,6 +34,7 @@ const lodash_1 = __importDefault(require("lodash"));
34
34
  const const_1 = require("./const");
35
35
  const Const = __importStar(require("./const"));
36
36
  const misc_1 = require("./misc");
37
+ const misc = __importStar(require("./misc"));
37
38
  const config_1 = require("./config");
38
39
  const serveFile_1 = require("./serveFile");
39
40
  const events_1 = __importDefault(require("./events"));
@@ -46,6 +47,7 @@ const kvstorage_1 = require("@rejetto/kvstorage");
46
47
  const first_1 = require("./first");
47
48
  const frontEndApis_1 = require("./frontEndApis");
48
49
  const index_1 = require("./index");
50
+ const block_1 = require("./block");
49
51
  exports.PATH = 'plugins';
50
52
  exports.DISABLING_SUFFIX = '-disabled';
51
53
  exports.STORAGE_FOLDER = 'storage';
@@ -134,6 +136,8 @@ async function initPlugin(pl, morePassedToInit) {
134
136
  getHfsConfig: config_1.getConfig,
135
137
  customApiCall,
136
138
  notifyClient: frontEndApis_1.notifyClient,
139
+ addBlock: block_1.addBlock,
140
+ misc,
137
141
  ...morePassedToInit
138
142
  })));
139
143
  }
@@ -159,7 +163,7 @@ const pluginsMiddleware = async (ctx, next) => {
159
163
  lastStatus = ctx.status;
160
164
  lastBody = ctx.body;
161
165
  }
162
- if (res === true && !ctx.isStopped) { // legacy pre-0.53
166
+ if (res === true && !ctx.isStopped) { //legacy pre-0.53
163
167
  ctx.stop();
164
168
  warnOnce(`plugin ${id} is using deprecated API (return true on middleware) and may not work with future versions (check for an update to "${id}")`);
165
169
  }
@@ -311,7 +315,7 @@ function getAvailablePlugins() {
311
315
  return Object.values(availablePlugins);
312
316
  }
313
317
  exports.getAvailablePlugins = getAvailablePlugins;
314
- const rescanAsap = (0, misc_1.debounceAsync)(rescan, 1000);
318
+ const rescanAsap = (0, misc_1.debounceAsync)(rescan, { wait: 1000 });
315
319
  if (!(0, fs_1.existsSync)(exports.PATH))
316
320
  try {
317
321
  (0, fs_1.mkdirSync)(exports.PATH);
package/src/serveFile.js CHANGED
@@ -75,10 +75,8 @@ async function serveFile(ctx, source, mime, content) {
75
75
  if (!stats.isFile())
76
76
  return ctx.status = const_1.HTTP_METHOD_NOT_ALLOWED;
77
77
  ctx.set('Last-Modified', stats.mtime.toUTCString());
78
- ctx.fileSource = // legacy pre-0.51
79
- ctx.state.fileSource = source;
80
- ctx.fileStats = // legacy pre-0.51
81
- ctx.state.fileStats = stats;
78
+ ctx.state.fileSource = source;
79
+ ctx.state.fileStats = stats;
82
80
  ctx.status = const_1.HTTP_OK;
83
81
  if (ctx.fresh)
84
82
  return ctx.status = const_1.HTTP_NOT_MODIFIED;
@@ -87,7 +85,7 @@ async function serveFile(ctx, source, mime, content) {
87
85
  const { size } = stats;
88
86
  const range = applyRange(ctx, size);
89
87
  ctx.body = (0, fs_1.createReadStream)(source, range);
90
- if (ctx.vfsNode)
88
+ if (ctx.state.vfsNode)
91
89
  monitorAsDownload(ctx, size, range === null || range === void 0 ? void 0 : range.start);
92
90
  }
93
91
  catch (e) {
@@ -101,7 +99,6 @@ function monitorAsDownload(ctx, size, offset) {
101
99
  const conn = (0, connections_1.getConnection)(ctx);
102
100
  ctx.body.on('end', () => (0, connections_1.updateConnection)(conn, {}, { opProgress: 1 }));
103
101
  (0, connections_1.updateConnection)(conn, {}, {
104
- op: 'download',
105
102
  opProgress: 0,
106
103
  opTotal: size,
107
104
  opOffset: size && offset && (offset / size),
@@ -102,6 +102,8 @@ const serveGuiAndSharedFiles = async (ctx, next) => {
102
102
  const { get } = ctx.query;
103
103
  if (node.default && path.endsWith('/') && !get) // final/ needed on browser to make resource urls correctly with html pages
104
104
  node = (_a = await (0, vfs_1.urlToNode)(node.default, ctx, node)) !== null && _a !== void 0 ? _a : node;
105
+ if (get === 'icon')
106
+ return (0, serveFile_1.serveFile)(ctx, node.icon || '|'); // pipe to cause not-found
105
107
  if (!await (0, vfs_1.nodeIsDirectory)(node))
106
108
  return node.url ? ctx.redirect(node.url)
107
109
  : !node.source ? (0, errorPages_1.sendErrorPage)(ctx, cross_const_1.HTTP_METHOD_NOT_ALLOWED) // !dir && !source is not supported at this moment
@@ -85,7 +85,7 @@ function adjustBundlerLinks(ctx, uri, data) {
85
85
  const getFaviconTimestamp = (0, misc_1.debounceAsync)(async () => {
86
86
  const f = adminApis_1.favicon.get();
87
87
  return !f ? 0 : promises_1.default.stat(f).then(x => (x === null || x === void 0 ? void 0 : x.mtimeMs) || 0, () => 0);
88
- }, 0, { retain: 5000 });
88
+ }, { retain: 5000 });
89
89
  async function treatIndex(ctx, filesUri, body) {
90
90
  const session = await (0, api_auth_1.refresh_session)({}, ctx);
91
91
  ctx.set('etag', '');
@@ -133,7 +133,7 @@ async function treatIndex(ctx, filesUri, body) {
133
133
  ...(0, misc_1.newObj)(misc_1.FRONTEND_OPTIONS, (v, k) => (0, config_1.getConfig)(k)),
134
134
  lang
135
135
  }, null, 4).replace(/<(\/script)/g, '<"+"$1') /*avoid breaking our script container*/}
136
- document.documentElement.setAttribute('ver', '${const_1.VERSION.split('-')[0] /*for style selectors*/}')
136
+ document.documentElement.setAttribute('ver', HFS.VERSION.split('-')[0])
137
137
  </script>
138
138
  `;
139
139
  if (isBody && isOpen)
package/src/update.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.update = exports.updateSupported = exports.localUpdateAvailable = exports.getUpdates = void 0;
7
+ exports.update = exports.updateSupported = exports.localUpdateAvailable = exports.getUpdates = exports.autoCheckUpdateResult = void 0;
8
8
  const github_1 = require("./github");
9
9
  const const_1 = require("./const");
10
10
  const path_1 = require("path");
@@ -17,15 +17,45 @@ const open_1 = __importDefault(require("open"));
17
17
  const config_1 = require("./config");
18
18
  const util_os_1 = require("./util-os");
19
19
  const first_1 = require("./first");
20
+ const persistence_1 = require("./persistence");
21
+ const lodash_1 = __importDefault(require("lodash"));
20
22
  const updateToBeta = (0, config_1.defineConfig)('update_to_beta', false);
23
+ const autoCheckUpdate = (0, config_1.defineConfig)('auto_check_update', true);
24
+ const lastCheckUpdate = persistence_1.storedMap.singleSync('lastCheckUpdate', 0);
25
+ const AUTO_CHECK_EVERY = misc_1.DAY;
26
+ exports.autoCheckUpdateResult = persistence_1.storedMap.singleSync('autoCheckUpdateResult', undefined);
27
+ exports.autoCheckUpdateResult.ready().then(() => {
28
+ exports.autoCheckUpdateResult.set(v => {
29
+ if (!v)
30
+ return; // refresh isNewer, as currentVersion may have changed
31
+ v.isNewer = config_1.currentVersion.olderThan(v.tag_name);
32
+ return v;
33
+ });
34
+ });
35
+ setInterval((0, misc_1.debounceAsync)(async () => {
36
+ if (!autoCheckUpdate.get())
37
+ return;
38
+ if (Date.now() < lastCheckUpdate.get() + AUTO_CHECK_EVERY)
39
+ return;
40
+ console.log("checking for updates");
41
+ const u = (await getUpdates(true))[0];
42
+ if (u)
43
+ console.log("new version available", u.name);
44
+ exports.autoCheckUpdateResult.set(u);
45
+ lastCheckUpdate.set(Date.now());
46
+ }), misc_1.MINUTE / 30);
47
+ const ReleaseKeys = ['prerelease', 'tag_name', 'name', 'body', 'assets', 'isNewer'];
48
+ const ReleaseAssetKeys = ['name', 'browser_download_url'];
21
49
  async function getUpdates(strict = false) {
50
+ (0, github_1.getProjectInfo)(); // check for alerts
22
51
  const stable = await (0, github_1.getRepoInfo)(const_1.HFS_REPO + '/releases/latest');
23
52
  const verStable = ver(stable);
24
53
  const ret = await getBetas();
25
54
  stable.isNewer = config_1.currentVersion.olderThan(stable.tag_name);
26
55
  if (stable.isNewer || const_1.RUNNING_BETA)
27
56
  ret.push(stable);
28
- return ret.filter(x => !strict || x.isNewer);
57
+ // prune a bit, as it will be serialized, but it has a lot of unused data
58
+ return ret.filter(x => !strict || x.isNewer).map(x => Object.assign(lodash_1.default.pick(x, ReleaseKeys), { assets: x.assets.map(a => lodash_1.default.pick(a, ReleaseAssetKeys)) }));
29
59
  function ver(x) {
30
60
  return (0, config_1.versionToScalar)(x.name);
31
61
  }
@@ -41,10 +71,10 @@ async function getUpdates(strict = false) {
41
71
  break;
42
72
  const curV = config_1.currentVersion.getScalar();
43
73
  for (const x of res) {
44
- if (!x.prerelease)
45
- continue; // prerelease are all the end
74
+ if (!x.prerelease || x.name.endsWith('-ignore'))
75
+ continue;
46
76
  const v = ver(x);
47
- if (v <= verStable) // prerelease-s are locally ordered, so as soon as we reach verStable we are done
77
+ if (v < verStable) // we don't consider betas before stable
48
78
  return ret;
49
79
  if (v === curV)
50
80
  continue; // skip current
@@ -115,7 +145,7 @@ async function update(tagOrUrl = '') {
115
145
  catch (_a) { }
116
146
  (0, fs_1.renameSync)(bin, oldBin);
117
147
  console.log("launching new version in background", newBinFile);
118
- launch(newBin, ['--updating', binFile], { sync: true }); // sync necessary to work on mac by double-click
148
+ launch(newBin, ['--updating', binFile], { sync: true }); // sync necessary to work on Mac by double-click
119
149
  });
120
150
  console.log("quitting");
121
151
  setTimeout(() => process.exit()); // give time to return (and caller to complete, eg: rest api to reply)
package/src/upload.js CHANGED
@@ -182,7 +182,7 @@ function uploadWriter(base, path, ctx) {
182
182
  let lastGot = 0;
183
183
  let lastGotTime = 0;
184
184
  const opTotal = reqSize + resume;
185
- Object.assign(ctx.state, { op: 'upload', opTotal, opOffset: resume / opTotal, opProgress: 0 });
185
+ Object.assign(ctx.state, { opTotal, opOffset: resume / opTotal, opProgress: 0 });
186
186
  const conn = (0, connections_1.updateConnectionForCtx)(ctx);
187
187
  if (!conn)
188
188
  return;
@@ -202,8 +202,7 @@ function uploadWriter(base, path, ctx) {
202
202
  throw e;
203
203
  }
204
204
  async function overwriteAnyway() {
205
- if (ctx.query.overwrite === undefined // legacy pre-0.52
206
- && ctx.query.existing !== 'overwrite')
205
+ if (ctx.query.existing !== 'overwrite')
207
206
  return;
208
207
  const n = await (0, vfs_1.getNodeByName)(path, base);
209
208
  return n && (0, vfs_1.hasPermission)(n, 'can_delete', ctx);
package/src/util-files.js CHANGED
@@ -15,6 +15,7 @@ const util_os_1 = require("./util-os");
15
15
  const stream_1 = require("stream");
16
16
  // @ts-ignore
17
17
  const unzip_stream_1 = __importDefault(require("unzip-stream"));
18
+ const dirStream_1 = require("./dirStream");
18
19
  async function isDirectory(path) {
19
20
  try {
20
21
  return (await (0, promises_1.stat)(path)).isDirectory();
@@ -80,25 +81,15 @@ exports.adjustStaticPathForGlob = adjustStaticPathForGlob;
80
81
  async function* dirStream(path, { depth = 0, onlyFiles = false, onlyFolders = false } = {}) {
81
82
  if (!await isDirectory(path))
82
83
  throw Error('ENOTDIR');
83
- const dirStream = fast_glob_1.default.stream(depth ? '**/*' : '*', {
84
- cwd: path,
85
- dot: true,
86
- deep: depth + 1,
87
- onlyFiles,
88
- onlyDirectories: onlyFolders,
89
- suppressErrors: true,
90
- objectMode: true,
91
- unique: false,
92
- });
93
84
  const skip = await getItemsToSkip(path);
94
- for await (const entry of dirStream) {
95
- let { path, dirent } = entry;
85
+ for await (const entry of (0, dirStream_1.createDirStream)(path, depth)) {
86
+ const dirent = entry;
96
87
  const isDir = dirent.isDirectory();
97
- if (!isDir && !dirent.isFile())
88
+ if (isDir ? onlyFiles : (onlyFolders || !dirent.isFile()))
89
+ continue;
90
+ if (skip === null || skip === void 0 ? void 0 : skip.includes(entry.path))
98
91
  continue;
99
- path = String(path);
100
- if (!(skip === null || skip === void 0 ? void 0 : skip.includes(path)))
101
- yield [path, isDir];
92
+ yield dirent;
102
93
  }
103
94
  async function getItemsToSkip(path) {
104
95
  if (!const_1.IS_WINDOWS)
package/src/vfs.js CHANGED
@@ -218,9 +218,27 @@ function statusCodeForMissingPerm(node, perm, ctx, assign = true) {
218
218
  }
219
219
  }
220
220
  exports.statusCodeForMissingPerm = statusCodeForMissingPerm;
221
+ 0 && setTimeout(async () => {
222
+ let n = 0;
223
+ //const node = await urlToNode(/*'/' || */'/users/rejetto/exp')
224
+ //if (!node) throw 'asd'
225
+ const dir = (0, misc_1.dirStream)('/', { depth: Infinity }); //walkNode(node)
226
+ console.time('FINISHED');
227
+ for await (const entry of dir) {
228
+ //console.log(entry.path)
229
+ if (n++ % 100000 === 0)
230
+ if (n >= 1000000)
231
+ break;
232
+ else
233
+ console.log(n.toLocaleString(), entry);
234
+ }
235
+ console.timeEnd('FINISHED');
236
+ console.log(n.toLocaleString());
237
+ //process.exit()
238
+ }, 1000);
221
239
  // it's responsibility of the caller to verify you have list permission on parent, as callers have different needs.
222
240
  async function* walkNode(parent, { ctx, depth = Infinity, prefixPath = '', requiredPerm, onlyFolders = false } = {}) {
223
- var _a;
241
+ var _a, _b;
224
242
  const { children, source } = parent;
225
243
  const took = prefixPath ? undefined : new Set();
226
244
  const maskApplier = parentMaskApplier(parent);
@@ -239,7 +257,7 @@ async function* walkNode(parent, { ctx, depth = Infinity, prefixPath = '', requi
239
257
  try {
240
258
  await promises_1.default.access(item.source);
241
259
  }
242
- catch (_b) {
260
+ catch (_c) {
243
261
  continue;
244
262
  }
245
263
  yield item;
@@ -259,10 +277,12 @@ async function* walkNode(parent, { ctx, depth = Infinity, prefixPath = '', requi
259
277
  try {
260
278
  let lastDir = prefixPath.slice(0, -1) || '.';
261
279
  parentsCache.set(lastDir, parent);
262
- // it's important to keep using dirStream in deep-mode, as it is manyfold faster (it parallelizes)
263
- for await (const [path, isFolder] of (0, misc_1.dirStream)(source, { depth, onlyFolders })) {
280
+ console.time('ASD');
281
+ for await (const entry of (0, misc_1.dirStream)(source, { depth, onlyFolders })) {
264
282
  if (ctx === null || ctx === void 0 ? void 0 : ctx.req.aborted)
265
283
  return;
284
+ const { path } = entry;
285
+ const isFolder = entry.isDirectory();
266
286
  const name = prefixPath + (((_a = parent.rename) === null || _a === void 0 ? void 0 : _a[path]) || path);
267
287
  if (took === null || took === void 0 ? void 0 : took.has(normalizeFilename(name)))
268
288
  continue;
@@ -277,15 +297,19 @@ async function* walkNode(parent, { ctx, depth = Infinity, prefixPath = '', requi
277
297
  source: (0, path_1.join)(source, path),
278
298
  rename: renameUnderPath(parent.rename, path),
279
299
  };
280
- if (isFolder) // store it even if we can't see it (masks), as its children can be produced by dirStream
300
+ if (isFolder) { // store it even if we can't see it (masks), as its children can be produced by dirStream
281
301
  parentsCache.set(name, item);
302
+ (_b = entry.closingBranch) === null || _b === void 0 ? void 0 : _b.then(p => parentsCache.delete(p));
303
+ }
282
304
  if (canSee(item))
283
305
  yield item;
284
306
  }
307
+ console.timeEnd('ASD');
285
308
  }
286
309
  catch (e) {
287
310
  console.debug('glob', source, e); // ENOTDIR, or lacking permissions
288
311
  }
312
+ parentsCache.clear(); // hoping for faster GC
289
313
  // item will be changed, so be sure to pass a temp node
290
314
  function canSee(item) {
291
315
  // we basename for depth>0 where we already have the rest of the path in the parent's url, and would be duplicated
package/src/watchLoad.js CHANGED
@@ -11,7 +11,7 @@ const misc_1 = require("./misc");
11
11
  function watchLoad(path, parser, { failedOnFirstAttempt, immediateFirst } = {}) {
12
12
  let doing = false;
13
13
  let watcher;
14
- const debounced = (0, misc_1.debounceAsync)(load, 500, { maxWait: 1000 });
14
+ const debounced = (0, misc_1.debounceAsync)(load, { wait: 500, maxWait: 1000 });
15
15
  let retry;
16
16
  let last;
17
17
  install(true);
package/src/zip.js CHANGED
@@ -17,12 +17,12 @@ const const_1 = require("./const");
17
17
  // expects 'node' to have had permissions checked by caller
18
18
  async function zipStreamFromFolder(node, ctx) {
19
19
  var _a;
20
- if ((0, vfs_1.statusCodeForMissingPerm)(node, 'can_archive', ctx))
20
+ const list = (_a = (0, misc_1.wantArray)(ctx.query.list)[0]) === null || _a === void 0 ? void 0 : _a.split('*'); // we are using * as separator because it cannot be used in a file name and doesn't need url encoding
21
+ if (!list && (0, vfs_1.statusCodeForMissingPerm)(node, 'can_archive', ctx))
21
22
  return;
22
23
  ctx.status = const_1.HTTP_OK;
23
24
  ctx.mime = 'zip';
24
25
  // ctx.query.list is undefined | string | string[]
25
- const list = (_a = (0, misc_1.wantArray)(ctx.query.list)[0]) === null || _a === void 0 ? void 0 : _a.split('*'); // we are using * as separator because it cannot be used in a file name and doesn't need url encoding
26
26
  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
27
  ctx.attachment(((0, misc_1.isWindowsDrive)(name) ? name[0] : (name || 'archive')) + '.zip');
28
28
  const filter = (0, misc_1.pattern2filter)(String(ctx.query.search || ''));
@@ -33,7 +33,7 @@ async function zipStreamFromFolder(node, ctx) {
33
33
  if (!subNode)
34
34
  continue;
35
35
  if (await (0, vfs_1.nodeIsDirectory)(subNode)) { // a directory needs to walked
36
- if ((0, vfs_1.hasPermission)(subNode, 'can_list', ctx)) {
36
+ if ((0, vfs_1.hasPermission)(subNode, 'can_list', ctx) && (0, vfs_1.hasPermission)(subNode, 'can_archive', ctx)) {
37
37
  yield subNode; // it could be empty
38
38
  yield* (0, vfs_1.walkNode)(subNode, { ctx, prefixPath: decodeURI(uri) + '/', requiredPerm: 'can_archive' });
39
39
  }