|
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()) |
| 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()); |
17 | 18 | echo.polka.use((req, res, next) => {
|
18 |
| - next() |
19 |
| - lastCalledUrl = req.url |
20 |
| - }) |
21 |
| - echo.polka.post('/add-providers/:cid', (req, res) => { |
22 |
| - callCount++ |
23 |
| - providers.set(req.params.cid, req.body) |
24 |
| - res.end() |
25 |
| - }) |
26 |
| - echo.polka.get('/routing/v1/providers/:cid', (req, res) => { |
27 |
| - callCount++ |
28 |
| - const records = providers.get(req.params.cid) ?? '[]' |
29 |
| - providers.delete(req.params.cid) |
30 |
| - res.end(records) |
31 |
| - }) |
32 |
| - echo.polka.post('/add-peers/:peerId', (req, res) => { |
33 |
| - callCount++ |
34 |
| - peers.set(req.params.peerId, req.body) |
35 |
| - res.end() |
36 |
| - }) |
37 |
| - echo.polka.get('/routing/v1/peers/:peerId', (req, res) => { |
38 |
| - callCount++ |
39 |
| - const records = peers.get(req.params.peerId) ?? '[]' |
40 |
| - peers.delete(req.params.peerId) |
| 19 | + next(); |
| 20 | + lastCalledUrl = req.url; |
| 21 | + }); |
41 | 22 |
|
42 |
| - res.end(records) |
43 |
| - }) |
44 |
| - echo.polka.post('/add-ipns/:peerId', (req, res) => { |
45 |
| - callCount++ |
46 |
| - ipnsGet.set(req.params.peerId, req.body) |
47 |
| - res.end() |
48 |
| - }) |
49 |
| - echo.polka.get('/routing/v1/ipns/:peerId', (req, res) => { |
50 |
| - callCount++ |
51 |
| - const record = ipnsGet.get(req.params.peerId) ?? '' |
52 |
| - ipnsGet.delete(req.params.peerId) |
| 23 | + echo.polka.post("/add-providers/:cid", (req, res) => { |
| 24 | + callCount++; |
| 25 | + try { |
| 26 | + console.log("Received POST request body:", req.body); |
| 27 | + console.log("Content-Type:", req.headers["content-type"]); |
53 | 28 |
|
54 |
| - res.end(record) |
55 |
| - }) |
56 |
| - echo.polka.put('/routing/v1/ipns/:peerId', (req, res) => { |
57 |
| - callCount++ |
58 |
| - ipnsPut.set(req.params.peerId, req.body) |
59 |
| - res.end() |
60 |
| - }) |
61 |
| - echo.polka.get('/get-ipns/:peerId', (req, res) => { |
62 |
| - callCount++ |
63 |
| - const record = ipnsPut.get(req.params.peerId) ?? '' |
64 |
| - ipnsPut.delete(req.params.peerId) |
| 29 | + // Only process if content-type is application/json |
| 30 | + if (req.headers["content-type"]?.includes("application/json")) { |
| 31 | + const data = |
| 32 | + typeof req.body === "string" |
| 33 | + ? { |
| 34 | + Providers: req.body |
| 35 | + .split("\n") |
| 36 | + .map((line) => JSON.parse(line)), |
| 37 | + } |
| 38 | + : req.body; |
| 39 | + providers.set(req.params.cid, data); |
| 40 | + res.end(JSON.stringify({ success: true })); |
| 41 | + } else { |
| 42 | + res.statusCode = 400; |
| 43 | + res.end( |
| 44 | + JSON.stringify({ |
| 45 | + error: "Invalid content type. Expected application/json", |
| 46 | + code: "ERR_INVALID_INPUT", |
| 47 | + }) |
| 48 | + ); |
| 49 | + // Clear any existing providers for this CID |
| 50 | + providers.delete(req.params.cid); |
| 51 | + } |
| 52 | + } catch (err) { |
| 53 | + console.error("Error in add-providers:", err); |
| 54 | + res.statusCode = 400; |
| 55 | + res.end( |
| 56 | + JSON.stringify({ |
| 57 | + error: err.message, |
| 58 | + code: "ERR_INVALID_INPUT", |
| 59 | + }) |
| 60 | + ); |
| 61 | + providers.delete(req.params.cid); |
| 62 | + } |
| 63 | + }); |
65 | 64 |
|
66 |
| - res.end(record) |
67 |
| - }) |
68 |
| - echo.polka.get('/get-call-count', (req, res) => { |
69 |
| - res.end(callCount.toString()) |
70 |
| - }) |
71 |
| - echo.polka.get('/reset-call-count', (req, res) => { |
72 |
| - callCount = 0 |
73 |
| - res.end() |
74 |
| - }) |
75 |
| - echo.polka.get('/last-called-url', (req, res) => { |
76 |
| - res.end(lastCalledUrl) |
77 |
| - }) |
| 65 | + echo.polka.get("/routing/v1/providers/:cid", (req, res) => { |
| 66 | + callCount++; |
| 67 | + try { |
| 68 | + console.log("GET request for CID:", req.params.cid); |
| 69 | + console.log("Accept header:", req.headers.accept); |
78 | 70 |
|
79 |
| - await echo.start() |
| 71 | + const providerData = providers.get(req.params.cid) || { |
| 72 | + Providers: [], |
| 73 | + }; |
| 74 | + |
| 75 | + const acceptHeader = req.headers.accept; |
| 76 | + if (acceptHeader?.includes("application/x-ndjson")) { |
| 77 | + res.setHeader("Content-Type", "application/x-ndjson"); |
| 78 | + const providers = Array.isArray(providerData.Providers) |
| 79 | + ? providerData.Providers |
| 80 | + : []; |
| 81 | + res.end(providers.map((p) => JSON.stringify(p)).join("\n")); |
| 82 | + } else { |
| 83 | + res.setHeader("Content-Type", "application/json"); |
| 84 | + res.end(JSON.stringify(providerData)); |
| 85 | + } |
| 86 | + } catch (err) { |
| 87 | + console.error("Error in get providers:", err); |
| 88 | + res.statusCode = 500; |
| 89 | + res.end(JSON.stringify({ error: err.message })); |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + echo.polka.post("/add-peers/:peerId", (req, res) => { |
| 94 | + callCount++; |
| 95 | + peers.set(req.params.peerId, req.body); |
| 96 | + res.end(); |
| 97 | + }); |
| 98 | + echo.polka.get("/routing/v1/peers/:peerId", (req, res) => { |
| 99 | + callCount++; |
| 100 | + const records = peers.get(req.params.peerId) ?? "[]"; |
| 101 | + peers.delete(req.params.peerId); |
| 102 | + |
| 103 | + res.end(records); |
| 104 | + }); |
| 105 | + echo.polka.post("/add-ipns/:peerId", (req, res) => { |
| 106 | + callCount++; |
| 107 | + ipnsGet.set(req.params.peerId, req.body); |
| 108 | + res.end(); |
| 109 | + }); |
| 110 | + echo.polka.get("/routing/v1/ipns/:peerId", (req, res) => { |
| 111 | + callCount++; |
| 112 | + const record = ipnsGet.get(req.params.peerId) ?? ""; |
| 113 | + ipnsGet.delete(req.params.peerId); |
| 114 | + |
| 115 | + res.end(record); |
| 116 | + }); |
| 117 | + echo.polka.put("/routing/v1/ipns/:peerId", (req, res) => { |
| 118 | + callCount++; |
| 119 | + ipnsPut.set(req.params.peerId, req.body); |
| 120 | + res.end(); |
| 121 | + }); |
| 122 | + echo.polka.get("/get-ipns/:peerId", (req, res) => { |
| 123 | + callCount++; |
| 124 | + const record = ipnsPut.get(req.params.peerId) ?? ""; |
| 125 | + ipnsPut.delete(req.params.peerId); |
| 126 | + |
| 127 | + res.end(record); |
| 128 | + }); |
| 129 | + echo.polka.get("/get-call-count", (req, res) => { |
| 130 | + res.end(callCount.toString()); |
| 131 | + }); |
| 132 | + echo.polka.get("/reset-call-count", (req, res) => { |
| 133 | + callCount = 0; |
| 134 | + res.end(); |
| 135 | + }); |
| 136 | + echo.polka.get("/last-called-url", (req, res) => { |
| 137 | + res.end(lastCalledUrl); |
| 138 | + }); |
| 139 | + |
| 140 | + await echo.start(); |
80 | 141 |
|
81 | 142 | return {
|
82 | 143 | env: {
|
83 |
| - ECHO_SERVER: `http://${echo.host}:${echo.port}` |
| 144 | + ECHO_SERVER: `http://${echo.host}:${echo.port}`, |
84 | 145 | },
|
85 |
| - echo |
86 |
| - } |
| 146 | + echo, |
| 147 | + }; |
87 | 148 | },
|
88 | 149 | after: async (_, beforeResult) => {
|
89 | 150 | if (beforeResult.echo != null) {
|
90 |
| - await beforeResult.echo.stop() |
| 151 | + await beforeResult.echo.stop(); |
91 | 152 | }
|
92 |
| - } |
93 |
| - } |
94 |
| -} |
| 153 | + }, |
| 154 | + }, |
| 155 | +}; |
95 | 156 |
|
96 |
| -export default options |
| 157 | +export default options; |
0 commit comments