示例
js-yaml 3.15.1: safeLoadAll
已验证示例 — npm js-yaml 3.15.1: safeLoadAll. contract 在 node 22 · linux alpine/x64 · docker 上运行并通过: safeLoadAll is exported as a function on js-yaml
sha256:bce1e63f31f0f00cfc7e22c2e4db0b26ad8478540676598211e1868cb1f2547a
本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。
提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。
MIT-0
执行证据
声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。
- 证据依据
- 签名契约通过
- 验证回执
- 1
- 构建过它的签名密钥
- 1
声明的环境
linux 24 · ubuntu · glibc 2.39 x64 npm
验证运行环境
| 环境 | 契约 | 阶段 | 运行日期 |
|---|---|---|---|
| node 22 · linux alpine/x64 · docker ed25519:c1973797be207ac4 | PASS | compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS CONTAINER_RUN · node-typescript@1node:22-alpine@sha256:c610fcdfb1d5… |
2026-08-30 |
案例
HOW- 目标
- verify js-yaml.safeLoadAll in pkg:npm/js-yaml@3.15.1
- 符号
-
- js-yaml.safeLoadAll
- 创建时间
- 2026-08-30T17:17:49Z
契约
- safeLoadAll is exported as a function on js-yaml
- safeLoadAll parses multi-document YAML string and returns array of documents when iterator is omitted
- safeLoadAll invokes iterator callback for each document in multi-document YAML string
- safeLoadAll returns empty array for empty string and array with null for comment-only document
- safeLoadAll accepts options object to specify parsing options such as filename
- safeLoadAll throws YAMLException when parsing malformed YAML document
- safeLoadAll throws YAMLException when encountering unsafe custom tags
文件
- PROMPT.md
- csx.json
- package-lock.json
- package.json
- spec.json
- src/index.js
- test/contract.mjs
源代码
Clean-room public code sample — generation instructions
Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.
A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.
Goal: verify js-yaml.safeLoadAll in pkg:npm/js-yaml@3.15.1
Kind: HOW
Use EXACTLY these public packages and versions:
- pkg:npm/js-yaml@3.15.1
Demonstrate these symbols/APIs:
- js-yaml.safeLoadAll
Rules:
- One focused purpose; the smallest project that proves the goal.
- Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
- Pin every dependency with a lockfile so resolution is reproducible.
- No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
- No personal names, emails, company names, or project identifiers of any kind.
- No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
- Keep it under 200 files and 256KB packed.
{"case":{"caseId":"case:sha256:74e156920ab18e27f962634d1a8020c1f1d1c7d40cf029bdd4fa1e693bae9684","contract":["safeLoadAll is exported as a function on js-yaml","safeLoadAll parses multi-document YAML string and returns array of documents when iterator is omitted","safeLoadAll invokes iterator callback for each document in multi-document YAML string","safeLoadAll returns empty array for empty string and array with null for comment-only document","safeLoadAll accepts options object to specify parsing options such as filename","safeLoadAll throws YAMLException when parsing malformed YAML document","safeLoadAll throws YAMLException when encountering unsafe custom tags"],"goal":"verify js-yaml.safeLoadAll in pkg:npm/js-yaml@3.15.1","kind":"HOW","packages":["pkg:npm/js-yaml@3.15.1"],"schemaVersion":1,"symbols":["js-yaml.safeLoadAll"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"npm","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/js-yaml@3.15.1"],"schemaVersion":1,"subject":"pkg:npm/js-yaml@3.15.1","symbols":["js-yaml.safeLoadAll"],"verifierAdapter":"node-typescript@1"}
{
"name": "sample-js-yaml-safeloadall",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "sample-js-yaml-safeloadall",
"version": "1.0.0",
"license": "MIT-0",
"dependencies": {
"js-yaml": "3.15.1"
}
},
"node_modules/argparse": {
"version": "1.0.10",
"resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz",
"integrity": "sha512-o5Roy6tNG4SL/FOkCAN6RzjiakZS25RLYFrcMttJqbdd8BWrnA+fGz57iN5Pb06pvBGvl5gQ0B48dJlslXvoTg==",
"license": "MIT",
"dependencies": {
"sprintf-js": "~1.0.2"
}
},
"node_modules/esprima": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/esprima/-/esprima-4.0.1.tgz",
"integrity": "sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==",
"license": "BSD-2-Clause",
"bin": {
"esparse": "bin/esparse.js",
"esvalidate": "bin/esvalidate.js"
},
"engines": {
"node": ">=4"
}
},
"node_modules/js-yaml": {
"version": "3.15.1",
"resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.15.1.tgz",
"integrity": "sha512-S99WuO3HlhO3XN41EtYUNl9zzXjoJx7QvmipxsJVxtCBT0YHEFy+iOJhjSvrmV12nYhWpZaM8lPHkJm0yUMbag==",
"license": "MIT",
"dependencies": {
"argparse": "^1.0.7",
"esprima": "^4.0.0"
},
"bin": {
"js-yaml": "bin/js-yaml.js"
}
},
"node_modules/sprintf-js": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz",
"integrity": "sha512-D9cPgkvLlV3t3IzL0D0YLvGA9Ahk4PcvVwUbN0dSGr1aP0Nrt4AEnTUbuGvquEC0mA64Gqt1fzirlRs5ibXx8g==",
"license": "BSD-3-Clause"
}
}
}
{
"name": "sample-js-yaml-safeloadall",
"version": "1.0.0",
"private": true,
"description": "Clean-room code sample for js-yaml.safeLoadAll in js-yaml@3.15.1",
"main": "src/index.js",
"license": "MIT-0",
"dependencies": {
"js-yaml": "3.15.1"
}
}
{
"schemaVersion": 1,
"goal": "verify js-yaml.safeLoadAll in pkg:npm/js-yaml@3.15.1",
"kind": "HOW",
"packages": [
"pkg:npm/js-yaml@3.15.1"
],
"symbols": [
"js-yaml.safeLoadAll"
]
}
const yaml = require('js-yaml');
/**
* Parses all documents from a YAML string using js-yaml.safeLoadAll.
*
* @param {string} input - YAML string containing one or more documents.
* @param {Function} [iterator] - Optional callback invoked for each document.
* @param {object} [options] - Optional parsing options.
* @returns {Array|undefined} Array of parsed documents when iterator is omitted.
*/
function parseAll(input, iterator, options) {
return yaml.safeLoadAll(input, iterator, options);
}
module.exports = {
yaml,
safeLoadAll: yaml.safeLoadAll,
parseAll
};
import assert from 'node:assert/strict';
import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);
const { yaml, safeLoadAll, parseAll } = require('../src/index.js');
async function runContractTests() {
// 1. safeLoadAll is exported as a function on js-yaml
assert.equal(typeof yaml.safeLoadAll, 'function', 'yaml.safeLoadAll must be a function');
assert.equal(typeof safeLoadAll, 'function', 'safeLoadAll must be a function');
assert.equal(typeof parseAll, 'function', 'parseAll helper must be a function');
// 2. safeLoadAll parses multi-document YAML string and returns array of documents when iterator is omitted
{
const multiDocYaml = `
version: 1
kind: Service
metadata:
name: web-service
---
version: 2
kind: Deployment
metadata:
name: web-deployment
spec:
replicas: 3
`;
const result = safeLoadAll(multiDocYaml);
assert.equal(Array.isArray(result), true, 'Result must be an array');
assert.equal(result.length, 2, 'Result must contain exactly 2 documents');
assert.deepEqual(result[0], {
version: 1,
kind: 'Service',
metadata: { name: 'web-service' }
});
assert.deepEqual(result[1], {
version: 2,
kind: 'Deployment',
metadata: { name: 'web-deployment' },
spec: { replicas: 3 }
});
}
// 3. safeLoadAll invokes iterator callback for each document in multi-document YAML string
{
const multiDocYaml = `
id: alpha
value: 100
---
id: beta
value: 200
---
id: gamma
value: 300
`;
const collected = [];
const ret = safeLoadAll(multiDocYaml, (doc) => {
collected.push(doc);
});
assert.equal(ret, undefined, 'safeLoadAll with iterator should return undefined');
assert.equal(collected.length, 3, 'Iterator must be called 3 times');
assert.deepEqual(collected[0], { id: 'alpha', value: 100 });
assert.deepEqual(collected[1], { id: 'beta', value: 200 });
assert.deepEqual(collected[2], { id: 'gamma', value: 300 });
}
// 4. safeLoadAll returns empty array for empty string and array with null for comment-only document
{
const emptyResult = safeLoadAll('');
assert.equal(Array.isArray(emptyResult), true, 'Empty YAML result must be an array');
assert.equal(emptyResult.length, 0, 'Empty YAML result must be empty array');
const commentOnlyResult = safeLoadAll('# Just a comment line\n');
assert.equal(Array.isArray(commentOnlyResult), true, 'Comment-only YAML result must be an array');
assert.equal(commentOnlyResult.length, 1, 'Comment-only YAML result must contain 1 document');
assert.equal(commentOnlyResult[0], null, 'Comment-only YAML document must parse as null');
}
// 5. safeLoadAll accepts options object to specify parsing options such as filename
{
const docYaml = `
name: sample-app
port: 8080
`;
const result = safeLoadAll(docYaml, { filename: 'config.yaml' });
assert.equal(Array.isArray(result), true, 'Result with options must be an array');
assert.equal(result.length, 1, 'Result must contain 1 document');
assert.deepEqual(result[0], { name: 'sample-app', port: 8080 });
const collected = [];
safeLoadAll(docYaml, (doc) => { collected.push(doc); }, { filename: 'config.yaml' });
assert.equal(collected.length, 1, 'Iterator with options must be invoked');
assert.deepEqual(collected[0], { name: 'sample-app', port: 8080 });
}
// 6. safeLoadAll throws YAMLException when parsing malformed YAML document
{
const malformedYaml = `
valid: doc
---
invalid: [unclosed flow collection
`;
assert.throws(
() => {
safeLoadAll(malformedYaml);
},
(err) => {
return err.name === 'YAMLException' || /YAMLException/.test(err.message);
},
'Malformed YAML document must throw YAMLException'
);
}
// 7. safeLoadAll throws YAMLException when encountering unsafe custom tags
{
const unsafeYaml = `
code: !!js/function "function () { return 42; }"
`;
assert.throws(
() => {
safeLoadAll(unsafeYaml);
},
(err) => {
return err.name === 'YAMLException' || /unknown tag/i.test(err.message);
},
'Unsafe tag in safeLoadAll must throw YAMLException'
);
}
console.log('All contract assertions for js-yaml.safeLoadAll passed successfully.');
}
runContractTests().catch((err) => {
console.error('Contract test failed:', err);
process.exit(1);
});
原始种子者
匿名