Skip to content

Conat -- replacing NATS with socketio + sqlite3 + valkey + nodejs #8346

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 227 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
227 commits
Select commit Hold shift + click to select a range
54d0708
nats: sketch out plan for persistence
williamstein May 11, 2025
54754a3
adding persistent streams
williamstein May 11, 2025
2a5dfe0
persist: implement transparent lz4 compression of binary message values
williamstein May 11, 2025
4ba2465
persist: switch to using zstd-napi for compression.
williamstein May 11, 2025
4b9ed3c
storage: update core storage implementation based on what we really need
williamstein May 12, 2025
4f0ef9c
persist: add support for key
williamstein May 12, 2025
07bebf1
persist: get core client/server connection working (similar to change…
williamstein May 12, 2025
e5d8d68
persistence: figure out how commands will work
williamstein May 12, 2025
5037d40
Merge branch 'master' into persist
williamstein May 12, 2025
a87cad6
fix a course error message
williamstein May 12, 2025
0d1cac7
persist storage: more usable client-facing api
williamstein May 12, 2025
a5bb9e9
persist: initial proof of concept integration with ephemeral stream
williamstein May 13, 2025
9416c04
add persist support to dstream
williamstein May 13, 2025
b7681da
persist: store database files in a more organized way
williamstein May 13, 2025
5398935
persist: rename sqlite -->` context since it's lots of context
williamstein May 13, 2025
a37bb21
persist: use Buffer properly
williamstein May 13, 2025
59e55ee
socketio-nats: proof of concept
williamstein May 13, 2025
04438b2
socketio-nats-server: start writing the server side
williamstein May 13, 2025
8148a1e
conat: add socketio cluster support
williamstein May 13, 2025
7940a2e
conat-server: better org
williamstein May 13, 2025
fbc72bd
conat: implement transparent message chunking!
williamstein May 14, 2025
5cc4718
conat: implement request/respond and greatly improve async iterator
williamstein May 14, 2025
ba26028
conats: implement requestMany
williamstein May 14, 2025
fbff86b
conat: implement the queue group pattern
williamstein May 14, 2025
c8b71c4
conats: work in progress
williamstein May 14, 2025
9499d38
Merge branch 'master' into conat
williamstein May 14, 2025
3c578c2
conat: clean up server sub handling; organize backend connecting
williamstein May 14, 2025
1478ee7
add conat connection to getEnv (for nats)
williamstein May 14, 2025
cba746e
conat: rewrite tiered-storage
williamstein May 14, 2025
f590c2c
conat: implement first round of how auth will work
williamstein May 14, 2025
66b8639
conat: instructions/test of uWebsocket.js
williamstein May 14, 2025
ed3fc4f
Merge branch 'master' into conat
williamstein May 14, 2025
e24c9db
conat: wildcard subjects
williamstein May 14, 2025
76a087a
conats: implement tiered storage properly again using wildcards
williamstein May 14, 2025
78c8b84
conats: automate ensuring all subscriptions are in sync on reconnect;…
williamstein May 15, 2025
340fc3d
conats: implement full permissions model; implement optional confirma…
williamstein May 15, 2025
d99b98c
conat: experimenting with using valkey for clustering
williamstein May 15, 2025
de36c27
conat: refactor auth
williamstein May 15, 2025
b50027f
conat: sync interest graph between socket.io servers
williamstein May 15, 2025
744b279
Merge branch 'master' into conat
williamstein May 15, 2025
efc0690
conat: implement connectionStateRecovery by default
williamstein May 15, 2025
c2fd3bd
conats: completely migrate the hub-api over to use it... and it just …
williamstein May 15, 2025
bb1c7b9
add waitUntilConnected to conat client
williamstein May 16, 2025
ea0c63b
conat: switch basic project api over to use conat
williamstein May 16, 2025
5edeb9a
conat: enable websocket compression
williamstein May 16, 2025
f31fec2
conat: rewrite llm server
williamstein May 16, 2025
deda6c7
conats: rewrite nats/services
williamstein May 16, 2025
d9ca3bc
conat: add subscriber count checking
williamstein May 16, 2025
1e3ca61
conat: service clean up and get them to use listener awareness
williamstein May 16, 2025
94b9b7e
conat: requestMany option no longer needed
williamstein May 16, 2025
cbb900a
conat: starting to update changefeeds
williamstein May 16, 2025
0f220d4
conat: switch changefeeds to use conat
williamstein May 16, 2025
f912150
conat: cursors (via pubsub)
williamstein May 16, 2025
2ab3e7a
conat: rewrite ephemeral streams
williamstein May 16, 2025
582c721
improve conats error messages, including adding an error code
williamstein May 17, 2025
c739fe5
conat: work in progress rewriting primus compat layer
williamstein May 17, 2025
3a7ba8e
conat: migrate primus api compat to conat, and also fix it so it works
williamstein May 17, 2025
bf9f57c
add conat-watch in backend
williamstein May 17, 2025
9f433be
conats: rewrite streaming file upload/download
williamstein May 17, 2025
b6b58c2
conat: rewrite persist to use conat instead of nats
williamstein May 17, 2025
5cae850
conat: delete browser api -- aren't using it
williamstein May 17, 2025
3af83bf
conat -- change msg to hold the raw data and decode only when requested
williamstein May 17, 2025
454ee30
conat: work in progress improving persistent storage data structure t…
williamstein May 17, 2025
efd7e70
conat: rewrite persist to make sense in terms of what data is stored …
williamstein May 17, 2025
7a6d33d
conat: get ephemeral streams to work again
williamstein May 17, 2025
8c95b53
conat: switch to using it for patches file editing by default
williamstein May 17, 2025
193d1bf
conat: rename ephemeral-stream --> conat-stream; add some kv function…
williamstein May 18, 2025
cbe6a43
conat rename: nats/server -> nats/core; also add persist server to au…
williamstein May 18, 2025
c69d521
conats: start writing unit tests
williamstein May 18, 2025
cebd586
dramatically speed up jest testing follwing https://github.com/jestjs…
williamstein May 18, 2025
4c52769
conat: improve basic tests
williamstein May 18, 2025
f297630
conat: write more tests and finally find a bug
williamstein May 18, 2025
cdea269
conats: more tests
williamstein May 18, 2025
adbd65c
conat: add some connection/reconnect tests
williamstein May 18, 2025
e362aa4
conat: more connectivity testing/improvements
williamstein May 18, 2025
40a157c
conat: write some overview documentation
williamstein May 18, 2025
ea71294
conat: remove calls to confirm that are no longer needed
williamstein May 18, 2025
72afc46
conats: rewrite how request/reply works to improve efficiency and fix…
williamstein May 18, 2025
3e4fc8a
conats: move "conat-stream" to "core-stream", which is finally the ri…
williamstein May 18, 2025
dc704f1
conat: start adding unit tests of core-stream
williamstein May 18, 2025
da69ce3
conats: core-stream -- test ephemeral leader basics
williamstein May 18, 2025
f942611
conat: more ephemeral stream testing
williamstein May 19, 2025
a1bef8a
conat: deal with some edge cases involving null and core stream
williamstein May 19, 2025
e545fc0
conat: more basic tests
williamstein May 19, 2025
d22b6a1
conat: implement sending entire state of ephemeral stream in one mess…
williamstein May 19, 2025
40a80a2
conat: more unit tests of dstream
williamstein May 19, 2025
db75469
conats: some clarity on undefined v null
williamstein May 19, 2025
2c03ad1
rename: @cocalc/nats --> @cocalc/conat
williamstein May 19, 2025
2dd553a
Merge branch 'master' into conat
williamstein May 19, 2025
41e9871
conat: rename packages/backend/nats to packages/backend/conat
williamstein May 19, 2025
0be0bd9
nats --> conat rename
williamstein May 19, 2025
d5024e9
conat: put all core testing together in backend/conat/test again
williamstein May 19, 2025
7c0ff55
conat: migrating time test; nailing down order of subscription startup
williamstein May 19, 2025
1c3394f
conat: fix llm test
williamstein May 19, 2025
e0eeb09
conat: basic service test
williamstein May 19, 2025
623f036
more nats -> conat renaming
williamstein May 19, 2025
b5fea3c
conat -- more service unit tests
williamstein May 19, 2025
6295b00
conat: more service unit tests
williamstein May 19, 2025
288702d
conat: more service unit tests
williamstein May 19, 2025
f9d0ace
conat: all tests updated except the sync ones
williamstein May 19, 2025
52016ff
conat: update dstream-ephemeral tests (and fix a bug)
williamstein May 20, 2025
1492393
conat dstream: fix some bugs and also start getting testing framework…
williamstein May 20, 2025
19bb6cc
conat: get dstream tests working
williamstein May 20, 2025
6ef2225
conat core-stream: implement loading more in persistent case, includi…
williamstein May 20, 2025
0392521
conat: core-stream -- raw doesn't have to be an array anymore
williamstein May 20, 2025
f00e732
conat: completely delete the NATS jetstream client code -- it's now
williamstein May 20, 2025
78c57a0
conat: more nats --> conat in the sync package
williamstein May 20, 2025
9cbd416
Merge branch 'master' into conat
williamstein May 20, 2025
92b9153
conat: mainly more nats --> conat renaming
williamstein May 20, 2025
7a3b2b1
conat: starting very basic kv support
williamstein May 20, 2025
d8f89ad
conat: adding a little more kv support
williamstein May 20, 2025
213dccc
conat: implement kv garbage collection
williamstein May 20, 2025
ac45d69
conat: implement deleting keys properly from core-stream
williamstein May 20, 2025
b07e33d
conat: implement kv previousSeq support for persistence
williamstein May 20, 2025
7f88669
conat: unit tests for general-dkv
williamstein May 21, 2025
dccdb31
conat: merge dkv and general-dkv
williamstein May 21, 2025
1d5463a
conat: get all dkv tests working :-)
williamstein May 21, 2025
49e1db4
conat: implement core stream renewal
williamstein May 21, 2025
65eadcf
fix typescript issue
williamstein May 21, 2025
82ea3d6
typescript issue
williamstein May 21, 2025
673aec3
another one
williamstein May 21, 2025
2e204c5
conat: get cocalc to start without requiring nats to be running
williamstein May 21, 2025
115667d
conat: update connection indicator
williamstein May 21, 2025
bda9ea3
delete nats auth callout -- about two weeks of HARD work. poof.
williamstein May 21, 2025
c4e7aee
nats --> conat: mainly what hub starts and uses; delete insane fronte…
williamstein May 21, 2025
1e64b7c
uninstall all nats packages from @cocalc/server; delete the entire ti…
williamstein May 21, 2025
33a3bc4
uninstall nats from frontend
williamstein May 21, 2025
970a7e4
uninstall nats from project
williamstein May 21, 2025
30f7825
deleting more nats code
williamstein May 21, 2025
d629be0
deleting more nats code
williamstein May 21, 2025
88c8af7
conat: rewrite akv to use conat and new persistence system
williamstein May 21, 2025
8bf3c81
conat async kv -- add ability to get all keys
williamstein May 21, 2025
3f74935
add ability to run any sqlite command to the persistent stream
williamstein May 21, 2025
b39eedd
improve akv unit tests
williamstein May 21, 2025
fcc9960
get all backend conats tests to pass; delete general-kv and kv (which…
williamstein May 21, 2025
c60df0a
delete the last traces of @nats-io from our codebase, I think
williamstein May 21, 2025
2d60f9f
fix some version mismatches
williamstein May 21, 2025
c73d433
skip long running async job test -- it screws up parallel unit testin…
williamstein May 21, 2025
82b387e
make backend unit tests more robust
williamstein May 21, 2025
02c85ba
typescript issue
williamstein May 21, 2025
ccef6df
fix running server tests (was grabbing .js files in dist/)
williamstein May 21, 2025
02e6830
fix typing issue
williamstein May 21, 2025
9ea0589
more ts fixes
williamstein May 21, 2025
e372b04
more test robustness improvement
williamstein May 21, 2025
9233cb8
Merge branch 'conat' of github.com:sagemathinc/cocalc into conat
williamstein May 21, 2025
905532d
fix new ts error
williamstein May 21, 2025
7e21f83
always build file-server (so tests do not run)
williamstein May 21, 2025
776659d
another ts issue
williamstein May 21, 2025
c4a6dad
update http-proxy-3
williamstein May 21, 2025
eebaa8c
make a test more robust
williamstein May 21, 2025
1604a98
github actions -- test with latest node 18 and node 20
williamstein May 21, 2025
a4d97e9
Merge branch 'master' into conat
williamstein May 21, 2025
fe06b9d
conat remove cluster support (in favor of valkey)
williamstein May 22, 2025
6cb4965
address a weird ts issue
williamstein May 22, 2025
4e274ab
remove nats-server startup from github action workflow
williamstein May 22, 2025
4837b64
remove socketio cluster support libs (not needed)
williamstein May 22, 2025
2740190
conat -- disable the general sqlite3 support for persist for now, of …
williamstein May 22, 2025
ad91330
sqlite test
williamstein May 22, 2025
0967147
conat: ephemeral subscriptions; use for services by default; also add…
williamstein May 22, 2025
641c994
conat: implement per user subscription limit
williamstein May 22, 2025
10abf2f
conat: persistent streams -- send results in batches
williamstein May 23, 2025
d58d2ba
persist send: cap total size of messages
williamstein May 23, 2025
fe794d1
conat: delete wrong msgID implementation; fix a flaky test
williamstein May 23, 2025
1edc789
conat: in persistent storage, properly reference count the stream
williamstein May 23, 2025
e448e16
conat: support msgID properly for persistent storage
williamstein May 23, 2025
1e706a5
conat: implement timeout for publish
williamstein May 23, 2025
aa777b1
conat: implement timeout for creation of subscription
williamstein May 23, 2025
c53b747
unit test msgID
williamstein May 23, 2025
07f4740
conat: implement persistent stream message delete
williamstein May 23, 2025
cee76b1
conat: finish implementing and unit testing stream delete
williamstein May 23, 2025
ccb7374
conat: persistent storage -- add api for setting/getting limits
williamstein May 23, 2025
d475126
dstream and dkv - ability to set/get limits through them
williamstein May 23, 2025
8a9f922
conat: limits --> config
williamstein May 23, 2025
e314f61
conat persistence: add storing the size of messages
williamstein May 23, 2025
efd43d9
conat persist: start implementing limits
williamstein May 23, 2025
9654054
conat: implement max_age
williamstein May 23, 2025
48fd264
conat: work in progress on implementing limits
williamstein May 24, 2025
4d3ff9d
conat: working on limits
williamstein May 24, 2025
0daea3b
conat: add typing to core-stream event; add msgID so it gets properly…
williamstein May 24, 2025
110a43a
conat: ttl for deleting keys from kv store
williamstein May 24, 2025
635050f
conat: more work on limits; fix issue with dkv state and saving where…
williamstein May 24, 2025
b2ae05f
fix all skipped conat tests
williamstein May 24, 2025
8a172ed
conat: *all* limits are implemented and unit tested. Wow.
williamstein May 24, 2025
9250406
conat: ephemeral dstream and dkv with working tests
williamstein May 24, 2025
b59275a
conat: implement heartbeat properly for core-stream (work in progress)
williamstein May 24, 2025
9085730
conat: delete old ephemeral leader-based ephemeral stream code
williamstein May 24, 2025
3828c77
Merge branch 'conat-hb' into conat
williamstein May 24, 2025
200d0a8
no need for printing a warning
williamstein May 24, 2025
d28dae4
Merge branch 'master' into conat
williamstein May 24, 2025
9ab4a64
terminal: properly implement write buffering (so very rarely have to …
williamstein May 24, 2025
a92f3a4
dstream options -- clean up
williamstein May 24, 2025
427a5b0
conat: add astream = async stream... for symmetry
williamstein May 24, 2025
a5a5750
jupyter/sync: use ephemeral dkv for ipywidgets, etc
williamstein May 24, 2025
1401fbd
delete console log message
williamstein May 25, 2025
25a2c42
Merge branch 'master' into conat
williamstein May 25, 2025
fe90a6d
conat: it turns out to be easy to have multiple subscriptions to same…
williamstein May 25, 2025
da95f9e
conat: make core stream much more robust to issues and add unit tests…
williamstein May 25, 2025
579adf4
ephemeral size cap
williamstein May 25, 2025
0e506f0
conat: implement permissions for persistent storage
williamstein May 25, 2025
b660628
fix subtle case of user query that broke due to JSON --> MsgPack switch
williamstein May 25, 2025
b56cff9
tests accessing private members
williamstein May 26, 2025
43f53b6
oops that validate was obviously wrong
williamstein May 26, 2025
9d2fc21
conat: redo how address of conat server is specified/stored/etc.
williamstein May 26, 2025
02a73b6
oops -- left debugging in
williamstein May 26, 2025
923ce25
conat: fix pub/sub (hence cursors)
williamstein May 26, 2025
12dfc6c
fix version mismatch with ttlcache
williamstein May 26, 2025
d38c5ca
conat client -- do allow self-signed cert for nodejs connections
williamstein May 26, 2025
e0f3190
fix computing address on frontend in some cases
williamstein May 26, 2025
f3f142f
improve database error message
williamstein May 26, 2025
2de9c8e
frontend typescript issue
williamstein May 26, 2025
ac73f2b
upgrade rspack
williamstein May 26, 2025
ff4f5b5
do not unset BASE_PATH
williamstein May 26, 2025
71f63d4
add ssl option for running hub
williamstein May 26, 2025
ea7caea
easy access to the core sync primitives from conat core client
williamstein May 26, 2025
cb1066f
typescript error
williamstein May 26, 2025
6550ead
do not show user as anonymous sometimes for a moment
williamstein May 26, 2025
acf73ee
conat: improve some logging
williamstein May 26, 2025
82d7d84
conat: eliminate account_id cookie
williamstein May 26, 2025
4931689
Revert "conat: eliminate account_id cookie"
williamstein May 27, 2025
4a956cd
conat auth: implementing it; added many unit tests
williamstein May 27, 2025
1f20fc3
conat: implement auth for hub
williamstein May 27, 2025
b956cf0
conat: implement auth
williamstein May 27, 2025
7305dcb
conat: just working on auth
williamstein May 27, 2025
312087a
conat core: do much more to surface permissions issues; specify inbox…
williamstein May 27, 2025
240ab55
project: generate project secret token in hub and set when starting p…
williamstein May 27, 2025
2748130
conat: make secret_token available (e.g., used by video chat)
williamstein May 27, 2025
3a5973d
conat: strip undefines before doing userQuery -- needed due to MsgPack
williamstein May 27, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 2 additions & 2 deletions .github/workflows/make-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ jobs:
matrix:
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
node-version:
- "18.17.1"
- "18"
- "20"
pg-version:
- "13.12"
- "16"
Expand Down Expand Up @@ -93,5 +94,4 @@ jobs:

- run: cd src && npm install -g pnpm
- run: cd src && pnpm run make
- run: cd src && pnpm nats-server-daemon
- run: cd src && pnpm run test
2 changes: 1 addition & 1 deletion src/compute/compute/lib/filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { apiCall } from "@cocalc/api-client";
import sendFiles from "./send-files";
import getFiles from "./get-files";
// ensure that the nats client is initialized so that syncfs can connect to nats properly.
import "@cocalc/project/nats";
import "@cocalc/project/conat";

const logger = getLogger("compute:filesystem");

Expand Down
2 changes: 1 addition & 1 deletion src/compute/compute/lib/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ The manager does the following:
*/

import debug from "debug";
import startProjectServers from "@cocalc/project/nats";
import startProjectServers from "@cocalc/project/conat";
import { pingProjectUntilSuccess, waitUntilFilesystemIsOfType } from "./util";
import { apiCall, project } from "@cocalc/api-client";

Expand Down
2 changes: 1 addition & 1 deletion src/compute/compute/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"@cocalc/backend": "workspace:*",
"@cocalc/compute": "link:",
"@cocalc/jupyter": "workspace:*",
"@cocalc/nats": "workspace:*",
"@cocalc/conat": "workspace:*",
"@cocalc/project": "workspace:*",
"@cocalc/sync": "workspace:*",
"@cocalc/sync-client": "workspace:*",
Expand Down
1 change: 1 addition & 0 deletions src/compute/conat
1 change: 0 additions & 1 deletion src/compute/nats

This file was deleted.

6 changes: 3 additions & 3 deletions src/compute/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/compute/pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ packages:
- backend
- comm
- jupyter
- nats
- conat
- project
- sync
- sync-fs
Expand Down
11 changes: 2 additions & 9 deletions src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,8 @@
"test-parallel": "unset DEBUG && pnpm run version-check && cd packages && pnpm run -r --parallel test",
"test": "unset DEBUG && pnpm run version-check && cd packages && pnpm run -r test",
"prettier-all": "cd packages/",
"nats-server": "cd ${COCALC_ROOT:=$INIT_CWD}/packages/backend && node -e \"require('@cocalc/backend/nats/install').main()\" && node -e \"require('@cocalc/backend/nats/conf').main()\" && node -e \"require('@cocalc/backend/nats/server').main()\"",
"build-nats": "cd ${COCALC_ROOT:=$INIT_CWD}/packages/util && pnpm install && pnpm build && cd ${COCALC_ROOT:=$INIT_CWD}/packages/nats && pnpm install && pnpm build && cd ${COCALC_ROOT:=$INIT_CWD}/packages/backend && pnpm install && pnpm build",
"nats-server-ci": "pnpm run build-nats && cd ${COCALC_ROOT:=$INIT_CWD}/packages/backend && node -e \"require('@cocalc/backend/nats/install').main()\" && node -e \"require('@cocalc/backend/nats/conf').main()\" && node -e \"require('@cocalc/backend/nats/server').main()\"",
"nats-server-daemon": "cd ${COCALC_ROOT:=$INIT_CWD}/packages/backend && node -e \"require('@cocalc/backend/nats/install').main()\" && node -e \"require('@cocalc/backend/nats/conf').main()\" && node -e \"require('@cocalc/backend/nats/server').main({daemon:true})\"",
"nats-server-verbose": "cd ${COCALC_ROOT:=$INIT_CWD}/packages/backend && node -e \"require('@cocalc/backend/nats/install').main()\" && node -e \"require('@cocalc/backend/nats/conf').main()\" && node -e \"require('@cocalc/backend/nats/server').main({verbose:true})\"",
"nats-cli": "cd ${COCALC_ROOT:=$INIT_CWD}/packages/backend && node -e \"require('@cocalc/backend/nats/cli').main()\"",
"nats-sys": "cd ${COCALC_ROOT:=$INIT_CWD}/packages/backend && node -e \"require('@cocalc/backend/nats/cli').main({user:'sys'})\"",
"nats-tiered-storage": "cd ${COCALC_ROOT:=$INIT_CWD}/packages/server && DEBUG=cocalc:* DEBUG_CONSOLE=yes node -e \"require('@cocalc/server/nats/tiered-storage').init()\"",
"local-ci": "./scripts/ci.sh"
"local-ci": "./scripts/ci.sh",
"conat-server": "cd packages/server && pnpm conat-server"
},
"repository": {
"type": "git",
Expand Down
15 changes: 15 additions & 0 deletions src/packages/backend/auth/cookie-names.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,18 @@ export const ACCOUNT_ID_COOKIE_NAME = basePathCookieName({
basePath,
name: "account_id",
});

export const PROJECT_SECRET_COOKIE_NAME = basePathCookieName({
basePath,
name: "project_secret",
});

export const PROJECT_ID_COOKIE_NAME = basePathCookieName({
basePath,
name: "project_id",
});

export const HUB_PASSWORD_COOKIE_NAME = basePathCookieName({
basePath,
name: "hub_password",
});
10 changes: 10 additions & 0 deletions src/packages/backend/bin/conat-watch.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { getEnv } = require('@cocalc/backend/conat')

async function main() {
const subject = process.argv[2] ?? '>';
console.log("watching ", {subject})
const {cn} = await getEnv()
cn.watch(subject)
}

main();
21 changes: 21 additions & 0 deletions src/packages/backend/conat/conat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { conatPassword, conatServer } from "@cocalc/backend/data";
import {
connect as connect0,
Client,
type ClientOptions,
} from "@cocalc/conat/core/client";
import { HUB_PASSWORD_COOKIE_NAME } from "@cocalc/backend/auth/cookie-names";
import { inboxPrefix } from "@cocalc/conat/names";

export type { Client };

export function connect(options?: ClientOptions): Client {
return connect0({
address: conatServer,
inboxPrefix: inboxPrefix({ hub_id: "hub" }),
extraHeaders: {
Cookie: `${HUB_PASSWORD_COOKIE_NAME}=${conatPassword}`,
},
...options,
});
}
9 changes: 9 additions & 0 deletions src/packages/backend/conat/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { sha1 } from "@cocalc/backend/sha1";
import { connect as getConatClient } from "./conat";

export async function getEnv(options?) {
const jc = null as any;
const nc = null as any;
const cn = getConatClient(options);
return { nc, jc, cn, sha1 };
}
12 changes: 12 additions & 0 deletions src/packages/backend/conat/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import getLogger from "@cocalc/backend/logger";
import { getEnv } from "./env";
export { getEnv };
import { setConatClient } from "@cocalc/conat/client";
import getConnection from "@cocalc/backend/conat/persistent-connection";

export { getConnection };

export function init() {
setConatClient({ getNatsEnv: getEnv, getLogger });
}
init();
21 changes: 21 additions & 0 deletions src/packages/backend/conat/persist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import "./index";
import betterSqlite3 from "better-sqlite3";
import { initContext } from "@cocalc/conat/persist/context";
import { compress, decompress } from "zstd-napi";
import { syncFiles } from "@cocalc/backend/data";
import ensureContainingDirectoryExists from "@cocalc/backend/misc/ensure-containing-directory-exists";

initContext({
betterSqlite3,
compress,
decompress,
syncFiles,
ensureContainingDirectoryExists,
});

export { pstream } from "@cocalc/conat/persist/storage";
export {
init as initServer,
terminate as terminateServer,
} from "@cocalc/conat/persist/server";
export { getAll, set, get } from "@cocalc/conat/persist/client";
42 changes: 42 additions & 0 deletions src/packages/backend/conat/persistent-connection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Create a nats connection that doesn't break.

The NATS docs

https://github.com/nats-io/nats.js/blob/main/core/README.md#connecting-to-a-nats-server

ensure us that "the client will always attempt to reconnect if the connection is
disrupted for a reason other than calling close()" but THAT IS NOT TRUE.
(I think the upstream code in disconnected in nats.js/core/src/protocol.ts is a lazy
and I disagree with it. It tries to connect but if anything goes slightly wrong,
just gives up forever.)

There are definitely situations where the connection gets permanently closed
and the close() function was not called, at least not by any of our code.
I've given up on getting them to fix or understand their bugs in general:

https://github.com/williamstein/nats-bugs/issues/8

We thus monitor the connection, and if it closed, we *swap out the protocol
object*, which is an evil hack to reconnect. This seems to work fine with all
our other code.

All that said, it's excellent that the NATS library separates the protocol from
the connection object itself, so it's possible to do this at all! :-)
*/

import { reuseInFlight } from "@cocalc/util/reuse-in-flight";
import type { NatsConnection } from "@cocalc/conat/types";

export function setConnectionOptions(_: any) {}

// gets the singleton connection
const getConnection = reuseInFlight(async (): Promise<NatsConnection> => {
return null as any;
});

export default getConnection;

export async function getNewConnection(): Promise<NatsConnection> {
return null as any;
}
40 changes: 40 additions & 0 deletions src/packages/backend/conat/sync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {
dstream as createDstream,
type DStream,
type DStreamOptions as DstreamCreateOptions,
} from "@cocalc/conat/sync/dstream";
import { dkv as createDKV, type DKV, type DKVOptions } from "@cocalc/conat/sync/dkv";
import { dko as createDKO, type DKO } from "@cocalc/conat/sync/dko";
import { akv as createAKV, type AKV } from "@cocalc/conat/sync/akv";
import { astream as createAStream, type AStream } from "@cocalc/conat/sync/astream";
import { createOpenFiles, type OpenFiles } from "@cocalc/conat/sync/open-files";
export { inventory } from "@cocalc/conat/sync/inventory";
import "./index";

export type { DStream, DKV, DKO, AKV };

export async function dstream<T = any>(
opts: DstreamCreateOptions,
): Promise<DStream<T>> {
return await createDstream<T>(opts);
}

export function astream<T = any>(opts: DstreamCreateOptions): AStream<T> {
return createAStream<T>(opts);
}

export async function dkv<T = any>(opts: DKVOptions): Promise<DKV<T>> {
return await createDKV<T>(opts);
}

export function akv<T = any>(opts: DKVOptions): AKV<T> {
return createAKV<T>(opts);
}

export async function dko<T = any>(opts: DKVOptions): Promise<DKO<T>> {
return await createDKO(opts);
}

export async function openFiles(project_id: string, opts?): Promise<OpenFiles> {
return await createOpenFiles({ project_id, ...opts });
}
Loading
Loading