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

Kerem/standalone server #5

Merged
merged 41 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
1ce91ad
compiling the worker for typechecking purposes
mechanical-turk Nov 8, 2023
58f1e29
exporting nexus as a standalone object
mechanical-turk Nov 9, 2023
b724ad6
moved json rpc types to own file
mechanical-turk Nov 10, 2023
26d92d9
class renamed to RpcProxyContext
mechanical-turk Nov 10, 2023
b4ec1a9
fixed bad import
mechanical-turk Nov 10, 2023
fc19dd5
request -> relay
mechanical-turk Nov 10, 2023
2e9572e
using the jsonrpcrequest type
mechanical-turk Nov 10, 2023
b779bca
fetch Request type removed from internals
mechanical-turk Nov 10, 2023
381a574
changed 500 error message
mechanical-turk Nov 10, 2023
061fd47
default config param to nexus
mechanical-turk Nov 10, 2023
90512e2
application/json header added to relayed request
mechanical-turk Nov 10, 2023
031a2a5
fetch + node request handlers separated
mechanical-turk Nov 10, 2023
acfaefd
console.warn added
mechanical-turk Nov 12, 2023
e91eea9
separated node and fetch into own handlers
mechanical-turk Nov 12, 2023
9c7f27d
package.json categories -> keywords
mechanical-turk Nov 12, 2023
6fd0fe1
nexus now pulls all configs together
mechanical-turk Nov 12, 2023
d07a221
docs removed from pnpm-lock
mechanical-turk Nov 12, 2023
de58890
node and fetch handlers are exported separately
mechanical-turk Nov 12, 2023
cb02afa
moved source files into /lib folder
mechanical-turk Nov 12, 2023
9b056bf
tests fixed
mechanical-turk Nov 12, 2023
2a5199e
worker uses the fetch import now
mechanical-turk Nov 12, 2023
aa75e6b
passing nexus object for config + registries
mechanical-turk Nov 12, 2023
677e048
fixed long relative import paths
mechanical-turk Nov 12, 2023
266f628
updated location of the cloudflare example
mechanical-turk Nov 13, 2023
ee2a0a9
docs is a package now
mechanical-turk Nov 13, 2023
b48cb3c
docs updated for tsconfig and multi-entry
mechanical-turk Nov 13, 2023
11a8e01
removed monorepo dev packages from cloudflare worker example
mechanical-turk Nov 13, 2023
57a8efd
removed tsup from cf worker example
mechanical-turk Nov 13, 2023
47fa06d
docs:broken-links script added to check for broken links
mechanical-turk Nov 13, 2023
336cfe0
import uses esm modules now
mechanical-turk Nov 13, 2023
422e402
deleted unintended js build from worker
mechanical-turk Nov 13, 2023
861f716
cf worker doc uses same ts version as the rest of the monorepo
mechanical-turk Nov 13, 2023
8625031
added noEmit to cf worker build
mechanical-turk Nov 13, 2023
ebb4275
added console.info for request handler
mechanical-turk Nov 14, 2023
08abccb
using whatwg-node server adapter
mechanical-turk Nov 15, 2023
24b3350
combining request context with constructor configs
mechanical-turk Nov 15, 2023
30094ad
changesets
mechanical-turk Nov 15, 2023
9348a06
removed whatwg-node/fetch
mechanical-turk Nov 15, 2023
797691e
removed old code
mechanical-turk Nov 15, 2023
578b242
changesets
mechanical-turk Nov 15, 2023
4ecb8a1
temporarily disabling node fetch handler
mechanical-turk Nov 15, 2023
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
5 changes: 5 additions & 0 deletions .changeset/early-apricots-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@whatsgood/nexus": patch
---

multi-entrypoint builds added
5 changes: 5 additions & 0 deletions .changeset/good-bottles-joke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@whatsgood/nexus": patch
---

introduced AbstractRequestHandler for cross-platform extensions
5 changes: 5 additions & 0 deletions .changeset/green-toes-tie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@whatsgood/nexus": patch
---

tsup builds with code splitting
5 changes: 5 additions & 0 deletions .changeset/little-yaks-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@whatsgood/nexus": patch
---

Introducing esm builds
5 changes: 5 additions & 0 deletions .changeset/many-ligers-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@whatsgood/nexus": patch
---

Nexus class created as main library and config object
6 changes: 6 additions & 0 deletions .changeset/spotty-mangos-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@whatsgood/example-cloudflare-worker": patch
"@whatsgood/nexus": patch
---

whatwg-node integrated for cross-platform support
5 changes: 5 additions & 0 deletions .changeset/tender-flowers-notice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@whatsgood/nexus": patch
---

Config object can now disable providers
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ jobs:
- name: Test
run: pnpm test:unit

- name: Docs broken links
run: pnpm docs:broken-links

- name: Slack Notification
id: slack
uses: slackapi/slack-github-action@v1.24.0
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
run: pnpm build

- name: Docs
run: pnpm generate:docs --frozen
run: pnpm docs:generate --frozen

