@prexjs/on-headers 0.0.0-stage → 1.0.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/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Prexjs (https://prexjs.com)
4
+ Copyright (c) 2026 Sorabh (https://sorabh.org)
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,93 @@
1
- # Temporary Holding Version
1
+ # @prexjs/on-headers
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ **Execute a listener when HTTP headers are about to be written to a response.**
4
+
5
+ [![npm version](https://img.shields.io/npm/v/@prexjs/on-headers)](https://www.npmjs.com/package/@prexjs/on-headers)
6
+ [![npm downloads](https://img.shields.io/npm/dm/@prexjs/on-headers)](https://www.npmjs.com/package/@prexjs/on-headers)
7
+ [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](./LICENSE)
8
+ [![TypeScript](https://img.shields.io/badge/TypeScript-supported-blue)](https://www.typescriptlang.org/)
9
+
10
+ `@prexjs/on-headers` intercepts `res.writeHead` and response flushes, executing a callback right before HTTP response headers are written to the client. This allows middleware (e.g. session managers, cookie setters, compression tools) to adjust headers at the last possible moment.
11
+
12
+ ## Features
13
+
14
+ - Guarantees execution right before headers are sent to the client.
15
+ - Automatically handles status code changes performed within listener callbacks.
16
+ - Supports object and array arguments passed to `res.writeHead`.
17
+ - Executes listeners in reverse order of addition (LIFO), allowing stacked middleware hooks.
18
+ - Dual ES Module (`.mjs`) and CommonJS (`.cjs`) support with TypeScript definitions.
19
+ - Zero external runtime dependencies.
20
+
21
+ ## Installation
22
+
23
+ Install using your preferred package manager:
24
+
25
+ ```bash
26
+ # npm
27
+ npm install @prexjs/on-headers
28
+
29
+ # pnpm
30
+ pnpm add @prexjs/on-headers
31
+
32
+ # Yarn
33
+ yarn add @prexjs/on-headers
34
+
35
+ # Bun
36
+ bun add @prexjs/on-headers
37
+ ```
38
+
39
+ ## Quick start
40
+
41
+ ```ts
42
+ import http from 'node:http';
43
+ import onHeaders from '@prexjs/on-headers';
44
+
45
+ const server = http.createServer((req, res) => {
46
+ const start = Date.now();
47
+
48
+ onHeaders(res, function () {
49
+ const duration = Date.now() - start;
50
+ this.setHeader('X-Response-Time', `${duration}ms`);
51
+ });
52
+
53
+ res.end('Hello World');
54
+ });
55
+ ```
56
+
57
+ Both default and named exports are supported:
58
+
59
+ ```ts
60
+ import onHeaders, { onHeaders as namedOnHeaders } from '@prexjs/on-headers';
61
+ ```
62
+
63
+ ## Examples
64
+
65
+ ### Conditional status code updates
66
+
67
+ ```ts
68
+ import http from 'node:http';
69
+ import onHeaders from '@prexjs/on-headers';
70
+
71
+ const server = http.createServer((req, res) => {
72
+ onHeaders(res, function () {
73
+ if (this.statusCode === 200 && req.headers['x-custom-flag']) {
74
+ this.statusCode = 201;
75
+ }
76
+ });
77
+
78
+ res.statusCode = 200;
79
+ res.end('Created');
80
+ });
81
+ ```
82
+
83
+ ## Compatibility
84
+
85
+ `@prexjs/on-headers` is designed for Node.js, Deno, and Bun HTTP servers utilizing `http.ServerResponse`.
86
+
87
+ ## License
88
+
89
+ This project is licensed under the MIT License. See [LICENSE](./LICENSE) for the full license text.
90
+
91
+ ## Related projects
92
+
93
+ - [prex.js](https://github.com/prexjs/prex.js) — prex.js monorepo and ecosystem.
package/lib/index.cjs ADDED
@@ -0,0 +1,7 @@
1
+ /*!
2
+ * @prexjs/on-headers
3
+ * Copyright (c) 2026 Prexjs (https://prexjs.com)
4
+ * Copyright (c) 2026 Sorabh (https://Sorabh.org)
5
+ * MIT Licensed
6
+ */
7
+ "use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}Object.defineProperty(exports,"__esModule",{value:!0});function t(e,t){let r=!1;return function(...o){const s=n.apply(this,o);return r||(r=!0,t.call(this),"number"==typeof s[0]&&this.statusCode!==s[0]&&(s[0]=this.statusCode,s.length=1)),e.apply(this,s)}}function r(e,r){if(!e)throw new TypeError("argument res is required");if("function"!=typeof r)throw new TypeError("argument listener must be a function");e.writeHead=t(e.writeHead,r)}function n(e,...t){const r=t.length+1,n=r>1&&"string"==typeof t[0]?2:1,s=r>=n+1?t[n-1]:void 0;this.statusCode=e,Array.isArray(s)?function(e,t){if(t.length&&Array.isArray(t[0]))!function(e,t){for(let r=0;r<t.length;r++){const n=t[r][0];n&&e.setHeader(n,t[r][1])}}(e,t);else{if(t.length%2!=0)throw new TypeError("headers array is malformed");o(e,t)}}(this,s):s&&function(e,t){const r=Object.keys(t);for(let n=0;n<r.length;n++){const o=r[n];o&&e.setHeader(o,t[o])}}(this,s);return[e,...t].slice(0,Math.min(r,n))}const o="function"==typeof e(require("node:http")).default.ServerResponse.prototype.appendHeader?function(e,t){for(let r=0;r<t.length;r+=2)e.removeHeader(t[r]);for(let r=0;r<t.length;r+=2){const n=t[r];n&&e.appendHeader(n,t[r+1])}}:function(e,t){for(let r=0;r<t.length;r+=2){const n=t[r];n&&e.setHeader(n,t[r+1])}};exports.default=r,exports.onHeaders=r;
@@ -0,0 +1,9 @@
1
+ import http from 'node:http';
2
+
3
+ type Listener = (this: http.ServerResponse) => void;
4
+ /**
5
+ * Execute a listener when a response is about to write headers.
6
+ */
7
+ declare function onHeaders(res: http.ServerResponse, listener: Listener): void;
8
+
9
+ export { onHeaders as default, onHeaders };
@@ -0,0 +1,9 @@
1
+ import http from 'node:http';
2
+
3
+ type Listener = (this: http.ServerResponse) => void;
4
+ /**
5
+ * Execute a listener when a response is about to write headers.
6
+ */
7
+ declare function onHeaders(res: http.ServerResponse, listener: Listener): void;
8
+
9
+ export { onHeaders as default, onHeaders };
package/lib/index.mjs ADDED
@@ -0,0 +1,7 @@
1
+ /*!
2
+ * @prexjs/on-headers
3
+ * Copyright (c) 2026 Prexjs (https://prexjs.com)
4
+ * Copyright (c) 2026 Sorabh (https://Sorabh.org)
5
+ * MIT Licensed
6
+ */
7
+ import e from"node:http";function t(e,t){let r=!1;return function(...o){const s=n.apply(this,o);return r||(r=!0,t.call(this),"number"==typeof s[0]&&this.statusCode!==s[0]&&(s[0]=this.statusCode,s.length=1)),e.apply(this,s)}}function r(e,r){if(!e)throw new TypeError("argument res is required");if("function"!=typeof r)throw new TypeError("argument listener must be a function");e.writeHead=t(e.writeHead,r)}function n(e,...t){const r=t.length+1,n=r>1&&"string"==typeof t[0]?2:1,s=r>=n+1?t[n-1]:void 0;this.statusCode=e,Array.isArray(s)?function(e,t){if(t.length&&Array.isArray(t[0]))!function(e,t){for(let r=0;r<t.length;r++){const n=t[r][0];n&&e.setHeader(n,t[r][1])}}(e,t);else{if(t.length%2!=0)throw new TypeError("headers array is malformed");o(e,t)}}(this,s):s&&function(e,t){const r=Object.keys(t);for(let n=0;n<r.length;n++){const o=r[n];o&&e.setHeader(o,t[o])}}(this,s);return[e,...t].slice(0,Math.min(r,n))}const o="function"==typeof e.ServerResponse.prototype.appendHeader?function(e,t){for(let r=0;r<t.length;r+=2)e.removeHeader(t[r]);for(let r=0;r<t.length;r+=2){const n=t[r];n&&e.appendHeader(n,t[r+1])}}:function(e,t){for(let r=0;r<t.length;r+=2){const n=t[r];n&&e.setHeader(n,t[r+1])}};export{r as default,r as onHeaders};
package/package.json CHANGED
@@ -1,6 +1,64 @@
1
1
  {
2
2
  "name": "@prexjs/on-headers",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.0.0",
4
+ "description": "Execute a listener when HTTP headers are about to be written to a response.",
5
+ "authors": [
6
+ {
7
+ "name": "Prexjs",
8
+ "email": "hello@prexjs.com",
9
+ "url": "https://prexjs.com"
10
+ },
11
+ {
12
+ "name": "Sorabh",
13
+ "email": "hello@sorabh.org",
14
+ "url": "https://sorabh.org"
15
+ }
16
+ ],
17
+ "license": "MIT",
18
+ "type": "module",
19
+ "sideEffects": false,
20
+ "files": [
21
+ "lib",
22
+ "README.md",
23
+ "LICENSE"
24
+ ],
25
+ "main": "./lib/index.cjs",
26
+ "module": "./lib/index.mjs",
27
+ "types": "./lib/index.d.cts",
28
+ "exports": {
29
+ ".": {
30
+ "import": {
31
+ "types": "./lib/index.d.mts",
32
+ "default": "./lib/index.mjs"
33
+ },
34
+ "require": {
35
+ "types": "./lib/index.d.cts",
36
+ "default": "./lib/index.cjs"
37
+ }
38
+ },
39
+ "./package.json": "./package.json"
40
+ },
41
+ "repository": {
42
+ "type": "git",
43
+ "url": "git+https://github.com/prexjs/prex.js.git",
44
+ "directory": "packages/on-headers"
45
+ },
46
+ "homepage": "https://github.com/prexjs/prex.js/tree/main/packages/on-headers#readme",
47
+ "bugs": {
48
+ "url": "https://github.com/prexjs/prex.js/issues"
49
+ },
50
+ "publishConfig": {
51
+ "access": "public"
52
+ },
53
+ "keywords": [
54
+ "prexjs",
55
+ "on-headers",
56
+ "http",
57
+ "headers",
58
+ "writehead",
59
+ "response",
60
+ "middleware",
61
+ "listener",
62
+ "typescript"
63
+ ]
64
+ }