@getstrata/core 2.2.50 → 2.2.52

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.
@@ -1,6 +1,10 @@
1
1
  // @bun
2
2
  // ../../src/core/logging/requestLoggingMiddleware.ts
3
- import { currentRequestMeta as currentRequestMeta2, runWithRequestMeta } from "@getstrata/core/http/requestMetaContext";
3
+ import {
4
+ currentRequestMeta as currentRequestMeta2,
5
+ resolveRequestId,
6
+ runWithRequestMeta
7
+ } from "@getstrata/core/http/requestMetaContext";
4
8
 
5
9
  // ../../src/core/http/clientIp.ts
6
10
  import { currentRequestMeta } from "@getstrata/core/http/requestMetaContext";
@@ -74,14 +78,15 @@ var appLogger = new Logger("app");
74
78
  function createRequestLoggingMiddleware() {
75
79
  return async (request, next) => {
76
80
  const ipAddress = readClientIp(request) ?? null;
81
+ const requestId = resolveRequestId(request);
77
82
  return await runWithRequestMeta({
78
83
  ...currentRequestMeta2(),
79
84
  ipAddress,
80
85
  userAgent: request.headers.get("user-agent"),
81
- request
86
+ request,
87
+ requestId
82
88
  }, async () => {
83
89
  const startedAt = performance.now();
84
- const requestId = request.headers.get("x-request-id") ?? crypto.randomUUID();
85
90
  const response = await next();
86
91
  const durationMs = Math.round(performance.now() - startedAt);
87
92
  appLogger.info("HTTP request completed", {
@@ -1,6 +1,7 @@
1
1
  // @bun
2
2
  // ../../src/core/queue/failedJobRepository.ts
3
3
  import { BaseRepository } from "@getstrata/core/database/baseRepository";
4
+ import { sqlTimestamp } from "@getstrata/core/database/dialect";
4
5
 
5
6
  // ../../src/core/queue/failedJobTable.ts
6
7
  import { defineTable } from "@getstrata/core/database/table";
@@ -16,6 +17,27 @@ class FailedJobRepository extends BaseRepository {
16
17
  constructor() {
17
18
  super(failedJobTable);
18
19
  }
20
+ async create(values) {
21
+ const stored = {
22
+ ...values,
23
+ ...values.payload === undefined ? {} : { payload: JSON.stringify(values.payload) },
24
+ ...values.failed_at === undefined ? {} : { failed_at: sqlTimestamp(values.failed_at) }
25
+ };
26
+ return hydrateFailure(await super.create(stored));
27
+ }
28
+ async findAll(options = {}) {
29
+ const rows = await super.findAll(options);
30
+ return options.select?.length ? rows : rows.map(hydrateFailure);
31
+ }
32
+ }
33
+ function hydrateFailure(row) {
34
+ const result = { ...row };
35
+ if (typeof row.payload === "string")
36
+ result.payload = JSON.parse(row.payload);
37
+ if ("failed_at" in row && !(row.failed_at instanceof Date)) {
38
+ result.failed_at = new Date(row.failed_at);
39
+ }
40
+ return result;
19
41
  }
20
42
  var failedJobRepository_default = FailedJobRepository;
21
43
 
@@ -192,8 +214,12 @@ async function runQueueJob(envelope, failedJobs, options = {}) {
192
214
  throw new Error(`Unknown job "${envelope.name}".`);
193
215
  }
194
216
  const attempts = envelope.attempts ?? 0;
217
+ const context = options.context ?? {
218
+ jobId: envelope.jobId ?? crypto.randomUUID(),
219
+ signal: new AbortController().signal
220
+ };
195
221
  try {
196
- await job.handle(envelope.payload, options.context);
222
+ await job.handle(envelope.payload, { ...context, attempt: attempts + 1 });
197
223
  } catch (error) {
198
224
  options.context?.signal.throwIfAborted();
199
225
  const nextAttempt = attempts + 1;
@@ -201,19 +227,19 @@ async function runQueueJob(envelope, failedJobs, options = {}) {
201
227
  if (nextAttempt < maxAttempts) {
202
228
  const backoffMs = job.backoffMs ?? queueConfig.backoffMs;
203
229
  if (options.deferRetry) {
204
- await options.deferRetry({ ...envelope, attempts: nextAttempt }, backoffMs * nextAttempt);
230
+ await options.deferRetry({ ...envelope, jobId: context.jobId, attempts: nextAttempt }, backoffMs * nextAttempt);
205
231
  return;
206
232
  }
207
233
  await delay(backoffMs * nextAttempt, undefined, { signal: options.context?.signal });
208
234
  await runQueueJob({
209
235
  ...envelope,
210
236
  attempts: nextAttempt
211
- }, failedJobs, options);
237
+ }, failedJobs, { ...options, context });
212
238
  return;
213
239
  }
214
240
  await failedJobs.recordFailure({
215
241
  jobName: envelope.name,
216
- jobId: options.context?.jobId ?? envelope.jobId,
242
+ jobId: context.jobId,
217
243
  payload: envelope.payload,
218
244
  exception: error instanceof Error ? error.stack ?? error.message : String(error)
219
245
  });
@@ -1,6 +1,7 @@
1
1
  // @bun
2
2
  // ../../src/core/queue/failedJobRepository.ts
3
3
  import { BaseRepository } from "@getstrata/core/database/baseRepository";
4
+ import { sqlTimestamp } from "@getstrata/core/database/dialect";
4
5
 
5
6
  // ../../src/core/queue/failedJobTable.ts
6
7
  import { defineTable } from "@getstrata/core/database/table";
@@ -16,6 +17,27 @@ class FailedJobRepository extends BaseRepository {
16
17
  constructor() {
17
18
  super(failedJobTable);
18
19
  }
20
+ async create(values) {
21
+ const stored = {
22
+ ...values,
23
+ ...values.payload === undefined ? {} : { payload: JSON.stringify(values.payload) },
24
+ ...values.failed_at === undefined ? {} : { failed_at: sqlTimestamp(values.failed_at) }
25
+ };
26
+ return hydrateFailure(await super.create(stored));
27
+ }
28
+ async findAll(options = {}) {
29
+ const rows = await super.findAll(options);
30
+ return options.select?.length ? rows : rows.map(hydrateFailure);
31
+ }
32
+ }
33
+ function hydrateFailure(row) {
34
+ const result = { ...row };
35
+ if (typeof row.payload === "string")
36
+ result.payload = JSON.parse(row.payload);
37
+ if ("failed_at" in row && !(row.failed_at instanceof Date)) {
38
+ result.failed_at = new Date(row.failed_at);
39
+ }
40
+ return result;
19
41
  }
20
42
  var failedJobRepository_default = FailedJobRepository;
21
43
  export {
@@ -124,8 +124,12 @@ async function runQueueJob(envelope, failedJobs, options = {}) {
124
124
  throw new Error(`Unknown job "${envelope.name}".`);
125
125
  }
126
126
  const attempts = envelope.attempts ?? 0;
127
+ const context = options.context ?? {
128
+ jobId: envelope.jobId ?? crypto.randomUUID(),
129
+ signal: new AbortController().signal
130
+ };
127
131
  try {
128
- await job.handle(envelope.payload, options.context);
132
+ await job.handle(envelope.payload, { ...context, attempt: attempts + 1 });
129
133
  } catch (error) {
130
134
  options.context?.signal.throwIfAborted();
131
135
  const nextAttempt = attempts + 1;
@@ -133,19 +137,19 @@ async function runQueueJob(envelope, failedJobs, options = {}) {
133
137
  if (nextAttempt < maxAttempts) {
134
138
  const backoffMs = job.backoffMs ?? queueConfig.backoffMs;
135
139
  if (options.deferRetry) {
136
- await options.deferRetry({ ...envelope, attempts: nextAttempt }, backoffMs * nextAttempt);
140
+ await options.deferRetry({ ...envelope, jobId: context.jobId, attempts: nextAttempt }, backoffMs * nextAttempt);
137
141
  return;
138
142
  }
139
143
  await delay(backoffMs * nextAttempt, undefined, { signal: options.context?.signal });
140
144
  await runQueueJob({
141
145
  ...envelope,
142
146
  attempts: nextAttempt
143
- }, failedJobs, options);
147
+ }, failedJobs, { ...options, context });
144
148
  return;
145
149
  }
146
150
  await failedJobs.recordFailure({
147
151
  jobName: envelope.name,
148
- jobId: options.context?.jobId ?? envelope.jobId,
152
+ jobId: context.jobId,
149
153
  payload: envelope.payload,
150
154
  exception: error instanceof Error ? error.stack ?? error.message : String(error)
151
155
  });
@@ -1,6 +1,7 @@
1
1
  // @bun
2
2
  // ../../src/core/queue/failedJobRepository.ts
3
3
  import { BaseRepository } from "@getstrata/core/database/baseRepository";
4
+ import { sqlTimestamp } from "@getstrata/core/database/dialect";
4
5
 
5
6
  // ../../src/core/queue/failedJobTable.ts
6
7
  import { defineTable } from "@getstrata/core/database/table";
@@ -16,6 +17,27 @@ class FailedJobRepository extends BaseRepository {
16
17
  constructor() {
17
18
  super(failedJobTable);
18
19
  }
20
+ async create(values) {
21
+ const stored = {
22
+ ...values,
23
+ ...values.payload === undefined ? {} : { payload: JSON.stringify(values.payload) },
24
+ ...values.failed_at === undefined ? {} : { failed_at: sqlTimestamp(values.failed_at) }
25
+ };
26
+ return hydrateFailure(await super.create(stored));
27
+ }
28
+ async findAll(options = {}) {
29
+ const rows = await super.findAll(options);
30
+ return options.select?.length ? rows : rows.map(hydrateFailure);
31
+ }
32
+ }
33
+ function hydrateFailure(row) {
34
+ const result = { ...row };
35
+ if (typeof row.payload === "string")
36
+ result.payload = JSON.parse(row.payload);
37
+ if ("failed_at" in row && !(row.failed_at instanceof Date)) {
38
+ result.failed_at = new Date(row.failed_at);
39
+ }
40
+ return result;
19
41
  }
20
42
  var failedJobRepository_default = FailedJobRepository;
21
43
 
@@ -192,8 +214,12 @@ async function runQueueJob(envelope, failedJobs, options = {}) {
192
214
  throw new Error(`Unknown job "${envelope.name}".`);
193
215
  }
194
216
  const attempts = envelope.attempts ?? 0;
217
+ const context = options.context ?? {
218
+ jobId: envelope.jobId ?? crypto.randomUUID(),
219
+ signal: new AbortController().signal
220
+ };
195
221
  try {
196
- await job.handle(envelope.payload, options.context);
222
+ await job.handle(envelope.payload, { ...context, attempt: attempts + 1 });
197
223
  } catch (error) {
198
224
  options.context?.signal.throwIfAborted();
199
225
  const nextAttempt = attempts + 1;
@@ -201,19 +227,19 @@ async function runQueueJob(envelope, failedJobs, options = {}) {
201
227
  if (nextAttempt < maxAttempts) {
202
228
  const backoffMs = job.backoffMs ?? queueConfig.backoffMs;
203
229
  if (options.deferRetry) {
204
- await options.deferRetry({ ...envelope, attempts: nextAttempt }, backoffMs * nextAttempt);
230
+ await options.deferRetry({ ...envelope, jobId: context.jobId, attempts: nextAttempt }, backoffMs * nextAttempt);
205
231
  return;
206
232
  }
207
233
  await delay(backoffMs * nextAttempt, undefined, { signal: options.context?.signal });
208
234
  await runQueueJob({
209
235
  ...envelope,
210
236
  attempts: nextAttempt
211
- }, failedJobs, options);
237
+ }, failedJobs, { ...options, context });
212
238
  return;
213
239
  }
214
240
  await failedJobs.recordFailure({
215
241
  jobName: envelope.name,
216
- jobId: options.context?.jobId ?? envelope.jobId,
242
+ jobId: context.jobId,
217
243
  payload: envelope.payload,
218
244
  exception: error instanceof Error ? error.stack ?? error.message : String(error)
219
245
  });
@@ -918,6 +918,7 @@ var {RedisClient: RedisClient3 } = globalThis.Bun;
918
918
 
919
919
  // ../../src/core/queue/failedJobRepository.ts
920
920
  import { BaseRepository } from "@getstrata/core/database/baseRepository";
921
+ import { sqlTimestamp } from "@getstrata/core/database/dialect";
921
922
 
922
923
  // ../../src/core/queue/failedJobTable.ts
923
924
  import { defineTable } from "@getstrata/core/database/table";
@@ -933,6 +934,27 @@ class FailedJobRepository extends BaseRepository {
933
934
  constructor() {
934
935
  super(failedJobTable);
935
936
  }
937
+ async create(values) {
938
+ const stored = {
939
+ ...values,
940
+ ...values.payload === undefined ? {} : { payload: JSON.stringify(values.payload) },
941
+ ...values.failed_at === undefined ? {} : { failed_at: sqlTimestamp(values.failed_at) }
942
+ };
943
+ return hydrateFailure(await super.create(stored));
944
+ }
945
+ async findAll(options = {}) {
946
+ const rows = await super.findAll(options);
947
+ return options.select?.length ? rows : rows.map(hydrateFailure);
948
+ }
949
+ }
950
+ function hydrateFailure(row) {
951
+ const result = { ...row };
952
+ if (typeof row.payload === "string")
953
+ result.payload = JSON.parse(row.payload);
954
+ if ("failed_at" in row && !(row.failed_at instanceof Date)) {
955
+ result.failed_at = new Date(row.failed_at);
956
+ }
957
+ return result;
936
958
  }
937
959
  var failedJobRepository_default = FailedJobRepository;
938
960
 
@@ -1109,8 +1131,12 @@ async function runQueueJob(envelope, failedJobs, options = {}) {
1109
1131
  throw new Error(`Unknown job "${envelope.name}".`);
1110
1132
  }
1111
1133
  const attempts = envelope.attempts ?? 0;
1134
+ const context = options.context ?? {
1135
+ jobId: envelope.jobId ?? crypto.randomUUID(),
1136
+ signal: new AbortController().signal
1137
+ };
1112
1138
  try {
1113
- await job.handle(envelope.payload, options.context);
1139
+ await job.handle(envelope.payload, { ...context, attempt: attempts + 1 });
1114
1140
  } catch (error) {
1115
1141
  options.context?.signal.throwIfAborted();
1116
1142
  const nextAttempt = attempts + 1;
@@ -1118,19 +1144,19 @@ async function runQueueJob(envelope, failedJobs, options = {}) {
1118
1144
  if (nextAttempt < maxAttempts) {
1119
1145
  const backoffMs = job.backoffMs ?? queueConfig.backoffMs;
1120
1146
  if (options.deferRetry) {
1121
- await options.deferRetry({ ...envelope, attempts: nextAttempt }, backoffMs * nextAttempt);
1147
+ await options.deferRetry({ ...envelope, jobId: context.jobId, attempts: nextAttempt }, backoffMs * nextAttempt);
1122
1148
  return;
1123
1149
  }
1124
1150
  await delay(backoffMs * nextAttempt, undefined, { signal: options.context?.signal });
1125
1151
  await runQueueJob({
1126
1152
  ...envelope,
1127
1153
  attempts: nextAttempt
1128
- }, failedJobs, options);
1154
+ }, failedJobs, { ...options, context });
1129
1155
  return;
1130
1156
  }
1131
1157
  await failedJobs.recordFailure({
1132
1158
  jobName: envelope.name,
1133
- jobId: options.context?.jobId ?? envelope.jobId,
1159
+ jobId: context.jobId,
1134
1160
  payload: envelope.payload,
1135
1161
  exception: error instanceof Error ? error.stack ?? error.message : String(error)
1136
1162
  });
@@ -127,8 +127,12 @@ async function runQueueJob(envelope, failedJobs, options = {}) {
127
127
  throw new Error(`Unknown job "${envelope.name}".`);
128
128
  }
129
129
  const attempts = envelope.attempts ?? 0;
130
+ const context = options.context ?? {
131
+ jobId: envelope.jobId ?? crypto.randomUUID(),
132
+ signal: new AbortController().signal
133
+ };
130
134
  try {
131
- await job.handle(envelope.payload, options.context);
135
+ await job.handle(envelope.payload, { ...context, attempt: attempts + 1 });
132
136
  } catch (error) {
133
137
  options.context?.signal.throwIfAborted();
134
138
  const nextAttempt = attempts + 1;
@@ -136,19 +140,19 @@ async function runQueueJob(envelope, failedJobs, options = {}) {
136
140
  if (nextAttempt < maxAttempts) {
137
141
  const backoffMs = job.backoffMs ?? queueConfig.backoffMs;
138
142
  if (options.deferRetry) {
139
- await options.deferRetry({ ...envelope, attempts: nextAttempt }, backoffMs * nextAttempt);
143
+ await options.deferRetry({ ...envelope, jobId: context.jobId, attempts: nextAttempt }, backoffMs * nextAttempt);
140
144
  return;
141
145
  }
142
146
  await delay(backoffMs * nextAttempt, undefined, { signal: options.context?.signal });
143
147
  await runQueueJob({
144
148
  ...envelope,
145
149
  attempts: nextAttempt
146
- }, failedJobs, options);
150
+ }, failedJobs, { ...options, context });
147
151
  return;
148
152
  }
149
153
  await failedJobs.recordFailure({
150
154
  jobName: envelope.name,
151
- jobId: options.context?.jobId ?? envelope.jobId,
155
+ jobId: context.jobId,
152
156
  payload: envelope.payload,
153
157
  exception: error instanceof Error ? error.stack ?? error.message : String(error)
154
158
  });
@@ -1,23 +1,25 @@
1
1
  // @bun
2
2
  // ../../src/core/security/safePath.ts
3
3
  import { isAbsolute, resolve, sep } from "path";
4
- function assertPathUnderRoot(rootDirectory, userPath) {
5
- const trimmed = userPath.trim();
6
- if (trimmed.length === 0) {
4
+ function resolvePathUnderRoot(rootDirectory, path) {
5
+ if (path.length === 0) {
7
6
  throw new Error("Path must not be empty.");
8
7
  }
9
- if (trimmed.includes("\x00")) {
8
+ if (path.includes("\x00")) {
10
9
  throw new Error("Path must not contain a null byte.");
11
10
  }
12
11
  const root = resolve(rootDirectory);
13
- const candidate = isAbsolute(trimmed) ? resolve(trimmed) : resolve(root, trimmed.replace(/^\/+/u, ""));
12
+ const candidate = isAbsolute(path) ? resolve(path) : resolve(root, path.replace(/^\/+/u, ""));
14
13
  if (candidate === root || candidate.startsWith(`${root}${sep}`)) {
15
14
  return candidate;
16
15
  }
17
16
  throw new Error("Path escapes the storage root.");
18
17
  }
18
+ function assertPathUnderRoot(rootDirectory, userPath) {
19
+ return resolvePathUnderRoot(rootDirectory, userPath.trim());
20
+ }
19
21
  function assertUrlPathUnderRoot(rootDirectory, urlPathname) {
20
- return assertPathUnderRoot(rootDirectory, urlPathname.replace(/^\/+/u, ""));
22
+ return resolvePathUnderRoot(rootDirectory, decodeURIComponent(urlPathname).replace(/^\/+/u, ""));
21
23
  }
22
24
  export {
23
25
  assertPathUnderRoot,
@@ -6,23 +6,25 @@ var {S3Client } = globalThis.Bun;
6
6
 
7
7
  // ../../src/core/security/safePath.ts
8
8
  import { isAbsolute, resolve, sep } from "path";
9
- function assertPathUnderRoot(rootDirectory, userPath) {
10
- const trimmed = userPath.trim();
11
- if (trimmed.length === 0) {
9
+ function resolvePathUnderRoot(rootDirectory, path) {
10
+ if (path.length === 0) {
12
11
  throw new Error("Path must not be empty.");
13
12
  }
14
- if (trimmed.includes("\x00")) {
13
+ if (path.includes("\x00")) {
15
14
  throw new Error("Path must not contain a null byte.");
16
15
  }
17
16
  const root = resolve(rootDirectory);
18
- const candidate = isAbsolute(trimmed) ? resolve(trimmed) : resolve(root, trimmed.replace(/^\/+/u, ""));
17
+ const candidate = isAbsolute(path) ? resolve(path) : resolve(root, path.replace(/^\/+/u, ""));
19
18
  if (candidate === root || candidate.startsWith(`${root}${sep}`)) {
20
19
  return candidate;
21
20
  }
22
21
  throw new Error("Path escapes the storage root.");
23
22
  }
23
+ function assertPathUnderRoot(rootDirectory, userPath) {
24
+ return resolvePathUnderRoot(rootDirectory, userPath.trim());
25
+ }
24
26
  function assertUrlPathUnderRoot(rootDirectory, urlPathname) {
25
- return assertPathUnderRoot(rootDirectory, urlPathname.replace(/^\/+/u, ""));
27
+ return resolvePathUnderRoot(rootDirectory, decodeURIComponent(urlPathname).replace(/^\/+/u, ""));
26
28
  }
27
29
 
28
30
  // ../../src/core/storage/storage.ts
@@ -101,7 +101,7 @@ export { createMetricsMiddleware } from "../core/http/metricsMiddleware.ts";
101
101
  export type { Middleware, RouteHandler } from "../core/http/middleware.ts";
102
102
  export { type ParsedUpload, parseMultipartUpload, sanitizeUploadFileName, validateUploadFile, } from "../core/http/parseMultipartUpload.ts";
103
103
  export type { RequestMeta } from "../core/http/requestMetaContext.ts";
104
- export { currentRequestMeta, requestMetaContext, runWithRequestMeta, } from "../core/http/requestMetaContext.ts";
104
+ export { currentRequestMeta, requestMetaContext, resolveRequestId, runWithRequestMeta, } from "../core/http/requestMetaContext.ts";
105
105
  export { createRequireAbilityMiddleware } from "../core/http/requireAbilityMiddleware.ts";
106
106
  export { createRequireGlobalAdminMiddleware } from "../core/http/requireGlobalAdminMiddleware.ts";
107
107
  export { createRequireWebAuthMiddleware } from "../core/http/requireWebAuthMiddleware.ts";