@certalis/agent-smith 0.1.12 → 0.1.14
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 +5 -0
- package/dist/bin.js +247 -133
- package/dist/edge.js +25 -10
- package/dist/https.js +34 -18
- package/dist/pool.js +247 -133
- package/dist/post-processor.worker.js +3 -1
- package/dist/server.js +247 -133
- package/package.json +1 -1
package/dist/https.js
CHANGED
|
@@ -129,6 +129,7 @@ var REQUEST_HEADERS = [
|
|
|
129
129
|
"session-id",
|
|
130
130
|
"openai-beta",
|
|
131
131
|
"x-client-request-id",
|
|
132
|
+
"x-stainless-retry-count",
|
|
132
133
|
"user-agent"
|
|
133
134
|
];
|
|
134
135
|
var RESPONSE_HEADERS = [
|
|
@@ -142,35 +143,45 @@ var RESPONSE_HEADERS = [
|
|
|
142
143
|
"anthropic-ratelimit-requests-reset",
|
|
143
144
|
"anthropic-ratelimit-tokens-limit",
|
|
144
145
|
"anthropic-ratelimit-tokens-remaining",
|
|
145
|
-
"anthropic-ratelimit-tokens-reset"
|
|
146
|
+
"anthropic-ratelimit-tokens-reset",
|
|
147
|
+
"content-security-policy",
|
|
148
|
+
"referrer-policy",
|
|
149
|
+
"x-frame-options"
|
|
146
150
|
];
|
|
151
|
+
function isEnrollmentRoute(method, path) {
|
|
152
|
+
return method === "GET" && path === "/pool-enroll" || method === "POST" && ["request", "start", "verify", "confirm", "disconnect"].some((action) => path === `/pool-enroll/${action}`);
|
|
153
|
+
}
|
|
147
154
|
function selectHeaders(headers, names) {
|
|
148
155
|
return Object.fromEntries(names.flatMap((name) => typeof headers[name] === "string" ? [[name, headers[name]]] : []));
|
|
149
156
|
}
|
|
150
|
-
function requestFailure(req, origin, maxBodyBytes) {
|
|
157
|
+
function requestFailure(req, origin, maxBodyBytes, enrollment) {
|
|
151
158
|
const health = req.method === "GET" && req.url === "/pool-health";
|
|
152
|
-
if (!health && !isInferenceRoute(req.method ?? "", req.url ?? ""))
|
|
159
|
+
if (!enrollment && !health && !isInferenceRoute(req.method ?? "", req.url ?? ""))
|
|
153
160
|
return 404;
|
|
154
|
-
if (req.headers.host !== new URL(origin).host || req.headers.origin !== undefined && req.headers.origin !== origin || req.headers["sec-fetch-site"] !== undefined && !["none", "same-origin"].includes(String(req.headers["sec-fetch-site"])))
|
|
161
|
+
if (req.headers.host !== new URL(origin).host || req.headers.origin !== undefined && req.headers.origin !== origin || req.headers["sec-fetch-site"] !== undefined && !(enrollment && req.method === "GET") && !["none", "same-origin"].includes(String(req.headers["sec-fetch-site"])))
|
|
155
162
|
return 403;
|
|
156
163
|
if (req.headers.upgrade || req.headers.expect)
|
|
157
164
|
return 400;
|
|
158
165
|
if (req.headers["content-length"] !== undefined && (!/^\d+$/.test(req.headers["content-length"]) || Number(req.headers["content-length"]) > maxBodyBytes))
|
|
159
166
|
return 413;
|
|
160
|
-
if (
|
|
167
|
+
if (enrollment) {
|
|
168
|
+
if (req.method === "POST" && req.headers.origin !== origin)
|
|
169
|
+
return 403;
|
|
170
|
+
} else if (!req.headers["x-api-key"] && !req.headers.authorization)
|
|
161
171
|
return 401;
|
|
162
172
|
return null;
|
|
163
173
|
}
|
|
164
174
|
function createProxyEdgeHandler({
|
|
165
175
|
origin,
|
|
166
176
|
socketPath,
|
|
177
|
+
enrollmentOrigin,
|
|
167
178
|
maxBodyBytes = 64 * 1024 * 1024,
|
|
168
179
|
timeoutMs = 300000,
|
|
169
180
|
maxConcurrentRequests = 128
|
|
170
181
|
}) {
|
|
171
182
|
const url = new URL(origin);
|
|
172
|
-
if (url.protocol !== "https:" || url.origin !== origin || !isAbsolute2(socketPath) || !Number.isSafeInteger(maxBodyBytes) || maxBodyBytes < 1 || !Number.isSafeInteger(timeoutMs) || timeoutMs < 1 || !Number.isSafeInteger(maxConcurrentRequests) || maxConcurrentRequests < 1) {
|
|
173
|
-
throw new Error("Proxy edge requires an exact HTTPS origin, an absolute socket path and positive limits");
|
|
183
|
+
if (url.protocol !== "https:" || url.origin !== origin || enrollmentOrigin !== undefined && enrollmentOrigin !== origin || !isAbsolute2(socketPath) || !Number.isSafeInteger(maxBodyBytes) || maxBodyBytes < 1 || !Number.isSafeInteger(timeoutMs) || timeoutMs < 1 || !Number.isSafeInteger(maxConcurrentRequests) || maxConcurrentRequests < 1) {
|
|
184
|
+
throw new Error("Proxy edge requires an exact HTTPS origin, a matching enrollment origin, an absolute socket path and positive limits");
|
|
174
185
|
}
|
|
175
186
|
let activeRequests = 0;
|
|
176
187
|
return (req, res) => {
|
|
@@ -187,7 +198,9 @@ function createProxyEdgeHandler({
|
|
|
187
198
|
});
|
|
188
199
|
res.end();
|
|
189
200
|
};
|
|
190
|
-
const
|
|
201
|
+
const enrollment = enrollmentOrigin !== undefined && isEnrollmentRoute(req.method ?? "", req.url ?? "");
|
|
202
|
+
const bodyLimit = enrollment ? Math.min(maxBodyBytes, 8192) : maxBodyBytes;
|
|
203
|
+
const rejected = requestFailure(req, origin, bodyLimit, enrollment);
|
|
191
204
|
if (rejected)
|
|
192
205
|
return finish(rejected);
|
|
193
206
|
if (activeRequests >= maxConcurrentRequests)
|
|
@@ -195,7 +208,9 @@ function createProxyEdgeHandler({
|
|
|
195
208
|
activeRequests++;
|
|
196
209
|
const path = req.method === "GET" && req.url === "/pool-health" ? "/health" : req.url ?? "";
|
|
197
210
|
const headers = selectHeaders(req.headers, REQUEST_HEADERS);
|
|
198
|
-
headers.host = "localhost";
|
|
211
|
+
headers.host = enrollment ? url.host : "localhost";
|
|
212
|
+
if (enrollment && req.headers.origin)
|
|
213
|
+
headers.origin = req.headers.origin;
|
|
199
214
|
headers.connection = "keep-alive";
|
|
200
215
|
headers["x-forwarded-for"] = "proxy-edge";
|
|
201
216
|
let upstreamResponse;
|
|
@@ -240,7 +255,7 @@ function createProxyEdgeHandler({
|
|
|
240
255
|
let bytes = 0;
|
|
241
256
|
req.on("data", (chunk) => {
|
|
242
257
|
bytes += chunk.length;
|
|
243
|
-
if (bytes >
|
|
258
|
+
if (bytes > bodyLimit) {
|
|
244
259
|
req.unpipe(upstream);
|
|
245
260
|
upstream.destroy();
|
|
246
261
|
finish(413);
|
|
@@ -319,7 +334,10 @@ async function prepareStandaloneState(options) {
|
|
|
319
334
|
async function startStandalone(options, poolEntry, env = process.env) {
|
|
320
335
|
process.umask(63);
|
|
321
336
|
await prepareStandaloneState(options);
|
|
322
|
-
const edge = createProxyEdgeHandler(
|
|
337
|
+
const edge = createProxyEdgeHandler({
|
|
338
|
+
...options,
|
|
339
|
+
enrollmentOrigin: env.AGENT_SMITH_ENROLLMENT_ORIGIN
|
|
340
|
+
});
|
|
323
341
|
const server = createServer((req, res) => {
|
|
324
342
|
if (req.method === "GET" && req.url === "/readyz" && req.headers.host === new URL(options.origin).host) {
|
|
325
343
|
res.writeHead(200, {
|
|
@@ -350,17 +368,15 @@ async function startStandalone(options, poolEntry, env = process.env) {
|
|
|
350
368
|
let stopPromise;
|
|
351
369
|
const stop = () => stopPromise ??= (async () => {
|
|
352
370
|
stopping = true;
|
|
353
|
-
|
|
354
|
-
await bounded(new Promise((resolve) => server.close(() => resolve())), 1e4);
|
|
355
|
-
} catch {
|
|
356
|
-
server.closeAllConnections();
|
|
357
|
-
}
|
|
371
|
+
const publicClosed = new Promise((resolve) => server.close(() => resolve()));
|
|
358
372
|
if (!exited)
|
|
359
373
|
child.kill("SIGTERM");
|
|
360
374
|
try {
|
|
361
|
-
await bounded(closed,
|
|
375
|
+
await bounded(Promise.all([publicClosed, closed]), 28000);
|
|
362
376
|
} catch {
|
|
363
|
-
|
|
377
|
+
server.closeAllConnections();
|
|
378
|
+
if (!exited)
|
|
379
|
+
child.kill("SIGKILL");
|
|
364
380
|
await closed;
|
|
365
381
|
}
|
|
366
382
|
})();
|