@hardlydifficult/http 1.0.5 → 1.0.6
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/README.md +64 -19
- 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:
|
|
3
|
+
HTTP utilities for safe request/response handling: body reading with size limits, constant-time comparison, and JSON responses with CORS.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -33,39 +33,48 @@ const server = createServer(async (req, res) => {
|
|
|
33
33
|
server.listen(3000);
|
|
34
34
|
```
|
|
35
35
|
|
|
36
|
-
##
|
|
36
|
+
## Core HTTP Utilities
|
|
37
37
|
|
|
38
|
-
###
|
|
38
|
+
### Reading Request Body
|
|
39
39
|
|
|
40
|
-
|
|
40
|
+
Safely reads request body with a configurable size limit (default 1 MB) to prevent memory exhaustion.
|
|
41
41
|
|
|
42
42
|
```typescript
|
|
43
|
-
import { readBody } from
|
|
43
|
+
import { readBody, MAX_BODY_BYTES } from '@hardlydifficult/http';
|
|
44
|
+
import { IncomingMessage } from 'http';
|
|
44
45
|
|
|
45
|
-
// Default
|
|
46
|
-
const body = await readBody(req);
|
|
46
|
+
// Default: 1,048,576 bytes (1 MB)
|
|
47
|
+
const body = await readBody(req as IncomingMessage);
|
|
48
|
+
const text = body.toString();
|
|
47
49
|
|
|
48
|
-
//
|
|
49
|
-
const
|
|
50
|
+
// Explicit limit
|
|
51
|
+
const body2 = await readBody(req as IncomingMessage, 500_000); // 500 KB limit
|
|
50
52
|
```
|
|
51
53
|
|
|
52
54
|
| Parameter | Type | Description |
|
|
53
|
-
|
|
54
|
-
|
|
|
55
|
-
|
|
|
55
|
+
|---|---|---|
|
|
56
|
+
| req | `IncomingMessage` | HTTP request stream |
|
|
57
|
+
| maxBytes? | `number` | Maximum body size in bytes (default: `MAX_BODY_BYTES`) |
|
|
56
58
|
|
|
57
|
-
Throws `
|
|
59
|
+
**Throws:** `Error` if body exceeds `maxBytes`.
|
|
58
60
|
|
|
59
|
-
###
|
|
61
|
+
### Sending JSON Responses
|
|
60
62
|
|
|
61
|
-
|
|
63
|
+
Sends JSON with `Content-Type: application/json` and CORS headers.
|
|
62
64
|
|
|
63
65
|
```typescript
|
|
64
|
-
import {
|
|
66
|
+
import { sendJson } from '@hardlydifficult/http';
|
|
67
|
+
import { ServerResponse } from 'http';
|
|
65
68
|
|
|
66
|
-
|
|
69
|
+
sendJson(res as ServerResponse, { success: true });
|
|
70
|
+
// Sets headers: Content-Type: application/json, Access-Control-Allow-Origin: *
|
|
67
71
|
```
|
|
68
72
|
|
|
73
|
+
| Parameter | Type | Description |
|
|
74
|
+
|---|---|---|
|
|
75
|
+
| res | `ServerResponse` | HTTP response object |
|
|
76
|
+
| data | `any` | Serializable data to send as JSON |
|
|
77
|
+
|
|
69
78
|
## Response Handling
|
|
70
79
|
|
|
71
80
|
### `sendJson`
|
|
@@ -79,7 +88,7 @@ sendJson(res, 200, { data: "example" }, "https://example.com");
|
|
|
79
88
|
```
|
|
80
89
|
|
|
81
90
|
| Parameter | Type | Description |
|
|
82
|
-
|
|
91
|
+
|---|---|---|
|
|
83
92
|
| `res` | `ServerResponse` | Node.js HTTP response |
|
|
84
93
|
| `status` | `number` | HTTP status code |
|
|
85
94
|
| `body` | `unknown` | Serializable data to send |
|
|
@@ -111,4 +120,40 @@ Handles:
|
|
|
111
120
|
- Unicode characters
|
|
112
121
|
- Empty strings
|
|
113
122
|
|
|
114
|
-
All comparisons run in time proportional to the first string's length.
|
|
123
|
+
All comparisons run in time proportional to the first string's length.
|
|
124
|
+
|
|
125
|
+
```typescript
|
|
126
|
+
import { safeCompare } from '@hardlydifficult/http';
|
|
127
|
+
|
|
128
|
+
// True (same content)
|
|
129
|
+
const match1 = safeCompare('abc', 'abc'); // => true
|
|
130
|
+
|
|
131
|
+
// False (different content)
|
|
132
|
+
const match2 = safeCompare('abc', 'abd'); // => false
|
|
133
|
+
|
|
134
|
+
// False (different length)
|
|
135
|
+
const match3 = safeCompare('abc', 'abcd'); // => false
|
|
136
|
+
|
|
137
|
+
// Works with unicode
|
|
138
|
+
const match4 = safeCompare('你好', '你好'); // => true
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
| Parameters | Type | Description |
|
|
142
|
+
|---|---|---|
|
|
143
|
+
| a | `string` | First string |
|
|
144
|
+
| b | `string` | Second string |
|
|
145
|
+
|
|
146
|
+
**Returns:** `boolean` — `true` if strings are identical, `false` otherwise.
|
|
147
|
+
|
|
148
|
+
## Constants
|
|
149
|
+
|
|
150
|
+
### `MAX_BODY_BYTES`
|
|
151
|
+
|
|
152
|
+
Default maximum body size in bytes (1,048,576 = 1 MB).
|
|
153
|
+
|
|
154
|
+
```typescript
|
|
155
|
+
import { MAX_BODY_BYTES } from '@hardlydifficult/http';
|
|
156
|
+
|
|
157
|
+
// MAX_BODY_BYTES === 1024 * 1024
|
|
158
|
+
console.log(MAX_BODY_BYTES); // 1048576
|
|
159
|
+
```
|