- 
                Notifications
    You must be signed in to change notification settings 
- Fork 17
Refactor network wrappers #243
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
Conversation
| Codecov Report
 @@            Coverage Diff             @@
##              6.x     #243      +/-   ##
==========================================
+ Coverage   98.27%   98.58%   +0.31%     
==========================================
  Files          17       17              
  Lines        2149     2127      -22     
  Branches      611      615       +4     
==========================================
- Hits         2112     2097      -15     
+ Misses         37       30       -7
 Continue to review full report at Codecov. 
 | 
|  | ||
| this.kuzzle.callbackRequired('Collection.scrollSpecifications', cb); | ||
|  | ||
| if (options && options.scroll) { | 
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.
Note: not needed here since scroll option is already managed within kuzzle.querymethod
|  | ||
| request.scrollId = scrollId; | ||
|  | ||
| if (options && options.scroll) { | 
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.
Note: not needed here since scroll option is already managed within kuzzle.querymethod
|  | ||
| if (!listeners || !listeners.length) { | ||
| return; | ||
| return false; | 
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.
Note:  fix KuzzleEventEmitter methods signatures, to be consistant with NodeJS event specitications (emit should return a boolean value - see https://nodejs.org/api/events.html#events_emitter_emit_eventname_args )
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.
A few nitpickings here and there, but apart from that this looks good to me. Though I have a hard time to figure out how all this will play together. I trust you on that :-)
        
          
                src/Kuzzle.js
              
                Outdated
          
        
      | object.jwt = this.jwt; | ||
| } | ||
|  | ||
| if (room.volatile) { | 
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.
This entire if block and the sdkVersion assignment below can be replaced with this one-liner:
Object.assign(object.volatile, room.volatile, {sdkVersion: this.sdkVersion});
(Object.assign will ignore undefined values so it's safe to use without testing for the existence of this object inside room)
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.
done
| object.jwt = this.jwt; | ||
| } | ||
|  | ||
| if (room.volatile) { | 
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.
        
          
                src/Kuzzle.js
              
                Outdated
          
        
      | self.network.onConnectError(function (error) { | ||
| var connectionError = new Error('Unable to connect to kuzzle proxy server at "' + self.host + ':' + self.port + '"'); | ||
| this.network.addListener('networkError', function (error) { | ||
| var connectionError = new Error('Unable to connect to kuzzle proxy server at "' + self.network.host + ':' + self.network.port + '"'); | 
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.
Nitpicking 1: use const instead of var
Nitpicking 2: use template litterals :-)
        
          
                src/Room.js
              
                Outdated
          
        
      | return this.kuzzle.bluebird.promisifyAll(this, { | ||
| suffix: 'Promise', | ||
| filter: function (name, func, target, passes) { | ||
| var whitelist = ['count', 'renew', 'subscribe', 'unsubscribe', 'onDone']; | 
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.
Nitpicking: use const instead of var
        
          
                src/Room.js
              
                Outdated
          
        
      | * @param {responseCallback} cb - Handles the query response | ||
| */ | ||
| count(cb) { | ||
| var data; | 
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.
Nitpicking: use const instead of var ( data can be declared directly line 119 instead of here)
        
          
                src/Room.js
              
                Outdated
          
        
      | * @return {*} this | ||
| */ | ||
| renew(cb) { | ||
| var | 
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.
Nitpicking: use const instead of var
| * Clean history from requests made more than 10s ago | ||
| */ | ||
| function cleanHistory (requestHistory) { | ||
| var | 
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.
Nitpicking: consider using const instead of var.
| var | ||
| now = Date.now(); | ||
|  | ||
| Object.keys(requestHistory).forEach(function (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.
Nitpicking: replace with for(const ... of ...)
| var reason = message; | ||
| this.client.onclose = (closeEvent, message) => { | ||
| let | ||
| error, | 
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.
This error variable is shadowed by the one declared in the function starting line 65.
Suggestion: declare this variable as a const directly line 58, as it seems to be the only block where it is used.
| this.client.onerror = function (error) { | ||
| this.client.onerror = error => { | ||
| if (!(error instanceof Error)) { | ||
| error = new Error(error); | 
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.
Nitpicking: function parameters should not be overwritten.
6bdd4c5    to
    0b5979a      
    Compare
  
    …rom Kuzzle root component to Network wrappers
* remove isValid() (obsolete method) * move queue management from Kuzzle to network wrappers * fix unit tests
+ use Babel for browsers
…JS `event` specitications: emit should return a boolean value. (see https://nodejs.org/api/events.html#events_emitter_emit_eventname_args )
…ead of the whole room) * add unit-tests for network.subscribe and network.unsubscribe methods
+ add 'use strict' in pure ES6 classes
to enable calls like `kuzzle.queryPromise({controller: 'server', action: 'healthCheck'}).then(...);`
    4ff1148    to
    dab1ecc      
    Compare
  
    | } | ||
| return new (require('./protocols/websocket'))(host, options); | ||
| case 'socketio': | ||
| if (!window.io) { | 
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.
We assume that no one will ever try to use Socket.IO outside a browser?
(after all, sounds fair to me, just asking to be sure it's on purpose)
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 we are outside a browser, we have native websocket support, so I don't see any reason to use socketIO...
        
          
                test/kuzzle/connect.test.js
              
                Outdated
          
        
      | }); | ||
|  | ||
| it('should dequeue requests automatically on a connection success', function (done) { | ||
| it('should keep a valid JWT Token at reconnection', function () { | 
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.
a valid JWT ?
        
          
                test/kuzzle/connect.test.js
              
                Outdated
          
        
      |  | ||
| describe('=> on reconnection', function () { | ||
| it('should exit offline mode when reconnecting', function (done) { | ||
| it('should empty the JWT Token at reconnection if it has expired', function () { | 
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.
JWT ?
        
          
                test/kuzzle/methods.test.js
              
                Outdated
          
        
      | should((function () {kuzzle.setDefaultIndex(null);})).throw(); | ||
| should((function () {kuzzle.setDefaultIndex(undefined);})).throw(); | ||
| describe('#getJwt', function () { | ||
| it('should return the current jwt token', function () { | 
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.
JWT
        
          
                test/kuzzle/methods.test.js
              
                Outdated
          
        
      | }); | ||
| should(kuzzle.jwt).be.undefined(); | ||
| should(loginAttemptStub).be.calledOnce(); | ||
| should(loginAttemptStub).be.calledWith({ error: 'Cannot find a valid JWT token in the following object: {"foo":"bar"}', success: false }); | 
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.
:(
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.
Ok for me!
ea2e97d    to
    7b767e7      
    Compare
  
    # Conflicts: # src/Kuzzle.js # test/network/networkWrapper.test.js
Improvements
Refactor the network wrappers to improve separation of concerns:
offline,connecting,connectedfor realtime protocole ;offline,readyfor future HTTP protocol)Kuzzleobject still exposes queuing methods, but now delegates their implementation to network protocols.Refactor the Room management : the
Roomclass now inherits fromEventEmitter:Some sample codes to manage rooms:
Boyscout improvements
KuzzleandRoomprototypesjwtTokenbyjwtBreaking changes
Renamed properties and method in
KuzzleprototypejwtToken=>jwtgetJwtToken()=>getJwt()setJwtToken(token)' =>setJwt(token)`unsetJwtToken()=>unsetJwt()replayQueue()=>playQueue()Removal of
SubscribeResultprototype:onDonemethod move toRoomprototype.Collection.subscribte()method returns now the createdRoomobjectChanged signature for
Collection.roommethod andRoomconstructorcollection.room([options])=>collection.room(filters, [options])new Room(collection, [options])=>new Room(collection, filters, [options])Changed signature for
Room.renewmethodrenew([filters], notificationCallback, subscriptionCallback)=>renew(subscriptionCallback)filtersis now an immutable property (we need to create a new Room if we want to change the filters)notificationCallbackhas become useless since theRoomis now an event emitter:New option
protocolto Kuzzle constructorMust be filled to use another protocol than native websocket
currently, 2 supported values:
websocket(default)socketio