mime-explorer 1.1.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/module.d.ts CHANGED
@@ -1,38 +1,16 @@
1
- import type { TypeMap, TypeMeta } from './type';
2
- /**
3
- * Define meta information/extensions for a mime type.
4
- *
5
- * @param key
6
- * @param value
7
- */
8
- export declare function define(key: string, value: string[] | TypeMeta): void;
9
- /**
10
- * Remove meta information.
11
- *
12
- * @param input
13
- */
14
- export declare function undefine(input: string): void;
15
- /**
16
- * Define multiple mime type -> extension/meta mappings.
17
- *
18
- * e.g. mime.define({'audio/ogg', ['oga', 'ogg', 'spx']});
19
- */
20
- export declare function defineMany(typeMap: TypeMap): void;
21
- /**
22
- * Lookup a mime type based on extension
23
- *
24
- * @param input
25
- */
26
- export declare function getType(input: string): string | undefined;
27
- /**
28
- * Get the file extension associated with a mime type.
29
- *
30
- * @param input
31
- */
32
- export declare function getExtension(input: string): string | undefined;
33
- /**
34
- * Retrieve meta information associated with a mime type.
35
- *
36
- * @param input
37
- */
38
- export declare function get(input: string): TypeMeta | undefined;
1
+ import type { IMimeExplorer, RegisterOptions, TypeMeta, TypeMetaCollection } from './types.ts';
2
+ export declare class MimeExplorer implements IMimeExplorer {
3
+ #private;
4
+ /**
5
+ * Create an independent registry, empty by default. Never throws: the first definition
6
+ * of a type or extension wins and unusable entries are skipped without being reported.
7
+ *
8
+ * @param definitions - Extensions or metadata per MIME type, such as the `mime-explorer/db` export.
9
+ */
10
+ constructor(definitions?: TypeMetaCollection);
11
+ register(definitions: TypeMetaCollection, options?: RegisterOptions): boolean;
12
+ unregister(contentType: string): boolean;
13
+ getMeta(contentType: string): TypeMeta | undefined;
14
+ getExtension(contentType: string): string | undefined;
15
+ getType(path: string): string | undefined;
16
+ }
package/dist/type.d.ts CHANGED
@@ -1,20 +1 @@
1
- export type TypeMap = Record<string, string[] | TypeMetaInput>;
2
- export type TypeMeta = {
3
- /**
4
- * Source of the mime type and associated information.
5
- */
6
- source?: string;
7
- /**
8
- * Extensions associated with the mime type.
9
- */
10
- extensions: string[];
11
- /**
12
- * Charset of the mime type content.
13
- */
14
- charset?: string;
15
- /**
16
- * Content of mime type is compressible?
17
- */
18
- compressible?: boolean;
19
- };
20
- export type TypeMetaInput = Partial<TypeMeta>;
1
+ export * from './types.ts';
@@ -0,0 +1,69 @@
1
+ export type TypeMeta = {
2
+ /**
3
+ * Source of the mime type and associated information.
4
+ */
5
+ readonly source?: string;
6
+ /**
7
+ * Extensions associated with the mime type. The first one is the default; empty for a
8
+ * type known only by its metadata.
9
+ */
10
+ readonly extensions: readonly string[];
11
+ /**
12
+ * Charset of the mime type content.
13
+ */
14
+ readonly charset?: string;
15
+ /**
16
+ * Content of mime type is compressible?
17
+ */
18
+ readonly compressible?: boolean;
19
+ };
20
+ export type TypeMetaInput = Partial<TypeMeta>;
21
+ export type TypeMetaCollection = Readonly<Record<string, readonly string[] | TypeMetaInput>>;
22
+ export type TypeMap = TypeMetaCollection;
23
+ export type RegisterOptions = {
24
+ /**
25
+ * Apply nothing and throw `RegistrationError` when any part cannot be applied.
26
+ *
27
+ * @default false
28
+ */
29
+ readonly throwOnFailure?: boolean;
30
+ };
31
+ export interface IMimeExplorer {
32
+ /**
33
+ * Add definitions keyed by MIME type. The first definition of a type or extension wins.
34
+ *
35
+ * @param definitions - Extensions or metadata per MIME type, such as `{ 'text/plain': ['txt'] }`.
36
+ * @param options - Set `throwOnFailure` to apply nothing and throw `RegistrationError` on any failure.
37
+ * @returns `true` when everything was applied, `false` when any part could not be. A contested
38
+ * extension stays listed on its record, but `getType()` keeps returning its first owner.
39
+ */
40
+ register(definitions: TypeMetaCollection, options?: RegisterOptions): boolean;
41
+ /**
42
+ * Remove a MIME type and every extension it owns.
43
+ *
44
+ * @param contentType - A MIME type or Content-Type header, such as `text/plain; charset=utf-8`.
45
+ * @returns `true` when a record was removed, `false` when the type is unknown.
46
+ */
47
+ unregister(contentType: string): boolean;
48
+ /**
49
+ * Find the read-only record of a MIME type.
50
+ *
51
+ * @param contentType - A MIME type or Content-Type header, such as `text/html; charset=utf-8`.
52
+ * @returns The record, or `undefined` when the type is unknown.
53
+ */
54
+ getMeta(contentType: string): TypeMeta | undefined;
55
+ /**
56
+ * Find the MIME type of a file.
57
+ *
58
+ * @param path - An extension, dotted extension, file name or path, such as `/uploads/report.JSON`.
59
+ * @returns The MIME type, such as `application/json`, or `undefined` when the extension is unknown.
60
+ */
61
+ getType(path: string): string | undefined;
62
+ /**
63
+ * Find the default file extension of a MIME type.
64
+ *
65
+ * @param contentType - A MIME type or Content-Type header, such as `text/html; charset=utf-8`.
66
+ * @returns The extension without a leading dot, such as `html`, or `undefined` when the type is unknown or has no extensions.
67
+ */
68
+ getExtension(contentType: string): string | undefined;
69
+ }
package/package.json CHANGED
@@ -1,16 +1,19 @@
1
1
  {
2
2
  "name": "mime-explorer",
3
- "version": "1.1.0",
4
- "description": "This is a library for mime types. It provides an ESM and CJS build.",
5
- "main": "dist/index.cjs",
6
- "module": "dist/index.mjs",
3
+ "version": "2.0.0",
4
+ "description": "Look up MIME types, file extensions and metadata, and register your own mappings.",
5
+ "type": "module",
6
+ "main": "dist/index.mjs",
7
7
  "types": "dist/index.d.ts",
8
8
  "exports": {
9
9
  "./package.json": "./package.json",
10
10
  ".": {
11
11
  "types": "./dist/index.d.ts",
12
- "import": "./dist/index.mjs",
13
- "require": "./dist/index.cjs"
12
+ "default": "./dist/index.mjs"
13
+ },
14
+ "./db": {
15
+ "types": "./dist/db.d.ts",
16
+ "default": "./dist/db.mjs"
14
17
  }
15
18
  },
16
19
  "files": [
@@ -18,14 +21,16 @@
18
21
  ],
19
22
  "scripts": {
20
23
  "build:types": "tsc --emitDeclarationOnly",
21
- "build:js": "rollup -c",
22
- "build": "node bin/db.js && npm run build:types && npm run build:js",
24
+ "build:js": "tsdown",
25
+ "build": "node bin/db.cjs && node -e \"require('node:fs').rmSync('dist', { recursive: true, force: true })\" && npm run build:js",
23
26
  "commit": "npx git-cz",
24
- "lint": "eslint --ext .js,.ts ./src",
27
+ "lint": "eslint src AGENTS.md CLAUDE.md .agents",
25
28
  "lint:fix": "npm run lint -- --fix",
26
- "test": "cross-env NODE_ENV=test jest --config ./test/jest.config.js",
27
- "test:coverage": "npm run test -- --coverage",
28
- "prepare": "npx husky install",
29
+ "test": "npm run test:types && cross-env NODE_ENV=test vitest run --config test/vitest.config.mts",
30
+ "test:types": "tsc -p test/types/tsconfig.json",
31
+ "test:package": "tsc -p test/package/tsconfig.json && node test/package/esm.mjs && node test/package/cjs.cjs",
32
+ "test:coverage": "npm test -- --coverage",
33
+ "prepare": "husky",
29
34
  "prepublishOnly": "npm run build"
30
35
  },
31
36
  "author": {
@@ -50,24 +55,21 @@
50
55
  },
51
56
  "homepage": "https://github.com/Tada5hi/mime-explorer#readme",
52
57
  "devDependencies": {
53
- "@rollup/plugin-json": "^6.0.1",
54
- "@rollup/plugin-node-resolve": "^15.2.3",
55
- "@rollup/plugin-swc": "^0.3.0",
56
- "@rollup/plugin-terser": "^0.4.4",
57
- "@tada5hi/commitlint-config": "^1.1.4",
58
- "@tada5hi/eslint-config-typescript": "^1.2.7",
59
- "@tada5hi/semantic-release": "^0.3.0",
60
- "@tada5hi/tsconfig": "^0.5.0",
61
- "@types/jest": "^29.4.0",
62
- "@types/node": "^20.10.0",
63
- "cross-env": "^7.0.3",
64
- "eslint": "^8.54.0",
65
- "husky": "^8.0.3",
66
- "jest": "^29.7.0",
67
- "mime-db": "^1.52.0",
68
- "rollup": "^4.6.0",
69
- "semantic-release": "^22.0.8",
70
- "ts-jest": "^29.1.1",
71
- "typescript": "^5.3.2"
58
+ "@tada5hi/commitlint-config": "^1.3.3",
59
+ "@tada5hi/eslint-config": "^3.3.1",
60
+ "@tada5hi/tsconfig": "^0.7.2",
61
+ "@types/node": "^24.19.2",
62
+ "@vitest/coverage-v8": "^5.1.0",
63
+ "cross-env": "^10.1.0",
64
+ "eslint": "^10.14.0",
65
+ "husky": "^9.1.7",
66
+ "mime-db": "^1.54.0",
67
+ "tsdown": "^0.23.7",
68
+ "typescript": "~6.0.3",
69
+ "typescript-eslint": "^8.72.1",
70
+ "vitest": "^5.1.0"
71
+ },
72
+ "dependencies": {
73
+ "@ebec/core": "^1.3.2"
72
74
  }
73
75
  }