All files / qr-scanner/src useQRScanner.ts

84.53% Statements 153/181
75.39% Branches 95/126
90% Functions 27/30
88.75% Lines 142/160

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599                    1x                                                                                                                                                                                                                                           1x                     1x                   1x     1x 1x 1x 1x                 7x                         147x   54x   30x   2x   2x   4x   55x                                                           147x   147x 147x 147x 147x           147x           147x   147x         7x           7x 7x     147x 38x       147x                         147x 147x 142x     147x 16x 16x   13x 13x   13x 13x 13x 10x 10x 10x         13x             147x 147x 147x 147x 147x 147x 147x   147x   147x   147x 38x 38x 38x       147x 11x 11x   11x 11x         11x           11x 11x 8x     11x                                   11x 11x     11x             10x               11x 9x 9x           11x           11x       147x 63x 63x 63x   24x         24x   63x 24x           147x   147x 66x 24x 24x   24x 24x 24x 24x   24x 12x   11x 11x 11x 11x   11x     24x 35x       35x 35x           24x   24x 24x 24x 24x 24x                 147x 147x 100x                                               147x 147x 147x 147x   147x         147x 1x 1x     147x 147x   147x   1x         147x 1x     147x   5x 5x 5x 5x 5x                 147x 2x 2x 1x 1x           147x         147x 120x                                                                                                
"use client";
 
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { useControllableState } from "@usefy/use-controllable-state";
import { useEventCallback } from "@usefy/use-event-callback";
import { useIsomorphicLayoutEffect } from "@usefy/use-isomorphic-layout-effect";
import { usePageVisibility } from "@usefy/use-page-visibility";
import { useUserMedia, type UserMediaStatus } from "@usefy/use-user-media";
import { QRCameraError, QRDecodeError } from "./errors";
import { decode, decodeFile, type DecodeOptions } from "./engine";
import { toImageData } from "./image/source";
import type { ImageSource, QRScanOptions, QRScanResult } from "./types";
import type { WorkerScanOptions } from "./worker-protocol";
 
/**
 * The scanning loop: a camera, a frame pump, and a decoder.
 *
 * Everything here is about *pacing*. Decoding is not free, a camera delivers
 * frames faster than any decoder needs them, and the one thing a user notices
 * immediately is a preview that stutters. So the loop skips frames while a
 * decode is in flight, throttles to a rate rather than chasing the camera, and
 * stops entirely when the tab is hidden.
 */
 
/** How the scanner is currently behaving — what a UI needs to say out loud. */
export type QRScannerState =
  /** No camera requested yet. */
  | "idle"
  /** The browser is asking the user for permission. */
  | "prompting"
  /** Running: frames are being decoded. */
  | "scanning"
  /** Started, but paused (by `paused`, or by a hidden tab). */
  | "paused"
  /** Permission was refused. */
  | "denied"
  /** No camera, or no way to use one here. */
  | "unsupported"
  /** Something failed; see `error`. */
  | "error";
 
/**
 * Something that can decode an image — a worker, or anything else.
 *
 * The options are the *transferable* subset: a custom decoder may well live
 * behind `postMessage`, where a function `binarizer` cannot follow. Typing the
 * boundary narrowly is what makes that a compile error rather than a silently
 * ignored option.
 */
export interface Decoder {
  decode(
    source: ImageSource,
    options?: WorkerScanOptions & { timestamp?: number },
  ): Promise<QRScanResult[]>;
}
 
export interface UseQRScannerOptions extends QRScanOptions {
  /** Called once per distinct value, subject to `dedupeMs`. */
  onScan?: (result: QRScanResult, all: readonly QRScanResult[]) => void;
  /** Called for **every** decode, including repeats — for throughput readouts. */
  onFrame?: (results: readonly QRScanResult[]) => void;
  onError?: (error: Error) => void;
  /** Open the camera as soon as the hook mounts. @default true */
  autoStart?: boolean;
  /**
   * Hold the loop without releasing the camera.
   *
   * Controllable: pass `paused` to drive it yourself, `defaultPaused` to let
   * the component own it while still hearing about changes through
   * `onPausedChange`, or neither and use `pause()` / `resume()`.
   */
  paused?: boolean;
  /** Initial paused state when `paused` is not supplied. @default false */
  defaultPaused?: boolean;
  /** Fired whenever the paused state changes, from either side. */
  onPausedChange?: (paused: boolean) => void;
  facingMode?: "user" | "environment";
  deviceId?: string;
  constraints?: MediaTrackConstraints;
  /**
   * Maximum decodes per second.
   * @default 12
   */
  scanRate?: number;
  /**
   * How long the same value is suppressed after a hit, in milliseconds. `0`
   * reports every frame.
   * @default 1500
   */
  dedupeMs?: number;
  /** Release the camera while the tab is hidden, not just pause. @default false */
  stopOnHide?: boolean;
  /** Decode off the main thread (see {@link createWorkerDecoder}). */
  decoder?: Decoder;
}
 
