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

Function $streamJSON

src/danfojs-base/io/node/io.json.ts:83–128  ·  view source on GitHub ↗
(
    filePath: string,
    callback: (df: DataFrame) => void,
    options?: request.RequiredUriUrl & request.CoreOptions,
)

Source from the content-addressed store, hash-verified

81 * ```
82 */
83const $streamJSON = async (
84 filePath: string,
85 callback: (df: DataFrame) => void,
86 options?: request.RequiredUriUrl & request.CoreOptions,
87) => {
88 const { method, headers, frameConfig } = { method: "GET", headers: {}, frameConfig: {}, ...options }
89 if (filePath.startsWith("http") || filePath.startsWith("https")) {
90 return new Promise((resolve, reject) => {
91 let count = 0;
92 const dataStream = request({ url: filePath, method, headers })
93
94 // reject any non-2xx status codes
95 dataStream.on('response', (response: any) => {
96 if (response.statusCode < 200 || response.statusCode >= 300) {
97 reject(new Error(`HTTP ${response.statusCode}: ${response.statusMessage}`));
98 }
99 });
100
101
102 const pipeline = dataStream.pipe(parser()).pipe(streamArray());
103 pipeline.on('data', ({ value }) => {
104 const df = new DataFrame([value], { ...frameConfig, index: [count++] });
105 callback(df);
106 });
107 pipeline.on('end', () => resolve(null));
108
109 });
110 } else {
111 return new Promise((resolve, reject) => {
112 fs.access(filePath, fs.constants.F_OK, (err) => {
113 if (err) {
114 reject("ENOENT: no such file or directory");
115 }
116
117 let count = 0
118 const fileStream = fs.createReadStream(filePath)
119 const pipeline = fileStream.pipe(parser()).pipe(streamArray());
120 pipeline.on('data', ({ value }) => {
121 const df = new DataFrame([value], { ...frameConfig, index: [count++] });
122 callback(df);
123 });
124 pipeline.on('end', () => resolve(null));
125 });
126 })
127 }
128};
129
130
131/**

Callers

nothing calls this directly

Calls 1

startsWithMethod · 0.80

Tested by

no test coverage detected