Skip to content

Commit 5dad9bf

Browse files
authored
tweak node logic (#814)
1 parent 946164a commit 5dad9bf

File tree

3 files changed

+14
-23
lines changed

3 files changed

+14
-23
lines changed

packages/node/src/lib/__tests__/env.test.ts

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { detectRuntime, RuntimeEnv } from '../env'
22

3-
const ogProcess = { ...process.env }
3+
const ogProcessEnv = { ...process.env }
4+
45
afterEach(() => {
5-
process.env = ogProcess
6+
process.env = ogProcessEnv
67
// @ts-ignore
78
delete globalThis.window
89

@@ -16,17 +17,10 @@ afterEach(() => {
1617
delete globalThis.window
1718
})
1819
describe(detectRuntime, () => {
19-
it('should return web worker if correct env', () => {
20-
// @ts-ignore
21-
// eslint-disable-next-line
22-
delete process.env
23-
// @ts-ignore
24-
globalThis.WorkerGlobalScope = {}
25-
// @ts-ignore
26-
globalThis.importScripts = () => {}
27-
28-
expect(detectRuntime()).toEqual<RuntimeEnv>('web-worker')
20+
it('should return node since these tests run in node', () => {
21+
expect(detectRuntime()).toEqual<RuntimeEnv>('node')
2922
})
23+
3024
it('should return browser if correct env', () => {
3125
// @ts-ignore
3226
// eslint-disable-next-line
@@ -35,12 +29,6 @@ describe(detectRuntime, () => {
3529
globalThis.window = {}
3630
expect(detectRuntime()).toEqual<RuntimeEnv>('browser')
3731
})
38-
it('should return node if correct env', () => {
39-
// @ts-ignore
40-
// eslint-disable-next-line
41-
process = { env: {} }
42-
expect(detectRuntime()).toEqual<RuntimeEnv>('node')
43-
})
4432

4533
it('should return cloudflare worker if correct env', () => {
4634
// @ts-ignore

packages/node/src/lib/env.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ export type RuntimeEnv =
77
| 'unknown'
88

99
export const detectRuntime = (): RuntimeEnv => {
10-
if (typeof process === 'object' && process && process.env) {
10+
if (
11+
typeof process === 'object' &&
12+
process &&
13+
typeof process.env === 'object' &&
14+
process.env &&
15+
typeof process.version === 'string'
16+
) {
1117
return 'node'
1218
}
1319

packages/node/src/plugins/segmentio/index.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ function normalizeEvent(ctx: Context) {
1010
ctx.updateEvent('context.library.version', version)
1111
const runtime = detectRuntime()
1212
if (runtime === 'node') {
13-
// extra guard here was important for vercel edge.
14-
if (typeof process.versions === 'object') {
15-
ctx.updateEvent('_metadata.nodeVersion', process.versions.node)
16-
}
13+
ctx.updateEvent('_metadata.nodeVersion', process.version)
1714
}
1815
ctx.updateEvent('_metadata.jsRuntime', runtime)
1916
}

0 commit comments

Comments
 (0)