Skip to content

Instantly share code, notes, and snippets.

@Wanchai
Last active October 31, 2024 14:15
Show Gist options
  • Select an option

  • Save Wanchai/1c4354e83f1fdb7b6d361f684a6a3228 to your computer and use it in GitHub Desktop.

Select an option

Save Wanchai/1c4354e83f1fdb7b6d361f684a6a3228 to your computer and use it in GitHub Desktop.
Reading through a HTTP stream with RxJs. It needs to be improved but this is a working solution to decode a stream of data.
from(
fetch('https://dev.com', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization:
'Bearer XXX',
},
body: JSON.stringify({}),
}),
)
.pipe(
map((resp) => {
return resp.body?.pipeThrough(new TextDecoderStream());
}),
tap((stream) => {
if (!stream) {
return;
}
const rd = stream.getReader();
of(rd)
.pipe(
switchMap((rd) => rd.read()),
repeat(),
tap((rd) => {
console.log(rd);
}),
takeWhile((rd) => !rd.done),
)
.subscribe();
}),
)
.subscribe();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment