Skip to content

Commit 2683290

Browse files
authored
feat: support cjs and esm both (#1)
BREAKING CHANGE: drop Node.js < 18 support eggjs/egg#5257 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Introduced Node.js CI workflow for automated testing and releases. - Added TypeScript support and enforced node prefix rules in ESLint configuration. - **Documentation** - Updated README with new badges and revised installation/usage instructions. - **Chores** - Updated `.gitignore` to include `dist` directory and `.tshy*` files. - Updated `package.json` with new dependencies, scripts, and metadata. - **Tests** - Added tests for caching content types functionality. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 8a43ff8 commit 2683290

File tree

14 files changed

+155
-85
lines changed

14 files changed

+155
-85
lines changed

.eslintrc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
{
2-
"extends": "eslint-config-egg"
2+
"extends": [
3+
"eslint-config-egg/typescript",
4+
"eslint-config-egg/lib/rules/enforce-node-prefix"
5+
]
36
}

.github/workflows/nodejs.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: Node.js CI
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
branches: [ master ]
8+
9+
jobs:
10+
Job:
11+
name: Node.js
12+
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
13+
with:
14+
os: 'ubuntu-latest'
15+
version: '18, 20, 22'
16+
secrets:
17+
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

.github/workflows/release.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
name: Release
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
7+
jobs:
8+
release:
9+
name: Node.js
10+
uses: node-modules/github-actions/.github/workflows/node-release.yml@master
11+
secrets:
12+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
13+
GIT_TOKEN: ${{ secrets.GIT_TOKEN }}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
node_modules
22
coverage
3+
dist
4+
.tshy*

.travis.yml

Lines changed: 0 additions & 12 deletions
This file was deleted.

History.md renamed to CHANGELOG.md

File renamed without changes.

README.md

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,33 @@
1-
## cache-content-type
1+
# cache-content-type
2+
3+
[![NPM version][npm-image]][npm-url]
4+
[![Node.js CI](https://github.com/node-modules/cache-content-type/actions/workflows/nodejs.yml/badge.svg)](https://github.com/node-modules/cache-content-type/actions/workflows/nodejs.yml)
5+
[![Test coverage][codecov-image]][codecov-url]
6+
[![Known Vulnerabilities][snyk-image]][snyk-url]
7+
[![npm download][download-image]][download-url]
8+
9+
[npm-image]: https://img.shields.io/npm/v/cache-content-type.svg?style=flat-square
10+
[npm-url]: https://npmjs.org/package/cache-content-type
11+
[codecov-image]: https://codecov.io/github/node-modules/cache-content-type/coverage.svg?branch=master
12+
[codecov-url]: https://codecov.io/github/node-modules/cache-content-type?branch=master
13+
[snyk-image]: https://snyk.io/test/npm/cache-content-type/badge.svg?style=flat-square
14+
[snyk-url]: https://snyk.io/test/npm/cache-content-type
15+
[download-image]: https://img.shields.io/npm/dm/cache-content-type.svg?style=flat-square
16+
[download-url]: https://npmjs.org/package/cache-content-type
217

318
The same as [mime-types](https://github.com/jshttp/mime-types)'s contentType method, but with result cached.
419

5-
### Install
20+
## Install
621

722
```bash
823
npm i cache-content-type
924
```
1025

11-
### Usage
26+
## Usage
27+
28+
```ts
29+
import { getType } from 'cache-content-type';
1230

13-
```js
14-
const getType = require('cache-content-type');
1531
const contentType = getType('html');
16-
assert(contentType === 'text/html; charset=utf-8');
32+
assert.equal(contentType, 'text/html; charset=utf-8');
1733
```

appveyor.yml

Lines changed: 0 additions & 16 deletions
This file was deleted.

index.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

package.json

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
11
{
22
"name": "cache-content-type",
33
"version": "1.0.1",
4-
"description": "Create a full Content-Type header given a MIME type or extension and catch the result",
5-
"main": "index.js",
6-
"files": [
7-
"index.js"
8-
],
4+
"engines": {
5+
"node": ">= 18.0.0"
6+
},
7+
"description": "Create a full Content-Type header given a MIME type or extension and cache the result",
98
"scripts": {
10-
"test": "egg-bin test",
11-
"cov": "egg-bin cov",
12-
"ci": "eslint . && npm run cov"
9+
"test": "npm run lint -- --fix && egg-bin test",
10+
"ci": "npm run lint && egg-bin cov && npm run prepublishOnly",
11+
"lint": "eslint src test",
12+
"prepublishOnly": "tshy && tshy-after"
1313
},
1414
"dependencies": {
15-
"mime-types": "^2.1.18",
16-
"ylru": "^1.2.0"
15+
"mime-types": "^2.1.35",
16+
"ylru": "^1.4.0"
1717
},
1818
"devDependencies": {
19-
"egg-bin": "^4.7.1",
20-
"egg-ci": "^1.8.0",
21-
"eslint": "^5.1.0",
22-
"eslint-config-egg": "^7.0.0",
23-
"mm": "^2.2.0"
19+
"@eggjs/tsconfig": "^1.3.3",
20+
"@types/mime-types": "^2.1.4",
21+
"@types/mocha": "^10.0.1",
22+
"@types/node": "^20.2.5",
23+
"egg-bin": "6",
24+
"eslint": "8",
25+
"eslint-config-egg": "13",
26+
"mm": "3",
27+
"tshy": "^1.15.1",
28+
"tshy-after": "^1.0.0",
29+
"typescript": "^5.4.5"
2430
},
2531
"repository": {
2632
"type": "git",
@@ -31,12 +37,34 @@
3137
"content-type",
3238
"lru"
3339
],
34-
"engines": {
35-
"node": ">= 6.0.0"
40+
"author": "dead_horse",
41+
"license": "MIT",
42+
"type": "module",
43+
"tshy": {
44+
"exports": {
45+
"./package.json": "./package.json",
46+
".": "./src/index.ts"
47+
}
3648
},
37-
"ci": {
38-
"version": "6, 8, 10"
49+
"exports": {
50+
"./package.json": "./package.json",
51+
".": {
52+
"import": {
53+
"source": "./src/index.ts",
54+
"types": "./dist/esm/index.d.ts",
55+
"default": "./dist/esm/index.js"
56+
},
57+
"require": {
58+
"source": "./src/index.ts",
59+
"types": "./dist/commonjs/index.d.ts",
60+
"default": "./dist/commonjs/index.js"
61+
}
62+
}
3963
},
40-
"author": "dead_horse",
41-
"license": "MIT"
64+
"files": [
65+
"dist",
66+
"src"
67+
],
68+
"main": "./dist/commonjs/index.js",
69+
"types": "./dist/commonjs/index.d.ts"
4270
}

src/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import mimeTypes from 'mime-types';
2+
import LRU from 'ylru';
3+
4+
let typeLRUCache: LRU;
5+
6+
export function getType(type: string): string | false {
7+
if (!typeLRUCache) {
8+
typeLRUCache = new LRU(100);
9+
}
10+
let mimeType = typeLRUCache.get<string | false>(type);
11+
if (mimeType === undefined) {
12+
mimeType = mimeTypes.contentType(type);
13+
typeLRUCache.set(type, mimeType);
14+
}
15+
return mimeType;
16+
}
17+
18+
export default getType;

test/index.test.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

test/index.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { strict as assert } from 'node:assert';
2+
import mimeTypes from 'mime-types';
3+
import mm from 'mm';
4+
import getTypeFromDefault, { getType } from '../src/index.js';
5+
6+
describe('cache-content-type', () => {
7+
afterEach(mm.restore);
8+
9+
it('should work with cache', () => {
10+
assert.equal(getType('html'), 'text/html; charset=utf-8');
11+
mm.syncError(mimeTypes, 'contentType', 'mock error');
12+
assert.equal(getType('html'), 'text/html; charset=utf-8');
13+
});
14+
15+
it('should return false when type not exists', () => {
16+
assert.equal(getTypeFromDefault('html-not-exists'), false);
17+
mm.syncError(mimeTypes, 'contentType', 'mock error');
18+
assert.equal(getTypeFromDefault('html-not-exists'), false);
19+
});
20+
});

tsconfig.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "@eggjs/tsconfig",
3+
"compilerOptions": {
4+
"strict": true,
5+
"noImplicitAny": true,
6+
"target": "ES2022",
7+
"module": "NodeNext",
8+
"moduleResolution": "NodeNext"
9+
}
10+
}

0 commit comments

Comments
 (0)