CodeSampleX

Пример

mime-db 1.52.0: lookup MIME type metadata and extensions in mime-db

Проверенный пример — npm mime-db 1.52.0: lookup MIME type metadata and extensions in mime-db. Контракт выполнен на node 22 · linux alpine/x64 · docker и…

sha256:6c1dfc987b0995d378c70092b5b3e3b468e22be8dfefe5dfd8a7f8cb73366be6

Эта сеть предлагает одно: образец, который собирается. Она запустила его в песочнице и сохранила подписанную квитанцию. Она ничего не оценивает и ничего не гарантирует — собирается ли тот же код у вас, она не измеряла. Сколько различных ключей подписи подали пройденную квитанцию контракта. Один — только автор; больше одного — значит, кто-то ещё тоже собрал. Ключ создаётся сам и не имеет зарегистрированной личности, поэтому считаются ключи, а не люди. MIT-0

Свидетельства выполнения

Заявленное окружение и подписанные запуски разделены, чтобы вы точно видели, что этот образец запускал и где.

Основа свидетельства
Подписанный контракт пройден
Квитанции проверки
1
Ключи подписи, собравшие его
1
Заявленная среда node 22 linux x64 node 22 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-19

Кейс

HOW
Цель
lookup MIME type metadata and extensions in mime-db
Пакеты
Символы
  • mime-db
Окружение
node 22
Создан
2026-08-18T18:07:59Z

Контракт

  1. db is an object mapping MIME type strings to metadata entries
  2. getType performs case-insensitive lookup and returns metadata entry with source, extensions, and charset
  3. getExtensions returns array of associated file extensions or empty array
  4. getDefaultExtension returns primary file extension without leading dot
  5. isCompressible returns boolean compressibility status of the media type
  6. findTypeByExtension looks up media type corresponding to a file extension
  7. getSource returns database origin source such as iana or apache
  8. getCharset returns defined charset encoding or undefined

Файлы

  • PROMPT.md
  • csx.json
  • index.js
  • package-lock.json
  • package.json
  • spec.json
  • 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: lookup MIME type metadata and extensions in mime-db
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/mime-db@1.52.0
Demonstrate these symbols/APIs:
  - mime-db
Constraints:
  - executionContext: node
Required runtime conditions:
  - ecosystem: npm
  - language: javascript
  - moduleSystem: cjs
  - packageManager: npm@11.8.0
  - runtime: node@24.13.1

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:12d74e8c2f394fabd1db4b5c503120475dd8d1c7c9d7602d55ed428b9dea38a5","constraints":{"executionContext":"node"},"contract":["db is an object mapping MIME type strings to metadata entries","getType performs case-insensitive lookup and returns metadata entry with source, extensions, and charset","getExtensions returns array of associated file extensions or empty array","getDefaultExtension returns primary file extension without leading dot","isCompressible returns boolean compressibility status of the media type","findTypeByExtension looks up media type corresponding to a file extension","getSource returns database origin source such as iana or apache","getCharset returns defined charset encoding or undefined"],"goal":"lookup MIME type metadata and extensions in mime-db","kind":"HOW","packages":["pkg:npm/mime-db@1.52.0"],"schemaVersion":1,"symbols":["mime-db"]},"contractCommand":["node","test/contract.js"],"environment":{"arch":"x64","ecosystem":"npm","executionContext":"node","language":"javascript","moduleSystem":"cjs","os":"linux","packageManager":"npm","packageManagerVersion":"10","runtime":"node","runtimeVersion":"22","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/mime-db@1.52.0"],"schemaVersion":1,"symbols":["mime-db"],"verifierAdapter":"node-typescript@1"}
index.js
'use strict';

var db = require('mime-db');

/**
 * Get the metadata entry for a MIME type (case-insensitive).
 * @param {string} type - MIME type string (e.g., 'application/json')
 * @returns {object|undefined}
 */
function getType(type) {
  if (typeof type !== 'string') {
    return undefined;
  }
  return db[type.toLowerCase().trim()];
}

/**
 * Get all extensions associated with a MIME type.
 * @param {string} type - MIME type string
 * @returns {string[]}
 */
function getExtensions(type) {
  var entry = getType(type);
  return (entry && Array.isArray(entry.extensions)) ? entry.extensions.slice() : [];
}

/**
 * Get the primary default extension for a MIME type.
 * @param {string} type - MIME type string
 * @returns {string|undefined}
 */
function getDefaultExtension(type) {
  var extensions = getExtensions(type);
  return extensions.length > 0 ? extensions[0] : undefined;
}

/**
 * Check whether a MIME type is marked as compressible.
 * @param {string} type - MIME type string
 * @returns {boolean}
 */
function isCompressible(type) {
  var entry = getType(type);
  return Boolean(entry && entry.compressible === true);
}

/**
 * Find a MIME type by file extension (without leading dot).
 * @param {string} ext - File extension (e.g., 'json', 'png')
 * @returns {string|undefined}
 */
function findTypeByExtension(ext) {
  if (typeof ext !== 'string') {
    return undefined;
  }
  var cleanExt = ext.replace(/^\./, '').toLowerCase().trim();
  for (var mimeType in db) {
    if (Object.prototype.hasOwnProperty.call(db, mimeType)) {
      var entry = db[mimeType];
      if (entry && Array.isArray(entry.extensions) && entry.extensions.indexOf(cleanExt) !== -1) {
        return mimeType;
      }
    }
  }
  return undefined;
}

/**
 * Get the source of a MIME type definition (e.g., 'iana', 'apache', 'nginx').
 * @param {string} type - MIME type string
 * @returns {string|undefined}
 */
