Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
semyenov committed Jul 2, 2024
2 parents 7594e95 + 06b88cd commit a6d0317
Show file tree
Hide file tree
Showing 24 changed files with 681 additions and 235 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ core
# Other
.scraper-*
*.log*

.idea
8 changes: 6 additions & 2 deletions apps/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"dist"
],
"scripts": {
"build": "cross-env NODE_OPTIONS=\"--no-warnings\" yarn rollup --config ./rollup.config.mjs",
"build": "cross-env NODE_OPTIONS=\"--no-warnings\" yarn rollup --config ./rollup.config.mjs",
"start": "cross-env NODE_OPTIONS=\"--no-warnings --experimental-modules --experimental-import-meta-resolve\" yarn node dist/esm/index.mjs",
"schema": "yarn typescript-json-schema --out schema.json --skipLibCheck src/index.ts Options",
"typecheck": "yarn tsc --noEmit",
Expand Down Expand Up @@ -90,9 +90,13 @@
},
"devDependencies": {
"@regioni/lib": "workspace:^",
"@rollup/plugin-wasm": "^6.2.2",
"@types/orbitdb__core": "workspace:^",
"esbuild": "^0.21.5",
"rollup": "^4.14.0",
"tsx": "^4.7.2"
"rollup-plugin-node-polyfills": "^0.2.1",
"rollup-plugin-terser": "^7.0.2",
"tsx": "^4.7.2",
"unbuild": "^2.0.0"
}
}
4 changes: 4 additions & 0 deletions apps/backend/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import build from '@regioni/build'
import wasm from '@rollup/plugin-wasm'

// import { terser } from 'rollup-plugin-terser'
// import nodePolyfills from 'rollup-plugin-node-polyfills'

