• 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

1.61
/modules/loader-utils/src/lib/worker-loader-utils/create-loader-worker.ts
1
/* eslint-disable no-restricted-globals */
2
import type {LoaderWithParser, LoaderOptions, LoaderContext} from '../../loader-types';
3
import {WorkerBody} from '@loaders.gl/worker-utils';
4
// import {validateLoaderVersion} from './validate-loader-version';
5

6
let requestId = 0;
348✔
7

8
/**
9
 * Set up a WebWorkerGlobalScope to talk with the main thread
10
 * @param loader
11
 */
12
export async function createLoaderWorker(loader: LoaderWithParser) {
13
  // Check that we are actually in a worker thread
UNCOV
14
  if (!(await WorkerBody.inWorkerThread())) {
×
15
    return;
×
16
  }
17

UNCOV
18
  WorkerBody.onmessage = async (type, payload) => {
×
UNCOV
19
    switch (type) {
×
20
      case 'process':
UNCOV
21
        try {
×
22
          // validateLoaderVersion(loader, data.source.split('@')[1]);
23

UNCOV
24
          const {input, options = {}, context = {}} = payload;
×
25

UNCOV
26
          const result = await parseData({
×
27
            loader,
28
            arrayBuffer: input,
29
            options,
30
            // @ts-expect-error fetch missing
31
            context: {
32
              ...context,
33
              _parse: parseOnMainThread
34
            }
35
          });
36
          WorkerBody.postMessage('done', {result});
×
37
        } catch (error) {
38
          const message = error instanceof Error ? error.message : '';
×
39
          WorkerBody.postMessage('error', {error: message});
×
40
        }
41
        break;
×
42
      default:
43
    }
44
  };
45
}
46

47
function parseOnMainThread(
48
  arrayBuffer: ArrayBuffer,
49
  loader: any,
50
  options?: LoaderOptions,
51
  context?: LoaderContext
52
): Promise<void> {
53
  return new Promise((resolve, reject) => {
×
54
    const id = requestId++;
×
55

56
    /**
57
     */
58
    const onMessage = (type, payload) => {
×
59
      if (payload.id !== id) {
×
60
        // not ours
61
        return;
×
62
      }
63

64
      switch (type) {
×
65
        case 'done':
66
          WorkerBody.removeEventListener(onMessage);
×
67
          resolve(payload.result);
×
68
          break;
×
69

70
        case 'error':
71
          WorkerBody.removeEventListener(onMessage);
×
72
          reject(payload.error);
×
73
          break;
×
74

75
        default:
76
        // ignore
77
      }
78
    };
79

80
    WorkerBody.addEventListener(onMessage);
×
81

82
    // Ask the main thread to decode data
83
    const payload = {id, input: arrayBuffer, options};
×
84
    WorkerBody.postMessage('process', payload);
×
85
  });
86
}
87

88
// TODO - Support byteOffset and byteLength (enabling parsing of embedded binaries without copies)
89
// TODO - Why not support async loader.parse* funcs here?
90
// TODO - Why not reuse a common function instead of reimplementing loader.parse* selection logic? Keeping loader small?
91
// TODO - Lack of appropriate parser functions can be detected when we create worker, no need to wait until parse
92
async function parseData({
93
  loader,
94
  arrayBuffer,
95
  options,
96
  context
97
}: {
98
  loader: LoaderWithParser;
99
  arrayBuffer: ArrayBuffer;
100
  options: LoaderOptions;
101
  context: LoaderContext;
102
}) {
103
  let data;
104
  let parser;
UNCOV
105
  if (loader.parseSync || loader.parse) {
×
UNCOV
106
    data = arrayBuffer;
×
UNCOV
107
    parser = loader.parseSync || loader.parse;
×
UNCOV
108
  } else if (loader.parseTextSync) {
×
109
    const textDecoder = new TextDecoder();
×
110
    data = textDecoder.decode(arrayBuffer);
×
111
    parser = loader.parseTextSync;
×
112
  } else {
113
    throw new Error(`Could not load data with ${loader.name} loader`);
×
114
  }
115

116
  // TODO - proper merge in of loader options...
UNCOV
117
  options = {
×
118
    ...options,
119
    modules: (loader && loader.options && loader.options.modules) || {},
×
120
    core: {
121
      ...options.core,
122
      worker: false
123
    }
124
  };
125

UNCOV
126
  return await parser(data, {...options}, context, loader);
×
127
}
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