Skip to content

Commit

Permalink
Merge branch 'fix/walletTxStream' of github.com:kajoseph/bitcore
Browse files Browse the repository at this point in the history
  • Loading branch information
kajoseph committed May 30, 2024
2 parents db75179 + 2c1079d commit c96899d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ export class BaseEVMExternalStateProvider extends InternalStateProvider implemen
const mergedStream = new MergedStream();
const txStreams: Readable[] = [];
// Only mergedStream writes to res object
const _mergedStream = ExternalApiStream.onStream(mergedStream, req!, res!);
const _mergedStream = ExternalApiStream.onStream(mergedStream, req!, res!, { jsonl: true });

// Default to pulling only the first 10 transactions per address
for (let i = 0; i < walletAddresses.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import axios from 'axios';
import { Request, Response } from 'express';
import { Readable, Stream, Transform, Writable } from 'stream';


export interface StreamOpts {
jsonl?: boolean;
}

export class ExternalApiStream extends Readable {
url: string;
headers: any;
Expand Down Expand Up @@ -71,22 +76,22 @@ export class ExternalApiStream extends Readable {
}

// handles events emitted by the streamed response, request from client, and response to client
static onStream(stream: Readable, req: Request, res: Response):
static onStream(stream: Readable, req: Request, res: Response, opts: StreamOpts = {}):
Promise<{ success: boolean, error?: any }> {
return new Promise<{ success: boolean, error?: any }>((resolve, reject) => {
let closed = false;
let isFirst = true;

req.on('close', function () {
req.on('close', function() {
closed = true;
});

res.type('json');
res.on('close', function () {
res.on('close', function() {
closed = true;
});

stream.on('error', function (err: any) {
stream.on('error', function(err: any) {
if (!closed) {
closed = true;
if (err.isAxiosError) {
Expand All @@ -108,27 +113,37 @@ export class ExternalApiStream extends Readable {
}
return;
});
stream.on('data', function (data) {
stream.on('data', function(data) {
if (!closed) {
if (isFirst) {
res.write('[\n');
isFirst = false;
if (opts.jsonl) {
if (isFirst) {
isFirst = false;
} else {
res.write('\n');
}
} else {
res.write(',\n');
if (isFirst) {
res.write('[\n');
isFirst = false;
} else {
res.write(',\n');
}
}
res.write(data);
} else {
stream.destroy();
}
});
stream.on('end', function () {
stream.on('end', function() {
if (!closed) {
if (isFirst) {
// there was no data
res.write('[]');
} else {
res.write('\n]');
closed = true;
closed = true;
if (!opts.jsonl) {
if (isFirst) {
// there was no data
res.write('[]');
} else {
res.write('\n]');
}
}
res.end();
resolve({ success: true });
Expand Down

0 comments on commit c96899d

Please sign in to comment.