Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
Draft
Show file tree
Hide file tree
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
18 changes: 17 additions & 1 deletion packages/cloudflare/src/vite/autoInstrument.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { basename, dirname, isAbsolute } from 'node:path';
import { basename, dirname, isAbsolute, resolve } from 'node:path';
import { createIdResolver, type ResolvedConfig } from 'vite';
import { collectAgentCandidates, detectAgentClasses, type ModuleResolver } from './agentClass';
import { buildOptionsImport, ENV_FALLBACK_OPTIONS_FN, resolveInstrumentFile } from './instrumentFile';
Expand Down Expand Up @@ -166,6 +166,22 @@ export function sentryCloudflareAutoInstrumentPlugin(options: { wranglerConfigPa
isVinext = config.plugins?.some(plugin => plugin.name?.startsWith('vinext:')) ?? false;

const result = resolveWranglerConfig(config.root, options.wranglerConfigPath);

// A native Void app. `void:routing` writes the Worker entry, and Void gives it to
// `@cloudflare/vite-plugin` in code, so a wrangler config is optional and its `main` is not used.
if (config.plugins?.some(plugin => plugin.name === 'void:routing')) {
wranglerConfig = result?.config ?? {
durableObjects: [],
workflows: [],
workerEntrypoints: [],
sameWorkerBindings: [],
};
entryFilePath = normalizePath(resolve(config.root, '.void/entry.ts'));
// `.void` is a generated directory that is not in version control.
instrumentFilePath = resolveInstrumentFile(config.root);
return;
}

if (!result) {
// An explicit path that fails is a misconfiguration worth naming;
// without one, hint at the option so custom-named configs (e.g. a
Expand Down
3 changes: 3 additions & 0 deletions packages/cloudflare/src/vite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ export interface SentryCloudflareVitePluginOptions {
* left alone, so this is safe alongside manual instrumentation. Set to
* `false` to opt out.
*
* A native Void app needs no wrangler config. The plugin wraps the Worker entry that Void generates and
* reads the options from an `instrument.server.*` file in the Vite root.
*
* The plugin also adds the bindings that resolve to the wrapped classes (this worker's own
* Durable Objects and self service bindings) to `rpcTracePropagationBindings`. Bindings to
* other workers stay opt-in, their receivers may not run Sentry.
Expand Down
42 changes: 42 additions & 0 deletions packages/cloudflare/test/vite/autoInstrument.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,48 @@ describe('wranglerConfigPath option', () => {
});
});

describe('native Void app', () => {
const entryCode = 'export default class extends WorkerEntrypoint { fetch(request) { return handle(request); } }';

it('wraps the generated entry with the options file from the Vite root, without a wrangler config', async () => {
const dir = writeTempDir({ 'instrument.server.ts': 'export default env => ({ dsn: env.SENTRY_DSN });' });
const warnings: string[] = [];
const plugin = sentryCloudflareAutoInstrumentPlugin();
plugin.configResolved({
root: dir,
plugins: [{ name: 'void:routing' }],
logger: { warn: msg => warnings.push(msg) },
});

const result = await plugin.transform.call(
{ parse: (c: string) => parseJS(c) },
entryCode,
join(dir, '.void/entry.ts'),
);

expect(warnings).toEqual([]);
expect(result.code).toBe(
[
"import * as __SENTRY__ from '@sentry/cloudflare';",
"import __SENTRY_OPTIONS_CALLBACK__ from '../instrument.server.ts';",
`var __SENTRY_DEFAULT_EXPORT__ = { default: ${entryCode.replace('export default ', '')} }.default;`,
'export { __SENTRY_DEFAULT_EXPORT__ as default };',
'__SENTRY_DEFAULT_EXPORT__ = __SENTRY__.withSentry(__SENTRY_OPTIONS_CALLBACK__, __SENTRY_DEFAULT_EXPORT__);',
].join('\n'),
);
});

it('uses the generated entry and the settings of a wrangler config that names another `main`', async () => {
const dir = writeTempDir({ 'wrangler.toml': 'name = "void-app"\nmain = "src/index.ts"' });
const plugin = sentryCloudflareAutoInstrumentPlugin();
plugin.configResolved({ root: dir, plugins: [{ name: 'void:routing' }] });
const tx = (id: string) => plugin.transform.call({ parse: (c: string) => parseJS(c) }, entryCode, id);

expect(await tx(join(dir, 'src/index.ts'))).toBeUndefined();
expect((await tx(join(dir, '.void/entry.ts'))).code).toContain('"name":"void-app"');
});
});

describe('package specifier as wrangler `main`', () => {
const MAIN = 'vinext/server/fetch-handler';
const code = 'export default { fetch() { return new Response("ok"); } };';
Expand Down
Loading