Skip to content

Commit

Permalink
v0.9.0
Browse files Browse the repository at this point in the history
  • Loading branch information
rjrodger committed Feb 6, 2024
1 parent 58dff56 commit e861b6e
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 47 deletions.
7 changes: 6 additions & 1 deletion dist/bud-provider.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type FullBudProviderOptions = {
};
declare function BudProvider(this: any, options: FullBudProviderOptions): {
exports: {
getTokens: () => Promise<true | {
requestTokens: () => Promise<true | {
when: number;
prev: {
refreshToken: any;
Expand All @@ -38,6 +38,11 @@ declare function BudProvider(this: any, options: FullBudProviderOptions): {
config: string;
};
}>;
loadTokens: () => Promise<{
when: number;
refreshToken: any;
accessToken: any;
}>;
getGateway: (spec: {
redirect_url: string;
clientid: string;
Expand Down
50 changes: 30 additions & 20 deletions dist/bud-provider.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/bud-provider.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@seneca/bud-provider",
"version": "0.8.0",
"version": "0.9.0",
"main": "dist/bud-provider.js",
"type": "commonjs",
"types": "dist/bud-provider.d.ts",
Expand Down
53 changes: 33 additions & 20 deletions src/bud-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ function BudProvider(this: any, options: FullBudProviderOptions) {
const tid = args[1].headers['X-SenecaBudProvider-TraceID'] =
(args[1].headers['X-SenecaBudProvider-TraceID'] || (traceid || seneca.util.Nid()))
options.print.request &&
console.log('BUDREQ', method, seneca.id, tid, phase, attempt, tokenState,
console.log('SP-BUDREQ', method, seneca.id, tid, phase, attempt, tokenState,
refreshToken && refreshToken.substring(0, 8),
accessToken && accessToken.substring(0, 8),
JSI(statsCounters),
Expand Down Expand Up @@ -202,7 +202,7 @@ function BudProvider(this: any, options: FullBudProviderOptions) {
}

function logstats(mark: string) {
console.log('BUDSTATS', mark, JSON.stringify(stats()).replace(/"/g, ''))
console.log('SP-BUDSTATS', mark, JSI(stats()))
}


Expand Down Expand Up @@ -574,7 +574,19 @@ function BudProvider(this: any, options: FullBudProviderOptions) {
}


async function getTokens() {

async function loadTokens() {
refreshToken = await options.store.loadToken('refresh')
accessToken = await options.store.loadToken('access')
return {
when: Date.now(),
refreshToken,
accessToken,
}
}


async function requestTokens() {
const prev = {
refreshToken,
accessToken,
Expand All @@ -591,12 +603,12 @@ function BudProvider(this: any, options: FullBudProviderOptions) {
}

statsCounters.refresh++
options.debug && console.log('BUD-GT-REFRESH', tokenState)
options.debug && console.log('SP-BUD-GT-REFRESH', tokenState)

let refreshResult =
await origFetcher(options.url + 'v1/oauth/token', refreshConfig)

options.debug && console.log('BUD-GT-REFRESH-RESULT', refreshResult.status)
options.debug && console.log('SP-BUD-GT-REFRESH-RESULT', refreshResult.status)

if (200 !== refreshResult.status) {
throw new Error('bud-provider: refresh-token: status:' + refreshResult.status)
Expand All @@ -617,7 +629,7 @@ function BudProvider(this: any, options: FullBudProviderOptions) {

isStart = false

options.debug && console.log('BUD-GT-REFRESH-DONE', tokenState,
options.debug && console.log('SP-BUD-GT-REFRESH-DONE', tokenState,
(refreshToken || '').substring(0, 22))

let accessConfig = {
Expand All @@ -631,12 +643,12 @@ function BudProvider(this: any, options: FullBudProviderOptions) {
}

statsCounters.access++
options.debug && console.log('BUD-GT-ACCESS', tokenState)
options.debug && console.log('SP-BUD-GT-ACCESS', tokenState)

let accessResult =
await origFetcher(options.url + 'v1/oauth/token', accessConfig)

options.debug && console.log('BUD-GT-ACCESS-RESULT', accessResult.status)
options.debug && console.log('SP-BUD-GT-ACCESS-RESULT', accessResult.status)

if (401 === accessResult.status) {
refreshToken = null
Expand All @@ -659,7 +671,7 @@ function BudProvider(this: any, options: FullBudProviderOptions) {

tokenState = 'active'

options.debug && console.log('BUD-GT-ACCESS-DONE', tokenState,
options.debug && console.log('SP-BUD-GT-ACCESS-DONE', tokenState,
(refreshToken || '').substring(0, 22),
(accessToken || '').substring(0, 22),
)
Expand Down Expand Up @@ -689,7 +701,7 @@ function BudProvider(this: any, options: FullBudProviderOptions) {
[fetchspec?.resource, fetchspec?.options])

options.debug &&
console.log('BUDRETRY', traceid, attempt, response?.status, tokenState, error?.message)
console.log('SP-BUDRETRY', traceid, attempt, response?.status, tokenState, error?.message)
options.debug && logstats('retryOn ' + traceid)

if (error) {
Expand All @@ -701,31 +713,31 @@ function BudProvider(this: any, options: FullBudProviderOptions) {
}

if (5 <= attempt) {
options.debug && console.log('BUDRETRY-BAIL', traceid, attempt, response.status, tokenState)
options.debug && console.log('SP-BUDRETRY-BAIL', traceid, attempt, response.status, tokenState)
return false
}

if (500 <= response.status && attempt <= 3) {
options.debug && console.log('BUDRETRY-500', traceid, attempt, response.status, tokenState)
options.debug && console.log('SP-BUDRETRY-500', traceid, attempt, response.status, tokenState)
return true
}

if (401 === response.status) {
options.debug && console.log('BUDRETRY-401', traceid, attempt, response.status, tokenState)
options.debug && console.log('SP-BUDRETRY-401', traceid, attempt, response.status, tokenState)

// Try to refresh the access token first.
if ('active' === tokenState) {
tokenState = 'refresh'
}

try {
options.debug && console.log('BUDRETRY-TOKEN-STATE-TOP', traceid, attempt, tokenState)
options.debug && console.log('SP-BUDRETRY-TOKEN-STATE-TOP', traceid, attempt, tokenState)

if ('active' !== (tokenState as any) && 'refresh' !== tokenState) {
tokenState = 'request'

let lastRefreshToken = await options.store.loadToken('refresh')
options.debug && console.log('BUDRETRY-LAST-REFRESH', traceid, attempt, lastRefreshToken, refreshToken)
options.debug && console.log('SP-BUDRETRY-LAST-REFRESH', traceid, attempt, lastRefreshToken, refreshToken)

if (
// Very first time, try to load the current refreshtoken
Expand All @@ -738,7 +750,7 @@ function BudProvider(this: any, options: FullBudProviderOptions) {
)) {
refreshToken = lastRefreshToken
statsCounters.loadrefresh++
options.debug && console.log('BUDRETRY-USING-LAST-REFRESH', traceid, attempt, tokenState)
options.debug && console.log('SP-BUDRETRY-USING-LAST-REFRESH', traceid, attempt, tokenState)
}
else {

Expand All @@ -752,12 +764,12 @@ function BudProvider(this: any, options: FullBudProviderOptions) {
}

statsCounters.refresh++
options.debug && console.log('BUDRETRY-REFRESH', traceid, attempt, response.status, tokenState)
options.debug && console.log('SP-BUDRETRY-REFRESH', traceid, attempt, response.status, tokenState)

let refreshResult =
await origFetcher(options.url + 'v1/oauth/token', refreshConfig)

options.debug && console.log('BUDRETRY-REFRESH-RESULT', traceid, refreshResult.status)
options.debug && console.log('SP-BUDRETRY-REFRESH-RESULT', traceid, refreshResult.status)

if (200 !== refreshResult.status) {
throw new Error('bud-provider: refresh-token: status:' + refreshResult.status)
Expand Down Expand Up @@ -915,7 +927,8 @@ function BudProvider(this: any, options: FullBudProviderOptions) {

return {
exports: {
getTokens,
requestTokens,
loadTokens,
getGateway,
sdk: () => null,
stats: () => statsCounters,
Expand All @@ -942,7 +955,7 @@ function BudProvider(this: any, options: FullBudProviderOptions) {

function JSI(o: any) {
try {
return (JSON.stringify(o) || '').replace(/"/g, '')
return (JSON.stringify(o) || '').replace(/["\n]/g, '')
}
catch (e: any) {
return o + ' JSI:' + e.message
Expand Down
11 changes: 7 additions & 4 deletions test/bud-provider.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ describe('bud-provider', () => {
})


test('getTokens', async () => {
test('requestTokens', async () => {
if (null == Config) return;

const seneca = await makeSeneca({debug:true, print:{request:true}})
const getTokens = seneca.export('BudProvider/getTokens')
const requestTokens = seneca.export('BudProvider/requestTokens')

const tokens = await getTokens()
const tokens = await requestTokens()
console.dir(tokens,{depth:null})

expect(tokens.when).toBeDefined()
Expand All @@ -56,7 +56,7 @@ describe('bud-provider', () => {

const seneca = await makeSeneca({debug:true, print:{request:true}})

seneca.entity("provider/bud/obp").list$()
// seneca.entity("provider/bud/obp").list$()
const list = await seneca.entity("provider/bud/obp").list$()

// console.log(list)
Expand Down Expand Up @@ -233,6 +233,9 @@ describe('bud-provider', () => {
})

console.log('PERSIST STATS', seneca.export('BudProvider/stats')())

const loadTokens = seneca.export('BudProvider/loadTokens')
console.log('loadTokens', await loadTokens())

}, 22222)

Expand Down

0 comments on commit e861b6e

Please sign in to comment.