Skip to content

Commit 0d29edd

Browse files
committed
Use vitest, add prettier
1 parent f3f16e8 commit 0d29edd

13 files changed

+3853
-6449
lines changed

.eslintrc.js

-39
This file was deleted.

.github/workflows/main.yml

+2-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ jobs:
1818
- uses: actions/setup-node@v4
1919
with:
2020
node-version: ${{ matrix.node }}
21-
cache: 'npm'
21+
cache: "npm"
2222
- run: npm install
23-
- run: npm run lint
24-
- run: npm run test
23+
- run: npm run check

README.md

+11-20
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
koa-helmet
2-
==========
1+
# koa-helmet
32

43
[![Version](https://img.shields.io/npm/v/koa-helmet.svg)](https://www.npmjs.com/package/koa-helmet)
5-
[![Dependency Status](https://img.shields.io/david/venables/koa-helmet.svg)](https://david-dm.org/venables/koa-helmet)
6-
[![js-semistandard-style](https://img.shields.io/badge/code%20style-semistandard-brightgreen.svg?style=flat-square)](https://github.com/Flet/semistandard)
74
[![Downloads](https://img.shields.io/npm/dm/koa-helmet.svg)](https://www.npmjs.com/package/koa-helmet)
85

96
koa-helmet is a wrapper for [helmet](https://github.com/helmetjs/helmet) to work with [koa](https://github.com/koajs/koa). It provides important security headers to make your app more secure by default.
107

11-
Installation
12-
------------
8+
## Installation
139

1410
```sh
1511
npm i koa-helmet
@@ -19,8 +15,7 @@ npm i koa-helmet
1915
yarn add koa-helmet
2016
```
2117

22-
Usage
23-
-----
18+
## Usage
2419

2520
Usage is the same as [helmet](https://github.com/helmetjs/helmet)
2621

@@ -46,36 +41,32 @@ app.use(helmet.xssFilter());
4641

4742
You can see more in [the documentation](https://helmetjs.github.io/docs/).
4843

49-
Example
50-
-------
44+
## Example
5145

5246
```js
53-
import Koa from 'koa';
54-
import helmet from 'koa-helmet';
47+
import Koa from "koa";
48+
import helmet from "koa-helmet";
5549

5650
const app = new Koa();
5751

5852
app.use(helmet());
5953

6054
app.use((ctx) => {
61-
ctx.body = "Hello World"
55+
ctx.body = "Hello World";
6256
});
6357

6458
app.listen(4000);
6559
```
6660

67-
68-
Testing
69-
-------
61+
## Testing
7062

7163
To run the tests, simply run
7264

7365
```
7466
npm test
7567
```
7668

77-
Versioning
78-
----------
69+
## Versioning
7970

80-
* koa-helmet >=2.x (master branch) supports koa 2.x
81-
* koa-helmet 1.x ([koa-1](https://github.com/venables/koa-helmet/tree/koa-1) branch) supports koa 0.x and koa 1.x
71+
- koa-helmet >=2.x (master branch) supports koa 2.x
72+
- koa-helmet 1.x ([koa-1](https://github.com/venables/koa-helmet/tree/koa-1) branch) supports koa 0.x and koa 1.x

eslint.config.mjs

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// @ts-check
2+
3+
import eslint from "@eslint/js";
4+
import configPrettier from "eslint-config-prettier";
5+
import tseslint from "typescript-eslint";
6+
import globals from "globals";
7+
8+
export default tseslint.config(
9+
eslint.configs.recommended,
10+
...tseslint.configs.strictTypeChecked,
11+
...tseslint.configs.stylisticTypeChecked,
12+
{
13+
languageOptions: {
14+
parserOptions: {
15+
project: true,
16+
tsconfigRootDir: import.meta.dirname,
17+
},
18+
globals: {
19+
...globals.node,
20+
},
21+
},
22+
rules: {
23+
"@typescript-eslint/no-require-imports": "off",
24+
"@typescript-eslint/unbound-method": "off",
25+
},
26+
},
27+
/**
28+
* Javascript files.
29+
*
30+
* Ignore type-checking
31+
*/
32+
{
33+
files: ["**/*.{js,mjs,cjs}"],
34+
...tseslint.configs.disableTypeChecked,
35+
},
36+
37+
/**
38+
* Disable rules that could conflict with prettier.
39+
* This should be the last rule.
40+
*/
41+
configPrettier,
42+
);

koa-helmet.d.ts

+68-51
Original file line numberDiff line numberDiff line change
@@ -5,65 +5,82 @@
55
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
66
// TypeScript Version: 2.3
77

8-
import helmet from 'helmet';
9-
import { Middleware, Context } from 'koa';
8+
import helmet from "helmet";
9+
import { Middleware, Context } from "koa";
1010

1111
type HelmetOptions = helmet.HelmetOptions;
1212

1313
declare namespace koaHelmet {
14-
type KoaHelmetContentSecurityPolicyDirectiveFunction = (req?: Context["req"], res?: Context["res"]) => string;
14+
type KoaHelmetContentSecurityPolicyDirectiveFunction = (
15+
req?: Context["req"],
16+
res?: Context["res"],
17+
) => string;
1518

16-
type KoaHelmetCspDirectiveValue = string | KoaHelmetContentSecurityPolicyDirectiveFunction;
19+
type KoaHelmetCspDirectiveValue =
20+
| string
21+
| KoaHelmetContentSecurityPolicyDirectiveFunction;
1722

18-
interface KoaHelmetContentSecurityPolicyDirectives {
19-
baseUri?: KoaHelmetCspDirectiveValue[];
20-
childSrc?: KoaHelmetCspDirectiveValue[];
21-
connectSrc?: KoaHelmetCspDirectiveValue[];
22-
defaultSrc?: KoaHelmetCspDirectiveValue[];
23-
fontSrc?: KoaHelmetCspDirectiveValue[];
24-
formAction?: KoaHelmetCspDirectiveValue[];
25-
frameAncestors?: KoaHelmetCspDirectiveValue[];
26-
frameSrc?: KoaHelmetCspDirectiveValue[];
27-
imgSrc?: KoaHelmetCspDirectiveValue[];
28-
mediaSrc?: KoaHelmetCspDirectiveValue[];
29-
objectSrc?: KoaHelmetCspDirectiveValue[];
30-
pluginTypes?: KoaHelmetCspDirectiveValue[];
31-
prefetchSrc?: KoaHelmetCspDirectiveValue[];
32-
reportTo?: string;
33-
reportUri?: string;
34-
sandbox?: KoaHelmetCspDirectiveValue[];
35-
scriptSrc?: KoaHelmetCspDirectiveValue[];
36-
scriptSrcAttr?: KoaHelmetCspDirectiveValue[];
37-
scriptSrcElem?: KoaHelmetCspDirectiveValue[];
38-
styleSrc?: KoaHelmetCspDirectiveValue[];
39-
styleSrcAttr?: KoaHelmetCspDirectiveValue[];
40-
styleSrcElem?: KoaHelmetCspDirectiveValue[];
41-
workerSrc?: KoaHelmetCspDirectiveValue[];
42-
}
23+
interface KoaHelmetContentSecurityPolicyDirectives {
24+
baseUri?: KoaHelmetCspDirectiveValue[];
25+
childSrc?: KoaHelmetCspDirectiveValue[];
26+
connectSrc?: KoaHelmetCspDirectiveValue[];
27+
defaultSrc?: KoaHelmetCspDirectiveValue[];
28+
fontSrc?: KoaHelmetCspDirectiveValue[];
29+
formAction?: KoaHelmetCspDirectiveValue[];
30+
frameAncestors?: KoaHelmetCspDirectiveValue[];
31+
frameSrc?: KoaHelmetCspDirectiveValue[];
32+
imgSrc?: KoaHelmetCspDirectiveValue[];
33+
mediaSrc?: KoaHelmetCspDirectiveValue[];
34+
objectSrc?: KoaHelmetCspDirectiveValue[];
35+
pluginTypes?: KoaHelmetCspDirectiveValue[];
36+
prefetchSrc?: KoaHelmetCspDirectiveValue[];
37+
reportTo?: string;
38+
reportUri?: string;
39+
sandbox?: KoaHelmetCspDirectiveValue[];
40+
scriptSrc?: KoaHelmetCspDirectiveValue[];
41+
scriptSrcAttr?: KoaHelmetCspDirectiveValue[];
42+
scriptSrcElem?: KoaHelmetCspDirectiveValue[];
43+
styleSrc?: KoaHelmetCspDirectiveValue[];
44+
styleSrcAttr?: KoaHelmetCspDirectiveValue[];
45+
styleSrcElem?: KoaHelmetCspDirectiveValue[];
46+
workerSrc?: KoaHelmetCspDirectiveValue[];
47+
}
4348

44-
interface KoaHelmetContentSecurityPolicyConfiguration {
45-
reportOnly?: boolean;
46-
useDefaults?: boolean;
47-
directives?: KoaHelmetContentSecurityPolicyDirectives;
48-
}
49+
interface KoaHelmetContentSecurityPolicyConfiguration {
50+
reportOnly?: boolean;
51+
useDefaults?: boolean;
52+
directives?: KoaHelmetContentSecurityPolicyDirectives;
53+
}
4954

50-
interface KoaHelmet {
51-
(options?: HelmetOptions): Middleware;
52-
contentSecurityPolicy(options?: KoaHelmetContentSecurityPolicyConfiguration): Middleware;
53-
crossOriginEmbedderPolicy(options?: HelmetOptions['crossOriginEmbedderPolicy']): Middleware;
54-
crossOriginOpenerPolicy(options?: HelmetOptions['crossOriginOpenerPolicy']): Middleware;
55-
crossOriginResourcePolicy(options?: HelmetOptions['crossOriginResourcePolicy']): Middleware;
56-
dnsPrefetchControl(options?: HelmetOptions['dnsPrefetchControl']): Middleware;
57-
expectCt(options?: HelmetOptions['expectCt']): Middleware;
58-
frameguard(options?: HelmetOptions['frameguard']): Middleware;
59-
hidePoweredBy(options?: HelmetOptions['hidePoweredBy']): Middleware;
60-
hsts(options?: HelmetOptions['hsts']): Middleware;
61-
ieNoOpen(options?: HelmetOptions['ieNoOpen']): Middleware;
62-
noSniff(options?: HelmetOptions['noSniff']): Middleware;
63-
permittedCrossDomainPolicies(options?: HelmetOptions['permittedCrossDomainPolicies']): Middleware;
64-
referrerPolicy(options?: HelmetOptions['referrerPolicy']): Middleware;
65-
xssFilter(options?: HelmetOptions['xssFilter']): Middleware;
66-
}
55+
interface KoaHelmet {
56+
(options?: HelmetOptions): Middleware;
57+
contentSecurityPolicy(
58+
options?: KoaHelmetContentSecurityPolicyConfiguration,
59+
): Middleware;
60+
crossOriginEmbedderPolicy(
61+
options?: HelmetOptions["crossOriginEmbedderPolicy"],
62+
): Middleware;
63+
crossOriginOpenerPolicy(
64+
options?: HelmetOptions["crossOriginOpenerPolicy"],
65+
): Middleware;
66+
crossOriginResourcePolicy(
67+
options?: HelmetOptions["crossOriginResourcePolicy"],
68+
): Middleware;
69+
dnsPrefetchControl(
70+
options?: HelmetOptions["dnsPrefetchControl"],
71+
): Middleware;
72+
expectCt(options?: HelmetOptions["expectCt"]): Middleware;
73+
frameguard(options?: HelmetOptions["frameguard"]): Middleware;
74+
hidePoweredBy(options?: HelmetOptions["hidePoweredBy"]): Middleware;
75+
hsts(options?: HelmetOptions["hsts"]): Middleware;
76+
ieNoOpen(options?: HelmetOptions["ieNoOpen"]): Middleware;
77+
noSniff(options?: HelmetOptions["noSniff"]): Middleware;
78+
permittedCrossDomainPolicies(
79+
options?: HelmetOptions["permittedCrossDomainPolicies"],
80+
): Middleware;
81+
referrerPolicy(options?: HelmetOptions["referrerPolicy"]): Middleware;
82+
xssFilter(options?: HelmetOptions["xssFilter"]): Middleware;
83+
}
6784
}
6885

6986
declare const koaHelmet: koaHelmet.KoaHelmet;

lib/koa-helmet.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
1-
'use strict';
1+
"use strict";
22

3-
const helmet = require('helmet');
4-
const { promisify } = require('util');
3+
const helmet = require("helmet");
4+
const { promisify } = require("util");
55

66
const koaHelmet = function () {
77
const helmetPromise = promisify(helmet.apply(null, arguments));
88

99
const middleware = (ctx, next) => {
1010
return helmetPromise(ctx.req, ctx.res).then(next);
1111
};
12-
middleware._name = 'helmet';
12+
middleware._name = "helmet";
1313
return middleware;
1414
};
1515

1616
Object.keys(helmet).forEach(function (helmetMethod) {
1717
koaHelmet[helmetMethod] = function () {
18-
const methodPromise = promisify(helmet[helmetMethod].apply(null, arguments));
18+
const methodPromise = promisify(
19+
helmet[helmetMethod].apply(null, arguments),
20+
);
1921

2022
return (ctx, next) => {
2123
return methodPromise(ctx.req, ctx.res).then(next);
2224
};
2325
};
2426
Object.keys(helmet[helmetMethod]).forEach((methodExports) => {
25-
koaHelmet[helmetMethod][methodExports] = helmet[helmetMethod][methodExports];
27+
koaHelmet[helmetMethod][methodExports] =
28+
helmet[helmetMethod][methodExports];
2629
});
2730
});
2831

0 commit comments

Comments
 (0)