| 1 |
= cockpit.js: Raw Channels |
| 2 |
|
| 3 |
At a low level Cockpit communicates with the system via messages passed |
| 4 |
through various channels. These are usually exposed via higher level |
| 5 |
APIs, such as the `+cockpit.spawn()+` function. It is rare to use raw |
| 6 |
channels directly. |
| 7 |
|
| 8 |
[[cockpit-channels-channel]] |
| 9 |
== cockpit.channel() |
| 10 |
|
| 11 |
.... |
| 12 |
channel = cockpit.channel(options) |
| 13 |
.... |
| 14 |
|
| 15 |
This function creates a new channel for communication with the system. |
| 16 |
It returns a new channel object. The `+options+` argument is a plain |
| 17 |
object. At least the `+"payload"+` option is required, and based on the |
| 18 |
payload type, other options may be required. |
| 19 |
|
| 20 |
`+"binary"+`:: |
| 21 |
Set to `+true+` to transfer binary payloads. Both messages sent via |
| 22 |
link:#cockpit-channels-send[`+channel.send()+`] and those received via |
| 23 |
link:#cockpit-channels-message[`+channel.onmessage+`] should be arrays |
| 24 |
of bytes, either `+Uint8Array+` or `+Array+` depending on browser |
| 25 |
support. |
| 26 |
`+"payload"+`:: |
| 27 |
The payload type for the channel. Only specific payload types are |
| 28 |
supported. |
| 29 |
`+"superuser"+`:: |
| 30 |
Set to `+"require"+` to open this channel as root. If the currently |
| 31 |
logged in user is not permitted to become root (eg: via `+pkexec+`) |
| 32 |
then the `+channel+` will immediately be |
| 33 |
link:#cockpit-channels-close-ev[closed] with a `+"access-denied"+` |
| 34 |
problem code. |
| 35 |
+ |
| 36 |
Set to `+"try"+` to try to open the channel as root, but if that |
| 37 |
fails, then fall back to an unprivileged channel. |
| 38 |
|
| 39 |
The channel object returned has the following fields and methods and |
| 40 |
events. You should call the `+channel.close()+` method when done with |
| 41 |
the channel. |
| 42 |
|
| 43 |
A valid channel will always be returned and the is ready to |
| 44 |
`+channel.send()+`. The channel may |
| 45 |
link:#cockpit-channels-close-ev[close shortly afterword] due to a |
| 46 |
failure. |
| 47 |
|
| 48 |
[[cockpit-channels-binary]] |
| 49 |
== channel.binary |
| 50 |
|
| 51 |
Will be `+true+` for an binary channel. Will be set to `+false+` if the |
| 52 |
channel is textual. |
| 53 |
|
| 54 |
[[cockpit-channels-options]] |
| 55 |
== channel.options |
| 56 |
|
| 57 |
The options used to open this channel. This should not be changed. |
| 58 |
|
| 59 |
[[cockpit-channels-valid]] |
| 60 |
== channel.valid |
| 61 |
|
| 62 |
Will be `+true+` for an open channel. Will be set to `+false+` if the |
| 63 |
channel closes. |
| 64 |
|
| 65 |
[[cockpit-channels-send]] |
| 66 |
== channel.send() |
| 67 |
|
| 68 |
.... |
| 69 |
channel.send(data) |
| 70 |
.... |
| 71 |
|
| 72 |
Send a message over the channel. The contents of the message depends on |
| 73 |
the payload type of the channel. If a binary channel, then `+data+` is |
| 74 |
expected to be an `+Array+` of bytes or a `+Uint8Array+`. If not binary, |
| 75 |
then the `+data+` will be converted to a string if not already a string. |
| 76 |
|
| 77 |
[[cockpit-channels-control]] |
| 78 |
== channel.control() |
| 79 |
|
| 80 |
.... |
| 81 |
channel.control(options) |
| 82 |
.... |
| 83 |
|
| 84 |
Notify the channel to tune certain parameters on the fly. The |
| 85 |
`+options+` is a plain javascript object, and the contents depend on the |
| 86 |
`+"payload"+` of the channel. |
| 87 |
|
| 88 |
One common operation is to set `+"command"+` to `+"done"+` in the |
| 89 |
options field. To indicate that no further messages will be sent through |
| 90 |
the channel. |
| 91 |
|
| 92 |
[[cockpit-channels-wait]] |
| 93 |
== channel.wait() |
| 94 |
|
| 95 |
.... |
| 96 |
promise = channel.wait([callback]) |
| 97 |
.... |
| 98 |
|
| 99 |
Returns a `+promise+` that is ready when the channel is ready, or fails |
| 100 |
if the client closes. If a `+callback+` is specified, it is attached to |
| 101 |
the promise. The promise will be rejected or resolved with the contents |
| 102 |
`+options+` passed to the |
| 103 |
link:#cockpit-channels-onready[channel.onready] and |
| 104 |
link:#cockpit-channels-close-ev[channel.onclose] events respectively. |
| 105 |
|
| 106 |
In general it's not necessary to wait for the channel before starting to |
| 107 |
use the channel. |
| 108 |
|
| 109 |
[[cockpit-channels-close]] |
| 110 |
== channel.close() |
| 111 |
|
| 112 |
.... |
| 113 |
channel.close([options]) |
| 114 |
.... |
| 115 |
|
| 116 |
Close the channel. |
| 117 |
|
| 118 |
If `+options+` is present it can be a plain javascript object containing |
| 119 |
additional channel close options to send to the peer. If closing for |
| 120 |
because of a problem, set the `+"problem"+` field to a |
| 121 |
xref:cockpit-error.adoc#cockpit-problems[problem code]. If `+options+` is not an object it |
| 122 |
will be treated as a `+"problem"+`. |
| 123 |
|
| 124 |
The link:#cockpit-channels-close-ev[close event] will fire. A channel |
| 125 |
can also be closed by a peer or if the underlying transport closes. |
| 126 |
|
| 127 |
[[cockpit-channels-message]] |
| 128 |
== channel.onmessage |
| 129 |
|
| 130 |
.... |
| 131 |
channel.addEventListener("message", function(event, data) { ... }) |
| 132 |
.... |
| 133 |
|
| 134 |
An event triggered when the channel receives a message. The message is |
| 135 |
passed as a string to the handler in the `+data+`. In the case of binary |
| 136 |
channels `+data+` is an `+Uint8Array+` or an `+Array+` of bytes if the |
| 137 |
former is not supported by the browser. The contents of the message |
| 138 |
depends on the payload type of the channel. |
| 139 |
|
| 140 |
[[cockpit-channels-oncontrol]] |
| 141 |
== channel.oncontrol |
| 142 |
|
| 143 |
.... |
| 144 |
channel.addEventListener("control", function(event, options) { ... }) |
| 145 |
.... |
| 146 |
|
| 147 |
An event triggered when the channel receives an control message in the |
| 148 |
middle of the flow. One particular use is when the `+command+` is set to |
| 149 |
`+"done"+` then no further messages will be received in the channel. The |
| 150 |
exact form of these messages depend on the `+"payload"+` of the channel. |
| 151 |
|
| 152 |
[[cockpit-channels-onready]] |
| 153 |
== channel.onready |
| 154 |
|
| 155 |
.... |
| 156 |
channel.addEventListener("ready", function(event, options) { ... }) |
| 157 |
.... |
| 158 |
|
| 159 |
An event triggered when the other end of the channel is ready to start |
| 160 |
processing messages. This indicates the channel is completely open. It |
| 161 |
is possible to start sending messages on the channel before this point. |
| 162 |
|
| 163 |
[[cockpit-channels-close-ev]] |
| 164 |
== channel.onclose |
| 165 |
|
| 166 |
.... |
| 167 |
channel.addEventListener("close", function(event, options) { ... }) |
| 168 |
.... |
| 169 |
|
| 170 |
An event triggered when the channel closes. This can happen either |
| 171 |
because link:#cockpit-channels-close[channel.close()] function was |
| 172 |
called, or if the peer closed the channel, or the underlying transport |
| 173 |
closes. |
| 174 |
|
| 175 |
The `+options+` will contain various close information, including a |
| 176 |
`+"problem"+` field which will be set if the channel was closed because |
| 177 |
of a problem. |
| 178 |
|
| 179 |
[[cockpit-transport-origin]] |
| 180 |
== cockpit.transport.origin |
| 181 |
|
| 182 |
.... |
| 183 |
cockpit.transport.origin |
| 184 |
.... |
| 185 |
|
| 186 |
The HTTP origin that is being used by the underlying channel transport. |
| 187 |
This is read-only, you should not assign a value. If the browser |
| 188 |
supports `+window.location.origin+` then this will be identical to that |
| 189 |
value. |
| 190 |
|
| 191 |
[[cockpit-transport-host]] |
| 192 |
== cockpit.transport.host |
| 193 |
|
| 194 |
.... |
| 195 |
cockpit.transport.host |
| 196 |
.... |
| 197 |
|
| 198 |
The host that this transport is going to talk to by default. This is |
| 199 |
read-only, you should not assign a value. If the value is null that |
| 200 |
means that the transport has not been setup yet. |
| 201 |
|
| 202 |
[[cockpit-transport-csrf-token]] |
| 203 |
== cockpit.transport.csrf_token |
| 204 |
|
| 205 |
.... |
| 206 |
cockpit.transport.csrf_token |
| 207 |
.... |
| 208 |
|
| 209 |
A cross site request forgery token for use with external channels. This |
| 210 |
becomes valid once the connection is properly established. |
| 211 |
|
| 212 |
[[cockpit-transport-options]] |
| 213 |
== cockpit.transport.options |
| 214 |
|
| 215 |
.... |
| 216 |
cockpit.transport.options |
| 217 |
.... |
| 218 |
|
| 219 |
Initialization options received over the underlying channel transport. |
| 220 |
These will be empty until connection is properly established. |
| 221 |
|
| 222 |
[[cockpit-transport-wait]] |
| 223 |
== cockpit.transport.wait() |
| 224 |
|
| 225 |
.... |
| 226 |
cockpit.transport.wait(callback) |
| 227 |
.... |
| 228 |
|
| 229 |
Call the `+callback+` function once the underlying channel transport is |
| 230 |
initialized. This will start the initialization if not already in |
| 231 |
progress or completed. If the channel transport is already initialized, |
| 232 |
then `+callback+` will be called immediately. |
| 233 |
|
| 234 |
In general it's not necessary to wait for the transport before starting |
| 235 |
to open channels. |
| 236 |
|
| 237 |
[[cockpit-transport-close]] |
| 238 |
== cockpit.transport.close() |
| 239 |
|
| 240 |
.... |
| 241 |
cockpit.transport.close([problem]) |
| 242 |
.... |
| 243 |
|
| 244 |
Close the underlying channel transport. All channels open channels will |
| 245 |
close. The `+problem+` argument should be a problem code string. If not |
| 246 |
specified it will default to `+"disconnected"+`. |
| 247 |
|
| 248 |
[[cockpit-transport-filter]] |
| 249 |
== cockpit.transport.filter() |
| 250 |
|
| 251 |
.... |
| 252 |
cockpit.transport.filter((message, channelid, control) => { ... }) |
| 253 |
.... |
| 254 |
|
| 255 |
Add a filter to the underlying channel transport. All incoming messages |
| 256 |
will be passed to each of the filter callbacks that are registered. |
| 257 |
|
| 258 |
This function is rarely used. |
| 259 |
|
| 260 |
Filter callbacks are called in the order they are registered. If a |
| 261 |
filter callback returns `+false+` then the message will not be |
| 262 |
dispatched further, whether to other filters, or to channels, etc. |
| 263 |
|
| 264 |
The `+message+` is the string or array with the raw message including, |
| 265 |
the framing. The `+channelid+` is the channel identifier or an empty |
| 266 |
string for control messages. If `+control+` is set then this is a |
| 267 |
control message,d and the `+control+` argument contains the parsed JSON |
| 268 |
object of the control message. |
| 269 |
|
| 270 |
[[cockpit-transport-inject]] |
| 271 |
== cockpit.transport.inject() |
| 272 |
|
| 273 |
.... |
| 274 |
cockpit.transport.inject(message, [out]) |
| 275 |
.... |
| 276 |
|
| 277 |
Inject a message into the underlying channel transport. The `+message+` |
| 278 |
should be a `+string+` or an array of bytes, and should be valid |
| 279 |
according to the Cockpit message protocol. If the `+out+` argument is |
| 280 |
equal to `+false+` then the message will be injected as an incoming |
| 281 |
message as if it was received on the underlying channel transport. |
| 282 |
|
| 283 |
This function is rarely used. In general you should only `+inject()+` |
| 284 |
messages you got from a `+filter()+`. |
| 285 |
|
| 286 |
[[cockpit-base64-encode]] |
| 287 |
== cockpit.base64_encode() |
| 288 |
|
| 289 |
.... |
| 290 |
string = cockpit.base64_encode(data) |
| 291 |
.... |
| 292 |
|
| 293 |
Encode binary data into a string using the Base64 encoding. The `+data+` |
| 294 |
argument can either be a `+string+`, an `+Array+`, an `+ArrayBuffer+` or |
| 295 |
a `+Uint8Array+`. The return value is a string. |
| 296 |
|
| 297 |
[[cockpit-base64-decode]] |
| 298 |
== cockpit.base64_decode() |
| 299 |
|
| 300 |
.... |
| 301 |
data = cockpit.base64_decode(string, [constructor]) |
| 302 |
.... |
| 303 |
|
| 304 |
Decode binary data from a Base64 encoded string. The `+string+` argument |
| 305 |
should be a javascript string. The returned `+data+`> will be an array |
| 306 |
of bytes. |
| 307 |
|
| 308 |
You can pass `+Uint8Array+`, `+Array+` or `+String+` as an alternate |
| 309 |
`+constructor+` if you want the decoded data in an alternate form. The |
| 310 |
default is to return an `+Array+`. Note that if you use a `+String+` for |
| 311 |
the decoded data, then you must guarantee that the data does not contain |
| 312 |
bytes that would be invalid for a string. |