@hardlydifficult/http 1.0.3 → 1.0.4

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.
Files changed (2) hide show
  1. package/README.md +48 -58
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @hardlydifficult/http
2
2
 
3
- HTTP utilities for safe request/response handling: constant-time string comparison, body reading with 1MB limit, and JSON responses with CORS.
3
+ HTTP utilities for safe request/response handling, including constant-time string comparison, body reading with size limits, and JSON responses with CORS headers.
4
4
 
5
5
  ## Installation
6
6
 
@@ -11,85 +11,75 @@ npm install @hardlydifficult/http
11
11
  ## Quick Start
12
12
 
13
13
  ```typescript
14
- import { readBody, sendJson, safeCompare, MAX_BODY_BYTES } from "@hardlydifficult/http";
15
- import { createServer } from "http";
16
-
17
- const server = createServer(async (req, res) => {
18
- const body = await readBody(req);
19
- const isValid = safeCompare(body, "expected-secret");
20
-
21
- sendJson(res, isValid ? 200 : 403, { authorized: isValid }, "https://example.com");
22
- });
23
-
24
- server.listen(3000);
25
- // → Server starts and safely compares incoming request bodies
26
- ```
14
+ import { readBody, sendJson, safeCompare } from '@hardlydifficult/http';
27
15
 
28
- ## Body Reading with Size Limit
16
+ // Safe string comparison
17
+ const isMatch = safeCompare('secret', 'secret'); // true
29
18
 
30
- Reads the full HTTP request body as a string, enforcing a configurable maximum size (default 1 MB). Throws an error if the payload exceeds the limit.
19
+ // Read request body with 1MB limit
20
+ const body = await readBody(req); // max 1MB
31
21
 
32
- ```typescript
33
- import { readBody, MAX_BODY_BYTES } from "@hardlydifficult/http";
22
+ // Send JSON response with CORS headers
23
+ sendJson(res, { status: 'ok' });
24
+ ```
34
25
 
35
- // Default limit: 1 MB
36
- const body = await readBody(request);
26
+ ## HTTP Utilities
37
27
 
38
- // Custom limit: 500 KB
39
- const body = await readBody(request, 1024 * 500);
40
- ```
28
+ ### Safe String Comparison
29
+
30
+ Performs constant-time comparison of two strings to prevent timing attacks.
41
31
 
42
- ### Error Handling
32
+ ```typescript
33
+ import { safeCompare } from '@hardlydifficult/http';
43
34
 
44
- If the body exceeds the specified limit, the promise rejects with an `"Payload too large"` error.
35
+ const result = safeCompare('abc123', 'abc123'); // true
36
+ const fail = safeCompare('abc123', 'abc124'); // false
37
+ ```
45
38
 
46
- ## JSON Response with CORS
39
+ | Parameter | Type | Description |
40
+ |-----------|--------|---------------------|
41
+ | a | string | First string to compare |
42
+ | b | string | Second string to compare |
47
43
 
48
- Sends a JSON response with CORS headers enabled.
44
+ ### Reading Request Body
49
45
 
50
- | Parameter | Type | Description |
51
- |---------|------|-------------|
52
- | `res` | `ServerResponse` | Node.js HTTP response object |
53
- | `status` | `number` | HTTP status code |
54
- | `body` | `unknown` | Any serializable data |
55
- | `corsOrigin` | `string` | Allowed origin for CORS (e.g., `"*"` or `"https://example.com"`) |
46
+ Reads and returns the request body as a string, enforcing a maximum size limit of 1 MB.
56
47
 
57
48
  ```typescript
58
- import { sendJson } from "@hardlydifficult/http";
59
-
60
- sendJson(
61
- res,
62
- 201,
63
- { id: 123, message: "Created" },
64
- "https://frontend.example.com"
65
- );
66
- // → Sets headers: Content-Type, Access-Control-Allow-Origin, etc.
49
+ import { readBody, MAX_BODY_BYTES } from '@hardlydifficult/http';
50
+
51
+ const body = await readBody(req); // max 1,048,576 bytes (1 MB)
52
+ // Throws Error if body exceeds MAX_BODY_BYTES
67
53
  ```
68
54
 
69
- ## Constant-Time String Comparison
55
+ | Parameter | Type | Description |
56
+ |-----------|--------------------|--------------------------------|
57
+ | req | IncomingMessage | Node.js HTTP request object |
70
58
 
71
- Performs secure string comparison using `crypto.timingSafeEqual` to prevent timing attacks.
59
+ ### Sending JSON Responses
60
+
61
+ Sends a JSON response with appropriate `Content-Type` and `Access-Control-Allow-Origin` headers.
72
62
 
73
63
  ```typescript
74
- import { safeCompare } from "@hardlydifficult/http";
64
+ import { sendJson } from '@hardlydifficult/http';
75
65
 
76
- const isMatch = safeCompare(userProvidedToken, storedToken);
77
- // true if strings are identical, false otherwise
66
+ sendJson(res, { message: 'Hello world' });
67
+ // Sends: {"message":"Hello world"} with CORS headers
78
68
  ```
79
69
 
80
- ### Behavior Details
70
+ | Parameter | Type | Description |
71
+ |-----------|----------|---------------------------------|
72
+ | res | ServerResponse | Node.js HTTP response object |
73
+ | data | unknown | Data to serialize as JSON |
74
+
75
+ ## Appendix
81
76
 
82
- - Returns `true` only for identical strings (including empty strings).
83
- - Returns `false` for different-length strings, with constant-time behavior.
84
- - Handles Unicode correctly by comparing UTF-8 encoded buffers.
77
+ ### Body Size Limit Behavior
85
78
 
86
- ### Example Edge Cases
79
+ The `readBody` function enforces a strict 1 MB (`MAX_BODY_BYTES = 1024 * 1024`) limit. If the request body exceeds this, it throws an error:
87
80
 
88
81
  ```typescript
89
- safeCompare("", ""); // true
90
- safeCompare("abc", "abc"); // true
91
- safeCompare("abc", "abd"); // false
92
- safeCompare("short", "longer"); // false
93
- safeCompare("héllo", "héllo"); // true
94
- safeCompare("héllo", "hello"); // false
82
+ if (received > MAX_BODY_BYTES) {
83
+ throw new Error(`Body exceeded maximum size of ${MAX_BODY_BYTES} bytes`);
84
+ }
95
85
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hardlydifficult/http",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "files": [