2020-01-08 00:27:45 +01: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.
|
|
|
|
|
*/
|
|
|
|
|
|
2022-04-06 23:57:14 +02:00
|
|
|
import type { ConnectionTransport, ProtocolRequest, ProtocolResponse } from './transport';
|
2022-04-07 22:55:44 +02:00
|
|
|
import { makeWaitForNextTask } from '../utils';
|
2022-04-07 23:36:13 +02:00
|
|
|
import { debugLogger } from '../common/debugLogger';
|
2020-01-08 00:27:45 +01:00
|
|
|
|
|
|
|
|
export class PipeTransport implements ConnectionTransport {
|
2022-06-30 19:58:22 +02:00
|
|
|
private _pipeRead: NodeJS.ReadableStream;
|
2020-05-12 00:00:13 +02:00
|
|
|
private _pipeWrite: NodeJS.WritableStream;
|
2022-08-18 16:53:56 +02:00
|
|
|
private _pendingBuffers: Buffer[] = [];
|
2020-08-22 16:07:13 +02:00
|
|
|
private _waitForNextTask = makeWaitForNextTask();
|
2020-05-12 00:00:13 +02:00
|
|
|
private _closed = false;
|
2022-06-30 19:58:22 +02:00
|
|
|
private _onclose?: () => void;
|
2020-03-10 00:53:33 +01:00
|
|
|
|
2020-03-27 23:18:34 +01:00
|
|
|
onmessage?: (message: ProtocolResponse) => void;
|
2020-01-08 00:27:45 +01:00
|
|
|
|
2020-08-17 23:12:31 +02:00
|
|
|
constructor(pipeWrite: NodeJS.WritableStream, pipeRead: NodeJS.ReadableStream) {
|
2022-06-30 19:58:22 +02:00
|
|
|
this._pipeRead = pipeRead;
|
2020-01-08 00:27:45 +01:00
|
|
|
this._pipeWrite = pipeWrite;
|
2020-10-27 19:09:41 +01:00
|
|
|
pipeRead.on('data', buffer => this._dispatch(buffer));
|
|
|
|
|
pipeRead.on('close', () => {
|
|
|
|
|
this._closed = true;
|
2022-06-30 19:58:22 +02:00
|
|
|
if (this._onclose)
|
|
|
|
|
this._onclose.call(null);
|
2020-12-29 18:59:35 +01:00
|
|
|
});
|
|
|
|
|
pipeRead.on('error', e => debugLogger.log('error', e));
|
|
|
|
|
pipeWrite.on('error', e => debugLogger.log('error', e));
|
2020-01-13 22:33:25 +01:00
|
|
|
this.onmessage = undefined;
|
2022-06-30 19:58:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get onclose() {
|
|
|
|
|
return this._onclose;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set onclose(onclose: undefined | (() => void)) {
|
|
|
|
|
this._onclose = onclose;
|
|
|
|
|
if (onclose && !this._pipeRead.readable)
|
|
|
|
|
onclose();
|
2020-01-08 00:27:45 +01:00
|
|
|
}
|
|
|
|
|
|
2020-03-27 23:18:34 +01:00
|
|
|
send(message: ProtocolRequest) {
|
2020-05-12 00:00:13 +02:00
|
|
|
if (this._closed)
|
|
|
|
|
throw new Error('Pipe has been closed');
|
|
|
|
|
this._pipeWrite.write(JSON.stringify(message));
|
|
|
|
|
this._pipeWrite.write('\0');
|
2020-01-08 00:27:45 +01:00
|
|
|
}
|
|
|
|
|
|
2020-03-10 00:53:33 +01:00
|
|
|
close() {
|
2020-04-03 01:57:12 +02:00
|
|
|
throw new Error('unimplemented');
|
2020-03-10 00:53:33 +01:00
|
|
|
}
|
|
|
|
|
|
2020-01-08 00:27:45 +01:00
|
|
|
_dispatch(buffer: Buffer) {
|
|
|
|
|
let end = buffer.indexOf('\0');
|
|
|
|
|
if (end === -1) {
|
2022-08-18 16:53:56 +02:00
|
|
|
this._pendingBuffers.push(buffer);
|
2020-01-08 00:27:45 +01:00
|
|
|
return;
|
|
|
|
|
}
|
2022-08-18 16:53:56 +02:00
|
|
|
this._pendingBuffers.push(buffer.slice(0, end));
|
|
|
|
|
const message = Buffer.concat(this._pendingBuffers).toString();
|
2020-02-06 23:14:46 +01:00
|
|
|
this._waitForNextTask(() => {
|
|
|
|
|
if (this.onmessage)
|
2020-03-27 07:30:55 +01:00
|
|
|
this.onmessage.call(null, JSON.parse(message));
|
2020-02-06 23:14:46 +01:00
|
|
|
});
|
2020-01-08 00:27:45 +01:00
|
|
|
|
|
|
|
|
let start = end + 1;
|
|
|
|
|
end = buffer.indexOf('\0', start);
|
|
|
|
|
while (end !== -1) {
|
2020-02-06 23:14:46 +01:00
|
|
|
const message = buffer.toString(undefined, start, end);
|
|
|
|
|
this._waitForNextTask(() => {
|
|
|
|
|
if (this.onmessage)
|
2020-03-27 07:30:55 +01:00
|
|
|
this.onmessage.call(null, JSON.parse(message));
|
2020-02-06 23:14:46 +01:00
|
|
|
});
|
2020-01-08 00:27:45 +01:00
|
|
|
start = end + 1;
|
|
|
|
|
end = buffer.indexOf('\0', start);
|
|
|
|
|
}
|
2022-08-18 16:53:56 +02:00
|
|
|
this._pendingBuffers = [buffer.slice(start)];
|
2020-01-08 00:27:45 +01:00
|
|
|
}
|
|
|
|
|
}
|