mirror of
https://github.com/the-djmaze/snappymail.git
synced 2025-10-08 20:56:03 +08:00
Improved streaming as preperation for #300
This commit is contained in:
parent
b138c1b4a0
commit
2fae820ee4
2 changed files with 27 additions and 26 deletions
|
@ -81,17 +81,18 @@ export class AbstractFetchRemote
|
||||||
* Can be used to stream lines of json encoded data, but does not work on all servers.
|
* Can be used to stream lines of json encoded data, but does not work on all servers.
|
||||||
* Apache needs 'flushpackets' like in <Proxy "fcgi://...." flushpackets=on></Proxy>
|
* Apache needs 'flushpackets' like in <Proxy "fcgi://...." flushpackets=on></Proxy>
|
||||||
*/
|
*/
|
||||||
streamPerLine(fCallback, sGetAdd) {
|
streamPerLine(fCallback, sGetAdd, postData) {
|
||||||
rl.fetch(getURL(sGetAdd))
|
rl.fetch(getURL(sGetAdd), {}, postData)
|
||||||
.then(response => response.body)
|
.then(response => response.body)
|
||||||
.then(body => {
|
.then(body => {
|
||||||
// Firefox TextDecoderStream is not defined
|
|
||||||
// const reader = body.pipeThrough(new TextDecoderStream()).getReader();
|
|
||||||
const reader = body.getReader(),
|
|
||||||
re = /\r\n|\n|\r/gm,
|
|
||||||
utf8decoder = new TextDecoder();
|
|
||||||
let buffer = '';
|
let buffer = '';
|
||||||
function processText({ done, value }) {
|
const
|
||||||
|
// Firefox TextDecoderStream is not defined
|
||||||
|
// reader = body.pipeThrough(new TextDecoderStream()).getReader();
|
||||||
|
reader = body.getReader(),
|
||||||
|
re = /\r\n|\n|\r/gm,
|
||||||
|
utf8decoder = new TextDecoder(),
|
||||||
|
processText = ({ done, value }) => {
|
||||||
buffer += value ? utf8decoder.decode(value, {stream: true}) : '';
|
buffer += value ? utf8decoder.decode(value, {stream: true}) : '';
|
||||||
for (;;) {
|
for (;;) {
|
||||||
let result = re.exec(buffer);
|
let result = re.exec(buffer);
|
||||||
|
@ -110,7 +111,7 @@ export class AbstractFetchRemote
|
||||||
// last line didn't end in a newline char
|
// last line didn't end in a newline char
|
||||||
fCallback(buffer);
|
fCallback(buffer);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
reader.read().then(processText);
|
reader.read().then(processText);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
namespace SnappyMail\HTTP;
|
namespace SnappyMail\HTTP;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Can be used with JavaScript AbstractFetchRemote.streamPerLine(fCallback, sGetAdd)
|
* Can be used with JavaScript AbstractFetchRemote.streamPerLine(fCallback, sGetAdd, postData)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
abstract class Stream
|
abstract class Stream
|
||||||
|
|
Loading…
Add table
Reference in a new issue