Skip to content

Commit

Permalink
extract server util to secsync-server
Browse files Browse the repository at this point in the history
  • Loading branch information
nikgraf committed Jun 1, 2024
1 parent eb3d364 commit bb14413
Show file tree
Hide file tree
Showing 24 changed files with 268 additions and 148 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
There is a changelog available for each package:

- [secsync](./packages/secsync/CHANGELOG.md)
- [secsync-server](./packages/secsync-server/CHANGELOG.md)
- [secsync-react-yjs](./packages/secsync-react-yjs/CHANGELOG.md)
- [secsync-react-automerge](./packages/secsync-react-automerge/CHANGELOG.md)
- [secsync-react-devtool](./packages/secsync-react-devtool/CHANGELOG.md)
Expand Down
2 changes: 1 addition & 1 deletion documentation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"devDependencies": {
"@svgr/webpack": "^8.1.0",
"@types/libsodium-wrappers": "^0.7.14",
"@types/node": "20.11.30",
"@types/node": "^20.13.0",
"autoprefixer": "^10.4.19",
"postcss": "^8.4.38",
"tailwindcss": "^3.4.3",
Expand Down
2 changes: 1 addition & 1 deletion documentation/pages/docs/getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ Secsync ships with a backend utility that handles a Websocket connection includi
- hasBroadcastAccess

```tsx
import { createWebSocketConnection } from "secsync";
import { createWebSocketConnection } from "secsync-server";
import { WebSocketServer } from "ws";

const webSocketServer = new WebSocketServer();
Expand Down
3 changes: 2 additions & 1 deletion examples/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.0",
"private": true,
"devDependencies": {
"@types/node": "20.12.7",
"@types/node": "^20.13.0",
"@types/uuid": "^9.0.8",
"@types/ws": "^8.5.10",
"@vercel/ncc": "^0.38.1",
Expand Down Expand Up @@ -32,6 +32,7 @@
"libsodium-wrappers": "^0.7.13",
"make-promises-safe": "^5.1.0",
"secsync": "workspace:^",
"secsync-server": "workspace:^",
"uuid": "^9.0.1",
"ws": "^8.16.0"
},
Expand Down
2 changes: 1 addition & 1 deletion examples/backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ require("make-promises-safe"); // installs an 'unhandledRejection' handler
import cors from "cors";
import express from "express";
import { createServer } from "http";
import { createWebSocketConnection } from "secsync";
import { createWebSocketConnection } from "secsync-server";
import { WebSocketServer } from "ws";
import { createSnapshot as createSnapshotDb } from "./database/createSnapshot";
import { createUpdate as createUpdateDb } from "./database/createUpdate";
Expand Down
14 changes: 14 additions & 0 deletions packages/secsync-server/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [0.3.0] - 2024-06-01

### Added

- Initial version
6 changes: 6 additions & 0 deletions packages/secsync-server/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
presets: [
["@babel/preset-env", { targets: { node: "current" } }],
"@babel/preset-typescript",
],
};
12 changes: 12 additions & 0 deletions packages/secsync-server/package-json-build-script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const fs = require("fs");
const data = fs.readFileSync("./package.json", { encoding: "utf8", flag: "r" });

// Display the file data
const dataJson = JSON.parse(data);

dataJson.module = "index.mjs";
dataJson.types = "index.d.ts";
dataJson.main = "index.js";
dataJson.browser = "index.mjs";

fs.writeFileSync("./dist/package.json", JSON.stringify(dataJson, null, 2));
45 changes: 45 additions & 0 deletions packages/secsync-server/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"name": "secsync-server",
"version": "0.3.0",
"main": "src/index",
"types": "src/index",
"scripts": {
"build": "pnpm build:xstate-types && pnpm tsup && pnpm build:package-json",
"build:xstate-types": "xstate typegen \"./src/**/*.ts?(x)\"",
"build:package-json": "node package-json-build-script.js",
"prepublishOnly": "pnpm run build",
"test": "jest",
"ts:check": "pnpm tsc --noEmit",
"lint": "echo \"No linting configured\""
},
"dependencies": {
"canonicalize": "^2.0.0",
"libsodium-wrappers": "^0.7.13"
},
"peerDependencies": {
"secsync": "*"
},
"devDependencies": {
"@babel/core": "^7.24.4",
"@babel/preset-env": "^7.24.4",
"@babel/preset-typescript": "^7.24.1",
"@types/jest": "^29.5.12",
"@types/libsodium-wrappers": "^0.7.14",
"@types/ws": "^8.5.10",
"jest": "^29.7.0",
"mock-socket": "^9.3.1"
},
"jest": {
"setupFilesAfterEnv": [
"<rootDir>/test/config/jestTestSetup.ts"
]
},
"publishConfig": {
"directory": "dist",
"linkDirectory": false
},
"repository": {
"type": "git",
"url": "git+https://github.com/serenity-kit/secsync.git"
}
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
import { IncomingMessage } from "http";
import sodium from "libsodium-wrappers";
import { parse as parseUrl } from "url";
import { WebSocket } from "ws";
import { verifySignature } from "../crypto/verifySignature";
import { parseEphemeralMessage } from "../ephemeralMessage/parseEphemeralMessage";
import {
SecsyncNewSnapshotRequiredError,
SecsyncSnapshotBasedOnOutdatedSnapshotError,
SecsyncSnapshotMissesUpdatesError,
} from "../errors";
import { parseSnapshotWithClientData } from "../snapshot/parseSnapshotWithClientData";
import {
AdditionalAuthenticationDataValidations,
CreateSnapshotParams,
CreateUpdateParams,
GetDocumentParams,
HasAccessParams,
HasBroadcastAccessParams,
SecsyncNewSnapshotRequiredError,
SecsyncSnapshotBasedOnOutdatedSnapshotError,
SecsyncSnapshotMissesUpdatesError,
Snapshot,
SnapshotProofChainEntry,
SnapshotUpdateClocks,
Update,
} from "../types";
import { parseUpdate } from "../update/parseUpdate";
import { canonicalizeAndToBase64 } from "../utils/canonicalizeAndToBase64";
import { retryAsyncFunction } from "../utils/retryAsyncFunction";
parseEphemeralMessage,
parseSnapshotWithClientData,
parseUpdate,
verifySignature,
} from "secsync";
import { parse as parseUrl } from "url";
import { WebSocket } from "ws";
import { addConnection, broadcastMessage, removeConnection } from "./store";
import { canonicalizeAndToBase64 } from "./utils/canonicalizeAndToBase64";
import { retryAsyncFunction } from "./utils/retryAsyncFunction";

type GetDocumentResult = {
snapshot?: Snapshot;
Expand Down
1 change: 1 addition & 0 deletions packages/secsync-server/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./createWebSocketConnection";
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { HasBroadcastAccessParams } from "secsync";
import WebSocket from "ws";
import { HasBroadcastAccessParams } from "../types";

type ConnectionEntry = { websocketSessionKey: string; websocket: WebSocket };

Expand Down
12 changes: 12 additions & 0 deletions packages/secsync-server/src/utils/canonicalizeAndToBase64.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import canonicalize from "canonicalize";

export const canonicalizeAndToBase64 = (
input: unknown,
sodium: typeof import("libsodium-wrappers")
): string => {
const canonicalized = canonicalize(input);
if (!canonicalized) {
throw new Error("Failed to canonicalize input");
}
return sodium.to_base64(canonicalized);
};
12 changes: 12 additions & 0 deletions packages/secsync-server/test/config/jestTestSetup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import sodium from "libsodium-wrappers";
import { WebSocket } from "mock-socket";

// @ts-expect-error
global.setImmediate = jest.useRealTimers;
global.WebSocket = WebSocket;

jest.setTimeout(5000);

beforeEach(async () => {
await sodium.ready;
});
10 changes: 10 additions & 0 deletions packages/secsync-server/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../../tsconfig.json",

"compilerOptions": {
"outDir": "./dist",
"types": ["jest"]
},

"include": ["src/**/*"]
}
8 changes: 8 additions & 0 deletions packages/secsync-server/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { defineConfig } from "tsup";

export default defineConfig({
entry: ["src/index.ts"],
clean: true,
dts: true,
format: ["cjs", "esm"],
});
1 change: 1 addition & 0 deletions packages/secsync/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@babel/preset-typescript": "^7.24.1",
"@types/jest": "^29.5.12",
"@types/libsodium-wrappers": "^0.7.14",
"@types/node": "^20.13.0",
"@types/ws": "^8.5.10",
"@xstate/cli": "^0.5.17",
"jest": "^29.7.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/secsync/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ export * from "./crypto/hash";
export * from "./crypto/sign";
export * from "./crypto/verifySignature";
export * from "./ephemeralMessage/createEphemeralMessage";
export * from "./ephemeralMessage/parseEphemeralMessage";
export * from "./ephemeralMessage/verifyAndDecryptEphemeralMessage";
export * from "./errors";
export * from "./server/createWebSocketConnection";
export * from "./snapshot/createInitialSnapshot";
export * from "./snapshot/createSnapshot";
export * from "./snapshot/parseSnapshotWithClientData";
export * from "./snapshot/verifyAndDecryptSnapshot";
export * from "./types";
export * from "./update/createUpdate";
export * from "./update/parseUpdate";
export * from "./update/verifyAndDecryptUpdate";
export * from "./utils/compareUpdateClocks";
export * from "./utils/deserializeUint8ArrayUpdates";
Expand Down
2 changes: 1 addition & 1 deletion packages/secsync/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

"compilerOptions": {
"outDir": "./dist",
"types": ["jest"]
"types": ["jest", "node"]
},

"include": ["src/**/*"]
Expand Down
Loading

0 comments on commit bb14413

Please sign in to comment.