@sap/odata-v4
Version:
OData V4.0 server library
68 lines (54 loc) • 1.74 kB
JavaScript
;
const CRLF = '\r\n';
const Reader = require('./Reader');
/**
* Events
*/
const EVENTS = {
DATA: 'line.data' // body reader consumed data from cache
};
/**
* Reads a line from the cache, the line is terminated by CRLF
*
* @extends Reader
*/
class LineReader extends Reader {
constructor() {
super();
this._line = null;
}
/**
* Returns the read line
* @returns {string}
*/
getLine() {
return this._line;
}
/**
* Reads a line from the cache. A line is terminated by CRLF.
* If the cache is empty and more data is required, this is signaled and readCach is called again. The state is preserved.
*
* @param {ContentDeserializer} reader - Current reader
* @param {Cache} cache - Cache containing data for processing
* @returns {boolean}
* this: this reader needs more data and caller should call this method again with more data in cache
* false: this reader is finished caller should pop this reader from stack
* null: new sub reader is on stack, call this method again after the sub reader is finished
*/
readCache(reader, cache) {
if (cache.getLength() < CRLF.length) {
return true;
}
const pos = cache.indexOf(CRLF, cache.getSearchPosition());
if (pos === -1) {
return true;
}
// line may be empty
this._line = cache.getBytesTo(pos);
this.emitAndConsume(cache, pos - cache.getReadPos(), EVENTS.DATA); // emit only the line, not the CLRF
cache.advance(CRLF.length);
return false;
}
}
LineReader.EVENTS = EVENTS;
module.exports = LineReader;