@getstrata/core 0.5.34 → 0.5.36
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/dist/entries/audit/exportAuditLogs.js +415 -0
- package/dist/entries/events.js +1 -0
- package/dist/entries/jobs/dispatchWebhookJob.js +60 -0
- package/dist/entries/jobs/invalidateCacheTagsJob.js +26 -0
- package/dist/entries/logging/logger.js +40 -0
- package/dist/entries/queue.js +32 -0
- package/dist/framework/public-api.d.ts +3 -1
- package/dist/index.js +2 -0
- package/package.json +27 -2
|
@@ -0,0 +1,415 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// ../../src/config/app.ts
|
|
3
|
+
var appConfig = {
|
|
4
|
+
name: "WorkHub",
|
|
5
|
+
env: process.env.APP_ENV ?? "local",
|
|
6
|
+
debug: (process.env.APP_DEBUG ?? "true") !== "false",
|
|
7
|
+
url: process.env.APP_URL ?? "http://localhost:3000",
|
|
8
|
+
apiPrefix: process.env.API_PREFIX ?? "/api/v1"
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
// ../../src/core/database/boundConnection.ts
|
|
12
|
+
var boundConnectionHolder = {
|
|
13
|
+
connection: null
|
|
14
|
+
};
|
|
15
|
+
function getBoundDatabaseConnection() {
|
|
16
|
+
return boundConnectionHolder.connection;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// ../../src/core/runtime/asyncContextStore.ts
|
|
20
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
21
|
+
function createAsyncContextStore(key) {
|
|
22
|
+
const symbol = Symbol.for(key);
|
|
23
|
+
const globalRecord = globalThis;
|
|
24
|
+
const existing = globalRecord[symbol];
|
|
25
|
+
if (existing) {
|
|
26
|
+
return existing;
|
|
27
|
+
}
|
|
28
|
+
const store = new AsyncLocalStorage;
|
|
29
|
+
globalRecord[symbol] = store;
|
|
30
|
+
return store;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// ../../src/core/database/connectionContext.ts
|
|
34
|
+
var activeConnection = createAsyncContextStore("@getstrata/databaseConnectionContext");
|
|
35
|
+
function getActiveDatabaseConnection(fallback) {
|
|
36
|
+
return activeConnection.getStore() ?? fallback;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// ../../src/core/database/queryProxy.ts
|
|
40
|
+
var POOL_CONNECTION_METHODS = new Set(["begin", "close", "connect"]);
|
|
41
|
+
function createDatabaseQueryProxy(pool) {
|
|
42
|
+
function resolveDatabase() {
|
|
43
|
+
return getActiveDatabaseConnection(pool);
|
|
44
|
+
}
|
|
45
|
+
function resolveDatabaseForProperty(property) {
|
|
46
|
+
if (typeof property === "string" && POOL_CONNECTION_METHODS.has(property)) {
|
|
47
|
+
return pool;
|
|
48
|
+
}
|
|
49
|
+
return resolveDatabase();
|
|
50
|
+
}
|
|
51
|
+
return new Proxy(function database() {}, {
|
|
52
|
+
apply(_target, _thisArg, args) {
|
|
53
|
+
return resolveDatabase()(...args);
|
|
54
|
+
},
|
|
55
|
+
get(_target, property) {
|
|
56
|
+
const connection = resolveDatabaseForProperty(property);
|
|
57
|
+
const value = connection[property];
|
|
58
|
+
return typeof value === "function" ? value.bind(connection) : value;
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// ../../src/core/database/defaultConnection.ts
|
|
64
|
+
var defaultPool = {
|
|
65
|
+
connection: null
|
|
66
|
+
};
|
|
67
|
+
var defaultQuery = {
|
|
68
|
+
connection: null
|
|
69
|
+
};
|
|
70
|
+
function registerDefaultDatabasePool(connection) {
|
|
71
|
+
defaultPool.connection = connection;
|
|
72
|
+
defaultQuery.connection = createDatabaseQueryProxy(connection);
|
|
73
|
+
}
|
|
74
|
+
function getDefaultDatabaseQuery() {
|
|
75
|
+
if (!defaultQuery.connection) {
|
|
76
|
+
throw new Error("Default database query handle is not registered. Call registerDefaultDatabasePool() during app bootstrap.");
|
|
77
|
+
}
|
|
78
|
+
return defaultQuery.connection;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// ../../src/core/database/repositoryConnection.ts
|
|
82
|
+
function resolveRepositoryConnection() {
|
|
83
|
+
return getBoundDatabaseConnection() ?? getDefaultDatabaseQuery();
|
|
84
|
+
}
|
|
85
|
+
var repositoryConnection = new Proxy(function repositoryConnection2() {}, {
|
|
86
|
+
apply(_target, _thisArg, args) {
|
|
87
|
+
return resolveRepositoryConnection()(...args);
|
|
88
|
+
},
|
|
89
|
+
get(_target, property) {
|
|
90
|
+
const connection = resolveRepositoryConnection();
|
|
91
|
+
const value = connection[property];
|
|
92
|
+
return typeof value === "function" ? value.bind(connection) : value;
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
// ../../src/core/security/safeUrl.ts
|
|
97
|
+
import { lookup as dnsLookupImpl } from "dns/promises";
|
|
98
|
+
|
|
99
|
+
// ../../src/core/errors/http.ts
|
|
100
|
+
class HttpError extends Error {
|
|
101
|
+
status;
|
|
102
|
+
details;
|
|
103
|
+
constructor(status, message, details) {
|
|
104
|
+
super(message);
|
|
105
|
+
this.name = new.target.name;
|
|
106
|
+
this.status = status;
|
|
107
|
+
this.details = details;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
class BadRequestError extends HttpError {
|
|
112
|
+
constructor(message = "Bad Request", details) {
|
|
113
|
+
super(400, message, details);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
class NotFoundError extends HttpError {
|
|
118
|
+
constructor(message = "Not Found", details) {
|
|
119
|
+
super(404, message, details);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
class ConflictError extends HttpError {
|
|
124
|
+
constructor(message = "Conflict", details) {
|
|
125
|
+
super(409, message, details);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
class UnprocessableEntityError extends HttpError {
|
|
130
|
+
constructor(message = "Unprocessable Entity", details) {
|
|
131
|
+
super(422, message, details);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
class ValidationError extends HttpError {
|
|
136
|
+
constructor(message = "Validation failed", details) {
|
|
137
|
+
super(422, message, details);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
class ForbiddenError extends HttpError {
|
|
142
|
+
constructor(message = "Forbidden", details) {
|
|
143
|
+
super(403, message, details);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
class UnauthorizedError extends HttpError {
|
|
148
|
+
constructor(message = "Unauthorized", details) {
|
|
149
|
+
super(401, message, details);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
class PayloadTooLargeError extends HttpError {
|
|
154
|
+
constructor(message = "Payload Too Large", details) {
|
|
155
|
+
super(413, message, details);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
class PreconditionFailedError extends HttpError {
|
|
160
|
+
constructor(message = "Precondition Failed", details) {
|
|
161
|
+
super(412, message, details);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// ../../src/core/security/safeUrl.ts
|
|
166
|
+
var dnsLookup = dnsLookupImpl;
|
|
167
|
+
var BLOCKED_HOSTNAMES = new Set([
|
|
168
|
+
"localhost",
|
|
169
|
+
"127.0.0.1",
|
|
170
|
+
"0.0.0.0",
|
|
171
|
+
"::1",
|
|
172
|
+
"metadata.google.internal"
|
|
173
|
+
]);
|
|
174
|
+
function isPrivateIpv4(hostname) {
|
|
175
|
+
const match = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/.exec(hostname);
|
|
176
|
+
if (!match) {
|
|
177
|
+
return false;
|
|
178
|
+
}
|
|
179
|
+
const octets = match.slice(1, 5).map((part) => Number.parseInt(part, 10));
|
|
180
|
+
if (octets.some((octet) => octet < 0 || octet > 255)) {
|
|
181
|
+
return true;
|
|
182
|
+
}
|
|
183
|
+
const [a = 0, b = 0] = octets;
|
|
184
|
+
if (a === 10) {
|
|
185
|
+
return true;
|
|
186
|
+
}
|
|
187
|
+
if (a === 127) {
|
|
188
|
+
return true;
|
|
189
|
+
}
|
|
190
|
+
if (a === 0) {
|
|
191
|
+
return true;
|
|
192
|
+
}
|
|
193
|
+
if (a === 169 && b === 254) {
|
|
194
|
+
return true;
|
|
195
|
+
}
|
|
196
|
+
if (a === 172 && b >= 16 && b <= 31) {
|
|
197
|
+
return true;
|
|
198
|
+
}
|
|
199
|
+
if (a === 192 && b === 168) {
|
|
200
|
+
return true;
|
|
201
|
+
}
|
|
202
|
+
return false;
|
|
203
|
+
}
|
|
204
|
+
function isBlockedHostname(hostname) {
|
|
205
|
+
const normalized = hostname.trim().toLowerCase();
|
|
206
|
+
if (normalized.length === 0) {
|
|
207
|
+
return true;
|
|
208
|
+
}
|
|
209
|
+
if (BLOCKED_HOSTNAMES.has(normalized)) {
|
|
210
|
+
return true;
|
|
211
|
+
}
|
|
212
|
+
if (normalized.endsWith(".local") || normalized.endsWith(".internal")) {
|
|
213
|
+
return true;
|
|
214
|
+
}
|
|
215
|
+
if (normalized.includes(":")) {
|
|
216
|
+
return true;
|
|
217
|
+
}
|
|
218
|
+
return isPrivateIpv4(normalized);
|
|
219
|
+
}
|
|
220
|
+
function assertSafeOutboundUrl(rawUrl, options = {}) {
|
|
221
|
+
let parsed;
|
|
222
|
+
try {
|
|
223
|
+
parsed = new URL(rawUrl);
|
|
224
|
+
} catch {
|
|
225
|
+
throw new BadRequestError("Webhook URL is invalid.");
|
|
226
|
+
}
|
|
227
|
+
if (parsed.protocol !== "https:" && !(options.allowHttp && parsed.protocol === "http:")) {
|
|
228
|
+
throw new BadRequestError("Webhook URL must use HTTPS.");
|
|
229
|
+
}
|
|
230
|
+
if (parsed.username || parsed.password) {
|
|
231
|
+
throw new BadRequestError("Webhook URL must not include credentials.");
|
|
232
|
+
}
|
|
233
|
+
if (isBlockedHostname(parsed.hostname)) {
|
|
234
|
+
throw new BadRequestError("Webhook URL targets a blocked host.");
|
|
235
|
+
}
|
|
236
|
+
return parsed;
|
|
237
|
+
}
|
|
238
|
+
function isBlockedIpAddress(address) {
|
|
239
|
+
return isBlockedHostname(address.trim().toLowerCase());
|
|
240
|
+
}
|
|
241
|
+
async function assertSafeOutboundUrlResolved(rawUrl, options = {}) {
|
|
242
|
+
const parsed = assertSafeOutboundUrl(rawUrl, options);
|
|
243
|
+
if (options.resolveDns === false) {
|
|
244
|
+
return parsed;
|
|
245
|
+
}
|
|
246
|
+
const hostname = parsed.hostname.trim().toLowerCase();
|
|
247
|
+
const results = await dnsLookup(hostname, { all: true, verbatim: true });
|
|
248
|
+
if (results.some((result) => isBlockedIpAddress(result.address))) {
|
|
249
|
+
throw new BadRequestError("Webhook URL targets a blocked host.");
|
|
250
|
+
}
|
|
251
|
+
return parsed;
|
|
252
|
+
}
|
|
253
|
+
function setDnsLookupForTests(lookupFn) {
|
|
254
|
+
dnsLookup = lookupFn;
|
|
255
|
+
}
|
|
256
|
+
function resetDnsLookupForTests() {
|
|
257
|
+
dnsLookup = dnsLookupImpl;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// ../../src/core/security/safeFetch.ts
|
|
261
|
+
var DEFAULT_FETCH_TIMEOUT_MS = 1e4;
|
|
262
|
+
async function safeFetch(input, init = {}, options = {}) {
|
|
263
|
+
const timeoutMs = options.timeoutMs ?? DEFAULT_FETCH_TIMEOUT_MS;
|
|
264
|
+
const maxRedirects = options.maxRedirects ?? 0;
|
|
265
|
+
const resolveDns = options.resolveDns ?? appConfig.env === "production";
|
|
266
|
+
const urlOptions = { allowHttp: options.allowHttp, resolveDns };
|
|
267
|
+
const controller = new AbortController;
|
|
268
|
+
const timeout = setTimeout(() => controller.abort(), timeoutMs);
|
|
269
|
+
try {
|
|
270
|
+
let currentUrl = (await assertSafeOutboundUrlResolved(input, urlOptions)).toString();
|
|
271
|
+
let redirectCount = 0;
|
|
272
|
+
while (true) {
|
|
273
|
+
const response = await fetch(currentUrl, {
|
|
274
|
+
...init,
|
|
275
|
+
signal: controller.signal,
|
|
276
|
+
redirect: "manual"
|
|
277
|
+
});
|
|
278
|
+
if (response.status >= 300 && response.status < 400) {
|
|
279
|
+
const location = response.headers.get("location");
|
|
280
|
+
if (!location || redirectCount >= maxRedirects) {
|
|
281
|
+
return response;
|
|
282
|
+
}
|
|
283
|
+
currentUrl = (await assertSafeOutboundUrlResolved(new URL(location, currentUrl).toString(), urlOptions)).toString();
|
|
284
|
+
redirectCount += 1;
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
return response;
|
|
288
|
+
}
|
|
289
|
+
} finally {
|
|
290
|
+
clearTimeout(timeout);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// ../../src/core/tenant/databaseTenantContext.ts
|
|
295
|
+
async function runWithMigrationBypass(callback) {
|
|
296
|
+
await repositoryConnection`SELECT set_config('app.bypass_rls', 'true', false)`;
|
|
297
|
+
try {
|
|
298
|
+
return await callback();
|
|
299
|
+
} finally {
|
|
300
|
+
await repositoryConnection`SELECT set_config('app.bypass_rls', 'false', false)`;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// ../../src/core/audit/siemFormatter.ts
|
|
305
|
+
function formatSiemAuditEvent(input) {
|
|
306
|
+
return {
|
|
307
|
+
timestamp: input.created_at.toISOString(),
|
|
308
|
+
event_type: "workhub.audit",
|
|
309
|
+
actor_user_id: input.user_id,
|
|
310
|
+
tenant_id: input.tenant_id ?? null,
|
|
311
|
+
trace_id: input.trace_id ?? null,
|
|
312
|
+
action: input.action,
|
|
313
|
+
subject_type: input.subject_type,
|
|
314
|
+
subject_id: input.subject_id,
|
|
315
|
+
ip_address: input.ip_address ?? null,
|
|
316
|
+
user_agent: input.user_agent ?? null,
|
|
317
|
+
checksum: input.checksum ?? null,
|
|
318
|
+
payload: input.payload ?? {}
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
function formatCefLine(event) {
|
|
322
|
+
const extension = [
|
|
323
|
+
`rt=${event.timestamp}`,
|
|
324
|
+
`suid=${event.actor_user_id ?? "unknown"}`,
|
|
325
|
+
`cs1=${event.action}`,
|
|
326
|
+
`cs1Label=Action`,
|
|
327
|
+
`cs2=${event.subject_type}`,
|
|
328
|
+
`cs2Label=SubjectType`,
|
|
329
|
+
`cs3=${event.subject_id ?? ""}`,
|
|
330
|
+
`cs3Label=SubjectId`,
|
|
331
|
+
`src=${event.ip_address ?? ""}`,
|
|
332
|
+
`request=${event.trace_id ?? ""}`
|
|
333
|
+
].join(" ");
|
|
334
|
+
return `CEF:0|WorkHub|API|1.0|${event.action}|${event.subject_type}|5|${extension}`;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// ../../src/core/audit/exportAuditLogs.ts
|
|
338
|
+
function resolveAuditExportConfig() {
|
|
339
|
+
const endpoint = process.env.SIEM_EXPORT_URL?.trim();
|
|
340
|
+
if (!endpoint) {
|
|
341
|
+
return null;
|
|
342
|
+
}
|
|
343
|
+
assertSafeOutboundUrl(endpoint, { allowHttp: appConfig.env !== "production" });
|
|
344
|
+
const batchSize = Number(process.env.SIEM_EXPORT_BATCH_SIZE ?? "100");
|
|
345
|
+
return {
|
|
346
|
+
endpoint,
|
|
347
|
+
format: process.env.SIEM_EXPORT_FORMAT === "cef" ? "cef" : "json",
|
|
348
|
+
batchSize: Number.isFinite(batchSize) ? batchSize : 100
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
async function exportPendingAuditLogs() {
|
|
352
|
+
const config = resolveAuditExportConfig();
|
|
353
|
+
if (!config) {
|
|
354
|
+
return 0;
|
|
355
|
+
}
|
|
356
|
+
return await runWithMigrationBypass(async () => {
|
|
357
|
+
const rows = await repositoryConnection`
|
|
358
|
+
SELECT
|
|
359
|
+
id,
|
|
360
|
+
user_id,
|
|
361
|
+
action,
|
|
362
|
+
subject_type,
|
|
363
|
+
subject_id,
|
|
364
|
+
payload,
|
|
365
|
+
ip_address,
|
|
366
|
+
user_agent,
|
|
367
|
+
checksum,
|
|
368
|
+
tenant_id,
|
|
369
|
+
trace_id,
|
|
370
|
+
created_at
|
|
371
|
+
FROM audit_log
|
|
372
|
+
WHERE exported_at IS NULL
|
|
373
|
+
ORDER BY id
|
|
374
|
+
LIMIT ${config.batchSize}
|
|
375
|
+
`;
|
|
376
|
+
if (rows.length === 0) {
|
|
377
|
+
return 0;
|
|
378
|
+
}
|
|
379
|
+
const events = rows.map((row) => formatSiemAuditEvent({
|
|
380
|
+
action: row.action,
|
|
381
|
+
subject_type: row.subject_type,
|
|
382
|
+
subject_id: row.subject_id,
|
|
383
|
+
user_id: row.user_id,
|
|
384
|
+
tenant_id: row.tenant_id,
|
|
385
|
+
trace_id: row.trace_id,
|
|
386
|
+
ip_address: row.ip_address,
|
|
387
|
+
user_agent: row.user_agent,
|
|
388
|
+
checksum: row.checksum,
|
|
389
|
+
payload: row.payload,
|
|
390
|
+
created_at: row.created_at
|
|
391
|
+
}));
|
|
392
|
+
const body = config.format === "cef" ? events.map((event) => formatCefLine(event)).join(`
|
|
393
|
+
`) : JSON.stringify({ events });
|
|
394
|
+
const response = await safeFetch(config.endpoint, {
|
|
395
|
+
method: "POST",
|
|
396
|
+
headers: {
|
|
397
|
+
"content-type": config.format === "cef" ? "text/plain" : "application/json",
|
|
398
|
+
...process.env.SIEM_EXPORT_TOKEN ? { authorization: `Bearer ${process.env.SIEM_EXPORT_TOKEN}` } : {}
|
|
399
|
+
},
|
|
400
|
+
body
|
|
401
|
+
}, { allowHttp: appConfig.env !== "production" });
|
|
402
|
+
if (!response.ok) {
|
|
403
|
+
throw new Error(`SIEM export failed with status ${response.status}.`);
|
|
404
|
+
}
|
|
405
|
+
const ids = rows.map((row) => row.id);
|
|
406
|
+
for (const id of ids) {
|
|
407
|
+
await repositoryConnection`UPDATE audit_log SET exported_at = NOW() WHERE id = ${id}`;
|
|
408
|
+
}
|
|
409
|
+
return rows.length;
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
export {
|
|
413
|
+
resolveAuditExportConfig,
|
|
414
|
+
exportPendingAuditLogs
|
|
415
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "../index.js";
|
|
@@ -93,6 +93,32 @@ var repositoryConnection = new Proxy(function repositoryConnection2() {}, {
|
|
|
93
93
|
}
|
|
94
94
|
});
|
|
95
95
|
|
|
96
|
+
// ../../src/core/queue/index.ts
|
|
97
|
+
class Job {
|
|
98
|
+
maxAttempts;
|
|
99
|
+
backoffMs;
|
|
100
|
+
priority;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
class SyncQueue {
|
|
104
|
+
async dispatch(job, payload) {
|
|
105
|
+
await job.handle(payload);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
class AsyncQueue {
|
|
110
|
+
async dispatch(job, payload) {
|
|
111
|
+
setTimeout(() => {
|
|
112
|
+
job.handle(payload).catch((error) => {
|
|
113
|
+
console.error("[AsyncQueue] Job failed:", error);
|
|
114
|
+
});
|
|
115
|
+
}, 0);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
function createQueue(driver) {
|
|
119
|
+
return driver === "async" ? new AsyncQueue : new SyncQueue;
|
|
120
|
+
}
|
|
121
|
+
|
|
96
122
|
// ../../src/core/security/safeUrl.ts
|
|
97
123
|
import { lookup as dnsLookupImpl } from "dns/promises";
|
|
98
124
|
|
|
@@ -256,3 +282,37 @@ function setDnsLookupForTests(lookupFn) {
|
|
|
256
282
|
function resetDnsLookupForTests() {
|
|
257
283
|
dnsLookup = dnsLookupImpl;
|
|
258
284
|
}
|
|
285
|
+
|
|
286
|
+
// ../../src/core/security/safeFetch.ts
|
|
287
|
+
var DEFAULT_FETCH_TIMEOUT_MS = 1e4;
|
|
288
|
+
async function safeFetch(input, init = {}, options = {}) {
|
|
289
|
+
const timeoutMs = options.timeoutMs ?? DEFAULT_FETCH_TIMEOUT_MS;
|
|
290
|
+
const maxRedirects = options.maxRedirects ?? 0;
|
|
291
|
+
const resolveDns = options.resolveDns ?? appConfig.env === "production";
|
|
292
|
+
const urlOptions = { allowHttp: options.allowHttp, resolveDns };
|
|
293
|
+
const controller = new AbortController;
|
|
294
|
+
const timeout = setTimeout(() => controller.abort(), timeoutMs);
|
|
295
|
+
try {
|
|
296
|
+
let currentUrl = (await assertSafeOutboundUrlResolved(input, urlOptions)).toString();
|
|
297
|
+
let redirectCount = 0;
|
|
298
|
+
while (true) {
|
|
299
|
+
const response = await fetch(currentUrl, {
|
|
300
|
+
...init,
|
|
301
|
+
signal: controller.signal,
|
|
302
|
+
redirect: "manual"
|
|
303
|
+
});
|
|
304
|
+
if (response.status >= 300 && response.status < 400) {
|
|
305
|
+
const location = response.headers.get("location");
|
|
306
|
+
if (!location || redirectCount >= maxRedirects) {
|
|
307
|
+
return response;
|
|
308
|
+
}
|
|
309
|
+
currentUrl = (await assertSafeOutboundUrlResolved(new URL(location, currentUrl).toString(), urlOptions)).toString();
|
|
310
|
+
redirectCount += 1;
|
|
311
|
+
continue;
|
|
312
|
+
}
|
|
313
|
+
return response;
|
|
314
|
+
}
|
|
315
|
+
} finally {
|
|
316
|
+
clearTimeout(timeout);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// ../../src/core/queue/index.ts
|
|
3
|
+
class Job {
|
|
4
|
+
maxAttempts;
|
|
5
|
+
backoffMs;
|
|
6
|
+
priority;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
class SyncQueue {
|
|
10
|
+
async dispatch(job, payload) {
|
|
11
|
+
await job.handle(payload);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
class AsyncQueue {
|
|
16
|
+
async dispatch(job, payload) {
|
|
17
|
+
setTimeout(() => {
|
|
18
|
+
job.handle(payload).catch((error) => {
|
|
19
|
+
console.error("[AsyncQueue] Job failed:", error);
|
|
20
|
+
});
|
|
21
|
+
}, 0);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function createQueue(driver) {
|
|
25
|
+
return driver === "async" ? new AsyncQueue : new SyncQueue;
|
|
26
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// ../../src/core/logging/logger.ts
|
|
3
|
+
class Logger {
|
|
4
|
+
channel;
|
|
5
|
+
constructor(channel = "app") {
|
|
6
|
+
this.channel = channel;
|
|
7
|
+
}
|
|
8
|
+
write(level, message, context = {}) {
|
|
9
|
+
const entry = {
|
|
10
|
+
level,
|
|
11
|
+
channel: this.channel,
|
|
12
|
+
message,
|
|
13
|
+
timestamp: new Date().toISOString(),
|
|
14
|
+
...context
|
|
15
|
+
};
|
|
16
|
+
const line = JSON.stringify(entry);
|
|
17
|
+
if (level === "error") {
|
|
18
|
+
console.error(line);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
console.log(line);
|
|
22
|
+
}
|
|
23
|
+
debug(message, context) {
|
|
24
|
+
this.write("debug", message, context);
|
|
25
|
+
}
|
|
26
|
+
info(message, context) {
|
|
27
|
+
this.write("info", message, context);
|
|
28
|
+
}
|
|
29
|
+
warn(message, context) {
|
|
30
|
+
this.write("warn", message, context);
|
|
31
|
+
}
|
|
32
|
+
error(message, context) {
|
|
33
|
+
this.write("error", message, context);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
var appLogger = new Logger("app");
|
|
37
|
+
export {
|
|
38
|
+
appLogger,
|
|
39
|
+
Logger
|
|
40
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// ../../src/core/queue/index.ts
|
|
3
|
+
class Job {
|
|
4
|
+
maxAttempts;
|
|
5
|
+
backoffMs;
|
|
6
|
+
priority;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
class SyncQueue {
|
|
10
|
+
async dispatch(job, payload) {
|
|
11
|
+
await job.handle(payload);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
class AsyncQueue {
|
|
16
|
+
async dispatch(job, payload) {
|
|
17
|
+
setTimeout(() => {
|
|
18
|
+
job.handle(payload).catch((error) => {
|
|
19
|
+
console.error("[AsyncQueue] Job failed:", error);
|
|
20
|
+
});
|
|
21
|
+
}, 0);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function createQueue(driver) {
|
|
25
|
+
return driver === "async" ? new AsyncQueue : new SyncQueue;
|
|
26
|
+
}
|
|
27
|
+
export {
|
|
28
|
+
createQueue,
|
|
29
|
+
SyncQueue,
|
|
30
|
+
Job,
|
|
31
|
+
AsyncQueue
|
|
32
|
+
};
|
|
@@ -48,7 +48,9 @@ export type { QueryJoin, QueryJoinOn, QueryOptions, QueryOrder, QuerySelectItem,
|
|
|
48
48
|
export type { WhereNode } from "../core/database/whereBuilder.ts";
|
|
49
49
|
export { WhereBuilder } from "../core/database/whereBuilder.ts";
|
|
50
50
|
export { BadRequestError, ConflictError, ForbiddenError, NotFoundError, PreconditionFailedError, UnauthorizedError, UnprocessableEntityError, ValidationError, } from "../core/errors/http.ts";
|
|
51
|
-
export {
|
|
51
|
+
export type { EventListener } from "../core/events/eventBus.ts";
|
|
52
|
+
export { EventBus, eventBus } from "../core/events/eventBus.ts";
|
|
53
|
+
export { modelEventName } from "../core/events/index.ts";
|
|
52
54
|
export { auth, cache, config, events, log, mail, policyGate, queue, storage, } from "../core/facades/index.ts";
|
|
53
55
|
export { createBodySizeLimitMiddleware } from "../core/http/bodySizeLimitMiddleware.ts";
|
|
54
56
|
export { conditionalJsonResponse } from "../core/http/conditionalResponse.ts";
|
package/dist/index.js
CHANGED
|
@@ -6770,6 +6770,7 @@ export {
|
|
|
6770
6770
|
morphTo,
|
|
6771
6771
|
morphOne,
|
|
6772
6772
|
morphMany,
|
|
6773
|
+
modelEventName,
|
|
6773
6774
|
minLength,
|
|
6774
6775
|
migrateDatabase,
|
|
6775
6776
|
maxLength,
|
|
@@ -6812,6 +6813,7 @@ export {
|
|
|
6812
6813
|
formatAdminValue,
|
|
6813
6814
|
filterMassAssignable,
|
|
6814
6815
|
events,
|
|
6816
|
+
eventBus,
|
|
6815
6817
|
etagFromResource,
|
|
6816
6818
|
emptyPaginateResult,
|
|
6817
6819
|
emailRule,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@getstrata/core",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.36",
|
|
4
4
|
"description": "Strata — Laravel-inspired Bun framework public API",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -85,6 +85,11 @@
|
|
|
85
85
|
"import": "./dist/entries/auth/tokenHash.js",
|
|
86
86
|
"default": "./dist/entries/auth/tokenHash.js"
|
|
87
87
|
},
|
|
88
|
+
"./audit/exportAuditLogs": {
|
|
89
|
+
"types": "./dist/core/audit/exportAuditLogs.d.ts",
|
|
90
|
+
"import": "./dist/entries/audit/exportAuditLogs.js",
|
|
91
|
+
"default": "./dist/entries/audit/exportAuditLogs.js"
|
|
92
|
+
},
|
|
88
93
|
"./cache/tags": {
|
|
89
94
|
"types": "./dist/core/cache/tags.d.ts",
|
|
90
95
|
"import": "./dist/entries/cache/tags.js",
|
|
@@ -150,6 +155,11 @@
|
|
|
150
155
|
"import": "./dist/entries/errors/http.js",
|
|
151
156
|
"default": "./dist/entries/errors/http.js"
|
|
152
157
|
},
|
|
158
|
+
"./events": {
|
|
159
|
+
"types": "./dist/core/events/index.d.ts",
|
|
160
|
+
"import": "./dist/entries/events.js",
|
|
161
|
+
"default": "./dist/entries/events.js"
|
|
162
|
+
},
|
|
153
163
|
"./http": {
|
|
154
164
|
"types": "./dist/core/http/index.d.ts",
|
|
155
165
|
"import": "./dist/entries/http.js",
|
|
@@ -215,11 +225,21 @@
|
|
|
215
225
|
"import": "./dist/entries/jobs/dispatchWebhookJob.js",
|
|
216
226
|
"default": "./dist/entries/jobs/dispatchWebhookJob.js"
|
|
217
227
|
},
|
|
228
|
+
"./jobs/invalidateCacheTagsJob": {
|
|
229
|
+
"types": "./dist/core/jobs/invalidateCacheTagsJob.d.ts",
|
|
230
|
+
"import": "./dist/entries/jobs/invalidateCacheTagsJob.js",
|
|
231
|
+
"default": "./dist/entries/jobs/invalidateCacheTagsJob.js"
|
|
232
|
+
},
|
|
218
233
|
"./lifecycle/gracefulShutdown": {
|
|
219
234
|
"types": "./dist/core/lifecycle/gracefulShutdown.d.ts",
|
|
220
235
|
"import": "./dist/entries/lifecycle/gracefulShutdown.js",
|
|
221
236
|
"default": "./dist/entries/lifecycle/gracefulShutdown.js"
|
|
222
237
|
},
|
|
238
|
+
"./logging/logger": {
|
|
239
|
+
"types": "./dist/core/logging/logger.d.ts",
|
|
240
|
+
"import": "./dist/entries/logging/logger.js",
|
|
241
|
+
"default": "./dist/entries/logging/logger.js"
|
|
242
|
+
},
|
|
223
243
|
"./metrics/prometheus": {
|
|
224
244
|
"types": "./dist/core/metrics/prometheus.d.ts",
|
|
225
245
|
"import": "./dist/entries/metrics/prometheus.js",
|
|
@@ -235,6 +255,11 @@
|
|
|
235
255
|
"import": "./dist/entries/pagination.js",
|
|
236
256
|
"default": "./dist/entries/pagination.js"
|
|
237
257
|
},
|
|
258
|
+
"./queue": {
|
|
259
|
+
"types": "./dist/core/queue/index.d.ts",
|
|
260
|
+
"import": "./dist/entries/queue.js",
|
|
261
|
+
"default": "./dist/entries/queue.js"
|
|
262
|
+
},
|
|
238
263
|
"./queue/createAppQueue": {
|
|
239
264
|
"types": "./dist/core/queue/createAppQueue.d.ts",
|
|
240
265
|
"import": "./dist/entries/queue/createAppQueue.js",
|
|
@@ -357,7 +382,7 @@
|
|
|
357
382
|
"build:bundle": "bun build index.ts --outdir dist --target bun --external bun --external eta",
|
|
358
383
|
"build:types": "tsc -p tsconfig.types.json",
|
|
359
384
|
"prepublishOnly": "bun run build && bun ../../scripts/prepare-core-package-publish.ts",
|
|
360
|
-
"build:subpaths": "bun build entries/auth/oauth/oidcProvider.ts entries/auth/oauth/providers.ts entries/auth/oauth/samlProvider.ts entries/auth/oauth/types.ts entries/auth/password.ts entries/auth/sessionCookie.ts entries/auth/tokenHash.ts entries/cache/tags.ts entries/cache/createCacheStore.ts entries/config/envSchema.ts entries/contracts/serviceTokens.ts entries/contracts/container.ts entries/contracts/di.ts entries/crypto/fieldEncryption.ts entries/crypto/mfaSecret.ts entries/database/factory.ts entries/database/seeders.ts entries/database/types.ts entries/errors/http.ts entries/http/bodySizeLimitMiddleware.ts entries/http/contentNegotiation.ts entries/http/csrfToken.ts entries/http/etag.ts entries/http/flashSession.ts entries/http/parseFormBody.ts entries/http/resources.ts entries/http/webErrorResponse.ts entries/http/webFormRequest.ts entries/jobs/dispatchWebhookJob.ts entries/lifecycle/gracefulShutdown.ts entries/metrics/prometheus.ts entries/openapi/registeredRoute.ts entries/pagination.ts entries/queue/createAppQueue.ts entries/queue/failedJobService.ts entries/queue/jobRunner.ts entries/queue/queueMetrics.ts entries/queue/publicQueue.ts entries/queue/types.ts entries/scheduler/schedule.ts entries/security/oauthState.ts entries/security/publicReads.ts entries/security/safeUrl.ts entries/security/stripeWebhook.ts entries/security/tokenExpiry.ts entries/security/totp.ts entries/storage/storage.ts entries/validation/rules.ts entries/view.ts --outdir dist --root . --target bun --external bun --external eta",
|
|
385
|
+
"build:subpaths": "bun build entries/auth/oauth/oidcProvider.ts entries/auth/oauth/providers.ts entries/auth/oauth/samlProvider.ts entries/auth/oauth/types.ts entries/auth/password.ts entries/auth/sessionCookie.ts entries/auth/tokenHash.ts entries/audit/exportAuditLogs.ts entries/cache/tags.ts entries/cache/createCacheStore.ts entries/config/envSchema.ts entries/contracts/serviceTokens.ts entries/contracts/container.ts entries/contracts/di.ts entries/crypto/fieldEncryption.ts entries/crypto/mfaSecret.ts entries/database/factory.ts entries/database/seeders.ts entries/database/types.ts entries/errors/http.ts entries/http/bodySizeLimitMiddleware.ts entries/http/contentNegotiation.ts entries/http/csrfToken.ts entries/http/etag.ts entries/http/flashSession.ts entries/http/parseFormBody.ts entries/http/resources.ts entries/http/webErrorResponse.ts entries/http/webFormRequest.ts entries/jobs/dispatchWebhookJob.ts entries/jobs/invalidateCacheTagsJob.ts entries/lifecycle/gracefulShutdown.ts entries/logging/logger.ts entries/metrics/prometheus.ts entries/openapi/registeredRoute.ts entries/pagination.ts entries/queue.ts entries/queue/createAppQueue.ts entries/queue/failedJobService.ts entries/queue/jobRunner.ts entries/queue/queueMetrics.ts entries/queue/publicQueue.ts entries/queue/types.ts entries/scheduler/schedule.ts entries/security/oauthState.ts entries/security/publicReads.ts entries/security/safeUrl.ts entries/security/stripeWebhook.ts entries/security/tokenExpiry.ts entries/security/totp.ts entries/storage/storage.ts entries/validation/rules.ts entries/view.ts --outdir dist --root . --target bun --external bun --external eta",
|
|
361
386
|
"build:shims": "bun ../../scripts/write-core-shared-shims.ts"
|
|
362
387
|
},
|
|
363
388
|
"publishConfig": {
|