Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix demux function types #92

Merged
merged 1 commit into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 32 additions & 28 deletions docs/modules/Demux.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,19 @@ the correct sink.
**Signature**

```ts
export declare const demuxMultiplexedSocket: (<E1, E2, E3>(
export declare const demuxMultiplexedSocket: (<A1, A2, E1, E2, E3>(
source: Stream.Stream<Uint8Array, E1, never>,
sink1: Sink.Sink<void, string | Uint8Array, never, E2, never>,
sink2: Sink.Sink<void, string | Uint8Array, never, E3, never>
) => (socket: MultiplexedStreamSocket) => Effect.Effect<void, Socket.SocketError | E1 | E2 | E3, never>) &
(<E1, E2, E3>(
sink1: Sink.Sink<A1, string | Uint8Array, never, E2, never>,
sink2: Sink.Sink<A2, string | Uint8Array, never, E3, never>
) => (
socket: MultiplexedStreamSocket
) => Effect.Effect<readonly [stdout: A1, stderr: A2], Socket.SocketError | E1 | E2 | E3, never>) &
(<A1, A2, E1, E2, E3>(
socket: MultiplexedStreamSocket,
source: Stream.Stream<Uint8Array, E1, never>,
sink1: Sink.Sink<void, string | Uint8Array, never, E2, never>,
sink2: Sink.Sink<void, string | Uint8Array, never, E3, never>
) => Effect.Effect<void, Socket.SocketError | E1 | E2 | E3, never>)
sink1: Sink.Sink<A1, string | Uint8Array, never, E2, never>,
sink2: Sink.Sink<A2, string | Uint8Array, never, E3, never>
) => Effect.Effect<readonly [stdout: A1, stderr: A2], Socket.SocketError | E1 | E2 | E3, never>)
```

Added in v1.0.0
Expand All @@ -97,15 +99,15 @@ combined on the same sink.
**Signature**

```ts
export declare const demuxRawSocket: (<E1, E2>(
export declare const demuxRawSocket: (<A, E1, E2>(
source: Stream.Stream<Uint8Array, E1, never>,
sink: Sink.Sink<void, string | Uint8Array, never, E2, never>
) => (socket: RawStreamSocket) => Effect.Effect<void, Socket.SocketError | E1 | E2, never>) &
(<E1, E2>(
sink: Sink.Sink<A, string | Uint8Array, never, E2, never>
) => (socket: RawStreamSocket) => Effect.Effect<A, Socket.SocketError | E1 | E2, never>) &
(<A, E1, E2>(
socket: RawStreamSocket,
source: Stream.Stream<Uint8Array, E1, never>,
sink: Sink.Sink<void, string | Uint8Array, never, E2, never>
) => Effect.Effect<void, Socket.SocketError | E1 | E2, never>)
sink: Sink.Sink<A, string | Uint8Array, never, E2, never>
) => Effect.Effect<A, Socket.SocketError | E1 | E2, never>)
```

Added in v1.0.0
Expand All @@ -122,26 +124,28 @@ and one for stderr.

```ts
export declare const demuxSocket: {
<E1, E2>(
<A1, E1, E2>(
socket: RawStreamSocket,
source: Stream.Stream<Uint8Array, E1, never>,
sink: Sink.Sink<void, string | Uint8Array, never, E2, never>
): Effect.Effect<void, Socket.SocketError | E1 | E2, never>
<E1, E2>(
sink: Sink.Sink<A1, string | Uint8Array, never, E2, never>
): Effect.Effect<A1, Socket.SocketError | E1 | E2, never>
<A1, E1, E2>(
source: Stream.Stream<Uint8Array, E1, never>,
sink: Sink.Sink<void, string | Uint8Array, never, E2, never>
): (socket: RawStreamSocket) => Effect.Effect<void, Socket.SocketError | E1 | E2, never>
<E1, E2, E3>(
sink: Sink.Sink<A1, string | Uint8Array, never, E2, never>
): (socket: RawStreamSocket) => Effect.Effect<A1, Socket.SocketError | E1 | E2, never>
<A1, A2, E1, E2, E3>(
socket: MultiplexedStreamSocket,
source: Stream.Stream<Uint8Array, E1, never>,
sink1: Sink.Sink<void, string | Uint8Array, never, E2, never>,
sink2: Sink.Sink<void, string | Uint8Array, never, E3, never>
): Effect.Effect<void, Socket.SocketError | E1 | E2 | E3, never>
<E1, E2, E3>(
sink1: Sink.Sink<A1, string | Uint8Array, never, E2, never>,
sink2: Sink.Sink<A2, string | Uint8Array, never, E3, never>
): Effect.Effect<readonly [stdout: A1, stderr: A2], Socket.SocketError | E1 | E2 | E3, never>
<A1, A2, E1, E2, E3>(
source: Stream.Stream<Uint8Array, E1, never>,
sink1: Sink.Sink<void, string | Uint8Array, never, E2, never>,
sink2: Sink.Sink<void, string | Uint8Array, never, E3, never>
): (socket: MultiplexedStreamSocket) => Effect.Effect<void, Socket.SocketError | E1 | E2 | E3, never>
sink1: Sink.Sink<A1, string | Uint8Array, never, E2, never>,
sink2: Sink.Sink<A2, string | Uint8Array, never, E3, never>
): (
socket: MultiplexedStreamSocket
) => Effect.Effect<readonly [stdout: A1, stderr: A2], Socket.SocketError | E1 | E2 | E3, never>
}
```

