Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Experimental) Add ESM support #300

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .github/workflows/esm.yml
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "./db.json" assert { type: "json" }
15 changes: 13 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>",
"Jonathan Ong <[email protected]> (http://jongleberry.com)",
Expand Down Expand Up @@ -38,7 +47,8 @@
"LICENSE",
"README.md",
"db.json",
"index.js"
"index.js",
"index.mjs"
],
"engines": {
"node": ">= 0.6"
Expand All @@ -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",
Expand Down
76 changes: 76 additions & 0 deletions test/esm.mjs
Original file line number Diff line number Diff line change
@@ -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')
})