-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix(nitro): Support nested _platform
properties in Nitro 2.11.7+
#17596
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
// fixme: Can this be exported like this? | ||
export { sentryCloudflareNitroPlugin } from './sentry-cloudflare.server'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import type { CfProperties, ExecutionContext } from '@cloudflare/workers-types'; | ||
|
||
interface EventBase { | ||
protocol: string; | ||
host: string; | ||
method: string; | ||
headers: Record<string, string>; | ||
} | ||
|
||
interface MinimalCloudflareProps { | ||
context: ExecutionContext; | ||
request?: Record<string, unknown>; | ||
env?: Record<string, unknown>; | ||
} | ||
|
||
interface CloudflareContext { | ||
cf: CfProperties; | ||
cloudflare: MinimalCloudflareProps; | ||
} | ||
|
||
// Direct shape: cf and cloudflare are directly on context | ||
interface CfEventDirect extends EventBase { | ||
context: CloudflareContext; | ||
} | ||
|
||
// Nested shape: cf and cloudflare are under _platform | ||
// Since Nitro v2.11.7 (PR: https://github.com/nitrojs/nitro/pull/3224) | ||
interface CfEventPlatform extends EventBase { | ||
context: { | ||
_platform: CloudflareContext; | ||
}; | ||
} | ||
|
||
export type CfEventType = CfEventDirect | CfEventPlatform; | ||
|
||
function hasCloudflareProperty(context: unknown): boolean { | ||
return ( | ||
context !== null && | ||
typeof context === 'object' && | ||
// context.cloudflare properties | ||
'cloudflare' in context && | ||
typeof context.cloudflare === 'object' && | ||
context.cloudflare !== null && | ||
'context' in context.cloudflare | ||
); | ||
} | ||
|
||
/** | ||
* Type guard to check if an event context object has cf properties | ||
*/ | ||
export function hasCfProperty(context: unknown): context is { cf: CfProperties } { | ||
return ( | ||
context !== null && | ||
typeof context === 'object' && | ||
// context.cf properties | ||
'cf' in context && | ||
typeof context.cf === 'object' && | ||
context.cf !== null | ||
); | ||
} | ||
|
||
function hasCfAndCloudflare(context: unknown): context is CloudflareContext { | ||
return hasCfProperty(context) && hasCloudflareProperty(context); | ||
} | ||
|
||
/** | ||
* Type guard to check if an event is a Cloudflare event (nested in _platform or direct) | ||
*/ | ||
export function isEventType(event: unknown): event is CfEventType { | ||
if (event === null || typeof event !== 'object') return false; | ||
|
||
return ( | ||
// basic properties | ||
'protocol' in event && | ||
'host' in event && | ||
typeof event.protocol === 'string' && | ||
typeof event.host === 'string' && | ||
// context property | ||
'context' in event && | ||
typeof event.context === 'object' && | ||
event.context !== null && | ||
// context.cf properties | ||
(hasCfAndCloudflare(event.context) || ('_platform' in event.context && hasCfAndCloudflare(event.context._platform))) | ||
); | ||
} | ||
|
||
/** | ||
* Extracts cf properties from a Cloudflare event | ||
*/ | ||
export function getCfProperties(event: CfEventType): CfProperties { | ||
if ('cf' in event.context) { | ||
return event.context.cf; | ||
} | ||
return event.context._platform.cf; | ||
} | ||
|
||
/** | ||
* Extracts cloudflare properties from a Cloudflare event | ||
*/ | ||
export function getCloudflareProperties(event: CfEventType): MinimalCloudflareProps { | ||
if ('cloudflare' in event.context) { | ||
return event.context.cloudflare; | ||
} | ||
return event.context._platform.cloudflare; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Cloudflare Plugin Trace Data Inconsistency
The Cloudflare plugin's trace data storage and retrieval use different priority orders for identifying the
cf
object as a WeakMap key. This inconsistency can cause trace data lookups to fail, resulting in missing Sentry tracing meta-tags in the HTML. Also, accessing_platform
on anH3Event
type may lead to TypeScript compilation errors.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is no problem when the same nitro version is used (which is the case on the same server)