chore: use alternative ways to bundle

This commit is contained in:
PalmDevs
2024-07-04 21:02:01 +07:00
parent ebf1ac7c08
commit 9b6ba56d99
50 changed files with 443 additions and 399 deletions

View File

@@ -1,5 +1,2 @@
# Safety measures, do not remove
IS_USING_DOT_ENV=1
# Your Wit.ai token
WIT_AI_TOKEN="YOUR_TOKEN_HERE"

View File

@@ -1,8 +0,0 @@
{
"$schema": "./config.schema.json",
"address": "127.0.0.1",
"port": 3000,
"ocrConcurrentQueues": 3,
"logLevel": "log"
}

View File

@@ -42,4 +42,4 @@ The possible levels (sorted by their importance descendingly) are:
The next page will tell you how to run and bundle the server.
Continue: [🏃🏻‍♂️ Running the server](./2_running.md)
Continue: [🏃🏻‍♂️ Running the server](./2_running_and_deploying.md)

View File

@@ -1,54 +0,0 @@
# 🏃🏻‍♂️ Running the server
There are many methods to run the server. Choose one that suits best for the situation.
## 👷🏻 Development mode (recommended)
There will be no compilation step, and Bun will automatically watch changes and restart the server for you.
You can quickly start the server by running:
```sh
bun dev
```
## 🌐 Production mode
Production mode runs no different from the development server, it simply has less debugging information printed to console by default. However, more production-specific features may come.
To start the server in production mode, you'll have to:
1. Set the `NODE_ENV` environment variable to `production`
> It is very possible to set the value in the `.env` file and let Bun load it, **but it is recommended to set the variable before Bun even starts**.
2. Start the server
```sh
bun dev
```
## 📦 Building
If you're looking to build and host the server somewhere else, you can run:
```sh
bun bundle
```
The files will be placed in the `dist` directory. **Configurations and `.env` files will NOT be copied automatically.**
To start up the server, you'll need to install `tesseract.js` first.
```sh
bun install tesseract.js
# or
bun install tesseract.js -g
# Run the server
bun run index.js
```
## ⏭️ What's next
The next page will tell you about packets.
Continue: [📨 Packets](./3_packets.md)

View File

@@ -0,0 +1,59 @@
# 🏃🏻‍♂️ Running and deploying the server
There are many methods to run the server. Choose one that suits best for the situation.
## 👷🏻 Development mode
There will be no compilation step, and Bun will automatically watch changes and restart the server for you.
You can quickly start the server by running:
```sh
bun dev
```
## 📦 Building
If you're looking to build and host the server somewhere else, you can run:
```sh
bun run build
```
The distribution files will be placed inside the `dist` directory. Inside will include:
- The default configuration for the API
- Compiled source files of the API
You'll need to also copy the `node_modules` directory dereferenced if you want to run the distribution files somewhere else.
## ✈️ Deploying
To deploy the API, you'll need to:
1. [Build the API as seen in the previous step](#-building)
2. Copy contents of the `dist` directory
```sh
# For instance, we'll copy them both to /usr/src/api
cp -R ./dist/* /usr/src/api
```
3. Replace the default configuration *(optional)*
4. Configure environment variables
As seen in [`.env.example`](../.env.example). You can also optionally use a `.env` file which **Bun will automatically load**.
5. Finally, you can run the API using these commands
```sh
cd /usr/src/api
bun run index.js
```
## ⏭️ What's next
The next page will tell you about packets.
Continue: [📨 Packets](./3_packets.md)

View File

@@ -6,7 +6,7 @@
"description": "🧦 WebSocket API server for bots assisting ReVanced",
"main": "dist/index.js",
"scripts": {
"bundle": "bun build src/index.ts --outdir=dist --target=bun -e tesseract.js",
"bundle": "bun run scripts/build.ts",
"dev": "bun run src/index.ts --watch",
"build": "bun bundle",
"watch": "bun dev"
@@ -30,9 +30,11 @@
"@revanced/bot-shared": "workspace:*",
"@sapphire/async-queue": "^1.5.2",
"chalk": "^5.3.0",
"tesseract.js": "^5.1.0"
"tesseract.js": "^5.1.0",
"ws": "^8.17.1"
},
"devDependencies": {
"@types/ws": "^8.5.10",
"typed-emitter": "^2.1.0"
}
}

View File

@@ -0,0 +1,23 @@
import { createLogger } from '@revanced/bot-shared'
import { cp } from 'fs/promises'
async function build(): Promise<void> {
const logger = createLogger()
logger.info('Building Tesseract.js worker...')
await Bun.build({
entrypoints: ['../../node_modules/tesseract.js/src/worker-script/node/index.js'],
target: 'bun',
outdir: './dist/worker',
})
logger.info('Building WebSocket API...')
await Bun.build({
entrypoints: ['./src/index.ts'],
outdir: './dist',
target: 'bun',
})
}
await build()
await cp('config.json', 'dist/config.json')

View File

@@ -1,6 +1,8 @@
import { createWorker as createTesseractWorker } from 'tesseract.js'
import { OEM, createWorker as createTesseractWorker } from 'tesseract.js'
import { join as joinPath } from 'path'
import { inspect as inspectObject } from 'util'
import { exists as pathExists } from 'fs/promises'
import Client from './classes/Client'
@@ -36,10 +38,6 @@ if (!['development', 'production'].includes(environment)) {
logger.info(`Running in ${environment} mode...`)
if (environment === 'production' && process.env['IS_USING_DOT_ENV']) {
logger.warn('You seem to be using .env files, this is generally not a good idea in production...')
}
if (!process.env['WIT_AI_TOKEN']) {
logger.error('WIT_AI_TOKEN is not defined in the environment variables')
process.exit(1)
@@ -47,7 +45,14 @@ if (!process.env['WIT_AI_TOKEN']) {
// Workers and API clients
const tesseract = await createTesseractWorker('eng')
const TesseractWorkerPath = joinPath(import.meta.dir, 'worker', 'index.js')
const TesseractCompiledWorkerExists = await pathExists(TesseractWorkerPath)
const tesseract = await createTesseractWorker(
'eng',
OEM.DEFAULT,
TesseractCompiledWorkerExists ? { workerPath: TesseractWorkerPath } : undefined,
)
const wit = {
token: process.env['WIT_AI_TOKEN']!,
async fetch(route: string, options?: RequestInit) {

View File

@@ -7,8 +7,9 @@
"target": "ESNext",
"lib": ["ESNext"],
"composite": false,
"skipLibCheck": true
"skipLibCheck": true,
"resolveJsonModule": true
},
"exclude": ["node_modules", "dist"],
"include": ["./*.json", "src/**/*.ts"]
"include": ["./*.json", "src/**/*.ts", "scripts/**/*.ts"]
}