2020-01-07 15:27:45 -08:00
|
|
|
/**
|
|
|
|
* Copyright 2018 Google Inc. All rights reserved.
|
|
|
|
* Modifications copyright (c) Microsoft Corporation.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { debugError, helper, RegisteredListener } from '../helper';
|
2020-03-27 15:18:34 -07:00
|
|
|
import { ConnectionTransport, ProtocolRequest, ProtocolResponse } from '../transport';
|
2020-01-07 15:27:45 -08:00
|
|
|
|
|
|
|
export class PipeTransport implements ConnectionTransport {
|
2020-01-13 13:33:25 -08:00
|
|
|
private _pipeWrite: NodeJS.WritableStream | null;
|
2020-01-07 15:27:45 -08:00
|
|
|
private _pendingMessage = '';
|
|
|
|
private _eventListeners: RegisteredListener[];
|
2020-04-01 14:42:47 -07:00
|
|
|
private _waitForNextTask = helper.makeWaitForNextTask();
|
2020-03-09 16:53:33 -07:00
|
|
|
private readonly _closeCallback: () => void;
|
|
|
|
|
2020-03-27 15:18:34 -07:00
|
|
|
onmessage?: (message: ProtocolResponse) => void;
|
2020-01-07 15:27:45 -08:00
|
|
|
onclose?: () => void;
|
|
|
|
|
2020-03-09 16:53:33 -07:00
|
|
|
constructor(pipeWrite: NodeJS.WritableStream, pipeRead: NodeJS.ReadableStream, closeCallback: () => void) {
|
2020-01-07 15:27:45 -08:00
|
|
|
this._pipeWrite = pipeWrite;
|
2020-03-09 16:53:33 -07:00
|
|
|
this._closeCallback = closeCallback;
|
2020-01-07 15:27:45 -08:00
|
|
|
this._eventListeners = [
|
|
|
|
helper.addEventListener(pipeRead, 'data', buffer => this._dispatch(buffer)),
|
|
|
|
helper.addEventListener(pipeRead, 'close', () => {
|
2020-03-09 16:53:33 -07:00
|
|
|
helper.removeEventListeners(this._eventListeners);
|
2020-01-07 15:27:45 -08:00
|
|
|
if (this.onclose)
|
|
|
|
this.onclose.call(null);
|
|
|
|
}),
|
|
|
|
helper.addEventListener(pipeRead, 'error', debugError),
|
|
|
|
helper.addEventListener(pipeWrite, 'error', debugError),
|
|
|
|
];
|
2020-01-13 13:33:25 -08:00
|
|
|
this.onmessage = undefined;
|
|
|
|
this.onclose = undefined;
|
2020-01-07 15:27:45 -08:00
|
|
|
}
|
|
|
|
|
2020-03-27 15:18:34 -07:00
|
|
|
send(message: ProtocolRequest) {
|
2020-03-26 23:30:55 -07:00
|
|
|
this._pipeWrite!.write(JSON.stringify(message));
|
2020-01-13 13:33:25 -08:00
|
|
|
this._pipeWrite!.write('\0');
|
2020-01-07 15:27:45 -08:00
|
|
|
}
|
|
|
|
|
2020-03-09 16:53:33 -07:00
|
|
|
close() {
|
|
|
|
this._closeCallback();
|
|
|
|
}
|
|
|
|
|
2020-01-07 15:27:45 -08:00
|
|
|
_dispatch(buffer: Buffer) {
|
|
|
|
let end = buffer.indexOf('\0');
|
|
|
|
if (end === -1) {
|
|
|
|
this._pendingMessage += buffer.toString();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const message = this._pendingMessage + buffer.toString(undefined, 0, end);
|
2020-02-06 14:14:46 -08:00
|
|
|
this._waitForNextTask(() => {
|
|
|
|
if (this.onmessage)
|
2020-03-26 23:30:55 -07:00
|
|
|
this.onmessage.call(null, JSON.parse(message));
|
2020-02-06 14:14:46 -08:00
|
|
|
});
|
2020-01-07 15:27:45 -08:00
|
|
|
|
|
|
|
let start = end + 1;
|
|
|
|
end = buffer.indexOf('\0', start);
|
|
|
|
while (end !== -1) {
|
2020-02-06 14:14:46 -08:00
|
|
|
const message = buffer.toString(undefined, start, end);
|
|
|
|
this._waitForNextTask(() => {
|
|
|
|
if (this.onmessage)
|
2020-03-26 23:30:55 -07:00
|
|
|
this.onmessage.call(null, JSON.parse(message));
|
2020-02-06 14:14:46 -08:00
|
|
|
});
|
2020-01-07 15:27:45 -08:00
|
|
|
start = end + 1;
|
|
|
|
end = buffer.indexOf('\0', start);
|
|
|
|
}
|
|
|
|
this._pendingMessage = buffer.toString(undefined, start);
|
|
|
|
}
|
|
|
|
}
|