Skip to content

Commit

Permalink
Use vitest, add prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
venables committed Nov 12, 2024
1 parent f3f16e8 commit 0d29edd
Show file tree
Hide file tree
Showing 13 changed files with 3,853 additions and 6,449 deletions.
39 changes: 0 additions & 39 deletions .eslintrc.js

This file was deleted.

5 changes: 2 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ jobs:
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
cache: 'npm'
cache: "npm"
- run: npm install
- run: npm run lint
- run: npm run test
- run: npm run check
31 changes: 11 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
koa-helmet
==========
# koa-helmet

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

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.

Installation
------------
## Installation

```sh
npm i koa-helmet
Expand All @@ -19,8 +15,7 @@ npm i koa-helmet
yarn add koa-helmet
```

Usage
-----
## Usage

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

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

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

Example
-------
## Example

```js
import Koa from 'koa';
import helmet from 'koa-helmet';
import Koa from "koa";
import helmet from "koa-helmet";

const app = new Koa();

app.use(helmet());

app.use((ctx) => {
ctx.body = "Hello World"
ctx.body = "Hello World";
});

app.listen(4000);
```


Testing
-------
## Testing

To run the tests, simply run

```
npm test
```

Versioning
----------
## Versioning

* koa-helmet >=2.x (master branch) supports koa 2.x
* koa-helmet 1.x ([koa-1](https://github.com/venables/koa-helmet/tree/koa-1) branch) supports koa 0.x and koa 1.x
- koa-helmet >=2.x (master branch) supports koa 2.x
- koa-helmet 1.x ([koa-1](https://github.com/venables/koa-helmet/tree/koa-1) branch) supports koa 0.x and koa 1.x
42 changes: 42 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// @ts-check

import eslint from "@eslint/js";
import configPrettier from "eslint-config-prettier";
import tseslint from "typescript-eslint";
import globals from "globals";

export default tseslint.config(
eslint.configs.recommended,
...tseslint.configs.strictTypeChecked,
...tseslint.configs.stylisticTypeChecked,
{
languageOptions: {
parserOptions: {
project: true,
tsconfigRootDir: import.meta.dirname,
},
globals: {
...globals.node,
},
},
rules: {
"@typescript-eslint/no-require-imports": "off",
"@typescript-eslint/unbound-method": "off",
},
},
/**
* Javascript files.
*
* Ignore type-checking
*/
{
files: ["**/*.{js,mjs,cjs}"],
...tseslint.configs.disableTypeChecked,
},

/**
* Disable rules that could conflict with prettier.
* This should be the last rule.
*/
configPrettier,
);
119 changes: 68 additions & 51 deletions koa-helmet.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,65 +5,82 @@
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3

import helmet from 'helmet';
import { Middleware, Context } from 'koa';
import helmet from "helmet";
import { Middleware, Context } from "koa";

type HelmetOptions = helmet.HelmetOptions;

declare namespace koaHelmet {
type KoaHelmetContentSecurityPolicyDirectiveFunction = (req?: Context["req"], res?: Context["res"]) => string;
type KoaHelmetContentSecurityPolicyDirectiveFunction = (
req?: Context["req"],
res?: Context["res"],
) => string;

type KoaHelmetCspDirectiveValue = string | KoaHelmetContentSecurityPolicyDirectiveFunction;
type KoaHelmetCspDirectiveValue =
| string
| KoaHelmetContentSecurityPolicyDirectiveFunction;

interface KoaHelmetContentSecurityPolicyDirectives {
baseUri?: KoaHelmetCspDirectiveValue[];
childSrc?: KoaHelmetCspDirectiveValue[];
connectSrc?: KoaHelmetCspDirectiveValue[];
defaultSrc?: KoaHelmetCspDirectiveValue[];
fontSrc?: KoaHelmetCspDirectiveValue[];
formAction?: KoaHelmetCspDirectiveValue[];
frameAncestors?: KoaHelmetCspDirectiveValue[];
frameSrc?: KoaHelmetCspDirectiveValue[];
imgSrc?: KoaHelmetCspDirectiveValue[];
mediaSrc?: KoaHelmetCspDirectiveValue[];
objectSrc?: KoaHelmetCspDirectiveValue[];
pluginTypes?: KoaHelmetCspDirectiveValue[];
prefetchSrc?: KoaHelmetCspDirectiveValue[];
reportTo?: string;
reportUri?: string;
sandbox?: KoaHelmetCspDirectiveValue[];
scriptSrc?: KoaHelmetCspDirectiveValue[];
scriptSrcAttr?: KoaHelmetCspDirectiveValue[];
scriptSrcElem?: KoaHelmetCspDirectiveValue[];
styleSrc?: KoaHelmetCspDirectiveValue[];
styleSrcAttr?: KoaHelmetCspDirectiveValue[];
styleSrcElem?: KoaHelmetCspDirectiveValue[];
workerSrc?: KoaHelmetCspDirectiveValue[];
}
interface KoaHelmetContentSecurityPolicyDirectives {
baseUri?: KoaHelmetCspDirectiveValue[];
childSrc?: KoaHelmetCspDirectiveValue[];
connectSrc?: KoaHelmetCspDirectiveValue[];
defaultSrc?: KoaHelmetCspDirectiveValue[];
fontSrc?: KoaHelmetCspDirectiveValue[];
formAction?: KoaHelmetCspDirectiveValue[];
frameAncestors?: KoaHelmetCspDirectiveValue[];
frameSrc?: KoaHelmetCspDirectiveValue[];
imgSrc?: KoaHelmetCspDirectiveValue[];
mediaSrc?: KoaHelmetCspDirectiveValue[];
objectSrc?: KoaHelmetCspDirectiveValue[];
pluginTypes?: KoaHelmetCspDirectiveValue[];
prefetchSrc?: KoaHelmetCspDirectiveValue[];
reportTo?: string;
reportUri?: string;
sandbox?: KoaHelmetCspDirectiveValue[];
scriptSrc?: KoaHelmetCspDirectiveValue[];
scriptSrcAttr?: KoaHelmetCspDirectiveValue[];
scriptSrcElem?: KoaHelmetCspDirectiveValue[];
styleSrc?: KoaHelmetCspDirectiveValue[];
styleSrcAttr?: KoaHelmetCspDirectiveValue[];
styleSrcElem?: KoaHelmetCspDirectiveValue[];
workerSrc?: KoaHelmetCspDirectiveValue[];
}

interface KoaHelmetContentSecurityPolicyConfiguration {
reportOnly?: boolean;
useDefaults?: boolean;
directives?: KoaHelmetContentSecurityPolicyDirectives;
}
interface KoaHelmetContentSecurityPolicyConfiguration {
reportOnly?: boolean;
useDefaults?: boolean;
directives?: KoaHelmetContentSecurityPolicyDirectives;
}

interface KoaHelmet {
(options?: HelmetOptions): Middleware;
contentSecurityPolicy(options?: KoaHelmetContentSecurityPolicyConfiguration): Middleware;
crossOriginEmbedderPolicy(options?: HelmetOptions['crossOriginEmbedderPolicy']): Middleware;
crossOriginOpenerPolicy(options?: HelmetOptions['crossOriginOpenerPolicy']): Middleware;
crossOriginResourcePolicy(options?: HelmetOptions['crossOriginResourcePolicy']): Middleware;
dnsPrefetchControl(options?: HelmetOptions['dnsPrefetchControl']): Middleware;
expectCt(options?: HelmetOptions['expectCt']): Middleware;
frameguard(options?: HelmetOptions['frameguard']): Middleware;
hidePoweredBy(options?: HelmetOptions['hidePoweredBy']): Middleware;
hsts(options?: HelmetOptions['hsts']): Middleware;
ieNoOpen(options?: HelmetOptions['ieNoOpen']): Middleware;
noSniff(options?: HelmetOptions['noSniff']): Middleware;
permittedCrossDomainPolicies(options?: HelmetOptions['permittedCrossDomainPolicies']): Middleware;
referrerPolicy(options?: HelmetOptions['referrerPolicy']): Middleware;
xssFilter(options?: HelmetOptions['xssFilter']): Middleware;
}
interface KoaHelmet {
(options?: HelmetOptions): Middleware;
contentSecurityPolicy(
options?: KoaHelmetContentSecurityPolicyConfiguration,
): Middleware;
crossOriginEmbedderPolicy(
options?: HelmetOptions["crossOriginEmbedderPolicy"],
): Middleware;
crossOriginOpenerPolicy(
options?: HelmetOptions["crossOriginOpenerPolicy"],
): Middleware;
crossOriginResourcePolicy(
options?: HelmetOptions["crossOriginResourcePolicy"],
): Middleware;
dnsPrefetchControl(
options?: HelmetOptions["dnsPrefetchControl"],
): Middleware;
expectCt(options?: HelmetOptions["expectCt"]): Middleware;
frameguard(options?: HelmetOptions["frameguard"]): Middleware;
hidePoweredBy(options?: HelmetOptions["hidePoweredBy"]): Middleware;
hsts(options?: HelmetOptions["hsts"]): Middleware;
ieNoOpen(options?: HelmetOptions["ieNoOpen"]): Middleware;
noSniff(options?: HelmetOptions["noSniff"]): Middleware;
permittedCrossDomainPolicies(
options?: HelmetOptions["permittedCrossDomainPolicies"],
): Middleware;
referrerPolicy(options?: HelmetOptions["referrerPolicy"]): Middleware;
xssFilter(options?: HelmetOptions["xssFilter"]): Middleware;
}
}

declare const koaHelmet: koaHelmet.KoaHelmet;
Expand Down
15 changes: 9 additions & 6 deletions lib/koa-helmet.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
'use strict';
"use strict";

const helmet = require('helmet');
const { promisify } = require('util');
const helmet = require("helmet");
const { promisify } = require("util");

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

const middleware = (ctx, next) => {
return helmetPromise(ctx.req, ctx.res).then(next);
};
middleware._name = 'helmet';
middleware._name = "helmet";
return middleware;
};

Object.keys(helmet).forEach(function (helmetMethod) {
koaHelmet[helmetMethod] = function () {
const methodPromise = promisify(helmet[helmetMethod].apply(null, arguments));
const methodPromise = promisify(
helmet[helmetMethod].apply(null, arguments),
);

return (ctx, next) => {
return methodPromise(ctx.req, ctx.res).then(next);
};
};
Object.keys(helmet[helmetMethod]).forEach((methodExports) => {
koaHelmet[helmetMethod][methodExports] = helmet[helmetMethod][methodExports];
koaHelmet[helmetMethod][methodExports] =
helmet[helmetMethod][methodExports];
});
});

Expand Down
Loading

0 comments on commit 0d29edd

Please sign in to comment.