UNPKG

@sentry/node

Version:

Sentry Node SDK using OpenTelemetry for performance instrumentation

119 lines (118 loc) • 4.4 kB
import { HttpsProxyAgent } from "../proxy/index.js"; import { consoleSandbox, createTransport, suppressTracing } from "@sentry/core"; import * as http from "node:http"; import * as https from "node:https"; import { Readable } from "node:stream"; import { createGzip } from "node:zlib"; // src/transports/http.ts const GZIP_THRESHOLD = 32768; /** * Gets a stream from a Uint8Array or string * Readable.from is ideal but was added in node.js v12.3.0 and v10.17.0 */ function streamFromBody(body) { return new Readable({ read() { this.push(body); this.push(null); } }); } /** * Creates a Transport that uses native the native 'http' and 'https' modules to send events to Sentry. */ function makeNodeTransport(options) { let urlSegments; try { urlSegments = new URL(options.url); } catch { consoleSandbox(() => { console.warn("[@sentry/node]: Invalid dsn or tunnel option, will not send any events. The tunnel option must be a full URL when used."); }); return createTransport(options, () => Promise.resolve({})); } const nativeHttpModule = urlSegments.protocol === "https:" ? https : http; const agent = getHttpAgent(urlSegments, { proxy: options.proxy, keepAlive: options.keepAlive }); const requestExecutor = createRequestExecutor(options, options.httpModule ?? nativeHttpModule, agent); return createTransport(options, requestExecutor); } /** * Returns the agent for requests to `url`: a proxy agent when a proxy applies, otherwise a plain keep-alive agent. * * Proxy prioritization: http => `options.proxy` | `process.env.http_proxy` * Proxy prioritization: https => `options.proxy` | `process.env.https_proxy` | `process.env.http_proxy` * Hosts listed in `process.env.no_proxy` are never proxied, and `options.proxy: false` disables proxying entirely. */ function getHttpAgent(url, options = {}) { const isHttps = url.protocol === "https:"; const proxy = options.proxy === false ? void 0 : applyNoProxyOption(url, options.proxy || (isHttps ? process.env.https_proxy : void 0) || process.env.http_proxy); if (proxy) return new HttpsProxyAgent(proxy); return new (isHttps ? https : http).Agent({ keepAlive: options.keepAlive ?? true, maxSockets: 30, timeout: 2e3 }); } /** * Honors the `no_proxy` env variable with the highest priority to allow for hosts exclusion. * * @param transportUrl The URL the transport intends to send events to. * @param proxy The client configured proxy. * @returns A proxy the transport should use. */ function applyNoProxyOption(transportUrlSegments, proxy) { const { no_proxy } = process.env; if (no_proxy?.split(",").some((exemption) => transportUrlSegments.host.endsWith(exemption) || transportUrlSegments.hostname.endsWith(exemption))) return; else return proxy; } /** * Creates a RequestExecutor to be used with `createTransport`. */ function createRequestExecutor(options, httpModule, agent) { const { hostname, pathname, port, protocol, search } = new URL(options.url); return function makeRequest(request) { const sendRequest = (canRetry) => new Promise((resolve, reject) => { suppressTracing(() => { let body = streamFromBody(request.body); const headers = { ...options.headers }; if (request.body.length > GZIP_THRESHOLD) { headers["content-encoding"] = "gzip"; body = body.pipe(createGzip()); } const hostnameIsIPv6 = hostname.startsWith("["); const req = httpModule.request({ method: "POST", agent, headers, hostname: hostnameIsIPv6 ? hostname.slice(1, -1) : hostname, path: `${pathname}${search}`, port, protocol, ca: options.caCerts }, (res) => { res.on("data", () => {}); res.on("end", () => {}); res.setEncoding("utf8"); const retryAfterHeader = res.headers["retry-after"] ?? null; const rateLimitsHeader = res.headers["x-sentry-rate-limits"] ?? null; resolve({ statusCode: res.statusCode, headers: { "retry-after": retryAfterHeader, "x-sentry-rate-limits": Array.isArray(rateLimitsHeader) ? rateLimitsHeader[0] || null : rateLimitsHeader } }); }); req.on("error", (error) => { if (canRetry && req.reusedSocket && error.code === "ECONNRESET") resolve(sendRequest(false)); else reject(error); }); body.pipe(req); }); }); return sendRequest(true); }; } export { getHttpAgent, makeNodeTransport }; //# sourceMappingURL=http.js.map