Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read response pathfinder2 #196

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Make lint happy
llunaCreixent committed May 15, 2023

Verified

This commit was signed with the committer’s verified signature.
Eliulm Elias Wahl
commit 659fd175b98cb0df5665a1ff8383c6b15e994e59
106 changes: 60 additions & 46 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -36,60 +36,68 @@ async function processResponseJson(response) {
}
resolve(response.body);
}
})
});
}

async function processResponseNdjson(response, data) {
let buffer = ''
let jsons = []
let final
return new Promise((resolve, reject) => {
resolve(response.body)
})
.then(res => {
let buffer = '';
let jsons = [];
let final;
return new Promise((resolve) => {
resolve(response.body);
}).then((res) => {
return new Promise((resolve, reject) => {
res.on('readable', () => {
console.log("readable...*");
let result
const decoder = new TextDecoder()
// console.log('readable...*');
let result;
const decoder = new TextDecoder();
while (null !== (result = res.read())) {
buffer += decoder.decode(result)
let idx = buffer.indexOf("\n")
while(idx !== -1) {
const text = buffer.substring(0, idx)
buffer += decoder.decode(result);
let idx = buffer.indexOf('\n');
while (idx !== -1) {
const text = buffer.substring(0, idx);
try {
const jsonText = JSON.parse(text)
console.log(jsonText)
jsons.push(jsonText)
const jsonText = JSON.parse(text);
// console.log(jsonText);
jsons.push(jsonText);
if (jsonText.result.maxFlowValue === data.params.value) {
final = jsonText
res.destroy()
final = jsonText;
res.destroy();
}
} catch(error) {
console.warn(text)
} catch (error) {
// console.warn(text);
reject(error);
}
buffer = buffer.substring(idx + 1)
idx = buffer.indexOf("\n")
buffer = buffer.substring(idx + 1);
idx = buffer.indexOf('\n');
}
}
})
res.on('end', () => { // If haven't received a matching result yet, then return the last result
console.log("END!");
console.log({final});
console.log({jsons});
resolve(jsons.pop())
});
res.on("close", function (err) {
console.log("Stream has been destroyed and file has been closed");
console.log({final});
console.log({jsons});
resolve(final ? final : jsons.pop())
})
})
})
res.on('end', () => {
// If haven't received a matching result yet, then return the last result
// console.log('END!');
// console.log({ final });
// console.log({ jsons });
resolve(jsons.pop());
});
res.on('close', function (err) {
// console.log('Stream has been destroyed and file has been closed');
// console.log({ final });
// console.log({ jsons });
if (err) {
reject(err);
}
resolve(final);
});
});
});
}

async function request(endpoint, userOptions, processResponse = processResponseJson) {
async function request(
endpoint,
userOptions,
processResponse = processResponseJson,
) {
const options = checkOptions(userOptions, {
path: {
type: 'array',
@@ -132,7 +140,9 @@ async function request(endpoint, userOptions, processResponse = processResponseJ
const url = `${endpoint}/${path.join('/')}${slash}${paramsStr}`;

try {
return fetch(url, request).then(response => processResponse(response, data));
return fetch(url, request).then((response) =>
processResponse(response, data),
);
} catch (err) {
throw new RequestError(url, err.message);
}
@@ -1185,12 +1195,16 @@ export default function createUtilsModule(web3, contracts, globalOptions) {
default: {},
},
});
return request(pathfinderServiceEndpoint, {
data: options.data,
method: 'POST',
path: [],
isTrailingSlash: false,
}, processResponseNdjson);
return request(
pathfinderServiceEndpoint,
{
data: options.data,
method: 'POST',
path: [],
isTrailingSlash: false,
},
processResponseNdjson,
);
},

/**
156 changes: 0 additions & 156 deletions test/iterative.test.js

This file was deleted.