From 6dc4aa8c6b354ecedc78b070764dd41abc41516b Mon Sep 17 00:00:00 2001 From: Spencer Pogorzelski <34356756+Scoder12@users.noreply.github.com> Date: Sat, 12 Aug 2023 12:23:45 -0700 Subject: [PATCH] monorepo part 2 --- client/package.json | 17 ++++++++ client/{ => src}/AdriftClient.ts | 4 +- client/{ => src}/Connection.ts | 6 +-- client/{ => src}/DevWsTransport.ts | 4 +- client/{ => src}/RTCTransport.ts | 64 ++++++++++++++---------------- client/{ => src}/firebase.ts | 9 ++--- client/src/index.ts | 4 ++ client/tsconfig.json | 1 - frontend/package.json | 6 ++- frontend/{ => src}/App.svelte | 23 +++++------ frontend/{ => src}/entry.ts | 4 +- frontend/src/env.d.ts | 9 +++++ frontend/svelte.config.cjs | 2 +- frontend/tsconfig.json | 33 +++++++++++++++ pnpm-lock.yaml | 41 +++++++++++++++++++ protocol/src/Transport.ts | 2 +- protocol/src/index.ts | 2 + 17 files changed, 165 insertions(+), 66 deletions(-) create mode 100644 client/package.json rename client/{ => src}/AdriftClient.ts (94%) rename client/{ => src}/Connection.ts (94%) rename client/{ => src}/DevWsTransport.ts (84%) rename client/{ => src}/RTCTransport.ts (64%) rename client/{ => src}/firebase.ts (75%) create mode 100644 client/src/index.ts rename frontend/{ => src}/App.svelte (85%) rename frontend/{ => src}/entry.ts (80%) create mode 100644 frontend/src/env.d.ts create mode 100644 frontend/tsconfig.json diff --git a/client/package.json b/client/package.json new file mode 100644 index 0000000..c4ea2f1 --- /dev/null +++ b/client/package.json @@ -0,0 +1,17 @@ +{ + "name": "client", + "version": "1.0.0", + "description": "", + "main": "src/index.ts", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "dependencies": { + "bare-client-custom": "file:../bare-client-custom", + "firebase": "^10.1.0", + "protocol": "workspace:*" + } +} diff --git a/client/AdriftClient.ts b/client/src/AdriftClient.ts similarity index 94% rename from client/AdriftClient.ts rename to client/src/AdriftClient.ts index c67f426..71d5640 100644 --- a/client/AdriftClient.ts +++ b/client/src/AdriftClient.ts @@ -7,7 +7,7 @@ import { ReadyStateCallback, WebSocketImpl, } from "bare-client-custom"; -import Connection from "./Connection"; +import { Connection } from "./Connection"; // export class Adrift { // bareclient:AdriftBareClient, @@ -45,7 +45,7 @@ export class AdriftBareClient extends Client { }); const headers = new Headers(); for (const [header, values] of Object.entries(payload.headers)) { - for (const value of values) { + for (const value of values) { headers.append(header, value); } } diff --git a/client/Connection.ts b/client/src/Connection.ts similarity index 94% rename from client/Connection.ts rename to client/src/Connection.ts index c712186..d9650f5 100644 --- a/client/Connection.ts +++ b/client/src/Connection.ts @@ -5,10 +5,10 @@ import { HTTPResponsePayload, S2CRequestType, S2CRequestTypes, -} from "../protocol"; -import Transport from "../protocol/Transport"; + Transport, +} from "protocol"; -export default class Connection { +export class Connection { callbacks: Record = {}; counter: number = 0; diff --git a/client/DevWsTransport.ts b/client/src/DevWsTransport.ts similarity index 84% rename from client/DevWsTransport.ts rename to client/src/DevWsTransport.ts index 4d11ce7..11e1f15 100644 --- a/client/DevWsTransport.ts +++ b/client/src/DevWsTransport.ts @@ -1,9 +1,9 @@ -import Transport from "../protocol/Transport"; +import { Transport } from "protocol"; export class DevWsTransport extends Transport { ws: WebSocket; - constructor(onopen, onclose) { + constructor(onopen: () => void, onclose: () => void) { super(onopen, onclose); this.ws = new WebSocket("ws://localhost:3000/dev-ws"); diff --git a/client/RTCTransport.ts b/client/src/RTCTransport.ts similarity index 64% rename from client/RTCTransport.ts rename to client/src/RTCTransport.ts index 0267504..8cc9380 100644 --- a/client/RTCTransport.ts +++ b/client/src/RTCTransport.ts @@ -1,39 +1,38 @@ -import Transport from "../protocol/Transport"; -import Connection from "./Connection"; +import { Transport } from "protocol"; const rtcConf = { iceServers: [ { - urls: "stun:stun.l.google.com:19302" - } - ] -} + urls: "stun:stun.l.google.com:19302", + }, + ], +}; enum RequestType { HttpRequest, } - -type Offer = { offer: any, localCandidates: any }; +type Offer = { offer: any; localCandidates: any }; interface RTCOptions { - - onconnectionstatechange?, - onsignalingstatechange?, - oniceconnectionstatechange?, - onicegatheringstatechange?, - onopen?, - onclose?, - onmessage? + onconnectionstatechange?: any; + onsignalingstatechange?: any; + oniceconnectionstatechange?: any; + onicegatheringstatechange?: any; + onopen?: any; + onclose?: any; + onmessage?: any; } export class RTCTransport extends Transport { peer: RTCPeerConnection; dataChannel: RTCDataChannel; - constructor(onopen, onclose, + constructor( + public onopen: () => void, + public onclose: () => void, public onconnectionstatechange: () => void, public onsignalingstatechange: () => void, - public onicegatheringstatechange: () => void, + public onicegatheringstatechange: () => void ) { super(onopen, onclose); this.peer = new RTCPeerConnection(rtcConf); @@ -41,22 +40,24 @@ export class RTCTransport extends Transport { this.peer.onsignalingstatechange = onsignalingstatechange; - this.peer.oniceconnectionstatechange = - (event) => { - console.log('ICE connection state:', this.peer.iceConnectionState); - if (this.peer.iceConnectionState == "disconnected" || this.peer.iceConnectionState == "failed") { - console.log("disconnected"); - onclose(); - } - }; + this.peer.oniceconnectionstatechange = (event) => { + console.log("ICE connection state:", this.peer.iceConnectionState); + if ( + this.peer.iceConnectionState == "disconnected" || + this.peer.iceConnectionState == "failed" + ) { + console.log("disconnected"); + onclose(); + } + }; this.peer.onicegatheringstatechange = onicegatheringstatechange; - this.dataChannel = this.peer.createDataChannel('host-server'); + this.dataChannel = this.peer.createDataChannel("host-server"); this.dataChannel.onopen = onopen; this.dataChannel.onclose = onclose; this.dataChannel.onmessage = async (event) => { let buf = await event.data.arrayBuffer(); - this.ondata(buf) + this.ondata(buf); }; } @@ -64,15 +65,10 @@ export class RTCTransport extends Transport { this.dataChannel.send(data); } - - async createOffer(): Promise> { - const localCandidates: RTCIceCandidate[] = []; - let readyPromise: Promise = new Promise((resolve, reject) => { - this.peer.onicecandidate = async (event) => { if (event.candidate) { localCandidates.push(event.candidate); @@ -83,8 +79,6 @@ export class RTCTransport extends Transport { }; }); - - const offer = await this.peer.createOffer(); await this.peer.setLocalDescription(offer); return readyPromise; diff --git a/client/firebase.ts b/client/src/firebase.ts similarity index 75% rename from client/firebase.ts rename to client/src/firebase.ts index 80bc660..7aced41 100644 --- a/client/firebase.ts +++ b/client/src/firebase.ts @@ -1,6 +1,5 @@ +import { getDatabase, onValue, ref, set } from "firebase/database"; import "../firebase-config"; -import { getDatabase, ref, onValue, set } from "firebase/database"; - const db = getDatabase(); console.log(db); @@ -11,16 +10,15 @@ let reff = ref(db, "/peers/demo"); // console.log(data); // }); +var callback: (answer: any, candidates: any[]) => void; -var callback; -export function setCallback(call) { +export function setCallback(call: typeof callback) { callback = call; } export function setOffer(offer: string) { set(reff, offer); } - onValue(reff, (snapshot) => { const data = snapshot.val(); console.log(data); @@ -31,4 +29,3 @@ onValue(reff, (snapshot) => { callback(answer, candidates); } }); - diff --git a/client/src/index.ts b/client/src/index.ts new file mode 100644 index 0000000..42c296c --- /dev/null +++ b/client/src/index.ts @@ -0,0 +1,4 @@ +export { AdriftBareClient } from "./AdriftClient"; +export { Connection } from "./Connection"; +export { DevWsTransport } from "./DevWsTransport"; +export { RTCTransport } from "./RTCTransport"; diff --git a/client/tsconfig.json b/client/tsconfig.json index 0048ce2..fa92694 100644 --- a/client/tsconfig.json +++ b/client/tsconfig.json @@ -16,7 +16,6 @@ "noImplicitThis": true, "noImplicitAny": true, "strictNullChecks": true, - "suppressImplicitAnyIndexErrors": true, "noUnusedLocals": true, "skipLibCheck": true, "allowSyntheticDefaultImports": true, diff --git a/frontend/package.json b/frontend/package.json index fe7f208..ef6d8be 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -20,7 +20,11 @@ "typescript": "^5.1.6", "vite": "^4.4.9", "vite-plugin-singlefile": "^0.13.5", - "vite-plugin-svelte": "^3.0.1" + "vite-plugin-svelte": "^3.0.1", + "bare-client-custom": "file:../bare-client-custom", + "firebase-config": "workspace:*", + "client": "workspace:*", + "protocol": "workspace:*" }, "devDependencies": {} } diff --git a/frontend/App.svelte b/frontend/src/App.svelte similarity index 85% rename from frontend/App.svelte rename to frontend/src/App.svelte index db29122..1470160 100644 --- a/frontend/App.svelte +++ b/frontend/src/App.svelte @@ -1,13 +1,14 @@

