@davaux/helmet 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CLAUDE.md ADDED
@@ -0,0 +1,95 @@
1
+ <!-- pka-generated -->
2
+ # @davaux/helmet
3
+
4
+ > Generated by Project Knowledge Analyzer on 2026-06-06T21:53:10.269Z
5
+
6
+ ## Overview
7
+
8
+ Security headers middleware for Davaux
9
+
10
+ **Version**: 0.8.0
11
+ **Author**: David L Dyess II
12
+ **License**: MIT
13
+ **Repository**: https://codeberg.org/davaux/davaux#readme
14
+
15
+ ## Tech Stack
16
+
17
+ - **Language**: TypeScript
18
+ - **Module System**: ESM (`type: module`)
19
+
20
+ ## Commands
21
+
22
+ - `npm run build` — tsc
23
+ - `npm run typecheck` — tsc --noEmit
24
+
25
+ ## Project Structure
26
+
27
+ ```
28
+ ├── CLAUDE.md
29
+ ├── README.md
30
+ ├── package.json
31
+ ├── tsconfig.json
32
+ └── src/
33
+ └── index.ts
34
+
35
+ ```
36
+
37
+ ## Entry Points
38
+
39
+ - `src/index.ts`
40
+
41
+ ## Files by Type
42
+
43
+ ### Documentation (2)
44
+ - `CLAUDE.md`
45
+ - `README.md`
46
+
47
+ ### Config (2)
48
+ - `package.json`
49
+ - `tsconfig.json`
50
+
51
+ ### Module (1)
52
+ - `src/index.ts`
53
+
54
+
55
+ ## Git
56
+ - **Branch**: main
57
+ - **Last Commit**: chore: Add alpha status note to README
58
+ - **Author**: David Dyess II
59
+ - **Date**: 2026-06-06 15:52:27 -0600
60
+ - **Remote**: https://codeberg.org/davaux/davaux.git
61
+
62
+ ### Recent Commits
63
+ ```
64
+ f527031 chore: Add alpha status note to README
65
+ 90c819e chore: Add repo info to package.json files
66
+ b200d9d feat(davaux)!: Add OmlCacheConfig - opt-in with includes option or opt-out with excludes option; Fix OML implementation to follow OML spec - use output instead of return
67
+ 3bce0c2 chore: Update and add READMEs to packages; update ROADMAP
68
+ fc27c20 fix(davaux): Remove old dist folder on new builds; fix server port per DavauxConfig
69
+ c760659 feat(davaux): Add minify CSS in production builds
70
+ c899ab8 fix(davaux): Added method JS and JSX extenstion to scanner
71
+ 7d99f04 feat(davaux): Add support for declarative partial updates
72
+ 3b47f37 chore: Bump package versions to 0.8.0
73
+ aa0460f chore: Add CHANGELOG.md
74
+ ```
75
+
76
+ ## Dependencies
77
+
78
+
79
+ ### Development
80
+ @types/node, davaux, typescript
81
+
82
+
83
+
84
+
85
+
86
+
87
+
88
+
89
+
90
+ ## Exported Symbols
91
+
92
+ **`src/index.ts`**
93
+ `helmet`, `CspOptions`, `HstsOptions`, `HelmetOptions`
94
+
95
+
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 David L Dyess II
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,85 @@
1
+ # @davaux/helmet
2
+
3
+ Security headers middleware for Davaux. Sets CSP, HSTS, X-Frame-Options, X-Content-Type-Options, Referrer-Policy, Permissions-Policy, and X-DNS-Prefetch-Control with sensible defaults.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @davaux/helmet
9
+ ```
10
+
11
+ ## Setup
12
+
13
+ ```ts
14
+ // davaux.config.ts
15
+ import { defineConfig } from 'davaux/config'
16
+ import { helmet } from '@davaux/helmet'
17
+
18
+ export default defineConfig({
19
+ middleware: [helmet()],
20
+ })
21
+ ```
22
+
23
+ All headers are enabled by default. Pass `false` to disable any of them:
24
+
25
+ ```ts
26
+ helmet({
27
+ hsts: false, // disable in development
28
+ xFrameOptions: 'DENY',
29
+ contentSecurityPolicy: {
30
+ directives: {
31
+ 'script-src': ["'self'", 'https://cdn.example.com'],
32
+ },
33
+ },
34
+ })
35
+ ```
36
+
37
+ ## Options
38
+
39
+ | Option | Type | Default | Description |
40
+ |---|---|---|---|
41
+ | `contentSecurityPolicy` | `false \| CspOptions` | Enabled with defaults | Content-Security-Policy header |
42
+ | `hsts` | `false \| HstsOptions` | Enabled, 180 days | Strict-Transport-Security header |
43
+ | `xFrameOptions` | `false \| 'DENY' \| 'SAMEORIGIN'` | `'SAMEORIGIN'` | X-Frame-Options header |
44
+ | `xContentTypeOptions` | `false` | Enabled (nosniff) | X-Content-Type-Options header |
45
+ | `referrerPolicy` | `false \| string` | `'strict-origin-when-cross-origin'` | Referrer-Policy header |
46
+ | `permissionsPolicy` | `false \| Record<string, string[]>` | Camera/mic/geo/payment blocked | Permissions-Policy header |
47
+ | `dnsPrefetchControl` | `false` | Enabled (off) | X-DNS-Prefetch-Control header |
48
+
49
+ ## Defaults
50
+
51
+ Calling `helmet()` with no arguments sets these headers:
52
+
53
+ **Content-Security-Policy** (directives merged with any overrides you provide):
54
+ ```
55
+ default-src 'self'
56
+ script-src 'self'
57
+ style-src 'self' 'unsafe-inline'
58
+ img-src 'self' data:
59
+ font-src 'self'
60
+ connect-src 'self'
61
+ object-src 'none'
62
+ base-uri 'self'
63
+ form-action 'self'
64
+ ```
65
+
66
+ **Strict-Transport-Security**: `max-age=15552000; includeSubDomains` (180 days)
67
+
68
+ **Permissions-Policy**: `camera=(), microphone=(), geolocation=(), payment=()`
69
+
70
+ **Referrer-Policy**: `strict-origin-when-cross-origin`
71
+
72
+ **X-Frame-Options**: `SAMEORIGIN`
73
+
74
+ **X-Content-Type-Options**: `nosniff`
75
+
76
+ **X-DNS-Prefetch-Control**: `off`
77
+
78
+ ## Notes
79
+
80
+ - All header values are pre-computed at middleware creation time — zero per-request overhead
81
+ - When passing `contentSecurityPolicy.directives`, your directives are merged with the defaults above — you only need to specify the ones you want to change or add
82
+
83
+ ## Documentation
84
+
85
+ [davaux.codeberg.page/docs/helmet](https://davaux.codeberg.page/docs/helmet)
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@davaux/helmet",
3
+ "version": "0.8.0",
4
+ "description": "Security headers middleware for Davaux",
5
+ "type": "module",
6
+ "author": "David L Dyess II",
7
+ "license": "MIT",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://codeberg.org/davaux/davaux"
11
+ },
12
+ "bugs": {
13
+ "url": "https://codeberg.org/davaux/davaux/issues"
14
+ },
15
+ "homepage": "https://codeberg.org/davaux/davaux#readme",
16
+ "exports": {
17
+ ".": {
18
+ "import": "./dist/index.js",
19
+ "types": "./dist/index.d.ts"
20
+ }
21
+ },
22
+ "scripts": {
23
+ "build": "tsc",
24
+ "typecheck": "tsc --noEmit"
25
+ },
26
+ "peerDependencies": {
27
+ "davaux": ">=0.8.0"
28
+ },
29
+ "devDependencies": {
30
+ "@types/node": "^25.0.0",
31
+ "davaux": "*",
32
+ "typescript": "^6.0.3"
33
+ }
34
+ }
package/src/index.ts ADDED
@@ -0,0 +1,154 @@
1
+ import type { MiddlewareFn } from 'davaux'
2
+
3
+ // ─── Options ──────────────────────────────────────────────────────────────────
4
+
5
+ export interface CspOptions {
6
+ /**
7
+ * CSP directives. Each key is a directive name; the value is a string or
8
+ * array of source expressions.
9
+ *
10
+ * @example
11
+ * { 'script-src': ["'self'", 'https://cdn.example.com'] }
12
+ */
13
+ directives?: Record<string, string | string[]>
14
+ }
15
+
16
+ export interface HstsOptions {
17
+ /** Max-Age in seconds. Default: 15552000 (180 days) */
18
+ maxAge?: number
19
+ /** Include subdomains. Default: true */
20
+ includeSubDomains?: boolean
21
+ /** Add preload directive. Default: false */
22
+ preload?: boolean
23
+ }
24
+
25
+ export interface HelmetOptions {
26
+ /** Content-Security-Policy. Pass `false` to disable. */
27
+ contentSecurityPolicy?: false | CspOptions
28
+ /** Strict-Transport-Security. Pass `false` to disable. */
29
+ hsts?: false | HstsOptions
30
+ /** X-Frame-Options. Pass `false` to disable. Default: 'SAMEORIGIN' */
31
+ xFrameOptions?: false | 'DENY' | 'SAMEORIGIN'
32
+ /** X-Content-Type-Options: nosniff. Pass `false` to disable. */
33
+ xContentTypeOptions?: false
34
+ /** Referrer-Policy. Pass `false` to disable. */
35
+ referrerPolicy?: false | string
36
+ /** Permissions-Policy. Pass `false` to disable. */
37
+ permissionsPolicy?: false | Record<string, string[]>
38
+ /** X-DNS-Prefetch-Control: off. Pass `false` to disable. */
39
+ dnsPrefetchControl?: false
40
+ }
41
+
42
+ // ─── Defaults ─────────────────────────────────────────────────────────────────
43
+
44
+ const DEFAULT_CSP_DIRECTIVES: Record<string, string[]> = {
45
+ 'default-src': ["'self'"],
46
+ 'script-src': ["'self'"],
47
+ 'style-src': ["'self'", "'unsafe-inline'"],
48
+ 'img-src': ["'self'", 'data:'],
49
+ 'font-src': ["'self'"],
50
+ 'connect-src': ["'self'"],
51
+ 'object-src': ["'none'"],
52
+ 'base-uri': ["'self'"],
53
+ 'form-action': ["'self'"],
54
+ }
55
+
56
+ const DEFAULT_PERMISSIONS: Record<string, string[]> = {
57
+ camera: [],
58
+ microphone: [],
59
+ geolocation: [],
60
+ payment: [],
61
+ }
62
+
63
+ // ─── Middleware ───────────────────────────────────────────────────────────────
64
+
65
+ /**
66
+ * Security headers middleware. Sets a sensible set of HTTP security headers on
67
+ * every response — CSP, HSTS, X-Frame-Options, X-Content-Type-Options,
68
+ * Referrer-Policy, Permissions-Policy, and X-DNS-Prefetch-Control.
69
+ *
70
+ * All header values are pre-computed at middleware creation time, so there is
71
+ * zero per-request overhead. Pass `false` for any option to disable that header.
72
+ *
73
+ * @example
74
+ * import { helmet } from '@davaux/helmet'
75
+ * export default helmet({ hsts: false, xFrameOptions: 'DENY' })
76
+ */
77
+ export function helmet(options: HelmetOptions = {}): MiddlewareFn {
78
+ const headers = buildHeaders(options)
79
+
80
+ return async (ctx, next) => {
81
+ for (const [name, value] of headers) {
82
+ ctx.res.setHeader(name, value)
83
+ }
84
+ await next()
85
+ }
86
+ }
87
+
88
+ // Pre-compute the header map at middleware creation time — zero per-request overhead.
89
+ function buildHeaders(options: HelmetOptions): [string, string][] {
90
+ const headers: [string, string][] = []
91
+
92
+ // Content-Security-Policy
93
+ if (options.contentSecurityPolicy !== false) {
94
+ const directives = {
95
+ ...DEFAULT_CSP_DIRECTIVES,
96
+ ...(options.contentSecurityPolicy?.directives ?? {}),
97
+ }
98
+ const csp = Object.entries(directives)
99
+ .map(([k, v]) => {
100
+ const sources = Array.isArray(v) ? v : [v]
101
+ return sources.length ? `${k} ${sources.join(' ')}` : k
102
+ })
103
+ .join('; ')
104
+ headers.push(['Content-Security-Policy', csp])
105
+ }
106
+
107
+ // Strict-Transport-Security
108
+ if (options.hsts !== false) {
109
+ const hsts = options.hsts ?? {}
110
+ const maxAge = hsts.maxAge ?? 15552000
111
+ let value = `max-age=${maxAge}`
112
+ if (hsts.includeSubDomains !== false) value += '; includeSubDomains'
113
+ if (hsts.preload) value += '; preload'
114
+ headers.push(['Strict-Transport-Security', value])
115
+ }
116
+
117
+ // X-Frame-Options
118
+ if (options.xFrameOptions !== false) {
119
+ headers.push(['X-Frame-Options', options.xFrameOptions ?? 'SAMEORIGIN'])
120
+ }
121
+
122
+ // X-Content-Type-Options
123
+ if (options.xContentTypeOptions !== false) {
124
+ headers.push(['X-Content-Type-Options', 'nosniff'])
125
+ }
126
+
127
+ // Referrer-Policy
128
+ if (options.referrerPolicy !== false) {
129
+ headers.push([
130
+ 'Referrer-Policy',
131
+ typeof options.referrerPolicy === 'string'
132
+ ? options.referrerPolicy
133
+ : 'strict-origin-when-cross-origin',
134
+ ])
135
+ }
136
+
137
+ // Permissions-Policy
138
+ if (options.permissionsPolicy !== false) {
139
+ const perms = options.permissionsPolicy ?? DEFAULT_PERMISSIONS
140
+ const value = Object.entries(perms)
141
+ .map(([feature, allowlist]) =>
142
+ allowlist.length ? `${feature}=(${allowlist.join(' ')})` : `${feature}=()`,
143
+ )
144
+ .join(', ')
145
+ if (value) headers.push(['Permissions-Policy', value])
146
+ }
147
+
148
+ // X-DNS-Prefetch-Control
149
+ if (options.dnsPrefetchControl !== false) {
150
+ headers.push(['X-DNS-Prefetch-Control', 'off'])
151
+ }
152
+
153
+ return headers
154
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ESNext",
4
+ "module": "NodeNext",
5
+ "moduleResolution": "NodeNext",
6
+ "strict": true,
7
+ "declaration": true,
8
+ "declarationMap": true,
9
+ "sourceMap": true,
10
+ "outDir": "./dist",
11
+ "rootDir": "./src",
12
+ "skipLibCheck": true,
13
+ "lib": ["ESNext"],
14
+ "types": ["node"]
15
+ },
16
+ "include": ["src/**/*"]
17
+ }