@jini-ai/http-kit 0.6.19 → 0.7.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/CHANGELOG.md +20 -0
- package/dist/adapter.js +1 -1
- package/dist/html-response.d.ts +21 -0
- package/dist/html-response.js +111 -0
- package/package.json +14 -8
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,25 @@
|
|
|
1
1
|
# @jini-ai/http-kit
|
|
2
2
|
|
|
3
|
+
## 0.7.0
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [8fe045b]
|
|
8
|
+
- Updated dependencies [c393010]
|
|
9
|
+
- Updated dependencies [41ff2ff]
|
|
10
|
+
- Updated dependencies [a266ba8]
|
|
11
|
+
- Updated dependencies [27063ef]
|
|
12
|
+
- Updated dependencies [6ab1d81]
|
|
13
|
+
- Updated dependencies [699b478]
|
|
14
|
+
- Updated dependencies [39cf677]
|
|
15
|
+
- @jini-ai/core@0.7.0
|
|
16
|
+
- @jini-ai/platform@0.7.0
|
|
17
|
+
- @jini-ai/protocol@0.7.0
|
|
18
|
+
|
|
19
|
+
## 0.6.22
|
|
20
|
+
|
|
21
|
+
- The internal error response says in plain words what happened instead of "an internal error occurred".
|
|
22
|
+
|
|
3
23
|
## 0.6.19
|
|
4
24
|
|
|
5
25
|
- Tests: the route adapter's app fixture is a real `express()` app typed as `Express`, replacing the `app as any` casts.
|
package/dist/adapter.js
CHANGED
|
@@ -165,6 +165,6 @@ function sendRouteException({ res, spec, adapter, error: e }, _optional = {}) {
|
|
|
165
165
|
const correlationId = randomUUID();
|
|
166
166
|
const sink = adapter?.onInternalError ?? defaultInternalErrorSink;
|
|
167
167
|
sink({ method: spec.method, path: spec.path, correlationId, error: e });
|
|
168
|
-
sendApiError({ res, status: 500, error: createApiError({ code: 'INTERNAL_ERROR', message: '
|
|
168
|
+
sendApiError({ res, status: 500, error: createApiError({ code: 'INTERNAL_ERROR', message: 'The server could not finish this request. Check whether your change was saved, then copy the request ID and report this problem.' }, { requestId: correlationId }) });
|
|
169
169
|
}
|
|
170
170
|
//# sourceMappingURL=adapter.js.map
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** Bounded HTML response capture for host-owned transformations, including files and streams.
|
|
2
|
+
* This transport primitive applies no content policy: overflow and failures preserve original bytes. */
|
|
3
|
+
import type { ServerResponse } from 'node:http';
|
|
4
|
+
type Transform = (required: {
|
|
5
|
+
body: Buffer;
|
|
6
|
+
}) => Promise<Buffer>;
|
|
7
|
+
/** Capture a response only while its uncompressed HTML fits the byte bound. Callbacks acknowledge
|
|
8
|
+
* buffered writes; overflow restores native writes immediately, so pipes retain native backpressure.
|
|
9
|
+
* A failed transform returns original bytes. Changed bytes drop stale length/validator headers.
|
|
10
|
+
* The transform receives a copy so mutation before a throw cannot corrupt the fallback (at most
|
|
11
|
+
* two maxBytes-sized input buffers, excluding output allocated by the transform).
|
|
12
|
+
* @complexity O(min(body bytes, maxBytes)) retained space; O(body bytes) work plus transform cost.
|
|
13
|
+
*/
|
|
14
|
+
export declare function captureHtmlResponse({ response, transform }: {
|
|
15
|
+
response: ServerResponse;
|
|
16
|
+
transform: Transform;
|
|
17
|
+
}, { maxBytes }?: {
|
|
18
|
+
maxBytes?: number;
|
|
19
|
+
}): void;
|
|
20
|
+
export {};
|
|
21
|
+
//# sourceMappingURL=html-response.d.ts.map
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
const DEFAULT_MAX_BYTES = 5 * 1024 * 1024;
|
|
2
|
+
function bufferOf(chunk, encoding) {
|
|
3
|
+
if (chunk === undefined || chunk === null || typeof chunk === 'function')
|
|
4
|
+
return Buffer.alloc(0);
|
|
5
|
+
if (Buffer.isBuffer(chunk))
|
|
6
|
+
return chunk;
|
|
7
|
+
if (chunk instanceof Uint8Array)
|
|
8
|
+
return Buffer.from(chunk);
|
|
9
|
+
return Buffer.from(String(chunk), typeof encoding === 'string' ? encoding : 'utf8');
|
|
10
|
+
}
|
|
11
|
+
/** Defer explicit headers until a buffered HTML transform has completed. */
|
|
12
|
+
function copyHeaders(response, values) {
|
|
13
|
+
if (Array.isArray(values)) {
|
|
14
|
+
for (let i = 0; i < values.length; i += 2)
|
|
15
|
+
response.setHeader(String(values[i]), values[i + 1]);
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
if (!values || typeof values !== 'object')
|
|
19
|
+
return;
|
|
20
|
+
for (const [name, value] of Object.entries(values))
|
|
21
|
+
if (value !== undefined)
|
|
22
|
+
response.setHeader(name, value);
|
|
23
|
+
}
|
|
24
|
+
function captures(response) {
|
|
25
|
+
const type = response.getHeader('content-type');
|
|
26
|
+
return response.req?.method !== 'HEAD' && !response.getHeader('content-encoding') && (!type || /\btext\/html\b/i.test(String(type)));
|
|
27
|
+
}
|
|
28
|
+
function htmlBody(response, body) {
|
|
29
|
+
const type = response.getHeader('content-type');
|
|
30
|
+
return /\btext\/html\b/i.test(String(type)) || !type && /^\s*<[a-z!]/i.test(body.toString('utf8'));
|
|
31
|
+
}
|
|
32
|
+
function completionCallback(chunk, encoding, callback) {
|
|
33
|
+
if (typeof chunk === 'function')
|
|
34
|
+
return chunk;
|
|
35
|
+
return typeof encoding === 'function' ? encoding : callback;
|
|
36
|
+
}
|
|
37
|
+
/** A changed body invalidates its original length and validators; pass-through keeps native headers. */
|
|
38
|
+
async function transformBody(response, body, transform) {
|
|
39
|
+
let result = body;
|
|
40
|
+
try {
|
|
41
|
+
if (body.length && htmlBody(response, body))
|
|
42
|
+
result = await transform({ body: Buffer.from(body) });
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
return body;
|
|
46
|
+
}
|
|
47
|
+
if (!body.equals(result)) {
|
|
48
|
+
response.removeHeader('Content-Length');
|
|
49
|
+
response.removeHeader('ETag');
|
|
50
|
+
response.removeHeader('Content-MD5');
|
|
51
|
+
}
|
|
52
|
+
return result;
|
|
53
|
+
}
|
|
54
|
+
/** Capture a response only while its uncompressed HTML fits the byte bound. Callbacks acknowledge
|
|
55
|
+
* buffered writes; overflow restores native writes immediately, so pipes retain native backpressure.
|
|
56
|
+
* A failed transform returns original bytes. Changed bytes drop stale length/validator headers.
|
|
57
|
+
* The transform receives a copy so mutation before a throw cannot corrupt the fallback (at most
|
|
58
|
+
* two maxBytes-sized input buffers, excluding output allocated by the transform).
|
|
59
|
+
* @complexity O(min(body bytes, maxBytes)) retained space; O(body bytes) work plus transform cost.
|
|
60
|
+
*/
|
|
61
|
+
export function captureHtmlResponse({ response, transform }, { maxBytes = DEFAULT_MAX_BYTES } = {}) {
|
|
62
|
+
const { write, end, writeHead, flushHeaders } = response;
|
|
63
|
+
const chunks = [];
|
|
64
|
+
let bytes = 0;
|
|
65
|
+
const restore = () => { response.write = write; response.end = end; response.writeHead = writeHead; response.flushHeaders = flushHeaders; };
|
|
66
|
+
const held = () => { const body = Buffer.concat(chunks, bytes); chunks.length = 0; bytes = 0; return body; };
|
|
67
|
+
const append = (body) => { chunks.push(body); bytes += body.length; };
|
|
68
|
+
const flushPrefix = () => { restore(); const prefix = held(); if (prefix.length)
|
|
69
|
+
Reflect.apply(write, response, [prefix]); };
|
|
70
|
+
response.writeHead = function (status, reasonOrHeaders, headers) {
|
|
71
|
+
response.statusCode = status;
|
|
72
|
+
if (typeof reasonOrHeaders === 'string')
|
|
73
|
+
response.statusMessage = reasonOrHeaders;
|
|
74
|
+
copyHeaders(response, typeof reasonOrHeaders === 'string' ? headers : reasonOrHeaders);
|
|
75
|
+
return response;
|
|
76
|
+
};
|
|
77
|
+
response.flushHeaders = function () {
|
|
78
|
+
if (!captures(response)) {
|
|
79
|
+
flushPrefix();
|
|
80
|
+
flushHeaders.call(response);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
response.write = function (chunk, encoding, callback) {
|
|
84
|
+
const body = bufferOf(chunk, encoding);
|
|
85
|
+
if (!captures(response) || bytes + body.length > maxBytes) {
|
|
86
|
+
flushPrefix();
|
|
87
|
+
return Reflect.apply(write, response, [chunk, encoding, callback]);
|
|
88
|
+
}
|
|
89
|
+
append(body);
|
|
90
|
+
const done = typeof encoding === 'function' ? encoding : callback;
|
|
91
|
+
if (typeof done === 'function')
|
|
92
|
+
queueMicrotask(() => done());
|
|
93
|
+
return true;
|
|
94
|
+
};
|
|
95
|
+
response.end = function (chunk, encoding, callback) {
|
|
96
|
+
const done = completionCallback(chunk, encoding, callback);
|
|
97
|
+
const body = bufferOf(chunk, encoding);
|
|
98
|
+
if (!captures(response) || bytes + body.length > maxBytes) {
|
|
99
|
+
flushPrefix();
|
|
100
|
+
return Reflect.apply(end, response, [body, typeof done === 'function' ? done : undefined]);
|
|
101
|
+
}
|
|
102
|
+
append(body);
|
|
103
|
+
void transformBody(response, held(), transform).then(result => {
|
|
104
|
+
restore();
|
|
105
|
+
if (!response.destroyed)
|
|
106
|
+
Reflect.apply(end, response, [result, typeof done === 'function' ? done : undefined]);
|
|
107
|
+
});
|
|
108
|
+
return response;
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
//# sourceMappingURL=html-response.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jini-ai/http-kit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "Generic Express transport primitives, request parsing, response serialization, origin guards and route-pack mounting.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -16,7 +16,8 @@
|
|
|
16
16
|
"./rate-limit": "node",
|
|
17
17
|
"./middleware": "node",
|
|
18
18
|
"./verified-origin": "universal",
|
|
19
|
-
"./observability": "node"
|
|
19
|
+
"./observability": "node",
|
|
20
|
+
"./html-response": "node"
|
|
20
21
|
},
|
|
21
22
|
"runtime": "node"
|
|
22
23
|
},
|
|
@@ -49,6 +50,11 @@
|
|
|
49
50
|
"types": "./dist/observability.d.ts",
|
|
50
51
|
"import": "./dist/observability.js",
|
|
51
52
|
"default": "./dist/observability.js"
|
|
53
|
+
},
|
|
54
|
+
"./html-response": {
|
|
55
|
+
"types": "./dist/html-response.d.ts",
|
|
56
|
+
"import": "./dist/html-response.js",
|
|
57
|
+
"default": "./dist/html-response.js"
|
|
52
58
|
}
|
|
53
59
|
},
|
|
54
60
|
"files": [
|
|
@@ -71,14 +77,14 @@
|
|
|
71
77
|
"@types/express": "^4.17.21",
|
|
72
78
|
"@vitest/coverage-v8": "^2.1.9",
|
|
73
79
|
"express": "^4.21.0",
|
|
74
|
-
"@jini-ai/
|
|
75
|
-
"@jini-ai/
|
|
76
|
-
"@jini-ai/
|
|
80
|
+
"@jini-ai/core": "0.7.0",
|
|
81
|
+
"@jini-ai/platform": "0.7.0",
|
|
82
|
+
"@jini-ai/protocol": "0.7.0"
|
|
77
83
|
},
|
|
78
84
|
"dependencies": {
|
|
79
|
-
"@jini-ai/
|
|
80
|
-
"@jini-ai/platform": "^0.
|
|
81
|
-
"@jini-ai/
|
|
85
|
+
"@jini-ai/core": "^0.7.0",
|
|
86
|
+
"@jini-ai/platform": "^0.7.0",
|
|
87
|
+
"@jini-ai/protocol": "^0.7.0"
|
|
82
88
|
},
|
|
83
89
|
"scripts": {
|
|
84
90
|
"build": "tsc -p tsconfig.json",
|