Skip to content

Commit

Permalink
Merge pull request #35 from BunnyWay/feat-add-middlewares
Browse files Browse the repository at this point in the history
feat: add middlewares for local dev
  • Loading branch information
antho-bunny authored Sep 10, 2024
2 parents 1692921 + 175325d commit 69b0e2f
Show file tree
Hide file tree
Showing 20 changed files with 528 additions and 13 deletions.
6 changes: 6 additions & 0 deletions .changeset/polite-pets-dress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"example/middleware-simple-http": minor
"example/deno-middleware-http": minor
---

Add new examples for middleware
5 changes: 5 additions & 0 deletions .changeset/silent-suns-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@bunny.net/edgescript-sdk": minor
---

Add middleware for local development
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Network for your deployed script.
With `@bunny.net/edgescript-sdk` you can write a script which will work with
Deno, with Node, and within our network.

```
```typescript
import * as BunnySDK from "@bunny.net/edgescript-sdk";

function sleep(ms: number): Promise<void> {
Expand Down
40 changes: 40 additions & 0 deletions example/deno-middleware-http/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/
/dist/
/esm/
/esm-bunny/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

.vscode

# local env files
.env*.local

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts
Empty file.
28 changes: 28 additions & 0 deletions example/deno-middleware-http/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "example/deno-middleware-http",
"version": "0.0.0",
"main": "src/index.ts",
"type": "module",
"private": true,
"repository": {
"type": "git",
"url": "git+https://github.com/BunnyWay/edge-script-sdk.git"
},
"keywords": [
"github",
"bunny"
],
"author": "Bunny Devs",
"license": "MIT",
"scripts": {
"lint": "deno lint",
"test": "deno test --allow-none",
"build": "echo \"No build with Deno!\"",
"dev": "deno run src/main.ts",
"release": "echo \"No release\""
},
"dependencies": {
"@bunny.net/edgescript-sdk": "^0.10.0"
},
"devDependencies": {}
}
19 changes: 19 additions & 0 deletions example/deno-middleware-http/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as BunnySDK from "../../../libs/bunny-sdk/esm/lib.mjs";

function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}

console.log("Starting server...");

BunnySDK.net.http.servePullZone({ url: "https://perdu.com/" }).onOriginRequest(async (ctx) => {
const req = ctx.request;
console.log(`[INFO]: ${req.method} - ${req.url}`);
await sleep(1);
return ctx.request;
}).onOriginResponse(async (ctx) => {
const res = ctx.response;
console.log(`[INFO]: ${res.status}`);
await sleep(1);
return ctx.response;
});
2 changes: 1 addition & 1 deletion example/deno-simple-http-page/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function sleep(ms: number): Promise<void> {
}

console.log("Starting server...");
BunnySDK.net.http.serve({}, async (req) => {
BunnySDK.net.http.serve(async (req) => {
console.log(`[INFO]: ${req.method} - ${req.url}`);
await sleep(1);
return new Response("blbl");
Expand Down
40 changes: 40 additions & 0 deletions example/middleware-simple-http/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/
/dist/
/esm/
/esm-bunny/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

.vscode

# local env files
.env*.local

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts
12 changes: 12 additions & 0 deletions example/middleware-simple-http/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import globals from "globals";
import pluginJs from "@eslint/js";
import tseslint from "typescript-eslint";


export default [
{ files: ["**/*.{js,mjs,cjs,ts}"] },
{ languageOptions: { globals: globals.browser } },
pluginJs.configs.recommended,
...tseslint.configs.recommended,
];

9 changes: 9 additions & 0 deletions example/middleware-simple-http/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default {
clearMocks: true,
moduleFileExtensions: ['js', 'ts'],
testEnvironment: 'node',
testMatch: ['**/*.test.ts'],
transform: {
'^.+\\.ts$': 'ts-jest'
},
}
46 changes: 46 additions & 0 deletions example/middleware-simple-http/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "example/middleware-simple-http",
"version": "0.0.0",
"main": "src/index.ts",
"type": "module",
"files": [
"dist"
],
"private": true,
"scripts": {
"lint": "eslint src",
"test": "jest --silent --coverage",
"dev": "pnpm run build && node dist/index.js",
"build": "ncc build src/main.ts -o dist/",
"release": "echo \"No release\""
},
"repository": {
"type": "git",
"url": "git+https://github.com/BunnyWay/edge-script-sdk.git"
},
"keywords": [
"github",
"bunny"
],
"author": "Bunny Devs",
"license": "MIT",
"devDependencies": {
"@eslint/js": "^9.8.0",
"@types/jest": "^29.5.12",
"@types/node": "^22.1.0",
"@typescript-eslint/eslint-plugin": "^8.0.1",
"@typescript-eslint/parser": "^8.0.1",
"@vercel/ncc": "^0.38.1",
"esbuild": "0.23.0",
"eslint": "^9.8.0",
"globals": "^15.9.0",
"jest": "^29.5.12",
"prettier": "^3.3.3",
"ts-jest": "^29.2.4",
"typescript": "^5.5.4",
"typescript-eslint": "^8.0.1"
},
"dependencies": {
"@bunny.net/edgescript-sdk": "workspace:*"
}
}
19 changes: 19 additions & 0 deletions example/middleware-simple-http/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as BunnySDK from "@bunny.net/edgescript-sdk";

function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}

console.log("Starting server...");

BunnySDK.net.http.servePullZone({ url: "https://perdu.com/" }).onOriginRequest(async (ctx) => {
const req = ctx.request;
console.log(`[INFO]: ${req.method} - ${req.url}`);
await sleep(1);
return ctx.request;
}).onOriginResponse(async (ctx) => {
const res = ctx.response;
console.log(`[INFO]: ${res.status}`);
await sleep(1);
return ctx.response;
});
8 changes: 8 additions & 0 deletions example/middleware-simple-http/tests/empty.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// import { jest } from '@jest/globals';

describe('empty', () => {
test('empty test', async () => {
expect(true).toBe(true);
});

});
28 changes: 28 additions & 0 deletions example/middleware-simple-http/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"compilerOptions": {
"target": "esnext",
"module": "nodenext",
"lib": [
"esnext"
],
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"allowSyntheticDefaultImports": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"downlevelIteration": true,
"forceConsistentCasingInFileNames": true,
"suppressExcessPropertyErrors": false,
"declaration": true,
"sourceMap": false,
"noImplicitAny": false,
"noEmitOnError": false,
"esModuleInterop": true,
"skipLibCheck": true
},
"exclude": [
"node_modules",
"**/*.test.*"
]
}
Loading

0 comments on commit 69b0e2f

Please sign in to comment.