Skip to content
Merged
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
14 changes: 13 additions & 1 deletion packages/core/supabase-js/src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,30 @@ import { SupabaseAuthClientOptions } from './types'
import { version } from './version'

let JS_ENV = ''
let JS_RUNTIME_VERSION: string | undefined
// @ts-ignore
if (typeof Deno !== 'undefined') {
JS_ENV = 'deno'
// @ts-ignore
JS_RUNTIME_VERSION = Deno.version?.deno
} else if (typeof document !== 'undefined') {
JS_ENV = 'web'
} else if (typeof navigator !== 'undefined' && navigator.product === 'ReactNative') {
JS_ENV = 'react-native'
} else {
JS_ENV = 'node'
JS_RUNTIME_VERSION =
typeof process !== 'undefined' ? process.version?.replace(/^v/, '') : undefined
}

export const DEFAULT_HEADERS = { 'X-Client-Info': `supabase-js-${JS_ENV}/${version}` }
const _runtimeMeta = [`runtime=${JS_ENV}`]
if (JS_RUNTIME_VERSION) {
_runtimeMeta.push(`runtime-version=${JS_RUNTIME_VERSION}`)
}

export const DEFAULT_HEADERS = {
'X-Client-Info': `supabase-js/${version}; ${_runtimeMeta.join('; ')}`,
}

export const DEFAULT_GLOBAL_OPTIONS = {
headers: DEFAULT_HEADERS,
Expand Down
29 changes: 22 additions & 7 deletions packages/core/supabase-js/test/unit/constants.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('constants', () => {

test('DEFAULT_HEADERS should contain X-Client-Info', () => {
expect(DEFAULT_HEADERS).toHaveProperty('X-Client-Info')
expect(DEFAULT_HEADERS['X-Client-Info']).toMatch(/^supabase-js-.*\/.*$/)
expect(DEFAULT_HEADERS['X-Client-Info']).toMatch(/^supabase-js\/.*; runtime=.*$/)
})

test('DEFAULT_GLOBAL_OPTIONS should contain headers', () => {
Expand Down Expand Up @@ -63,13 +63,25 @@ describe('constants', () => {

describe('JS_ENV detection', () => {
test('should detect Deno environment', () => {
;(global as any).Deno = { version: { deno: '1.37.0' } }

// Re-import to trigger the detection logic
jest.resetModules()
const { DEFAULT_HEADERS: newHeaders } = require('../../src/lib/constants')

expect(newHeaders['X-Client-Info']).toContain('runtime=deno')
expect(newHeaders['X-Client-Info']).toContain('runtime-version=1.37.0')
})

test('should detect Deno environment without version', () => {
;(global as any).Deno = {}

// Re-import to trigger the detection logic
jest.resetModules()
const { DEFAULT_HEADERS: newHeaders } = require('../../src/lib/constants')

expect(newHeaders['X-Client-Info']).toContain('supabase-js-deno')
expect(newHeaders['X-Client-Info']).toContain('runtime=deno')
expect(newHeaders['X-Client-Info']).not.toContain('runtime-version=')
})

test('should detect web environment', () => {
Expand All @@ -79,7 +91,8 @@ describe('constants', () => {
jest.resetModules()
const { DEFAULT_HEADERS: newHeaders } = require('../../src/lib/constants')

expect(newHeaders['X-Client-Info']).toContain('supabase-js-web')
expect(newHeaders['X-Client-Info']).toContain('runtime=web')
expect(newHeaders['X-Client-Info']).not.toContain('runtime-version=')
})

test('should detect React Native environment', () => {
Expand All @@ -89,17 +102,19 @@ describe('constants', () => {
jest.resetModules()
const { DEFAULT_HEADERS: newHeaders } = require('../../src/lib/constants')

expect(newHeaders['X-Client-Info']).toContain('supabase-js-react-native')
expect(newHeaders['X-Client-Info']).toContain('runtime=react-native')
expect(newHeaders['X-Client-Info']).not.toContain('runtime-version=')
})

test('should default to node environment when no specific environment is detected', () => {
// No globals set
test('should default to node environment with process version', () => {
// No globals set - falls through to node branch

// Re-import to trigger the detection logic
jest.resetModules()
const { DEFAULT_HEADERS: newHeaders } = require('../../src/lib/constants')

expect(newHeaders['X-Client-Info']).toContain('supabase-js-node')
expect(newHeaders['X-Client-Info']).toContain('runtime=node')
expect(newHeaders['X-Client-Info']).toMatch(/runtime-version=\d+\.\d+\.\d+/)
})
})
})
Loading