Skip to content

Commit

Permalink
feat: make clientKey optional
Browse files Browse the repository at this point in the history
  • Loading branch information
guanzo committed Nov 8, 2023
1 parent 9217c16 commit a783a4e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
41 changes: 22 additions & 19 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ export class Saturn {
downloadTimeout: 0
}, config)

if (!this.config.clientKey) {
throw new Error('clientKey is required')
}

this.logs = []
this.nodes = []
this.reportingLogs = process?.env?.NODE_ENV !== 'development'
Expand All @@ -74,17 +70,20 @@ export class Saturn {
if (!opts.originFallback) {
const [cid] = (cidPath ?? '').split('/')
CID.parse(cid)
const jwt = await getJWT(options, this.storage)
options.jwt = jwt
}

if (!isBrowserContext) {
options.headers = {
...(options.headers || {}),
Authorization: 'Bearer ' + options.jwt
if (options.clientKey) {
options.jwt = await getJWT(options, this.storage)
}
}

options.headers = {
...(options.headers || {})
}

if (!isBrowserContext && options.jwt) {
options.headers.Authorization = 'Bearer ' + options.jwt
}

let nodes = options.nodes
if (!nodes || nodes.length === 0) {
const replacementNode = { url: options.cdnURL }
Expand Down Expand Up @@ -165,8 +164,10 @@ export class Saturn {
if (!opts.originFallback) {
const [cid] = (cidPath ?? '').split('/')
CID.parse(cid)
const jwt = await getJWT(this.config, this.storage)
options.jwt = jwt

if (options.clientKey) {
options.jwt = await getJWT(options, this.storage)
}
}

const node = options.nodes && options.nodes[0]
Expand All @@ -184,12 +185,14 @@ export class Saturn {
controller.abort()
}, options.connectTimeout)

if (!isBrowserContext) {
options.headers = {
...(options.headers || {}),
Authorization: 'Bearer ' + options.jwt
}
options.headers = {
...(options.headers || {})
}

if (!isBrowserContext && options.jwt) {
options.headers.Authorization = 'Bearer ' + options.jwt
}

let res
try {
res = await fetch(parseUrl(url), { signal: controller.signal, ...options })
Expand Down Expand Up @@ -417,7 +420,7 @@ export class Saturn {
url.searchParams.set('dag-scope', 'entity')
}

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

Expand Down
1 change: 1 addition & 0 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* @property {boolean} [originFallback] - Is this a fallback to the customer origin
* @property {boolean} [raceNodes] - Does the fetch race multiple nodes on requests.
* @property {string} [customerFallbackURL] - Customer Origin that is a fallback.
* @property {string} [jwt] - JWT for L1 request.
* @property {number} [connectTimeout=5000] - Connection timeout in milliseconds.
* @property {number} [downloadTimeout=0] - Download timeout in milliseconds.
* @property {AbortController} [controller]
Expand Down

0 comments on commit a783a4e

Please sign in to comment.