Skip to content

Commit

Permalink
Merge pull request #7 from derbyjs/debrowserified
Browse files Browse the repository at this point in the history
Debrowserify module
  • Loading branch information
craigbeck authored Mar 2, 2023
2 parents b5704f7 + 514482f commit f270613
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 57 deletions.
29 changes: 18 additions & 11 deletions lib/browser.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
var racer = require('racer');
var BCSocket = require('browserchannel/dist/bcsocket-uncompressed').BCSocket;
const racer = require('racer');
const BCSocket = require('browserchannel/dist/bcsocket-uncompressed').BCSocket;

var CLIENT_OPTIONS = {{clientOptions}};
const DEFAULT_CLIENT_OPTIONS = {
reconnect: true,
base: '/channel',
}

racer.Model.prototype._createSocket = function(bundle) {
var options = CLIENT_OPTIONS;
if (bundle.browserChannel) {
options = racer.util.mergeInto({}, options);
racer.util.mergeInto(options, bundle.browserChannel);
module.exports = function init(clientOptions) {
if (!process.browser) {
return;
}
var base = options.base || '/channel';
return new BCSocket(base, options);
};
const options = Object.assign({}, DEFAULT_CLIENT_OPTIONS, clientOptions);

racer.Model.prototype._createSocket = function(bundle) {
if (bundle.browserChannel) {
racer.util.mergeInto(options, bundle.browserChannel);
}
return new BCSocket(options.base, options);
};
}
48 changes: 9 additions & 39 deletions lib/server.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,12 @@
var Duplex = require('stream').Duplex;
var browserChannel = require('browserchannel').server;
var through = require('through');
var path = require('path');
var util = require('util');
const Duplex = require('stream').Duplex;
const browserChannel = require('browserchannel').server;
const util = require('util');

// Pass in pass through stream
module.exports = function(backend, serverOptions, clientOptions) {
if (!clientOptions) clientOptions = {};
if (clientOptions.reconnect == null) clientOptions.reconnect = true;
var clientOptionsJson = JSON.stringify(clientOptions);

// Add the client side script to the Browserify bundle. Set the clientOptions
// needed to connect to the corresponding server by injecting them into the
// file during bundling
backend.on('bundle', function(bundle) {
var browserFilename = path.join(__dirname, 'browser.js');
bundle.transform(function(filename) {
if (filename !== browserFilename) return through();
var file = '';
return through(
function write(data) {
file += data;
}
, function end() {
var rendered = file.replace('{{clientOptions}}', clientOptionsJson);
this.queue(rendered);
this.queue(null);
}
);
});
bundle.add(browserFilename);
});

var middleware = browserChannel(serverOptions, function(client, connectRequest) {
var stream = createStream(client);
module.exports = function(backend, serverOptions) {
return browserChannel(serverOptions, function(client, connectRequest) {
const stream = createStream(client);
backend.listen(stream, connectRequest);
});
return middleware;
};

/**
Expand All @@ -45,10 +15,10 @@ module.exports = function(backend, serverOptions, clientOptions) {
* @return {Duplex} stream
*/
function createStream(client) {
var stream = new ClientStream(client);
const stream = new ClientStream(client);

client.on('message', function onMessage(message) {
var data;
let data;
try {
data = JSON.parse(message);
} catch (err) {
Expand All @@ -72,7 +42,7 @@ function ClientStream(client) {

this.client = client;

var self = this;
const self = this;

this.on('error', function(error) {
console.warn('BrowserChannel client message stream error', error.stack || error);
Expand Down
16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "racer-browserchannel",
"name": "@derbyjs/racer-browserchannel",
"description": "Browserchannel socket plugin for Racer",
"homepage": "http://racerjs.com/",
"repository": {
Expand All @@ -8,14 +8,16 @@
},
"version": "0.5.0",
"main": "./lib/server.js",
"browser": {
"main": "./lib/browser.js"
},
"browser": "./lib/browser.js",
"dependencies": {
"browserchannel": "^2.0",
"through": "^2.3.4"
"browserchannel": "^2.0"
},
"peerDependencies": {
"racer": "*"
},
"publishConfig": {
"access": "public"
},
"devDependencies": {},
"optionalDependencies": {},
"bugs": {
"url": "https://github.com/derbyjs/racer-browserchannel/issues"
Expand Down

0 comments on commit f270613

Please sign in to comment.