@h-ai/crypto 0.1.0-alpha.73 → 0.1.0-alpha.74
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/README.en.md +11 -0
- package/README.ja.md +11 -0
- package/README.md +11 -0
- package/dist/index.d.ts +5 -1
- package/dist/index.js +14 -4
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/README.en.md
CHANGED
|
@@ -296,6 +296,17 @@ The admin console's online encryption demo calls the Crypto SM3/SM4 APIs directl
|
|
|
296
296
|
|
|
297
297
|
Production transport registries must declare stateScope=shared and actually use Redis or PostgreSQL/MySQL. Memory, Redis factories over memory, and RelDB factories over SQLite are rejected. All nodes load the same managed versioned key ring. Initialization checks apply when NODE_ENV or HAI_CORE_ENV is production; only explicit HAI_E2E=1/HAI_E2E_MOCK=1 tests are exempt. [State, concurrency and recovery (Chinese)](../../.agents/instructions/distributed-state.md)。
|
|
298
298
|
|
|
299
|
+
## Module requirements and explicit limits
|
|
300
|
+
|
|
301
|
+
`resolveTransportBodyLimits(limit?, { requiredPlaintextBytes? })` derives capacity from enabled modules only when limit is omitted; without module requirements it remains 1 MiB. A smaller explicit limit throws RangeError instead of silently increasing or reducing capacity. All values remain bounded to 1 KiB–64 MiB; encrypted capacity is twice plaintext plus 32 KiB. Serv/Kit configuration accepts `auto`, normalized to undefined at runtime.
|
|
302
|
+
|
|
303
|
+
```ts
|
|
304
|
+
import { resolveTransportBodyLimits } from '@h-ai/crypto'
|
|
305
|
+
|
|
306
|
+
const limits = resolveTransportBodyLimits(24 * 1024 * 1024, { requiredPlaintextBytes: 11250348 })
|
|
307
|
+
// plaintextBytes: 25165824; encryptedPayloadBytes: 50364416
|
|
308
|
+
```
|
|
309
|
+
|
|
299
310
|
## Development and testing
|
|
300
311
|
|
|
301
312
|
```bash
|
package/README.ja.md
CHANGED
|
@@ -296,6 +296,17 @@ if (!result.success) {
|
|
|
296
296
|
|
|
297
297
|
本番の伝送 registry は stateScope=shared と実際の Redis または PostgreSQL/MySQL が必須です。memory、および memory/SQLite を包む工場も拒否します。各ノードは同じ管理されたバージョン付き鍵セットを読み込みます。 NODE_ENV または HAI_CORE_ENV が production の場合、初期化成功前に検証します。明示的な HAI_E2E=1/HAI_E2E_MOCK=1 テストのみ除外します。 [状態・並行制御・復旧範囲(中国語)](../../.agents/instructions/distributed-state.md)。
|
|
298
298
|
|
|
299
|
+
## モジュール要件と明示的な上限
|
|
300
|
+
|
|
301
|
+
`resolveTransportBodyLimits(limit?, { requiredPlaintextBytes? })` は limit 省略時だけ有効なモジュールの必要量から計算し、要件がなければ 1 MiB です。明示上限が必要量未満なら RangeError とし、勝手に拡張・縮小しません。範囲は 1 KiB~64 MiB、暗号文は平文の2倍+32 KiBです。Serv/Kit の `auto` は実行時に undefined へ正規化します。
|
|
302
|
+
|
|
303
|
+
```ts
|
|
304
|
+
import { resolveTransportBodyLimits } from '@h-ai/crypto'
|
|
305
|
+
|
|
306
|
+
const limits = resolveTransportBodyLimits(24 * 1024 * 1024, { requiredPlaintextBytes: 11250348 })
|
|
307
|
+
// plaintextBytes: 25165824; encryptedPayloadBytes: 50364416
|
|
308
|
+
```
|
|
309
|
+
|
|
299
310
|
## 開発とテスト
|
|
300
311
|
|
|
301
312
|
```bash
|
package/README.md
CHANGED
|
@@ -296,6 +296,17 @@ if (!result.success) {
|
|
|
296
296
|
|
|
297
297
|
生产传输 registry 必须声明 stateScope=shared 并实际使用 Redis 或 PostgreSQL/MySQL。memory、包装 memory 的 Redis 工厂、包装 SQLite 的 RelDB 工厂会被拒绝;各节点加载相同受管版本化密钥环。 NODE_ENV 或 HAI_CORE_ENV 为 production 时在初始化发布成功前校验;仅 HAI_E2E=1/HAI_E2E_MOCK=1 明确测试可豁免。 [状态、并发和恢复边界](../../.agents/instructions/distributed-state.md)。
|
|
298
298
|
|
|
299
|
+
## 模块容量与显式上限
|
|
300
|
+
|
|
301
|
+
`resolveTransportBodyLimits(limit?, { requiredPlaintextBytes? })` 只在 limit 未设置时按模块需求推导,无模块需求时仍为 1 MiB。显式上限小于模块需求时抛出 RangeError,不能静默扩大或缩小。所有值仍须在 1 KiB~64 MiB 范围;密文预算为明文两倍加 32 KiB。Serv/Kit 配置可用 `auto` 表示未显式限额,运行时将它规范化为 undefined。
|
|
302
|
+
|
|
303
|
+
```ts
|
|
304
|
+
import { resolveTransportBodyLimits } from '@h-ai/crypto'
|
|
305
|
+
|
|
306
|
+
const limits = resolveTransportBodyLimits(24 * 1024 * 1024, { requiredPlaintextBytes: 11250348 })
|
|
307
|
+
// limits.plaintextBytes === 25165824;limits.encryptedPayloadBytes === 50364416
|
|
308
|
+
```
|
|
309
|
+
|
|
299
310
|
## 开发测试
|
|
300
311
|
|
|
301
312
|
```bash
|
package/dist/index.d.ts
CHANGED
|
@@ -52,6 +52,8 @@ declare const TRANSPORT_BODY_LIMITS: TransportBodyLimits;
|
|
|
52
52
|
* 校验单一业务明文容量并推导双向密文预算;返回不可变快照。
|
|
53
53
|
* SM4-CBC 使用十六进制编码,预算为明文的两倍加 32 KiB(填充及认证封装)。
|
|
54
54
|
* @param maxPlaintextBytes - UTF-8 字节数,默认 1 MiB,有效范围 1 KiB~64 MiB 的整数。
|
|
55
|
+
* @param options - 已启用模块所需的最小明文预算;只在宿主未显式设置上限时自动推导。
|
|
56
|
+
* @param options.requiredPlaintextBytes - 模块需要的最小 UTF-8 明文字节数,仍受 1 KiB~64 MiB 范围约束。
|
|
55
57
|
* @returns 明文、密文及独立安全预算。
|
|
56
58
|
* @throws RangeError - 配置无效;不接受字符串、null、无限制或静默回退。
|
|
57
59
|
* @example
|
|
@@ -61,7 +63,9 @@ declare const TRANSPORT_BODY_LIMITS: TransportBodyLimits;
|
|
|
61
63
|
* const nginxLimit = limits.encryptedPayloadBytes
|
|
62
64
|
* ```
|
|
63
65
|
*/
|
|
64
|
-
declare function resolveTransportBodyLimits(maxPlaintextBytes?: number
|
|
66
|
+
declare function resolveTransportBodyLimits(maxPlaintextBytes?: number, options?: {
|
|
67
|
+
requiredPlaintextBytes?: number;
|
|
68
|
+
}): TransportBodyLimits;
|
|
65
69
|
/**
|
|
66
70
|
* 可识别的容量错误;只包含方向、阶段和字节数,不包含报文内容。
|
|
67
71
|
* @example
|
package/dist/index.js
CHANGED
|
@@ -58,7 +58,8 @@ var en_US_default = {
|
|
|
58
58
|
crypto_transportSessionKeyDecryptFailed: "Failed to decrypt transport session key",
|
|
59
59
|
crypto_transportBodyLimitInvalid: "Transport plaintext limit must be an integer between 1024 and 67108864 bytes",
|
|
60
60
|
crypto_transportBodyTooLarge: "Encrypted transport payload exceeds the configured byte limit",
|
|
61
|
-
crypto_transportTimeInvalid: "Your device and server clocks may be out of sync, or the request has expired. Enable automatic time synchronization and refresh to retry. If this continues, ask an administrator to check the server clock."
|
|
61
|
+
crypto_transportTimeInvalid: "Your device and server clocks may be out of sync, or the request has expired. Enable automatic time synchronization and refresh to retry. If this continues, ask an administrator to check the server clock.",
|
|
62
|
+
crypto_transportBodyLimitConflict: "Explicit transport limit {limit} bytes is below the enabled module requirement of {required} bytes; adjust the module budget or explicit limit"
|
|
62
63
|
};
|
|
63
64
|
|
|
64
65
|
// messages/ja-JP.json
|
|
@@ -116,7 +117,8 @@ var ja_JP_default = {
|
|
|
116
117
|
crypto_transportSessionKeyDecryptFailed: "\u901A\u4FE1\u30BB\u30C3\u30B7\u30E7\u30F3\u9375\u306E\u5FA9\u53F7\u306B\u5931\u6557\u3057\u307E\u3057\u305F",
|
|
117
118
|
crypto_transportBodyLimitInvalid: "\u901A\u4FE1\u5E73\u6587\u306E\u4E0A\u9650\u306F 1024 \u304B\u3089 67108864 \u30D0\u30A4\u30C8\u306E\u6574\u6570\u3067\u6307\u5B9A\u3057\u3066\u304F\u3060\u3055\u3044",
|
|
118
119
|
crypto_transportBodyTooLarge: "\u6697\u53F7\u5316\u901A\u4FE1\u306E\u30C7\u30FC\u30BF\u304C\u8A2D\u5B9A\u3055\u308C\u305F\u30D0\u30A4\u30C8\u4E0A\u9650\u3092\u8D85\u3048\u3066\u3044\u307E\u3059",
|
|
119
|
-
crypto_transportTimeInvalid: "\u7AEF\u672B\u3068\u30B5\u30FC\u30D0\u30FC\u306E\u6642\u523B\u304C\u540C\u671F\u3057\u3066\u3044\u306A\u3044\u304B\u3001\u8981\u6C42\u306E\u6709\u52B9\u671F\u9650\u304C\u5207\u308C\u3066\u3044\u307E\u3059\u3002\u81EA\u52D5\u6642\u523B\u540C\u671F\u3092\u6709\u52B9\u306B\u3057\u3066\u30DA\u30FC\u30B8\u3092\u518D\u8AAD\u307F\u8FBC\u307F\u3057\u3066\u304F\u3060\u3055\u3044\u3002\u89E3\u6D88\u3057\u306A\u3044\u5834\u5408\u306F\u7BA1\u7406\u8005\u306B\u30B5\u30FC\u30D0\u30FC\u306E\u6642\u523B\u78BA\u8A8D\u3092\u4F9D\u983C\u3057\u3066\u304F\u3060\u3055\u3044\u3002"
|
|
120
|
+
crypto_transportTimeInvalid: "\u7AEF\u672B\u3068\u30B5\u30FC\u30D0\u30FC\u306E\u6642\u523B\u304C\u540C\u671F\u3057\u3066\u3044\u306A\u3044\u304B\u3001\u8981\u6C42\u306E\u6709\u52B9\u671F\u9650\u304C\u5207\u308C\u3066\u3044\u307E\u3059\u3002\u81EA\u52D5\u6642\u523B\u540C\u671F\u3092\u6709\u52B9\u306B\u3057\u3066\u30DA\u30FC\u30B8\u3092\u518D\u8AAD\u307F\u8FBC\u307F\u3057\u3066\u304F\u3060\u3055\u3044\u3002\u89E3\u6D88\u3057\u306A\u3044\u5834\u5408\u306F\u7BA1\u7406\u8005\u306B\u30B5\u30FC\u30D0\u30FC\u306E\u6642\u523B\u78BA\u8A8D\u3092\u4F9D\u983C\u3057\u3066\u304F\u3060\u3055\u3044\u3002",
|
|
121
|
+
crypto_transportBodyLimitConflict: "\u660E\u793A\u3055\u308C\u305F\u8EE2\u9001\u4E0A\u9650 {limit} \u30D0\u30A4\u30C8\u306F\u6709\u52B9\u306A\u30E2\u30B8\u30E5\u30FC\u30EB\u306E\u5FC5\u8981\u91CF {required} \u30D0\u30A4\u30C8\u672A\u6E80\u3067\u3059\u3002\u4E88\u7B97\u307E\u305F\u306F\u4E0A\u9650\u3092\u5909\u66F4\u3057\u3066\u304F\u3060\u3055\u3044"
|
|
120
122
|
};
|
|
121
123
|
|
|
122
124
|
// messages/zh-CN.json
|
|
@@ -174,7 +176,8 @@ var zh_CN_default = {
|
|
|
174
176
|
crypto_transportSessionKeyDecryptFailed: "\u4F20\u8F93\u52A0\u5BC6\u4F1A\u8BDD\u5BC6\u94A5\u89E3\u5BC6\u5931\u8D25",
|
|
175
177
|
crypto_transportBodyLimitInvalid: "\u4F20\u8F93\u660E\u6587\u5BB9\u91CF\u5FC5\u987B\u662F 1024 \u5230 67108864 \u5B57\u8282\u4E4B\u95F4\u7684\u6574\u6570",
|
|
176
178
|
crypto_transportBodyTooLarge: "\u52A0\u5BC6\u4F20\u8F93\u62A5\u6587\u8D85\u8FC7\u914D\u7F6E\u7684\u5B57\u8282\u4E0A\u9650",
|
|
177
|
-
crypto_transportTimeInvalid: "\u8BBE\u5907\u4E0E\u670D\u52A1\u5668\u65F6\u95F4\u53EF\u80FD\u4E0D\u540C\u6B65\uFF0C\u6216\u8BF7\u6C42\u5DF2\u8FC7\u671F\u3002\u8BF7\u5F00\u542F\u81EA\u52A8\u6821\u65F6\u5E76\u5237\u65B0\u91CD\u8BD5\uFF1B\u82E5\u4ECD\u5931\u8D25\uFF0C\u8BF7\u8054\u7CFB\u7BA1\u7406\u5458\u68C0\u67E5\u670D\u52A1\u5668\u65F6\u95F4\u3002"
|
|
179
|
+
crypto_transportTimeInvalid: "\u8BBE\u5907\u4E0E\u670D\u52A1\u5668\u65F6\u95F4\u53EF\u80FD\u4E0D\u540C\u6B65\uFF0C\u6216\u8BF7\u6C42\u5DF2\u8FC7\u671F\u3002\u8BF7\u5F00\u542F\u81EA\u52A8\u6821\u65F6\u5E76\u5237\u65B0\u91CD\u8BD5\uFF1B\u82E5\u4ECD\u5931\u8D25\uFF0C\u8BF7\u8054\u7CFB\u7BA1\u7406\u5458\u68C0\u67E5\u670D\u52A1\u5668\u65F6\u95F4\u3002",
|
|
180
|
+
crypto_transportBodyLimitConflict: "\u663E\u5F0F\u4F20\u8F93\u4E0A\u9650 {limit} \u5B57\u8282\u5C0F\u4E8E\u5DF2\u542F\u7528\u6A21\u5757\u6240\u9700\u7684 {required} \u5B57\u8282\uFF1B\u8BF7\u8C03\u6574\u6A21\u5757\u9884\u7B97\u6216\u663E\u5F0F\u4E0A\u9650"
|
|
178
181
|
};
|
|
179
182
|
|
|
180
183
|
// src/crypto-i18n.ts
|
|
@@ -852,9 +855,16 @@ var TRANSPORT_BODY_LIMITS = Object.freeze({
|
|
|
852
855
|
/** 密文 JSON 的 UTF-8 总字节数(2 MiB + 32 KiB,含 CBC 填充与全部认证字段)。 */
|
|
853
856
|
encryptedPayloadBytes: 2129920
|
|
854
857
|
});
|
|
855
|
-
function resolveTransportBodyLimits(maxPlaintextBytes =
|
|
858
|
+
function resolveTransportBodyLimits(maxPlaintextBytes, options = {}) {
|
|
859
|
+
const required = options.requiredPlaintextBytes;
|
|
860
|
+
if (required !== void 0 && (!Number.isSafeInteger(required) || required < TRANSPORT_PLAINTEXT_RANGE.min || required > TRANSPORT_PLAINTEXT_RANGE.max))
|
|
861
|
+
throw new RangeError(cryptoM("crypto_transportBodyLimitInvalid"));
|
|
862
|
+
if (maxPlaintextBytes === void 0)
|
|
863
|
+
maxPlaintextBytes = required ?? TRANSPORT_BODY_LIMITS.plaintextBytes;
|
|
856
864
|
if (!Number.isSafeInteger(maxPlaintextBytes) || maxPlaintextBytes < TRANSPORT_PLAINTEXT_RANGE.min || maxPlaintextBytes > TRANSPORT_PLAINTEXT_RANGE.max)
|
|
857
865
|
throw new RangeError(cryptoM("crypto_transportBodyLimitInvalid"));
|
|
866
|
+
if (required !== void 0 && maxPlaintextBytes < required)
|
|
867
|
+
throw new RangeError(cryptoM("crypto_transportBodyLimitConflict", { params: { limit: maxPlaintextBytes, required } }));
|
|
858
868
|
return Object.freeze({ ...TRANSPORT_BODY_LIMITS, plaintextBytes: maxPlaintextBytes, encryptedPayloadBytes: 2 * maxPlaintextBytes + 32768 });
|
|
859
869
|
}
|
|
860
870
|
var TransportBodyTooLargeError = class extends RangeError {
|