export interface UseQRScannerReturn {
  /** Attach to a `<video>`; the hook wires up `srcObject` and playback. */
  videoRef: React.RefObject<HTMLVideoElement | null>;
  state: QRScannerState;
  /** The most recent decode. */
  result: QRScanResult | null;
  /** Every symbol found in the most recent frame. */
  results: readonly QRScanResult[];
  error: Error | null;
 
  start: () => Promise<void>;
  stop: () => void;
  /** Hold the loop; the camera stays open so resuming is instant. */
  pause: () => void;
  resume: () => void;
 
  devices: MediaDeviceInfo[];
  activeDeviceId: string | null;
  selectDevice: (deviceId: string) => Promise<void>;
  switchCamera: () => Promise<void>;
 
  isTorchSupported: boolean;
  torch: boolean;
  setTorch: (on: boolean) => Promise<boolean>;
 
  /** Decode a file the user picked, dropped or pasted. */
  scanFile: (file: Blob) => Promise<QRScanResult[]>;
  /** The current frame's pixels — for a "scan the still image" fallback. */
  capture: () => Promise<ImageData | null>;
}
 
/** How many camera frames pass between attempts at an inverted (dark-mode) read. */
const INVERT_EVERY = 4;
 
/**
 * Consecutive empty frames after which the session stops asking the platform
 * detector and uses the internal engine for good.
 *
 * At the default twelve frames a second this is a second of pointing at
 * nothing — long enough that a user simply looking for a code never triggers
 * it, short enough that a detector which advertises QR and then never answers
 * costs at most one second before the working engine takes over.
 */
const NATIVE_MISS_STREAK = 12;
 
/**
 * Declared locally rather than pulled in from `@types/node` (qr-code's
 * `render/warnings.ts` precedent): this package has no Node dependency, and the
 * literal `process.env.NODE_ENV` text is what bundlers look for when stripping
 * development-only code.
 */
declare const process: { env?: { NODE_ENV?: string } } | undefined;
 
let warnedAboutBinarizer = false;
 
function warnBinarizerNotTransferable(): void {
  const isProduction = typeof process !== "undefined" && process.env?.NODE_ENV === "production";
  Iif (warnedAboutBinarizer || isProduction) return;
  warnedAboutBinarizer = true;
  console.warn(
    "[@usefy/qr-scanner] A function `binarizer` cannot be sent to a worker, so " +
      "frames are being decoded on the main thread instead. Use `binarizer: " +
      '"hybrid" | "otsu"` to keep the worker in play.',
  );
}
 
/** Whether an error came from the camera layer (a `UserMediaError`). */
function isCameraFailure(error: unknown): boolean {
  return (
    typeof error === "object" &&
    error !== null &&
    "reason" in error &&
    (error as { name?: unknown }).name === "UserMediaError"
  );
}
 
function toScannerState(
  media: UserMediaStatus,
  paused: boolean,
  hasStream: boolean,
): QRScannerState {
  switch (media) {
    case "idle":
      return "idle";
    case "prompting":
      return "prompting";
    case "denied":
      return "denied";
    case "unsupported":
      return "unsupported";
    case "error":
      return "error";
    case "granted":
      return paused || !hasStream ? "paused" : "scanning";
  }
}
 
/**
 * Drive a camera and decode QR codes from its frames.
 *
 * @example
 * ```tsx
 * const scanner = useQRScanner({ onScan: (result) => setValue(result.text) });
 * return <video ref={scanner.videoRef} playsInline muted />;
 * ```
 */
