diff --git a/packages/cli/script/build.ts b/packages/cli/script/build.ts index c760d9a61b3..380fc6e8825 100755 --- a/packages/cli/script/build.ts +++ b/packages/cli/script/build.ts @@ -155,7 +155,7 @@ export default { path: file, version: ${JSON.stringify(opencodePty.version)}, sh const executablePath = await compileExecutable(item) console.log(`building ${name}`) const result = await Bun.build({ - entrypoints: ["./src/index.ts"], + entrypoints: ["./src/index.ts", "./src/photon-worker.ts"], tsconfig: "./tsconfig.json", plugins: [ appAssetsPlugin, diff --git a/packages/cli/src/photon-worker.ts b/packages/cli/src/photon-worker.ts new file mode 100644 index 00000000000..da96bdf481b --- /dev/null +++ b/packages/cli/src/photon-worker.ts @@ -0,0 +1,3 @@ +// Second compile entrypoint: it lands next to index.js and the shared chunks in +// the executable, where Image.Photon resolves "./photon-worker.js". +import "../../core/src/image/photon-worker" diff --git a/packages/core/src/image/photon-normalize.ts b/packages/core/src/image/photon-normalize.ts new file mode 100644 index 00000000000..de84616a59f --- /dev/null +++ b/packages/core/src/image/photon-normalize.ts @@ -0,0 +1,148 @@ +export * as PhotonNormalize from "./photon-normalize.js" + +import photonWasm from "#photon-wasm" +import path from "node:path" +import { fileURLToPath } from "node:url" +import type { FileSystem } from "../filesystem.js" +import type { Limits } from "../image.js" + +// Plain data on purpose: this module runs on the server thread and inside the +// photon worker, and its results cross postMessage. + +export type Photon = typeof import("@silvia-odwyer/photon-node") +export type Content = FileSystem.Content & { readonly encoding: "base64" } +export type Size = { + readonly width: number + readonly height: number + readonly bytes: number + readonly maxWidth: number + readonly maxHeight: number + readonly maxBytes: number +} +// Unchanged lets the worker skip copying the caller's base64 back. +export type Result = + | { readonly _tag: "Unchanged" } + | { readonly _tag: "Content"; readonly content: Content } + | { readonly _tag: "DecodeError" } + | ({ readonly _tag: "SizeError" } & Size) + +const PNG_PASSTHROUGH_MAX_BASE64_BYTES = 384 * 1024 +const SMALL_PNG_MAX_PIXELS = 256 * 256 +const JPEG_QUALITIES = [80, 70, 55, 40] + +// A runtime without a photon wasm artifact (#photon-wasm resolves to the empty +// string on workerd) has no resizer, by declaration. +export const available = photonWasm !== "" + +export async function load(): Promise { + if (!available) throw new Error("photon wasm is unavailable") + ;(globalThis as typeof globalThis & { __OPENCODE_PHOTON_WASM_PATH?: string }).__OPENCODE_PHOTON_WASM_PATH = + path.isAbsolute(photonWasm) ? photonWasm : fileURLToPath(new URL(photonWasm, import.meta.url)) + return await import("@silvia-odwyer/photon-node") +} + +// Base64 uses four bytes per three input bytes, including padding. +const base64Length = (bytes: Uint8Array) => Math.ceil(bytes.length / 3) * 4 + +// Samples an evenly spaced grid of up to 64×64 pixels. +function hasTransparency(pixels: Uint8Array, width: number, height: number) { + const columns = Math.min(64, width) + const rows = Math.min(64, height) + let transparent = 0 + for (let row = 0; row < rows; row++) { + const y = Math.floor(((row + 0.5) * height) / rows) + for (let column = 0; column < columns; column++) { + const x = Math.floor(((column + 0.5) * width) / columns) + if (pixels[(y * width + x) * 4 + 3]! < 250) transparent++ + } + } + return transparent / (columns * rows) > 0.05 +} + +export function normalize(photon: Photon, content: Content, limits: Readonly): Result { + let decoded: InstanceType + try { + decoded = photon.PhotonImage.new_from_byteslice(Buffer.from(content.content, "base64")) + } catch { + return { _tag: "DecodeError" } + } + try { + const width = decoded.get_width() + const height = decoded.get_height() + const bytes = Buffer.byteLength(content.content, "utf-8") + const sizeError: Result = { + _tag: "SizeError", + width, + height, + bytes, + maxWidth: limits.maxWidth, + maxHeight: limits.maxHeight, + maxBytes: limits.maxBase64Bytes, + } + const fits = width <= limits.maxWidth && height <= limits.maxHeight && bytes <= limits.maxBase64Bytes + const smallPng = bytes <= PNG_PASSTHROUGH_MAX_BASE64_BYTES || width * height <= SMALL_PNG_MAX_PIXELS + if (fits && (!limits.autoResize || content.mime !== "image/png" || smallPng)) return { _tag: "Unchanged" } + if (!limits.autoResize) return sizeError + const transparent = content.mime !== "image/jpeg" && hasTransparency(decoded.get_raw_pixels(), width, height) + // An image that already fits only changes when JPEG makes it smaller. + if (fits) { + if (transparent) return { _tag: "Unchanged" } + const jpeg = decoded.get_bytes_jpeg(JPEG_QUALITIES[0]!) + return base64Length(jpeg) < bytes + ? { + _tag: "Content", + content: { + ...content, + content: Buffer.from(jpeg).toString("base64"), + encoding: "base64", + mime: "image/jpeg", + }, + } + : { _tag: "Unchanged" } + } + const scale = Math.min(1, limits.maxWidth / width, limits.maxHeight / height) + const sizes = Array.from({ length: 32 }).reduce>((acc) => { + const previous = acc.at(-1) ?? { + width: Math.max(1, Math.round(width * scale)), + height: Math.max(1, Math.round(height * scale)), + } + const next = + acc.length === 0 + ? previous + : { + width: previous.width === 1 ? 1 : Math.max(1, Math.floor(previous.width * 0.75)), + height: previous.height === 1 ? 1 : Math.max(1, Math.floor(previous.height * 0.75)), + } + return acc.some((item) => item.width === next.width && item.height === next.height) ? acc : [...acc, next] + }, []) + for (const size of sizes) { + const resized = + size.width === width && size.height === height + ? decoded + : photon.resize(decoded, size.width, size.height, photon.SamplingFilter.Lanczos3) + try { + const pngEncoder = ["image/png", () => resized.get_bytes()] as const + const jpegEncoders = JPEG_QUALITIES.map( + (quality) => ["image/jpeg", () => resized.get_bytes_jpeg(quality)] as const, + ) + const pngFirst = transparent || size.width * size.height <= SMALL_PNG_MAX_PIXELS + const encoders: Array Uint8Array]> = pngFirst + ? [pngEncoder, ...jpegEncoders] + : [...jpegEncoders, pngEncoder] + for (const [mime, encode] of encoders) { + const candidate = encode() + if (base64Length(candidate) <= limits.maxBase64Bytes) + return { + _tag: "Content", + content: { ...content, content: Buffer.from(candidate).toString("base64"), encoding: "base64", mime }, + } + } + } finally { + if (resized !== decoded) resized.free() + } + } + return sizeError + } finally { + decoded.free() + } +} diff --git a/packages/core/src/image/photon-worker.ts b/packages/core/src/image/photon-worker.ts new file mode 100644 index 00000000000..060a792a13c --- /dev/null +++ b/packages/core/src/image/photon-worker.ts @@ -0,0 +1,26 @@ +import { parentPort } from "node:worker_threads" +import type { Limits } from "../image.js" +import { PhotonNormalize } from "./photon-normalize.js" + +export type Request = { + readonly content: PhotonNormalize.Content + readonly limits: Readonly +} +export type Reply = { readonly _tag: "Ready" } | { readonly _tag: "Done"; readonly result: PhotonNormalize.Result } + +const port = parentPort +if (port) { + // A photon load failure throws out of the worker before Ready, so the server + // falls back to resizing in-process. A throw while resizing (a wasm panic + // leaves the instance unusable) is left uncaught so it kills the worker + // instead of serving later requests from a poisoned heap. + void PhotonNormalize.load().then((photon) => { + port.on("message", (request: Request) => { + port.postMessage({ + _tag: "Done", + result: PhotonNormalize.normalize(photon, request.content, request.limits), + } satisfies Reply) + }) + port.postMessage({ _tag: "Ready" } satisfies Reply) + }) +} diff --git a/packages/core/src/image/photon.ts b/packages/core/src/image/photon.ts index 7357c0bf225..d1dd330fe34 100644 --- a/packages/core/src/image/photon.ts +++ b/packages/core/src/image/photon.ts @@ -1,147 +1,156 @@ -import photonWasm from "#photon-wasm" -import { Effect } from "effect" -import path from "node:path" -import { fileURLToPath } from "node:url" -import { FileSystem } from "../filesystem.js" +export * as Photon from "./photon.js" + +import { Effect, Exit, Schema, Semaphore } from "effect" +import type { Worker } from "node:worker_threads" +import type { FileSystem } from "../filesystem.js" import { DecodeError, ResizerUnavailableError, SizeError, type Limits } from "../image.js" +import { PhotonNormalize } from "./photon-normalize.js" +import type { Reply, Request } from "./photon-worker.js" -const PNG_PASSTHROUGH_MAX_BASE64_BYTES = 384 * 1024 -const SMALL_PNG_MAX_PIXELS = 256 * 256 -const JPEG_QUALITIES = [80, 70, 55, 40] +// The worker holds photon's wasm heap (hundreds of MB after a large image); +// terminating it when idle returns that memory to the OS. +const WORKER_IDLE_MILLIS = 10_000 -// Base64 uses four bytes per three input bytes, including padding. -const base64Length = (bytes: Uint8Array) => Math.ceil(bytes.length / 3) * 4 +export class WorkerUnavailableError extends Schema.TaggedError()( + "Image.Photon.WorkerUnavailableError", + { cause: Schema.Defect() }, +) {} -// Samples an evenly spaced grid of up to 64×64 pixels. -function hasTransparency(pixels: Uint8Array, width: number, height: number) { - const columns = Math.min(64, width) - const rows = Math.min(64, height) - let transparent = 0 - for (let row = 0; row < rows; row++) { - const y = Math.floor(((row + 0.5) * height) / rows) - for (let column = 0; column < columns; column++) { - const x = Math.floor(((column + 0.5) * width) / columns) - if (pixels[(y * width + x) * 4 + 3]! < 250) transparent++ +// Resolves once the worker has loaded photon. Anything else (no worker_threads, +// a bundle without the worker entry, photon failing to load) means the worker +// is unavailable, and a worker that never got ready is terminated. +export const spawnWorker = (url?: URL) => + Effect.tryPromise({ + try: async (signal) => { + const { Worker } = await import("node:worker_threads") + const worker = new Worker(url ?? new URL("./photon-worker.js", import.meta.url)) + // Only fires on interruption, which discards the worker even if it just got ready. + const abort = () => void worker.terminate() + if (signal.aborted) abort() + signal.addEventListener("abort", abort, { once: true }) + try { + await new Promise((resolve, reject) => { + worker.once("message", () => resolve()) + worker.once("error", reject) + worker.once("exit", (code) => reject(new Error(`photon worker exited with code ${code}`))) + }) + } catch (cause) { + void worker.terminate() + throw cause + } + return worker + }, + catch: (cause) => new WorkerUnavailableError({ cause }), + }) + +// A panic or an unreadable reply fails the request the way an in-process +// photon throw would. The worker is only ref'd while a request is pending. +const request = (worker: Worker, content: PhotonNormalize.Content, limits: Readonly) => + Effect.callback((resume) => { + const settle = (effect: Effect.Effect) => { + cleanup() + resume(effect) } - } - return transparent / (columns * rows) > 0.05 -} + const reply = (reply: Reply) => { + if (reply._tag === "Done") settle(Effect.succeed(reply.result)) + } + const error = (error: Error) => settle(Effect.die(new Error(error.message))) + const exit = (code: number) => settle(Effect.die(new Error(`photon worker exited with code ${code}`))) + const cleanup = () => { + worker.off("message", reply).off("messageerror", error).off("error", error).off("exit", exit) + worker.unref() + } + worker.on("message", reply).on("messageerror", error).on("error", error).on("exit", exit) + worker.ref() + worker.postMessage({ content, limits } satisfies Request) + return Effect.sync(cleanup) + }) + +export function makeWith(spawn: Effect.Effect, idleMillis = WORKER_IDLE_MILLIS) { + // One request at a time, so a worker never holds a queue that a panic could + // take down with it, and a stale reply can never answer the next request. + const permit = Semaphore.makeUnsafe(1) + let current: Worker | undefined + let idle: ReturnType | undefined + let useWorker = true + let photon: Promise | undefined + + const discard = (worker: Worker) => { + if (current === worker) current = undefined + void worker.terminate() + } + const track = (worker: Worker) => + Effect.sync(() => { + current = worker + worker.unref() + // A worker that dies while idle is replaced by the next call. + const forget = () => { + if (current === worker) current = undefined + } + worker.on("error", forget).once("exit", forget) + return worker + }) + // Interruptible only while spawning and while the request runs, so from the + // moment a worker exists it is either tracked with an idle timer or discarded. + // A failed or interrupted request takes its worker down: the heap may be + // poisoned, or the worker is still busy with a reply nobody will read. + const inWorker = (content: PhotonNormalize.Content, limits: Readonly) => + Effect.uninterruptibleMask((restore) => + Effect.gen(function* () { + clearTimeout(idle) + const worker = current ?? (yield* restore(spawn).pipe(Effect.flatMap(track))) + return yield* restore(request(worker, content, limits)).pipe( + Effect.onExit((exit) => + Effect.sync(() => { + if (Exit.isFailure(exit)) return discard(worker) + // Unref'd so an idle worker never holds the process open. + idle = setTimeout(() => discard(worker), idleMillis) + idle.unref() + }), + ), + ) + }), + ).pipe(permit.withPermits(1)) + const inProcess = (content: PhotonNormalize.Content, limits: Readonly) => + Effect.tryPromise({ + try: () => (photon ??= PhotonNormalize.load()), + catch: () => new ResizerUnavailableError(), + }).pipe(Effect.map((photon) => PhotonNormalize.normalize(photon, content, limits))) -export const make = Effect.gen(function* () { - const loadPhoton = yield* Effect.cached( - // A runtime without a photon wasm artifact (#photon-wasm resolves to the - // empty string on workerd) has no resizer, by declaration: fail typed - // before touching URLs or module loading. The path resolution and import - // for runtimes that DO have an artifact stay inside the guard too — a - // throw outside it (workerd's undefined import.meta.url was one) is a - // defect that escapes the ResizerUnavailableError handling and turns any - // image-bearing prompt into a 500 instead of degrading to passthrough. - photonWasm === "" - ? Effect.fail(new ResizerUnavailableError()) - : Effect.tryPromise({ - try: async () => { - ;(globalThis as typeof globalThis & { __OPENCODE_PHOTON_WASM_PATH?: string }).__OPENCODE_PHOTON_WASM_PATH = - path.isAbsolute(photonWasm) ? photonWasm : fileURLToPath(new URL(photonWasm, import.meta.url)) - return await import("@silvia-odwyer/photon-node") - }, - catch: () => new ResizerUnavailableError(), - }), - ) return Effect.fn("Image.Photon.normalize")(function* ( resource: string, content: FileSystem.Content & { readonly encoding: "base64" }, limits: Readonly, ) { - const photon = yield* loadPhoton - const decoded = yield* Effect.try({ - try: () => photon.PhotonImage.new_from_byteslice(Buffer.from(content.content, "base64")), - catch: () => new DecodeError({ resource }), - }) - try { - const width = decoded.get_width() - const height = decoded.get_height() - const bytes = Buffer.byteLength(content.content, "utf-8") - const fits = width <= limits.maxWidth && height <= limits.maxHeight && bytes <= limits.maxBase64Bytes - const smallPng = bytes <= PNG_PASSTHROUGH_MAX_BASE64_BYTES || width * height <= SMALL_PNG_MAX_PIXELS - if (fits && (!limits.autoResize || content.mime !== "image/png" || smallPng)) return content - if (!limits.autoResize) - return yield* new SizeError({ - resource, - width, - height, - bytes, - maxWidth: limits.maxWidth, - maxHeight: limits.maxHeight, - maxBytes: limits.maxBase64Bytes, - }) - const transparent = content.mime !== "image/jpeg" && hasTransparency(decoded.get_raw_pixels(), width, height) - // An image that already fits only changes when JPEG makes it smaller. - if (fits) { - if (transparent) return content - const jpeg = decoded.get_bytes_jpeg(JPEG_QUALITIES[0]!) - return base64Length(jpeg) < bytes - ? { - ...content, - content: Buffer.from(jpeg).toString("base64"), - encoding: "base64" as const, - mime: "image/jpeg", - } - : content + // Fail typed before touching URLs, workers or module loading: workerd has + // no photon artifact, and a throw outside the typed path (its undefined + // import.meta.url was one) turns any image-bearing prompt into a 500. + if (!PhotonNormalize.available) return yield* new ResizerUnavailableError() + const result = useWorker + ? yield* inWorker(content, limits).pipe( + Effect.catchTag("Image.Photon.WorkerUnavailableError", (error) => { + if (!useWorker) return inProcess(content, limits) + useWorker = false + return Effect.logWarning("photon worker unavailable, resizing images on the main thread", { + cause: error.cause, + }).pipe(Effect.andThen(inProcess(content, limits))) + }), + ) + : yield* inProcess(content, limits) + switch (result._tag) { + case "Unchanged": + return content + case "Content": + return result.content + case "DecodeError": + return yield* new DecodeError({ resource }) + case "SizeError": { + const { _tag, ...size } = result + return yield* new SizeError({ resource, ...size }) } - const scale = Math.min(1, limits.maxWidth / width, limits.maxHeight / height) - const sizes = Array.from({ length: 32 }).reduce>((acc) => { - const previous = acc.at(-1) ?? { - width: Math.max(1, Math.round(width * scale)), - height: Math.max(1, Math.round(height * scale)), - } - const next = - acc.length === 0 - ? previous - : { - width: previous.width === 1 ? 1 : Math.max(1, Math.floor(previous.width * 0.75)), - height: previous.height === 1 ? 1 : Math.max(1, Math.floor(previous.height * 0.75)), - } - return acc.some((item) => item.width === next.width && item.height === next.height) ? acc : [...acc, next] - }, []) - for (const size of sizes) { - const resized = - size.width === width && size.height === height - ? decoded - : photon.resize(decoded, size.width, size.height, photon.SamplingFilter.Lanczos3) - try { - const pngEncoder = ["image/png", () => resized.get_bytes()] as const - const jpegEncoders = JPEG_QUALITIES.map( - (quality) => ["image/jpeg", () => resized.get_bytes_jpeg(quality)] as const, - ) - const pngFirst = transparent || size.width * size.height <= SMALL_PNG_MAX_PIXELS - const encoders: Array Uint8Array]> = pngFirst - ? [pngEncoder, ...jpegEncoders] - : [...jpegEncoders, pngEncoder] - for (const [mime, encode] of encoders) { - const candidate = encode() - if (base64Length(candidate) <= limits.maxBase64Bytes) - return { - ...content, - content: Buffer.from(candidate).toString("base64"), - encoding: "base64" as const, - mime, - } - } - } finally { - if (resized !== decoded) resized.free() - } - } - return yield* new SizeError({ - resource, - width, - height, - bytes, - maxWidth: limits.maxWidth, - maxHeight: limits.maxHeight, - maxBytes: limits.maxBase64Bytes, - }) - } finally { - decoded.free() } }) -}) +} + +// One worker per process, shared by every Location. +export const make = Effect.succeed(makeWith(spawnWorker())) diff --git a/packages/core/test/fixture/photon-crash-worker.ts b/packages/core/test/fixture/photon-crash-worker.ts new file mode 100644 index 00000000000..d90766544bd --- /dev/null +++ b/packages/core/test/fixture/photon-crash-worker.ts @@ -0,0 +1,10 @@ +import { parentPort } from "node:worker_threads" + +// Stands in for a photon worker whose wasm panics on its first request. +const port = parentPort +if (port) { + port.on("message", () => { + throw new Error("unreachable executed") + }) + port.postMessage({ _tag: "Ready" }) +} diff --git a/packages/core/test/fixture/photon-exit.ts b/packages/core/test/fixture/photon-exit.ts new file mode 100644 index 00000000000..f3523e4a748 --- /dev/null +++ b/packages/core/test/fixture/photon-exit.ts @@ -0,0 +1,29 @@ +import photon from "@silvia-odwyer/photon-node" +import { Effect } from "effect" +import { Photon } from "@opencode/core/image/photon" + +// Runs one photon worker job, then lets the process exit on its own. The test +// times how long that takes, so nothing here may hold the event loop open. +if (process.argv[2] === "abandon-spawn") { + await Effect.runPromise( + Photon.spawnWorker(new URL("./photon-hang-worker.ts", import.meta.url)).pipe( + Effect.timeout("200 millis"), + Effect.ignore, + ), + ) +} else { + const source = new photon.PhotonImage(new Uint8Array(400 * 400 * 4).fill(200), 400, 400) + const content = { + uri: "file:///fixture.png", + content: Buffer.from(source.get_bytes()).toString("base64"), + encoding: "base64" as const, + mime: "image/png", + } + source.free() + let spawned = 0 + const normalize = Photon.makeWith(Photon.spawnWorker().pipe(Effect.tap(() => Effect.sync(() => spawned++)))) + const limits = { autoResize: true, maxWidth: 100, maxHeight: 100, maxBase64Bytes: 5 * 1024 * 1024 } + const result = await Effect.runPromise(normalize("fixture.png", content, limits)) + if (spawned !== 1 || result === content) process.exit(2) +} +console.log(Date.now()) diff --git a/packages/core/test/fixture/photon-hang-worker.ts b/packages/core/test/fixture/photon-hang-worker.ts new file mode 100644 index 00000000000..dee86c5b5cd --- /dev/null +++ b/packages/core/test/fixture/photon-hang-worker.ts @@ -0,0 +1,2 @@ +// Stands in for a photon worker that never finishes loading. +setInterval(() => {}, 1_000) diff --git a/packages/core/test/image-photon.test.ts b/packages/core/test/image-photon.test.ts new file mode 100644 index 00000000000..d10b926355c --- /dev/null +++ b/packages/core/test/image-photon.test.ts @@ -0,0 +1,184 @@ +import { describe, expect, test } from "bun:test" +import photon from "@silvia-odwyer/photon-node" +import { createHash } from "node:crypto" +import { fileURLToPath } from "node:url" +import { Deferred, Effect, Exit, Fiber, Logger, Ref } from "effect" +import { Photon } from "@opencode/core/image/photon" +import { it } from "./lib/effect" + +const limits = { autoResize: true, maxWidth: 2_000, maxHeight: 2_000, maxBase64Bytes: 5 * 1024 * 1024 } + +// 2400×1200 with a translucent right half: it resizes, tries PNG first, then lands on JPEG. +const content = (() => { + const width = 2400 + const height = 1200 + const pixels = new Uint8Array(width * height * 4) + for (let y = 0; y < height; y++) + for (let x = 0; x < width; x++) { + const i = (y * width + x) * 4 + pixels[i] = (x * 7 + y * 3) & 255 + pixels[i + 1] = (x ^ y) & 255 + pixels[i + 2] = (x * y) & 255 + pixels[i + 3] = x < 1200 ? 255 : y & 255 + } + const source = new photon.PhotonImage(pixels, width, height) + const bytes = source.get_bytes() + source.free() + return { + uri: "file:///fixture.png", + content: Buffer.from(bytes).toString("base64"), + encoding: "base64" as const, + mime: "image/png", + } +})() + +// The output of the in-process resizer before it moved to a worker. +const expected = { mime: "image/jpeg", sha256: "356a08f3cd35a6277f6a066ab12141d26212a93d8ec5f53c66af7b9c5844db95" } +const digest = (result: { mime: string; content: string }) => ({ + mime: result.mime, + sha256: createHash("sha256").update(result.content).digest("hex"), +}) + +const counted = (spawned: Ref.Ref, url?: (count: number) => URL | undefined) => + Ref.getAndUpdate(spawned, (count) => count + 1).pipe(Effect.flatMap((count) => Photon.spawnWorker(url?.(count)))) + +// Runs the fixture in its own process and returns how long the process took to +// exit after its work was done. +const exitLag = async (mode: string) => { + const child = Bun.spawn( + [process.execPath, fileURLToPath(new URL("./fixture/photon-exit.ts", import.meta.url)), mode], + { + stdout: "pipe", + stderr: "inherit", + }, + ) + const timeout = setTimeout(() => child.kill(), 20_000) + const output = await new Response(child.stdout).text() + const code = await child.exited + clearTimeout(timeout) + expect(code).toBe(0) + return Date.now() - Number(output.trim()) +} + +describe("Image.Photon", () => { + it.live("resizes in a worker to the same bytes as before", () => + Effect.gen(function* () { + const normalize = Photon.makeWith(Photon.spawnWorker()) + expect(digest(yield* normalize("fixture.png", content, limits))).toEqual(expected) + }), + ) + + it.live("shares one resizer across the process", () => + Effect.gen(function* () { + expect(yield* Photon.make).toBe(yield* Photon.make) + }), + ) + + it.live("falls back to resizing in-process when the worker cannot start, warning once", () => { + const warnings: unknown[] = [] + const logger = Logger.make(({ logLevel, message }) => { + if (logLevel === "Warn") warnings.push(message) + }) + return Effect.gen(function* () { + const normalize = Photon.makeWith(Photon.spawnWorker(new URL("./missing-photon-worker.js", import.meta.url))) + expect(digest(yield* normalize("fixture.png", content, limits))).toEqual(expected) + expect(digest(yield* normalize("fixture.png", content, limits))).toEqual(expected) + expect(warnings).toHaveLength(1) + }).pipe(Effect.provide(Logger.layer([logger]))) + }) + + it.live("queues concurrent calls on one worker", () => + Effect.gen(function* () { + const spawned = yield* Ref.make(0) + const normalize = Photon.makeWith(counted(spawned)) + const reference = Photon.makeWith(Photon.spawnWorker(new URL("./missing.js", import.meta.url))) + const sizes = [400, 300, 200].map((maxWidth) => ({ ...limits, maxWidth, maxHeight: maxWidth })) + const results = yield* Effect.forEach(sizes, (size) => normalize("fixture.png", content, size), { + concurrency: "unbounded", + }) + expect(yield* Ref.get(spawned)).toBe(1) + expect(new Set(results.map((result) => result.content)).size).toBe(3) + for (const [index, size] of sizes.entries()) + expect(digest(results[index]!)).toEqual(digest(yield* reference("fixture.png", content, size))) + }), + ) + + it.live("fails only the request a crash interrupts, then runs the queue on a fresh worker", () => + Effect.gen(function* () { + const spawned = yield* Ref.make(0) + const normalize = Photon.makeWith( + counted(spawned, (count) => + count === 0 ? new URL("./fixture/photon-crash-worker.ts", import.meta.url) : undefined, + ), + ) + const exits = yield* Effect.forEach( + [0, 1, 2], + () => normalize("fixture.png", content, limits).pipe(Effect.exit), + { concurrency: "unbounded" }, + ) + expect(Exit.hasDies(exits[0]!)).toBe(true) + for (const exit of exits.slice(1)) expect(Exit.isSuccess(exit) && digest(exit.value)).toEqual(expected) + expect(yield* Ref.get(spawned)).toBe(2) + }), + ) + + it.live("terminates a worker when interrupted between acquiring it and sending the request", () => + Effect.gen(function* () { + const exited = yield* Deferred.make() + // Interrupts the caller as the new worker is stored, the last step before the request. + const normalize = Photon.makeWith( + Effect.withFiber((caller) => + Photon.spawnWorker().pipe( + Effect.tap((worker) => + Effect.sync(() => { + worker.once("exit", () => Deferred.doneUnsafe(exited, Effect.void)) + const unref = worker.unref.bind(worker) + worker.unref = () => { + worker.unref = unref + unref() + caller.interruptUnsafe() + } + }), + ), + ), + ), + ) + const call = yield* Effect.forkChild(normalize("fixture.png", content, limits)) + expect(Exit.hasInterrupts(yield* Fiber.await(call))).toBe(true) + yield* Deferred.await(exited).pipe(Effect.timeout("3 seconds")) + }), + ) + + it.live("terminates the worker once it has been idle", () => + Effect.gen(function* () { + const spawned = yield* Ref.make(0) + const exited = yield* Deferred.make() + const normalize = Photon.makeWith( + counted(spawned).pipe( + Effect.tap((worker) => + Effect.sync(() => worker.once("exit", () => Deferred.doneUnsafe(exited, Effect.void))), + ), + ), + 200, + ) + const small = { ...limits, maxWidth: 200, maxHeight: 200 } + yield* normalize("fixture.png", content, small) + yield* normalize("fixture.png", content, small) + expect(yield* Ref.get(spawned)).toBe(1) + expect(yield* Deferred.isDone(exited)).toBe(false) + + yield* Deferred.await(exited) + + yield* normalize("fixture.png", content, small) + expect(yield* Ref.get(spawned)).toBe(2) + }), + ) + + test("an idle worker does not keep the process alive", async () => { + expect(await exitLag("resize")).toBeLessThan(3_000) + }, 30_000) + + test("terminates a worker whose startup is interrupted", async () => { + expect(await exitLag("abandon-spawn")).toBeLessThan(3_000) + }, 30_000) +})