-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a2dc92e
commit 7761607
Showing
61 changed files
with
454 additions
and
179 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
"libc", | ||
"milkomeda", | ||
"mirs", | ||
"mithril", | ||
"mydomain", | ||
"nutcoin", | ||
"nutlink", | ||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+31.3 KB
.yarn/cache/@fastify-merge-json-schemas-npm-0.1.1-12ae828277-d5b976f82e.zip
Binary file not shown.
Binary file renamed
BIN
+20.4 KB
...tgres-npm-5.2.0-b8b8a90661-3a680e1d63.zip → ...tgres-npm-5.2.2-b9d3bb4591-095e77d43f.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed
BIN
-10.4 KB
.yarn/cache/fast-content-type-parse-npm-1.0.0-08ad8e1d9f-9e9187be17.zip
Binary file not shown.
Binary file added
BIN
+11.4 KB
.yarn/cache/fast-content-type-parse-npm-1.1.0-035173e566-c8c995b60e.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+14.7 KB
.yarn/cache/json-schema-ref-resolver-npm-1.0.1-b4bc8e91c0-b4215cd58b.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file renamed
BIN
+123 KB
...nist-npm-0.39.5-cbd4ee161e-6669d687a4.zip → ...nist-npm-0.39.6-a69a970c11-10cb09aa33.zip
Binary file not shown.
Binary file not shown.
Binary file removed
BIN
-9.57 KB
.yarn/cache/pino-abstract-transport-npm-1.1.0-178ca571bf-cc84caabee.zip
Binary file not shown.
Binary file added
BIN
+14.3 KB
.yarn/cache/pino-abstract-transport-npm-1.2.0-8567d0d819-3336c51fb9.zip
Binary file not shown.
Binary file renamed
BIN
+275 KB
...pino-npm-8.15.1-d6d7d1593f-cbc6aa4e7f.zip → .../pino-npm-9.1.0-f8149231b0-6bece8583b.zip
Binary file not shown.
Binary file removed
BIN
-14.7 KB
.yarn/cache/pino-std-serializers-npm-6.0.0-c73a1b0dfd-d9dc1779b3.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file renamed
BIN
+13.8 KB
...parse-npm-2.6.0-58f1c4aba4-6a10529b09.zip → ...parse-npm-2.7.0-d5b89b0a3e-d9d7d5a01f.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains 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 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import fastifyHttpProxy from '@fastify/http-proxy'; | ||
import { FastifyInstance, FastifyReply } from 'fastify'; | ||
import { getConfig } from '../config.js'; | ||
import { pathToRegexp } from 'path-to-regexp'; | ||
import { handle404 } from '../utils/error-handler.js'; | ||
|
||
const config = getConfig(); | ||
|
||
export const matchUrlToEndpoint = (requestUrl: string, allowedEndpointPatterns: string[]) => { | ||
for (const allowedEndpointPattern of allowedEndpointPatterns) { | ||
const regexp = pathToRegexp(allowedEndpointPattern); | ||
const match = requestUrl.match(regexp); | ||
|
||
if (match) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
}; | ||
|
||
export const registerMithrilProxy = (app: FastifyInstance) => { | ||
app.register(fastifyHttpProxy, { | ||
upstream: config.mithril.aggregator, | ||
prefix: '/mithril', | ||
proxyPayloads: false, | ||
http: { | ||
requestOptions: { | ||
timeout: 30_000, | ||
}, | ||
}, | ||
preHandler: async (request, reply) => { | ||
// strip /mithril and query params | ||
const url = request.url.replace('/mithril', '').split('?')[0]; | ||
const allowedEndpoints = config.mithril.allowedEndpoints; | ||
|
||
console.log('url', url); | ||
const match = matchUrlToEndpoint(url, allowedEndpoints); | ||
|
||
console.log('match', match); | ||
|
||
if (!match) { | ||
return reply.code(400).send({ | ||
error: 'Bad Request', | ||
message: 'Invalid path. Please check https://docs.blockfrost.io/', | ||
status_code: 400, | ||
}); | ||
} | ||
}, | ||
replyOptions: { | ||
onResponse: async (_request, reply, response) => { | ||
const isErrorResponse = reply.statusCode >= 400; | ||
|
||
if (isErrorResponse) { | ||
// error response returned from the proxy can originate from: | ||
// 1) backend, 2) varnish, 3) nginx, 4) whatever | ||
// If the error is not in blockfrost format (e.g. html errors from nginx or varnish) then return generic 500 | ||
// Otherwise forward the original response | ||
// const errorBody = await convertStreamToString(response); | ||
if (reply.statusCode === 404) { | ||
return handle404(reply as FastifyReply); | ||
} else if (reply.statusCode === 412) { | ||
return reply.code(412).send({ | ||
error: 'Api Version mismatch', | ||
message: 'Invalid path. Please check https://docs.blockfrost.io/', | ||
status_code: 400, | ||
}); | ||
} else { | ||
return reply.code(500).send({ | ||
error: 'Internal Server Error', | ||
message: 'Internal Server Error', | ||
status_code: 500, | ||
}); | ||
} | ||
} | ||
|
||
return reply.send(response); | ||
}, | ||
}, | ||
}); | ||
}; |
Oops, something went wrong.