Skip to content

Commit

Permalink
Refactor Deno workflow and update configurations: streamline environm…
Browse files Browse the repository at this point in the history
…ent setup, enhance test execution, and improve README links
  • Loading branch information
EricZhou committed Nov 5, 2024
1 parent 4939e6e commit 4c0e255
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 89 deletions.
51 changes: 25 additions & 26 deletions .github/workflows/deno.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.mp3
*.exe
.idea/
.idea/
dist/
16 changes: 8 additions & 8 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -57,4 +57,4 @@
"attachSimplePort": 9229
}
]
}
}
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
- [Edge browser TTS detail analysis by BurpSuite](https://github.com/mojocn/codeape/wiki/Microsoft-Edge-Browser-TTS-API-Request-Info-by-Burpsuit)
16 changes: 13 additions & 3 deletions deno.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
4 changes: 2 additions & 2 deletions edgetts.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 2 additions & 5 deletions edgetts_test.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -29,5 +28,3 @@ Deno.test('test edge TTS voice list', async () => {
const voices = await tts.voices();
assertExists(voices);
});


8 changes: 3 additions & 5 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


export * from "./sse.ts";
export * from "./vless.ts";
export * from "./edgetts.ts";
export * from './sse.ts';
export * from './vless.ts';
export * from './edgetts.ts';
2 changes: 1 addition & 1 deletion sse_example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ function fetch(_req: Request) {

export default { fetch };

// deno run -A sse_example.ts
// deno serve -A sse_example.ts
10 changes: 5 additions & 5 deletions terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ 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);
const age = prompt('Please enter your age:', '18');
console.log('Age:', age);
}

bashPrompt();
bashPrompt();
30 changes: 1 addition & 29 deletions vless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Response> {
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);
30 changes: 30 additions & 0 deletions vless_example.ts
Original file line number Diff line number Diff line change
@@ -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<Response> {
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

0 comments on commit 4c0e255

Please sign in to comment.