streamseed 0.1.0 → 0.2.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/README.md +31 -2
- package/dist/manifest/cbor.d.ts +54 -0
- package/dist/manifest/cbor.js +306 -0
- package/dist/manifest/codec.d.ts +6 -0
- package/dist/manifest/codec.js +360 -0
- package/dist/manifest/constants.d.ts +2 -0
- package/dist/manifest/constants.js +2 -0
- package/dist/manifest/errors.d.ts +20 -0
- package/dist/manifest/errors.js +23 -0
- package/dist/manifest/index.d.ts +6 -0
- package/dist/manifest/index.js +6 -0
- package/dist/manifest/language.d.ts +4 -0
- package/dist/manifest/language.js +26 -0
- package/dist/manifest/types.d.ts +73 -0
- package/dist/manifest/types.js +2 -0
- package/package.json +20 -7
package/README.md
CHANGED
|
@@ -78,12 +78,41 @@ DATA 的 `mediaContainer` 是 exact `[bstr, ...]` CBOR,`mediaBlocks` 返回其
|
|
|
78
78
|
|
|
79
79
|
错误应读取 `StreamSeedError.code`。常见代码包括 `SEED_HASH_MISMATCH`、`STREAM_ID_MISMATCH`、`NODE_ID_MISMATCH`、`INVALID_REFERENCE`、`INVALID_MEDIA_CONTAINER`、`PAYLOAD_HASH_MISMATCH`、`SIGNATURE_MISMATCH`、`FIELD_TOO_LARGE` 和 `INVALID_VERIFIED_PROGRAM`。
|
|
80
80
|
|
|
81
|
+
## 发布节目单
|
|
82
|
+
|
|
83
|
+
静态与直播节目单从独立子路径导入,不与旧 `ProgramInfo` 入口混淆:
|
|
84
|
+
|
|
85
|
+
```ts
|
|
86
|
+
import { encodeProgramManifest, decodeProgramManifest, normalizeLanguageTag } from "streamseed/manifest";
|
|
87
|
+
import type { LiveProgramManifest } from "streamseed/manifest";
|
|
88
|
+
|
|
89
|
+
declare const coverBytes: Uint8Array;
|
|
90
|
+
const manifest: LiveProgramManifest = {
|
|
91
|
+
schema: "bsv8.program-manifest",
|
|
92
|
+
version: 1,
|
|
93
|
+
kind: "live",
|
|
94
|
+
title: "城市夜景直播",
|
|
95
|
+
description: "",
|
|
96
|
+
cover: { mediaType: "image/png", data: coverBytes },
|
|
97
|
+
source: { transport: "streamseed", sessionId: "live_city_walk_01" },
|
|
98
|
+
tracks: {
|
|
99
|
+
video: [{ id: "main", width: 1920, height: 1080 }],
|
|
100
|
+
audio: [{ id: "ja-main", language: "ja" }],
|
|
101
|
+
subtitles: [{ id: "zh-sub", language: normalizeLanguageTag("ZH-hans-cn") }]
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
const bytes = encodeProgramManifest(manifest);
|
|
105
|
+
const decoded = decodeProgramManifest(bytes);
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
`encodeProgramManifest` 和 `decodeProgramManifest` 是同步 API,输入输出使用 `Uint8Array`,封面不设固定大小上限。解码错误使用 `ProgramManifestError.code` 和 `path`;`path` 是 RFC 6901 JSON Pointer。
|
|
109
|
+
|
|
81
110
|
## 本地开发
|
|
82
111
|
|
|
83
112
|
```sh
|
|
84
113
|
pnpm install --frozen-lockfile
|
|
85
|
-
pnpm run
|
|
114
|
+
pnpm run build
|
|
86
115
|
pnpm run pack:check
|
|
87
116
|
```
|
|
88
117
|
|
|
89
|
-
|
|
118
|
+
生产包不提供本地私钥 signer。生产环境应实现 typed `NodeSigner` 并接入 Web Crypto、Vault、HSM 或远程签名服务;可运行 `examples/typescript-basic` 查看最小接入示例。
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
export type CborValue = {
|
|
2
|
+
kind: "uint";
|
|
3
|
+
value: bigint;
|
|
4
|
+
} | {
|
|
5
|
+
kind: "negative";
|
|
6
|
+
value: bigint;
|
|
7
|
+
} | {
|
|
8
|
+
kind: "bytes";
|
|
9
|
+
value: Uint8Array;
|
|
10
|
+
} | {
|
|
11
|
+
kind: "text";
|
|
12
|
+
value: string;
|
|
13
|
+
} | {
|
|
14
|
+
kind: "array";
|
|
15
|
+
value: CborValue[];
|
|
16
|
+
indefinite: boolean;
|
|
17
|
+
declared: bigint;
|
|
18
|
+
} | {
|
|
19
|
+
kind: "map";
|
|
20
|
+
value: CborMap;
|
|
21
|
+
indefinite: boolean;
|
|
22
|
+
declared: bigint;
|
|
23
|
+
} | {
|
|
24
|
+
kind: "other";
|
|
25
|
+
};
|
|
26
|
+
export interface CborMapEntry {
|
|
27
|
+
key: string;
|
|
28
|
+
keyBytes: Uint8Array;
|
|
29
|
+
value: CborValue;
|
|
30
|
+
}
|
|
31
|
+
export interface CborMap {
|
|
32
|
+
entries: CborMapEntry[];
|
|
33
|
+
index: Map<string, number>;
|
|
34
|
+
}
|
|
35
|
+
export declare function pointerChild(path: string, segment: string): string;
|
|
36
|
+
export declare function parseCBORDocument(bytes: Uint8Array): {
|
|
37
|
+
root: CborValue;
|
|
38
|
+
nonCanonical: boolean;
|
|
39
|
+
};
|
|
40
|
+
export declare function canonicalCBOR(value: CborValue): Uint8Array;
|
|
41
|
+
export declare function cborMapValue(object: Map<string, CborValue>): Extract<CborValue, {
|
|
42
|
+
kind: "map";
|
|
43
|
+
}>;
|
|
44
|
+
export declare function cborTextValue(value: string): Extract<CborValue, {
|
|
45
|
+
kind: "text";
|
|
46
|
+
}>;
|
|
47
|
+
export declare function cborUintValue(value: bigint): Extract<CborValue, {
|
|
48
|
+
kind: "uint";
|
|
49
|
+
}>;
|
|
50
|
+
export declare function cborModelInteger(value: number): CborValue;
|
|
51
|
+
export declare function cborBytesValue(value: Uint8Array): Extract<CborValue, {
|
|
52
|
+
kind: "bytes";
|
|
53
|
+
}>;
|
|
54
|
+
//# sourceMappingURL=cbor.d.ts.map
|
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
import { PROGRAM_MANIFEST_ERROR_CODES, ProgramManifestError } from "./errors.js";
|
|
2
|
+
function childPath(parent, segment) {
|
|
3
|
+
return { parent, segment };
|
|
4
|
+
}
|
|
5
|
+
function renderPath(node) {
|
|
6
|
+
const segments = [];
|
|
7
|
+
for (let current = node; current !== undefined; current = current.parent)
|
|
8
|
+
segments.push(current.segment);
|
|
9
|
+
return segments.reverse().map((segment) => `/${segment.replaceAll("~", "~0").replaceAll("/", "~1")}`).join("");
|
|
10
|
+
}
|
|
11
|
+
export function pointerChild(path, segment) {
|
|
12
|
+
return `${path}/${segment.replaceAll("~", "~0").replaceAll("/", "~1")}`;
|
|
13
|
+
}
|
|
14
|
+
function newFrame(value, path) {
|
|
15
|
+
return {
|
|
16
|
+
value, path, state: 0, indefinite: value.indefinite, remaining: value.declared,
|
|
17
|
+
pendingKey: "", pendingKeyBytes: new Uint8Array(), previousKeyBytes: undefined, index: 0
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
class CborParser {
|
|
21
|
+
data;
|
|
22
|
+
offset = 0;
|
|
23
|
+
nonCanonical = false;
|
|
24
|
+
constructor(data) {
|
|
25
|
+
this.data = data;
|
|
26
|
+
}
|
|
27
|
+
parseDocument() {
|
|
28
|
+
if (this.data.length === 0)
|
|
29
|
+
this.fail("", "empty CBOR document");
|
|
30
|
+
const root = this.readValue(undefined);
|
|
31
|
+
const stack = [];
|
|
32
|
+
if (root.kind === "array" || root.kind === "map")
|
|
33
|
+
stack.push(newFrame(root, undefined));
|
|
34
|
+
while (stack.length > 0) {
|
|
35
|
+
const top = stack[stack.length - 1];
|
|
36
|
+
if (top.value.kind === "array") {
|
|
37
|
+
if (top.indefinite) {
|
|
38
|
+
if (this.takeBreak()) {
|
|
39
|
+
stack.pop();
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
else if (top.remaining === 0n) {
|
|
44
|
+
stack.pop();
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
const path = childPath(top.path, String(top.index));
|
|
48
|
+
const child = this.readValue(path);
|
|
49
|
+
top.value.value.push(child);
|
|
50
|
+
top.index += 1;
|
|
51
|
+
if (!top.indefinite)
|
|
52
|
+
top.remaining -= 1n;
|
|
53
|
+
if (child.kind === "array" || child.kind === "map")
|
|
54
|
+
stack.push(newFrame(child, path));
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
if (top.state === 0) {
|
|
58
|
+
if (top.indefinite) {
|
|
59
|
+
if (this.takeBreak()) {
|
|
60
|
+
stack.pop();
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
else if (top.remaining === 0n) {
|
|
65
|
+
stack.pop();
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
const keyValue = this.readValue(top.path);
|
|
69
|
+
if (keyValue.kind !== "text")
|
|
70
|
+
this.fail(renderPath(top.path), "CBOR map keys must be text strings");
|
|
71
|
+
const keyBytes = canonicalCBOR(keyValue);
|
|
72
|
+
const keyPath = childPath(top.path, keyValue.value);
|
|
73
|
+
if (top.value.value.index.has(keyValue.value))
|
|
74
|
+
this.fail(renderPath(keyPath), "duplicate CBOR map key");
|
|
75
|
+
if (top.previousKeyBytes !== undefined && bytesCompare(top.previousKeyBytes, keyBytes) > 0)
|
|
76
|
+
this.nonCanonical = true;
|
|
77
|
+
top.pendingKey = keyValue.value;
|
|
78
|
+
top.pendingKeyBytes = keyBytes;
|
|
79
|
+
top.previousKeyBytes = keyBytes;
|
|
80
|
+
top.state = 1;
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
const path = childPath(top.path, top.pendingKey);
|
|
84
|
+
const child = this.readValue(path);
|
|
85
|
+
top.value.value.index.set(top.pendingKey, top.value.value.entries.length);
|
|
86
|
+
top.value.value.entries.push({ key: top.pendingKey, keyBytes: top.pendingKeyBytes, value: child });
|
|
87
|
+
top.pendingKey = "";
|
|
88
|
+
top.pendingKeyBytes = new Uint8Array();
|
|
89
|
+
top.state = 0;
|
|
90
|
+
if (!top.indefinite)
|
|
91
|
+
top.remaining -= 1n;
|
|
92
|
+
if (child.kind === "array" || child.kind === "map")
|
|
93
|
+
stack.push(newFrame(child, path));
|
|
94
|
+
}
|
|
95
|
+
if (this.offset !== this.data.length)
|
|
96
|
+
this.fail("", "trailing bytes after root CBOR value");
|
|
97
|
+
return { root, nonCanonical: this.nonCanonical };
|
|
98
|
+
}
|
|
99
|
+
readValue(path) {
|
|
100
|
+
const { major, argument, indefinite } = this.readHead(path);
|
|
101
|
+
switch (major) {
|
|
102
|
+
case 0: return { kind: "uint", value: argument };
|
|
103
|
+
case 1: return { kind: "negative", value: argument };
|
|
104
|
+
case 2:
|
|
105
|
+
if (indefinite) {
|
|
106
|
+
this.nonCanonical = true;
|
|
107
|
+
return { kind: "bytes", value: this.readIndefiniteString(path, false) };
|
|
108
|
+
}
|
|
109
|
+
return { kind: "bytes", value: this.readStringBytes(argument, path) };
|
|
110
|
+
case 3:
|
|
111
|
+
if (indefinite) {
|
|
112
|
+
this.nonCanonical = true;
|
|
113
|
+
return { kind: "text", value: this.decodeText(this.readIndefiniteString(path, true), path) };
|
|
114
|
+
}
|
|
115
|
+
return { kind: "text", value: this.decodeText(this.readStringBytes(argument, path), path) };
|
|
116
|
+
case 4:
|
|
117
|
+
if (indefinite)
|
|
118
|
+
this.nonCanonical = true;
|
|
119
|
+
return { kind: "array", value: [], indefinite, declared: argument };
|
|
120
|
+
case 5:
|
|
121
|
+
if (indefinite)
|
|
122
|
+
this.nonCanonical = true;
|
|
123
|
+
return { kind: "map", value: { entries: [], index: new Map() }, indefinite, declared: argument };
|
|
124
|
+
case 6: this.fail(renderPath(path), "CBOR tags are not allowed");
|
|
125
|
+
case 7:
|
|
126
|
+
if (indefinite)
|
|
127
|
+
this.fail(renderPath(path), "unexpected CBOR break");
|
|
128
|
+
return { kind: "other" };
|
|
129
|
+
default: this.fail(renderPath(path), "unsupported CBOR major type");
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
readHead(path) {
|
|
133
|
+
const first = this.data[this.offset++];
|
|
134
|
+
if (first === undefined)
|
|
135
|
+
this.fail(renderPath(path), "unexpected end of input");
|
|
136
|
+
const major = first >> 5;
|
|
137
|
+
const additional = first & 0x1f;
|
|
138
|
+
if (additional < 24)
|
|
139
|
+
return { major, argument: BigInt(additional), indefinite: false };
|
|
140
|
+
if (additional === 31) {
|
|
141
|
+
if (major < 2 || major > 5)
|
|
142
|
+
this.fail(renderPath(path), "indefinite-length header is only valid for byte strings, text strings, arrays and maps");
|
|
143
|
+
return { major, argument: 0n, indefinite: true };
|
|
144
|
+
}
|
|
145
|
+
if (additional !== 24 && additional !== 25 && additional !== 26 && additional !== 27) {
|
|
146
|
+
this.fail(renderPath(path), "reserved CBOR additional information");
|
|
147
|
+
}
|
|
148
|
+
const count = 1 << (additional - 24);
|
|
149
|
+
if (this.offset + count > this.data.length)
|
|
150
|
+
this.fail(renderPath(path), "truncated CBOR argument");
|
|
151
|
+
let argument = 0n;
|
|
152
|
+
for (let index = 0; index < count; index += 1)
|
|
153
|
+
argument = (argument << 8n) | BigInt(this.data[this.offset++]);
|
|
154
|
+
const shortest = additional === 24 ? 24n : additional === 25 ? 0x100n : additional === 26 ? 0x10000n : 0x100000000n;
|
|
155
|
+
if ((major !== 7 && argument < shortest) || (major === 7 && additional === 24 && argument < 24n))
|
|
156
|
+
this.nonCanonical = true;
|
|
157
|
+
return { major, argument, indefinite: false };
|
|
158
|
+
}
|
|
159
|
+
readStringBytes(length, path) {
|
|
160
|
+
if (length > BigInt(this.data.length - this.offset))
|
|
161
|
+
this.fail(renderPath(path), "truncated CBOR byte or text string");
|
|
162
|
+
const size = Number(length);
|
|
163
|
+
const output = this.data.slice(this.offset, this.offset + size);
|
|
164
|
+
this.offset += size;
|
|
165
|
+
return output;
|
|
166
|
+
}
|
|
167
|
+
readIndefiniteString(path, text) {
|
|
168
|
+
const chunks = [];
|
|
169
|
+
while (true) {
|
|
170
|
+
if (this.offset >= this.data.length)
|
|
171
|
+
this.fail(renderPath(path), "unterminated indefinite CBOR string");
|
|
172
|
+
if (this.takeBreak())
|
|
173
|
+
break;
|
|
174
|
+
const { major, argument, indefinite } = this.readHead(path);
|
|
175
|
+
const expected = text ? 3 : 2;
|
|
176
|
+
if (major !== expected || indefinite)
|
|
177
|
+
this.fail(renderPath(path), "invalid indefinite CBOR string chunk");
|
|
178
|
+
const chunk = this.readStringBytes(argument, path);
|
|
179
|
+
if (text)
|
|
180
|
+
this.decodeText(chunk, path);
|
|
181
|
+
chunks.push(chunk);
|
|
182
|
+
}
|
|
183
|
+
const output = new Uint8Array(chunks.reduce((total, chunk) => total + chunk.length, 0));
|
|
184
|
+
let offset = 0;
|
|
185
|
+
for (const chunk of chunks) {
|
|
186
|
+
output.set(chunk, offset);
|
|
187
|
+
offset += chunk.length;
|
|
188
|
+
}
|
|
189
|
+
return output;
|
|
190
|
+
}
|
|
191
|
+
decodeText(bytes, path) {
|
|
192
|
+
try {
|
|
193
|
+
return new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(bytes);
|
|
194
|
+
}
|
|
195
|
+
catch (cause) {
|
|
196
|
+
throw new ProgramManifestError(PROGRAM_MANIFEST_ERROR_CODES.INVALID_CBOR, renderPath(path), "CBOR text string is not valid UTF-8", { cause });
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
takeBreak() {
|
|
200
|
+
if (this.data[this.offset] !== 0xff)
|
|
201
|
+
return false;
|
|
202
|
+
this.offset += 1;
|
|
203
|
+
return true;
|
|
204
|
+
}
|
|
205
|
+
fail(path, message) {
|
|
206
|
+
throw new ProgramManifestError(PROGRAM_MANIFEST_ERROR_CODES.INVALID_CBOR, path, message);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
export function parseCBORDocument(bytes) {
|
|
210
|
+
return new CborParser(bytes).parseDocument();
|
|
211
|
+
}
|
|
212
|
+
export function canonicalCBOR(value) {
|
|
213
|
+
const output = [];
|
|
214
|
+
appendCanonicalCBOR(value, output);
|
|
215
|
+
return Uint8Array.from(output);
|
|
216
|
+
}
|
|
217
|
+
function appendCanonicalCBOR(value, output) {
|
|
218
|
+
switch (value.kind) {
|
|
219
|
+
case "uint":
|
|
220
|
+
appendHead(output, 0, value.value);
|
|
221
|
+
return;
|
|
222
|
+
case "bytes":
|
|
223
|
+
appendHead(output, 2, BigInt(value.value.length));
|
|
224
|
+
for (const byte of value.value)
|
|
225
|
+
output.push(byte);
|
|
226
|
+
return;
|
|
227
|
+
case "text": {
|
|
228
|
+
const encoded = new TextEncoder().encode(value.value);
|
|
229
|
+
appendHead(output, 3, BigInt(encoded.length));
|
|
230
|
+
for (const byte of encoded)
|
|
231
|
+
output.push(byte);
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
case "array":
|
|
235
|
+
appendHead(output, 4, BigInt(value.value.length));
|
|
236
|
+
for (const child of value.value)
|
|
237
|
+
appendCanonicalCBOR(child, output);
|
|
238
|
+
return;
|
|
239
|
+
case "map": {
|
|
240
|
+
const entries = [...value.value.entries].sort((left, right) => bytesCompare(left.keyBytes, right.keyBytes));
|
|
241
|
+
appendHead(output, 5, BigInt(entries.length));
|
|
242
|
+
for (const entry of entries) {
|
|
243
|
+
for (const byte of entry.keyBytes)
|
|
244
|
+
output.push(byte);
|
|
245
|
+
appendCanonicalCBOR(entry.value, output);
|
|
246
|
+
}
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
case "negative":
|
|
250
|
+
case "other": return;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
function appendHead(output, major, value) {
|
|
254
|
+
if (value < 24n)
|
|
255
|
+
output.push((major << 5) | Number(value));
|
|
256
|
+
else if (value <= 0xffn)
|
|
257
|
+
output.push((major << 5) | 24, Number(value));
|
|
258
|
+
else if (value <= 0xffffn)
|
|
259
|
+
output.push((major << 5) | 25, Number(value >> 8n), Number(value & 0xffn));
|
|
260
|
+
else if (value <= 0xffffffffn) {
|
|
261
|
+
output.push((major << 5) | 26);
|
|
262
|
+
for (let shift = 24n; shift >= 0n; shift -= 8n)
|
|
263
|
+
output.push(Number((value >> shift) & 0xffn));
|
|
264
|
+
}
|
|
265
|
+
else {
|
|
266
|
+
output.push((major << 5) | 27);
|
|
267
|
+
for (let shift = 56n; shift >= 0n; shift -= 8n)
|
|
268
|
+
output.push(Number((value >> shift) & 0xffn));
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
function bytesCompare(left, right) {
|
|
272
|
+
const length = Math.min(left.length, right.length);
|
|
273
|
+
for (let index = 0; index < length; index += 1) {
|
|
274
|
+
if (left[index] !== right[index])
|
|
275
|
+
return left[index] < right[index] ? -1 : 1;
|
|
276
|
+
}
|
|
277
|
+
return left.length - right.length;
|
|
278
|
+
}
|
|
279
|
+
export function cborMapValue(object) {
|
|
280
|
+
const value = {
|
|
281
|
+
kind: "map", value: { entries: [], index: new Map() }, indefinite: false, declared: 0n
|
|
282
|
+
};
|
|
283
|
+
for (const [key, child] of object) {
|
|
284
|
+
const keyBytes = canonicalCBOR(cborTextValue(key));
|
|
285
|
+
value.value.index.set(key, value.value.entries.length);
|
|
286
|
+
value.value.entries.push({ key, keyBytes, value: child });
|
|
287
|
+
}
|
|
288
|
+
return value;
|
|
289
|
+
}
|
|
290
|
+
export function cborTextValue(value) {
|
|
291
|
+
return { kind: "text", value };
|
|
292
|
+
}
|
|
293
|
+
export function cborUintValue(value) {
|
|
294
|
+
return { kind: "uint", value };
|
|
295
|
+
}
|
|
296
|
+
export function cborModelInteger(value) {
|
|
297
|
+
if (!Number.isSafeInteger(value))
|
|
298
|
+
return { kind: "other" };
|
|
299
|
+
if (value < 0)
|
|
300
|
+
return { kind: "negative", value: BigInt(-(value + 1)) + 1n };
|
|
301
|
+
return cborUintValue(BigInt(value));
|
|
302
|
+
}
|
|
303
|
+
export function cborBytesValue(value) {
|
|
304
|
+
return { kind: "bytes", value: value.slice() };
|
|
305
|
+
}
|
|
306
|
+
//# sourceMappingURL=cbor.js.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { ProgramManifest } from "./types.js";
|
|
2
|
+
/** 验证模型并返回确定性 CBOR 字节。 */
|
|
3
|
+
export declare function encodeProgramManifest(manifest: ProgramManifest): Uint8Array;
|
|
4
|
+
/** 严格验证原字节并返回已验证的静态或直播节目单。 */
|
|
5
|
+
export declare function decodeProgramManifest(bytes: Uint8Array): ProgramManifest;
|
|
6
|
+
//# sourceMappingURL=codec.d.ts.map
|
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
import { MANIFEST_SCHEMA } from "./constants.js";
|
|
2
|
+
import { PROGRAM_MANIFEST_ERROR_CODES, ProgramManifestError } from "./errors.js";
|
|
3
|
+
import { cborBytesValue, cborMapValue, cborModelInteger, cborTextValue, canonicalCBOR, parseCBORDocument, pointerChild } from "./cbor.js";
|
|
4
|
+
import { normalizeLanguageTagValue } from "./language.js";
|
|
5
|
+
const encoder = new TextEncoder();
|
|
6
|
+
const identifierPattern = /^[a-z0-9][a-z0-9_-]{0,63}$/;
|
|
7
|
+
const seedHashPattern = /^[0-9a-f]{64}$/;
|
|
8
|
+
const sessionIDPattern = /^live_[A-Za-z0-9_-]{1,96}$/;
|
|
9
|
+
/** 验证模型并返回确定性 CBOR 字节。 */
|
|
10
|
+
export function encodeProgramManifest(manifest) {
|
|
11
|
+
let root;
|
|
12
|
+
try {
|
|
13
|
+
root = toCborValue(manifest, "");
|
|
14
|
+
}
|
|
15
|
+
catch (error) {
|
|
16
|
+
if (error instanceof ProgramManifestError)
|
|
17
|
+
throw error;
|
|
18
|
+
throw schemaError("", "manifest model cannot be represented as CBOR");
|
|
19
|
+
}
|
|
20
|
+
validateManifestCBOR(root);
|
|
21
|
+
return canonicalCBOR(root);
|
|
22
|
+
}
|
|
23
|
+
/** 严格验证原字节并返回已验证的静态或直播节目单。 */
|
|
24
|
+
export function decodeProgramManifest(bytes) {
|
|
25
|
+
if (!(bytes instanceof Uint8Array))
|
|
26
|
+
throw schemaError("", "input must be Uint8Array");
|
|
27
|
+
const { root, nonCanonical } = parseCBORDocument(bytes);
|
|
28
|
+
const manifest = validateManifestCBOR(root);
|
|
29
|
+
if (nonCanonical || !bytesEqual(canonicalCBOR(root), bytes)) {
|
|
30
|
+
throw new ProgramManifestError(PROGRAM_MANIFEST_ERROR_CODES.NON_CANONICAL, "", "manifest bytes are not deterministic CBOR");
|
|
31
|
+
}
|
|
32
|
+
return manifest;
|
|
33
|
+
}
|
|
34
|
+
function toCborValue(input, path, ancestors = new WeakSet()) {
|
|
35
|
+
if (input === null)
|
|
36
|
+
return { kind: "other" };
|
|
37
|
+
if (input instanceof Uint8Array)
|
|
38
|
+
return cborBytesValue(input);
|
|
39
|
+
switch (typeof input) {
|
|
40
|
+
case "string": return cborTextValue(input);
|
|
41
|
+
case "boolean": return { kind: "other" };
|
|
42
|
+
case "number": return cborModelInteger(input);
|
|
43
|
+
case "object": break;
|
|
44
|
+
default: return { kind: "other" };
|
|
45
|
+
}
|
|
46
|
+
if (ancestors.has(input))
|
|
47
|
+
throw schemaError(path, "cyclic values are not valid CBOR");
|
|
48
|
+
ancestors.add(input);
|
|
49
|
+
try {
|
|
50
|
+
if (Array.isArray(input)) {
|
|
51
|
+
const value = [];
|
|
52
|
+
for (let index = 0; index < input.length; index += 1)
|
|
53
|
+
value.push(toCborValue(input[index], pointerChild(path, String(index)), ancestors));
|
|
54
|
+
return { kind: "array", value, indefinite: false, declared: 0n };
|
|
55
|
+
}
|
|
56
|
+
if (Object.getOwnPropertySymbols(input).some((symbol) => Object.prototype.propertyIsEnumerable.call(input, symbol))) {
|
|
57
|
+
throw schemaError(path, "symbol keys are not valid CBOR fields");
|
|
58
|
+
}
|
|
59
|
+
const value = new Map();
|
|
60
|
+
for (const key of Object.keys(input))
|
|
61
|
+
value.set(key, toCborValue(input[key], pointerChild(path, key), ancestors));
|
|
62
|
+
return cborMapValue(value);
|
|
63
|
+
}
|
|
64
|
+
finally {
|
|
65
|
+
ancestors.delete(input);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
function validateManifestCBOR(root) {
|
|
69
|
+
const object = requireMap(root, "");
|
|
70
|
+
const schema = requiredText(object, "schema", "");
|
|
71
|
+
if (schema !== MANIFEST_SCHEMA)
|
|
72
|
+
throw schemaError("/schema", `schema must be ${MANIFEST_SCHEMA}`);
|
|
73
|
+
const version = requiredValue(object, "version", "");
|
|
74
|
+
if (version.kind !== "uint")
|
|
75
|
+
throw schemaError("/version", "version must be an unsigned integer");
|
|
76
|
+
if (version.value !== 1n) {
|
|
77
|
+
throw new ProgramManifestError(PROGRAM_MANIFEST_ERROR_CODES.UNSUPPORTED_VERSION, "/version", "unsupported program manifest version");
|
|
78
|
+
}
|
|
79
|
+
const kind = requiredText(object, "kind", "");
|
|
80
|
+
if (kind !== "vod" && kind !== "live")
|
|
81
|
+
throw schemaError("/kind", "kind must be vod or live");
|
|
82
|
+
const allowed = new Set(["schema", "version", "kind", "title", "description", "cover"]);
|
|
83
|
+
if (kind === "vod")
|
|
84
|
+
allowed.add("parts");
|
|
85
|
+
else {
|
|
86
|
+
allowed.add("source");
|
|
87
|
+
allowed.add("tracks");
|
|
88
|
+
}
|
|
89
|
+
checkMapKeys(object, allowed, "");
|
|
90
|
+
const title = requiredText(object, "title", "");
|
|
91
|
+
if (title.length === 0 || encoder.encode(title).length > 1024 || isAllWhitespace(title)) {
|
|
92
|
+
throw schemaError("/title", "title must contain 1-1024 UTF-8 bytes and a non-whitespace character");
|
|
93
|
+
}
|
|
94
|
+
const description = requiredText(object, "description", "");
|
|
95
|
+
if (encoder.encode(description).length > 65535)
|
|
96
|
+
throw schemaError("/description", "description exceeds 65535 UTF-8 bytes");
|
|
97
|
+
const cover = decodeCover(requiredValue(object, "cover", ""), "/cover");
|
|
98
|
+
const base = { schema: MANIFEST_SCHEMA, version: 1, title, description, cover };
|
|
99
|
+
if (kind === "vod") {
|
|
100
|
+
return { ...base, kind: "vod", parts: decodeParts(requiredValue(object, "parts", ""), "/parts") };
|
|
101
|
+
}
|
|
102
|
+
return {
|
|
103
|
+
...base,
|
|
104
|
+
kind: "live",
|
|
105
|
+
source: decodeLiveSource(requiredValue(object, "source", ""), "/source"),
|
|
106
|
+
tracks: decodeLiveTracks(requiredValue(object, "tracks", ""), "/tracks")
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
function requiredValue(object, key, path) {
|
|
110
|
+
const value = object.get(key);
|
|
111
|
+
if (value === undefined)
|
|
112
|
+
throw schemaError(pointerChild(path, key), "required field is missing");
|
|
113
|
+
return value;
|
|
114
|
+
}
|
|
115
|
+
function requireMap(value, path) {
|
|
116
|
+
if (value.kind !== "map")
|
|
117
|
+
throw schemaError(path, "value must be a map");
|
|
118
|
+
return new Map(value.value.entries.map((entry) => [entry.key, entry.value]));
|
|
119
|
+
}
|
|
120
|
+
function requireArray(value, path, minimum, maximum) {
|
|
121
|
+
if (value.kind !== "array")
|
|
122
|
+
throw schemaError(path, "value must be an array");
|
|
123
|
+
if (value.value.length < minimum || value.value.length > maximum)
|
|
124
|
+
throw schemaError(path, `array length must be ${minimum}-${maximum}`);
|
|
125
|
+
return value.value;
|
|
126
|
+
}
|
|
127
|
+
function requiredText(object, key, path) {
|
|
128
|
+
return requireText(requiredValue(object, key, path), pointerChild(path, key));
|
|
129
|
+
}
|
|
130
|
+
function requireText(value, path) {
|
|
131
|
+
if (value.kind !== "text")
|
|
132
|
+
throw schemaError(path, "value must be a text string");
|
|
133
|
+
if (hasUnpairedSurrogate(value.value)) {
|
|
134
|
+
throw schemaError(path, "text contains an unpaired surrogate");
|
|
135
|
+
}
|
|
136
|
+
return value.value;
|
|
137
|
+
}
|
|
138
|
+
function requiredInteger(object, key, path, minimum, maximum) {
|
|
139
|
+
const value = requiredValue(object, key, path);
|
|
140
|
+
if (value.kind !== "uint")
|
|
141
|
+
throw schemaError(pointerChild(path, key), "value must be an unsigned integer");
|
|
142
|
+
if (value.value < BigInt(minimum) || value.value > BigInt(maximum)) {
|
|
143
|
+
throw schemaError(pointerChild(path, key), `unsigned integer must be in range ${minimum}-${maximum}`);
|
|
144
|
+
}
|
|
145
|
+
return Number(value.value);
|
|
146
|
+
}
|
|
147
|
+
function checkMapKeys(object, allowed, path) {
|
|
148
|
+
const unknown = [...object.keys()].filter((key) => !allowed.has(key)).sort();
|
|
149
|
+
if (unknown.length > 0)
|
|
150
|
+
throw schemaError(pointerChild(path, unknown[0]), "unknown field");
|
|
151
|
+
}
|
|
152
|
+
function decodeCover(value, path) {
|
|
153
|
+
const object = requireMap(value, path);
|
|
154
|
+
checkMapKeys(object, new Set(["mediaType", "data"]), path);
|
|
155
|
+
const mediaType = requiredText(object, "mediaType", path);
|
|
156
|
+
if (mediaType !== "image/jpeg" && mediaType !== "image/png" && mediaType !== "image/webp") {
|
|
157
|
+
throw schemaError(`${path}/mediaType`, "unsupported cover media type");
|
|
158
|
+
}
|
|
159
|
+
const data = requiredValue(object, "data", path);
|
|
160
|
+
if (data.kind !== "bytes")
|
|
161
|
+
throw schemaError(`${path}/data`, "cover data must be a byte string");
|
|
162
|
+
if (data.value.length === 0)
|
|
163
|
+
throw schemaError(`${path}/data`, "cover data must not be empty");
|
|
164
|
+
return { mediaType, data: Uint8Array.from(data.value) };
|
|
165
|
+
}
|
|
166
|
+
function decodeParts(value, path) {
|
|
167
|
+
const items = requireArray(value, path, 1, 64);
|
|
168
|
+
const partIDs = new Set();
|
|
169
|
+
return items.map((item, index) => {
|
|
170
|
+
const itemPath = pointerChild(path, String(index));
|
|
171
|
+
const part = decodePart(item, itemPath);
|
|
172
|
+
if (partIDs.has(part.id))
|
|
173
|
+
throw schemaError(`${itemPath}/id`, "duplicate part ID");
|
|
174
|
+
partIDs.add(part.id);
|
|
175
|
+
return part;
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
function decodePart(value, path) {
|
|
179
|
+
const object = requireMap(value, path);
|
|
180
|
+
checkMapKeys(object, new Set(["id", "video", "audio", "subtitles"]), path);
|
|
181
|
+
const id = requiredIdentifier(object, "id", path);
|
|
182
|
+
const video = decodeStaticVideos(requiredValue(object, "video", path), `${path}/video`);
|
|
183
|
+
const audio = decodeStaticAudio(requiredValue(object, "audio", path), `${path}/audio`);
|
|
184
|
+
const subtitles = decodeStaticSubtitles(requiredValue(object, "subtitles", path), `${path}/subtitles`);
|
|
185
|
+
const trackIDs = new Set();
|
|
186
|
+
registerTrackIDs(trackIDs, video, `${path}/video`);
|
|
187
|
+
registerTrackIDs(trackIDs, audio, `${path}/audio`);
|
|
188
|
+
registerTrackIDs(trackIDs, subtitles, `${path}/subtitles`);
|
|
189
|
+
return { id, video, audio, subtitles };
|
|
190
|
+
}
|
|
191
|
+
function decodeStaticVideos(value, path) {
|
|
192
|
+
const items = requireArray(value, path, 1, 16);
|
|
193
|
+
const resolutions = new Set();
|
|
194
|
+
return items.map((item, index) => {
|
|
195
|
+
const itemPath = pointerChild(path, String(index));
|
|
196
|
+
const object = requireMap(item, itemPath);
|
|
197
|
+
checkMapKeys(object, new Set(["id", "width", "height", "mediaType", "seedHash"]), itemPath);
|
|
198
|
+
const id = requiredIdentifier(object, "id", itemPath);
|
|
199
|
+
const width = requiredInteger(object, "width", itemPath, 1, 16384);
|
|
200
|
+
const height = requiredInteger(object, "height", itemPath, 1, 16384);
|
|
201
|
+
const resolution = `${width}x${height}`;
|
|
202
|
+
if (resolutions.has(resolution))
|
|
203
|
+
throw schemaError(itemPath, "duplicate video resolution");
|
|
204
|
+
resolutions.add(resolution);
|
|
205
|
+
const mediaType = requiredText(object, "mediaType", itemPath);
|
|
206
|
+
if (mediaType !== "video/mp4" && mediaType !== "video/webm")
|
|
207
|
+
throw schemaError(`${itemPath}/mediaType`, "unsupported video media type");
|
|
208
|
+
const seedHash = requiredSeedHash(object, itemPath);
|
|
209
|
+
return { id, width, height, mediaType, seedHash };
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
function decodeStaticAudio(value, path) {
|
|
213
|
+
return requireArray(value, path, 0, 32).map((item, index) => {
|
|
214
|
+
const itemPath = pointerChild(path, String(index));
|
|
215
|
+
const object = requireMap(item, itemPath);
|
|
216
|
+
checkMapKeys(object, new Set(["id", "language", "mediaType", "seedHash"]), itemPath);
|
|
217
|
+
return decodeStaticFileTrack(object, itemPath, "audio");
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
function decodeStaticSubtitles(value, path) {
|
|
221
|
+
return requireArray(value, path, 0, 32).map((item, index) => {
|
|
222
|
+
const itemPath = pointerChild(path, String(index));
|
|
223
|
+
const object = requireMap(item, itemPath);
|
|
224
|
+
checkMapKeys(object, new Set(["id", "language", "mediaType", "seedHash"]), itemPath);
|
|
225
|
+
return decodeStaticFileTrack(object, itemPath, "subtitle");
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
function decodeStaticFileTrack(object, path, kind) {
|
|
229
|
+
const id = requiredIdentifier(object, "id", path);
|
|
230
|
+
const language = requiredManifestLanguage(object, path);
|
|
231
|
+
const mediaType = requiredText(object, "mediaType", path);
|
|
232
|
+
const valid = kind === "audio"
|
|
233
|
+
? mediaType === "audio/mp4" || mediaType === "audio/webm" || mediaType === "audio/mpeg"
|
|
234
|
+
: mediaType === "text/vtt";
|
|
235
|
+
if (!valid)
|
|
236
|
+
throw schemaError(`${path}/mediaType`, `unsupported ${kind} media type`);
|
|
237
|
+
const seedHash = requiredSeedHash(object, path);
|
|
238
|
+
return { id, language, mediaType, seedHash };
|
|
239
|
+
}
|
|
240
|
+
function decodeLiveSource(value, path) {
|
|
241
|
+
const object = requireMap(value, path);
|
|
242
|
+
checkMapKeys(object, new Set(["transport", "sessionId"]), path);
|
|
243
|
+
const transport = requiredText(object, "transport", path);
|
|
244
|
+
if (transport !== "streamseed")
|
|
245
|
+
throw schemaError(`${path}/transport`, "transport must be streamseed");
|
|
246
|
+
const sessionId = requiredText(object, "sessionId", path);
|
|
247
|
+
if (!sessionIDPattern.test(sessionId))
|
|
248
|
+
throw schemaError(`${path}/sessionId`, "invalid live session ID");
|
|
249
|
+
return { transport: "streamseed", sessionId };
|
|
250
|
+
}
|
|
251
|
+
function decodeLiveTracks(value, path) {
|
|
252
|
+
const object = requireMap(value, path);
|
|
253
|
+
checkMapKeys(object, new Set(["video", "audio", "subtitles"]), path);
|
|
254
|
+
const video = decodeLiveVideos(requiredValue(object, "video", path), `${path}/video`);
|
|
255
|
+
const audio = decodeLiveAudio(requiredValue(object, "audio", path), `${path}/audio`);
|
|
256
|
+
const subtitles = decodeLiveSubtitles(requiredValue(object, "subtitles", path), `${path}/subtitles`);
|
|
257
|
+
const trackIDs = new Set();
|
|
258
|
+
registerTrackIDs(trackIDs, video, `${path}/video`);
|
|
259
|
+
registerTrackIDs(trackIDs, audio, `${path}/audio`);
|
|
260
|
+
registerTrackIDs(trackIDs, subtitles, `${path}/subtitles`);
|
|
261
|
+
return { video, audio, subtitles };
|
|
262
|
+
}
|
|
263
|
+
function decodeLiveVideos(value, path) {
|
|
264
|
+
const items = requireArray(value, path, 1, 16);
|
|
265
|
+
const resolutions = new Set();
|
|
266
|
+
return items.map((item, index) => {
|
|
267
|
+
const itemPath = pointerChild(path, String(index));
|
|
268
|
+
const object = requireMap(item, itemPath);
|
|
269
|
+
checkMapKeys(object, new Set(["id", "width", "height"]), itemPath);
|
|
270
|
+
const id = requiredIdentifier(object, "id", itemPath);
|
|
271
|
+
const width = requiredInteger(object, "width", itemPath, 1, 16384);
|
|
272
|
+
const height = requiredInteger(object, "height", itemPath, 1, 16384);
|
|
273
|
+
const resolution = `${width}x${height}`;
|
|
274
|
+
if (resolutions.has(resolution))
|
|
275
|
+
throw schemaError(itemPath, "duplicate video resolution");
|
|
276
|
+
resolutions.add(resolution);
|
|
277
|
+
return { id, width, height };
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
function decodeLiveAudio(value, path) {
|
|
281
|
+
return requireArray(value, path, 0, 32).map((item, index) => decodeLiveLanguageTrack(item, pointerChild(path, String(index))));
|
|
282
|
+
}
|
|
283
|
+
function decodeLiveSubtitles(value, path) {
|
|
284
|
+
return requireArray(value, path, 0, 32).map((item, index) => decodeLiveLanguageTrack(item, pointerChild(path, String(index))));
|
|
285
|
+
}
|
|
286
|
+
function decodeLiveLanguageTrack(value, path) {
|
|
287
|
+
const object = requireMap(value, path);
|
|
288
|
+
checkMapKeys(object, new Set(["id", "language"]), path);
|
|
289
|
+
const id = requiredIdentifier(object, "id", path);
|
|
290
|
+
const language = requiredManifestLanguage(object, path);
|
|
291
|
+
return { id, language };
|
|
292
|
+
}
|
|
293
|
+
function requiredIdentifier(object, key, path) {
|
|
294
|
+
const id = requiredText(object, key, path);
|
|
295
|
+
if (!identifierPattern.test(id))
|
|
296
|
+
throw schemaError(pointerChild(path, key), "invalid identifier");
|
|
297
|
+
return id;
|
|
298
|
+
}
|
|
299
|
+
function requiredManifestLanguage(object, path) {
|
|
300
|
+
const language = requiredText(object, "language", path);
|
|
301
|
+
const normalized = normalizeLanguageTagValue(language);
|
|
302
|
+
if (normalized === undefined || normalized !== language) {
|
|
303
|
+
throw new ProgramManifestError(PROGRAM_MANIFEST_ERROR_CODES.INVALID_LANGUAGE, `${path}/language`, "language tag must use canonical v1 casing");
|
|
304
|
+
}
|
|
305
|
+
return language;
|
|
306
|
+
}
|
|
307
|
+
function requiredSeedHash(object, path) {
|
|
308
|
+
const seedHash = requiredText(object, "seedHash", path);
|
|
309
|
+
if (!seedHashPattern.test(seedHash))
|
|
310
|
+
throw schemaError(`${path}/seedHash`, "seedHash must be 64 lowercase hexadecimal characters");
|
|
311
|
+
return seedHash;
|
|
312
|
+
}
|
|
313
|
+
function registerTrackIDs(ids, tracks, path) {
|
|
314
|
+
tracks.forEach((track, index) => {
|
|
315
|
+
if (ids.has(track.id))
|
|
316
|
+
throw schemaError(`${pointerChild(path, String(index))}/id`, "duplicate track ID");
|
|
317
|
+
ids.add(track.id);
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
function isAllWhitespace(value) {
|
|
321
|
+
for (const character of value) {
|
|
322
|
+
const codePoint = character.codePointAt(0);
|
|
323
|
+
const whitespace = (codePoint >= 0x0009 && codePoint <= 0x000d)
|
|
324
|
+
|| codePoint === 0x0020 || codePoint === 0x0085 || codePoint === 0x00a0
|
|
325
|
+
|| codePoint === 0x1680 || (codePoint >= 0x2000 && codePoint <= 0x200a)
|
|
326
|
+
|| codePoint === 0x2028 || codePoint === 0x2029 || codePoint === 0x202f
|
|
327
|
+
|| codePoint === 0x205f || codePoint === 0x3000;
|
|
328
|
+
if (!whitespace)
|
|
329
|
+
return false;
|
|
330
|
+
}
|
|
331
|
+
return true;
|
|
332
|
+
}
|
|
333
|
+
function hasUnpairedSurrogate(value) {
|
|
334
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
335
|
+
const code = value.charCodeAt(index);
|
|
336
|
+
if (code >= 0xd800 && code <= 0xdbff) {
|
|
337
|
+
if (index + 1 >= value.length)
|
|
338
|
+
return true;
|
|
339
|
+
const next = value.charCodeAt(index + 1);
|
|
340
|
+
if (next < 0xdc00 || next > 0xdfff)
|
|
341
|
+
return true;
|
|
342
|
+
index += 1;
|
|
343
|
+
}
|
|
344
|
+
else if (code >= 0xdc00 && code <= 0xdfff)
|
|
345
|
+
return true;
|
|
346
|
+
}
|
|
347
|
+
return false;
|
|
348
|
+
}
|
|
349
|
+
function bytesEqual(left, right) {
|
|
350
|
+
if (left.length !== right.length)
|
|
351
|
+
return false;
|
|
352
|
+
for (let index = 0; index < left.length; index += 1)
|
|
353
|
+
if (left[index] !== right[index])
|
|
354
|
+
return false;
|
|
355
|
+
return true;
|
|
356
|
+
}
|
|
357
|
+
function schemaError(path, message) {
|
|
358
|
+
return new ProgramManifestError(PROGRAM_MANIFEST_ERROR_CODES.INVALID_SCHEMA, path, message);
|
|
359
|
+
}
|
|
360
|
+
//# sourceMappingURL=codec.js.map
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export declare const PROGRAM_MANIFEST_ERROR_CODES: {
|
|
2
|
+
readonly INVALID_CBOR: "INVALID_CBOR";
|
|
3
|
+
readonly INVALID_SCHEMA: "INVALID_SCHEMA";
|
|
4
|
+
readonly UNSUPPORTED_VERSION: "UNSUPPORTED_VERSION";
|
|
5
|
+
readonly INVALID_LANGUAGE: "INVALID_LANGUAGE";
|
|
6
|
+
readonly NON_CANONICAL: "NON_CANONICAL";
|
|
7
|
+
};
|
|
8
|
+
export type ProgramManifestErrorCode = (typeof PROGRAM_MANIFEST_ERROR_CODES)[keyof typeof PROGRAM_MANIFEST_ERROR_CODES];
|
|
9
|
+
/** 节目单 SDK 的稳定错误;path 是 RFC 6901 JSON Pointer。 */
|
|
10
|
+
export declare class ProgramManifestError extends Error {
|
|
11
|
+
readonly code: ProgramManifestErrorCode;
|
|
12
|
+
readonly path: string;
|
|
13
|
+
constructor(code: ProgramManifestErrorCode, path: string, message?: string, options?: {
|
|
14
|
+
cause?: unknown;
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
export declare const ManifestError: typeof ProgramManifestError;
|
|
18
|
+
export type ManifestErrorCode = ProgramManifestErrorCode;
|
|
19
|
+
export declare function isProgramManifestError(value: unknown): value is ProgramManifestError;
|
|
20
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export const PROGRAM_MANIFEST_ERROR_CODES = {
|
|
2
|
+
INVALID_CBOR: "INVALID_CBOR",
|
|
3
|
+
INVALID_SCHEMA: "INVALID_SCHEMA",
|
|
4
|
+
UNSUPPORTED_VERSION: "UNSUPPORTED_VERSION",
|
|
5
|
+
INVALID_LANGUAGE: "INVALID_LANGUAGE",
|
|
6
|
+
NON_CANONICAL: "NON_CANONICAL"
|
|
7
|
+
};
|
|
8
|
+
/** 节目单 SDK 的稳定错误;path 是 RFC 6901 JSON Pointer。 */
|
|
9
|
+
export class ProgramManifestError extends Error {
|
|
10
|
+
code;
|
|
11
|
+
path;
|
|
12
|
+
constructor(code, path, message, options) {
|
|
13
|
+
super(message ?? code, options);
|
|
14
|
+
this.name = "ProgramManifestError";
|
|
15
|
+
this.code = code;
|
|
16
|
+
this.path = path;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export const ManifestError = ProgramManifestError;
|
|
20
|
+
export function isProgramManifestError(value) {
|
|
21
|
+
return value instanceof ProgramManifestError;
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { PROGRAM_MANIFEST_ERROR_CODES, ProgramManifestError } from "./errors.js";
|
|
2
|
+
const languagePattern = /^[A-Za-z]{2,3}(?:-[A-Za-z]{4})?(?:-(?:[A-Za-z]{2}|[0-9]{3}))?$/;
|
|
3
|
+
/** 校验 v1 语言结构并统一大小写;无效结构返回 path 为空的 INVALID_LANGUAGE。 */
|
|
4
|
+
export function normalizeLanguageTag(tag) {
|
|
5
|
+
const normalized = normalizeLanguageTagValue(tag);
|
|
6
|
+
if (normalized === undefined) {
|
|
7
|
+
throw new ProgramManifestError(PROGRAM_MANIFEST_ERROR_CODES.INVALID_LANGUAGE, "", "invalid v1 language tag");
|
|
8
|
+
}
|
|
9
|
+
return normalized;
|
|
10
|
+
}
|
|
11
|
+
export function normalizeLanguageTagValue(tag) {
|
|
12
|
+
if (typeof tag !== "string" || !languagePattern.test(tag))
|
|
13
|
+
return undefined;
|
|
14
|
+
const parts = tag.split("-");
|
|
15
|
+
const primary = parts[0].toLowerCase();
|
|
16
|
+
if (primary === "und" && parts.length !== 1)
|
|
17
|
+
return undefined;
|
|
18
|
+
return [primary, ...parts.slice(1).map((part) => {
|
|
19
|
+
if (part.length === 4)
|
|
20
|
+
return part[0].toUpperCase() + part.slice(1).toLowerCase();
|
|
21
|
+
if (part.length === 2)
|
|
22
|
+
return part.toUpperCase();
|
|
23
|
+
return part;
|
|
24
|
+
})].join("-");
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=language.js.map
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
export interface Cover {
|
|
2
|
+
mediaType: "image/jpeg" | "image/png" | "image/webp";
|
|
3
|
+
data: Uint8Array;
|
|
4
|
+
}
|
|
5
|
+
export interface VodVideo {
|
|
6
|
+
id: string;
|
|
7
|
+
width: number;
|
|
8
|
+
height: number;
|
|
9
|
+
mediaType: "video/mp4" | "video/webm";
|
|
10
|
+
seedHash: string;
|
|
11
|
+
}
|
|
12
|
+
export interface VodAudio {
|
|
13
|
+
id: string;
|
|
14
|
+
language: string;
|
|
15
|
+
mediaType: "audio/mp4" | "audio/webm" | "audio/mpeg";
|
|
16
|
+
seedHash: string;
|
|
17
|
+
}
|
|
18
|
+
export interface VodSubtitle {
|
|
19
|
+
id: string;
|
|
20
|
+
language: string;
|
|
21
|
+
mediaType: "text/vtt";
|
|
22
|
+
seedHash: string;
|
|
23
|
+
}
|
|
24
|
+
export interface VodPart {
|
|
25
|
+
id: string;
|
|
26
|
+
video: VodVideo[];
|
|
27
|
+
audio: VodAudio[];
|
|
28
|
+
subtitles: VodSubtitle[];
|
|
29
|
+
}
|
|
30
|
+
export interface LiveSource {
|
|
31
|
+
transport: "streamseed";
|
|
32
|
+
sessionId: string;
|
|
33
|
+
}
|
|
34
|
+
export interface LiveVideo {
|
|
35
|
+
id: string;
|
|
36
|
+
width: number;
|
|
37
|
+
height: number;
|
|
38
|
+
}
|
|
39
|
+
export interface LiveAudio {
|
|
40
|
+
id: string;
|
|
41
|
+
language: string;
|
|
42
|
+
}
|
|
43
|
+
export interface LiveSubtitle {
|
|
44
|
+
id: string;
|
|
45
|
+
language: string;
|
|
46
|
+
}
|
|
47
|
+
export interface LiveTracks {
|
|
48
|
+
video: LiveVideo[];
|
|
49
|
+
audio: LiveAudio[];
|
|
50
|
+
subtitles: LiveSubtitle[];
|
|
51
|
+
}
|
|
52
|
+
interface ProgramManifestBase {
|
|
53
|
+
schema: "bsv8.program-manifest";
|
|
54
|
+
version: 1;
|
|
55
|
+
title: string;
|
|
56
|
+
description: string;
|
|
57
|
+
cover: Cover;
|
|
58
|
+
}
|
|
59
|
+
export interface VodProgramManifest extends ProgramManifestBase {
|
|
60
|
+
kind: "vod";
|
|
61
|
+
parts: VodPart[];
|
|
62
|
+
source?: never;
|
|
63
|
+
tracks?: never;
|
|
64
|
+
}
|
|
65
|
+
export interface LiveProgramManifest extends ProgramManifestBase {
|
|
66
|
+
kind: "live";
|
|
67
|
+
source: LiveSource;
|
|
68
|
+
tracks: LiveTracks;
|
|
69
|
+
parts?: never;
|
|
70
|
+
}
|
|
71
|
+
export type ProgramManifest = VodProgramManifest | LiveProgramManifest;
|
|
72
|
+
export {};
|
|
73
|
+
//# sourceMappingURL=types.d.ts.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "streamseed",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "StreamSeed V1 publisher-authenticated headerless sequential media-block stream SDK",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -11,11 +11,30 @@
|
|
|
11
11
|
"types": "./dist/index.d.ts",
|
|
12
12
|
"import": "./dist/index.js",
|
|
13
13
|
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"./manifest": {
|
|
16
|
+
"types": "./dist/manifest/index.d.ts",
|
|
17
|
+
"import": "./dist/manifest/index.js",
|
|
18
|
+
"default": "./dist/manifest/index.js"
|
|
14
19
|
}
|
|
15
20
|
},
|
|
16
21
|
"files": [
|
|
17
22
|
"dist/index.js",
|
|
18
23
|
"dist/index.d.ts",
|
|
24
|
+
"dist/manifest/index.js",
|
|
25
|
+
"dist/manifest/index.d.ts",
|
|
26
|
+
"dist/manifest/constants.js",
|
|
27
|
+
"dist/manifest/constants.d.ts",
|
|
28
|
+
"dist/manifest/errors.js",
|
|
29
|
+
"dist/manifest/errors.d.ts",
|
|
30
|
+
"dist/manifest/types.js",
|
|
31
|
+
"dist/manifest/types.d.ts",
|
|
32
|
+
"dist/manifest/language.js",
|
|
33
|
+
"dist/manifest/language.d.ts",
|
|
34
|
+
"dist/manifest/cbor.js",
|
|
35
|
+
"dist/manifest/cbor.d.ts",
|
|
36
|
+
"dist/manifest/codec.js",
|
|
37
|
+
"dist/manifest/codec.d.ts",
|
|
19
38
|
"dist/internal/cbor-codec.js",
|
|
20
39
|
"dist/internal/cbor-codec.d.ts",
|
|
21
40
|
"dist/internal/ed25519.js",
|
|
@@ -67,16 +86,10 @@
|
|
|
67
86
|
"masterseed": "1.1.0"
|
|
68
87
|
},
|
|
69
88
|
"devDependencies": {
|
|
70
|
-
"@types/node": "^22.10.2",
|
|
71
|
-
"esbuild": "0.28.2",
|
|
72
89
|
"typescript": "^5.7.3"
|
|
73
90
|
},
|
|
74
91
|
"scripts": {
|
|
75
92
|
"build": "tsc -p tsconfig.json",
|
|
76
|
-
"build:test": "tsc -p tsconfig.test.json",
|
|
77
|
-
"test:runtime": "node --test dist-test/test/*.test.js",
|
|
78
|
-
"check:browser": "node scripts/browser-bundle.mjs",
|
|
79
|
-
"check": "pnpm run build && pnpm run build:test && pnpm run test:runtime && pnpm run check:browser",
|
|
80
93
|
"pack:check": "pnpm pack --dry-run"
|
|
81
94
|
}
|
|
82
95
|
}
|