• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

visgl / loaders.gl / 24153816851

08 Apr 2026 07:17PM UTC coverage: 53.247% (-12.1%) from 65.319%
24153816851

push

github

web-flow
chore: Move from tape to vitest (#3351)

8651 of 17291 branches covered (50.03%)

Branch coverage included in aggregate %.

7 of 7 new or added lines in 1 file covered. (100.0%)

2031 existing lines in 296 files now uncovered.

17563 of 31940 relevant lines covered (54.99%)

5279.54 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/modules/polyfills/src/load-library/require-utils.node.ts
1
// Fork of https://github.com/floatdrop/require-from-string/blob/master/index.js
2
// Copyright (c) Vsevolod Strukchinsky <floatdrop@gmail.com> (github.com/floatdrop)
3
// MIT license
4

5
// this file is not visible to webpack (it is excluded in the package.json "browser" field).
6

7
import Module from 'module';
8
import path from 'path';
9
import fs from 'fs';
10
import {ensureArrayBuffer} from '@loaders.gl/loader-utils';
11

12
/**
13
 * Load a file from local file system
14
 * @param filename
15
 * @returns
16
 */
17
export async function readFileAsArrayBuffer(filename: string): Promise<ArrayBuffer> {
UNCOV
18
  if (filename.startsWith('http')) {
×
19
    const response = await fetch(filename);
×
20
    return await response.arrayBuffer();
×
21
  }
UNCOV
22
  const buffer = fs.readFileSync(filename);
×
UNCOV
23
  return ensureArrayBuffer(buffer.buffer);
×
24
}
25

26
/**
27
 * Load a file from local file system
28
 * @param filename
29
 * @returns
30
 */
31
export async function readFileAsText(filename: string): Promise<string> {
32
  if (filename.startsWith('http')) {
×
33
    const response = await fetch(filename);
×
34
    return await response.text();
×
35
  }
36
  const text = fs.readFileSync(filename, 'utf8');
×
37
  return text;
×
38
}
39

40
// Node.js Dynamically require from file
41
// Relative names are resolved relative to cwd
42
// This indirect function is provided because webpack will try to bundle `module.require`.
43
// this file is not visible to webpack (it is excluded in the package.json "browser" field).
44
export async function requireFromFile(filename: string): Promise<any> {
UNCOV
45
  if (filename.startsWith('http')) {
×
UNCOV
46
    const response = await fetch(filename);
×
UNCOV
47
    const code = await response.text();
×
UNCOV
48
    return requireFromString(code);
×
49
  }
50

UNCOV
51
  if (!filename.startsWith('/')) {
×
UNCOV
52
    filename = `${process.cwd()}/${filename}`;
×
53
  }
UNCOV
54
  const code = fs.readFileSync(filename, 'utf8');
×
UNCOV
55
  return requireFromString(code);
×
56
}
57

58
// Dynamically require from string
59
// - `code` - Required - Type: string - Module code.
60
// - `filename` - Type: string - Default: '' - Optional filename.
61
// - `options.appendPaths` Type: Array List of paths, that will be appended to module paths.
62
// Useful, when you want to be able require modules from these paths.
63
// - `options.prependPaths` Type: Array Same as appendPaths, but paths will be prepended.
64
export function requireFromString(
65
  code: string,
66
  filename = '',
×
67
  options?: {
68
    prependPaths?: string[];
69
    appendPaths?: string[];
70
  }
71
): any {
UNCOV
72
  if (typeof filename === 'object') {
×
73
    options = filename;
×
74
    filename = '';
×
75
  }
UNCOV
76
  filename = filename.replace('file://', '');
×
77

UNCOV
78
  if (typeof code !== 'string') {
×
79
    throw new Error(`code must be a string, not ${typeof code}`);
×
80
  }
81

82
  // @ts-ignore
UNCOV
83
  const paths = Module._nodeModulePaths(path.dirname(filename));
×
84

UNCOV
85
  const parent = typeof module !== 'undefined' && module?.parent;
×
86

87
  // @ts-ignore
UNCOV
88
  const newModule = new Module(filename, parent);
×
UNCOV
89
  newModule.filename = filename;
×
UNCOV
90
  newModule.paths = ([] as string[])
×
91
    .concat(options?.prependPaths || [])
×
92
    .concat(paths)
93
    .concat(options?.appendPaths || []);
×
94
  // @ts-ignore
UNCOV
95
  newModule._compile(code, filename);
×
96

UNCOV
97
  if (parent && parent.children) {
×
98
    parent.children.splice(parent.children.indexOf(newModule), 1);
×
99
  }
100

UNCOV
101
  return newModule.exports;
×
102
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc