-
Notifications
You must be signed in to change notification settings - Fork 25
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
feat: detect the underlying runtime #66
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9dbf1e0
feat: detect the underlying runtime
QuiiBz 1edd5e8
fix: export `runtimeName` & avoid duplicating
QuiiBz 2a8b911
refactor: update detection logic
QuiiBz 6955788
fix: remove Bun from global
QuiiBz 05ad871
fix: failing test
QuiiBz f1665b6
refactor: move detection logic to helper flags
QuiiBz 2e081b6
refactor: simplify
pi0 3428b6e
remove unused import
pi0 5d689a9
keep detectRuntime private for now
pi0 9fbe378
update docs
pi0 1a4c318
small fix
pi0 851f3aa
update fixture
pi0 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 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 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 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,57 @@ | ||
/* eslint-disable no-var */ | ||
declare global { | ||
var EdgeRuntime: any; | ||
var Netlify: any; | ||
var Deno: any; | ||
var __lagon__: any; | ||
var fastly: any; | ||
} | ||
|
||
// https://runtime-keys.proposal.wintercg.org/ | ||
export type RuntimeName = | ||
| "workerd" | ||
| "deno" | ||
| "lagon" | ||
| "netlify" | ||
| "node" | ||
| "bun" | ||
| "edge-light" | ||
| "fastly" | ||
| ""; | ||
|
||
export type RuntimeInfo = { name: RuntimeName }; | ||
|
||
export const isNetlify = !!globalThis.Netlify; | ||
export const isEdgeLight = !!globalThis.EdgeRuntime; | ||
// https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent | ||
export const isWorkerd = | ||
globalThis.navigator?.userAgent === "Cloudflare-Workers"; | ||
export const isDeno = !!globalThis.Deno; | ||
// https://nodejs.org/api/process.html#processrelease | ||
export const isLagon = !!globalThis.__lagon__; | ||
export const isNode = globalThis.process?.release?.name === "node"; | ||
export const isBun = globalThis.process?.release?.name === "bun"; | ||
export const isFastly = !!globalThis.fastly; | ||
|
||
const runtimeChecks: [boolean, RuntimeName][] = [ | ||
[isNetlify, "netlify"], | ||
[isEdgeLight, "edge-light"], | ||
[isWorkerd, "workerd"], | ||
[isDeno, "deno"], | ||
[isLagon, "lagon"], | ||
[isNode, "node"], | ||
[isBun, "bun"], | ||
[isFastly, "fastly"], | ||
]; | ||
|
||
function _detectRuntime(): RuntimeInfo | undefined { | ||
const detectedRuntime = runtimeChecks.find((check) => check[0]); | ||
if (detectedRuntime) { | ||
const name = detectedRuntime[1]; | ||
return { name }; | ||
} | ||
} | ||
|
||
export const runtimeInfo = _detectRuntime(); | ||
|
||
export const runtime = runtimeInfo?.name || ""; |
This file contains 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
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.
i could be mistaken but i believe we updated this property to be "node" due to a compatibility fix of an old package.
use !!globalThis.bun or !!process.versions.bun