hfs 0.38.2 → 0.40.0

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.
@@ -59,14 +59,14 @@ function serveStatic(uri) {
59
59
  const content = await (0, misc_1.getOrSet)(cache, ctx.path, async () => {
60
60
  const data = await promises_1.default.readFile(fullPath).catch(() => null);
61
61
  return serveApp || !data ? data
62
- : adjustBundlerLinks(ctx.path, uri, data);
62
+ : adjustBundlerLinks(ctx, uri, data);
63
63
  });
64
64
  if (content === null)
65
65
  return ctx.status = const_1.HTTP_NOT_FOUND;
66
66
  if (!serveApp)
67
- return (0, serveFile_1.serveFile)(fullPath, 'auto', content)(ctx, next);
67
+ return (0, serveFile_1.serveFile)(ctx, fullPath, 'auto', content);
68
68
  // we don't cache the index as it's small and may prevent plugins change to apply
69
- ctx.body = await treatIndex(ctx, String(content), uri);
69
+ ctx.body = await treatIndex(ctx, uri, String(content));
70
70
  ctx.type = 'html';
71
71
  ctx.set('Cache-Control', 'no-store, no-cache, must-revalidate');
72
72
  };
@@ -75,17 +75,18 @@ function shouldServeApp(ctx) {
75
75
  var _a;
76
76
  return (_a = ctx.state).serveApp || (_a.serveApp = ctx.path.endsWith('/'));
77
77
  }
