CodeSampleX

示例

js-yaml 3.15.1: safeLoad

已验证示例 — npm js-yaml 3.15.1: safeLoad. contract 在 node 22 · linux alpine/x64 · docker 上运行并通过: assert yaml.safeLoad parses key-value mappings into javascript…

sha256:82519a2442ff929ee10c2b7d75009ece5f95eaab5fda04a2a6b428c6690a0296

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

验证运行环境

环境 契约 阶段 运行日期
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.safeLoad in pkg:npm/js-yaml@3.15.1
包
符号
  • js-yaml.safeLoad
环境
node 22.23.2
创建时间
2026-08-30T17:13:36Z

契约

  1. assert yaml.safeLoad parses key-value mappings into javascript objects
  2. assert yaml.safeLoad parses scalar integers, floats, booleans, and null literals
  3. assert yaml.safeLoad parses nested structures, mappings, and sequences into arrays and objects
  4. assert yaml.safeLoad parses sequences into javascript arrays
  5. assert yaml.safeLoad parses folded and literal multiline block scalars
  6. assert yaml.safeLoad returns undefined for empty string and null for comment-only content
  7. assert yaml.safeLoad parses content using custom schemas such as JSON_SCHEMA
  8. assert yaml.safeLoad throws YAMLException on invalid YAML syntax
  9. assert yaml.safeLoad throws YAMLException and rejects unsafe JavaScript function constructors

文件

  • PROMPT.md
  • csx.json
  • index.js
  • package-lock.json
  • package.json
  • spec.json
  • test/contract.mjs

下载源代码构件 (tar.gz)

源代码

PROMPT.md
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.safeLoad 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.safeLoad
Constraints:
  - executionContext: node
