|
| 1 | +// Copyright (c) 2026 RobotWebTools Contributors. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + |
| 9 | +'use strict'; |
| 10 | + |
| 11 | +const EventEmitter = require('events'); |
| 12 | + |
| 13 | +/** |
| 14 | + * @typedef {Object} CapabilityFrame |
| 15 | + * @property {string|number} [id] - Caller-assigned request id (echoed in reply). |
| 16 | + * @property {'call'|'publish'|'subscribe'|'unsubscribe'|'action'} [kind] - Request kind (C→S only). `'action'` is reserved and currently returns `code: 'not_implemented'`. |
| 17 | + * @property {string} [capability] - Capability name, e.g. `/cmd_vel`. |
| 18 | + * @property {*} [payload] - Message payload (call request, publish msg, sub event). |
| 19 | + * @property {string|number} [subId] - For unsubscribe: id of the original subscribe. |
| 20 | + * @property {boolean} [ok] - Reply success flag (S→C only). |
| 21 | + * @property {string} [event] - `'message'` for streamed subscription deliveries. |
| 22 | + * @property {string} [error] - Human-readable error message on failure. |
| 23 | + * @property {string} [code] - Stable machine-readable error code. |
| 24 | + */ |
| 25 | + |
| 26 | +/** |
| 27 | + * Per-connection abstraction handed to the runtime by a transport adapter. |
| 28 | + * |
| 29 | + * The transport owns the wire (WebSocket today; other adapters may follow) |
| 30 | + * and exposes each connection as a `Connection` so the runtime can stay |
| 31 | + * transport-agnostic. Concrete adapters subclass `Connection` and call |
| 32 | + * `this.emit('message', frame)` / `this.emit('close')` as frames arrive on |
| 33 | + * the wire, and implement `send(frame)` / `close(code, reason)` to push |
| 34 | + * frames back out. |
| 35 | + * |
| 36 | + * @experimental — the base class shape may grow (e.g. backpressure hooks) |
| 37 | + * and is not part of the SemVer-stable surface for third-party adapter |
| 38 | + * authors. The first-party `WebSocketTransport` is stable and recommended |
| 39 | + * for production use. |
| 40 | + * |
| 41 | + * @event Connection#message |
| 42 | + * @type {CapabilityFrame} |
| 43 | + * |
| 44 | + * @event Connection#close |
| 45 | + */ |
| 46 | +class Connection extends EventEmitter { |
| 47 | + /** |
| 48 | + * Push a frame to the remote peer. Implementations should silently drop |
| 49 | + * sends on a closed connection rather than throwing. |
| 50 | + * @param {CapabilityFrame} frame |
| 51 | + */ |
| 52 | + // eslint-disable-next-line no-unused-vars |
| 53 | + send(frame) { |
| 54 | + throw new Error('Connection.send() must be implemented by the transport'); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Close the connection. |
| 59 | + * @param {number} [code] |
| 60 | + * @param {string} [reason] |
| 61 | + */ |
| 62 | + // eslint-disable-next-line no-unused-vars |
| 63 | + close(code, reason) { |
| 64 | + throw new Error('Connection.close() must be implemented by the transport'); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +module.exports = { Connection }; |
0 commit comments