-
Notifications
You must be signed in to change notification settings - Fork 7
fix: getProviders recognizes application/json accept header #155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
29ccab6
modified-contentType&-added-testcases
Nkovaturient 5d10d98
fix: explicit Content-Type null check
Nkovaturient 6c6de7f
fix-eslint-issues
Nkovaturient 16df06c
fixes-&-improve-code
Nkovaturient f9bce42
fix: cache ok responses
Nkovaturient ebb9997
chore: lint fix
SgtPooki b88cd64
chore: reduce number of changes
SgtPooki bf2fa2d
chore: revert response cache change
SgtPooki e2860db
chore: remove unused package
SgtPooki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,200 @@ | ||
import EchoServer from 'aegir/echo-server' | ||
import body from 'body-parser' | ||
import EchoServer from "aegir/echo-server"; | ||
import body from "body-parser"; | ||
|
||
/** @type {import('aegir').PartialOptions} */ | ||
const options = { | ||
test: { | ||
before: async () => { | ||
let callCount = 0 | ||
let lastCalledUrl = '' | ||
const providers = new Map() | ||
const peers = new Map() | ||
const ipnsGet = new Map() | ||
const ipnsPut = new Map() | ||
const echo = new EchoServer() | ||
echo.polka.use(body.raw({ type: 'application/vnd.ipfs.ipns-record'})) | ||
echo.polka.use(body.text()) | ||
let callCount = 0; | ||
let lastCalledUrl = ""; | ||
const providers = new Map(); | ||
const peers = new Map(); | ||
const ipnsGet = new Map(); | ||
const ipnsPut = new Map(); | ||
const echo = new EchoServer(); | ||
echo.polka.use(body.raw({ type: "application/vnd.ipfs.ipns-record" })); | ||
echo.polka.use(body.text()); | ||
echo.polka.use(body.json()); | ||
echo.polka.use((req, res, next) => { | ||
next() | ||
lastCalledUrl = req.url | ||
}) | ||
next(); | ||
lastCalledUrl = req.url; | ||
}); | ||
// echo.polka.post("/add-providers/:cid", (req, res) => { | ||
|
||
// callCount++; | ||
// try { | ||
// console.log("Received POST request body:", req.body); | ||
// console.log("Content-Type:", req.headers["content-type"]); | ||
|
||
// if (req.headers["content-type"]?.includes("application/json")) { | ||
// const data = | ||
// typeof req.body === "string" | ||
// ? { | ||
// Providers: req.body | ||
// .split("\n") | ||
// .map((line) => JSON.parse(line)), | ||
// } | ||
// : req.body; | ||
// providers.set(req.params.cid, data); | ||
// res.end(JSON.stringify({ success: true })); | ||
// } else { | ||
// res.statusCode = 400; | ||
// res.end( | ||
// JSON.stringify({ | ||
// error: "Invalid content type. Expected application/json", | ||
// code: "ERR_INVALID_INPUT", | ||
// }) | ||
// ); | ||
// providers.delete(req.params.cid); | ||
// } | ||
// } catch (err) { | ||
// console.error("Error in add-providers:", err); | ||
// res.statusCode = 400; | ||
// res.end( | ||
// JSON.stringify({ | ||
// error: err.message, | ||
// code: "ERR_INVALID_INPUT", | ||
// }) | ||
// ); | ||
// providers.delete(req.params.cid); | ||
// } | ||
// }); | ||
echo.polka.post('/add-providers/:cid', (req, res) => { | ||
callCount++ | ||
providers.set(req.params.cid, req.body) | ||
res.end() | ||
try { | ||
if (!req.headers['content-type']?.includes('application/json')) { | ||
res.statusCode = 400 | ||
res.end(JSON.stringify({ | ||
error: 'Invalid content type. Expected application/json', | ||
code: 'ERR_INVALID_INPUT' | ||
})) | ||
providers.delete(req.params.cid) | ||
return | ||
} | ||
|
||
const data = typeof req.body === 'string' | ||
? { Providers: req.body.split('\n').map(line => JSON.parse(line)) } | ||
: req.body | ||
|
||
providers.set(req.params.cid, data) | ||
res.end(JSON.stringify({ success: true })) | ||
} catch (err) { | ||
console.error('Error in add-providers:', err) | ||
res.statusCode = 400 | ||
res.end(JSON.stringify({ | ||
error: err.message, | ||
code: 'ERR_INVALID_INPUT' | ||
})) | ||
providers.delete(req.params.cid) | ||
} | ||
}) | ||
echo.polka.get('/routing/v1/providers/:cid', (req, res) => { | ||
callCount++ | ||
const records = providers.get(req.params.cid) ?? '[]' | ||
providers.delete(req.params.cid) | ||
res.end(records) | ||
try { | ||
const providerData = providers.get(req.params.cid) || { Providers: [] } | ||
const acceptHeader = req.headers.accept | ||
|
||
if (acceptHeader?.includes('application/x-ndjson')) { | ||
res.setHeader('Content-Type', 'application/x-ndjson') | ||
const providers = Array.isArray(providerData.Providers) ? providerData.Providers : [] | ||
res.end(providers.map(p => JSON.stringify(p)).join('\n')) | ||
} else { | ||
res.setHeader('Content-Type', 'application/json') | ||
res.end(JSON.stringify(providerData)) | ||
} | ||
} catch (err) { | ||
console.error('Error in get providers:', err) | ||
res.statusCode = 500 | ||
res.end(JSON.stringify({ error: err.message })) | ||
} | ||
}) | ||
echo.polka.post('/add-peers/:peerId', (req, res) => { | ||
callCount++ | ||
peers.set(req.params.peerId, req.body) | ||
res.end() | ||
}) | ||
echo.polka.get('/routing/v1/peers/:peerId', (req, res) => { | ||
callCount++ | ||
const records = peers.get(req.params.peerId) ?? '[]' | ||
peers.delete(req.params.peerId) | ||
// echo.polka.get("/routing/v1/providers/:cid", (req, res) => { | ||
// callCount++; | ||
// try { | ||
// console.log("GET request for CID:", req.params.cid); | ||
// console.log("Accept header:", req.headers.accept); | ||
|
||
res.end(records) | ||
}) | ||
echo.polka.post('/add-ipns/:peerId', (req, res) => { | ||
callCount++ | ||
ipnsGet.set(req.params.peerId, req.body) | ||
res.end() | ||
}) | ||
echo.polka.get('/routing/v1/ipns/:peerId', (req, res) => { | ||
callCount++ | ||
const record = ipnsGet.get(req.params.peerId) ?? '' | ||
ipnsGet.delete(req.params.peerId) | ||
// const providerData = providers.get(req.params.cid) || { | ||
// Providers: [], | ||
// }; | ||
// const acceptHeader = req.headers.accept; | ||
// if (acceptHeader?.includes("application/x-ndjson")) { | ||
// res.setHeader("Content-Type", "application/x-ndjson"); | ||
// const providers = Array.isArray(providerData.Providers) | ||
// ? providerData.Providers | ||
// : []; | ||
// res.end(providers.map((p) => JSON.stringify(p)).join("\n")); | ||
// } else { | ||
// res.setHeader("Content-Type", "application/json"); | ||
// res.end(JSON.stringify(providerData)); | ||
// } | ||
// } catch (err) { | ||
// console.error("Error in get providers:", err); | ||
// res.statusCode = 500; | ||
// res.end(JSON.stringify({ error: err.message })); | ||
// } | ||
// }); | ||
echo.polka.post("/add-peers/:peerId", (req, res) => { | ||
callCount++; | ||
peers.set(req.params.peerId, req.body); | ||
res.end(); | ||
}); | ||
echo.polka.get("/routing/v1/peers/:peerId", (req, res) => { | ||
callCount++; | ||
const records = peers.get(req.params.peerId) ?? "[]"; | ||
peers.delete(req.params.peerId); | ||
|
||
res.end(record) | ||
}) | ||
echo.polka.put('/routing/v1/ipns/:peerId', (req, res) => { | ||
callCount++ | ||
ipnsPut.set(req.params.peerId, req.body) | ||
res.end() | ||
}) | ||
echo.polka.get('/get-ipns/:peerId', (req, res) => { | ||
callCount++ | ||
const record = ipnsPut.get(req.params.peerId) ?? '' | ||
ipnsPut.delete(req.params.peerId) | ||
res.end(records); | ||
}); | ||
echo.polka.post("/add-ipns/:peerId", (req, res) => { | ||
callCount++; | ||
ipnsGet.set(req.params.peerId, req.body); | ||
res.end(); | ||
}); | ||
echo.polka.get("/routing/v1/ipns/:peerId", (req, res) => { | ||
callCount++; | ||
const record = ipnsGet.get(req.params.peerId) ?? ""; | ||
ipnsGet.delete(req.params.peerId); | ||
|
||
res.end(record) | ||
}) | ||
echo.polka.get('/get-call-count', (req, res) => { | ||
res.end(callCount.toString()) | ||
}) | ||
echo.polka.get('/reset-call-count', (req, res) => { | ||
callCount = 0 | ||
res.end() | ||
}) | ||
echo.polka.get('/last-called-url', (req, res) => { | ||
res.end(lastCalledUrl) | ||
}) | ||
res.end(record); | ||
}); | ||
echo.polka.put("/routing/v1/ipns/:peerId", (req, res) => { | ||
callCount++; | ||
ipnsPut.set(req.params.peerId, req.body); | ||
res.end(); | ||
}); | ||
echo.polka.get("/get-ipns/:peerId", (req, res) => { | ||
callCount++; | ||
const record = ipnsPut.get(req.params.peerId) ?? ""; | ||
ipnsPut.delete(req.params.peerId); | ||
|
||
res.end(record); | ||
}); | ||
echo.polka.get("/get-call-count", (req, res) => { | ||
res.end(callCount.toString()); | ||
}); | ||
echo.polka.get("/reset-call-count", (req, res) => { | ||
callCount = 0; | ||
res.end(); | ||
}); | ||
echo.polka.get("/last-called-url", (req, res) => { | ||
res.end(lastCalledUrl); | ||
}); | ||
|
||
await echo.start() | ||
await echo.start(); | ||
|
||
return { | ||
env: { | ||
ECHO_SERVER: `http://${echo.host}:${echo.port}` | ||
ECHO_SERVER: `http://${echo.host}:${echo.port}`, | ||
}, | ||
echo | ||
} | ||
echo, | ||
}; | ||
}, | ||
after: async (_, beforeResult) => { | ||
if (beforeResult.echo != null) { | ||
await beforeResult.echo.stop() | ||
await beforeResult.echo.stop(); | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
}, | ||
}; | ||
|
||
export default options | ||
export default options; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need to remove stylistic changes here and in the rest of the PR.
You might need to
git checkout main -- <pathToFile>
and then discard everything except the changes that are needed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay! lemme try it out👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this file still has a lot of single quotes changed to double quote, and semicolons added...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i cannot understand, despite disabling autosave, eslint, why extra spaces, semicolons and double quotes are added. can you please guide me how to configure my vs-setup--I have downloaded this vsc extension--https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint--and then ran
npm run lint -- --fix
, even did git checkout main --, and tried minimal code changes, yetThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so, from the root of the project, because this is a monorepo, you actually have to run
npm run lint -- -- --fix