@logtail/next
Version:
Better Stack Telemetry Next.js client
71 lines • 3.78 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.POST = exports.createBetterStackProxyHandler = void 0;
const config_1 = require("./config");
const shared_1 = require("./shared");
const defaultTimeoutMs = 5000;
// Route handler alternative to the withBetterStackNextConfig rewrites, which turn any
// upstream failure (e.g. ETIMEDOUT) into a 500 for the client. Always responds 2xx and
// logs delivery failures server-side instead. Mounting instructions: README.
function createBetterStackProxyHandler(options = {}) {
var _a;
const timeoutMs = (_a = options.timeoutMs) !== null && _a !== void 0 ? _a : defaultTimeoutMs;
return function handleProxiedIngest(request) {
return __awaiter(this, void 0, void 0, function* () {
// Strip trailing slash so `trailingSlash: true` in next.config.js still matches.
const pathname = new URL(request.url).pathname.replace(/\/$/, '');
const endpointType = pathname.endsWith(`/${shared_1.EndpointType.webVitals}`)
? shared_1.EndpointType.webVitals
: pathname.endsWith(`/${shared_1.EndpointType.logs}`)
? shared_1.EndpointType.logs
: undefined;
if (!endpointType) {
return new Response(null, { status: 404 });
}
const ingestURL = config_1.config.getIngestURL(endpointType);
if (!ingestURL) {
// Same as the client behavior without env vars: send to /dev/null.
return new Response(null, { status: 204 });
}
const headers = {
'Content-Type': request.headers.get('content-type') || 'application/json',
};
const authorization = request.headers.get('authorization') || (config_1.config.token ? `Bearer ${config_1.config.token}` : null);
if (authorization) {
headers['Authorization'] = authorization;
}
const userAgent = request.headers.get('user-agent');
if (userAgent) {
headers['User-Agent'] = userAgent;
}
try {
const response = yield fetch(ingestURL, {
method: 'POST',
body: yield request.arrayBuffer(),
headers,
signal: AbortSignal.timeout(timeoutMs),
});
if (!response.ok) {
console.warn(`Failed to send telemetry to Better Stack: ${ingestURL} responded with ${response.status}`);
}
}
catch (error) {
// Telemetry delivery must never break the app — log and pretend success.
console.warn(`Failed to send telemetry to Better Stack: ${ingestURL} is unreachable`, error);
}
return new Response(null, { status: 204 });
});
};
}
exports.createBetterStackProxyHandler = createBetterStackProxyHandler;
exports.POST = createBetterStackProxyHandler();
//# sourceMappingURL=proxy.js.map