streamseed 0.0.0-stage → 0.1.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/LICENSE +661 -0
- package/README.md +88 -2
- package/dist/control/control.d.ts +89 -0
- package/dist/control/control.js +396 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +15 -0
- package/dist/internal/abort.d.ts +8 -0
- package/dist/internal/abort.js +52 -0
- package/dist/internal/cbor-codec.d.ts +25 -0
- package/dist/internal/cbor-codec.js +134 -0
- package/dist/internal/ed25519.d.ts +15 -0
- package/dist/internal/ed25519.js +55 -0
- package/dist/internal/secp256k1.d.ts +3 -0
- package/dist/internal/secp256k1.js +15 -0
- package/dist/node/codec.d.ts +23 -0
- package/dist/node/codec.js +231 -0
- package/dist/node/document.d.ts +10 -0
- package/dist/node/document.js +123 -0
- package/dist/node/media.d.ts +6 -0
- package/dist/node/media.js +69 -0
- package/dist/node/sequence.d.ts +44 -0
- package/dist/node/sequence.js +137 -0
- package/dist/node/types.d.ts +93 -0
- package/dist/node/types.js +105 -0
- package/dist/program/codec.d.ts +10 -0
- package/dist/program/codec.js +165 -0
- package/dist/program/resolver.d.ts +25 -0
- package/dist/program/resolver.js +189 -0
- package/dist/program/types.d.ts +61 -0
- package/dist/program/types.js +90 -0
- package/dist/protocol/constants.d.ts +33 -0
- package/dist/protocol/constants.js +32 -0
- package/dist/protocol/errors.d.ts +46 -0
- package/dist/protocol/errors.js +57 -0
- package/dist/protocol/hash.d.ts +14 -0
- package/dist/protocol/hash.js +42 -0
- package/dist/protocol/ids.d.ts +60 -0
- package/dist/protocol/ids.js +139 -0
- package/dist/wire/artifact.d.ts +28 -0
- package/dist/wire/artifact.js +74 -0
- package/dist/wire/codec.d.ts +12 -0
- package/dist/wire/codec.js +42 -0
- package/package.json +79 -3
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { MAX_MEDIA_BLOCKS, MAX_MEDIA_CONTAINER_BYTES } from "../protocol/constants.js";
|
|
2
|
+
import { ERROR_CODES, streamSeedError } from "../protocol/errors.js";
|
|
3
|
+
import { bytesEqual, copyBytes } from "../protocol/ids.js";
|
|
4
|
+
import { CBORDecoder, CBOREncoder } from "../internal/cbor-codec.js";
|
|
5
|
+
/** Encode the exact deterministic-CBOR media array authenticated by DATA. */
|
|
6
|
+
export function encodeMediaContainer(blocks) {
|
|
7
|
+
if (blocks.length === 0 || blocks.length > MAX_MEDIA_BLOCKS) {
|
|
8
|
+
throw streamSeedError(ERROR_CODES.INVALID_MEDIA_CONTAINER, `media block count ${blocks.length} is outside 1..${MAX_MEDIA_BLOCKS}`);
|
|
9
|
+
}
|
|
10
|
+
let total = cborHeadSize(BigInt(blocks.length));
|
|
11
|
+
for (let index = 0; index < blocks.length; index += 1) {
|
|
12
|
+
const block = blocks[index];
|
|
13
|
+
if (!(block instanceof Uint8Array) || block.length === 0) {
|
|
14
|
+
throw streamSeedError(ERROR_CODES.INVALID_MEDIA_CONTAINER, `media block ${index} is empty or invalid`);
|
|
15
|
+
}
|
|
16
|
+
if (block.length > MAX_MEDIA_CONTAINER_BYTES) {
|
|
17
|
+
throw streamSeedError(ERROR_CODES.FIELD_TOO_LARGE, `media block ${index} exceeds ${MAX_MEDIA_CONTAINER_BYTES} bytes`);
|
|
18
|
+
}
|
|
19
|
+
total += cborHeadSize(BigInt(block.length)) + block.length;
|
|
20
|
+
if (total > MAX_MEDIA_CONTAINER_BYTES) {
|
|
21
|
+
throw streamSeedError(ERROR_CODES.FIELD_TOO_LARGE, `media container exceeds ${MAX_MEDIA_CONTAINER_BYTES} bytes`);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
const output = new CBOREncoder();
|
|
25
|
+
output.array(blocks.length);
|
|
26
|
+
for (const block of blocks)
|
|
27
|
+
output.bytes(block);
|
|
28
|
+
return output.finish();
|
|
29
|
+
}
|
|
30
|
+
/** Strictly decode the exact media array bytes carried by DATA. */
|
|
31
|
+
export function decodeMediaContainer(data) {
|
|
32
|
+
if (data.length === 0)
|
|
33
|
+
throw streamSeedError(ERROR_CODES.INVALID_MEDIA_CONTAINER, "media container is empty");
|
|
34
|
+
if (data.length > MAX_MEDIA_CONTAINER_BYTES) {
|
|
35
|
+
throw streamSeedError(ERROR_CODES.FIELD_TOO_LARGE, `media container exceeds ${MAX_MEDIA_CONTAINER_BYTES} bytes`);
|
|
36
|
+
}
|
|
37
|
+
const input = new CBORDecoder(data, BigInt(MAX_MEDIA_CONTAINER_BYTES));
|
|
38
|
+
const count = input.array();
|
|
39
|
+
if (count === 0 || count > MAX_MEDIA_BLOCKS) {
|
|
40
|
+
throw streamSeedError(ERROR_CODES.INVALID_MEDIA_CONTAINER, `media block count ${count} is outside 1..${MAX_MEDIA_BLOCKS}`);
|
|
41
|
+
}
|
|
42
|
+
const blocks = [];
|
|
43
|
+
for (let index = 0; index < count; index += 1) {
|
|
44
|
+
const block = input.bytes(MAX_MEDIA_CONTAINER_BYTES);
|
|
45
|
+
if (block.length === 0)
|
|
46
|
+
throw streamSeedError(ERROR_CODES.INVALID_MEDIA_CONTAINER, `media block ${index} is empty`);
|
|
47
|
+
blocks.push(block);
|
|
48
|
+
}
|
|
49
|
+
input.requireEOF();
|
|
50
|
+
if (!bytesEqual(encodeMediaContainer(blocks), data)) {
|
|
51
|
+
throw streamSeedError(ERROR_CODES.NON_CANONICAL_CBOR, "media container does not re-encode byte-for-byte");
|
|
52
|
+
}
|
|
53
|
+
return blocks;
|
|
54
|
+
}
|
|
55
|
+
export function cloneMediaBlocks(blocks) {
|
|
56
|
+
return blocks === undefined ? undefined : blocks.map((block) => copyBytes(block));
|
|
57
|
+
}
|
|
58
|
+
function cborHeadSize(value) {
|
|
59
|
+
if (value < 24n)
|
|
60
|
+
return 1;
|
|
61
|
+
if (value <= 0xffn)
|
|
62
|
+
return 2;
|
|
63
|
+
if (value <= 0xffffn)
|
|
64
|
+
return 3;
|
|
65
|
+
if (value <= 0xffffffffn)
|
|
66
|
+
return 5;
|
|
67
|
+
return 9;
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=media.js.map
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { type NodeID } from "../protocol/ids.js";
|
|
2
|
+
import type { BuiltNode, NodeRef, NodeSigner } from "./types.js";
|
|
3
|
+
/** 恢复连续节点构造所需的已提交状态。 */
|
|
4
|
+
export interface NodeBuilderState {
|
|
5
|
+
/** 下一次构造将使用的序号。 */
|
|
6
|
+
nextNodeId: NodeID;
|
|
7
|
+
/** 最新在前、最多 64 条已提交节点引用。 */
|
|
8
|
+
recentRefs: NodeRef[];
|
|
9
|
+
/** END 是否已经成功构造。 */
|
|
10
|
+
sealed: boolean;
|
|
11
|
+
/** 已经构造 MaxUint64 节点且无法继续递增。 */
|
|
12
|
+
exhausted: boolean;
|
|
13
|
+
/** 后续节点默认写入的引用数量。 */
|
|
14
|
+
referenceCount: number;
|
|
15
|
+
}
|
|
16
|
+
/** 串行安全的状态式连续 Node 构造器。 */
|
|
17
|
+
export declare class NodeBuilder {
|
|
18
|
+
private readonly streamId;
|
|
19
|
+
private readonly signer;
|
|
20
|
+
private next;
|
|
21
|
+
private recentRefs;
|
|
22
|
+
private readonly refLimit;
|
|
23
|
+
private sealed;
|
|
24
|
+
private exhausted;
|
|
25
|
+
private queue;
|
|
26
|
+
constructor(streamId: Uint8Array, signer: NodeSigner, state?: NodeBuilderState, referenceCount?: number);
|
|
27
|
+
/** 从已提交状态恢复 builder。 */
|
|
28
|
+
static restore(streamId: Uint8Array, signer: NodeSigner, state: NodeBuilderState, referenceCount?: number): NodeBuilder;
|
|
29
|
+
/** 返回下一次构造将使用的 node ID。 */
|
|
30
|
+
currentNodeId(): bigint;
|
|
31
|
+
/** 返回可用于持久化与恢复的构造状态深拷贝。 */
|
|
32
|
+
state(): NodeBuilderState;
|
|
33
|
+
/** END 成功后为 true。 */
|
|
34
|
+
isSealed(): boolean;
|
|
35
|
+
/** 构造当前 ID 的 DATA;失败不会跳号。调用会按提交顺序串行化。 */
|
|
36
|
+
buildNextData(mediaBlocks: readonly Uint8Array[], signal?: AbortSignal): Promise<BuiltNode>;
|
|
37
|
+
/** 构造当前 ID 的 END;成功后 sealed。 */
|
|
38
|
+
buildEnd(signal?: AbortSignal): Promise<BuiltNode>;
|
|
39
|
+
private fields;
|
|
40
|
+
private checkOpen;
|
|
41
|
+
private commit;
|
|
42
|
+
private serial;
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=sequence.d.ts.map
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { DEFAULT_REFERENCE_COUNT, MAX_MEDIA_BLOCKS, MAX_REFERENCES } from "../protocol/constants.js";
|
|
2
|
+
import { ERROR_CODES, streamSeedError } from "../protocol/errors.js";
|
|
3
|
+
import { assertNodeID, copyBytes, newStreamID, nextNodeID } from "../protocol/ids.js";
|
|
4
|
+
import { MAX_UINT64 } from "../protocol/constants.js";
|
|
5
|
+
import { awaitWithAbort } from "../internal/abort.js";
|
|
6
|
+
import { isValidPublicKey } from "../internal/ed25519.js";
|
|
7
|
+
import { validateReferences } from "./document.js";
|
|
8
|
+
import { buildDataNode, buildEndNode } from "./codec.js";
|
|
9
|
+
/** 串行安全的状态式连续 Node 构造器。 */
|
|
10
|
+
export class NodeBuilder {
|
|
11
|
+
streamId;
|
|
12
|
+
signer;
|
|
13
|
+
next;
|
|
14
|
+
recentRefs;
|
|
15
|
+
refLimit;
|
|
16
|
+
sealed;
|
|
17
|
+
exhausted = false;
|
|
18
|
+
queue = Promise.resolve();
|
|
19
|
+
constructor(streamId, signer, state = { nextNodeId: 0n, recentRefs: [], sealed: false, exhausted: false, referenceCount: DEFAULT_REFERENCE_COUNT }, referenceCount = state.referenceCount || DEFAULT_REFERENCE_COUNT) {
|
|
20
|
+
if (streamId.length !== 32)
|
|
21
|
+
throw streamSeedError(ERROR_CODES.INVALID_LENGTH, "stream ID must be 32 bytes");
|
|
22
|
+
if (!signer || typeof signer.publicKey !== "function" || typeof signer.signNode !== "function")
|
|
23
|
+
throw streamSeedError(ERROR_CODES.SIGNER_FAILURE, "NodeSigner is required");
|
|
24
|
+
if (!isValidPublicKey(signer.publicKey()))
|
|
25
|
+
throw streamSeedError(ERROR_CODES.INVALID_PUBLIC_KEY, "NodeSigner public key is invalid");
|
|
26
|
+
if (!Number.isSafeInteger(referenceCount) || referenceCount < 1 || referenceCount > MAX_REFERENCES) {
|
|
27
|
+
throw streamSeedError(ERROR_CODES.INVALID_LENGTH, `reference count must be between 1 and ${MAX_REFERENCES}`);
|
|
28
|
+
}
|
|
29
|
+
assertNodeID(state.nextNodeId);
|
|
30
|
+
if (state.exhausted) {
|
|
31
|
+
if (state.nextNodeId !== MAX_UINT64)
|
|
32
|
+
throw streamSeedError(ERROR_CODES.INVALID_NODE_SEQUENCE, "exhausted state must retain MaxUint64 node ID");
|
|
33
|
+
validateReferenceHistory(state.nextNodeId, state.recentRefs);
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
validateReferences(state.nextNodeId, state.recentRefs);
|
|
37
|
+
}
|
|
38
|
+
if (state.nextNodeId > 0n && state.recentRefs.length === 0) {
|
|
39
|
+
throw streamSeedError(ERROR_CODES.INVALID_REFERENCE, `restored node ${state.nextNodeId} requires its immediate predecessor`);
|
|
40
|
+
}
|
|
41
|
+
this.streamId = newStreamID(streamId);
|
|
42
|
+
this.signer = signer;
|
|
43
|
+
this.next = state.nextNodeId;
|
|
44
|
+
this.recentRefs = cloneRefs(state.recentRefs);
|
|
45
|
+
this.refLimit = referenceCount;
|
|
46
|
+
this.sealed = state.sealed;
|
|
47
|
+
this.exhausted = state.exhausted;
|
|
48
|
+
}
|
|
49
|
+
/** 从已提交状态恢复 builder。 */
|
|
50
|
+
static restore(streamId, signer, state, referenceCount = state.referenceCount || DEFAULT_REFERENCE_COUNT) {
|
|
51
|
+
return new NodeBuilder(streamId, signer, state, referenceCount);
|
|
52
|
+
}
|
|
53
|
+
/** 返回下一次构造将使用的 node ID。 */
|
|
54
|
+
currentNodeId() { return this.next; }
|
|
55
|
+
/** 返回可用于持久化与恢复的构造状态深拷贝。 */
|
|
56
|
+
state() {
|
|
57
|
+
return { nextNodeId: this.next, recentRefs: cloneRefs(this.recentRefs), sealed: this.sealed, exhausted: this.exhausted, referenceCount: this.refLimit };
|
|
58
|
+
}
|
|
59
|
+
/** END 成功后为 true。 */
|
|
60
|
+
isSealed() { return this.sealed; }
|
|
61
|
+
/** 构造当前 ID 的 DATA;失败不会跳号。调用会按提交顺序串行化。 */
|
|
62
|
+
async buildNextData(mediaBlocks, signal) {
|
|
63
|
+
if (!Array.isArray(mediaBlocks))
|
|
64
|
+
throw streamSeedError(ERROR_CODES.INVALID_MEDIA_CONTAINER, "mediaBlocks must be an array");
|
|
65
|
+
if (mediaBlocks.length === 0 || mediaBlocks.length > MAX_MEDIA_BLOCKS) {
|
|
66
|
+
throw streamSeedError(ERROR_CODES.INVALID_MEDIA_CONTAINER, `media block count ${mediaBlocks.length} is outside 1..${MAX_MEDIA_BLOCKS}`);
|
|
67
|
+
}
|
|
68
|
+
const snapshot = mediaBlocks.map((block) => copyBytes(block));
|
|
69
|
+
return this.serial(async () => {
|
|
70
|
+
this.checkOpen();
|
|
71
|
+
const node = await buildDataNode(this.fields(), snapshot, this.signer, signal);
|
|
72
|
+
this.commit(node);
|
|
73
|
+
return node;
|
|
74
|
+
}, signal);
|
|
75
|
+
}
|
|
76
|
+
/** 构造当前 ID 的 END;成功后 sealed。 */
|
|
77
|
+
async buildEnd(signal) {
|
|
78
|
+
return this.serial(async () => {
|
|
79
|
+
this.checkOpen();
|
|
80
|
+
const node = await buildEndNode(this.fields(), this.signer, signal);
|
|
81
|
+
this.sealed = true;
|
|
82
|
+
this.commit(node);
|
|
83
|
+
return node;
|
|
84
|
+
}, signal);
|
|
85
|
+
}
|
|
86
|
+
fields() {
|
|
87
|
+
const refs = this.recentRefs.slice(0, this.refLimit);
|
|
88
|
+
return {
|
|
89
|
+
streamId: newStreamID(this.streamId),
|
|
90
|
+
nodeId: this.next,
|
|
91
|
+
publisherPublicKey: this.signer.publicKey(),
|
|
92
|
+
refs: cloneRefs(refs)
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
checkOpen() {
|
|
96
|
+
if (this.sealed)
|
|
97
|
+
throw streamSeedError(ERROR_CODES.INVALID_NODE_SEQUENCE, "NodeBuilder is sealed");
|
|
98
|
+
if (this.exhausted)
|
|
99
|
+
throw streamSeedError(ERROR_CODES.NODE_ID_OVERFLOW, "node ID cannot advance");
|
|
100
|
+
}
|
|
101
|
+
commit(node) {
|
|
102
|
+
this.recentRefs = [{ nodeId: node.document.nodeId, seedHash: copyBytes(node.seedHash) }, ...this.recentRefs].slice(0, MAX_REFERENCES);
|
|
103
|
+
if (this.next === MAX_UINT64) {
|
|
104
|
+
this.exhausted = true;
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
this.next = nextNodeID(this.next);
|
|
108
|
+
}
|
|
109
|
+
async serial(operation, signal) {
|
|
110
|
+
// 任务本身挂在队列链上;调用方取消只能结束自己的等待,不能提前释放内部队列。
|
|
111
|
+
const run = this.queue.then(operation);
|
|
112
|
+
// 无论本任务成功还是失败,后续任务都必须等到本任务真正结束后再开始。
|
|
113
|
+
this.queue = run.then(() => undefined, () => undefined);
|
|
114
|
+
return await awaitWithAbort(run, signal);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
function cloneRefs(refs) {
|
|
118
|
+
return refs.map((ref) => ({ nodeId: ref.nodeId, seedHash: copyBytes(ref.seedHash) }));
|
|
119
|
+
}
|
|
120
|
+
function validateReferenceHistory(tailId, refs) {
|
|
121
|
+
if (refs.length === 0 || refs.length > MAX_REFERENCES || refs[0]?.nodeId !== tailId) {
|
|
122
|
+
throw streamSeedError(ERROR_CODES.INVALID_REFERENCE, `recent references do not start at tail node ${tailId}`);
|
|
123
|
+
}
|
|
124
|
+
let expected = tailId;
|
|
125
|
+
for (let index = 0; index < refs.length; index += 1) {
|
|
126
|
+
const ref = refs[index];
|
|
127
|
+
if (ref.nodeId !== expected)
|
|
128
|
+
throw streamSeedError(ERROR_CODES.INVALID_REFERENCE, `reference ${index} has node ID ${ref.nodeId}, expected ${expected}`);
|
|
129
|
+
if (expected === 0n) {
|
|
130
|
+
if (index !== refs.length - 1)
|
|
131
|
+
throw streamSeedError(ERROR_CODES.INVALID_REFERENCE, "references continue past node 0");
|
|
132
|
+
break;
|
|
133
|
+
}
|
|
134
|
+
expected -= 1n;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
//# sourceMappingURL=sequence.js.map
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { type Digest, type NodeID, type PublicKey, type Signature, type StreamID } from "../protocol/ids.js";
|
|
2
|
+
import type { WireKind } from "../protocol/constants.js";
|
|
3
|
+
/** 指向同一节目中一个已提交节点的完整文件 MasterSeed Seed Hash。 */
|
|
4
|
+
export interface NodeRef {
|
|
5
|
+
/** 被引用节点的序号。 */
|
|
6
|
+
nodeId: NodeID;
|
|
7
|
+
/** 被引用完整节点文件的 32 字节 MasterSeed Seed Hash。 */
|
|
8
|
+
seedHash: Digest;
|
|
9
|
+
}
|
|
10
|
+
/** 构造 Node 时的公共定位字段。 */
|
|
11
|
+
export interface NodeFields {
|
|
12
|
+
/** Node 所属的 stream ID。 */
|
|
13
|
+
streamId: StreamID;
|
|
14
|
+
/** Node 在 stream 中的 bigint 编号。 */
|
|
15
|
+
nodeId: NodeID;
|
|
16
|
+
/** 必须与 signer.publicKey() 相同。 */
|
|
17
|
+
publisherPublicKey: PublicKey;
|
|
18
|
+
/** 严格降序的紧邻前序引用;序号 0 必须为空。 */
|
|
19
|
+
refs: readonly NodeRef[];
|
|
20
|
+
}
|
|
21
|
+
/** DATA/END 共用的六元认证子文档。 */
|
|
22
|
+
export interface NodeDocument {
|
|
23
|
+
/** Node 所属的 stream ID。 */
|
|
24
|
+
streamId: StreamID;
|
|
25
|
+
/** 从 0 开始递增的 Node 编号。 */
|
|
26
|
+
nodeId: NodeID;
|
|
27
|
+
/** 签署此 Node 的 Ed25519 公钥。 */
|
|
28
|
+
publisherPublicKey: PublicKey;
|
|
29
|
+
/** exact media-container CBOR 的完整字节数;END 必须为 0。 */
|
|
30
|
+
payloadSize: bigint;
|
|
31
|
+
/** exact media-container CBOR 的 SHA-256;END 必须是 SHA-256(empty)。 */
|
|
32
|
+
payloadHash: Digest;
|
|
33
|
+
/** 最多 64 条严格降序的紧邻前序引用。 */
|
|
34
|
+
refs: NodeRef[];
|
|
35
|
+
}
|
|
36
|
+
/** typed signer 收到的结构化签名字段。 */
|
|
37
|
+
export interface NodeSigningFields {
|
|
38
|
+
/** 2=DATA,3=END,防止跨 Kind 换壳。 */
|
|
39
|
+
kind: Exclude<WireKind, 1>;
|
|
40
|
+
/** 待签名的认证子文档。 */
|
|
41
|
+
document: NodeDocument;
|
|
42
|
+
}
|
|
43
|
+
declare const DECODED_NODE_TOKEN: unique symbol;
|
|
44
|
+
/** 严格解析但尚未绑定目标 ProgramInfo 或签名信任的 Node;byte getter 返回副本。 */
|
|
45
|
+
export declare class DecodedNode {
|
|
46
|
+
#private;
|
|
47
|
+
/** @internal 只有 node codec 通过模块内 token 创建实例。 */
|
|
48
|
+
constructor(kind: Exclude<WireKind, 1>, document: NodeDocument, documentBytes: Uint8Array, signature: Signature, seedHash: Digest, mediaContainer: Uint8Array | undefined, mediaBlocks: Uint8Array[] | undefined, objectBytes: Uint8Array, token: typeof DECODED_NODE_TOKEN);
|
|
49
|
+
get kind(): Exclude<WireKind, 1>;
|
|
50
|
+
get document(): NodeDocument;
|
|
51
|
+
get documentBytes(): Uint8Array;
|
|
52
|
+
get signature(): Signature;
|
|
53
|
+
get seedHash(): Digest;
|
|
54
|
+
get mediaContainer(): Uint8Array | undefined;
|
|
55
|
+
get mediaBlocks(): Uint8Array[] | undefined;
|
|
56
|
+
/** 保留为 mediaContainer 的兼容名称。 */
|
|
57
|
+
get payload(): Uint8Array | undefined;
|
|
58
|
+
get objectBytes(): Uint8Array;
|
|
59
|
+
}
|
|
60
|
+
/** 构造成功且已对 signer 返回的签名做本地复验的 Node。 */
|
|
61
|
+
export type BuiltNode = DecodedNode;
|
|
62
|
+
/** @internal 供 node codec 使用;不从 SDK 根入口导出。 */
|
|
63
|
+
export declare function createDecodedNode(kind: Exclude<WireKind, 1>, document: NodeDocument, documentBytes: Uint8Array, signature: Signature, seedHash: Digest, mediaContainer: Uint8Array | undefined, mediaBlocks: Uint8Array[] | undefined, objectBytes: Uint8Array): DecodedNode;
|
|
64
|
+
declare const VERIFIED_NODE_TOKEN: unique symbol;
|
|
65
|
+
/** 已绑定期望 Seed Hash、序号、ProgramInfo 公钥并通过签名验证的 Node。 */
|
|
66
|
+
export declare class VerifiedNode {
|
|
67
|
+
#private;
|
|
68
|
+
/** @internal 只有 verifyNodeObject 通过模块内 token 创建实例。 */
|
|
69
|
+
constructor(kind: Exclude<WireKind, 1>, document: NodeDocument, documentBytes: Uint8Array, signature: Signature, seedHash: Digest, mediaContainer: Uint8Array | undefined, mediaBlocks: Uint8Array[] | undefined, objectBytes: Uint8Array, token: typeof VERIFIED_NODE_TOKEN);
|
|
70
|
+
get kind(): Exclude<WireKind, 1>;
|
|
71
|
+
get document(): NodeDocument;
|
|
72
|
+
get documentBytes(): Uint8Array;
|
|
73
|
+
get signature(): Signature;
|
|
74
|
+
get seedHash(): Digest;
|
|
75
|
+
get mediaContainer(): Uint8Array | undefined;
|
|
76
|
+
get mediaBlocks(): Uint8Array[] | undefined;
|
|
77
|
+
/** 保留为 mediaContainer 的兼容名称。 */
|
|
78
|
+
get payload(): Uint8Array | undefined;
|
|
79
|
+
get objectBytes(): Uint8Array;
|
|
80
|
+
}
|
|
81
|
+
/** @internal 供 node codec 使用;不从 SDK 根入口导出。 */
|
|
82
|
+
export declare function createVerifiedNode(kind: Exclude<WireKind, 1>, document: NodeDocument, documentBytes: Uint8Array, signature: Signature, seedHash: Digest, mediaContainer: Uint8Array | undefined, mediaBlocks: Uint8Array[] | undefined, objectBytes: Uint8Array): VerifiedNode;
|
|
83
|
+
/** 判断值是否为 SDK verifyNodeObject 生成的实例。 */
|
|
84
|
+
export declare function isVerifiedNode(value: unknown): value is VerifiedNode;
|
|
85
|
+
/** NodeSigner 是可由本地私钥、Worker、Vault 或 HSM 实现的异步窄接口。 */
|
|
86
|
+
export interface NodeSigner {
|
|
87
|
+
/** 返回 signer 公钥的独立副本。 */
|
|
88
|
+
publicKey(): PublicKey;
|
|
89
|
+
/** 根据结构化字段签署唯一的 V1 域分离消息。 */
|
|
90
|
+
signNode(fields: NodeSigningFields, signal?: AbortSignal): Promise<Signature>;
|
|
91
|
+
}
|
|
92
|
+
export {};
|
|
93
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { cloneMediaBlocks } from "./media.js";
|
|
2
|
+
import { copyBytes } from "../protocol/ids.js";
|
|
3
|
+
const DECODED_NODE_TOKEN = Symbol("streamseed.decoded-node");
|
|
4
|
+
/** 严格解析但尚未绑定目标 ProgramInfo 或签名信任的 Node;byte getter 返回副本。 */
|
|
5
|
+
export class DecodedNode {
|
|
6
|
+
#storedKind;
|
|
7
|
+
#storedDocument;
|
|
8
|
+
#storedDocumentBytes;
|
|
9
|
+
#storedSignature;
|
|
10
|
+
#storedSeedHash;
|
|
11
|
+
#storedMediaContainer;
|
|
12
|
+
#storedMediaBlocks;
|
|
13
|
+
#storedObjectBytes;
|
|
14
|
+
/** @internal 只有 node codec 通过模块内 token 创建实例。 */
|
|
15
|
+
constructor(kind, document, documentBytes, signature, seedHash, mediaContainer, mediaBlocks, objectBytes, token) {
|
|
16
|
+
if (token !== DECODED_NODE_TOKEN)
|
|
17
|
+
throw new TypeError("DecodedNode must be created by node codec");
|
|
18
|
+
this.#storedKind = kind;
|
|
19
|
+
this.#storedDocument = cloneDocument(document);
|
|
20
|
+
this.#storedDocumentBytes = copyBytes(documentBytes);
|
|
21
|
+
this.#storedSignature = copyBytes(signature);
|
|
22
|
+
this.#storedSeedHash = copyBytes(seedHash);
|
|
23
|
+
this.#storedMediaContainer = mediaContainer === undefined ? undefined : copyBytes(mediaContainer);
|
|
24
|
+
this.#storedMediaBlocks = cloneMediaBlocks(mediaBlocks);
|
|
25
|
+
this.#storedObjectBytes = copyBytes(objectBytes);
|
|
26
|
+
Object.freeze(this);
|
|
27
|
+
}
|
|
28
|
+
get kind() { return this.#storedKind; }
|
|
29
|
+
get document() { return cloneDocument(this.#storedDocument); }
|
|
30
|
+
get documentBytes() { return copyBytes(this.#storedDocumentBytes); }
|
|
31
|
+
get signature() { return copyBytes(this.#storedSignature); }
|
|
32
|
+
get seedHash() { return copyBytes(this.#storedSeedHash); }
|
|
33
|
+
get mediaContainer() {
|
|
34
|
+
return this.#storedMediaContainer === undefined ? undefined : copyBytes(this.#storedMediaContainer);
|
|
35
|
+
}
|
|
36
|
+
get mediaBlocks() { return cloneMediaBlocks(this.#storedMediaBlocks); }
|
|
37
|
+
/** 保留为 mediaContainer 的兼容名称。 */
|
|
38
|
+
get payload() { return this.mediaContainer; }
|
|
39
|
+
get objectBytes() { return copyBytes(this.#storedObjectBytes); }
|
|
40
|
+
}
|
|
41
|
+
Object.freeze(DecodedNode.prototype);
|
|
42
|
+
/** @internal 供 node codec 使用;不从 SDK 根入口导出。 */
|
|
43
|
+
export function createDecodedNode(kind, document, documentBytes, signature, seedHash, mediaContainer, mediaBlocks, objectBytes) {
|
|
44
|
+
return new DecodedNode(kind, document, documentBytes, signature, seedHash, mediaContainer, mediaBlocks, objectBytes, DECODED_NODE_TOKEN);
|
|
45
|
+
}
|
|
46
|
+
const VERIFIED_NODE_TOKEN = Symbol("streamseed.verified-node");
|
|
47
|
+
const verifiedNodeInstances = new WeakSet();
|
|
48
|
+
/** 已绑定期望 Seed Hash、序号、ProgramInfo 公钥并通过签名验证的 Node。 */
|
|
49
|
+
export class VerifiedNode {
|
|
50
|
+
#storedKind;
|
|
51
|
+
#storedDocument;
|
|
52
|
+
#storedDocumentBytes;
|
|
53
|
+
#storedSignature;
|
|
54
|
+
#storedSeedHash;
|
|
55
|
+
#storedMediaContainer;
|
|
56
|
+
#storedMediaBlocks;
|
|
57
|
+
#storedObjectBytes;
|
|
58
|
+
/** @internal 只有 verifyNodeObject 通过模块内 token 创建实例。 */
|
|
59
|
+
constructor(kind, document, documentBytes, signature, seedHash, mediaContainer, mediaBlocks, objectBytes, token) {
|
|
60
|
+
if (token !== VERIFIED_NODE_TOKEN)
|
|
61
|
+
throw new TypeError("VerifiedNode must be created by verifyNodeObject");
|
|
62
|
+
this.#storedKind = kind;
|
|
63
|
+
this.#storedDocument = cloneDocument(document);
|
|
64
|
+
this.#storedDocumentBytes = copyBytes(documentBytes);
|
|
65
|
+
this.#storedSignature = copyBytes(signature);
|
|
66
|
+
this.#storedSeedHash = copyBytes(seedHash);
|
|
67
|
+
this.#storedMediaContainer = mediaContainer === undefined ? undefined : copyBytes(mediaContainer);
|
|
68
|
+
this.#storedMediaBlocks = cloneMediaBlocks(mediaBlocks);
|
|
69
|
+
this.#storedObjectBytes = copyBytes(objectBytes);
|
|
70
|
+
verifiedNodeInstances.add(this);
|
|
71
|
+
Object.freeze(this);
|
|
72
|
+
}
|
|
73
|
+
get kind() { return this.#storedKind; }
|
|
74
|
+
get document() { return cloneDocument(this.#storedDocument); }
|
|
75
|
+
get documentBytes() { return copyBytes(this.#storedDocumentBytes); }
|
|
76
|
+
get signature() { return copyBytes(this.#storedSignature); }
|
|
77
|
+
get seedHash() { return copyBytes(this.#storedSeedHash); }
|
|
78
|
+
get mediaContainer() {
|
|
79
|
+
return this.#storedMediaContainer === undefined ? undefined : copyBytes(this.#storedMediaContainer);
|
|
80
|
+
}
|
|
81
|
+
get mediaBlocks() { return cloneMediaBlocks(this.#storedMediaBlocks); }
|
|
82
|
+
/** 保留为 mediaContainer 的兼容名称。 */
|
|
83
|
+
get payload() { return this.mediaContainer; }
|
|
84
|
+
get objectBytes() { return copyBytes(this.#storedObjectBytes); }
|
|
85
|
+
}
|
|
86
|
+
Object.freeze(VerifiedNode.prototype);
|
|
87
|
+
/** @internal 供 node codec 使用;不从 SDK 根入口导出。 */
|
|
88
|
+
export function createVerifiedNode(kind, document, documentBytes, signature, seedHash, mediaContainer, mediaBlocks, objectBytes) {
|
|
89
|
+
return new VerifiedNode(kind, document, documentBytes, signature, seedHash, mediaContainer, mediaBlocks, objectBytes, VERIFIED_NODE_TOKEN);
|
|
90
|
+
}
|
|
91
|
+
/** 判断值是否为 SDK verifyNodeObject 生成的实例。 */
|
|
92
|
+
export function isVerifiedNode(value) {
|
|
93
|
+
return typeof value === "object" && value !== null && verifiedNodeInstances.has(value);
|
|
94
|
+
}
|
|
95
|
+
function cloneDocument(document) {
|
|
96
|
+
return {
|
|
97
|
+
streamId: copyBytes(document.streamId),
|
|
98
|
+
nodeId: document.nodeId,
|
|
99
|
+
publisherPublicKey: copyBytes(document.publisherPublicKey),
|
|
100
|
+
payloadSize: document.payloadSize,
|
|
101
|
+
payloadHash: copyBytes(document.payloadHash),
|
|
102
|
+
refs: document.refs.map((ref) => ({ nodeId: ref.nodeId, seedHash: copyBytes(ref.seedHash) }))
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type DecodedProgram, type ProgramInfo, type ProgramObject, type VerifiedProgram } from "./types.js";
|
|
2
|
+
/** 规范化并编码 ProgramInfo,同时计算 MasterSeed stream ID。 */
|
|
3
|
+
export declare function buildProgramObject(info: ProgramInfo): Promise<ProgramObject>;
|
|
4
|
+
/** 编码 ProgramInfo 的唯一 V1 deterministic-CBOR。 */
|
|
5
|
+
export declare function encodeProgramInfo(info: ProgramInfo): Uint8Array;
|
|
6
|
+
/** 只做严格结构解析,不建立 stream ID 或发布者信任。 */
|
|
7
|
+
export declare function decodeProgramObject(data: Uint8Array): DecodedProgram;
|
|
8
|
+
/** 先验证完整对象 MasterSeed Seed Hash,再严格解析并返回 VerifiedProgram。 */
|
|
9
|
+
export declare function verifyProgramObject(expectedStreamId: Uint8Array, data: Uint8Array, signal?: AbortSignal): Promise<VerifiedProgram>;
|
|
10
|
+
//# sourceMappingURL=codec.d.ts.map
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { KIND_PROGRAM_INFO, MAX_COVER_BYTES, MAX_DESCRIPTION_BYTES, MAX_NAME_BYTES, MAX_PROGRAM_OBJECT_SIZE, PROGRAM_INFO_ARRAY_LENGTH, WIRE_VERSION } from "../protocol/constants.js";
|
|
2
|
+
import { ERROR_CODES, streamSeedError } from "../protocol/errors.js";
|
|
3
|
+
import { bytesEqual, newChannelPublicKey, newPublicKey, newReleaseSalt, newStreamID, copyBytes } from "../protocol/ids.js";
|
|
4
|
+
import { masterSeedHash } from "../protocol/hash.js";
|
|
5
|
+
import { CBORDecoder, CBOREncoder } from "../internal/cbor-codec.js";
|
|
6
|
+
import { isValidPublicKey } from "../internal/ed25519.js";
|
|
7
|
+
import { cloneProgramInfo, createDecodedProgram, createProgramObject, createVerifiedProgram } from "./types.js";
|
|
8
|
+
const encoder = new TextEncoder();
|
|
9
|
+
/** 规范化并编码 ProgramInfo,同时计算 MasterSeed stream ID。 */
|
|
10
|
+
export async function buildProgramObject(info) {
|
|
11
|
+
const normalized = normalizeForBuild(info);
|
|
12
|
+
const bytes = encodeNormalizedProgramInfo(normalized);
|
|
13
|
+
return createProgramObject(cloneProgramInfo(normalized), newStreamID(await masterSeedHash(bytes)), bytes);
|
|
14
|
+
}
|
|
15
|
+
/** 编码 ProgramInfo 的唯一 V1 deterministic-CBOR。 */
|
|
16
|
+
export function encodeProgramInfo(info) {
|
|
17
|
+
return encodeNormalizedProgramInfo(normalizeForBuild(info));
|
|
18
|
+
}
|
|
19
|
+
/** 只做严格结构解析,不建立 stream ID 或发布者信任。 */
|
|
20
|
+
export function decodeProgramObject(data) {
|
|
21
|
+
if (data.length > MAX_PROGRAM_OBJECT_SIZE) {
|
|
22
|
+
throw streamSeedError(ERROR_CODES.FIELD_TOO_LARGE, `program object exceeds ${MAX_PROGRAM_OBJECT_SIZE} bytes`);
|
|
23
|
+
}
|
|
24
|
+
const info = decodeProgramInfo(data);
|
|
25
|
+
return createDecodedProgram(cloneProgramInfo(info), data);
|
|
26
|
+
}
|
|
27
|
+
/** 先验证完整对象 MasterSeed Seed Hash,再严格解析并返回 VerifiedProgram。 */
|
|
28
|
+
export async function verifyProgramObject(expectedStreamId, data, signal) {
|
|
29
|
+
if (data.length > MAX_PROGRAM_OBJECT_SIZE) {
|
|
30
|
+
throw streamSeedError(ERROR_CODES.FIELD_TOO_LARGE, `program object exceeds ${MAX_PROGRAM_OBJECT_SIZE} bytes`);
|
|
31
|
+
}
|
|
32
|
+
const snapshot = copyBytes(data);
|
|
33
|
+
const expected = newStreamID(expectedStreamId);
|
|
34
|
+
if (!bytesEqual(await masterSeedHash(snapshot, signal), expected)) {
|
|
35
|
+
throw streamSeedError(ERROR_CODES.STREAM_ID_MISMATCH, "program object MasterSeed hash does not match expected stream ID");
|
|
36
|
+
}
|
|
37
|
+
const decoded = decodeProgramObject(snapshot);
|
|
38
|
+
return createVerifiedProgram(decoded.info, expected, snapshot);
|
|
39
|
+
}
|
|
40
|
+
function encodeNormalizedProgramInfo(info) {
|
|
41
|
+
const output = new CBOREncoder();
|
|
42
|
+
output.array(PROGRAM_INFO_ARRAY_LENGTH);
|
|
43
|
+
output.uint(BigInt(WIRE_VERSION));
|
|
44
|
+
output.uint(BigInt(KIND_PROGRAM_INFO));
|
|
45
|
+
output.text(info.name);
|
|
46
|
+
output.uint(info.publishedAtMs);
|
|
47
|
+
output.bytes(info.nodePublisherKey);
|
|
48
|
+
output.text(info.description);
|
|
49
|
+
output.text(info.coverMediaType);
|
|
50
|
+
output.bytes(info.coverBytes);
|
|
51
|
+
output.bytes(info.releaseSalt);
|
|
52
|
+
output.bytes(info.channelPublisherKey);
|
|
53
|
+
return output.finish();
|
|
54
|
+
}
|
|
55
|
+
function decodeProgramInfo(data) {
|
|
56
|
+
const input = new CBORDecoder(data, BigInt(MAX_PROGRAM_OBJECT_SIZE));
|
|
57
|
+
if (input.array() !== PROGRAM_INFO_ARRAY_LENGTH) {
|
|
58
|
+
throw streamSeedError(ERROR_CODES.INVALID_LENGTH, "ProgramInfo array must contain exactly 10 items");
|
|
59
|
+
}
|
|
60
|
+
if (input.uint() !== BigInt(WIRE_VERSION)) {
|
|
61
|
+
throw streamSeedError(ERROR_CODES.UNSUPPORTED_VERSION, "unsupported ProgramInfo wire version");
|
|
62
|
+
}
|
|
63
|
+
if (input.uint() !== BigInt(KIND_PROGRAM_INFO)) {
|
|
64
|
+
throw streamSeedError(ERROR_CODES.UNSUPPORTED_KIND, "unsupported ProgramInfo kind");
|
|
65
|
+
}
|
|
66
|
+
const name = input.text(MAX_NAME_BYTES);
|
|
67
|
+
const publishedAtMs = input.uint();
|
|
68
|
+
const nodeKeyBytes = input.bytes(32);
|
|
69
|
+
if (nodeKeyBytes.length !== 32 || !isValidPublicKey(nodeKeyBytes)) {
|
|
70
|
+
throw streamSeedError(ERROR_CODES.INVALID_PUBLIC_KEY, "ProgramInfo node public key is not a strict Ed25519 key");
|
|
71
|
+
}
|
|
72
|
+
const description = input.text(MAX_DESCRIPTION_BYTES);
|
|
73
|
+
const coverMediaType = input.text(64);
|
|
74
|
+
const coverBytes = input.bytes(MAX_COVER_BYTES);
|
|
75
|
+
const saltBytes = input.bytes(32);
|
|
76
|
+
if (saltBytes.length !== 32 || saltBytes.every((byte) => byte === 0)) {
|
|
77
|
+
throw streamSeedError(ERROR_CODES.INVALID_LENGTH, "release salt must be 32 non-zero bytes");
|
|
78
|
+
}
|
|
79
|
+
const channelKeyBytes = input.bytes(33);
|
|
80
|
+
const channelPublisherKey = newChannelPublicKey(channelKeyBytes);
|
|
81
|
+
input.requireEOF();
|
|
82
|
+
const info = {
|
|
83
|
+
name,
|
|
84
|
+
publishedAtMs,
|
|
85
|
+
nodePublisherKey: newPublicKey(nodeKeyBytes),
|
|
86
|
+
description,
|
|
87
|
+
coverMediaType,
|
|
88
|
+
coverBytes,
|
|
89
|
+
releaseSalt: newReleaseSalt(saltBytes),
|
|
90
|
+
channelPublisherKey
|
|
91
|
+
};
|
|
92
|
+
validateCanonicalText(info.name, MAX_NAME_BYTES, false);
|
|
93
|
+
validateCanonicalText(info.description, MAX_DESCRIPTION_BYTES, true);
|
|
94
|
+
validateCoverMediaType(info.coverMediaType);
|
|
95
|
+
if (!bytesEqual(encodeNormalizedProgramInfo(info), data)) {
|
|
96
|
+
throw streamSeedError(ERROR_CODES.NON_CANONICAL_CBOR, "ProgramInfo does not re-encode byte-for-byte");
|
|
97
|
+
}
|
|
98
|
+
return info;
|
|
99
|
+
}
|
|
100
|
+
function normalizeForBuild(info) {
|
|
101
|
+
const name = normalizeText(info.name, MAX_NAME_BYTES, false);
|
|
102
|
+
const description = normalizeText(info.description, MAX_DESCRIPTION_BYTES, true);
|
|
103
|
+
if (!isValidPublicKey(info.nodePublisherKey)) {
|
|
104
|
+
throw streamSeedError(ERROR_CODES.INVALID_PUBLIC_KEY, "ProgramInfo node public key is not a strict Ed25519 key");
|
|
105
|
+
}
|
|
106
|
+
validateCoverMediaType(info.coverMediaType);
|
|
107
|
+
if (!(info.coverBytes instanceof Uint8Array))
|
|
108
|
+
throw streamSeedError(ERROR_CODES.INVALID_LENGTH, "coverBytes must be Uint8Array");
|
|
109
|
+
if (info.coverBytes.length > MAX_COVER_BYTES) {
|
|
110
|
+
throw streamSeedError(ERROR_CODES.FIELD_TOO_LARGE, `cover exceeds ${MAX_COVER_BYTES} bytes`);
|
|
111
|
+
}
|
|
112
|
+
const releaseSalt = newReleaseSalt(info.releaseSalt);
|
|
113
|
+
if (typeof info.publishedAtMs !== "bigint" || info.publishedAtMs < 0n || info.publishedAtMs > 0xffffffffffffffffn) {
|
|
114
|
+
throw streamSeedError(ERROR_CODES.INVALID_LENGTH, "publishedAtMs must be bigint in uint64 range");
|
|
115
|
+
}
|
|
116
|
+
return {
|
|
117
|
+
name,
|
|
118
|
+
publishedAtMs: info.publishedAtMs,
|
|
119
|
+
nodePublisherKey: newPublicKey(info.nodePublisherKey),
|
|
120
|
+
description,
|
|
121
|
+
coverMediaType: info.coverMediaType,
|
|
122
|
+
coverBytes: copyBytes(info.coverBytes),
|
|
123
|
+
releaseSalt,
|
|
124
|
+
channelPublisherKey: newChannelPublicKey(info.channelPublisherKey)
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
function validateCoverMediaType(value) {
|
|
128
|
+
if (value !== "image/jpeg" && value !== "image/png" && value !== "image/webp") {
|
|
129
|
+
throw streamSeedError(ERROR_CODES.UNSUPPORTED_MEDIA_TYPE, `unsupported cover media type ${value}`);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
function normalizeText(value, limit, allowEmpty) {
|
|
133
|
+
if (typeof value !== "string" || hasUnpairedSurrogate(value)) {
|
|
134
|
+
throw streamSeedError(ERROR_CODES.INVALID_UTF8, "text is not valid UTF-8");
|
|
135
|
+
}
|
|
136
|
+
const normalized = value.normalize("NFC");
|
|
137
|
+
validateCanonicalText(normalized, limit, allowEmpty);
|
|
138
|
+
return normalized;
|
|
139
|
+
}
|
|
140
|
+
function validateCanonicalText(value, limit, allowEmpty) {
|
|
141
|
+
if (hasUnpairedSurrogate(value))
|
|
142
|
+
throw streamSeedError(ERROR_CODES.INVALID_UTF8, "text is not valid UTF-8");
|
|
143
|
+
if (value.normalize("NFC") !== value)
|
|
144
|
+
throw streamSeedError(ERROR_CODES.NON_CANONICAL_TEXT, "text is not Unicode NFC");
|
|
145
|
+
const length = encoder.encode(value).length;
|
|
146
|
+
if ((!allowEmpty && length === 0) || length > limit) {
|
|
147
|
+
throw streamSeedError(ERROR_CODES.INVALID_LENGTH, `text UTF-8 length ${length} is outside allowed range`);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
function hasUnpairedSurrogate(value) {
|
|
151
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
152
|
+
const code = value.charCodeAt(index);
|
|
153
|
+
if (code >= 0xd800 && code <= 0xdbff) {
|
|
154
|
+
const next = value.charCodeAt(index + 1);
|
|
155
|
+
if (next < 0xdc00 || next > 0xdfff)
|
|
156
|
+
return true;
|
|
157
|
+
index += 1;
|
|
158
|
+
}
|
|
159
|
+
else if (code >= 0xdc00 && code <= 0xdfff) {
|
|
160
|
+
return true;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return false;
|
|
164
|
+
}
|
|
165
|
+
//# sourceMappingURL=codec.js.map
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type Digest } from "../protocol/ids.js";
|
|
2
|
+
import type { VerifiedProgram } from "./types.js";
|
|
3
|
+
/** 调用方提供的按 32 字节摘要获取 exact object bytes 的抽象。 */
|
|
4
|
+
export type GetByHash = (hash: Digest, signal?: AbortSignal) => Promise<Uint8Array>;
|
|
5
|
+
/** 可被 SDK 限制读取量的异步字节流。 */
|
|
6
|
+
export type ByteStream = AsyncIterable<Uint8Array> | ReadableStream<Uint8Array>;
|
|
7
|
+
/** 流式内容寻址获取函数;SDK 会在读取阶段执行 maxBytes 限制。 */
|
|
8
|
+
export type GetByHashReader = (hash: Digest, signal?: AbortSignal) => Promise<ByteStream>;
|
|
9
|
+
/** 获取并验证 ProgramInfo;普通 []byte getter 使用协议默认上限。 */
|
|
10
|
+
export declare function resolveProgram(streamId: Uint8Array, getter: GetByHash, signal?: AbortSignal): Promise<VerifiedProgram>;
|
|
11
|
+
/**
|
|
12
|
+
* 为 []byte getter 增加调用方大小上限。
|
|
13
|
+
*
|
|
14
|
+
* 该上限保护 SDK 的 hash/解析阶段;若要限制供应商读取阶段的内存,请使用
|
|
15
|
+
* resolveProgramReader。供应商在 GetByHash 回调内部如何分配内存不受此函数控制。
|
|
16
|
+
*/
|
|
17
|
+
export declare function resolveProgramWithMaxBytes(streamId: Uint8Array, getter: GetByHash, maxBytes: number, signal?: AbortSignal): Promise<VerifiedProgram>;
|
|
18
|
+
/**
|
|
19
|
+
* 流式获取并验证 ProgramInfo。
|
|
20
|
+
*
|
|
21
|
+
* 内部最多保留 maxBytes+1 字节;读到超限时立即取消 ReadableStream,或停止消费
|
|
22
|
+
* AsyncIterable,避免超大对象被完整读入内存。
|
|
23
|
+
*/
|
|
24
|
+
export declare function resolveProgramReader(streamId: Uint8Array, getter: GetByHashReader, maxBytes?: number, signal?: AbortSignal): Promise<VerifiedProgram>;
|
|
25
|
+
//# sourceMappingURL=resolver.d.ts.map
|