Skip to content

feat: support cjs and esm both #1

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

Merged
merged 1 commit into from
Jun 8, 2024
Merged
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
5 changes: 4 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"extends": "eslint-config-egg"
"extends": [
"eslint-config-egg/typescript",
"eslint-config-egg/lib/rules/enforce-node-prefix"
]
}
17 changes: 17 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -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 }}
13 changes: 13 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -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 }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
node_modules
coverage
dist
.tshy*
12 changes: 0 additions & 12 deletions .travis.yml

This file was deleted.

File renamed without changes.
28 changes: 22 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -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');
```
16 changes: 0 additions & 16 deletions appveyor.yml

This file was deleted.

15 changes: 0 additions & 15 deletions index.js

This file was deleted.

70 changes: 49 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"
}
18 changes: 18 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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<string | false>(type);
if (mimeType === undefined) {
mimeType = mimeTypes.contentType(type);
typeLRUCache.set(type, mimeType);
}
return mimeType;
}
Comment on lines +6 to +16
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure typeLRUCache is initialized before use to prevent runtime errors.

- let typeLRUCache: LRU;
+ let typeLRUCache: LRU = new LRU(100);
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
export function getType(type: string): string | false {
if (!typeLRUCache) {
typeLRUCache = new LRU(100);
}
let mimeType = typeLRUCache.get<string | false>(type);
if (mimeType === undefined) {
mimeType = mimeTypes.contentType(type);
typeLRUCache.set(type, mimeType);
}
return mimeType;
}
export function getType(type: string): string | false {
let typeLRUCache: LRU = new LRU(100);
let mimeType = typeLRUCache.get<string | false>(type);
if (mimeType === undefined) {
mimeType = mimeTypes.contentType(type);
typeLRUCache.set(type, mimeType);
}
return mimeType;
}


export default getType;
14 changes: 0 additions & 14 deletions test/index.test.js

This file was deleted.

20 changes: 20 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
10 changes: 10 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "@eggjs/tsconfig",
"compilerOptions": {
"strict": true,
"noImplicitAny": true,
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext"
}
}
Loading