Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tough-rooms-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

Reject NDJSON values without a JSON representation.
10 changes: 9 additions & 1 deletion packages/effect/src/unstable/encoding/Ndjson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,15 @@ export const encodeString = <IE = never, Done = unknown>(): Channel.Channel<
Channel.fromTransform((upstream, _scope) =>
Effect.succeed(Effect.flatMap(upstream, (input) => {
try {
return Effect.succeed(Arr.of(input.map((item) => JSON.stringify(item)).join("\n") + "\n"))
return Effect.succeed(Arr.of(
input.map((item) => {
const output = JSON.stringify(item)
if (output === undefined) {
throw new TypeError("Value cannot be represented as JSON")
}
return output
}).join("\n") + "\n"
))
} catch (cause) {
return Effect.fail(new NdjsonError({ kind: "Pack", cause }))
}
Expand Down
16 changes: 16 additions & 0 deletions packages/effect/test/unstable/encoding/Ndjson.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ import * as Schema from "effect/Schema"
import * as Ndjson from "effect/unstable/encoding/Ndjson"

describe("Ndjson", () => {
it.effect("fails for values without a JSON representation", () =>
Effect.gen(function*() {
const inputs = [undefined, () => {}, Symbol("x")]

for (const input of inputs) {
const error = yield* Stream.make(input).pipe(
Stream.pipeThroughChannel(Ndjson.encodeString()),
Stream.runCollect,
Effect.flip
)

assert.instanceOf(error, Ndjson.NdjsonError)
assert.strictEqual(error.kind, "Pack")
}
}))

it.effect("decodeSchema decodes records split across Uint8Array chunks", () =>
Effect.gen(function*() {
const messages = yield* Stream.make(
Expand Down
Loading