Socket
Socket
Sign inDemoInstall

fb-watchman

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fb-watchman - npm Package Compare versions

Comparing version 0.0.1 to 1.0.0

bser.js

22

index.js
/* Copyright 2014-present Facebook, Inc.
* Licensed under the Apache License, Version 2.0 */
var nextback = require('nextback');
var net = require('net');
var EE = require('events').EventEmitter;
var util = require('util');
var JSONStream = require('json-stream');
var childProcess = require('child_process');
var bser = require('./bser');

@@ -47,3 +46,3 @@ // We'll emit the responses to these when they get sent down to us

this.socket.write(JSON.stringify(this.currentCommand.cmd) + '\n');
this.socket.write(bser.dumpToBuffer(this.currentCommand.cmd));
}

@@ -74,6 +73,6 @@

function makeSock(sockname) {
// jstream will decode the JSON line protocol for us
self.jstream = new JSONStream();
// bunser will decode the watchman BSER protocol for us
self.bunser = new bser.BunserBuf();
// For each decoded line:
self.jstream.on('data', function(obj) {
self.bunser.on('value', function(obj) {
// Figure out if this is a unliteral response or if it is the

@@ -107,2 +106,5 @@ // response portion of a request-response sequence. At the time

});
self.bunser.on('error', function(err) {
self.emit('error', err);
});

@@ -119,6 +121,8 @@ self.socket = net.createConnection(sockname);

});
self.socket.pipe(self.jstream);
self.socket.on('data', function(buf) {
self.bunser.append(buf);
});
self.socket.on('end', function() {
self.socket = null;
self.jstream = null;
self.bunser = null;
self.cancelCommands('The watchman connection was closed');

@@ -190,3 +194,3 @@ self.emit('end');

}
this.jstream = null;
this.bunser = null;
}
{
"name": "fb-watchman",
"version": "0.0.1",
"version": "1.0.0",
"description": "Bindings for the Watchman file watching service",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "node test/bser.js"
},

@@ -14,4 +14,2 @@ "repository": {

"dependencies": {
"json-stream": "0.2.2",
"nextback": "~0.1.0"
},

@@ -38,4 +36,5 @@ "keywords": [

"files": [
"index.js"
"index.js",
"bser.js"
]
}

@@ -26,5 +26,5 @@ # fb-watchman

- Each watched directory is called a `root`.
- You must initiate a `watch` on a `root` using the `watch` command prior to subscribing to changes
- Rather than watching many sibling directories, prefer to watch a common root directory.
- change notifications are relative to the `root`
- You must initiate a `watch` on a `root` using the `watch-project` command prior to subscribing to changes
- Rather than separately watching many sibling directories, `watch-project` consolidates and re-uses existing watches relative to a project root (the location of your `.watchmanconfig` or source control repository root)
- change notifications are relative to the project root

@@ -64,3 +64,3 @@ ## Usage

// error; Watchman will re-use an existing watch.
client.command(['watch', process.cwd()], function(error, resp) {
client.command(['watch-project', process.cwd()], function(error, resp) {
if (error) {

@@ -71,2 +71,4 @@ console.error('Error initiating watch:', error);

// It is considered to be best practice to show any 'warning' or 'error'
// information to the user, as it may suggest steps for remediation
if ('warning' in resp) {

@@ -82,11 +84,37 @@ console.log('warning: ', resp.warning);

// watch-project may re-use an existing watch at a higher level in the
// filesystem. It will tell us the relative path to the directory that
// we expressed interest in, so we need to adjust for it in our results
var path_prefix = '';
var root = resp['watch'];
if ('relative_path' in resp) {
path_prefix = resp['relative_path'];
console.log('(re)using project watch at ', root, ', our dir is relative: ',
path_prefix);
}
function get_relative_name(proj_rel) {
if (path_prefix.length == 0) {
return proj_rel;
}
if (proj_rel.substr(0, path_prefix.length) == path_prefix) {
return proj_rel.substr(path_prefix.length + 1);
}
return null;
}
// Subscribe to notifications about .js files
// https://facebook.github.io/watchman/docs/cmd/subscribe.html
client.command(['subscribe', process.cwd(), 'mysubscription', {
client.command(['subscribe', root, 'mysubscription', {
// Match any .js file under process.cwd()
// https://facebook.github.io/watchman/docs/file-query.html#expressions
// Has more on the supported expression syntax
expression: ["match", "*.js"],
expression: ["allof",
["match", "*.js"],
// focus on the relative path from the project to the path
// of interest
['dirname', path_prefix]
],
// Which fields we're interested in
fields: ["name", "size", "exists", "mode"]
fields: ["name", "size", "exists", "type"]
}],

@@ -104,2 +132,5 @@ function(error, resp) {

// Subscription results are emitted via the subscription event.
// Note that this emits for all subscriptions. If you have
// subscriptions with different `fields` you will need to check
// the subscription name and handle the differing data accordingly
client.on('subscription', function(resp) {

@@ -125,3 +156,15 @@ // Each entry in `resp.files` will have the fields you requested

// mode: 33188 } ] }
console.log(resp.root, resp.subscription, resp.files);
console.log(resp.root, resp.subscription);
for (var i in resp.files) {
var f = resp.files[i];
// Fixup name for watch-project offset
if (resp.subscription == 'mysubscription') {
// we requested a set of fields in this subscription
f.name = get_relative_name(f.name);
} else {
// the other subscription we set up returns only the name
f = get_relative_name(f);
}
console.log(f);
}
});

@@ -131,3 +174,3 @@

// current point in time
client.command(['clock', process.cwd()], function(error, resp) {
client.command(['clock', root], function(error, resp) {
if (error) {

@@ -138,4 +181,9 @@ console.error('Failed to query clock:', error);

client.command(['subscribe', process.cwd(), 'sincesub', {
expression: ["match", "*.js"],
client.command(['subscribe', root, 'sincesub', {
expression: ['allof',
["match", "*.js"],
// focus on the relative path from the project to the path
// of interest
['dirname', path_prefix]
],
// Note: since we only request a single field, the `sincesub` subscription

@@ -176,3 +224,3 @@ // response will just set files to an array of filenames, not an array of

```
client.command(['watch', process.cwd()], function(error, resp) {
client.command(['watch-project', process.cwd()], function(error, resp) {
if (error) {

@@ -185,3 +233,9 @@ console.log('watch failed: ', error);

}
console.log('watching ', resp.watch);
if ('relative_path' in resp) {
// We will need to remember and adjust for relative_path
console.log('watching project ', resp.watch, ' relative path to cwd is ',
resp.relative_path);
} else {
console.log('watching ', resp.watch);
}
});

@@ -188,0 +242,0 @@ ```

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc