vscode-jsonrpc
Version:
A json rpc implementation over streams
108 lines • 4.3 kB
JavaScript
/* --------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */
'use strict';
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var events_1 = require('./events');
var is = require('./is');
var ContentLength = 'Content-Length: ';
var CRLF = '\r\n';
var AbstractMessageWriter = (function () {
function AbstractMessageWriter() {
this.errorEmitter = new events_1.Emitter();
this.closeEmitter = new events_1.Emitter();
}
Object.defineProperty(AbstractMessageWriter.prototype, "onError", {
get: function () {
return this.errorEmitter.event;
},
enumerable: true,
configurable: true
});
AbstractMessageWriter.prototype.fireError = function (error, message, count) {
this.errorEmitter.fire([this.asError(error), message, count]);
};
Object.defineProperty(AbstractMessageWriter.prototype, "onClose", {
get: function () {
return this.closeEmitter.event;
},
enumerable: true,
configurable: true
});
AbstractMessageWriter.prototype.fireClose = function () {
this.closeEmitter.fire(undefined);
};
AbstractMessageWriter.prototype.asError = function (error) {
if (error instanceof Error) {
return error;
}
else {
return new Error("Writer recevied error. Reason: " + (is.string(error.message) ? error.message : 'unknown'));
}
};
return AbstractMessageWriter;
}());
exports.AbstractMessageWriter = AbstractMessageWriter;
var StreamMessageWriter = (function (_super) {
__extends(StreamMessageWriter, _super);
function StreamMessageWriter(writable, encoding) {
var _this = this;
if (encoding === void 0) { encoding = 'utf8'; }
_super.call(this);
this.writable = writable;
this.encoding = encoding;
this.errorCount = 0;
this.writable.on('error', function (error) { return _this.fireError(error); });
this.writable.on('close', function () { return _this.fireClose(); });
}
StreamMessageWriter.prototype.write = function (msg) {
var json = JSON.stringify(msg);
var contentLength = Buffer.byteLength(json, this.encoding);
var headers = [
ContentLength, contentLength.toString(), CRLF,
CRLF
];
try {
// Header must be written in ASCII encoding
this.writable.write(headers.join(''), 'ascii');
// Now write the content. This can be written in any encoding
this.writable.write(json, this.encoding);
this.errorCount = 0;
}
catch (error) {
this.errorCount++;
this.fireError(error, msg, this.errorCount);
}
};
return StreamMessageWriter;
}(AbstractMessageWriter));
exports.StreamMessageWriter = StreamMessageWriter;
var IPCMessageWriter = (function (_super) {
__extends(IPCMessageWriter, _super);
function IPCMessageWriter(process) {
var _this = this;
_super.call(this);
this.process = process;
this.errorCount = 0;
this.process.on('error', function (error) { return _this.fireError(error); });
this.process.on('close', function () { return _this.fireClose; });
}
IPCMessageWriter.prototype.write = function (msg) {
try {
this.process.send(msg);
this.errorCount = 0;
}
catch (error) {
this.errorCount++;
this.fireError(error, msg, this.errorCount);
}
};
return IPCMessageWriter;
}(AbstractMessageWriter));
exports.IPCMessageWriter = IPCMessageWriter;
//# sourceMappingURL=messageWriter.js.map