MCPcopy Create free account
hub / github.com/facebook/memlab / readCallback

Method readCallback

packages/core/src/lib/StringLoader.ts:60–115  ·  view source on GitHub ↗
(err: NodeFSError, bytes: number)

Source from the content-addressed store, hash-verified

58 let _pos = 0;
59
60 const readCallback = function (err: NodeFSError, bytes: number) {
61 _errHandler(err);
62
63 const str = _buffer.slice(0, bytes).toString('utf8');
64
65 if (!_begin && !_startSig) {
66 // start reserving content if no start signature is specified
67 _begin = true;
68 }
69
70 _result += str;
71
72 // look for start signature
73 if (!_begin && _startSig != null) {
74 const idx = _result.indexOf(_startSig);
75 if (idx >= 0) {
76 _begin = true;
77 _result = _result.substring(idx + _startSig.length);
78 } else if (_result.length > _startSig.length) {
79 _result = _result.substring(_result.length - _startSig.length);
80 }
81 }
82
83 // look for end signature
84 if (_begin && _endSig) {
85 let searchIdx = Math.max(
86 0,
87 _result.length - str.length - _endSig.length,
88 );
89 if (_startSig && searchIdx < _startSig.length) {
90 searchIdx = _startSig.length;
91 }
92 const idx = _result.indexOf(_endSig, searchIdx);
93 if (idx >= 0) {
94 _result = _result.substring(0, idx);
95 _end = true;
96 }
97 }
98
99 if (_end || bytes < _buffer.length) {
100 if (!_begin) {
101 _result = '';
102 }
103 if (inclusive && _startSig) {
104 _result = _startSig + _result;
105 }
106 if (inclusive && _endSig && _end) {
107 _result += _endSig;
108 }
109 _resolve(_result);
110 fs.close(_fd, _errHandler);
111 } else {
112 _pos += bytes;
113 fs.read(_fd, _buffer, 0, _buffer.length, _pos, readCallback);
114 }
115 };
116
117 fs.read(_fd, _buffer, 0, _buffer.length, _pos, readCallback);

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected