@digitraffic/common 2026.10.30-2 → 2026.11.13-1

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.
@@ -131,11 +131,15 @@ describe("createKyHooks", () => {
131
131
  expect(httpLogger.logResponse).toHaveBeenCalledWith(200, 5);
132
132
  expect(cloneSpy).not.toHaveBeenCalled();
133
133
  });
134
- test("afterResponse measures body length when header is missing and keeps body readable", async () => {
134
+ test("afterResponse leaves length unknown when header is missing and keeps body readable", async () => {
135
135
  const { httpLogger, fn } = hook("afterResponse");
136
136
  const response = new Response("hello", { status: 201 });
137
+ const cloneSpy = vi.spyOn(response, "clone");
137
138
  const result = (await fn({ response }));
138
- expect(httpLogger.logResponse).toHaveBeenCalledWith(201, 5);
139
+ // The body must not be read just for logging. Buffering it would hold
140
+ // the whole payload in memory and block streaming consumers.
141
+ expect(httpLogger.logResponse).toHaveBeenCalledWith(201, undefined);
142
+ expect(cloneSpy).not.toHaveBeenCalled();
139
143
  expect(await result.text()).toBe("hello");
140
144
  });
141
145
  test("beforeError logs and returns non-HTTP errors", () => {
@@ -86,11 +86,24 @@ describe("convertJsonSchemaNullableToOneOfDeep", () => {
86
86
  });
87
87
  test("keeps null allowed when nullable schema is only a reference", () => {
88
88
  const schema = convertJsonSchemaNullableToOneOfDeep({
89
- ref: "#/definitions/Vessel",
90
- nullable: true,
89
+ type: "object",
90
+ definitions: {
91
+ Vessel: { type: "object" },
92
+ },
93
+ properties: {
94
+ vessel: { ref: "#/definitions/Vessel", nullable: true },
95
+ },
91
96
  });
92
97
  expect(schema).toEqual({
93
- anyOf: [{ ref: "#/definitions/Vessel" }, { type: "null" }],
98
+ type: "object",
99
+ definitions: {
100
+ Vessel: { type: "object" },
101
+ },
102
+ properties: {
103
+ vessel: {
104
+ anyOf: [{ ref: "#/definitions/Vessel" }, { type: "null" }],
105
+ },
106
+ },
94
107
  });
95
108
  });
96
109
  test("keeps null allowed when nullable schema is empty", () => {
@@ -280,6 +293,138 @@ describe("convertJsonSchemaNullableToOneOfDeep", () => {
280
293
  ],
281
294
  });
282
295
  });
296
+ test("throws when a sibling reference points inside a wrapped schema", () => {
297
+ // The reference target moves under oneOf[0] when address is wrapped,
298
+ // so the pointer would be left dangling
299
+ expect(() => convertJsonSchemaNullableToOneOfDeep({
300
+ type: "object",
301
+ properties: {
302
+ address: {
303
+ type: "object",
304
+ nullable: true,
305
+ properties: { code: { type: "string" } },
306
+ },
307
+ billingCode: { ref: "#/properties/address/properties/code" },
308
+ },
309
+ })).toThrow(/#\/properties\/address\/properties\/code/);
310
+ });
311
+ test("throws when a definitions reference points inside a wrapped schema", () => {
312
+ expect(() => convertJsonSchemaNullableToOneOfDeep({
313
+ type: "object",
314
+ definitions: {
315
+ Shortcut: { ref: "#/properties/address/properties/code" },
316
+ },
317
+ properties: {
318
+ address: {
319
+ type: "object",
320
+ nullable: true,
321
+ properties: { code: { type: "string" } },
322
+ },
323
+ link: { ref: "#/definitions/Shortcut" },
324
+ },
325
+ })).toThrow(/#\/properties\/address\/properties\/code/);
326
+ });
327
+ test("allows references whose target does not move", () => {
328
+ // address is wrapped, but the reference points to plain, which stays put
329
+ const schema = convertJsonSchemaNullableToOneOfDeep({
330
+ type: "object",
331
+ properties: {
332
+ plain: { type: "string" },
333
+ address: { type: "object", nullable: true },
334
+ copy: { ref: "#/properties/plain" },
335
+ },
336
+ });
337
+ expect(schema).toEqual({
338
+ type: "object",
339
+ properties: {
340
+ plain: { type: "string" },
341
+ address: { oneOf: [{ type: "object" }, { type: "null" }] },
342
+ copy: { ref: "#/properties/plain" },
343
+ },
344
+ });
345
+ });
346
+ test("accepts percent-encoded references", () => {
347
+ // "#/definitions/Postal%20Code" is a valid URI fragment pointer to
348
+ // a definition named "Postal Code"
349
+ const schema = {
350
+ type: "object",
351
+ definitions: {
352
+ "Postal Code": { type: "string" },
353
+ },
354
+ properties: {
355
+ code: { ref: "#/definitions/Postal%20Code" },
356
+ name: { type: "string", nullable: true },
357
+ },
358
+ };
359
+ expect(() => convertJsonSchemaNullableToOneOfDeep(schema)).not.toThrow();
360
+ });
361
+ test("rejects an array index with a leading zero", () => {
362
+ // "01" is not a valid JSON Pointer array index, so the reference
363
+ // cannot be resolved even though Number("01") is 1
364
+ expect(() => convertJsonSchemaNullableToOneOfDeep({
365
+ type: "object",
366
+ properties: {
367
+ choice: { anyOf: [{ type: "string" }, { type: "number" }] },
368
+ copy: { ref: "#/properties/choice/anyOf/01" },
369
+ name: { type: "string", nullable: true },
370
+ },
371
+ })).toThrow(/#\/properties\/choice\/anyOf\/01/);
372
+ });
373
+ test("rejects references to inherited object properties", () => {
374
+ // toString exists on Object.prototype but not in the JSON document
375
+ expect(() => convertJsonSchemaNullableToOneOfDeep({
376
+ type: "object",
377
+ definitions: {},
378
+ properties: {
379
+ bad: { ref: "#/definitions/toString" },
380
+ name: { type: "string", nullable: true },
381
+ },
382
+ })).toThrow(/#\/definitions\/toString/);
383
+ });
384
+ test("treats an encoded slash as a separator per RFC 6901", () => {
385
+ // RFC 6901 section 6: the whole fragment is percent-decoded first and
386
+ // then tokenized, so %2F becomes a separator. A slash inside a key must
387
+ // be escaped as ~1 instead.
388
+ expect(() => convertJsonSchemaNullableToOneOfDeep({
389
+ type: "object",
390
+ definitions: {
391
+ "a/b": { type: "string" },
392
+ },
393
+ properties: {
394
+ bad: { ref: "#/definitions/a%2Fb" },
395
+ name: { type: "string", nullable: true },
396
+ },
397
+ })).toThrow(/#\/definitions\/a%2Fb/);
398
+ });
399
+ test("resolves a slash in a key escaped as ~1", () => {
400
+ expect(() => convertJsonSchemaNullableToOneOfDeep({
401
+ type: "object",
402
+ definitions: {
403
+ "a/b": { type: "string" },
404
+ },
405
+ properties: {
406
+ good: { ref: "#/definitions/a~1b" },
407
+ name: { type: "string", nullable: true },
408
+ },
409
+ })).not.toThrow();
410
+ });
411
+ test("keeps null type working when it is also marked nullable", () => {
412
+ // { type: "null", nullable: true } must not become
413
+ // oneOf: [{ type: "null" }, { type: "null" }], because null would then
414
+ // match both alternatives and oneOf would reject it
415
+ const schema = convertJsonSchemaNullableToOneOfDeep({
416
+ type: "object",
417
+ properties: {
418
+ empty: { type: "null", nullable: true },
419
+ },
420
+ });
421
+ expect(schema).toEqual({
422
+ type: "object",
423
+ properties: {
424
+ empty: { type: "null" },
425
+ },
426
+ });
427
+ });
283
428
  test("creates an API Gateway schema from Zod", () => {
284
429
  const schema = toApiGatewayJsonSchema(z.object({
285
430
  reason: z.string().nullable(),
@@ -2,7 +2,7 @@ import type { IVpc } from "aws-cdk-lib/aws-ec2";
2
2
  import { CfnDBProxyEndpoint, DatabaseProxy } from "aws-cdk-lib/aws-rds";
3
3
  import type { ISecret } from "aws-cdk-lib/aws-secretsmanager";
4
4
  import { Stack } from "aws-cdk-lib/core";
5
- import type { Construct } from "constructs/lib/construct.js";
5
+ import type { Construct } from "constructs";
6
6
  import type { InfraStackConfiguration } from "./intra-stack-configuration.js";
7
7
  export interface ProxyConfiguration {
8
8
  readonly secretArn: string;
@@ -3,7 +3,7 @@ import { Key } from "aws-cdk-lib/aws-kms";
3
3
  import type { AuroraPostgresEngineVersion, DatabaseClusterProps, IParameterGroup } from "aws-cdk-lib/aws-rds";
4
4
  import { DatabaseCluster } from "aws-cdk-lib/aws-rds";
5
5
  import { Stack } from "aws-cdk-lib/core";
6
- import type { Construct } from "constructs/lib/construct.js";
6
+ import type { Construct } from "constructs";
7
7
  import type { InfraStackConfiguration } from "./intra-stack-configuration.js";
8
8
  export interface DbConfiguration {
9
9
  readonly cluster?: ClusterConfiguration;
@@ -1,7 +1,7 @@
1
1
  import type { IVpc } from "aws-cdk-lib/aws-ec2";
2
2
  import { Vpc } from "aws-cdk-lib/aws-ec2";
3
3
  import { Stack } from "aws-cdk-lib/core";
4
- import type { Construct } from "constructs/lib/construct.js";
4
+ import type { Construct } from "constructs";
5
5
  import type { InfraStackConfiguration } from "./intra-stack-configuration.js";
6
6
  export interface NetworkConfiguration {
7
7
  readonly vpcName: string;
@@ -59,17 +59,12 @@ export function createKyHooks(httpLogger) {
59
59
  },
60
60
  ],
61
61
  afterResponse: [
62
- async (state) => {
63
- const contentLengthFromHeader = parseContentLengthFromHeader(state.response.headers.get("content-length"));
64
- // use header when available to determine content length
65
- if (contentLengthFromHeader !== undefined) {
66
- httpLogger.logResponse(state.response.status, contentLengthFromHeader);
67
- return state.response;
68
- }
69
- // create clone from the response, so we can read its body without affecting the original response
70
- const resp = state.response.clone();
71
- const contentLength = (await resp.bytes()).length;
72
- httpLogger.logResponse(resp.status, contentLength);
62
+ (state) => {
63
+ // Content length is taken from the header only. When the header is
64
+ // missing, for example with chunked responses, the length is left
65
+ // unknown: reading the body here would buffer the whole payload in
66
+ // memory and block streaming consumers until EOF.
67
+ httpLogger.logResponse(state.response.status, parseContentLengthFromHeader(state.response.headers.get("content-length")));
73
68
  return state.response;
74
69
  },
75
70
  ],
@@ -30,10 +30,12 @@ type JsonSchemaWithNullable = JsonSchema & {
30
30
  * allowed. The conversion is applied recursively to nested schemas. Field
31
31
  * names and literal values such as defaults are left untouched.
32
32
  *
33
- * Limitation: when a nullable schema is wrapped, only references to
34
- * `#/definitions/...` stay valid. If the schema contains local references to
35
- * other locations, such as `#/properties/...`, an error is thrown instead of
36
- * producing a model with broken references. Zod output never contains such
33
+ * Limitation: wrapping moves schema content under oneOf/anyOf, which can
34
+ * break local references such as `#/properties/...` pointing at the old
35
+ * location. After conversion every local reference is checked against the
36
+ * result, and an error is thrown instead of producing a model with dangling
37
+ * references. References to `#/definitions/...` targets stay valid because
38
+ * definitions are kept at the root. Zod output never contains such
37
39
  * references, so this only matters for hand-written schemas.
38
40
  */
39
41
  export declare function convertJsonSchemaNullableToOneOfDeep(schema: JsonSchemaWithNullable): JsonSchemaWithNullable;
@@ -90,21 +90,77 @@ function findLocalReferences(schema) {
90
90
  }
91
91
  return references;
92
92
  }
93
+ /**
94
+ * Checks whether a local JSON Pointer such as "#/definitions/Code" resolves
95
+ * to a value in the document. The pointer is a URI fragment: per RFC 6901
96
+ * section 6 the whole fragment is percent-decoded first and then tokenized,
97
+ * so an encoded slash (%2F) is a separator. A slash inside a key must be
98
+ * escaped as ~1.
99
+ */
100
+ function resolvesInDocument(root, pointer) {
101
+ let decoded;
102
+ try {
103
+ decoded = decodeURIComponent(pointer.slice(1));
104
+ }
105
+ catch {
106
+ // malformed percent-encoding cannot resolve
107
+ return false;
108
+ }
109
+ const parts = decoded
110
+ .slice(1)
111
+ .split("/")
112
+ .map((part) => part.replace(/~1/g, "/").replace(/~0/g, "~"));
113
+ let current = root;
114
+ for (const part of parts) {
115
+ if (Array.isArray(current)) {
116
+ // RFC 6901: an array index is "0" or digits without a leading zero
117
+ if (!/^(0|[1-9][0-9]*)$/.test(part)) {
118
+ return false;
119
+ }
120
+ const index = Number(part);
121
+ if (index >= current.length) {
122
+ return false;
123
+ }
124
+ current = current[index];
125
+ }
126
+ else if (isObject(current)) {
127
+ // own property only: inherited ones such as toString are not
128
+ // members of the JSON document
129
+ if (!Object.hasOwn(current, part)) {
130
+ return false;
131
+ }
132
+ current = current[part];
133
+ }
134
+ else {
135
+ return false;
136
+ }
137
+ }
138
+ return true;
139
+ }
140
+ /**
141
+ * Throws if the converted schema contains local references that no longer
142
+ * resolve. Wrapping nullable schemas moves their content under oneOf/anyOf,
143
+ * which can leave references pointing at the old location. Failing at synth
144
+ * time is safer than deploying a model with dangling references.
145
+ *
146
+ * This is an existence check only. In the unlikely case that a reference
147
+ * path stays occupied but its content changes, for example "#/oneOf/1"
148
+ * pointing at a union that gets a null alternative inserted, the change is
149
+ * not detected. Do not reference union alternatives by index.
150
+ */
151
+ function assertLocalReferencesResolve(schema) {
152
+ const broken = findLocalReferences(schema).filter((reference) => !resolvesInDocument(schema, reference));
153
+ if (broken.length > 0) {
154
+ throw new Error(`Converting nullable schemas broke local references: ${broken.join(", ")}. Restructure the schema, for example move the referenced schema to definitions.`);
155
+ }
156
+ }
93
157
  /**
94
158
  * Wraps a schema and a null alternative into a union. Definitions are kept at
95
159
  * the root, otherwise local references like "#/definitions/X" would point to
96
160
  * nothing after wrapping.
97
- *
98
- * Throws if the schema contains local references to other locations, such as
99
- * "#/properties/...". Wrapping would silently break them, so failing at synth
100
- * time is safer than deploying a broken model.
101
161
  */
102
162
  function wrapSchemaWithNull(schema, unionKeyword) {
103
163
  const { definitions, ...rest } = schema;
104
- const brokenReferences = findLocalReferences(rest).filter((reference) => !reference.startsWith("#/definitions/"));
105
- if (brokenReferences.length > 0) {
106
- throw new Error(`Cannot convert nullable schema: wrapping would break local references: ${brokenReferences.join(", ")}. Only #/definitions/ references are supported.`);
107
- }
108
164
  const wrapped = {
109
165
  [unionKeyword]: [rest, { type: "null" }],
110
166
  };
@@ -127,6 +183,12 @@ function convertNullableSchema(schema) {
127
183
  if (nullable !== true) {
128
184
  return rest;
129
185
  }
186
+ // A null type already allows only null, so adding a null alternative
187
+ // would make null match two oneOf alternatives and be rejected.
188
+ // biome-ignore lint/complexity/useLiteralKeys: index signature requires bracket notation
189
+ if (rest["type"] === "null") {
190
+ return rest;
191
+ }
130
192
  // A simple typed schema cannot overlap with null, so oneOf is safe.
131
193
  // biome-ignore lint/complexity/useLiteralKeys: index signature requires bracket notation
132
194
  if (typeof rest["type"] === "string") {
@@ -184,14 +246,18 @@ function removePatternKeyword(schema) {
184
246
  * allowed. The conversion is applied recursively to nested schemas. Field
185
247
  * names and literal values such as defaults are left untouched.
186
248
  *
187
- * Limitation: when a nullable schema is wrapped, only references to
188
- * `#/definitions/...` stay valid. If the schema contains local references to
189
- * other locations, such as `#/properties/...`, an error is thrown instead of
190
- * producing a model with broken references. Zod output never contains such
249
+ * Limitation: wrapping moves schema content under oneOf/anyOf, which can
250
+ * break local references such as `#/properties/...` pointing at the old
251
+ * location. After conversion every local reference is checked against the
252
+ * result, and an error is thrown instead of producing a model with dangling
253
+ * references. References to `#/definitions/...` targets stay valid because
254
+ * definitions are kept at the root. Zod output never contains such
191
255
  * references, so this only matters for hand-written schemas.
192
256
  */
193
257
  export function convertJsonSchemaNullableToOneOfDeep(schema) {
194
- return transformSchemaDeep(schema, convertNullableSchema);
258
+ const converted = transformSchemaDeep(schema, convertNullableSchema);
259
+ assertLocalReferencesResolve(converted);
260
+ return converted;
195
261
  }
196
262
  /**
197
263
  * Creates a JSON Schema that can be used as an API Gateway model from a Zod schema.
@@ -226,7 +292,9 @@ export function toApiGatewayJsonSchema(schema) {
226
292
  target: "openapi-3.0",
227
293
  unrepresentable: "any",
228
294
  });
229
- return transformSchemaDeep(jsonSchema, (schemaObject) => convertNullableSchema(removePatternKeyword(schemaObject)));
295
+ const converted = transformSchemaDeep(jsonSchema, (schemaObject) => convertNullableSchema(removePatternKeyword(schemaObject)));
296
+ assertLocalReferencesResolve(converted);
297
+ return converted;
230
298
  }
231
299
  /**
232
300
  * Get a reference to an OpenAPI model object in a REST API.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@digitraffic/common",
3
- "version": "2026.10.30-2",
3
+ "version": "2026.11.13-1",
4
4
  "private": false,
5
5
  "description": "",
6
6
  "repository": {
@@ -112,63 +112,63 @@
112
112
  "dist/**/*.d.ts"
113
113
  ],
114
114
  "devDependencies": {
115
- "@aws-sdk/client-api-gateway": "3.1098.0",
116
- "@aws-sdk/client-s3": "3.1098.0",
117
- "@aws-sdk/client-secrets-manager": "3.1098.0",
118
- "@aws-sdk/client-sns": "3.1098.0",
119
- "@aws-sdk/lib-storage": "3.1098.0",
120
- "@biomejs/biome": "2.5.6",
115
+ "@aws-sdk/client-api-gateway": "3.1163.0",
116
+ "@aws-sdk/client-s3": "3.1163.0",
117
+ "@aws-sdk/client-secrets-manager": "3.1163.0",
118
+ "@aws-sdk/client-sns": "3.1163.0",
119
+ "@aws-sdk/lib-storage": "3.1163.0",
120
+ "@biomejs/biome": "2.6.5",
121
121
  "@date-fns/tz": "1.5.0",
122
- "@rushstack/heft": "1.2.22",
123
- "@rushstack/heft-typescript-plugin": "1.3.17",
124
- "@smithy/fetch-http-handler": "5.6.13",
125
- "@smithy/node-http-handler": "4.9.13",
126
- "@smithy/types": "4.16.1",
127
- "@types/aws-lambda": "8.10.162",
122
+ "@rushstack/heft": "1.3.5",
123
+ "@rushstack/heft-typescript-plugin": "1.3.27",
124
+ "@smithy/fetch-http-handler": "5.8.1",
125
+ "@smithy/node-http-handler": "4.14.2",
126
+ "@smithy/types": "4.20.0",
127
+ "@types/aws-lambda": "8.10.164",
128
128
  "@types/etag": "1.8.4",
129
129
  "@types/geojson": "7946.0.16",
130
130
  "@types/geojson-validation": "1.0.3",
131
131
  "@types/madge": "5.0.3",
132
- "@types/node": "24.13.3",
132
+ "@types/node": "24.19.2",
133
133
  "@vitest/coverage-v8": "4.1.12",
134
- "aws-cdk-lib": "2.262.2",
135
- "constructs": "10.7.2",
134
+ "aws-cdk-lib": "2.275.0",
135
+ "constructs": "10.8.1",
136
136
  "date-fns": "4.4.0",
137
- "es-toolkit": "1.50.0",
137
+ "es-toolkit": "1.53.2",
138
138
  "etag": "1.8.1",
139
139
  "geojson-validation": "1.0.2",
140
- "ky": "2.0.2",
141
- "lefthook": "2.1.10",
140
+ "ky": "2.2.0",
141
+ "lefthook": "2.2.2",
142
142
  "madge": "8.0.0",
143
- "pg-native": "3.8.0",
144
- "pg-promise": "12.7.0",
145
- "pg-query-stream": "4.16.0",
146
- "rimraf": "6.1.3",
147
- "sort-package-json": "4.0.0",
143
+ "pg-native": "3.9.1",
144
+ "pg-promise": "12.7.1",
145
+ "pg-query-stream": "4.17.0",
146
+ "rimraf": "6.1.4",
147
+ "sort-package-json": "4.1.0",
148
148
  "typescript": "5.9.3",
149
- "undici": "8.12.2",
149
+ "undici": "8.12.4",
150
150
  "velocityjs": "2.1.7",
151
151
  "vitest": "4.1.12",
152
152
  "zod": "4.6.7"
153
153
  },
154
154
  "peerDependencies": {
155
- "@aws-sdk/client-api-gateway": "3.1098.0",
156
- "@aws-sdk/client-s3": "3.1098.0",
157
- "@aws-sdk/client-secrets-manager": "3.1098.0",
158
- "@aws-sdk/client-sns": "3.1098.0",
159
- "@aws-sdk/lib-storage": "3.1098.0",
155
+ "@aws-sdk/client-api-gateway": "3.1163.0",
156
+ "@aws-sdk/client-s3": "3.1163.0",
157
+ "@aws-sdk/client-secrets-manager": "3.1163.0",
158
+ "@aws-sdk/client-sns": "3.1163.0",
159
+ "@aws-sdk/lib-storage": "3.1163.0",
160
160
  "@date-fns/tz": "1.5.0",
161
- "@smithy/fetch-http-handler": "5.6.13",
162
- "@smithy/node-http-handler": "4.9.13",
163
- "aws-cdk-lib": "2.262.2",
164
- "constructs": "10.7.2",
161
+ "@smithy/fetch-http-handler": "5.8.1",
162
+ "@smithy/node-http-handler": "4.14.2",
163
+ "aws-cdk-lib": "2.275.0",
164
+ "constructs": "10.8.1",
165
165
  "date-fns": "4.4.0",
166
- "es-toolkit": "1.50.0",
166
+ "es-toolkit": "1.53.2",
167
167
  "geojson-validation": "1.0.2",
168
- "ky": "2.0.2",
169
- "pg-native": "3.8.0",
170
- "pg-promise": "12.7.0",
171
- "undici": "8.12.2",
168
+ "ky": "2.2.0",
169
+ "pg-native": "3.9.1",
170
+ "pg-promise": "12.7.1",
171
+ "undici": "8.12.4",
172
172
  "zod": "4.6.7"
173
173
  },
174
174
  "engines": {