CodeSampleX

Ejemplo

content-type 2.0.0: parse and format HTTP Content-Type header with parameters

Muestra verificada para npm content-type 2.0.0: parse and format HTTP Content-Type header with parameters. El contrato se ejecutó en node 22 · linux…

sha256:b3d33fff9279e81525173205ad754db6ec711790e315d28055d0393ddf037e48

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
parse and format HTTP Content-Type header with parameters
Paquetes
Símbolos
  • contentType.format
  • contentType.parse
Creado
2026-08-18T17:53:14Z

Contrato

  1. contentType.parse parses media type and parameters from Content-Type header string
  2. contentType.parse normalizes media type to lowercase
  3. contentType.parse supports skipping parameters with options.parameters false
  4. contentType.format formats type and parameters into Content-Type header string
  5. contentType.format formats media type without parameters
  6. contentType.format quotes parameter values containing special characters

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: parse and format HTTP Content-Type header with parameters
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/content-type@2.0.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:44fc5f0b69fe5569ec6a6d5bab5d2ec6c73a9adeeb6a23c0882cad0191ff0983","contract":["contentType.parse parses media type and parameters from Content-Type header string","contentType.parse normalizes media type to lowercase","contentType.parse supports skipping parameters with options.parameters false","contentType.format formats type and parameters into Content-Type header string","contentType.format formats media type without parameters","contentType.format quotes parameter values containing special characters"],"goal":"parse and format HTTP Content-Type header with parameters","kind":"HOW","packages":["pkg:npm/content-type@2.0.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.0.0"],"schemaVersion":1,"symbols":["contentType.format","contentType.parse"],"verifierAdapter":"node-typescript@1"}
index.mjs
import contentType from 'content-type';

/**
 * Parse an HTTP Content-Type header string.
 * @param {string} header
 * @param {{ parameters?: boolean }} [options]
 * @returns {{ type: string, parameters: Record<string, string> }}
 */
export function parseContentType(header, options) {
  return contentType.parse(header, options);
}

/**
 * Format a media type and parameters into a Content-Type header string.
 * @param {string} type
 * @param {Record<string, string>} [parameters]
 * @returns {string}
 */
export function formatContentType(type, parameters) {
  const obj = { type };
  if (parameters) {
    obj.parameters = parameters;
  }
  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.0.0"
      }
    },
    "node_modules/content-type": {
      "version": "2.0.0",
      "resolved": "https://registry.npmjs.org/content-type/-/content-type-2.0.0.tgz",
      "integrity": "sha512-j/O/d7GcZCyNl7/hwZAb606rzqkyvaDctLmckbxLzHvFBzTJHuGEdodATcP3yIRoDrLHkIATJuvzbFlp/ki2cQ==",
      "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.0.0"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "parse and format HTTP Content-Type header with parameters",
  "kind": "HOW",
  "packages": [
    "pkg:npm/content-type@2.0.0"
  ],
  "symbols": [
    "contentType.format",
    "contentType.parse"
  ]
}
test/contract.mjs
import assert from 'node:assert/strict';
import contentType from 'content-type';

// Contract 1: contentType.parse parses media type and parameters from Content-Type header string
{
  const parsed = contentType.parse('text/html; charset=utf-8; foo=bar');
  assert.equal(parsed.type, 'text/html');
  assert.equal(parsed.parameters.charset, 'utf-8');
  assert.equal(parsed.parameters.foo, 'bar');
}

// Contract 2: contentType.parse normalizes media type to lowercase
{
  const parsed = contentType.parse('IMAGE/PNG');
  assert.equal(parsed.type, 'image/png');
  assert.deepEqual(Object.keys(parsed.parameters), []);
}

// Contract 3: contentType.parse supports skipping parameters with options.parameters false
{
  const parsed = contentType.parse('text/html; charset=utf-8', { parameters: false });
  assert.equal(parsed.type, 'text/html');
  assert.deepEqual(Object.keys(parsed.parameters), []);
}

// Contract 4: contentType.format formats type and parameters into Content-Type header string
{
  const formatted = contentType.format({
    type: 'text/html',
    parameters: { charset: 'utf-8' }
  });
  assert.equal(formatted, 'text/html; charset=utf-8');
}

// Contract 5: contentType.format formats media type without parameters
{
  const formatted = contentType.format({
    type: 'application/json'
  });
  assert.equal(formatted, 'application/json');
}

// Contract 6: contentType.format quotes parameter values containing special characters
{
  const formatted = contentType.format({
    type: 'text/plain',
    parameters: { boundary: 'something with spaces' }
  });
  assert.equal(formatted, 'text/plain; boundary="something with spaces"');
}

console.log('All contract assertions passed successfully.');

Seeder de origen

anónimo