|
1 |
| -import EchoServer from "aegir/echo-server"; |
2 |
| -import body from "body-parser"; |
3 |
| - |
4 |
| -/** @type {import('aegir').PartialOptions} */ |
5 |
| -const options = { |
6 |
| - test: { |
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()); |
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 |
| - // }); |
61 |
| - echo.polka.post('/add-providers/:cid', (req, res) => { |
62 |
| - callCount++ |
63 |
| - try { |
64 |
| - if (!req.headers['content-type']?.includes('application/json')) { |
65 |
| - res.statusCode = 400 |
66 |
| - res.end(JSON.stringify({ |
67 |
| - error: 'Invalid content type. Expected application/json', |
68 |
| - code: 'ERR_INVALID_INPUT' |
69 |
| - })) |
70 |
| - providers.delete(req.params.cid) |
71 |
| - return |
72 |
| - } |
73 |
| - |
74 |
| - const data = typeof req.body === 'string' |
75 |
| - ? { Providers: req.body.split('\n').map(line => JSON.parse(line)) } |
76 |
| - : req.body |
77 |
| - |
78 |
| - providers.set(req.params.cid, data) |
79 |
| - res.end(JSON.stringify({ success: true })) |
80 |
| - } catch (err) { |
81 |
| - console.error('Error in add-providers:', err) |
82 |
| - res.statusCode = 400 |
83 |
| - res.end(JSON.stringify({ |
84 |
| - error: err.message, |
85 |
| - code: 'ERR_INVALID_INPUT' |
86 |
| - })) |
87 |
| - providers.delete(req.params.cid) |
88 |
| - } |
89 |
| - }) |
90 |
| - echo.polka.get('/routing/v1/providers/:cid', (req, res) => { |
91 |
| - callCount++ |
92 |
| - try { |
93 |
| - const providerData = providers.get(req.params.cid) || { Providers: [] } |
94 |
| - const acceptHeader = req.headers.accept |
95 |
| - |
96 |
| - if (acceptHeader?.includes('application/x-ndjson')) { |
97 |
| - res.setHeader('Content-Type', 'application/x-ndjson') |
98 |
| - const providers = Array.isArray(providerData.Providers) ? providerData.Providers : [] |
99 |
| - res.end(providers.map(p => JSON.stringify(p)).join('\n')) |
100 |
| - } else { |
101 |
| - res.setHeader('Content-Type', 'application/json') |
102 |
| - res.end(JSON.stringify(providerData)) |
103 |
| - } |
104 |
| - } catch (err) { |
105 |
| - console.error('Error in get providers:', err) |
106 |
| - res.statusCode = 500 |
107 |
| - res.end(JSON.stringify({ error: err.message })) |
108 |
| - } |
109 |
| - }) |
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); |
145 |
| - |
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); |
157 |
| - |
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); |
169 |
| - |
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 |
| - }); |
182 |
| - |
183 |
| - await echo.start(); |
184 |
| - |
185 |
| - return { |
186 |
| - env: { |
187 |
| - ECHO_SERVER: `http://${echo.host}:${echo.port}`, |
188 |
| - }, |
189 |
| - echo, |
190 |
| - }; |
191 |
| - }, |
192 |
| - after: async (_, beforeResult) => { |
193 |
| - if (beforeResult.echo != null) { |
194 |
| - await beforeResult.echo.stop(); |
195 |
| - } |
196 |
| - }, |
197 |
| - }, |
198 |
| -}; |
199 |
| - |
200 |
| -export default options; |
| 1 | +import EchoServer from 'aegir/echo-server' |
| 2 | +import body from 'body-parser' |
| 3 | + |
| 4 | +/** @type {import('aegir').PartialOptions} */ |
| 5 | +const options = { |
| 6 | + test: { |
| 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()) |
| 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 | + if (!req.headers['content-type']?.includes('application/json')) { |
| 26 | + res.statusCode = 400 |
| 27 | + res.end(JSON.stringify({ |
| 28 | + error: 'Invalid content type. Expected application/json', |
| 29 | + code: 'ERR_INVALID_INPUT' |
| 30 | + })) |
| 31 | + providers.delete(req.params.cid) |
| 32 | + return |
| 33 | + } |
| 34 | + |
| 35 | + const data = typeof req.body === 'string' |
| 36 | + ? { Providers: req.body.split('\n').map(line => JSON.parse(line)) } |
| 37 | + : req.body |
| 38 | + |
| 39 | + providers.set(req.params.cid, data) |
| 40 | + res.end(JSON.stringify({ success: true })) |
| 41 | + } catch (err) { |
| 42 | + console.error('Error in add-providers:', err) |
| 43 | + res.statusCode = 400 |
| 44 | + res.end(JSON.stringify({ |
| 45 | + error: err.message, |
| 46 | + code: 'ERR_INVALID_INPUT' |
| 47 | + })) |
| 48 | + providers.delete(req.params.cid) |
| 49 | + } |
| 50 | + }) |
| 51 | + echo.polka.get('/routing/v1/providers/:cid', (req, res) => { |
| 52 | + callCount++ |
| 53 | + try { |
| 54 | + const providerData = providers.get(req.params.cid) || { Providers: [] } |
| 55 | + const acceptHeader = req.headers.accept |
| 56 | + |
| 57 | + if (acceptHeader?.includes('application/x-ndjson')) { |
| 58 | + res.setHeader('Content-Type', 'application/x-ndjson') |
| 59 | + const providers = Array.isArray(providerData.Providers) ? providerData.Providers : [] |
| 60 | + res.end(providers.map(p => JSON.stringify(p)).join('\n')) |
| 61 | + } else { |
| 62 | + res.setHeader('Content-Type', 'application/json') |
| 63 | + res.end(JSON.stringify(providerData)) |
| 64 | + } |
| 65 | + } catch (err) { |
| 66 | + console.error('Error in get providers:', err) |
| 67 | + res.statusCode = 500 |
| 68 | + res.end(JSON.stringify({ error: err.message })) |
| 69 | + } |
| 70 | + }) |
| 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) |
| 80 | + |
| 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) |
| 92 | + |
| 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) |
| 104 | + |
| 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 | + }) |
| 117 | + |
| 118 | + await echo.start() |
| 119 | + |
| 120 | + return { |
| 121 | + env: { |
| 122 | + ECHO_SERVER: `http://${echo.host}:${echo.port}` |
| 123 | + }, |
| 124 | + echo |
| 125 | + } |
| 126 | + }, |
| 127 | + after: async (_, beforeResult) => { |
| 128 | + if (beforeResult.echo != null) { |
| 129 | + await beforeResult.echo.stop() |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +export default options |
0 commit comments