pipeTransport.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.PipeTransport = void 0;
  6. var _utils = require("../utils");
  7. var _debugLogger = require("../common/debugLogger");
  8. /**
  9. * Copyright 2018 Google Inc. All rights reserved.
  10. * Modifications copyright (c) Microsoft Corporation.
  11. *
  12. * Licensed under the Apache License, Version 2.0 (the "License");
  13. * you may not use this file except in compliance with the License.
  14. * You may obtain a copy of the License at
  15. *
  16. * http://www.apache.org/licenses/LICENSE-2.0
  17. *
  18. * Unless required by applicable law or agreed to in writing, software
  19. * distributed under the License is distributed on an "AS IS" BASIS,
  20. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. * See the License for the specific language governing permissions and
  22. * limitations under the License.
  23. */
  24. class PipeTransport {
  25. constructor(pipeWrite, pipeRead) {
  26. this._pipeRead = void 0;
  27. this._pipeWrite = void 0;
  28. this._pendingBuffers = [];
  29. this._waitForNextTask = (0, _utils.makeWaitForNextTask)();
  30. this._closed = false;
  31. this._onclose = void 0;
  32. this.onmessage = void 0;
  33. this._pipeRead = pipeRead;
  34. this._pipeWrite = pipeWrite;
  35. pipeRead.on('data', buffer => this._dispatch(buffer));
  36. pipeRead.on('close', () => {
  37. this._closed = true;
  38. if (this._onclose) this._onclose.call(null);
  39. });
  40. pipeRead.on('error', e => _debugLogger.debugLogger.log('error', e));
  41. pipeWrite.on('error', e => _debugLogger.debugLogger.log('error', e));
  42. this.onmessage = undefined;
  43. }
  44. get onclose() {
  45. return this._onclose;
  46. }
  47. set onclose(onclose) {
  48. this._onclose = onclose;
  49. if (onclose && !this._pipeRead.readable) onclose();
  50. }
  51. send(message) {
  52. if (this._closed) throw new Error('Pipe has been closed');
  53. this._pipeWrite.write(JSON.stringify(message));
  54. this._pipeWrite.write('\0');
  55. }
  56. close() {
  57. throw new Error('unimplemented');
  58. }
  59. _dispatch(buffer) {
  60. let end = buffer.indexOf('\0');
  61. if (end === -1) {
  62. this._pendingBuffers.push(buffer);
  63. return;
  64. }
  65. this._pendingBuffers.push(buffer.slice(0, end));
  66. const message = Buffer.concat(this._pendingBuffers).toString();
  67. this._waitForNextTask(() => {
  68. if (this.onmessage) this.onmessage.call(null, JSON.parse(message));
  69. });
  70. let start = end + 1;
  71. end = buffer.indexOf('\0', start);
  72. while (end !== -1) {
  73. const message = buffer.toString(undefined, start, end);
  74. this._waitForNextTask(() => {
  75. if (this.onmessage) this.onmessage.call(null, JSON.parse(message));
  76. });
  77. start = end + 1;
  78. end = buffer.indexOf('\0', start);
  79. }
  80. this._pendingBuffers = [buffer.slice(start)];
  81. }
  82. }
  83. exports.PipeTransport = PipeTransport;