@contrail/util 1.4.0 → 1.5.0

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/CHANGELOG.md CHANGED
@@ -14,6 +14,22 @@ Versioning follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
14
14
  - `RESPONSE_BYTE_LIMIT` — the cap minus the buffer, in bytes, for size checks that work in bytes.
15
15
  - `getSerializedByteSize` — serialized byte size of any value. Treats `undefined` as `null`, since `JSON.stringify(undefined)` returns `undefined` rather than a string and `Buffer.byteLength` throws on it.
16
16
 
17
+ ## [1.5.0] - 2026-09-25
18
+
19
+ Maintenance release on the 1.x line; the same changes ship on 2.x as 2.2.0 and 2.2.1.
20
+
21
+ ### Added
22
+
23
+ - `response-size-util` — measures whether a payload fits within the AWS Lambda response limit, so services can decide to offload to S3 based on actual serialized bytes rather than a row count.
24
+ - `exceedsResponseBudget(value, options?)` — the decision helper. For arrays it serializes one element at a time and stops as soon as the budget is crossed, so the cost is bounded by the budget rather than by the size of the payload.
25
+ - `getJsonByteLength(value)` / `getUtf8ByteLength(str)` — exact UTF-8 byte measurement.
26
+ - `getResponseBudgetBytes(options?)` — the budget a measured payload must stay under.
27
+ - `LAMBDA_MAX_RESPONSE_BYTES`, `RESPONSE_GUARD_LIMIT_BYTES`, `DEFAULT_RESPONSE_BUFFER_BYTES`, `DEFAULT_RESPONSE_BUDGET_BYTES` — the ladder of thresholds a response crosses, each sitting below the one above it so a payload offloads before `core`'s response guard returns a 413, and the guard fires before Lambda rejects the invocation.
28
+
29
+ ### Fixed
30
+
31
+ - `ObjectUtil.mergeDeep` no longer discards a value when the target cannot be merged into. An incoming object landing on an existing array, string, or number replaces it, instead of being silently skipped. Merging two objects is unchanged: keys the source omits still survive.
32
+
17
33
  ## [1.2.1] - 2026-03-25
18
34
 
19
35
  ### Added
package/lib/index.d.ts CHANGED
@@ -7,6 +7,7 @@ export * from './object-util/object-util';
7
7
  export * from './order-util/order-util';
8
8
  export * from './performance-util/performance-util';
9
9
  export * from './promise-util/promise-util';
10
+ export * from './response-size-util/response-size-util';
10
11
  export * from './retry-util/retry-util';
11
12
  export * from './string-util/string-util';
12
13
  export * from './timer-util/timer-util';
package/lib/index.js CHANGED
@@ -23,6 +23,7 @@ __exportStar(require("./object-util/object-util"), exports);
23
23
  __exportStar(require("./order-util/order-util"), exports);
24
24
  __exportStar(require("./performance-util/performance-util"), exports);
25
25
  __exportStar(require("./promise-util/promise-util"), exports);
26
+ __exportStar(require("./response-size-util/response-size-util"), exports);
26
27
  __exportStar(require("./retry-util/retry-util"), exports);
27
28
  __exportStar(require("./string-util/string-util"), exports);
28
29
  __exportStar(require("./timer-util/timer-util"), exports);
