-
Notifications
You must be signed in to change notification settings - Fork 18
/
client.js
180 lines (171 loc) · 5.13 KB
/
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Watch and sync a local folder with a remote one.
// All the local operations will be repeated on the remote.
//
// This this the local watcher.
var cs, isDir, kit, send;
kit = require('nokit');
cs = kit.require('brush');
isDir = function(path) {
return path.slice(-1) === kit.path.sep;
};
module.exports = function(conf, watch) {
var push, watchHandler;
if (watch == null) {
watch = true;
}
kit._.defaults(conf, require('./config.default'));
process.env.pollingWatch = conf.pollingInterval;
watchHandler = function(type, path, oldPath, stats) {
if (stats.isDirectory())
path = path + kit.path.sep;
var remotePath;
kit.log(cs.cyan(type) + ': ' + path + (oldPath ? cs.cyan(' <- ') + oldPath : ''));
remotePath = kit.path.join(conf.remoteDir, conf._useFilename ? kit.path.basename(path) : kit.path.relative(conf.localDir, path), isDir(path) ? '/' : '');
return send({
conf: conf,
path: path,
type: type,
remotePath: remotePath,
oldPath: oldPath,
stats: stats
}).then(function() {
var ref;
return (ref = conf.onChange) != null ? ref.call(0, type, path, oldPath, stats) : void 0;
})["catch"](function(err) {
return kit.log(cs.red(err.stack));
});
};
push = function(path, stats) {
return watchHandler('create', path, null, stats);
};
if (watch) {
return kit.watchDir(conf.localDir, {
patterns: conf.pattern,
handler: watchHandler
}).then(function(list) {
return kit.log(cs.cyan('Watched: ') + kit._.keys(list).length);
})["catch"](function(err) {
return kit.log(cs.red(err.stack));
});
} else {
conf.glob = conf.localDir;
return kit.lstat(conf.localDir).then(function(stat) {
if (stat.isDirectory()) {
if (kit._.isString(conf.pattern)) {
conf.pattern = [conf.pattern];
}
return conf.glob = conf.pattern.map(function(p) {
if (p[0] === '!') {
return '!' + kit.path.join(conf.localDir, p.slice(1));
} else {
return kit.path.join(conf.localDir, p);
}
});
} else {
return conf._useFilename = true;
}
}, function(err) {
conf._useFilename = true;
return kit.Promise.resolve(err);
}).then(function() {
return kit.glob(conf.glob, {
all: true,
iter: function(info) {
if (!info.isDir) {
return push(info.path, info.stats);
}
}
});
});
}
};
/**
* Send single request.
* @param {Object} opts Defaults:
* ```js
* { conf, path, type, remotePath, oldPath, stats }
* ```
* @return {Promise}
*/
module.exports.send = send = function(opts) {
var PassThrough, cipher, conf, crypto, data, encodeInfo, operationInfo, p, rdata;
conf = opts.conf;
if (opts.isPipeToStdout == null) {
opts.isPipeToStdout = true;
}
encodeInfo = function(info) {
var str;
str = kit._.isString(info) ? info : JSON.stringify(info);
if (conf.password) {
return kit.encrypt(str, conf.password, conf.algorithm).toString('hex');
} else {
return encodeURIComponent(str);
}
};
operationInfo = {
type: opts.type,
path: opts.remotePath
};
rdata = {
url: "http://" + conf.host + ":" + conf.port + "/",
method: 'POST',
body: false
};
p = kit.Promise.resolve();
switch (opts.type) {
case 'create':
case 'modify':
if (!isDir(opts.path)) {
if (opts.stats) {
operationInfo.mode = opts.stats.mode;
}
rdata.reqPipe = kit.createReadStream(opts.path);
crypto = kit.require('crypto', __dirname);
if (conf.password) {
cipher = crypto.createCipher('aes128', conf.password);
rdata.reqPipe = rdata.reqPipe.pipe(cipher);
}
}
break;
case 'move':
rdata.reqData = kit.path.join(conf.remoteDir, opts.oldPath.replace(conf.localDir, '').replace('/', ''));
if (conf.password) {
rdata.reqData = kit.encrypt(rdata.reqData, conf.password, conf.algorithm);
}
break;
case 'execute':
if (conf.password) {
rdata.reqData = kit.encrypt(opts.source, conf.password, conf.algorithm);
crypto = kit.require('crypto', __dirname);
rdata.resPipe = crypto.createDecipher(conf.algorithm, conf.password);
if (opts.isPipeToStdout) {
rdata.resPipe.pipe(process.stdout);
}
} else {
PassThrough = kit.require('stream', __dirname).PassThrough;
rdata.reqData = opts.source;
rdata.resPipe = new PassThrough;
}
data = new Buffer(0);
rdata.resPipe.on('data', function(chunk) {
if (!conf.password) {
process.stdout.write(chunk);
}
return data = Buffer.concat([data, chunk]);
});
rdata.url += encodeInfo(operationInfo);
return kit.request(rdata).then(function() {
return data;
});
}
return p = p.then(function() {
rdata.url += encodeInfo(operationInfo);
return kit.request(rdata);
}).then(function(res) {
if (res.statusCode === 200) {
return kit.log(cs.green('Synced: ') + opts.path);
} else {
return kit.log(res.body);
}
});
};