mirror of
https://github.com/hcengineering/platform.git
synced 2025-04-22 16:27:22 +00:00
Token
moved to server-core
Signed-off-by: Andrey Platov <andrey@hardcoreeng.com>
This commit is contained in:
parent
64539d6aa7
commit
fd6c4064c1
@ -59,3 +59,10 @@ export interface FullTextAdapter {
|
|||||||
* @public
|
* @public
|
||||||
*/
|
*/
|
||||||
export type FullTextAdapterFactory = (url: string, workspace: string) => Promise<FullTextAdapter>
|
export type FullTextAdapterFactory = (url: string, workspace: string) => Promise<FullTextAdapter>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @public
|
||||||
|
*/
|
||||||
|
export interface Token {
|
||||||
|
workspace: string
|
||||||
|
}
|
||||||
|
@ -19,8 +19,8 @@ import type { DbAdapter, TxAdapter } from '@anticrm/server-core'
|
|||||||
|
|
||||||
import { MongoClient, Db, Filter, Document, Sort } from 'mongodb'
|
import { MongoClient, Db, Filter, Document, Sort } from 'mongodb'
|
||||||
|
|
||||||
function translateQuery<T extends Doc> (query: DocumentQuery<T>): Filter<T> {
|
function translateQuery<T extends Doc> (query: DocumentQuery<T>): Filter<Document> {
|
||||||
return query as Filter<T>
|
return query as Filter<Document>
|
||||||
}
|
}
|
||||||
|
|
||||||
function translateDoc (doc: Doc): Document {
|
function translateDoc (doc: Doc): Document {
|
||||||
|
@ -23,6 +23,7 @@
|
|||||||
"jwt-simple": "^0.5.6",
|
"jwt-simple": "^0.5.6",
|
||||||
"ws": "^8.0.0",
|
"ws": "^8.0.0",
|
||||||
"@anticrm/platform": "~0.6.5",
|
"@anticrm/platform": "~0.6.5",
|
||||||
"@anticrm/core": "~0.6.11"
|
"@anticrm/core": "~0.6.11",
|
||||||
|
"@anticrm/server-core": "~0.6.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,13 +15,13 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
import { readResponse, serialize } from '@anticrm/platform'
|
import { readResponse, serialize } from '@anticrm/platform'
|
||||||
import { _Token } from '../server'
|
import type { Token } from '@anticrm/server-core'
|
||||||
import { encode } from 'jwt-simple'
|
import { encode } from 'jwt-simple'
|
||||||
import WebSocket from 'ws'
|
import WebSocket from 'ws'
|
||||||
|
|
||||||
describe('server', () => {
|
describe('server', () => {
|
||||||
function connect (): WebSocket {
|
function connect (): WebSocket {
|
||||||
const payload: _Token = {
|
const payload: Token = {
|
||||||
workspace: 'ws1'
|
workspace: 'ws1'
|
||||||
}
|
}
|
||||||
const token = encode(payload, 'secret')
|
const token = encode(payload, 'secret')
|
||||||
|
@ -15,7 +15,8 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
import { readResponse, serialize } from '@anticrm/platform'
|
import { readResponse, serialize } from '@anticrm/platform'
|
||||||
import { start, _Token, disableLogging } from '../server'
|
import { start, disableLogging } from '../server'
|
||||||
|
import type { Token } from '@anticrm/server-core'
|
||||||
import { encode } from 'jwt-simple'
|
import { encode } from 'jwt-simple'
|
||||||
import WebSocket from 'ws'
|
import WebSocket from 'ws'
|
||||||
|
|
||||||
@ -34,7 +35,7 @@ describe('server', () => {
|
|||||||
}), 3333)
|
}), 3333)
|
||||||
|
|
||||||
function connect (): WebSocket {
|
function connect (): WebSocket {
|
||||||
const payload: _Token = {
|
const payload: Token = {
|
||||||
workspace: 'latest'
|
workspace: 'latest'
|
||||||
}
|
}
|
||||||
const token = encode(payload, 'secret')
|
const token = encode(payload, 'secret')
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
import { readRequest, serialize, Response } from '@anticrm/platform'
|
import { readRequest, serialize, Response } from '@anticrm/platform'
|
||||||
|
import type { Token } from '@anticrm/server-core'
|
||||||
import { createServer, IncomingMessage } from 'http'
|
import { createServer, IncomingMessage } from 'http'
|
||||||
import WebSocket, { Server } from 'ws'
|
import WebSocket, { Server } from 'ws'
|
||||||
import { decode } from 'jwt-simple'
|
import { decode } from 'jwt-simple'
|
||||||
@ -25,17 +26,10 @@ let LOGGING_ENABLED = true
|
|||||||
|
|
||||||
export function disableLogging (): void { LOGGING_ENABLED = false }
|
export function disableLogging (): void { LOGGING_ENABLED = false }
|
||||||
|
|
||||||
/**
|
|
||||||
* @internal
|
|
||||||
*/
|
|
||||||
export interface _Token {
|
|
||||||
workspace: string
|
|
||||||
}
|
|
||||||
|
|
||||||
class Session implements Storage {
|
class Session implements Storage {
|
||||||
constructor (
|
constructor (
|
||||||
private readonly manager: SessionManager,
|
private readonly manager: SessionManager,
|
||||||
private readonly token: _Token,
|
private readonly token: Token,
|
||||||
private readonly storage: ServerStorage
|
private readonly storage: ServerStorage
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
@ -62,7 +56,7 @@ interface Workspace {
|
|||||||
class SessionManager {
|
class SessionManager {
|
||||||
private readonly workspaces = new Map<string, Workspace>()
|
private readonly workspaces = new Map<string, Workspace>()
|
||||||
|
|
||||||
async addSession (ws: WebSocket, token: _Token, storageFactory: (ws: string) => Promise<ServerStorage>): Promise<Session> {
|
async addSession (ws: WebSocket, token: Token, storageFactory: (ws: string) => Promise<ServerStorage>): Promise<Session> {
|
||||||
const workspace = this.workspaces.get(token.workspace)
|
const workspace = this.workspaces.get(token.workspace)
|
||||||
if (workspace === undefined) {
|
if (workspace === undefined) {
|
||||||
const storage = await storageFactory(token.workspace)
|
const storage = await storageFactory(token.workspace)
|
||||||
@ -80,7 +74,7 @@ class SessionManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
close (ws: WebSocket, token: _Token, code: number, reason: string): void {
|
close (ws: WebSocket, token: Token, code: number, reason: string): void {
|
||||||
if (LOGGING_ENABLED) console.log(`closing websocket, code: ${code}, reason: ${reason}`)
|
if (LOGGING_ENABLED) console.log(`closing websocket, code: ${code}, reason: ${reason}`)
|
||||||
const workspace = this.workspaces.get(token.workspace)
|
const workspace = this.workspaces.get(token.workspace)
|
||||||
if (workspace === undefined) {
|
if (workspace === undefined) {
|
||||||
@ -93,7 +87,7 @@ class SessionManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
broadcast (from: Session, token: _Token, resp: Response<any>): void {
|
broadcast (from: Session, token: Token, resp: Response<any>): void {
|
||||||
const workspace = this.workspaces.get(token.workspace)
|
const workspace = this.workspaces.get(token.workspace)
|
||||||
if (workspace === undefined) {
|
if (workspace === undefined) {
|
||||||
throw new Error('internal: cannot find sessions')
|
throw new Error('internal: cannot find sessions')
|
||||||
@ -127,7 +121,7 @@ export function start (storageFactory: (workspace: string) => Promise<ServerStor
|
|||||||
|
|
||||||
const wss = new Server({ noServer: true })
|
const wss = new Server({ noServer: true })
|
||||||
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
||||||
wss.on('connection', async (ws: WebSocket, request: any, token: _Token) => {
|
wss.on('connection', async (ws: WebSocket, request: any, token: Token) => {
|
||||||
const buffer: string[] = []
|
const buffer: string[] = []
|
||||||
|
|
||||||
ws.on('message', (msg: string) => { buffer.push(msg) })
|
ws.on('message', (msg: string) => { buffer.push(msg) })
|
||||||
|
Loading…
Reference in New Issue
Block a user