CodeSampleX

Ejemplo

content-type 2.1.0: contentType.format, contentType.parse

Muestra verificada para npm content-type 2.1.0: contentType.format, contentType.parse. El contrato se ejecutó en node 22 · linux alpine/x64 · docker y pasó.

sha256:1627d1dfdd9080858f9409e2b9c79930ef45ad3bdca6de71de66022e2037e399

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 11 x64 npm

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-19

Caso

HOW
Objetivo
verify pkg:npm/content-type@2.1.0
Paquetes
Símbolos
  • contentType.format
  • contentType.parse
Creado
2026-08-18T17:54:11Z

Contrato

  1. contentType.parse parses Content-Type header string with type and parameters
  2. contentType.parse parses media type with quoted parameter values
  3. contentType.parse supports options.parameters false to skip parameter parsing
  4. contentType.parse supports options.comma true to parse Accept header media range
  5. contentType.format formats type and parameters object into Content-Type header string
  6. contentType.format throws TypeError on invalid media type or invalid parameter name

Archivos

  • PROMPT.md
  • csx.json
  • index.mjs
  • package-lock.json
  • package.json
  • 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/content-type@2.1.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/content-type@2.1.0
Demonstrate these symbols/APIs:
  - contentType.format
  - contentType.parse

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:c99501aa081d51d9cebb4d1c95ec9e1942f5aab8aebb359ac99948cdb54e8532","contract":["contentType.parse parses Content-Type header string with type and parameters","contentType.parse parses media type with quoted parameter values","contentType.parse supports options.parameters false to skip parameter parsing","contentType.parse supports options.comma true to parse Accept header media range","contentType.format formats type and parameters object into Content-Type header string","contentType.format throws TypeError on invalid media type or invalid parameter name"],"goal":"verify pkg:npm/content-type@2.1.0","kind":"HOW","packages":["pkg:npm/content-type@2.1.0"],"schemaVersion":1,"symbols":["contentType.format","contentType.parse"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","ecosystem":"npm","os":"windows","osVersionBucket":"11","packageManager":"npm","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/content-type@2.1.0"],"schemaVersion":1,"symbols":["contentType.format","contentType.parse"],"verifierAdapter":"node-typescript@1"}
index.mjs
import contentType from 'content-type';

/**
 * Parses a Content-Type header string into media type and parameters.
 * @param {string} header
 * @param {object} [options]
 * @returns {{type: string, index: number, parameters: Record<string, string>}}
 */
export function parseHeader(header, options) {
  return contentType.parse(header, options);
}

/**
 * Formats a media type and optional parameters object into a Content-Type string.
 * @param {{type: string, parameters?: Record<string, string>}} obj
 * @returns {string}
 */
export function formatHeader(obj) {
  return contentType.format(obj);
}
package-lock.json
{
  "name": "sample",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample",
      "version": "1.0.0",
      "dependencies": {
        "content-type": "2.1.0"
      }
    },
    "node_modules/content-type": {
      "version": "2.1.0",
      "resolved": "https://registry.npmjs.org/content-type/-/content-type-2.1.0.tgz",
      "integrity": "sha512-mj7UPXE0jaqaOsukNZRUEfEi2AcL7C/vwmwcHV0O97eO1E1pxBZuyjlZrx5seTaNBg1U6+o35wpa35Qfcc+7ag==",
      "license": "MIT",
      "engines": {
        "node": ">=18"
      },
      "funding": {
        "type": "opencollective",
        "url": "https://opencollective.com/express"
      }
    }
  }
}
package.json
{
  "name": "sample",
  "version": "1.0.0",
  "type": "module",
  "dependencies": {
    "content-type": "2.1.0"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:npm/content-type@2.1.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/content-type@2.1.0"
  ],
  "symbols": [
    "contentType.format",
    "contentType.parse"
  ]
}
test/contract.mjs
import assert from 'node:assert/strict';
import contentType from 'content-type';
import { parseHeader, formatHeader } from '../index.mjs';

// Case 1: parse basic Content-Type header with charset parameter
{
  const header = 'text/html; charset=utf-8';
  const parsed = contentType.parse(header);
  assert.equal(parsed.type, 'text/html', 'type should match text/html');
  assert.equal(parsed.parameters.charset, 'utf-8', 'charset parameter should match utf-8');
}

// Case 2: parse header with multiple parameters and quoted values
{
  const header = 'application/json; charset="utf-8"; boundary="something"';
  const parsed = contentType.parse(header);
  assert.equal(parsed.type, 'application/json', 'type should match application/json');
  assert.equal(parsed.parameters.charset, 'utf-8', 'charset parameter should match utf-8');
  assert.equal(parsed.parameters.boundary, 'something', 'boundary parameter should match something');
}

// Case 3: parse header with options.parameters set to false
{
  const header = 'text/html; charset=utf-8';
  const parsed = contentType.parse(header, { parameters: false });
  assert.equal(parsed.type, 'text/html', 'type should match text/html');
  assert.deepEqual(Object.keys(parsed.parameters), [], 'parameters should be empty');
}

// Case 4: parse header with options.comma set to true (media range / Accept header)
{
  const header = 'text/html; q=0.8, text/plain; q=0.5';
  const parsed = contentType.parse(header, { comma: true });
  assert.equal(parsed.type, 'text/html', 'type should match first media range');
  assert.equal(parsed.parameters.q, '0.8', 'parameter q should match 0.8');
}

// Case 5: format Content-Type object with type and parameters
{
  const formatted = contentType.format({
    type: 'text/html',
    parameters: { charset: 'utf-8' }
  });
  assert.equal(formatted, 'text/html; charset=utf-8', 'formatted string should match expected header');
}

// Case 6: format Content-Type object without parameters
{
  const formatted = contentType.format({
    type: 'application/json'
  });
  assert.equal(formatted, 'application/json', 'formatted string should match expected header');
}

// Case 7: format throws TypeError on invalid media type
{
  assert.throws(
    () => contentType.format({ type: 'invalid type with spaces' }),
    TypeError,
    'formatting invalid type should throw TypeError'
  );
}

// Case 8: format throws TypeError on invalid parameter name
{
  assert.throws(
    () => contentType.format({ type: 'text/plain', parameters: { 'invalid param': 'val' } }),
    TypeError,
    'formatting invalid parameter name should throw TypeError'
  );
}

// Case 9: module wrapper functions match direct library behavior
{
  const parsed = parseHeader('image/png');
  assert.equal(parsed.type, 'image/png', 'parseHeader should parse image/png');

  const formatted = formatHeader({ type: 'image/png' });
  assert.equal(formatted, 'image/png', 'formatHeader should format image/png');
}

console.log('PASS: all content-type contract assertions passed');

Seeder de origen

anónimo