Expand Down
105 changes: 56 additions & 49 deletions src/Demux.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import * as Effect from "effect/Effect";
import * as Function from "effect/Function";
import * as Sink from "effect/Sink";
import * as Stream from "effect/Stream";
import * as Tuple from "effect/Tuple";

import { IExposeSocketOnEffectClientResponse } from "./Requests.js";

Expand Down Expand Up @@ -147,22 +148,22 @@ export enum MultiplexedStreamSocketHeaderType {
* @see https://docs.docker.com/engine/api/v1.43/#tag/Container/operation/ContainerAttach
*/
export const demuxRawSocket = Function.dual<
<E1, E2>(
<A, E1, E2>(
source: Stream.Stream<Uint8Array, E1, never>,
sink: Sink.Sink<void, string | Uint8Array, never, E2, never>
) => (socket: RawStreamSocket) => Effect.Effect<void, E1 | E2 | Socket.SocketError, never>,
<E1, E2>(
sink: Sink.Sink<A, string | Uint8Array, never, E2, never>
) => (socket: RawStreamSocket) => Effect.Effect<A, E1 | E2 | Socket.SocketError, never>,
<A, E1, E2>(
socket: RawStreamSocket,
source: Stream.Stream<Uint8Array, E1, never>,
sink: Sink.Sink<void, string | Uint8Array, never, E2, never>
) => Effect.Effect<void, E1 | E2 | Socket.SocketError, never>
sink: Sink.Sink<A, string | Uint8Array, never, E2, never>
) => Effect.Effect<A, E1 | E2 | Socket.SocketError, never>
>(
3,
<E1, E2>(
<A, E1, E2>(
socket: RawStreamSocket,
source: Stream.Stream<Uint8Array, E1, never>,
sink: Sink.Sink<void, string | Uint8Array, never, E2, never>
): Effect.Effect<void, E1 | E2 | Socket.SocketError, never> =>
sink: Sink.Sink<A, string | Uint8Array, never, E2, never>
): Effect.Effect<A, E1 | E2 | Socket.SocketError, never> =>
Function.pipe(source, Stream.pipeThroughChannel(Socket.toChannel(socket)), Stream.run(sink))
);

Expand All @@ -178,25 +179,27 @@ export const demuxRawSocket = Function.dual<
* @see https://docs.docker.com/engine/api/v1.43/#tag/Container/operation/ContainerAttach
*/
export const demuxMultiplexedSocket = Function.dual<
<E1, E2, E3>(
<A1, A2, E1, E2, E3>(
source: Stream.Stream<Uint8Array, E1, never>,
sink1: Sink.Sink<void, string | Uint8Array, never, E2, never>,
sink2: Sink.Sink<void, string | Uint8Array, never, E3, never>
) => (socket: MultiplexedStreamSocket) => Effect.Effect<void, E1 | E2 | E3 | Socket.SocketError, never>,
<E1, E2, E3>(
sink1: Sink.Sink<A1, string | Uint8Array, never, E2, never>,
sink2: Sink.Sink<A2, string | Uint8Array, never, E3, never>
) => (
socket: MultiplexedStreamSocket
) => Effect.Effect<readonly [stdout: A1, stderr: A2], E1 | E2 | E3 | Socket.SocketError, never>,
<A1, A2, E1, E2, E3>(
socket: MultiplexedStreamSocket,
source: Stream.Stream<Uint8Array, E1, never>,
sink1: Sink.Sink<void, string | Uint8Array, never, E2, never>,
sink2: Sink.Sink<void, string | Uint8Array, never, E3, never>
) => Effect.Effect<void, E1 | E2 | E3 | Socket.SocketError, never>
sink1: Sink.Sink<A1, string | Uint8Array, never, E2, never>,
sink2: Sink.Sink<A2, string | Uint8Array, never, E3, never>
) => Effect.Effect<readonly [stdout: A1, stderr: A2], E1 | E2 | E3 | Socket.SocketError, never>
>(
4,
<E1, E2, E3>(
<A1, A2, E1, E2, E3>(
socket: MultiplexedStreamSocket,
source: Stream.Stream<Uint8Array, E1, never>,
sink1: Sink.Sink<void, string | Uint8Array, never, E2, never>,
sink2: Sink.Sink<void, string | Uint8Array, never, E3, never>
): Effect.Effect<void, E1 | E2 | E3 | Socket.SocketError, never> =>
sink1: Sink.Sink<A1, string | Uint8Array, never, E2, never>,
sink2: Sink.Sink<A2, string | Uint8Array, never, E3, never>
): Effect.Effect<readonly [stdout: A1, stderr: A2], E1 | E2 | E3 | Socket.SocketError, never> =>
Function.pipe(
source,
Stream.pipeThroughChannel(Socket.toChannel(socket)),
Expand All @@ -221,16 +224,18 @@ export const demuxMultiplexedSocket = Function.dual<
type === MultiplexedStreamSocketHeaderType.Stderr
),
Stream.partition(({ type }) => type === MultiplexedStreamSocketHeaderType.Stderr),
Effect.map(([stdout, stderr]) => [
stdout.pipe(
Stream.map(({ contents }) => contents),
Stream.run(sink1)
),
stderr.pipe(
Stream.map(({ contents }) => contents),
Stream.run(sink2)
),
]),
Effect.map(
Tuple.mapBoth({
onFirst: Function.compose(
Stream.map(({ contents }) => contents),
Stream.run(sink1)
),
onSecond: Function.compose(
Stream.map(({ contents }) => contents),
Stream.run(sink2)
),
})
),
Effect.map(Effect.all),
Effect.flatten,
Effect.scoped
Expand All @@ -248,34 +253,36 @@ export const demuxMultiplexedSocket = Function.dual<
* @category Demux
*/
export const demuxSocket: {
<E1, E2>(
<A1, E1, E2>(
socket: RawStreamSocket,
source: Stream.Stream<Uint8Array, E1, never>,
sink: Sink.Sink<void, string | Uint8Array, never, E2, never>
): Effect.Effect<void, E1 | E2 | Socket.SocketError, never>;
<E1, E2>(
sink: Sink.Sink<A1, string | Uint8Array, never, E2, never>
): Effect.Effect<A1, E1 | E2 | Socket.SocketError, never>;
<A1, E1, E2>(
source: Stream.Stream<Uint8Array, E1, never>,
sink: Sink.Sink<void, string | Uint8Array, never, E2, never>
): (socket: RawStreamSocket) => Effect.Effect<void, E1 | E2 | Socket.SocketError, never>;
<E1, E2, E3>(
sink: Sink.Sink<A1, string | Uint8Array, never, E2, never>
): (socket: RawStreamSocket) => Effect.Effect<A1, E1 | E2 | Socket.SocketError, never>;
<A1, A2, E1, E2, E3>(
socket: MultiplexedStreamSocket,
source: Stream.Stream<Uint8Array, E1, never>,
sink1: Sink.Sink<void, string | Uint8Array, never, E2, never>,
sink2: Sink.Sink<void, string | Uint8Array, never, E3, never>
): Effect.Effect<void, E1 | E2 | E3 | Socket.SocketError, never>;
<E1, E2, E3>(
sink1: Sink.Sink<A1, string | Uint8Array, never, E2, never>,
sink2: Sink.Sink<A2, string | Uint8Array, never, E3, never>
): Effect.Effect<readonly [stdout: A1, stderr: A2], E1 | E2 | E3 | Socket.SocketError, never>;
<A1, A2, E1, E2, E3>(
source: Stream.Stream<Uint8Array, E1, never>,
sink1: Sink.Sink<void, string | Uint8Array, never, E2, never>,
sink2: Sink.Sink<void, string | Uint8Array, never, E3, never>
): (socket: MultiplexedStreamSocket) => Effect.Effect<void, E1 | E2 | E3 | Socket.SocketError, never>;
sink1: Sink.Sink<A1, string | Uint8Array, never, E2, never>,
sink2: Sink.Sink<A2, string | Uint8Array, never, E3, never>
): (
socket: MultiplexedStreamSocket
) => Effect.Effect<readonly [stdout: A1, stderr: A2], E1 | E2 | E3 | Socket.SocketError, never>;
} = Function.dual(
(arguments_) => arguments_[0][Socket.TypeId],
<E1, E2, E3>(
<A1, A2, E1, E2, E3>(
socket: RawStreamSocket | MultiplexedStreamSocket,
source: Stream.Stream<Uint8Array, E1, never>,
sink1: Sink.Sink<void, string | Uint8Array, never, E2, never>,
sink2: Sink.Sink<void, string | Uint8Array, never, E3, never>
): Effect.Effect<void, E1 | E2 | E3 | Socket.SocketError, never> => {
sink1: Sink.Sink<A1, string | Uint8Array, never, E2, never>,
sink2: Sink.Sink<A2, string | Uint8Array, never, E3, never>
): Effect.Effect<A1 | readonly [stdout: A1, stderr: A2], E1 | E2 | E3 | Socket.SocketError, never> => {
switch (socket["content-type"]) {
case "application/vnd.docker.raw-stream": {
return demuxRawSocket(socket, source, sink1);
Expand Down
Loading