Historical repository. SmartFlowSocial no longer uses this worker. The maintained utility is nostrmash/nostr-read-gateway. That project has its own Git history and does not inherit this one. This repository stays available as a reference. It has not been transferred or archived.
This tree is the older Cloudflare Worker that routed Primal-compatible reads through a fixed upstream order. It is not an application backend anymore.
Related historical pieces: app, publisher, Blastr, Blossom.
The GitHub repository is smartflowsocial-cache-worker. The Cloudflare script name in wrangler.toml stays smartflowsocial-primal-cache-worker.
- HTTP POST interface for cache queries
- Native NostrMash reads for profiles, profile batches, events, and zap-free threads, translated back into Primal
EVENTframes - Fallback to the NostrMash Primal WebSocket compatibility socket, then to Primal Cache
- CORS support for browser-based applications
- Support for 80+ Primal cache request types
- Automatic connection retry with timeout handling
- Request bodies are limited to 256 KiB while they are read. An oversized
Content-Lengthis rejected before the body is read. A missing or smaller length is not trusted.
- Node.js 26.13.1+ and pnpm installed
- Cloudflare account
- Wrangler (via devDependencies; or install globally with
pnpm add -g wrangler)
# Install dependencies
pnpm install
# Login to Cloudflare
pnpm exec wrangler login
# Deploy the worker
pnpm run deploySend a POST request to your worker URL with the following JSON body:
{
"requestType": "net_stats"
}curl -X POST https://your-worker.workers.dev \
-H "Content-Type: application/json" \
-d '{
"requestType": "thread_view",
"params": {
"event_id": "202cfc372f96c585cd9653a612a4274bfd4ac566a77b1d4dd235c5db24e003e8",
"limit": 100
}
}'const response = await fetch('https://your-worker.workers.dev', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
requestType: 'user_profile',
params: {
pubkey: '64-hex-pubkey-here',
},
}),
});
const data = await response.json();
console.log(data);The worker accepts the 89 requestType values in src/data/primal-request-types.json. Examples and the full list are in Request types.
The worker returns a Primal-shaped array of messages. NostrMash native HTTP answers are translated into that array. A NostrMash WebSocket or Primal answer is already in this shape:
[
["EVENT", "subscription-id", { "kind": 1, "content": "hello" }],
["EOSE", "subscription-id"]
]The last message is always ["EOSE", "subscription-id"] indicating "End Of Stored Events".
200- Success400- Bad request (invalid JSON, validation / unknownrequestType, etc.)405- Method not allowed (only POST is accepted)413- Request body larger than 256 KiB502- Bad gateway (upstream error, malformed upstream payload, or all endpoints failed with non-timeout errors)504- Gateway timeout (connect or response timeout on every attempted endpoint)500- Unexpected internal error
Responses include an X-Request-Id header (echoed from the client X-Request-Id when valid, otherwise generated) for log correlation.
Successful responses also include X-Cache-Source:
nostrmash-native— NostrMash/api/v1, reshaped into PrimalEVENT/EOSEframesnostrmash-ws— NostrMashwss://…/primal/ws/v1primal— a Primal cache socket
{
"error": "Error message here"
}Supported Primal requestType values are maintained in src/data/primal-request-types.json (validated before any upstream WebSocket is opened).
flowchart LR
client[Client] --> worker[Cache worker]
worker --> http[NostrMash HTTP]
worker --> ws[NostrMash WebSocket]
worker --> primal[Primal Cache]
For each POST, the worker tries:
- NostrMash native HTTP (
NOSTRMASH_HTTP_BASE, defaulthttps://api.nostrmash.com) foruser_profile,user_infos,events, andthread_view. The JSON is translated into the sameEVENT/EOSEarray SmartFlowSocial already parses. A thread with zap receipts is not answered here, because/api/v1/threadsdoes not include kind9735receipts. - NostrMash WebSocket compatibility (
NOSTRMASH_WS_URL, defaultwss://api.nostrmash.com/primal/ws/v1). This is the Primal iOS cache protocol. The older/primal/wssocket is not used: its profile payload is not a kind0event. - Primal Cache, in order:
wss://cache.smartflowsocial.com/v1,wss://cache1.primal.net/v1,wss://cache2.primal.net/v1.
A tier is skipped when it is unreachable, returns an error, or returns no events SmartFlowSocial can use. event_actions stays on Primal unless a tier returns events of the requested kind. NostrMash answers that call with profile metadata, which would hide likes and reposts.
Each socket uses a 3-second connect timeout and a 5-second response timeout. Native HTTP calls use a 2-second timeout (2.5 seconds for threads).
The worker includes CORS headers allowing requests from any origin:
Access-Control-Allow-Origin: *Access-Control-Allow-Methods: GET, POST, OPTIONSAccess-Control-Allow-Headers: Content-Type, X-Request-IdAccess-Control-Expose-Headers: X-Request-Id, X-Cache-Source
# Run in development mode
pnpm run devThis will start a local server using Wrangler for testing.
pnpm run validateRuns Prettier (check), ESLint, TypeScript, cf-types:check, and Vitest. After changing wrangler.toml or bindings, run pnpm run cf-types and commit worker-configuration.d.ts if it changes.
.
├── src/
│ ├── index.ts # Worker fetch handler and upstream chain
│ ├── native-nostrmash.ts # NostrMash `/api/v1` translation
│ ├── cache-socket.ts # One Primal-style cache WebSocket call
│ ├── frames.ts # EVENT frame helpers
│ ├── body-schema.ts # Zod validation for POST JSON
│ ├── http-error.ts # Typed HTTP errors
│ ├── request-types.ts # Loads allowlist JSON
│ └── data/
│ └── primal-request-types.json # Supported Primal requestType values
├── test/ # Vitest + @cloudflare/vitest-plugin
├── worker-configuration.d.ts # Generated: pnpm run cf-types
├── wrangler.toml
├── package.json
├── CONTRIBUTING.md
└── README.md
Released into the public domain under the Unlicense. This project follows the Code of Conduct. Report security issues through SECURITY.md.
See CONTRIBUTING.md. Pull requests are welcome.
For issues and questions, please open an issue on the GitHub repository.