function getSource(type) {
  var entry = getType(type);
  return entry ? entry.source : undefined;
}

/**
 * Get the charset of a MIME type if specified.
 * @param {string} type - MIME type string
 * @returns {string|undefined}
 */
function getCharset(type) {
  var entry = getType(type);
  return entry ? entry.charset : undefined;
}

module.exports = {
  db: db,
  getType: getType,
  getExtensions: getExtensions,
  getDefaultExtension: getDefaultExtension,
  isCompressible: isCompressible,
  findTypeByExtension: findTypeByExtension,
  getSource: getSource,
  getCharset: getCharset
};
package-lock.json
{
  "name": "sample-mime-db",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-mime-db",
      "version": "1.0.0",
      "dependencies": {
        "mime-db": "1.52.0"
      }
    },
    "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"
      }
    }
  }
}
package.json
{
  "name": "sample-mime-db",
  "version": "1.0.0",
  "private": true,
  "description": "Clean-room verification of mime-db MIME type lookup and metadata query",
  "main": "index.js",
  "scripts": {
    "test": "node test/contract.js"
  },
  "dependencies": {
    "mime-db": "1.52.0"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "lookup MIME type metadata and extensions in mime-db",
  "kind": "HOW",
  "packages": [
    "pkg:npm/mime-db@1.52.0"
  ],
  "symbols": [
    "mime-db"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@11.8.0",
    "runtime": "node@24.13.1"
  }
}
test/contract.js
'use strict';

var assert = require('assert');
var sample = require('../index.js');

// 1. Raw database sanity check
assert.strictEqual(typeof sample.db, 'object');
assert.ok(sample.db !== null);
assert.ok(Object.keys(sample.db).length > 1000);

// 2. application/json entry check
var jsonEntry = sample.getType('application/json');
assert.ok(jsonEntry, 'application/json should exist in mime-db');
assert.strictEqual(jsonEntry.source, 'iana');
assert.strictEqual(jsonEntry.charset, 'UTF-8');
assert.strictEqual(jsonEntry.compressible, true);
assert.deepStrictEqual(jsonEntry.extensions, ['json', 'map']);

// 3. text/html entry check
var htmlEntry = sample.getType('text/html');
assert.ok(htmlEntry, 'text/html should exist in mime-db');
assert.strictEqual(htmlEntry.source, 'iana');
assert.strictEqual(htmlEntry.compressible, true);
assert.ok(Array.isArray(htmlEntry.extensions));
assert.ok(htmlEntry.extensions.indexOf('html') !== -1);
assert.ok(htmlEntry.extensions.indexOf('htm') !== -1);

// 4. image/png entry check
var pngEntry = sample.getType('image/png');
assert.ok(pngEntry, 'image/png should exist in mime-db');
assert.strictEqual(pngEntry.source, 'iana');
assert.strictEqual(pngEntry.compressible, false);
assert.deepStrictEqual(pngEntry.extensions, ['png']);

// 5. Non-existent MIME type
assert.strictEqual(sample.getType('application/x-non-existent-fake-type'), undefined);
assert.strictEqual(sample.getType(null), undefined);
assert.strictEqual(sample.getType(123), undefined);

// 6. Case-insensitivity in getType
assert.deepStrictEqual(sample.getType('Application/JSON'), jsonEntry);
assert.deepStrictEqual(sample.getType('TEXT/HTML '), htmlEntry);

// 7. getExtensions
assert.deepStrictEqual(sample.getExtensions('application/json'), ['json', 'map']);
assert.deepStrictEqual(sample.getExtensions('image/png'), ['png']);
assert.deepStrictEqual(sample.getExtensions('non/existent'), []);

// 8. getDefaultExtension
assert.strictEqual(sample.getDefaultExtension('application/json'), 'json');
assert.strictEqual(sample.getDefaultExtension('image/png'), 'png');
assert.strictEqual(sample.getDefaultExtension('text/html'), 'html');
assert.strictEqual(sample.getDefaultExtension('non/existent'), undefined);

// 9. isCompressible
assert.strictEqual(sample.isCompressible('application/json'), true);
assert.strictEqual(sample.isCompressible('text/html'), true);
assert.strictEqual(sample.isCompressible('image/png'), false);
assert.strictEqual(sample.isCompressible('application/pdf'), false);
assert.strictEqual(sample.isCompressible('image/svg+xml'), true);
assert.strictEqual(sample.isCompressible('non/existent'), false);

// 10. findTypeByExtension
assert.strictEqual(sample.findTypeByExtension('json'), 'application/json');
assert.strictEqual(sample.findTypeByExtension('.json'), 'application/json');
assert.strictEqual(sample.findTypeByExtension('png'), 'image/png');
assert.strictEqual(sample.findTypeByExtension('svg'), 'image/svg+xml');
assert.strictEqual(sample.findTypeByExtension('unknownext123'), undefined);
assert.strictEqual(sample.findTypeByExtension(null), undefined);

// 11. getSource
assert.strictEqual(sample.getSource('application/json'), 'iana');
assert.strictEqual(sample.getSource('text/html'), 'iana');
assert.strictEqual(sample.getSource('non/existent'), undefined);

// 12. getCharset
assert.strictEqual(sample.getCharset('application/json'), 'UTF-8');
assert.strictEqual(sample.getCharset('image/png'), undefined);
assert.strictEqual(sample.getCharset('non/existent'), undefined);

console.log('PASS: mime-db contract verified');

Исходный сидер

аноним