@getstrata/core 3.0.4 → 3.0.7

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.
Files changed (103) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/README.md +17 -9
  3. package/dist/core/auth/policy.d.ts +4 -1
  4. package/dist/core/auth/saml/samlServiceProvider.d.ts +2 -3
  5. package/dist/core/cache/createCacheStore.d.ts +1 -1
  6. package/dist/core/cache/expiration.d.ts +2 -0
  7. package/dist/core/cache/redisCacheScript.d.ts +1 -1
  8. package/dist/core/cache/redisCacheStore.d.ts +5 -5
  9. package/dist/core/cache/repository.d.ts +2 -0
  10. package/dist/core/cache/simpleCache.d.ts +5 -5
  11. package/dist/core/cache/simpleCacheStore.d.ts +3 -3
  12. package/dist/core/cache/store.d.ts +5 -4
  13. package/dist/core/cache/taggedCache.d.ts +2 -0
  14. package/dist/core/cache/tokens.d.ts +1 -1
  15. package/dist/core/config/envSchema.d.ts +13 -0
  16. package/dist/core/contracts/application.d.ts +4 -0
  17. package/dist/core/contracts/di.d.ts +2 -2
  18. package/dist/core/contracts/provider.d.ts +13 -0
  19. package/dist/core/contracts/serviceTokens.d.ts +12 -11
  20. package/dist/core/database/factory.d.ts +3 -0
  21. package/dist/core/database/index.d.ts +1 -1
  22. package/dist/core/database/migrations/declaration.d.ts +11 -0
  23. package/dist/core/database/migrations/runner.d.ts +2 -0
  24. package/dist/core/database/model.d.ts +16 -3
  25. package/dist/core/database/modelQuery.d.ts +1 -10
  26. package/dist/core/database/modelRelationLoader.d.ts +31 -0
  27. package/dist/core/database/mysqlConnection.d.ts +2 -2
  28. package/dist/core/database/mysqlNative.d.ts +2 -0
  29. package/dist/core/database/ormRuntime.d.ts +1 -1
  30. package/dist/core/database/rawMysql.d.ts +1 -1
  31. package/dist/core/events/eventBusClass.d.ts +6 -3
  32. package/dist/core/events/index.d.ts +1 -1
  33. package/dist/core/http/formRequest.d.ts +2 -0
  34. package/dist/core/http/loginThrottleMiddleware.d.ts +1 -2
  35. package/dist/core/http/memoryThrottleMiddleware.d.ts +1 -3
  36. package/dist/core/http/webFormRequest.d.ts +3 -5
  37. package/dist/core/mail/mailer.d.ts +6 -3
  38. package/dist/core/mail/markdownMailable.d.ts +2 -2
  39. package/dist/core/notifications/dispatcher.d.ts +5 -5
  40. package/dist/core/notifications/index.d.ts +1 -1
  41. package/dist/core/notifications/types.d.ts +9 -2
  42. package/dist/core/queue/index.d.ts +8 -3
  43. package/dist/core/queue/queueHandoff.d.ts +2 -2
  44. package/dist/core/runtime/application.d.ts +2 -0
  45. package/dist/core/security/oauthState.d.ts +12 -2
  46. package/dist/core/security/safeUrl.d.ts +1 -11
  47. package/dist/core/storage/storage.d.ts +23 -6
  48. package/dist/core/testing.d.ts +5 -0
  49. package/dist/core/testingState.d.ts +22 -0
  50. package/dist/entries/audit/exportAuditLogs.js +1 -0
  51. package/dist/entries/auth/intendedUrlCookie.js +106 -18
  52. package/dist/entries/auth/jwt.js +68 -6
  53. package/dist/entries/auth/jwtGuard.js +76 -12
  54. package/dist/entries/auth/oauth/samlProvider.js +4 -5
  55. package/dist/entries/auth/passwordConfirmCookie.js +69 -3
  56. package/dist/entries/auth/saml/samlServiceProvider.js +5 -7
  57. package/dist/entries/auth/sessionCookie.js +69 -6
  58. package/dist/entries/auth/sessionGuard.js +69 -6
  59. package/dist/entries/cache/createCacheStore.js +45 -23
  60. package/dist/entries/cache/repository.js +20 -5
  61. package/dist/entries/cache/simpleCache.js +21 -7
  62. package/dist/entries/cache/simpleCacheStore.js +2 -3
  63. package/dist/entries/config/envSchema.js +13 -0
  64. package/dist/entries/contracts/serviceTokens.js +2 -0
  65. package/dist/entries/database/bindings.js +19 -95
  66. package/dist/entries/database/factory.js +4 -0
  67. package/dist/entries/database/migrations.js +714 -1
  68. package/dist/entries/database/ownedReadOnlyObservation.js +1 -5
  69. package/dist/entries/database/sqliteConnection.js +1 -5
  70. package/dist/entries/http/flashMiddleware.js +33 -14
  71. package/dist/entries/http/flashSession.js +33 -14
  72. package/dist/entries/http/formRequest.js +12 -6
  73. package/dist/entries/http/memoryThrottleMiddleware.js +12 -11
  74. package/dist/entries/http/pagination.js +4 -4
  75. package/dist/entries/http/parseMultipartUpload.js +62 -9
  76. package/dist/entries/http/requirePasswordConfirmMiddleware.js +5 -2
  77. package/dist/entries/http/requireWebAuthMiddleware.js +5 -2
  78. package/dist/entries/http/routeModelBinding.js +4 -4
  79. package/dist/entries/http/safeInternalPath.js +5 -2
  80. package/dist/entries/http/scimThrottleMiddleware.js +5 -2
  81. package/dist/entries/http/securedRouteModelBinding.js +4 -4
  82. package/dist/entries/http/throttleMiddleware.js +5 -2
  83. package/dist/entries/http/uploads.js +62 -9
  84. package/dist/entries/http/validation.js +4 -4
  85. package/dist/entries/http/webFormRequest.js +74 -95
  86. package/dist/entries/jobs/exportAuditLogsJob.js +9 -4
  87. package/dist/entries/jobs/invalidateCacheTagsJob.js +9 -4
  88. package/dist/entries/queue/createAppQueue.js +10 -5
  89. package/dist/entries/queue/publicQueue.js +10 -5
  90. package/dist/entries/queue/queueWorkerTransport.js +10 -5
  91. package/dist/entries/queue.js +9 -4
  92. package/dist/entries/runtime/application.js +50 -3
  93. package/dist/entries/security/oauthState.js +61 -2
  94. package/dist/entries/security/tokenExpiry.js +62 -9
  95. package/dist/entries/storage/storage.js +102 -1
  96. package/dist/entries/testing.js +18 -0
  97. package/dist/framework/public-api.d.ts +16 -13
  98. package/dist/index.js +380 -266
  99. package/dist/internal/mysqlNative.js +97 -0
  100. package/dist/internal/ormRuntime.js +41 -4
  101. package/dist/internal/testingState.js +31 -0
  102. package/dist/types/services.d.ts +8 -1
  103. package/package.json +17 -4
