Skip to content

Commit 8e00cb1

Browse files
committed
add comments
1 parent f0ed660 commit 8e00cb1

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

packages/bitcore-node/src/providers/chain-state/evm/api/ecsp.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,18 @@ import {
2020
import { unixToDate } from '../../../../utils/convert';
2121
import { StatsUtil } from '../../../../utils/stats';
2222
import MoralisAPI from '../../external/providers/moralis';
23-
import { ExternalApiStream, MergedStream, ParseTransform } from '../../external/streams/apiStream';
23+
import { ExternalApiStream, MergedStream, ParseStream } from '../../external/streams/apiStream';
2424
import { NodeQueryStream } from '../../external/streams/nodeStream';
2525
import { InternalStateProvider } from '../../internal/internal';
2626
import { EVMTransactionStorage } from '../models/transaction';
2727
import { EVMTransactionJSON } from '../types';
2828
import { BaseEVMStateProvider } from './csp';
29+
import { PopulateReceiptTransform } from './populateReceiptTransform';
2930
import {
3031
getProvider,
3132
isValidProviderType
3233
} from './provider';
3334
import { EVMListTransactionsStream } from './transform';
34-
import { PopulateReceiptTransform } from './populateReceiptTransform';
3535

3636

3737
export interface GetWeb3Response { rpc: CryptoRpc; web3: Web3; dataType: string }
@@ -268,16 +268,10 @@ export class BaseEVMExternalStateProvider extends InternalStateProvider implemen
268268
const txStreams: Readable[] = [];
269269
const ethTransactionTransform = new EVMListTransactionsStream(walletAddresses);
270270
const populateReceipt = new PopulateReceiptTransform();
271-
const parseStrings = new ParseTransform();
271+
const parseStream = new ParseStream();
272272
const mergedStream = new MergedStream(); // Stream to combine the output of multiple streams
273273
const resultStream = new MergedStream(); // Stream to write to the res object
274274

275-
// Transform transactions proccessed through merged stream
276-
mergedStream
277-
.pipe(parseStrings)
278-
.pipe(populateReceipt)
279-
.pipe(ethTransactionTransform)
280-
.pipe(resultStream);
281275
// Tip height used to calculate confirmations
282276
args.tipHeight = tip ? tip.height : 0;
283277
// Defaults to pulling only the first 10 transactions per address
@@ -287,7 +281,13 @@ export class BaseEVMExternalStateProvider extends InternalStateProvider implemen
287281
}
288282
// Pipe all txStreams to the mergedStream
289283
ExternalApiStream.mergeStreams(txStreams, mergedStream);
290-
// Ensure mergeStream resolves
284+
// Transform transactions proccessed through merged stream
285+
mergedStream
286+
.pipe(parseStream)
287+
.pipe(populateReceipt)
288+
.pipe(ethTransactionTransform)
289+
.pipe(resultStream);
290+
// Ensure resultStream resolves
291291
const result = await ExternalApiStream.onStream(resultStream, req!, res!, { jsonl: true });
292292
if (result?.success === false) {
293293
logger.error('Error mid-stream (streamWalletTransactions): %o', result.error?.log || result.error);

packages/bitcore-node/src/providers/chain-state/external/streams/apiStream.ts

+10-9
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ export class ExternalApiStream extends Readable {
103103
}
104104
}
105105
if (!isFirst) {
106+
// Data has already been written to the stream and status 200 headers have already been sent
107+
// We notify and log the error instead of throwing
106108
const errMsg = '{"error": "An error occurred during data stream"}';
107109
if (opts.jsonl) {
108110
res.write(`${errMsg}`);
@@ -113,27 +115,26 @@ export class ExternalApiStream extends Readable {
113115
res.destroy();
114116
return resolve({ success: false, error: err });
115117
} else {
118+
// Rejecting here allows downstream to send status 500
116119
return reject(err);
117120
}
118121
}
119122
return;
120123
});
121124
stream.on('data', function(data) {
122125
if (!closed) {
123-
if (opts.jsonl) {
124-
if (isFirst) {
125-
isFirst = false;
126-
} else if (!data.endsWith('\n')) {
127-
res.write('\n');
128-
}
129-
} else {
126+
// We are assuming jsonl data appended a new line upstream
127+
if (!opts.jsonl) {
130128
if (isFirst) {
131129
res.write('[\n');
132-
isFirst = false;
133130
} else {
134131
res.write(',\n');
135132
}
136133
}
134+
if (isFirst) {
135+
// All cases need isFirst set correctly for proper error handling
136+
isFirst = false;
137+
}
137138
res.write(data);
138139
} else {
139140
stream.destroy();
@@ -175,7 +176,7 @@ export class ExternalApiStream extends Readable {
175176
}
176177
}
177178

178-
export class ParseTransform extends Transform {
179+
export class ParseStream extends Transform {
179180
constructor() {
180181
super({ objectMode: true });
181182
}

0 commit comments

Comments
 (0)