Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add examples #1

Merged
merged 1 commit into from
Aug 13, 2024
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
6 changes: 6 additions & 0 deletions .changeset/sharp-pens-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"example/simple-http-page": minor
"@bunny.net/edgescript-sdk": minor
---

Add examples
40 changes: 40 additions & 0 deletions example/deno-simple-http-page/.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.
15 changes: 15 additions & 0 deletions example/deno-simple-http-page/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "example/simple-http-page",
"main": "src/index.ts",
"type": "module",
"scripts": {
"lint": "deno lint",
"test": "deno test",
"build": "echo \"No build with Deno!\"",
"dev": "deno run src/main.ts"
},
"dependencies": {
"@bunny.net/edgescript-sdk": "workspace:*"
},
"devDependencies": {}
}
13 changes: 13 additions & 0 deletions example/deno-simple-http-page/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as BunnySDK from "../../../libs/bunny-sdk/esm-bunny/lib.mjs";

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

BunnySDK.net.http.serve({ port: 8080, hostname: '127.0.0.1' }, async (req) => {
console.log("blbl");
console.log(req.url);
return new Response("blbl");
});
console.log("net");
await sleep(100);
Empty file.
7 changes: 5 additions & 2 deletions example/simple-http-page/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"name": "example/simple-http-page",
"version": "0.1.0",
"main": "dist/index.js",
"main": "src/index.ts",
"type": "module",
"files": [
"dist"
],
Expand Down Expand Up @@ -37,5 +38,7 @@
"typescript": "^5.5.4",
"typescript-eslint": "^8.0.1"
},
"dependencies": {}
"dependencies": {
"@bunny.net/edgescript-sdk": "workspace:*"
}
}
15 changes: 13 additions & 2 deletions example/simple-http-page/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
export function main() {
console.log("blbl");
import * as BunnySDK from "@bunny.net/edgescript-sdk";

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

BunnySDK.net.http.serve({ port: 8080, hostname: '127.0.0.1' }, async (req) => {
console.log("blbl");
console.log(req.url);
return new Response("blbl");
});
console.log("net");
await sleep(10000);

20 changes: 17 additions & 3 deletions example/simple-http-page/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"target": "esnext",
"module": "nodenext",
"lib": [
"esnext"
],
"outDir": "./dist",
"baseUrl": "./src",
"rootDir": "./src",
"strict": true,
"allowSyntheticDefaultImports": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"downlevelIteration": true,
"forceConsistentCasingInFileNames": true,
"suppressExcessPropertyErrors": false,
"declaration": true,
"sourceMap": false,
"noImplicitAny": false,
"esModuleInterop": true
"noEmitOnError": true,
"esModuleInterop": true,
"skipLibCheck": true
},
"exclude": [
"node_modules",
Expand Down
61 changes: 30 additions & 31 deletions libs/bunny-sdk/build.mjs
Original file line number Diff line number Diff line change
@@ -1,33 +1,28 @@
import * as esbuild from 'esbuild'
import path from 'node:path'
import npmDTS from 'npm-dts';
import { readFile } from 'fs/promises';
import { defineConfig } from 'tsup'
import { build } from 'tsup'

const pkg = JSON.parse(await readFile(new URL('./package.json', import.meta.url)));

const sharedConfig = {
entryPoints: ['src/lib.ts'],
bundle: true,
minify: true,
outfile: 'dist/index.js',
entry: ['src/lib.ts'],
clean: true,
experimentalDts: true,
define: {
"VERSION": `"${pkg.version}"`,
}
};

await new npmDTS.Generator({
entry: 'src/lib.ts',
output: 'dist/index.d.ts',
}).generate();

// CJS Build
await esbuild.build({
await build({
minify: false,
splitting: false,
sourcemap: false,
outDir: "dist/",
platform: "node",
...sharedConfig,
platform: 'node',
outfile: "dist/index.js",
});


})

let noNodeImpl = {
name: 'example',
Expand All @@ -42,22 +37,26 @@ let noNodeImpl = {
}))
},
}