@@ -9,7 +9,7 @@ function mergeDeep(target, ...sources) {
9
9
  if ((0, isObject_1.isObject)(target) && (0, isObject_1.isObject)(source)) {
10
10
  for (const key in source) {
11
11
  if ((0, isObject_1.isObject)(source[key]) && !isDate(source[key])) {
12
- if (!target[key])
12
+ if (!(0, isObject_1.isObject)(target[key]))
13
13
  Object.assign(target, { [key]: {} });
14
14
  mergeDeep(target[key], source[key]);
15
15
  }
@@ -0,0 +1,12 @@
1
+ export declare const LAMBDA_MAX_RESPONSE_BYTES = 6000000;
2
+ export declare const RESPONSE_GUARD_LIMIT_BYTES = 5500000;
3
+ export declare const DEFAULT_RESPONSE_BUFFER_BYTES = 500000;
4
+ export declare const DEFAULT_RESPONSE_BUDGET_BYTES: number;
5
+ export interface ResponseBudgetOptions {
6
+ limitBytes?: number;
7
+ bufferBytes?: number;
8
+ }
9
+ export declare function getResponseBudgetBytes(options?: ResponseBudgetOptions): number;
10
+ export declare function getUtf8ByteLength(str: string): number;
11
+ export declare function getJsonByteLength(value: unknown): number;
12
+ export declare function exceedsResponseBudget(value: unknown, options?: ResponseBudgetOptions): boolean;
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DEFAULT_RESPONSE_BUDGET_BYTES = exports.DEFAULT_RESPONSE_BUFFER_BYTES = exports.RESPONSE_GUARD_LIMIT_BYTES = exports.LAMBDA_MAX_RESPONSE_BYTES = void 0;
4
+ exports.getResponseBudgetBytes = getResponseBudgetBytes;
5
+ exports.getUtf8ByteLength = getUtf8ByteLength;
6
+ exports.getJsonByteLength = getJsonByteLength;
7
+ exports.exceedsResponseBudget = exceedsResponseBudget;
8
+ exports.LAMBDA_MAX_RESPONSE_BYTES = 6000000;
9
+ exports.RESPONSE_GUARD_LIMIT_BYTES = 5500000;
10
+ exports.DEFAULT_RESPONSE_BUFFER_BYTES = 500000;
11
+ exports.DEFAULT_RESPONSE_BUDGET_BYTES = exports.RESPONSE_GUARD_LIMIT_BYTES - exports.DEFAULT_RESPONSE_BUFFER_BYTES;
12
+ const JSON_NULL_BYTES = 'null'.length;
13
+ function getResponseBudgetBytes(options = {}) {
14
+ var _a, _b;
15
+ const limitBytes = (_a = options.limitBytes) !== null && _a !== void 0 ? _a : exports.RESPONSE_GUARD_LIMIT_BYTES;
16
+ const bufferBytes = (_b = options.bufferBytes) !== null && _b !== void 0 ? _b : exports.DEFAULT_RESPONSE_BUFFER_BYTES;
17
+ return Math.max(0, limitBytes - bufferBytes);
18
+ }
19
+ function getUtf8ByteLength(str) {
20
+ if (typeof Buffer !== 'undefined') {
21
+ return Buffer.byteLength(str, 'utf8');
22
+ }
23
+ return new TextEncoder().encode(str).length;
24
+ }
25
+ function getJsonByteLength(value) {
26
+ const serialized = JSON.stringify(value);
27
+ return serialized === undefined ? 0 : getUtf8ByteLength(serialized);
28
+ }
29
+ function exceedsResponseBudget(value, options = {}) {
30
+ const budgetBytes = getResponseBudgetBytes(options);
31
+ if (value === null || value === undefined) {
32
+ return false;
33
+ }
34
+ if (!Array.isArray(value)) {
35
+ return getJsonByteLength(value) > budgetBytes;
36
+ }
37
+ if (value.length === 0) {
38
+ return false;
39
+ }
40
+ const bracketBytes = '[]'.length;
41
+ const separatorBytes = value.length - 1;
42
+ let totalBytes = bracketBytes + separatorBytes;
43
+ for (const element of value) {
44
+ if (totalBytes > budgetBytes) {
45
+ return true;
46
+ }
47
+ const serialized = JSON.stringify(element);
48
+ totalBytes += serialized === undefined ? JSON_NULL_BYTES : getUtf8ByteLength(serialized);
49
+ }
50
+ return totalBytes > budgetBytes;
51
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrail/util",
3
- "version": "1.4.0",
3
+ "version": "1.5.0",
4
4
  "description": "General JavaScript utilities",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",