hfs 0.57.26 → 0.57.27-beta3

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.
Files changed (71) hide show
  1. package/admin/assets/{index-CHMhmNYS.js → index-CrWL6F86.js} +104 -103
  2. package/admin/assets/{sha512-Dsv49IAs.js → sha512-Dt-WJwnp.js} +1 -1
  3. package/admin/index.html +1 -1
  4. package/frontend/assets/{index-legacy-BI8icwFp.js → index-legacy-BdpconSd.js} +4 -4
  5. package/frontend/assets/{sha512-legacy-D82alHBq.js → sha512-legacy-Dbrn-acf.js} +1 -1
  6. package/frontend/index.html +1 -1
  7. package/package.json +4 -3
  8. package/src/QuickZipStream.js +11 -8
  9. package/src/SendList.js +10 -8
  10. package/src/ThrottledStream.js +8 -6
  11. package/src/acme.js +5 -7
  12. package/src/adminApis.js +6 -7
  13. package/src/api.accounts.js +4 -6
  14. package/src/api.auth.js +7 -11
  15. package/src/api.get_file_list.js +11 -16
  16. package/src/api.lang.js +1 -1
  17. package/src/api.monitor.js +2 -3
  18. package/src/api.net.js +1 -2
  19. package/src/api.plugins.js +8 -10
  20. package/src/api.vfs.js +14 -19
  21. package/src/apiMiddleware.js +4 -4
  22. package/src/auth.js +6 -9
  23. package/src/basicWeb.js +1 -2
  24. package/src/block.js +1 -1
  25. package/src/commands.js +9 -9
  26. package/src/comments.js +4 -5
  27. package/src/config.js +8 -9
  28. package/src/connections.js +11 -6
  29. package/src/consoleLog.js +6 -6
  30. package/src/const.js +4 -4
  31. package/src/cross.js +19 -21
  32. package/src/customHtml.js +3 -4
  33. package/src/ddns.js +4 -4
  34. package/src/debounceAsync.js +3 -3
  35. package/src/errorPages.js +2 -3
  36. package/src/events.js +7 -10
  37. package/src/fileAttr.js +6 -6
  38. package/src/first.js +7 -5
  39. package/src/frontEndApis.js +5 -5
  40. package/src/geo.js +3 -5
  41. package/src/github.js +14 -17
  42. package/src/i18n.js +3 -4
  43. package/src/icons.js +1 -1
  44. package/src/index.js +2 -3
  45. package/src/lang.js +5 -6
  46. package/src/listen.js +25 -28
  47. package/src/log.js +18 -17
  48. package/src/middlewares.js +12 -15
  49. package/src/misc.js +9 -11
  50. package/src/nat.js +14 -16
  51. package/src/outboundProxy.js +7 -7
  52. package/src/perm.js +7 -12
  53. package/src/persistence.js +1 -1
  54. package/src/plugins.js +64 -74
  55. package/src/roots.js +1 -2
  56. package/src/selfCheck.js +5 -4
  57. package/src/serveFile.js +6 -7
  58. package/src/serveGuiAndSharedFiles.js +7 -9
  59. package/src/serveGuiFiles.js +13 -20
  60. package/src/stat.js +40 -0
  61. package/src/statWorker.js +11 -0
  62. package/src/throttler.js +5 -7
  63. package/src/update.js +7 -7
  64. package/src/upload.js +11 -13
  65. package/src/util-files.js +42 -32
  66. package/src/util-http.js +22 -22
  67. package/src/util-os.js +2 -4
  68. package/src/vfs.js +78 -68
  69. package/src/walkDir.js +9 -9
  70. package/src/watchLoad.js +4 -4
  71. package/src/zip.js +5 -6
@@ -20,6 +20,7 @@ const config_1 = require("./config");
20
20
  const lang_1 = require("./lang");
21
21
  const upload_1 = require("./upload");
22
22
  const icons_1 = require("./icons");
23
+ const middlewares_1 = require("./middlewares");
23
24
  const size1024 = (0, config_1.defineConfig)(misc_1.CFG.size_1024, false, x => misc_1.formatBytes.k = x ? 1024 : 1000); // we both configure formatBytes, and also provide a compiled version (number instead of boolean)
24
25
  const splitUploads = (0, config_1.defineConfig)(misc_1.CFG.split_uploads, 0);
25
26
  exports.logGui = (0, config_1.defineConfig)(misc_1.CFG.log_gui, false);
@@ -40,7 +41,7 @@ function serveStatic(uri) {
40
41
  return ctx.status = const_1.HTTP_METHOD_NOT_ALLOWED;
41
42
  const serveApp = shouldServeApp(ctx);
42
43
  const fullPath = (0, path_1.join)(__dirname, '..', folder, serveApp ? '/index.html' : ctx.path);
43
- const content = await (0, misc_1.parseFileContent)(fullPath, raw => serveApp || !raw.length ? raw : adjustBundlerLinks(ctx, uri, raw))
44
+ const content = await (0, misc_1.parseFile)(fullPath, raw => serveApp || !raw.length ? raw : adjustBundlerLinks(ctx, uri, raw))
44
45
  .catch(() => null);
45
46
  if (content === null)
46
47
  return ctx.status = const_1.HTTP_NOT_FOUND;
@@ -51,8 +52,7 @@ function serveStatic(uri) {
51
52
  };
52
53
  }
