@getstrata/core 0.5.42 → 0.5.43
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/admin/formatValue.js +32 -0
- package/dist/entries/admin/registry.js +32 -0
- package/dist/entries/audit/siemFormatter.js +37 -0
- package/dist/entries/auth/scimAuthMiddleware.js +241 -0
- package/dist/entries/auth/sessionGuard.js +58 -0
- package/dist/entries/database/schema.js +1054 -0
- package/dist/entries/http/webErrorResponse.js +58 -0
- package/dist/entries/mail/mailer.js +208 -0
- package/dist/entries/mail/markdownMail.js +63 -0
- package/dist/entries/mail/markdownMailable.js +78 -0
- package/dist/entries/notifications.js +152 -0
- package/dist/entries/openapi/generator.js +178 -0
- package/dist/entries/openapi/validate.js +28 -0
- package/dist/entries/queue/createAppQueue.js +58 -0
- package/dist/entries/queue/failedJobRepository.js +58 -0
- package/dist/entries/queue/publicQueue.js +58 -0
- package/dist/entries/queue/queueMetrics.js +58 -0
- package/dist/entries/runtime/asyncContextStore.js +17 -0
- package/dist/entries/security/safeFetch.js +211 -0
- package/dist/entries/security/timingSafeCompare.js +14 -0
- package/dist/entries/tenant/databaseTenantContext.js +116 -0
- package/dist/entries/view.js +58 -0
- package/package.json +77 -2
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// ../../src/core/admin/formatValue.ts
|
|
3
|
+
function formatAdminValue(value, type = "text") {
|
|
4
|
+
if (value === null || value === undefined) {
|
|
5
|
+
return "";
|
|
6
|
+
}
|
|
7
|
+
if (type === "boolean") {
|
|
8
|
+
return value ? "yes" : "no";
|
|
9
|
+
}
|
|
10
|
+
if (type === "number") {
|
|
11
|
+
return String(value);
|
|
12
|
+
}
|
|
13
|
+
if (type === "datetime") {
|
|
14
|
+
if (value instanceof Date) {
|
|
15
|
+
return value.toISOString();
|
|
16
|
+
}
|
|
17
|
+
return String(value);
|
|
18
|
+
}
|
|
19
|
+
if (type === "code") {
|
|
20
|
+
if (typeof value === "string") {
|
|
21
|
+
return value;
|
|
22
|
+
}
|
|
23
|
+
return JSON.stringify(value, null, 2);
|
|
24
|
+
}
|
|
25
|
+
if (typeof value === "object") {
|
|
26
|
+
return JSON.stringify(value);
|
|
27
|
+
}
|
|
28
|
+
return String(value);
|
|
29
|
+
}
|
|
30
|
+
export {
|
|
31
|
+
formatAdminValue
|
|
32
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// ../../src/core/admin/registry.ts
|
|
3
|
+
class AdminResourceRegistry {
|
|
4
|
+
resources = new Map;
|
|
5
|
+
constructor() {}
|
|
6
|
+
register(resource) {
|
|
7
|
+
if (this.resources.has(resource.name)) {
|
|
8
|
+
throw new Error(`Admin resource "${resource.name}" is already registered.`);
|
|
9
|
+
}
|
|
10
|
+
this.resources.set(resource.name, resource);
|
|
11
|
+
}
|
|
12
|
+
get(name) {
|
|
13
|
+
return this.resources.get(name);
|
|
14
|
+
}
|
|
15
|
+
list() {
|
|
16
|
+
const definitions = [];
|
|
17
|
+
for (const resource of this.resources.values()) {
|
|
18
|
+
const { handlers: _handlers, ...definition } = resource;
|
|
19
|
+
definitions.push(definition);
|
|
20
|
+
}
|
|
21
|
+
return definitions;
|
|
22
|
+
}
|
|
23
|
+
all() {
|
|
24
|
+
return [...this.resources.values()];
|
|
25
|
+
}
|
|
26
|
+
clear() {
|
|
27
|
+
this.resources.clear();
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
export {
|
|
31
|
+
AdminResourceRegistry
|
|
32
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// ../../src/core/audit/siemFormatter.ts
|
|
3
|
+
function formatSiemAuditEvent(input) {
|
|
4
|
+
return {
|
|
5
|
+
timestamp: input.created_at.toISOString(),
|
|
6
|
+
event_type: "workhub.audit",
|
|
7
|
+
actor_user_id: input.user_id,
|
|
8
|
+
tenant_id: input.tenant_id ?? null,
|
|
9
|
+
trace_id: input.trace_id ?? null,
|
|
10
|
+
action: input.action,
|
|
11
|
+
subject_type: input.subject_type,
|
|
12
|
+
subject_id: input.subject_id,
|
|
13
|
+
ip_address: input.ip_address ?? null,
|
|
14
|
+
user_agent: input.user_agent ?? null,
|
|
15
|
+
checksum: input.checksum ?? null,
|
|
16
|
+
payload: input.payload ?? {}
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
function formatCefLine(event) {
|
|
20
|
+
const extension = [
|
|
21
|
+
`rt=${event.timestamp}`,
|
|
22
|
+
`suid=${event.actor_user_id ?? "unknown"}`,
|
|
23
|
+
`cs1=${event.action}`,
|
|
24
|
+
`cs1Label=Action`,
|
|
25
|
+
`cs2=${event.subject_type}`,
|
|
26
|
+
`cs2Label=SubjectType`,
|
|
27
|
+
`cs3=${event.subject_id ?? ""}`,
|
|
28
|
+
`cs3Label=SubjectId`,
|
|
29
|
+
`src=${event.ip_address ?? ""}`,
|
|
30
|
+
`request=${event.trace_id ?? ""}`
|
|
31
|
+
].join(" ");
|
|
32
|
+
return `CEF:0|WorkHub|API|1.0|${event.action}|${event.subject_type}|5|${extension}`;
|
|
33
|
+
}
|
|
34
|
+
export {
|
|
35
|
+
formatSiemAuditEvent,
|
|
36
|
+
formatCefLine
|
|
37
|
+
};
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// ../../src/core/database/boundConnection.ts
|
|
3
|
+
var boundConnectionHolder = {
|
|
4
|
+
connection: null
|
|
5
|
+
};
|
|
6
|
+
function bindDatabaseConnection(connection) {
|
|
7
|
+
boundConnectionHolder.connection = connection;
|
|
8
|
+
}
|
|
9
|
+
function getBoundDatabaseConnection() {
|
|
10
|
+
return boundConnectionHolder.connection;
|
|
11
|
+
}
|
|
12
|
+
function resetBoundDatabaseConnection() {
|
|
13
|
+
boundConnectionHolder.connection = null;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// ../../src/core/database/bindConnection.ts
|
|
17
|
+
function bindDatabaseConnection2(connection) {
|
|
18
|
+
bindDatabaseConnection(connection);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// ../../src/core/runtime/asyncContextStore.ts
|
|
22
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
23
|
+
function createAsyncContextStore(key) {
|
|
24
|
+
const symbol = Symbol.for(key);
|
|
25
|
+
const globalRecord = globalThis;
|
|
26
|
+
const existing = globalRecord[symbol];
|
|
27
|
+
if (existing) {
|
|
28
|
+
return existing;
|
|
29
|
+
}
|
|
30
|
+
const store = new AsyncLocalStorage;
|
|
31
|
+
globalRecord[symbol] = store;
|
|
32
|
+
return store;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// ../../src/core/database/connectionContext.ts
|
|
36
|
+
var activeConnection = createAsyncContextStore("@getstrata/databaseConnectionContext");
|
|
37
|
+
function runWithDatabaseConnection(connection, callback) {
|
|
38
|
+
return activeConnection.run(connection, callback);
|
|
39
|
+
}
|
|
40
|
+
function getActiveDatabaseConnection(fallback) {
|
|
41
|
+
return activeConnection.getStore() ?? fallback;
|
|
42
|
+
}
|
|
43
|
+
function hasActiveDatabaseConnection() {
|
|
44
|
+
return activeConnection.getStore() !== undefined;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// ../../src/core/database/queryProxy.ts
|
|
48
|
+
var POOL_CONNECTION_METHODS = new Set(["begin", "close", "connect"]);
|
|
49
|
+
function createDatabaseQueryProxy(pool) {
|
|
50
|
+
function resolveDatabase() {
|
|
51
|
+
return getActiveDatabaseConnection(pool);
|
|
52
|
+
}
|
|
53
|
+
function resolveDatabaseForProperty(property) {
|
|
54
|
+
if (typeof property === "string" && POOL_CONNECTION_METHODS.has(property)) {
|
|
55
|
+
return pool;
|
|
56
|
+
}
|
|
57
|
+
return resolveDatabase();
|
|
58
|
+
}
|
|
59
|
+
return new Proxy(function database() {}, {
|
|
60
|
+
apply(_target, _thisArg, args) {
|
|
61
|
+
return resolveDatabase()(...args);
|
|
62
|
+
},
|
|
63
|
+
get(_target, property) {
|
|
64
|
+
const connection = resolveDatabaseForProperty(property);
|
|
65
|
+
const value = connection[property];
|
|
66
|
+
return typeof value === "function" ? value.bind(connection) : value;
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// ../../src/core/database/defaultConnection.ts
|
|
72
|
+
var defaultPool = {
|
|
73
|
+
connection: null
|
|
74
|
+
};
|
|
75
|
+
var defaultQuery = {
|
|
76
|
+
connection: null
|
|
77
|
+
};
|
|
78
|
+
function registerDefaultDatabasePool(connection) {
|
|
79
|
+
defaultPool.connection = connection;
|
|
80
|
+
defaultQuery.connection = createDatabaseQueryProxy(connection);
|
|
81
|
+
}
|
|
82
|
+
function getDefaultDatabasePool() {
|
|
83
|
+
if (!defaultPool.connection) {
|
|
84
|
+
throw new Error("Default database pool is not registered. Call registerDefaultDatabasePool() during app bootstrap.");
|
|
85
|
+
}
|
|
86
|
+
return defaultPool.connection;
|
|
87
|
+
}
|
|
88
|
+
function getDefaultDatabaseQuery() {
|
|
89
|
+
if (!defaultQuery.connection) {
|
|
90
|
+
throw new Error("Default database query handle is not registered. Call registerDefaultDatabasePool() during app bootstrap.");
|
|
91
|
+
}
|
|
92
|
+
return defaultQuery.connection;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// ../../src/domain/scim.ts
|
|
96
|
+
var TEST_SCIM_BEARER_TOKEN = "workhub-scim-test-token";
|
|
97
|
+
|
|
98
|
+
// ../../src/core/security/timingSafeCompare.ts
|
|
99
|
+
import { timingSafeEqual } from "crypto";
|
|
100
|
+
function timingSafeCompareString(left, right) {
|
|
101
|
+
const leftBuffer = Buffer.from(left);
|
|
102
|
+
const rightBuffer = Buffer.from(right);
|
|
103
|
+
if (leftBuffer.length !== rightBuffer.length) {
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
return timingSafeEqual(leftBuffer, rightBuffer);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// ../../src/core/security/scimTenantTokens.ts
|
|
110
|
+
function parseScimTenantTokens(raw) {
|
|
111
|
+
const tokens = new Map;
|
|
112
|
+
if (!raw?.trim()) {
|
|
113
|
+
return tokens;
|
|
114
|
+
}
|
|
115
|
+
for (const entry of raw.split(",")) {
|
|
116
|
+
const [tenantPart, tokenPart] = entry.split(":");
|
|
117
|
+
if (!tenantPart || !tokenPart) {
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
const tenantId = Number.parseInt(tenantPart.trim(), 10);
|
|
121
|
+
const token = tokenPart.trim();
|
|
122
|
+
if (Number.isInteger(tenantId) && tenantId > 0 && token.length > 0) {
|
|
123
|
+
tokens.set(tenantId, token);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return tokens;
|
|
127
|
+
}
|
|
128
|
+
function resolveScimTenantFromToken(token) {
|
|
129
|
+
const tenantTokens = parseScimTenantTokens(process.env.SCIM_TENANT_TOKENS);
|
|
130
|
+
for (const [tenantId, expectedToken] of tenantTokens) {
|
|
131
|
+
if (timingSafeCompareString(token, expectedToken)) {
|
|
132
|
+
return tenantId;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
const fallbackToken = process.env.SCIM_BEARER_TOKEN ?? TEST_SCIM_BEARER_TOKEN;
|
|
136
|
+
if (timingSafeCompareString(token, fallbackToken)) {
|
|
137
|
+
return 1;
|
|
138
|
+
}
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// ../../src/core/database/repositoryConnection.ts
|
|
143
|
+
function resolveRepositoryConnection() {
|
|
144
|
+
return getBoundDatabaseConnection() ?? getDefaultDatabaseQuery();
|
|
145
|
+
}
|
|
146
|
+
var repositoryConnection = new Proxy(function repositoryConnection2() {}, {
|
|
147
|
+
apply(_target, _thisArg, args) {
|
|
148
|
+
return resolveRepositoryConnection()(...args);
|
|
149
|
+
},
|
|
150
|
+
get(_target, property) {
|
|
151
|
+
const connection = resolveRepositoryConnection();
|
|
152
|
+
const value = connection[property];
|
|
153
|
+
return typeof value === "function" ? value.bind(connection) : value;
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
// ../../src/core/tenant/resolveTenant.ts
|
|
158
|
+
async function resolveTenant(tenantId) {
|
|
159
|
+
const rows = await repositoryConnection`
|
|
160
|
+
SELECT id, slug, plan, region
|
|
161
|
+
FROM tenant
|
|
162
|
+
WHERE id = ${tenantId}
|
|
163
|
+
LIMIT 1
|
|
164
|
+
`;
|
|
165
|
+
const row = rows[0];
|
|
166
|
+
return row ? { id: row.id, slug: row.slug, plan: row.plan, region: row.region ?? "eu" } : null;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// ../../src/core/tenant/tenantContext.ts
|
|
170
|
+
var tenantContext = createAsyncContextStore("@getstrata/tenantContext");
|
|
171
|
+
function runWithTenant(tenant, callback) {
|
|
172
|
+
return tenantContext.run(tenant, callback);
|
|
173
|
+
}
|
|
174
|
+
function currentTenant() {
|
|
175
|
+
return tenantContext.getStore() ?? null;
|
|
176
|
+
}
|
|
177
|
+
function currentTenantId() {
|
|
178
|
+
return currentTenant()?.id ?? 1;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// ../../src/core/tenant/tenantDatabaseScope.ts
|
|
182
|
+
async function applyTenantContextToTransaction(transaction, tenantId) {
|
|
183
|
+
await transaction.unsafe(`SELECT set_config('app.tenant_id', $1, true)`, [String(tenantId)]);
|
|
184
|
+
await transaction.unsafe(`SELECT set_config('app.bypass_rls', $1, true)`, ["false"]);
|
|
185
|
+
}
|
|
186
|
+
async function runWithTenantDatabase(tenant, callback) {
|
|
187
|
+
if (hasActiveDatabaseConnection()) {
|
|
188
|
+
const activeConnection2 = getActiveDatabaseConnection(getDefaultDatabasePool());
|
|
189
|
+
await applyTenantContextToTransaction(activeConnection2, tenant.id);
|
|
190
|
+
return await runWithTenant(tenant, callback);
|
|
191
|
+
}
|
|
192
|
+
return await getDefaultDatabasePool().begin(async (transaction) => {
|
|
193
|
+
await applyTenantContextToTransaction(transaction, tenant.id);
|
|
194
|
+
return await runWithDatabaseConnection(transaction, async () => {
|
|
195
|
+
return await runWithTenant(tenant, callback);
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
function isInsideTenantDatabaseScope(tenantId = currentTenant()?.id) {
|
|
200
|
+
return hasActiveDatabaseConnection() && currentTenant()?.id === tenantId;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// ../../src/core/auth/scimAuthMiddleware.ts
|
|
204
|
+
function createScimAuthMiddleware() {
|
|
205
|
+
return async (request, next) => {
|
|
206
|
+
const authorization = request.headers.get("authorization");
|
|
207
|
+
if (!authorization?.startsWith("Bearer ")) {
|
|
208
|
+
return jsonScimError("SCIM bearer token required.", 401);
|
|
209
|
+
}
|
|
210
|
+
const token = authorization.slice("Bearer ".length).trim();
|
|
211
|
+
const tenantId = resolveScimTenantFromToken(token);
|
|
212
|
+
if (tenantId === null) {
|
|
213
|
+
return jsonScimError("Invalid SCIM bearer token.", 401);
|
|
214
|
+
}
|
|
215
|
+
const tenant = await resolveTenant(tenantId);
|
|
216
|
+
if (!tenant) {
|
|
217
|
+
return jsonScimError("SCIM tenant not found.", 401);
|
|
218
|
+
}
|
|
219
|
+
return await runWithTenantDatabase(tenant, async () => {
|
|
220
|
+
bindDatabaseConnection2(getActiveDatabaseConnection(getDefaultDatabasePool()));
|
|
221
|
+
try {
|
|
222
|
+
return await next();
|
|
223
|
+
} finally {
|
|
224
|
+
resetBoundDatabaseConnection();
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
function jsonScimError(detail, status) {
|
|
230
|
+
return Response.json({
|
|
231
|
+
schemas: ["urn:ietf:params:scim:api:messages:2.0:Error"],
|
|
232
|
+
detail,
|
|
233
|
+
status: String(status)
|
|
234
|
+
}, {
|
|
235
|
+
status,
|
|
236
|
+
headers: { "content-type": "application/scim+json" }
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
export {
|
|
240
|
+
createScimAuthMiddleware
|
|
241
|
+
};
|
|
@@ -2131,6 +2131,45 @@ class Blueprint {
|
|
|
2131
2131
|
});
|
|
2132
2132
|
}
|
|
2133
2133
|
}
|
|
2134
|
+
// ../../src/core/database/schema/driver.ts
|
|
2135
|
+
function normalizeConnectionName(connection) {
|
|
2136
|
+
const normalized = connection.trim().toLowerCase();
|
|
2137
|
+
if (normalized === "pgsql" || normalized === "postgres" || normalized === "postgresql") {
|
|
2138
|
+
return "pgsql";
|
|
2139
|
+
}
|
|
2140
|
+
if (normalized === "mysql" || normalized === "mariadb") {
|
|
2141
|
+
return "mysql";
|
|
2142
|
+
}
|
|
2143
|
+
if (normalized === "sqlite") {
|
|
2144
|
+
return "sqlite";
|
|
2145
|
+
}
|
|
2146
|
+
throw new Error(`Unsupported DB_CONNECTION: ${connection}`);
|
|
2147
|
+
}
|
|
2148
|
+
function resolveDriverFromUrl(url) {
|
|
2149
|
+
const normalized = url.trim().toLowerCase();
|
|
2150
|
+
if (normalized.startsWith("postgres://") || normalized.startsWith("postgresql://") || normalized.startsWith("postgres:")) {
|
|
2151
|
+
return "pgsql";
|
|
2152
|
+
}
|
|
2153
|
+
if (normalized.startsWith("mysql://") || normalized.startsWith("mysql:")) {
|
|
2154
|
+
return "mysql";
|
|
2155
|
+
}
|
|
2156
|
+
if (normalized.startsWith("sqlite:")) {
|
|
2157
|
+
return "sqlite";
|
|
2158
|
+
}
|
|
2159
|
+
return null;
|
|
2160
|
+
}
|
|
2161
|
+
function resolveDatabaseDriver(options = {}) {
|
|
2162
|
+
const connection = options.connection ?? process.env.DB_CONNECTION;
|
|
2163
|
+
if (connection) {
|
|
2164
|
+
return normalizeConnectionName(connection);
|
|
2165
|
+
}
|
|
2166
|
+
const url = options.url ?? process.env.DATABASE_URL ?? "";
|
|
2167
|
+
const fromUrl = resolveDriverFromUrl(url);
|
|
2168
|
+
if (fromUrl) {
|
|
2169
|
+
return fromUrl;
|
|
2170
|
+
}
|
|
2171
|
+
return "pgsql";
|
|
2172
|
+
}
|
|
2134
2173
|
// ../../src/core/database/schema/errors.ts
|
|
2135
2174
|
class UnsupportedSchemaFeatureError extends Error {
|
|
2136
2175
|
constructor(feature, driver) {
|
|
@@ -2475,6 +2514,25 @@ class SchemaBuilder {
|
|
|
2475
2514
|
}
|
|
2476
2515
|
}
|
|
2477
2516
|
}
|
|
2517
|
+
|
|
2518
|
+
class Schema {
|
|
2519
|
+
static builder(driver) {
|
|
2520
|
+
return new SchemaBuilder(driver ?? resolveDatabaseDriver());
|
|
2521
|
+
}
|
|
2522
|
+
static async run(db, driver, callback) {
|
|
2523
|
+
const schema = Schema.builder(driver);
|
|
2524
|
+
await callback(schema);
|
|
2525
|
+
await schema.execute(db);
|
|
2526
|
+
}
|
|
2527
|
+
}
|
|
2528
|
+
function createSchemaBuilder(db, driver) {
|
|
2529
|
+
const builder = Schema.builder(driver);
|
|
2530
|
+
return Object.assign(builder, {
|
|
2531
|
+
async commit() {
|
|
2532
|
+
await builder.execute(db);
|
|
2533
|
+
}
|
|
2534
|
+
});
|
|
2535
|
+
}
|
|
2478
2536
|
// ../../src/core/database/table.ts
|
|
2479
2537
|
function defineTable(definition) {
|
|
2480
2538
|
return definition;
|