provider-ipcsocket.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { connect } from "net";
  2. import { SocketProvider } from "./provider-socket.js";
  3. import type { Socket } from "net";
  4. import type { JsonRpcApiProviderOptions } from "./provider-jsonrpc.js";
  5. import type { Networkish } from "./network.js";
  6. // @TODO: Is this sufficient? Is this robust? Will newlines occur between
  7. // all payloads and only between payloads?
  8. function splitBuffer(data: Buffer): { messages: Array<string>, remaining: Buffer } {
  9. const messages: Array<string> = [ ];
  10. let lastStart = 0;
  11. while (true) {
  12. const nl = data.indexOf(10, lastStart);
  13. if (nl === -1) { break; }
  14. messages.push(data.subarray(lastStart, nl).toString().trim());
  15. lastStart = nl + 1;
  16. }
  17. return { messages, remaining: data.subarray(lastStart) };
  18. }
  19. /**
  20. * An **IpcSocketProvider** connects over an IPC socket on the host
  21. * which provides fast access to the node, but requires the node and
  22. * the script run on the same machine.
  23. */
  24. export class IpcSocketProvider extends SocketProvider {
  25. #socket: Socket;
  26. /**
  27. * The connected socket.
  28. */
  29. get socket(): Socket { return this.#socket; }
  30. constructor(path: string, network?: Networkish, options?: JsonRpcApiProviderOptions) {
  31. super(network, options);
  32. this.#socket = connect(path);
  33. this.socket.on("ready", async () => {
  34. try {
  35. await this._start();
  36. } catch (error) {
  37. console.log("failed to start IpcSocketProvider", error);
  38. // @TODO: Now what? Restart?
  39. }
  40. });
  41. let response = Buffer.alloc(0);
  42. this.socket.on("data", (data) => {
  43. response = Buffer.concat([ response, data ]);
  44. const { messages, remaining } = splitBuffer(response);
  45. messages.forEach((message) => {
  46. this._processMessage(message);
  47. });
  48. response = remaining;
  49. });
  50. this.socket.on("end", () => {
  51. this.emit("close");
  52. this.socket.destroy();
  53. this.socket.end();
  54. });
  55. }
  56. destroy(): void {
  57. this.socket.destroy();
  58. this.socket.end();
  59. super.destroy();
  60. }
  61. async _write(message: string): Promise<void> {
  62. if (!message.endsWith("\n")) { message += "\n"; }
  63. this.socket.write(message);
  64. }
  65. }