Permalink
Browse files

[feature] Add custom spark id generator option (#601)

  • Loading branch information...
nanowizard authored and lpinca committed Jul 22, 2017
1 parent aa46404 commit 93ca015bedcae1313644046f823526b48e9099ea
Showing with 23 additions and 2 deletions.
  1. +8 −0 README.md
  2. +1 −0 primus.d.ts
  3. +3 −2 spark.js
  4. +11 −0 test/spark.test.js
View
@@ -135,6 +135,7 @@ global | Set a custom client class / global name | `Primus`
compression | Use permessage-deflate / HTTP compression | `false`
maxLength | Maximum allowed packet size, in bytes | `10485760`
transport | Transformer specific configuration | `{}`
idGenerator | Custom spark id generator function | `undefined`
origins | **cors** List of origins | `*`
methods | **cors** List of accepted HTTP methods | `GET,HEAD,PUT,POST,DELETE,OPTIONS`
credentials | **cors** Allow sending of credentials | `true`
@@ -158,6 +159,13 @@ The `pingInterval` option specifies the interval at which heartbeats are
transmitted. It is possible to completely disable the heartbeats by setting the
value of the `pingInterval` option to `false`.
The `idGenerator` option can be used to define a function which will be called
to set each [`spark.id`](#sparkid). The generator function should return
a unique string each time it is invoked. If `idGenerator` is not defined, Primus
will try to use ids provided by the transformer. If the transformer does not
provide ids, Primus will use [yeast](https://github.com/unshiftio/yeast) to
generate `Spark` ids.
If you don't have a pre-existing server where you want or can attach your Primus
server to you can also use the `Primus.createServer` convenience method. The
`createServer` method will automatically:
View
@@ -47,6 +47,7 @@ export interface IPrimusOptions {
strategy?: any;
timeout?: number;
transformer?: string;
idGenerator?: Function;
[key: string]: any;
}
View
@@ -28,10 +28,11 @@ function Spark(primus, headers, address, query, id, request) {
this.fuse();
var writable = this.writable
, spark = this;
, spark = this
, idgen = primus.options.idGenerator;
query = query || {};
id = id || yeast();
id = idgen ? idgen() : (id || yeast());
headers = headers || {};
address = address || {};
request = request || headers['primus::req::backup'];
View
@@ -59,6 +59,17 @@ describe('Spark', function () {
var spark = new primus.Spark();
});
it('accepts a third-party spark id generator', function () {
var primus = new Primus(server, {
idGenerator: function () {
return 'foo';
}
});
var spark = new primus.Spark();
expect(spark.id).to.equal('foo');
});
it('emits a `readyStateChange` event when the readyState changes', function (done) {
var spark = new primus.Spark();

0 comments on commit 93ca015

Please sign in to comment.