CodeSampleX

샘플

mime 1.6.0

검증된 샘플 — npm mime 1.6.0. node 22 · linux alpine/x64 · docker에서 contract를 실행해 통과했습니다: mime.lookup returns the correct MIME content type for standard file…

sha256:6447e33fae448da0ff6bdb7e3e3ea43498f8d75516ce67a5e704c04ae0241524

이 네트워크가 제공하는 것은 하나입니다. 빌드되는 샘플. 샌드박스에서 돌리고 서명된 영수증을 보관합니다. 등급을 매기지 않고 무엇도 보증하지 않습니다 — 같은 코드가 당신 환경에서 빌드되는지는 측정한 적이 없습니다. 통과한 계약 영수증을 낸 서로 다른 서명 키의 수입니다. 하나면 작성자 혼자이고, 둘 이상이면 다른 사람도 빌드했다는 뜻입니다. 키는 스스로 만드는 것이고 뒤에 등록된 신원이 없으므로, 세는 것은 사람이 아니라 키입니다. 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:2175b912ea1c23b1 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1
2026-08-20

케이스

HOW
목표
verify pkg:npm/mime@1.6.0
패키지
환경
node 22.23.2
생성일
2026-08-20T01:44:38Z

컨트랙트

  1. mime.lookup returns the correct MIME content type for standard file paths and extensions
  2. mime.lookup handles case-insensitive file extensions and nested paths
  3. mime.lookup returns application/octet-stream fallback for unrecognized file extensions
  4. mime.extension returns the canonical file extension for a given MIME type
  5. mime.charsets.lookup returns default character sets such as UTF-8 for text MIME types
  6. mime.define allows registering custom MIME types and extension mappings

파일

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

소스 아티팩트 내려받기 (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 pkg:npm/mime@1.6.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/mime@1.6.0
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:6ac01e7e6fcf438f669d29af0403001e5112220b5a5ce0b5825fd073c8159342","constraints":{"executionContext":"node"},"contract":["mime.lookup returns the correct MIME content type for standard file paths and extensions","mime.lookup handles case-insensitive file extensions and nested paths","mime.lookup returns application/octet-stream fallback for unrecognized file extensions","mime.extension returns the canonical file extension for a given MIME type","mime.charsets.lookup returns default character sets such as UTF-8 for text MIME types","mime.define allows registering custom MIME types and extension mappings"],"goal":"verify pkg:npm/mime@1.6.0","kind":"HOW","packages":["pkg:npm/mime@1.6.0"],"schemaVersion":1},"contractCommand":["node","test/contract.js"],"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/mime@1.6.0"],"schemaVersion":1,"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "mime-sample",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "mime-sample",
      "version": "1.0.0",
      "dependencies": {
        "mime": "1.6.0"
      }
    },
    "node_modules/mime": {
      "version": "1.6.0",
      "resolved": "https://registry.npmjs.org/mime/-/mime-1.6.0.tgz",
      "integrity": "sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg==",
      "license": "MIT",
      "bin": {
        "mime": "cli.js"
      },
      "engines": {
        "node": ">=4"
      }
    }
  }
}
package.json
{
  "name": "mime-sample",
  "version": "1.0.0",
  "dependencies": {
    "mime": "1.6.0"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:npm/mime@1.6.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/mime@1.6.0"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@10.9.8",
    "runtime": "node@22.23.2"
  }
}
src/index.js
const mime = require('mime');

/**
 * Looks up the MIME content type for a given file path or extension.
 *
 * @param {string} path - File path or extension (e.g., 'document.pdf', 'file.txt', 'image.png').
 * @param {string} [fallback] - Optional fallback MIME type if unknown.
 * @returns {string} The MIME type string.
 */
function getMimeType(path, fallback) {
  const type = mime.lookup(path);
  if (type === mime.default_type && fallback !== undefined) {
    return fallback;
  }
  return type;
}

/**
 * Gets the default file extension for a given MIME content type.
 *
 * @param {string} type - MIME content type (e.g., 'text/plain', 'image/png').
 * @returns {string|undefined} The default extension, or undefined if unknown.
 */
function getExtension(type) {
  return mime.extension(type);
}

/**
 * Looks up the default charset for a given MIME content type.
 *
 * @param {string} type - MIME content type (e.g., 'text/plain', 'text/html').
 * @param {string} [defaultCharset] - Fallback charset if none defined.
 * @returns {string|undefined} The charset string (e.g., 'UTF-8').
 */
function getCharset(type, defaultCharset) {
  return mime.charsets.lookup(type, defaultCharset);
}

/**
 * Registers custom MIME type to extension mappings.
 *
 * @param {Record<string, string[]>} typeMap - Mapping of MIME types to array of extensions.
 */
function defineCustomTypes(typeMap) {
  mime.define(typeMap);
}

module.exports = {
  mime,
  getMimeType,
  getExtension,
  getCharset,
  defineCustomTypes,
};
test/contract.js
const assert = require('node:assert/strict');
const {
  mime,
  getMimeType,
  getExtension,
  getCharset,
  defineCustomTypes,
} = require('../src/index.js');

