@jskit-ai/kernel 0.1.32 → 0.1.33

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jskit-ai/kernel",
3
- "version": "0.1.32",
3
+ "version": "0.1.33",
4
4
  "type": "module",
5
5
  "dependencies": {
6
6
  "typebox": "^1.0.81"
@@ -236,7 +236,7 @@ test("registerRoutes attaches visibilityContext from route visibility resolvers"
236
236
  }
237
237
 
238
238
  return {
239
- userOwnerId: context?.actor?.id,
239
+ userId: context?.actor?.id,
240
240
  requiresActorScope: true
241
241
  };
242
242
  }
@@ -281,7 +281,7 @@ test("registerRoutes attaches visibilityContext from route visibility resolvers"
281
281
  scopeKind: null,
282
282
  requiresActorScope: true,
283
283
  scopeOwnerId: null,
284
- userOwnerId: 23
284
+ userId: "23"
285
285
  });
286
286
  assert.deepEqual(observed[0].context.requestMeta.visibilityContext, observed[0].context.visibilityContext);
287
287
  assert.equal(observed[0].context.requestMeta.routeVisibility, "user");
@@ -309,7 +309,7 @@ test("registerRoutes keeps actor scope requirement for core user visibility with
309
309
  }
310
310
 
311
311
  return {
312
- userOwnerId: context?.actor?.id
312
+ userId: context?.actor?.id
313
313
  };
314
314
  }
315
315
  }));
@@ -353,7 +353,7 @@ test("registerRoutes keeps actor scope requirement for core user visibility with
353
353
  scopeKind: null,
354
354
  requiresActorScope: true,
355
355
  scopeOwnerId: null,
356
- userOwnerId: 23
356
+ userId: "23"
357
357
  });
358
358
  assert.equal(observed[0].context.requestMeta.routeVisibility, "user");
359
359
  });
@@ -399,7 +399,7 @@ test("registerRoutes does not infer actor scope from non-core route visibility t
399
399
  scopeKind: null,
400
400
  requiresActorScope: false,
401
401
  scopeOwnerId: null,
402
- userOwnerId: null
402
+ userId: null
403
403
  });
404
404
  assert.equal(observed[0].context.requestMeta.routeVisibility, "workspace_user");
405
405
  });
@@ -43,10 +43,10 @@ function resolveVisibilityScope(visibilityContext = {}, runtimeContext = {}) {
43
43
  const visibility = normalizeText(visibilityContext.visibility).toLowerCase();
44
44
  const scopeKind = normalizeText(visibilityContext.scopeKind || visibility).toLowerCase();
45
45
  const scopeOwnerId = normalizeOpaqueId(visibilityContext.scopeOwnerId);
46
- const userOwnerId = normalizeOpaqueId(visibilityContext.userOwnerId);
46
+ const userId = normalizeOpaqueId(visibilityContext.userId);
47
47
  const requiresActorScope = visibilityContext.requiresActorScope === true;
48
48
 
49
- if (requiresActorScope && userOwnerId == null) {
49
+ if (requiresActorScope && userId == null) {
50
50
  return null;
51
51
  }
52
52
 
@@ -57,7 +57,7 @@ function resolveVisibilityScope(visibilityContext = {}, runtimeContext = {}) {
57
57
  };
58
58
  if (requiresActorScope) {
59
59
  scope.scopeId = scopeOwnerId;
60
- scope.userId = userOwnerId;
60
+ scope.userId = userId;
61
61
  }
62
62
  return scope;
63
63
  }
@@ -72,10 +72,10 @@ function resolveVisibilityScope(visibilityContext = {}, runtimeContext = {}) {
72
72
  };
73
73
  }
74
74
 
