OptionaldownloadThe download function; downloadExportViaNats unless a test replaces it.
Requests a CSV export from an edge box's exporter and collects the streamed reply.
Steps, in order:
payload as JSON to requestSubject, with format: "csv" and
ack_subject set to the ack inbox, and flushes.requestSubject (the box is offline or its exporter is not
running), and the call rejects at once instead of waiting for the idle
timeout. Otherwise the Avena-Export-Frame header names each frame; a message
without the header counts as a chunk. Each chunk is copied and stored (or
passed to options.onChunk, which is awaited), and then acknowledged by
publishing an empty message to the ack inbox. meta and summary frames are
parsed as JSON and kept. Frames with another name, or whose type field does
not match the header, are ignored.complete frame, joins the stored parts into one Blob (an empty one when
options.onChunk took the chunks) and resolves.If the call ends any other way after the request was sent (cancelled through
options.signal, idle timeout, a bad frame, the subscription ending, or
options.onChunk failing), it publishes a cancel message on the ack subject,
with the header Avena-Export-Frame: cancel and the body {"type":"cancel"}, so
the exporter stops instead of waiting for acks.
No cancel is sent after an error frame or a NATS status reply, since the exporter
has already stopped or never started.
Chunks are folded into a Blob part every 8 MiB, so at most that much CSV sits in
JavaScript memory at a time; the rest is held in the browser's Blob storage until
the caller saves the result. With options.onChunk nothing is kept at all.
Connected service from "nats.svelte"!connect.
Export request subject, e.g. from
subjects!archiveExportRequestSubject:
avenars.<site>.<box>.<source>.export.request.
Request fields. format is overwritten with csv.
Progress callbacks, chunk consumer, idle timeout and abort signal.
Resolves to the assembled CSV, its file name, size and missing channels.
If publishing fails, no exporter is listening on requestSubject, the
server returns another error status, a non-chunk frame is not valid JSON, the
exporter sends an error frame, no message arrives within the idle timeout, the reply
subscription ends before a complete frame, options.onChunk throws (that
error), or options.signal is aborted (an error named AbortError).
OptionalonTakes each chunk as it arrives, before it is acknowledged, instead of the chunks
being collected into the result's Blob (see ExportStreamOptions.onChunk).
OptionalonProgress, summary and chunk callbacks, passed on to downloadExportViaNats.
OptionalonOpens the export's connection; resolves to null when that fails.
Request fields.
Cancels the export, including while the connection is being opened.
Export request subject (archiveExportRequestSubject).
Options of runExportOnOwnConnection.