-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
adapter.ts
67 lines (58 loc) · 2.26 KB
/
adapter.ts
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
import { fetch, Request, Response } from 'cross-fetch';
import fs from 'fs';
import { WebGLRenderingContext } from 'gl';
import { settings, utils } from '@pixi/core';
import { NodeCanvasElement } from './NodeCanvasElement';
import { DOMParser } from '@xmldom/xmldom';
import type { IAdapter } from '@pixi/core';
export const NodeAdapter = {
/**
* Creates a canvas element of the given size.
* This canvas is created using the node-canvas package and uses the gl package to create a webgl context.
* @param width - width of the canvas
* @param height - height of the canvas
*/
createCanvas: (width?: number, height?: number) => new NodeCanvasElement(width, height),
/** Returns a webgl rendering context using the gl package. */
getWebGLRenderingContext: () => WebGLRenderingContext,
/** Returns the fake user agent string of `node` */
getNavigator: () => ({ userAgent: 'node' }),
/** Returns the path from which the process is being run */
getBaseUrl: () => process.cwd(),
getFontFaceSet: () => null,
fetch: (url: RequestInfo, options?: RequestInit) =>
{
const request = new Request(url, options);
// Check if urls starts with http(s) as only these are supported by node-fetch
if (utils.path.isUrl(request.url))
{
return fetch(url, request);
}
return new Promise((resolve, reject) =>
{
const filePath = utils.path.normalize(request.url);
if (!fs.existsSync(filePath))
{
reject(`File not found: ${filePath}`);
}
const readStream = fs.createReadStream(filePath);
readStream.on('open', () =>
{
resolve(new Response(readStream as unknown as ReadableStream, {
url: request.url,
status: 200,
statusText: 'OK',
size: fs.statSync(filePath).size,
timeout: (request as any).timeout,
} as ResponseInit));
});
});
},
parseXML: (xml: string) =>
{
const parser = new DOMParser();
return parser.parseFromString(xml, 'text/xml');
},
} as IAdapter;
settings.ADAPTER = NodeAdapter;
export { settings };