-
-
Notifications
You must be signed in to change notification settings - Fork 54
feat: support promise api #31
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
564b8a7
feat: req.destroySession promise api
lokshunhung d51752e
refactor: move destroy-session function
lokshunhung 43314ac
test: destroy-session error cases
lokshunhung 45ed7e8
feat: sessionStore promise api
lokshunhung 6b1cdab
docs: add promise apis docs
lokshunhung File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,18 @@ | ||
| 'use strict' | ||
|
|
||
| module.exports = function destroySession (done) { | ||
| const request = this | ||
| if (!done) { | ||
| return new Promise((resolve, reject) => { | ||
| request.sessionStore.destroy(request.session.sessionId, (err) => { | ||
| request.session = null | ||
| if (err) reject(err) | ||
| else resolve() | ||
| }) | ||
| }) | ||
| } | ||
| request.sessionStore.destroy(request.session.sessionId, (err) => { | ||
| request.session = null | ||
| done(err) | ||
| }) | ||
| } | ||
This file contains hidden or 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 hidden or 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,50 @@ | ||
| 'use strict' | ||
|
|
||
| module.exports = function createProxyStore (store) { | ||
| const promisifyStore = { | ||
| get (sessionId, callback) { | ||
| if (callback) { | ||
| return store.get(sessionId, callback) | ||
| } | ||
| return new Promise((resolve, reject) => { | ||
| store.get(sessionId, (err, session) => { | ||
| if (err) reject(err) | ||
| else resolve(session) | ||
| }) | ||
| }) | ||
| }, | ||
|
|
||
| set (sessionId, session, callback) { | ||
| if (callback) { | ||
| return store.set(sessionId, session, callback) | ||
| } | ||
| return new Promise((resolve, reject) => { | ||
| store.set(sessionId, session, (err) => { | ||
| if (err) reject(err) | ||
| else resolve() | ||
| }) | ||
| }) | ||
| }, | ||
|
|
||
| destroy (sessionId, callback) { | ||
| if (callback) { | ||
| return store.destroy(sessionId, callback) | ||
| } | ||
| return new Promise((resolve, reject) => { | ||
| store.destroy(sessionId, (err) => { | ||
| if (err) reject(err) | ||
| else resolve() | ||
| }) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| return new Proxy(store, { | ||
| get (target, property) { | ||
| if (property in promisifyStore) { | ||
| return promisifyStore[property] | ||
| } | ||
| return target[property] | ||
| } | ||
| }) | ||
| } |
This file contains hidden or 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,88 @@ | ||
| 'use strict' | ||
|
|
||
| const test = require('ava') | ||
| const { testServer, request, DEFAULT_OPTIONS } = require('./util') | ||
|
|
||
| class ErrorStore { | ||
| get (sessionId, callback) { | ||
| throw new Error('Not implemented') | ||
| } | ||
|
|
||
| set (sessionId, session, callback) { | ||
| throw new Error('Not implemented') | ||
| } | ||
|
|
||
| destroy (sessionId, callback) { | ||
| callback(new Error('ErrorStore#destroy')) | ||
| } | ||
| } | ||
|
|
||
| test('should successfully destroy the session with callback api', async (t) => { | ||
| t.plan(3) | ||
| const port = await testServer((request, reply) => { | ||
| request.destroySession((err) => { | ||
| t.falsy(err) | ||
| t.is(request.session, null) | ||
| reply.send(200) | ||
| }) | ||
| }, DEFAULT_OPTIONS) | ||
|
|
||
| const { response } = await request(`http://localhost:${port}`) | ||
|
|
||
| t.is(response.statusCode, 200) | ||
| }) | ||
|
|
||
| test('should fail to destroy the session with callback api', async (t) => { | ||
| t.plan(3) | ||
| const options = { | ||
| secret: 'cNaoPYAwF60HZJzkcNaoPYAwF60HZJzk', | ||
| store: new ErrorStore() | ||
| } | ||
| const port = await testServer((request, reply) => { | ||
| request.destroySession((err) => { | ||
| t.true(err instanceof Error) | ||
| t.is(err.message, 'ErrorStore#destroy') | ||
| reply.send(200) | ||
| }) | ||
| }, options) | ||
|
|
||
| const { response } = await request(`http://localhost:${port}`) | ||
|
|
||
| t.is(response.statusCode, 200) | ||
| }) | ||
|
|
||
| test('should successfully destroy the session with promise api', async (t) => { | ||
| t.plan(2) | ||
| const port = await testServer(async (request, reply) => { | ||
| try { | ||
| await request.destroySession() | ||
| t.is(request.session, null) | ||
| reply.send(200) | ||
| } catch (err) { | ||
| t.fail(err) | ||
| } | ||
| }, DEFAULT_OPTIONS) | ||
|
|
||
| const { response } = await request(`http://localhost:${port}`) | ||
|
|
||
| t.is(response.statusCode, 200) | ||
| }) | ||
|
|
||
| test('should fail to destroy the session with promise api', async (t) => { | ||
| t.plan(2) | ||
| const options = { | ||
| secret: 'cNaoPYAwF60HZJzkcNaoPYAwF60HZJzk', | ||
| store: new ErrorStore() | ||
| } | ||
| const port = await testServer(async (request, reply) => { | ||
| await t.throwsAsync(async () => await request.destroySession(), { | ||
| instanceOf: Error, | ||
| message: 'ErrorStore#destroy' | ||
| }) | ||
| reply.send(200) | ||
| }, options) | ||
|
|
||
| const { response } = await request(`http://localhost:${port}`) | ||
|
|
||
| t.is(response.statusCode, 200) | ||
| }) |
This file contains hidden or 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,159 @@ | ||
| 'use strict' | ||
|
|
||
| const test = require('ava') | ||
| const MemoryStore = require('../lib/store') | ||
| const createProxyStore = require('../lib/proxy-store') | ||
|
|
||
| test('should work with callbacks for `get`, `set`, `destroy', async (t) => { | ||
| t.plan(6) | ||
| const store = new MemoryStore() | ||
| const proxyStore = createProxyStore(store) | ||
| await new Promise(resolve => { | ||
| proxyStore.set('one', { foo: 1 }, (err) => { | ||
| t.falsy(err) | ||
| proxyStore.get('one', (err, data) => { | ||
| t.falsy(err) | ||
| t.deepEqual(data, { foo: 1 }) | ||
| proxyStore.destroy('one', (err) => { | ||
| t.falsy(err) | ||
| proxyStore.get('one', (err, data) => { | ||
| t.falsy(err) | ||
| t.falsy(data) | ||
| resolve() | ||
| }) | ||
| }) | ||
| }) | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| test('should work with promise api for `get`, `set`, `destroy`', async (t) => { | ||
| t.plan(2) | ||
| const store = new MemoryStore() | ||
| const proxyStore = createProxyStore(store) | ||
| await proxyStore.set('one', { foo: 1 }) | ||
| const data = await proxyStore.get('one') | ||
| t.deepEqual(data, { foo: 1 }) | ||
| await proxyStore.destroy('one') | ||
| const empty = await proxyStore.get('one') | ||
| t.falsy(empty) | ||
| }) | ||
|
|
||
| test('should reject promise if callback argument is not used', async (t) => { | ||
| t.plan(3) | ||
| class ErrorStore { | ||
| get (sessionId, callback) { | ||
| callback(new Error('ErrorStore#get'), null) | ||
| } | ||
|
|
||
| set (sessionId, session, callback) { | ||
| callback(new Error('ErrorStore#set')) | ||
| } | ||
|
|
||
| destroy (sessionId, callback) { | ||
| callback(new Error('ErrorStore#destroy')) | ||
| } | ||
| } | ||
| const store = new ErrorStore() | ||
| const proxyStore = createProxyStore(store) | ||
| try { | ||
| await proxyStore.get('id') | ||
| t.fail('expect promise rejection with no callback argument') | ||
| } catch (err) { | ||
| t.true(err instanceof Error) | ||
| } | ||
| try { | ||
| await proxyStore.set('id', { foo: 1 }) | ||
| t.fail('expect promise rejection with no callback argument') | ||
| } catch (err) { | ||
| t.true(err instanceof Error) | ||
| } | ||
| try { | ||
| await proxyStore.destroy('id') | ||
| t.fail('expect promise rejection with no callback argument') | ||
| } catch (err) { | ||
| t.true(err instanceof Error) | ||
| } | ||
| }) | ||
|
|
||
| test('should not reject promise if callback is passed', async (t) => { | ||
| t.plan(4) | ||
| class ErrorStore { | ||
| get (sessionId, callback) { | ||
| callback(new Error('ErrorStore#get'), null) | ||
| } | ||
|
|
||
| set (sessionId, session, callback) { | ||
| callback(new Error('ErrorStore#set')) | ||
| } | ||
|
|
||
| destroy (sessionId, callback) { | ||
| callback(new Error('ErrorStore#destroy')) | ||
| } | ||
| } | ||
| const store = new ErrorStore() | ||
| const proxyStore = createProxyStore(store) | ||
| try { | ||
| await proxyStore.get('id', (err, data) => { | ||
| t.true(err instanceof Error) | ||
| t.falsy(data) | ||
| }) | ||
| } catch (err) { | ||
| t.fail('unexpected promise rejection with callback argument passed') | ||
| } | ||
| try { | ||
| await proxyStore.set('id', { foo: 1 }, (err) => { | ||
| t.true(err instanceof Error) | ||
| }) | ||
| } catch (err) { | ||
| t.fail('unexpected promise rejection with callback argument passed') | ||
| } | ||
| try { | ||
| await proxyStore.destroy('id', (err) => { | ||
| t.true(err instanceof Error) | ||
| }) | ||
| } catch (err) { | ||
| t.fail('unexpected promise rejection with callback argument passed') | ||
| } | ||
| }) | ||
|
|
||
| test('should not mutate store instance by monkey-patch methods', (t) => { | ||
| t.plan(3) | ||
| class Store { | ||
| constructor () { | ||
| this.originalMethods = { get: this.get, set: this.set, destroy: this.destroy } | ||
| } | ||
|
|
||
| get () {} | ||
| set () {} | ||
| destroy () {} | ||
| } | ||
| const store = new Store() | ||
| const originalMethods = store.originalMethods | ||
| createProxyStore(store) | ||
| t.is(store.get, originalMethods.get) | ||
| t.is(store.set, originalMethods.set) | ||
| t.is(store.destroy, originalMethods.destroy) | ||
| }) | ||
|
|
||
| test('should allow referencing store methods other than `get`, `set`, `destroy`', (t) => { | ||
| t.plan(4) | ||
| class Store { | ||
| get () {} | ||
| set () {} | ||
| destroy () {} | ||
| foo () { | ||
| return 1 | ||
| } | ||
|
|
||
| bar () { | ||
| return this.foo() + 1 | ||
| } | ||
| } | ||
| const store = new Store() | ||
| const proxyStore = createProxyStore(store) | ||
| t.is(proxyStore.foo(), 1) | ||
| t.is(proxyStore.bar(), 2) | ||
| t.is(proxyStore.foo, store.foo) | ||
| t.is(proxyStore.bar, store.bar) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.