better code

Massimo Melina committed May 25, 2023 at 16:34 UTC 8d49f864347753c8ec682ea921484953eee72ff2
5 files changed +15 -12
frontend/index.html
-1
@@ -5,7 +5,6 @@
5 <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=0" />
6 <link href="/fontello.css" rel="stylesheet" />
7 <script type="module" src="/src/index.ts"></script>
8 - <script>HFS={}</script>
8 </head>
9 <body>
10 <noscript>You need to enable JavaScript to run this app.</noscript>
frontend/src/misc.ts
+2 -2
@@ -4,7 +4,7 @@ import React, { createElement as h } from 'react'
4 import { Spinner } from './components'
5 import { newDialog } from './dialog'
6 import { Icon } from './icons'
7 -import { Dict } from '@hfs/shared'
7 +import { Dict, getHFS } from '@hfs/shared'
8 import { state } from './state'
9 import { t } from './i18n'
10 import * as dialogLib from './dialog'
@@ -66,7 +66,7 @@ const tools = { h, React, state, t, _, dialogLib, apiCall, reloadList, logout, I
66 return subscribeKey(up ? uploadState : state as any, up || k, cb, true)
67 }
68 }
69 -Object.assign((window as any).HFS ||= {}, {
69 +Object.assign(getHFS(), {
70 ...tools,
71 onEvent(name: string, cb: (params:any, tools: any, output:any) => any) {
72 const key = 'hfs.' + name
shared/index.ts
+12
@@ -11,6 +11,18 @@ type Truthy<T> = T extends false | '' | 0 | null | undefined ? never : T
11
12 (window as any)._ = _
13
14 +const HFS = getHFS()
15 +Object.assign(HFS, {
16 + getPluginKey: () => getScriptAttr('plugin'),
17 + getPluginPublic: () => getScriptAttr('src')?.match(/^.*\//)?.[0],
18 + getPluginConfig: () => HFS.plugins[HFS.getPluginKey()],
19 +})
20 +
21 +function getScriptAttr(k: string) {
22 + return document.currentScript?.getAttribute(k)
23 + || console.error("this function must be called at the very top of your file")
24 +}
25 +
26 export const urlParams = Object.fromEntries(new URLSearchParams(window.location.search).entries())
27
28 const MULTIPLIERS = ['', 'K', 'M', 'G', 'T']
src/github.ts
+1 -2
@@ -3,8 +3,6 @@
3 import events from './events'
4 import { httpsString, httpsStream, unzip } from './misc'
5 import { getAvailablePlugins, mapPlugins, parsePluginSource, PATH as PLUGINS_PATH, rescan } from './plugins'
6 -// @ts-ignore
7 -import unzipper from 'unzip-stream'
6 import { ApiError } from './apiMiddleware'
7 import _ from 'lodash'
8 import { DAY, HFS_REPO, HTTP_BAD_REQUEST, HTTP_CONFLICT } from './const'
@@ -44,6 +42,7 @@ export async function downloadPlugin(repo: string, branch='', overwrite?: boolea
42 rootWithinZip + '-' + process.platform,
43 rootWithinZip,
44 ].map(x => x + '/')
45 + // this zip doesn't have content-length, so we cannot produce progress event
46 const stream = await httpsStream(`https://github.com/${repo}/archive/refs/heads/${branch}.zip`)
47 await unzip(stream, path => {
48 const folder = foldersToCopy.find(x => path.startsWith(x))
src/serveGuiFiles.ts
-7
@@ -108,13 +108,6 @@ async function treatIndex(ctx: Koa.Context, filesUri: string, body: string) {
108 }, null, 4)
109 .replace(/<(\/script)/g, '<"+"$1') /*avoid breaking our script container*/}
110 document.documentElement.setAttribute('ver', '${VERSION.split('-')[0] /*for style selectors*/}')
111 - function getScriptAttr(k) {
112 - return document.currentScript?.getAttribute(k)
113 - || console.error("this function must be called at the very top of your file")
114 - }
115 - HFS.getPluginKey = () => getScriptAttr('plugin')
116 - HFS.getPluginConfig = () => HFS.plugins[HFS.getPluginKey()]
117 - HFS.getPluginPublic = () => getScriptAttr('src')?.match(/^.*\\//)[0]
111 </script>
112 <style>
113 :root {