This repository has been archived by the owner on May 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 566
/
namedpipeinputchannel.js
82 lines (67 loc) · 2.01 KB
/
namedpipeinputchannel.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
/**
* Copyright (c) Microsoft. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Module dependencies.
var net = require('net');
var xml2js = require('xml2js');
function NamedPipeInputChannel() {
this.closeOnRead = true;
this.client = null;
}
NamedPipeInputChannel.prototype.readInputChannel = function (name, parseXml, callback) {
var callbackError = function (error) {
error.fileName = name;
callback(error);
};
this._readData(name, function (error, data) {
if (error) {
return callbackError(error);
}
var parts = data.toString().split('\r\n');
var xml;
// If there is more than one part (and the \r\n wasnt the final marker).
if (parts.length > 1 && parts[1].length > 0) {
xml = parts[1];
} else {
xml = parts[0];
}
if (parseXml) {
var parser = new xml2js.Parser();
error = null;
parser.on('error', function (e) { error = e; });
parser.parseString(xml);
if (parser.resultObject) {
xml = parser.resultObject;
}
}
callback(error, xml);
});
};
NamedPipeInputChannel.prototype.end = function () {
this.client.end();
};
NamedPipeInputChannel.prototype._readData = function (name, callback) {
var self = this;
self.client = net.connect(name);
self.client.on('data', function (data) {
if (self.closeOnRead) {
self.client.end();
}
callback(undefined, data);
});
self.client.on('error', function (error) {
callback(error);
});
};
module.exports = NamedPipeInputChannel;