-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
3 changed files
with
174 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 ENS Labs Limited | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 ENS Labs Limited | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,132 @@ | ||
# @ensdomains/ccip-read-router | ||
|
||
A lightweight package for routing CCIP-Read requests, using [itty-router](https://itty.dev/itty-router/). | ||
|
||
## Getting started | ||
|
||
Install with: | ||
|
||
```bash | ||
bun add @ensdomains/ccip-read-router itty-router | ||
``` | ||
|
||
### Basic usage | ||
|
||
```typescript | ||
import { CcipReadRouter } from "@ensdomains/ccip-read-router"; | ||
|
||
// 1. Create a router instance | ||
const router = CcipReadRouter(); | ||
|
||
// 2. Add a route | ||
router.add({ | ||
// Function signature for the route | ||
type: "function bar(uint256) pure returns (uint256)", | ||
// Handler, with args based on function signature | ||
handle: async ([x]) => { | ||
// Return type based on function signature, or arbitrary hex (0x...) | ||
return [x * 2n]; | ||
}, | ||
}); | ||
|
||
// 3. Consume the router... | ||
``` | ||
|
||
### Customisation | ||
|
||
Most customisation is inherited from [itty-router](https://itty.dev/itty-router/). | ||
|
||
#### Base URL | ||
|
||
```typescript | ||
import { CcipReadRouter } from "@ensdomains/ccip-read-router"; | ||
|
||
const router = CcipReadRouter({ base: "/ccip-read" }); | ||
``` | ||
|
||
#### CORS | ||
|
||
From itty-router [docs](https://itty.dev/itty-router/cors#cors-itty-router): | ||
|
||
```typescript | ||
import { CcipReadRouter } from "@ensdomains/ccip-read-router"; | ||
import { cors } from "itty-router"; | ||
|
||
// get preflight and corsify pair | ||
const { preflight, corsify } = cors(); | ||
|
||
const router = CcipReadRouter({ | ||
before: [preflight], // add preflight upstream | ||
finally: [corsify], // and corsify downstream | ||
}); | ||
``` | ||
|
||
### Runtimes | ||
|
||
#### Cloudflare Workers | ||
|
||
Just export the router from your worker file. | ||
|
||
```typescript | ||
import { CcipReadRouter } from "@ensdomains/ccip-read-router"; | ||
|
||
const router = CcipReadRouter(); | ||
|
||
router.add({ | ||
type: "function bar(uint256) pure returns (uint256)", | ||
handle: async ([x]) => { | ||
return [x * 2n]; | ||
}, | ||
}); | ||
|
||
export default { ...router }; | ||
``` | ||
|
||
#### Bun | ||
|
||
Initialise the router with the port you want to use, and export. | ||
|
||
```typescript | ||
import { CcipReadRouter } from "@ensdomains/ccip-read-router"; | ||
|
||
const router = CcipReadRouter({ port: 3001 }); | ||
|
||
router.add({ | ||
type: "function bar(uint256) pure returns (uint256)", | ||
handle: async ([x]) => { | ||
return [x * 2n]; | ||
}, | ||
}); | ||
|
||
export default router; | ||
``` | ||
|
||
#### Node usage | ||
|
||
Use the [@whatwg-node/server](https://www.npmjs.com/package/@whatwg-node/server) adapter. | ||
|
||
```typescript | ||
import { CcipReadRouter } from "@ensdomains/ccip-read-router"; | ||
import { createServerAdapter } from "@whatwg-node/server"; | ||
import { createServer } from "http"; | ||
|
||
const router = CcipReadRouter(); | ||
|
||
router.add({ | ||
type: "function bar(uint256) pure returns (uint256)", | ||
handle: async ([x]) => { | ||
return [x * 2n]; | ||
}, | ||
}); | ||
|
||
// create a @whatwg-node/server | ||
const ccipReadServer = createServerAdapter(router.fetch); | ||
|
||
// then pass that to Node | ||
const httpServer = createServer(ccipReadServer); | ||
httpServer.listen(3001); | ||
``` | ||
|
||
### Other runtimes | ||
|
||
See the [runtime guides](https://itty.dev/itty-router/guides/) section of the itty-router docs. |