From e835707341e9d7ffb7c60b5f5d07af352d316362 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Sat, 8 Jun 2024 19:24:35 +0800 Subject: [PATCH] feat: support cjs and esm both BREAKING CHANGE: drop Node.js < 18 support https://github.com/eggjs/egg/issues/5257 --- .eslintrc | 5 ++- .github/workflows/nodejs.yml | 17 +++++++++ .github/workflows/release.yml | 13 +++++++ .gitignore | 2 + .travis.yml | 12 ------ History.md => CHANGELOG.md | 0 README.md | 28 +++++++++++--- appveyor.yml | 16 -------- index.js | 15 -------- package.json | 70 ++++++++++++++++++++++++----------- src/index.ts | 18 +++++++++ test/index.test.js | 14 ------- test/index.test.ts | 20 ++++++++++ tsconfig.json | 10 +++++ 14 files changed, 155 insertions(+), 85 deletions(-) create mode 100644 .github/workflows/nodejs.yml create mode 100644 .github/workflows/release.yml delete mode 100644 .travis.yml rename History.md => CHANGELOG.md (100%) delete mode 100644 appveyor.yml delete mode 100644 index.js create mode 100644 src/index.ts delete mode 100644 test/index.test.js create mode 100644 test/index.test.ts create mode 100644 tsconfig.json diff --git a/.eslintrc b/.eslintrc index c799fe5..9bcdb46 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,3 +1,6 @@ { - "extends": "eslint-config-egg" + "extends": [ + "eslint-config-egg/typescript", + "eslint-config-egg/lib/rules/enforce-node-prefix" + ] } diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml new file mode 100644 index 0000000..80bc671 --- /dev/null +++ b/.github/workflows/nodejs.yml @@ -0,0 +1,17 @@ +name: Node.js CI + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +jobs: + Job: + name: Node.js + uses: node-modules/github-actions/.github/workflows/node-test.yml@master + with: + os: 'ubuntu-latest' + version: '18, 20, 22' + secrets: + CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..1c6cbb1 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,13 @@ +name: Release + +on: + push: + branches: [ master ] + +jobs: + release: + name: Node.js + uses: node-modules/github-actions/.github/workflows/node-release.yml@master + secrets: + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} + GIT_TOKEN: ${{ secrets.GIT_TOKEN }} diff --git a/.gitignore b/.gitignore index ba2a97b..affe76b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ node_modules coverage +dist +.tshy* diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index bdf6f64..0000000 --- a/.travis.yml +++ /dev/null @@ -1,12 +0,0 @@ -sudo: false -language: node_js -node_js: - - '6' - - '8' - - '10' -install: - - npm i npminstall && npminstall -script: - - npm run ci -after_script: - - npminstall codecov && codecov diff --git a/History.md b/CHANGELOG.md similarity index 100% rename from History.md rename to CHANGELOG.md diff --git a/README.md b/README.md index 605d6c4..d653a70 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,33 @@ -## cache-content-type +# cache-content-type + +[![NPM version][npm-image]][npm-url] +[![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) +[![Test coverage][codecov-image]][codecov-url] +[![Known Vulnerabilities][snyk-image]][snyk-url] +[![npm download][download-image]][download-url] + +[npm-image]: https://img.shields.io/npm/v/cache-content-type.svg?style=flat-square +[npm-url]: https://npmjs.org/package/cache-content-type +[codecov-image]: https://codecov.io/github/node-modules/cache-content-type/coverage.svg?branch=master +[codecov-url]: https://codecov.io/github/node-modules/cache-content-type?branch=master +[snyk-image]: https://snyk.io/test/npm/cache-content-type/badge.svg?style=flat-square +[snyk-url]: https://snyk.io/test/npm/cache-content-type +[download-image]: https://img.shields.io/npm/dm/cache-content-type.svg?style=flat-square +[download-url]: https://npmjs.org/package/cache-content-type The same as [mime-types](https://github.com/jshttp/mime-types)'s contentType method, but with result cached. -### Install +## Install ```bash npm i cache-content-type ``` -### Usage +## Usage + +```ts +import { getType } from 'cache-content-type'; -```js -const getType = require('cache-content-type'); const contentType = getType('html'); -assert(contentType === 'text/html; charset=utf-8'); +assert.equal(contentType, 'text/html; charset=utf-8'); ``` diff --git a/appveyor.yml b/appveyor.yml deleted file mode 100644 index 3dc637e..0000000 --- a/appveyor.yml +++ /dev/null @@ -1,16 +0,0 @@ -environment: - matrix: - - nodejs_version: '6' - - nodejs_version: '8' - - nodejs_version: '10' - -install: - - ps: Install-Product node $env:nodejs_version - - npm i npminstall && node_modules\.bin\npminstall - -test_script: - - node --version - - npm --version - - npm run test - -build: off diff --git a/index.js b/index.js deleted file mode 100644 index 60e6667..0000000 --- a/index.js +++ /dev/null @@ -1,15 +0,0 @@ -'use strict'; - -const mimeTypes = require('mime-types'); -const LRU = require('ylru'); - -const typeLRUCache = new LRU(100); - -module.exports = type => { - let mimeType = typeLRUCache.get(type); - if (!mimeType) { - mimeType = mimeTypes.contentType(type); - typeLRUCache.set(type, mimeType); - } - return mimeType; -}; diff --git a/package.json b/package.json index d3f0c76..07357e1 100644 --- a/package.json +++ b/package.json @@ -1,26 +1,32 @@ { "name": "cache-content-type", "version": "1.0.1", - "description": "Create a full Content-Type header given a MIME type or extension and catch the result", - "main": "index.js", - "files": [ - "index.js" - ], + "engines": { + "node": ">= 18.0.0" + }, + "description": "Create a full Content-Type header given a MIME type or extension and cache the result", "scripts": { - "test": "egg-bin test", - "cov": "egg-bin cov", - "ci": "eslint . && npm run cov" + "test": "npm run lint -- --fix && egg-bin test", + "ci": "npm run lint && egg-bin cov && npm run prepublishOnly", + "lint": "eslint src test", + "prepublishOnly": "tshy && tshy-after" }, "dependencies": { - "mime-types": "^2.1.18", - "ylru": "^1.2.0" + "mime-types": "^2.1.35", + "ylru": "^1.4.0" }, "devDependencies": { - "egg-bin": "^4.7.1", - "egg-ci": "^1.8.0", - "eslint": "^5.1.0", - "eslint-config-egg": "^7.0.0", - "mm": "^2.2.0" + "@eggjs/tsconfig": "^1.3.3", + "@types/mime-types": "^2.1.4", + "@types/mocha": "^10.0.1", + "@types/node": "^20.2.5", + "egg-bin": "6", + "eslint": "8", + "eslint-config-egg": "13", + "mm": "3", + "tshy": "^1.15.1", + "tshy-after": "^1.0.0", + "typescript": "^5.4.5" }, "repository": { "type": "git", @@ -31,12 +37,34 @@ "content-type", "lru" ], - "engines": { - "node": ">= 6.0.0" + "author": "dead_horse", + "license": "MIT", + "type": "module", + "tshy": { + "exports": { + "./package.json": "./package.json", + ".": "./src/index.ts" + } }, - "ci": { - "version": "6, 8, 10" + "exports": { + "./package.json": "./package.json", + ".": { + "import": { + "source": "./src/index.ts", + "types": "./dist/esm/index.d.ts", + "default": "./dist/esm/index.js" + }, + "require": { + "source": "./src/index.ts", + "types": "./dist/commonjs/index.d.ts", + "default": "./dist/commonjs/index.js" + } + } }, - "author": "dead_horse", - "license": "MIT" + "files": [ + "dist", + "src" + ], + "main": "./dist/commonjs/index.js", + "types": "./dist/commonjs/index.d.ts" } diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..20760be --- /dev/null +++ b/src/index.ts @@ -0,0 +1,18 @@ +import mimeTypes from 'mime-types'; +import LRU from 'ylru'; + +let typeLRUCache: LRU; + +export function getType(type: string): string | false { + if (!typeLRUCache) { + typeLRUCache = new LRU(100); + } + let mimeType = typeLRUCache.get(type); + if (mimeType === undefined) { + mimeType = mimeTypes.contentType(type); + typeLRUCache.set(type, mimeType); + } + return mimeType; +} + +export default getType; diff --git a/test/index.test.js b/test/index.test.js deleted file mode 100644 index 920d840..0000000 --- a/test/index.test.js +++ /dev/null @@ -1,14 +0,0 @@ -'use strict'; - -const mimeTypes = require('mime-types'); -const assert = require('assert'); -const mm = require('mm'); -const getType = require('../'); - -describe('cache-content-type', () => { - it('should work with cache', () => { - assert(getType('html') === 'text/html; charset=utf-8'); - mm.syncError(mimeTypes, 'contentType', 'mock error'); - assert(getType('html') === 'text/html; charset=utf-8'); - }); -}); diff --git a/test/index.test.ts b/test/index.test.ts new file mode 100644 index 0000000..28c91c7 --- /dev/null +++ b/test/index.test.ts @@ -0,0 +1,20 @@ +import { strict as assert } from 'node:assert'; +import mimeTypes from 'mime-types'; +import mm from 'mm'; +import getTypeFromDefault, { getType } from '../src/index.js'; + +describe('cache-content-type', () => { + afterEach(mm.restore); + + it('should work with cache', () => { + assert.equal(getType('html'), 'text/html; charset=utf-8'); + mm.syncError(mimeTypes, 'contentType', 'mock error'); + assert.equal(getType('html'), 'text/html; charset=utf-8'); + }); + + it('should return false when type not exists', () => { + assert.equal(getTypeFromDefault('html-not-exists'), false); + mm.syncError(mimeTypes, 'contentType', 'mock error'); + assert.equal(getTypeFromDefault('html-not-exists'), false); + }); +}); diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..ff41b73 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "@eggjs/tsconfig", + "compilerOptions": { + "strict": true, + "noImplicitAny": true, + "target": "ES2022", + "module": "NodeNext", + "moduleResolution": "NodeNext" + } +}