Files
orca/config/scripts/test-runtime/dom/happy-dom-offscreen-canvas.ts
Neil 9bd6272fdf refactor(tooling): organize build, CI, and test owners (#38130)
* refactor(tooling): organize test runtime and maintenance owners

* test(tooling): preserve relocated runtime fixture dependency edges

* refactor(tooling): preserve xterm routing owner checks

* ci(tooling): include routing owner in sparse checkout

* test(tooling): preserve copied workflow dependency closures

* fix(tooling): normalize hashed JSON assets in release parity

* fix(tooling): distinguish asset aliases from literal references

* fix(tooling): frame normalized asset reference payloads

* fix(tooling): normalize decoded JSON asset references

* refactor(tooling): organize build, CI and release owners

* fix(tooling): align moved generator and workflow path contracts

* fix(tooling): update generated plugin bundle owner banners

* test(tooling): create relocated mobile verifier fixture directories

* test(tooling): copy headless detector dependency closure

* test(tooling): align native batch policy fixtures with owner path

* fix(tooling): link moved reliability command to historical evidence

* fix(tooling): preserve nested sandbox owners and canonical guide source

* fix(tooling): target release version owners in checkout fixture
2026-11-04 21:08:19 -08:00

59 lines
1.9 KiB
TypeScript

// happy-dom exposes adapter-less OffscreenCanvas that xterm prefers; reuse the existing HTML canvas text-metric double.
const OFFSCREEN_CANVAS_COMPATIBILITY_INSTALLED = Symbol.for(
'orca.happyDomOffscreenCanvasCompatibility'
)
type PatchableCanvas = {
getContext?: (contextType: string, contextAttributes?: unknown) => unknown
} & Record<symbol, unknown>
type CanvasConstructor = {
prototype: PatchableCanvas
new (width: number, height: number): PatchableCanvas
}
type HappyDomGlobals = {
OffscreenCanvas?: CanvasConstructor
HTMLCanvasElement?: { prototype: PatchableCanvas }
document?: Pick<Document, 'createElement'>
}
/** Make happy-dom's adapter-less OffscreenCanvas honor the existing HTML canvas test double. */
export function installHappyDomOffscreenCanvasCompatibility(): boolean {
const globals = globalThis as unknown as HappyDomGlobals
const offscreenCanvas = globals.OffscreenCanvas
const htmlCanvas = globals.HTMLCanvasElement
const document = globals.document
if (!offscreenCanvas || !htmlCanvas || !document) {
return false
}
const prototype = offscreenCanvas.prototype
if (prototype[OFFSCREEN_CANVAS_COMPATIBILITY_INSTALLED] === true) {
return true
}
const originalGetContext = prototype.getContext
if (!originalGetContext || !htmlCanvas.prototype.getContext) {
return false
}
prototype.getContext = function patchedGetContext(
this: PatchableCanvas,
contextType: string,
contextAttributes?: unknown
): unknown {
const context = originalGetContext.call(this, contextType, contextAttributes)
if (contextType !== '2d' || context) {
return context
}
const canvas = document.createElement('canvas')
return htmlCanvas.prototype.getContext!.call(canvas, contextType, contextAttributes)
}
prototype[OFFSCREEN_CANVAS_COMPATIBILITY_INSTALLED] = true
return true
}
installHappyDomOffscreenCanvasCompatibility()