mirror of
https://github.com/geoffsee/open-gsio.git
synced 2025-09-08 22:56:46 +00:00
change semantics
Update README deployment steps and add deploy:secrets script to package.json update local inference script and README update lockfile reconfigure package scripts for development update test execution pass server tests Update README with revised Bun commands and workspace details remove pnpm package manager designator create bun server
This commit is contained in:

committed by
Geoff Seemueller

parent
1055cda2f1
commit
497eb22ad8
87
packages/cloudflare-workers/analytics/analytics_worker.js
Normal file
87
packages/cloudflare-workers/analytics/analytics_worker.js
Normal file
@@ -0,0 +1,87 @@
|
||||
addEventListener("fetch", (event) => {
|
||||
event.respondWith(handleRequest(event));
|
||||
});
|
||||
|
||||
async function handleRequest(event) {
|
||||
const url = new URL(event.request.url);
|
||||
const { request } = event;
|
||||
const { headers } = request;
|
||||
|
||||
const referer = headers.get("Referer") || "";
|
||||
const userAgent = headers.get("User-Agent");
|
||||
const refHost = (() => {
|
||||
try {
|
||||
return new URL(referer).hostname;
|
||||
} catch (e) {
|
||||
return "";
|
||||
}
|
||||
})();
|
||||
const uuid = getOrCreateUuid(headers);
|
||||
|
||||
event.waitUntil(logAnalyticsData(event, url, uuid, userAgent, referer));
|
||||
|
||||
const response = new Response(null, {
|
||||
status: 204,
|
||||
statusText: "No Content",
|
||||
});
|
||||
|
||||
if (!headers.get("cookie")?.includes("uuid=")) {
|
||||
response.headers.set(
|
||||
"Set-Cookie",
|
||||
`uuid=${uuid}; Expires=${new Date(Date.now() + 365 * 86400 * 30 * 1000).toUTCString()}; Path='/';`,
|
||||
);
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
function shouldBlockRequest(refHost, userAgent, url) {
|
||||
if (!refHost || !userAgent || !url.search.includes("ga=")) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function getOrCreateUuid(headers) {
|
||||
const cookie = headers.get("cookie") || "";
|
||||
const uuidMatch = cookie.match(/uuid=([^;]+)/);
|
||||
if (uuidMatch) {
|
||||
return uuidMatch[1];
|
||||
}
|
||||
return crypto.randomUUID();
|
||||
}
|
||||
|
||||
async function logAnalyticsData(event, url, uuid, userAgent, pageUrl) {
|
||||
const { ANALYTICS_ENGINE } = globalThis;
|
||||
const params = url.searchParams;
|
||||
|
||||
const dataPoint = {
|
||||
blobs: [
|
||||
pageUrl, // Page URL
|
||||
userAgent, // User Agent
|
||||
params.get("dt") || "", // Page Title
|
||||
params.get("de") || "", // Document Encoding
|
||||
params.get("dr") || "", // Document Referrer
|
||||
params.get("ul") || "", // User Language
|
||||
params.get("sd") || "", // Screen Colors
|
||||
params.get("sr") || "", // Screen Resolution
|
||||
params.get("vp") || "", // Viewport Size
|
||||
uuid, // Client ID
|
||||
],
|
||||
doubles: [
|
||||
parseFloat(params.get("plt") || "0"), // Page Load Time
|
||||
parseFloat(params.get("dns") || "0"), // DNS Time
|
||||
parseFloat(params.get("pdt") || "0"), // Page Download Time
|
||||
parseFloat(params.get("rrt") || "0"), // Redirect Response Time
|
||||
parseFloat(params.get("tcp") || "0"), // TCP Connect Time
|
||||
parseFloat(params.get("srt") || "0"), // Server Response Time
|
||||
parseFloat(params.get("dit") || "0"), // DOM Interactive Time
|
||||
parseFloat(params.get("clt") || "0"), // Content Loaded Time
|
||||
],
|
||||
indexes: [
|
||||
event.request.headers.get("CF-Connecting-IP") || "", // User IP
|
||||
],
|
||||
};
|
||||
|
||||
ANALYTICS_ENGINE.writeDataPoint(dataPoint);
|
||||
}
|
23
packages/cloudflare-workers/analytics/wrangler.jsonc
Normal file
23
packages/cloudflare-workers/analytics/wrangler.jsonc
Normal file
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"compatibility_date": "2025-05-27",
|
||||
"main": "analytics_worker.js",
|
||||
"name": "analytics",
|
||||
"analytics_engine_datasets": [
|
||||
{
|
||||
"binding": "ANALYTICS_ENGINE",
|
||||
"dataset": "global_analytics"
|
||||
}
|
||||
],
|
||||
"routes": [
|
||||
{
|
||||
"custom_domain": true,
|
||||
"pattern": "your.route.here"
|
||||
}
|
||||
],
|
||||
"dev": {
|
||||
"port": 3003
|
||||
},
|
||||
"placement": {
|
||||
"mode": "smart"
|
||||
}
|
||||
}
|
141
packages/cloudflare-workers/open-gsio/README.md
Normal file
141
packages/cloudflare-workers/open-gsio/README.md
Normal file
@@ -0,0 +1,141 @@
|
||||
# open-gsio
|
||||
[](https://github.com/geoffsee/open-gsio/actions/workflows/test.yml)
|
||||
[](https://opensource.org/licenses/MIT)
|
||||
</br>
|
||||
<p align="center">
|
||||
<img src="https://github.com/user-attachments/assets/620d2517-e7be-4bb0-b2b7-3aa0cba37ef0" width="250" />
|
||||
</p>
|
||||
|
||||
> **Note**: This project is currently under active development. The styling is a work in progress and some functionality
|
||||
> may be broken. Tests are being actively ported and stability will improve over time. Thank you for your patience!
|
||||
|
||||
## Table of Contents
|
||||
|
||||
- [Stack](#stack)
|
||||
- [Installation](#installation)
|
||||
- [Deployment](#deployment)
|
||||
- [Local Inference](#local-inference)
|
||||
- [mlx-omni-server (default)](#mlx-omni-server)
|
||||
- [Adding models](#adding-models-for-local-inference-apple-silicon)
|
||||
- [Ollama](#ollama)
|
||||
- [Adding models](#adding-models-for-local-inference-ollama)
|
||||
- [Testing](#testing)
|
||||
- [Troubleshooting](#troubleshooting)
|
||||
- [History](#history)
|
||||
- [License](#license)
|
||||
|
||||
## Stack
|
||||
* [TypeScript](https://www.typescriptlang.org/)
|
||||
* [Vike](https://vike.dev/)
|
||||
* [React](https://react.dev/)
|
||||
* [Cloudflare Workers](https://developers.cloudflare.com/workers/)
|
||||
* [itty‑router](https://github.com/kwhitley/itty-router)
|
||||
* [MobX‑State‑Tree](https://mobx-state-tree.js.org/)
|
||||
* [OpenAI SDK](https://github.com/openai/openai-node)
|
||||
* [Vitest](https://vitest.dev/)
|
||||
|
||||
|
||||
## Installation
|
||||
|
||||
1. `bun i && bun test`
|
||||
1. [Add your own `GROQ_API_KEY` in .dev.vars](https://console.groq.com/keys)
|
||||
1. In isolated shells, run `bun run server:dev` and `bun run client:dev`
|
||||
|
||||
> Note: it should be possible to use pnpm in place of bun.
|
||||
|
||||
## Deployment
|
||||
1. Setup the KV_STORAGE bindings in `wrangler.jsonc`
|
||||
1. [Add another `GROQ_API_KEY` in secrets.json](https://console.groq.com/keys)
|
||||
1. Run `bun run deploy && bun run deploy:secrets && bun run deploy`
|
||||
|
||||
> Note: Subsequent deployments should omit `bun run deploy:secrets`
|
||||
|
||||
|
||||
## Local Inference
|
||||
> Local inference is achieved by overriding the `OPENAI_API_KEY` and `OPENAI_API_ENDPOINT` environment variables. See below.
|
||||
|
||||
### mlx-omni-server
|
||||
(default) (Apple Silicon Only) - Use Ollama for other platforms.
|
||||
~~~bash
|
||||
# (prereq) install mlx-omni-server
|
||||
brew tap seemueller-io/tap
|
||||
brew install seemueller-io/tap/mlx-omni-server
|
||||
|
||||
bun run openai:local mlx-omni-server # Start mlx-omni-server
|
||||
bun run openai:local:enable # Configure connection
|
||||
bun run server:dev # Restart server
|
||||
~~~
|
||||
#### Adding models for local inference (Apple Silicon)
|
||||
|
||||
~~~bash
|
||||
# ensure mlx-omni-server is running
|
||||
|
||||
# See https://huggingface.co/mlx-community for available models
|
||||
MODEL_TO_ADD=mlx-community/gemma-3-4b-it-8bit
|
||||
|
||||
curl http://localhost:10240/v1/chat/completions \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "{
|
||||
\"model\": \"$MODEL_TO_ADD\",
|
||||
\"messages\": [{\"role\": \"user\", \"content\": \"Hello\"}]
|
||||
}"
|
||||
~~~
|
||||
|
||||
### Ollama
|
||||
~~~bash
|
||||
bun run openai:local ollama # Start ollama server
|
||||
bun run openai:local:enable # Configure connection
|
||||
bun run server:dev # Restart server
|
||||
~~~
|
||||
#### Adding models for local inference (ollama)
|
||||
|
||||
~~~bash
|
||||
# See https://ollama.com/library for available models
|
||||
MODEL_TO_ADD=gemma3
|
||||
docker exec -it ollama ollama run ${MODEL_TO_ADD}
|
||||
~~~
|
||||
|
||||
|
||||
## Testing
|
||||
|
||||
Tests are located in `__tests__` directories next to the code they test. Testing is incomplete at this time.
|
||||
|
||||
> `bun run test` will run all tests
|
||||
|
||||
|
||||
## Troubleshooting
|
||||
1. `bun run clean`
|
||||
1. `bun i`
|
||||
1. `bun server:dev`
|
||||
1. `bun client:dev`
|
||||
1. Submit an issue
|
||||
|
||||
History
|
||||
---
|
||||
A high-level overview for the development history of the parent repository, [geoff-seemueller-io](https://geoff.seemueller.io), is provided in [LEGACY.md](../../LEGACY.md).
|
||||
|
||||
## License
|
||||
~~~text
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2025 Geoff Seemueller
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
~~~
|
||||
|
6
packages/cloudflare-workers/open-gsio/main.ts
Normal file
6
packages/cloudflare-workers/open-gsio/main.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import Server from "@open-gsio/server";
|
||||
import ServerCoordinator from "@open-gsio/server/ServerCoordinator";
|
||||
|
||||
export {ServerCoordinator}
|
||||
|
||||
export default Server.Router();
|
16
packages/cloudflare-workers/open-gsio/package.json
Normal file
16
packages/cloudflare-workers/open-gsio/package.json
Normal file
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"name": "@open-gsio/worker",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "wrangler dev",
|
||||
"deploy": "bun run build:client && NODE_ENV=production wrangler deploy --minify",
|
||||
"deploy:dry-run": "bun run build:client && NODE_ENV=production wrangler deploy --minify --dry-run",
|
||||
"build:client": "(cd ../../../packages/client && vite build)",
|
||||
"build": "wrangler build"
|
||||
},
|
||||
"dependencies": {
|
||||
"@cloudflare/vite-plugin": "^1.3.1",
|
||||
"vite": "6.3.5",
|
||||
"wrangler": "^4.18.0"
|
||||
}
|
||||
}
|
15
packages/cloudflare-workers/open-gsio/tsconfig.json
Normal file
15
packages/cloudflare-workers/open-gsio/tsconfig.json
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "esnext",
|
||||
"lib": ["DOM", "DOM.Iterable", "ESNext"],
|
||||
"types": ["vite/client"],
|
||||
"module": "esnext",
|
||||
"esModuleInterop": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"strict": true,
|
||||
"allowJs": true,
|
||||
"skipLibCheck": true,
|
||||
"jsx": "react-jsx"
|
||||
},
|
||||
"exclude": ["*.test.ts"]
|
||||
}
|
6
packages/cloudflare-workers/open-gsio/vite.config.ts
Normal file
6
packages/cloudflare-workers/open-gsio/vite.config.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import {defineConfig} from "vite";
|
||||
import { cloudflare } from "@cloudflare/vite-plugin";
|
||||
|
||||
export default defineConfig({
|
||||
plugins: [cloudflare()]
|
||||
});
|
48
packages/cloudflare-workers/open-gsio/wrangler.jsonc
Normal file
48
packages/cloudflare-workers/open-gsio/wrangler.jsonc
Normal file
@@ -0,0 +1,48 @@
|
||||
{
|
||||
"$schema": "https://workers.cloudflare.com/sites/config-schema.json",
|
||||
"name": "open-gsio",
|
||||
"assets": {
|
||||
"binding": "ASSETS",
|
||||
"directory": "../../../packages/client/dist/client"
|
||||
},
|
||||
"dev": {
|
||||
"ip": "localhost",
|
||||
"port": 3001
|
||||
},
|
||||
"compatibility_date": "2025-05-28",
|
||||
"compatibility_flags": [
|
||||
"nodejs_compat"
|
||||
],
|
||||
"main": "main.ts",
|
||||
"preview_urls": false,
|
||||
"workers_dev": true,
|
||||
"kv_namespaces": [
|
||||
{
|
||||
"binding": "KV_STORAGE",
|
||||
// $ npx wrangler kv namespace create open-gsio
|
||||
"id": "placeholderId",
|
||||
// $ npx wrangler kv namespace create open-gsio --preview
|
||||
"preview_id": "placeholderIdPreview"
|
||||
}
|
||||
],
|
||||
"migrations": [
|
||||
{
|
||||
"new_classes": [
|
||||
"SiteCoordinator"
|
||||
],
|
||||
"tag": "v1"
|
||||
}
|
||||
],
|
||||
"durable_objects": {
|
||||
"bindings": [
|
||||
{
|
||||
"class_name": "ServerCoordinator",
|
||||
"name": "SERVER_COORDINATOR",
|
||||
"script_name": "open-gsio"
|
||||
}
|
||||
]
|
||||
},
|
||||
"observability": {
|
||||
"enabled": true
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user