MCPcopy Create free account
hub / github.com/getsentry/sentry-javascript / CodeInjection

Class CodeInjection

packages/bundler-plugins/src/core/utils.ts:446–484  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

444}
445
446export class CodeInjection {
447 // The code below is mostly ternary operators because it saves bundle size.
448 // The checks are to support as many environments as possible. (Node.js, Browser, webworkers, etc.)
449 private readonly header: string;
450 private readonly footer: string;
451
452 constructor(private body: string = '') {
453 this.header = `!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{};`;
454 this.footer = '}catch(e){}}();';
455 }
456
457 public code(): string {
458 if (this.isEmpty()) {
459 return '';
460 }
461
462 return this.header + this.body + this.footer;
463 }
464
465 public isEmpty(): boolean {
466 return this.body.length === 0;
467 }
468
469 public append(code: CodeInjection | string): void {
470 if (code instanceof CodeInjection) {
471 this.body += code.body;
472 } else {
473 this.body += code;
474 }
475 }
476
477 public clear(): void {
478 this.body = '';
479 }
480
481 public clone(): CodeInjection {
482 return new CodeInjection(this.body);
483 }
484}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected