@@ -147,7 +147,10 @@ function extractSyncToken(syncTokenParam: string | undefined) {
147147 const tokenMap = JSON . parse ( syncTokenParam ) as { [ component : string ] : string | number } ;
148148 return Object . entries ( tokenMap ) . reduce < { [ component : string ] : Buffer | number } > (
149149 ( acc , [ component , token ] ) => {
150- acc [ component ] = typeof token === 'string' ? Buffer . from ( token , 'base64' ) : token ;
150+ acc [ component ] =
151+ typeof token === 'string' && ! / ^ \d + $ / . exec ( token )
152+ ? Buffer . from ( token , 'base64' )
153+ : Number ( token ) ;
151154 return acc ;
152155 } ,
153156 { } ,
@@ -172,7 +175,7 @@ async function statelyProfile(
172175 } ;
173176 const timerPrefix = response . sync ? 'profileSync' : 'profileStately' ;
174177 const counterPrefix = response . sync ? 'sync' : 'stately' ;
175- const syncTokens : { [ component : string ] : string } = { } ;
178+ const syncTokens : { [ component : string ] : string | number } = { } ;
176179 const addSyncToken = (
177180 name : string ,
178181 token : ListToken | { canSync : boolean ; tokenData : number } ,
@@ -181,14 +184,14 @@ async function statelyProfile(
181184 syncTokens [ name ] =
182185 token . tokenData instanceof Uint8Array
183186 ? Buffer . from ( token . tokenData ) . toString ( 'base64' )
184- : token . tokenData . toString ( ) ;
187+ : token . tokenData ;
185188 }
186189 } ;
187190 const getSyncToken = < T extends number | Buffer > ( name : string ) => {
188191 const tokenData = incomingSyncTokens ?. [ name ] ;
189- if ( incomingSyncTokens && ! tokenData ) {
190- throw new Error ( `Missing sync token: ${ name } ` ) ;
191- }
192+ // if (incomingSyncTokens && !tokenData) {
193+ // throw new Error(`Missing sync token: ${name}`);
194+ // }
192195 return tokenData as T | undefined ;
193196 } ;
194197
@@ -202,29 +205,22 @@ async function statelyProfile(
202205 // Load settings from Stately. If they're there, you're done. Otherwise load from Postgres.
203206 const start = new Date ( ) ;
204207
205- const statelySettings = await querySettings ( bungieMembershipId ) ;
206- if ( ! statelySettings . settings ) {
207- const now = Date . now ( ) ;
208- const pgSettings = await readTransaction ( async ( pgClient ) =>
209- getSettings ( pgClient , bungieMembershipId ) ,
210- ) ;
211- if ( pgSettings ) {
212- const tokenData = getSyncToken < number > ( 's' ) ;
213- if ( tokenData === undefined || pgSettings . lastModifiedAt > tokenData ) {
214- response . settings = { ...defaultSettings , ...pgSettings . settings } ;
215- }
216- } else {
217- response . settings = defaultSettings ;
208+ const now = Date . now ( ) ;
209+ // TODO: Should add the token to the query to avoid fetching if unchanged
210+ const pgSettings = await readTransaction ( async ( pgClient ) =>
211+ getSettings ( pgClient , bungieMembershipId ) ,
212+ ) ;
213+ if ( pgSettings ) {
214+ const tokenData = getSyncToken < number > ( 's' ) ;
215+ if ( tokenData === undefined || pgSettings . lastModifiedAt > tokenData ) {
216+ response . settings = { ...defaultSettings , ...pgSettings . settings } ;
218217 }
219218 addSyncToken ( 's' , { canSync : true , tokenData : pgSettings ?. lastModifiedAt ?? now } ) ;
220219 } else {
221220 const tokenData = getSyncToken < Buffer > ( 'settings' ) ;
222221 const { settings : storedSettings , token : settingsToken } = tokenData
223222 ? await syncSettings ( tokenData )
224- : {
225- settings : statelySettings . settings ?? defaultSettings ,
226- token : statelySettings . token ,
227- } ;
223+ : await querySettings ( bungieMembershipId ) ;
228224 response . settings = storedSettings ;
229225 addSyncToken ( 'settings' , settingsToken ) ;
230226 }
@@ -335,6 +331,6 @@ async function statelyProfile(
335331 return response ;
336332}
337333
338- function serializeSyncToken ( syncTokens : { [ component : string ] : string } ) {
334+ function serializeSyncToken ( syncTokens : { [ component : string ] : string | number } ) {
339335 return JSON . stringify ( syncTokens ) ;
340336}
0 commit comments