Skip to content

Commit

Permalink
Merge pull request #11 from jsonjoy-com/ops
Browse files Browse the repository at this point in the history
Ops
  • Loading branch information
streamich authored Sep 20, 2024
2 parents 8e04672 + 3de07b7 commit b96d1ca
Show file tree
Hide file tree
Showing 7 changed files with 635 additions and 34 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"prettier:check": "prettier --ignore-path .gitignore --list-different 'src/**/*.{ts,tsx,js,jsx}'",
"lint": "yarn tslint",
"tslint": "tslint 'src/**/*.{js,jsx,ts,tsx}' -t verbose --project .",
"clean": "rimraf lib typedocs coverage gh-pages yarn-error.log db",
"clean": "rimraf lib typedocs coverage gh-pages yarn-error.log db dist",
"build": "tsc --project tsconfig.build.json --module commonjs --target es2020 --outDir lib",
"jest": "node -r ts-node/register ./node_modules/.bin/jest",
"test": "jest --maxWorkers 7",
Expand All @@ -55,6 +55,7 @@
"demo:e2e:sample-api:uws": "ts-node src/__demos__/sample-api/main-uws.ts",
"demo:e2e:json-crdt-server:http1": "ts-node src/__demos__/json-crdt-server/main-http1.ts",
"demo:e2e:json-crdt-server:uws": "ts-node src/__demos__/json-crdt-server/main-uws.ts",
"start:json-crdt-server:http1": "NODE_ENV=production PORT=80 JSON_CRDT_STORE=level pm2 start lib/__demos__/json-crdt-server/main-http1.js",
"coverage": "yarn test --collectCoverage",
"typedoc": "typedoc",
"build:pages": "rimraf gh-pages && mkdir -p gh-pages && cp -r typedocs/* gh-pages && cp -r coverage gh-pages/coverage",
Expand Down
23 changes: 14 additions & 9 deletions src/__demos__/json-crdt-server/main-http1.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
// Run: npx ts-node src/json-crdt-server/main-http1.ts
// curl localhost:9999/rpc -H 'Content-Type: rpc.rx.compact.json' -d '[1,1,"util.ping"]'

import {createCaller} from './routes';
import {Services} from './services/Services';
import {createCaller, createServices} from './routes';
import {RpcServer} from '../../server/http1/RpcServer';

export type JsonJoyDemoRpcCaller = ReturnType<typeof createCaller>['caller'];

const server = RpcServer.startWithDefaults({
port: +(process.env.PORT || 9999),
caller: createCaller(new Services()).caller,
logger: console,
});
const main = async () => {
const services = await createServices();
const server = RpcServer.startWithDefaults({
port: +(process.env.PORT || 9999),
caller: createCaller(services).caller,
logger: console,
});

// tslint:disable-next-line:no-console
console.log(server + '');
// tslint:disable-next-line:no-console
console.log(server + '');
};

// tslint:disable-next-line no-console
main().catch(console.error);
25 changes: 15 additions & 10 deletions src/__demos__/json-crdt-server/main-uws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@

import {App} from 'uWebSockets.js';
import {RpcApp} from '../../server/uws/RpcApp';
import {createCaller} from './routes';
import {Services} from './services/Services';
import {createCaller, createServices} from './routes';
import type {MyCtx} from './services/types';

export type JsonJoyDemoRpcCaller = ReturnType<typeof createCaller>['caller'];

const app = new RpcApp<MyCtx>({
uws: App({}),
caller: createCaller(new Services()).caller,
port: +(process.env.PORT || 9999),
});
app.startWithDefaults();
const main = async () => {
const services = await createServices();
const app = new RpcApp<MyCtx>({
uws: App({}),
caller: createCaller(services).caller,
port: +(process.env.PORT || 9999),
});
app.startWithDefaults();

// tslint:disable-next-line:no-console
console.log(app + '');
// tslint:disable-next-line:no-console
console.log(app + '');
};

// tslint:disable-next-line no-console
main().catch(console.error);
17 changes: 17 additions & 0 deletions src/__demos__/json-crdt-server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import {ObjectValueCaller} from '../../../common/rpc/caller/ObjectValueCaller';
import {system} from './system';
import {ObjectValue} from 'json-joy/lib/json-type-value/ObjectValue';
import {Services} from '../services/Services';
import {MemoryStore} from '../services/blocks/store/MemoryStore';
import {LevelStore} from '../services/blocks/store/level/LevelStore';
import {ClassicLevel} from 'classic-level';
import {Store} from '../services/blocks/store/types';
import type {RouteDeps} from './types';

export const createRouter = (services: Services) => {
Expand Down Expand Up @@ -32,3 +36,16 @@ export const createCaller = (services: Services = new Services()) => {
});
return {router, caller, services};
};

export const createServices = async () => {
let store: Store = new MemoryStore();
if (process.env.JSON_CRDT_STORE === 'level') {
const path = process.env.JSON_CRDT_STORE_PATH || './db';
const kv = new ClassicLevel<string, Uint8Array>(path, {valueEncoding: 'view'});
await kv.open();
store = new LevelStore(<any>kv);
await kv.close();
}
const services = new Services({store});
return services;
};
6 changes: 3 additions & 3 deletions src/__tests__/e2e/clients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const setupRpcPersistentClient = (codec: RpcCodec) => {
client.start();
const call = client.call.bind(client);
const call$ = client.call$.bind(client);
const stop = async () => void client.stop.bind(client);
const stop = async () => void client.stop();
return {client, call, call$, stop};
};

Expand All @@ -34,7 +34,7 @@ export const setupFetchRpcClient = (codec: RpcCodec) => {
});
const call = client.call.bind(client);
const call$ = client.call$.bind(client);
const stop = async () => void client.stop.bind(client);
const stop = async () => void client.stop();
return {client, call, call$, stop};
};

Expand Down Expand Up @@ -68,6 +68,6 @@ export const setupStreamingRpcClient = (codec: RpcCodec) => {
});
const call = client.call.bind(client);
const call$ = client.call$.bind(client);
const stop = async () => void client.stop.bind(client);
const stop = async () => void client.stop();
return {client, call, call$, stop};
};
2 changes: 1 addition & 1 deletion src/__tests__/e2e/json-crdt-server/clients.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ApiTestSetup, ApiTestSetupResult} from '../../../common/rpc/__tests__/runApiTests';
import {ApiTestSetup} from '../../../common/rpc/__tests__/runApiTests';
import {runUtilTests} from '../../json-crdt-server/util';
import {runPubsubTests} from '../../json-crdt-server/pubsub';
import {runPresenceTests} from '../../json-crdt-server/presence';
Expand Down
Loading

0 comments on commit b96d1ca

Please sign in to comment.