Skip to content
Permalink
Browse files
added Deno
  • Loading branch information
Touchless Inc committed Aug 13, 2022
1 parent 576ace0 commit d69af47b9892ec9bf6584bb723838bdb200cce77
Show file tree
Hide file tree
Showing 12 changed files with 203 additions and 0 deletions.
@@ -0,0 +1,3 @@
{
"deno.enable": true
}
@@ -0,0 +1,50 @@
{
"$schema": "https://raw.githubusercontent.com/skymethod/denoflare/v0.5.6/common/config.schema.json",
"scripts": {
"ab": {
"path": "ab/index.ts",
"localPort": 3030
},
"layer": {
"path": "layer/index.ts",
"localPort": 3030
},
"script1-local": {
"path": "/Users/me/path/to/script1.js",
"bindings": {
"foo": { "value": "bar" }
},
"localPort": 3002
},
"script2-local": {
"path": "/Users/me/path/to/script2.ts",
"bindings": {
"memoryNamespace": { "doNamespace": "local:MyDO" }
},
"localPort": 8080
},
"script3-local": {
"path": "/Users/me/path/to/script3.ts",
"bindings": {
"MyKV": { "kvNamespace": "123bddca456789b0a12c345b6gus78d9" },
"LocalHostAndPort": { "value": "localhost:${localPort}" }
},
"localPort": 3031,
"localHostname": "test.example.com",
"localIsolation": "isolate"
},
"script4-prod": {
"path": "/Users/me/path/to/script4.ts",
"bindings": {
"version": { "value": "1.2.3" }
},
"customDomains": [ "production.example.com" ]
},
"script4-local": {
"path": "/Users/me/path/to/script4.ts",
"bindings": {
"version": { "value": "local" }
}
}
}
}
@@ -0,0 +1,2 @@
CF_ACCOUNT_ID=[account ID here]
CF_API_TOKEN=[secret token here]
@@ -0,0 +1 @@
.env
@@ -0,0 +1,57 @@
### To Install Deno on Mac
Your export paths may be different than below. They are shown after the installation is finished.
```
curl -fsSL https://deno.land/x/install/install.sh | sh
export DENO_INSTALL="/Users/home/.deno"
export PATH="$DENO_INSTALL/bin:$PATH"
```

### To Create A New Package
```
npx nx g npm-package [name]
```

### To Install Denoflare
```
deno install --unstable --allow-read --allow-net --allow-env --allow-run --name denoflare --force \
https://raw.githubusercontent.com/skymethod/denoflare/v0.5.6/cli/cli.ts
```

### To Run Locally:
```
denoflare serve ab
```

### To Deploy:
```
denoflare push ab
```

### To Tail Live Logs
```
denoflare tail ab
```

### Denoflare Commands
[Denoflare.dev](https://denoflare.dev/cli/)

```
USAGE:
denoflare <command> <args> <options>
COMMANDS:
serve Run a Cloudflare worker script in a local Deno web server
push Upload a Cloudflare worker script to Cloudflare
tail View a real-time stream of logs from a published Cloudflare worker
site Develop and deploy a static docs site to Cloudflare Pages
analytics Dump stats via the Cloudflare GraphQL Analytics API
cfapi Call the Cloudflare REST API
r2 Manage Cloudflare R2 storage using the S3 compatibility API
pubsub Interact with your Cloudflare Pub/Sub broker
d1 Manage and query your Cloudflare D1 databases
version Dump cli version
For command-specific help: denoflare <command> --help
```

@@ -0,0 +1 @@
export type { IncomingRequestCf } from "https://raw.githubusercontent.com/skymethod/denoflare/v0.3.0/common/cloudflare_workers_types.d.ts";
@@ -0,0 +1,33 @@
console.log('Hello World');

import { IncomingRequestCf } from "./deps.ts";

/**
* Based on the A/B Testing Cloudflare Worker example.
* Ref: https://developers.cloudflare.com/workers/examples/ab-testing
*/
function fetch(request: IncomingRequestCf): Response {
const NAME = "experiment-0";

const TEST_RESPONSE = new Response("Test group");
const CONTROL_RESPONSE = new Response("Control group");

// Determine which group this requester is in.
const cookie = request.headers.get("cookie");
if (cookie && cookie.includes(`${NAME}=control`)) {
return CONTROL_RESPONSE;
} else if (cookie && cookie.includes(`${NAME}=test`)) {
return TEST_RESPONSE;
} else {
// If there is no cookie, this is a new client. Choose a group and set the cookie.
const group = Math.random() < 0.5 ? "test" : "control"; // 50/50 split
const response = group === "control" ? CONTROL_RESPONSE : TEST_RESPONSE;
response.headers.append("Set-Cookie", `${NAME}=${group}; path=/`);

return response;
}
}

export default {
fetch,
};
@@ -0,0 +1,8 @@
{
"name": "@exo/denoflare-ab",
"version": "0.0.0",
"scripts": {
"test": "node index.ts",
"denoflare": "deno run --unstable --allow-read --allow-net --allow-env https://raw.githubusercontent.com/skymethod/denoflare/v0.3.3/cli/cli.ts"
}
}
@@ -0,0 +1 @@
export type { IncomingRequestCf } from "https://raw.githubusercontent.com/skymethod/denoflare/v0.3.0/common/cloudflare_workers_types.d.ts";
@@ -0,0 +1,33 @@
console.log('Hello World');

import { IncomingRequestCf } from "./deps.ts";

/**
* Based on the A/B Testing Cloudflare Worker example.
* Ref: https://developers.cloudflare.com/workers/examples/ab-testing
*/
function fetch(request: IncomingRequestCf): Response {
const NAME = "experiment-0";

const TEST_RESPONSE = new Response("Test group");
const CONTROL_RESPONSE = new Response("Control group");

// Determine which group this requester is in.
const cookie = request.headers.get("cookie");
if (cookie && cookie.includes(`${NAME}=control`)) {
return CONTROL_RESPONSE;
} else if (cookie && cookie.includes(`${NAME}=test`)) {
return TEST_RESPONSE;
} else {
// If there is no cookie, this is a new client. Choose a group and set the cookie.
const group = Math.random() < 0.5 ? "test" : "control"; // 50/50 split
const response = group === "control" ? CONTROL_RESPONSE : TEST_RESPONSE;
response.headers.append("Set-Cookie", `${NAME}=${group}; path=/`);

return response;
}
}

export default {
fetch,
};
@@ -0,0 +1,8 @@
{
"name": "@exo/denoflare-layer",
"version": "0.0.0",
"scripts": {
"test": "node index.ts",
"denoflare": "deno run --unstable --allow-read --allow-net --allow-env https://raw.githubusercontent.com/skymethod/denoflare/v0.3.3/cli/cli.ts"
}
}
@@ -2,6 +2,12 @@
"$schema": "./node_modules/nx/schemas/workspace-schema.json",
"version": 2,
"projects": {
"denoflare-ab": {
"root": "libs/deno/denoflare/ab"
},
"denoflare-layer": {
"root": "libs/deno/denoflare/layer"
},
"gatsby": {
"root": "apps/gatsby",
"sourceRoot": "apps/gatsby/src",

0 comments on commit d69af47

Please sign in to comment.