@kne/fastify-app-manager 0.1.5 → 0.1.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.
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const fp = require('fastify-plugin');
|
|
2
|
+
const { Readable } = require('node:stream');
|
|
2
3
|
const { rewriteLocation, rewriteSetCookiePath, shouldRewriteBody, rewriteBody } = require('../utils/pathRewrite');
|
|
3
4
|
|
|
4
5
|
module.exports = fp(async (fastify, options) => {
|
|
@@ -20,6 +21,12 @@ module.exports = fp(async (fastify, options) => {
|
|
|
20
21
|
const dest = `http://127.0.0.1:${app.port}${targetPath}`;
|
|
21
22
|
const mountPrefix = `${pathPrefix}/${app.name}`;
|
|
22
23
|
|
|
24
|
+
// The gateway runs in onRequest, before body parsing; reply-from only forwards request.body,
|
|
25
|
+
// so hand it the raw stream or POST/PUT bodies reach the app empty.
|
|
26
|
+
if (request.method !== 'GET' && request.method !== 'HEAD') {
|
|
27
|
+
request.body = request.raw;
|
|
28
|
+
}
|
|
29
|
+
|
|
23
30
|
return reply.from(dest, {
|
|
24
31
|
rewriteRequestHeaders: (req, headers) => {
|
|
25
32
|
const next = { ...headers };
|
|
@@ -51,7 +58,8 @@ module.exports = fp(async (fastify, options) => {
|
|
|
51
58
|
}
|
|
52
59
|
const buf = Buffer.concat(chunks.map(c => (Buffer.isBuffer(c) ? c : Buffer.from(c))));
|
|
53
60
|
const text = rewriteBody(buf.toString('utf8'), mountPrefix);
|
|
54
|
-
|
|
61
|
+
// A stream payload keeps host onSend hooks that wrap string bodies (e.g. response envelopes) from altering app responses.
|
|
62
|
+
return reply.send(Readable.from([Buffer.from(text)], { objectMode: false }));
|
|
55
63
|
}
|
|
56
64
|
return reply.send(res.stream);
|
|
57
65
|
}
|