diff --git a/.github/workflows/esm.yml b/.github/workflows/esm.yml new file mode 100644 index 000000000..7b8160aba --- /dev/null +++ b/.github/workflows/esm.yml @@ -0,0 +1,19 @@ +name: esm ci + +on: + - pull_request + - push + +jobs: + build: + strategy: + matrix: + version: [18, 20] + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: ${{ matrix.version }} + - run: npm install + - run: node test/esm.mjs diff --git a/index.mjs b/index.mjs new file mode 100644 index 000000000..c05da399f --- /dev/null +++ b/index.mjs @@ -0,0 +1 @@ +export { default } from "./db.json" assert { type: "json" } diff --git a/package.json b/package.json index 8956d3c92..747d8e734 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,15 @@ "name": "mime-db", "description": "Media Type Database", "version": "1.52.0", + "main": "index.js", + "module": "index.mjs", + "sideEffects": false, + "exports": { + ".": { + "require": "./index.js", + "import": "./index.mjs" + } + }, "contributors": [ "Douglas Christopher Wilson ", "Jonathan Ong (http://jongleberry.com)", @@ -38,7 +47,8 @@ "LICENSE", "README.md", "db.json", - "index.js" + "index.js", + "index.mjs" ], "engines": { "node": ">= 0.6" @@ -47,7 +57,8 @@ "build": "node scripts/build", "fetch": "node scripts/fetch-apache && node scripts/fetch-iana && node scripts/fetch-nginx", "lint": "eslint .", - "test": "mocha --reporter spec --bail --check-leaks test/", + "test": "mocha --reporter spec --bail --check-leaks test/**/*.js", + "test:esm": "node test/esm.mjs", "test-ci": "nyc --reporter=lcovonly --reporter=text npm test", "test-cov": "nyc --reporter=html --reporter=text npm test", "update": "npm run fetch && npm run build", diff --git a/test/esm.mjs b/test/esm.mjs new file mode 100644 index 000000000..1645439ca --- /dev/null +++ b/test/esm.mjs @@ -0,0 +1,76 @@ +import assert from "node:assert" +import { readdirSync } from "node:fs" +import { resolve } from "node:path" +import { fileURLToPath } from "node:url" +import test from 'node:test'; + +import typer from "media-typer" + +import db from "../index.mjs" + +const __dirname = fileURLToPath(new URL('.', import.meta.url)); +const path = resolve(__dirname, '..', 'src') +const types = [] + +test("should not contain types not in src", async () => { + // collect all source types + for (const file of readdirSync(path)) { + if (!/-types\.json$/.test(file)) continue + + const imported = await import(resolve(path, file), { + assert: { + type: "json" + } + }) + types.push(...Object.keys(imported.default)) + } + + Object.keys(db).forEach((name) => { + assert.ok(types.includes(name), `type ${name} should be in src/`) + }) +}) + +test("should contain only valid mime types", () => { + Object.keys(db).forEach((mime) => { + assert.ok(typer.test(mime), `type ${mime} is a valid mime type`) + }) +}) + +test("should not have any uppercased letters in names", () => { + Object.keys(db).forEach((name) => { + assert.strictEqual(name, name.toLowerCase(), `type ${name} should be lowercase`) + }) +}) + +test("should have .json and .js as having UTF-8 charsets", () => { + assert.strictEqual('UTF-8', db['application/json'].charset) + assert.strictEqual('UTF-8', db['application/javascript'].charset) +}) + +test("should set audio/x-flac with extension=flac", () => { + assert.strictEqual('flac', db['audio/x-flac'].extensions[0], 'should set audio/x-flac with extension=flac') +}) + +test("should have guessed application/mathml+xml", () => { + assert(db['application/mathml+xml']) +}) + +test("should not have an empty .extensions", () => { + assert(Object.entries(db).every((mime) => { + if (!mime.extensions) return true + return mime.extensions.length + })) +}) + +test("should have only lowercase .extensions", () => { + Object.keys(db).forEach((name) => { + (db[name].extensions || []).forEach((ext) => { + assert.strictEqual(ext, ext.toLowerCase(), `extension ${ext} in type ${name} should be lowercase`) + }) + }) +}) + +test("should have the default .extension as the first", () => { + assert.strictEqual(db['text/plain'].extensions[0], 'txt') + assert.strictEqual(db['video/x-matroska'].extensions[0], 'mkv') +})