hfs 0.26.8 → 0.26.9

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 (161) hide show
  1. package/admin/assets/index.bb5198ec.js +281 -0
  2. package/admin/assets/index.dcc78777.css +1 -0
  3. package/admin/assets/sha512.9dfe82e1.js +8 -0
  4. package/admin/index.html +3 -1
  5. package/admin/{public/logo.svg → logo.svg} +0 -0
  6. package/frontend/assets/index.27a78796.js +85 -0
  7. package/frontend/assets/index.93366732.css +1 -0
  8. package/frontend/assets/sha512.6af42937.js +8 -0
  9. package/frontend/{public/fontello.css → fontello.css} +0 -0
  10. package/frontend/{public/fontello.woff2 → fontello.woff2} +0 -0
  11. package/frontend/index.html +3 -1
  12. package/package.json +1 -1
  13. package/src/QuickZipStream.js +285 -0
  14. package/src/ThrottledStream.js +93 -0
  15. package/src/adminApis.js +169 -0
  16. package/src/api.accounts.js +59 -0
  17. package/src/api.auth.js +128 -0
  18. package/src/api.file_list.js +103 -0
  19. package/src/api.helpers.js +32 -0
  20. package/src/api.monitor.js +102 -0
  21. package/src/api.plugins.js +127 -0
  22. package/src/api.vfs.js +164 -0
  23. package/src/apiMiddleware.js +120 -0
  24. package/src/block.js +33 -0
  25. package/src/commands.js +124 -0
  26. package/src/config.js +168 -0
  27. package/src/connections.js +57 -0
  28. package/src/const.js +83 -0
  29. package/src/crypt.js +21 -0
  30. package/src/debounceAsync.js +48 -0
  31. package/src/events.js +9 -0
  32. package/src/frontEndApis.js +38 -0
  33. package/src/github.js +102 -0
  34. package/src/index.js +56 -0
  35. package/src/listen.js +235 -0
  36. package/src/log.js +137 -0
  37. package/src/middlewares.js +175 -0
  38. package/src/misc.js +160 -0
  39. package/src/pbkdf2.js +74 -0
  40. package/src/perm.js +181 -0
  41. package/src/plugins.js +343 -0
  42. package/src/serveFile.js +105 -0
  43. package/src/serveGuiFiles.js +113 -0
  44. package/src/sse.js +29 -0
  45. package/src/throttler.js +91 -0
  46. package/src/update.js +69 -0
  47. package/src/util-files.js +148 -0
  48. package/src/util-generators.js +30 -0
  49. package/src/util-http.js +30 -0
  50. package/src/vfs.js +230 -0
  51. package/src/watchLoad.js +73 -0
  52. package/src/zip.js +72 -0
  53. package/admin/.DS_Store +0 -0
  54. package/admin/.eslintrc +0 -8
  55. package/admin/.gitignore +0 -23
  56. package/admin/package.json +0 -67
  57. package/admin/src/AccountForm.ts +0 -92
  58. package/admin/src/AccountsPage.ts +0 -143
  59. package/admin/src/App.ts +0 -83
  60. package/admin/src/ArrayField.ts +0 -84
  61. package/admin/src/ConfigPage.ts +0 -279
  62. package/admin/src/FileField.ts +0 -52
  63. package/admin/src/FileForm.ts +0 -148
  64. package/admin/src/FilePicker.ts +0 -166
  65. package/admin/src/HomePage.ts +0 -96
  66. package/admin/src/InstalledPlugins.ts +0 -158
  67. package/admin/src/LoginRequired.ts +0 -75
  68. package/admin/src/LogoutPage.ts +0 -27
  69. package/admin/src/LogsPage.ts +0 -75
  70. package/admin/src/MainMenu.ts +0 -74
  71. package/admin/src/MenuButton.ts +0 -38
  72. package/admin/src/MonitorPage.ts +0 -200
  73. package/admin/src/OnlinePlugins.ts +0 -101
  74. package/admin/src/PermField.ts +0 -80
  75. package/admin/src/PluginsPage.ts +0 -27
  76. package/admin/src/VfsMenuBar.ts +0 -58
  77. package/admin/src/VfsPage.ts +0 -124
  78. package/admin/src/VfsTree.ts +0 -95
  79. package/admin/src/addFiles.ts +0 -59
  80. package/admin/src/api.ts +0 -246
  81. package/admin/src/dialog.ts +0 -203
  82. package/admin/src/index.css +0 -21
  83. package/admin/src/index.ts +0 -10
  84. package/admin/src/md.ts +0 -31
  85. package/admin/src/misc.ts +0 -141
  86. package/admin/src/react-app-env.d.ts +0 -1
  87. package/admin/src/reportWebVitals.ts +0 -15
  88. package/admin/src/setupTests.ts +0 -5
  89. package/admin/src/state.ts +0 -40
  90. package/admin/src/theme.ts +0 -37
  91. package/admin/tsconfig.json +0 -26
  92. package/admin/vite.config.ts +0 -32
  93. package/frontend/.DS_Store +0 -0
  94. package/frontend/.eslintrc +0 -8
  95. package/frontend/.gitignore +0 -23
  96. package/frontend/package.json +0 -51
  97. package/frontend/src/App.ts +0 -25
  98. package/frontend/src/Breadcrumbs.ts +0 -43
  99. package/frontend/src/BrowseFiles.ts +0 -141
  100. package/frontend/src/Head.ts +0 -45
  101. package/frontend/src/UserPanel.ts +0 -52
  102. package/frontend/src/api.ts +0 -78
  103. package/frontend/src/components.ts +0 -54
  104. package/frontend/src/dialog.css +0 -76
  105. package/frontend/src/dialog.ts +0 -105
  106. package/frontend/src/icons.ts +0 -46
  107. package/frontend/src/index.scss +0 -307
  108. package/frontend/src/index.ts +0 -10
  109. package/frontend/src/login.ts +0 -50
  110. package/frontend/src/menu.ts +0 -188
  111. package/frontend/src/misc.ts +0 -54
  112. package/frontend/src/options.ts +0 -52
  113. package/frontend/src/react-app-env.d.ts +0 -1
  114. package/frontend/src/reportWebVitals.ts +0 -15
  115. package/frontend/src/setupTests.ts +0 -5
  116. package/frontend/src/state.ts +0 -82
  117. package/frontend/src/useAuthorized.ts +0 -17
  118. package/frontend/src/useFetchList.ts +0 -144
  119. package/frontend/src/useTheme.ts +0 -23
  120. package/frontend/tsconfig.json +0 -26
  121. package/frontend/vite.config.ts +0 -21
  122. package/src/QuickZipStream.ts +0 -279
  123. package/src/ThrottledStream.ts +0 -98
  124. package/src/adminApis.ts +0 -161
  125. package/src/api.accounts.ts +0 -78
  126. package/src/api.auth.ts +0 -131
  127. package/src/api.file_list.ts +0 -102
  128. package/src/api.helpers.ts +0 -30
  129. package/src/api.monitor.ts +0 -106
  130. package/src/api.plugins.ts +0 -139
  131. package/src/api.vfs.ts +0 -182
  132. package/src/apiMiddleware.ts +0 -124
  133. package/src/block.ts +0 -35
  134. package/src/commands.ts +0 -122
  135. package/src/config.ts +0 -166
  136. package/src/connections.ts +0 -60
  137. package/src/const.ts +0 -57
  138. package/src/crypt.ts +0 -16
  139. package/src/debounceAsync.ts +0 -51
  140. package/src/events.ts +0 -6
  141. package/src/frontEndApis.ts +0 -17
  142. package/src/github.ts +0 -102
  143. package/src/index.ts +0 -53
  144. package/src/listen.ts +0 -220
  145. package/src/log.ts +0 -128
  146. package/src/middlewares.ts +0 -176
  147. package/src/misc.ts +0 -149
  148. package/src/pbkdf2.ts +0 -83
  149. package/src/perm.ts +0 -194
  150. package/src/plugins.ts +0 -342
  151. package/src/serveFile.ts +0 -104
  152. package/src/serveGuiFiles.ts +0 -95
  153. package/src/sse.ts +0 -29
  154. package/src/throttler.ts +0 -106
  155. package/src/update.ts +0 -67
  156. package/src/util-files.ts +0 -137
  157. package/src/util-generators.ts +0 -29
  158. package/src/util-http.ts +0 -29
  159. package/src/vfs.ts +0 -258
  160. package/src/watchLoad.ts +0 -75
  161. package/src/zip.ts +0 -69
