io/commands/internal/Pumps.protos
- Repository
- guillermomolina/protos
- Commit
379c6fa63685c39a5954cfbd577bfda3b4df8e75- Tree
8cd4f4afe29ae4c3866381f38c1858108e9799f6- Blob
f803b96cadc85057428422f5b9f89785ddc93c72- Source
- View exact source on GitHub
// THE LICENSED WORK IS PROVIDED UNDER THE TERMS OF THE ADAPTIVE PUBLIC LICENSE
// ("LICENSE") AS FIRST COMPLETED BY: Guillermo Adrián Molina. ANY USE, PUBLIC
// DISPLAY, PUBLIC PERFORMANCE, REPRODUCTION OR DISTRIBUTION OF, OR PREPARATION OF
// DERIVATIVE WORKS BASED ON, THE LICENSED WORK CONSTITUTES RECIPIENT'S ACCEPTANCE
// OF THIS LICENSE AND ITS TERMS, WHETHER OR NOT SUCH RECIPIENT READS THE TERMS OF
// THE LICENSE. "LICENSED WORK" AND "RECIPIENT" ARE DEFINED IN THE LICENSE. A COPY
// OF THE LICENSE IS LOCATED IN THE TEXT FILE ENTITLED "LICENSE.TXT" ACCOMPANYING
// THE CONTENTS OF THIS FILE. IF A COPY OF THE LICENSE DOES NOT ACCOMPANY THIS
// FILE, A COPY OF THE LICENSE MAY ALSO BE OBTAINED AT THE FOLLOWING WEB SITE:
// https://github.com/guillermomolina/protos
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
// the specific language governing rights and limitations under the License.
//! COMMANDS.md §6: the one byte transfer kernel shared by every route (input,
//! links, final output, stderr, and drain-and-discard). Exactly one read or
//! write is outstanding at a time; the next read is issued only after the
//! previous write completed, so payload retention is bounded by one chunk and
//! the destination's backpressure reaches the source. Bytes are forwarded as
//! the exact values read: never decoded, copied into a queue or retried.
// Implementation-selected request size; not a portable API guarantee.
chunkSize: 65536
// The standard Bytes parent, reached through an ordinary encoding.
bytesParent: Encoding.UTF8.encode("").parent()
// ByteReadable boundary check of one non-null read result: a nonempty
// standard Bytes sequence of at most the requested size. Scalars are rejected
// before any message reaches them. A malformed result is an ordinary Error:
// it is never forwarded, taken as EOF or recorded as an operation failure.
// The parent check is a boundary check, not an authentication of the value;
// the destination's own write admission stays authoritative.
admitChunk: (chunk) => {
malformed: (chunk === true) || (chunk === false) || Integer.recognizes(chunk) ||
Float.recognizes(chunk) || String.recognizes(chunk) || Array.recognizes(chunk)
malformed.ifFalse(() => {
malformed = (chunk.parent() === bytesParent) == false
})
malformed.ifFalse(() => {
size: chunk.size()
malformed = (Integer.recognizes(size) == false) || (size < 1) || (size > chunkSize)
})
malformed.ifTrue(() => { Error().signal() })
chunk
}
/// Moves bytes from `source` to `destination` (null discards) until EOF, a
/// stop, or a failed or cancelled operation. `control` is the run's private
/// coordination object; `readSite` and `writeSite` carry the stage, stream
/// and whether that endpoint is a borrowed application endpoint. The stop
/// latch is checked before every read and every write. Answers "eof",
/// "stopped" or "ended" (an operation failed or was cancelled;
/// `control.settle` already recorded it and latched the stop).
transfer: (control, source, readSite, destination, writeSite) => {
state: "running"
chunk: null
while(state == "running") {
if(control.stopped()) {
state = "stopped"
} else {
readRecord: control.issue(readSite.stage, "transfer", readSite.stream, "read",
source.read(chunkSize), readSite.borrowed)
outcome: control.settle(readRecord)
readRecord = null
if(outcome.kind != "resolved") {
state = "ended"
} else {
chunk = outcome.value
outcome = null
if(chunk === null) {
state = "eof"
} else {
admitChunk(chunk)
if(destination !== null) {
if(control.stopped()) {
state = "stopped"
} else {
writeRecord: control.issue(writeSite.stage, "transfer", writeSite.stream, "write",
destination.write(chunk), writeSite.borrowed)
if(control.settle(writeRecord).kind != "resolved") {
state = "ended"
}
}
}
}
// Release the chunk before the next read is issued.
chunk = null
}
}
}
state
}