{"version":3,"file":"engine.mjs","names":[],"sources":["../../../src/services/verify/engine.ts"],"sourcesContent":["import { createHash } from \"node:crypto\";\nimport { Context, Data, Effect, Layer } from \"effect\";\nimport type { ZodType } from \"zod\";\nimport { fetchRaw } from \"../../utils/http-client\";\n\nconst defaultVerifyFetch = (url: string): Promise<Response> =>\n  fetchRaw(url, undefined, \"30 seconds\");\n\nexport class IntegrityMismatch extends Data.TaggedError(\"IntegrityMismatch\")<{\n  readonly url: string;\n  readonly expected: string;\n  readonly actual: string;\n}> {}\n\nexport class ProvenanceUnknown extends Data.TaggedError(\"ProvenanceUnknown\")<{\n  readonly url: string;\n  readonly cause: unknown;\n  readonly status?: number;\n}> {}\n\nexport class SignatureInvalid extends Data.TaggedError(\"SignatureInvalid\")<{\n  readonly url: string;\n  readonly reason: string;\n}> {}\n\nexport type VerifyError = IntegrityMismatch | ProvenanceUnknown | SignatureInvalid;\n\nexport const isVerifyError = (error: unknown): error is VerifyError =>\n  error instanceof IntegrityMismatch ||\n  error instanceof ProvenanceUnknown ||\n  error instanceof SignatureInvalid;\n\nexport declare const verified: unique symbol;\n\nexport type Verified<T> = T & { readonly [verified]: true };\n\nexport const mark = <T>(value: T): Verified<T> => value as Verified<T>;\n\nexport type Digest = `sha384-${string}`;\n\nexport const asDigest = (sri: string): Digest => sri as Digest;\n\nexport type VerifiedBytes = Verified<Uint8Array>;\n\nexport interface VerifyCallOptions {\n  readonly fetchImpl?: typeof fetch;\n  readonly cache?: boolean;\n}\n\nexport interface VerifierShape {\n  readonly fetchVerified: (\n    url: string,\n    expected: Digest,\n    options?: VerifyCallOptions,\n  ) => Effect.Effect<VerifiedBytes, VerifyError>;\n  readonly verifyDocument: <T>(\n    url: string,\n    expected: Digest,\n    schema: ZodType<T>,\n    options?: VerifyCallOptions,\n  ) => Effect.Effect<Verified<T>, VerifyError>;\n  readonly clear: () => void;\n}\n\nconst verifiedCache = new Map<string, VerifiedBytes>();\n\nexport const clearVerifiedCache = (): void => {\n  verifiedCache.clear();\n};\n\nexport const computeSri = (content: string | Uint8Array): Digest =>\n  `sha384-${createHash(\"sha384\").update(content).digest(\"base64\")}`;\n\nconst textOf = (bytes: Uint8Array): string => new TextDecoder().decode(bytes);\n\nconst fetchVerifiedEffect = (\n  url: string,\n  expected: Digest,\n  options?: VerifyCallOptions,\n): Effect.Effect<VerifiedBytes, VerifyError> =>\n  Effect.gen(function* () {\n    if (options?.cache !== false) {\n      const cached = verifiedCache.get(`${url}::${expected}`);\n      if (cached) return cached;\n    }\n    const response = yield* Effect.tryPromise({\n      try: () => (options?.fetchImpl ?? defaultVerifyFetch)(url),\n      catch: (cause) => new ProvenanceUnknown({ url, cause }),\n    });\n    if (!response.ok) {\n      return yield* new ProvenanceUnknown({\n        url,\n        cause: response.statusText || `HTTP ${response.status}`,\n        status: response.status,\n      });\n    }\n    const bytes = new Uint8Array(\n      yield* Effect.tryPromise({\n        try: () => response.arrayBuffer(),\n        catch: (cause) => new ProvenanceUnknown({ url, cause }),\n      }),\n    );\n    const actual = computeSri(bytes);\n    if (actual !== expected) {\n      return yield* new IntegrityMismatch({ url, expected, actual });\n    }\n    const verifiedBytes = mark(bytes);\n    if (options?.cache !== false) verifiedCache.set(`${url}::${expected}`, verifiedBytes);\n    return verifiedBytes;\n  });\n\nconst verifyDocumentEffect = <T>(\n  url: string,\n  expected: Digest,\n  schema: ZodType<T>,\n  options?: VerifyCallOptions,\n): Effect.Effect<Verified<T>, VerifyError> =>\n  Effect.gen(function* () {\n    const bytes = yield* fetchVerifiedEffect(url, expected, options);\n    const document = yield* Effect.try({\n      try: () => schema.parse(JSON.parse(textOf(bytes))),\n      catch: (cause) => new ProvenanceUnknown({ url, cause }),\n    });\n    return mark(document);\n  });\n\nexport class Verifier extends Context.Service<Verifier, VerifierShape>()(\n  \"everything-dev/verify/Verifier\",\n) {\n  static readonly layer: Layer.Layer<Verifier> = Layer.succeed(\n    Verifier,\n    Verifier.of({\n      fetchVerified: (url, expected, options) => fetchVerifiedEffect(url, expected, options),\n      verifyDocument: (url, expected, schema, options) =>\n        verifyDocumentEffect(url, expected, schema, options),\n      clear: clearVerifiedCache,\n    }),\n  );\n}\n\nexport const fetchVerified = (input: {\n  url: string;\n  expected: Digest;\n  fetchImpl?: typeof fetch;\n  cache?: boolean;\n}): Promise<VerifiedBytes> =>\n  Effect.runPromise(\n    fetchVerifiedEffect(input.url, input.expected, {\n      fetchImpl: input.fetchImpl,\n      cache: input.cache,\n    }),\n  );\n\nexport const verifyDocument = <T>(input: {\n  url: string;\n  expected: Digest;\n  schema: ZodType<T>;\n  fetchImpl?: typeof fetch;\n  cache?: boolean;\n}): Promise<Verified<T>> =>\n  Effect.runPromise(\n    verifyDocumentEffect(input.url, input.expected, input.schema, {\n      fetchImpl: input.fetchImpl,\n      cache: input.cache,\n    }),\n  );\n"],"mappings":";;;;;AAKA,MAAM,sBAAsB,QAC1B,SAAS,KAAK,QAAW,YAAY;AAEvC,IAAa,oBAAb,cAAuC,KAAK,YAAY,mBAAmB,CAAC,CAIzE,CAAC;AAEJ,IAAa,oBAAb,cAAuC,KAAK,YAAY,mBAAmB,CAAC,CAIzE,CAAC;AAEJ,IAAa,mBAAb,cAAsC,KAAK,YAAY,kBAAkB,CAAC,CAGvE,CAAC;AAIJ,MAAa,iBAAiB,UAC5B,iBAAiB,qBACjB,iBAAiB,qBACjB,iBAAiB;AAMnB,MAAa,QAAW,UAA0B;AAIlD,MAAa,YAAY,QAAwB;AAwBjD,MAAM,gCAAgB,IAAI,IAA2B;AAErD,MAAa,2BAAiC;CAC5C,cAAc,MAAM;AACtB;AAEA,MAAa,cAAc,YACzB,UAAU,WAAW,QAAQ,CAAC,CAAC,OAAO,OAAO,CAAC,CAAC,OAAO,QAAQ;AAEhE,MAAM,UAAU,UAA8B,IAAI,YAAY,CAAC,CAAC,OAAO,KAAK;AAE5E,MAAM,uBACJ,KACA,UACA,YAEA,OAAO,IAAI,aAAa;CACtB,IAAI,SAAS,UAAU,OAAO;EAC5B,MAAM,SA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