@samitouri / QOSami-HFS / commits / a282e37f

better code

Massimo Melina committed Feb 16, 2025 at 15:00 UTC a282e37f66db85f6a615629b3a737fad0fa37551
4 files changed +9 -9
plugins/antibrute/plugin.js
+2 -2
@@ -15,12 +15,12 @@ exports.configDialog = {
15 const byIp = {}
16
17 exports.init = api => {
18 - const { getOrSet, isLocalHost, HOUR } = api.misc
18 + const { isLocalHost, HOUR } = api.misc
19 api.events.multi({
20 async attemptingLogin({ ctx }) {
21 const { ip } = ctx
22 const now = new Date
23 - const rec = getOrSet(byIp, ip, () => ({ attempts: 0, next: now }))
23 + const rec = byIp[ip] ||= { attempts: 0, next: now }
24 const max = api.getConfig('max') * 1000
25 const delay = Math.min(max, 1000 * api.getConfig('increment') * ++rec.attempts)
26 const wait = rec.next - now
src/perm.ts
+2 -2
@@ -1,7 +1,7 @@
1 // This file is part of HFS - Copyright 2021-2023, Massimo Melina <a@rejetto.com> - License https://www.gnu.org/licenses/gpl-3.0.txt
2
3 import _ from 'lodash'
4 -import { getOrSet, HTTP_BAD_REQUEST, objRenameKey, objSameKeys, setHidden, typedEntries, wantArray } from './misc'
4 +import { HTTP_BAD_REQUEST, objRenameKey, objSameKeys, setHidden, typedEntries, wantArray } from './misc'
5 import { defineConfig, saveConfigAsap } from './config'
6 import { createVerifierAndSalt, SRPParameters, SRPRoutines } from 'tssrp6a'
7 import events from './events'
@@ -46,7 +46,7 @@ export function expandUsername(who: string): string[] {
46
47 // check if current username or any ancestor match the provided usernames
48 export function ctxBelongsTo(ctx: Koa.Context, usernames: string[]) {
49 - return getOrSet(ctx.state, 'usernames', () => expandUsername(getCurrentUsername(ctx))) // cache ancestors' usernames inside context state
49 + return (ctx.state.usernames ||= expandUsername(getCurrentUsername(ctx))) // cache ancestors' usernames inside context state
50 .some((u: string) => usernames.includes(u))
51 }
52
src/plugins.ts
+2 -2
@@ -9,7 +9,7 @@ import {
9 import * as Const from './const'
10 import Koa from 'koa'
11 import {
12 - adjustStaticPathForGlob, callable, Callback, CFG, debounceAsync, Dict, getOrSet, objSameKeys, onlyTruthy,
12 + adjustStaticPathForGlob, callable, Callback, CFG, debounceAsync, Dict, objSameKeys, onlyTruthy,
13 PendingPromise, pendingPromise, Promisable, same, tryJson, wait, waitFor, wantArray, watchDir, objFromKeys, patchKey
14 } from './misc'
15 import * as misc from './misc'
@@ -594,7 +594,7 @@ function deleteModule(id: string) {
594 for (const k in cache)
595 if (k !== id)
596 for (const child of wantArray(cache[k]?.children))
597 - getOrSet(requiredBy, child.id, ()=> [] as string[]).push(k)
597 + (requiredBy[child.id] ||= []).push(k)
598 const deleted: string[] = []
599 ;(function deleteCache(id: string) {
600 const mod = cache[id]
src/throttler.ts
+3 -3
@@ -4,7 +4,7 @@ import { Readable } from 'stream'
4 import Koa from 'koa'
5 import { ThrottledStream, ThrottleGroup } from './ThrottledStream'
6 import { defineConfig } from './config'
7 -import { getOrSet, isLocalHost } from './misc'
7 +import { isLocalHost } from './misc'
8 import { Connection, getConnection, updateConnection } from './connections'
9 import _ from 'lodash'
10 import events from './events'
@@ -40,10 +40,10 @@ export const throttler: Koa.Middleware = async (ctx, next) => {
40 return
41 // we wrap the stream also for unlimited connections to get speed and other features
42 const noLimit = ctx.state.account?.ignore_limits || isLocalHost(ctx)
43 - const ipGroup = getOrSet(ip2group, noLimit ? '' : ctx.ip, () => ({
43 + const ipGroup = ip2group[noLimit ? '' : ctx.ip] ||= {
44 count:0,
45 group: new ThrottleGroup(noLimit ? Infinity : maxKbpsPerIp.get(), noLimit ? undefined : mainThrottleGroup),
46 - }))
46 + }
47 const conn = getConnection(ctx)
48 if (!conn) throw 'assert throttler connection'
49