function testLookupStandardTypes() {
  // Common text formats
  assert.strictEqual(mime.lookup('document.txt'), 'text/plain');
  assert.strictEqual(mime.lookup('index.html'), 'text/html');
  assert.strictEqual(mime.lookup('style.css'), 'text/css');
  assert.strictEqual(mime.lookup('app.js'), 'application/javascript');
  assert.strictEqual(getMimeType('data.json'), 'application/json');
  assert.strictEqual(getMimeType('doc.xml'), 'application/xml');

  // Binary and media formats
  assert.strictEqual(mime.lookup('photo.png'), 'image/png');
  assert.strictEqual(mime.lookup('photo.jpg'), 'image/jpeg');
  assert.strictEqual(mime.lookup('photo.jpeg'), 'image/jpeg');
  assert.strictEqual(mime.lookup('photo.gif'), 'image/gif');
  assert.strictEqual(mime.lookup('photo.svg'), 'image/svg+xml');
  assert.strictEqual(mime.lookup('document.pdf'), 'application/pdf');
  assert.strictEqual(mime.lookup('archive.zip'), 'application/zip');
  assert.strictEqual(mime.lookup('audio.mp3'), 'audio/mpeg');
  assert.strictEqual(mime.lookup('video.mp4'), 'video/mp4');
}

function testLookupPathAndCaseHandling() {
  // Nested paths
  assert.strictEqual(mime.lookup('/path/to/nested/file.txt'), 'text/plain');
  assert.strictEqual(mime.lookup('relative/dir/image.png'), 'image/png');

  // Case insensitivity
  assert.strictEqual(mime.lookup('PHOTO.PNG'), 'image/png');
  assert.strictEqual(mime.lookup('DOCUMENT.PDF'), 'application/pdf');
  assert.strictEqual(mime.lookup('DATA.JSON'), 'application/json');

  // Extension-only or dotted strings
  assert.strictEqual(mime.lookup('txt'), 'text/plain');
  assert.strictEqual(mime.lookup('.txt'), 'text/plain');
}

function testLookupFallbacks() {
  // Unknown extensions return default_type (application/octet-stream)
  assert.strictEqual(mime.lookup('unknown.unrecognized_xyz_ext'), 'application/octet-stream');
  assert.strictEqual(mime.lookup('noextensionfile'), 'application/octet-stream');

  // getMimeType helper allows custom fallback
  assert.strictEqual(
    getMimeType('unknown.custom_ext_123', 'text/plain'),
    'text/plain'
  );
  assert.strictEqual(
    getMimeType('photo.png', 'text/plain'),
    'image/png'
  );
}

function testExtensionLookup() {
  // Standard MIME types to extensions
  assert.strictEqual(mime.extension('text/plain'), 'txt');
  assert.strictEqual(mime.extension('text/html'), 'html');
  assert.strictEqual(mime.extension('text/css'), 'css');
  assert.strictEqual(mime.extension('application/json'), 'json');
  assert.strictEqual(mime.extension('application/pdf'), 'pdf');
  assert.strictEqual(mime.extension('image/png'), 'png');
  assert.strictEqual(mime.extension('image/jpeg'), 'jpeg');

  // Helper function
  assert.strictEqual(getExtension('application/zip'), 'zip');

  // Unknown MIME type returns undefined
  assert.strictEqual(mime.extension('application/x-nonexistent-type'), undefined);
  assert.strictEqual(getExtension('application/x-nonexistent-type'), undefined);
}

function testCharsetLookup() {
  // Text types default to UTF-8
  assert.strictEqual(mime.charsets.lookup('text/plain'), 'UTF-8');
  assert.strictEqual(mime.charsets.lookup('text/html'), 'UTF-8');
  assert.strictEqual(mime.charsets.lookup('application/json'), 'UTF-8');

  // Non-text types return undefined or fallback
  assert.strictEqual(mime.charsets.lookup('image/png'), undefined);
  assert.strictEqual(mime.charsets.lookup('image/png', 'ISO-8859-1'), 'ISO-8859-1');

  // Helper function
  assert.strictEqual(getCharset('text/plain'), 'UTF-8');
  assert.strictEqual(getCharset('image/jpeg', 'US-ASCII'), 'US-ASCII');
}

function testCustomTypeDefinition() {
  // Define custom MIME types
  defineCustomTypes({
    'text/x-custom-sample': ['cstm', 'customsample'],
    'application/x-csx-data': ['csxdat'],
  });

  // Verify custom lookup
  assert.strictEqual(mime.lookup('file.cstm'), 'text/x-custom-sample');
  assert.strictEqual(mime.lookup('data.customsample'), 'text/x-custom-sample');
  assert.strictEqual(mime.lookup('record.csxdat'), 'application/x-csx-data');

  // Verify custom extension
  assert.strictEqual(mime.extension('text/x-custom-sample'), 'cstm');
  assert.strictEqual(mime.extension('application/x-csx-data'), 'csxdat');
}

function main() {
  testLookupStandardTypes();
  testLookupPathAndCaseHandling();
  testLookupFallbacks();
  testExtensionLookup();
  testCharsetLookup();
  testCustomTypeDefinition();

  console.log('All pkg:npm/mime@1.6.0 contracts passed successfully.');
}

main();

오리진 시더

익명