Skip to content

Commit 5f0e7a3

Browse files
authored
feat: include worker scopes when checking for browser runtime (#21)
1 parent de91e82 commit 5f0e7a3

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

src/client.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { asAsyncIterable, asyncIteratorToBuffer } from './utils/itr.js'
55
import { randomUUID } from './utils/uuid.js'
66
import { memoryStorage } from './storage/index.js'
77
import { getJWT } from './utils/jwt.js'
8+
import { isBrowserContext } from './utils/runtime.js'
89

910
export class Saturn {
1011
/**
@@ -34,8 +35,8 @@ export class Saturn {
3435
this.logs = []
3536
this.storage = this.opts.storage || memoryStorage()
3637
this.reportingLogs = process?.env?.NODE_ENV !== 'development'
37-
this.hasPerformanceAPI = typeof window !== 'undefined' && window?.performance
38-
this.isBrowser = typeof window !== 'undefined'
38+
this.hasPerformanceAPI = isBrowserContext && self?.performance
39+
3940
if (this.reportingLogs && this.hasPerformanceAPI) {
4041
this._monitorPerformanceBuffer()
4142
}
@@ -69,7 +70,7 @@ export class Saturn {
6970
controller.abort()
7071
}, options.connectTimeout)
7172

72-
if (!this.isBrowser) {
73+
if (!isBrowserContext) {
7374
options.headers = {
7475
...(options.headers || {}),
7576
Authorization: 'Bearer ' + options.jwt
@@ -173,7 +174,7 @@ export class Saturn {
173174
url.searchParams.set('dag-scope', 'entity')
174175
}
175176

176-
if (this.isBrowser) {
177+
if (isBrowserContext) {
177178
url.searchParams.set('jwt', opts.jwt)
178179
}
179180

src/storage/indexed-db-storage.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const DEFAULT_SATURN_STORAGE_NAME = 'saturn-client'
1111
* @returns {import('./index.js').Storage}
1212
*/
1313
export function indexedDbStorage () {
14-
const indexedDbExists = (typeof window !== 'undefined') && window?.indexedDB
14+
const indexedDbExists = (typeof self !== 'undefined') && self?.indexedDB
1515
let dbPromise
1616
if (indexedDbExists) {
1717
dbPromise = openDB(DEFAULT_IDB_STORAGE_NAME, DEFAULT_IDB_VERSION, {

src/utils/runtime.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export const isBrowser =
2+
typeof window !== 'undefined' && typeof window.document !== 'undefined'
3+
4+
export const isServiceWorker = typeof ServiceWorkerGlobalScope !== 'undefined'
5+
6+
export const isWebWorker = typeof DedicatedWorkerGlobalScope !== 'undefined'
7+
8+
export const isSharedWorker = typeof SharedWorkerGlobalScope !== 'undefined'
9+
10+
export const isBrowserContext = isBrowser || isServiceWorker || isWebWorker || isSharedWorker
11+
12+
export const isNode =
13+
typeof process !== 'undefined' &&
14+
process.versions != null &&
15+
process.versions.node != null

0 commit comments

Comments
 (0)