Skip to content

Commit

Permalink
refactor(scheduler-utils): share defaults. Update JSDoc for new confi…
Browse files Browse the repository at this point in the history
…guration aa1ad02
  • Loading branch information
TillaTheHun0 committed Aug 6, 2024
1 parent 5281ac3 commit 945809e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
4 changes: 2 additions & 2 deletions scheduler-utils/src/index.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ export * from './index.common.js'
const GRAPHQL_URL = globalThis.GRAPHQL_URL || undefined
const CACHE_SIZE = globalThis.SCHEDULER_UTILS_CACHE_SIZE || undefined
const FOLLOW_REDIRECTS = globalThis.SCHEDULER_UTILS_FOLLOW_REDIRECTS === 'true' || undefined
const GRAPHQL_MAX_RETRIES = globalThis.GRAPHQL_MAX_RETRIES || 0
const GRAPHQL_RETRY_BACKOFF = globalThis.GRAPHQL_RETRY_BACKOFF || 300
const GRAPHQL_MAX_RETRIES = globalThis.GRAPHQL_MAX_RETRIES || undefined
const GRAPHQL_RETRY_BACKOFF = globalThis.GRAPHQL_RETRY_BACKOFF || undefined

const { locate, validate, raw } = connect({ GRAPHQL_URL, cacheSize: CACHE_SIZE, followRedirects: FOLLOW_REDIRECTS, GRAPHQL_MAX_RETRIES, GRAPHQL_RETRY_BACKOFF })

Expand Down
19 changes: 15 additions & 4 deletions scheduler-utils/src/index.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,41 @@ import { validateWith } from './validate.js'

export * from './err.js'

const DEFAULT_CACHE_SIZE = 100
const DEFAULT_GRAPHQL_URL = 'https://arweave.net/graphql'
const DEFAULT_GRAPHQL_MAX_RETRIES = 0
const DEFAULT_GRAPHQL_RETRY_BACKOFF = 300
const DEFAULT_FOLLOW_REDIRECTS = false

/**
* @typedef ConnectParams
* @property {number} [cacheSize] - the size of the internal LRU cache
* @property {boolean} [followRedirects] - whether to follow redirects and cache that url instead
* @property {string} [GRAPHQL_URL] - the url of the gateway to be used
* @property {number} [GRAPHQL_MAX_RETRIES] - the number of times to retry querying the gateway, utilizing an exponential backoff
* @property {number} [GRAPHQL_RETRY_BACKOFF] - the initial backoff, in milliseconds
*
* Build the apis using the provided configuration. You can currently specify
*
* - a GRAPHQL_URL. Defaults to https://arweave.net/graphql
* - a cache size for the internal LRU cache. Defaults to 100
* - whether or not to follow redirects when locating a scheduler. Defaults to false
* - a max amount of retries on gateway queries. Defaults to 0
* - retry delay for retries on gateway queries. Defaults to 300 ms
* - a GRAPHQL_URL. Defaults to https://arweave.net/graphql
* - a GRAPHQL_MAX_RETRIES. Defaults to 0
* - a GRAPHQL_RETRY_BACKOFF. Defaults to 300 (moot if GRAPHQL_MAX_RETRIES is set to 0)
*
* If any value is not provided, a default will be used.
* Invoking connect() with no parameters or an empty object is functionally equivalent
* to using the top-lvl exports
*
* @param {ConnectParams} [params]
*/
export function connect ({ cacheSize = 100, GRAPHQL_URL = DEFAULT_GRAPHQL_URL, followRedirects = false, GRAPHQL_MAX_RETRIES = DEFAULT_GRAPHQL_MAX_RETRIES, GRAPHQL_RETRY_BACKOFF = DEFAULT_GRAPHQL_RETRY_BACKOFF } = {}) {
export function connect ({
cacheSize = DEFAULT_CACHE_SIZE,
followRedirects = DEFAULT_FOLLOW_REDIRECTS,
GRAPHQL_URL = DEFAULT_GRAPHQL_URL,
GRAPHQL_MAX_RETRIES = DEFAULT_GRAPHQL_MAX_RETRIES,
GRAPHQL_RETRY_BACKOFF = DEFAULT_GRAPHQL_RETRY_BACKOFF
} = {}) {
const _cache = InMemoryClient.createLruCache({ size: cacheSize })

const loadScheduler = GatewayClient.loadSchedulerWith({ fetch, GRAPHQL_URL, GRAPHQL_MAX_RETRIES, GRAPHQL_RETRY_BACKOFF })
Expand Down
4 changes: 2 additions & 2 deletions scheduler-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ export * from './index.common.js'
const GRAPHQL_URL = process.env.GRAPHQL_URL || undefined
const CACHE_SIZE = process.env.SCHEDULER_UTILS_CACHE_SIZE || undefined
const FOLLOW_REDIRECTS = process.env.SCHEDULER_UTILS_FOLLOW_REDIRECTS === 'true' || undefined
const GRAPHQL_MAX_RETRIES = process.env.GRAPHQL_MAX_RETRIES || 0
const GRAPHQL_RETRY_BACKOFF = process.env.GRAPHQL_RETRY_BACKOFF || 300
const GRAPHQL_MAX_RETRIES = process.env.GRAPHQL_MAX_RETRIES || undefined
const GRAPHQL_RETRY_BACKOFF = process.env.GRAPHQL_RETRY_BACKOFF || undefined

const { locate, validate, raw } = connect({ GRAPHQL_URL, cacheSize: CACHE_SIZE, followRedirects: FOLLOW_REDIRECTS, GRAPHQL_MAX_RETRIES, GRAPHQL_RETRY_BACKOFF })

Expand Down

0 comments on commit 945809e

Please sign in to comment.