- name: Slack Notification
id: slack
Expand Down
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,22 @@ yarn add @whatsgood/nexus
```ts
// Cloudflare worker example

import { Config, RpcProxyResponseHandler } from "@whatsgood/nexus";
import { Nexus } from "@whatsgood/nexus";
import { RequestHandler } from "@whatsgood/nexus/fetch";

export default {
async fetch(
request: Request,
env: Record<string, string>
): Promise<Response> {
const config = new Config({
const nexus = new Nexus({
env,
});
const responseHandler = new RpcProxyResponseHandler({
config,
});
return responseHandler.handle(request);
const requestHandler = new RequestHandler(nexus, request);
return requestHandler.handle();
},
};

```

<!-- TODO: remove the nexus/README.md from version control, and only generate it pre npm publish -->
3 changes: 3 additions & 0 deletions docs/_snippets/warning-module-resolution.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<Warning>
`@whatsgood/nexus` makes use of [Package entry points](https://nodejs.org/api/packages.html#package-entry-points), which causes `tsc` to throw errors when compiling your project. To fix this, you need to update the `compilerOptions` section of your `tsconfig.json` to set `"moduleResolution": "NodeNext"` and `"module": "NodeNext"` . Note that `Node16` will also work, but `NodeNext` is preferred.
</Warning>
6 changes: 5 additions & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
"main": "index.js",
"private": true,
"scripts": {
"docs:dev": "mintlify dev"
"docs:dev": "mintlify dev",
"docs:broken-links": "mintlify broken-links"
},
"devDependencies": {
"mintlify": "4.0.49"
},
"keywords": [],
"author": "",
Expand Down
115 changes: 52 additions & 63 deletions docs/recipes/cloudflare-worker.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ description: "Deploy Nexus as a Cloudflare Worker in under 5 minutes"

<Tip icon="github">
If you want to skip this guide, follow [this
link](https://github.com/whats-good/nexus/tree/main/examples/nexus-worker) for
link](https://github.com/whats-good/nexus/tree/main/examples/cloudflare-worker) for
a complete codebase example.
</Tip>

Expand Down Expand Up @@ -58,7 +58,7 @@ You can now `cd` into your new project directory and open it in your favorite ed
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20230419.0",
"typescript": "^5.0.4",
"typescript": "5.2.2",
"wrangler": "^3.0.0"
}
}
Expand All @@ -68,54 +68,6 @@ You can now `cd` into your new project directory and open it in your favorite ed
name = "nexus-rpc-worker"
main = "src/index.ts"
compatibility_date = "2023-10-30"

# Variable bindings. These are arbitrary, plaintext strings (similar to environment variables)
# Note: Use secrets to store sensitive data.
# Docs: https://developers.cloudflare.com/workers/platform/environment-variables
# [vars]
# MY_VARIABLE = "production_value"

# Bind a KV Namespace. Use KV as persistent storage for small key-value pairs.
# Docs: https://developers.cloudflare.com/workers/runtime-apis/kv
# [[kv_namespaces]]
# binding = "MY_KV_NAMESPACE"
# id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

# Bind an R2 Bucket. Use R2 to store arbitrarily large blobs of data, such as files.
# Docs: https://developers.cloudflare.com/r2/api/workers/workers-api-usage/
# [[r2_buckets]]
# binding = "MY_BUCKET"
# bucket_name = "my-bucket"

# Bind a Queue producer. Use this binding to schedule an arbitrary task that may be processed later by a Queue consumer.
# Docs: https://developers.cloudflare.com/queues/get-started
# [[queues.producers]]
# binding = "MY_QUEUE"
# queue = "my-queue"

# Bind a Queue consumer. Queue Consumers can retrieve tasks scheduled by Producers to act on them.
# Docs: https://developers.cloudflare.com/queues/get-started
# [[queues.consumers]]
# queue = "my-queue"

# Bind another Worker service. Use this binding to call another Worker without network overhead.
# Docs: https://developers.cloudflare.com/workers/platform/services
# [[services]]
# binding = "MY_SERVICE"
# service = "my-service"

# Bind a Durable Object. Durable objects are a scale-to-zero compute primitive based on the actor model.
# Durable Objects can live for as long as needed. Use these when you need a long-running "server", such as in realtime apps.
# Docs: https://developers.cloudflare.com/workers/runtime-apis/durable-objects
# [[durable_objects.bindings]]
# name = "MY_DURABLE_OBJECT"
# class_name = "MyDurableObject"

# Durable Object migrations.
# Docs: https://developers.cloudflare.com/workers/learning/using-durable-objects#configure-durable-object-classes-with-migrations
# [[migrations]]
# tag = "v1"
# new_classes = ["MyDurableObject"]
```

```json tsconfig.json
Expand Down Expand Up @@ -287,36 +239,53 @@ yarn add @whatsgood/nexus

</CodeGroup>

## Update tsconfig

<Snippet file="warning-module-resolution.mdx" />

Open `tsconfig.json` and update the `compilerOptions` to include the following. Feel free to keep other options as you please, as long as they don't conflict with the ones below.

