Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat async fs #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
"start": "node ./bin/server.js"
},
"engines": {
"node": ">= 12.0"
"node": ">= 16.0"
},
"dependencies": {
"cookie-parser": "~1.4.4",
"debug": "~4.1.1",
"express": "~4.17.1",
"http-errors": "~1.8.0",
"cookie-parser": "~1.4.6",
"debug": "~4.3.4",
"express": "~4.18.1",
"http-errors": "~2.0.0",
"morgan": "~1.10.0",
"node-sass-middleware": "~0.11.0",
"openid-client": "~3.15.9",
"pug": "~3.0.0",
"yargs": "^15.4.1"
"node-sass-middleware": "~1.0.1",
"openid-client": "~5.4.0",
"pug": "~3.0.2",
"yargs": "^17.5.1"
}
}
2 changes: 1 addition & 1 deletion routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ let router = express.Router();

/* GET home page. */
router.get('/', function (req, res, next) {
res.render('index', {title: 'Express'});
res.render('index', {title: 'Node.JS Server demo'});
});

module.exports = router;
20 changes: 10 additions & 10 deletions utils/api.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const {Issuer, TokenSet} = require('openid-client');
const debug = require('debug')('public-api-demo:api');
const fs = require('fs');
const fs = require('fs').promises;
const jose = require('jose');

// constants
Expand All @@ -19,12 +19,12 @@ let state = {
};
let client;

function loadState() {
return fs.readFileSync('./configState.json', 'utf8');
async function loadState() {
return await fs.readFile('./configState.json', 'utf8');
}

function saveState() {
fs.writeFileSync('./configState.json', JSON.stringify(state, null, 2), 'utf8');
async function saveState() {
await fs.writeFile('./configState.json', JSON.stringify(state, null, 2), 'utf8');
debug('State saved');
}

Expand All @@ -43,7 +43,7 @@ async function checkToken() {

async function initialize() {
debug('Initializing API');
let data = loadState();
let data = await loadState();
data = JSON.parse(data);
if (data.issuer) {
state.issuer = new Issuer(data.issuer);
Expand All @@ -67,11 +67,11 @@ async function initialize() {
}
client = new state.issuer.Client({
// For personal access token we can use PAT/PAT.
// This is only needed because the library requires a client_id where as the API endpoint does not require it
// This is only needed because the library requires a client_id whereas the API endpoint does not require it
client_id: 'PAT',
client_secret: 'PAT',
});
saveState();
await saveState();
debug('API initialized');
}

Expand All @@ -87,10 +87,10 @@ function tokenDetails() {
let ret = {};

if (state.token.access_token) {
ret.access_token = jose.JWT.decode(state.token.access_token);
ret.access_token = jose.decodeJwt(state.token.access_token);
}
if (state.token.refresh_token) {
ret.refresh_token = jose.JWT.decode(state.token.refresh_token);
ret.refresh_token = jose.decodeJwt(state.token.refresh_token);
}

return ret;
Expand Down
2 changes: 2 additions & 0 deletions views/index.pug
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ extends layout
block content
h1= title
p Welcome to #{title}

a(href='/organizations') Organizations
Loading