Skip to content

Commit

Permalink
Merge pull request #5 from whats-good/kerem/standalone-server
Browse files Browse the repository at this point in the history
Kerem/standalone server
  • Loading branch information
mechanical-turk authored Nov 15, 2023
2 parents b093312 + 4ecb8a1 commit 83ca60d
Show file tree
Hide file tree
Showing 63 changed files with 2,967 additions and 322 deletions.
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
}
}
File renamed without changes.
19 changes: 0 additions & 19 deletions examples/nexus-worker/src/index.ts

This file was deleted.

Loading

0 comments on commit 83ca60d

Please sign in to comment.