MCPcopy Index your code
hub / github.com/javascriptdata/danfojs / $streamCSV

Function $streamCSV

src/danfojs-base/io/node/io.csv.ts:171–266  ·  view source on GitHub ↗
(filePath: string, callback: (df: DataFrame) => void, options?: CsvInputOptionsNode)

Source from the content-addressed store, hash-verified

169 * ```
170 */
171const $streamCSV = async (filePath: string, callback: (df: DataFrame) => void, options?: CsvInputOptionsNode): Promise<null> => {
172 const frameConfig = options?.frameConfig || {}
173
174 if (filePath.startsWith("http") || filePath.startsWith("https")) {
175 const optionsWithDefaults = {
176 header: true,
177 dynamicTyping: true,
178 ...options,
179 }
180 return new Promise((resolve, reject) => {
181 let count = 0;
182 let hasError = false;
183 const dataStream = request.get(filePath);
184
185 // reject any non-2xx status codes
186 dataStream.on('response', (response: any) => {
187 if (response.statusCode < 200 || response.statusCode >= 300) {
188 hasError = true;
189 reject(new Error(`HTTP ${response.statusCode}: ${response.statusMessage}`));
190 }
191 });
192
193 const parseStream: any = Papa.parse(Papa.NODE_STREAM_INPUT, optionsWithDefaults);
194 dataStream.pipe(parseStream);
195
196 parseStream.on("data", (chunk: any) => {
197 if (hasError) return;
198 try {
199 const df = new DataFrame([chunk], { ...frameConfig, index: [count++] });
200 callback(df);
201 } catch (error) {
202 hasError = true;
203 const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';
204 reject(new Error(`Failed to process CSV chunk: ${errorMessage}`));
205 }
206 });
207
208 parseStream.on("error", (error: any) => {
209 hasError = true;
210 reject(new Error(`Failed to parse CSV: ${error.message}`));
211 });
212
213 parseStream.on("finish", () => {
214 if (!hasError) {
215 resolve(null);
216 }
217 });
218 });
219 } else {
220 return new Promise((resolve, reject) => {
221 fs.access(filePath, fs.constants.F_OK, (err) => {
222 if (err) {
223 reject(new Error("ENOENT: no such file or directory"));
224 return;
225 }
226
227 const fileStream = fs.createReadStream(filePath)
228 let hasError = false;

Callers

nothing calls this directly

Calls 4

startsWithMethod · 0.80
getMethod · 0.80
joinMethod · 0.80
mapMethod · 0.65

Tested by

no test coverage detected