diff --git a/.changeset/effect-4-1-crypto.md b/.changeset/effect-4-1-crypto.md new file mode 100644 index 0000000..04ee717 --- /dev/null +++ b/.changeset/effect-4-1-crypto.md @@ -0,0 +1,18 @@ +--- +"@effect-kit/redis": patch +--- + +Support Effect 4.1. Effect 4.1 removed the root `Crypto` module, so importing any +entrypoint of 0.1.1 with `effect@4.1` failed with +`The requested module 'effect' does not provide an export named 'Crypto'`. + +Script hashing now uses the `effect/crypto` services: + +- `RedisScript.make` requires `Digest.Digest` (from `effect/crypto`) instead of `Crypto.Crypto`. +- `cryptoLayer` from `@effect-kit/redis/node` and `@effect-kit/redis/bun` now provides + native `Digest.Digest` and `SecureRandom.SecureRandom` instead of `Crypto.Crypto`. + `RedisNode`/`RedisBun` Layers expose the same services. +- Digest failures are defects, matching the `Digest` service contract. + +The `effect` peer range is now `^4.1.0`; Effect 4.0 is no longer supported because its +`effect` package has no `effect/crypto` entrypoint. diff --git a/AGENTS.md b/AGENTS.md index a417cc6..4d61726 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -13,7 +13,7 @@ - Library: `packages/redis`; npm name: `@effect-kit/redis`. Use pnpm and the Node/Bun versions pinned in the repository. Keep the workspace root private. -- Effect v4 is the only runtime peer; the supported range is `^4.0.6`. Changes to +- Effect v4 is the only runtime peer; the supported range is `^4.1.0`. Changes to the minimum require behavior tests, including interruption and cleanup. - Use catalog ranges and commit `pnpm-lock.yaml`. CI installs with `--frozen-lockfile`. Do not add dependencies when existing tools/platform APIs suffice. diff --git a/packages/redis/README.md b/packages/redis/README.md index 9c5eb9c..9c6e8f2 100644 --- a/packages/redis/README.md +++ b/packages/redis/README.md @@ -16,7 +16,7 @@ bun add @effect-kit/redis effect yarn add @effect-kit/redis effect ``` -Requires Effect `^4.0.6` and ESM. Tested native runtimes: Node.js 24.22.0 and Bun 1.4.4. +Requires Effect `^4.1.0` and ESM. Tested native runtimes: Node.js 24.22.0 and Bun 1.4.4. ## Quick start @@ -96,6 +96,10 @@ tracking and client-side topology routing require native sockets. HTTP transacti reject ambiguous raw/custom/script replies. Binary request positions are limited; use native Pub/Sub for binary or multiline messages. +`RedisScript.make` hashes sources with Effect's `Digest` service from `effect/crypto`. +`RedisNode`/`RedisBun` Layers and the `cryptoLayer` exported by `/node` and `/bun` +provide native `Digest` and `SecureRandom`; elsewhere, use `Digest.layerWebCrypto`. + ## Typed values and commands - `get` returns `Option` with strict UTF-8 decoding; `getBytes` returns diff --git a/packages/redis/package.json b/packages/redis/package.json index ce96cf3..11ded64 100644 --- a/packages/redis/package.json +++ b/packages/redis/package.json @@ -106,7 +106,7 @@ "vitest": "catalog:" }, "peerDependencies": { - "effect": "catalog:" + "effect": "catalog:peer" }, "engines": { "node": ">=24.22.0" diff --git a/packages/redis/src/client/Native.ts b/packages/redis/src/client/Native.ts index 1e9f753..2367dc2 100644 --- a/packages/redis/src/client/Native.ts +++ b/packages/redis/src/client/Native.ts @@ -1,5 +1,6 @@ import { Effect, Layer } from "effect"; -import type { Config, Crypto } from "effect"; +import type { Config } from "effect"; +import type { Digest, SecureRandom } from "effect/crypto"; import type { NativeClientInput, NativeClusterInput } from "#/client/Input"; import { standaloneLayer } from "#/client/Standalone"; @@ -10,7 +11,7 @@ import type { Transport } from "#/transport/Transport"; /** Runtime entrypoints supply platform services; universal modules never choose a runtime. */ export const nativeLayers = ( transport: Layer.Layer, - crypto: Layer.Layer, + crypto: Layer.Layer, ) => { const layer = (input: NativeClientInput) => standaloneLayer(input).pipe(Layer.provide(transport), Layer.provideMerge(crypto)); diff --git a/packages/redis/src/platform/bun/Crypto.ts b/packages/redis/src/platform/bun/Crypto.ts index e2e6661..1d26f0c 100644 --- a/packages/redis/src/platform/bun/Crypto.ts +++ b/packages/redis/src/platform/bun/Crypto.ts @@ -1,35 +1,36 @@ -import { Crypto, Effect, Layer, PlatformError, Redacted } from "effect"; +import { Effect, Layer } from "effect"; +import { Digest, SecureRandom } from "effect/crypto"; + +import { CryptoHasher } from "bun"; const algorithms = { + MD5: "md5", "SHA-1": "sha1", "SHA-256": "sha256", "SHA-384": "sha384", "SHA-512": "sha512", } as const; -import { CryptoHasher } from "bun"; +const hasher = (algorithm: Digest.Algorithm, bytes: Uint8Array) => + new CryptoHasher(algorithms[algorithm.name]).update(bytes); /** Native hashing plus Web Crypto entropy; no Node compatibility imports are needed. */ -export const cryptoLayer = Layer.succeed( - Crypto.Crypto, - Crypto.make({ - randomBytes: (size) => { +export const cryptoLayer: Layer.Layer = Layer.mergeAll( + Layer.succeed(Digest.Digest, { + // Digest has no error channel; a native hashing failure is a defect. + digest: (algorithm, bytes) => + Effect.sync(() => new Uint8Array(hasher(algorithm, bytes).digest())), + digestHex: (algorithm, bytes) => Effect.sync(() => hasher(algorithm, bytes).digest("hex")), + }), + // getRandomValues rejects requests above 65,536 bytes; SecureRandom.make checks sizes first. + Layer.succeed( + SecureRandom.SecureRandom, + SecureRandom.make((size) => { const bytes = new Uint8Array(size); for (let offset = 0; offset < size; offset += 65536) globalThis.crypto.getRandomValues(bytes.subarray(offset, offset + 65536)); return bytes; - }, - digest: (algorithm, bytes) => - Effect.try({ - try: () => new Uint8Array(new CryptoHasher(algorithms[algorithm]).update(bytes).digest()), - catch: (cause) => - PlatformError.systemError({ - _tag: "Unknown", - module: "Crypto", - method: "digest", - cause: Redacted.make(cause), - }), - }), - }), + }), + ), ); diff --git a/packages/redis/src/platform/node/Crypto.ts b/packages/redis/src/platform/node/Crypto.ts index 08b5c76..410cb2e 100644 --- a/packages/redis/src/platform/node/Crypto.ts +++ b/packages/redis/src/platform/node/Crypto.ts @@ -1,22 +1,18 @@ import { createHash, randomBytes } from "node:crypto"; -import { Crypto, Effect, Layer, PlatformError, Redacted } from "effect"; +import { Effect, Layer } from "effect"; +import { Digest, SecureRandom } from "effect/crypto"; -/** Optional platform service for RedisScript.make; ordinary Redis connections do not require it. */ -export const cryptoLayer = Layer.succeed( - Crypto.Crypto, - Crypto.make({ - randomBytes, - digest: (algorithm, bytes) => - Effect.try({ - try: () => new Uint8Array(createHash(algorithm.replace("-", "")).update(bytes).digest()), - catch: (cause) => - PlatformError.systemError({ - _tag: "Unknown", - module: "Crypto", - method: "digest", - cause: Redacted.make(cause), - }), - }), +const digestBytes = (algorithm: Digest.Algorithm, bytes: Uint8Array) => + new Uint8Array(createHash(algorithm.name.replace("-", "").toLowerCase()).update(bytes).digest()); + +/** Optional platform services for RedisScript.make; ordinary Redis connections do not require them. */ +export const cryptoLayer: Layer.Layer = Layer.mergeAll( + Layer.succeed(Digest.Digest, { + // Digest has no error channel; a native hashing failure is a defect. + digest: (algorithm, bytes) => Effect.sync(() => digestBytes(algorithm, bytes)), + digestHex: (algorithm, bytes) => + Effect.sync(() => Buffer.from(digestBytes(algorithm, bytes)).toString("hex")), }), + Layer.succeed(SecureRandom.SecureRandom, SecureRandom.make(randomBytes)), ); diff --git a/packages/redis/src/scripting/RedisScript.ts b/packages/redis/src/scripting/RedisScript.ts index 65cfc14..91f6259 100644 --- a/packages/redis/src/scripting/RedisScript.ts +++ b/packages/redis/src/scripting/RedisScript.ts @@ -1,4 +1,5 @@ -import { Crypto, Effect, Redacted, Schema } from "effect"; +import { Effect, Redacted, Schema } from "effect"; +import { Digest } from "effect/crypto"; import type { ReplyDecoder } from "#/command/Command"; import { Argument, parse } from "#/commands/internal/Input"; @@ -20,12 +21,14 @@ export const make = Effect.fnUntraced(function* (input: { const body = yield* parse(Argument, input.source); const readOnly = yield* parse(Schema.Boolean, input.readOnly ?? false); const source = typeof body === "string" ? new TextEncoder().encode(body) : new Uint8Array(body); - const crypto = yield* Crypto.Crypto; - const digest = yield* crypto.digest("SHA-1", source); + const digest = yield* Digest.Digest; + + // Hex is derived locally so custom Digest services cannot change the Redis digest format. + const bytes = yield* digest.digest(Digest.sha1, source); return { source: Redacted.make(source), - sha1: Array.from(digest, (byte) => byte.toString(16).padStart(2, "0")).join(""), + sha1: Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join(""), readOnly, decode: input.decode, } satisfies RedisScript; diff --git a/packages/redis/test/fixtures/distribution/runtime.mjs b/packages/redis/test/fixtures/distribution/runtime.mjs index 431880e..ba5c8d0 100644 --- a/packages/redis/test/fixtures/distribution/runtime.mjs +++ b/packages/redis/test/fixtures/distribution/runtime.mjs @@ -1,6 +1,7 @@ import assert from "node:assert/strict"; import { Effect, Layer } from "effect"; +import { SecureRandom } from "effect/crypto"; import { Commands, RedisProfiles, RedisScript, RedisTelemetry, Reply } from "@effect-kit/redis"; import { decodeUpstashConfig } from "@effect-kit/redis/upstash"; @@ -23,7 +24,14 @@ const script = await Effect.runPromise( Effect.provide(adapter.cryptoLayer), ), ); -assert.match(script.sha1, /^[a-f0-9]{40}$/); +assert.equal(script.sha1, "e0e1f9fabfc9d4800c877a703b823ac0578ff8db"); +const random = await Effect.runPromise( + SecureRandom.SecureRandom.pipe( + Effect.flatMap((service) => service.randomBytes(65537)), + Effect.provide(adapter.cryptoLayer), + ), +); +assert.equal(random.length, 65537); await Effect.runPromise(Effect.scoped(Layer.build(RedisTelemetry.layer({ tracing: true })))); assert.equal(typeof decodeUpstashConfig, "function"); console.log( diff --git a/packages/redis/test/integration/scripting/Descriptor.test.ts b/packages/redis/test/integration/scripting/Descriptor.test.ts index 1d716ef..89635ea 100644 --- a/packages/redis/test/integration/scripting/Descriptor.test.ts +++ b/packages/redis/test/integration/scripting/Descriptor.test.ts @@ -1,12 +1,13 @@ import { expect, it } from "@effect/vitest"; import { Effect, Inspectable, Redacted } from "effect"; +import { Digest, SecureRandom } from "effect/crypto"; import { makeRedisScripts, RedisScript, Reply } from "#/index"; import { cryptoLayer } from "#/node"; import { bytes } from "#test/support/connection/Controlled"; import { sessionFixture } from "#test/support/sessions/Fixture"; -it.effect("SCRIPT: native Crypto hashes an owned source snapshot without exposing its body", () => +it.effect("SCRIPT: native Digest hashes an owned source snapshot without exposing its body", () => Effect.gen(function* () { const source = bytes("return 1"); const descriptor = yield* RedisScript.make({ source, decode: Reply.integer }).pipe( @@ -20,6 +21,22 @@ it.effect("SCRIPT: native Crypto hashes an owned source snapshot without exposin }), ); +it.effect("SCRIPT: native cryptoLayer provides Effect Digest and SecureRandom services", () => + Effect.gen(function* () { + const digest = yield* Digest.Digest; + const random = yield* SecureRandom.SecureRandom; + const abc = bytes("abc"); + + expect(yield* digest.digestHex(Digest.sha256, abc)).toBe( + "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", + ); + expect(yield* digest.digest(Digest.sha1, abc)).toEqual( + Uint8Array.from(Buffer.from("a9993e364706816aba3e25717850c26c9cd0d89d", "hex")), + ); + expect(yield* random.randomBytes(65537)).toHaveLength(65537); + }).pipe(Effect.provide(cryptoLayer)), +); + it.effect("SCRIPT: binary recovery snapshots are bounded before allocation or command writes", () => Effect.gen(function* () { const { sessions, primary } = yield* sessionFixture(); diff --git a/packages/redis/test/runners/artifacts/Consumers.ts b/packages/redis/test/runners/artifacts/Consumers.ts index 18a3f51..dd5c8d1 100644 --- a/packages/redis/test/runners/artifacts/Consumers.ts +++ b/packages/redis/test/runners/artifacts/Consumers.ts @@ -104,7 +104,7 @@ export const checkConsumers = async ( manifest.name !== "@effect-kit/redis" || manifest.private === true || manifest.dependencies !== undefined || - manifest.peerDependencies?.effect !== "^4.0.6" || + manifest.peerDependencies?.effect !== "^4.1.0" || manifest.license !== "Apache-2.0" || manifest.repository?.url !== "git+https://github.com/effect-kit/redis.git" || manifest.publishConfig?.access !== "public" || diff --git a/packages/redis/test/runners/bun/Client.ts b/packages/redis/test/runners/bun/Client.ts index dd7f695..12f03bd 100644 --- a/packages/redis/test/runners/bun/Client.ts +++ b/packages/redis/test/runners/bun/Client.ts @@ -1,4 +1,5 @@ -import { Crypto, Effect, Option, Result, Schema, Stream } from "effect"; +import { Effect, Option, Result, Schema, Stream } from "effect"; +import { Digest, SecureRandom } from "effect/crypto"; import { readyClient } from "#test/runners/bun/Ready"; import { core, ensure, equalBytes } from "#test/runners/bun/Runtime"; @@ -9,12 +10,13 @@ export const client = Effect.fnUntraced(function* (input: unknown) { const redis = core.makeRedis(core.makeExecutor(connection)); const prefix = `bun:${crypto.randomUUID()}`; const bytes = new Uint8Array([0, 255, 128, 13, 10]); - const cryptography = yield* Crypto.Crypto; + const random = yield* SecureRandom.SecureRandom; + const hashing = yield* Digest.Digest; ensure( - (yield* cryptography.randomBytes(65537)).length === 65537, + (yield* random.randomBytes(65537)).length === 65537, "Native entropy crossed the Web Crypto quota incorrectly", ); - const digest = yield* cryptography.digest("SHA-256", new TextEncoder().encode("abc")); + const digest = yield* hashing.digest(Digest.sha256, new TextEncoder().encode("abc")); ensure( Array.from(digest, (byte) => byte.toString(16).padStart(2, "0")).join("") === "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", diff --git a/packages/redis/test/support/commands/Crypto.ts b/packages/redis/test/support/commands/Crypto.ts index ca25a1b..9a12436 100644 --- a/packages/redis/test/support/commands/Crypto.ts +++ b/packages/redis/test/support/commands/Crypto.ts @@ -1,15 +1,9 @@ import { webcrypto } from "node:crypto"; -import { Crypto, Effect, Layer } from "effect"; +import { Layer } from "effect"; +import { Digest, SecureRandom, WebCrypto } from "effect/crypto"; /** Real platform cryptography for public script descriptor consumers. */ -export const cryptoLayer = Layer.succeed( - Crypto.Crypto, - Crypto.make({ - randomBytes: (size) => webcrypto.getRandomValues(new Uint8Array(size)), - digest: (algorithm, bytes) => - Effect.promise( - async () => new Uint8Array(await webcrypto.subtle.digest(algorithm, bytes.slice())), - ), - }), +export const cryptoLayer = Layer.mergeAll(Digest.layerWebCrypto, SecureRandom.layerWebCrypto).pipe( + Layer.provide(WebCrypto.layer(webcrypto as Crypto)), ); diff --git a/packages/redis/test/types/Bun.ts b/packages/redis/test/types/Bun.ts index 0059dbf..dd45276 100644 --- a/packages/redis/test/types/Bun.ts +++ b/packages/redis/test/types/Bun.ts @@ -1,5 +1,6 @@ import { Effect, Layer } from "effect"; -import type { Scope, Crypto } from "effect"; +import type { Scope } from "effect"; +import type { Digest } from "effect/crypto"; import { connect, cryptoLayer, layer } from "#/bun"; import { CommandExecutor, Connection, Redis, RedisScript, Reply } from "#/index"; @@ -27,7 +28,7 @@ type Assert = T; export type BunContracts = [ Assert, TransportConnection>>, Assert, Scope.Scope>>, - Assert, Crypto.Crypto>>, + Assert, Digest.Digest>>, Assert, never>>, Assert, bigint>>, Assert, never>>, diff --git a/packages/redis/test/types/Scripting.ts b/packages/redis/test/types/Scripting.ts index 764a01c..59d14b4 100644 --- a/packages/redis/test/types/Scripting.ts +++ b/packages/redis/test/types/Scripting.ts @@ -1,5 +1,5 @@ import { Context, Effect } from "effect"; -import type { Crypto } from "effect"; +import type { Digest } from "effect/crypto"; import { Commands, RedisScript, RedisScripts, Reply } from "#/index"; import type { RedisClient, RedisScriptsClient } from "#/index"; @@ -20,7 +20,7 @@ type Equal = (() => T extends A ? 1 : 2) extends () => T extends B ? 1 : 2 ? true : false; type Assert = T; export type ScriptingContracts = [ - Assert, Crypto.Crypto>>, + Assert, Digest.Digest>>, Assert, DecoderContext>>, Assert, string>>, Assert, DecoderContext>>, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7797deb..16279a6 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -174,8 +174,8 @@ catalogs: specifier: ^21.2.4 version: 21.2.4 '@effect/vitest': - specifier: ^4.0.6 - version: 4.0.6 + specifier: 4.1.0 + version: 4.1.0 '@types/bun': specifier: '>=1.4.4 <1.4.5' version: 1.4.4 @@ -186,8 +186,8 @@ catalogs: specifier: ^2.5.3 version: 2.5.3 effect: - specifier: ^4.0.6 - version: 4.0.6 + specifier: 4.1.0 + version: 4.1.0 ioredis: specifier: ^6.0.0 version: 6.0.0 @@ -234,7 +234,7 @@ importers: dependencies: effect: specifier: 'catalog:' - version: 4.0.6 + version: 4.1.0 devDependencies: '@changesets/cli': specifier: 'catalog:' @@ -277,7 +277,7 @@ importers: devDependencies: '@effect/vitest': specifier: 'catalog:' - version: 4.0.6(effect@4.0.6)(vitest@5.0.5(@types/node@24.19.2)(vite@8.3.5(@types/node@24.19.2)(jiti@2.7.0)(yaml@2.9.1))) + version: 4.1.0(effect@4.1.0)(vitest@5.0.5(@types/node@24.19.2)(vite@8.3.5(@types/node@24.19.2)(jiti@2.7.0)(yaml@2.9.1))) '@types/bun': specifier: 'catalog:' version: 1.4.4 @@ -289,7 +289,7 @@ importers: version: 2.5.3 effect: specifier: 'catalog:' - version: 4.0.6 + version: 4.1.0 ioredis: specifier: 'catalog:' version: 6.0.0 @@ -488,10 +488,10 @@ packages: resolution: {integrity: sha512-aalGyl7dbB5PArRebDIX43ZvBlXrYm9uWzGJ26t+4SzJVPsOuvfILGGbw5X4yX7i50YEmJ8zvbiWnqH/AAnZqg==} engines: {node: '>=22'} - '@effect/vitest@4.0.6': - resolution: {integrity: sha512-oKNbtyUHK5+0o730CXwojULEu7f3+c235pYFD4U5T//o+hHrJbYbZU4bRkcv936tPr5IOnmujeyQiC/LuJJjHw==} + '@effect/vitest@4.1.0': + resolution: {integrity: sha512-+APF69oTYgGj5WCewowk1/udepH74xz4ONyH7d+UfcK0SrF3osiSwsWEyODUpJvWd6MIk5F5PcFJiE4evMuG2A==} peerDependencies: - effect: ^4.0.6 + effect: ^4.1.0 vitest: '>=5.0.0 <6.0.0' '@grpc/grpc-js@1.14.6': @@ -1535,8 +1535,8 @@ packages: eastasianwidth@0.2.0: resolution: {integrity: sha512-I88TYZWc9XiYHRQ4/3c5rjjfgkjhLyW2luGIheGERbNQ6OY7yTybanSpDXZa8y7VUP9YmDcYa+eyq4ca7iLqWA==} - effect@4.0.6: - resolution: {integrity: sha512-Tc3mkzuLR6t6P9dhLt0D2GrxYtmU5td4XEJEiXCmILz53IRIfJyHsKZWjTA3xEjiwpNTtXBgI4iqYPxMztBDrw==} + effect@4.1.0: + resolution: {integrity: sha512-lGx2MJJyrXA8RZb2UpQ/0Nkb5Utaimby7oD92+18noBxLPE85IFOvptdzau8dvcxAOwfwHo9iqapbKeFzqQM/Q==} emoji-regex@10.6.0: resolution: {integrity: sha512-toUI84YS5YmxW219erniWD0CIVOo46xGKColeNQRgOzDorgBi1v4D71/OFzgD9GO2UGKIv1C3Sp8DAn0+j5w7A==} @@ -2746,9 +2746,9 @@ snapshots: '@conventional-changelog/template@1.4.0': {} - '@effect/vitest@4.0.6(effect@4.0.6)(vitest@5.0.5(@types/node@24.19.2)(vite@8.3.5(@types/node@24.19.2)(jiti@2.7.0)(yaml@2.9.1)))': + '@effect/vitest@4.1.0(effect@4.1.0)(vitest@5.0.5(@types/node@24.19.2)(vite@8.3.5(@types/node@24.19.2)(jiti@2.7.0)(yaml@2.9.1)))': dependencies: - effect: 4.0.6 + effect: 4.1.0 vitest: 5.0.5(@types/node@24.19.2)(vite@8.3.5(@types/node@24.19.2)(jiti@2.7.0)(yaml@2.9.1)) '@grpc/grpc-js@1.14.6': @@ -3482,7 +3482,7 @@ snapshots: eastasianwidth@0.2.0: {} - effect@4.0.6: {} + effect@4.1.0: {} emoji-regex@10.6.0: {} diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index 7ad5d51..d7aedf1 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -2,8 +2,8 @@ packages: - packages/* catalog: - effect: "^4.0.6" - "@effect/vitest": "^4.0.6" + effect: "4.1.0" + "@effect/vitest": "4.1.0" vitest: "^5.0.5" vite: "^8.3.5" klarity: "^0.3.0" @@ -24,6 +24,11 @@ catalog: redis: "^6.3.0" "@valkey/valkey-glide": "^2.5.3" +# Published peer range; development and tests pin the minimum supported Effect release. +catalogs: + peer: + effect: "^4.1.0" + catalogMode: strict saveExact: false savePrefix: "^"