@gravito/spectrum 1.0.0 → 1.0.1
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 +1 -1
- package/dist/index.cjs +705 -0
- package/dist/index.d.cts +187 -0
- package/dist/index.d.ts +187 -0
- package/dist/index.js +666 -0
- package/package.json +6 -6
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { GravitoContext, GravitoOrbit, PlanetCore } from '@gravito/core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @gravito/spectrum - Types
|
|
5
|
+
*/
|
|
6
|
+
interface CapturedRequest {
|
|
7
|
+
id: string;
|
|
8
|
+
method: string;
|
|
9
|
+
path: string;
|
|
10
|
+
url: string;
|
|
11
|
+
status: number;
|
|
12
|
+
duration: number;
|
|
13
|
+
startTime: number;
|
|
14
|
+
ip: string;
|
|
15
|
+
userAgent?: string;
|
|
16
|
+
requestHeaders: Record<string, string>;
|
|
17
|
+
responseHeaders: Record<string, string>;
|
|
18
|
+
requestBody?: any;
|
|
19
|
+
responseBody?: any;
|
|
20
|
+
}
|
|
21
|
+
interface CapturedLog {
|
|
22
|
+
id: string;
|
|
23
|
+
level: 'debug' | 'info' | 'warn' | 'error';
|
|
24
|
+
message: string;
|
|
25
|
+
args: any[];
|
|
26
|
+
timestamp: number;
|
|
27
|
+
}
|
|
28
|
+
interface CapturedQuery {
|
|
29
|
+
id: string;
|
|
30
|
+
connection: string;
|
|
31
|
+
sql: string;
|
|
32
|
+
bindings: any[];
|
|
33
|
+
duration: number;
|
|
34
|
+
timestamp: number;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @gravito/spectrum - Storage Contract
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
interface SpectrumStorage {
|
|
42
|
+
/**
|
|
43
|
+
* Initialize storage driver
|
|
44
|
+
*/
|
|
45
|
+
init(): Promise<void>;
|
|
46
|
+
/**
|
|
47
|
+
* Store a captured request
|
|
48
|
+
*/
|
|
49
|
+
storeRequest(req: CapturedRequest): Promise<void>;
|
|
50
|
+
/**
|
|
51
|
+
* Store a captured log
|
|
52
|
+
*/
|
|
53
|
+
storeLog(log: CapturedLog): Promise<void>;
|
|
54
|
+
/**
|
|
55
|
+
* Store a captured database query
|
|
56
|
+
*/
|
|
57
|
+
storeQuery(query: CapturedQuery): Promise<void>;
|
|
58
|
+
/**
|
|
59
|
+
* Retrieve recent requests
|
|
60
|
+
*/
|
|
61
|
+
getRequests(limit?: number, offset?: number): Promise<CapturedRequest[]>;
|
|
62
|
+
/**
|
|
63
|
+
* Retrieve specific request by ID
|
|
64
|
+
*/
|
|
65
|
+
getRequest(id: string): Promise<CapturedRequest | null>;
|
|
66
|
+
/**
|
|
67
|
+
* Retrieve recent logs
|
|
68
|
+
*/
|
|
69
|
+
getLogs(limit?: number, offset?: number): Promise<CapturedLog[]>;
|
|
70
|
+
/**
|
|
71
|
+
* Retrieve recent queries
|
|
72
|
+
*/
|
|
73
|
+
getQueries(limit?: number, offset?: number): Promise<CapturedQuery[]>;
|
|
74
|
+
/**
|
|
75
|
+
* Clear all data
|
|
76
|
+
*/
|
|
77
|
+
clear(): Promise<void>;
|
|
78
|
+
/**
|
|
79
|
+
* Prune old data (maintenance)
|
|
80
|
+
*/
|
|
81
|
+
prune(maxItems: number): Promise<void>;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* @gravito/spectrum - SpectrumOrbit
|
|
86
|
+
*/
|
|
87
|
+
|
|
88
|
+
interface SpectrumConfig {
|
|
89
|
+
path?: string;
|
|
90
|
+
maxItems?: number;
|
|
91
|
+
enabled?: boolean;
|
|
92
|
+
storage?: SpectrumStorage;
|
|
93
|
+
/**
|
|
94
|
+
* Authorization gate. Return true to allow access.
|
|
95
|
+
*/
|
|
96
|
+
gate?: (c: GravitoContext) => boolean | Promise<boolean>;
|
|
97
|
+
/**
|
|
98
|
+
* Sample rate (0.0 to 1.0). Default: 1.0 (100%)
|
|
99
|
+
*/
|
|
100
|
+
sampleRate?: number;
|
|
101
|
+
}
|
|
102
|
+
interface SpectrumOrbitDeps {
|
|
103
|
+
atlas?: {
|
|
104
|
+
Connection?: {
|
|
105
|
+
queryListeners: Array<(query: any) => void>;
|
|
106
|
+
};
|
|
107
|
+
} | null;
|
|
108
|
+
loadAtlas?: () => Promise<{
|
|
109
|
+
Connection?: {
|
|
110
|
+
queryListeners: Array<(query: any) => void>;
|
|
111
|
+
};
|
|
112
|
+
} | null>;
|
|
113
|
+
}
|
|
114
|
+
declare class SpectrumOrbit implements GravitoOrbit {
|
|
115
|
+
static instance: SpectrumOrbit | undefined;
|
|
116
|
+
readonly name = "spectrum";
|
|
117
|
+
private config;
|
|
118
|
+
private listeners;
|
|
119
|
+
private warnedSecurity;
|
|
120
|
+
private deps;
|
|
121
|
+
constructor(config?: SpectrumConfig, deps?: SpectrumOrbitDeps);
|
|
122
|
+
private shouldCapture;
|
|
123
|
+
private broadcast;
|
|
124
|
+
install(core: PlanetCore): Promise<void>;
|
|
125
|
+
private setupDatabaseCollection;
|
|
126
|
+
private setupHttpCollection;
|
|
127
|
+
private setupLogCollection;
|
|
128
|
+
private captureLog;
|
|
129
|
+
private registerRoutes;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* @gravito/spectrum - FileStorage
|
|
134
|
+
*
|
|
135
|
+
* Persistent storage using JSONL files.
|
|
136
|
+
*/
|
|
137
|
+
|
|
138
|
+
interface FileStorageConfig {
|
|
139
|
+
directory: string;
|
|
140
|
+
}
|
|
141
|
+
declare class FileStorage implements SpectrumStorage {
|
|
142
|
+
private config;
|
|
143
|
+
private requestsPath;
|
|
144
|
+
private logsPath;
|
|
145
|
+
private queriesPath;
|
|
146
|
+
private cache;
|
|
147
|
+
constructor(config: FileStorageConfig);
|
|
148
|
+
init(): Promise<void>;
|
|
149
|
+
private loadCache;
|
|
150
|
+
private append;
|
|
151
|
+
storeRequest(req: CapturedRequest): Promise<void>;
|
|
152
|
+
storeLog(log: CapturedLog): Promise<void>;
|
|
153
|
+
storeQuery(query: CapturedQuery): Promise<void>;
|
|
154
|
+
getRequests(limit?: number, offset?: number): Promise<CapturedRequest[]>;
|
|
155
|
+
getRequest(id: string): Promise<CapturedRequest | null>;
|
|
156
|
+
getLogs(limit?: number, offset?: number): Promise<CapturedLog[]>;
|
|
157
|
+
getQueries(limit?: number, offset?: number): Promise<CapturedQuery[]>;
|
|
158
|
+
clear(): Promise<void>;
|
|
159
|
+
prune(maxItems: number): Promise<void>;
|
|
160
|
+
private rewrite;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* @gravito/spectrum - MemoryStorage
|
|
165
|
+
*
|
|
166
|
+
* Non-persistent storage for development.
|
|
167
|
+
*/
|
|
168
|
+
|
|
169
|
+
declare class MemoryStorage implements SpectrumStorage {
|
|
170
|
+
private requests;
|
|
171
|
+
private logs;
|
|
172
|
+
private queries;
|
|
173
|
+
private maxItems;
|
|
174
|
+
init(): Promise<void>;
|
|
175
|
+
storeRequest(req: CapturedRequest): Promise<void>;
|
|
176
|
+
storeLog(log: CapturedLog): Promise<void>;
|
|
177
|
+
storeQuery(query: CapturedQuery): Promise<void>;
|
|
178
|
+
getRequests(limit?: number, offset?: number): Promise<CapturedRequest[]>;
|
|
179
|
+
getRequest(id: string): Promise<CapturedRequest | null>;
|
|
180
|
+
getLogs(limit?: number, offset?: number): Promise<CapturedLog[]>;
|
|
181
|
+
getQueries(limit?: number, offset?: number): Promise<CapturedQuery[]>;
|
|
182
|
+
clear(): Promise<void>;
|
|
183
|
+
prune(maxItems: number): Promise<void>;
|
|
184
|
+
private trim;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export { type CapturedLog, type CapturedQuery, type CapturedRequest, FileStorage, type FileStorageConfig, MemoryStorage, type SpectrumConfig, SpectrumOrbit, type SpectrumStorage };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { GravitoContext, GravitoOrbit, PlanetCore } from '@gravito/core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @gravito/spectrum - Types
|
|
5
|
+
*/
|
|
6
|
+
interface CapturedRequest {
|
|
7
|
+
id: string;
|
|
8
|
+
method: string;
|
|
9
|
+
path: string;
|
|
10
|
+
url: string;
|
|
11
|
+
status: number;
|
|
12
|
+
duration: number;
|
|
13
|
+
startTime: number;
|
|
14
|
+
ip: string;
|
|
15
|
+
userAgent?: string;
|
|
16
|
+
requestHeaders: Record<string, string>;
|
|
17
|
+
responseHeaders: Record<string, string>;
|
|
18
|
+
requestBody?: any;
|
|
19
|
+
responseBody?: any;
|
|
20
|
+
}
|
|
21
|
+
interface CapturedLog {
|
|
22
|
+
id: string;
|
|
23
|
+
level: 'debug' | 'info' | 'warn' | 'error';
|
|
24
|
+
message: string;
|
|
25
|
+
args: any[];
|
|
26
|
+
timestamp: number;
|
|
27
|
+
}
|
|
28
|
+
interface CapturedQuery {
|
|
29
|
+
id: string;
|
|
30
|
+
connection: string;
|
|
31
|
+
sql: string;
|
|
32
|
+
bindings: any[];
|
|
33
|
+
duration: number;
|
|
34
|
+
timestamp: number;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @gravito/spectrum - Storage Contract
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
interface SpectrumStorage {
|
|
42
|
+
/**
|
|
43
|
+
* Initialize storage driver
|
|
44
|
+
*/
|
|
45
|
+
init(): Promise<void>;
|
|
46
|
+
/**
|
|
47
|
+
* Store a captured request
|
|
48
|
+
*/
|
|
49
|
+
storeRequest(req: CapturedRequest): Promise<void>;
|
|
50
|
+
/**
|
|
51
|
+
* Store a captured log
|
|
52
|
+
*/
|
|
53
|
+
storeLog(log: CapturedLog): Promise<void>;
|
|
54
|
+
/**
|
|
55
|
+
* Store a captured database query
|
|
56
|
+
*/
|
|
57
|
+
storeQuery(query: CapturedQuery): Promise<void>;
|
|
58
|
+
/**
|
|
59
|
+
* Retrieve recent requests
|
|
60
|
+
*/
|
|
61
|
+
getRequests(limit?: number, offset?: number): Promise<CapturedRequest[]>;
|
|
62
|
+
/**
|
|
63
|
+
* Retrieve specific request by ID
|
|
64
|
+
*/
|
|
65
|
+
getRequest(id: string): Promise<CapturedRequest | null>;
|
|
66
|
+
/**
|
|
67
|
+
* Retrieve recent logs
|
|
68
|
+
*/
|
|
69
|
+
getLogs(limit?: number, offset?: number): Promise<CapturedLog[]>;
|
|
70
|
+
/**
|
|
71
|
+
* Retrieve recent queries
|
|
72
|
+
*/
|
|
73
|
+
getQueries(limit?: number, offset?: number): Promise<CapturedQuery[]>;
|
|
74
|
+
/**
|
|
75
|
+
* Clear all data
|
|
76
|
+
*/
|
|
77
|
+
clear(): Promise<void>;
|
|
78
|
+
/**
|
|
79
|
+
* Prune old data (maintenance)
|
|
80
|
+
*/
|
|
81
|
+
prune(maxItems: number): Promise<void>;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* @gravito/spectrum - SpectrumOrbit
|
|
86
|
+
*/
|
|
87
|
+
|
|
88
|
+
interface SpectrumConfig {
|
|
89
|
+
path?: string;
|
|
90
|
+
maxItems?: number;
|
|
91
|
+
enabled?: boolean;
|
|
92
|
+
storage?: SpectrumStorage;
|
|
93
|
+
/**
|
|
94
|
+
* Authorization gate. Return true to allow access.
|
|
95
|
+
*/
|
|
96
|
+
gate?: (c: GravitoContext) => boolean | Promise<boolean>;
|
|
97
|
+
/**
|
|
98
|
+
* Sample rate (0.0 to 1.0). Default: 1.0 (100%)
|
|
99
|
+
*/
|
|
100
|
+
sampleRate?: number;
|
|
101
|
+
}
|
|
102
|
+
interface SpectrumOrbitDeps {
|
|
103
|
+
atlas?: {
|
|
104
|
+
Connection?: {
|
|
105
|
+
queryListeners: Array<(query: any) => void>;
|
|
106
|
+
};
|
|
107
|
+
} | null;
|
|
108
|
+
loadAtlas?: () => Promise<{
|
|
109
|
+
Connection?: {
|
|
110
|
+
queryListeners: Array<(query: any) => void>;
|
|
111
|
+
};
|
|
112
|
+
} | null>;
|
|
113
|
+
}
|
|
114
|
+
declare class SpectrumOrbit implements GravitoOrbit {
|
|
115
|
+
static instance: SpectrumOrbit | undefined;
|
|
116
|
+
readonly name = "spectrum";
|
|
117
|
+
private config;
|
|
118
|
+
private listeners;
|
|
119
|
+
private warnedSecurity;
|
|
120
|
+
private deps;
|
|
121
|
+
constructor(config?: SpectrumConfig, deps?: SpectrumOrbitDeps);
|
|
122
|
+
private shouldCapture;
|
|
123
|
+
private broadcast;
|
|
124
|
+
install(core: PlanetCore): Promise<void>;
|
|
125
|
+
private setupDatabaseCollection;
|
|
126
|
+
private setupHttpCollection;
|
|
127
|
+
private setupLogCollection;
|
|
128
|
+
private captureLog;
|
|
129
|
+
private registerRoutes;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* @gravito/spectrum - FileStorage
|
|
134
|
+
*
|
|
135
|
+
* Persistent storage using JSONL files.
|
|
136
|
+
*/
|
|
137
|
+
|
|
138
|
+
interface FileStorageConfig {
|
|
139
|
+
directory: string;
|
|
140
|
+
}
|
|
141
|
+
declare class FileStorage implements SpectrumStorage {
|
|
142
|
+
private config;
|
|
143
|
+
private requestsPath;
|
|
144
|
+
private logsPath;
|
|
145
|
+
private queriesPath;
|
|
146
|
+
private cache;
|
|
147
|
+
constructor(config: FileStorageConfig);
|
|
148
|
+
init(): Promise<void>;
|
|
149
|
+
private loadCache;
|
|
150
|
+
private append;
|
|
151
|
+
storeRequest(req: CapturedRequest): Promise<void>;
|
|
152
|
+
storeLog(log: CapturedLog): Promise<void>;
|
|
153
|
+
storeQuery(query: CapturedQuery): Promise<void>;
|
|
154
|
+
getRequests(limit?: number, offset?: number): Promise<CapturedRequest[]>;
|
|
155
|
+
getRequest(id: string): Promise<CapturedRequest | null>;
|
|
156
|
+
getLogs(limit?: number, offset?: number): Promise<CapturedLog[]>;
|
|
157
|
+
getQueries(limit?: number, offset?: number): Promise<CapturedQuery[]>;
|
|
158
|
+
clear(): Promise<void>;
|
|
159
|
+
prune(maxItems: number): Promise<void>;
|
|
160
|
+
private rewrite;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* @gravito/spectrum - MemoryStorage
|
|
165
|
+
*
|
|
166
|
+
* Non-persistent storage for development.
|
|
167
|
+
*/
|
|
168
|
+
|
|
169
|
+
declare class MemoryStorage implements SpectrumStorage {
|
|
170
|
+
private requests;
|
|
171
|
+
private logs;
|
|
172
|
+
private queries;
|
|
173
|
+
private maxItems;
|
|
174
|
+
init(): Promise<void>;
|
|
175
|
+
storeRequest(req: CapturedRequest): Promise<void>;
|
|
176
|
+
storeLog(log: CapturedLog): Promise<void>;
|
|
177
|
+
storeQuery(query: CapturedQuery): Promise<void>;
|
|
178
|
+
getRequests(limit?: number, offset?: number): Promise<CapturedRequest[]>;
|
|
179
|
+
getRequest(id: string): Promise<CapturedRequest | null>;
|
|
180
|
+
getLogs(limit?: number, offset?: number): Promise<CapturedLog[]>;
|
|
181
|
+
getQueries(limit?: number, offset?: number): Promise<CapturedQuery[]>;
|
|
182
|
+
clear(): Promise<void>;
|
|
183
|
+
prune(maxItems: number): Promise<void>;
|
|
184
|
+
private trim;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export { type CapturedLog, type CapturedQuery, type CapturedRequest, FileStorage, type FileStorageConfig, MemoryStorage, type SpectrumConfig, SpectrumOrbit, type SpectrumStorage };
|