Skip to content

Commit

Permalink
Implement request and response header forwarding as well as statusCod…
Browse files Browse the repository at this point in the history
…e and statusText (#4)

* correctly handle respone headers

* adjusted relay reporting

* update phttp-lib
  • Loading branch information
esterlus authored Apr 26, 2024
1 parent 1ba524c commit 93828db
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 32 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"typescript": "^5.3.3"
},
"dependencies": {
"@hoprnet/phttp-lib": "^1.1.0",
"@hoprnet/phttp-lib": "^1.2.0",
"debug": "^4.3.4",
"isomorphic-ws": "^5.0.0",
"sqlite3": "^5.1.7",
Expand Down
52 changes: 25 additions & 27 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import Version from './version';
const SocketReconnectTimeout = 1e3; // 1sek
const RequestPurgeTimeout = 60e3; // 60sek
const ValidCounterPeriod = 1e3 * 60 * 60; // 1hour
const RelayNodesCompatVersions = ['2.0.6'];
const RelayNodesCompatVersions = ['2.1'];
const SetupRelayPeriod = 1e3 * 60 * 15; // 15 min

type State = {
Expand Down Expand Up @@ -176,10 +176,12 @@ async function setupRelays(state: State, ops: Ops) {
.filter(({ status }) => status === 'Open')
.map(({ peerAddress }) => peerAddress);
const openChannels = new Set(openChannelsArr);

state.relays = relays
.filter(({ peerAddress }) => openChannels.has(peerAddress))
.map(({ peerId }) => peerId);
log.info('found %d potential relays', relays.length);

log.info('found %d potential relays', state.relays.length);
} catch (err) {
log.error('error during relay setup: %s[%o]', JSON.stringify(err), err);
} finally {
Expand Down Expand Up @@ -358,37 +360,33 @@ async function completeSegmentsEntry(
}

// do actual endpoint request
const { endpoint, body, method, headers } = reqPayload;
const params = { body, method, headers };
const fetchStartedAt = performance.now();
const resFetch = await EndpointApi.fetchUrl(endpoint, params).catch((err: Error) => {
log.error(
'error doing RPC req on %s with %o: %s[%o]',
endpoint,
params,
JSON.stringify(err),
err,
);
// HTTP critical fail response
const resp: Payload.RespPayload = {
type: Payload.RespType.Error,
reason: JSON.stringify(err),
};
return sendResponse(sendParams, resp);
});
const resFetch = await EndpointApi.fetchUrl(reqPayload.endpoint, reqPayload).catch(
(err: Error) => {
log.error(
'error doing RPC req on %s with %o: %s[%o]',
reqPayload.endpoint,
reqPayload,
JSON.stringify(err),
err,
);
// HTTP critical fail response
const resp: Payload.RespPayload = {
type: Payload.RespType.Error,
reason: JSON.stringify(err),
};
return sendResponse(sendParams, resp);
},
);
if (!resFetch) {
return;
}

const fetchDur = Math.round(performance.now() - fetchStartedAt);
// http fail response
if (Res.isErr(resFetch)) {
const resp: Payload.RespPayload = { type: Payload.RespType.Error, reason: resFetch.error };
return sendResponse(sendParams, addLatencies(reqPayload, resp, { fetchDur, recvAt }));
}

const { status, text } = resFetch.res;
const resp: Payload.RespPayload = { type: Payload.RespType.Resp, status, text };
const resp: Payload.RespPayload = {
type: Payload.RespType.Resp,
...resFetch,
};
return sendResponse(sendParams, addLatencies(reqPayload, resp, { fetchDur, recvAt }));
}

Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -355,10 +355,10 @@
"@noble/curves" "^1.3.0"
"@noble/hashes" "^1.3.3"

"@hoprnet/phttp-lib@^1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@hoprnet/phttp-lib/-/phttp-lib-1.1.0.tgz#a4762cbfa41b31421ad7e3390d3503c661d88141"
integrity sha512-TUFOb706/j7uxCgSvxL800/1v9wfSVOyHbT+o6wqzzupJmQV/hRvZ1E4grq0UUqG7U+wCD4yi5UXFGRLM06iDQ==
"@hoprnet/phttp-lib@^1.2.0":
version "1.2.0"
resolved "https://registry.yarnpkg.com/@hoprnet/phttp-lib/-/phttp-lib-1.2.0.tgz#b61fdaade83dae41c0f0334fb526114125d98763"
integrity sha512-otn8gobkQxgTNkfqMP+yHDd0CpbYxAJTXcqeYgrU8aJoqa8KSOsaWONO6siU/SZgvNCS6rn+2lH0UvF97/mmsQ==
dependencies:
"@hoprnet/phttp-crypto" "^1.0.0"
isomorphic-ws "^5.0.0"
Expand Down

0 comments on commit 93828db

Please sign in to comment.