index.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /* Copyright 2014-present Facebook, Inc.
  2. * Licensed under the Apache License, Version 2.0 */
  3. 'use strict';
  4. var net = require('net');
  5. var EE = require('events').EventEmitter;
  6. var util = require('util');
  7. var childProcess = require('child_process');
  8. var bser = require('bser');
  9. // We'll emit the responses to these when they get sent down to us
  10. var unilateralTags = ['subscription', 'log'];
  11. /**
  12. * @param options An object with the following optional keys:
  13. * * 'watchmanBinaryPath' (string) Absolute path to the watchman binary.
  14. * If not provided, the Client locates the binary using the PATH specified
  15. * by the node child_process's default env.
  16. */
  17. function Client(options) {
  18. var self = this;
  19. EE.call(this);
  20. this.watchmanBinaryPath = 'watchman';
  21. if (options && options.watchmanBinaryPath) {
  22. this.watchmanBinaryPath = options.watchmanBinaryPath.trim();
  23. };
  24. this.commands = [];
  25. }
  26. util.inherits(Client, EE);
  27. module.exports.Client = Client;
  28. // Try to send the next queued command, if any
  29. Client.prototype.sendNextCommand = function() {
  30. if (this.currentCommand) {
  31. // There's a command pending response, don't send this new one yet
  32. return;
  33. }
  34. this.currentCommand = this.commands.shift();
  35. if (!this.currentCommand) {
  36. // No further commands are queued
  37. return;
  38. }
  39. this.socket.write(bser.dumpToBuffer(this.currentCommand.cmd));
  40. }
  41. Client.prototype.cancelCommands = function(why) {
  42. var error = new Error(why);
  43. // Steal all pending commands before we start cancellation, in
  44. // case something decides to schedule more commands
  45. var cmds = this.commands;
  46. this.commands = [];
  47. if (this.currentCommand) {
  48. cmds.unshift(this.currentCommand);
  49. this.currentCommand = null;
  50. }
  51. // Synthesize an error condition for any commands that were queued
  52. cmds.forEach(function(cmd) {
  53. cmd.cb(error);
  54. });
  55. }
  56. Client.prototype.connect = function() {
  57. var self = this;
  58. function makeSock(sockname) {
  59. // bunser will decode the watchman BSER protocol for us
  60. self.bunser = new bser.BunserBuf();
  61. // For each decoded line:
  62. self.bunser.on('value', function(obj) {
  63. // Figure out if this is a unliteral response or if it is the
  64. // response portion of a request-response sequence. At the time
  65. // of writing, there are only two possible unilateral responses.
  66. var unilateral = false;
  67. for (var i = 0; i < unilateralTags.length; i++) {
  68. var tag = unilateralTags[i];
  69. if (tag in obj) {
  70. unilateral = tag;
  71. }
  72. }
  73. if (unilateral) {
  74. self.emit(unilateral, obj);
  75. } else if (self.currentCommand) {
  76. var cmd = self.currentCommand;
  77. self.currentCommand = null;
  78. if ('error' in obj) {
  79. var error = new Error(obj.error);
  80. error.watchmanResponse = obj;
  81. cmd.cb(error);
  82. } else {
  83. cmd.cb(null, obj);
  84. }
  85. }
  86. // See if we can dispatch the next queued command, if any
  87. self.sendNextCommand();
  88. });
  89. self.bunser.on('error', function(err) {
  90. self.emit('error', err);
  91. });
  92. self.socket = net.createConnection(sockname);
  93. self.socket.on('connect', function() {
  94. self.connecting = false;
  95. self.emit('connect');
  96. self.sendNextCommand();
  97. });
  98. self.socket.on('error', function(err) {
  99. self.connecting = false;
  100. self.emit('error', err);
  101. });
  102. self.socket.on('data', function(buf) {
  103. if (self.bunser) {
  104. self.bunser.append(buf);
  105. }
  106. });
  107. self.socket.on('end', function() {
  108. self.socket = null;
  109. self.bunser = null;
  110. self.cancelCommands('The watchman connection was closed');
  111. self.emit('end');
  112. });
  113. }
  114. // triggers will export the sock path to the environment.
  115. // If we're invoked in such a way, we can simply pick up the
  116. // definition from the environment and avoid having to fork off
  117. // a process to figure it out
  118. if (process.env.WATCHMAN_SOCK) {
  119. makeSock(process.env.WATCHMAN_SOCK);
  120. return;
  121. }
  122. // We need to ask the client binary where to find it.
  123. // This will cause the service to start for us if it isn't
  124. // already running.
  125. var args = ['--no-pretty', 'get-sockname'];
  126. // We use the more elaborate spawn rather than exec because there
  127. // are some error cases on Windows where process spawning can hang.
  128. // It is desirable to pipe stderr directly to stderr live so that
  129. // we can discover the problem.
  130. var proc = null;
  131. var spawnFailed = false;
  132. function spawnError(error) {
  133. if (spawnFailed) {
  134. // For ENOENT, proc 'close' will also trigger with a negative code,
  135. // let's suppress that second error.
  136. return;
  137. }
  138. spawnFailed = true;
  139. if (error.errno === 'EACCES') {
  140. error.message = 'The Watchman CLI is installed but cannot ' +
  141. 'be spawned because of a permission problem';
  142. } else if (error.errno === 'ENOENT') {
  143. error.message = 'Watchman was not found in PATH. See ' +
  144. 'https://facebook.github.io/watchman/docs/install.html ' +
  145. 'for installation instructions';
  146. }
  147. console.error('Watchman: ', error.message);
  148. self.emit('error', error);
  149. }
  150. try {
  151. proc = childProcess.spawn(this.watchmanBinaryPath, args, {
  152. stdio: ['ignore', 'pipe', 'pipe']
  153. });
  154. } catch (error) {
  155. spawnError(error);
  156. return;
  157. }
  158. var stdout = [];
  159. var stderr = [];
  160. proc.stdout.on('data', function(data) {
  161. stdout.push(data);
  162. });
  163. proc.stderr.on('data', function(data) {
  164. data = data.toString('utf8');
  165. stderr.push(data);
  166. console.error(data);
  167. });
  168. proc.on('error', function(error) {
  169. spawnError(error);
  170. });
  171. proc.on('close', function (code, signal) {
  172. if (code !== 0) {
  173. spawnError(new Error(
  174. self.watchmanBinaryPath + ' ' + args.join(' ') +
  175. ' returned with exit code=' + code + ', signal=' +
  176. signal + ', stderr= ' + stderr.join('')));
  177. return;
  178. }
  179. try {
  180. var obj = JSON.parse(stdout.join(''));
  181. if ('error' in obj) {
  182. var error = new Error(obj.error);
  183. error.watchmanResponse = obj;
  184. self.emit('error', error);
  185. return;
  186. }
  187. makeSock(obj.sockname);
  188. } catch (e) {
  189. self.emit('error', e);
  190. }
  191. });
  192. }
  193. Client.prototype.command = function(args, done) {
  194. done = done || function() {};
  195. // Queue up the command
  196. this.commands.push({cmd: args, cb: done});
  197. // Establish a connection if we don't already have one
  198. if (!this.socket) {
  199. if (!this.connecting) {
  200. this.connecting = true;
  201. this.connect();
  202. return;
  203. }
  204. return;
  205. }
  206. // If we're already connected and idle, try sending the command immediately
  207. this.sendNextCommand();
  208. }
  209. var cap_versions = {
  210. "cmd-watch-del-all": "3.1.1",
  211. "cmd-watch-project": "3.1",
  212. "relative_root": "3.3",
  213. "term-dirname": "3.1",
  214. "term-idirname": "3.1",
  215. "wildmatch": "3.7",
  216. }
  217. // Compares a vs b, returns < 0 if a < b, > 0 if b > b, 0 if a == b
  218. function vers_compare(a, b) {
  219. a = a.split('.');
  220. b = b.split('.');
  221. for (var i = 0; i < 3; i++) {
  222. var d = parseInt(a[i] || '0') - parseInt(b[i] || '0');
  223. if (d != 0) {
  224. return d;
  225. }
  226. }
  227. return 0; // Equal
  228. }
  229. function have_cap(vers, name) {
  230. if (name in cap_versions) {
  231. return vers_compare(vers, cap_versions[name]) >= 0;
  232. }
  233. return false;
  234. }
  235. // This is a helper that we expose for testing purposes
  236. Client.prototype._synthesizeCapabilityCheck = function(
  237. resp, optional, required) {
  238. resp.capabilities = {}
  239. var version = resp.version;
  240. optional.forEach(function (name) {
  241. resp.capabilities[name] = have_cap(version, name);
  242. });
  243. required.forEach(function (name) {
  244. var have = have_cap(version, name);
  245. resp.capabilities[name] = have;
  246. if (!have) {
  247. resp.error = 'client required capability `' + name +
  248. '` is not supported by this server';
  249. }
  250. });
  251. return resp;
  252. }
  253. Client.prototype.capabilityCheck = function(caps, done) {
  254. var optional = caps.optional || [];
  255. var required = caps.required || [];
  256. var self = this;
  257. this.command(['version', {
  258. optional: optional,
  259. required: required
  260. }], function (error, resp) {
  261. if (error) {
  262. done(error);
  263. return;
  264. }
  265. if (!('capabilities' in resp)) {
  266. // Server doesn't support capabilities, so we need to
  267. // synthesize the results based on the version
  268. resp = self._synthesizeCapabilityCheck(resp, optional, required);
  269. if (resp.error) {
  270. error = new Error(resp.error);
  271. error.watchmanResponse = resp;
  272. done(error);
  273. return;
  274. }
  275. }
  276. done(null, resp);
  277. });
  278. }
  279. // Close the connection to the service
  280. Client.prototype.end = function() {
  281. this.cancelCommands('The client was ended');
  282. if (this.socket) {
  283. this.socket.end();
  284. this.socket = null;
  285. }
  286. this.bunser = null;
  287. }