CodeSampleX

Ejemplo

media-typer 0.3.0

Muestra verificada para npm media-typer 0.3.0. El contrato se ejecutó en node 22 · linux alpine/x64 · docker y pasó: mediaTyper.parse parses a media type…

sha256:7f27940dbda0abff5d5ff727693c058106c5f65e678dbd7bc8db58090d9d8946

Esta red ofrece una sola cosa: una muestra que compila. La ejecutó en un sandbox y guardó el recibo firmado. No califica ni garantiza nada: si el mismo código compila donde estás no es algo que haya medido. Cuántas claves de firma distintas presentaron un recibo de contrato aprobado. Una es solo el autor; más de una significa que alguien más también lo compiló. Una clave se genera sola y no tiene identidad registrada detrás, así que cuenta claves, no personas. MIT-0

Evidencia de ejecución

El entorno declarado y las ejecuciones firmadas se muestran por separado, para que veas exactamente qué ejecutó esta muestra y dónde.

Base de evidencia
Contrato firmado aprobado
Recibos de verificación
1
Claves de firma que lo compilaron
1
Entorno declarado windows 10 x64 npm vm

Entornos de las ejecuciones de verificación

Entorno Contrato Etapas Ejecución
node 22 · linux alpine/x64 · docker ed25519:2175b912ea1c23b1 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1
2026-08-20

Caso

HOW
Objetivo
verify pkg:npm/media-typer@0.3.0
Paquetes
Creado
2026-08-20T00:36:54Z

Contrato

  1. mediaTyper.parse parses a media type string into type, subtype, suffix, and parameters
  2. mediaTyper.format formats a media type object into a media type string
  3. mediaTyper.parse and format throw TypeError for invalid media types

Archivos

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

Descargar el artefacto de código fuente (tar.gz)

Código fuente

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 pkg:npm/media-typer@0.3.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/media-typer@0.3.0

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:87bab51dfd8371addd9f30cadb64b562db97d1efa4ac3039bb1b5361a24624af","contract":["mediaTyper.parse parses a media type string into type, subtype, suffix, and parameters","mediaTyper.format formats a media type object into a media type string","mediaTyper.parse and format throw TypeError for invalid media types"],"goal":"verify pkg:npm/media-typer@0.3.0","kind":"HOW","packages":["pkg:npm/media-typer@0.3.0"],"schemaVersion":1,"symbols":["media-typer.parse","media-typer.format"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","ecosystem":"npm","os":"windows","osVersionBucket":"10","packageManager":"npm","schemaVersion":1,"virtualization":"vm"},"license":"MIT-0","packages":["pkg:npm/media-typer@0.3.0"],"schemaVersion":1,"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample",
      "version": "1.0.0",
      "dependencies": {
        "media-typer": "0.3.0"
      }
    },
    "node_modules/media-typer": {
      "version": "0.3.0",
      "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz",
      "integrity": "sha512-dq+qelQ9akHpcOl/gUVRTxVIOkAJ1wR3QAvb4RsVjS8oVoFjDGTc679wJYmUmknUF5HwMLOgb5O+a3KxfWapPQ==",
      "engines": {
        "node": ">= 0.6"
      }
    }
  }
}
package.json
{
  "name": "sample",
  "version": "1.0.0",
  "type": "module",
  "dependencies": {
    "media-typer": "0.3.0"
  }
}
sample.mjs
import typer from 'media-typer';

/**
 * Parses a media type string into an object describing its components.
 * @param {string} mediaType - Media type string (e.g., 'application/json; charset=utf-8')
 * @returns {{ type: string, subtype: string, suffix?: string, parameters: Record<string, string> }}
 */
export function parseMediaType(mediaType) {
  return typer.parse(mediaType);
}

/**
 * Formats a media type object into a media type string.
 * @param {{ type: string, subtype: string, suffix?: string, parameters?: Record<string, string> }} obj
 * @returns {string}
 */
export function formatMediaType(obj) {
  return typer.format(obj);
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:npm/media-typer@0.3.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/media-typer@0.3.0"
  ]
}
test/contract.mjs
import assert from 'node:assert/strict';
import typer from 'media-typer';
import { parseMediaType, formatMediaType } from '../sample.mjs';

function testParseMediaType() {
  const parsed1 = parseMediaType('application/json');
  assert.equal(parsed1.type, 'application');
  assert.equal(parsed1.subtype, 'json');
  assert.equal(parsed1.suffix, undefined);
  assert.deepEqual(parsed1.parameters, {});

  const parsed2 = parseMediaType('application/vnd.api+json; charset=utf-8');
  assert.equal(parsed2.type, 'application');
  assert.equal(parsed2.subtype, 'vnd.api');
  assert.equal(parsed2.suffix, 'json');
  assert.deepEqual(parsed2.parameters, { charset: 'utf-8' });

  const parsed3 = typer.parse('image/svg+xml; q=0.8');
  assert.equal(parsed3.type, 'image');
  assert.equal(parsed3.subtype, 'svg');
  assert.equal(parsed3.suffix, 'xml');
  assert.deepEqual(parsed3.parameters, { q: '0.8' });

  console.log('Contract 1 passed: parseMediaType parses media types correctly');
}

function testFormatMediaType() {
  const formatted1 = formatMediaType({ type: 'text', subtype: 'html' });
  assert.equal(formatted1, 'text/html');

  const formatted2 = formatMediaType({ type: 'image', subtype: 'svg', suffix: 'xml' });
  assert.equal(formatted2, 'image/svg+xml');

  const formatted3 = typer.format({
    type: 'application',
    subtype: 'json',
    parameters: { charset: 'utf-8', q: '1' }
  });
  assert.equal(formatted3, 'application/json; charset=utf-8; q=1');

  console.log('Contract 2 passed: formatMediaType formats objects into media type strings');
}

function testInvalidInputs() {
  assert.throws(() => {
    parseMediaType('invalid type');
  }, TypeError);

  assert.throws(() => {
    parseMediaType('');
  }, TypeError);

  assert.throws(() => {
    formatMediaType({ type: 'invalid type', subtype: 'json' });
  }, TypeError);

  assert.throws(() => {
    formatMediaType(null);
  }, TypeError);

  console.log('Contract 3 passed: validation errors thrown for invalid inputs');
}

function main() {
  testParseMediaType();
  testFormatMediaType();
  testInvalidInputs();
  console.log('All contracts passed successfully.');
}

main();

Seeder de origen

anónimo