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

visgl / loaders.gl / 24139120841

08 Apr 2026 01:53PM UTC coverage: 65.319% (+30.2%) from 35.137%
24139120841

push

github

web-flow
chore: Replace puppeteer with playwright (#3350)

14216 of 18890 branches covered (75.26%)

Branch coverage included in aggregate %.

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

3248 existing lines in 369 files now uncovered.

73509 of 115413 relevant lines covered (63.69%)

5763.45 hits per line

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

50.45
/modules/core/src/lib/api/encode.ts
1
// loaders.gl
1✔
2
// SPDX-License-Identifier: MIT
1✔
3
// Copyright (c) vis.gl contributors
1✔
4

1✔
5
import type {
1✔
6
  WriterOptions,
1✔
7
  WriterWithEncoder,
1✔
8
  WriterOptionsType,
1✔
9
  WriterDataType,
1✔
10
  WriterBatchType
1✔
11
} from '@loaders.gl/loader-utils';
1✔
12
import {
1✔
13
  canEncodeWithWorker,
1✔
14
  NodeFile,
1✔
15
  resolvePath,
1✔
16
  isBrowser,
1✔
17
  ensureArrayBuffer
1✔
18
} from '@loaders.gl/loader-utils';
1✔
19
import {processOnWorker} from '@loaders.gl/worker-utils';
1✔
20
import {fetchFile} from '../fetch/fetch-file';
1✔
21
import {getLoaderOptions} from './loader-options';
1✔
22

1✔
23
/**
14✔
24
 * Encode loaded data into a binary ArrayBuffer using the specified Writer.
14✔
25
 */
14✔
26
export async function encode<WriterT extends WriterWithEncoder>(
14✔
27
  data: WriterDataType<WriterT>,
31✔
28
  writer: WriterT,
31✔
29
  options_?: WriterOptionsType<WriterT>
31✔
30
): Promise<ArrayBuffer> {
31✔
31
  const globalOptions = getLoaderOptions() as WriterOptions;
31!
32
  // const globalOptions: WriterOptions = {}; // getWriterOptions();
31✔
33
  const options = {...globalOptions, ...options_};
31!
34

31✔
35
  // Handle the special case where we are invoking external command-line tools
31✔
36
  if (writer.encodeURLtoURL) {
31!
37
    return encodeWithCommandLineTool(writer, data, options);
60✔
38
  }
60✔
39

60✔
40
  // Worker support
60✔
41
  if (canEncodeWithWorker(writer, options)) {
60!
42
    return await processOnWorker(writer, data, options);
×
43
  }
×
44

×
45
  // TODO Merge default writer options with options argument like it is done in load module.
×
46
  return await writer.encode(data, options);
31✔
47
}
×
UNCOV
48

×
UNCOV
49
/**
×
UNCOV
50
 * Encode loaded data into a binary ArrayBuffer using the specified Writer.
×
UNCOV
51
 */
×
UNCOV
52
export function encodeSync<WriterT extends WriterWithEncoder>(
✔
53
  data: WriterDataType<WriterT>,
60✔
54
  writer: WriterT,
60✔
55
  options?: WriterOptionsType<WriterT>
60✔
56
): ArrayBuffer {
60✔
57
  if (writer.encodeSync) {
60✔
58
    return writer.encodeSync(data, options);
60✔
59
  }
60✔
60
  if (writer.encodeTextSync) {
×
61
    return ensureArrayBuffer(new TextEncoder().encode(writer.encodeTextSync(data, options)));
×
62
  }
×
63
  throw new Error(`Writer ${writer.name} could not synchronously encode data`);
✔
64
}
6✔
65

6✔
66
/**
6✔
67
 * Encode loaded data to text using the specified Writer
6✔
68
 * @note This is a convenience function not intended for production use on large input data.
6✔
69
 * It is not optimized for performance. Data maybe converted from text to binary and back.
6✔
70
 * @throws if the writer does not generate text output
6✔
71
 */
6✔
72
export async function encodeText<WriterT extends WriterWithEncoder>(
1✔
73
  data: WriterDataType<WriterT>,
×
74
  writer: WriterT,
×
75
  options?: WriterOptionsType<WriterT>
×
76
): Promise<string> {
×
77
  if (writer.encodeText) {
×
78
    return await writer.encodeText(data, options);
×
79
  }
×
80

×
81
  if (writer.encodeTextSync) {
×
82
    return writer.encodeTextSync(data, options);
×
83
  }
×
84

×
85
  if (writer.text) {
×
86
    const arrayBuffer = await writer.encode(data, options);
×
87
    return new TextDecoder().decode(arrayBuffer);
×
88
  }
×
89

×
90
  throw new Error(`Writer ${writer.name} could not encode data as text`);
×
91
}
×
92

1✔
93
/**
1✔
94
 * Encode loaded data to text using the specified Writer
1✔
95
 * @note This is a convenience function not intended for production use on large input data.
1✔
96
 * It is not optimized for performance. Data maybe converted from text to binary and back.
1✔
97
 * @throws if the writer does not generate text output
1✔
98
 */
1✔
99
export function encodeTextSync<WriterT extends WriterWithEncoder>(
1✔
100
  data: WriterDataType<WriterT>,
6✔
101
  writer: WriterT,
6✔
102
  options?: WriterOptionsType<WriterT>
6✔
103
): string {
6✔
104
  if (writer.encodeTextSync) {
6✔
105
    return writer.encodeTextSync(data, options);
6✔
106
  }
×
107

×
108
  if (writer.text && writer.encodeSync) {
×
109
    const arrayBuffer = encodeSync(data, writer, options);
×
110
    return new TextDecoder().decode(arrayBuffer);
×
111
  }
×
112

×
113
  throw new Error(`Writer ${writer.name} could not encode data as text`);
×
114
}
×
UNCOV
115

×
UNCOV
116
/**
×
UNCOV
117
 * Encode loaded data into a sequence (iterator) of binary ArrayBuffers using the specified Writer.
×
UNCOV
118
 */
×
UNCOV
119
export function encodeInBatches<WriterT extends WriterWithEncoder>(
×
120
  data: WriterBatchType<WriterT>,
×
121
  writer: WriterT,
×
122
  options?: WriterOptionsType<WriterT>
×
123
): AsyncIterable<ArrayBuffer> {
×
124
  if (writer.encodeInBatches) {
×
125
    const dataIterator = getIterator(data);
×
126
    // @ts-expect-error
×
127
    return writer.encodeInBatches(dataIterator, options);
×
128
  }
×
129
  // TODO -fall back to atomic encode?
×
130
  throw new Error(`Writer ${writer.name} could not encode in batches`);
×
131
}
×
132

1✔
133
/**
1✔
134
 * Encode loaded data into a sequence (iterator) of binary ArrayBuffers using the specified Writer.
1✔
135
 */
1✔
136
export function encodeTextInBatches(
1✔
137
  data: unknown,
×
138
  writer: WriterWithEncoder,
×
139
  options?: WriterOptions
×
140
): AsyncIterable<ArrayBuffer> {
×
141
  if (writer.encodeTextInBatches) {
×
142
    const dataIterator = getIterator(data);
×
143
    // @ts-expect-error
×
144
    return writer.encodeTextInBatches(dataIterator, options);
×
145
  }
×
146
  // TODO -fall back to atomic encode?
×
147
  throw new Error(`Writer ${writer.name} could not encode text in batches`);
×
148
}
×
149

1✔
150
/**
1✔
151
 * Encode data stored in a file (on disk) to another file.
1✔
152
 * @note Node.js only. This function enables using command-line converters as "writers".
1✔
153
 */
1✔
154
export async function encodeURLtoURL(
1✔
155
  inputUrl: string,
1✔
156
  outputUrl: string,
1✔
157
  writer: Omit<WriterWithEncoder, 'encode'>,
1✔
158
  options?: WriterOptions
1✔
159
): Promise<string> {
1✔
160
  inputUrl = resolvePath(inputUrl);
1✔
161
  outputUrl = resolvePath(outputUrl);
1✔
162
  if (isBrowser || !writer.encodeURLtoURL) {
1!
163
    throw new Error();
×
164
  }
×
165
  const outputFilename = await writer.encodeURLtoURL(inputUrl, outputUrl, options);
1✔
166
  return outputFilename;
1✔
167
}
1✔
168

1✔
169
/** Helper function to encode via external tool (typically command line execution in Node.js) */
1✔
170
async function encodeWithCommandLineTool(
×
171
  writer: WriterWithEncoder,
×
172
  data: unknown,
×
173
  options: WriterOptions
×
174
): Promise<ArrayBuffer> {
×
175
  if (isBrowser) {
×
176
    throw new Error(`Writer ${writer.name} not supported in browser`);
×
177
  }
×
178
  // TODO - how to generate filenames with correct extensions?
×
179
  const tmpInputFilename = getTemporaryFilename('input');
×
180
  const file = new NodeFile(tmpInputFilename, 'w');
×
181
  await file.write(data as ArrayBuffer);
×
182

×
183
  const tmpOutputFilename = getTemporaryFilename('output');
×
184

×
185
  const outputFilename = await encodeURLtoURL(tmpInputFilename, tmpOutputFilename, writer, options);
×
186

×
187
  const response = await fetchFile(outputFilename);
×
188
  return response.arrayBuffer();
×
189
}
×
190

1✔
191
/**
1✔
192
 * @todo TODO - this is an unacceptable hack!!!
1✔
193
 */
1✔
194
function getIterator(data: any): Iterable<{table: any; start: number; end: number}> {
×
195
  const dataIterator = [{...data, start: 0, end: data.length}];
×
196
  return dataIterator;
×
197
}
×
198

1✔
199
/**
1✔
200
 * @todo Move to utils
1✔
201
 */
1✔
202
function getTemporaryFilename(filename: string): string {
×
203
  return `/tmp/${filename}`;
×
204
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc