hfs 0.26.6 → 0.26.8

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 (162) hide show
  1. package/admin/.DS_Store +0 -0
  2. package/admin/.eslintrc +8 -0
  3. package/admin/.gitignore +23 -0
  4. package/admin/index.html +1 -3
  5. package/admin/package.json +67 -0
  6. package/admin/{logo.svg → public/logo.svg} +0 -0
  7. package/admin/src/AccountForm.ts +92 -0
  8. package/admin/src/AccountsPage.ts +143 -0
  9. package/admin/src/App.ts +83 -0
  10. package/admin/src/ArrayField.ts +84 -0
  11. package/admin/src/ConfigPage.ts +279 -0
  12. package/admin/src/FileField.ts +52 -0
  13. package/admin/src/FileForm.ts +148 -0
  14. package/admin/src/FilePicker.ts +166 -0
  15. package/admin/src/HomePage.ts +96 -0
  16. package/admin/src/InstalledPlugins.ts +158 -0
  17. package/admin/src/LoginRequired.ts +75 -0
  18. package/admin/src/LogoutPage.ts +27 -0
  19. package/admin/src/LogsPage.ts +75 -0
  20. package/admin/src/MainMenu.ts +74 -0
  21. package/admin/src/MenuButton.ts +38 -0
  22. package/admin/src/MonitorPage.ts +200 -0
  23. package/admin/src/OnlinePlugins.ts +101 -0
  24. package/admin/src/PermField.ts +80 -0
  25. package/admin/src/PluginsPage.ts +27 -0
  26. package/admin/src/VfsMenuBar.ts +58 -0
  27. package/admin/src/VfsPage.ts +124 -0
  28. package/admin/src/VfsTree.ts +95 -0
  29. package/admin/src/addFiles.ts +59 -0
  30. package/admin/src/api.ts +246 -0
  31. package/admin/src/dialog.ts +203 -0
  32. package/admin/src/index.css +21 -0
  33. package/admin/src/index.ts +10 -0
  34. package/admin/src/md.ts +31 -0
  35. package/admin/src/misc.ts +141 -0
  36. package/admin/src/react-app-env.d.ts +1 -0
  37. package/admin/src/reportWebVitals.ts +15 -0
  38. package/admin/src/setupTests.ts +5 -0
  39. package/admin/src/state.ts +40 -0
  40. package/admin/src/theme.ts +37 -0
  41. package/admin/tsconfig.json +26 -0
  42. package/admin/vite.config.ts +32 -0
  43. package/frontend/.DS_Store +0 -0
  44. package/frontend/.eslintrc +8 -0
  45. package/frontend/.gitignore +23 -0
  46. package/frontend/index.html +1 -3
  47. package/frontend/package.json +51 -0
  48. package/frontend/{fontello.css → public/fontello.css} +0 -0
  49. package/frontend/{fontello.woff2 → public/fontello.woff2} +0 -0
  50. package/frontend/src/App.ts +25 -0
  51. package/frontend/src/Breadcrumbs.ts +43 -0
  52. package/frontend/src/BrowseFiles.ts +141 -0
  53. package/frontend/src/Head.ts +45 -0
  54. package/frontend/src/UserPanel.ts +52 -0
  55. package/frontend/src/api.ts +78 -0
  56. package/frontend/src/components.ts +54 -0
  57. package/frontend/src/dialog.css +76 -0
  58. package/frontend/src/dialog.ts +105 -0
  59. package/frontend/src/icons.ts +46 -0
  60. package/frontend/src/index.scss +307 -0
  61. package/frontend/src/index.ts +10 -0
  62. package/frontend/src/login.ts +50 -0
  63. package/frontend/src/menu.ts +188 -0
  64. package/frontend/src/misc.ts +54 -0
  65. package/frontend/src/options.ts +52 -0
  66. package/frontend/src/react-app-env.d.ts +1 -0
  67. package/frontend/src/reportWebVitals.ts +15 -0
  68. package/frontend/src/setupTests.ts +5 -0
  69. package/frontend/src/state.ts +82 -0
  70. package/frontend/src/useAuthorized.ts +17 -0
  71. package/frontend/src/useFetchList.ts +144 -0
  72. package/frontend/src/useTheme.ts +23 -0
  73. package/frontend/tsconfig.json +26 -0
  74. package/frontend/vite.config.ts +21 -0
  75. package/package.json +2 -1
  76. package/plugins/vhosting/plugin.js +1 -1
  77. package/src/QuickZipStream.ts +279 -0
  78. package/src/ThrottledStream.ts +98 -0
  79. package/src/adminApis.ts +161 -0
  80. package/src/api.accounts.ts +78 -0
  81. package/src/api.auth.ts +131 -0
  82. package/src/api.file_list.ts +102 -0
  83. package/src/api.helpers.ts +30 -0
  84. package/src/api.monitor.ts +106 -0
  85. package/src/api.plugins.ts +139 -0
  86. package/src/api.vfs.ts +182 -0
  87. package/src/apiMiddleware.ts +124 -0
  88. package/src/block.ts +35 -0
  89. package/src/commands.ts +122 -0
  90. package/src/config.ts +166 -0
  91. package/src/connections.ts +60 -0
  92. package/src/const.ts +57 -0
  93. package/src/crypt.ts +16 -0
  94. package/src/debounceAsync.ts +51 -0
  95. package/src/events.ts +6 -0
  96. package/src/frontEndApis.ts +17 -0
  97. package/src/github.ts +102 -0
  98. package/src/index.ts +53 -0
  99. package/src/listen.ts +220 -0
  100. package/src/log.ts +128 -0
  101. package/src/middlewares.ts +176 -0
  102. package/src/misc.ts +149 -0
  103. package/src/pbkdf2.ts +83 -0
  104. package/src/perm.ts +194 -0
  105. package/src/plugins.ts +342 -0
  106. package/src/serveFile.ts +104 -0
  107. package/src/serveGuiFiles.ts +95 -0
  108. package/src/sse.ts +29 -0
  109. package/src/throttler.ts +106 -0
  110. package/src/update.ts +67 -0
  111. package/src/util-files.ts +137 -0
  112. package/src/util-generators.ts +29 -0
  113. package/src/util-http.ts +29 -0
  114. package/src/vfs.ts +258 -0
  115. package/src/watchLoad.ts +75 -0
  116. package/src/zip.ts +69 -0
  117. package/admin/assets/index.3129dad1.js +0 -282
  118. package/admin/assets/index.dcc78777.css +0 -1
  119. package/admin/assets/sha512.e9b1ee42.js +0 -8
  120. package/frontend/assets/index.1151988f.js +0 -85
  121. package/frontend/assets/index.93366732.css +0 -1
  122. package/frontend/assets/sha512.bb881250.js +0 -8
  123. package/src/QuickZipStream.js +0 -285
  124. package/src/ThrottledStream.js +0 -93
  125. package/src/adminApis.js +0 -169
  126. package/src/api.accounts.js +0 -59
  127. package/src/api.auth.js +0 -130
  128. package/src/api.file_list.js +0 -103
  129. package/src/api.helpers.js +0 -32
  130. package/src/api.monitor.js +0 -102
  131. package/src/api.plugins.js +0 -127
  132. package/src/api.vfs.js +0 -164
  133. package/src/apiMiddleware.js +0 -136
  134. package/src/block.js +0 -33
  135. package/src/commands.js +0 -124
  136. package/src/config.js +0 -168
  137. package/src/connections.js +0 -57
  138. package/src/const.js +0 -83
  139. package/src/crypt.js +0 -21
  140. package/src/debounceAsync.js +0 -48
  141. package/src/events.js +0 -9
  142. package/src/frontEndApis.js +0 -38
  143. package/src/github.js +0 -102
  144. package/src/index.js +0 -55
  145. package/src/listen.js +0 -235
  146. package/src/log.js +0 -137
  147. package/src/middlewares.js +0 -154
  148. package/src/misc.js +0 -160
  149. package/src/pbkdf2.js +0 -74
  150. package/src/perm.js +0 -176
  151. package/src/plugins.js +0 -343
  152. package/src/serveFile.js +0 -104
  153. package/src/serveGuiFiles.js +0 -113
  154. package/src/sse.js +0 -29
  155. package/src/throttler.js +0 -91
  156. package/src/update.js +0 -69
  157. package/src/util-files.js +0 -148
  158. package/src/util-generators.js +0 -30
  159. package/src/util-http.js +0 -30
  160. package/src/vfs.js +0 -226
  161. package/src/watchLoad.js +0 -73
  162. package/src/zip.js +0 -69
