• 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

6.06
/modules/polyfills/src/filesystems/fetch-node.ts
1
// loaders.gl
2
// SPDX-License-Identifier: MIT
3
// Copyright (c) vis.gl contributors
4

5
import fs from 'fs';
6
import {Readable} from 'stream';
7
import {resolvePath} from '@loaders.gl/loader-utils';
8
import {decompressReadStream} from './stream-utils.node';
9

10
const isBoolean = x => typeof x === 'boolean';
4✔
11
const isFunction = x => typeof x === 'function';
4✔
12
const isObject = x => x !== null && typeof x === 'object';
4!
13
const isReadableNodeStream = x =>
4✔
UNCOV
14
  isObject(x) && isFunction(x.read) && isFunction(x.pipe) && isBoolean(x.readable);
×
15

16
/**
17
 * Enables
18
 * @param url
19
 * @param options
20
 * @returns
21
 */
22
// eslint-disable-next-line max-statements
23
export async function fetchNode(url: string, options?: RequestInit): Promise<Response> {
24
  // Support `file://` protocol
UNCOV
25
  const FILE_PROTOCOL_REGEX = /^file:\/\//;
×
UNCOV
26
  url.replace(FILE_PROTOCOL_REGEX, '/');
×
27

28
  // Remove any query parameters, as they have no meaning
UNCOV
29
  let noqueryUrl = url.split('?')[0];
×
UNCOV
30
  noqueryUrl = resolvePath(noqueryUrl);
×
31

UNCOV
32
  const responseHeaders = new Headers();
×
33
  // Automatically decompress gzipped files with .gz extension
UNCOV
34
  if (url.endsWith('.gz')) {
×
35
    // url = url.slice(0, -3);
UNCOV
36
    responseHeaders.set('content-encoding', 'gzip');
×
37
  }
UNCOV
38
  if (url.endsWith('.br')) {
×
39
    // url = url.slice(0, -3);
40
    responseHeaders.set('content-encoding', 'br');
×
41
  }
42

UNCOV
43
  try {
×
44
    // Now open the stream
UNCOV
45
    const body = await new Promise<fs.ReadStream>((resolve, reject) => {
×
46
      // @ts-ignore
UNCOV
47
      const stream = fs.createReadStream(noqueryUrl, {encoding: null});
×
UNCOV
48
      stream.once('readable', () => resolve(stream));
×
UNCOV
49
      stream.on('error', error => reject(error));
×
50
    });
51

UNCOV
52
    let bodyStream: Readable = body;
×
53

54
    // Check for content-encoding and create a decompression stream
UNCOV
55
    if (isReadableNodeStream(body)) {
×
UNCOV
56
      bodyStream = decompressReadStream(body, responseHeaders);
×
UNCOV
57
    } else if (typeof body === 'string') {
×
58
      bodyStream = Readable.from([new TextEncoder().encode(body)]);
×
59
    } else {
60
      bodyStream = Readable.from([body || new ArrayBuffer(0)]);
×
61
    }
62

UNCOV
63
    const status = 200;
×
UNCOV
64
    const statusText = 'OK';
×
UNCOV
65
    const headers = getHeadersForFile(noqueryUrl);
×
66
    // @ts-expect-error
UNCOV
67
    const response = new Response(bodyStream, {headers, status, statusText});
×
UNCOV
68
    Object.defineProperty(response, 'url', {value: url});
×
UNCOV
69
    return response;
×
70
  } catch (error) {
71
    // console.error(error);
UNCOV
72
    const errorMessage = (error as Error).message;
×
UNCOV
73
    const status = 400;
×
UNCOV
74
    const statusText = errorMessage;
×
UNCOV
75
    const headers = {};
×
UNCOV
76
    const response = new Response(errorMessage, {headers, status, statusText});
×
UNCOV
77
    Object.defineProperty(response, 'url', {value: url});
×
UNCOV
78
    return response;
×
79
  }
80
}
81

82
function getHeadersForFile(noqueryUrl: string): Headers {
UNCOV
83
  const headers = {};
×
84

85
  // Fix up content length if we can for best progress experience
UNCOV
86
  if (!headers['content-length']) {
×
UNCOV
87
    const stats = fs.statSync(noqueryUrl);
×
UNCOV
88
    headers['content-length'] = stats.size;
×
89
  }
90

91
  // Automatically decompress gzipped files with .gz extension
UNCOV
92
  if (noqueryUrl.endsWith('.gz')) {
×
UNCOV
93
    noqueryUrl = noqueryUrl.slice(0, -3);
×
UNCOV
94
    headers['content-encoding'] = 'gzip';
×
95
  }
96

UNCOV
97
  return new Headers(headers);
×
98
}
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