Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions RELEASE-NOTES.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.1.0

- Increase usability in other projects by separating `node` and browser modules [#100](https://github.com/Simperium/node-simperium/pull/100)

## 1.0.4

- Update diff-match-patch to newer revision of surrogate-pair encoding fix
Expand Down
14 changes: 9 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
{
"name": "simperium",
"version": "1.0.4",
"version": "1.1.0",
"description": "A simperium client for node.js",
"main": "./lib/simperium/index.js",
"browser": {
"./lib/simperium/http-request.js": "./lib/simperium/http-request.browser.js"
},
"repository": {
"type": "git",
"url": "git://github.com/Simperium/node-simperium.git"
Expand All @@ -22,6 +25,8 @@
"license": "BSD-2-Clause",
"dependencies": {
"@babel/polyfill": "7.7.0",
"events": "3.1.0",
"inherits": "2.0.4",
"uuid": "3.3.3",
"websocket": "1.0.31"
},
Expand Down
53 changes: 14 additions & 39 deletions src/simperium/auth.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// @flow
import events from 'events'
import { request } from 'https'
import url from 'url'
import request from './http-request';

// @flow
type User = {
Expand All @@ -22,7 +21,7 @@ const fromJSON = ( json: string ): User => {

const { EventEmitter } = events;

const URL = 'https://auth.simperium.com/1';
const baseUrl = 'https://auth.simperium.com/1';

export class AuthError extends Error {
underlyingError: Error
Expand Down Expand Up @@ -73,44 +72,20 @@ export class Auth extends EventEmitter {
return this.request( 'create/', body );
}

getUrlOptions( path: string ) {
const { port, ...options } = url.parse( `${URL}/${ this.appId }/${ path}` );
return {
... options,
port: port ? Number( port ) : undefined,
method: 'POST',
headers: {'X-Simperium-API-Key': this.appSecret }
};
}

request( endpoint: string, body: string ): Promise<User> {
return new Promise( ( resolve, reject ) => {
const req = request( this.getUrlOptions( endpoint ), ( res ) => {
let responseData = '';

res.on( 'data', ( data ) => {
responseData += data.toString();
} );

res.on( 'end', () => {
try {
const user = fromJSON( responseData );
resolve( user );
this.emit( 'authorize', user );
} catch ( error ) {
return reject( new AuthError( error ) );
}
} );
} );

req.on( 'error', ( e ) => {
reject( e );
} );

req.end( body );
} );
const authUrl = `${ baseUrl }/${ this.appId }/${ endpoint }`;

return request( this.appSecret, authUrl, body ).then( response => {
try {
const user = fromJSON( response );
this.emit( 'authorize', user );
return user;
} catch ( error ) {
throw new AuthError( error );
}
} )
}
};
}

export default ( appId: string, appSecret: string ) => {
return new Auth( appId, appSecret );
Expand Down
2 changes: 1 addition & 1 deletion src/simperium/bucket.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EventEmitter } from 'events'
import { inherits } from 'util'
import inherits from 'inherits';
import { v4 as uuid } from 'uuid';

/**
Expand Down
8 changes: 4 additions & 4 deletions src/simperium/channel.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*eslint no-shadow: 0*/
import {format, inherits} from 'util'
import inherits from 'inherits';
import {EventEmitter} from 'events'
import {change as change_util, parseMessage, parseVersionMessage} from './util'
import {v4 as uuid} from 'uuid'
Expand Down Expand Up @@ -585,7 +585,7 @@ Channel.prototype.onConnect = function() {
version: '0.0.1'
};

this.send( format( 'init:%s', JSON.stringify( init ) ) );
this.send( `init:${ JSON.stringify( init ) }` );
};

Channel.prototype.onIndex = function( data ) {
Expand Down Expand Up @@ -615,11 +615,11 @@ Channel.prototype.onIndex = function( data ) {
};

Channel.prototype.sendIndexRequest = function( mark ) {
this.send( format( 'i:1:%s::10', mark ? mark : '' ) );
this.send( `i:1:${ mark ? mark : '' }::10` );
};

Channel.prototype.sendChangeVersionRequest = function( cv ) {
this.send( format( 'cv:%s', cv ) );
this.send( `cv:${ cv }` );
};

Channel.prototype.onChanges = function( changes ) {
Expand Down
14 changes: 7 additions & 7 deletions src/simperium/client.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { format, inherits } from 'util'
import inherits from 'inherits';
import { EventEmitter } from 'events'
import Bucket from './bucket'
import Channel from './channel'
Expand Down Expand Up @@ -63,7 +63,7 @@ export default function Client( appId, accessToken, options ) {

this.appId = appId;

options.url = options.url || format( 'wss://api.simperium.com/sock/1/%s/websocket', this.appId );
options.url = options.url || `wss://api.simperium.com/sock/1/${ this.appId }/websocket`;

this.reconnect = true;

Expand Down Expand Up @@ -97,7 +97,7 @@ Client.prototype.bucket = function( name ) {
channel.on( 'send', send );

this.on( 'connect', channel.onConnect.bind( channel ) );
this.on( format( 'channel:%d', channelId ), receive );
this.on( `channel:${ channelId }`, receive );
this.on( 'access-token', function( token ) {
channel.access_token = token;
} );
Expand Down Expand Up @@ -154,14 +154,14 @@ Client.prototype.parseMessage = function( event ) {
this.emit( 'message', data );

if ( isNaN( channelId ) ) {
this.emit( format( 'message:%s', prefix ), channelMessage );
this.emit( `message:${ prefix }`, channelMessage );
} else {
this.emit( format( 'channel:%d', channelId ), channelMessage );
this.emit( `channel:${ channelId }`, channelMessage );
}
};

Client.prototype.sendHeartbeat = function( count ) {
this.send( format( 'h:%d', count ) );
this.send( `h:${ count }` );
};

Client.prototype.send = function( data ) {
Expand All @@ -175,7 +175,7 @@ Client.prototype.send = function( data ) {
};

Client.prototype.sendChannelMessage = function( id, message ) {
this.send( format( '%d:%s', id, message ) );
this.send( `${ id }:${ message }` );
};

Client.prototype.connect = function() {
Expand Down
18 changes: 18 additions & 0 deletions src/simperium/http-request.browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// @flow
export default function(
apiKey: string,
url: string,
body: string
): Promise<string> {
return new Promise( ( resolve, reject ) => {
const xhr = new XMLHttpRequest();

xhr.open( 'POST', url );
xhr.setRequestHeader( 'X-Simperium-API-Key', apiKey );

xhr.onload = () => resolve( xhr.responseText );
xhr.onerror = () => reject();

xhr.send( body );
} );
}
32 changes: 32 additions & 0 deletions src/simperium/http-request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// @flow
import { request } from 'https'

export default function(
apiKey: string,
url: string,
body: string,
): Promise<string> {
return new Promise( ( resolve, reject ) => {
const headers = {
'X-Simperium-API-Key': apiKey
};

const req = request( url, { method: 'POST', headers }, res => {
let responseData = '';

res.on( 'data', data => {
responseData += data.toString();
} );

res.on( 'end', () => {
resolve( responseData );
} );
} );

req.on( 'error', ( e ) => {
reject( e );
} );

req.end( body );
} );
}
10 changes: 5 additions & 5 deletions src/simperium/server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { server as WebSocketServer } from 'websocket'
import http from 'http'
import { inherits, format } from 'util'
import inherits from 'inherits';
import { EventEmitter } from 'events'
import { parseMessage } from './util'

Expand Down Expand Up @@ -46,7 +46,7 @@ Session.prototype.onMessage = function( msg ) {
if ( channelId === 'h' ) {
i = parseInt( message.data );
if ( isNaN( i ) ) i = 0;
this.connection.send( format( 'h:%d', i + 1 ) );
this.connection.send( `h:${ i + 1 }` );
return;
}

Expand All @@ -66,10 +66,10 @@ Session.prototype.getChannel = function( id ) {
this.channels[id] = channel;
channel
.on( 'send', function( data ) {
connection.send( format( '%d:%s', id, data ) );
connection.send( `${ id }:${ data }` );
} )
.on( 'unauthorized', function() {
connection.send( format( '%d:auth:%s', id, JSON.stringify( {code: 500} ) ) );
connection.send( `${ id }:auth:${ JSON.stringify( {code: 500} ) }` );
connection.close();
} );
}
Expand Down Expand Up @@ -108,7 +108,7 @@ Channel.prototype.init = function( data ) {

this.bucket.initialize( options, function( e, user ) {
if ( e ) return emit( 'unauthorized', e );
emit( 'send', format( 'auth:%s', user.email ) );
emit( 'send', `auth:${ user.email }` );
} );
};

Expand Down
12 changes: 2 additions & 10 deletions test/simperium/auth_test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import buildAuth from '../../src/simperium/auth'
import https from 'https'
import { equal, deepEqual } from 'assert'
import { equal } from 'assert'
import { EventEmitter } from 'events'

const originalRequest = https.request;
const stub = ( respond ) => {
https.request = ( options, handler ) => {
https.request = ( url, options, handler ) => {
const req = new EventEmitter()
req.end = ( body ) => respond( body, handler )
return req
Expand All @@ -31,14 +31,6 @@ describe( 'Auth', () => {
auth = buildAuth( 'token', 'secret' );
} );

it( 'getUrlOptions', () => {
const { hostname, headers, pathname, method } = auth.getUrlOptions( 'path' )
equal( method, 'POST' )
equal( hostname, 'auth.simperium.com' )
equal( pathname, '/1/token/path' )
deepEqual( headers, { 'X-Simperium-API-Key': 'secret' } )
} )

it( 'should request auth token', () => {
stub( ( data, handler ) => {
const { username, password } = JSON.parse( data )
Expand Down