export function useQRScanner(options: UseQRScannerOptions = {}): UseQRScannerReturn {
  const {
    onScan,
    onFrame,
    onError,
    autoStart = true,
    paused,
    defaultPaused = false,
    onPausedChange,
    facingMode = "environment",
    deviceId,
    constraints,
    scanRate = 12,
    dedupeMs = 1500,
    stopOnHide = false,
    decoder,
    ...scanOptions
  } = options;
 
  const videoRef = useRef<HTMLVideoElement | null>(null);
  const [results, setResults] = useState<readonly QRScanResult[]>([]);
  const [result, setResult] = useState<QRScanResult | null>(null);
  const [error, setError] = useState<Error | null>(null);
 
  // One paused state, whoever owns it: a `paused` prop, a `defaultPaused` plus
  // `onPausedChange`, or `pause()`/`resume()`. Two separate flags — a prop and
  // an internal one — would let them disagree, and "I called resume() but the
  // prop still says paused" is a bug report nobody enjoys.
  const [effectivelyPaused, setPaused] = useControllableState<boolean>({
    value: paused,
    defaultValue: defaultPaused,
    onChange: onPausedChange,
  });
 
  const isVisible = usePageVisibility();
 
  const handleError = useEventCallback((caught: unknown) => {
    // Camera failures arrive as the hook's `UserMediaError`; they are re-typed
    // as this package's `QRCameraError` so a consumer narrows on one error type
    // from one package rather than importing a sibling's to write a `catch`.
    const normalized =
      isCameraFailure(caught) || caught instanceof QRCameraError
        ? QRCameraError.from(caught)
        : caught instanceof Error
          ? caught
          : new Error(String(caught));
 
    setError(normalized);
    onError?.(normalized);
  });
 
  const mediaConstraints = useMemo<MediaStreamConstraints>(
    () => ({ video: constraints ?? true, audio: false }),
    [constraints],
  );
 
  const camera = useUserMedia({
    constraints: mediaConstraints,
    autoStart,
    facingMode,
    deviceId,
    onError: handleError,
  });
 
  /* ───────────────────────── the frame pump ───────────────────────── */
 
  // Written in a layout effect rather than during render: a render React throws
  // away must not mutate a ref, and the house `useEventCallback` uses the same
  // pattern for exactly this reason.
  const decodeOptionsRef = useRef(scanOptions);
  useIsomorphicLayoutEffect(() => {
    decodeOptionsRef.current = scanOptions;
  });
 
  const emitScan = useEventCallback((frame: readonly QRScanResult[]) => {
    onFrame?.(frame);
    if (frame.length === 0) return;
 
    setResults(frame);
    const now = frame[0]!.timestamp;
 
    for (const candidate of frame) {
      const previous = seenRef.current.get(candidate.text);
      if (previous !== undefined && dedupeMs > 0 && now - previous < dedupeMs) continue;
      seenRef.current.set(candidate.text, now);
      setResult(candidate);
      onScan?.(candidate, frame);
    }
 
    // The dedupe map is per-session, and a long shift at a checkout desk would
    // otherwise grow it without bound.
    Iif (seenRef.current.size > 256) {
      for (const [value, at] of seenRef.current) {
        if (now - at > dedupeMs) seenRef.current.delete(value);
      }
    }
  });
 
  const seenRef = useRef(new Map<string, number>());
  const runningRef = useRef(false);
  const inFlightRef = useRef(false);
  const frameCountRef = useRef(0);
  const lastDecodeRef = useRef(0);
  const mountedRef = useRef(true);
  const bufferRef = useRef<Uint8ClampedArray | undefined>(undefined);
  /** Consecutive frames the native detector found nothing in. */
  const nativeMissesRef = useRef(0);
  /** Once the native path has proven unhelpful, the session stays internal. */
  const nativeGaveUpRef = useRef(false);
 
  useEffect(() => {
    mountedRef.current = true;
    return () => {
      mountedRef.current = false;
    };
  }, []);
 
  const decodeFrame = useEventCallback(async (video: HTMLVideoElement) => {
    Iif (inFlightRef.current) return;
    inFlightRef.current = true;
 
    try {
      const frameIndex = frameCountRef.current++;
      // The inverted (light-on-dark) pass doubles the cost of a frame that
      // found nothing, so the live loop pays for it once every few frames
      // rather than on every one. A still image gets it every time — that is
      // `decodeFile`'s call, not this one's.
      const invert = frameIndex % INVERT_EVERY === 0 ? "auto" : "never";
 
      // The luminance plane is the biggest per-frame allocation, and at twelve
      // frames a second a fresh one is megabytes of garbage a minute. One
      // buffer sized for the full frame serves every working resolution,
      // because `toGray` takes a subarray of whatever it is handed.
      const pixels = video.videoWidth * video.videoHeight;
      if (pixels > 0 && (!bufferRef.current || bufferRef.current.length < pixels)) {
        bufferRef.current = new Uint8ClampedArray(pixels);
      }
 
      const request: DecodeOptions = {
        ...decodeOptionsRef.current,
        invert: decodeOptionsRef.current.invert ?? invert,
        buffer: bufferRef.current,
        timestamp: performance.now(),
        // On a live camera the native detector gets the frame to itself: running
        // the internal engine too, on every frame that found nothing, doubles
        // the cost of the most common frame there is. A still image is the
        // opposite trade and keeps the fallback (`engine.ts`).
        fallbackToInternal: false,
        forceInternal: nativeGaveUpRef.current || undefined,
      };
 
      // A custom binarizer is a function, and functions do not survive
      // `postMessage`. Rather than dropping it silently — which would leave a
      // consumer debugging why their thresholding never runs — the frame goes
      // to the main thread, honouring the explicit choice over the implicit
      // performance preference, and dev mode says so once.
      const workerCanRun = typeof request.binarizer !== "function";
      if (decoder && !workerCanRun) warnBinarizerNotTransferable();
 
      const frame =
        decoder && workerCanRun
          ? await decoder.decode(video, request as WorkerScanOptions & { timestamp?: number })
          : await decode(video, request);
 
      // Decoding is async; the component can be gone by the time it answers,
      // and delivering a scan to a component that no longer exists is a bug in
      // the consumer's app that they did not write.
      Iif (!mountedRef.current) return;
 
      // ── The native miss-streak switch ──
      // `BarcodeDetector` is faster when it works, but some builds advertise QR
      // and then quietly find nothing (a symbology they only partly implement,
      // a frame format they dislike). Rather than scanning forever with a
      // detector that never answers, the session gives up on it after a run of
      // empty frames and stays internal.
      if (!nativeGaveUpRef.current && !decoder) {
        if (frame.length > 0) {
          nativeMissesRef.current = 0;
        E} else if (++nativeMissesRef.current >= NATIVE_MISS_STREAK) {
          nativeGaveUpRef.current = true;
        }
      }
 
      emitScan(frame);
    } catch (caught) {
      // A frame that cannot be decoded is the normal case, not an error — only
      // a camera or environment failure is worth surfacing.
      if (mountedRef.current && !(caught instanceof QRDecodeError)) handleError(caught);
    } finally {
      inFlightRef.current = false;
    }
  });
 
  useEffect(() => {
    const video = videoRef.current;
    const stream = camera.stream;
    if (!video || !stream) return;
 
    video.srcObject = stream;
    // Two guards in one line. `play()` rejects if the element is removed
    // mid-call, which is routine during navigation and not worth reporting —
    // and it returns `undefined` rather than a promise in older engines, where
    // an unguarded `.catch` is a TypeError that takes the scanner down.
    void video.play()?.catch(() => {});
 
    return () => {
      video.srcObject = null;
    };
  }, [camera.stream]);
 
  // A hidden tab stops the loop whether or not the camera is released: decoding
  // frames nobody can see is pure battery.
  const active = !effectivelyPaused && isVisible && camera.status === "granted";
 
  useEffect(() => {
    if (!active) return;
    const video = videoRef.current;
    Iif (!video) return;
 
    runningRef.current = true;
    const interval = 1000 / Math.max(1, scanRate);
    let handle = 0;
    let frameHandle = 0;
 
    const tick = (): void => {
      if (!runningRef.current) return;
 
      const now = performance.now();
      Eif (now - lastDecodeRef.current >= interval && video.readyState >= 2) {
        lastDecodeRef.current = now;
        void decodeFrame(video);
      }
      schedule();
    };
 
    const schedule = (): void => {
      Iif (!runningRef.current) return;
      // `requestVideoFrameCallback` fires once per *presented* frame, so the
      // loop never decodes the same picture twice and never spins between
      // frames. rAF is the fallback where it does not exist (Firefox).
      if (typeof video.requestVideoFrameCallback === "function") {
        frameHandle = video.requestVideoFrameCallback(() => tick());
      } else E{
        handle = requestAnimationFrame(() => tick());
      }
    };
 
    schedule();
 
    return () => {
      runningRef.current = false;
      Iif (handle) cancelAnimationFrame(handle);
      Eif (frameHandle && typeof video.cancelVideoFrameCallback === "function") {
        video.cancelVideoFrameCallback(frameHandle);
      }
    };
  }, [active, scanRate, decodeFrame]);
 
  // Releasing the camera while hidden is opt-in: it frees the LED and the
  // sensor, but re-acquiring costs a permission-free round trip and a visible
  // black frame, which is the wrong trade for a scanner the user is switching
  // back to in a second.
  const hiddenReleaseRef = useRef(false);
  useEffect(() => {
    Eif (!stopOnHide) return;
 
    if (!isVisible) {
      hiddenReleaseRef.current = true;
      camera.stop();
      return;
    }
 
    // Only re-acquire what *this* effect released. On mount the tab is visible
    // and `useUserMedia` has already acquired (or deliberately not, if
    // `autoStart` is false); starting again here would mean two `getUserMedia`
    // calls, a superseded stream, and a visible blink of the camera LED.
    if (hiddenReleaseRef.current) {
      hiddenReleaseRef.current = false;
      if (autoStart) void camera.start();
    }
  }, [isVisible, stopOnHide, autoStart, camera]);
 
  /* ───────────────────────── imperative API ───────────────────────── */
 
  // Each of these depends on one *stable* function from the camera hook rather
  // than on the whole object. Depending on `camera` would make every action's
  // identity change on every render — and a consumer's `useEffect([scanner])`
  // would then re-run on every decoded frame.
  const cameraStart = camera.start;
  const cameraStop = camera.stop;
  const cameraSelect = camera.selectDevice;
  const cameraSwitch = camera.switchDevice;
 
  const start = useCallback(async () => {
    setError(null);
    await cameraStart();
  }, [cameraStart]);
 
  const stop = useCallback(() => {
    runningRef.current = false;
    cameraStop();
  }, [cameraStop]);
 
  const pause = useCallback(() => setPaused(true), [setPaused]);
  const resume = useCallback(() => setPaused(false), [setPaused]);
 
  const selectDevice = useCallback(
    async (id: string) => {
      await cameraSelect(id);
    },
    [cameraSelect],
  );
 
  const switchCamera = useCallback(async () => {
    await cameraSwitch();
  }, [cameraSwitch]);
 
  const scanFile = useCallback(
    async (file: Blob) => {
      setError(null);
      try {
        const found = await decodeFile(file, decodeOptionsRef.current);
        emitScan(found);
        return found;
      } catch (caught) {
        handleError(caught);
        return [];
      }
    },
    [emitScan, handleError],
  );
 
  const capture = useCallback(async () => {
    const video = videoRef.current;
    if (!video || video.readyState < 2) return null;
    try {
      return await toImageData(video);
    } catch {
      return null;
    }
  }, []);
 
  const state = toScannerState(camera.status, effectivelyPaused || !isVisible, camera.stream !== null);
 
  // Memoized for the same reason the camera hook's return is: a scanner
  // re-renders on every decoded frame, and a fresh object each time would churn
  // every consumer's dependency array twelve times a second.
  return useMemo(
    () => ({
      videoRef,
      state,
      result,
      results,
      // `camera.error` is only reached if the hook set one without calling
      // `onError` — it is re-typed here for the same reason as above, so `error`
      // is always this package's own type whatever route it took.
      error: error ?? (camera.error ? QRCameraError.from(camera.error) : null),
      start,
      stop,
      pause,
      resume,
      devices: camera.devices,
      activeDeviceId: camera.activeDeviceId,
      selectDevice,
      switchCamera,
      isTorchSupported: camera.isTorchSupported,
      torch: camera.torch,
      setTorch: camera.setTorch,
      scanFile,
      capture,
    }),
    [
      state,
      result,
      results,
      error,
      camera.error,
      camera.devices,
      camera.activeDeviceId,
      camera.isTorchSupported,
      camera.torch,
      camera.setTorch,
      start,
      stop,
      pause,
      resume,
      selectDevice,
      switchCamera,
      scanFile,
      capture,
    ],
  );
}
 
/** Re-exported so a consumer can narrow on the camera's own failure type. */
export { QRCameraError };