Skip to content
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

switch rpc.ts to itty-router instead of connect cors body-parser #3467

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"@polkadot/util": "^12.6.2",
"@polkadot/wasm-crypto": "^7.3.2",
"@scure/base": "^1.1.7",
"@whatwg-node/server": "^0.9.34",
"abstract-level": "^1.0.3",
"body-parser": "^1.19.2",
"chalk": "^4.1.2",
Expand All @@ -81,6 +82,7 @@
"debug": "^4.3.3",
"ethereum-cryptography": "^2.1.3",
"it-pipe": "^1.1.0",
"itty-router": "^5.0.17",
"jayson": "^4.0.0",
"js-sdsl": "^4.4.0",
"kzg-wasm": "^0.4.0",
Expand Down
40 changes: 40 additions & 0 deletions packages/client/src/util/rpc.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { createServerAdapter } from '@whatwg-node/server'
import bodyParser from 'body-parser'
import Connect from 'connect'
import cors from 'cors'
import { createServer } from 'http'
import { AutoRouter, cors as cors2 } from 'itty-router'
import jayson from 'jayson/promise/index.js'
import { inspect } from 'util'

Expand All @@ -12,6 +14,7 @@ import type { Logger } from '../logging.js'
import type { RPCManager } from '../rpc/index.js'
import type { IncomingMessage } from 'connect'
import type { HttpServer } from 'jayson/promise'

const { json: jsonParser } = bodyParser
const { decode } = jwt

Expand Down Expand Up @@ -185,6 +188,42 @@ function checkHeaderAuth(req: any, jwtSecret: Uint8Array): void {
export function createRPCServerListener(opts: CreateRPCServerListenerOpts): HttpServer {
const { server, withEngineMiddleware, rpcCors } = opts

const { preflight, corsify } = cors2({
origin: typeof rpcCors === 'string' ? rpcCors : '*',
})
const router = AutoRouter({
before: [preflight],
finally: [corsify],
})

router.all('*', jsonParser({ limit: '11mb' }))

if (withEngineMiddleware) {
const { jwtSecret, unlessFn } = withEngineMiddleware
router.all('*', (req: any, res: any, next: any) => {
try {
if (unlessFn && unlessFn(req)) return next()
checkHeaderAuth(req, jwtSecret)
return next()
} catch (error) {
if (error instanceof Error) {
res.writeHead(401)
res.end(`Unauthorized: ${error}`)
return
}
next(error)
}
})
}

router.all('*', server.middleware())
const ittyServer = createServerAdapter(router.fetch)
const httpServer = createServer(ittyServer)
return httpServer

//Connect Way
/*

const app = Connect() as any
if (typeof rpcCors === 'string') app.use(cors({ origin: rpcCors }))
// GOSSIP_MAX_SIZE_BELLATRIX is proposed to be 10MiB
Expand All @@ -211,6 +250,7 @@ export function createRPCServerListener(opts: CreateRPCServerListenerOpts): Http
app.use(server.middleware())
const httpServer = createServer(app)
return httpServer
*/
}

export function createWsRPCServerListener(opts: CreateWSServerOpts): HttpServer | undefined {
Expand Down
Loading