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
18 changes: 18 additions & 0 deletions .changeset/effect-4-1-crypto.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 5 additions & 1 deletion packages/redis/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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<string>` with strict UTF-8 decoding; `getBytes` returns
Expand Down
2 changes: 1 addition & 1 deletion packages/redis/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
"vitest": "catalog:"
},
"peerDependencies": {
"effect": "catalog:"
"effect": "catalog:peer"
},
"engines": {
"node": ">=24.22.0"
Expand Down
5 changes: 3 additions & 2 deletions packages/redis/src/client/Native.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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<Transport>,
crypto: Layer.Layer<Crypto.Crypto>,
crypto: Layer.Layer<Digest.Digest | SecureRandom.SecureRandom>,
) => {
const layer = <R = never>(input: NativeClientInput<R>) =>
standaloneLayer(input).pipe(Layer.provide(transport), Layer.provideMerge(crypto));
Expand Down
39 changes: 20 additions & 19 deletions packages/redis/src/platform/bun/Crypto.ts
Original file line number Diff line number Diff line change
@@ -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<Digest.Digest | SecureRandom.SecureRandom> = 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),
}),
}),
}),
}),
),
);
30 changes: 13 additions & 17 deletions packages/redis/src/platform/node/Crypto.ts
Original file line number Diff line number Diff line change
@@ -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<Digest.Digest | SecureRandom.SecureRandom> = 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)),
);
11 changes: 7 additions & 4 deletions packages/redis/src/scripting/RedisScript.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -20,12 +21,14 @@ export const make = Effect.fnUntraced(function* <A, E, R>(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<A, E, R>;
Expand Down
10 changes: 9 additions & 1 deletion packages/redis/test/fixtures/distribution/runtime.mjs
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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(
Expand Down
19 changes: 18 additions & 1 deletion packages/redis/test/integration/scripting/Descriptor.test.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion packages/redis/test/runners/artifacts/Consumers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" ||
Expand Down
10 changes: 6 additions & 4 deletions packages/redis/test/runners/bun/Client.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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",
Expand Down
14 changes: 4 additions & 10 deletions packages/redis/test/support/commands/Crypto.ts
Original file line number Diff line number Diff line change
@@ -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)),
);
5 changes: 3 additions & 2 deletions packages/redis/test/types/Bun.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -27,7 +28,7 @@ type Assert<T extends true> = T;
export type BunContracts = [
Assert<Equal<Effect.Success<typeof acquired>, TransportConnection>>,
Assert<Equal<Effect.Services<typeof acquired>, Scope.Scope>>,
Assert<Equal<Effect.Services<typeof script>, Crypto.Crypto>>,
Assert<Equal<Effect.Services<typeof script>, Digest.Digest>>,
Assert<Equal<Effect.Services<typeof hashed>, never>>,
Assert<Equal<Effect.Success<typeof runnable>, bigint>>,
Assert<Equal<Effect.Services<typeof runnable>, never>>,
Expand Down
4 changes: 2 additions & 2 deletions packages/redis/test/types/Scripting.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -20,7 +20,7 @@ type Equal<A, B> =
(<T>() => T extends A ? 1 : 2) extends <T>() => T extends B ? 1 : 2 ? true : false;
type Assert<T extends true> = T;
export type ScriptingContracts = [
Assert<Equal<Effect.Services<typeof descriptor>, Crypto.Crypto>>,
Assert<Equal<Effect.Services<typeof descriptor>, Digest.Digest>>,
Assert<Equal<Effect.Services<typeof executed>, DecoderContext>>,
Assert<Equal<Effect.Success<typeof executed>, string>>,
Assert<Equal<Effect.Services<typeof direct>, DecoderContext>>,
Expand Down
Loading
Loading