Skip to content
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: include worker scopes when checking for browser runtime #21

Merged
merged 1 commit into from
Oct 12, 2023
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
9 changes: 5 additions & 4 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { asAsyncIterable, asyncIteratorToBuffer } from './utils/itr.js'
import { randomUUID } from './utils/uuid.js'
import { memoryStorage } from './storage/index.js'
import { getJWT } from './utils/jwt.js'
import { isBrowserContext } from './utils/runtime.js'

export class Saturn {
/**
Expand Down Expand Up @@ -34,8 +35,8 @@ export class Saturn {
this.logs = []
this.storage = this.opts.storage || memoryStorage()
this.reportingLogs = process?.env?.NODE_ENV !== 'development'
this.hasPerformanceAPI = typeof window !== 'undefined' && window?.performance
this.isBrowser = typeof window !== 'undefined'
this.hasPerformanceAPI = isBrowserContext && self?.performance

if (this.reportingLogs && this.hasPerformanceAPI) {
this._monitorPerformanceBuffer()
}
Expand Down Expand Up @@ -69,7 +70,7 @@ export class Saturn {
controller.abort()
}, options.connectTimeout)

if (!this.isBrowser) {
if (!isBrowserContext) {
options.headers = {
...(options.headers || {}),
Authorization: 'Bearer ' + options.jwt
Expand Down Expand Up @@ -173,7 +174,7 @@ export class Saturn {
url.searchParams.set('dag-scope', 'entity')
}

if (this.isBrowser) {
if (isBrowserContext) {
url.searchParams.set('jwt', opts.jwt)
}

Expand Down
2 changes: 1 addition & 1 deletion src/storage/indexed-db-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const DEFAULT_SATURN_STORAGE_NAME = 'saturn-client'
* @returns {import('./index.js').Storage}
*/
export function indexedDbStorage () {
const indexedDbExists = (typeof window !== 'undefined') && window?.indexedDB
const indexedDbExists = (typeof self !== 'undefined') && self?.indexedDB
let dbPromise
if (indexedDbExists) {
dbPromise = openDB(DEFAULT_IDB_STORAGE_NAME, DEFAULT_IDB_VERSION, {
Expand Down
15 changes: 15 additions & 0 deletions src/utils/runtime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const isBrowser =
typeof window !== 'undefined' && typeof window.document !== 'undefined'

export const isServiceWorker = typeof ServiceWorkerGlobalScope !== 'undefined'

export const isWebWorker = typeof DedicatedWorkerGlobalScope !== 'undefined'

export const isSharedWorker = typeof SharedWorkerGlobalScope !== 'undefined'

export const isBrowserContext = isBrowser || isServiceWorker || isWebWorker || isSharedWorker

export const isNode =
typeof process !== 'undefined' &&
process.versions != null &&
process.versions.node != null
Loading