(options: {
readonly runRaw: <_, E, R>(
handler: (_: string | Uint8Array) => Effect.Effect<_, E, R> | void,
options?: {
readonly onOpen?: Effect.Effect<void> | undefined
}
) => Effect.Effect<void, SocketError | E, R>
readonly run?: <_, E, R>(
handler: (_: Uint8Array) => Effect.Effect<_, E, R> | void,
options?: {
readonly onOpen?: Effect.Effect<void> | undefined
}
) => Effect.Effect<void, SocketError | E, R>
readonly runString?: <_, E, R>(
handler: (_: string) => Effect.Effect<_, E, R> | void,
options?: {
readonly onOpen?: Effect.Effect<void> | undefined
}
) => Effect.Effect<void, SocketError | E, R>
readonly writer: Effect.Effect<
(chunk: Uint8Array | string | CloseEvent) => Effect.Effect<void, SocketError>,
never,
Scope.Scope
>
})
| 98 | * @since 4.0.0 |
| 99 | */ |
| 100 | export const make = (options: { |
| 101 | readonly runRaw: <_, E, R>( |
| 102 | handler: (_: string | Uint8Array) => Effect.Effect<_, E, R> | void, |
| 103 | options?: { |
| 104 | readonly onOpen?: Effect.Effect<void> | undefined |
| 105 | } |
| 106 | ) => Effect.Effect<void, SocketError | E, R> |
| 107 | readonly run?: <_, E, R>( |
| 108 | handler: (_: Uint8Array) => Effect.Effect<_, E, R> | void, |
| 109 | options?: { |
| 110 | readonly onOpen?: Effect.Effect<void> | undefined |
| 111 | } |
| 112 | ) => Effect.Effect<void, SocketError | E, R> |
| 113 | readonly runString?: <_, E, R>( |
| 114 | handler: (_: string) => Effect.Effect<_, E, R> | void, |
| 115 | options?: { |
| 116 | readonly onOpen?: Effect.Effect<void> | undefined |
| 117 | } |
| 118 | ) => Effect.Effect<void, SocketError | E, R> |
| 119 | readonly writer: Effect.Effect< |
| 120 | (chunk: Uint8Array | string | CloseEvent) => Effect.Effect<void, SocketError>, |
| 121 | never, |
| 122 | Scope.Scope |
| 123 | > |
| 124 | }): Socket => |
| 125 | Socket.of({ |
| 126 | [TypeId]: TypeId, |
| 127 | runRaw: options.runRaw, |
| 128 | run: options.run ?? ((handler, opts) => |
| 129 | options.runRaw((data) => |
| 130 | typeof data === "string" |
| 131 | ? handler(encoder.encode(data)) |
| 132 | : data instanceof Uint8Array |
| 133 | ? handler(data) |
| 134 | : handler(new Uint8Array(data)), opts)), |
| 135 | runString: options.runString ?? |
| 136 | (options.run ? |
| 137 | (handler, opts) => options.run!((data) => handler(decoder.decode(data)), opts) : |
| 138 | (handler, opts) => |
| 139 | options.runRaw((data) => |
| 140 | typeof data === "string" |
| 141 | ? handler(data) |
| 142 | : data instanceof Uint8Array |
| 143 | ? handler(decoder.decode(data)) |
| 144 | : handler(decoder.decode(new Uint8Array(data))), opts)), |
| 145 | writer: options.writer |
| 146 | }) |
| 147 | |
| 148 | const encoder = new TextEncoder() |
| 149 | const decoder = new TextDecoder() |
no test coverage detected