( options?: EncodeOptions )
| 456 | * @since 4.0.5 |
| 457 | */ |
| 458 | export const encode = <IE = never, Done = unknown, In extends Entry<any, any> = Entry>( |
| 459 | options?: EncodeOptions |
| 460 | ): Channel.Channel< |
| 461 | Arr.NonEmptyReadonlyArray<Uint8Array>, |
| 462 | TarError | IE | (In extends Entry<infer E, any> ? E : never), |
| 463 | Done, |
| 464 | Arr.NonEmptyReadonlyArray<In>, |
| 465 | IE, |
| 466 | Done, |
| 467 | In extends Entry<any, infer R> ? R : never |
| 468 | > => { |
| 469 | // Distribute over entry unions to infer body errors and services. |
| 470 | type E = In extends Entry<infer E, any> ? E : never |
| 471 | type R = In extends Entry<any, infer R> ? R : never |
| 472 | const omitEndOfArchive = options?.omitEndOfArchive ?? false |
| 473 | return Channel.fromTransform((upstream: Pull.Pull<Arr.NonEmptyReadonlyArray<In>, IE, Done>, scope) => |
| 474 | Effect.sync(() => { |
| 475 | const encoder = new Encoder() |
| 476 | let entries: ReadonlyArray<Entry<E, R>> = [] |
| 477 | let index = 0 |
| 478 | let done: { readonly value: Done } | undefined |
| 479 | let body: Pull.Pull<Arr.NonEmptyReadonlyArray<Uint8Array>, E, void, R> | undefined |
| 480 | let bodyScope: Scope.Closeable | undefined |
| 481 | |
| 482 | const write = (f: () => void): Effect.Effect<void, TarError> => { |
| 483 | try { |
| 484 | f() |
| 485 | } catch (error) { |
| 486 | return isTarError(error) ? Effect.fail(error) : Effect.die(error) |
| 487 | } |
| 488 | return Effect.void |
| 489 | } |
| 490 | |
| 491 | return Effect.gen(function*() { |
| 492 | while (true) { |
| 493 | if (encoder.buffered >= SLAB_SIZE) return encoder.flush() |
| 494 | if (body !== undefined) { |
| 495 | const chunks = yield* Pull.catchDone(body, () => Effect.undefined) |
| 496 | if (chunks !== undefined) { |
| 497 | yield* write(() => { |
| 498 | for (const chunk of chunks) encoder.body(chunk) |
| 499 | }) |
| 500 | continue |
| 501 | } |
| 502 | body = undefined |
| 503 | yield* Scope.close(bodyScope!, Exit.void) |
| 504 | yield* write(() => encoder.end()) |
| 505 | continue |
| 506 | } |
| 507 | if (index < entries.length) { |
| 508 | const entry = entries[index++] |
| 509 | // Directories, links and in-memory files have nothing to stream |
| 510 | const bytes = entry.body === Stream.empty ? emptyBody : inMemoryBodies.get(entry.body) |
| 511 | yield* write(() => { |
| 512 | encoder.begin(entry.header) |
| 513 | if (bytes !== undefined) { |
| 514 | encoder.body(bytes) |
| 515 | encoder.end() |
no test coverage detected