export default build({
src: './src',
Expand Down
1 change: 1 addition & 0 deletions apps/backend/src/context.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// import { createAjv } from '@regioni/lib/ajv'
import { createAjv } from '@regioni/lib/ajv'
import { bullmq } from '@regioni/lib/bullmq'
import { createRedisStore } from '@regioni/lib/redis'
Expand Down
1 change: 1 addition & 0 deletions apps/backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { createContext } from './context'
import { router } from './router'

import type { Router } from './router'
export * from './router'

const logger = consola.withTag('server')
export const app = createHTTPServer({
Expand Down
1 change: 1 addition & 0 deletions apps/backend/src/orbitdb/keystore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ await ipfs.start()

const keystore = await KeyStore({ path: keysPath })
const identities = await Identities({ keystore, ipfs })

const provider = PublicKeyIdentityProvider({ keystore })

const identity = await identities.createIdentity({ id, provider })
Expand Down
2 changes: 1 addition & 1 deletion apps/backend/src/routers/data.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { queueEvents } from '@regioni/lib/bullmq/events'
import { queueEvents } from '@regioni/lib/bullmq'
import { observable } from '@trpc/server/observable'
import consola from 'consola'
import { z } from 'zod'
Expand Down
4 changes: 2 additions & 2 deletions apps/backend/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://json.schemastore.org/tsconfig.json",
"compilerOptions": {
"target": "ESNext",
"target": "ES2017",
"lib": [
"ESNext",
"DOM"
Expand All @@ -28,4 +28,4 @@
"include": [
"src/**/*"
]
}
}
5 changes: 3 additions & 2 deletions apps/frontend/src/plugins/client.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { WebSocketBrowserProxy, transformer } from '@sozdev/share-libs/dist/browser'
import { transformer } from '@regioni/lib/transformer'
import { WebSocketBrowserProxy } from '@regioni/lib/ws/browser'
import { type CreateTRPCProxyClient, createTRPCProxyClient, createWSClient, wsLink } from '@trpc/client'
import consola from 'consola'

import type { AppRouter } from '@sozdev/backend'
import type { Router as AppRouter } from '@regioni/backend'

declare module '#app/nuxt' {
export interface NuxtApp {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"frontend:build": "yarn workspace @regioni/nuxt-frontend run build",
"backend:dev": "yarn workspace @regioni/backend dev",
"backend:build": "yarn workspace @regioni/backend run build",
"packages:build": "yarn workspace @regioni/build run build && yarn workspace",
"packages:build": "yarn workspace @regioni/build run build && yarn workspace @regioni/lib run build",
"generate:types": "yarn tsx scripts/generate_types.ts",
"generate:keys": "yarn tsx scripts/generate_keys.ts"
},
Expand All @@ -57,6 +57,7 @@
"apps/*"
],
"devDependencies": {
"@regioni/lib": "workspace:^",
"@sozdev/eslint-config": "^0.0.11",
"@sozdev/rollup-build": "^0.0.10",
"@types/ws": "^8.5.10",
Expand Down
2 changes: 1 addition & 1 deletion packages/build/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ function build(options?: Options) {
},
resolve: {
preferBuiltins: true,

},
commonjs: {
exclude: external,
},
esbuild: {
exclude: external,
minify: true,
tsconfig: tsconfigFile,
},
Expand Down
70 changes: 53 additions & 17 deletions packages/lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,66 @@
"keywords": [],
"sideEffects": false,
"exports": {
"./ajv": {
"import": "./dist/ajv/index.js",
"types": "./dist/ajv/index.d.ts"
},
"./automerge": {
"import": "./dist/automerge/index.js",
"types": "./dist/automerge/index.d.ts"
},
"./bullmq": {
"import": "./dist/bullmq/index.js",
"types": "./dist/bullmq/index.d.ts"
},
"./jose": {
"import": "./dist/jose/index.js",
"types": "./dist/jose/index.d.ts"
},
"./logger": {
"import": "./dist/logger/index.js",
"types": "./dist/logger/index.d.ts"
},
"./mongodb": {
"import": "./dist/mongodb/index.js",
"types": "./dist/mongodb/index.d.ts"
},
"./quicktype": {
"import": "./dist/quicktype/index.js",
"types": "./dist/quicktype/index.d.ts"
},
"./redis": {
"import": "./dist/redis/index.js",
"types": "./dist/redis/index.d.ts"
},
"./radix": {
"import": "./dist/radix/index.js",
"types": "./dist/radix/index.d.ts"
},
"./superjson": {
"import": "./dist/superjson/index.js",
"types": "./dist/superjson/index.d.ts"
},
"./transformer": {
"import": "./dist/transformer/index.js",
"types": "./dist/transformer/index.d.ts"
},
"./ws": {
"types": "./dist/types/index.d.ts",
"import": "./dist/esm/ws/index.mjs",
"require": "./dist/cjs/ws/index.cjs"
}
},
"main": "./dist/cjs/index.cjs",
"module": "./dist/esm/index.mjs",
"types": "./dist/index.d.ts",
"typesVersions": {
"*": {
"*": [
"./dist/*",
"./dist/index.d.ts"
]
"import": "./dist/ws/index.js",
"types": "./dist/ws/index.d.ts"
},
"./ws/browser": {
"import": "./dist/ws/browser.js",
"types": "./dist/ws/browser.d.ts"
}
},
"files": [
"*"
"dist"
],
"scripts": {
"build": "yarn tsc -b",
"dev": "yarn esno src/index.ts",
"typecheck": "yarn tsc --noEmit",
"build": "cross-env NODE_OPTIONS=\"--no-warnings\" yarn rollup --configPlugin esbuild --config rollup.config.ts",
"start": "cross-env NODE_OPTIONS=\"--no-warnings --experimental-modules --experimental-import-meta-resolve\" yarn node src/esm/index.mjs",
"schema": "yarn typescript-json-schema --out schema.json --skipLibCheck src/index.ts Options",
"release": "yarn npm publish",
Expand Down Expand Up @@ -75,7 +111,7 @@
"events": "^3.3.0",
"lint-staged": "^15.2.7",
"simple-git-hooks": "^2.9.0",
"typescript": "^5.2.2",
"typescript": "^5.5.2",
"typescript-json-schema": "0.62.0"
},
"simple-git-hooks": {
Expand Down
4 changes: 2 additions & 2 deletions packages/lib/src/ajv/schema.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { JSONSchemaType } from 'ajv'
import type { TestUserInfo } from 'types'
// import type { TestUserInfo } from 'types'

export const userSchema: JSONSchemaType<{
status: string
info?: TestUserInfo
// info?: TestUserInfo
date: string
}> = {
type: 'object',
Expand Down
1 change: 1 addition & 0 deletions packages/lib/src/bullmq/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Queue } from 'bullmq'
export * from './events'

export const bullmq = new Queue<
{ message: string },
Expand Down
7 changes: 7 additions & 0 deletions packages/lib/src/jose/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import type * as jose from 'jose'
import type { JWTVerifyGetKey } from 'jose'

export type KeyPair = jose.GenerateKeyPairResult<
jose.KeyLike & jose.JWK
>


export interface IJoseVerify {
key: KeyPair
jwks: JWTVerifyGetKey
}
106 changes: 106 additions & 0 deletions packages/lib/src/ws/browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import consola from 'consola'

import { sign, verify } from '../jose/sign'

import type { IJoseVerify } from '../jose/types'

export * from './types'

const logger = consola.withTag('ws/browser')
export class WebSocketBrowserProxy extends WebSocket {
jose?: IJoseVerify
public constructor(
address: string | URL,
protocols?: string | string[],
jose?: IJoseVerify,
) {
super(address, protocols)
this.jose = jose

return wrapSocket(this)
}
}

export function wrapSocket(ws: WebSocketBrowserProxy) {
return new Proxy(ws, {
get: (target, prop) => {
logger.debug('Getting', prop, target)
switch (prop) {
case 'addEventListener':
return customOn.bind(target)
case 'send':
return customSend.bind(target)
}

return Reflect.get(target, prop)
},
})
}

type BufferLike = string | ArrayBufferView | ArrayBufferLike | Blob

async function customOn(
this: WebSocketBrowserProxy,
event: string,
listener: (...args: any[]) => void,
) {
this.addEventListener(event, customListener)

async function customListener(this: WebSocketBrowserProxy, ...args: any[]) {
if (event === 'message') {
const [event] = args as [MessageEvent<string>]
const data = event.data

if (!this.jose) {
logger.debug('Receiving: jose not initialized', data)

return listener.call(this, event)
}
try {
const { payload, ..._jws } = await verify(data.toString(), this.jose.jwks)

const newEvent = createMessageEvent(event, payload)

logger.debug('Receiving payload"', { payload, event: newEvent })

return listener.call(this, newEvent)
}
catch (error) {
const newEvent = createMessageEvent(event, {})
return listener.call(this, newEvent)
}
}

logger.debug('Receiving', event, args)

return listener.call(this, ...args)
}
}

function createMessageEvent(event: MessageEvent, payload: unknown) {
return new MessageEvent('message', {
data: JSON.stringify(payload),
origin: event.origin,
source: event.source,
lastEventId: event.lastEventId,
ports: [...event.ports],
})
}

async function customSend(this: WebSocketBrowserProxy, data: BufferLike) {
if (!this.jose) {
logger.debug('Sending: jose not initialized', data)
this.send(data)
return
}

logger.debug('Signing payload: ', { payload: data, jose: this.jose })

const jws = await sign(this.jose.key, {
payload: JSON.parse(data.toString()),
})

logger.debug('Sending', jws)

this.send(jws)
}
1 change: 1 addition & 0 deletions packages/lib/src/ws/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './ws'
export * from './wss'
export * from './types'
16 changes: 16 additions & 0 deletions packages/lib/src/ws/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { IJoseVerify } from '../jose/types'

declare module 'ws' {
export interface WebSocketServer {
jose?: IJoseVerify
}
export interface WebSocket {
jose?: IJoseVerify
}
}

declare global {
export interface WebSocket {
jose?: IJoseVerify
}
}
Loading

0 comments on commit a6d0317

Please sign in to comment.