gx
chenyc
2025-06-12 7b72ac13a83764a662159d4a49b7fffb90476ecb
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
/* eslint-disable max-classes-per-file */
import { createFileSystem } from './createFileSystem';
import { Environment } from './types';
 
export function createNodejsEnv(): Environment {
  // eslint-disable-next-line dot-notation
  const Canvas = global['Canvas'] || global.HTMLCanvasElement;
  const Image = global.Image || global.HTMLImageElement;
  // eslint-disable-next-line dot-notation
  const Video = global['Video'] || global.HTMLVideoElement;
 
  const createCanvasElement = () => {
    if (Canvas) return new Canvas();
    throw new Error('createCanvasElement - missing Canvas implementation for nodejs environment');
  };
 
  const createImageElement = () => {
    if (Image) return new Image();
    throw new Error('createImageElement - missing Image implementation for nodejs environment');
  };
 
  const createVideoElement = () => {
    if (Video) return new Video();
    throw new Error('createVideoElement - missing Video implementation for nodejs environment');
  };
 
  const fetch = global.fetch;
  // if (!fetch) throw new Error('fetch - missing fetch implementation for nodejs environment');
 
  const fileSystem = createFileSystem();
 
  return {
    Canvas: Canvas || class {},
    CanvasRenderingContext2D: global.CanvasRenderingContext2D || class {},
    Image: Image || class {},
    ImageData: global.ImageData || class {},
    Video: global.HTMLVideoElement || class {},
    createCanvasElement,
    createImageElement,
    createVideoElement,
    fetch,
    ...fileSystem,
  };
}