Skip to content
Merged
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
42 changes: 42 additions & 0 deletions .github/workflows/cf-worker-example-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Run CF Worker Example App
on:
pull_request:
branches: [main]

jobs:
build:
runs-on:
labels: ubuntu-latest-4-core
strategy:
matrix:
node-version: [18.x]
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'yarn'
- name: Run Yarn
run: yarn --immutable
- name: Setup .dev.vars file
run: echo "DEVCYCLE_SERVER_SDK_KEY=${{ secrets.DEVCYCLE_SERVER_SDK_KEY }}" > examples/js-cloud-server/cloudflare-worker/.dev.vars
- name: Run example app in background
run: |
yarn nx serve example-js-cloud-server-sdk-cf-worker &
echo "SERVER_PID=$!" >> $GITHUB_ENV
continue-on-error: true
- name: Wait for the server to be up
run: sleep 10
- name: Test server with curl
run: |
RESPONSE=$(curl -s http://localhost:8787) # Replace with your server's port
# Check the response or do something based on the result.
if [[ "$RESPONSE" != *"DevCycle Variables:"* ]]; then
echo "Server didn't return the expected 'DevCycle Variables:' response"
exit 1
fi
- name: Cleanup server
run: kill ${{ env.SERVER_PID }}
18 changes: 18 additions & 0 deletions examples/js-cloud-server/cloudflare-worker/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extends": ["../../../.eslintrc.json"],
"ignorePatterns": ["!**/*", "node_modules/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
}
]
}
6 changes: 6 additions & 0 deletions examples/js-cloud-server/cloudflare-worker/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.env
.env.*
dist
node_modules
.dev.vars
.wrangler
21 changes: 21 additions & 0 deletions examples/js-cloud-server/cloudflare-worker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# DevCycle JS Cloud Bucketing Server SDK - Cloudflare Worker Example App
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whats the purpose of this example app? is it to provide an example for the public looking to use devcycle in a cloudflare worker? or is it a development aid for trying out the SDK the worker?

if its the former, I don't think it should be in the monorepo

if its the latter, it's not gonna be locally linked to the source files of the SDK like the other examples are

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As with all the example apps in this repo, its a bit of both.

Why do you say its not going to be linked?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually I guess it will be if webpack is able to resolve all of the packages through the tsconfig aliases

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea like it works as is right now, and the npm package isn't published yet.


Example app for using the DevCycle JS Cloud Bucketing Server SDK with Cloudflare Workers.

## Serve Worker Locally

1. Run `yarn` in the root of this repo to install all dependencies.
2. Set up a `.dev.vars` file in this folder containing your DevCycle Server SDK Key for your project:
- ```DEVCYCLE_SERVER_SDK_KEY=<YOUR SERVER SDK KEY>```
3. Run this worker locally using: `yarn nx serve example-js-cloud-server-sdk-cf-worker`

## Deploy Worker to Cloudflare

1. Run `yarn` in the root of this repo to install all dependencies.
2. Set a `DEVCYCLE_SERVER_SDK_KEY` secret in Cloudflare using the wrangler CLI from the root:
- ```yarn wrangler secret put DEVCYCLE_SERVER_SDK_KEY -c examples/js-cloud-server/cloudflare-worker/wrangler.toml```
- you may be prompted to login to Wrangler with your Cloudflare account
3. Set your Cloudflare account ID in `wrangler.toml`:
- you can find this under your account's profile on the Cloudflare dashboard
4. Deploy this worker to Cloudflare using: `yarn nx deploy example-js-cloud-server-sdk-cf-worker`

