-
Notifications
You must be signed in to change notification settings - Fork 367
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
49 changed files
with
390 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
/node_modules/ | ||
|
||
/dist/ | ||
/tmp-commonjs-index.ts | ||
|
||
/coverage/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
/dist/ | ||
/coverage/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Helmet maintainer readme | ||
|
||
These are notes for any maintainers of Helmet...which is currently just [Evan Hahn](https://evanhahn.com/). These notes may be of interest to future maintainers, contributors, or people making forks. | ||
|
||
## Releasing Helmet | ||
|
||
tl;dr: | ||
|
||
- `npm publish` builds and releases the `helmet` package | ||
- `npm run build-middleware-package -- $MIDDLEWARE_NAME` builds `$MIDDLEWARE_NAME` and puts it in a temporary directory to be published | ||
|
||
Helmet releases have the following goals: | ||
|
||
- Users should be able to import the package with CommonJS. The following code snippet should work as expected: | ||
|
||
```js | ||
const helmet = require("helmet"); | ||
app.use(helmet()); | ||
app.use(helmet.ieNoOpen()); | ||
``` | ||
|
||
- Users should be able to import the package with ECMAScript modules. The default export should be a function, and the rest of the functions should be available too. The following snippets should work as expected: | ||
|
||
```js | ||
import helmet from "helmet"; | ||
app.use(helmet()); | ||
``` | ||
|
||
```js | ||
import { ieNoOpen } from "helmet"; | ||
app.use(ieNoOpen()); | ||
``` | ||
|
||
- TypeScript users should be able to import Helmet and various exported types. | ||
|
||
```ts | ||
import helmet, { HelmetOptions, ContentSecurityPolicyOptions } from "helmet"; | ||
``` | ||
|
||
- Some middlewares have their own npm packages, such as `helmet-csp` or `frameguard`. | ||
|
||
- `helmet` should have no production dependencies, including middleware packages, to simplify installation. | ||
|
||
To that end, there are special scripts for building `helmet` and for building middleware packages. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#!/usr/bin/env node | ||
import * as path from "path"; | ||
import { fileURLToPath } from "url"; | ||
import rollupTypescript from "@rollup/plugin-typescript"; | ||
import { writeRollup, withCommonJsFile } from "./helpers.js"; | ||
|
||
const thisPath = fileURLToPath(import.meta.url); | ||
const rootPath = path.join(path.dirname(thisPath), ".."); | ||
const esmSourcePath = path.join(rootPath, "index.ts"); | ||
const esmDistPath = path.join(rootPath, "dist", "index.js"); | ||
const commonJsDistPath = path.join(rootPath, "dist", "index.cjs"); | ||
|
||
const compileEsm = () => | ||
writeRollup( | ||
{ | ||
input: esmSourcePath, | ||
plugins: [rollupTypescript({ tsconfig: "./tsconfig-esm.json" })], | ||
}, | ||
{ file: esmDistPath } | ||
); | ||
|
||
const compileCommonjs = () => | ||
withCommonJsFile(esmSourcePath, (commonJsSourcePath) => | ||
writeRollup( | ||
{ | ||
input: commonJsSourcePath, | ||
plugins: [rollupTypescript({ tsconfig: "./tsconfig-commonjs.json" })], | ||
}, | ||
{ | ||
exports: "default", | ||
file: commonJsDistPath, | ||
format: "cjs", | ||
} | ||
) | ||
); | ||
|
||
async function main() { | ||
await compileEsm(); | ||
await compileCommonjs(); | ||
} | ||
|
||
main(process.argv).catch((err) => { | ||
console.error(err); | ||
process.exit(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
#!/usr/bin/env node | ||
// This lets us remove files on all platforms. Notably, `rm` is missing on Windows. | ||
const path = require("path"); | ||
const fs = require("fs"); | ||
import * as path from "path"; | ||
import * as fs from "fs"; | ||
import { fileURLToPath } from "url"; | ||
|
||
const distPath = path.join(__dirname, "..", "dist"); | ||
const thisPath = fileURLToPath(import.meta.url); | ||
const distPath = path.join(path.dirname(thisPath), "..", "dist"); | ||
|
||
fs.rmSync(distPath, { recursive: true, force: true }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import * as path from "path"; | ||
import { promises as fs } from "fs"; | ||
import { rollup } from "rollup"; | ||
|
||
export async function writeRollup(inputOptions, outputOptions) { | ||
const bundle = await rollup(inputOptions); | ||
await bundle.write(outputOptions); | ||
await bundle.close(); | ||
} | ||
|
||
export async function withCommonJsFile(esmSourcePath, fn) { | ||
const commonJsSourcePath = path.join( | ||
path.dirname(esmSourcePath), | ||
"tmp-commonjs-index.ts" | ||
); | ||
|
||
const lines = (await fs.readFile(esmSourcePath, "utf8")).split(/\r?\n/); | ||
const resultLines = lines.slice( | ||
0, | ||
lines.findIndex((line) => line.includes("!helmet-end-of-commonjs")) | ||
); | ||
|
||
try { | ||
await fs.writeFile(commonJsSourcePath, resultLines.join("\n")); | ||
|
||
await fn(commonJsSourcePath); | ||
} finally { | ||
await fs.unlink(commonJsSourcePath); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.