From 6717e6b0de62e8bb3e7777b153f38004da42fab0 Mon Sep 17 00:00:00 2001 From: Szymon Nowakowski Date: Thu, 9 Oct 2025 16:43:59 +0200 Subject: [PATCH] Fix more errors, Uint8Array disambiguation --- packages/backend/src/services/Database.ts | 2 +- packages/common/src/Cbor.test.ts | 5 +++-- packages/common/src/index.ts | 16 ++++++++++++++-- packages/common/src/the_api.ts | 6 +++--- packages/frontend/src/routes/Repertoires.tsx | 2 +- 5 files changed, 22 insertions(+), 9 deletions(-) diff --git a/packages/backend/src/services/Database.ts b/packages/backend/src/services/Database.ts index c61c8ae..d7b929e 100644 --- a/packages/backend/src/services/Database.ts +++ b/packages/backend/src/services/Database.ts @@ -40,7 +40,7 @@ export interface AttachmentTable extends AttachmentData, SystemInformation { export interface FileTable { sha256: ColumnType; - data: ColumnType; + data: ColumnType, Uint8Array, never>; } export interface OptionTable { diff --git a/packages/common/src/Cbor.test.ts b/packages/common/src/Cbor.test.ts index 6951581..7782157 100644 --- a/packages/common/src/Cbor.test.ts +++ b/packages/common/src/Cbor.test.ts @@ -2,6 +2,7 @@ import { describe, test } from "bun:test"; import * as cbor from "cbor2"; +import { Uint8ArrayArrayBufferFromSelf } from "common"; import { Effect, Schema } from "effect"; import * as Cbor from "./Cbor"; import * as Test from "./Test"; @@ -61,7 +62,7 @@ describe("encodeSchema", () => { const schema = Schema.Struct({ x: Schema.String, y: Schema.DateFromSelf, - z: Schema.Uint8ArrayFromSelf, + z: Uint8ArrayArrayBufferFromSelf, }); const object = schema.make({ @@ -84,7 +85,7 @@ describe("decodeSchema", () => { const schema = Schema.Struct({ x: Schema.String, y: Schema.DateFromSelf, - z: Schema.Uint8ArrayFromSelf, + z: Uint8ArrayArrayBufferFromSelf, }); const object = schema.make({ diff --git a/packages/common/src/index.ts b/packages/common/src/index.ts index e61a084..cf5ce7d 100644 --- a/packages/common/src/index.ts +++ b/packages/common/src/index.ts @@ -1,7 +1,19 @@ -import { pipe, Schema } from "effect"; +import { Arbitrary, Array, Equal, Equivalence, pipe, Predicate, Pretty, Schema } from "effect"; + +export const isUint8ArrayArrayBuffer = (input: unknown): input is Uint8Array => input instanceof Uint8Array && input.buffer instanceof ArrayBuffer; + +export class Uint8ArrayArrayBufferFromSelf extends Schema.declare( + isUint8ArrayArrayBuffer, + { + identifier: "Uint8ArrayArrayBufferFromSelf", + pretty: (): Pretty.Pretty> => (u8arr) => `new Uint8Array(${JSON.stringify(globalThis.Array.from(u8arr))})`, + arbitrary: (): Arbitrary.LazyArbitrary> => (fc) => fc.uint8Array() as any, + equivalence: (): Equivalence.Equivalence => Array.getEquivalence(Equal.equals) as any, + }, +) { } export const Sha256 = pipe( - Schema.Uint8ArrayFromSelf, + Uint8ArrayArrayBufferFromSelf, Schema.filter((arr) => arr.byteLength === 32), Schema.brand("Sha256"), ); diff --git a/packages/common/src/the_api.ts b/packages/common/src/the_api.ts index 8ec640a..2d9ecf7 100644 --- a/packages/common/src/the_api.ts +++ b/packages/common/src/the_api.ts @@ -1,4 +1,4 @@ -import { AttachmentId, PieceId, RepertoireId, Sha256, UserId } from "common"; +import { AttachmentId, PieceId, RepertoireId, Sha256, Uint8ArrayArrayBufferFromSelf, UserId } from "common"; import { pipe, Schema } from "effect"; import { constant } from "effect/Function"; import * as Api from "./Api"; @@ -173,7 +173,7 @@ export default Api.bundle({ pieceId: PieceId, filename: Schema.NonEmptyString, mediaType: Schema.NonEmptyString, - data: Schema.Uint8ArrayFromSelf, + data: Uint8ArrayArrayBufferFromSelf, }), Attachment, Schema.Union(Unauthenticated, Unauthorized, NotFound), @@ -183,7 +183,7 @@ export default Api.bundle({ Schema.Struct({ filename: Schema.NonEmptyString, mediaType: Schema.NonEmptyString, - data: Schema.Uint8ArrayFromSelf, + data: Uint8ArrayArrayBufferFromSelf, }), Schema.Union(Unauthenticated, Unauthorized, NotFound), ), diff --git a/packages/frontend/src/routes/Repertoires.tsx b/packages/frontend/src/routes/Repertoires.tsx index 1640d89..6d3685c 100644 --- a/packages/frontend/src/routes/Repertoires.tsx +++ b/packages/frontend/src/routes/Repertoires.tsx @@ -21,7 +21,7 @@ export function Repertoires() { const { isLoading, error, data: repertoireIds } = useLoading(Effect.gen(function* () { yield* debounce.current; - const data = yield* client.queryRepertoire({ + const data = yield* client.queryRepertoires({ name: name !== "" ? Option.some(name) : Option.none(), offset: 0, limit: 100,