Skip to content

Instantly share code, notes, and snippets.

@tweinfeld
Created August 17, 2019 17:00
Show Gist options
  • Save tweinfeld/9e002c5e72afde09c8c9e90e53d3cb9e to your computer and use it in GitHub Desktop.
Save tweinfeld/9e002c5e72afde09c8c9e90e53d3cb9e to your computer and use it in GitHub Desktop.
Read a JSONL file over chunked response stream over Kefir
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/kefir@3.8.5/dist/kefir.min.js"></script>
</head>
<body>
<script>
Kefir
.fromPromise(fetch('./sample.jsonl').then((res)=> res.body.getReader()))
.flatMap((reader)=>{
return Kefir
.stream(({ emit, error, end })=> {
const iterate = ()=> reader.read().then(({ value, done })=> (done ? end : _.flow(emit, iterate))(new TextDecoder("utf-8").decode(value)), error);
iterate();
})
.flatten(((ac)=>{
return (chunk)=> {
let arr = _.split(ac + chunk, '\n');
const val = arr.splice('\n');
ac = arr[0];
return val;
};
})(''));
})
.map(JSON.parse)
.log('>');
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment