-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eea6984
commit 9bd16d2
Showing
6 changed files
with
2,254 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import fs from 'fs/promises'; | ||
|
||
export default class ValueCache | ||
{ | ||
constructor(key) { | ||
this._key = key; | ||
} | ||
|
||
get path() { | ||
return `storage/${this._key}.json`; | ||
} | ||
|
||
async _getRawItem() { | ||
try { | ||
let data = await fs.readFile(this.path); | ||
|
||
return JSON.parse(data); | ||
} catch (e) { | ||
return null; | ||
} | ||
} | ||
|
||
async get() { | ||
let item = await this._getRawItem(); | ||
|
||
// If the cached value is already expired, return null | ||
if (item && item.expires && item.expires < new Date) { | ||
return null; | ||
} | ||
|
||
return item; | ||
} | ||
|
||
async getData() { | ||
let item = await this.get(); | ||
|
||
return item && item.data; | ||
} | ||
|
||
async setData(data, expires = null) { | ||
let serialized = JSON.stringify({ expires, data }, undefined, 2); | ||
|
||
await fs.writeFile(this.path, serialized); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import CoralApi from 'nxapi/coral'; | ||
import { addUserAgent } from "nxapi"; | ||
import ValueCache from '../common/ValueCache.mjs'; | ||
|
||
let _nxapiInitialized = false; | ||
|
||
function initializeNxapi() { | ||
if (!_nxapiInitialized) { | ||
addUserAgent(process.env.USER_AGENT); | ||
} | ||
|
||
_nxapiInitialized = true; | ||
} | ||
|
||
export default class NsoClient | ||
{ | ||
constructor(nintendoToken = null) { | ||
initializeNxapi(); | ||
|
||
this.nintendoToken = nintendoToken || process.env.NINTENDO_TOKEN; | ||
this.coralCache = new ValueCache('coral'); | ||
} | ||
|
||
_calculateCacheExpiry(expiresIn) { | ||
let expires = Date.now() + expiresIn * 1000; | ||
|
||
// Expire 5min early to make sure we have time to execute requests | ||
return expires - 5 * 60 * 1000; | ||
} | ||
|
||
async getCoralApi() { | ||
let data = await this.coralCache.getData(); | ||
|
||
if (!data) { | ||
data = await this._createCoralSession(); | ||
} | ||
|
||
return CoralApi.createWithSavedToken(data); | ||
} | ||
|
||
async _createCoralSession() { | ||
console.debug('Creating Coral session'); | ||
let { data } = await CoralApi.createWithSessionToken(this.nintendoToken); | ||
|
||
let expires = this._calculateCacheExpiry(data.credential.expiresIn); | ||
console.debug(`Caching Coral session until: ${expires}`); | ||
await this.coralCache.setData(data, expires); | ||
|
||
return data; | ||
} | ||
|
||
async getWebServiceToken(id) { | ||
let tokenCache = new ValueCache(`webservicetoken.${id}`); | ||
let token = await tokenCache.getData(); | ||
|
||
if (!token) { | ||
token = await this._createWebServiceToken(id, tokenCache); | ||
} | ||
|
||
return token.accessToken; | ||
} | ||
|
||
async _createWebServiceToken(id, tokenCache) { | ||
let coral = await this.getCoralApi(); | ||
|
||
console.debug(`Creating web service token for ID ${id}`); | ||
let { result } = await coral.getWebServiceToken(id); | ||
|
||
let expires = this._calculateCacheExpiry(result.expiresIn); | ||
console.debug(`Caching web service token for ID ${id} until: ${expires}`); | ||
await tokenCache.setData(result, expires); | ||
|
||
return result; | ||
} | ||
} |
Oops, something went wrong.