Add store header OCR and market observation outbox
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
import { defineConfig, loadEnv } from "vite";
|
||||
|
||||
const MAX_BATCH_BYTES = 512 * 1024;
|
||||
|
||||
export async function forwardMarketBatch(body, { url, token, fetchImpl = fetch }) {
|
||||
if (!url || !token) return new Response("Market import is not configured", { status: 503 });
|
||||
return fetchImpl(url, {
|
||||
method: "POST",
|
||||
headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json" },
|
||||
body,
|
||||
});
|
||||
}
|
||||
|
||||
function marketImportProxy(options) {
|
||||
return {
|
||||
name: "market-import-proxy",
|
||||
configureServer(server) {
|
||||
server.middlewares.use("/api/market-import", async (request, response) => {
|
||||
if (request.method !== "POST") {
|
||||
response.statusCode = 405;
|
||||
response.end();
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const body = await readBody(request);
|
||||
const upstream = await forwardMarketBatch(body, options);
|
||||
response.statusCode = upstream.status;
|
||||
response.setHeader("Content-Type", upstream.headers.get("content-type") || "application/json");
|
||||
response.end(await upstream.text());
|
||||
} catch (error) {
|
||||
response.statusCode = error.code === "BATCH_TOO_LARGE" ? 413 : 502;
|
||||
response.end(error.code === "BATCH_TOO_LARGE" ? "Batch too large" : "Market import failed");
|
||||
}
|
||||
});
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function readBody(request) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const chunks = [];
|
||||
let size = 0;
|
||||
let tooLarge = false;
|
||||
request.on("data", (chunk) => {
|
||||
if (tooLarge) return;
|
||||
size += chunk.length;
|
||||
if (size > MAX_BATCH_BYTES) {
|
||||
tooLarge = true;
|
||||
const error = new Error("Batch too large");
|
||||
error.code = "BATCH_TOO_LARGE";
|
||||
reject(error);
|
||||
return;
|
||||
}
|
||||
chunks.push(chunk);
|
||||
});
|
||||
request.on("end", () => {
|
||||
if (!tooLarge) resolve(Buffer.concat(chunks).toString("utf8"));
|
||||
});
|
||||
request.on("error", reject);
|
||||
});
|
||||
}
|
||||
|
||||
export default defineConfig(({ mode }) => {
|
||||
const environment = loadEnv(mode, process.cwd(), "");
|
||||
return {
|
||||
plugins: [
|
||||
marketImportProxy({
|
||||
url: environment.L2_MARKET_IMPORT_URL,
|
||||
token: environment.L2_MARKET_IMPORT_TOKEN,
|
||||
}),
|
||||
],
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user