-
Notifications
You must be signed in to change notification settings - Fork 17
[KZL-1098] proxify kuzzle to avoid mistyping error #395
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 5 commits
4866187
ccd8dc2
978fbf1
d59a309
d91e22f
31e7754
c64523f
f3d6787
3f3a171
c492121
d00fa56
2b5bc22
d169552
af90c3d
35efac8
b0fac8c
b0e5ad0
500601a
5ce6157
f6012da
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 | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,81 @@ | ||||||
|
|
||||||
| const getOptions = options => ({ | ||||||
| name: 'object', | ||||||
| seal: true, | ||||||
| sealGet: false, | ||||||
| deprecated: [], | ||||||
| exposeApi: false, | ||||||
| apiNamespace: '__proxy__', | ||||||
| ...options | ||||||
| }); | ||||||
|
|
||||||
| const getPropertyNames = (obj) => { | ||||||
|
||||||
| const getPropertyNames = (obj) => { | |
| const getPropertyNames = obj => { |
Outdated
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.
Could be a Set too
Aschen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Aschen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
scottinet marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Outdated
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.
you should remove the typecheck made on name:
- it can only be a string or a symbol anyway, since this is a getter trap
- if I pass a non-existing symbol to your handler, I won't get any error even with
sealGetactive, which does not seem to be the intended behavior
Outdated
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.
you must add some kind of flag to trigger that warning only once per deprecated property accessed, otherwise this can clog the console up pretty quickly
Outdated
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.
ditto
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| const | ||
| should = require('should'), | ||
| sinon = require('sinon'), | ||
| proxify = require('../../src/proxify'); | ||
|
|
||
| describe('proxify', () => { | ||
| let | ||
| warn, | ||
| srcObj; | ||
|
|
||
| beforeEach(() => { | ||
| warn = console.warn; | ||
| console.warn = sinon.stub(); | ||
|
|
||
| srcObj = { | ||
| prop: 42, | ||
| method: () => {}, | ||
| method2: () => {} | ||
| }; | ||
| }); | ||
|
|
||
| after(() => { | ||
| console.warn = warn; | ||
| }); | ||
|
|
||
| it('should not throw if use object valid property', () => { | ||
| const obj = proxify(srcObj); | ||
| should.doesNotThrow(() => { | ||
| obj.prop += 1; | ||
| }); | ||
| }); | ||
|
|
||
| it('should not throw if use object unvalid property without seal', () => { | ||
| const obj = proxify(srcObj, { seal: false }); | ||
| should.doesNotThrow(() => { | ||
| obj.prop2 = 42; | ||
| }); | ||
| }); | ||
|
|
||
| it('should throw if use object unvalid property', () => { | ||
| const obj = proxify(srcObj); | ||
| should.throws(() => { | ||
| obj.prop2 = 42; | ||
| }); | ||
| }); | ||
|
|
||
| it('should not warn if use non-deprecated property', () => { | ||
| const obj = proxify(srcObj, { | ||
| deprecated: ['method'] | ||
| }); | ||
| obj.method2(); | ||
| should(console.warn).have.callCount(0); | ||
| }); | ||
|
|
||
| it('should warn if use deprecated property', () => { | ||
| const obj = proxify(srcObj, { | ||
| deprecated: ['method'] | ||
| }); | ||
| obj.method(); | ||
| should(console.warn).have.callCount(1); | ||
| }); | ||
|
|
||
| it('should expose api to manipulate proxy props', () => { | ||
| const obj = proxify(srcObj, { | ||
| exposeApi: true, | ||
| }); | ||
| should.doesNotThrow(() => { | ||
| should(obj.__proxy__).be.Object(); | ||
| should(obj.__proxy__.registerProps).be.Function(); | ||
| should(obj.__proxy__.unregisterProps).be.Function(); | ||
| should(obj.__proxy__.hasProp).be.Function(); | ||
| }); | ||
| }); | ||
|
|
||
| it('should expose api under custom namespace', () => { | ||
| const obj = proxify(srcObj, { | ||
| exposeApi: true, | ||
| apiNamespace: 'custom' | ||
| }); | ||
| should.doesNotThrow(() => { | ||
| should(obj.custom).be.Object(); | ||
| should(obj.custom.registerProps).be.Function(); | ||
| should(obj.custom.unregisterProps).be.Function(); | ||
| should(obj.custom.hasProp).be.Function(); | ||
| }); | ||
| }); | ||
|
|
||
| it('should register new props', () => { | ||
| const obj = proxify(srcObj, { | ||
| exposeApi: true, | ||
| }); | ||
| should.throws(() => { | ||
| obj.foo = 42; | ||
| }); | ||
| obj.__proxy__.registerProps('foo'); | ||
| should.doesNotThrow(() => { | ||
| obj.foo += 1; | ||
| }); | ||
| }); | ||
|
|
||
| it('should unregister props', () => { | ||
| const obj = proxify(srcObj, { | ||
| exposeApi: true, | ||
| }); | ||
| should.doesNotThrow(() => { | ||
| obj.prop = 42; | ||
| }); | ||
| obj.__proxy__.unregisterProps('prop'); | ||
| should.throws(() => { | ||
| obj.prop += 1; | ||
| }); | ||
| }); | ||
|
|
||
| it('should check has props without warn', () => { | ||
| const obj = proxify(srcObj, { | ||
| exposeApi: true, | ||
| }); | ||
| let res; | ||
| should.doesNotThrow(() => { | ||
| res = obj.__proxy__.hasProp('foo'); | ||
| }); | ||
| should(res).be.eql(false); | ||
| }); | ||
|
|
||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| const | ||
| should = require('should'), | ||
| sinon = require('sinon'), | ||
| Kuzzle = require('../../src/Kuzzle'), | ||
| AuthController = require('../../src/controllers/auth'), | ||
| BulkController = require('../../src/controllers/bulk'), | ||
| CollectionController = require('../../src/controllers/collection'), | ||
| DocumentController = require('../../src/controllers/document'), | ||
| IndexController = require('../../src/controllers/index'), | ||
| MemoryStorageController = require('../../src/controllers/memoryStorage'), | ||
| SecurityController = require('../../src/controllers/security'), | ||
| ServerController = require('../../src/controllers/server'), | ||
| RealTimeController = require('../../src/controllers/realtime'), | ||
| { | ||
| SocketIO, | ||
| WebSocket, | ||
| Http | ||
| } = require('../../src/protocols'), | ||
| ProtocolMock = require('../mocks/protocol.mock'); | ||
|
|
||
| describe('Kuzzle proxy', () => { | ||
| const protocolMock = new ProtocolMock('somewhere'); | ||
|
|
||
| it('should throw an error if one tries to set unvalid properties', () => { | ||
thomasarbona marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const kuzzle = new Kuzzle(protocolMock); | ||
| should.throws(() => { | ||
Aschen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| kuzzle.jvt = 'foobar'; | ||
| }); | ||
| }); | ||
|
|
||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.