12 changes: 12 additions & 0 deletions examples/js-cloud-server/cloudflare-worker/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "js-cloud-server-sdk-test-worker",
"version": "1.0.0",
"private": true,
"scripts": {
"deploy": "wrangler deploy",
"start": "wrangler dev"
},
"dependencies": {
"@devcycle/js-cloud-server-sdk": "1.0.0"
}
}
44 changes: 44 additions & 0 deletions examples/js-cloud-server/cloudflare-worker/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "example-js-cloud-server-sdk-cf-worker",
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "examples/js-cloud-server/cloudflare-worker/src",
"projectType": "application",
"targets": {
"build": {
"executor": "@nx/webpack:webpack",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/examples/js-cloud-server/cloudflare-worker",
"main": "examples/js-cloud-server/cloudflare-worker/src/index.ts",
"tsConfig": "examples/js-cloud-server/cloudflare-worker/tsconfig.json",
"compiler": "tsc",
"target": "node"
}
},
"serve": {
"executor": "nx:run-commands",
"options": {
"command": "yarn run -T wrangler dev",
"cwd": "examples/js-cloud-server/cloudflare-worker"
}
},
"deploy": {
"executor": "nx:run-commands",
"options": {
"command": "yarn run -T wrangler deploy",
"cwd": "examples/js-cloud-server/cloudflare-worker"
},
"dependsOn": ["upload-sourcemaps"]
},
"lint": {
"executor": "@nx/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": [
"examples/js-cloud-server/cloudflare-worker/**/*.ts"
]
}
}
},
"tags": []
}
23 changes: 23 additions & 0 deletions examples/js-cloud-server/cloudflare-worker/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { initializeDevCycle } from '@devcycle/js-cloud-server-sdk'

export interface Env {
DEVCYCLE_SERVER_SDK_KEY: string
}

export default {
async fetch(
request: Request,
env: Env,
ctx: ExecutionContext,
): Promise<Response> {
const devcycleClient = initializeDevCycle(env.DEVCYCLE_SERVER_SDK_KEY)

const variables = await devcycleClient.allVariables({
user_id: 'test-user',
})

return new Response(
'DevCycle Variables: ' + JSON.stringify(variables, null, 2),
)
},
}
29 changes: 29 additions & 0 deletions examples/js-cloud-server/cloudflare-worker/tsconfig.app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../../dist/out-tsc",

"target": "es2021" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
"lib": [
"es2021"
] /* Specify a set of bundled library declaration files that describe the target runtime environment. */,
"jsx": "react" /* Specify what JSX code is generated. */,
"module": "es2022" /* Specify what module code is generated. */,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note that this is the first time we're compiling to es modules in this repo. May be relevant later

"moduleResolution": "node" /* Specify how TypeScript looks up a file from a given module specifier. */,
"types": [
"@cloudflare/workers-types"
] /* Specify type package names to be included without being referenced in a source file. */,
"resolveJsonModule": true /* Enable importing .json files */,
"allowJs": true /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */,
"checkJs": false /* Enable error reporting in type-checked JavaScript files. */,
"noEmit": true /* Disable emitting files from a compilation. */,
"isolatedModules": true /* Ensure that each file can be safely transpiled without relying on other imports. */,
"allowSyntheticDefaultImports": true /* Allow 'import x from y' when a module doesn't have a default export. */,
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,

"strict": true /* Enable all strict type-checking options. */,
"skipLibCheck": true /* Skip type checking all .d.ts files. */
},
"exclude": ["**/*.spec.ts", "**/*.test.ts"],
"include": ["**/*.ts"]
}
13 changes: 13 additions & 0 deletions examples/js-cloud-server/cloudflare-worker/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "../../../tsconfig.base.json",
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.app.json"
},
{
"path": "./tsconfig.spec.json"
}
]
}
4 changes: 4 additions & 0 deletions examples/js-cloud-server/cloudflare-worker/wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name = "js-cloud-server-sdk-test-worker"
main = "src/index.ts"
compatibility_date = "2023-09-18"
account_id = "<Your Cloudflare Account ID>"
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@
"validate-npm-package-name": "^5.0.0",
"verdaccio": "^5.15.4",
"webpack": "^5.75.0",
"webpack-merge": "^5.8.0"
"webpack-merge": "^5.8.0",
"wrangler": "^3.0.0"
},
"lint-staged": {
"**/*.{ts,js}": [
Expand Down
Loading