Skip to content

Promises instead callbacks. Now server not crashed if send bad token.#3

Closed
OsirisAnubiz wants to merge 2 commits into
HowProgrammingWorks:masterfrom
OsirisAnubiz:master
Closed

Promises instead callbacks. Now server not crashed if send bad token.#3
OsirisAnubiz wants to merge 2 commits into
HowProgrammingWorks:masterfrom
OsirisAnubiz:master

Conversation

@OsirisAnubiz
Copy link
Copy Markdown

@OsirisAnubiz OsirisAnubiz commented Mar 22, 2023

  1. All callback into storage.js to promise (async/await).
    1.1. Now function safePath takes a promise as the first argument and return promise, instead callback contract.
    const safePath = (fn) => (token, ...args) => {
    const safePath = (promise) => (token, ...args) => {
    1.2. All methods in Storage async. This method await safePath promises.
    async get(key) {
    data = await readSession(key);
    async save(key) {
    await writeSession(key, data);
    async delete(key) {
    await deleteSession(key);
  2. Storage return undefined if not found file session.
try {
  data = await readSession(key);
} catch (err) {
  return undefined;
}

This makes Storage contract look more like Map contract(if not found return undefined).
3. Session.restore method

static restore(client) {
  const { cookie } = client;
  if (!cookie) return;
  const sessionToken = cookie.token;
  if (sessionToken) {
    return new Promise((resolve, reject) => {
      storage.get(sessionToken, (err, session) => {
        if (err) reject(new Error('No session'));
        Object.setPrototypeOf(session, Session.prototype);
        client.token = sessionToken;
        client.session = session;
        resolve(session);
      });
    });
  }
}
static async restore(client) {
  const { cookie } = client;
  if (!cookie) return;
  const sessionToken = cookie.token;
  if (!sessionToken) return;
  const session = await storage.get(sessionToken);
  if (!session) return;
  Object.setPrototypeOf(session, Session.prototype);
  client.token = sessionToken;
  client.session = session;
  return session;
}

Now if you pass a bad token, the server will not crash.

Copy link
Copy Markdown
Member

@tshemsedinov tshemsedinov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make separate example in new folder instead changing callback-style code. For example move JavaScript to JavaScript/Callbacks and put this example to JavaScript/AsyncAwait

Comment thread JavaScript/storage.js
callback(new Error('Invalid session token'));
return;
}
const safePath = (promise) => (token, ...args) => {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

promise is not an instance of Promise, it's an AsyncFunction so naming is confusing

Comment thread JavaScript/storage.js
let data;
try {
data = await readSession(key);
} catch (err) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

err defined but not used

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to open separate PRs from different branches

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants