mirror of
https://github.com/expo/watchman.git
synced 2026-11-21 08:49:26 -10:00
Summary: this is a javascript implementation of BSER encoding/decoding. This helps us workaround the perf issues in Refs: https://github.com/facebook/watchman/issues/113 I've performed only functional testing thus far, so I don't have any real data on perf impact. I'm hoping to avoid writing C++ to interface with the Node/V8 internals. For decoding, the strategy is to read the PDU length up-front, accumulate the data into the buffer, and when we have the complete data, recursively process and decode it. This is much simpler than doing something like a SAX streaming parser. We can incrementally accumulate the data so that we can plug into the node event loop. The BunserBuf class will emit an event for each decoded value in the stream. A convenience method is provided that wraps around BunserBuf and decodes an input string or Buffer and returns the result (rather than emitting an event). For encoding, to make things simpler, and because the input data tends to be small and relatively simple, we only encode integers as int32 values. This gives us a predictable size in the header region and allows us to go back and fill in the size. Since json numbers are all real values, all number values passed to the BSER encoder will occupy 8 bytes. Removed unused deps from the package, and bumped up the version number. Test Plan: `npm test`. also: ``` cd watchman/node npm link cd .. NODE_PATH=/usr/local/lib/node_modules node node/example.js ``` Reviewers: amasad Reviewed By: amasad Subscribers: sid0 Differential Revision: https://reviews.facebook.net/D40479
192 lines
5.2 KiB
JavaScript
192 lines
5.2 KiB
JavaScript
/* Copyright 2014-present Facebook, Inc.
|
|
* Licensed under the Apache License, Version 2.0 */
|
|
|
|
var net = require('net');
|
|
var EE = require('events').EventEmitter;
|
|
var util = require('util');
|
|
var childProcess = require('child_process');
|
|
var bser = require('./bser');
|
|
|
|
// We'll emit the responses to these when they get sent down to us
|
|
var unilateralTags = ['subscription', 'log'];
|
|
|
|
/**
|
|
* @param options An object with the following optional keys:
|
|
* * 'watchmanBinaryPath' (string) Absolute path to the watchman binary.
|
|
* If not provided, the Client locates the binary using the PATH specified
|
|
* by the node child_process's default env.
|
|
*/
|
|
function Client(options) {
|
|
var self = this;
|
|
EE.call(this);
|
|
|
|
this.watchmanBinaryPath = 'watchman';
|
|
if (options && options.watchmanBinaryPath) {
|
|
this.watchmanBinaryPath = options.watchmanBinaryPath.trim();
|
|
};
|
|
this.commands = [];
|
|
}
|
|
util.inherits(Client, EE);
|
|
|
|
module.exports.Client = Client;
|
|
|
|
// Try to send the next queued command, if any
|
|
Client.prototype.sendNextCommand = function() {
|
|
if (this.currentCommand) {
|
|
// There's a command pending response, don't send this new one yet
|
|
return;
|
|
}
|
|
|
|
this.currentCommand = this.commands.shift();
|
|
if (!this.currentCommand) {
|
|
// No further commands are queued
|
|
return;
|
|
}
|
|
|
|
this.socket.write(bser.dumpToBuffer(this.currentCommand.cmd));
|
|
}
|
|
|
|
Client.prototype.cancelCommands = function(why) {
|
|
var error = new Error(why);
|
|
|
|
// Steal all pending commands before we start cancellation, in
|
|
// case something decides to schedule more commands
|
|
var cmds = this.commands;
|
|
this.commands = [];
|
|
|
|
if (this.currentCommand) {
|
|
cmds.unshift(this.currentCommand);
|
|
this.currentCommand = null;
|
|
}
|
|
|
|
// Synthesize an error condition for any commands that were queued
|
|
cmds.forEach(function(cmd) {
|
|
cmd.cb(error);
|
|
});
|
|
}
|
|
|
|
Client.prototype.connect = function() {
|
|
var self = this;
|
|
|
|
function makeSock(sockname) {
|
|
// bunser will decode the watchman BSER protocol for us
|
|
self.bunser = new bser.BunserBuf();
|
|
// For each decoded line:
|
|
self.bunser.on('value', function(obj) {
|
|
// Figure out if this is a unliteral response or if it is the
|
|
// response portion of a request-response sequence. At the time
|
|
// of writing, there are only two possible unilateral responses.
|
|
var unilateral = false;
|
|
for (var i = 0; i < unilateralTags.length; i++) {
|
|
var tag = unilateralTags[i];
|
|
if (tag in obj) {
|
|
unilateral = tag;
|
|
}
|
|
}
|
|
|
|
if (unilateral) {
|
|
self.emit(unilateral, obj);
|
|
} else if (self.currentCommand) {
|
|
var cmd = self.currentCommand;
|
|
self.currentCommand = null;
|
|
if ('error' in obj) {
|
|
var error = new Error(obj.error);
|
|
error.watchmanResponse = obj;
|
|
cmd.cb(error);
|
|
} else {
|
|
cmd.cb(null, obj);
|
|
}
|
|
}
|
|
|
|
// See if we can dispatch the next queued command, if any
|
|
self.sendNextCommand();
|
|
});
|
|
self.bunser.on('error', function(err) {
|
|
self.emit('error', err);
|
|
});
|
|
|
|
self.socket = net.createConnection(sockname);
|
|
self.socket.on('connect', function() {
|
|
self.connecting = false;
|
|
self.emit('connect');
|
|
self.sendNextCommand();
|
|
});
|
|
self.socket.on('error', function(err) {
|
|
self.connecting = false;
|
|
self.emit('error', err);
|
|
});
|
|
self.socket.on('data', function(buf) {
|
|
self.bunser.append(buf);
|
|
});
|
|
self.socket.on('end', function() {
|
|
self.socket = null;
|
|
self.bunser = null;
|
|
self.cancelCommands('The watchman connection was closed');
|
|
self.emit('end');
|
|
});
|
|
}
|
|
|
|
// triggers will export the sock path to the environment.
|
|
// If we're invoked in such a way, we can simply pick up the
|
|
// definition from the environment and avoid having to fork off
|
|
// a process to figure it out
|
|
if (process.env.WATCHMAN_SOCK) {
|
|
makeSock(process.env.WATCHMAN_SOCK);
|
|
return;
|
|
}
|
|
|
|
// We need to ask the client binary where to find it.
|
|
// This will cause the service to start for us if it isn't
|
|
// already running.
|
|
var watchmanCommand = this.watchmanBinaryPath + ' get-sockname';
|
|
childProcess.exec(watchmanCommand,
|
|
function(error, stdout, stderr) {
|
|
if (error) {
|
|
self.emit('error', error);
|
|
return;
|
|
}
|
|
try {
|
|
var obj = JSON.parse(stdout);
|
|
if ('error' in obj) {
|
|
error = new Error(obj.error);
|
|
error.watchmanResponse = obj;
|
|
self.emit('error', error);
|
|
return;
|
|
}
|
|
makeSock(obj.sockname);
|
|
} catch (e) {
|
|
self.emit('error', e);
|
|
}
|
|
});
|
|
}
|
|
|
|
Client.prototype.command = function(args, done) {
|
|
done = done || function() {};
|
|
|
|
// Queue up the command
|
|
this.commands.push({cmd: args, cb: done});
|
|
|
|
// Establish a connection if we don't already have one
|
|
if (!this.socket) {
|
|
if (!this.connecting) {
|
|
this.connecting = true;
|
|
this.connect();
|
|
return;
|
|
}
|
|
return;
|
|
}
|
|
|
|
// If we're already connected and idle, try sending the command immediately
|
|
this.sendNextCommand();
|
|
}
|
|
|
|
// Close the connection to the service
|
|
Client.prototype.end = function() {
|
|
this.cancelCommands('The client was ended');
|
|
if (this.socket) {
|
|
this.socket.end();
|
|
this.socket = null;
|
|
}
|
|
this.bunser = null;
|
|
}
|