Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
AmeanAsad committed Nov 2, 2023
1 parent 786c272 commit b848790
Showing 1 changed file with 19 additions and 20 deletions.
39 changes: 19 additions & 20 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class Saturn {
* @param {import('./storage/index.js').Storage} [opts.storage]
*/
constructor (opts = {}) {
this.opts = Object.assign({}, {
this.config = Object.assign({}, {
clientId: randomUUID(),
cdnURL: 'l1s.saturn.ms',
logURL: 'https://twb3qukm2i654i3tnvx36char40aymqq.lambda-url.us-west-2.on.aws/',
Expand All @@ -46,7 +46,7 @@ export class Saturn {
downloadTimeout: 0
}, opts)

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

Expand All @@ -57,8 +57,8 @@ export class Saturn {
if (this.reportingLogs && this.hasPerformanceAPI) {
this._monitorPerformanceBuffer()
}
this.storage = this.opts.storage || memoryStorage()
this.loadNodesPromise = this.opts.experimental ? this._loadNodes(this.opts) : null
this.storage = this.config.storage || memoryStorage()
this.loadNodesPromise = this.config.experimental ? this._loadNodes(this.config) : null
}

/**
Expand All @@ -68,16 +68,15 @@ export class Saturn {
* @returns {Promise<object>}
*/
async fetchCIDWithRace (cidPath, opts = {}) {
const options = Object.assign({}, this.opts, opts)
const options = Object.assign({}, this.config, { format: 'car ' }, opts)

if (!opts.originFallback) {
const [cid] = (cidPath ?? '').split('/')
CID.parse(cid)
const jwt = await getJWT(this.opts, this.storage)
const jwt = await getJWT(options, this.storage)
options.jwt = jwt
}

options.format = opts.format ? opts.format : 'car'

if (!isBrowserContext) {
options.headers = {
...(options.headers || {}),
Expand All @@ -87,7 +86,7 @@ export class Saturn {

let nodes = options.nodes
if (!nodes || nodes.length === 0) {
const replacementNode = { url: this.opts.cdnURL }
const replacementNode = { url: options.cdnURL }
nodes = [replacementNode]
}
const controllers = []
Expand Down Expand Up @@ -161,17 +160,16 @@ export class Saturn {
* @returns {Promise<object>}
*/
async fetchCID (cidPath, opts = {}) {
const options = Object.assign({}, this.opts, opts)
const options = Object.assign({}, this.config, { format: 'car '} opts)
if (!opts.originFallback) {
const [cid] = (cidPath ?? '').split('/')
CID.parse(cid)
const jwt = await getJWT(this.opts, this.storage)
const jwt = await getJWT(options, this.storage)
options.jwt = jwt
}
options.format = opts.format ? opts.format : 'car'

const node = options.nodes && options.nodes[0]
const origin = node?.url ?? this.opts.cdnURL
const origin = node?.url ?? this.config.cdnURL
const url = this.createRequestURL(cidPath, { ...options, url: origin })

let log = {
Expand Down Expand Up @@ -279,7 +277,7 @@ export class Saturn {
// Use CDN origin if node list is not loaded
if (this.nodes.length === 0) {
// fetch from origin in the case that no nodes are loaded
opts.nodes = Array({ url: this.opts.cdnURL })
opts.nodes = Array({ url: this.config.cdnURL })
try {
yield * fetchContent()
return
Expand All @@ -296,7 +294,7 @@ export class Saturn {
let fallbackCount = 0
const nodes = this.nodes
for (let i = 0; i < nodes.length; i++) {
if (fallbackCount > this.opts.fallbackLimit || skipNodes) {
if (fallbackCount > this.config.fallbackLimit || skipNodes) {
break
}
if (opts.raceNodes) {
Expand All @@ -308,6 +306,7 @@ export class Saturn {
yield * fetchContent()
return
} catch (err) {
console.log(err)
lastError = err
if (err.res?.status === 410 || isErrorUnavoidable(err)) {
break
Expand All @@ -317,7 +316,7 @@ export class Saturn {
}

if (lastError) {
const originUrl = opts.customerFallbackURL ?? this.opts.customerFallbackURL
const originUrl = opts.customerFallbackURL ?? this.config.customerFallbackURL
// Use customer origin if cid is not retrievable by lassie.
if (originUrl) {
opts.nodes = Array({ url: originUrl })
Expand Down Expand Up @@ -394,7 +393,7 @@ export class Saturn {
* @returns {URL}
*/
createRequestURL (cidPath, opts = {}) {
let origin = opts.url ?? this.opts.cdnURL
let origin = opts.url ?? this.config.cdnURL
origin = addHttpPrefix(origin)
if (opts.originFallback) {
return new URL(origin)
Expand Down Expand Up @@ -444,10 +443,10 @@ export class Saturn {
: this.logs

await fetch(
this.opts.logURL,
this.config.logURL,
{
method: 'POST',
body: JSON.stringify({ bandwidthLogs, logSender: this.opts.logSender })
body: JSON.stringify({ bandwidthLogs, logSender: this.config.logSender })
}
)

Expand Down Expand Up @@ -569,7 +568,7 @@ export class Saturn {

const url = new URL(origin)
const controller = new AbortController()
const options = Object.assign({}, { method: 'GET' }, this.opts)
const options = Object.assign({}, { method: 'GET' }, this.config)

const connectTimeout = setTimeout(() => {
controller.abort()
Expand Down

0 comments on commit b848790

Please sign in to comment.