package/src/const.js ADDED
@@ -0,0 +1,83 @@
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.APP_PATH = exports.IS_WINDOWS = exports.UNAUTHORIZED = exports.FORBIDDEN = exports.NO_CONTENT = exports.METHOD_NOT_ALLOWED = exports.PLUGINS_PUB_URI = exports.API_URI = exports.ADMIN_URI = exports.FRONTEND_URI = exports.SPECIAL_URI = exports.COMPATIBLE_API_VERSION = exports.API_VERSION = exports.SESSION_DURATION = exports.DAY = exports.VERSION = exports.BUILD_TIMESTAMP = exports.HFS_STARTED = exports.ORIGINAL_CWD = exports.DEV = exports.argv = void 0;
31
+ const minimist_1 = __importDefault(require("minimist"));
32
+ const fs = __importStar(require("fs"));
33
+ const os_1 = require("os");
34
+ const fs_1 = require("fs");
35
+ const path_1 = require("path");
36
+ exports.argv = (0, minimist_1.default)(process.argv.slice(2));
37
+ exports.DEV = process.env.DEV || exports.argv.dev ? 'DEV' : '';
38
+ exports.ORIGINAL_CWD = process.cwd();
39
+ exports.HFS_STARTED = new Date();
40
+ const PKG_PATH = (0, path_1.join)(__dirname, '..', 'package.json');
41
+ exports.BUILD_TIMESTAMP = fs.statSync(PKG_PATH).mtime.toISOString();
42
+ const pkg = JSON.parse(fs.readFileSync(PKG_PATH, 'utf8'));
43
+ exports.VERSION = pkg.version;
44
+ exports.DAY = 86400000;
45
+ exports.SESSION_DURATION = exports.DAY;
46
+ exports.API_VERSION = 4.1; // array.$width
47
+ exports.COMPATIBLE_API_VERSION = 1; // while changes in the api are not breaking, this number stays the same, otherwise is made equal to API_VERSION
48
+ exports.SPECIAL_URI = '/~/';
49
+ exports.FRONTEND_URI = exports.SPECIAL_URI + 'frontend/';
50
+ exports.ADMIN_URI = exports.SPECIAL_URI + 'admin/';
51
+ exports.API_URI = exports.SPECIAL_URI + 'api/';
52
+ exports.PLUGINS_PUB_URI = exports.SPECIAL_URI + 'plugins/';
53
+ exports.METHOD_NOT_ALLOWED = 405;
54
+ exports.NO_CONTENT = 204;
55
+ exports.FORBIDDEN = 403;
56
+ exports.UNAUTHORIZED = 401;
57
+ exports.IS_WINDOWS = process.platform === 'win32';
58
+ const IS_BINARY = !(0, path_1.basename)(process.argv0).includes('node'); // this won't be node if pkg was used
59
+ exports.APP_PATH = (0, path_1.dirname)(IS_BINARY ? process.argv0 : __dirname);
60
+ // we want this to be the first stuff to be printed, then we print it in this module, that is executed at the beginning
61
+ if (exports.DEV)
62
+ console.clear();
63
+ else
64
+ console.debug = () => { };
65
+ console.log(`HFS ~ HTTP File Server - Copyright 2021-2022, Massimo Melina <a@rejetto.com>`);
66
+ console.log(`License https://www.gnu.org/licenses/gpl-3.0.txt`);
67
+ console.log('started', exports.HFS_STARTED.toLocaleString(), exports.DEV);
68
+ console.log('version', exports.VERSION || '-');
69
+ console.log('build', exports.BUILD_TIMESTAMP || '-');
70
+ if (exports.argv.cwd)
71
+ process.chdir(exports.argv.cwd);
72
+ else if (!process.argv0.endsWith('.exe')) { // still considering whether to use this behavior with Windows users, who may be less accustomed to it
73
+ const dir = (0, path_1.join)((0, os_1.homedir)(), '.hfs');
74
+ try {
75
+ (0, fs_1.mkdirSync)(dir);
76
+ }
77
+ catch (e) {
78
+ if (e.code !== 'EEXIST')
79
+ console.error(e);
80
+ }
81
+ process.chdir(dir);
82
+ }
83
+ console.log('cwd', process.cwd());
package/src/crypt.js ADDED
@@ -0,0 +1,21 @@
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.verifyPassword = exports.hashPassword = void 0;
8
+ // simple wrapper
9
+ // @ts-ignore
10
+ const pbkdf2_1 = require("./pbkdf2");
11
+ const assert_1 = __importDefault(require("assert"));
12
+ async function hashPassword(s) {
13
+ return 'p2:' + await (0, pbkdf2_1.pbkdf2)(s);
14
+ }
15
+ exports.hashPassword = hashPassword;
16
+ async function verifyPassword(hashed, given) {
17
+ const i = hashed.indexOf(':');
18
+ (0, assert_1.default)(i > 0, 'bad hashed');
19
+ return await (0, pbkdf2_1.pbkdf2Verify)(hashed.slice(i + 1), given); // for the time being we totally ignore the "method" part
20
+ }
21
+ exports.verifyPassword = verifyPassword;
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ // like lodash.debounce, but also avoids async invocations to overlap
4
+ function debounceAsync(callback, wait = 100, { leading = false, maxWait = Infinity } = {}) {
5
+ let started = 0; // latest callback invocation
6
+ let runningCallback; // latest callback invocation result
7
+ let runningDebouncer; // latest wrapper invocation
8
+ let waitingSince = 0; // we are delaying invocation since
9
+ let whoIsWaiting; // args' array object identifies the pending instance, and incidentally stores args
10
+ const interceptingWrapper = (...args) => runningDebouncer = debouncer.apply(null, args);
11
+ return Object.assign(interceptingWrapper, {
12
+ cancel: () => {
13
+ waitingSince = 0;
14
+ whoIsWaiting = undefined;
15
+ },
16
+ flush: () => runningCallback !== null && runningCallback !== void 0 ? runningCallback : exec(),
17
+ });
18
+ async function debouncer(...args) {
19
+ if (runningCallback)
20
+ return await runningCallback;
21
+ whoIsWaiting = args;
22
+ waitingSince || (waitingSince = Date.now());
23
+ const waitingCap = maxWait - (Date.now() - (waitingSince || started));
24
+ const waitFor = Math.min(waitingCap, leading ? wait - (Date.now() - started) : wait);
25
+ if (waitFor > 0)
26
+ await new Promise(resolve => setTimeout(resolve, waitFor));
27
+ if (!whoIsWaiting) // canceled
28
+ return void (waitingSince = 0);
29
+ if (whoIsWaiting !== args) // another fresher call is waiting
30
+ return runningDebouncer;
31
+ return await exec();
32
+ }
33
+ async function exec() {
34
+ if (!whoIsWaiting)
35
+ return;
36
+ waitingSince = 0;
37
+ started = Date.now();
38
+ try {
39
+ runningCallback = callback.apply(null, whoIsWaiting);
40
+ return await runningCallback;
41
+ }
42
+ finally {
43
+ whoIsWaiting = undefined;
44
+ runningCallback = undefined;
45
+ }
46
+ }
47
+ }
48
+ exports.default = debounceAsync;
package/src/events.js ADDED
@@ -0,0 +1,9 @@
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
+ const events_1 = __importDefault(require("events"));
8
+ // app-wide events
9
+ exports.default = new events_1.default();
@@ -0,0 +1,38 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
27
+ exports.frontEndApis = void 0;
28
+ const api_file_list_1 = require("./api.file_list");
29
+ const api_auth = __importStar(require("./api.auth"));
30
+ const config_1 = require("./config");
31
+ const customHeader = (0, config_1.defineConfig)('custom_header');
32
+ exports.frontEndApis = {
33
+ file_list: api_file_list_1.file_list,
34
+ ...api_auth,
35
+ config() {
36
+ return Object.fromEntries([customHeader].map(x => [x.key(), x.get()]));
37
+ }
38
+ };
package/src/github.js ADDED
@@ -0,0 +1,102 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.searchPlugins = exports.getFolder2repo = exports.readOnlinePlugin = exports.getRepoInfo = exports.downloadPlugin = void 0;
7
+ const events_1 = __importDefault(require("./events"));
8
+ const misc_1 = require("./misc");
9
+ const plugins_1 = require("./plugins");
10
+ const apiMiddleware_1 = require("./apiMiddleware");
11
+ const lodash_1 = __importDefault(require("lodash"));
12
+ const DIST_ROOT = 'dist/';
13
+ const downloading = {};
14
+ function downloadProgress(id, status) {
15
+ if (status === undefined)
16
+ delete downloading[id];
17
+ else
18
+ downloading[id] = status;
19
+ events_1.default.emit('pluginDownload_' + id, status);
20
+ }
21
+ async function downloadPlugin(repo, branch = '', overwrite) {
22
+ if (downloading[repo])
23
+ return new apiMiddleware_1.ApiError(409, "already downloading");
24
+ downloadProgress(repo, true);
25
+ const rec = await getRepoInfo(repo);
26
+ if (!branch)
27
+ branch = rec.default_branch;
28
+ const short = repo.split('/')[1]; // second part, repo without the owner
29
+ const folder2repo = getFolder2repo();
30
+ const folder = overwrite ? lodash_1.default.findKey(folder2repo, x => x === repo) // use existing folder
31
+ : short in folder2repo ? repo.replace('/', '-') // longer form only if another plugin is using short form
32
+ : short;
33
+ const installPath = plugins_1.PATH + '/' + folder;
34
+ const GITHUB_ZIP_ROOT = short + '-' + branch; // GitHub puts everything within this folder
35
+ const rootWithinZip = GITHUB_ZIP_ROOT + '/' + DIST_ROOT;
36
+ const stream = await (0, misc_1.httpsStream)(`https://github.com/${repo}/archive/refs/heads/${branch}.zip`);
37
+ await (0, misc_1.unzip)(stream, path => path.startsWith(rootWithinZip) && installPath + '/' + path.slice(rootWithinZip.length));
38
+ downloadProgress(repo, undefined);
39
+ await (0, plugins_1.rescan)(); // workaround: for some reason, operations are not triggering the rescan of the watched folder. Let's invoke it.
40
+ return folder;
41
+ }
42
+ exports.downloadPlugin = downloadPlugin;
43
+ function getRepoInfo(id) {
44
+ return apiGithub('repos/' + id);
45
+ }
46
+ exports.getRepoInfo = getRepoInfo;
47
+ async function readOnlinePlugin(repoInfo, branch = '') {
48
+ const url = `https://raw.githubusercontent.com/${repoInfo.full_name}/${branch || repoInfo.default_branch}/${DIST_ROOT}plugin.js`;
49
+ const res = await (0, misc_1.httpsString)(url);
50
+ if (!res.ok)
51
+ throw res.statusCode;
52
+ const pl = (0, plugins_1.parsePluginSource)(repoInfo.full_name, res.body); // use 'repo' as 'id' client-side
53
+ pl.branch = branch || undefined;
54
+ return pl;
55
+ }
56
+ exports.readOnlinePlugin = readOnlinePlugin;
57
+ function getFolder2repo() {
58
+ const ret = Object.fromEntries((0, plugins_1.getAvailablePlugins)().map(x => [x.id, x.repo]));
59
+ Object.assign(ret, Object.fromEntries((0, plugins_1.mapPlugins)(x => [x.id, x.getData().repo])));
60
+ return ret;
61
+ }
62
+ exports.getFolder2repo = getFolder2repo;
63
+ async function apiGithub(uri) {
64
+ const res = await (0, misc_1.httpsString)('https://api.github.com/' + uri, {
65
+ headers: {
66
+ 'User-Agent': 'HFS',
67
+ Accept: 'application/vnd.github.v3+json',
68
+ }
69
+ });
70
+ if (!res.ok)
71
+ throw res.statusCode;
72
+ return JSON.parse(res.body);
73
+ }
74
+ async function* searchPlugins(text) {
75
+ var _a;
76
+ const res = await apiGithub('search/repositories?q=topic:hfs-plugin+' + encodeURI(text));
77
+ for (const it of res.items) {
78
+ const repo = it.full_name;
79
+ let pl = await readOnlinePlugin(it);
80
+ if (!pl.apiRequired)
81
+ continue; // mandatory field
82
+ if (pl.badApi) { // we try other branches (starting with 'api')
83
+ const branches = (await apiGithub('repos/' + it.full_name + '/branches'))
84
+ .map((x) => x.name).filter((x) => x.startsWith('api')).sort();
85
+ for (const branch of branches) {
86
+ pl = await readOnlinePlugin(it, branch);
87
+ if (!pl.apiRequired)
88
+ pl.badApi = '-';
89
+ if (!pl.badApi)
90
+ break;
91
+ }
92
+ }
93
+ if (pl.badApi)
94
+ continue;
95
+ Object.assign(pl, {
96
+ downloading: downloading[repo],
97
+ license: (_a = it.license) === null || _a === void 0 ? void 0 : _a.spdx_id,
98
+ }, lodash_1.default.pick(it, ['pushed_at', 'stargazers_count']));
99
+ yield pl;
100
+ }
101
+ }
102
+ exports.searchPlugins = searchPlugins;
package/src/index.js ADDED
@@ -0,0 +1,56 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ // This file is part of HFS - Copyright 2021-2022, Massimo Melina <a@rejetto.com> - License https://www.gnu.org/licenses/gpl-3.0.txt
4
+ var __importDefault = (this && this.__importDefault) || function (mod) {
5
+ return (mod && mod.__esModule) ? mod : { "default": mod };
6
+ };
7
+ var _a;
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.app = void 0;
10
+ const koa_1 = __importDefault(require("koa"));
11
+ const koa_mount_1 = __importDefault(require("koa-mount"));
12
+ const apiMiddleware_1 = require("./apiMiddleware");
13
+ const const_1 = require("./const");
14
+ const frontEndApis_1 = require("./frontEndApis");
15
+ const log_1 = require("./log");
16
+ const plugins_1 = require("./plugins");
17
+ const throttler_1 = require("./throttler");
18
+ const middlewares_1 = require("./middlewares");
19
+ require("./listen");
20
+ require("./commands");
21
+ const adminApis_1 = require("./adminApis");
22
+ const config_1 = require("./config");
23
+ const assert_1 = require("assert");
24
+ const lodash_1 = __importDefault(require("lodash"));
25
+ const misc_1 = require("./misc");
26
+ (0, assert_1.ok)(lodash_1.default.intersection(Object.keys(frontEndApis_1.frontEndApis), Object.keys(adminApis_1.adminApis)).length === 0); // they share same endpoints
27
+ const keys = ((_a = process.env.COOKIE_SIGN_KEYS) === null || _a === void 0 ? void 0 : _a.split(',')) || [(0, misc_1.randomId)(30)];
28
+ exports.app = new koa_1.default({ keys });
29
+ exports.app.use(middlewares_1.someSecurity)
30
+ .use((0, middlewares_1.sessions)(exports.app))
31
+ .use(middlewares_1.prepareState)
32
+ .use(middlewares_1.headRequests)
33
+ .use((0, log_1.log)())
34
+ .use(throttler_1.throttler)
35
+ .use(middlewares_1.gzipper)
36
+ .use(middlewares_1.paramsDecoder)
37
+ .use((0, plugins_1.pluginsMiddleware)())
38
+ .use((0, koa_mount_1.default)(const_1.API_URI, (0, apiMiddleware_1.apiMiddleware)({ ...frontEndApis_1.frontEndApis, ...adminApis_1.adminApis })))
39
+ .use(middlewares_1.serveGuiAndSharedFiles)
40
+ .on('error', errorHandler);
41
+ function errorHandler(err) {
42
+ const { code } = err;
43
+ if (const_1.DEV && code === 'ENOENT' && err.path.endsWith('sockjs-node'))
44
+ return; // spam out dev stuff
45
+ if (code === 'ECANCELED' || code === 'ECONNRESET' || code === 'ECONNABORTED' || code === 'EPIPE')
46
+ return; // someone interrupted, don't care
47
+ console.error('server error', err);
48
+ }
49
+ process.on('uncaughtException', err => {
50
+ if (err.syscall !== 'watch')
51
+ console.error(err);
52
+ });
53
+ (0, config_1.defineConfig)('proxies', 0).sub(n => {
54
+ exports.app.proxy = n > 0;
55
+ exports.app.maxIpsCount = n;
56
+ });
package/src/listen.js ADDED
@@ -0,0 +1,235 @@
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.getUrls = exports.getStatus = exports.httpsPortCfg = exports.openAdmin = exports.portCfg = void 0;
31
+ const http = __importStar(require("http"));
32
+ const config_1 = require("./config");
33
+ const index_1 = require("./index");
34
+ const https = __importStar(require("https"));
35
+ const watchLoad_1 = require("./watchLoad");
36
+ const os_1 = require("os");
37
+ const connections_1 = require("./connections");
38
+ const open_1 = __importDefault(require("open"));
39
+ const misc_1 = require("./misc");
40
+ const const_1 = require("./const");
41
+ const find_process_1 = __importDefault(require("find-process"));
42
+ const perm_1 = require("./perm");
43
+ const lodash_1 = __importDefault(require("lodash"));
44
+ let httpSrv;
45
+ let httpsSrv;
46
+ const openBrowserAtStart = (0, config_1.defineConfig)('open_browser_at_start', !const_1.DEV);
47
+ exports.portCfg = (0, config_1.defineConfig)('port', 80);
48
+ exports.portCfg.sub(async (port) => {
49
+ while (!index_1.app)
50
+ await (0, misc_1.wait)(100);
51
+ stopServer(httpSrv).then();
52
+ httpSrv = Object.assign(http.createServer(index_1.app.callback()), { name: 'http' });
53
+ port = await startServer(httpSrv, { port });
54
+ if (!port)
55
+ return;
56
+ httpSrv.on('connection', connections_1.newConnection);
57
+ printUrls(port, 'http');
58
+ if (openBrowserAtStart.get())
59
+ openAdmin();
60
+ });
61
+ function openAdmin() {
62
+ for (const srv of [httpSrv, httpsSrv]) {
63
+ const a = srv.address();
64
+ if (!a || typeof a === 'string')
65
+ continue;
66
+ const baseUrl = srv.name + '://localhost:' + a.port;
67
+ (0, open_1.default)(baseUrl + const_1.ADMIN_URI, { wait: true }).catch(e => {
68
+ console.debug(String(e));
69
+ console.warn("cannot launch browser on this machine >PLEASE< open your browser and reach one of these (you may need a different address)", ...Object.values(getUrls()).flat().map(x => '\n - ' + x + const_1.ADMIN_URI));
70
+ if (!(0, perm_1.anyAccountCanLoginAdmin)())
71
+ console.log(`HINT: you can enter command: create-admin YOUR_PASSWORD`);
72
+ });
73
+ return true;
74
+ }
75
+ console.log("openAdmin failed");
76
+ }
77
+ exports.openAdmin = openAdmin;
78
+ const considerHttps = (0, misc_1.debounceAsync)(async () => {
79
+ var _a, _b;
80
+ stopServer(httpsSrv).then();
81
+ let port = exports.httpsPortCfg.get();
82
+ try {
83
+ while (!index_1.app)
84
+ await (0, misc_1.wait)(100);
85
+ httpsSrv = Object.assign(https.createServer(port < 0 ? {} : { key: httpsOptions.private_key, cert: httpsOptions.cert }, index_1.app.callback()), { name: 'https', error: undefined });
86
+ if (port >= 0) {
87
+ const namesForOutput = { cert: 'certificate', private_key: 'private key' };
88
+ const missing = (_a = httpsNeeds.find(x => !x.get())) === null || _a === void 0 ? void 0 : _a.key();
89
+ if (missing)
90
+ return httpsSrv.error = "missing " + namesForOutput[missing];
91
+ const cantRead = (_b = httpsNeeds.find(x => !httpsOptions[x.key()])) === null || _b === void 0 ? void 0 : _b.key();
92
+ if (cantRead)
93
+ return httpsSrv.error = "cannot read " + namesForOutput[cantRead];
94
+ }
95
+ }
96
+ catch (e) {
97
+ httpsSrv.error = "bad private key or certificate";
98
+ console.log("failed to create https server: check your private key and certificate", String(e));
99
+ return;
100
+ }
101
+ port = await startServer(httpsSrv, { port });
102
+ if (!port)
103
+ return;
104
+ httpsSrv.on('connection', connections_1.newConnection);
105
+ printUrls(port, 'https');
106
+ });
107
+ const cert = (0, config_1.defineConfig)('cert');
108
+ const privateKey = (0, config_1.defineConfig)('private_key');
109
+ const httpsNeeds = [cert, privateKey];
110
+ const httpsOptions = { cert: '', private_key: '' };
111
+ for (const cfg of httpsNeeds) {
112
+ let unwatch;
113
+ cfg.sub(async (v) => {
114
+ unwatch === null || unwatch === void 0 ? void 0 : unwatch();
115
+ const k = cfg.key();
116
+ httpsOptions[k] = v;
117
+ if (!v || v.includes('\n'))
118
+ return considerHttps();
119
+ // v is a path
120
+ httpsOptions[k] = '';
121
+ unwatch = (0, watchLoad_1.watchLoad)(v, data => {
122
+ httpsOptions[k] = data;
123
+ considerHttps();
124
+ }).unwatch;
125
+ await considerHttps();
126
+ });
127
+ }
128
+ exports.httpsPortCfg = (0, config_1.defineConfig)('https_port', -1);
129
+ exports.httpsPortCfg.sub(considerHttps);
130
+ function startServer(srv, { port, host }) {
131
+ return new Promise(async (resolve) => {
132
+ try {
133
+ if (port < 0 || !host && !await testIpV4()) // !host means ipV4+6, and if v4 port alone is busy we won't be notified of the failure, so we'll first test it on its own
134
+ return resolve(0);
135
+ port = await listen(host);
136
+ if (port)
137
+ console.log(srv.name, "serving on", host || "any network", ':', port);
138
+ resolve(port);
139
+ }
140
+ catch (e) {
141
+ srv.error = String(e);
142
+ console.error(srv.name, "couldn't listen on port", port, srv.error);
143
+ resolve(0);
144
+ }
145
+ });
146
+ async function testIpV4() {
147
+ const res = await listen('0.0.0.0');
148
+ await new Promise(res => srv.close(res));
149
+ return res > 0;
150
+ }
151
+ function listen(host) {
152
+ return new Promise(async (resolve, reject) => {
153
+ srv.listen({ port, host }, () => {
154
+ const ad = srv.address();
155
+ if (!ad)
156
+ return reject('no address');
157
+ if (typeof ad === 'string') {
158
+ srv.close();
159
+ return reject('type of socket not supported');
160
+ }
161
+ resolve(ad.port);
162
+ }).on('error', async (e) => {
163
+ srv.error = String(e);
164
+ srv.busy = undefined;
165
+ const { code } = e;
166
+ if (code === 'EADDRINUSE') {
167
+ srv.busy = (0, find_process_1.default)('port', port).then(res => { var _a; return ((_a = res === null || res === void 0 ? void 0 : res[0]) === null || _a === void 0 ? void 0 : _a.name) || ''; }, () => '');
168
+ srv.error = `port ${port} busy: ${await srv.busy || "unknown process"}`;
169
+ }
170
+ console.error(srv.name, srv.error);
171
+ const k = (srv === httpSrv ? exports.portCfg : exports.httpsPortCfg).key();
172
+ console.log(` >> try specifying a different port, enter this command: config ${k} 8011`);
173
+ resolve(0);
174
+ });
175
+ });
176
+ }
177
+ }
178
+ function stopServer(srv) {
179
+ return new Promise(resolve => {
180
+ if (!(srv === null || srv === void 0 ? void 0 : srv.listening))
181
+ return resolve(null);
182
+ const ad = srv.address();
183
+ if (ad && typeof ad !== 'string')
184
+ console.log("stopped port", ad.port);
185
+ srv.close(err => {
186
+ if (err && err.code !== 'ERR_SERVER_NOT_RUNNING')
187
+ console.debug("failed to stop server", String(err));
188
+ resolve(err);
189
+ });
190
+ });
191
+ }
192
+ function getStatus() {
193
+ return {
194
+ httpSrv,
195
+ httpsSrv,
196
+ };
197
+ }
198
+ exports.getStatus = getStatus;
199
+ const ignore = /^(lo|.*loopback.*|virtualbox.*|.*\(wsl\).*|llw\d|awdl\d|utun\d|anpi\d)$/i; // avoid giving too much information
200
+ function getUrls() {
201
+ return Object.fromEntries((0, misc_1.onlyTruthy)([httpSrv, httpsSrv].map(srv => {
202
+ var _a;
203
+ if (!(srv === null || srv === void 0 ? void 0 : srv.listening))
204
+ return false;
205
+ const port = (_a = srv === null || srv === void 0 ? void 0 : srv.address()) === null || _a === void 0 ? void 0 : _a.port;
206
+ const appendPort = port === (srv.name === 'https' ? 443 : 80) ? '' : ':' + port;
207
+ const urls = (0, misc_1.onlyTruthy)(Object.entries((0, os_1.networkInterfaces)()).map(([name, nets]) => nets && !ignore.test(name) && nets.map(net => {
208
+ if (net.internal)
209
+ return;
210
+ let { address } = net;
211
+ if (address.includes(':'))
212
+ address = '[' + address + ']';
213
+ return srv.name + '://' + address + appendPort;
214
+ })).flat());
215
+ return urls.length && [srv.name, urls];
216
+ })));
217
+ }
218
+ exports.getUrls = getUrls;
219
+ function printUrls(port, proto) {
220
+ if (!port)
221
+ return;
222
+ for (const [name, nets] of Object.entries((0, os_1.networkInterfaces)())) {
223
+ if (!nets || ignore.test(name))
224
+ continue;
225
+ lodash_1.default.remove(nets, 'internal');
226
+ if (!nets.length)
227
+ continue;
228
+ const best = lodash_1.default.find(nets, { family: 'IPv4' }) || nets[0];
229
+ const appendPort = port === (proto === 'https' ? 443 : 80) ? '' : ':' + port;
230
+ let { address } = best;
231
+ if (address.includes(':'))
232
+ address = '[' + address + ']';
233
+ console.log('network', name, proto + '://' + address + appendPort);
234
+ }
235
+ }