蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

独立输入与实现:splitting-probe.cjs

JAVASCRIPTWindows / Node24.22.1 / SwaggerUI5.34.1 / Edge156 / Python3.12 / 固定生成器历史兼容版本splitting-probe.cjs
splitting-probe.cjs3 行
const fs=require('node:fs');const path=require('node:path');const assert=require('node:assert/strict');const {fileURLToPath}=require('node:url');const Parser=require('@apidevtools/swagger-parser');const crypto=require('node:crypto');
(async()=>{const root=fs.realpathSync(path.join(__dirname,'specs/split'));const requested=[];function allowed(file){try{if(/^[a-z]+:/i.test(file.url)&&!/^file:/i.test(file.url)&&!/^\w:[\\/]/.test(file.url))return false;const name=fs.realpathSync(file.url.startsWith('file:')?fileURLToPath(file.url):file.url),rel=path.relative(root,name);return !rel.startsWith('..')&&!path.isAbsolute(rel)&&['.json','.yaml','.yml'].includes(path.extname(name))}catch{return false}}const options={resolve:{http:false,file:{order:1,canRead:allowed,read:file=>{assert(allowed(file),'Refused outside-owned reference');const name=fs.realpathSync(file.url.startsWith('file:')?fileURLToPath(file.url):file.url);requested.push(path.relative(root,name).replaceAll(path.sep,'/'));return fs.readFileSync(name,'utf8')}}}};const parser=new Parser(),bundled=await parser.bundle(path.join(root,'openapi.json'),options);await Parser.validate(bundled,{resolve:{external:false,file:false,http:false}});assert(bundled.components.schemas.Book.properties.isbn);assert(bundled.paths['/books'].get.operationId==='listBooks');let rejected=false;try{await new Parser().bundle(path.join(root,'bad-reference.json'),options)}catch{rejected=true}assert(rejected,'Outside-root reference must fail');const result={passed:true,owned_dependencies:[...new Set(requested)],outside_reference_rejected:true,operations:Object.values(bundled.paths).reduce((n,p)=>n+Object.keys(p).length,0),bundle_sha256:crypto.createHash('sha256').update(JSON.stringify(bundled)).digest('hex'),scope:'Actual owned JSON split/bundle and whitelist only; no remote resolution',source_scripts_executed:false};console.log(JSON.stringify(result))})().catch(e=>{console.error(e.stack);process.exitCode=1});
本例需在 Windows / Node24.22.1 / SwaggerUI5.34.1 / Edge156 / Python3.12 / 固定生成器历史兼容版本 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows / Node24.22.1 / SwaggerUI5.34.1 / Edge156 / Python3.12 / 固定生成器历史兼容版本

入口文件:projects/swagger-lab/splitting-probe.cjs

预期结果与验证条件观察提示
  • 按本章对应实际结果核对,未运行的其它环境仍待处理

请结合实际环境逐项观察。

附件与关联工程包下载

下载课程关联工程包:Swagger

课程关联工程包(登记关联13章) · 含当前章节或实例 · mbskycom202611a548.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习