Skip to content

Conversation

ballinette
Copy link

@ballinette ballinette commented Jun 27, 2018

What does this PR do?

See KZL-175

Change the signature of SDK constructor to match actual signature in Go SDK and enable developers to instantiate the SDK with their custom network protocol:

// Before: 
const kuzzle = new Kuzzle('hostname', {protocol: 'websocket'});

// Now - with embed protocol:
const kuzzle = new Kuzzle('websocket', {host: 'hostname'});

// Now - with custom protocol:
const protocol = new MyCustomProtocol();
const kuzzle = new Kuzzle(protocol, {});

Other changes

Expose also AbstractNetwork and KuzzleEventEmitter:
Thus, custom protocol can easily inherits from AbstractNetwork and only implements connect and send methods:

const
  Kuzzle = require('kuzzle-sdk').Kuzzle,
  KuzzleAbstractNetwork = require('kuzzle-sdk').KuzzleAbstractNetwork;

class MyCustomProtocol extends KuzzleAbstractNetwork {
  connect() {
    // (...) do custom connection steps...

    // change state and resolve:
    this.state = 'ready';
    return Promise.resolve();
  }

  send(request) {
    // (...) here the protocol-specific code to send the request to kuzzle and get the result into `result` variable

    // Send back the result to SDK and resolve:
    this.emit(request.requestId, {result});
    return Promise.resolve();
  }
}

const protocol = new MyCustomProtocol();
const kuzzle = new Kuzzle(protocol);
kuzzle.connect()
.then(() => kuzzle.server.now())
.then(res => {
  console.log(`Current Kuzzle timestamp: ${res}`);
})

Another way to implement a custom protocol is to implement isReady and query methods, as well as javascript Event API:

const
  Kuzzle = require('kuzzle-sdk').Kuzzle,
  KuzzleEventEmitter = require('kuzzle-sdk').KuzzleEventEmitter;

class MyCustomProtocol extends KuzzleEventEmitter {

  isReady() {
    return true;
  }

  query (request, options) {
    // (...) here the protocol-specific code to send the request to kuzzle and get the result into `result` variable

    // Resolves the response:
    return Promise.resolve({result});
  }
}

Boyscout

Some minor cosmetic changes in network wrappers code.

Ballinette added 2 commits June 27, 2018 18:34
* enable to instantiate Kuzzle SDK with a custom network object
* move `host` parameter to a member of `options` (which is required for embed network protocols, but could be useless for custom ones)
* some small refactor to clean the code
@codecov-io
Copy link

codecov-io commented Jun 27, 2018

Codecov Report

Merging #294 into 6.x will decrease coverage by 0.25%.
The diff coverage is 75.55%.

Impacted file tree graph

@@            Coverage Diff             @@
##              6.x     #294      +/-   ##
==========================================
- Coverage   94.92%   94.66%   -0.26%     
==========================================
  Files          28       28              
  Lines        1319     1331      +12     
==========================================
+ Hits         1252     1260       +8     
- Misses         67       71       +4
Impacted Files Coverage Δ
src/Kuzzle.js 99.3% <100%> (ø) ⬆️
src/networkWrapper/index.js 100% <100%> (+10%) ⬆️
src/networkWrapper/protocols/http.js 86.2% <33.33%> (-1.89%) ⬇️
src/networkWrapper/protocols/abstract/common.js 58.25% <57.14%> (ø) ⬆️
src/networkWrapper/protocols/socketio.js 94.64% <75%> (-1.66%) ⬇️
src/networkWrapper/protocols/websocket.js 98.07% <75%> (-1.93%) ⬇️
src/networkWrapper/protocols/abstract/realtime.js 94.44% <77.77%> (-2.53%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1e5b4c7...3f9ffbc. Read the comment docs.

@scottinet scottinet changed the title Enable to instantiate Kuzzle SDK with a custom network object Allow passing a custom network object to Kuzzle SDK constructor Jun 29, 2018
@ballinette ballinette merged commit b880c82 into 6.x Jul 4, 2018
@ballinette ballinette deleted the KZL-175-sdk-constructor branch July 4, 2018 15:43
@Aschen Aschen mentioned this pull request Dec 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants