|
| 1 | +import { getToolRuntimeAssetPath } from "../tools/tool-defaults.js"; |
| 2 | +import RateLimitCommon from "./rate_limit_common.js"; |
| 3 | + |
| 4 | +/** @import { ConfigModule, IdentityModule } from "./board_runtime_core.js" */ |
| 5 | +/** @import { LiveBoardMessage, RateLimitKind } from "../../types/app-runtime" */ |
| 6 | + |
| 7 | +const RATE_LIMIT_KINDS = /** @type {RateLimitKind[]} */ ( |
| 8 | + RateLimitCommon.RATE_LIMIT_KINDS |
| 9 | +); |
| 10 | + |
| 11 | +/** |
| 12 | + * @param {string} assetPath |
| 13 | + * @returns {string} |
| 14 | + */ |
| 15 | +export function normalizeBoardAssetPath(assetPath) { |
| 16 | + if ( |
| 17 | + assetPath.startsWith("./") || |
| 18 | + assetPath.startsWith("../") || |
| 19 | + assetPath.startsWith("/") || |
| 20 | + assetPath.startsWith("data:") || |
| 21 | + assetPath.startsWith("http://") || |
| 22 | + assetPath.startsWith("https://") |
| 23 | + ) { |
| 24 | + return assetPath; |
| 25 | + } |
| 26 | + return `../${assetPath}`; |
| 27 | +} |
| 28 | + |
| 29 | +export class AssetModule { |
| 30 | + /** @param {(assetPath: string) => string} resolveAssetPath */ |
| 31 | + constructor(resolveAssetPath) { |
| 32 | + this.resolveAssetPath = resolveAssetPath; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @param {string} toolName |
| 37 | + * @param {string} assetFile |
| 38 | + */ |
| 39 | + getToolAssetUrl(toolName, assetFile) { |
| 40 | + return this.resolveAssetPath(getToolRuntimeAssetPath(toolName, assetFile)); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +export class InteractionModule { |
| 45 | + constructor() { |
| 46 | + this.drawingEvent = true; |
| 47 | + this.showMarker = true; |
| 48 | + this.showOtherCursors = true; |
| 49 | + this.showMyCursor = true; |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +export class IdModule { |
| 54 | + /** |
| 55 | + * @param {string} [prefix] |
| 56 | + * @param {string} [suffix] |
| 57 | + */ |
| 58 | + generateUID(prefix, suffix) { |
| 59 | + let uid = Date.now().toString(36); |
| 60 | + uid += Math.round(Math.random() * 36).toString(36); |
| 61 | + if (prefix) uid = prefix + uid; |
| 62 | + if (suffix) uid = uid + suffix; |
| 63 | + return uid; |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +const rateLimitModuleState = new WeakMap(); |
| 68 | + |
| 69 | +export class RateLimitModule { |
| 70 | + /** |
| 71 | + * @param {ConfigModule} config |
| 72 | + * @param {IdentityModule} identity |
| 73 | + */ |
| 74 | + constructor(config, identity) { |
| 75 | + rateLimitModuleState.set(this, { config, identity }); |
| 76 | + } |
| 77 | + |
| 78 | + /** @param {RateLimitKind} kind */ |
| 79 | + getRateLimitDefinition(kind) { |
| 80 | + const state = |
| 81 | + /** @type {{config: ConfigModule, identity: IdentityModule}} */ ( |
| 82 | + rateLimitModuleState.get(this) |
| 83 | + ); |
| 84 | + const configured = state.config.serverConfig.RATE_LIMITS || {}; |
| 85 | + if (configured && configured[kind]) return configured[kind]; |
| 86 | + |
| 87 | + return { |
| 88 | + limit: 0, |
| 89 | + anonymousLimit: 0, |
| 90 | + periodMs: 0, |
| 91 | + }; |
| 92 | + } |
| 93 | + |
| 94 | + /** @param {RateLimitKind} kind */ |
| 95 | + getEffectiveRateLimit(kind) { |
| 96 | + const state = |
| 97 | + /** @type {{config: ConfigModule, identity: IdentityModule}} */ ( |
| 98 | + rateLimitModuleState.get(this) |
| 99 | + ); |
| 100 | + return RateLimitCommon.getEffectiveRateLimitDefinition( |
| 101 | + this.getRateLimitDefinition(kind), |
| 102 | + state.identity.boardName, |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + /** @param {LiveBoardMessage} message */ |
| 107 | + getBufferedWriteCosts(message) { |
| 108 | + return RATE_LIMIT_KINDS.reduce( |
| 109 | + (costs, kind) => { |
| 110 | + costs[kind] = RateLimitCommon.getRateLimitCost(kind, message); |
| 111 | + return costs; |
| 112 | + }, |
| 113 | + /** @type {import("../../types/app-runtime").RateLimitCosts} */ ({}), |
| 114 | + ); |
| 115 | + } |
| 116 | +} |
0 commit comments