refactor(desktop): organize lib modules by domain
This commit is contained in:
1 parent
90f0594e93
commit
7e7c4944e9
700 files changed
+1292
-1264
No files matched your search
@@ -0,0 +1,31 @@
|
||||
export const MQ_PINNED_VERSION_AUTO = "auto";
|
||||
|
||||
export interface MqPinnedVersionOption {
|
||||
value: string;
|
||||
label: string;
|
||||
description: string;
|
||||
}
|
||||
|
||||
export const MQ_PINNED_VERSION_OPTIONS: MqPinnedVersionOption[] = [
|
||||
{
|
||||
value: MQ_PINNED_VERSION_AUTO,
|
||||
label: "Auto detect",
|
||||
description: "Probe /admin/v2/brokers/version and use the default 3.1.x profile if probing fails.",
|
||||
},
|
||||
{
|
||||
value: "3.1.x",
|
||||
label: "Apache Pulsar 3.1.x",
|
||||
description: "Supported Pulsar 3.x admin API profile, compatible with the 3.0/3.1 LTS line.",
|
||||
},
|
||||
];
|
||||
|
||||
export function pinnedVersionToSelection(pinnedVersion: string | null | undefined): string {
|
||||
const value = pinnedVersion?.trim();
|
||||
if (!value) return MQ_PINNED_VERSION_AUTO;
|
||||
if (/^3(?:\.|$)/.test(value)) return "3.1.x";
|
||||
return MQ_PINNED_VERSION_AUTO;
|
||||
}
|
||||
|
||||
export function selectionToPinnedVersion(selection: string): string | undefined {
|
||||
return MQ_PINNED_VERSION_OPTIONS.some((option) => option.value === selection) && selection !== MQ_PINNED_VERSION_AUTO ? selection : undefined;
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
import type { BacklogQuota, DispatchRate, PublishRate, RetentionPolicy, SubscribeRate } from "@/types/mq";
|
||||
|
||||
export interface MqPolicyForms {
|
||||
publishForm: PublishRate;
|
||||
dispatchForm: DispatchRate;
|
||||
subscribeForm: SubscribeRate;
|
||||
backlogForm: BacklogQuota;
|
||||
retentionForm: RetentionPolicy;
|
||||
}
|
||||
|
||||
export function defaultMqPolicyForms(): MqPolicyForms {
|
||||
return {
|
||||
publishForm: {
|
||||
publishThrottlingRateInMsg: 0,
|
||||
publishThrottlingRateInByte: 0,
|
||||
},
|
||||
dispatchForm: {
|
||||
dispatchThrottlingRateInMsg: 0,
|
||||
dispatchThrottlingRateInByte: 0,
|
||||
ratePeriodInSecond: 1,
|
||||
},
|
||||
subscribeForm: {
|
||||
subscribeThrottlingRatePerConsumer: 0,
|
||||
ratePeriodInSecond: 1,
|
||||
},
|
||||
backlogForm: {
|
||||
limitSize: -1,
|
||||
limitTime: -1,
|
||||
policy: "producer_request_hold",
|
||||
quotaType: "destination_storage",
|
||||
},
|
||||
retentionForm: {
|
||||
retentionTimeInMinutes: -1,
|
||||
retentionSizeInMb: -1,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function policyFormsFromEffectivePolicies(raw: unknown, fallback: MqPolicyForms): MqPolicyForms {
|
||||
const root = objectRecord(raw);
|
||||
const topicPolicies = objectRecord(root.topicPolicies);
|
||||
const namespacePolicies = objectRecord(root.namespacePolicies);
|
||||
const namespaceSource = Object.keys(namespacePolicies).length ? namespacePolicies : root;
|
||||
|
||||
const publish = firstRecord(topicPolicies.publishRate, root.publishRate, namespaceSource.publishRate, namespaceSource.publishMaxMessageRate);
|
||||
const dispatch = firstRecord(topicPolicies.dispatchRate, root.dispatchRate, namespaceSource.dispatchRate);
|
||||
const subscribe = firstRecord(topicPolicies.subscribeRate, root.subscribeRate, namespaceSource.subscribeRate);
|
||||
const backlog = normalizeBacklogQuota(firstDefined(topicPolicies.backlogQuota, root.backlogQuota, namespaceSource.backlogQuota, namespaceSource.backlogQuotaMap, namespaceSource.backlog_quota_map));
|
||||
const retention = firstRecord(topicPolicies.retention, root.retention, namespaceSource.retention, namespaceSource.retentionPolicies, namespaceSource.retention_policies);
|
||||
|
||||
return {
|
||||
publishForm: publish
|
||||
? {
|
||||
publishThrottlingRateInMsg: numberField(publish.publishThrottlingRateInMsg) ?? fallback.publishForm.publishThrottlingRateInMsg,
|
||||
publishThrottlingRateInByte: numberField(publish.publishThrottlingRateInByte) ?? fallback.publishForm.publishThrottlingRateInByte,
|
||||
}
|
||||
: { ...fallback.publishForm },
|
||||
dispatchForm: dispatch
|
||||
? {
|
||||
dispatchThrottlingRateInMsg: numberField(dispatch.dispatchThrottlingRateInMsg) ?? fallback.dispatchForm.dispatchThrottlingRateInMsg,
|
||||
dispatchThrottlingRateInByte: numberField(dispatch.dispatchThrottlingRateInByte) ?? fallback.dispatchForm.dispatchThrottlingRateInByte,
|
||||
ratePeriodInSecond: numberField(dispatch.ratePeriodInSecond) ?? fallback.dispatchForm.ratePeriodInSecond,
|
||||
}
|
||||
: { ...fallback.dispatchForm },
|
||||
subscribeForm: subscribe
|
||||
? {
|
||||
subscribeThrottlingRatePerConsumer: numberField(subscribe.subscribeThrottlingRatePerConsumer) ?? fallback.subscribeForm.subscribeThrottlingRatePerConsumer,
|
||||
ratePeriodInSecond: numberField(subscribe.ratePeriodInSecond) ?? fallback.subscribeForm.ratePeriodInSecond,
|
||||
}
|
||||
: { ...fallback.subscribeForm },
|
||||
backlogForm: backlog
|
||||
? {
|
||||
limitSize: numberField(backlog.limitSize) ?? numberField(backlog.limit) ?? fallback.backlogForm.limitSize,
|
||||
limitTime: numberField(backlog.limitTime) ?? fallback.backlogForm.limitTime,
|
||||
policy: stringField(backlog.policy) || fallback.backlogForm.policy,
|
||||
quotaType: stringField(backlog.quotaType) || fallback.backlogForm.quotaType,
|
||||
}
|
||||
: { ...fallback.backlogForm },
|
||||
retentionForm: retention
|
||||
? {
|
||||
retentionTimeInMinutes: numberField(retention.retentionTimeInMinutes) ?? fallback.retentionForm.retentionTimeInMinutes,
|
||||
retentionSizeInMb: numberField(retention.retentionSizeInMb) ?? numberField(retention.retentionSizeInMB) ?? fallback.retentionForm.retentionSizeInMb,
|
||||
}
|
||||
: { ...fallback.retentionForm },
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeBacklogQuota(value: unknown): Record<string, unknown> | undefined {
|
||||
const quota = objectRecord(value);
|
||||
if (!Object.keys(quota).length) return undefined;
|
||||
if (hasQuotaFields(quota)) return quota;
|
||||
|
||||
const entries = Object.entries(quota);
|
||||
for (const [quotaType, body] of entries) {
|
||||
const normalized = objectRecord(body);
|
||||
if (hasQuotaFields(normalized)) {
|
||||
return {
|
||||
...normalized,
|
||||
quotaType: normalized.quotaType ?? quotaType,
|
||||
};
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function hasQuotaFields(value: Record<string, unknown>): boolean {
|
||||
return "limit" in value || "limitSize" in value || "limitTime" in value || "policy" in value;
|
||||
}
|
||||
|
||||
function firstRecord(...values: unknown[]): Record<string, unknown> | undefined {
|
||||
for (const value of values) {
|
||||
const record = objectRecord(value);
|
||||
if (Object.keys(record).length) return record;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function firstDefined(...values: unknown[]) {
|
||||
return values.find((value) => value !== undefined && value !== null);
|
||||
}
|
||||
|
||||
function objectRecord(value: unknown): Record<string, unknown> {
|
||||
return value && typeof value === "object" && !Array.isArray(value) ? (value as Record<string, unknown>) : {};
|
||||
}
|
||||
|
||||
function numberField(value: unknown): number | undefined {
|
||||
if (typeof value === "number" && Number.isFinite(value)) return value;
|
||||
if (typeof value === "string" && value.trim() !== "") {
|
||||
const parsed = Number(value);
|
||||
return Number.isFinite(parsed) ? parsed : undefined;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function stringField(value: unknown): string | undefined {
|
||||
return typeof value === "string" ? value : undefined;
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
import type { TenantConfig } from "@/types/mq";
|
||||
|
||||
export function normalizeClusterOptions(clusters: readonly unknown[]): string[] {
|
||||
const seen = new Set<string>();
|
||||
const result: string[] = [];
|
||||
|
||||
for (const cluster of clusters) {
|
||||
if (typeof cluster !== "string") continue;
|
||||
const name = cluster.trim();
|
||||
if (!name || seen.has(name)) continue;
|
||||
seen.add(name);
|
||||
result.push(name);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
export function mqClusterOptionsFromExtra(extra: unknown): string[] {
|
||||
if (!extra || typeof extra !== "object") return [];
|
||||
|
||||
const clusters = (extra as { clusters?: unknown }).clusters;
|
||||
if (!Array.isArray(clusters)) return [];
|
||||
|
||||
return normalizeClusterOptions(clusters);
|
||||
}
|
||||
|
||||
export function defaultTenantConfig(clusterOptions: readonly string[]): TenantConfig {
|
||||
return {
|
||||
adminRoles: [],
|
||||
allowedClusters: normalizeClusterOptions(clusterOptions),
|
||||
};
|
||||
}
|
||||
|
||||
export function validateTenantForm(name: string | undefined, config: TenantConfig): string | undefined {
|
||||
if (!name?.trim()) {
|
||||
return "Tenant name is required";
|
||||
}
|
||||
if (!normalizeClusterOptions(config.allowedClusters).length) {
|
||||
return "Allowed clusters are required";
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
export type MqTokenIssueErrorView =
|
||||
| {
|
||||
kind: "missingSigningKey";
|
||||
title: string;
|
||||
message: string;
|
||||
detail: string;
|
||||
}
|
||||
| {
|
||||
kind: "generic";
|
||||
title?: undefined;
|
||||
message: string;
|
||||
detail?: undefined;
|
||||
};
|
||||
|
||||
export function formatMqTokenIssueError(error: unknown): MqTokenIssueErrorView {
|
||||
const message = error instanceof Error ? error.message : String(error ?? "");
|
||||
if (isMissingTokenSigningConfig(message)) {
|
||||
return {
|
||||
kind: "missingSigningKey",
|
||||
title: "未配置 Token 签发密钥",
|
||||
message: "当前 MQ 连接还没有配置 Broker Token 签发密钥,无法生成客户端 Token。",
|
||||
detail: "请编辑该连接,在 MQ 配置中设置“Broker Token 签发”为 HS256 SECRET 或 RS256 PRIVATE,并填写签发密钥后再生成。",
|
||||
};
|
||||
}
|
||||
return { kind: "generic", message };
|
||||
}
|
||||
|
||||
function isMissingTokenSigningConfig(message: string) {
|
||||
const normalized = message.toLowerCase();
|
||||
return normalized.includes("token signing is not configured") || normalized.includes("token signing key is required") || normalized.includes("broker token signing key is required");
|
||||
}
|
||||
Reference in new issue
Block a user