CodeSampleX

Beispiel

type-is 1.6.18

Verifiziertes Beispiel für npm type-is 1.6.18. Der Vertrag lief auf node 22 · linux debian/x64 · docker und bestand: typeis(req, types) matches the…

sha256:e63d651f7eacee7ff8ce9d42810cf0d70228c90a4c1ce4c201383449e78e5c6a

Dieses Netzwerk bietet eine Sache: ein Sample, das baut. Es hat es in einer Sandbox ausgeführt und die signierte Quittung behalten. Es bewertet nichts und garantiert nichts — ob derselbe Code bei Ihnen baut, hat es nicht gemessen. Wie viele verschiedene Signaturschlüssel eine bestandene Vertragsquittung eingereicht haben. Einer ist der Autor allein; mehr als einer heißt, jemand anderes hat es auch gebaut. Ein Schlüssel wird selbst erzeugt und hat keine registrierte Identität dahinter — gezählt werden Schlüssel, nicht Personen. MIT-0

Ausführungsbelege

Die deklarierte Umgebung und die signierten Läufe stehen getrennt, damit Sie genau sehen, was dieses Sample ausgeführt hat und wo.

Beleggrundlage
Signierter Vertrag bestanden
Verifizierungsbelege
1
Signaturschlüssel, die es gebaut haben
1
Deklarierte Umgebung node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

Umgebungen der Verifizierungsläufe

Umgebung Contract Stufen Lauf
node 22 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22@sha256:8a34c4ab3ea2…
2026-09-16

Fall

HOW
Ziel
verify pkg:npm/type-is@1.6.18
Pakete
Umgebung
node 22.23.2
Erstellt
2026-09-16T22:21:05Z

Contract

  1. typeis(req, types) matches the Content-Type header of a request against candidate types and returns the matching type
  2. typeis(req, types) returns null when the request has no body headers (neither content-length nor transfer-encoding)
  3. typeis(req, types) returns false when the request body content-type does not match candidate types
  4. typeis.hasBody(req) returns true when request has content-length or transfer-encoding headers
  5. typeis.is(mediaType, types) matches media type strings against extensions, types, or wildcards
  6. typeis.normalize(type) normalizes type extensions and shorthand names to full MIME types

Dateien

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

Quellartefakt herunterladen (tar.gz)