package/src/perm.js DELETED
@@ -1,176 +0,0 @@
1
- "use strict";
2
- // This file is part of HFS - Copyright 2021-2022, Massimo Melina <a@rejetto.com> - License https://www.gnu.org/licenses/gpl-3.0.txt
3
- var __importDefault = (this && this.__importDefault) || function (mod) {
4
- return (mod && mod.__esModule) ? mod : { "default": mod };
5
- };
6
- Object.defineProperty(exports, "__esModule", { value: true });
7
- exports.anyAccountCanLoginAdmin = exports.accountCanLoginAdmin = exports.accountCanLogin = exports.accountHasPassword = exports.getFromAccount = exports.delAccount = exports.setAccount = exports.addAccount = exports.renameAccount = exports.updateAccount = exports.allowClearTextLogin = exports.saveSrpInfo = exports.getAccount = exports.getCurrentUsernameExpanded = exports.getCurrentUsername = exports.getAccounts = void 0;
8
- const lodash_1 = __importDefault(require("lodash"));
9
- const crypt_1 = require("./crypt");
10
- const misc_1 = require("./misc");
11
- const config_1 = require("./config");
12
- const tssrp6a_1 = require("tssrp6a");
13
- const events_1 = __importDefault(require("./events"));
14
- let accounts = {};
15
- function getAccounts() {
16
- return accounts;
17
- }
18
- exports.getAccounts = getAccounts;
19
- function getCurrentUsername(ctx) {
20
- var _a, _b;
21
- return ((_a = ctx.state.account) === null || _a === void 0 ? void 0 : _a.username) || ((_b = ctx.session) === null || _b === void 0 ? void 0 : _b.username) || '';
22
- }
23
- exports.getCurrentUsername = getCurrentUsername;
24
- // provides the username and all other usernames it inherits based on the 'belongs' attribute. Useful to check permissions
25
- function getCurrentUsernameExpanded(ctx) {
26
- const who = getCurrentUsername(ctx);
27
- if (!who)
28
- return [];
29
- const ret = [who];
30
- for (const u of ret) {
31
- const a = getAccount(u);
32
- if (a === null || a === void 0 ? void 0 : a.belongs)
33
- ret.push(...a.belongs);
34
- }
35
- return ret;
36
- }
37
- exports.getCurrentUsernameExpanded = getCurrentUsernameExpanded;
38
- function getAccount(username) {
39
- return username ? accounts[username] : undefined;
40
- }
41
- exports.getAccount = getAccount;
42
- function saveSrpInfo(account, salt, verifier) {
43
- account.srp = String(salt) + '|' + String(verifier);
44
- }
45
- exports.saveSrpInfo = saveSrpInfo;
46
- exports.allowClearTextLogin = (0, config_1.defineConfig)('allow_clear_text_login');
47
- const srp6aNimbusRoutines = new tssrp6a_1.SRPRoutines(new tssrp6a_1.SRPParameters());
48
- async function updateAccount(account, changer) {
49
- const was = JSON.stringify(account);
50
- await (changer === null || changer === void 0 ? void 0 : changer(account));
51
- const { username } = account;
52
- if (account.password) {
53
- console.debug('hashing password for', username);
54
- if (exports.allowClearTextLogin.get())
55
- account.hashed_password = await (0, crypt_1.hashPassword)(account.password);
56
- const res = await (0, tssrp6a_1.createVerifierAndSalt)(srp6aNimbusRoutines, username, account.password);
57
- saveSrpInfo(account, res.s, res.v);
58
- delete account.password;
59
- }
60
- else if (!account.srp && account.hashed_password) {
61
- console.log('please reset password for account', username);
62
- process.exit(1);
63
- }
64
- if (account.belongs)
65
- account.belongs = (0, misc_1.wantArray)(account.belongs).filter(b => b in accounts // at this stage the group record may still be null if specified later in the file
66
- || console.error(`account ${username} belongs to non-existing ${b}`));
67
- if (was !== JSON.stringify(account))
68
- saveAccountsAsap();
69
- }
70
- exports.updateAccount = updateAccount;
71
- const saveAccountsAsap = config_1.saveConfigAsap;
72
- const accountsConfig = (0, config_1.defineConfig)('accounts', {});
73
- accountsConfig.sub(async (v) => {
74
- // we should validate content here
75
- accounts = v; // keep local reference
76
- await Promise.all(lodash_1.default.map(accounts, async (rec, k) => {
77
- const norm = normalizeUsername(k);
78
- if (!rec) // an empty object in yaml is stored as null
79
- rec = accounts[norm] = { username: norm };
80
- else
81
- (0, misc_1.objRenameKey)(accounts, k, norm);
82
- (0, misc_1.setHidden)(rec, { username: norm });
83
- await updateAccount(rec);
84
- }));
85
- });
86
- function normalizeUsername(username) {
87
- return username.toLocaleLowerCase();
88
- }
89
- function renameAccount(from, to) {
90
- from = normalizeUsername(from);
91
- to = normalizeUsername(to);
92
- if (!to || !accounts[from] || accounts[to])
93
- return false;
94
- if (to === from)
95
- return true;
96
- (0, misc_1.objRenameKey)(accounts, from, to);
97
- updateReferences();
98
- saveAccountsAsap();
99
- return true;
100
- function updateReferences() {
101
- var _a;
102
- (0, misc_1.setHidden)(accounts[to], { username: to });
103
- for (const a of Object.values(accounts)) {
104
- const idx = (_a = a.belongs) === null || _a === void 0 ? void 0 : _a.indexOf(from);
105
- if (idx !== undefined && idx >= 0)
106
- a.belongs[idx] = to;
107
- }
108
- events_1.default.emit('accountRenamed', from, to); // everybody, take care of your stuff
109
- }
110
- }
111
- exports.renameAccount = renameAccount;
112
- // we consider all the following fields, when falsy, as equivalent to be missing. If this changes in the future, please adjust addAccount and setAccount
113
- const assignableProps = ['redirect', 'ignore_limits', 'belongs', 'admin'];
114
- function addAccount(username, props) {
115
- if (!username || accounts[username])
116
- return;
117
- const copy = (0, misc_1.setHidden)(lodash_1.default.pickBy(lodash_1.default.pick(props, assignableProps), Boolean), { username }); // have the field in the object but hidden so that stringification won't include it
118
- accountsConfig.set(accounts => Object.assign(accounts, { [username]: copy }));
119
- saveAccountsAsap().then();
120
- return copy;
121
- }
122
- exports.addAccount = addAccount;
123
- function setAccount(username, changes) {
124
- const acc = getAccount(username);
125
- if (!acc)
126
- return false;
127
- const rest = lodash_1.default.pick(changes, assignableProps);
128
- for (const [k, v] of Object.entries(rest))
129
- if (!v)
130
- rest[k] = undefined;
131
- Object.assign(acc, rest);
132
- if (changes.username)
133
- renameAccount(username, changes.username);
134
- saveAccountsAsap().then();
135
- return true;
136
- }
137
- exports.setAccount = setAccount;
138
- function delAccount(username) {
139
- if (!getAccount(username))
140
- return false;
141
- accountsConfig.set(accounts => Object.assign(accounts, { [username]: undefined }));
142
- saveAccountsAsap().then();
143
- return true;
144
- }
145
- exports.delAccount = delAccount;
146
- // get some property from account, searching in its groups if necessary. Search is breadth-first, and this determines priority of inheritance.
147
- function getFromAccount(account, getter) {
148
- const search = [account];
149
- for (const accountOrUsername of search) {
150
- const a = typeof accountOrUsername === 'string' ? getAccount(accountOrUsername) : accountOrUsername;
151
- if (!a)
152
- continue;
153
- const res = getter(a);
154
- if (res !== undefined)
155
- return res;
156
- if (a.belongs)
157
- search.push(...a.belongs);
158
- }
159
- }
160
- exports.getFromAccount = getFromAccount;
161
- function accountHasPassword(account) {
162
- return Boolean(account.password || account.hashed_password || account.srp);
163
- }
164
- exports.accountHasPassword = accountHasPassword;
165
- function accountCanLogin(account) {
166
- return accountHasPassword(account);
167
- }
168
- exports.accountCanLogin = accountCanLogin;
169
- function accountCanLoginAdmin(account) {
170
- return accountCanLogin(account) && getFromAccount(account, a => a.admin);
171
- }
172
- exports.accountCanLoginAdmin = accountCanLoginAdmin;
173
- function anyAccountCanLoginAdmin() {
174
- return Object.values(accounts).find(accountCanLoginAdmin);
175
- }
176
- exports.anyAccountCanLoginAdmin = anyAccountCanLoginAdmin;
package/src/plugins.js DELETED
@@ -1,343 +0,0 @@
1
- "use strict";
2
- // This file is part of HFS - Copyright 2021-2022, Massimo Melina <a@rejetto.com> - License https://www.gnu.org/licenses/gpl-3.0.txt
3
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
- if (k2 === undefined) k2 = k;
5
- var desc = Object.getOwnPropertyDescriptor(m, k);
6
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
- desc = { enumerable: true, get: function() { return m[k]; } };
8
- }
9
- Object.defineProperty(o, k2, desc);
10
- }) : (function(o, m, k, k2) {
11
- if (k2 === undefined) k2 = k;
12
- o[k2] = m[k];
13
- }));
14
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
- Object.defineProperty(o, "default", { enumerable: true, value: v });
16
- }) : function(o, v) {
17
- o["default"] = v;
18
- });
19
- var __importStar = (this && this.__importStar) || function (mod) {
20
- if (mod && mod.__esModule) return mod;
21
- var result = {};
22
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
23
- __setModuleDefault(result, mod);
24
- return result;
25
- };
26
- var __importDefault = (this && this.__importDefault) || function (mod) {
27
- return (mod && mod.__esModule) ? mod : { "default": mod };
28
- };
29
- Object.defineProperty(exports, "__esModule", { value: true });
30
- exports.parsePluginSource = exports.rescan = exports.pluginsConfig = exports.enablePlugins = exports.pluginsWatcher = exports.getAvailablePlugins = exports.Plugin = exports.pluginsMiddleware = exports.getPluginConfigFields = exports.mapPlugins = exports.getPluginInfo = exports.setPluginConfig = exports.enablePlugin = exports.isPluginRunning = exports.DISABLING_POSTFIX = exports.PATH = void 0;
31
- const fast_glob_1 = __importDefault(require("fast-glob"));
32
- const watchLoad_1 = require("./watchLoad");
33
- const lodash_1 = __importDefault(require("lodash"));
34
- const path_1 = __importDefault(require("path"));
35
- const const_1 = require("./const");
36
- const Const = __importStar(require("./const"));
37
- const misc_1 = require("./misc");
38
- const config_1 = require("./config");
39
- const serveFile_1 = require("./serveFile");
40
- const events_1 = __importDefault(require("./events"));
41
- const promises_1 = require("fs/promises");
42
- const fs_1 = require("fs");
43
- const connections_1 = require("./connections");
44
- exports.PATH = 'plugins';
45
- exports.DISABLING_POSTFIX = '-disabled';
46
- const plugins = {};
47
- function isPluginRunning(id) {
48
- var _a;
49
- return (_a = plugins[id]) === null || _a === void 0 ? void 0 : _a.started;
50
- }
51
- exports.isPluginRunning = isPluginRunning;
52
- function enablePlugin(id, state = true) {
53
- exports.enablePlugins.set(arr => arr.includes(id) === state ? arr
54
- : state ? [...arr, id]
55
- : arr.filter((x) => x !== id));
56
- }
57
- exports.enablePlugin = enablePlugin;
58
- // nullish values are equivalent to defaultValues
59
- function setPluginConfig(id, changes) {
60
- exports.pluginsConfig.set(allConfigs => {
61
- const fields = getPluginConfigFields(id);
62
- const oldConfig = allConfigs[id];
63
- const newConfig = lodash_1.default.pickBy({ ...oldConfig, ...changes }, (v, k) => { var _a; return v != null && !(0, misc_1.same)(v, (_a = fields === null || fields === void 0 ? void 0 : fields[k]) === null || _a === void 0 ? void 0 : _a.defaultValue); });
64
- return { ...allConfigs, [id]: lodash_1.default.isEmpty(newConfig) ? undefined : newConfig };
65
- });
66
- }
67
- exports.setPluginConfig = setPluginConfig;
68
- function getPluginInfo(id) {
69
- var _a;
70
- const running = (_a = plugins[id]) === null || _a === void 0 ? void 0 : _a.getData();
71
- return running && Object.assign(running, { id }) || availablePlugins[id];
72
- }
73
- exports.getPluginInfo = getPluginInfo;
74
- function mapPlugins(cb) {
75
- return lodash_1.default.map(plugins, (pl, plName) => {
76
- try {
77
- return cb(pl, plName);
78
- }
79
- catch (e) {
80
- console.log('plugin error', plName, String(e));
81
- }
82
- }).filter(x => x !== undefined);
83
- }
84
- exports.mapPlugins = mapPlugins;
85
- function getPluginConfigFields(id) {
86
- var _a;
87
- return (_a = plugins[id]) === null || _a === void 0 ? void 0 : _a.getData().config;
88
- }
89
- exports.getPluginConfigFields = getPluginConfigFields;
90
- function pluginsMiddleware() {
91
- return async (ctx, next) => {
92
- var _a;
93
- const after = [];
94
- // run middleware plugins
95
- for (const id in plugins)
96
- try {
97
- const pl = plugins[id];
98
- const res = await ((_a = pl.middleware) === null || _a === void 0 ? void 0 : _a.call(pl, ctx));
99
- if (res === true)
100
- ctx.pluginStopped = true;
101
- if (typeof res === 'function')
102
- after.push(res);
103
- }
104
- catch (e) {
105
- console.log('error middleware plugin', id, String(e));
106
- console.debug(e);
107
- }
108
- // expose public plugins' files
109
- const { path } = ctx;
110
- if (!ctx.pluginStopped) {
111
- if (path.startsWith(const_1.PLUGINS_PUB_URI)) {
112
- const a = path.substring(const_1.PLUGINS_PUB_URI.length).split('/');
113
- if (plugins.hasOwnProperty(a[0])) { // do it only if the plugin is loaded
114
- a.splice(1, 0, 'public');
115
- await (0, serveFile_1.serveFile)(exports.PATH + '/' + a.join('/'), 'auto')(ctx, next);
116
- }
117
- }
118
- await next();
119
- }
120
- for (const f of after)
121
- await f();
122
- };
123
- }
124
- exports.pluginsMiddleware = pluginsMiddleware;
125
- class Plugin {
126
- constructor(id, data, unwatch) {
127
- this.id = id;
128
- this.data = data;
129
- this.unwatch = unwatch;
130
- this.started = new Date();
131
- if (!data)
132
- throw 'invalid data';
133
- if (plugins[id])
134
- throw "unload first: " + id;
135
- plugins[id] = this;
136
- this.data = data = { ...data }; // clone to make object modifiable. Objects coming from import are not.
137
- // some validation
138
- for (const k of ['frontend_css', 'frontend_js']) {
139
- const v = data[k];
140
- if (typeof v === 'string')
141
- data[k] = [v];
142
- else if (v && !Array.isArray(v)) {
143
- delete data[k];
144
- console.warn('invalid', k);
145
- }
146
- }
147
- }
148
- get middleware() {
149
- var _a;
150
- return (_a = this.data) === null || _a === void 0 ? void 0 : _a.middleware;
151
- }
152
- get frontend_css() {
153
- var _a;
154
- return (_a = this.data) === null || _a === void 0 ? void 0 : _a.frontend_css;
155
- }
156
- get frontend_js() {
157
- var _a;
158
- return (_a = this.data) === null || _a === void 0 ? void 0 : _a.frontend_js;
159
- }
160
- get onDirEntry() {
161
- var _a;
162
- return (_a = this.data) === null || _a === void 0 ? void 0 : _a.onDirEntry;
163
- }
164
- getData() {
165
- return { ...this.data };
166
- }
167
- async unload(reloading = false) {
168
- var _a, _b;
169
- const { id } = this;
170
- try {
171
- await ((_b = (_a = this.data) === null || _a === void 0 ? void 0 : _a.unload) === null || _b === void 0 ? void 0 : _b.call(_a));
172
- console.log('unloaded plugin', id);
173
- }
174
- catch (e) {
175
- console.log('error unloading plugin', id, String(e));
176
- }
177
- delete plugins[id];
178
- if (reloading)
179
- return;
180
- this.unwatch();
181
- if (availablePlugins[id])
182
- events_1.default.emit('pluginStopped', availablePlugins[id]);
183
- else
184
- events_1.default.emit('pluginUninstalled', id);
185
- }
186
- }
187
- exports.Plugin = Plugin;
188
- let availablePlugins = {};
189
- function getAvailablePlugins() {
190
- return Object.values(availablePlugins);
191
- }
192
- exports.getAvailablePlugins = getAvailablePlugins;
193
- const rescanAsap = (0, misc_1.debounceAsync)(rescan, 1000);
194
- if (!(0, fs_1.existsSync)(exports.PATH))
195
- try {
196
- (0, fs_1.mkdirSync)(exports.PATH);
197
- }
198
- catch (_a) { }
199
- exports.pluginsWatcher = (0, misc_1.watchDir)(exports.PATH, rescanAsap);
200
- exports.enablePlugins = (0, config_1.defineConfig)('enable_plugins', ['antibrute']);
201
- exports.enablePlugins.sub(rescanAsap);
202
- exports.pluginsConfig = (0, config_1.defineConfig)('plugins_config', {});
203
- async function rescan() {
204
- console.debug('scanning plugins');
205
- const found = [];
206
- const foundDisabled = {};
207
- const MASK = exports.PATH + '/*/plugin.js'; // be sure to not use path.join as fast-glob doesn't work with \
208
- for (const f of await (0, fast_glob_1.default)([(0, misc_1.adjustStaticPathForGlob)(const_1.APP_PATH) + '/' + MASK, MASK])) {
209
- const id = f.split('/').slice(-2)[0];
210
- if (id.endsWith(exports.DISABLING_POSTFIX))
211
- continue;
212
- if (!exports.enablePlugins.get().includes(id)) {
213
- try {
214
- const source = await (0, promises_1.readFile)(f, 'utf8');
215
- foundDisabled[id] = parsePluginSource(id, source);
216
- }
217
- catch (_a) { }
218
- continue;
219
- }
220
- found.push(id);
221
- if (plugins[id]) // already loaded
222
- continue;
223
- const module = path_1.default.resolve(f);
224
- const { unwatch } = (0, watchLoad_1.watchLoad)(f, async () => {
225
- try {
226
- const alreadyRunning = plugins[id];
227
- console.log(alreadyRunning ? "reloading plugin" : "loading plugin", id);
228
- const { init, ...data } = await Promise.resolve().then(() => __importStar(require(module)));
229
- delete data.default;
230
- deleteModule(require.resolve(module)); // avoid caching at next import
231
- calculateBadApi(data);
232
- if (data.badApi)
233
- console.log("plugin", id, data.badApi);
234
- await (alreadyRunning === null || alreadyRunning === void 0 ? void 0 : alreadyRunning.unload(true));
235
- console.debug("starting plugin", id);
236
- const res = await (init === null || init === void 0 ? void 0 : init.call(null, {
237
- srcDir: __dirname,
238
- const: Const,
239
- require,
240
- getConnections: connections_1.getConnections,
241
- events: events_1.default,
242
- log(...args) {
243
- console.log('plugin', id, ':', ...args);
244
- },
245
- getConfig: (cfgKey) => { var _a, _b, _c, _d, _e; return (_c = (_b = (_a = exports.pluginsConfig.get()) === null || _a === void 0 ? void 0 : _a[id]) === null || _b === void 0 ? void 0 : _b[cfgKey]) !== null && _c !== void 0 ? _c : (_e = (_d = data.config) === null || _d === void 0 ? void 0 : _d[cfgKey]) === null || _e === void 0 ? void 0 : _e.defaultValue; },
246
- setConfig: (cfgKey, value) => setPluginConfig(id, { [cfgKey]: value }),
247
- subscribeConfig(cfgKey, cb) {
248
- let last = this.getConfig(cfgKey);
249
- cb(last);
250
- return exports.pluginsConfig.sub(() => {
251
- const now = this.getConfig(cfgKey);
252
- if ((0, misc_1.same)(now, last))
253
- return;
254
- try {
255
- cb(last = now);
256
- }
257
- catch (e) {
258
- console.log('plugin', id, String(e));
259
- }
260
- });
261
- },
262
- getHfsConfig: config_1.getConfig,
263
- }));
264
- Object.assign(data, res);
265
- const plugin = new Plugin(id, data, unwatch);
266
- if (alreadyRunning)
267
- events_1.default.emit('pluginUpdated', Object.assign(lodash_1.default.pick(plugin, 'started'), getPluginInfo(id)));
268
- else {
269
- const wasInstalled = availablePlugins[id];
270
- if (wasInstalled)
271
- delete availablePlugins[id];
272
- events_1.default.emit(wasInstalled ? 'pluginStarted' : 'pluginInstalled', plugin);
273
- }
274
- }
275
- catch (e) {
276
- console.log("plugin error:", e);
277
- }
278
- });
279
- }
280
- for (const id in foundDisabled) {
281
- const p = foundDisabled[id];
282
- const a = availablePlugins[id];
283
- if ((0, misc_1.same)(a, p))
284
- continue;
285
- availablePlugins[id] = p;
286
- if (a)
287
- events_1.default.emit('pluginUpdated', p);
288
- else if (!plugins[id])
289
- events_1.default.emit('pluginInstalled', p);
290
- }
291
- for (const id in availablePlugins)
292
- if (!foundDisabled[id] && !found.includes(id) && !plugins[id]) {
293
- delete availablePlugins[id];
294
- events_1.default.emit('pluginUninstalled', id);
295
- }
296
- for (const id in plugins)
297
- if (!found.includes(id))
298
- await plugins[id].unload();
299
- }
300
- exports.rescan = rescan;
301
- function deleteModule(id) {
302
- var _a;
303
- const { cache } = require;
304
- // build reversed map of dependencies
305
- const requiredBy = { '.': ['.'] }; // don't touch main entry
306
- for (const k in cache)
307
- if (k !== id)
308
- for (const child of (0, misc_1.wantArray)((_a = cache[k]) === null || _a === void 0 ? void 0 : _a.children))
309
- (0, misc_1.getOrSet)(requiredBy, child.id, () => []).push(k);
310
- const deleted = [];
311
- recur(id);
312
- function recur(id) {
313
- let mod = cache[id];
314
- if (!mod)
315
- return;
316
- delete cache[id];
317
- deleted.push(id);
318
- for (const child of mod.children)
319
- if (!lodash_1.default.difference(requiredBy[child.id], deleted).length)
320
- recur(child.id);
321
- }
322
- }
323
- (0, misc_1.onProcessExit)(() => Promise.allSettled(mapPlugins(pl => pl.unload())));
324
- function parsePluginSource(id, source) {
325
- var _a, _b, _c, _d, _e, _f;
326
- const pl = { id };
327
- pl.description = (0, misc_1.tryJson)((_a = /exports.description *= *(".*")/.exec(source)) === null || _a === void 0 ? void 0 : _a[1]);
328
- pl.repo = (_b = /exports.repo *= *"(.*)"/.exec(source)) === null || _b === void 0 ? void 0 : _b[1];
329
- pl.version = (_d = Number((_c = /exports.version *= *(\d*\.?\d+)/.exec(source)) === null || _c === void 0 ? void 0 : _c[1])) !== null && _d !== void 0 ? _d : undefined;
330
- pl.apiRequired = (_f = (0, misc_1.tryJson)((_e = /exports.apiRequired *= *([ \d.,[\]]+)/.exec(source)) === null || _e === void 0 ? void 0 : _e[1])) !== null && _f !== void 0 ? _f : undefined;
331
- if (Array.isArray(pl.apiRequired) && (pl.apiRequired.length !== 2 || !pl.apiRequired.every(lodash_1.default.isFinite))) // validate [from,to] form
332
- pl.apiRequired = undefined;
333
- calculateBadApi(pl);
334
- return pl;
335
- }
336
- exports.parsePluginSource = parsePluginSource;
337
- function calculateBadApi(data) {
338
- const r = data.apiRequired;
339
- const [min, max] = Array.isArray(r) ? r : [r, r]; // normalize data type
340
- data.badApi = min > const_1.API_VERSION ? "may not work correctly as it is designed for a newer version of HFS - check for updates"
341
- : max < const_1.COMPATIBLE_API_VERSION ? "may not work correctly as it is designed for an older version of HFS - check for updates"
342
- : undefined;
343
- }
package/src/serveFile.js DELETED
@@ -1,104 +0,0 @@
1
- "use strict";
2
- // This file is part of HFS - Copyright 2021-2022, Massimo Melina <a@rejetto.com> - License https://www.gnu.org/licenses/gpl-3.0.txt
3
- var __importDefault = (this && this.__importDefault) || function (mod) {
4
- return (mod && mod.__esModule) ? mod : { "default": mod };
5
- };
6
- Object.defineProperty(exports, "__esModule", { value: true });
7
- exports.getRange = exports.serveFile = exports.serveFileNode = void 0;
8
- const fs_1 = require("fs");
9
- const const_1 = require("./const");
10
- const vfs_1 = require("./vfs");
11
- const mime_types_1 = __importDefault(require("mime-types"));
12
- const config_1 = require("./config");
13
- const micromatch_1 = require("micromatch");
14
- const lodash_1 = __importDefault(require("lodash"));
15
- const path_1 = __importDefault(require("path"));
16
- const util_1 = require("util");
17
- const allowedReferer = (0, config_1.defineConfig)('allowed_referer', '');
18
- function serveFileNode(node) {
19
- const { source, mime } = node;
20
- const name = (0, vfs_1.getNodeName)(node);
21
- const mimeString = typeof mime === 'string' ? mime
22
- : lodash_1.default.find(mime, (val, mask) => (0, micromatch_1.isMatch)(name, mask));
23
- return (ctx, next) => {
24
- var _a;
25
- const allowed = allowedReferer.get();
26
- if (allowed) {
27
- const ref = (_a = /\/\/([^:/]+)/.exec(ctx.get('referer'))) === null || _a === void 0 ? void 0 : _a[1]; // extract host from url
28
- if (ref && ref !== host() // automatic accept if referer is basically the hosting domain
29
- && !(0, micromatch_1.isMatch)(ref, allowed))
30
- return ctx.status = const_1.FORBIDDEN;
31
- function host() {
32
- const s = ctx.get('host');
33
- return s[0] === '[' ? s.slice(1, s.indexOf(']')) : s === null || s === void 0 ? void 0 : s.split(':')[0];
34
- }
35
- }
36
- ctx.vfsNode = node; // useful to tell service files from files shared by the user
37
- return serveFile(source || '', mimeString)(ctx, next);
38
- };
39
- }
40
- exports.serveFileNode = serveFileNode;
41
- const mimeCfg = (0, config_1.defineConfig)('mime', { '*': 'auto' });
42
- function serveFile(source, mime, content) {
43
- return async (ctx) => {
44
- if (!source)
45
- return;
46
- const fn = path_1.default.basename(source);
47
- mime = mime !== null && mime !== void 0 ? mime : lodash_1.default.find(mimeCfg.get(), (v, k) => k > '' && (0, micromatch_1.isMatch)(fn, k)); // isMatch throws on an empty string
48
- if (mime === vfs_1.MIME_AUTO)
49
- mime = mime_types_1.default.lookup(source) || '';
50
- if (mime)
51
- ctx.type = mime;
52
- if (ctx.method === 'OPTIONS') {
53
- ctx.status = const_1.NO_CONTENT;
54
- ctx.set({ Allow: 'OPTIONS, GET, HEAD' });
55
- return;
56
- }
57
- if (ctx.method !== 'GET')
58
- return ctx.status = const_1.METHOD_NOT_ALLOWED;
59
- try {
60
- const stats = await (0, util_1.promisify)(fs_1.stat)(source); // using fs's function instead of fs/promises, because only the former is supported by pkg
61
- ctx.set('Last-Modified', stats.mtime.toUTCString());
62
- ctx.fileSource = source;
63
- ctx.status = 200;
64
- if (ctx.fresh)
65
- return ctx.status = 304;
66
- if (content !== undefined)
67
- return ctx.body = content;
68
- const range = getRange(ctx, stats.size);
69
- ctx.body = (0, fs_1.createReadStream)(source, range);
70
- }
71
- catch (_a) {
72
- return ctx.status = 404;
73
- }
74
- };
75
- }
76
- exports.serveFile = serveFile;
77
- function getRange(ctx, totalSize) {
78
- ctx.set('Accept-Ranges', 'bytes');
79
- const { range } = ctx.request.header;
80
- if (!range) {
81
- ctx.response.length = totalSize;
82
- return;
83
- }
84
- const ranges = range.split('=')[1];
85
- if (ranges.includes(','))
86
- return ctx.throw(400, 'multi-range not supported');
87
- let bytes = ranges === null || ranges === void 0 ? void 0 : ranges.split('-');
88
- if (!(bytes === null || bytes === void 0 ? void 0 : bytes.length))
89
- return ctx.throw(400, 'bad range');
90
- const max = totalSize - 1;
91
- const start = bytes[0] ? Number(bytes[0]) : Math.max(0, totalSize - Number(bytes[1])); // a negative start is relative to the end
92
- const end = bytes[0] ? Number(bytes[1] || max) : max; // NaN in case we are asked for last N bytes without knowing max
93
- if (isNaN(end) || end > max || start > max) {
94
- ctx.status = 416;
95
- ctx.set('Content-Range', `bytes ${totalSize}`);
96
- ctx.body = 'Requested Range Not Satisfiable';
97
- return;
98
- }
99
- ctx.status = 206;
100
- ctx.set('Content-Range', `bytes ${start}-${end}/${isNaN(totalSize) ? '*' : totalSize}`);
101
- ctx.response.length = end - start + 1;
102
- return { start, end };
103
- }
104
- exports.getRange = getRange;