MCPcopy
hub / github.com/Effect-TS/effect / toPersisted

Function toPersisted

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

Source from the content-addressed store, hash-verified

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