{#if !import.meta.env.VITE_ADRIFT_DEV} {:else} - connected to dev server + {/if}

diff --git a/frontend/entry.ts b/frontend/src/entry.ts similarity index 80% rename from frontend/entry.ts rename to frontend/src/entry.ts index ea9fb2f..20c84cd 100644 --- a/frontend/entry.ts +++ b/frontend/src/entry.ts @@ -1,7 +1,7 @@ - import App from "./App.svelte"; + const app = new App({ - target: document.getElementById("app") + target: document.getElementById("app")!, }); if (!import.meta.env.VITE_ADRIFT_SINGLEFILE) { diff --git a/frontend/src/env.d.ts b/frontend/src/env.d.ts new file mode 100644 index 0000000..cbd2f8a --- /dev/null +++ b/frontend/src/env.d.ts @@ -0,0 +1,9 @@ +/// + +interface ImportMetaEnv { + readonly VITE_ADRIFT_DEV: string; +} + +interface ImportMeta { + readonly env: ImportMetaEnv; +} diff --git a/frontend/svelte.config.cjs b/frontend/svelte.config.cjs index 85ffa9f..fa7dec5 100644 --- a/frontend/svelte.config.cjs +++ b/frontend/svelte.config.cjs @@ -10,4 +10,4 @@ const config = { ], }; -module.exports = config; \ No newline at end of file +module.exports = config; diff --git a/frontend/tsconfig.json b/frontend/tsconfig.json new file mode 100644 index 0000000..d0083ea --- /dev/null +++ b/frontend/tsconfig.json @@ -0,0 +1,33 @@ +{ + "compilerOptions": { + "experimentalDecorators": true, + "baseUrl": ".", + "outDir": "build/dist", + "module": "esnext", + "target": "es2017", + "lib": ["es6", "dom", "esnext.asynciterable", "es2017"], + "sourceMap": true, + "allowJs": true, + "jsx": "react", + "moduleResolution": "node", + "rootDir": "..", + "forceConsistentCasingInFileNames": true, + "noImplicitReturns": true, + "noImplicitThis": true, + "noImplicitAny": true, + "strictNullChecks": true, + "noUnusedLocals": true, + "skipLibCheck": true, + "allowSyntheticDefaultImports": true, + "removeComments": true + }, + "exclude": [ + "node_modules", + "build", + "scripts", + "acceptance-tests", + "webpack", + "jest", + "src/setupTests.ts" + ] +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6adf983..4e37ca9 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -8,6 +8,18 @@ importers: .: {} + client: + dependencies: + bare-client-custom: + specifier: file:../bare-client-custom + version: file:bare-client-custom + firebase: + specifier: ^10.1.0 + version: 10.1.0(react-native@0.72.3) + protocol: + specifier: workspace:* + version: link:../protocol + firebase-config: dependencies: firebase: @@ -19,9 +31,21 @@ importers: '@sveltejs/vite-plugin-svelte': specifier: ^2.4.5 version: 2.4.5(svelte@4.2.0)(vite@4.4.9) + bare-client-custom: + specifier: file:../bare-client-custom + version: file:bare-client-custom + client: + specifier: workspace:* + version: link:../client firebase: specifier: ^10.1.0 version: 10.1.0(react-native@0.72.3) + firebase-config: + specifier: workspace:* + version: link:../firebase-config + protocol: + specifier: workspace:* + version: link:../protocol svelte: specifier: ^4.2.0 version: 4.2.0 @@ -2798,6 +2822,10 @@ packages: resolution: {integrity: sha512-Hl219/BT5fLAaz6NDkSuhzasy49dwQS/DSdu4MdggFB8zcXv7vflBI3xp7FEmkmdDkBUI2bPUNeMttp2knYdxw==} dev: false + /@types/uuid@9.0.2: + resolution: {integrity: sha512-kNnC1GFBLuhImSnV7w4njQkUiJi0ZXUycu1rUaouPqiKlXkh77JKgdRnTAp1x5eBwcIwbtI+3otwzuIDEuDoxQ==} + dev: false + /@types/webrtc@0.0.36: resolution: {integrity: sha512-tYFarc92EluXU7XyRmWbkQXSbZIOHTdDOudFPal9u/TNTQuouWpIHV/2o9bNAdqvTJFjLJh/zflCOLWbL30tEQ==} dev: true @@ -6407,6 +6435,11 @@ packages: engines: {node: '>= 0.4.0'} dev: false + /uuid@9.0.0: + resolution: {integrity: sha512-MXcSTerfPa4uqyzStbRoTgt5XIe3x5+42+q1sDuy3R5MDk66URdLMOZe5aPX/SQd+kuYAh0FdP/pO28IkQyTeg==} + hasBin: true + dev: false + /v8-compile-cache-lib@3.0.1: resolution: {integrity: sha512-wa7YjyUGfNZngI/vtK0UHAN+lgDCxBPCylVXGp0zu59Fz5aiGtNXaq3DhIov063MorB+VfufLh3JlF2KdTK3xg==} dev: false @@ -6725,3 +6758,11 @@ packages: resolution: {integrity: sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==} engines: {node: '>=10'} dev: false + + file:bare-client-custom: + resolution: {directory: bare-client-custom, type: directory} + name: '@tomphttp/bare-client' + dependencies: + '@types/uuid': 9.0.2 + uuid: 9.0.0 + dev: false diff --git a/protocol/src/Transport.ts b/protocol/src/Transport.ts index 85b8e9a..03d3fe1 100644 --- a/protocol/src/Transport.ts +++ b/protocol/src/Transport.ts @@ -1,4 +1,4 @@ -export default abstract class Transport { +export abstract class Transport { public ondata: (data: ArrayBuffer) => void = () => {}; constructor(public onopen: () => void, public onclose: () => void) {} diff --git a/protocol/src/index.ts b/protocol/src/index.ts index 26b0871..24e8205 100644 --- a/protocol/src/index.ts +++ b/protocol/src/index.ts @@ -30,3 +30,5 @@ export type HTTPResponsePayload = { statusText: string; headers: ProtoBareHeaders; }; + +export { Transport } from "./Transport";