@shiva004/surveys 0.8.0 → 0.10.0
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.
- package/README.md +52 -0
- package/dist/App.d.ts +1 -0
- package/dist/elements.d.ts +33 -0
- package/dist/elements.js +20190 -0
- package/dist/errortracking/ErrorTrackingApp.d.ts +7 -0
- package/dist/errortracking/data.d.ts +112 -0
- package/dist/events/data.d.ts +2 -0
- package/dist/flags/controls.d.ts +13 -0
- package/dist/flags/data.d.ts +33 -4
- package/dist/flags/icons.d.ts +40 -0
- package/dist/flags/targeting-ui.d.ts +16 -0
- package/dist/flags/targeting.d.ts +93 -0
- package/dist/index.d.ts +40 -1
- package/dist/index.js +9913 -5608
- package/dist/livelogs/LiveLogsApp.d.ts +7 -0
- package/dist/livelogs/data.d.ts +47 -0
- package/dist/mockapi/MockApiApp.d.ts +7 -0
- package/dist/mockapi/data.d.ts +116 -0
- package/dist/notifications/NotificationsApp.d.ts +10 -0
- package/dist/notifications/data.d.ts +112 -0
- package/dist/queues/QueuesApp.d.ts +9 -0
- package/dist/queues/charts.d.ts +12 -0
- package/dist/queues/data.d.ts +136 -0
- package/dist/ratelimit/RateLimitApp.d.ts +8 -0
- package/dist/ratelimit/data.d.ts +147 -0
- package/dist/ratelimit/limiter.d.ts +29 -0
- package/dist/recorder/capture.d.ts +30 -0
- package/dist/recorder/db.d.ts +6 -0
- package/dist/recorder/recorder.d.ts +45 -0
- package/dist/releases/ReleasesApp.d.ts +10 -0
- package/dist/releases/data.d.ts +107 -0
- package/dist/sessions/SessionsApp.d.ts +10 -0
- package/dist/sessions/data.d.ts +64 -0
- package/dist/store.d.ts +43 -0
- package/dist/style.css +1 -1
- package/dist/ui.d.ts +41 -0
- package/package.json +20 -10
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { type Level } from '../logs/data';
|
|
2
|
+
import { type LinkStatus } from '../store';
|
|
3
|
+
export type LiveLevel = Level;
|
|
4
|
+
export declare const LIVE_LEVELS: Level[];
|
|
5
|
+
export interface LiveLog {
|
|
6
|
+
id: number;
|
|
7
|
+
ts: number;
|
|
8
|
+
level: LiveLevel;
|
|
9
|
+
service: string;
|
|
10
|
+
message: string;
|
|
11
|
+
http?: number;
|
|
12
|
+
durationMs?: number;
|
|
13
|
+
traceId?: string;
|
|
14
|
+
stack?: string; /** Other fields the backend sent: ip, user, spanId, apiUrl, license, appVersion, browser, os ... */
|
|
15
|
+
extra?: Record<string, string>;
|
|
16
|
+
}
|
|
17
|
+
export type LiveLogInput = Omit<LiveLog, 'id'>;
|
|
18
|
+
export interface LiveLogsConfig {
|
|
19
|
+
environment?: string; /** Lines kept in memory. Default 500. */
|
|
20
|
+
max?: number;
|
|
21
|
+
}
|
|
22
|
+
export declare const configureLiveLogs: (c?: LiveLogsConfig) => void;
|
|
23
|
+
export declare const getLiveEnvironment: (override?: string) => string;
|
|
24
|
+
export declare const useLiveLogs: () => {
|
|
25
|
+
logs: LiveLog[];
|
|
26
|
+
paused: boolean;
|
|
27
|
+
held: number;
|
|
28
|
+
link: LinkStatus;
|
|
29
|
+
};
|
|
30
|
+
/** Add one line (or several, oldest first) from your own source. */
|
|
31
|
+
export declare const pushLiveLog: (l: LiveLogInput | LiveLogInput[]) => void;
|
|
32
|
+
export declare const setLivePaused: (paused: boolean) => void;
|
|
33
|
+
export declare const clearLiveLogs: () => void;
|
|
34
|
+
/** Feed Live Logs from this app: logger lines (with history) and fetch() calls with their status and duration. Safe to call twice. */
|
|
35
|
+
export declare const startLocalLiveLogs: () => (() => void);
|
|
36
|
+
/** Turn one JSON line from a backend into a LiveLog. Accepts the usual field names (msg/message, severity/level, status/http, trace_id/traceId...). */
|
|
37
|
+
export declare const toLiveLog: (raw: unknown) => LiveLogInput | null;
|
|
38
|
+
export interface LiveLogsConnection {
|
|
39
|
+
/** ws:// or wss:// = WebSocket. http(s):// = Server-Sent Events, or polling when `poll` is set. */
|
|
40
|
+
url: string;
|
|
41
|
+
poll?: boolean;
|
|
42
|
+
pollMs?: number;
|
|
43
|
+
/** Used for polling only (browsers cannot set headers on WebSocket / EventSource). */
|
|
44
|
+
headers?: Record<string, string>;
|
|
45
|
+
}
|
|
46
|
+
/** Stream real log lines from your backend. Returns a function that disconnects. */
|
|
47
|
+
export declare const connectLiveLogs: (o: LiveLogsConnection) => (() => void);
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { type LinkStatus } from '../store';
|
|
2
|
+
export type Method = 'ANY' | 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
3
|
+
export declare const METHODS: Method[];
|
|
4
|
+
export interface Mock {
|
|
5
|
+
id: string;
|
|
6
|
+
name: string;
|
|
7
|
+
enabled: boolean;
|
|
8
|
+
method: Method;
|
|
9
|
+
/** `/api/users/:id`, `/api/*`, or a full URL (`https://api.example.com/users`). Query strings are ignored. */
|
|
10
|
+
path: string;
|
|
11
|
+
status: number;
|
|
12
|
+
/** Fixed wait before answering, plus a random 0..jitterMs. */
|
|
13
|
+
delayMs: number;
|
|
14
|
+
jitterMs: number;
|
|
15
|
+
headers: Record<string, string>;
|
|
16
|
+
/** Response body. `{{params.id}}`, `{{query.q}}`, `{{body.email}}`, `{{now}}`, `{{uuid}}` and `{{count}}` are filled in. */
|
|
17
|
+
body: string;
|
|
18
|
+
/** Share of calls (0-100) that fail with `failStatus` instead of the normal answer. */
|
|
19
|
+
failRate: number;
|
|
20
|
+
failStatus: number;
|
|
21
|
+
hits: number;
|
|
22
|
+
lastHit?: number;
|
|
23
|
+
/** True for mocks loaded from your backend (read-only here). */
|
|
24
|
+
remote?: boolean;
|
|
25
|
+
}
|
|
26
|
+
export type MockInput = Partial<Omit<Mock, 'id' | 'hits' | 'lastHit' | 'remote'>> & {
|
|
27
|
+
path: string;
|
|
28
|
+
};
|
|
29
|
+
export interface MockHit {
|
|
30
|
+
id: string;
|
|
31
|
+
ts: number;
|
|
32
|
+
method: string;
|
|
33
|
+
/** Path and query of the call. */
|
|
34
|
+
url: string;
|
|
35
|
+
mockId: string;
|
|
36
|
+
mockName: string;
|
|
37
|
+
status: number;
|
|
38
|
+
ms: number;
|
|
39
|
+
/** The answer was a failure injected by `failRate`. */
|
|
40
|
+
injected: boolean;
|
|
41
|
+
requestBody?: string;
|
|
42
|
+
responsePreview: string;
|
|
43
|
+
}
|
|
44
|
+
export interface MockApiConfig {
|
|
45
|
+
environment?: string;
|
|
46
|
+
/** Calls whose URL contains / matches are never mocked. */
|
|
47
|
+
ignoreUrls?: Array<string | RegExp>;
|
|
48
|
+
/** Mock calls kept in the log. Default 200. */
|
|
49
|
+
max?: number;
|
|
50
|
+
/** localStorage keys prefix. Default "mk". */
|
|
51
|
+
storageKey?: string;
|
|
52
|
+
/** Called for every mocked call. */
|
|
53
|
+
onHit?: (h: MockHit) => void;
|
|
54
|
+
}
|
|
55
|
+
export declare const useMockApi: () => {
|
|
56
|
+
mocks: Mock[];
|
|
57
|
+
hits: MockHit[];
|
|
58
|
+
enabled: boolean;
|
|
59
|
+
link: LinkStatus;
|
|
60
|
+
};
|
|
61
|
+
export declare const getMocks: () => Mock[];
|
|
62
|
+
export declare const getMock: (id: string) => Mock | undefined;
|
|
63
|
+
export declare const getMockHits: () => MockHit[];
|
|
64
|
+
export declare const getMockEnvironment: (override?: string) => string;
|
|
65
|
+
export declare const configureMockApi: (c?: MockApiConfig) => void;
|
|
66
|
+
/** Build a complete Mock from loose input. Missing fields get safe defaults. */
|
|
67
|
+
export declare const normalizeMock: (m: MockInput & {
|
|
68
|
+
id?: string;
|
|
69
|
+
}) => Mock;
|
|
70
|
+
export declare const createMock: (i: MockInput) => Mock | null;
|
|
71
|
+
export declare const updateMock: (id: string, patch: Partial<MockInput>) => void;
|
|
72
|
+
export declare const toggleMock: (id: string) => void;
|
|
73
|
+
export declare const deleteMock: (id: string) => void;
|
|
74
|
+
export declare const duplicateMock: (id: string) => Mock | null;
|
|
75
|
+
/** Master switch: off = every call goes to the network, mocks stay defined. */
|
|
76
|
+
export declare const setMockApiEnabled: (enabled: boolean) => void;
|
|
77
|
+
export declare const clearMockHits: () => void;
|
|
78
|
+
export declare const clearMocks: () => void;
|
|
79
|
+
/** Mocks as JSON text, to share or keep in your repo. Backend mocks are left out. */
|
|
80
|
+
export declare const exportMocks: () => string;
|
|
81
|
+
/** Add mocks from JSON text or an array. Returns how many were added; throws on invalid JSON. */
|
|
82
|
+
export declare const importMocks: (input: string | unknown[]) => number;
|
|
83
|
+
export interface Matched {
|
|
84
|
+
mock: Mock;
|
|
85
|
+
params: Record<string, string>;
|
|
86
|
+
query: Record<string, string>;
|
|
87
|
+
path: string;
|
|
88
|
+
}
|
|
89
|
+
/** The first enabled mock that answers this call (method + URL), with the path parameters it captured. */
|
|
90
|
+
export declare const matchMock: (url: string, method: string, mocks?: Mock[]) => Matched | null;
|
|
91
|
+
export declare const pathOf: (url: string) => string;
|
|
92
|
+
/** Fill `{{...}}` placeholders. Unknown ones become an empty string. */
|
|
93
|
+
export declare const fillTemplate: (tpl: string, ctx: {
|
|
94
|
+
params: Record<string, string>;
|
|
95
|
+
query: Record<string, string>;
|
|
96
|
+
body: unknown;
|
|
97
|
+
count: number;
|
|
98
|
+
}) => string;
|
|
99
|
+
/** Answer one call with a mock, or return null when none matches (the caller then uses the network). Exposed so tests and non-fetch clients (axios adapters) can use it. */
|
|
100
|
+
export declare const respondWithMock: (url: string, method: string, opts?: {
|
|
101
|
+
body?: string;
|
|
102
|
+
}) => Promise<Response | null>;
|
|
103
|
+
/** Answer matching fetch() calls with their mock. Call it before startEventCapture(), so Live Events records the mocked answers as api_request. Safe to call twice. Returns a function that turns it off. */
|
|
104
|
+
export declare const startMockApi: () => (() => void);
|
|
105
|
+
/** Turn one mock definition from a backend into a read-only Mock. Accepts the usual names (path/url/route, status/statusCode, delay/latency, response/json ...). */
|
|
106
|
+
export declare const toRemoteMock: (raw: unknown) => Mock | null;
|
|
107
|
+
export interface MockApiConnection {
|
|
108
|
+
/** JSON endpoint: an array of mocks, or `{ "mocks": [] }`. */
|
|
109
|
+
url: string;
|
|
110
|
+
pollMs?: number;
|
|
111
|
+
headers?: Record<string, string>;
|
|
112
|
+
}
|
|
113
|
+
/** Load mock definitions from your backend every few seconds (read-only, listed before your own). Returns a function that disconnects. */
|
|
114
|
+
export declare const connectMockApi: (o: MockApiConnection) => (() => void);
|
|
115
|
+
/** Pull the backend now (the Refresh button). */
|
|
116
|
+
export declare const refreshMockApi: () => void | undefined;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import '../logs/logs.css';
|
|
3
|
+
import './notifications.css';
|
|
4
|
+
export interface NotificationsAppProps {
|
|
5
|
+
environment?: string;
|
|
6
|
+
/** Called by the "Open Live Events" / "Open Error Tracking" links in the details panel. */
|
|
7
|
+
onOpenEvents?: () => void;
|
|
8
|
+
onOpenErrors?: () => void;
|
|
9
|
+
}
|
|
10
|
+
export declare const NotificationsApp: React.FC<NotificationsAppProps>;
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { type LinkStatus } from '../store';
|
|
2
|
+
export type Severity = 'critical' | 'warning' | 'info';
|
|
3
|
+
export declare const SEVERITIES: Severity[];
|
|
4
|
+
export type RuleKind = 'event' | 'log';
|
|
5
|
+
export interface Sample {
|
|
6
|
+
ts: number;
|
|
7
|
+
name: string;
|
|
8
|
+
text: string;
|
|
9
|
+
page?: string;
|
|
10
|
+
traceId?: string;
|
|
11
|
+
}
|
|
12
|
+
export interface Notification {
|
|
13
|
+
id: string;
|
|
14
|
+
ts: number;
|
|
15
|
+
severity: Severity;
|
|
16
|
+
title: string;
|
|
17
|
+
message: string;
|
|
18
|
+
/** Rule name, "app" for notify(), or what your backend says. */
|
|
19
|
+
source: string;
|
|
20
|
+
read: boolean;
|
|
21
|
+
ruleId?: string;
|
|
22
|
+
/** Events or log lines that caused it (rules only), newest first. */
|
|
23
|
+
samples: Sample[];
|
|
24
|
+
/** How many matches the rule had counted when it fired. */
|
|
25
|
+
count: number;
|
|
26
|
+
link?: string;
|
|
27
|
+
remote?: boolean;
|
|
28
|
+
}
|
|
29
|
+
export interface NotifyInput {
|
|
30
|
+
title: string;
|
|
31
|
+
message?: string;
|
|
32
|
+
severity?: Severity;
|
|
33
|
+
source?: string;
|
|
34
|
+
link?: string;
|
|
35
|
+
ts?: number;
|
|
36
|
+
}
|
|
37
|
+
export interface Rule {
|
|
38
|
+
id: string;
|
|
39
|
+
name: string;
|
|
40
|
+
enabled: boolean;
|
|
41
|
+
kind: RuleKind;
|
|
42
|
+
/** event: event names, comma separated, `*` is a wildcard (js_error, log_*, release_*).
|
|
43
|
+
* Besides the real names, `api_request_failed` matches fetch calls that errored or answered 5xx, and `api_request_4xx` those that answered 4xx.
|
|
44
|
+
* log: the level to watch (ERROR, WARN, INFO or DEBUG). */
|
|
45
|
+
match: string;
|
|
46
|
+
/** Only when the event properties / log message contain this text (case-insensitive). */
|
|
47
|
+
contains?: string;
|
|
48
|
+
/** Fire after this many matches inside `windowSec`. */
|
|
49
|
+
threshold: number;
|
|
50
|
+
windowSec: number;
|
|
51
|
+
/** After firing, stay quiet for this long. */
|
|
52
|
+
cooldownSec: number;
|
|
53
|
+
severity: Severity;
|
|
54
|
+
}
|
|
55
|
+
export type RuleInput = Omit<Rule, 'id' | 'enabled'> & {
|
|
56
|
+
enabled?: boolean;
|
|
57
|
+
};
|
|
58
|
+
export interface NotificationsConfig {
|
|
59
|
+
environment?: string;
|
|
60
|
+
/** Notifications kept in memory. Default 200. */
|
|
61
|
+
max?: number;
|
|
62
|
+
/** localStorage keys. Defaults "nt_items_v1" and "nt_rules_v1". */
|
|
63
|
+
storageKey?: string;
|
|
64
|
+
/** Rules used when nothing is saved yet. Default: DEFAULT_RULES. */
|
|
65
|
+
defaultRules?: RuleInput[];
|
|
66
|
+
/** Called for every new notification: send it to Slack, play a sound ... */
|
|
67
|
+
onNotify?: (n: Notification) => void;
|
|
68
|
+
}
|
|
69
|
+
export declare const DEFAULT_RULES: RuleInput[];
|
|
70
|
+
export declare const useNotifications: () => {
|
|
71
|
+
items: Notification[];
|
|
72
|
+
rules: Rule[];
|
|
73
|
+
desktop: boolean;
|
|
74
|
+
link: LinkStatus;
|
|
75
|
+
};
|
|
76
|
+
export declare const getNotifications: () => Notification[];
|
|
77
|
+
export declare const getRules: () => Rule[];
|
|
78
|
+
export declare const getNotificationsEnvironment: (override?: string) => string;
|
|
79
|
+
export declare const unreadCount: (items?: Notification[]) => number;
|
|
80
|
+
/** Set once at app start. Every field is optional. */
|
|
81
|
+
export declare const configureNotifications: (c?: NotificationsConfig) => void;
|
|
82
|
+
/** Tell the user something. Shown in the inbox (and as a desktop notification when enabled). */
|
|
83
|
+
export declare const notify: (i: NotifyInput) => Notification | null;
|
|
84
|
+
export declare const createRule: (r: RuleInput) => Rule;
|
|
85
|
+
export declare const updateRule: (id: string, patch: Partial<RuleInput>) => void;
|
|
86
|
+
export declare const toggleRule: (id: string) => void;
|
|
87
|
+
export declare const deleteRule: (id: string) => void;
|
|
88
|
+
export declare const resetRules: () => void;
|
|
89
|
+
/** Start watching this app's events and logger lines. Lines that already exist are not replayed. Safe to call twice. */
|
|
90
|
+
export declare const startLocalNotifications: () => (() => void);
|
|
91
|
+
export declare const markNotificationRead: (id: string, read?: boolean) => void;
|
|
92
|
+
export declare const markAllRead: () => void;
|
|
93
|
+
export declare const deleteNotification: (id: string) => void;
|
|
94
|
+
/** Remove the notifications raised in this browser. Ones from your backend stay. */
|
|
95
|
+
export declare const clearNotifications: () => void;
|
|
96
|
+
export declare const desktopSupported: () => boolean;
|
|
97
|
+
/** Turn browser notifications on or off. Asks for permission the first time. Resolves to whether they are now on. */
|
|
98
|
+
export declare const setDesktopNotifications: (on: boolean) => Promise<boolean>;
|
|
99
|
+
/** Turn one notification from a backend into a Notification. Accepts the usual names (title/subject, message/body, severity/level, created_at ...). */
|
|
100
|
+
export declare const toRemoteNotification: (raw: unknown) => Notification | null;
|
|
101
|
+
export interface NotificationsConnection {
|
|
102
|
+
/** JSON endpoint: an array of notifications, or `{ "notifications": [] }`. */
|
|
103
|
+
url: string;
|
|
104
|
+
pollMs?: number;
|
|
105
|
+
headers?: Record<string, string>;
|
|
106
|
+
/** How "read" reaches your backend. PATCH sends `{ read: true }` to `url/<id>`; PUT and POST send `{ id, read: true }` to `url`. Default "none": read state is kept in this browser only. */
|
|
107
|
+
readMethod?: 'PATCH' | 'PUT' | 'POST' | 'none';
|
|
108
|
+
}
|
|
109
|
+
/** Load notifications from your backend every few seconds. New ones raise a desktop notification when enabled. Returns a function that disconnects. */
|
|
110
|
+
export declare const connectNotifications: (o: NotificationsConnection) => (() => void);
|
|
111
|
+
/** Pull the backend now (the Refresh button). */
|
|
112
|
+
export declare const refreshNotifications: () => void | undefined;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import '../logs/logs.css';
|
|
3
|
+
import './queues.css';
|
|
4
|
+
export interface QueuesAppProps {
|
|
5
|
+
/** Shown under the title. Defaults to the configureQueues() environment. */
|
|
6
|
+
environment?: string;
|
|
7
|
+
}
|
|
8
|
+
/** Queue viewer. Same layout as the Log Viewer and Heatmap: queues | charts and messages | details. */
|
|
9
|
+
export declare const QueuesApp: React.FC<QueuesAppProps>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { type QStatus, type Queue } from './data';
|
|
3
|
+
export declare const STATUS_COLOR: Record<QStatus, string>;
|
|
4
|
+
export declare const STATUSES: QStatus[];
|
|
5
|
+
/** Stacked bars: waiting messages per minute (last 60), split by each queue's current status. */
|
|
6
|
+
export declare const BacklogChart: React.FC<{
|
|
7
|
+
queues: Queue[];
|
|
8
|
+
}>;
|
|
9
|
+
/** Ingress vs egress (messages per second) for the queues passed in. */
|
|
10
|
+
export declare const ThroughputChart: React.FC<{
|
|
11
|
+
queues: Queue[];
|
|
12
|
+
}>;
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { type LinkStatus } from '../store';
|
|
2
|
+
export type QStatus = 'backlogged' | 'degraded' | 'healthy' | 'paused';
|
|
3
|
+
export type QKind = 'standard' | 'topic' | 'group-topic';
|
|
4
|
+
export declare const QUEUE_KINDS: QKind[];
|
|
5
|
+
export declare const POINTS = 60;
|
|
6
|
+
export declare const DEFAULT_CAPACITY = 10000;
|
|
7
|
+
export interface Queue {
|
|
8
|
+
name: string;
|
|
9
|
+
kind: QKind;
|
|
10
|
+
/** Configured capacity (messages). Depth / capacity drives the status. */
|
|
11
|
+
capacity: number;
|
|
12
|
+
depth: number;
|
|
13
|
+
inflight: number;
|
|
14
|
+
/** Messages per second in / out. */
|
|
15
|
+
ingress: number;
|
|
16
|
+
egress: number;
|
|
17
|
+
consumers: number;
|
|
18
|
+
/** 95th percentile processing time (ms). */
|
|
19
|
+
p95ms: number;
|
|
20
|
+
/** Age of the oldest waiting message (seconds). */
|
|
21
|
+
oldestSec: number;
|
|
22
|
+
paused: boolean;
|
|
23
|
+
/** Dead-letter queue name, if any. */
|
|
24
|
+
dlq: string | null;
|
|
25
|
+
/** topic / group-topic only */
|
|
26
|
+
topic?: string;
|
|
27
|
+
group?: string;
|
|
28
|
+
partitions?: number;
|
|
29
|
+
createdAt: number;
|
|
30
|
+
/** Last 60 minutes, oldest first. */
|
|
31
|
+
series: {
|
|
32
|
+
depth: number[];
|
|
33
|
+
ingress: number[];
|
|
34
|
+
egress: number[];
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
export interface Msg {
|
|
38
|
+
id: string;
|
|
39
|
+
queue: string;
|
|
40
|
+
body: string;
|
|
41
|
+
ageSec: number;
|
|
42
|
+
attempts: number;
|
|
43
|
+
sizeB: number;
|
|
44
|
+
error?: string;
|
|
45
|
+
consumer?: string;
|
|
46
|
+
}
|
|
47
|
+
export interface Consumer {
|
|
48
|
+
id: string;
|
|
49
|
+
queue: string;
|
|
50
|
+
host: string;
|
|
51
|
+
state: 'active' | 'idle' | 'stalled';
|
|
52
|
+
prefetch: number;
|
|
53
|
+
inflight: number;
|
|
54
|
+
rate: number;
|
|
55
|
+
lastAckSec: number;
|
|
56
|
+
}
|
|
57
|
+
export interface Exchange {
|
|
58
|
+
name: string;
|
|
59
|
+
type: 'direct' | 'topic' | 'fanout';
|
|
60
|
+
bindings: {
|
|
61
|
+
key: string;
|
|
62
|
+
queue: string;
|
|
63
|
+
}[];
|
|
64
|
+
}
|
|
65
|
+
export interface QueuesState {
|
|
66
|
+
queues: Queue[]; /** Messages waiting (a peek), any queue. */
|
|
67
|
+
waiting: Msg[];
|
|
68
|
+
dead: Msg[];
|
|
69
|
+
poison: Msg[];
|
|
70
|
+
consumers: Consumer[];
|
|
71
|
+
exchanges: Exchange[];
|
|
72
|
+
}
|
|
73
|
+
export type QueuesView = QueuesState & {
|
|
74
|
+
link: LinkStatus;
|
|
75
|
+
};
|
|
76
|
+
export type QueueAction = {
|
|
77
|
+
type: 'create';
|
|
78
|
+
queue: Queue;
|
|
79
|
+
} | {
|
|
80
|
+
type: 'pause' | 'resume' | 'purge' | 'delete';
|
|
81
|
+
name: string;
|
|
82
|
+
} | {
|
|
83
|
+
type: 'redrive' | 'deleteDead' | 'quarantine' | 'release' | 'dropPoison';
|
|
84
|
+
ids: string[];
|
|
85
|
+
} | {
|
|
86
|
+
type: 'restartConsumer';
|
|
87
|
+
id: string;
|
|
88
|
+
};
|
|
89
|
+
export interface QueuesConfig {
|
|
90
|
+
/** Shown under the title. Defaults to the host the app runs on, or VITE_ENV_NAME. */
|
|
91
|
+
environment?: string;
|
|
92
|
+
initialQueues?: Queue[];
|
|
93
|
+
/** Fires after every user action with what happened: call your broker here. */
|
|
94
|
+
onAction?: (a: QueueAction) => void;
|
|
95
|
+
/** Fires after every user action with the whole new state. */
|
|
96
|
+
onChange?: (s: QueuesState) => void;
|
|
97
|
+
}
|
|
98
|
+
export declare const statusOf: (q: Queue) => QStatus;
|
|
99
|
+
export declare const configureQueues: (c?: QueuesConfig) => void;
|
|
100
|
+
export declare const getEnvironment: (override?: string) => string;
|
|
101
|
+
export declare const useQueues: () => QueuesView;
|
|
102
|
+
/** Replace what the view shows (call this from your own poller). Fields you leave out are kept. */
|
|
103
|
+
export declare const setQueues: (queues: Queue[], extra?: Partial<Omit<QueuesState, "queues">>) => void;
|
|
104
|
+
export declare const NAME_RE: RegExp;
|
|
105
|
+
export interface NewQueue {
|
|
106
|
+
name: string;
|
|
107
|
+
kind: QKind;
|
|
108
|
+
capacity: number;
|
|
109
|
+
topic?: string;
|
|
110
|
+
group?: string;
|
|
111
|
+
partitions?: number;
|
|
112
|
+
dlq: boolean;
|
|
113
|
+
}
|
|
114
|
+
/** Returns an error message, or null on success. */
|
|
115
|
+
export declare const createQueue: (n: NewQueue) => string | null;
|
|
116
|
+
export declare const pauseQueue: (name: string) => void;
|
|
117
|
+
export declare const resumeQueue: (name: string) => void;
|
|
118
|
+
export declare const purgeQueue: (name: string) => void;
|
|
119
|
+
export declare const deleteQueue: (name: string) => void;
|
|
120
|
+
/** Send dead-lettered messages back to their source queue. */
|
|
121
|
+
export declare const redriveDead: (ids: string[]) => void;
|
|
122
|
+
export declare const deleteDead: (ids: string[]) => void;
|
|
123
|
+
export declare const quarantineToPoison: (ids: string[]) => void;
|
|
124
|
+
export declare const releasePoison: (ids: string[]) => void;
|
|
125
|
+
export declare const dropPoison: (ids: string[]) => void;
|
|
126
|
+
export declare const restartConsumer: (id: string) => void;
|
|
127
|
+
/** Turn a backend response into queue state. Accepts an array of queues, or { queues, waiting, dead, poison, consumers, exchanges }. */
|
|
128
|
+
export declare const toQueuesState: (body: unknown) => QueuesState;
|
|
129
|
+
export interface QueuesConnection {
|
|
130
|
+
url: string;
|
|
131
|
+
headers?: Record<string, string>;
|
|
132
|
+
pollMs?: number;
|
|
133
|
+
}
|
|
134
|
+
/** Load real queues from your backend now and every pollMs (default 5 s). Returns a function that disconnects. */
|
|
135
|
+
export declare const connectQueues: (o: QueuesConnection) => (() => void);
|
|
136
|
+
export declare const compact: (n: number, d?: number) => string;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import '../logs/logs.css';
|
|
3
|
+
import './ratelimit.css';
|
|
4
|
+
export interface RateLimitAppProps {
|
|
5
|
+
environment?: string;
|
|
6
|
+
}
|
|
7
|
+
/** Rate limit admin: Rules Overview (KPI tiles, filters, rules table, details) and Violation Logs. */
|
|
8
|
+
export declare const RateLimitApp: React.FC<RateLimitAppProps>;
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { type LinkStatus } from '../store';
|
|
2
|
+
import { STRATEGIES, type Decision, type Strategy } from './limiter';
|
|
3
|
+
export { STRATEGIES };
|
|
4
|
+
export type { Strategy };
|
|
5
|
+
export type RuleAction = 'block' | 'throttle' | 'log';
|
|
6
|
+
export type RuleScope = 'global' | 'user' | 'ip' | 'api_key' | 'endpoint';
|
|
7
|
+
export type RuleEnv = 'production' | 'staging' | 'development';
|
|
8
|
+
export declare const RULE_ACTIONS: RuleAction[];
|
|
9
|
+
export declare const RULE_SCOPES: RuleScope[];
|
|
10
|
+
export declare const RULE_ENVS: RuleEnv[];
|
|
11
|
+
export declare const METHODS: readonly ["ANY", "GET", "POST", "PUT", "PATCH", "DELETE"];
|
|
12
|
+
/** Display name of an action / scope / env / strategy value. */
|
|
13
|
+
export declare const label: (v: string) => string;
|
|
14
|
+
/** 60 -> "per 1m", 3600 -> "per 1h". */
|
|
15
|
+
export declare const fmtWindow: (s: number) => string;
|
|
16
|
+
export interface Rule {
|
|
17
|
+
id: string;
|
|
18
|
+
name: string;
|
|
19
|
+
policy: string;
|
|
20
|
+
/** URL path to match: "*" (everything), "/api/*" (glob) or "/login" (path contains it). */
|
|
21
|
+
match: string;
|
|
22
|
+
method: string;
|
|
23
|
+
scope: RuleScope;
|
|
24
|
+
limit: number;
|
|
25
|
+
windowSec: number;
|
|
26
|
+
action: RuleAction;
|
|
27
|
+
strategy: Strategy;
|
|
28
|
+
env: RuleEnv;
|
|
29
|
+
enabled: boolean;
|
|
30
|
+
violations: number;
|
|
31
|
+
createdAt: number;
|
|
32
|
+
}
|
|
33
|
+
export type RuleInput = Partial<Omit<Rule, 'id' | 'violations' | 'createdAt'>> & {
|
|
34
|
+
name: string;
|
|
35
|
+
};
|
|
36
|
+
export interface Violation {
|
|
37
|
+
id: string;
|
|
38
|
+
ts: number;
|
|
39
|
+
ruleId: string;
|
|
40
|
+
rule: string;
|
|
41
|
+
key: string;
|
|
42
|
+
scope: RuleScope | 'backend';
|
|
43
|
+
method: string;
|
|
44
|
+
url: string;
|
|
45
|
+
action: RuleAction;
|
|
46
|
+
/** "local" = this app's own limiter, "backend" = your API answered 429. */
|
|
47
|
+
source: 'local' | 'backend';
|
|
48
|
+
status?: number;
|
|
49
|
+
retryAfterSec?: number;
|
|
50
|
+
/** Set when your backend sends them (see server.ts). */
|
|
51
|
+
traceId?: string;
|
|
52
|
+
spanId?: string;
|
|
53
|
+
ip?: string;
|
|
54
|
+
user?: string;
|
|
55
|
+
durationMs?: number;
|
|
56
|
+
}
|
|
57
|
+
export interface RateLimitConfig {
|
|
58
|
+
/** Name used for the storage key. Default "default". */
|
|
59
|
+
project?: string;
|
|
60
|
+
/** Which rules apply here. Default: "development" on localhost, otherwise "production". */
|
|
61
|
+
environment?: RuleEnv;
|
|
62
|
+
/** localStorage key. Default "rl_<project>_v1". */
|
|
63
|
+
storageKey?: string;
|
|
64
|
+
/** Sent as the API-key scope key when a request has no Authorization / X-API-Key header. */
|
|
65
|
+
apiKey?: string;
|
|
66
|
+
/** Used by the "ip" scope (browsers do not know their own IP). Default "this-client". */
|
|
67
|
+
clientIp?: string;
|
|
68
|
+
/** fetch() URLs containing / matching these are never limited. */
|
|
69
|
+
ignoreUrls?: (string | RegExp)[];
|
|
70
|
+
/** Violations kept in memory. Default 500. */
|
|
71
|
+
max?: number;
|
|
72
|
+
/** Called after every change to the rules, e.g. to save them to your backend. */
|
|
73
|
+
onChange?: (rules: Rule[]) => void;
|
|
74
|
+
/** Called for every violation (local or from your backend). */
|
|
75
|
+
onViolation?: (v: Violation) => void;
|
|
76
|
+
}
|
|
77
|
+
export declare const useRateLimit: () => {
|
|
78
|
+
rules: Rule[];
|
|
79
|
+
violations: Violation[];
|
|
80
|
+
link: LinkStatus;
|
|
81
|
+
};
|
|
82
|
+
export declare const getRules: () => Rule[];
|
|
83
|
+
export declare const getViolations: () => Violation[];
|
|
84
|
+
export declare const getRateLimitEnvironment: (override?: string) => string;
|
|
85
|
+
/** Set once at app start. Every field is optional. */
|
|
86
|
+
export declare const configureRateLimit: (c?: RateLimitConfig) => void;
|
|
87
|
+
/** 60, "60", "30s", "1m", "2h", "1d" -> seconds. */
|
|
88
|
+
export declare const parseWindow: (v: unknown, fallback?: number) => number;
|
|
89
|
+
export declare const toRule: (raw: unknown) => Rule | null;
|
|
90
|
+
export declare const toViolation: (raw: unknown) => Violation | null;
|
|
91
|
+
/** Add a rule. Returns an error message, or null on success. */
|
|
92
|
+
export declare const createRule: (i: RuleInput) => string | null;
|
|
93
|
+
export declare const toggleRule: (id: string) => void;
|
|
94
|
+
export declare const deleteRule: (id: string) => void;
|
|
95
|
+
export declare const setRules: (rules: Rule[]) => void;
|
|
96
|
+
export declare const clearViolations: () => void;
|
|
97
|
+
export interface CheckInput {
|
|
98
|
+
url?: string;
|
|
99
|
+
method?: string;
|
|
100
|
+
headers?: HeadersInit;
|
|
101
|
+
/** Check one rule by name or id instead of matching by URL (used by withRateLimit and the Test button). */
|
|
102
|
+
rule?: string;
|
|
103
|
+
}
|
|
104
|
+
export interface CheckResult {
|
|
105
|
+
/** false = a "block" rule is over its limit. */
|
|
106
|
+
allowed: boolean;
|
|
107
|
+
/** "throttle" rules: how long to wait before going ahead. */
|
|
108
|
+
delayMs: number;
|
|
109
|
+
/** When not allowed: how long until the next request would pass. */
|
|
110
|
+
retryAfterMs: number;
|
|
111
|
+
rule?: Rule;
|
|
112
|
+
decision?: Decision;
|
|
113
|
+
}
|
|
114
|
+
/** Count one request against every enabled rule that applies and say what to do. Never throws. */
|
|
115
|
+
export declare const checkRateLimit: (i?: CheckInput) => CheckResult;
|
|
116
|
+
/** Run one rule's check from the admin page. */
|
|
117
|
+
export declare const testRule: (id: string) => string;
|
|
118
|
+
export declare class RateLimitError extends Error {
|
|
119
|
+
retryAfterMs: number;
|
|
120
|
+
rule: string;
|
|
121
|
+
constructor(rule: string, retryAfterMs: number);
|
|
122
|
+
}
|
|
123
|
+
/** Wrap any function with a named rule. Blocked calls throw RateLimitError; throttled calls wait. */
|
|
124
|
+
export declare const withRateLimit: <A extends unknown[], R>(rule: string, fn: (...a: A) => R | Promise<R>) => (...a: A) => Promise<R>;
|
|
125
|
+
/** Record that your backend rate limited a request (HTTP 429). Done for you on fetch(); call it yourself from axios / XHR code. */
|
|
126
|
+
export declare const reportBackendLimit: (i: {
|
|
127
|
+
url: string;
|
|
128
|
+
method?: string;
|
|
129
|
+
retryAfterSec?: number;
|
|
130
|
+
status?: number;
|
|
131
|
+
headers?: Headers;
|
|
132
|
+
}) => Violation;
|
|
133
|
+
/** Enforce the rules on this app's fetch() calls and catch 429 answers. Safe to call twice. Returns a function that turns it off.
|
|
134
|
+
A blocked request is answered with a 429 Response (Retry-After set) without touching the network, like a real server would. */
|
|
135
|
+
export declare const startRateLimit: () => (() => void);
|
|
136
|
+
export interface RateLimitConnection {
|
|
137
|
+
/** GET returns `[Rule, ...]` or `{ rules: [...], violations?: [...] }`. */
|
|
138
|
+
url: string;
|
|
139
|
+
pollMs?: number;
|
|
140
|
+
headers?: Record<string, string>;
|
|
141
|
+
/** Send local changes (toggle / create / delete) back to `url`. Default "none": the backend stays read-only. */
|
|
142
|
+
saveMethod?: 'PUT' | 'POST' | 'none';
|
|
143
|
+
}
|
|
144
|
+
/** Load rules (and violations) from your backend every few seconds. Returns a function that disconnects. */
|
|
145
|
+
export declare const connectRateLimit: (o: RateLimitConnection) => (() => void);
|
|
146
|
+
/** Pull the backend now (the Refresh button). */
|
|
147
|
+
export declare const refreshRateLimit: () => void | undefined;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export type Strategy = 'fixed_window' | 'sliding_window' | 'token_bucket' | 'leaky_bucket';
|
|
2
|
+
export declare const STRATEGIES: Strategy[];
|
|
3
|
+
export interface LimiterOptions {
|
|
4
|
+
/** Requests allowed per window. */
|
|
5
|
+
limit: number;
|
|
6
|
+
/** Window length in seconds. */
|
|
7
|
+
windowSec: number;
|
|
8
|
+
/** Default "sliding_window". */
|
|
9
|
+
strategy?: Strategy;
|
|
10
|
+
}
|
|
11
|
+
export interface Decision {
|
|
12
|
+
ok: boolean;
|
|
13
|
+
limit: number;
|
|
14
|
+
/** Requests left right now (0 when blocked). */
|
|
15
|
+
remaining: number;
|
|
16
|
+
/** When blocked: how long until one more request is allowed. 0 when allowed. */
|
|
17
|
+
retryAfterMs: number;
|
|
18
|
+
/** How long until the counter is fully back to normal. */
|
|
19
|
+
resetMs: number;
|
|
20
|
+
}
|
|
21
|
+
export interface Limiter {
|
|
22
|
+
/** Count one request for `key` and say whether it is allowed. */
|
|
23
|
+
take: (key?: string, now?: number) => Decision;
|
|
24
|
+
/** Forget one key (or all keys). */
|
|
25
|
+
reset: (key?: string) => void;
|
|
26
|
+
}
|
|
27
|
+
export declare const createLimiter: (o: LimiterOptions) => Limiter;
|
|
28
|
+
/** Does a URL path match a rule pattern? "*" = everything, "/api/*" = glob, "/login" = path contains it. Query string is ignored for plain patterns. */
|
|
29
|
+
export declare const matchPath: (pattern: string, path: string) => boolean;
|