Quelltext

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/type-is@1.6.18
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/type-is@1.6.18

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:0ffa535c7afd256a9045a2255d473220c9b660da6a6aad641a2ccba36cb2b9e8","contract":["typeis(req, types) matches the Content-Type header of a request against candidate types and returns the matching type","typeis(req, types) returns null when the request has no body headers (neither content-length nor transfer-encoding)","typeis(req, types) returns false when the request body content-type does not match candidate types","typeis.hasBody(req) returns true when request has content-length or transfer-encoding headers","typeis.is(mediaType, types) matches media type strings against extensions, types, or wildcards","typeis.normalize(type) normalizes type extensions and shorthand names to full MIME types"],"goal":"verify pkg:npm/type-is@1.6.18","kind":"HOW","packages":["pkg:npm/type-is@1.6.18"],"schemaVersion":1},"contractCommand":["node","test/contract.cjs"],"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/type-is@1.6.18"],"schemaVersion":1,"subject":"pkg:npm/type-is@1.6.18","verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "type-is-sample",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "type-is-sample",
      "version": "1.0.0",
      "dependencies": {
        "type-is": "1.6.18"
      }
    },
    "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==",
      "license": "MIT",
      "engines": {
        "node": ">= 0.6"
      }
    },
    "node_modules/mime-db": {
      "version": "1.52.0",
      "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz",
      "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==",
      "license": "MIT",
      "engines": {
        "node": ">= 0.6"
      }
    },
    "node_modules/mime-types": {
      "version": "2.1.35",
      "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz",
      "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==",
      "license": "MIT",
      "dependencies": {
        "mime-db": "1.52.0"
      },
      "engines": {
        "node": ">= 0.6"
      }
    },
    "node_modules/type-is": {
      "version": "1.6.18",
      "resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz",
      "integrity": "sha512-TkRKr9sUTxEH8MdfuCSP7VizJyzRNMjj2J2do2Jr3Kym598JVdEksuzPQCnlFPW4ky9Q+iA+ma9BGm06XQBy8g==",
      "license": "MIT",
      "dependencies": {
        "media-typer": "0.3.0",
        "mime-types": "~2.1.24"
      },
      "engines": {
        "node": ">= 0.6"
      }
    }
  }
}
package.json
{
  "name": "type-is-sample",
  "version": "1.0.0",
  "description": "Minimal sample verifying type-is",
  "main": "src/index.js",
  "scripts": {
    "test": "node test/contract.cjs"
  },
  "dependencies": {
    "type-is": "1.6.18"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:npm/type-is@1.6.18",
  "kind": "HOW",
  "packages": [
    "pkg:npm/type-is@1.6.18"
  ]
}
src/index.js
'use strict';

const typeis = require('type-is');

/**
 * Checks if the incoming request has a body and matches any of the candidate types.
 *
 * @param {object} req - HTTP request object with headers.
 * @param {string|string[]} candidateTypes - Acceptable MIME types, shorthands, or wildcards.
 * @returns {string|false|null} - Matching type string, false if mismatch, or null if no body.
 */
function checkRequestType(req, candidateTypes) {
  const types = Array.isArray(candidateTypes) ? candidateTypes : [candidateTypes];
  return typeis(req, types);
}

/**
 * Checks whether an HTTP request contains a request body.
 *
 * @param {object} req - HTTP request object with headers.
 * @returns {boolean} - True if body is present, false otherwise.
 */
function hasRequestBody(req) {
  return typeis.hasBody(req);
}

/**
 * Compares a media type string against candidate types.
 *
 * @param {string} mediaType - Media type string (e.g. 'application/json').
 * @param {string|string[]} candidateTypes - Candidate types, shorthands, or wildcards.
 * @returns {string|false} - Matching type string or false.
 */
function matchMediaType(mediaType, candidateTypes) {
  const types = Array.isArray(candidateTypes) ? candidateTypes : [candidateTypes];
  return typeis.is(mediaType, types);
}

/**
 * Normalizes a type shorthand or extension into a full MIME type.
 *
 * @param {string} type - Extension or shorthand (e.g. 'json', '+json', 'html').
 * @returns {string|false} - Normalized MIME pattern or false.
 */
function normalizeType(type) {
  return typeis.normalize(type);
}

module.exports = {
  typeis,
  checkRequestType,
  hasRequestBody,
  matchMediaType,
  normalizeType,
};
test/contract.cjs
const assert = require('node:assert/strict');
const typeis = require('type-is');
const {
  checkRequestType,
  hasRequestBody,
  matchMediaType,
  normalizeType,
} = require('../src/index.js');

function createMockReq(headers = {}) {
  return {
    headers: { ...headers },
  };
}

async function main() {
  // Contract 1: typeis(req, types) matches the Content-Type header of a request against candidate types and returns the matching type
  {
    const reqJson = createMockReq({
      'content-type': 'application/json; charset=utf-8',
      'content-length': '42',
    });
    assert.strictEqual(typeis(reqJson, ['json', 'html']), 'json');
    assert.strictEqual(typeis(reqJson, ['application/json']), 'application/json');
    assert.strictEqual(checkRequestType(reqJson, ['json', 'html']), 'json');

    const reqHtml = createMockReq({
      'content-type': 'text/html; charset=utf-8',
      'transfer-encoding': 'chunked',
    });
    assert.strictEqual(typeis(reqHtml, ['html', 'json']), 'html');
    assert.strictEqual(checkRequestType(reqHtml, 'html'), 'html');

    const reqCustom = createMockReq({
      'content-type': 'application/vnd.api+json',
      'content-length': '100',
    });
    assert.strictEqual(typeis(reqCustom, ['+json']), 'application/vnd.api+json');
    assert.strictEqual(checkRequestType(reqCustom, ['+json']), 'application/vnd.api+json');
  }

  // Contract 2: typeis(req, types) returns null when the request has no body headers (neither content-length nor transfer-encoding)
  {
    const reqNoBody = createMockReq({
      'content-type': 'application/json',
    });
    assert.strictEqual(typeis(reqNoBody, ['json']), null);
    assert.strictEqual(checkRequestType(reqNoBody, ['json']), null);

    const reqEmptyHeaders = createMockReq({});
    assert.strictEqual(typeis(reqEmptyHeaders, ['json', 'html']), null);
    assert.strictEqual(checkRequestType(reqEmptyHeaders, ['json', 'html']), null);
  }

  // Contract 3: typeis(req, types) returns false when the request body content-type does not match candidate types
  {
    const reqText = createMockReq({
      'content-type': 'text/plain',
      'content-length': '20',
    });
    assert.strictEqual(typeis(reqText, ['json', 'html']), false);
    assert.strictEqual(checkRequestType(reqText, ['json', 'html']), false);

    const reqPng = createMockReq({
      'content-type': 'image/png',
      'transfer-encoding': 'chunked',
    });
    assert.strictEqual(typeis(reqPng, ['application/json', 'text/*']), false);
    assert.strictEqual(checkRequestType(reqPng, ['application/json', 'text/*']), false);
  }

  // Contract 4: typeis.hasBody(req) returns true when request has content-length or transfer-encoding headers
  {
    const reqLength = createMockReq({ 'content-length': '15' });
    assert.strictEqual(typeis.hasBody(reqLength), true);
    assert.strictEqual(hasRequestBody(reqLength), true);

    const reqChunked = createMockReq({ 'transfer-encoding': 'chunked' });
    assert.strictEqual(typeis.hasBody(reqChunked), true);
    assert.strictEqual(hasRequestBody(reqChunked), true);

    const reqEmpty = createMockReq({});
    assert.strictEqual(typeis.hasBody(reqEmpty), false);
    assert.strictEqual(hasRequestBody(reqEmpty), false);
  }

  // Contract 5: typeis.is(mediaType, types) matches media type strings against extensions, types, or wildcards
  {
    assert.strictEqual(typeis.is('application/json', ['json']), 'json');
    assert.strictEqual(typeis.is('application/json', ['text/html', 'application/json']), 'application/json');
    assert.strictEqual(typeis.is('image/png', ['image/*']), 'image/png');
    assert.strictEqual(typeis.is('text/html', ['json', 'image/*']), false);

    assert.strictEqual(matchMediaType('application/json', ['json']), 'json');
    assert.strictEqual(matchMediaType('image/png', ['image/*']), 'image/png');
    assert.strictEqual(matchMediaType('text/html', ['json']), false);
  }

  // Contract 6: typeis.normalize(type) normalizes type extensions and shorthand names to full MIME types
  {
    assert.strictEqual(typeis.normalize('json'), 'application/json');
    assert.strictEqual(typeis.normalize('html'), 'text/html');
    assert.strictEqual(typeis.normalize('+json'), '*/*+json');
    assert.strictEqual(typeis.normalize('application/json'), 'application/json');

    assert.strictEqual(normalizeType('json'), 'application/json');
    assert.strictEqual(normalizeType('html'), 'text/html');
    assert.strictEqual(normalizeType('+json'), '*/*+json');
  }

  console.log('All contracts verified successfully.');
}

main().catch((err) => {
  console.error(err);
  process.exit(1);
});

Ursprungs-Seeder

anonym