Skip to content

fix(node): import worker_threads and fix node v14 types #10791

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 2 additions & 18 deletions packages/node-experimental/src/integrations/anr/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { URL } from 'url';
import { defineIntegration, getCurrentScope } from '@sentry/core';
import type { Contexts, Event, EventHint, IntegrationFn } from '@sentry/types';
import { dynamicRequire, logger } from '@sentry/utils';
import type { Worker, WorkerOptions } from 'worker_threads';
import { logger } from '@sentry/utils';
import { Worker } from 'worker_threads';
import { NODE_MAJOR, NODE_VERSION } from '../../nodeVersion';
import type { NodeClient } from '../../sdk/client';
import type { AnrIntegrationOptions, WorkerStartData } from './common';
Expand All @@ -11,24 +11,10 @@ import { base64WorkerScript } from './worker-script';
const DEFAULT_INTERVAL = 50;
const DEFAULT_HANG_THRESHOLD = 5000;

type WorkerNodeV14 = Worker & { new (filename: string | URL, options?: WorkerOptions): Worker };

type WorkerThreads = {
Worker: WorkerNodeV14;
};

function log(message: string, ...args: unknown[]): void {
logger.log(`[ANR] ${message}`, ...args);
}

/**
* We need to use dynamicRequire because worker_threads is not available in node < v12 and webpack error will when
* targeting those versions
*/
function getWorkerThreads(): WorkerThreads {
return dynamicRequire(module, 'worker_threads');
}

/**
* Gets contexts by calling all event processors. This relies on being called after all integrations are setup
*/
Expand Down Expand Up @@ -112,8 +98,6 @@ async function _startWorker(client: NodeClient, _options: Partial<AnrIntegration
}
}

const { Worker } = getWorkerThreads();

const worker = new Worker(new URL(`data:application/javascript;base64,${base64WorkerScript}`), {
workerData: options,
});
Expand Down