75
- if (scopeKind === "user" && userOwnerId != null) {
75
+ if (scopeKind === "user" && userId != null) {
76
76
  return {
77
77
  kind: "user",
78
- id: userOwnerId
78
+ id: userId
79
79
  };
80
80
  }
81
81
 
@@ -33,9 +33,9 @@ test("entity change publisher emits normalized event payload", async () => {
33
33
  });
34
34
 
35
35
  assert.equal(payload?.operation, "created");
36
- assert.equal(payload?.entityId, 5);
37
- assert.deepEqual(payload?.scope, { kind: "scope", id: 23 });
38
- assert.equal(payload?.actorId, 17);
36
+ assert.equal(payload?.entityId, "5");
37
+ assert.deepEqual(payload?.scope, { kind: "scope", id: "23" });
38
+ assert.equal(payload?.actorId, "17");
39
39
  assert.equal(payload?.commandId, "cmd-1");
40
40
  assert.equal(payload?.sourceClientId, "client-a");
41
41
  assert.equal(payload?.meta?.service?.token, "crud.customers");
@@ -111,7 +111,7 @@ test("entity change publisher infers scoped owner from service context when visi
111
111
  }
112
112
  );
113
113
 
114
- assert.deepEqual(payload?.scope, { kind: "workspace", id: 23 });
114
+ assert.deepEqual(payload?.scope, { kind: "workspace", id: "23" });
115
115
  assert.equal(published.length, 1);
116
116
  });
117
117
 
@@ -133,7 +133,7 @@ test("entity change publisher supports opaque actor and scope identifiers", asyn
133
133
  visibilityContext: {
134
134
  scopeKind: "workspace_user",
135
135
  scopeOwnerId: "workspace_23",
136
- userOwnerId: "user_17",
136
+ userId: "user_17",
137
137
  requiresActorScope: true
138
138
  }
139
139
  }
@@ -24,7 +24,7 @@ test("requireAuth accepts actor context", () => {
24
24
  }
25
25
  });
26
26
 
27
- assert.equal(actor.id, 7);
27
+ assert.equal(actor.id, "7");
28
28
  });
29
29
 
