(
targetSocketPath: string,
message: UdsMessage,
opts: { authToken?: string } = {},
)
| 708 | * session wants to push a message to a peer's server). |
| 709 | */ |
| 710 | export async function sendUdsMessage( |
| 711 | targetSocketPath: string, |
| 712 | message: UdsMessage, |
| 713 | opts: { authToken?: string } = {}, |
| 714 | ): Promise<void> { |
| 715 | const { createConnection } = await import('net') |
| 716 | const token = opts.authToken ?? authToken |
| 717 | if (!token) { |
| 718 | throw new Error('Cannot send UDS message without auth token') |
| 719 | } |
| 720 | const outbound = withRequestAuthToken( |
| 721 | { |
| 722 | ...message, |
| 723 | from: message.from ?? socketPath ?? undefined, |
| 724 | ts: message.ts ?? new Date().toISOString(), |
| 725 | }, |
| 726 | token, |
| 727 | ) |
| 728 | |
| 729 | return new Promise<void>((resolve, reject) => { |
| 730 | let settled = false |
| 731 | let conn: ReturnType<typeof createConnection> |
| 732 | const finish = (error?: Error): void => { |
| 733 | if (settled) return |
| 734 | settled = true |
| 735 | if (error) { |
| 736 | conn.destroy(error) |
| 737 | reject(error) |
| 738 | } else { |
| 739 | conn.end() |
| 740 | resolve() |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | conn = createConnection(targetSocketPath, () => { |
| 745 | conn.write(jsonStringify(outbound) + '\n', err => { |
| 746 | if (err) finish(err) |
| 747 | }) |
| 748 | }) |
| 749 | attachUdsResponseReader(conn, { |
| 750 | maxFrameBytes: MAX_UDS_FRAME_BYTES, |
| 751 | acceptPong: true, |
| 752 | onSettled: finish, |
| 753 | }) |
| 754 | // Timeout so we don't hang on unreachable sockets |
| 755 | conn.setTimeout(5000, () => { |
| 756 | finish(new Error('Connection timed out')) |
| 757 | }) |
| 758 | }) |
| 759 | } |
no test coverage detected