the-moby-effect
Version:
Moby/Docker API client built using effect-ts
78 lines • 3.94 kB
JavaScript
import * as Socket from "@effect/platform/Socket";
import * as Channel from "effect/Channel";
import * as Chunk from "effect/Chunk";
import * as Effect from "effect/Effect";
import * as Either from "effect/Either";
import * as Exit from "effect/Exit";
import * as Function from "effect/Function";
import * as Match from "effect/Match";
import * as Option from "effect/Option";
import * as Predicate from "effect/Predicate";
import * as Queue from "effect/Queue";
import * as Sink from "effect/Sink";
import * as Stream from "effect/Stream";
import { makeMultiplexedChannel, multiplexedFromStreamWith, MultiplexedHeaderType } from "./multiplexed.js";
import { demuxStdioRawToSeparateSinks } from "./raw.js";
/** @internal */
export const pack = /*#__PURE__*/Function.dual(arguments_ => "stdin" in arguments_[0] || "stdout" in arguments_[0] || "stderr" in arguments_[0], /*#__PURE__*/Effect.fnUntraced(function* (stdio, options) {
const mutex = yield* Effect.makeSemaphore(1);
const context = yield* Effect.context();
const capacity = options.requestedCapacity;
const stdoutConsumerQueue = yield* Queue.bounded(capacity);
const stderrConsumerQueue = yield* Queue.bounded(capacity);
const stdinProducerQueue = yield* Queue.bounded(capacity);
// Demux everything to and fro the correct places. We can touch
// this more than once because it is wrapped in the mutex.
const mother = demuxStdioRawToSeparateSinks(stdio, {
stdin: Function.pipe(Stream.fromQueue(stdinProducerQueue, {
shutdown: true
}), Stream.map(Either.match({
onRight: Exit.succeed,
onLeft: Function.flow(Exit.mapBoth({
onFailure: Option.some,
onSuccess: Function.compose(Option.none, Exit.fail)
}), Exit.flatten)
})), Stream.flattenExitOption, Stream.flattenChunks),
stdout: Sink.fromQueue(stdoutConsumerQueue, {
shutdown: true
}),
stderr: Sink.fromQueue(stderrConsumerQueue, {
shutdown: true
})
}, {
encoding: options?.encoding
}).pipe(mutex.withPermitsIfAvailable(1)).pipe(Effect.asVoid).pipe(Channel.fromEffect).pipe(Channel.provideContext(context));
// Convert the streams to the multiplexed streams
const textEncoder = new TextEncoder();
const encode = Function.pipe(Match.type(), Match.when(Predicate.isUint8Array, data => data), Match.when(Predicate.isString, data => textEncoder.encode(data)), Match.when(Socket.isCloseEvent, () => new Uint8Array()), Match.exhaustive);
const mapOutEntry = type => data => {
const encoded = encode(data);
const size = encoded.length;
const result = new Uint8Array(8 + encoded.length);
// Set the header
result[0] = type;
result[1] = 0;
result[2] = 0;
result[3] = 0;
// Set the size bytes in big-endian order
result[4] = size >>> 24 & 0xff;
result[5] = size >>> 16 & 0xff;
result[6] = size >>> 8 & 0xff;
result[7] = size & 0xff;
result.set(encoded, 8);
return result;
};
const concurrency = {
concurrent: true
};
const independentStdinChannel = Channel.toQueue(stdinProducerQueue).pipe(Channel.mapInput(Function.constVoid)).pipe(Channel.mapInputError(Function.unsafeCoerce)).pipe(Channel.mapInputIn(input => Chunk.map(input, encode))).pipe(Channel.zipLeft(mother, concurrency));
const {
underlying: independentStdoutChannel
} = Stream.fromQueue(stdoutConsumerQueue).pipe(Stream.encodeText).pipe(Stream.map(mapOutEntry(MultiplexedHeaderType.Stdout))).pipe(multiplexedFromStreamWith());
const {
underlying: independentStderrChannel
} = Stream.fromQueue(stderrConsumerQueue).pipe(Stream.encodeText).pipe(Stream.map(mapOutEntry(MultiplexedHeaderType.Stderr))).pipe(multiplexedFromStreamWith());
const mixedOutputChannel = Channel.zip(independentStdoutChannel, independentStderrChannel, concurrency);
return makeMultiplexedChannel(Channel.zip(independentStdinChannel, mixedOutputChannel, concurrency));
}));
//# sourceMappingURL=pack.js.map