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