GeezerVison is showing github.com — ref(core): Keep the Node tunnel handler out of bundles without a tunnel · getsentry/sentry-javascript@033d6b4
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/core/src/integrations/http/server-subscription.ts +3-4Lines changed: 3 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -25,8 +25,7 @@ import { DEBUG_BUILD } from '../../debug-build'; | 25 | 25 | import { debug } from '../../utils/debug-logger'; | 26 | 26 | import { safeCallback } from '../../utils/safeCallback'; | 27 | 27 | import { isServedTunnelRequest } from '../../utils/tunnel'; | 28 | | -import { handleServedNodeTunnelRequest } from '../../utils/tunnelForward'; | 29 | | -import { setHttpServerAnswersTunnel } from '../../utils/tunnelStore'; | | 28 | +import { getNodeTunnelHandler, setHttpServerHandlesTunnel } from '../../utils/tunnelStore'; | 30 | 29 | import { getClient, getCurrentScope, getIsolationScope, withIsolationScope } from '../../currentScopes'; | 31 | 30 | import { hasSpansEnabled } from '../../utils/hasSpansEnabled'; | 32 | 31 | import { | @@ -139,7 +138,7 @@ export function instrumentServer(options: HttpInstrumentationOptions, server: Ht | 139 | 138 | // second wrapper for the tunnel. With `intercept: false` the app answers them, so they keep their own isolation | 140 | 139 | // scope. | 141 | 140 | if (isServedTunnelRequest(request)) { | 142 | | - if (handleServedNodeTunnelRequest(request, response)) { | | 141 | + if (getNodeTunnelHandler()?.(request, response)) { | 143 | 142 | return true; | 144 | 143 | } | 145 | 144 |
| @@ -258,7 +257,7 @@ export function getHttpServerSubscriptions(options: HttpInstrumentationOptions): | 258 | 257 | // this because it creates spans through a separate code path). | 259 | 258 | const userWrap = options.wrapServerEmitRequest; | 260 | 259 | const spanWrap = buildServerSpanWrap(options); | 261 | | - setHttpServerAnswersTunnel(); | | 260 | + setHttpServerHandlesTunnel(); | 262 | 261 |
| 263 | 262 | const effectiveOptions: HttpInstrumentationOptions = { | 264 | 263 | ...options, | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/server.ts +4-1Lines changed: 4 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -14,7 +14,10 @@ export { | 14 | 14 | isServedTunnelRequest as _INTERNAL_isServedTunnelRequest, | 15 | 15 | isTunnelRequest, | 16 | 16 | } from './utils/tunnel'; | 17 | | -export { httpServerAnswersTunnel as _INTERNAL_httpServerAnswersTunnel } from './utils/tunnelStore'; | | 17 | +export { | | 18 | + httpServerHandlesTunnel as _INTERNAL_httpServerHandlesTunnel, | | 19 | + setNodeTunnelHandler as _INTERNAL_setNodeTunnelHandler, | | 20 | +} from './utils/tunnelStore'; | 18 | 21 | export { | 19 | 22 | forwardTunnelRequest as _INTERNAL_forwardTunnelRequest, | 20 | 23 | handleServedNodeTunnelRequest as _INTERNAL_handleServedNodeTunnelRequest, | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/utils/tunnelForward.ts +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -130,6 +130,11 @@ export function handleTunnelRequestIfMatched( | 130 | 130 | ); | 131 | 131 | } | 132 | 132 |
| | 133 | +/** | | 134 | + * Answers a tunnel request on a Node server ahead of the app, see {@link handleServedNodeTunnelRequest}. | | 135 | + */ | | 136 | +export type NodeTunnelHandler = (request: NodeTunnelRequest, response: NodeTunnelResponse) => boolean; | | 137 | + | 133 | 138 | /** | 134 | 139 | * Answers a request to the tunnel `serveTunnelIntegration` serves on a Node server, ahead of the app. Returns `true` | 135 | 140 | * when it claimed the request and answers it, `false` to hand the request to the app. | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/utils/tunnelStore.ts +25-8Lines changed: 25 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -3,6 +3,7 @@ import { getGlobalSingleton } from '../carrier'; | 3 | 3 | import type { IgnoreSpanFilter } from '../types/options'; | 4 | 4 | import type { TunnelDefaults } from './tunnelDefaults'; | 5 | 5 | import type { ServedTunnel, TunnelForwarder } from './tunnel'; | | 6 | +import type { NodeTunnelHandler } from './tunnelForward'; | 6 | 7 |
| 7 | 8 | /** | 8 | 9 | * What the tunnel keeps per server client. | @@ -23,8 +24,10 @@ export interface ClientTunnelState { | 23 | 24 | export interface TunnelStore { | 24 | 25 | clients: WeakMap<Client, ClientTunnelState>; | 25 | 26 | defaults?: TunnelDefaults; | 26 | | - /** Whether core's HTTP server instrumentation is set up, whose `emit` wrapper answers tunnel requests itself. */ | 27 | | - httpServerAnswersTunnel?: boolean; | | 27 | + /** Whether core's HTTP server instrumentation is set up, whose `emit` wrapper handles tunnel requests itself. */ | | 28 | + httpServerHandlesTunnel?: boolean; | | 29 | + /** Answers tunnel requests on Node servers. The tunnel sets it, so apps without one don't bundle it. */ | | 30 | + nodeHandler?: NodeTunnelHandler; | 28 | 31 | } | 29 | 32 |
| 30 | 33 | /** | @@ -64,16 +67,30 @@ export function setStoredTunnelDefaults(defaults: TunnelDefaults | undefined): v | 64 | 67 | } | 65 | 68 |
| 66 | 69 | /** | 67 | | - * Records that core's HTTP server instrumentation is set up. Its `emit` wrapper answers tunnel requests, so `node:http` | | 70 | + * Records that core's HTTP server instrumentation is set up. Its `emit` wrapper handles tunnel requests, so `node:http` | 68 | 71 | * servers need no second one for the tunnel. | 69 | 72 | */ | 70 | | -export function setHttpServerAnswersTunnel(): void { | 71 | | - getTunnelStore().httpServerAnswersTunnel = true; | | 73 | +export function setHttpServerHandlesTunnel(): void { | | 74 | + getTunnelStore().httpServerHandlesTunnel = true; | 72 | 75 | } | 73 | 76 |
| 74 | 77 | /** | 75 | | - * Whether core's HTTP server instrumentation answers tunnel requests, see {@link setHttpServerAnswersTunnel}. | | 78 | + * Whether core's HTTP server instrumentation handles tunnel requests, see {@link setHttpServerHandlesTunnel}. | 76 | 79 | */ | 77 | | -export function httpServerAnswersTunnel(): boolean { | 78 | | - return !!getTunnelStore().httpServerAnswersTunnel; | | 80 | +export function httpServerHandlesTunnel(): boolean { | | 81 | + return !!getTunnelStore().httpServerHandlesTunnel; | | 82 | +} | | 83 | + | | 84 | +/** | | 85 | + * Sets what answers tunnel requests on Node servers, like the forwarder `serveTunnelIntegration` brings in. | | 86 | + */ | | 87 | +export function setNodeTunnelHandler(handler: NodeTunnelHandler): void { | | 88 | + getTunnelStore().nodeHandler = handler; | | 89 | +} | | 90 | + | | 91 | +/** | | 92 | + * What answers tunnel requests on Node servers, see {@link setNodeTunnelHandler}. | | 93 | + */ | | 94 | +export function getNodeTunnelHandler(): NodeTunnelHandler | undefined { | | 95 | + return getTunnelStore().nodeHandler; | 79 | 96 | } |
Collapse file Copy file name to clipboardExpand all lines: packages/server-utils/src/integrations/serveTunnel.ts +9-1Lines changed: 9 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import type { ServerTunnelOptions } from '@sentry/core'; | 2 | 2 | import { defineIntegration } from '@sentry/core'; | 3 | | -import { HTTP_ON_SERVER_REQUEST, serveTunnelIntegration as coreServeTunnelIntegration } from '@sentry/core/server'; | | 3 | +import { | | 4 | + _INTERNAL_handleServedNodeTunnelRequest as handleServedNodeTunnelRequest, | | 5 | + _INTERNAL_setNodeTunnelHandler as setNodeTunnelHandler, | | 6 | + HTTP_ON_SERVER_REQUEST, | | 7 | + serveTunnelIntegration as coreServeTunnelIntegration, | | 8 | +} from '@sentry/core/server'; | 4 | 9 | import { subscribe } from '../utils/diagnosticsChannel'; | 5 | 10 | import { instrumentTunnelServer } from '../utils/tunnelServer'; | 6 | 11 |
| @@ -16,6 +21,9 @@ export function instrumentTunnelServers(): void { | 16 | 21 | } | 17 | 22 |
| 18 | 23 | isSubscribed = true; | | 24 | + // Core's HTTP server instrumentation answers tunnel requests through it, without bundling it for apps that have no | | 25 | + // tunnel. | | 26 | + setNodeTunnelHandler(handleServedNodeTunnelRequest); | 19 | 27 | subscribe(HTTP_ON_SERVER_REQUEST, instrumentTunnelServer); | 20 | 28 | } | 21 | 29 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/server-utils/src/utils/tunnelServer.ts +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { debug } from '@sentry/core'; | 2 | 2 | import { | 3 | 3 | _INTERNAL_handleServedNodeTunnelRequest as handleServedNodeTunnelRequest, | 4 | | - _INTERNAL_httpServerAnswersTunnel as httpServerAnswersTunnel, | | 4 | + _INTERNAL_httpServerHandlesTunnel as httpServerHandlesTunnel, | 5 | 5 | } from '@sentry/core/server'; | 6 | 6 | import { DEBUG_BUILD } from '../debug-build'; | 7 | 7 |
| @@ -20,7 +20,7 @@ const tunnelServers = new WeakSet<TunnelServer>(); | 20 | 20 | export function instrumentTunnelServer(message: unknown): void { | 21 | 21 | try { | 22 | 22 | const server = (message as { server?: TunnelServer } | undefined)?.server; | 23 | | - if (!server || tunnelServers.has(server) || httpServerAnswersTunnel()) { | | 23 | + if (!server || tunnelServers.has(server) || httpServerHandlesTunnel()) { | 24 | 24 | return; | 25 | 25 | } | 26 | 26 |
| |
|