Created
August 17, 2019 17:00
-
-
Save tweinfeld/9e002c5e72afde09c8c9e90e53d3cb9e to your computer and use it in GitHub Desktop.
Read a JSONL file over chunked response stream over Kefir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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