// ESM Build
await esbuild.build({
...sharedConfig,
await build({
minify: false,
splitting: true,
sourcemap: false,
outDir: "esm/",
platform: 'neutral',
outfile: "esm/index.js",
plugins: [noNodeImpl],
// external: ["*/_impl/node.ts", "*/_impl/node/"],
format: "esm"
});

// Bunny Build
await esbuild.build({
esbuildPlugins: [noNodeImpl],
format: "esm",
...sharedConfig,
})

// ESM Bunny Build
await build({
minify: true,
splitting: false,
sourcemap: false,
outDir: "esm-bunny/",
platform: 'neutral',
outfile: "esm-bunny/index.js",
esbuildPlugins: [noNodeImpl],
format: "esm",
plugins: [noNodeImpl],
});
...sharedConfig,
})
15 changes: 8 additions & 7 deletions libs/bunny-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
{
"name": "@bunny.net/edgescript-sdk",
"version": "0.2.0",
"main": "dist/index.js",
"module": "esm/index.js",
"types": "dist/index.d.ts",
"main": "dist/lib.js",
"module": "esm/lib.mjs",
"types": "dist/_tsup-dts-rollup.d.ts",
"files": [
"dist",
"esm",
"esm-bunny",
"dist/index.d.ts"
"esm-bunny"
],
"private": true,
"scripts": {
"lint": "eslint src",
"test": "jest --silent --coverage",
"build": "rm -rf dist esm && node build.mjs"
"build": "rm -rf dist esm esm-bunny && node build.mjs"
},
"repository": {
"type": "git",
Expand All @@ -29,8 +28,9 @@
"devDependencies": {
"@deno/shim-deno": "^0.19.2",
"@eslint/js": "^9.8.0",
"@microsoft/api-extractor": "^7.47.6",
"@types/jest": "^29.5.12",
"@types/node": "^22.1.0",
"@types/node": "^20.14.15",
"@typescript-eslint/eslint-plugin": "^8.0.1",
"@typescript-eslint/parser": "^8.0.1",
"@vercel/ncc": "^0.38.1",
Expand All @@ -41,6 +41,7 @@
"npm-dts": "^1.3.13",
"prettier": "^3.3.3",
"ts-jest": "^29.2.4",
"tsup": "^8.2.4",
"typescript": "^5.5.4",
"typescript-eslint": "^8.0.1"
},
Expand Down
11 changes: 0 additions & 11 deletions libs/bunny-sdk/src/internal/index.ts

This file was deleted.

10 changes: 0 additions & 10 deletions libs/bunny-sdk/src/internal/v1.ts

This file was deleted.

4 changes: 2 additions & 2 deletions libs/bunny-sdk/src/lib.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as net from "./net/";
import * as net from "./net/index.ts";

export {
/**
* The `net` module contains Network related functions and utilities.
*/
net,
}
};
8 changes: 4 additions & 4 deletions libs/bunny-sdk/src/net/_impl/node/serve.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as Tcp from "../../tcp";
import * as Ip from "../../ip";
import { ServeHandler, ServerHandler } from "../../serve";
import * as Tcp from "../../tcp.ts";
import * as Ip from "../../ip.ts";
import { ServeHandler, ServerHandler } from "../../serve.ts";
import * as HonoNode from "@hono/node-server";
import { Hono } from "hono";
import * as SocketAddr from "../../socket_addr";
import * as SocketAddr from "../../socket_addr.ts";

export function node_serve(listener: Tcp.TcpListener, handler: ServerHandler): ServeHandler {
const addr = Tcp.unstable_local_addr(listener);
Expand Down
6 changes: 3 additions & 3 deletions libs/bunny-sdk/src/net/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as tcp from './tcp';
import * as socketAddr from './socket_addr';
import * as http from './serve';
import * as tcp from './tcp.ts';
import * as socketAddr from './socket_addr.ts';
import * as http from './serve.ts';


export {
Expand Down
12 changes: 6 additions & 6 deletions libs/bunny-sdk/src/net/serve.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { internal_getPlatform } from "../platform";
import * as NodeImpl from "./_impl/node/serve";
import { TcpListener } from "./tcp";
import * as Tcp from "./tcp";
import * as Ip from "./ip";
import * as SocketAddr from './socket_addr';
import { internal_getPlatform } from "../platform.ts";
import * as NodeImpl from "./_impl/node/serve.ts";
import { TcpListener } from "./tcp.ts";
import * as Tcp from "./tcp.ts";
import * as Ip from "./ip.ts";
import * as SocketAddr from './socket_addr.ts';

/**
* A handler for HTTP Requests.
Expand Down
2 changes: 1 addition & 1 deletion libs/bunny-sdk/src/net/socket_addr.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IPv4 } from "./ip";
import { IPv4 } from "./ip.ts";

export type SocketAddr = {
readonly _tag: "SocketAddrV4",
Expand Down
2 changes: 1 addition & 1 deletion libs/bunny-sdk/src/net/tcp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as SocketAddr from "./socket_addr";
import * as SocketAddr from "./socket_addr.ts";

export type TcpListener = {
/**
Expand Down
1 change: 0 additions & 1 deletion libs/bunny-sdk/src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ type PlatformName =
// Defined by `build` step.
declare const VERSION: string;


type Platform = {
/**
* @internal
Expand Down
Loading
Loading