From b15a7a3ce6087d1cd59f240f32a73c0e88b37d78 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Wed, 14 Aug 2024 11:46:10 +0000 Subject: [PATCH] fix(deno): Don't rely on `Deno.permissions.querySync` --- packages/deno/src/client.ts | 5 +++++ packages/deno/src/integrations/context.ts | 6 +++--- .../deno/src/integrations/normalizepaths.ts | 5 +++++ packages/deno/src/transports/index.ts | 21 ++++++++++++------- 4 files changed, 27 insertions(+), 10 deletions(-) diff --git a/packages/deno/src/client.ts b/packages/deno/src/client.ts index 1db45a5cb960..6f4e37f1ed74 100644 --- a/packages/deno/src/client.ts +++ b/packages/deno/src/client.ts @@ -4,6 +4,11 @@ import { SDK_VERSION, ServerRuntimeClient } from '@sentry/core'; import type { DenoClientOptions } from './types'; function getHostName(): string | undefined { + // Deno.permissions.querySync is not available on Deno Deploy + if (!Deno.permissions.querySync) { + return undefined; + } + const result = Deno.permissions.querySync({ name: 'sys', kind: 'hostname' }); return result.state === 'granted' ? Deno.hostname() : undefined; } diff --git a/packages/deno/src/integrations/context.ts b/packages/deno/src/integrations/context.ts index 69ef164bb32d..926fcd065f1b 100644 --- a/packages/deno/src/integrations/context.ts +++ b/packages/deno/src/integrations/context.ts @@ -16,8 +16,8 @@ function getOSName(): string { } } -function getOSRelease(): string | undefined { - return Deno.permissions.querySync({ name: 'sys', kind: 'osRelease' }).state === 'granted' +async function getOSRelease(): Promise { + return (await Deno.permissions.query({ name: 'sys', kind: 'osRelease' })).state === 'granted' ? Deno.osRelease() : undefined; } @@ -35,7 +35,7 @@ async function addDenoRuntimeContext(event: Event): Promise { }, os: { name: getOSName(), - version: getOSRelease(), + version: await getOSRelease(), }, v8: { name: 'v8', diff --git a/packages/deno/src/integrations/normalizepaths.ts b/packages/deno/src/integrations/normalizepaths.ts index f984d5bebc33..07e0f872338b 100644 --- a/packages/deno/src/integrations/normalizepaths.ts +++ b/packages/deno/src/integrations/normalizepaths.ts @@ -42,6 +42,11 @@ function appRootFromErrorStack(error: Error): string | undefined { } function getCwd(): string | undefined { + // Deno.permissions.querySync is not available on Deno Deploy + if (!Deno.permissions.querySync) { + return undefined; + } + // We don't want to prompt for permissions so we only get the cwd if // permissions are already granted const permission = Deno.permissions.querySync({ name: 'read', path: './' }); diff --git a/packages/deno/src/transports/index.ts b/packages/deno/src/transports/index.ts index 964c1a9347af..c678688c2462 100644 --- a/packages/deno/src/transports/index.ts +++ b/packages/deno/src/transports/index.ts @@ -1,6 +1,6 @@ import { createTransport } from '@sentry/core'; import type { BaseTransportOptions, Transport, TransportMakeRequestResponse, TransportRequest } from '@sentry/types'; -import { consoleSandbox, rejectedSyncPromise } from '@sentry/utils'; +import { consoleSandbox, logger, rejectedSyncPromise } from '@sentry/utils'; export interface DenoTransportOptions extends BaseTransportOptions { /** Custom headers for the transport. Used by the XHRTransport and FetchTransport */ @@ -13,13 +13,20 @@ export interface DenoTransportOptions extends BaseTransportOptions { export function makeFetchTransport(options: DenoTransportOptions): Transport { const url = new URL(options.url); - if (Deno.permissions.querySync({ name: 'net', host: url.host }).state !== 'granted') { - consoleSandbox(() => { - // eslint-disable-next-line no-console - console.warn(`Sentry SDK requires 'net' permission to send events. - Run with '--allow-net=${url.host}' to grant the requires permissions.`); + Deno.permissions + .query({ name: 'net', host: url.host }) + .then(({ state }) => { + if (state !== 'granted') { + consoleSandbox(() => { + // eslint-disable-next-line no-console + console.warn(`Sentry SDK requires 'net' permission to send events. + Run with '--allow-net=${url.host}' to grant the requires permissions.`); + }); + } + }) + .catch(() => { + logger.warn('Failed to read the "net" permission.'); }); - } function makeRequest(request: TransportRequest): PromiseLike { const requestOptions: RequestInit = {