30
30
  test("requireAuth accepts non-numeric actor ids", () => {
@@ -180,6 +180,34 @@ function normalizePositiveInteger(value, { fallback = 0 } = {}) {
180
180
  return numeric;
181
181
  }
182
182
 
183
+ function normalizeCanonicalRecordIdText(value, { fallback = null } = {}) {
184
+ if (value == null) {
185
+ return fallback;
186
+ }
187
+
188
+ const normalized = String(value).trim();
189
+ return /^[1-9][0-9]*$/.test(normalized) ? normalized : fallback;
190
+ }
191
+
192
+ function normalizeRecordId(value, { fallback = null } = {}) {
193
+ if (value == null) {
194
+ return fallback;
195
+ }
196
+
197
+ if (typeof value === "string") {
198
+ return normalizeCanonicalRecordIdText(value, { fallback });
199
+ }
200
+
201
+ if (typeof value === "bigint") {
202
+ if (value < 1n) {
203
+ return fallback;
204
+ }
205
+ return normalizeCanonicalRecordIdText(value, { fallback });
206
+ }
207
+
208
+ return fallback;
209
+ }
210
+
183
211
  function normalizeOpaqueId(value, { fallback = null } = {}) {
184
212
  if (value == null) {
185
213
  return fallback;
@@ -191,7 +219,7 @@ function normalizeOpaqueId(value, { fallback = null } = {}) {
191
219
  }
192
220
 
193
221
  if (typeof value === "number") {
194
- return Number.isFinite(value) ? value : fallback;
222
+ return Number.isFinite(value) ? String(value) : fallback;
195
223
  }
196
224
 
197
225
  if (typeof value === "bigint") {
@@ -244,6 +272,8 @@ export {
244
272
  normalizeUniqueTextList,
245
273
  normalizeInteger,
246
274
  normalizePositiveInteger,
275
+ normalizeCanonicalRecordIdText,
276
+ normalizeRecordId,
247
277
  normalizeOpaqueId,
248
278
  normalizeOneOf,
249
279
  ensureNonEmptyText
@@ -3,11 +3,13 @@ import assert from "node:assert/strict";
3
3
  import {
4
4
  hasValue,
5
5
  normalizeBoolean,
6
+ normalizeCanonicalRecordIdText,
6
7
  normalizeFiniteInteger,
7
8
  normalizeFiniteNumber,
8
9
  normalizeIfInSource,
9
10
  normalizeIfPresent,
10
11
  normalizeOrNull,
12
+ normalizeRecordId,
11
13
  normalizeOpaqueId,
12
14
  normalizePositiveInteger,
13
15
  normalizeOneOf,
@@ -172,10 +174,27 @@ test("normalizeOrNull normalizes non-nullish values and coerces nullish to null"
172
174
  );
173
175
  });
174
176
 
177
+ test("normalizeRecordId accepts canonical string and bigint identifiers only", () => {
178
+ const unsafeNumericId = Number(9007199254740993n);
179
+ assert.equal(normalizeRecordId(" 7 "), "7");
180
+ assert.equal(normalizeRecordId(10n), "10");
181
+ assert.equal(normalizeRecordId(7), null);
182
+ assert.equal(normalizeRecordId(unsafeNumericId), null);
183
+ assert.equal(normalizeRecordId(""), null);
184
+ assert.equal(normalizeRecordId(null), null);
185
+ });
186
+
187
+ test("normalizeCanonicalRecordIdText validates canonical positive decimal identifiers", () => {
188
+ assert.equal(normalizeCanonicalRecordIdText(" 7 "), "7");
189
+ assert.equal(normalizeCanonicalRecordIdText("007"), null);
190
+ assert.equal(normalizeCanonicalRecordIdText("0"), null);
191
+ assert.equal(normalizeCanonicalRecordIdText("abc"), null);
192
+ });
193
+
175
194
  test("normalizeOpaqueId preserves opaque identifiers", () => {
176
195
  assert.equal(normalizeOpaqueId(" user-123 "), "user-123");
177
- assert.equal(normalizeOpaqueId(7), 7);
178
- assert.equal(normalizeOpaqueId(0), 0);
196
+ assert.equal(normalizeOpaqueId(7), "7");
197
+ assert.equal(normalizeOpaqueId(0), "0");
179
198
  assert.equal(normalizeOpaqueId(10n), "10");
180
199
  assert.equal(normalizeOpaqueId(""), null);
181
200
  assert.equal(normalizeOpaqueId(null), null);
@@ -49,7 +49,7 @@ function normalizeVisibilityContext(value = {}) {
49
49
  scopeKind: normalizedScopeKind,
50
50
  requiresActorScope: source.requiresActorScope === true,
51
51
  scopeOwnerId: normalizeOpaqueId(source.scopeOwnerId),
52
- userOwnerId: normalizeOpaqueId(source.userOwnerId)
52
+ userId: normalizeOpaqueId(source.userId)
53
53
  });
54
54
  }
55
55
 
@@ -19,20 +19,20 @@ test("normalizeRouteVisibilityToken normalizes visibility tokens for module-leve
19
19
  });
20
20
 
21
21
  test("normalizeVisibilityContext normalizes mode and owner identifiers", () => {
22
- assert.deepEqual(normalizeVisibilityContext({ visibility: "user", userOwnerId: "7" }), {
22
+ assert.deepEqual(normalizeVisibilityContext({ visibility: "user", userId: "7" }), {
23
23
  visibility: "user",
24
24
  scopeKind: null,
25
25
  requiresActorScope: false,
26
26
  scopeOwnerId: null,
27
- userOwnerId: "7"
27
+ userId: "7"
28
28
  });
29
29
 
30
- assert.deepEqual(normalizeVisibilityContext({ visibility: "workspace_user", scopeOwnerId: "4", userOwnerId: 9 }), {
30
+ assert.deepEqual(normalizeVisibilityContext({ visibility: "workspace_user", scopeOwnerId: "4", userId: 9 }), {
31
31
  visibility: "workspace_user",
32
32
  scopeKind: null,
33
33
  requiresActorScope: false,
34
34
  scopeOwnerId: "4",
35
- userOwnerId: 9
35
+ userId: "9"
36
36
  });
37
37
 
38
38
  assert.deepEqual(normalizeVisibilityContext({ visibility: "workspace", scopeOwnerId: "0" }), {
@@ -40,6 +40,6 @@ test("normalizeVisibilityContext normalizes mode and owner identifiers", () => {
40
40
  scopeKind: null,
41
41
  requiresActorScope: false,
42
42
  scopeOwnerId: "0",
43
- userOwnerId: null
43
+ userId: null
44
44
  });
45
45
  });
@@ -1,14 +1,13 @@
1
1
  import { Type } from "typebox";
2
- import { normalizeText } from "../support/normalize.js";
3
2
  import { normalizeObjectInput } from "./inputNormalization.js";
4
- import { positiveIntegerValidator } from "./recordIdParamsValidator.js";
3
+ import { positiveIntegerValidator, recordIdInputSchema, recordIdValidator } from "./recordIdParamsValidator.js";
5
4
 
6
5
  function normalizeCursorPaginationQuery(input = {}) {
7
6
  const source = normalizeObjectInput(input);
8
7
  const normalized = {};
9
8
 
10
9
  if (Object.hasOwn(source, "cursor")) {
11
- normalized.cursor = normalizeText(source.cursor);
10
+ normalized.cursor = recordIdValidator.normalize(source.cursor);
12
11
  }
13
12
 
14
13
  if (Object.hasOwn(source, "limit")) {
@@ -21,7 +20,7 @@ function normalizeCursorPaginationQuery(input = {}) {
21
20
  const cursorPaginationQueryValidator = Object.freeze({
22
21
  schema: Type.Object(
23
22
  {
24
- cursor: Type.Optional(positiveIntegerValidator.schema),
23
+ cursor: Type.Optional(recordIdInputSchema),
25
24
  limit: Type.Optional(positiveIntegerValidator.schema)
26
25
  },
27
26
  { additionalProperties: false }
@@ -11,9 +11,8 @@ test("cursorPaginationQueryValidator normalizes numeric strings as cursor text",
11
11
 
12
12
  test("cursorPaginationQueryValidator schema rejects opaque cursor strings", () => {
13
13
  assert.equal(
14
- cursorPaginationQueryValidator.schema.properties.cursor.anyOf.some(
15
- (entry) => entry.type === "string" && entry.pattern === "^[1-9][0-9]*$"
16
- ),
14
+ cursorPaginationQueryValidator.schema.properties.cursor.type === "string" &&
15
+ cursorPaginationQueryValidator.schema.properties.cursor.pattern === "^[1-9][0-9]*$",
17
16
  true
18
17
  );
19
18
  });
@@ -8,7 +8,17 @@ export {
8
8
  export { mergeObjectSchemas } from "./mergeObjectSchemas.js";
9
9
  export { mergeValidators } from "./mergeValidators.js";
10
10
  export { nestValidator } from "./nestValidator.js";
11
- export { recordIdParamsValidator, positiveIntegerValidator } from "./recordIdParamsValidator.js";
11
+ export {
12
+ RECORD_ID_PATTERN,
13
+ recordIdSchema,
14
+ recordIdInputSchema,
15
+ nullableRecordIdSchema,
16
+ nullableRecordIdInputSchema,
17
+ recordIdValidator,
18
+ nullableRecordIdValidator,
19
+ recordIdParamsValidator,
20
+ positiveIntegerValidator
21
+ } from "./recordIdParamsValidator.js";
12
22
  export { normalizeSettingsFieldInput, normalizeSettingsFieldOutput } from "./settingsFieldNormalization.js";
13
23
  export {
14
24
  normalizeRequiredFieldList,
@@ -1,23 +1,51 @@
1
1
  import { Type } from "typebox";
2
2
  import { normalizeObjectInput } from "./inputNormalization.js";
3
- import { normalizePositiveInteger, normalizeText } from "../support/normalize.js";
3
+ import { normalizePositiveInteger, normalizeRecordId } from "../support/normalize.js";
4
4
 
5
- function normalizeRecordId(value) {
6
- return normalizePositiveInteger(normalizeText(value));
7
- }
5
+ const RECORD_ID_PATTERN = "^[1-9][0-9]*$";
6
+
7
+ const recordIdSchema = Type.String({
8
+ minLength: 1,
9
+ pattern: RECORD_ID_PATTERN
10
+ });
11
+
12
+ const recordIdInputSchema = recordIdSchema;
13
+
14
+ const nullableRecordIdSchema = Type.Union([recordIdSchema, Type.Null()]);
15
+ const nullableRecordIdInputSchema = Type.Union([recordIdInputSchema, Type.Null()]);
8
16
 
9
17
  const positiveIntegerValidator = Object.freeze({
10
18
  schema: Type.Union([
11
19
  Type.Integer({ minimum: 1 }),
12
- Type.String({ minLength: 1, pattern: "^[1-9][0-9]*$" })
20
+ Type.String({ minLength: 1, pattern: RECORD_ID_PATTERN })
13
21
  ]),
14
- normalize: normalizeRecordId
22
+ normalize(value) {
23
+ return normalizePositiveInteger(value);
24
+ }
25
+ });
26
+
27
+ const recordIdValidator = Object.freeze({
28
+ schema: recordIdInputSchema,
29
+ normalize(value) {
30
+ return normalizeRecordId(value, {
31
+ fallback: ""
32
+ });
33
+ }
34
+ });
35
+
36
+ const nullableRecordIdValidator = Object.freeze({
37
+ schema: nullableRecordIdInputSchema,
38
+ normalize(value) {
39
+ return normalizeRecordId(value, {
40
+ fallback: null
41
+ });
42
+ }
15
43
  });
16
44
 
17
45
  const recordIdParamsValidator = Object.freeze({
18
46
  schema: Type.Object(
19
47
  {
20
- recordId: Type.Optional(positiveIntegerValidator.schema)
48
+ recordId: Type.Optional(recordIdInputSchema)
21
49
  },
22
50
  { additionalProperties: false }
23
51
  ),
@@ -26,11 +54,21 @@ const recordIdParamsValidator = Object.freeze({
26
54
  const normalized = {};
27
55
 
28
56
  if (Object.hasOwn(source, "recordId")) {
29
- normalized.recordId = normalizeRecordId(source.recordId);
57
+ normalized.recordId = recordIdValidator.normalize(source.recordId);
30
58
  }
31
59
 
32
60
  return normalized;
33
61
  }
34
62
  });
35
63
 
36
- export { recordIdParamsValidator, positiveIntegerValidator };
64
+ export {
65
+ RECORD_ID_PATTERN,
66
+ recordIdSchema,
67
+ recordIdInputSchema,
68
+ nullableRecordIdSchema,
69
+ nullableRecordIdInputSchema,
70
+ recordIdValidator,
71
+ nullableRecordIdValidator,
72
+ recordIdParamsValidator,
73
+ positiveIntegerValidator
74
+ };
@@ -3,15 +3,18 @@ import assert from "node:assert/strict";
3
3
 
4
4
  import { recordIdParamsValidator } from "./recordIdParamsValidator.js";
5
5
 
6
- test("recordIdParamsValidator normalizes string id to positive integer", () => {
6
+ test("recordIdParamsValidator normalizes canonical string ids", () => {
7
7
  assert.deepEqual(recordIdParamsValidator.normalize({ recordId: "42" }), {
8
- recordId: 42
8
+ recordId: "42"
9
9
  });
10
10
  });
11
11
 
12
- test("recordIdParamsValidator normalizes invalid id to 0", () => {
12
+ test("recordIdParamsValidator rejects invalid ids", () => {
13
13
  assert.deepEqual(recordIdParamsValidator.normalize({ recordId: "nope" }), {
14
- recordId: 0
14
+ recordId: ""
15
+ });
16
+ assert.deepEqual(recordIdParamsValidator.normalize({ recordId: 42 }), {
17
+ recordId: ""
15
18
  });
16
19
  });
17
20