@@ -6,13 +6,18 @@ class TaggedCache {
6
6
  constructor(store, tags) {
7
7
  this.store = store;
8
8
  this.tags = tags;
9
+ if (typeof store.getOrSetTagged !== "function") {
10
+ throw new TypeError("Cache tags require atomic tagged loading support.");
11
+ }
12
+ }
13
+ async put(key, value, ttlMs) {
14
+ await this.store.set(key, value, ttlMs, this.tags);
15
+ }
16
+ async forever(key, value) {
17
+ await this.store.set(key, value, null, this.tags);
9
18
  }
10
19
  async remember(key, callback, ttlMs) {
11
- if (this.store.getOrSetTagged)
12
- return this.store.getOrSetTagged(key, callback, this.tags, ttlMs);
13
- const value = await this.store.getOrSet(key, callback, ttlMs);
14
- await this.store.attachTags(key, this.tags);
15
- return value;
20
+ return this.store.getOrSetTagged(key, callback, this.tags, ttlMs);
16
21
  }
17
22
  async flush() {
18
23
  return this.store.flushTags(this.tags);
@@ -32,6 +37,12 @@ class CacheRepository {
32
37
  async get(key) {
33
38
  return this.store.get(key);
34
39
  }
40
+ async put(key, value, ttlMs) {
41
+ await this.store.set(key, value, ttlMs);
42
+ }
43
+ async forever(key, value) {
44
+ await this.store.set(key, value, null);
45
+ }
35
46
  async remember(key, callback, ttlMs) {
36
47
  return this.store.getOrSet(key, callback, ttlMs);
37
48
  }
@@ -65,6 +76,8 @@ function createGuardedCache(backend, operations) {
65
76
  const forget = (key) => operations.operation(() => backend.forget(key));
66
77
  const flush = () => operations.operation(() => backend.flush());
67
78
  return Object.freeze({
79
+ put: (key, value, ttlMs) => operations.operation(() => backend.put(key, value, ttlMs)),
80
+ forever: (key, value) => operations.operation(() => backend.forever(key, value)),
68
81
  get: (key) => operations.operation(() => backend.get(key)),
69
82
  remember,
70
83
  forget,
@@ -73,6 +86,8 @@ function createGuardedCache(backend, operations) {
73
86
  operations.assertActive();
74
87
  const tags = [...names];
75
88
  return Object.freeze({
89
+ put: (key, value, ttlMs) => operations.operation(() => backend.tags(...tags).put(key, value, ttlMs)),
90
+ forever: (key, value) => operations.operation(() => backend.tags(...tags).forever(key, value)),
76
91
  remember: (key, loader, ttlMs) => operations.operation(() => backend.tags(...tags).remember(key, (signal) => operations.operation(() => loader(signal)), ttlMs)),
77
92
  flush: () => operations.operation(() => backend.tags(...tags).flush())
78
93
  });
@@ -1,9 +1,16 @@
1
1
  // @bun
2
+ // ../../src/core/cache/expiration.ts
3
+ function resolveCacheTtl(ttlMs, defaultTtlMs) {
4
+ const resolved = ttlMs === undefined ? defaultTtlMs : ttlMs;
5
+ if (resolved !== null && (typeof resolved !== "number" || !Number.isSafeInteger(resolved))) {
6
+ throw new RangeError("ttlMs must be a safe integer number of milliseconds, or null for forever.");
7
+ }
8
+ return resolved;
9
+ }
10
+
2
11
  // ../../src/core/cache/simpleCache.ts
3
12
  function validateSimpleCacheOptions(ttlMs, maxEntries) {
4
- if (!Number.isFinite(ttlMs) || ttlMs < 0) {
5
- throw new RangeError("ttlMs must be a non-negative number.");
6
- }
13
+ resolveCacheTtl(ttlMs, ttlMs);
7
14
  if (!Number.isInteger(maxEntries) || maxEntries < 1) {
8
15
  throw new RangeError("maxEntries must be a positive integer.");
9
16
  }
@@ -24,20 +31,27 @@ class SimpleCache {
24
31
  get(key) {
25
32
  return this.getFreshEntry(key)?.value;
26
33
  }
27
- set(key, value, ttlMs) {
34
+ set(key, value, ttlMs, tags = []) {
28
35
  const now = Date.now();
29
- const resolvedTtlMs = ttlMs ?? this.ttlMs;
36
+ const resolvedTtlMs = resolveCacheTtl(ttlMs, this.ttlMs);
37
+ if (resolvedTtlMs !== null && resolvedTtlMs <= 0) {
38
+ this.invalidate(key);
39
+ return;
40
+ }
41
+ this.inflight.delete(key);
30
42
  this.cache.set(key, {
31
43
  value,
32
- expiresAt: now + resolvedTtlMs,
44
+ expiresAt: resolvedTtlMs === null ? Infinity : now + resolvedTtlMs,
33
45
  lastAccessedAt: now
34
46
  });
47
+ this.attachTags(key, tags);
35
48
  this.evictOverflow();
36
49
  }
37
50
  async getOrSet(key, loader, ttlMs) {
38
51
  return this.getOrSetTagged(key, loader, [], ttlMs);
39
52
  }
40
53
  async getOrSetTagged(key, loader, tags, ttlMs) {
54
+ resolveCacheTtl(ttlMs, this.ttlMs);
41
55
  this.pruneExpired();
42
56
  const cachedEntry = this.getFreshEntry(key);
43
57
  if (cachedEntry) {
@@ -179,7 +193,7 @@ class SimpleCache {
179
193
  oldestKey = key;
180
194
  }
181
195
  }
182
- if (!oldestKey) {
196
+ if (oldestKey === undefined) {
183
197
  return;
184
198
  }
185
199
  this.invalidate(oldestKey);
@@ -8,9 +8,8 @@ class SimpleCacheStore {
8
8
  get(key) {
9
9
  return Promise.resolve(this.cache.get(key));
10
10
  }
11
- set(key, value, ttlMs) {
12
- this.cache.set(key, value, ttlMs);
13
- return Promise.resolve();
11
+ async set(key, value, ttlMs, tags = []) {
12
+ this.cache.set(key, value, ttlMs, tags);
14
13
  }
15
14
  getOrSet(key, loader, ttlMs) {
16
15
  return this.cache.getOrSet(key, loader, ttlMs);
@@ -47,7 +47,20 @@ function validateEnv(schema, env = process.env) {
47
47
  }
48
48
  return resolved;
49
49
  }
50
+ function resolveEnv(schema, env = process.env) {
51
+ const validated = validateEnv(schema, env);
52
+ const resolved = {};
53
+ for (const [name, rule] of Object.entries(schema)) {
54
+ const value = validated[name];
55
+ Object.defineProperty(resolved, name, {
56
+ value: value === undefined ? undefined : rule.integer ? Number(value) : value,
57
+ enumerable: true
58
+ });
59
+ }
60
+ return Object.freeze(resolved);
61
+ }
50
62
  export {
51
63
  defineEnvSchema,
64
+ resolveEnv,
52
65
  validateEnv
53
66
  };
@@ -15,6 +15,7 @@ var FAILED_JOB_SERVICE_TOKEN = createServiceToken("core.failedJobs");
15
15
  var CORE_QUEUE_TOKEN = createServiceToken("core.queue");
16
16
  var CORE_EVENT_BUS_TOKEN = createServiceToken("core.eventBus");
17
17
  var CORE_POLICY_GATE_TOKEN = createServiceToken("core.policyGate");
18
+ var CORE_POLICY_REGISTRY_TOKEN = createServiceToken("core.policyRegistry");
18
19
  var CORE_AUTH_TOKEN = createServiceToken("core.auth");
19
20
  var CORE_ABILITY_CHECKER_TOKEN = createServiceToken("core.abilityChecker");
20
21
  var CORE_AUTH_USER_DIRECTORY_TOKEN = createServiceToken("core.authUserDirectory");
@@ -53,6 +54,7 @@ export {
53
54
  CORE_JSON_RESPONSE_POLICY_TOKEN,
54
55
  CORE_MAIL_TOKEN,
55
56
  CORE_POLICY_GATE_TOKEN,
57
+ CORE_POLICY_REGISTRY_TOKEN,
56
58
  CORE_PUBLIC_TENANCY_TOKEN,
57
59
  CORE_QUEUE_TOKEN,
58
60
  CORE_STORAGE_TOKEN,
@@ -121,7 +121,6 @@ import {
121
121
 
122
122
  // ../../src/core/events/eventBusClass.ts
123
123
  class EventBus {
124
- constructor() {}
125
124
  transactionalListeners = new Map;
126
125
  listenTransactional(event, listener) {
127
126
  const handlers = this.transactionalListeners.get(event) ?? new Set;
@@ -515,89 +514,8 @@ function createBoundRegistryFactory(prepared, context, ownership, configuredPool
515
514
  };
516
515
  }
517
516
 
518
- // ../../src/core/runtime/optionalPeer.ts
519
- function missingOptionalPeer(packageName, reason, error) {
520
- return new Error(`Install ${packageName} ${reason} (\`bun add ${packageName}\`).`, {
521
- cause: error
522
- });
523
- }
524
-
525
- // ../../src/core/database/mysqlNative.ts
526
- var MYSQL_SESSION_UTC = "SET time_zone = '+00:00'";
527
- var mysqlModule;
528
- var mysqlPending;
529
- var importMysql = defaultImportMysql;
530
- async function defaultImportMysql() {
531
- return import("mysql2/promise");
532
- }
533
- function mysqlApi(mod) {
534
- if (typeof mod.createPool === "function") {
535
- return mod;
536
- }
537
- const withDefault = mod;
538
- if (typeof withDefault.default?.createPool === "function") {
539
- return withDefault.default;
540
- }
541
- throw new Error("mysql2/promise did not export createPool.");
542
- }
543
- async function loadMysql() {
544
- if (mysqlModule) {
545
- return mysqlModule;
546
- }
547
- if (!mysqlPending) {
548
- mysqlPending = (async () => {
549
- let mod;
550
- try {
551
- mod = await importMysql();
552
- } catch (error) {
553
- mysqlPending = undefined;
554
- throw missingOptionalPeer("mysql2", "to open a MySQL connection", error);
555
- }
556
- try {
557
- mysqlModule = mysqlApi(mod);
558
- return mysqlModule;
559
- } catch (error) {
560
- mysqlPending = undefined;
561
- throw error;
562
- }
563
- })();
564
- }
565
- return mysqlPending;
566
- }
567
- function rowsFromResult(result) {
568
- if (Array.isArray(result)) {
569
- return result;
570
- }
571
- if (result && typeof result === "object") {
572
- return [result];
573
- }
574
- return [];
575
- }
576
- async function executeMysql(target, query, params) {
577
- const [result] = params.length === 0 && target.query ? await target.query(query) : await target.execute(query, [...params]);
578
- return rowsFromResult(result);
579
- }
580
- function pinSessionToUtc(connection) {
581
- connection.query(MYSQL_SESSION_UTC, (error) => {
582
- if (error) {
583
- console.warn("[mysql] Could not set the session time zone to UTC; DATETIME comparisons may drift.");
584
- }
585
- });
586
- }
587
- function createPoolFromModule(mysql, url, max) {
588
- const pool = mysql.createPool({
589
- uri: url,
590
- timezone: "Z",
591
- ...max === undefined ? {} : { connectionLimit: max }
592
- });
593
- pool.on("connection", (connection) => {
594
- pinSessionToUtc(connection);
595
- });
596
- return pool;
597
- }
598
- async function createMysqlPool(url, max) {
599
- return createPoolFromModule(await loadMysql(), url, max);
600
- }
517
+ // ../../src/core/database/rawMysql.ts
518
+ import { createMysqlPool, executeMysql } from "#strata-mysql-native";
601
519
 
602
520
  // ../../src/core/database/nativeTransactions.ts
603
521
  var failure = (error) => Object.freeze({ error });
@@ -990,6 +908,7 @@ function createMysqlPin(connection) {
990
908
  async function createRawMysqlPool(options) {
991
909
  const pool = await createMysqlPool(options.url, options.max);
992
910
  let closed = false;
911
+ let closing;
993
912
  let retainedCleanup;
994
913
  const acquire = async () => {
995
914
  if (closed)
@@ -1073,11 +992,16 @@ async function createRawMysqlPool(options) {
1073
992
  return { ok: false, error, disposition: "unknown", cleanupErrors: [acknowledgement.error] };
1074
993
  }
1075
994
  },
1076
- async close() {
1077
- if (retainedCleanup)
1078
- throw retainedCleanup.error;
1079
- await pool.end();
995
+ close() {
996
+ if (closing)
997
+ return closing;
1080
998
  closed = true;
999
+ closing = Promise.resolve().then(async () => {
1000
+ if (retainedCleanup)
1001
+ throw retainedCleanup.error;
1002
+ await pool.end();
1003
+ });
1004
+ return closing;
1081
1005
  }
1082
1006
  });
1083
1007
  }
@@ -1151,6 +1075,7 @@ function postgresPin(connection) {
1151
1075
  }
1152
1076
  function createRawPostgresPoolFromNative(pool) {
1153
1077
  let closed = false;
1078
+ let closing;
1154
1079
  return Object.freeze({
1155
1080
  dialect: dialectFor("pgsql"),
1156
1081
  capabilities: Object.freeze({
@@ -1183,9 +1108,12 @@ function createRawPostgresPoolFromNative(pool) {
1183
1108
  return { ok: false, error, disposition: "not-acquired" };
1184
1109
  }
1185
1110
  },
1186
- async close() {
1187
- await pool.close();
1111
+ close() {
1112
+ if (closing)
1113
+ return closing;
1188
1114
  closed = true;
1115
+ closing = Promise.resolve().then(() => pool.close());
1116
+ return closing;
1189
1117
  }
1190
1118
  });
1191
1119
  }
@@ -1201,10 +1129,6 @@ function sqliteFilename(filename) {
1201
1129
  throw new Error("SQLite path is not configured. Pass a filename or :memory:.");
1202
1130
  return filename === ":memory:" ? filename : resolve(filename);
1203
1131
  }
1204
- function isRowReturning(sql) {
1205
- const upper = sql.replace(/\s+/g, " ").trim().toUpperCase();
1206
- return upper.includes(" RETURNING ") || upper.startsWith("SELECT") || upper.startsWith("WITH") || upper.startsWith("PRAGMA") || upper.startsWith("EXPLAIN");
1207
- }
1208
1132
  function openRawSqlitePool(options) {
1209
1133
  let filename;
1210
1134
  try {
@@ -1248,7 +1172,7 @@ function openRawSqlitePool(options) {
1248
1172
  check();
1249
1173
  const query = db.query(statement.text);
1250
1174
  const params = statement.params.map((value) => value instanceof Date ? value.toISOString() : value);
1251
- if (isRowReturning(statement.text))
1175
+ if (query.columnNames.length > 0)
1252
1176
  return Promise.resolve(query.all(...params));
1253
1177
  query.run(...params);
1254
1178
  return Promise.resolve([]);
@@ -214,7 +214,11 @@ class Factory {
214
214
  return this.instance.behavior.persist(values);
215
215
  }
216
216
  }
217
+ function createFactory(behavior) {
218
+ return Factory.from(defineFactory({ construct: () => behavior }));
219
+ }
217
220
  export {
218
221
  Factory,
222
+ createFactory,
219
223
  defineFactory
220
224
  };