@sentry/wizard
Version:
Sentry wizard helping you to configure your project
116 lines • 4.88 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.wrapWorkerWithSentry = void 0;
const recast = __importStar(require("recast"));
// @ts-expect-error - magicast is ESM and TS complains about that. It works though
const magicast_1 = require("magicast");
// @ts-expect-error - clack is ESM and TS complains about that. It works though
const clack = __importStar(require("@clack/prompts"));
const ast_utils_1 = require("../utils/ast-utils");
const chalk_1 = __importDefault(require("chalk"));
const b = recast.types.builders;
/**
* Wraps a Cloudflare Worker's default export with Sentry.withSentry()
*
* Before:
* ```
* export default {
* async fetch(request, env, ctx) { ... }
* } satisfies ExportedHandler<Env>;
* ```
*
* After:
* ```
* import * as Sentry from '@sentry/cloudflare';
*
* export default Sentry.withSentry(
* (env) => ({
* dsn: 'your-dsn',
* tracesSampleRate: 1,
* }),
* {
* async fetch(request, env, ctx) { ... }
* } satisfies ExportedHandler<Env>
* );
* ```
*
* @param workerFilePath - Path to the worker file to wrap
* @param dsn - Sentry DSN for initialization
* @param selectedFeatures - Feature flags for optional Sentry features
*/
async function wrapWorkerWithSentry(workerFilePath, dsn, selectedFeatures) {
const workerAst = await (0, magicast_1.loadFile)(workerFilePath);
if ((0, ast_utils_1.hasSentryContent)(workerAst.$ast)) {
clack.log.warn(`Sentry is already configured in ${chalk_1.default.cyan(workerFilePath)}. Skipping wrapping with Sentry.`);
return;
}
workerAst.imports.$add({
from: '@sentry/cloudflare',
imported: '*',
local: 'Sentry',
});
recast.visit(workerAst.$ast, {
visitExportDefaultDeclaration(path) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
const originalDeclaration = path.value.declaration;
const sentryConfig = createSentryConfigFunction(dsn, selectedFeatures);
const wrappedExport = b.callExpression(b.memberExpression(b.identifier('Sentry'), b.identifier('withSentry')), [sentryConfig, originalDeclaration]);
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
path.value.declaration = wrappedExport;
return false;
},
});
await (0, magicast_1.writeFile)(workerAst.$ast, workerFilePath);
}
exports.wrapWorkerWithSentry = wrapWorkerWithSentry;
/**
* Creates the Sentry config function: (env) => ({ dsn: '...', ... })
*/
function createSentryConfigFunction(dsn, selectedFeatures) {
const configProperties = [
b.objectProperty(b.identifier('dsn'), b.stringLiteral(dsn)),
];
if (selectedFeatures.performance) {
const tracesSampleRateProperty = b.objectProperty(b.identifier('tracesSampleRate'), b.numericLiteral(1));
tracesSampleRateProperty.comments = [
b.commentLine(' Define how likely traces are sampled. Adjust this value in production, or use tracesSampler for greater control.', true, false),
];
configProperties.push(tracesSampleRateProperty);
}
if (selectedFeatures.logs) {
const enableLogsProperty = b.objectProperty(b.identifier('enableLogs'), b.booleanLiteral(true));
enableLogsProperty.comments = [
b.commentLine(' Enable logs to be sent to Sentry', true, false),
];
configProperties.push(enableLogsProperty);
}
const configObject = b.objectExpression(configProperties);
return b.arrowFunctionExpression([b.identifier('env')], configObject);
}
//# sourceMappingURL=wrap-worker.js.map