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

Function toPersisted

packages/platform/src/Multipart.ts:514–557  ·  view source on GitHub ↗
(
  stream: Stream.Stream<Part, MultipartError>,
  writeFile = defaultWriteFile
)

Source from the content-addressed store, hash-verified

512 * @category Conversions
513 */
514export const toPersisted = (
515 stream: Stream.Stream<Part, MultipartError>,
516 writeFile = defaultWriteFile
517): Effect.Effect<Persisted, MultipartError, FileSystem.FileSystem | Path.Path | Scope.Scope> =>
518 Effect.gen(function*() {
519 const fs = yield* FileSystem.FileSystem
520 const path_ = yield* Path.Path
521 const dir = yield* fs.makeTempDirectoryScoped()
522 const persisted: Record<string, Array<PersistedFile> | Array<string> | string> = Object.create(null)
523 yield* Stream.runForEach(stream, (part) => {
524 if (part._tag === "Field") {
525 if (!(part.key in persisted)) {
526 persisted[part.key] = part.value
527 } else if (typeof persisted[part.key] === "string") {
528 persisted[part.key] = [persisted[part.key] as string, part.value]
529 } else {
530 ;(persisted[part.key] as Array<string>).push(part.value)
531 }
532 return Effect.void
533 } else if (part.name === "") {
534 return Effect.void
535 }
536 const file = part
537 const path = path_.join(dir, path_.basename(file.name).slice(-128))
538 const filePart = new PersistedFileImpl(
539 file.key,
540 file.name,
541 file.contentType,
542 path
543 )
544 if (Array.isArray(persisted[part.key])) {
545 ;(persisted[part.key] as Array<PersistedFile>).push(filePart)
546 } else {
547 persisted[part.key] = [filePart]
548 }
549 return writeFile(path, file)
550 })
551 return persisted
552 }).pipe(
553 Effect.catchTags({
554 SystemError: (cause) => Effect.fail(new MultipartError({ reason: "InternalError", cause })),
555 BadArgument: (cause) => Effect.fail(new MultipartError({ reason: "InternalError", cause }))
556 })
557 )
558
559class PersistedFileImpl extends PartBase implements PersistedFile {
560 readonly _tag = "PersistedFile"

Callers 1

Chat.tsFile · 0.85

Calls 5

createMethod · 0.80
pipeMethod · 0.65
joinMethod · 0.65
failMethod · 0.65
writeFileFunction · 0.50

Tested by

no test coverage detected