-
Notifications
You must be signed in to change notification settings - Fork 5
Promises instead callbacks. Now server not crashed if send bad token. #3
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,63 +1,51 @@ | ||
| 'use strict'; | ||
|
|
||
| const fs = require('node:fs'); | ||
| const fs = require('node:fs').promises; | ||
| const path = require('node:path'); | ||
| const v8 = require('node:v8'); | ||
|
|
||
| const PATH = `${__dirname}/sessions`; | ||
|
|
||
| const safePath = (fn) => (token, ...args) => { | ||
| const callback = args[args.length - 1]; | ||
| if (typeof token !== 'string') { | ||
| callback(new Error('Invalid session token')); | ||
| return; | ||
| } | ||
| const safePath = (promise) => (token, ...args) => { | ||
| if (typeof token !== 'string') throw new Error('Invalid session token'); | ||
| const fileName = path.join(PATH, token); | ||
| if (!fileName.startsWith(PATH)) { | ||
| callback(new Error('Invalid session token')); | ||
| return; | ||
| } | ||
| fn(fileName, ...args); | ||
| if (!fileName.startsWith(PATH)) throw new Error('Invalid session token'); | ||
| return promise(fileName, ...args) | ||
| }; | ||
|
|
||
| const readSession = safePath(fs.readFile); | ||
| const writeSession = safePath(fs.writeFile); | ||
| const deleteSession = safePath(fs.unlink); | ||
|
|
||
| class Storage extends Map { | ||
| get(key, callback) { | ||
| async get(key) { | ||
| const value = super.get(key); | ||
| if (value) { | ||
| callback(null, value); | ||
| return; | ||
| if (value) return value; | ||
| let data; | ||
| try { | ||
| data = await readSession(key); | ||
| } catch (err) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be better to create 2 pull requests? First for fix crashing server. Second for async/await syntax.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be better to open separate PRs from different branches
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you. I will do as you said. I apologize for the inconvenience. |
||
| return undefined; | ||
| } | ||
| readSession(key, (err, data) => { | ||
| if (err) { | ||
| callback(err); | ||
| return; | ||
| } | ||
| console.log(`Session loaded: ${key}`); | ||
| const session = v8.deserialize(data); | ||
| super.set(key, session); | ||
| callback(null, session); | ||
| }); | ||
| console.log('Session loaded: ${key}'); | ||
| const session = v8.deserialize(data); | ||
| super.set(key, session); | ||
| return session; | ||
| } | ||
|
|
||
| save(key) { | ||
| async save(key) { | ||
| const value = super.get(key); | ||
| if (value) { | ||
| const data = v8.serialize(value); | ||
| writeSession(key, data, () => { | ||
| console.log(`Session saved: ${key}`); | ||
| }); | ||
| await writeSession(key, data); | ||
| console.log(`Session saved: ${key}`); | ||
| } | ||
| } | ||
|
|
||
| delete(key) { | ||
| async delete(key) { | ||
| console.log('Delete: ', key); | ||
| deleteSession(key, () => { | ||
| console.log(`Session deleted: ${key}`); | ||
| }); | ||
| await deleteSession(key); | ||
| console.log(`Session deleted: ${key}`); | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
promiseis not an instance ofPromise, it's anAsyncFunctionso naming is confusing