@getstrata/core 0.5.40 → 0.5.42
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/core/cache/simpleCache.d.ts +1 -0
- package/dist/core/cache/simpleCacheStore.d.ts +1 -0
- package/dist/core/database/baseRepository.d.ts +1 -0
- package/dist/core/queue/failedJobRepository.d.ts +1 -0
- package/dist/core/queue/failedJobService.d.ts +1 -0
- package/dist/entries/audit/exportAuditLogs.js +18 -0
- package/dist/entries/auth/sessionGuard.js +443 -0
- package/dist/entries/cache/simpleCache.js +178 -0
- package/dist/entries/cache/simpleCacheStore.js +42 -0
- package/dist/entries/database/baseRepository.js +1388 -0
- package/dist/entries/database/bindConnection.js +22 -0
- package/dist/entries/database/boundConnection.js +19 -0
- package/dist/entries/database/connection.js +12 -0
- package/dist/entries/database/errors.js +128 -0
- package/dist/entries/database/model.js +948 -0
- package/dist/entries/database/query.js +436 -0
- package/dist/entries/database/relationships.js +162 -0
- package/dist/entries/database/table.js +8 -0
- package/dist/entries/database/transaction.js +129 -0
- package/dist/entries/http/authMiddleware.js +47 -0
- package/dist/entries/http/authorizeMiddleware.js +104 -0
- package/dist/entries/http/metricsMiddleware.js +91 -0
- package/dist/entries/http/parseMultipartUpload.js +144 -0
- package/dist/entries/http/securedRouteModelBinding.js +6 -0
- package/dist/entries/http/webErrorResponse.js +443 -0
- package/dist/entries/http/webFormRequest.js +6 -0
- package/dist/entries/jobs/dispatchWebhookJob.js +18 -0
- package/dist/entries/queue/createAppQueue.js +449 -0
- package/dist/entries/queue/failedJobRepository.js +2306 -0
- package/dist/entries/queue/failedJobService.js +3 -0
- package/dist/entries/queue/publicQueue.js +449 -0
- package/dist/entries/queue/queueMetrics.js +449 -0
- package/dist/entries/queue/redisQueue.js +232 -0
- package/dist/entries/security/scimTenantTokens.js +51 -0
- package/dist/entries/tenant/tenantDatabaseScope.js +113 -0
- package/dist/entries/view.js +443 -0
- package/package.json +102 -2
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// ../../src/core/queue/redisQueue.ts
|
|
3
|
+
var {RedisClient } = globalThis.Bun;
|
|
4
|
+
|
|
5
|
+
// ../../src/core/queue/jobRegistry.ts
|
|
6
|
+
class JobRegistry {
|
|
7
|
+
factories = new Map;
|
|
8
|
+
instances = new WeakMap;
|
|
9
|
+
register(name, factory) {
|
|
10
|
+
this.factories.set(name, factory);
|
|
11
|
+
}
|
|
12
|
+
resolveName(job) {
|
|
13
|
+
return this.instances.get(job);
|
|
14
|
+
}
|
|
15
|
+
track(name, job) {
|
|
16
|
+
this.instances.set(job, name);
|
|
17
|
+
return job;
|
|
18
|
+
}
|
|
19
|
+
create(name) {
|
|
20
|
+
const factory = this.factories.get(name);
|
|
21
|
+
if (!factory) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
return factory();
|
|
25
|
+
}
|
|
26
|
+
names() {
|
|
27
|
+
return [...this.factories.keys()];
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
var JOB_REGISTRY_KEY = Symbol.for("@getstrata/jobRegistry");
|
|
31
|
+
function readSharedJobRegistry() {
|
|
32
|
+
const globalRegistry = globalThis[JOB_REGISTRY_KEY];
|
|
33
|
+
if (globalRegistry) {
|
|
34
|
+
return globalRegistry;
|
|
35
|
+
}
|
|
36
|
+
const registry = new JobRegistry;
|
|
37
|
+
globalThis[JOB_REGISTRY_KEY] = registry;
|
|
38
|
+
return registry;
|
|
39
|
+
}
|
|
40
|
+
var jobRegistry = readSharedJobRegistry();
|
|
41
|
+
|
|
42
|
+
// ../../src/core/contracts/serviceTokens.ts
|
|
43
|
+
var CORE_CONFIG_TOKEN = "core.config";
|
|
44
|
+
var CORE_CACHE_TOKEN = "core.cache";
|
|
45
|
+
var CORE_QUEUE_TOKEN = "core.queue";
|
|
46
|
+
var CORE_EVENT_BUS_TOKEN = "core.eventBus";
|
|
47
|
+
var CORE_POLICY_GATE_TOKEN = "core.policyGate";
|
|
48
|
+
var CORE_AUTH_TOKEN = "core.auth";
|
|
49
|
+
var CORE_TOKEN_SERVICE_TOKEN = "core.tokenService";
|
|
50
|
+
// ../../src/bootstrap/config.ts
|
|
51
|
+
var DEFAULT_QUEUE_DRIVER = "sync";
|
|
52
|
+
|
|
53
|
+
// ../../src/config/queue.ts
|
|
54
|
+
var queueConfig = {
|
|
55
|
+
driver: process.env.QUEUE_DRIVER === "redis" || process.env.QUEUE_DRIVER === "async" || process.env.QUEUE_DRIVER === "sync" ? process.env.QUEUE_DRIVER : DEFAULT_QUEUE_DRIVER,
|
|
56
|
+
maxAttempts: Number(process.env.QUEUE_MAX_ATTEMPTS ?? "3"),
|
|
57
|
+
backoffMs: Number(process.env.QUEUE_BACKOFF_MS ?? "1000")
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
// ../../src/core/queue/jobRunner.ts
|
|
61
|
+
async function runQueueJob(envelope, failedJobs) {
|
|
62
|
+
const job = jobRegistry.create(envelope.name);
|
|
63
|
+
if (!job) {
|
|
64
|
+
throw new Error(`Unknown job "${envelope.name}".`);
|
|
65
|
+
}
|
|
66
|
+
const attempts = envelope.attempts ?? 0;
|
|
67
|
+
try {
|
|
68
|
+
await job.handle(envelope.payload);
|
|
69
|
+
} catch (error) {
|
|
70
|
+
const nextAttempt = attempts + 1;
|
|
71
|
+
const maxAttempts = job.maxAttempts ?? queueConfig.maxAttempts;
|
|
72
|
+
if (nextAttempt < maxAttempts) {
|
|
73
|
+
const backoffMs = job.backoffMs ?? queueConfig.backoffMs;
|
|
74
|
+
await new Promise((resolve) => setTimeout(resolve, backoffMs * nextAttempt));
|
|
75
|
+
await runQueueJob({
|
|
76
|
+
...envelope,
|
|
77
|
+
attempts: nextAttempt
|
|
78
|
+
}, failedJobs);
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
await failedJobs.recordFailure({
|
|
82
|
+
jobName: envelope.name,
|
|
83
|
+
payload: envelope.payload,
|
|
84
|
+
exception: error instanceof Error ? error.stack ?? error.message : String(error)
|
|
85
|
+
});
|
|
86
|
+
throw error;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// ../../src/core/queue/redisQueue.ts
|
|
91
|
+
var QUEUE_LIST_KEY = "workhub:queue:default";
|
|
92
|
+
var QUEUE_HIGH_KEY = "workhub:queue:high";
|
|
93
|
+
var QUEUE_LOW_KEY = "workhub:queue:low";
|
|
94
|
+
var QUEUE_KEYS = [QUEUE_HIGH_KEY, QUEUE_LIST_KEY, QUEUE_LOW_KEY];
|
|
95
|
+
function queueKeyForPriority(priority = "default") {
|
|
96
|
+
switch (priority) {
|
|
97
|
+
case "high":
|
|
98
|
+
return QUEUE_HIGH_KEY;
|
|
99
|
+
case "low":
|
|
100
|
+
return QUEUE_LOW_KEY;
|
|
101
|
+
default:
|
|
102
|
+
return QUEUE_LIST_KEY;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
function parseQueueJobEnvelope(rawPayload) {
|
|
106
|
+
let parsed;
|
|
107
|
+
try {
|
|
108
|
+
parsed = JSON.parse(rawPayload);
|
|
109
|
+
} catch {
|
|
110
|
+
console.error("[QueueWorker] Ignoring malformed queue payload");
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
if (!parsed || typeof parsed !== "object") {
|
|
114
|
+
console.error("[QueueWorker] Ignoring non-object queue payload");
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
const envelope = parsed;
|
|
118
|
+
if (typeof envelope.name !== "string" || envelope.name.length === 0) {
|
|
119
|
+
console.error("[QueueWorker] Ignoring queue payload without job name");
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
if (!jobRegistry.create(envelope.name)) {
|
|
123
|
+
console.error(`[QueueWorker] Ignoring unknown job name: ${envelope.name}`);
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
if (envelope.payload !== undefined && (typeof envelope.payload !== "object" || envelope.payload === null)) {
|
|
127
|
+
console.error("[QueueWorker] Ignoring queue payload with invalid payload object");
|
|
128
|
+
return null;
|
|
129
|
+
}
|
|
130
|
+
return {
|
|
131
|
+
name: envelope.name,
|
|
132
|
+
payload: envelope.payload ?? {},
|
|
133
|
+
attempts: typeof envelope.attempts === "number" ? envelope.attempts : 0
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
class RedisQueue {
|
|
138
|
+
client;
|
|
139
|
+
constructor(redisUrl) {
|
|
140
|
+
this.client = new RedisClient(redisUrl);
|
|
141
|
+
}
|
|
142
|
+
async dispatch(job, payload) {
|
|
143
|
+
const name = jobRegistry.resolveName(job);
|
|
144
|
+
if (!name) {
|
|
145
|
+
throw new Error("Job is not registered with the queue worker registry.");
|
|
146
|
+
}
|
|
147
|
+
const envelope = {
|
|
148
|
+
name,
|
|
149
|
+
payload,
|
|
150
|
+
attempts: 0
|
|
151
|
+
};
|
|
152
|
+
const queueKey = queueKeyForPriority(job.priority);
|
|
153
|
+
await this.client.lpush(queueKey, JSON.stringify(envelope));
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
class QueueWorker {
|
|
158
|
+
failedJobs;
|
|
159
|
+
timeoutSeconds;
|
|
160
|
+
running = false;
|
|
161
|
+
stopping = false;
|
|
162
|
+
client;
|
|
163
|
+
constructor(redisUrl, failedJobs, timeoutSeconds = 5) {
|
|
164
|
+
this.failedJobs = failedJobs;
|
|
165
|
+
this.timeoutSeconds = timeoutSeconds;
|
|
166
|
+
this.client = new RedisClient(redisUrl);
|
|
167
|
+
}
|
|
168
|
+
requestStop() {
|
|
169
|
+
this.stopping = true;
|
|
170
|
+
}
|
|
171
|
+
isRunning() {
|
|
172
|
+
return this.running;
|
|
173
|
+
}
|
|
174
|
+
async processNext() {
|
|
175
|
+
let result = null;
|
|
176
|
+
for (const queueKey of QUEUE_KEYS) {
|
|
177
|
+
result = await this.client.brpop(queueKey, 1);
|
|
178
|
+
if (result) {
|
|
179
|
+
break;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
if (!result) {
|
|
183
|
+
result = await this.client.brpop(QUEUE_LIST_KEY, this.timeoutSeconds);
|
|
184
|
+
}
|
|
185
|
+
if (!result) {
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
const [, rawPayload] = result;
|
|
189
|
+
const envelope = parseQueueJobEnvelope(rawPayload);
|
|
190
|
+
if (!envelope) {
|
|
191
|
+
return true;
|
|
192
|
+
}
|
|
193
|
+
try {
|
|
194
|
+
await runQueueJob(envelope, this.failedJobs);
|
|
195
|
+
} catch (error) {
|
|
196
|
+
console.error("[QueueWorker] Job failed:", error);
|
|
197
|
+
}
|
|
198
|
+
return true;
|
|
199
|
+
}
|
|
200
|
+
async run() {
|
|
201
|
+
this.running = true;
|
|
202
|
+
while (!this.stopping) {
|
|
203
|
+
await this.processNext();
|
|
204
|
+
}
|
|
205
|
+
this.running = false;
|
|
206
|
+
}
|
|
207
|
+
close() {
|
|
208
|
+
this.client.close();
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
async function countPendingQueueJobs(redisUrl) {
|
|
212
|
+
const client = new RedisClient(redisUrl);
|
|
213
|
+
try {
|
|
214
|
+
let total = 0;
|
|
215
|
+
for (const queueKey of QUEUE_KEYS) {
|
|
216
|
+
total += await client.llen(queueKey);
|
|
217
|
+
}
|
|
218
|
+
return total;
|
|
219
|
+
} finally {
|
|
220
|
+
client.close();
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
export {
|
|
224
|
+
queueKeyForPriority,
|
|
225
|
+
parseQueueJobEnvelope,
|
|
226
|
+
countPendingQueueJobs,
|
|
227
|
+
RedisQueue,
|
|
228
|
+
QueueWorker,
|
|
229
|
+
QUEUE_LOW_KEY,
|
|
230
|
+
QUEUE_LIST_KEY,
|
|
231
|
+
QUEUE_HIGH_KEY
|
|
232
|
+
};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// ../../src/domain/scim.ts
|
|
3
|
+
var TEST_SCIM_BEARER_TOKEN = "workhub-scim-test-token";
|
|
4
|
+
|
|
5
|
+
// ../../src/core/security/timingSafeCompare.ts
|
|
6
|
+
import { timingSafeEqual } from "crypto";
|
|
7
|
+
function timingSafeCompareString(left, right) {
|
|
8
|
+
const leftBuffer = Buffer.from(left);
|
|
9
|
+
const rightBuffer = Buffer.from(right);
|
|
10
|
+
if (leftBuffer.length !== rightBuffer.length) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
return timingSafeEqual(leftBuffer, rightBuffer);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// ../../src/core/security/scimTenantTokens.ts
|
|
17
|
+
function parseScimTenantTokens(raw) {
|
|
18
|
+
const tokens = new Map;
|
|
19
|
+
if (!raw?.trim()) {
|
|
20
|
+
return tokens;
|
|
21
|
+
}
|
|
22
|
+
for (const entry of raw.split(",")) {
|
|
23
|
+
const [tenantPart, tokenPart] = entry.split(":");
|
|
24
|
+
if (!tenantPart || !tokenPart) {
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
const tenantId = Number.parseInt(tenantPart.trim(), 10);
|
|
28
|
+
const token = tokenPart.trim();
|
|
29
|
+
if (Number.isInteger(tenantId) && tenantId > 0 && token.length > 0) {
|
|
30
|
+
tokens.set(tenantId, token);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return tokens;
|
|
34
|
+
}
|
|
35
|
+
function resolveScimTenantFromToken(token) {
|
|
36
|
+
const tenantTokens = parseScimTenantTokens(process.env.SCIM_TENANT_TOKENS);
|
|
37
|
+
for (const [tenantId, expectedToken] of tenantTokens) {
|
|
38
|
+
if (timingSafeCompareString(token, expectedToken)) {
|
|
39
|
+
return tenantId;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
const fallbackToken = process.env.SCIM_BEARER_TOKEN ?? TEST_SCIM_BEARER_TOKEN;
|
|
43
|
+
if (timingSafeCompareString(token, fallbackToken)) {
|
|
44
|
+
return 1;
|
|
45
|
+
}
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
export {
|
|
49
|
+
resolveScimTenantFromToken,
|
|
50
|
+
parseScimTenantTokens
|
|
51
|
+
};
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// ../../src/core/runtime/asyncContextStore.ts
|
|
3
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
4
|
+
function createAsyncContextStore(key) {
|
|
5
|
+
const symbol = Symbol.for(key);
|
|
6
|
+
const globalRecord = globalThis;
|
|
7
|
+
const existing = globalRecord[symbol];
|
|
8
|
+
if (existing) {
|
|
9
|
+
return existing;
|
|
10
|
+
}
|
|
11
|
+
const store = new AsyncLocalStorage;
|
|
12
|
+
globalRecord[symbol] = store;
|
|
13
|
+
return store;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// ../../src/core/database/connectionContext.ts
|
|
17
|
+
var activeConnection = createAsyncContextStore("@getstrata/databaseConnectionContext");
|
|
18
|
+
function runWithDatabaseConnection(connection, callback) {
|
|
19
|
+
return activeConnection.run(connection, callback);
|
|
20
|
+
}
|
|
21
|
+
function getActiveDatabaseConnection(fallback) {
|
|
22
|
+
return activeConnection.getStore() ?? fallback;
|
|
23
|
+
}
|
|
24
|
+
function hasActiveDatabaseConnection() {
|
|
25
|
+
return activeConnection.getStore() !== undefined;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// ../../src/core/database/queryProxy.ts
|
|
29
|
+
var POOL_CONNECTION_METHODS = new Set(["begin", "close", "connect"]);
|
|
30
|
+
function createDatabaseQueryProxy(pool) {
|
|
31
|
+
function resolveDatabase() {
|
|
32
|
+
return getActiveDatabaseConnection(pool);
|
|
33
|
+
}
|
|
34
|
+
function resolveDatabaseForProperty(property) {
|
|
35
|
+
if (typeof property === "string" && POOL_CONNECTION_METHODS.has(property)) {
|
|
36
|
+
return pool;
|
|
37
|
+
}
|
|
38
|
+
return resolveDatabase();
|
|
39
|
+
}
|
|
40
|
+
return new Proxy(function database() {}, {
|
|
41
|
+
apply(_target, _thisArg, args) {
|
|
42
|
+
return resolveDatabase()(...args);
|
|
43
|
+
},
|
|
44
|
+
get(_target, property) {
|
|
45
|
+
const connection = resolveDatabaseForProperty(property);
|
|
46
|
+
const value = connection[property];
|
|
47
|
+
return typeof value === "function" ? value.bind(connection) : value;
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// ../../src/core/database/defaultConnection.ts
|
|
53
|
+
var defaultPool = {
|
|
54
|
+
connection: null
|
|
55
|
+
};
|
|
56
|
+
var defaultQuery = {
|
|
57
|
+
connection: null
|
|
58
|
+
};
|
|
59
|
+
function registerDefaultDatabasePool(connection) {
|
|
60
|
+
defaultPool.connection = connection;
|
|
61
|
+
defaultQuery.connection = createDatabaseQueryProxy(connection);
|
|
62
|
+
}
|
|
63
|
+
function getDefaultDatabasePool() {
|
|
64
|
+
if (!defaultPool.connection) {
|
|
65
|
+
throw new Error("Default database pool is not registered. Call registerDefaultDatabasePool() during app bootstrap.");
|
|
66
|
+
}
|
|
67
|
+
return defaultPool.connection;
|
|
68
|
+
}
|
|
69
|
+
function getDefaultDatabaseQuery() {
|
|
70
|
+
if (!defaultQuery.connection) {
|
|
71
|
+
throw new Error("Default database query handle is not registered. Call registerDefaultDatabasePool() during app bootstrap.");
|
|
72
|
+
}
|
|
73
|
+
return defaultQuery.connection;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// ../../src/core/tenant/tenantContext.ts
|
|
77
|
+
var tenantContext = createAsyncContextStore("@getstrata/tenantContext");
|
|
78
|
+
function runWithTenant(tenant, callback) {
|
|
79
|
+
return tenantContext.run(tenant, callback);
|
|
80
|
+
}
|
|
81
|
+
function currentTenant() {
|
|
82
|
+
return tenantContext.getStore() ?? null;
|
|
83
|
+
}
|
|
84
|
+
function currentTenantId() {
|
|
85
|
+
return currentTenant()?.id ?? 1;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// ../../src/core/tenant/tenantDatabaseScope.ts
|
|
89
|
+
async function applyTenantContextToTransaction(transaction, tenantId) {
|
|
90
|
+
await transaction.unsafe(`SELECT set_config('app.tenant_id', $1, true)`, [String(tenantId)]);
|
|
91
|
+
await transaction.unsafe(`SELECT set_config('app.bypass_rls', $1, true)`, ["false"]);
|
|
92
|
+
}
|
|
93
|
+
async function runWithTenantDatabase(tenant, callback) {
|
|
94
|
+
if (hasActiveDatabaseConnection()) {
|
|
95
|
+
const activeConnection2 = getActiveDatabaseConnection(getDefaultDatabasePool());
|
|
96
|
+
await applyTenantContextToTransaction(activeConnection2, tenant.id);
|
|
97
|
+
return await runWithTenant(tenant, callback);
|
|
98
|
+
}
|
|
99
|
+
return await getDefaultDatabasePool().begin(async (transaction) => {
|
|
100
|
+
await applyTenantContextToTransaction(transaction, tenant.id);
|
|
101
|
+
return await runWithDatabaseConnection(transaction, async () => {
|
|
102
|
+
return await runWithTenant(tenant, callback);
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
function isInsideTenantDatabaseScope(tenantId = currentTenant()?.id) {
|
|
107
|
+
return hasActiveDatabaseConnection() && currentTenant()?.id === tenantId;
|
|
108
|
+
}
|
|
109
|
+
export {
|
|
110
|
+
runWithTenantDatabase,
|
|
111
|
+
isInsideTenantDatabaseScope,
|
|
112
|
+
applyTenantContextToTransaction
|
|
113
|
+
};
|