Skip to content

Commit 4c6d3c5

Browse files
authored
Update/universalize app (#100)
While attempting to import this library into Automattic/wp-calypso I ran into unexpected problems as a result of the node core imports. Calypso's webpack configs explicitly exclude the built-in polyfills with node: false as the setting. As a result this fails to build properly there and is unusable. While this can be resolved in Calypso it's indicative that this library doesn't follow what has become a standard for handling these cases: either use libraries which run in both node and in the browser; or use the "browser" field in package.json to provide replacement modules for the browser context. In this patch we're applying both techniques to make this work in more contexts. We've replaced the use of util.inherits with the inherits package and replaced the use of util.format with string template literals. Finally instead of importing a generic requests library we're including native versions of the request mechanism for node and for the browser and using the "browser" field to choose the proper one.
1 parent 8dc2241 commit 4c6d3c5

File tree

11 files changed

+102
-72
lines changed

11 files changed

+102
-72
lines changed

RELEASE-NOTES.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 1.1.0
4+
5+
- Increase usability in other projects by separating `node` and browser modules [#100](https://github.com/Simperium/node-simperium/pull/100)
6+
37
## 1.0.4
48

59
- Update diff-match-patch to newer revision of surrogate-pair encoding fix

package-lock.json

Lines changed: 9 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
{
22
"name": "simperium",
3-
"version": "1.0.4",
3+
"version": "1.1.0",
44
"description": "A simperium client for node.js",
55
"main": "./lib/simperium/index.js",
6+
"browser": {
7+
"./lib/simperium/http-request.js": "./lib/simperium/http-request.browser.js"
8+
},
69
"repository": {
710
"type": "git",
811
"url": "git://github.com/Simperium/node-simperium.git"
@@ -22,6 +25,8 @@
2225
"license": "BSD-2-Clause",
2326
"dependencies": {
2427
"@babel/polyfill": "7.7.0",
28+
"events": "3.1.0",
29+
"inherits": "2.0.4",
2530
"uuid": "3.3.3",
2631
"websocket": "1.0.31"
2732
},

src/simperium/auth.js

Lines changed: 14 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// @flow
22
import events from 'events'
3-
import { request } from 'https'
4-
import url from 'url'
3+
import request from './http-request';
54

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

2322
const { EventEmitter } = events;
2423

25-
const URL = 'https://auth.simperium.com/1';
24+
const baseUrl = 'https://auth.simperium.com/1';
2625

2726
export class AuthError extends Error {
2827
underlyingError: Error
@@ -73,44 +72,20 @@ export class Auth extends EventEmitter {
7372
return this.request( 'create/', body );
7473
}
7574

76-
getUrlOptions( path: string ) {
77-
const { port, ...options } = url.parse( `${URL}/${ this.appId }/${ path}` );
78-
return {
79-
... options,
80-
port: port ? Number( port ) : undefined,
81-
method: 'POST',
82-
headers: {'X-Simperium-API-Key': this.appSecret }
83-
};
84-
}
85-
8675
request( endpoint: string, body: string ): Promise<User> {
87-
return new Promise( ( resolve, reject ) => {
88-
const req = request( this.getUrlOptions( endpoint ), ( res ) => {
89-
let responseData = '';
90-
91-
res.on( 'data', ( data ) => {
92-
responseData += data.toString();
93-
} );
94-
95-
res.on( 'end', () => {
96-
try {
97-
const user = fromJSON( responseData );
98-
resolve( user );
99-
this.emit( 'authorize', user );
100-
} catch ( error ) {
101-
return reject( new AuthError( error ) );
102-
}
103-
} );
104-
} );
105-
106-
req.on( 'error', ( e ) => {
107-
reject( e );
108-
} );
109-
110-
req.end( body );
111-
} );
76+
const authUrl = `${ baseUrl }/${ this.appId }/${ endpoint }`;
77+
78+
return request( this.appSecret, authUrl, body ).then( response => {
79+
try {
80+
const user = fromJSON( response );
81+
this.emit( 'authorize', user );
82+
return user;
83+
} catch ( error ) {
84+
throw new AuthError( error );
85+
}
86+
} )
11287
}
113-
};
88+
}
11489

11590
export default ( appId: string, appSecret: string ) => {
11691
return new Auth( appId, appSecret );

src/simperium/bucket.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { EventEmitter } from 'events'
2-
import { inherits } from 'util'
2+
import inherits from 'inherits';
33
import { v4 as uuid } from 'uuid';
44

55
/**

src/simperium/channel.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*eslint no-shadow: 0*/
2-
import {format, inherits} from 'util'
2+
import inherits from 'inherits';
33
import {EventEmitter} from 'events'
44
import {change as change_util, parseMessage, parseVersionMessage} from './util'
55
import {v4 as uuid} from 'uuid'
@@ -585,7 +585,7 @@ Channel.prototype.onConnect = function() {
585585
version: '0.0.1'
586586
};
587587

588-
this.send( format( 'init:%s', JSON.stringify( init ) ) );
588+
this.send( `init:${ JSON.stringify( init ) }` );
589589
};
590590

591591
Channel.prototype.onIndex = function( data ) {
@@ -615,11 +615,11 @@ Channel.prototype.onIndex = function( data ) {
615615
};
616616

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

621621
Channel.prototype.sendChangeVersionRequest = function( cv ) {
622-
this.send( format( 'cv:%s', cv ) );
622+
this.send( `cv:${ cv }` );
623623
};
624624

625625
Channel.prototype.onChanges = function( changes ) {

src/simperium/client.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { format, inherits } from 'util'
1+
import inherits from 'inherits';
22
import { EventEmitter } from 'events'
33
import Bucket from './bucket'
44
import Channel from './channel'
@@ -63,7 +63,7 @@ export default function Client( appId, accessToken, options ) {
6363

6464
this.appId = appId;
6565

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

6868
this.reconnect = true;
6969

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

9999
this.on( 'connect', channel.onConnect.bind( channel ) );
100-
this.on( format( 'channel:%d', channelId ), receive );
100+
this.on( `channel:${ channelId }`, receive );
101101
this.on( 'access-token', function( token ) {
102102
channel.access_token = token;
103103
} );
@@ -154,14 +154,14 @@ Client.prototype.parseMessage = function( event ) {
154154
this.emit( 'message', data );
155155

156156
if ( isNaN( channelId ) ) {
157-
this.emit( format( 'message:%s', prefix ), channelMessage );
157+
this.emit( `message:${ prefix }`, channelMessage );
158158
} else {
159-
this.emit( format( 'channel:%d', channelId ), channelMessage );
159+
this.emit( `channel:${ channelId }`, channelMessage );
160160
}
161161
};
162162

163163
Client.prototype.sendHeartbeat = function( count ) {
164-
this.send( format( 'h:%d', count ) );
164+
this.send( `h:${ count }` );
165165
};
166166

167167
Client.prototype.send = function( data ) {
@@ -175,7 +175,7 @@ Client.prototype.send = function( data ) {
175175
};
176176

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

181181
Client.prototype.connect = function() {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// @flow
2+
export default function(
3+
apiKey: string,
4+
url: string,
5+
body: string
6+
): Promise<string> {
7+
return new Promise( ( resolve, reject ) => {
8+
const xhr = new XMLHttpRequest();
9+
10+
xhr.open( 'POST', url );
11+
xhr.setRequestHeader( 'X-Simperium-API-Key', apiKey );
12+
13+
xhr.onload = () => resolve( xhr.responseText );
14+
xhr.onerror = () => reject();
15+
16+
xhr.send( body );
17+
} );
18+
}

src/simperium/http-request.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// @flow
2+
import { request } from 'https'
3+
4+
export default function(
5+
apiKey: string,
6+
url: string,
7+
body: string,
8+
): Promise<string> {
9+
return new Promise( ( resolve, reject ) => {
10+
const headers = {
11+
'X-Simperium-API-Key': apiKey
12+
};
13+
14+
const req = request( url, { method: 'POST', headers }, res => {
15+
let responseData = '';
16+
17+
res.on( 'data', data => {
18+
responseData += data.toString();
19+
} );
20+
21+
res.on( 'end', () => {
22+
resolve( responseData );
23+
} );
24+
} );
25+
26+
req.on( 'error', ( e ) => {
27+
reject( e );
28+
} );
29+
30+
req.end( body );
31+
} );
32+
}

src/simperium/server.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { server as WebSocketServer } from 'websocket'
22
import http from 'http'
3-
import { inherits, format } from 'util'
3+
import inherits from 'inherits';
44
import { EventEmitter } from 'events'
55
import { parseMessage } from './util'
66

@@ -46,7 +46,7 @@ Session.prototype.onMessage = function( msg ) {
4646
if ( channelId === 'h' ) {
4747
i = parseInt( message.data );
4848
if ( isNaN( i ) ) i = 0;
49-
this.connection.send( format( 'h:%d', i + 1 ) );
49+
this.connection.send( `h:${ i + 1 }` );
5050
return;
5151
}
5252

@@ -66,10 +66,10 @@ Session.prototype.getChannel = function( id ) {
6666
this.channels[id] = channel;
6767
channel
6868
.on( 'send', function( data ) {
69-
connection.send( format( '%d:%s', id, data ) );
69+
connection.send( `${ id }:${ data }` );
7070
} )
7171
.on( 'unauthorized', function() {
72-
connection.send( format( '%d:auth:%s', id, JSON.stringify( {code: 500} ) ) );
72+
connection.send( `${ id }:auth:${ JSON.stringify( {code: 500} ) }` );
7373
connection.close();
7474
} );
7575
}
@@ -108,7 +108,7 @@ Channel.prototype.init = function( data ) {
108108

109109
this.bucket.initialize( options, function( e, user ) {
110110
if ( e ) return emit( 'unauthorized', e );
111-
emit( 'send', format( 'auth:%s', user.email ) );
111+
emit( 'send', `auth:${ user.email }` );
112112
} );
113113
};
114114

0 commit comments

Comments
 (0)