Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobwgillespie committed Apr 13, 2024
0 parents commit 8878219
Show file tree
Hide file tree
Showing 25 changed files with 4,725 additions and 0 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist/
25 changes: 25 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* eslint-env node */

/** @type {import('eslint').ESLint.ConfigData} */
module.exports = {
extends: ['prettier'],

// Extra rules for TypeScript files
overrides: [
{
files: ['*.ts', '*.tsx'],
extends: ['plugin:@typescript-eslint/recommended', 'prettier'],
parser: '@typescript-eslint/parser',
parserOptions: {
tsconfigRootDir: __dirname,
project: ['./tsconfig.json', './example/tsconfig.json'],
},
plugins: ['@typescript-eslint'],
rules: {
'@typescript-eslint/consistent-type-imports': 'error',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-floating-promises': 'error',
},
},
],
}
20 changes: 20 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: ci
on: push

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v3
with:
version: 8.x.x
- uses: actions/setup-node@v4
with:
node-version: '20'
cache: pnpm
- run: pnpm install --frozen-lockfile
- run: pnpm fmt:check
- run: pnpm type-check
- run: pnpm lint
- run: pnpm build
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.wrangler/
dist/
node_modules/
*.log
.dev.vars
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pnpm-lock.yaml
11 changes: 11 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"npm.packageManager": "pnpm",
"typescript.enablePromptUseWorkspaceTsdk": true,
"typescript.tsdk": "node_modules/typescript/lib",
"prettier.prettierPath": "node_modules/prettier/index.cjs",
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit"
},
"eslint.lintTask.enable": true,
"eslint.lintTask.options": "--cache --cache-location node_modules/.cache/.eslintcache ."
}
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# `@depot/connectrpc-workers`

A [Connect RPC](https://connectrpc.com/) adapter for [Cloudflare Workers](https://workers.cloudflare.com/).

## Installation

You can install the module with your favorite package manager:

```bash
pnpm add @depot/connectrpc-workers
```

This package depends on the peer dependencies `@connectrpc/connect` and `@cloudflare/workers-types`.

## Usage

You can construct a function to register handlers for your Connect services, then construct a Cloudflare Workers fetch event handler from those routes:

```typescript
import type {ConnectRouter} from '@connectrpc/connect'
import {connectWorkersAdapter} from '@depot/connectrpc-workers'

function routes(router: ConnectRouter) {
// implement rpc Say(SayRequest) returns (SayResponse)
router.rpc(ElizaService, ElizaService.methods.say, async (req) => ({
sentence: `you said: ${req.sentence}`,
}))
}

const handler = connectWorkersAdapter({routes})

export default {
fetch(request, env, context) {
return handler(request, env, context)
},
} satisfies ExportedHandler
```

## License

MIT License, see `LICENSE`.
1 change: 1 addition & 0 deletions example/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pnpm-lock.yaml
27 changes: 27 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Example

## Setup

```bash
pnpm install
```

## Run

In one terminal:

```bash
pnpm run server
```

Then in another terminal:

```bash
pnpm run client
```

Or to run against a deployed worker (to test HTTP/2):

```bash
BASE_URL=https://example.worker.workers.dev run client
```
12 changes: 12 additions & 0 deletions example/buf.gen.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: v1
plugins:
- name: es
out: src/proto
opt:
- target=ts
- import_extension=none
- name: connect-es
out: src/proto
opt:
- target=ts
- import_extension=none
3 changes: 3 additions & 0 deletions example/buf.work.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
version: v1
directories:
- proto
25 changes: 25 additions & 0 deletions example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"type": "module",
"private": true,
"scripts": {
"client": "tsx src/client.ts",
"generate": "buf generate --include-imports && prettier --write src/proto",
"server": "wrangler dev",
"type-check": "tsc --noEmit"
},
"dependencies": {
"@bufbuild/protobuf": "^1.8.0",
"@cloudflare/workers-types": "^4.20240405.0",
"@connectrpc/connect": "^1.4.0",
"@connectrpc/connect-node": "^1.4.0"
},
"devDependencies": {
"@bufbuild/protoc-gen-es": "^1.8.0",
"@connectrpc/protoc-gen-connect-es": "^1.4.0",
"@types/node": "^20.12.7",
"esbuild": "^0.20.2",
"tsx": "^4.7.2",
"typescript": "^5.4.5",
"wrangler": "^3.50.0"
}
}
Loading

0 comments on commit 8878219

Please sign in to comment.