@@ -19,77 +19,120 @@ const options = {
1919 next ( ) ;
2020 lastCalledUrl = req . url ;
2121 } ) ;
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"]);
2227
23- echo . polka . post ( "/add-providers/:cid" , ( req , res ) => {
24- callCount ++ ;
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 ++
2563 try {
26- console . log ( "Received POST request body:" , req . body ) ;
27- console . log ( "Content-Type:" , req . headers [ "content-type" ] ) ;
28-
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 ) ;
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
5172 }
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 } ) )
5280 } 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 ) ;
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 )
6288 }
63- } ) ;
64-
65- echo . polka . get ( "/routing/v1/providers/:cid" , ( req , res ) => {
66- callCount ++ ;
89+ } )
90+ echo . polka . get ( '/routing/v1/providers/:cid' , ( req , res ) => {
91+ callCount ++
6792 try {
68- console . log ( "GET request for CID:" , req . params . cid ) ;
69- console . log ( "Accept header:" , req . headers . accept ) ;
70-
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" ) ) ;
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' ) )
82100 } else {
83- res . setHeader ( " Content-Type" , " application/json" ) ;
84- res . end ( JSON . stringify ( providerData ) ) ;
101+ res . setHeader ( ' Content-Type' , ' application/json' )
102+ res . end ( JSON . stringify ( providerData ) )
85103 }
86104 } catch ( err ) {
87- console . error ( " Error in get providers:" , err ) ;
88- res . statusCode = 500 ;
89- res . end ( JSON . stringify ( { error : err . message } ) ) ;
105+ console . error ( ' Error in get providers:' , err )
106+ res . statusCode = 500
107+ res . end ( JSON . stringify ( { error : err . message } ) )
90108 }
91- } ) ;
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);
92115
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+ // });
93136 echo . polka . post ( "/add-peers/:peerId" , ( req , res ) => {
94137 callCount ++ ;
95138 peers . set ( req . params . peerId , req . body ) ;
0 commit comments