Skip to content

Commit 16df06c

Browse files
committed
fixes-&-improve-code
1 parent 6c6de7f commit 16df06c

File tree

4 files changed

+186
-357
lines changed

4 files changed

+186
-357
lines changed

packages/client/.aegir.js

Lines changed: 67 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,24 @@
1-
import EchoServer from "aegir/echo-server";
2-
import body from "body-parser";
1+
import EchoServer from 'aegir/echo-server'
2+
import body from 'body-parser'
33

44
/** @type {import('aegir').PartialOptions} */
55
const options = {
66
test: {
77
before: async () => {
8-
let callCount = 0;
9-
let lastCalledUrl = "";
10-
const providers = new Map();
11-
const peers = new Map();
12-
const ipnsGet = new Map();
13-
const ipnsPut = new Map();
14-
const echo = new EchoServer();
15-
echo.polka.use(body.raw({ type: "application/vnd.ipfs.ipns-record" }));
16-
echo.polka.use(body.text());
17-
echo.polka.use(body.json());
8+
let callCount = 0
9+
let lastCalledUrl = ''
10+
const providers = new Map()
11+
const peers = new Map()
12+
const ipnsGet = new Map()
13+
const ipnsPut = new Map()
14+
const echo = new EchoServer()
15+
echo.polka.use(body.raw({ type: 'application/vnd.ipfs.ipns-record'}))
16+
echo.polka.use(body.text())
17+
echo.polka.use(body.json())
1818
echo.polka.use((req, res, next) => {
19-
next();
20-
lastCalledUrl = req.url;
21-
});
22-
// echo.polka.post("/add-providers/:cid", (req, res) => {
23-
// callCount++;
24-
// try {
25-
// console.log("Received POST request body:", req.body);
26-
// console.log("Content-Type:", req.headers["content-type"]);
27-
28-
// if (req.headers["content-type"]?.includes("application/json")) {
29-
// const data =
30-
// typeof req.body === "string"
31-
// ? {
32-
// Providers: req.body
33-
// .split("\n")
34-
// .map((line) => JSON.parse(line)),
35-
// }
36-
// : req.body;
37-
// providers.set(req.params.cid, data);
38-
// res.end(JSON.stringify({ success: true }));
39-
// } else {
40-
// res.statusCode = 400;
41-
// res.end(
42-
// JSON.stringify({
43-
// error: "Invalid content type. Expected application/json",
44-
// code: "ERR_INVALID_INPUT",
45-
// })
46-
// );
47-
// providers.delete(req.params.cid);
48-
// }
49-
// } catch (err) {
50-
// console.error("Error in add-providers:", err);
51-
// res.statusCode = 400;
52-
// res.end(
53-
// JSON.stringify({
54-
// error: err.message,
55-
// code: "ERR_INVALID_INPUT",
56-
// })
57-
// );
58-
// providers.delete(req.params.cid);
59-
// }
60-
// });
19+
next()
20+
lastCalledUrl = req.url
21+
})
6122
echo.polka.post('/add-providers/:cid', (req, res) => {
6223
callCount++
6324
try {
@@ -107,94 +68,68 @@ const options = {
10768
res.end(JSON.stringify({ error: err.message }))
10869
}
10970
})
110-
// echo.polka.get("/routing/v1/providers/:cid", (req, res) => {
111-
// callCount++;
112-
// try {
113-
// console.log("GET request for CID:", req.params.cid);
114-
// console.log("Accept header:", req.headers.accept);
115-
116-
// const providerData = providers.get(req.params.cid) || {
117-
// Providers: [],
118-
// };
119-
// const acceptHeader = req.headers.accept;
120-
// if (acceptHeader?.includes("application/x-ndjson")) {
121-
// res.setHeader("Content-Type", "application/x-ndjson");
122-
// const providers = Array.isArray(providerData.Providers)
123-
// ? providerData.Providers
124-
// : [];
125-
// res.end(providers.map((p) => JSON.stringify(p)).join("\n"));
126-
// } else {
127-
// res.setHeader("Content-Type", "application/json");
128-
// res.end(JSON.stringify(providerData));
129-
// }
130-
// } catch (err) {
131-
// console.error("Error in get providers:", err);
132-
// res.statusCode = 500;
133-
// res.end(JSON.stringify({ error: err.message }));
134-
// }
135-
// });
136-
echo.polka.post("/add-peers/:peerId", (req, res) => {
137-
callCount++;
138-
peers.set(req.params.peerId, req.body);
139-
res.end();
140-
});
141-
echo.polka.get("/routing/v1/peers/:peerId", (req, res) => {
142-
callCount++;
143-
const records = peers.get(req.params.peerId) ?? "[]";
144-
peers.delete(req.params.peerId);
71+
echo.polka.post('/add-peers/:peerId', (req, res) => {
72+
callCount++
73+
peers.set(req.params.peerId, req.body)
74+
res.end()
75+
})
76+
echo.polka.get('/routing/v1/peers/:peerId', (req, res) => {
77+
callCount++
78+
const records = peers.get(req.params.peerId) ?? '[]'
79+
peers.delete(req.params.peerId)
14580

146-
res.end(records);
147-
});
148-
echo.polka.post("/add-ipns/:peerId", (req, res) => {
149-
callCount++;
150-
ipnsGet.set(req.params.peerId, req.body);
151-
res.end();
152-
});
153-
echo.polka.get("/routing/v1/ipns/:peerId", (req, res) => {
154-
callCount++;
155-
const record = ipnsGet.get(req.params.peerId) ?? "";
156-
ipnsGet.delete(req.params.peerId);
81+
res.end(records)
82+
})
83+
echo.polka.post('/add-ipns/:peerId', (req, res) => {
84+
callCount++
85+
ipnsGet.set(req.params.peerId, req.body)
86+
res.end()
87+
})
88+
echo.polka.get('/routing/v1/ipns/:peerId', (req, res) => {
89+
callCount++
90+
const record = ipnsGet.get(req.params.peerId) ?? ''
91+
ipnsGet.delete(req.params.peerId)
15792

158-
res.end(record);
159-
});
160-
echo.polka.put("/routing/v1/ipns/:peerId", (req, res) => {
161-
callCount++;
162-
ipnsPut.set(req.params.peerId, req.body);
163-
res.end();
164-
});
165-
echo.polka.get("/get-ipns/:peerId", (req, res) => {
166-
callCount++;
167-
const record = ipnsPut.get(req.params.peerId) ?? "";
168-
ipnsPut.delete(req.params.peerId);
93+
res.end(record)
94+
})
95+
echo.polka.put('/routing/v1/ipns/:peerId', (req, res) => {
96+
callCount++
97+
ipnsPut.set(req.params.peerId, req.body)
98+
res.end()
99+
})
100+
echo.polka.get('/get-ipns/:peerId', (req, res) => {
101+
callCount++
102+
const record = ipnsPut.get(req.params.peerId) ?? ''
103+
ipnsPut.delete(req.params.peerId)
169104

170-
res.end(record);
171-
});
172-
echo.polka.get("/get-call-count", (req, res) => {
173-
res.end(callCount.toString());
174-
});
175-
echo.polka.get("/reset-call-count", (req, res) => {
176-
callCount = 0;
177-
res.end();
178-
});
179-
echo.polka.get("/last-called-url", (req, res) => {
180-
res.end(lastCalledUrl);
181-
});
105+
res.end(record)
106+
})
107+
echo.polka.get('/get-call-count', (req, res) => {
108+
res.end(callCount.toString())
109+
})
110+
echo.polka.get('/reset-call-count', (req, res) => {
111+
callCount = 0
112+
res.end()
113+
})
114+
echo.polka.get('/last-called-url', (req, res) => {
115+
res.end(lastCalledUrl)
116+
})
182117

183-
await echo.start();
118+
await echo.start()
184119

185120
return {
186121
env: {
187-
ECHO_SERVER: `http://${echo.host}:${echo.port}`,
122+
ECHO_SERVER: `http://${echo.host}:${echo.port}`
188123
},
189-
echo,
190-
};
124+
echo
125+
}
191126
},
192127
after: async (_, beforeResult) => {
193128
if (beforeResult.echo != null) {
194-
await beforeResult.echo.stop();
129+
await beforeResult.echo.stop()
195130
}
196-
},
197-
},
198-
};
131+
}
132+
}
133+
}
199134

200-
export default options;
135+
export default options

packages/client/src/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV
170170
}
171171
} catch (err) {
172172
log.error('getProviders errored:', err)
173-
throw err
174173
} finally {
175174
signal.clear()
176175
onFinish.resolve()
@@ -397,6 +396,7 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV
397396
const requestMethod = options.method ?? 'GET'
398397
const key = `${requestMethod}-${url}`
399398

399+
// Only try to use cache for GET requests
400400
if (requestMethod === 'GET') {
401401
const cachedResponse = await this.cache?.match(url)
402402
if (cachedResponse?.ok === true) {

0 commit comments

Comments
 (0)