From c3778507d4d7bab639082aa725d7b2e540208c0e Mon Sep 17 00:00:00 2001 From: hzrd149 Date: Tue, 25 Mar 2025 08:26:53 +0000 Subject: [PATCH] remove node sea add CACHE_TIME variable --- .changeset/strong-mice-slide.md | 5 --- .env.example | 3 ++ .github/workflows/node-sea.yml | 56 --------------------------------- .gitignore | 2 +- package.json | 9 +++--- sea-config.json | 5 --- src/cache.ts | 35 ++++++++++----------- src/env.ts | 2 ++ tsconfig.json | 2 +- 9 files changed, 28 insertions(+), 91 deletions(-) delete mode 100644 .changeset/strong-mice-slide.md delete mode 100644 .github/workflows/node-sea.yml delete mode 100644 sea-config.json diff --git a/.changeset/strong-mice-slide.md b/.changeset/strong-mice-slide.md deleted file mode 100644 index 0808625..0000000 --- a/.changeset/strong-mice-slide.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -"nsite-gateway": minor ---- - -Change build folder diff --git a/.env.example b/.env.example index abbb664..a3e2f1b 100644 --- a/.env.example +++ b/.env.example @@ -2,6 +2,9 @@ # can be in-memory, redis:// or sqlite:// CACHE_PATH="in-memory" +# How long to keep a pubkeys relays and blossom servers in cache (in seconds) +CACHE_TIME=3600 + # A list of relays to find users relay lists (10002) and blossom servers (10063) LOOKUP_RELAYS=wss://user.kindpag.es,wss://purplepag.es diff --git a/.github/workflows/node-sea.yml b/.github/workflows/node-sea.yml deleted file mode 100644 index 2ce98f0..0000000 --- a/.github/workflows/node-sea.yml +++ /dev/null @@ -1,56 +0,0 @@ -name: Build SEA - -on: - push: - branches: - - master - -concurrency: ${{ github.workflow }}-${{ github.ref }} - -jobs: - sea-action: - name: Build SEA - strategy: - matrix: - os: [ubuntu-latest] - runs-on: ${{ matrix.os }} - steps: - - name: Checkout - id: checkout - uses: actions/checkout@v4 - - - uses: pnpm/action-setup@v4 - - - name: Setup Node.js - id: setup-node - uses: actions/setup-node@v4 - with: - node-version-file: .nvmrc - cache: pnpm - - - name: Install Dependencies - run: pnpm install - - - name: Build and bundle - run: pnpm build && pnpm bundle - - - name: Find Node - id: find-node - run: echo "node=$(node -e 'console.log(process.argv[0]);')" >> - $env:GITHUB_OUTPUT - - - name: SEA - id: sea - uses: bryopsida/node-sea-action@v1 - with: - working-dir: . - output-dir: build/release - executable-name: nsite-gateway - sea-config-path: sea-config.json - node-path: ${{ steps.find-node.outputs.node }} - - - uses: actions/upload-artifact@v4 - with: - name: ${{ matrix.os }}-sea - path: build/release - if-no-files-found: error diff --git a/.gitignore b/.gitignore index 725830d..2b89ae8 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,4 @@ build data .netrc screenshots -dist + diff --git a/package.json b/package.json index 70a8418..6a01e28 100644 --- a/package.json +++ b/package.json @@ -2,21 +2,20 @@ "name": "nsite-gateway", "version": "0.7.0", "description": "A blossom server implementation written in Typescript", - "main": "dist/index.js", + "main": "build/index.js", "type": "module", "author": "hzrd149", "license": "MIT", "scripts": { - "start": "node dist/index.js", + "start": "node build/index.js", "prepack": "tsc", "build": "tsc", "dev": "nodemon -i '**/data/**' --exec 'node' --loader @swc-node/register/esm src/index.ts", - "bundle": "esbuild --bundle dist/index.js --format=esm --platform=node --outfile=build/bundle.mjs", "format": "prettier -w ." }, - "bin": "dist/index.js", + "bin": "build/index.js", "files": [ - "dist", + "build", "public" ], "dependencies": { diff --git a/sea-config.json b/sea-config.json deleted file mode 100644 index e118a08..0000000 --- a/sea-config.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "main": "build/bundle.mjs", - "output": "build/nsite-gateway.blob", - "assets": [] -} diff --git a/src/cache.ts b/src/cache.ts index 8f009b1..d229e81 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -1,10 +1,5 @@ import Keyv from "keyv"; -import pfs from "fs/promises"; -import { CACHE_PATH } from "./env.js"; - -try { - await pfs.mkdir("data"); -} catch (error) {} +import { CACHE_PATH, CACHE_TIME } from "./env.js"; async function createStore() { if (!CACHE_PATH || CACHE_PATH === "in-memory") return undefined; @@ -26,26 +21,30 @@ store?.on("error", (err) => { const opts = store ? { store } : {}; -/** domain -> pubkey */ -export const userDomains = new Keyv({ +/** A cache that maps a domain to a pubkey ( domain -> pubkey ) */ +export const userDomains = new Keyv({ ...opts, namespace: "domains", - // cache domains for an hour - ttl: 60 * 60 * 1000, + ttl: CACHE_TIME * 1000, }); -/** pubkey -> blossom servers */ -export const userServers = new Keyv({ +/** A cache that maps a pubkey to a set of blossom servers ( pubkey -> servers ) */ +export const userServers = new Keyv({ ...opts, namespace: "servers", - // cache servers for an hour - ttl: 60 * 60 * 1000, + ttl: CACHE_TIME * 1000, }); -/** pubkey -> relays */ -export const userRelays = new Keyv({ +/** A cache that maps a pubkey to a set of relays ( pubkey -> relays ) */ +export const userRelays = new Keyv({ ...opts, namespace: "relays", - // cache relays for an hour - ttl: 60 * 60 * 1000, + ttl: CACHE_TIME * 1000, +}); + +/** A cache that maps a pubkey + path to blossom servers that had the blob ( pubkey/path -> servers ) */ +export const pathServers = new Keyv({ + ...opts, + namespace: "paths", + ttl: CACHE_TIME * 1000, }); diff --git a/src/env.ts b/src/env.ts index e32cdb4..be211f6 100644 --- a/src/env.ts +++ b/src/env.ts @@ -18,6 +18,7 @@ const MAX_FILE_SIZE = process.env.MAX_FILE_SIZE ? xbytes.parseSize(process.env.M const NGINX_CACHE_DIR = process.env.NGINX_CACHE_DIR; const CACHE_PATH = process.env.CACHE_PATH; +const CACHE_TIME = process.env.CACHE_TIME ? parseInt(process.env.CACHE_TIME) : 60 * 60; const PAC_PROXY = process.env.PAC_PROXY; const TOR_PROXY = process.env.TOR_PROXY; @@ -50,4 +51,5 @@ export { ENABLE_SCREENSHOTS, SCREENSHOTS_DIR, ONION_HOST, + CACHE_TIME, }; diff --git a/tsconfig.json b/tsconfig.json index 21c4cce..a0f9a6a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,7 +3,7 @@ "module": "NodeNext", "target": "es2020", "moduleResolution": "NodeNext", - "outDir": "dist", + "outDir": "build", "skipLibCheck": true, "strict": true, "sourceMap": true