```json tsconfig.json
{
"exclude": ["node_modules"],
"include": ["./src/**/*.ts"],
"compilerOptions": {
"target": "es2021",
"lib": ["es2021"],
"types": ["@cloudflare/workers-types"],
"moduleResolution": "NodeNext",
"module": "NodeNext"
}
}
```

## Write code

Once you have installed Nexus, all you need to do is connect the `RpcProxyResponseHandler` from `@whatsgood/nexus` to your Worker's `fetch` handler.

<Note>
Note how we're passing the `env` object from the `fetch` handler to the
`RpcProxyResponseHandler`'s `Config` constructor. This is required for Nexus
to be able to resolve `Cloudflare Worker` environment variables - as
`Cloudflare` does not expose these to the `process.env` object.
Note how we pass the `env` object from the `fetch` handler to `Nexus` constructor. This is required for Nexus to be able to resolve `Cloudflare Worker` environment variables - as
`Cloudflare` does not expose these to the `process.env` object. Also note how we import the `RequestHandler` from `@whatsgood/nexus/fetch`, and `Nexus` from `@whatsgood/nexus`. This is to ensure that we are using the correct `RequestHandler` instance for the `Cloudflare Worker` environment, as `Nexus` supports multiple runtimes, which are not always compatible with each other.
</Note>

For the purposes of this tutorial, update the `"dev"` script in `package.json` from `"dev": wrangler dev` to `"dev": "wrangler dev --port 4005"`
For the purposes of this tutorial, update the `"dev"` script in `package.json` from `"dev": wrangler dev` to `"dev": "wrangler dev --port 4005"`.

<CodeGroup>

```ts src/index.ts
import { Config, RpcProxyResponseHandler } from "@whatsgood/nexus";
import { Nexus } from "@whatsgood/nexus";
import { RequestHandler } from "@whatsgood/nexus/fetch";

export default {
async fetch(
request: Request,
env: Record<string, string>
): Promise<Response> {
const config = new Config({
const nexus = new Nexus({
env,
});
const responseHandler = new RpcProxyResponseHandler({
config,
});
return responseHandler.handle(request);
const requestHandler = new RequestHandler(nexus, request);
return requestHandler.handle();
},
};
```
Expand All @@ -333,7 +302,7 @@ export default {
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20230419.0",
"typescript": "^5.0.4",
"typescript": "5.2.2",
"wrangler": "^3.0.0"
},
"dependencies": {
Expand All @@ -342,6 +311,26 @@ export default {
}
```

```json tsconfig.json
{
"exclude": ["node_modules"],
"include": ["./src/**/*.ts"],
"compilerOptions": {
"target": "es2021",
"lib": ["es2021"],
"types": ["@cloudflare/workers-types"],
"moduleResolution": "NodeNext",
"module": "NodeNext"
}
}
```

```toml wrangler.toml
name = "nexus-rpc-worker"
main = "src/index.ts"
compatibility_date = "2023-10-30"
```

</CodeGroup>

## Deploy
Expand Down Expand Up @@ -421,7 +410,7 @@ npm run dev
```

```bash pnpm
pnpm run dev
pnpm dev
```

```bash yarn
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# @whatsgood/nexus-worker
# @whatsgood/example-cloudflare-worker

## 0.1.14

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@whatsgood/nexus-worker",
"name": "@whatsgood/example-cloudflare-worker",
"version": "0.1.14",
"private": true,
"author": {
Expand All @@ -8,16 +8,14 @@
},
"scripts": {
"deploy": "wrangler deploy",
"dev": "wrangler dev --port 4005"
"dev": "wrangler dev --port 4005",
"build": "tsc"
},
"dependencies": {
"@whatsgood/nexus": "workspace:*"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20230419.0",
"eslint-config-custom": "workspace:*",
"tsconfig": "workspace:*",
"tsup": "^6.1.3",
"typescript": "5.2.2",
"wrangler": "^3.0.0"
}
Expand Down
31 changes: 31 additions & 0 deletions examples/cloudflare-worker/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Nexus } from "@whatsgood/nexus";

// TODO: add config alerts to indicate that the key access is incomplete
// TODO: add onboarding & UX. (setup admin access, login, etc)
// TODO: add tests for the worker

const server = Nexus.createServer({
providers: {
// alchemy: {
// disabled: true,
// },
base: {
disabled: true,
},
infura: {
disabled: true,
},
ankr: {
disabled: true,
},
},
});

export default {
async fetch(
request: Request,
env: Record<string, string>
): Promise<Response> {
return server.fetch(request, env);
},
};
13 changes: 13 additions & 0 deletions examples/cloudflare-worker/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"exclude": ["node_modules"],
"include": ["./src/**/*.ts"],
"compilerOptions": {
"target": "es2021",
"lib": ["es2021"],
"types": ["@cloudflare/workers-types"],
"moduleResolution": "NodeNext",
"module": "NodeNext",
"noEmit": true,
"skipLibCheck": true
}
}
19 changes: 0 additions & 19 deletions examples/nexus-worker/src/index.ts

This file was deleted.

Loading