runlocal 0.9.0 → 0.10.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/lib.js +17 -7
- package/package.json +1 -1
package/lib.js
CHANGED
|
@@ -105,16 +105,23 @@ function filterHeaders(headers) {
|
|
|
105
105
|
}
|
|
106
106
|
|
|
107
107
|
function buildWsUrl(host, apiKey, subdomain) {
|
|
108
|
-
const params = new URLSearchParams({ vsn: "2.0.0" });
|
|
108
|
+
const params = new URLSearchParams({ vsn: "2.0.0", caps: "binary-bodies" });
|
|
109
109
|
if (apiKey) params.set("api_key", apiKey);
|
|
110
110
|
if (subdomain) params.set("subdomain", subdomain);
|
|
111
111
|
return `${host}/tunnel/websocket?${params.toString()}`;
|
|
112
112
|
}
|
|
113
113
|
|
|
114
114
|
function handleRequest(ws, joinRef, topic, payload, target, nextRef, log) {
|
|
115
|
-
const { request_id, method, path, query_string, headers, body } = payload;
|
|
115
|
+
const { request_id, method, path, query_string, headers, body, body_encoding } = payload;
|
|
116
116
|
const fullPath = query_string ? `${path}?${query_string}` : path;
|
|
117
117
|
|
|
118
|
+
let requestBody = null;
|
|
119
|
+
if (body && body.length > 0) {
|
|
120
|
+
requestBody = body_encoding === "base64"
|
|
121
|
+
? Buffer.from(body, "base64")
|
|
122
|
+
: Buffer.from(body);
|
|
123
|
+
}
|
|
124
|
+
|
|
118
125
|
const timestamp = new Date().toLocaleTimeString();
|
|
119
126
|
log(
|
|
120
127
|
`${DIM}${timestamp}${RESET} ${BOLD}${method}${RESET} ${fullPath}`
|
|
@@ -136,7 +143,7 @@ function handleRequest(ws, joinRef, topic, payload, target, nextRef, log) {
|
|
|
136
143
|
const chunks = [];
|
|
137
144
|
proxyRes.on("data", (chunk) => chunks.push(chunk));
|
|
138
145
|
proxyRes.on("end", () => {
|
|
139
|
-
const
|
|
146
|
+
const respBuffer = Buffer.concat(chunks);
|
|
140
147
|
const respHeaders = [];
|
|
141
148
|
for (const [k, v] of Object.entries(proxyRes.headers)) {
|
|
142
149
|
if (Array.isArray(v)) {
|
|
@@ -164,7 +171,8 @@ function handleRequest(ws, joinRef, topic, payload, target, nextRef, log) {
|
|
|
164
171
|
request_id,
|
|
165
172
|
status: proxyRes.statusCode,
|
|
166
173
|
headers: respHeaders,
|
|
167
|
-
body:
|
|
174
|
+
body: respBuffer.toString("base64"),
|
|
175
|
+
body_encoding: "base64",
|
|
168
176
|
},
|
|
169
177
|
])
|
|
170
178
|
);
|
|
@@ -175,6 +183,7 @@ function handleRequest(ws, joinRef, topic, payload, target, nextRef, log) {
|
|
|
175
183
|
log(
|
|
176
184
|
`${DIM}${timestamp}${RESET} ${RED}ERR${RESET} ${fullPath} — ${err.message}`
|
|
177
185
|
);
|
|
186
|
+
const errBody = `Could not connect to ${target.display} — ${err.message}`;
|
|
178
187
|
ws.send(
|
|
179
188
|
JSON.stringify([
|
|
180
189
|
joinRef,
|
|
@@ -185,14 +194,15 @@ function handleRequest(ws, joinRef, topic, payload, target, nextRef, log) {
|
|
|
185
194
|
request_id,
|
|
186
195
|
status: 502,
|
|
187
196
|
headers: [["content-type", "text/plain"]],
|
|
188
|
-
body:
|
|
197
|
+
body: Buffer.from(errBody, "utf8").toString("base64"),
|
|
198
|
+
body_encoding: "base64",
|
|
189
199
|
},
|
|
190
200
|
])
|
|
191
201
|
);
|
|
192
202
|
});
|
|
193
203
|
|
|
194
|
-
if (
|
|
195
|
-
proxyReq.write(
|
|
204
|
+
if (requestBody && requestBody.length > 0) {
|
|
205
|
+
proxyReq.write(requestBody);
|
|
196
206
|
}
|
|
197
207
|
proxyReq.end();
|
|
198
208
|
}
|