53
54
  function shouldServeApp(ctx) {
54
- var _a;
55
- return (_a = ctx.state).serveApp || (_a.serveApp = ctx.path.endsWith('/') && !ctx.headers.upgrade); // skip websockets
55
+ return ctx.state.serveApp ||= ctx.path.endsWith('/') && !ctx.headers.upgrade; // skip websockets
56
56
  }
57
57
  function adjustBundlerLinks(ctx, uri, data) {
58
58
  const ext = (0, path_1.extname)(ctx.path);
@@ -61,8 +61,8 @@ function adjustBundlerLinks(ctx, uri, data) {
61
61
  }
62
62
  const getFaviconTimestamp = (0, misc_1.debounceAsync)(async () => {
63
63
  const f = adminApis_1.favicon.get();
64
- return !f ? 0 : (0, misc_1.statWithTimeout)(f).then(x => (x === null || x === void 0 ? void 0 : x.mtimeMs) || 0, () => 0);
65
- }, { retain: 5000 });
64
+ return !f ? 0 : (0, misc_1.statWithTimeout)(f).then(x => x?.mtimeMs || 0, () => 0);
65
+ }, { retain: 5_000 });
66
66
  async function treatIndex(ctx, filesUri, body) {
67
67
  const session = await (0, api_auth_1.refresh_session)({}, ctx);
68
68
  ctx.set('etag', '');
@@ -72,13 +72,9 @@ async function treatIndex(ctx, filesUri, body) {
72
72
  const pub = ctx.state.revProxyPath + const_1.PLUGINS_PUB_URI;
73
73
  // expose plugins' configs that are declared with 'frontend' attribute
74
74
  const plugins = Object.fromEntries((0, misc_1.onlyTruthy)((0, plugins_1.mapPlugins)((pl, name) => {
75
- var _a, _b;
76
- let configs = (0, misc_1.newObj)((0, plugins_1.getPluginConfigFields)(name), (v, k, skip) => {
77
- var _a, _b, _c;
78
- return !v.frontend ? skip() :
79
- adjustValueByConfig((_b = (_a = plugins_1.pluginsConfig.get()) === null || _a === void 0 ? void 0 : _a[name]) === null || _b === void 0 ? void 0 : _b[k], (_c = pl.getData().config) === null || _c === void 0 ? void 0 : _c[k]);
80
- });
81
- configs = ((_b = (_a = (0, plugins_1.getPluginInfo)(name)).onFrontendConfig) === null || _b === void 0 ? void 0 : _b.call(_a, configs)) || configs;
75
+ let configs = (0, misc_1.newObj)((0, plugins_1.getPluginConfigFields)(name), (v, k, skip) => !v.frontend ? skip() :
76
+ adjustValueByConfig(plugins_1.pluginsConfig.get()?.[name]?.[k], pl.getData().config?.[k]));
77
+ configs = (0, plugins_1.getPluginInfo)(name).onFrontendConfig?.(configs) || configs;
82
78
  return !lodash_1.default.isEmpty(configs) && [name, configs];
83
79
  })));
84
80
  const timestamp = await getFaviconTimestamp();
@@ -98,8 +94,9 @@ async function treatIndex(ctx, filesUri, body) {
98
94
  SPECIAL_URI: const_1.SPECIAL_URI, PLUGINS_PUB_URI: const_1.PLUGINS_PUB_URI, FRONTEND_URI: const_1.FRONTEND_URI,
99
95
  session: session instanceof apiMiddleware_1.ApiError ? null : session,
100
96
  plugins,
101
- loadScripts: Object.fromEntries((0, plugins_1.mapPlugins)((p, id) => { var _a; return [id, (_a = p.frontend_js) === null || _a === void 0 ? void 0 : _a.map(f => f.includes('//') ? f : pub + id + '/' + f)]; })),
97
+ loadScripts: Object.fromEntries((0, plugins_1.mapPlugins)((p, id) => [id, p.frontend_js?.map(f => f.includes('//') ? f : pub + id + '/' + f)])),
102
98
  prefixUrl: ctx.state.revProxyPath,
99
+ proxyDetected: Boolean((0, middlewares_1.getProxyDetected)()),
103
100
  dontOverwriteUploading: upload_1.dontOverwriteUploading.get(),
104
101
  splitUploads: splitUploads.get(),
105
102
  kb: size1024.compiled(),
@@ -132,10 +129,7 @@ async function treatIndex(ctx, filesUri, body) {
132
129
  }
133
130
  ${isFrontend && (0, customHtml_1.getSection)('style')}
134
131
  </style>
135
- ${isFrontend && (0, plugins_1.mapPlugins)((plug, id) => {
136
- var _a;
137
- return (_a = plug.frontend_css) === null || _a === void 0 ? void 0 : _a.map(f => `<link rel='stylesheet' type='text/css' href='${f.includes('//') ? f : pub + id + '/' + f}' plugin=${JSON.stringify(id)}/>`);
138
- })
132
+ ${isFrontend && (0, plugins_1.mapPlugins)((plug, id) => plug.frontend_css?.map(f => `<link rel='stylesheet' type='text/css' href='${f.includes('//') ? f : pub + id + '/' + f}' plugin=${JSON.stringify(id)}/>`))
139
133
  .flat().filter(Boolean).join('\n')}
140
134
  `;
141
135
  if (isBody && isClose)
@@ -143,7 +137,7 @@ async function treatIndex(ctx, filesUri, body) {
143
137
  return all; // unchanged
144
138
  });
145
139
  function adjustValueByConfig(v, cfg) {
146
- v !== null && v !== void 0 ? v : (v = cfg.defaultValue);
140
+ v ??= cfg.defaultValue;
147
141
  const { type } = cfg;
148
142
  if (v && type === 'vfs_path') {
149
143
  v = (0, misc_1.enforceStarting)('/', v);
@@ -162,9 +156,8 @@ async function treatIndex(ctx, filesUri, body) {
162
156
  }
163
157
  }
164
158
  function serializeCss(v) {
165
- var _a;
166
159
  return typeof v === 'string' && /^#[0-9a-fA-F]{3,8}|rgba?\(.+\)$/.test(v) ? v // colors
167
- : (0, misc_1.isPrimitive)(v) ? (_a = JSON.stringify(v)) === null || _a === void 0 ? void 0 : _a.replace(/</g, '&lt;') : undefined;
160
+ : (0, misc_1.isPrimitive)(v) ? JSON.stringify(v)?.replace(/</g, '&lt;') : undefined;
168
161
  }
169
162
  function serveProxied(port, uri) {
170
163
  if (!port)
package/src/stat.js ADDED
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getStatWorker = getStatWorker;
4
+ const node_worker_threads_1 = require("node:worker_threads");
5
+ const node_fs_1 = require("node:fs");
6
+ const cross_1 = require("./cross");
7
+ // all stat requests for the same worker are serialized, potentially introducing extra latency
8
+ const pool = new Map();
9
+ function getStatWorker(key) {
10
+ const existing = pool.get(key);
11
+ if (existing)
12
+ return existing;
13
+ const worker = new node_worker_threads_1.Worker(__dirname + '/statWorker.js');
14
+ worker.unref();
15
+ const requests = new Map();
16
+ worker.on('message', (msg) => {
17
+ const k = msg.path;
18
+ requests.get(k)?.resolve(msg.error ? Promise.reject(new Error(msg.error))
19
+ : Object.setPrototypeOf(msg.result, node_fs_1.Stats.prototype));
20
+ requests.delete(k);
21
+ });
22
+ worker.on('error', (err) => {
23
+ for (const p of requests.values())
24
+ p.reject(err);
25
+ requests.clear();
26
+ worker.terminate().catch(() => { });
27
+ pool.delete(key);
28
+ });
29
+ pool.set(key, query);
30
+ return query;
31
+ function query(path) {
32
+ const was = requests.get(path);
33
+ if (was)
34
+ return was;
35
+ const ret = (0, cross_1.pendingPromise)();
36
+ requests.set(path, ret);
37
+ worker.postMessage(path);
38
+ return ret;
39
+ }
40
+ }
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ const { parentPort } = require('node:worker_threads');
3
+ const { statSync } = require('node:fs');
4
+ parentPort.on('message', async (path) => {
5
+ try { // we use statSync to not use (and not risk to saturate) libuv's thread pool
6
+ parentPort.postMessage({ path, result: { ...statSync(path) } });
7
+ }
8
+ catch (err) {
9
+ parentPort.postMessage({ path, error: err?.message || String(err) });
10
+ }
11
+ });
package/src/throttler.js CHANGED
@@ -26,8 +26,6 @@ maxKbpsPerIp.sub(v => {
26
26
  group.updateLimit(v);
27
27
  });
28
28
  const throttler = async (ctx, next) => {
29
- var _a;
30
- var _b;
31
29
  await next();
32
30
  let { body } = ctx;
33
31
  const downloadTotal = ctx.response.length;
@@ -36,11 +34,11 @@ const throttler = async (ctx, next) => {
36
34
  if (!body || !(body instanceof stream_1.Readable))
37
35
  return;
38
36
  // we wrap the stream also for unlimited connections to get speed and other features
39
- const noLimit = ((_a = ctx.state.account) === null || _a === void 0 ? void 0 : _a.ignore_limits) || (0, misc_1.isLocalHost)(ctx);
40
- const ipGroup = ip2group[_b = noLimit ? '' : ctx.ip] || (ip2group[_b] = {
37
+ const noLimit = ctx.state.account?.ignore_limits || (0, misc_1.isLocalHost)(ctx);
38
+ const ipGroup = ip2group[noLimit ? '' : ctx.ip] ||= {
41
39
  count: 0,
42
40
  group: new ThrottledStream_1.ThrottleGroup(noLimit ? Infinity : maxKbpsPerIp.get(), noLimit ? undefined : mainThrottleGroup),
43
- });
41
+ };
44
42
  const conn = (0, connections_1.getConnection)(ctx);
45
43
  if (!conn)
46
44
  throw 'assert throttler connection';
@@ -95,12 +93,12 @@ setInterval(() => {
95
93
  last = now;
96
94
  {
97
95
  const v = exports.totalSent.get();
98
- exports.totalOutSpeed = roundSpeed((v - (lastSent !== null && lastSent !== void 0 ? lastSent : v)) / past);
96
+ exports.totalOutSpeed = roundSpeed((v - (lastSent ?? v)) / past);
99
97
  lastSent = v;
100
98
  }
101
99
  {
102
100
  const v = exports.totalGot.get();
103
- exports.totalInSpeed = roundSpeed((v - (lastGot !== null && lastGot !== void 0 ? lastGot : v)) / past);
101
+ exports.totalInSpeed = roundSpeed((v - (lastGot ?? v)) / past);
104
102
  lastGot = v;
105
103
  }
106
104
  }, 1000);
package/src/update.js CHANGED
@@ -52,7 +52,7 @@ setInterval((0, misc_1.debounceAsync)(async () => {
52
52
  exports.autoCheckUpdateResult.set(u);
53
53
  lastCheckUpdate.set(Date.now());
54
54
  }
55
- catch (_a) { }
55
+ catch { }
56
56
  }), misc_1.HOUR);
57
57
  const ReleaseKeys = ['prerelease', 'tag_name', 'name', 'body', 'assets', 'isNewer', 'versionScalar'];
58
58
  const ReleaseAssetKeys = ['name', 'browser_download_url'];
@@ -75,7 +75,7 @@ async function getVersions(interrupt) {
75
75
  const rel = prepareRelease(x);
76
76
  if (rel.versionScalar === curV)
77
77
  continue;
78
- if (interrupt === null || interrupt === void 0 ? void 0 : interrupt(rel))
78
+ if (interrupt?.(rel))
79
79
  break; // avoid fetching too much data
80
80
  ret.push(rel);
81
81
  }
@@ -154,7 +154,7 @@ async function update(tagOrUrl = '') {
154
154
  const { mode } = await (0, misc_1.statWithTimeout)(bin);
155
155
  await (0, promises_1.chmod)(newBin, mode).catch(console.error);
156
156
  }
157
- await (0, promises_1.rename)(INSTALLED_FN, PREVIOUS_FN).catch(e => (e === null || e === void 0 ? void 0 : e.code) !== 'ENOENT' && console.warn(String(e)));
157
+ await (0, promises_1.rename)(INSTALLED_FN, PREVIOUS_FN).catch(e => e?.code !== 'ENOENT' && console.warn(String(e)));
158
158
  await (0, promises_1.rename)(LOCAL_UPDATE, INSTALLED_FN).catch(console.warn);
159
159
  (0, first_1.onProcessExit)(() => {
160
160
  const oldBinFile = 'old-' + binFile;
@@ -162,7 +162,7 @@ async function update(tagOrUrl = '') {
162
162
  try {
163
163
  (0, fs_1.unlinkSync)(oldBin);
164
164
  }
165
- catch (_a) { }
165
+ catch { }
166
166
  (0, fs_1.renameSync)(bin, oldBin);
167
167
  console.log("launching new version in background", newBinFile);
168
168
  launch(newBin, ['--updating', binFile, '--cwd .'], { sync: true }); // sync necessary to work on Mac by double-click
@@ -172,11 +172,11 @@ async function update(tagOrUrl = '') {
172
172
  }
173
173
  catch (e) {
174
174
  plugins_1.pluginsWatcher.unpause();
175
- throw (e === null || e === void 0 ? void 0 : e.message) || String(e);
175
+ throw e?.message || String(e);
176
176
  }
177
177
  }
178
178
  function launch(cmd, pars = [], options) {
179
- return ((options === null || options === void 0 ? void 0 : options.sync) ? child_process_1.spawnSync : child_process_1.spawn)((0, util_os_1.cmdEscape)(cmd), pars, { detached: true, shell: true, stdio: [0, 1, 2], ...options });
179
+ return (options?.sync ? child_process_1.spawnSync : child_process_1.spawn)((0, util_os_1.cmdEscape)(cmd), pars, { detached: true, shell: true, stdio: [0, 1, 2], ...options });
180
180
  }
181
181
  if (argv_1.argv.updating) { // we were launched with a temporary name, restore original name to avoid breaking references
182
182
  const bin = process.execPath;
@@ -195,7 +195,7 @@ if (argv_1.argv.updating) { // we were launched with a temporary name, restore o
195
195
  try {
196
196
  (0, fs_1.writeFileSync)(const_1.ARGS_FILE, JSON.stringify(['--updated', '--cwd', process.cwd().replaceAll(' ', '\\ ')]));
197
197
  }
198
- catch (_a) { }
198
+ catch { }
199
199
  void (0, open_1.default)(dest);
200
200
  }
201
201
  process.exit();
package/src/upload.js CHANGED
@@ -23,7 +23,7 @@ const events_1 = __importDefault(require("./events"));
23
23
  const promises_1 = require("fs/promises");
24
24
  const expiringCache_1 = require("./expiringCache");
25
25
  const first_1 = require("./first");
26
- exports.deleteUnfinishedUploadsAfter = (0, config_1.defineConfig)('delete_unfinished_uploads_after', 86400);
26
+ exports.deleteUnfinishedUploadsAfter = (0, config_1.defineConfig)('delete_unfinished_uploads_after', 86_400);
27
27
  exports.minAvailableMb = (0, config_1.defineConfig)('min_available_mb', 100);
28
28
  exports.dontOverwriteUploading = (0, config_1.defineConfig)('dont_overwrite_uploading', true);
29
29
  const waitingToBeDeleted = {};
@@ -35,7 +35,7 @@ const waitingToBeDeleted = {};
35
35
  try {
36
36
  fs_1.default.rmSync(path, { force: true });
37
37
  }
38
- catch (_a) { }
38
+ catch { }
39
39
  });
40
40
  const ATTR_UPLOADER = 'uploader';
41
41
  function getUploadMeta(path) {
@@ -50,11 +50,11 @@ function setUploadMeta(path, ctx) {
50
50
  function getUploadTempFor(fullPath) {
51
51
  return (0, path_1.join)((0, path_1.dirname)(fullPath), 'hfs$upload-' + (0, path_1.basename)(fullPath).slice(-200));
52
52
  }
53
- const diskSpaceCache = (0, expiringCache_1.expiringCache)(3000); // invalidate shortly
53
+ const diskSpaceCache = (0, expiringCache_1.expiringCache)(3_000); // invalidate shortly
54
54
  const uploadingFiles = new Map();
55
55
  // stay sync because we use this function with formidable()
56
56
  function uploadWriter(base, baseUri, path, ctx) {
57
- if ((0, misc_1.dirTraversal)(path))
57
+ if ((0, misc_1.hasDirTraversal)(path))
58
58
  return fail(const_1.HTTP_FOOL);
59
59
  if ((0, vfs_1.statusCodeForMissingPerm)(base, 'can_upload', ctx))
60
60
  return fail();
@@ -77,7 +77,7 @@ function uploadWriter(base, baseUri, path, ctx) {
77
77
  try {
78
78
  // refer to the source of the closest node that actually belongs to the vfs, so that cache is more effective
79
79
  let closestVfsNode = base; // if base=root, there's no parent and no original
80
- while ((closestVfsNode === null || closestVfsNode === void 0 ? void 0 : closestVfsNode.parent) && !closestVfsNode.original)
80
+ while (closestVfsNode?.parent && !closestVfsNode.original)
81
81
  closestVfsNode = closestVfsNode.parent; // if it's not original, it surely has a parent
82
82
  const dirToCheck = closestVfsNode.source;
83
83
  const res = diskSpaceCache.try(dirToCheck, () => (0, util_os_1.getDiskSpaceSync)(dirToCheck));
@@ -108,7 +108,7 @@ function uploadWriter(base, baseUri, path, ctx) {
108
108
  try {
109
109
  fs_1.default.accessSync(tempName, fs_1.default.constants.W_OK);
110
110
  }
111
- catch (_a) {
111
+ catch {
112
112
  try {
113
113
  fs_1.default.closeSync(fs_1.default.openSync(tempName, 'w'));
114
114
  fs_1.default.unlinkSync(tempName);
@@ -118,7 +118,7 @@ function uploadWriter(base, baseUri, path, ctx) {
118
118
  }
119
119
  }
120
120
  const stats = (0, misc_1.try_)(() => fs_1.default.statSync(tempName));
121
- const resumableSize = (stats === null || stats === void 0 ? void 0 : stats.size) || 0;
121
+ const resumableSize = stats?.size || 0;
122
122
  // checks for resume feature
123
123
  const par = String(ctx.query.resume || '');
124
124
  const resume = parseInt(par) || 0;
@@ -136,13 +136,13 @@ function uploadWriter(base, baseUri, path, ctx) {
136
136
  // append if resuming
137
137
  if (!resume && stats)
138
138
  fs_1.default.unlinkSync(tempName);
139
- const writeStream = (0, misc_1.createStreamLimiter)(contentLength !== null && contentLength !== void 0 ? contentLength : Infinity);
139
+ const writeStream = (0, misc_1.createStreamLimiter)(contentLength ?? Infinity);
140
140
  const fullSize = stillToWrite + resume;
141
141
  ctx.state.uploadDestinationPath = tempName;
142
142
  // allow plugins to mess with the write-stream, because the read-stream can be complicated in case of multipart
143
143
  const obj = { ctx, writeStream, fullPath, tempName, resume, fullSize, uri: '' };
144
144
  const resEvent = events_1.default.emit('uploadStart', obj);
145
- if (resEvent === null || resEvent === void 0 ? void 0 : resEvent.isDefaultPrevented())
145
+ if (resEvent?.isDefaultPrevented())
146
146
  return;
147
147
  const fileStream = fs_1.default.createWriteStream(tempName, resume ? { flags: 'r+', start: resume } : undefined);
148
148
  writeStream.on('error', e => {
@@ -256,8 +256,7 @@ function uploadWriter(base, baseUri, path, ctx) {
256
256
  return false;
257
257
  }
258
258
  function delayedDelete(path, secs) {
259
- var _a;
260
- clearTimeout((_a = waitingToBeDeleted[path]) === null || _a === void 0 ? void 0 : _a.timeout);
259
+ clearTimeout(waitingToBeDeleted[path]?.timeout);
261
260
  return waitingToBeDeleted[path] = {
262
261
  mtime,
263
262
  expires: Date.now() + secs * 1000,
@@ -268,8 +267,7 @@ function uploadWriter(base, baseUri, path, ctx) {
268
267
  };
269
268
  }
270
269
  function cancelDeletion(path) {
271
- var _a;
272
- clearTimeout((_a = waitingToBeDeleted[path]) === null || _a === void 0 ? void 0 : _a.timeout);
270
+ clearTimeout(waitingToBeDeleted[path]?.timeout);
273
271
  delete waitingToBeDeleted[path];
274
272
  }
275
273
  function releaseFile() {
package/src/util-files.js CHANGED
@@ -7,18 +7,18 @@ Object.defineProperty(exports, "__esModule", { value: true });
7
7
  exports.parseFileCache = void 0;
8
8
  exports.statWithTimeout = statWithTimeout;
9
9
  exports.isDirectory = isDirectory;
10
- exports.readFileBusy = readFileBusy;
10
+ exports.readFileWithBusyRetry = readFileWithBusyRetry;
11
11
  exports.watchDir = watchDir;
12
- exports.dirTraversal = dirTraversal;
13
- exports.adjustStaticPathForGlob = adjustStaticPathForGlob;
12
+ exports.hasDirTraversal = hasDirTraversal;
13
+ exports.escapeGlobPath = escapeGlobPath;
14
14
  exports.unzip = unzip;
15
- exports.prepareFolder = prepareFolder;
15
+ exports.ensureParentFolder = ensureParentFolder;
16
16
  exports.createFileWithPath = createFileWithPath;
17
- exports.safeWriteStream = safeWriteStream;
17
+ exports.createSafeWriteStream = createSafeWriteStream;
18
18
  exports.isValidFileName = isValidFileName;
19
19
  exports.exists = exists;
20
+ exports.loadFileCached = loadFileCached;
20
21
  exports.parseFile = parseFile;
21
- exports.parseFileContent = parseFileContent;
22
22
  const promises_1 = require("fs/promises");
23
23
  const cross_1 = require("./cross");
24
24
  const config_1 = require("./config");
@@ -27,24 +27,34 @@ const path_1 = require("path");
27
27
  const fast_glob_1 = __importDefault(require("fast-glob"));
28
28
  const const_1 = require("./const");
29
29
  const events_1 = require("events");
30
+ const stat_1 = require("./stat");
30
31
  // @ts-ignore
31
32
  const unzip_stream_1 = __importDefault(require("unzip-stream"));
32
33
  const fileTimeout = (0, config_1.defineConfig)('file_timeout', 3, x => x * 1000);
33
- function statWithTimeout(path) {
34
- return (0, cross_1.haveTimeout)(fileTimeout.compiled(), (0, promises_1.stat)(path));
34
+ // a smart (and a bit arbitrary) way to decide if we need the stat-workers functionality. Without it, we may be a bit faster. We'll see with experience if we need a dedicated configuration.
35
+ const disableStatWorkers = Number(process.env.UV_THREADPOOL_SIZE) >= 10;
36
+ // some paths (mostly offline networked ones) can take 20+ seconds to fail
37
+ async function statWithTimeout(path) {
38
+ // with just 4 requests we saturate the default UV_THREADPOOL_SIZE, blocking even local fs operations, so we move all UNC requests to worker threads
39
+ const workerKey = !disableStatWorkers && const_1.IS_WINDOWS && getUncHost(path); // pool requests by server name
40
+ const op = workerKey ? (0, stat_1.getStatWorker)(workerKey)(path) : (0, promises_1.stat)(path);
41
+ return (0, cross_1.haveTimeout)(fileTimeout.compiled(), op);
42
+ }
43
+ function getUncHost(path) {
44
+ return /^\\\\([^\\]+)\\/.exec(path)?.[1];
35
45
  }
36
46
  async function isDirectory(path) {
37
47
  try {
38
48
  return (await statWithTimeout(path)).isDirectory();
39
49
  }
40
- catch (_a) { }
50
+ catch { }
41
51
  }
42
- async function readFileBusy(path) {
52
+ async function readFileWithBusyRetry(path) {
43
53
  return (0, promises_1.readFile)(path, 'utf8').catch(e => {
44
- if ((e === null || e === void 0 ? void 0 : e.code) !== 'EBUSY')
54
+ if (e?.code !== 'EBUSY')
45
55
  throw e;
46
56
  console.debug('busy');
47
- return (0, cross_1.wait)(100).then(() => readFileBusy(path));
57
+ return (0, cross_1.wait)(100).then(() => readFileWithBusyRetry(path));
48
58
  });
49
59
  }
50
60
  function watchDir(dir, cb, atStart = false) {
@@ -53,7 +63,7 @@ function watchDir(dir, cb, atStart = false) {
53
63
  try {
54
64
  watcher = (0, fs_1.watch)(dir, controlledCb);
55
65
  }
56
- catch (_a) {
66
+ catch {
57
67
  // failing watching the content of the dir, we try to monitor its parent, but filtering events only for our target dir
58
68
  const base = (0, path_1.basename)(dir);
59
69
  try {
@@ -64,7 +74,7 @@ function watchDir(dir, cb, atStart = false) {
64
74
  watcher.close(); // if we succeed, we give up the parent watching
65
75
  watcher = (0, fs_1.watch)(dir, controlledCb); // attempt at passing to a more specific watching
66
76
  }
67
- catch (_a) { }
77
+ catch { }
68
78
  controlledCb();
69
79
  });
70
80
  }
@@ -76,7 +86,7 @@ function watchDir(dir, cb, atStart = false) {
76
86
  controlledCb();
77
87
  return {
78
88
  working() { return Boolean(watcher); },
79
- stop() { watcher === null || watcher === void 0 ? void 0 : watcher.close(); },
89
+ stop() { watcher?.close(); },
80
90
  pause() { paused = true; },
81
91
  unpause() { paused = false; },
82
92
  };
@@ -85,29 +95,29 @@ function watchDir(dir, cb, atStart = false) {
85
95
  cb();
86
96
  }
87
97
  }
88
- function dirTraversal(s) {
98
+ function hasDirTraversal(s) {
89
99
  return s && /(^|[/\\])\.\.($|[/\\])/.test(s);
90
100
  }
91
101
  // apply this to paths that may contain \ as separator (not supported by fast-glob) and other special chars to be escaped (parenthesis)
92
- function adjustStaticPathForGlob(path) {
102
+ function escapeGlobPath(path) {
93
103
  return fast_glob_1.default.escapePath(path.replace(/\\/g, '/'));
94
104
  }
95
105
  async function unzip(stream, cb) {
96
- let pending = Promise.resolve();
106
+ let chain = Promise.resolve();
97
107
  return new Promise((resolve, reject) => stream.pipe(unzip_stream_1.default.Parse())
98
- .on('end', () => pending.then(resolve))
108
+ .on('end', () => chain.then(resolve))
99
109
  .on('error', reject)
100
- .on('entry', (entry) => pending = pending.then(async () => {
110
+ .on('entry', (entry) => chain = chain.then(async () => {
101
111
  const { path, type } = entry;
102
112
  const dest = await (0, cross_1.try_)(() => cb(path), e => console.warn(String(e)));
103
113
  if (!dest || type !== 'File')
104
114
  return entry.autodrain();
105
115
  console.debug('unzip', dest);
106
- const thisFile = entry.pipe(await safeWriteStream(dest));
116
+ const thisFile = entry.pipe(await createSafeWriteStream(dest));
107
117
  await (0, events_1.once)(thisFile, 'finish');
108
118
  })));
109
119
  }
110
- async function prepareFolder(path, dirnameIt = true) {
120
+ async function ensureParentFolder(path, dirnameIt = true) {
111
121
  if (dirnameIt)
112
122
  path = (0, path_1.dirname)(path);
113
123
  if ((0, cross_1.isWindowsDrive)(path))
@@ -116,7 +126,7 @@ async function prepareFolder(path, dirnameIt = true) {
116
126
  await (0, promises_1.mkdir)(path, { recursive: true });
117
127
  return true;
118
128
  }
119
- catch (_a) {
129
+ catch {
120
130
  return false;
121
131
  }
122
132
  }
@@ -126,13 +136,13 @@ function createFileWithPath(path, options) {
126
136
  try {
127
137
  (0, fs_1.mkdirSync)(folder, { recursive: true });
128
138
  }
129
- catch (_a) {
139
+ catch {
130
140
  return;
131
141
  }
132
142
  return (0, fs_1.createWriteStream)(path, options);
133
143
  }
134
- async function safeWriteStream(path, options) {
135
- await prepareFolder(path);
144
+ async function createSafeWriteStream(path, options) {
145
+ await ensureParentFolder(path);
136
146
  return new Promise((resolve, reject) => {
137
147
  const first = (0, fs_1.createWriteStream)(path, options)
138
148
  .on('open', () => resolve(first))
@@ -142,7 +152,7 @@ async function safeWriteStream(path, options) {
142
152
  if (typeof options === 'string')
143
153
  options = { encoding: options };
144
154
  else
145
- options || (options = {});
155
+ options ||= {};
146
156
  if (options.flags && options.flags !== 'w') // we only handle the 'w' case
147
157
  return reject(e);
148
158
  options.flags = 'r+';
@@ -153,22 +163,22 @@ async function safeWriteStream(path, options) {
153
163
  });
154
164
  }
155
165
  function isValidFileName(name) {
156
- return !(const_1.IS_WINDOWS ? /[/:"*?<>|\\]/ : /\//).test(name) && !dirTraversal(name);
166
+ return !(const_1.IS_WINDOWS ? /[/:"*?<>|\\]/ : /\//).test(name) && !hasDirTraversal(name);
157
167
  }
158
168
  function exists(path) {
159
169
  return (0, promises_1.access)(path).then(() => true, () => false);
160
170
  }
161
171
  // parse a file, caching unless timestamp has changed
162
172
  exports.parseFileCache = new Map();
163
- async function parseFile(path, parse) {
173
+ async function loadFileCached(path, loader) {
164
174
  const { mtime: ts } = await statWithTimeout(path);
165
175
  const cached = exports.parseFileCache.get(path);
166
176
  if (cached && Number(ts) === Number(cached.ts))
167
177
  return cached.parsed;
168
- const parsed = parse(path);
178
+ const parsed = loader(path);
169
179
  exports.parseFileCache.set(path, { ts, parsed });
170
180
  return parsed;
171
181
  }
172
- async function parseFileContent(path, parse) {
173
- return parseFile(path, () => (0, promises_1.readFile)(path).then(parse));
182
+ async function parseFile(path, parse) {
183
+ return loadFileCached(path, () => (0, promises_1.readFile)(path).then(parse));
174
184
  }
package/src/util-http.js CHANGED
@@ -60,22 +60,19 @@ async function httpWithBody(url, options) {
60
60
  });
61
61
  }
62
62
  function httpStream(url, { body, proxy, jar, noRedirect, httpThrow = true, ...options } = {}) {
63
- var _a;
64
63
  const controller = new AbortController();
65
- (_a = options.signal) !== null && _a !== void 0 ? _a : (options.signal = controller.signal);
64
+ options.signal ??= controller.signal;
66
65
  return Object.assign(new Promise(async (resolve, reject) => {
67
- var _a, _b, _c, _d;
68
- var _e, _f, _g;
69
- proxy !== null && proxy !== void 0 ? proxy : (proxy = httpStream.defaultProxy);
70
- (_a = options.headers) !== null && _a !== void 0 ? _a : (options.headers = {});
66
+ proxy ??= httpStream.defaultProxy;
67
+ options.headers ??= {};
71
68
  if (body) {
72
- options.method || (options.method = 'POST');
69
+ options.method ||= 'POST';
73
70
  if (lodash_1.default.isPlainObject(body)) {
74
- (_b = (_e = options.headers)['content-type']) !== null && _b !== void 0 ? _b : (_e['content-type'] = 'application/json');
71
+ options.headers['content-type'] ??= 'application/json';
75
72
  body = JSON.stringify(body);
76
73
  }
77
74
  if (!(body instanceof node_stream_1.Readable))
78
- (_c = (_f = options.headers)['content-length']) !== null && _c !== void 0 ? _c : (_f['content-length'] = Buffer.byteLength(body));
75
+ options.headers['content-length'] ??= Buffer.byteLength(body);
79
76
  }
80
77
  if (jar)
81
78
  options.headers.cookie = lodash_1.default.map(jar, (v, k) => `${k}=${v}; `).join('')
@@ -90,17 +87,15 @@ function httpStream(url, { body, proxy, jar, noRedirect, httpThrow = true, ...op
90
87
  }
91
88
  if (proxy) {
92
89
  options.path = url; // full url as path
93
- (_d = (_g = options.headers).host) !== null && _d !== void 0 ? _d : (_g.host = (0, node_url_1.parse)(url).host || undefined); // keep original host header
90
+ options.headers.host ??= (0, node_url_1.parse)(url).host || undefined; // keep original host header
94
91
  }
95
92
  // this needs the prefix "proxy-"
96
- const proxyAuth = (proxyParsed === null || proxyParsed === void 0 ? void 0 : proxyParsed.auth) ? { 'proxy-authorization': `Basic ${Buffer.from(proxyParsed.auth, 'utf8').toString('base64')}` } : undefined;
93
+ const proxyAuth = proxyParsed?.auth ? { 'proxy-authorization': `Basic ${Buffer.from(proxyParsed.auth, 'utf8').toString('base64')}` } : undefined;
97
94
  // https through proxy is better with CONNECT
98
95
  if (!proxy || parsed.protocol === 'http:' || !await connect())
99
96
  Object.assign(options.headers, proxyAuth);
100
97
  const proto = options.protocol === 'https:' ? node_https_1.default : node_http_1.default;
101
98
  const req = proto.request(options, res => {
102
- var _a;
103
- var _b;
104
99
  console.debug("http responded", res.statusCode, "to", url);
105
100
  if (jar)
106
101
  for (const entry of res.headers['set-cookie'] || []) {
@@ -117,18 +112,18 @@ function httpStream(url, { body, proxy, jar, noRedirect, httpThrow = true, ...op
117
112
  let r = res.headers.location;
118
113
  if (r && !noRedirect) {
119
114
  r = new URL(r, url).toString(); // rewrite in case r is just a path, and thus relative to current url
120
- const stack = ((_b = options)._stack || (_b._stack = []));
115
+ const stack = (options._stack ||= []);
121
116
  if (stack.length > 20 || stack.includes(r))
122
117
  return reject(new Error('endless http redirection'));
123
118
  stack.push(r);
124
119
  delete options.method; // redirections are always GET
125
- (_a = options.headers) === null || _a === void 0 ? true : delete _a['content-length'];
120
+ delete options.headers?.['content-length'];
126
121
  delete options.auth;
127
122
  return resolve(httpStream(r, options));
128
123
  }
129
124
  resolve(res);
130
125
  }).on('error', (e) => {
131
- if (proxy && (e === null || e === void 0 ? void 0 : e.code) === 'ECONNREFUSED')
126
+ if (proxy && e?.code === 'ECONNREFUSED')
132
127
  console.debug("cannot connect to proxy ", proxy);
133
128
  reject(req.res || e);
134
129
  });
@@ -138,19 +133,24 @@ function httpStream(url, { body, proxy, jar, noRedirect, httpThrow = true, ...op
138
133
  req.end(body);
139
134
  function connect() {
140
135
  return proxyParsed && new Promise(resolve => {
136
+ const path = `${parsed.hostname}:${parsed.port || 443}`;
141
137
  (proxyParsed.protocol === 'https:' ? node_https_1.default : node_http_1.default).request({
142
138
  ...proxyParsed,
139
+ auth: undefined, // void proxyParsed.auth
143
140
  method: 'CONNECT',
144
- path: `${parsed.hostname}:${parsed.port || 443}`,
145
- auth: undefined,
146
- headers: proxyAuth
147
- }).on('error', reject).on('connect', (res, socket) => {
141
+ path,
142
+ headers: { Host: path, ...proxyAuth }
143
+ }).on('connect', (res, socket) => {
148
144
  if (res.statusCode !== 200)
149
145
  return resolve(false);
150
- // a TLS for every request is inefficient. Consider optimizing in the future, especially for reading plugins from github, which makes tens of requests.
146
+ // we are creating a TLS for every request, very inefficient. Consider optimizing in the future, especially for reading plugins from github, which makes tens of requests.
151
147
  options.createConnection = () => tls.connect({ socket, servername: parsed.hostname || undefined });
152
148
  resolve(true);
153
- }).end();
149
+ }).on('response', res => {
150
+ console.debug("proxy CONNECT response", res.statusCode, res.statusMessage);
151
+ resolve(false);
152
+ }).on('error', reject)
153
+ .end();
154
154
  });
155
155
  }
156
156
  }), {