InputStream
to read; put a writable or a file to create there, and it receives an OutputStream to write.
- The Channels API is removed from all five SDKs.
createChannel,ChannelReader,ChannelWriter,readerRef, andwriterRef(Pythoncreate_channel,create_channel_async,reader_ref,writer_ref; Rustiii_sdk::helpers::create_channelandiii_sdk::channel::*; GoCreateChannel,Channel,ChannelReader,ChannelWriter,OpenReader,OpenWriter, andExtractChannelRefs; the browser’screateChannel,Channel,ChannelReader, andChannelWriter) give way toInputStreamandOutputStream, and toiii.pipe()for an explicit pipe. This is a breaking change with no deprecated aliases. - The wire is unchanged. A 0.26 engine serves 0.27 SDKs, and a ref has the same JSON in every
SDK, so workers on 0.26 and 0.27 SDKs work together in one project. For streaming, the 0.27 engine
changes only the text of its
payload_too_largeerrors, which now link to Streaming / Payload size, as the errors of every 0.27 SDK do. - VM workers are explicit in
worker-compose.yaml. A container that runs in a microVM now says so in itsscripts, and Compose rewrites a 0.26 file on its next run. A project with a VM worker also follows Upgrading VM workers from 0.26.x. - Container keys are directory names. A key that starts with
.or holds/, among others, loaded in 0.26 and now fails withINVALID_CONTAINER_KEY. iii callresolves indefaultwithout-n, also in a script that Compose starts. Seeiii call.
Before you upgrade
Who needs to migrate
Every project, once, for the changes in Upgrade the project: the compose file and the scripts that runiii call. For streaming, only the workers whose SDK you upgrade to
0.27, and in them only the code that uses channels. A project that keeps its SDKs on 0.26 needs no
streaming change, an engine upgrade alone changes nothing for streaming, and you can move one worker
at a time.
Upgrade the project
Do this once per project, before the first run of the 0.27iii:
- Stop the project, and commit
worker-compose.yamlandworker-compose.lock. The 0.27 Compose rewrites the containers that 0.26 ran in a VM on its next run, and a rewritten file no longer loads in 0.26: the commit is your way back. - Rename each container key that is empty or blank, holds
/or\, starts with., or holds a NUL, such as.internalora/b, and thestart_afterentries that name it. A container key is now also the name of its directories,.iii/build/<key>/and.iii/workers/<key>/, so such a key fails withINVALID_CONTAINER_KEYin any project, with or without a VM worker. - If the project has VM workers, a registry
bundleor apath://worker whoseiii.worker.yamlsetsruntime.base_image, follow Upgrading VM workers from 0.26.x. It covers the sandbox adapter, which must be a 0.5 release, and the containers that must stay on the host. - Add
-n <namespace>to eachiii callin a script that targets a function of the project’s namespace, as iniii call.
Compatibility
Mixed fleets
A ref is the same JSON whichever SDK sent it, so a stream crosses SDK versions. What the receiving worker gets depends on its SDK:
The 0.27 lifecycle rules apply on the 0.27 side only. A handler on a 0.26 SDK that receives a writer
still has to close it, and a 0.27 caller that passed it a sink waits until it does: its call
resolves only after the sink has received the whole stream. If the 0.26 handler never opens the
writer, the call fails with
TIMEOUT when its timeout expires, and the SDK aborts the sink.
A 0.26 TypeScript or Python writer that closes without writing anything never opens its socket, so
its reader, on any SDK version, waits until the engine expires the pipe. This is 0.26 behavior: a
0.27 writer opens the socket to close it, so its reader sees an empty stream at once. Upgrade the
writing side to fix it.
Payload size with older SDKs and engines
When a worker registers, the engine gives it one of two receive limits for JSON messages. For the limits, see Streaming / Payload size. A worker on a Rust, Node, Python, or Go SDK from 0.25.0 declares thelarge-messages.v1 protocol feature and gets
the larger limit.
These rules apply to older SDKs and to older engines:
- A worker on an older Rust, Node, Python, or Go SDK does not declare the feature. The engine then sets the limit from the SDK version that the worker reports. The worker gets the larger limit when the version is 0.24.5 or later. It gets the smaller limit when the version is missing, when the engine cannot read it, or when it is older than 0.24.5.
- The engine reads the version as semver or as PEP 440. Examples of semver are
0.24.6and0.25.0-rc.5. Examples of PEP 440 are0.25.0rc5and0.24.5.post1. A pre-release of 0.24.5 is older than 0.24.5. - A Rust worker can report its own version. SDKs 0.24.5 through 0.25.0-rc.5 do not declare
large-messages.v1. A Rust worker on one of these SDKs can setWorkerMetadata.versionto its own version. If that version is older than 0.24.5, the worker gets the smaller limit. Upgrade the worker to SDK 0.25.0 or later. - The engine accepts the large single frames that SDKs before 0.24.5 send. The engine accepts a message as one frame or as fragments.
- An engine from before the receive limit check does not check the limit of the target. It sends each message as one frame. A worker on an older SDK can then disconnect. Use an engine and SDKs from the same release, or newer.
Find what to change
Search each worker for the removed APIs. A few hits, such assendMessage on another library’s
socket, may be unrelated:
- Send a stream where a caller creates a channel and passes its
readerRef. - Return a stream or
write into the caller’s sink where a caller creates a channel and
passes its
writerRef. - HTTP
request_bodyandresponsein route handlers that stream. - Messages where code calls
sendMessageoronMessage. - Abort where a writer destroys its stream or is dropped on failure.
- Ref helpers where code finds refs by hand.
createChanneltoiii.pipe()where a pipe has to exist before its data.- The browser SDK for a page that streams.
- Check the behavior changes,
iii call, and the checklist.
What changed
The SDK also detects streams in what it sends and turns refs back into streams in what it receives,
in handler input and, new in 0.27, in call results. See
Using iii / Streaming for the full API.
Send a stream
Put the source in the payload. The SDK creates the pipe, sends the ref, and pumps the data while the call runs, so the caller no longer writes a channel next to the call. The handler reads the field the same way as before, without.stream in TypeScript. The payload field keeps its name: it is
your function’s contract.
- Node / TypeScript
- Python
- Rust
- Go
Before (0.26)After (0.27)
Return a stream
In 0.26, refs in a result were not turned into readers, so a function that produced a stream needed its caller to create a channel and pass awriterRef in. In 0.27 the function returns the stream
and the caller reads it from the result. The caller no longer passes a writer, so this changes the
function’s payload: move its callers in the same change. When the caller has a place for the bytes,
such as a file, pass a sink instead.
- Node / TypeScript
- Python
- Rust
- Go
Before (0.26)After (0.27)
Write into the caller’s sink
When the caller copied the reader end of its channel into a file or another writable, pass that sink in the payload instead. The handler receives anOutputStream, and the caller’s call resolves once
the sink has received everything.
- Node / TypeScript
- Python
- Rust
- Go
Before (0.26)After (0.27)
OutputStream are dropped, and the call
still succeeds. If the caller also read the channel’s messages (onMessage), pass the output of a
pipe instead and read its input.
HTTP request_body and response
The HTTP worker still passes a route’s request body and response as refs in request_body and
response, and it reads the status and the headers from the same JSON messages. Only their SDK
types change: request_body is an InputStream and response is an OutputStream. The
request_body of the TypeScript HttpRequest and StreamRequest types and of the Python
StreamRequest is an InputStream too. In Go, the RequestBody of iiihttp.Request,
iiihttp.InternalRequest, and iiihttp.StreamRequest is an *iii.InputStream, and
InternalRequest.Response is an *iii.OutputStream.
- Node / TypeScript
- Python
- Rust
- Go
Before (0.26)After (0.27)
{ status_code, body }) without touching
response needs no change: the HTTP worker calls a route through an observer, and in that case the
SDK leaves an untouched response alone when the handler returns a value.
Streaming response helpers
TheStreamResponse helpers (the res of the TypeScript http() wrapper, of the Python
StreamResponse, and of the Go iiihttp.Wrap) wrap the OutputStream now. In TypeScript and
Python, every method that writes is asynchronous:
In Go,
Status, Headers, Write, and Close keep their signatures. Wrap closes the response
when the handler returns and aborts it when the handler fails, as before:
Before (0.26)
Messages
send takes a value and sends it as JSON in one text frame, so drop your own JSON.stringify or
json.dumps. messages() parses each text frame, and yields a frame that is not JSON as its raw
string, so messages from a writer on a 0.26 SDK that sent plain text still arrive. A reader on a
0.26 SDK receives what send wrote as JSON text: a bare string arrives quoted.
- Node / TypeScript
- Python
- Rust
- Go
Before (0.26)After (0.27)
frames().
Abort
In 0.26 each SDK aborted differently: Node withwriter.stream.destroy() and no argument, Rust by
dropping the writer without close(), Go with a Close() after a failed Write, and Python and
the browser had no abort call. In 0.27 the SDK aborts for you in the common cases, and
abort(reason) is one call everywhere:
- A source the SDK pumps (a generator, a readable, a file) that fails aborts its stream.
- A handler that throws aborts every received
OutputStreamit has written to. await output.abort(reason)aborts on purpose. If the stream’s socket never opened, it opens it first, so the reader always learns of the failure.
OutputStream leaves that stream alone, as in
0.26: the caller learns of the failure from the call’s error.
The reader gets a typed error after the data that was already sent.
- Node / TypeScript
- Python
- Rust
- Go
Before (0.26)After (0.27)
reason is the engine’s description of the close, such as
channel writer aborted: closed with code 1011: the engine does not relay the writer’s own reason.
Report the cause in the call’s error or result. A caller that passed a sink gets the abort as its
call’s error, with the code STREAM_ABORTED, even when the function returned.
Ref helpers move to the protocol module
The SDK now finds refs and turns them into streams, so most code that calledextractChannelRefs or
isChannelRef can drop it. Code that still works with the raw ref JSON, such as a router that
inspects payloads, imports the helpers from the low-level protocol module:
In Go,
iii.StreamChannelRef and iii.ChannelDirection stay in package iii, and
iii.ExtractChannelRefs is removed: unmarshal the raw ref JSON into an iii.StreamChannelRef.
The helpers see refs as JSON. A handler on a 0.27 SDK receives streams, not refs, so call them on
the raw JSON you hold, such as a value read from storage.
createChannel to iii.pipe()
Most channels become a value in the payload or the result, as above. Keep an explicit pipe when the
stream has to exist before its data, when it goes to a call made later, or for a two-way session
(two pipes). iii.pipe() returns its two ends, input and output; put the end the other side
uses in the payload, where 0.26 code put a ref.
- Node / TypeScript
- Python
- Rust
- Go
Before (0.26)After (0.27)
In Go,
client.Pipe(ctx) returns the two ends as (input, output, err): channel.Writer becomes
output, channel.Reader becomes input, and the ends go in the payload where channel.ReaderRef
and channel.WriterRef went. In the browser, createChannel(worker, bufferSize) becomes
worker.pipe({ bufferSize }).
The worker that calls pipe() owns the pipe, as with createChannel: keep it connected until the
other side has attached.
The browser SDK
The browser SDK follows the TypeScript API, with Web streams only. AFile or a Blob from an
<input type="file">, a ReadableStream such as response.body of a fetch, and an async
generator are sources. A WritableStream, such as the one a File System Access handle returns from
createWritable(), is a sink.
Send a file from a page:
Before (0.26)
toReadable() or toWritable() in the browser: toWebStream() returns the Web stream
of an InputStream or an OutputStream. abort(reason) closes the stream’s socket with code 4011,
because a browser can only send 1000 or a code from 3000 to 4999. The engine treats it as an abort,
so a reader on any 0.27 SDK gets STREAM_ABORTED, with the reason
channel writer aborted: closed with code 4011.
Behavior changes
Beyond the renamed API, these rules are new for a worker on a 0.27 SDK:- A received
OutputStreamis closed when the handler returns, and aborted when it throws. A 0.26 handler that returned and kept writing in the background callskeepOpen()(Python and Rustkeep_open(), GoKeepOpen()) first, and then closes or aborts the stream itself. A stream the handler never touched is closed as well when it returns, except in a call through an observer (an HTTP route, for example), where it is closed only when the handler returns nothing. - A received
InputStreamthat the handler started reading and did not finish is cancelled when the handler returns. Finish reading before returning: a read still running after it, in a background task for example, fails withSTREAM_CLOSED. - Refs in call results arrive as streams. In 0.26 a ref in a result stayed JSON. Code that read
a ref out of a result and opened it now receives an
InputStreamor anOutputStreaminstead. - A stream has one reader. A second read of the same
InputStreamfails; calltee()before the first read for copies in the same process. Go has notee(): copy the bytes withio.TeeReaderorio.MultiWriteras you read. - Streams need a live call. A call sent with
CallAction.Enqueue(Goiii.EnqueueAction(queue)) and a stream or a ref anywhere in its payload is refused before it is sent, with the codeSTREAM_NOT_ALLOWED. Store the data and enqueue its key.CallAction.Voidis allowed. - Observers receive
StreamInfo. When an observer captures theinputoroutputof a call that carried a stream, its target receives{ stream: true, direction, id }in that field (Goiii.StreamInfo). A 0.26 TypeScript, Python, or browser target got a live reader there, and a 0.26 Go target got the ref; opening either raced the observed function for the bytes. See Observe a stream. - Rust streams bind lazily. An
InputStreamorOutputStreamdecoded from JSON binds to the handler’s worker inside a handler, and to the only client in the process elsewhere. A process with several clients callsstream.bind(&worker)before using a stream decoded outside a handler. - Go streams bind lazily too. A stream unmarshalled from a handler’s input or from a call’s
result binds to that client. One unmarshalled from other JSON binds to the only client in the
process; with several, call
stream.Bind(worker)before using it. - The
payload_too_largeerror links to the new page:https://iii.dev/docs/creating-workers/streaming#payload-size.
iii call
The 0.27 iii call changes for scripts:
- Without
-n,iii callresolves indefault. In 0.26 it took the namespace ofIII_NAMESPACEwhen that variable was set, and Compose sets it for every process it starts. Ascriptsline, or a script it runs, reached a function of the project’s namespace with a plainiii call <function>. In 0.27 that call looks indefaultand does not find the function. Name the namespace withiii call -n <namespace> <function>. A project indefaultneeds no change, and neither does a call to a function indefault, such as the engine’ssandbox::buildandsandbox::start. - Arguments that cannot make a call exit with
2. - A function that fails prints its
Error:JSON on one line when standard error is not a terminal, so a script can parse it. Other failures print plain text.
Checklist
- Commit
worker-compose.yamlandworker-compose.lockbefore the first run of the 0.27iii - Rename each container key that fails with
INVALID_CONTAINER_KEY, and thestart_afterentries that name it - In a project with VM workers, follow Upgrading VM workers from 0.26.x
- Add
-n <namespace>to eachiii callin a script that targets a function outsidedefault - Upgrade one worker’s SDK at a time; workers on 0.26 SDKs keep working unchanged on the same engine
- Replace each
createChannel/create_channel/create_channel_asyncwhose writer feeds a call with the source itself in the payload - Replace each
writerRef/writer_refpassed in for a function to fill with a returned stream, or with a sink in the payload, and move that function’s callers in the same change - Type stream fields as
InputStreamandOutputStream; in Rust, replaceextract_channel_refsandChannelReader::new/ChannelWriter::newwith typed struct fields; in Go, replaceExtractChannelRefs,OpenReader, andOpenWriterwith*iii.InputStreamand*iii.OutputStreamfields - In Go, wrap what you send with
iii.InputStreamFromReader,iii.InputStreamFromBytes, oriii.OutputStreamToWriter, and changeiiihttp.Wrap(client, fn)toiiihttp.Wrap(fn) - In the browser, pass a
Blob, aFile, or aReadableStreamin the payload instead of writing a channel - Replace
reader.streamandwriter.streamwith the stream itself,write, or the native bridges (toReadable,toWritable) - Replace
sendMessage/send_messagewithsend, andonMessage/on_messagewithmessages(), without your own JSON encoding - Replace
writer.stream.destroy(), dropping a Rust writer, and a GoClose()after a failedWritewithabort(reason)or a throw, and catchStreamAbortedError(RustError::StreamAborted, Go an*iii.InvocationErrorwithiii.CodeStreamAborted) where you caught the old errors - Remove explicit closes the handler no longer needs, and call
keepOpen()(GoKeepOpen()) where a handler keeps writing after it returns - Import
extractChannelRefs,isChannelRef,StreamChannelRef,ChannelDirection, andChannelItemfrom the protocol module - Move calls queued with
CallAction.Enqueuethat carried a ref to stored data and a key - Update observer targets that read a captured stream field to expect a
StreamInfo
To call functions with streams, see Using iii / Streaming. To write
functions that receive and return streams, see Creating workers /
Streaming. For how a stream works, see Understanding iii /
Streaming. For each SDK’s API, see the SDK reference:
Node, Python, Rust,
Go, and Browser.