|
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' |
3 | 3 |
|
4 | 4 | /** @type {import('aegir').PartialOptions} */ |
5 | 5 | const options = { |
6 | 6 | test: { |
7 | 7 | 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()) |
18 | 18 | 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 | + }) |
61 | 22 | echo.polka.post('/add-providers/:cid', (req, res) => { |
62 | 23 | callCount++ |
63 | 24 | try { |
@@ -107,94 +68,68 @@ const options = { |
107 | 68 | res.end(JSON.stringify({ error: err.message })) |
108 | 69 | } |
109 | 70 | }) |
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) |
145 | 80 |
|
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) |
157 | 92 |
|
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) |
169 | 104 |
|
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 | + }) |
182 | 117 |
|
183 | | - await echo.start(); |
| 118 | + await echo.start() |
184 | 119 |
|
185 | 120 | return { |
186 | 121 | env: { |
187 | | - ECHO_SERVER: `http://${echo.host}:${echo.port}`, |
| 122 | + ECHO_SERVER: `http://${echo.host}:${echo.port}` |
188 | 123 | }, |
189 | | - echo, |
190 | | - }; |
| 124 | + echo |
| 125 | + } |
191 | 126 | }, |
192 | 127 | after: async (_, beforeResult) => { |
193 | 128 | if (beforeResult.echo != null) { |
194 | | - await beforeResult.echo.stop(); |
| 129 | + await beforeResult.echo.stop() |
195 | 130 | } |
196 | | - }, |
197 | | - }, |
198 | | -}; |
| 131 | + } |
| 132 | + } |
| 133 | +} |
199 | 134 |
|
200 | | -export default options; |
| 135 | +export default options |
0 commit comments