From 4c0e255959d41e5520bd5a02abb40ab392a12812 Mon Sep 17 00:00:00 2001 From: EricZhou Date: Tue, 5 Nov 2024 17:23:54 +0800 Subject: [PATCH] Refactor Deno workflow and update configurations: streamline environment setup, enhance test execution, and improve README links --- .github/workflows/deno.yml | 51 +++++++++++++++++++------------------- .gitignore | 3 ++- .vscode/launch.json | 16 ++++++------ README.md | 7 +++--- deno.json | 16 +++++++++--- edgetts.ts | 4 +-- edgetts_test.ts | 7 ++---- mod.ts | 8 +++--- sse_example.ts | 2 +- terminal.ts | 10 ++++---- vless.ts | 30 +--------------------- vless_example.ts | 30 ++++++++++++++++++++++ 12 files changed, 95 insertions(+), 89 deletions(-) create mode 100644 vless_example.ts diff --git a/.github/workflows/deno.yml b/.github/workflows/deno.yml index 7166ed9..c2cefd2 100644 --- a/.github/workflows/deno.yml +++ b/.github/workflows/deno.yml @@ -9,35 +9,34 @@ name: Deno on: - push: - branches: ["main"] - pull_request: - branches: ["main"] + push: + branches: ['main'] + pull_request: + branches: ['main'] env: - CLOUDFLARE_AUTH_TOKEN: ${{ secrets.CLOUDFLARE_AUTH_TOKEN }} - CLOUDFLARE_ACCOUNT_ID: ${{ vars.CLOUDFLARE_ACCOUNT_ID }} + AI_XXX: hello world permissions: - contents: read + contents: read jobs: - test: - runs-on: ubuntu-latest - environment: production - steps: - - name: Setup repo - uses: actions/checkout@v4 - - - name: Setup Deno - uses: denoland/setup-deno@v2 - with: - deno-version: v2.x - - - name: Run tests - env: - CLOUDFLARE_AUTH_TOKEN: ${{ secrets.CLOUDFLARE_AUTH_TOKEN }} - CLOUDFLARE_ACCOUNT_ID: ${{ vars.CLOUDFLARE_ACCOUNT_ID }} - run: | - echo "$CLOUDFLARE_ACCOUNT_ID" - deno test -A cf_ai_test.ts + test: + runs-on: ubuntu-latest + environment: production # must same with repo -> settings -> environments + steps: + - name: Setup repo + uses: actions/checkout@v4 + + - name: Setup Deno + uses: denoland/setup-deno@v2 + with: + deno-version: v2.x + + - name: Run tests + env: + CLOUDFLARE_AUTH_TOKEN: ${{ secrets.CLOUDFLARE_AUTH_TOKEN }} + CLOUDFLARE_ACCOUNT_ID: ${{ vars.CLOUDFLARE_ACCOUNT_ID }} + run: | + echo "$CLOUDFLARE_ACCOUNT_ID" + deno test -A cf_ai_test.ts diff --git a/.gitignore b/.gitignore index 5867ac3..d3ca79c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.mp3 *.exe -.idea/ \ No newline at end of file +.idea/ +dist/ \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json index 8b98dae..b0e2629 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -20,14 +20,14 @@ }, { "request": "launch", - "name": "Start SSE Test Server", + "name": "Start SSE Example Server", "type": "node", - "program": "${workspaceFolder}/sse_test.ts", + "program": "${workspaceFolder}/sse_example.ts", "cwd": "${workspaceFolder}", "env": {}, "runtimeExecutable": "deno.EXE", "runtimeArgs": [ - "run", + "serve", "--watch", "--unstable", "--inspect-wait", @@ -38,16 +38,16 @@ }, { "request": "launch", - "name": "vless", + "name": "vless example server", "type": "node", - "program": "${workspaceFolder}/vless.ts", + "program": "${workspaceFolder}/vless_example.ts", "cwd": "${workspaceFolder}", "env": { - "USER_UUID": "bcaa12d6-1f1f-4f32-84b6-b701de2e1eec", + "USER_UUID": "bcaa12d6-1f1f-4f32-84b6-b701de2e1eec" }, "runtimeExecutable": "deno.EXE", "runtimeArgs": [ - "run", + "serve", // "--watch", "--unstable", "--inspect-wait", @@ -57,4 +57,4 @@ "attachSimplePort": 9229 } ] -} \ No newline at end of file +} diff --git a/README.md b/README.md index 3513065..920f62f 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,11 @@ # Edge Text to Speech with Deno.js -[![JSR Score](https://jsr.io/badges/@mojocn/code/score)](https://jsr.io/@mojocn/codeape) +[![JSR Score](https://jsr.io/badges/@mojocn/code/score)](https://jsr.io/@mojocn/codeape) ## Youtube Videos Tutorial 1. [Use Free Edge Text to Speech API with Deno.js](https://www.youtube.com/watch?v=uATgw1KVj_4) +## Links - -## Links -- [Edge browser TTS detail analysis by BurpSuite](https://github.com/mojocn/codeape/wiki/Microsoft-Edge-Browser-TTS-API-Request-Info-by-Burpsuit) \ No newline at end of file +- [Edge browser TTS detail analysis by BurpSuite](https://github.com/mojocn/codeape/wiki/Microsoft-Edge-Browser-TTS-API-Request-Info-by-Burpsuit) diff --git a/deno.json b/deno.json index 5b2e349..61c6b61 100644 --- a/deno.json +++ b/deno.json @@ -1,7 +1,17 @@ { - "name":"@mojocn/codeape", - "version":"0.0.1", - "exports":"./mod.ts", + "name": "@mojocn/codeape", + "version": "0.0.1", + "exports": "./mod.ts", + "publish": { + "include": [ + "LICENSE", + "README.md", + "src/**/*.ts" + ], + "exclude": [ + "src/tests" + ] + }, "tasks": { "dev": "deno run --watch main.ts", "lint": "deno lint" diff --git a/edgetts.ts b/edgetts.ts index eeed47c..67cc3f6 100644 --- a/edgetts.ts +++ b/edgetts.ts @@ -1,4 +1,4 @@ -export const toMs = (duration: number):number => Math.floor(duration / 10_000); +export const toMs = (duration: number): number => Math.floor(duration / 10_000); interface WordBoundary { Type: 'WordBoundary'; Data: { @@ -112,7 +112,7 @@ class TtsResult { this.audioParts = []; this.marks = []; } - get mp3Blob():Blob { + get mp3Blob(): Blob { return new Blob(this.audioParts, { type: 'audio/mpeg' }); } async writeToFile(path?: string) { diff --git a/edgetts_test.ts b/edgetts_test.ts index 6f21c35..bdb99f1 100644 --- a/edgetts_test.ts +++ b/edgetts_test.ts @@ -1,8 +1,7 @@ -import { assertExists ,} from '@std/assert'; +import { assertExists } from '@std/assert'; import { EdgeTts } from './edgetts.ts'; -const txt = - `Deno is awesome!`; +const txt = `Deno is awesome!`; Deno.test({ name: 'test edge TTS speak', @@ -29,5 +28,3 @@ Deno.test('test edge TTS voice list', async () => { const voices = await tts.voices(); assertExists(voices); }); - - diff --git a/mod.ts b/mod.ts index 9dc585c..0075beb 100644 --- a/mod.ts +++ b/mod.ts @@ -1,5 +1,3 @@ - - -export * from "./sse.ts"; -export * from "./vless.ts"; -export * from "./edgetts.ts"; \ No newline at end of file +export * from './sse.ts'; +export * from './vless.ts'; +export * from './edgetts.ts'; diff --git a/sse_example.ts b/sse_example.ts index 4b564d9..0e78321 100644 --- a/sse_example.ts +++ b/sse_example.ts @@ -31,4 +31,4 @@ function fetch(_req: Request) { export default { fetch }; -// deno run -A sse_example.ts \ No newline at end of file +// deno serve -A sse_example.ts diff --git a/terminal.ts b/terminal.ts index 44de53d..221288a 100644 --- a/terminal.ts +++ b/terminal.ts @@ -2,10 +2,10 @@ function bashPrompt() { alert('Would you liken to learn more about deno terminal?'); console.log('The message has been acknowledged.'); const shouldProceed = confirm('Do you want to proceed?'); - if (!shouldProceed) { - console.log('User has cancelled the operation.'); - return; - } + if (!shouldProceed) { + console.log('User has cancelled the operation.'); + return; + } console.log('Should proceed?', shouldProceed); const name = prompt('Please enter your name:'); console.log('Name:', name); @@ -13,4 +13,4 @@ function bashPrompt() { console.log('Age:', age); } -bashPrompt(); \ No newline at end of file +bashPrompt(); diff --git a/vless.ts b/vless.ts index 4765df8..9364b8c 100644 --- a/vless.ts +++ b/vless.ts @@ -8,7 +8,7 @@ function toUUIDWithoutDash(arr: Uint8Array) { // https://xtls.github.io/development/protocols/vless.html // https://github.com/zizifn/excalidraw-backup/blob/main/v2ray-protocol.excalidraw -export class VlessPayload { +class VlessPayload { hasError: boolean; message: string; userUUID: string; @@ -499,31 +499,3 @@ export class Session { } } } - -function vlessProxy(request: Request): Response { - const { socket, response } = Deno.upgradeWebSocket(request); - const envUserUUID = Deno.env.get('USER_UUID') || ''; - - const checkAuth: AuthFn = (userUuidWithoutDashes: string): boolean => { - return userUuidWithoutDashes === envUserUUID.replaceAll(/-/g, ''); - }; - const trafficFn: TrafficFn = (kk: string, trafficDeltaMB: number) => { - console.info(`trafficFn: ${kk} ${trafficDeltaMB}`); - }; - const session = new Session(socket, checkAuth, trafficFn); - session.start(); - // record user's traffic - return response; -} - -async function handleRequest(request: Request): Promise { - const env = Deno.env; - if (request.headers.get('Upgrade') === 'websocket') { - return await vlessProxy(request); - } - return new Response('Hello World', { - headers: { 'content-type': 'text/plain' }, - }); -} - -Deno.serve(handleRequest); diff --git a/vless_example.ts b/vless_example.ts new file mode 100644 index 0000000..b788a91 --- /dev/null +++ b/vless_example.ts @@ -0,0 +1,30 @@ +import { AuthFn, Session, TrafficFn } from './vless.ts'; + +function vlessProxy(request: Request): Response { + const { socket, response } = Deno.upgradeWebSocket(request); + const envUserUUID = Deno.env.get('USER_UUID') || ''; + + const checkAuth: AuthFn = (userUuidWithoutDashes: string): boolean => { + return userUuidWithoutDashes === envUserUUID.replaceAll(/-/g, ''); + }; + const trafficFn: TrafficFn = (kk: string, trafficDeltaMB: number) => { + console.info(`trafficFn: ${kk} ${trafficDeltaMB}`); + }; + const session = new Session(socket, checkAuth, trafficFn); + session.start(); + // record user's traffic + return response; +} + +async function handleRequest(request: Request): Promise { + const env = Deno.env; + if (request.headers.get('Upgrade') === 'websocket') { + return await vlessProxy(request); + } + return new Response('Hello World', { + headers: { 'content-type': 'text/plain' }, + }); +} + +export default { fetch: handleRequest }; +//deno serve --allow-net --allow-env vless.ts