@lunagerm-studio/video-cut-skill 0.0.0-stage → 1.0.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 +13 -2
- package/package.json +12 -4
- package/skills/ai-video-opening/SKILL.md +62 -0
- package/skills/ai-video-opening/references/export-and-validation.md +9 -0
- package/skills/ai-video-opening/references/motion-and-layout.md +7 -0
- package/skills/ai-video-opening/references/series-cases.md +7 -0
- package/skills/blue-white-ui-motion/SKILL.md +30 -0
- package/skills/blue-white-ui-motion/agents/openai.yaml +2 -0
- package/skills/blue-white-ui-motion/references/review.md +25 -0
- package/skills/chengfeng-videocut-skills/LICENSE +201 -0
- package/skills/chengfeng-videocut-skills/NOTICE.md +27 -0
- package/skills/chengfeng-videocut-skills/README.md +265 -0
- package/skills/chengfeng-videocut-skills//345/211/252/345/217/243/346/222/255/SKILL.md +37 -0
- package/skills/chengfeng-videocut-skills//345/211/252/345/217/243/346/222/255/agents/openai.yaml +2 -0
- package/skills/chengfeng-videocut-skills//345/211/252/345/217/243/346/222/255/scripts/cut_video.sh +182 -0
- package/skills/chengfeng-videocut-skills//345/211/252/345/217/243/346/222/255/scripts/detect_en_stutter.js +49 -0
- package/skills/chengfeng-videocut-skills//345/211/252/345/217/243/346/222/255/scripts/generate_review.js +1484 -0
- package/skills/chengfeng-videocut-skills//345/211/252/345/217/243/346/222/255/scripts/generate_srt_for_video.sh +75 -0
- package/skills/chengfeng-videocut-skills//345/211/252/345/217/243/346/222/255/scripts/generate_subtitles.js +141 -0
- package/skills/chengfeng-videocut-skills//345/211/252/345/217/243/346/222/255/scripts/review_server.js +381 -0
- package/skills/chengfeng-videocut-skills//345/211/252/345/217/243/346/222/255/scripts/volcengine_transcribe.sh +224 -0
- package/skills/chengfeng-videocut-skills//345/211/252/345/217/243/346/222/255/scripts/watch_cut_done.js +86 -0
- package/skills/chengfeng-videocut-skills//345/211/252/345/217/243/346/222/255//347/224/250/346/210/267/344/271/240/346/203/257/1-/346/240/270/345/277/203/345/216/237/345/210/231.md +56 -0
- package/skills/chengfeng-videocut-skills//345/211/252/345/217/243/346/222/255//347/224/250/346/210/267/344/271/240/346/203/257/10-/345/210/240/351/231/244/351/243/216/351/231/251/345/210/206/345/261/202.md +44 -0
- package/skills/chengfeng-videocut-skills//345/211/252/345/217/243/346/222/255//347/224/250/346/210/267/344/271/240/346/203/257/11-/345/217/243/346/222/255/347/250/277/345/257/271/351/275/220.md +40 -0
- package/skills/chengfeng-videocut-skills//345/211/252/345/217/243/346/222/255//347/224/250/346/210/267/344/271/240/346/203/257/2-/350/257/255/346/260/224/350/257/215/346/243/200/346/265/213.md +30 -0
- package/skills/chengfeng-videocut-skills//345/211/252/345/217/243/346/222/255//347/224/250/346/210/267/344/271/240/346/203/257/3-/351/235/231/351/237/263/346/256/265/345/244/204/347/220/206.md +56 -0
- package/skills/chengfeng-videocut-skills//345/211/252/345/217/243/346/222/255//347/224/250/346/210/267/344/271/240/346/203/257/4-/351/207/215/345/244/215/346/243/200/346/265/213.md +77 -0
- package/skills/chengfeng-videocut-skills//345/211/252/345/217/243/346/222/255//347/224/250/346/210/267/344/271/240/346/203/257/5-/345/215/241/351/241/277/350/257/215.md +48 -0
- package/skills/chengfeng-videocut-skills//345/211/252/345/217/243/346/222/255//347/224/250/346/210/267/344/271/240/346/203/257/7-/350/277/236/347/273/255/350/257/255/346/260/224/350/257/215.md +32 -0
- package/skills/chengfeng-videocut-skills//345/211/252/345/217/243/346/222/255//347/224/250/346/210/267/344/271/240/346/203/257/9-/346/256/213/345/217/245/346/243/200/346/265/213.md +79 -0
- package/skills/chengfeng-videocut-skills//345/211/252/345/217/243/346/222/255//347/224/250/346/210/267/344/271/240/346/203/257/README.md +35 -0
- package/skills/chengfeng-videocut-skills//345/217/243/346/222/255/346/210/220/347/211/207/SKILL.md +32 -0
- package/skills/chengfeng-videocut-skills//345/217/243/346/222/255/346/210/220/347/211/207/agents/openai.yaml +2 -0
- package/skills/chengfeng-videocut-skills//345/217/243/346/222/255/346/210/220/347/211/207/references/artifact-contracts.md +168 -0
- package/skills/chengfeng-videocut-skills//345/217/243/346/222/255/346/210/220/347/211/207/scripts/animation_style.cjs +53 -0
- package/skills/chengfeng-videocut-skills//345/217/243/346/222/255/346/210/220/347/211/207/scripts/export_final_video.cjs +200 -0
- package/skills/chengfeng-videocut-skills//345/217/243/346/222/255/346/210/220/347/211/207/scripts/serve_range_preview.cjs +128 -0
- package/skills/chengfeng-videocut-skills//345/217/243/346/222/255/346/210/220/347/211/207/scripts/user_config.cjs +80 -0
- package/skills/chengfeng-videocut-skills//345/217/243/346/222/255/346/210/220/347/211/207/scripts/write_render_mode.cjs +149 -0
- package/skills/chengfeng-videocut-skills//345/217/243/346/222/255/346/210/220/347/211/207//347/224/250/346/210/267/351/205/215/347/275/256/default.json +4 -0
- package/skills/chengfeng-videocut-skills//350/207/252/350/277/233/345/214/226/README.md +23 -0
- package/skills/chengfeng-videocut-skills//350/207/252/350/277/233/345/214/226/SKILL.md +126 -0
- package/skills/continuous-collage-motion/SKILL.md +30 -0
- package/skills/continuous-collage-motion/agents/openai.yaml +4 -0
- package/skills/continuous-collage-motion/references/reference-motion.md +32 -0
- package/skills/continuous-collage-motion/references/shot-plan.md +31 -0
- package/skills/continuous-collage-motion/references/style-check.md +17 -0
- package/skills/table-video/LICENSES/ATTRIBUTION.md +31 -0
- package/skills/table-video/LICENSES/CC-BY-4.0.txt +396 -0
- package/skills/table-video/LICENSES/MIT.txt +21 -0
- package/skills/table-video/SKILL.md +32 -0
- package/skills/table-video/SOURCES.md +61 -0
- package/skills/table-video/agents/openai.yaml +2 -0
- package/skills/table-video/references/complete-motion.md +97 -0
- package/skills/table-video/references/examples/01_/344/270/244/346/256/265/346/274/224/347/244/272.md +73 -0
- package/skills/table-video/references/examples/02_/347/224/237/345/233/276/347/224/237/350/247/206/351/242/221/346/217/220/347/244/272/350/257/215/347/244/272/344/276/213.md +50 -0
- package/skills/table-video/references/examples/03_DESIGN/345/241/253/345/206/231/347/244/272/344/276/213.md +333 -0
- package/skills/table-video/references/examples/04_/350/277/236/347/273/255MG/346/255/243/345/217/215/350/277/207/347/250/213.md +101 -0
- package/skills/table-video/references/examples/05_/345/256/214/346/225/264/350/277/207/347/250/213/346/272/220/347/240/201.md +55 -0
- package/skills/table-video/references/examples/05_/346/205/242/351/225/234/345/244/264/344/270/216/345/220/214/346/236/204/345/233/276/345/233/236/345/275/222.md +35 -0
- package/skills/table-video/references/experience//345/255/227/345/271/225/344/270/216/345/260/201/351/235/242.md +30 -0
- package/skills/table-video/references/experience//345/275/225/345/261/217/345/211/252/350/276/221.md +31 -0
- package/skills/table-video/references/experience//346/211/213/345/212/250/347/224/237/345/233/276/347/224/237/350/247/206/351/242/221.md +53 -0
- package/skills/table-video/references/experience//350/247/206/350/247/211/345/217/231/344/272/213/344/270/216/350/277/236/347/273/255MG.md +102 -0
- package/skills/table-video/references/experience//351/225/234/345/244/264/346/216/250/350/277/233/344/270/216/350/275/254/345/234/272.md +178 -0
- package/skills/table-video/references/intake.md +84 -0
- package/skills/table-video/references/library.md +26 -0
- package/skills/table-video/references/motion-check-method.md +147 -0
- package/skills/table-video/references/planning.md +62 -0
- package/skills/table-video/references/production.md +70 -0
- package/skills/table-video/references/project-rules.md +26 -0
- package/skills/table-video/references/review-and-repair.md +64 -0
- package/skills/table-video/references/self-diagnosis.md +46 -0
- package/skills/table-video/references/sop/01_/345/277/253/351/200/237/345/274/200/345/247/213.md +55 -0
- package/skills/table-video/references/sop/02_/345/210/266/344/275/234SOP.md +77 -0
- package/skills/table-video/references/sop/03_/347/233/256/345/275/225/344/270/216/345/271/266/350/241/214/345/215/217/344/275/234.md +72 -0
- package/skills/table-video/references/sop/04_/351/252/214/346/224/266/344/270/216/350/277/224/344/277/256.md +146 -0
- package/skills/table-video/references/sop/05_/346/226/207/346/241/210/346/243/200/346/237/245_/345/217/257/351/200/211.md +55 -0
- package/skills/table-video/references/sop/06_/351/237/263/346/225/210/344/270/216/351/205/215/344/271/220_/345/217/257/351/200/211.md +138 -0
- package/skills/table-video/references/sop/07_/345/267/245/345/205/267/344/270/216/347/216/257/345/242/203.md +52 -0
- package/skills/table-video/references/sop/08_/346/235/245/346/272/220/344/270/216/350/256/270/345/217/257.md +36 -0
- package/skills/table-video/references/sop/09_DESIGN/345/273/272/347/253/213/344/270/216/346/240/241/351/252/214.md +52 -0
- package/skills/table-video/references/state-contract.md +267 -0
- package/skills/table-video/references/style-and-motion.md +163 -0
- package/skills/table-video/schemas/request.schema.json +44 -0
- package/skills/table-video/schemas/state.schema.json +140 -0
- package/skills/table-video/scripts/cli.mjs +35 -0
- package/skills/table-video/scripts/core.mjs +977 -0
- package/skills/table-video/scripts/gain-proof.mjs +48 -0
- package/skills/table-video/scripts/init-project.mjs +7 -0
- package/skills/table-video/scripts/motion-audit.mjs +589 -0
- package/skills/table-video/scripts/motion-check.mjs +345 -0
- package/skills/table-video/scripts/motion-gates.mjs +997 -0
- package/skills/table-video/scripts/motion-observation.cjs +204 -0
- package/skills/table-video/scripts/preflight.mjs +24 -0
- package/skills/table-video/scripts/process-locate.mjs +57 -0
- package/skills/table-video/scripts/render-motion-example.mjs +143 -0
- package/skills/table-video/scripts/save-reference-image.mjs +249 -0
- package/skills/table-video/scripts/schema.mjs +71 -0
- package/skills/table-video/scripts/stillness.mjs +386 -0
- package/skills/table-video/scripts/storage.mjs +328 -0
- package/skills/table-video/scripts/workflow.mjs +24 -0
- package/skills/talking-head-motion-workflow/SKILL.md +36 -0
- package/skills/talking-head-motion-workflow/agents/openai.yaml +4 -0
- package/skills/talking-head-motion-workflow/references/cinematic-motion-and-sync.md +72 -0
- package/skills/talking-head-motion-workflow/references/classic-ui.md +11 -0
- package/skills/talking-head-motion-workflow/references/history-coverage.md +50 -0
- package/skills/talking-head-motion-workflow/references/layered-assets-and-characters.md +39 -0
- package/skills/talking-head-motion-workflow/references/layout-references.md +41 -0
- package/skills/talking-head-motion-workflow/references/lens-shot-selection.md +138 -0
- package/skills/talking-head-motion-workflow/references/live-interface-motion.md +69 -0
- package/skills/talking-head-motion-workflow/references/motion-recipes.md +40 -0
- package/skills/talking-head-motion-workflow/references/mouse-meter2-case.md +113 -0
- package/skills/talking-head-motion-workflow/references/pop-collage.md +61 -0
- package/skills/talking-head-motion-workflow/references/portable-production.md +32 -0
- package/skills/talking-head-motion-workflow/references/production-requirements.md +85 -0
- package/skills/talking-head-motion-workflow/references/reference-library.md +41 -0
- package/skills/talking-head-motion-workflow/references/safe-areas-and-sound.md +47 -0
- package/skills/talking-head-motion-workflow/references/scene-illustrations.md +19 -0
- package/skills/talking-head-motion-workflow/references/sfx-presets.md +57 -0
- package/skills/talking-head-motion-workflow/references/style-13-github-production.md +33 -0
- package/skills/talking-head-motion-workflow/references/style-13-scene-performance.md +76 -0
- package/skills/talking-head-motion-workflow/references/style-presets.md +111 -0
- package/skills/talking-head-motion-workflow/references/table-video-spatial-mg.md +36 -0
- package/skills/talking-head-motion-workflow/scripts/extract_docx.py +19 -0
- package/skills/talking-head-motion-workflow/scripts/generate_foley.py +93 -0
- package/skills/talking-head-motion-workflow/scripts/render_sfx.py +135 -0
- package/skills/video-production-review/SKILL.md +30 -0
- package/skills/video-production-review/agents/openai.yaml +2 -0
- package/skills/video-production-review/references/episode-review-desk.md +30 -0
- package/skills/video-production-review/references/motion-adjustment-review.md +21 -0
- package/skills/video-production-workflow/SKILL.md +76 -0
- package/skills/video-production-workflow/agents/openai.yaml +2 -0
- package/skills/video-production-workflow/references/table-video-ep09.md +17 -0
- package/skills/video-production-workflow/scripts/check_motion_project.py +83 -0
- package/skills/video-reference-analysis/SKILL.md +32 -0
- package/skills/video-reference-analysis/agents/openai.yaml +2 -0
- package/skills/video-reference-analysis/references/case-format.md +95 -0
- package/skills/video-reference-analysis/references/production-method.md +21 -0
- package/skills/video-reference-analysis/scripts/library.py +371 -0
- package/skills/video-resource-access/SKILL.md +35 -0
- package/skills/video-resource-access/online.enabled +1 -0
- package/skills/video-resource-access/scripts/asset_client.py +159 -0
- package/skills/video-resource-access/scripts/remote_library.py +50 -0
- package//345/256/211/350/243/205/350/257/264/346/230/216.txt +5 -0
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Save an already chosen image. This is not a webpage scraper or a visual approval tool.
|
|
4
|
+
import fs from 'node:fs/promises';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import crypto from 'node:crypto';
|
|
7
|
+
import {fileURLToPath} from 'node:url';
|
|
8
|
+
|
|
9
|
+
export const LIMITS = Object.freeze({bytes: 20 * 1024 * 1024, pixels: 40_000_000, dimension: 32768, timeoutMs: 15000, redirects: 5});
|
|
10
|
+
const EXTENSIONS = ['png', 'jpg', 'jpeg', 'webp', 'gif'];
|
|
11
|
+
const DEFAULT_DIRECTORY = '03_视觉设计/参考图';
|
|
12
|
+
const need = (condition, code, message) => { if (!condition) throw Object.assign(new Error(message), {code}); };
|
|
13
|
+
const hash = bytes => crypto.createHash('sha256').update(bytes).digest('hex');
|
|
14
|
+
const within = (root, target) => { const rel = path.relative(root, target); return rel === '' || (!path.isAbsolute(rel) && rel !== '..' && !rel.startsWith('..' + path.sep)); };
|
|
15
|
+
const exists = async p => { try { return await fs.lstat(p); } catch (e) { if (e.code === 'ENOENT') return null; throw e; } };
|
|
16
|
+
|
|
17
|
+
/** Provenance deliberately omits credentials, all query parameters and fragments. */
|
|
18
|
+
export function safeSourceUrl(input) {
|
|
19
|
+
let u; try { u = new URL(input); } catch { throw Object.assign(new Error('来源页必须是 HTTP(S) URL。'), {code:'SOURCE_URL'}); }
|
|
20
|
+
need(['http:', 'https:'].includes(u.protocol), 'SOURCE_URL', '来源页必须是 HTTP(S) URL。');
|
|
21
|
+
u.username = ''; u.password = ''; u.search = ''; u.hash = '';
|
|
22
|
+
return u.toString();
|
|
23
|
+
}
|
|
24
|
+
function requestUrl(input) {
|
|
25
|
+
let u; try { u = new URL(input); } catch { throw Object.assign(new Error('图像 URL 无效。'), {code:'INPUT_URL'}); }
|
|
26
|
+
need(['http:', 'https:'].includes(u.protocol), 'INPUT_URL', '只支持 HTTP(S) 图像 URL。');
|
|
27
|
+
need(!u.username && !u.password, 'URL_CREDENTIALS', '不接受含用户名或密码的下载 URL;请使用可公开访问的图像地址。');
|
|
28
|
+
u.hash = '';
|
|
29
|
+
return u;
|
|
30
|
+
}
|
|
31
|
+
function dimensions(width, height) {
|
|
32
|
+
need(Number.isInteger(width) && Number.isInteger(height) && width > 0 && height > 0, 'IMAGE_DIMENSIONS', '图像尺寸无效。');
|
|
33
|
+
need(width <= LIMITS.dimension && height <= LIMITS.dimension && width * height <= LIMITS.pixels, 'IMAGE_TOO_LARGE', '图像尺寸超过本工具的安全上限。');
|
|
34
|
+
}
|
|
35
|
+
const crcTable = Uint32Array.from({length:256}, (_, n) => { for (let k=0;k<8;k++) n = n & 1 ? 0xedb88320 ^ (n >>> 1) : n >>> 1; return n >>> 0; });
|
|
36
|
+
function crc32(b) { let c=0xffffffff; for (const v of b) c=crcTable[(c^v)&255]^(c>>>8); return (c^0xffffffff)>>>0; }
|
|
37
|
+
|
|
38
|
+
function png(b) {
|
|
39
|
+
let offset=8, width, height, imageData=false, ended=false, chunks=0;
|
|
40
|
+
while (offset < b.length) {
|
|
41
|
+
need(offset+12 <= b.length, 'IMAGE_TRUNCATED', 'PNG 块头或结尾被截断。');
|
|
42
|
+
const length=b.readUInt32BE(offset), end=offset+12+length;
|
|
43
|
+
need(end <= b.length, 'IMAGE_TRUNCATED', 'PNG 块数据被截断。');
|
|
44
|
+
const type=b.toString('ascii',offset+4,offset+8);
|
|
45
|
+
need(/^[A-Za-z]{4}$/.test(type), 'IMAGE_STRUCTURE', 'PNG 块类型无效。');
|
|
46
|
+
need(b.readUInt32BE(end-4)===crc32(b.subarray(offset+4,end-4)), 'IMAGE_STRUCTURE', 'PNG 块校验不一致。');
|
|
47
|
+
if (chunks++===0) {
|
|
48
|
+
need(type==='IHDR' && length===13, 'IMAGE_STRUCTURE', 'PNG 缺少完整 IHDR。');
|
|
49
|
+
width=b.readUInt32BE(offset+8); height=b.readUInt32BE(offset+12); dimensions(width,height);
|
|
50
|
+
const depth=b[offset+16], color=b[offset+17], permitted={0:[1,2,4,8,16],2:[8,16],3:[1,2,4,8],4:[8,16],6:[8,16]};
|
|
51
|
+
need(permitted[color]?.includes(depth) && b[offset+18]===0 && b[offset+19]===0 && b[offset+20]<=1, 'IMAGE_STRUCTURE', 'PNG IHDR 参数无效。');
|
|
52
|
+
} else need(type!=='IHDR', 'IMAGE_STRUCTURE', 'PNG 出现重复 IHDR。');
|
|
53
|
+
if (type==='IDAT' && length>0) imageData=true;
|
|
54
|
+
if (type==='IEND') { need(length===0 && end===b.length, 'IMAGE_STRUCTURE', 'PNG 结尾无效或包含额外数据。'); ended=true; break; }
|
|
55
|
+
offset=end;
|
|
56
|
+
}
|
|
57
|
+
need(ended && imageData, 'IMAGE_TRUNCATED', 'PNG 缺少图像数据或 IEND。');
|
|
58
|
+
return {format:'png',extension:'png',width,height};
|
|
59
|
+
}
|
|
60
|
+
function jpeg(b) {
|
|
61
|
+
let offset=2,width,height,scan=false,ended=false;
|
|
62
|
+
const sof=new Set([0xc0,0xc1,0xc2,0xc3,0xc5,0xc6,0xc7,0xc9,0xca,0xcb,0xcd,0xce,0xcf]);
|
|
63
|
+
while(offset<b.length) {
|
|
64
|
+
need(b[offset]===0xff, 'IMAGE_STRUCTURE', 'JPEG 标记结构无效。');
|
|
65
|
+
while(offset<b.length && b[offset]===0xff) offset++;
|
|
66
|
+
need(offset<b.length, 'IMAGE_TRUNCATED', 'JPEG 标记被截断。');
|
|
67
|
+
const marker=b[offset++];
|
|
68
|
+
if(marker===0xd9) { ended=true; need(offset===b.length, 'IMAGE_STRUCTURE', 'JPEG EOI 后有额外数据。'); break; }
|
|
69
|
+
need(marker!==0 && marker!==0xd8 && !(marker>=0xd0&&marker<=0xd7), 'IMAGE_STRUCTURE', 'JPEG 标记位置无效。');
|
|
70
|
+
if(marker===1) continue;
|
|
71
|
+
need(offset+2<=b.length, 'IMAGE_TRUNCATED', 'JPEG 段长被截断。');
|
|
72
|
+
const length=b.readUInt16BE(offset), end=offset+length;
|
|
73
|
+
need(length>=2 && end<=b.length, 'IMAGE_TRUNCATED', 'JPEG 段数据被截断。');
|
|
74
|
+
if(sof.has(marker)) {
|
|
75
|
+
need(length>=8 && length===8+3*b[offset+7], 'IMAGE_STRUCTURE', 'JPEG SOF 结构无效。');
|
|
76
|
+
const h=b.readUInt16BE(offset+3),w=b.readUInt16BE(offset+5); dimensions(w,h);
|
|
77
|
+
need(!width || (width===w&&height===h), 'IMAGE_STRUCTURE', 'JPEG 帧尺寸不一致。'); width=w;height=h;
|
|
78
|
+
}
|
|
79
|
+
if(marker===0xda) {
|
|
80
|
+
need(width && length>=6 && length===6+2*b[offset+2], 'IMAGE_STRUCTURE', 'JPEG SOS 结构无效。'); scan=true;
|
|
81
|
+
offset=end;
|
|
82
|
+
while(offset<b.length) {
|
|
83
|
+
if(b[offset]!==0xff) { offset++;continue; }
|
|
84
|
+
let next=offset+1;while(next<b.length&&b[next]===0xff)next++;
|
|
85
|
+
need(next<b.length, 'IMAGE_TRUNCATED', 'JPEG 扫描数据被截断。');
|
|
86
|
+
if(b[next]===0 || (b[next]>=0xd0&&b[next]<=0xd7)) {offset=next+1;continue;}
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
} else offset=end;
|
|
90
|
+
}
|
|
91
|
+
need(ended&&scan&&width, 'IMAGE_TRUNCATED', 'JPEG 缺少图像帧、扫描数据或 EOI。');
|
|
92
|
+
return {format:'jpeg',extension:'jpg',width,height};
|
|
93
|
+
}
|
|
94
|
+
function webp(b) {
|
|
95
|
+
need(b.length>=20 && b.readUInt32LE(4)+8===b.length, 'IMAGE_TRUNCATED', 'WebP RIFF 长度不完整或不一致。');
|
|
96
|
+
let offset=12,width,height,frame=false;
|
|
97
|
+
while(offset<b.length) {
|
|
98
|
+
need(offset+8<=b.length, 'IMAGE_TRUNCATED', 'WebP 块头被截断。');
|
|
99
|
+
const type=b.toString('ascii',offset,offset+4),length=b.readUInt32LE(offset+4),start=offset+8,end=start+length,padded=end+(length%2);
|
|
100
|
+
need(padded<=b.length, 'IMAGE_TRUNCATED', 'WebP 块数据被截断。');
|
|
101
|
+
if(type==='VP8X') {need(length===10, 'IMAGE_STRUCTURE', 'WebP VP8X 结构无效。');width=b.readUIntLE(start+4,3)+1;height=b.readUIntLE(start+7,3)+1;dimensions(width,height);}
|
|
102
|
+
if(type==='VP8 ') {
|
|
103
|
+
need(length>10 && (b[start]&1)===0 && b.subarray(start+3,start+6).equals(Buffer.from([0x9d,0x01,0x2a])), 'IMAGE_STRUCTURE', 'WebP VP8 帧头无效。');
|
|
104
|
+
const w=b.readUInt16LE(start+6)&0x3fff,h=b.readUInt16LE(start+8)&0x3fff;dimensions(w,h);if(!width){width=w;height=h;}frame=true;
|
|
105
|
+
}
|
|
106
|
+
if(type==='VP8L') {need(length>5 && b[start]===0x2f && (b[start+4]&0xe0)===0, 'IMAGE_STRUCTURE', 'WebP VP8L 帧头无效。');const bits=b.readUInt32LE(start+1),w=(bits&0x3fff)+1,h=((bits>>>14)&0x3fff)+1;dimensions(w,h);if(!width){width=w;height=h;}frame=true;}
|
|
107
|
+
if(type==='ANMF') {
|
|
108
|
+
need(width && length>=24, 'IMAGE_STRUCTURE', 'WebP 动画帧头无效。');
|
|
109
|
+
const x=b.readUIntLE(start,3)*2,y=b.readUIntLE(start+3,3)*2,w=b.readUIntLE(start+6,3)+1,h=b.readUIntLE(start+9,3)+1;
|
|
110
|
+
need(x+w<=width&&y+h<=height, 'IMAGE_STRUCTURE', 'WebP 动画帧越出画布。');
|
|
111
|
+
let at=start+16,hasFrame=false;
|
|
112
|
+
while(at<end) {need(at+8<=end, 'IMAGE_TRUNCATED', 'WebP 动画子块被截断。');const sub=b.toString('ascii',at,at+4),n=b.readUInt32LE(at+4);at+=8+n+(n%2);need(at<=end, 'IMAGE_TRUNCATED', 'WebP 动画子块数据被截断。');if(['VP8 ','VP8L'].includes(sub))hasFrame=true;}
|
|
113
|
+
need(hasFrame, 'IMAGE_STRUCTURE', 'WebP 动画没有图像子块。');frame=true;
|
|
114
|
+
}
|
|
115
|
+
offset=padded;
|
|
116
|
+
}
|
|
117
|
+
need(frame&&width, 'IMAGE_STRUCTURE', 'WebP 没有可识别图像帧。');
|
|
118
|
+
return {format:'webp',extension:'webp',width,height};
|
|
119
|
+
}
|
|
120
|
+
function gif(b) {
|
|
121
|
+
need(b.length>=14, 'IMAGE_TRUNCATED', 'GIF 文件头被截断。');
|
|
122
|
+
const width=b.readUInt16LE(6),height=b.readUInt16LE(8);dimensions(width,height);
|
|
123
|
+
let offset=13,frames=0,ended=false;
|
|
124
|
+
if(b[10]&0x80) offset+=3*(1<<((b[10]&7)+1));
|
|
125
|
+
need(offset<=b.length, 'IMAGE_TRUNCATED', 'GIF 全局调色板被截断。');
|
|
126
|
+
const subBlocks=()=>{let bytes=0;while(true){need(offset<b.length,'IMAGE_TRUNCATED','GIF 子块被截断。');const n=b[offset++];if(n===0)return bytes;need(offset+n<=b.length,'IMAGE_TRUNCATED','GIF 子块数据被截断。');bytes+=n;offset+=n;}};
|
|
127
|
+
while(offset<b.length) {
|
|
128
|
+
const type=b[offset++];
|
|
129
|
+
if(type===0x3b) {need(offset===b.length,'IMAGE_STRUCTURE','GIF 结束标记后有额外数据。');ended=true;break;}
|
|
130
|
+
if(type===0x21) {need(offset<b.length,'IMAGE_TRUNCATED','GIF 扩展被截断。');offset++;subBlocks();continue;}
|
|
131
|
+
need(type===0x2c,'IMAGE_STRUCTURE','GIF 数据块类型无效。');
|
|
132
|
+
need(offset+9<=b.length,'IMAGE_TRUNCATED','GIF 图像描述符被截断。');
|
|
133
|
+
const x=b.readUInt16LE(offset),y=b.readUInt16LE(offset+2),w=b.readUInt16LE(offset+4),h=b.readUInt16LE(offset+6),flags=b[offset+8];
|
|
134
|
+
need(w>0&&h>0&&x+w<=width&&y+h<=height,'IMAGE_STRUCTURE','GIF 图像尺寸越界。');offset+=9;
|
|
135
|
+
if(flags&0x80)offset+=3*(1<<((flags&7)+1));
|
|
136
|
+
need(offset<b.length,'IMAGE_TRUNCATED','GIF 局部调色板或图像数据被截断。');
|
|
137
|
+
const lzw=b[offset++];need(lzw>=2&&lzw<=8,'IMAGE_STRUCTURE','GIF LZW 参数无效。');need(subBlocks()>0,'IMAGE_STRUCTURE','GIF 缺少图像数据。');frames++;
|
|
138
|
+
}
|
|
139
|
+
need(ended&&frames>0,'IMAGE_TRUNCATED','GIF 缺少图像或结束标记。');
|
|
140
|
+
return {format:'gif',extension:'gif',width,height};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** Container/header validation only. Pixel decoding and actual viewing remain separate. */
|
|
144
|
+
export function inspectImageHeader(bytes) {
|
|
145
|
+
need(Buffer.isBuffer(bytes),'IMAGE_INPUT','图像输入必须是字节。');
|
|
146
|
+
need(bytes.length>0&&bytes.length<=LIMITS.bytes,'IMAGE_TOO_LARGE','图像为空或超过20 MiB上限。');
|
|
147
|
+
let result;
|
|
148
|
+
if(bytes.subarray(0,8).equals(Buffer.from([137,80,78,71,13,10,26,10]))) result=png(bytes);
|
|
149
|
+
else if(bytes[0]===0xff&&bytes[1]===0xd8) result=jpeg(bytes);
|
|
150
|
+
else if(bytes.toString('ascii',0,4)==='RIFF'&&bytes.toString('ascii',8,12)==='WEBP') result=webp(bytes);
|
|
151
|
+
else if(['GIF87a','GIF89a'].includes(bytes.toString('ascii',0,6))) result=gif(bytes);
|
|
152
|
+
else throw Object.assign(new Error('不是支持的真实图像头;仅接受 PNG、JPEG、WebP、GIF,不接受网页、SVG或检索摘要。'),{code:'IMAGE_FORMAT'});
|
|
153
|
+
return {...result,bytes:bytes.length,sha256:hash(bytes),validation:'header-valid',decoded:false,viewed:false};
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
async function download(input, timeoutMs=LIMITS.timeoutMs) {
|
|
157
|
+
need(typeof fetch==='function','RUNTIME_FETCH','当前 Node.js 没有内置 fetch;请使用已可用的支持该功能的运行时,不自动安装。');
|
|
158
|
+
let u=requestUrl(input);const requested=safeSourceUrl(u),controller=new AbortController();
|
|
159
|
+
// Shorter timeouts are useful to callers/tests; callers cannot remove the bound.
|
|
160
|
+
need(Number.isFinite(timeoutMs)&&timeoutMs>0&&timeoutMs<=LIMITS.timeoutMs,'TIMEOUT_OPTION','下载超时必须在1–15000毫秒内。');
|
|
161
|
+
const timer=setTimeout(()=>controller.abort(),timeoutMs);let redirects=0;
|
|
162
|
+
try {
|
|
163
|
+
while(true) {
|
|
164
|
+
const response=await fetch(u,{redirect:'manual',credentials:'omit',headers:{Accept:'image/png,image/jpeg,image/webp,image/gif,application/octet-stream;q=0.5'},signal:controller.signal});
|
|
165
|
+
if([301,302,303,307,308].includes(response.status)) {
|
|
166
|
+
const location=response.headers.get('location');await response.body?.cancel();
|
|
167
|
+
need(location&&redirects<LIMITS.redirects,'HTTP_REDIRECT','重定向缺少目标或次数超过上限。');
|
|
168
|
+
u=requestUrl(new URL(location,u));redirects++;continue;
|
|
169
|
+
}
|
|
170
|
+
if(!response.ok) {await response.body?.cancel();throw Object.assign(new Error(`图像请求返回HTTP ${response.status};未保存图像。`),{code:'HTTP_STATUS'});}
|
|
171
|
+
const mime=(response.headers.get('content-type')||'').split(';')[0].trim().toLowerCase();
|
|
172
|
+
if(['text/html','application/xhtml+xml'].includes(mime)) {await response.body?.cancel();throw Object.assign(new Error('地址返回网页而不是图像;未抓取页面内链。'),{code:'HTTP_HTML'});}
|
|
173
|
+
const declared=Number(response.headers.get('content-length'));
|
|
174
|
+
if(declared>LIMITS.bytes) {await response.body?.cancel();throw Object.assign(new Error('图像响应超过20 MiB上限。'),{code:'IMAGE_TOO_LARGE'});}
|
|
175
|
+
need(response.body,'HTTP_EMPTY','图像响应没有内容。');const chunks=[];let size=0;
|
|
176
|
+
for await(const chunk of response.body) {size+=chunk.length;if(size>LIMITS.bytes){controller.abort();throw Object.assign(new Error('图像响应超过20 MiB上限。'),{code:'IMAGE_TOO_LARGE'});}chunks.push(Buffer.from(chunk));}
|
|
177
|
+
return {bytes:Buffer.concat(chunks),source:{type:'http',requestedUrl:requested,finalUrl:safeSourceUrl(u),redirects,reportedContentType:mime||null,urlQueryAndFragmentOmitted:true}};
|
|
178
|
+
}
|
|
179
|
+
} catch(e) {
|
|
180
|
+
if(typeof e.code==='string'&&['HTTP_REDIRECT','URL_CREDENTIALS','INPUT_URL','HTTP_STATUS','HTTP_HTML','IMAGE_TOO_LARGE','HTTP_EMPTY','TIMEOUT_OPTION'].includes(e.code)) throw e;
|
|
181
|
+
throw Object.assign(new Error(controller.signal.aborted?'图像请求超时;未保存图像。':'图像请求失败;未保存图像。'),{code:controller.signal.aborted?'HTTP_TIMEOUT':'HTTP_FETCH'});
|
|
182
|
+
} finally {clearTimeout(timer);}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
async function outputDirectory(project, relative) {
|
|
186
|
+
need(typeof project==='string'&&project.length>0,'PROJECT_REQUIRED','必须指定项目目录。');
|
|
187
|
+
const absolute=path.resolve(project),stat=await fs.stat(absolute);need(stat.isDirectory(),'PROJECT_DIRECTORY','项目路径必须是已有目录。');
|
|
188
|
+
const root=await fs.realpath(absolute);
|
|
189
|
+
need(typeof relative==='string'&&relative.length>0&&!path.isAbsolute(relative)&&!path.win32.isAbsolute(relative),'OUTPUT_PATH','输出目录必须是项目内相对路径。');
|
|
190
|
+
const parts=relative.split(/[\\/]/).filter(x=>x&&x!=='.');
|
|
191
|
+
need(!parts.includes('..')&&parts.every(x=>!/[\x00-\x1f:*?"<>|]/.test(x)&&!/[ .]$/.test(x)),'OUTPUT_PATH','输出目录含无效字符或上级路径。');
|
|
192
|
+
let current=root;
|
|
193
|
+
for(const part of parts) {
|
|
194
|
+
current=path.join(current,part);let s=await exists(current);
|
|
195
|
+
if(!s) {try{await fs.mkdir(current);}catch(e){if(e.code!=='EEXIST')throw e;}s=await fs.lstat(current);}
|
|
196
|
+
const real=await fs.realpath(current);need(within(root,real),'PATH_ESCAPE','输出目录经链接解析后越出项目;未写入图像。');
|
|
197
|
+
need((await fs.stat(current)).isDirectory(),'OUTPUT_DIRECTORY','输出路径包含非目录对象。');current=real;
|
|
198
|
+
}
|
|
199
|
+
return {root,directory:current};
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export async function saveReferenceImage({project,input,name,sourcePage,directory=DEFAULT_DIRECTORY,timeoutMs=LIMITS.timeoutMs}) {
|
|
203
|
+
need(typeof name==='string'&&/^[\p{L}\p{N}][\p{L}\p{N}_-]{0,79}$/u.test(name)&&!/^(con|prn|aux|nul|com[1-9]|lpt[1-9])$/i.test(name),'NAME','名称只用字母、汉字、数字、短横线或下划线,不带扩展名。');
|
|
204
|
+
need(typeof input==='string'&&input.length>0&&input.length<=8192,'INPUT','必须提供图像 URL 或本地路径。');
|
|
205
|
+
const page=sourcePage?safeSourceUrl(sourcePage):null;
|
|
206
|
+
// Resolve all output parents before reading/downloading; never write through an escaping junction.
|
|
207
|
+
const output=await outputDirectory(project,directory);
|
|
208
|
+
let loaded;
|
|
209
|
+
if(/^https?:/i.test(input)) loaded=await download(input,timeoutMs);
|
|
210
|
+
else {
|
|
211
|
+
need(!/^[a-z][a-z0-9+.-]*:\/\//i.test(input)&&!/^data:/i.test(input),'INPUT_URL','不支持此输入协议;本地文件请使用实际路径。');
|
|
212
|
+
const local=path.resolve(input),stat=await fs.stat(local);need(stat.isFile(),'LOCAL_FILE','本地输入必须是文件。');
|
|
213
|
+
need(stat.size>0&&stat.size<=LIMITS.bytes,'IMAGE_TOO_LARGE','本地图像为空或超过20 MiB上限。');
|
|
214
|
+
loaded={bytes:await fs.readFile(local),source:{type:'local',path:local}};
|
|
215
|
+
}
|
|
216
|
+
const details=inspectImageHeader(loaded.bytes),imagePath=path.join(output.directory,`${name}.${details.extension}`),sourcePath=path.join(output.directory,`${name}.source.json`);
|
|
217
|
+
// Recheck the resolved directory immediately before the exclusive writes.
|
|
218
|
+
need(within(output.root,await fs.realpath(output.directory)),'PATH_ESCAPE','输出目录已改变或越出项目。');
|
|
219
|
+
for(const ext of EXTENSIONS) {
|
|
220
|
+
const other=path.join(output.directory,`${name}.${ext}`),s=await exists(other);
|
|
221
|
+
if(s) need(other===imagePath&&!s.isSymbolicLink()&&s.isFile(),'OUTPUT_CONFLICT','同名候选已有其他文件或链接;请换名称,不覆盖。');
|
|
222
|
+
}
|
|
223
|
+
const old=await exists(imagePath),oldSource=await exists(sourcePath);
|
|
224
|
+
need(!oldSource||(!oldSource.isSymbolicLink()&&oldSource.isFile()),'OUTPUT_CONFLICT','来源记录已存在且不是普通文件。');
|
|
225
|
+
if(old) need(hash(await fs.readFile(imagePath))===details.sha256,'OUTPUT_CONFLICT','同名图像内容不同;请换名称,不覆盖旧候选。');
|
|
226
|
+
else need(!oldSource,'OUTPUT_CONFLICT','同名来源记录已存在但图像缺失;请检查旧候选后换名称。');
|
|
227
|
+
let provenance;
|
|
228
|
+
if(oldSource) {
|
|
229
|
+
try{provenance=JSON.parse(await fs.readFile(sourcePath,'utf8'));}catch{throw Object.assign(new Error('已有来源记录无法读取;未覆盖。'),{code:'OUTPUT_CONFLICT'});}
|
|
230
|
+
need(provenance.schemaVersion===1&&['local','http'].includes(provenance.source?.type)&&provenance.image?.sha256===details.sha256&&provenance.image?.file===path.basename(imagePath),'OUTPUT_CONFLICT','已有来源记录与图片不一致;未覆盖。');
|
|
231
|
+
for(const key of ['requestedUrl','finalUrl','pageUrl']) if(provenance.source[key]) need(provenance.source[key]===safeSourceUrl(provenance.source[key]),'OUTPUT_CONFLICT','已有来源记录含未清理的URL信息;未覆盖或输出该记录。');
|
|
232
|
+
} else {
|
|
233
|
+
provenance={schemaVersion:1,savedAt:new Date().toISOString(),source:{...loaded.source,...(page?{pageUrl:page,pageVerification:'provided-not-verified'}:{})},image:{file:path.basename(imagePath),...details},purpose:'candidate-reference-only',note:'仅结构及文件头校验。必须用实际图像工具打开检查,再从本地交审;保存不代表用户选定、可用于成片或允许再分发。'};
|
|
234
|
+
if(!old) await fs.writeFile(imagePath,loaded.bytes,{flag:'wx'});
|
|
235
|
+
await fs.writeFile(sourcePath,JSON.stringify(provenance,null,2)+'\n',{encoding:'utf8',flag:'wx'});
|
|
236
|
+
}
|
|
237
|
+
// Never echo unknown fields from an existing provenance file (it may have been edited elsewhere).
|
|
238
|
+
const visibleSource={};for(const key of ['type','path','requestedUrl','finalUrl','redirects','reportedContentType','urlQueryAndFragmentOmitted','pageUrl','pageVerification'])if(Object.hasOwn(provenance.source,key))visibleSource[key]=provenance.source[key];
|
|
239
|
+
return {status:old?'existing':'saved',path:imagePath,relativePath:path.relative(output.root,imagePath).split(path.sep).join('/'),sourceRecord:sourcePath,...details,source:visibleSource,next:'Open the local image with an image-viewing tool; verify visible content and show its local path. No visual approval recorded.'};
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
async function main() {
|
|
243
|
+
const args=process.argv.slice(2);
|
|
244
|
+
if(args.length===1&&args[0]==='--help') {console.log('node scripts/save-reference-image.mjs --project ROOT --input URL_OR_LOCAL_PATH --name ref-01 [--source-page URL] [--directory PROJECT_RELATIVE_DIRECTORY]\nDefault: 03_视觉设计/参考图. Existing Node.js only. Downloads/copies the chosen image; no webpage scraping, auth, state initialization or visual approval.');return;}
|
|
245
|
+
const allowed=new Set(['project','input','name','source-page','directory']),options={};
|
|
246
|
+
for(let i=0;i<args.length;i+=2) {const key=args[i].slice(2);need(args[i].startsWith('--')&&allowed.has(key)&&args[i+1]!==undefined&&!Object.hasOwn(options,key),'ARGUMENT','参数无效或重复;使用 --help 查看用法。');options[key]=args[i+1];}
|
|
247
|
+
const result=await saveReferenceImage({...options,sourcePage:options['source-page']});console.log(JSON.stringify(result,null,2));
|
|
248
|
+
}
|
|
249
|
+
if(process.argv[1]&&path.resolve(process.argv[1])===fileURLToPath(import.meta.url)) main().catch(e=>{const code=typeof e.code==='string'?e.code:'IO_ERROR';console.error(JSON.stringify({status:'failed',code,message:code.startsWith('E')?'本地文件操作失败;未覆盖已有候选。':e.message}));process.exitCode=1;});
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
export class WorkflowError extends Error {
|
|
3
|
+
constructor(code, message) { super(message); this.code = code; }
|
|
4
|
+
}
|
|
5
|
+
export function requireValue(condition, code, message) {
|
|
6
|
+
if (!condition) throw new WorkflowError(code, message);
|
|
7
|
+
}
|
|
8
|
+
export function text(value, name) {
|
|
9
|
+
requireValue(typeof value === 'string' && value.trim().length > 0, 'INVALID_INPUT', name + ' must be a non-empty string');
|
|
10
|
+
return value;
|
|
11
|
+
}
|
|
12
|
+
export function id(value, name = 'id') {
|
|
13
|
+
text(value, name);
|
|
14
|
+
requireValue(/^[A-Za-z0-9][A-Za-z0-9_.:-]{0,119}$/.test(value), 'INVALID_ID', name + ' has unsupported characters');
|
|
15
|
+
return value;
|
|
16
|
+
}
|
|
17
|
+
export const SLOTS = ['script', 'narration', 'design', 'storyboard', 'visualOverview', 'mainProject', 'finalVideo'];
|
|
18
|
+
export const KINDS = ['script', 'narration', 'design', 'storyboard', 'static', 'pilot', 'video', 'asset', 'config', 'other'];
|
|
19
|
+
export const ACTIONS = ['storyboard', 'design', 'animate', 'batch-animate', 'pilot', 'picture-lock', 'final', 'publish', ...SLOTS.map(s => 'adopt:' + s)];
|
|
20
|
+
export const RESULTS = ['通过', '未通过', '未验证', '不适用'];
|
|
21
|
+
export const METHODS = ['technical', 'human-view', 'human-listen', 'inspection'];
|
|
22
|
+
export const PAYLOADS = {
|
|
23
|
+
'risk-proof': ['riskObjectId', 'pilotObjectId'],
|
|
24
|
+
'narration-gain-adopt': ['objectId', 'approvalId', 'evidence'],
|
|
25
|
+
choices: ['partial'], register: ['id', 'path', 'kind'], dependency: ['from', 'to'],
|
|
26
|
+
review: ['id', 'objectId', 'aspect', 'allows'], approve: ['reviewId', 'decision', 'evidence'],
|
|
27
|
+
adopt: ['slot', 'objectId', 'approvalId'], qa: ['checkId', 'objectId', 'aspect', 'result', 'method', 'evidence'],
|
|
28
|
+
stage: ['stage'], rebind: ['objectId', 'path'], revoke: ['approvalId'],
|
|
29
|
+
segment: ['id', 'objectId', 'narrative', 'anchors', 'route'], finalize: ['mode'],
|
|
30
|
+
};
|
|
31
|
+
// Deliberately small, dependency-free validator for the bundled schemas.
|
|
32
|
+
export function checkSchema(value, schema, at = '$') {
|
|
33
|
+
if (schema.type) {
|
|
34
|
+
const valid = schema.type === 'object' ? value !== null && typeof value === 'object' && !Array.isArray(value)
|
|
35
|
+
: schema.type === 'array' ? Array.isArray(value)
|
|
36
|
+
: schema.type === 'integer' ? Number.isSafeInteger(value)
|
|
37
|
+
: typeof value === schema.type;
|
|
38
|
+
requireValue(valid, 'SCHEMA', at + ' must be ' + schema.type);
|
|
39
|
+
}
|
|
40
|
+
if (schema.enum) requireValue(schema.enum.includes(value), 'SCHEMA', at + ' is outside enum');
|
|
41
|
+
if (schema.minimum !== undefined) requireValue(value >= schema.minimum, 'SCHEMA', at + ' below minimum');
|
|
42
|
+
if (schema.minLength !== undefined) requireValue(value.length >= schema.minLength, 'SCHEMA', at + ' empty');
|
|
43
|
+
if (schema.minItems !== undefined) requireValue(value.length >= schema.minItems, 'SCHEMA', at + ' has too few items');
|
|
44
|
+
if (schema.required) for (const key of schema.required) requireValue(Object.hasOwn(value, key), 'SCHEMA', at + '.' + key + ' required');
|
|
45
|
+
if (schema.properties) for (const [key, child] of Object.entries(value)) {
|
|
46
|
+
if (schema.properties[key]) checkSchema(child, schema.properties[key], at + '.' + key);
|
|
47
|
+
else if (schema.additionalProperties === false) throw new WorkflowError('SCHEMA', at + '.' + key + ' unsupported');
|
|
48
|
+
}
|
|
49
|
+
if (schema.items) value.forEach((v, i) => checkSchema(v, schema.items, at + '[' + i + ']'));
|
|
50
|
+
}
|
|
51
|
+
export async function validateState(state) {
|
|
52
|
+
const schema = JSON.parse(await fs.readFile(new URL('../schemas/state.schema.json', import.meta.url), 'utf8'));
|
|
53
|
+
checkSchema(state, schema);
|
|
54
|
+
for (const [key, artifact] of Object.entries(state.artifacts)) {
|
|
55
|
+
id(key); requireValue(artifact.id === key, 'SCHEMA', 'artifact key mismatch');
|
|
56
|
+
text(artifact.path, 'artifact.path'); requireValue(KINDS.includes(artifact.kind), 'SCHEMA', 'artifact.kind invalid');
|
|
57
|
+
}
|
|
58
|
+
for (const dep of state.dependencies) {
|
|
59
|
+
requireValue(state.artifacts[dep.from] && state.artifacts[dep.to], 'SCHEMA', 'dependency points to missing object');
|
|
60
|
+
}
|
|
61
|
+
for (const slot of SLOTS) requireValue(state.adopted[slot] === null || typeof state.adopted[slot] === 'object', 'SCHEMA', 'adopted slot invalid');
|
|
62
|
+
return state;
|
|
63
|
+
}
|
|
64
|
+
export async function validateRequest(request) {
|
|
65
|
+
const schema = JSON.parse(await fs.readFile(new URL('../schemas/request.schema.json', import.meta.url), 'utf8'));
|
|
66
|
+
checkSchema(request, schema); id(request.operationId, 'operationId');
|
|
67
|
+
const required = PAYLOADS[request.type];
|
|
68
|
+
requireValue(required, 'INVALID_OPERATION', 'unsupported operation type');
|
|
69
|
+
for (const key of required) requireValue(Object.hasOwn(request.payload, key), 'INVALID_INPUT', request.type + '.' + key + ' required');
|
|
70
|
+
return request;
|
|
71
|
+
}
|