-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[kbnServer/extensions] formalize request factories with helper #12697
Merged
spalger
merged 4 commits into
elastic:master
from
spalger:server/formalize-request-factories
Jul 11, 2017
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0739098
[kbnServer/extensions] add server.addMemoizedFactoryToRequest() helper
spalger 6c71806
[server/uiSettings] use server.addMemoizedFactoryToRequest() helper
spalger f8d63c4
Merge branch 'master' of github.com:elastic/kibana into server/formal…
spalger 109a0c8
[kbnServer/extensions] name `this` for clarity
spalger 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 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
101 changes: 101 additions & 0 deletions
101
src/server/server_extensions/__tests__/add_memoized_factory_to_request.js
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,101 @@ | ||
import sinon from 'sinon'; | ||
import expect from 'expect.js'; | ||
|
||
import { serverExtensionsMixin } from '../server_extensions_mixin'; | ||
|
||
describe('server.addMemoizedFactoryToRequest()', () => { | ||
const setup = () => { | ||
class Request {} | ||
|
||
class Server { | ||
constructor() { | ||
sinon.spy(this, 'decorate'); | ||
} | ||
decorate(type, name, value) { | ||
switch (type) { | ||
case 'request': return Request.prototype[name] = value; | ||
case 'server': return Server.prototype[name] = value; | ||
default: throw new Error(`Unexpected decorate type ${type}`); | ||
} | ||
} | ||
} | ||
|
||
const server = new Server(); | ||
serverExtensionsMixin({}, server); | ||
return { server, Request }; | ||
}; | ||
|
||
it('throws when propertyName is not a string', () => { | ||
const { server } = setup(); | ||
expect(() => server.addMemoizedFactoryToRequest()).to.throwError('propertyName must be a string'); | ||
expect(() => server.addMemoizedFactoryToRequest(null)).to.throwError('propertyName must be a string'); | ||
expect(() => server.addMemoizedFactoryToRequest(1)).to.throwError('propertyName must be a string'); | ||
expect(() => server.addMemoizedFactoryToRequest(true)).to.throwError('propertyName must be a string'); | ||
expect(() => server.addMemoizedFactoryToRequest(/abc/)).to.throwError('propertyName must be a string'); | ||
expect(() => server.addMemoizedFactoryToRequest(['foo'])).to.throwError('propertyName must be a string'); | ||
expect(() => server.addMemoizedFactoryToRequest([1])).to.throwError('propertyName must be a string'); | ||
expect(() => server.addMemoizedFactoryToRequest({})).to.throwError('propertyName must be a string'); | ||
}); | ||
|
||
it('throws when factory is not a function', () => { | ||
const { server } = setup(); | ||
expect(() => server.addMemoizedFactoryToRequest('name')).to.throwError('factory must be a function'); | ||
expect(() => server.addMemoizedFactoryToRequest('name', null)).to.throwError('factory must be a function'); | ||
expect(() => server.addMemoizedFactoryToRequest('name', 1)).to.throwError('factory must be a function'); | ||
expect(() => server.addMemoizedFactoryToRequest('name', true)).to.throwError('factory must be a function'); | ||
expect(() => server.addMemoizedFactoryToRequest('name', /abc/)).to.throwError('factory must be a function'); | ||
expect(() => server.addMemoizedFactoryToRequest('name', ['foo'])).to.throwError('factory must be a function'); | ||
expect(() => server.addMemoizedFactoryToRequest('name', [1])).to.throwError('factory must be a function'); | ||
expect(() => server.addMemoizedFactoryToRequest('name', {})).to.throwError('factory must be a function'); | ||
}); | ||
|
||
it('throws when factory takes more than one arg', () => { | ||
const { server } = setup(); | ||
/* eslint-disable no-unused-vars */ | ||
expect(() => server.addMemoizedFactoryToRequest('name', () => {})).to.not.throwError('more than one argument'); | ||
expect(() => server.addMemoizedFactoryToRequest('name', (a) => {})).to.not.throwError('more than one argument'); | ||
|
||
expect(() => server.addMemoizedFactoryToRequest('name', (a, b) => {})).to.throwError('more than one argument'); | ||
expect(() => server.addMemoizedFactoryToRequest('name', (a, b, c) => {})).to.throwError('more than one argument'); | ||
expect(() => server.addMemoizedFactoryToRequest('name', (a, b, c, d) => {})).to.throwError('more than one argument'); | ||
expect(() => server.addMemoizedFactoryToRequest('name', (a, b, c, d, e) => {})).to.throwError('more than one argument'); | ||
/* eslint-enable no-unused-vars */ | ||
}); | ||
|
||
it('decorates request objects with a function at `propertyName`', () => { | ||
const { server, Request } = setup(); | ||
|
||
expect(new Request()).to.not.have.property('decorated'); | ||
server.addMemoizedFactoryToRequest('decorated', () => {}); | ||
expect(new Request()).to.have.property('decorated').a('function'); | ||
}); | ||
|
||
it('caches invocations of the factory to the request instance', () => { | ||
const { server, Request } = setup(); | ||
const factory = sinon.stub().returnsArg(0); | ||
server.addMemoizedFactoryToRequest('foo', factory); | ||
|
||
const request1 = new Request(); | ||
const request2 = new Request(); | ||
|
||
// call `foo()` on both requests a bunch of times, each time | ||
// the return value should be exactly the same | ||
expect(request1.foo()).to.be(request1); | ||
expect(request1.foo()).to.be(request1); | ||
expect(request1.foo()).to.be(request1); | ||
expect(request1.foo()).to.be(request1); | ||
expect(request1.foo()).to.be(request1); | ||
expect(request1.foo()).to.be(request1); | ||
|
||
expect(request2.foo()).to.be(request2); | ||
expect(request2.foo()).to.be(request2); | ||
expect(request2.foo()).to.be(request2); | ||
expect(request2.foo()).to.be(request2); | ||
|
||
// only two different requests, so factory should have only been | ||
// called twice with the two request objects | ||
sinon.assert.calledTwice(factory); | ||
sinon.assert.calledWithExactly(factory, request1); | ||
sinon.assert.calledWithExactly(factory, request2); | ||
}); | ||
}); |
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 @@ | ||
export { serverExtensionsMixin } from './server_extensions_mixin'; |
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,44 @@ | ||
|
||
export function serverExtensionsMixin(kbnServer, server) { | ||
/** | ||
* Decorate all request objects with a new method, `methodName`, | ||
* that will call the `factory` on first invocation and return | ||
* the result of the first call to subsequent invocations. | ||
* | ||
* @method server.addMemoizedFactoryToRequest | ||
* @param {string} methodName location on the request this | ||
* factory should be added | ||
* @param {Function} factory the factory to add to the request, | ||
* which will be called once per request | ||
* with a single argument, the request. | ||
* @return {undefined} | ||
*/ | ||
server.decorate('server', 'addMemoizedFactoryToRequest', (methodName, factory) => { | ||
if (typeof methodName !== 'string') { | ||
throw new TypeError('methodName must be a string'); | ||
} | ||
|
||
if (typeof factory !== 'function') { | ||
throw new TypeError('factory must be a function'); | ||
} | ||
|
||
if (factory.length > 1) { | ||
throw new TypeError(` | ||
factory must not take more than one argument, the request object. | ||
Memoization is done based on the request instance and is cached and reused | ||
regardless of other arguments. If you want to have a per-request cache that | ||
also does some sort of secondary memoization then return an object or function | ||
from the memoized decorator and do secordary memoization there. | ||
`); | ||
} | ||
|
||
const requestCache = new WeakMap(); | ||
server.decorate('request', methodName, function () { | ||
if (!requestCache.has(this)) { | ||
requestCache.set(this, factory(this)); | ||
} | ||
|
||
return requestCache.get(this); | ||
}); | ||
}); | ||
} |
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
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
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.
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.
Is
this
the only way to do this? No other access to therequest
?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.
If not, maybe
const request = this
above this line? Just to make it super-obvious.