78
- function adjustBundlerLinks(path, uri, data) {
79
- const ext = (0, path_1.extname)(path);
78
+ function adjustBundlerLinks(ctx, uri, data) {
79
+ const ext = (0, path_1.extname)(ctx.path);
80
80
  return ext && !ext.match(/\.(css|html|js|ts|scss)/) ? data
81
- : String(data).replace(/((?:import | from )['"])\//g, `$1${uri}`);
81
+ : String(data).replace(/((?:import | from )['"])\//g, `$1${ctx.state.revProxyPath}${uri}`);
82
82
  }
83
- async function treatIndex(ctx, body, filesUri) {
83
+ async function treatIndex(ctx, filesUri, body) {
84
84
  const session = await (0, api_auth_1.refresh_session)({}, ctx);
85
85
  ctx.set('etag', '');
86
86
  const isFrontend = filesUri === const_1.FRONTEND_URI;
87
- const css = (0, plugins_1.mapPlugins)((plug, k) => { var _a; return (_a = (isFrontend ? plug.frontend_css : null)) === null || _a === void 0 ? void 0 : _a.map(f => const_1.PLUGINS_PUB_URI + k + '/' + f); }).flat().filter(Boolean);
88
- const js = (0, plugins_1.mapPlugins)((plug, k) => { var _a; return (_a = (isFrontend ? plug.frontend_js : null)) === null || _a === void 0 ? void 0 : _a.map(f => const_1.PLUGINS_PUB_URI + k + '/' + f); }).flat().filter(Boolean);
87
+ const pub = ctx.state.revProxyPath + const_1.PLUGINS_PUB_URI;
88
+ const css = (0, plugins_1.mapPlugins)((plug, k) => { var _a; return (_a = (isFrontend ? plug.frontend_css : null)) === null || _a === void 0 ? void 0 : _a.map(f => pub + k + '/' + f); }).flat().filter(Boolean);
89
+ const js = (0, plugins_1.mapPlugins)((plug, k) => { var _a; return (_a = (isFrontend ? plug.frontend_js : null)) === null || _a === void 0 ? void 0 : _a.map(f => pub + k + '/' + f); }).flat().filter(Boolean);
89
90
  // expose plugins' configs that are declared with 'frontend' attribute
90
91
  const plugins = Object.fromEntries((0, misc_1.onlyTruthy)((0, plugins_1.mapPlugins)((pl, name) => {
91
92
  var _a, _b;
@@ -98,7 +99,7 @@ async function treatIndex(ctx, body, filesUri) {
98
99
  return !lodash_1.default.isEmpty(configs) && [name, configs];
99
100
  })));
100
101
  let ret = body
101
- .replace(/((?:src|href) *= *['"])\/?(?![a-z]+:\/\/)/g, '$1' + filesUri)
102
+ .replace(/((?:src|href) *= *['"])\/?(?![a-z]+:\/\/)/g, '$1' + ctx.state.revProxyPath + filesUri)
102
103
  .replace('</head>', () => `
103
104
  ${!isFrontend ? '' : `
104
105
  <title>${adminApis_1.title.get()}</title>
@@ -110,6 +111,7 @@ async function treatIndex(ctx, body, filesUri) {
110
111
  API_VERSION: const_1.API_VERSION,
111
112
  session: session instanceof apiMiddleware_1.ApiError ? null : session,
112
113
  plugins,
114
+ prefixUrl: ctx.state.revProxyPath,
113
115
  customHtml: lodash_1.default.omit(Object.fromEntries(customHtml_1.customHtmlState.sections), ['top', 'bottom']), // excluding sections we apply in this phase
114
116
  }, null, 4)}
115
117
  document.documentElement.setAttribute('ver', '${const_1.VERSION.split('-')[0] /*for style selectors*/}')
@@ -141,8 +143,8 @@ function serveProxied(port, uri) {
141
143
  proxy = lib.default('127.0.0.1:' + port, {
142
144
  proxyReqPathResolver: (ctx) => shouldServeApp(ctx) ? '/' : ctx.path,
143
145
  userResDecorator(res, data, ctx) {
144
- return shouldServeApp(ctx) ? treatIndex(ctx, String(data), uri)
145
- : adjustBundlerLinks(ctx.path, uri, data);
146
+ return shouldServeApp(ctx) ? treatIndex(ctx, uri, String(data))
147
+ : adjustBundlerLinks(ctx, uri, data);
146
148
  }
147
149
  }));
148
150
  return function () {
package/src/throttler.js CHANGED
@@ -65,6 +65,8 @@ const throttler = async (ctx, next) => {
65
65
  ctx.body = ctx.body.pipe(ts);
66
66
  if (bak)
67
67
  ctx.response.length = bak;
68
+ ts.once('end', () => // in case of compressed response, we offer calculation of real size
69
+ ctx.state.length = ts.getBytesSent());
68
70
  };
69
71
  exports.throttler = throttler;
70
72
  function roundSpeed(n) {
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.cantReadStatusCode = exports.walkNode = exports.hasPermission = exports.nodeIsDirectory = exports.getNodeName = exports.saveVfs = exports.vfs = exports.urlToNode = exports.MIME_AUTO = exports.defaultPerms = void 0;
7
+ exports.cantReadStatusCode = exports.walkNode = exports.hasPermission = exports.nodeIsDirectory = exports.getNodeName = exports.saveVfs = exports.vfs = exports.urlToNode = exports.isSameFilenameAs = exports.MIME_AUTO = exports.defaultPerms = void 0;
8
8
  const promises_1 = __importDefault(require("fs/promises"));
9
9
  const path_1 = require("path");
10
10
  const micromatch_1 = require("micromatch");
@@ -14,7 +14,6 @@ const config_1 = require("./config");
14
14
  const const_1 = require("./const");
15
15
  const events_1 = __importDefault(require("./events"));
16
16
  const perm_1 = require("./perm");
17
- const misc_2 = require("./misc");
18
17
  const WHO_ANYONE = true;
19
18
  const WHO_NO_ONE = false;
20
19
  const WHO_ANY_ACCOUNT = '*';
@@ -38,6 +37,11 @@ function inheritFromParent(parent, child) {
38
37
  child.mime || (child.mime = parent.mime);
39
38
  return child;
40
39
  }
40
+ function isSameFilenameAs(name) {
41
+ const lc = name.toLowerCase();
42
+ return (other) => lc === (typeof other === 'string' ? other : getNodeName(other)).toLowerCase();
43
+ }
44
+ exports.isSameFilenameAs = isSameFilenameAs;
41
45
  async function urlToNode(url, ctx, parent = exports.vfs, getRest) {
42
46
  var _a;
43
47
  let initialSlashes = 0;
@@ -54,9 +58,7 @@ async function urlToNode(url, ctx, parent = exports.vfs, getRest) {
54
58
  return;
55
59
  }
56
60
  // does the tree node have a child that goes by this name?
57
- const sameName = !const_1.IS_WINDOWS ? (x) => x === name // easy
58
- : (0, misc_2.with_)(name.toLowerCase(), lc => (x) => x.toLowerCase() === lc);
59
- const child = (_a = parent.children) === null || _a === void 0 ? void 0 : _a.find(x => sameName(getNodeName(x)));
61
+ const child = (_a = parent.children) === null || _a === void 0 ? void 0 : _a.find(isSameFilenameAs(name));
60
62
  const ret = {
61
63
  ...child,
62
64
  original: child,
@@ -124,8 +126,7 @@ exports.nodeIsDirectory = nodeIsDirectory;
124
126
  function hasPermission(node, perm, ctx) {
125
127
  var _a;
126
128
  return (node.source || perm !== 'can_upload') // Upload possible only if we know where to store. First check node.source because is supposedly faster.
127
- && matchWho((_a = node[perm]) !== null && _a !== void 0 ? _a : exports.defaultPerms[perm], ctx)
128
- && (perm !== 'can_see' || hasPermission(node, 'can_read', ctx)); // can_see is used to hide something you nonetheless can_read, so you MUST also can_read
129
+ && matchWho((_a = node[perm]) !== null && _a !== void 0 ? _a : exports.defaultPerms[perm], ctx);
129
130
  }
130
131
  exports.hasPermission = hasPermission;
131
132
  async function* walkNode(parent, ctx, depth = 0, prefixPath = '') {