Skip to content

Commit

Permalink
chore: convert to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeed committed Dec 20, 2023
1 parent 50c135b commit b76d07e
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ node_modules

/index.js
/index.mjs
/src/index.js
/src/index.ts
64 changes: 39 additions & 25 deletions bin/index.js → bin/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
// @ts-check
const fs = require('fs');
const { join, dirname } = require('path');
const DB = require('mime-db');
/// <reference types="node" />
import { join, dirname, resolve } from 'node:path';
import { spawnSync } from 'node:child_process';
import * as fs from 'node:fs';
import DB from 'mime-db';

const input = join(__dirname, '../src/$index.js');
const output = join(__dirname, '../src/index.js');
const denomod = join(__dirname, '../deno/mod.ts');
const ROOT = resolve('.');

const input = join(ROOT, 'src/$index.ts');
const denomod = join(ROOT, 'deno/mod.ts');

function write(file: string, data: string) {
let f = resolve(ROOT, file);
fs.writeFileSync(f, data);
console.log('~> "%s" created', file);
}

// iana > mimedb > apache > nginx
// https://github.com/jshttp/mime-types/blob/master/index.js#L156
const scores = { nginx: 1, apache: 2, iana: 4 };

/**
* @param {string} prev
* @param {string} next
* @return {string}
*/
function compare(prev, next) {
const SOURCES = {
nginx: 1,
apache: 2,
iana: 4,
};

function compare(prev: string, next: string): string {
let [p1] = prev.split('/', 1);
let [p2] = next.split('/', 1);

Expand All @@ -28,17 +36,17 @@ function compare(prev, next) {
if (p2 === 'application') return next;
}
// compare sources
let s1 = scores[DB[prev].source] || 3;
let s2 = scores[DB[next].source] || 3;
let s1 = SOURCES[DB[prev].source] || 3;
let s2 = SOURCES[DB[next].source] || 3;
if (s1 !== s2) return s2 > s1 ? next : prev;
return prev.length >= next.length ? next : prev;
}

let dict = {};
let ignore = /[/](x-|vnd\.)/;

let mtype, arr;
let i=0, extn, prev;
let mtype: string, arr: string[];
let i=0, extn: string, prev: string;

for (mtype in DB) {
if (ignore.test(mtype)) continue;
Expand All @@ -50,7 +58,7 @@ for (mtype in DB) {
extn = arr[i];
prev = dict[extn];

if (prev) {
if (prev && prev !== mtype) {
let msg = `Found existing "${extn}" value:`;
msg += `\n (prev) "${prev}"`;
msg += `\n (next) "${mtype}"`;
Expand All @@ -65,31 +73,37 @@ for (mtype in DB) {

let content = fs.readFileSync(input, 'utf8').replace(
'{}', JSON.stringify(dict, null, 2)
);
) + '\n';

let esm = content + 'export { mimes, lookup };\n';
let cjs = content + 'exports.mimes = mimes;\nexports.lookup = lookup;\n';

fs.writeFileSync(output, content);
console.log('~> "src/index.js" created');
// build exports
write('index.mjs', esm);
write('index.js', cjs);

let denodir = dirname(denomod);
fs.existsSync(denodir) || fs.mkdirSync(denodir);

fs.copyFileSync('readme.md', join(denodir, 'readme.md'));
console.log('\n~> "deno/readme.md" created');

fs.writeFileSync(denomod, content.replace(
write('deno/mod.ts', esm.replace(
'function lookup(extn) {',
'function lookup(extn: string): string | undefined {',
).replace(
'const mimes = {',
'const mimes: Record<string, string> = {',
));
console.log('~> "deno/mod.ts" created');

if (!process.env.CI) {
try {
require('child_process').spawnSync('deno', ['fmt', denomod]);
spawnSync('deno', ['fmt', denomod], { cwd: ROOT });
console.log('\n~> $ deno fmt "deno/mod.ts"');
} catch (err) {
console.log('[deno]', err.stack);
}
}

fs.copyFileSync(denomod, 'src/index.ts');
console.log('\n~> "src/index.ts" created');
1 change: 1 addition & 0 deletions bin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"private": true,
"devDependencies": {
"@types/node": "20.10.5",
"mime-db": "1.49.0"
}
}
6 changes: 4 additions & 2 deletions deno/mod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const mimes: Record<string, string> = {
const mimes: Record<string, string> = {
"ez": "application/andrew-inset",
"aw": "application/applixware",
"atom": "application/atom+xml",
Expand Down Expand Up @@ -407,8 +407,10 @@ export const mimes: Record<string, string> = {
"webm": "video/webm",
};

export function lookup(extn: string): string | undefined {
function lookup(extn: string): string | undefined {
let tmp = ("" + extn).trim().toLowerCase();
let idx = tmp.lastIndexOf(".");
return mimes[!~idx ? tmp : tmp.substring(++idx)];
}

export { lookup, mimes };
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,16 @@
"node": ">=10"
},
"scripts": {
"build": "node bin && bundt",
"test": "uvu -r esm test"
"build": "tsm bin/index.ts",
"test": "uvu -r tsm test"
},
"keywords": [
"mime",
"extension",
"mimetype"
],
"devDependencies": {
"bundt": "1.1.1",
"esm": "3.2.25",
"tsm": "2.3.0",
"uvu": "0.5.2"
}
}
4 changes: 2 additions & 2 deletions src/$index.js → src/$index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const mimes = {};
const mimes = {};

export function lookup(extn) {
function lookup(extn) {
let tmp = ('' + extn).trim().toLowerCase();
let idx = tmp.lastIndexOf('.');
return mimes[!~idx ? tmp : tmp.substring(++idx)];
Expand Down
File renamed without changes.

0 comments on commit b76d07e

Please sign in to comment.