MCPcopy Create free account
hub / github.com/Effect-TS/effect / encode

Function encode

packages/effect/src/encoding/Zip.ts:526–657  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

524 * @since 4.0.6
525 */
526export const encode = <
527 IE = never,
528 Done = unknown,
529 In extends Entry<any, any> | ContentEntry = Entry | ContentEntry
530>(): Channel.Channel<
531 Arr.NonEmptyReadonlyArray<Uint8Array>,
532 ZipError | IE | (In extends Entry<infer E, any> ? E : never),
533 Done,
534 Arr.NonEmptyReadonlyArray<In>,
535 IE,
536 Done,
537 In extends Entry<any, infer R> ? R : never
538> => {
539 // Distribute over entry unions to infer body errors and services.
540 type E = In extends Entry<infer E, any> ? E : never
541 type R = In extends Entry<any, infer R> ? R : never
542 return Channel.fromTransform((upstream: Pull.Pull<Arr.NonEmptyReadonlyArray<In>, IE, Done>, scope) =>
543 Effect.sync(() => {
544 const writer = new Writer()
545 let entries: ReadonlyArray<Entry<E, R> | ContentEntry> = []
546 let index = 0
547 let done: { readonly value: Done } | undefined
548 let body: Pull.Pull<Arr.NonEmptyReadonlyArray<Uint8Array>, E | ZipError, void, R> | undefined
549 let bodyScope: Scope.Closeable | undefined
550 // In-memory entries are deflated ahead of the one being written, so the
551 // deflates overlap
552 let deflates: Array<Deflating | undefined> = []
553
554 const startDeflates = (from: number): void => {
555 for (let i = from; i < Math.min(entries.length, from + DEFLATE_AHEAD); i++) {
556 if (deflates[i] !== undefined) continue
557 const { content, header } = entries[i]
558 if (header.type !== "File" || header.method !== "Deflate" || content === undefined) continue
559 if (i > from && content.length > DEFLATE_AHEAD_SIZE) continue
560 const deflating: Deflating = { promise: deflateBytes(content), data: undefined }
561 // Failures surface when the entry is written; deflates left behind by
562 // a failed encode finish on their own
563 deflating.promise.then((data) => {
564 deflating.data = data
565 }, () => {})
566 deflates[i] = deflating
567 }
568 }
569
570 const write = (f: () => void): Effect.Effect<void, ZipError> => {
571 try {
572 f()
573 } catch (error) {
574 return isZipError(error) ? Effect.fail(error) : Effect.die(error)
575 }
576 return Effect.void
577 }
578
579 return Effect.gen(function*() {
580 while (true) {
581 if (writer.buffered >= SLAB_SIZE) return writer.flush()
582 if (body !== undefined) {
583 const chunks = yield* Pull.catchDone(body, () => Effect.undefined)

Callers

nothing calls this directly

Calls 15

flushMethod · 0.95
dataMethod · 0.95
endMethod · 0.95
entryMethod · 0.95
beginMethod · 0.95
updateMethod · 0.95
finishMethod · 0.95
startDeflatesFunction · 0.85
deflateBytesFunction · 0.85
deflateFunction · 0.85
writeFunction · 0.70
validateFunction · 0.70

Tested by

no test coverage detected