Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import type { LightPlugin, LightClient, Transaction, SpanStatus, LightConfig } from '../types';
import { now, isObject } from '../utils/helper';
interface RouterPluginConfig {
enable?: boolean;
instrumentHistory?: boolean;
instrumentHash?: boolean;
routeNameFormatter?: (url: string) => string;
maxTransactionDurationMs?: number;
}
class RouterPlugin implements LightPlugin {
name = 'router';
version = '1.0.0';
private client!: LightClient;
private config: RouterPluginConfig = {
enable: true,
instrumentHistory: true,
instrumentHash: true,
maxTransactionDurationMs: 30000,
};
private currentTransaction: Transaction | null = null;
private lastRoute: string = '';
private originalPushState?: typeof history.pushState;
private originalReplaceState?: typeof history.replaceState;
private maxDurationTimer: ReturnType<typeof setTimeout> | null = null;
private routeChangeTimer: ReturnType<typeof setTimeout> | null = null;
setup(client: LightClient): void {
this.client = client;
this.loadConfig();
if (typeof window === 'undefined') return;
if (!this.config.enable) return;
if (this.config.instrumentHistory) {
this.instrumentHistory();
}
if (this.config.instrumentHash) {
this.instrumentHash();
}
}
private loadConfig(): void {
const clientConfig = this.client.config as LightConfig;
if (clientConfig && isObject((clientConfig as Record<string, unknown>).router)) {
this.config = { ...this.config, ...((clientConfig as Record<string, unknown>).router as object) };
}
}
private instrumentHistory(): void {
if (typeof history === 'undefined' || !history.pushState) return;
this.originalPushState = history.pushState.bind(history);
this.originalReplaceState = history.replaceState.bind(history);
history.pushState = (data: unknown, title: string, url?: string | null): void => {
this.originalPushState!(data, title, url);
this.handleRouteChange(url || location.pathname + location.search, 'pushState');
};
history.replaceState = (data: unknown, title: string, url?: string | null): void => {
this.originalReplaceState!(data, title, url);
this.handleRouteChange(url || location.pathname + location.search, 'replaceState');
};
window.addEventListener('popstate', this.handlePopState);
}
private handlePopState = (): void => {
this.handleRouteChange(location.pathname + location.search, 'popstate');
};
private instrumentHash(): void {
window.addEventListener('hashchange', this.handleHashChange);
}
private handleHashChange = (): void => {
this.handleRouteChange(location.hash || '#/', 'hashchange');
};
private handleRouteChange(to: string, source: string): void {
if (this.routeChangeTimer) {
clearTimeout(this.routeChangeTimer);
}
this.routeChangeTimer = setTimeout(() => {
this.routeChangeTimer = null;
this.processRouteChange(to, source);
}, 50);
}
private processRouteChange(to: string, source: string): void {
const routeName = this.config.routeNameFormatter
? this.config.routeNameFormatter(to)
: to.split('?')[0];
if (routeName === this.lastRoute) return;
this.lastRoute = routeName;
this.finishCurrentTransaction('ok');
this.startNavigationTransaction(routeName, source);
}
private startNavigationTransaction(name: string, source: string): void {
if (!this.client) return;
const transaction = this.client.startTransaction({
name,
op: 'navigation',
description: `Navigation to ${name}`,
type: 'navigation',
tags: {
'route.name': name,
'route.source': source,
},
});
if (transaction) {
this.currentTransaction = transaction;
this.startMaxDurationTimer();
}
}
private startMaxDurationTimer(): void {
if (this.maxDurationTimer) {
clearTimeout(this.maxDurationTimer);
}
const maxDuration = this.config.maxTransactionDurationMs ?? 30000;
this.maxDurationTimer = setTimeout(() => {
if (this.currentTransaction) {
this.currentTransaction.setStatus('deadline_exceeded');
this.finishCurrentTransaction('deadline_exceeded');
}
}, maxDuration);
}
private finishCurrentTransaction(status: SpanStatus = 'ok'): void {
if (this.maxDurationTimer) {
clearTimeout(this.maxDurationTimer);
this.maxDurationTimer = null;
}
if (this.currentTransaction) {
this.currentTransaction.setStatus(status);
this.currentTransaction.finish();
this.currentTransaction = null;
}
}
destroy(): void {
if (this.originalPushState && typeof history !== 'undefined') {
history.pushState = this.originalPushState;
}
if (this.originalReplaceState && typeof history !== 'undefined') {
history.replaceState = this.originalReplaceState;
}
if (typeof window !== 'undefined') {
window.removeEventListener('popstate', this.handlePopState);
window.removeEventListener('hashchange', this.handleHashChange);
}
if (this.maxDurationTimer) {
clearTimeout(this.maxDurationTimer);
this.maxDurationTimer = null;
}
if (this.routeChangeTimer) {
clearTimeout(this.routeChangeTimer);
this.routeChangeTimer = null;
}
this.finishCurrentTransaction('aborted');
}
}
export default RouterPlugin;
|