Required runtime conditions:
  - ecosystem: npm
  - language: javascript
  - moduleSystem: cjs
  - packageManager: npm@10.9.8
  - runtime: node@22.23.2

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.
csx.json
{"case":{"caseId":"case:sha256:246ab6803ab9e6a555c1cf13989cba63db87976c4955e3a34dbe144ff1c31d8a","constraints":{"executionContext":"node"},"contract":["assert yaml.safeLoad parses key-value mappings into javascript objects","assert yaml.safeLoad parses scalar integers, floats, booleans, and null literals","assert yaml.safeLoad parses nested structures, mappings, and sequences into arrays and objects","assert yaml.safeLoad parses sequences into javascript arrays","assert yaml.safeLoad parses folded and literal multiline block scalars","assert yaml.safeLoad returns undefined for empty string and null for comment-only content","assert yaml.safeLoad parses content using custom schemas such as JSON_SCHEMA","assert yaml.safeLoad throws YAMLException on invalid YAML syntax","assert yaml.safeLoad throws YAMLException and rejects unsafe JavaScript function constructors"],"goal":"verify js-yaml.safeLoad in pkg:npm/js-yaml@3.15.1","kind":"HOW","packages":["pkg:npm/js-yaml@3.15.1"],"schemaVersion":1,"symbols":["js-yaml.safeLoad"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","executionContext":"node","language":"javascript","libc":"glibc","libcVersion":"2.39","moduleSystem":"cjs","os":"linux","osVersionBucket":"24","packageManager":"npm","packageManagerVersion":"10.9.8","runtime":"node","runtimeVersion":"22.23.2","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.safeLoad"],"verifierAdapter":"node-typescript@1"}
index.js
const yaml = require('js-yaml');

function parseYaml(content, options) {
  return yaml.safeLoad(content, options);
}

function parseYamlScalar(content) {
  return yaml.safeLoad(content);
}

function parseYamlMapping(content) {
  return yaml.safeLoad(content);
}

function parseYamlSequence(content) {
  return yaml.safeLoad(content);
}

function parseYamlWithSchema(content, schema) {
  return yaml.safeLoad(content, { schema });
}

module.exports = {
  safeLoad: yaml.safeLoad,
  YAMLException: yaml.YAMLException,
  CORE_SCHEMA: yaml.CORE_SCHEMA,
  JSON_SCHEMA: yaml.JSON_SCHEMA,
  parseYaml,
  parseYamlScalar,
  parseYamlMapping,
  parseYamlSequence,
  parseYamlWithSchema,
};
package-lock.json
{
  "name": "sample-js-yaml",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-js-yaml",
      "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"
    }
  }
}
package.json
{
  "name": "sample-js-yaml",
  "version": "1.0.0",
  "description": "Minimal clean-room sample verifying js-yaml.safeLoad in js-yaml",
  "main": "index.js",
  "license": "MIT-0",
  "dependencies": {
    "js-yaml": "3.15.1"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify js-yaml.safeLoad in pkg:npm/js-yaml@3.15.1",
  "kind": "HOW",
  "packages": [
    "pkg:npm/js-yaml@3.15.1"
  ],
  "symbols": [
    "js-yaml.safeLoad"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@10.9.8",
    "runtime": "node@22.23.2"
  }
}
test/contract.mjs
import assert from 'node:assert';
import indexPkg from '../index.js';

const {
  safeLoad,
  YAMLException,
  CORE_SCHEMA,
  JSON_SCHEMA,
  parseYaml,
  parseYamlScalar,
  parseYamlMapping,
  parseYamlSequence,
  parseYamlWithSchema,
} = indexPkg;

// 1. Basic key-value mapping
const mappingYaml = 'name: CodeSampleX\nversion: 1.0.0\nenabled: true';
const mappingResult = parseYamlMapping(mappingYaml);
assert.deepStrictEqual(
  mappingResult,
  { name: 'CodeSampleX', version: '1.0.0', enabled: true },
  'yaml.safeLoad must parse key-value mappings into javascript objects'
);

// 2. Scalar types (integers, floats, booleans, null)
const intVal = parseYamlScalar('12345');
assert.strictEqual(intVal, 12345, 'yaml.safeLoad must parse integers');

const floatVal = parseYamlScalar('3.14159');
assert.strictEqual(floatVal, 3.14159, 'yaml.safeLoad must parse floats');

const boolTrue = parseYamlScalar('true');
assert.strictEqual(boolTrue, true, 'yaml.safeLoad must parse boolean true');

const boolFalse = parseYamlScalar('false');
assert.strictEqual(boolFalse, false, 'yaml.safeLoad must parse boolean false');

const nullVal = parseYamlScalar('null');
assert.strictEqual(nullVal, null, 'yaml.safeLoad must parse null literals');

// 3. Nested objects and sequences
const nestedYaml = `
project:
  title: Test Suite
  tags:
    - parser
    - yaml
    - node
  config:
    retries: 3
`;
const nestedResult = parseYaml(nestedYaml);
assert.deepStrictEqual(
  nestedResult,
  {
    project: {
      title: 'Test Suite',
      tags: ['parser', 'yaml', 'node'],
      config: {
        retries: 3,
      },
    },
  },
  'yaml.safeLoad must parse nested structures and sequences'
);

// 4. Sequence parsing
const sequenceYaml = `
- apple
- banana
- cherry
`;
const sequenceResult = parseYamlSequence(sequenceYaml);
assert.deepStrictEqual(
  sequenceResult,
  ['apple', 'banana', 'cherry'],
  'yaml.safeLoad must parse lists into javascript arrays'
);

// 5. Multiline strings and folded block scalars
const blockYaml = `
description: >
  This is a long line
  that folds into a single
  paragraph.
literal: |
  Line 1
  Line 2
`;
const blockResult = parseYaml(blockYaml);
assert.strictEqual(
  blockResult.description,
  'This is a long line that folds into a single paragraph.\n',
  'yaml.safeLoad must parse folded multiline strings'
);
assert.strictEqual(
  blockResult.literal,
  'Line 1\nLine 2\n',
  'yaml.safeLoad must parse literal block multiline strings'
);

// 6. Empty string returns undefined and comment-only string returns null
const emptyResult = parseYaml('');
assert.strictEqual(emptyResult, undefined, 'yaml.safeLoad must return undefined for empty string');

const commentResult = parseYaml('# Only comments\n# nothing else\n');
assert.strictEqual(commentResult, null, 'yaml.safeLoad must return null for comment-only content');

// 7. Schema options (e.g. JSON_SCHEMA)
const jsonSchemaYaml = 'key: "value"';
const jsonSchemaResult = parseYamlWithSchema(jsonSchemaYaml, JSON_SCHEMA);
assert.deepStrictEqual(
  jsonSchemaResult,
  { key: 'value' },
  'yaml.safeLoad must parse content with JSON_SCHEMA'
);

// 8. Syntax error throws YAMLException
assert.throws(
  () => {
    safeLoad('invalid: [unclosed list');
  },
  (err) => {
    return err instanceof YAMLException || (err && err.name === 'YAMLException');
  },
  'yaml.safeLoad must throw YAMLException on invalid syntax'
);

// 9. Unsafe JavaScript functions are rejected by safeLoad
assert.throws(
  () => {
    safeLoad('fn: !!js/function "function () { return 42; }"');
  },
  (err) => {
    return err instanceof YAMLException || (err && err.name === 'YAMLException');
  },
  'yaml.safeLoad must reject unsafe function constructors'
);

console.log('All js-yaml.safeLoad contract assertions passed.');

原始种子者

匿名