Skip to content

Commit

Permalink
Merge pull request #4 from dd-center/shigma/ts
Browse files Browse the repository at this point in the history
Migrate to TypeScript
  • Loading branch information
simon300000 authored Feb 23, 2020
2 parents 4d26308 + 0a08f82 commit f67821a
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 137 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
tsconfig.tsbuildinfo
**/*.d.ts
src/*.js
index.js
package-lock.json

# Logs
logs
*.log
Expand Down
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "./src"
14 changes: 10 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,27 @@
"version": "0.0.0",
"private": true,
"scripts": {
"test": "npm run unit",
"unit": "nyc mocha --reporter=landing",
"coverage": "mkdir coverage; nyc report --reporter=text-lcov >> ./coverage/lcov.info"
"build": "tsc -b"
},
"devDependencies": {
"chai": "^4.2.0",
"coveralls": "^3.0.6",
"mocha": "^6.2.0",
"nyc": "^14.1.1"
"nyc": "^14.1.1",
"ts-node": "^8.6.2",
"typescript": "^3.7.5"
},
"dependencies": {
"@types/lru-cache": "^5.1.0",
"@types/node": "^13.1.8",
"@types/socket.io": "^2.1.4",
"@types/socket.io-client": "^1.4.32",
"@types/ws": "^7.2.0",
"athome": "^0.3.0",
"lru-cache": "^5.1.1",
"socket.io": "^2.3.0",
"socket.io-client": "^2.3.0",
"state-center": "file:state-center",
"ws": "^7.1.2"
}
}
37 changes: 22 additions & 15 deletions index.js → src/home.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
const Server = require('socket.io')
const LRU = require('lru-cache')
const AtHome = require('athome')
import SocketIO from 'socket.io'
import LRU from 'lru-cache'
import AtHome from 'athome'
import CState from '../state-center/api'
import { map } from './metadata'

const CState = require('./state-center/api')
export const cState = new CState({ name: 'cluster' })

const clusterWs = require('./ws')
const io = SocketIO(9012, { serveClient: false })

const cState = new CState({ name: 'cluster' })

const io = new Server(9012, { serveClient: false })
const httpHome = new AtHome({
export const httpHome: InstanceType<typeof AtHome> = new AtHome({
retries: 16,
validator: result => {
if (!result) {
Expand All @@ -23,24 +22,33 @@ const httpHome = new AtHome({
return false
}
return !result.code
}
},
})

const cache = new LRU({ max: 10000, maxAge: 1000 * 60 })

cState.stateRoute({
export const router = {
pulls() {
return httpHome.pulls.length
},
pending() {
return httpHome.pending.length
},
homes() {
return [...httpHome.homes.values()].map(({ runtime, version, platform, docker, name, resolves, rejects, lastSeen, id }) => ({ runtime, version, platform, docker, name, resolves, rejects, lastSeen, id }))
return [...httpHome.homes.values()]
.map(home => {
const { resolves, rejects, lastSeen } = home
const { id }: any = home
const metadata = map.get(home)
return { resolves, rejects, lastSeen, id, ...metadata }
})
},
online() {
return httpHome.homes.size
}
})
},
}

cState.stateRoute(router)

const pending = new Map()

Expand Down Expand Up @@ -76,4 +84,3 @@ io.on('connect', socket => {

console.log('Cluster center online')
console.log('socket.io: 9012')
clusterWs(httpHome, cState.log)
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './ws'
7 changes: 7 additions & 0 deletions src/metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import AtHome from 'athome'

export const metadatas = ['runtime', 'platform', 'version', 'name', 'docker'] as const

type MetadataKey = typeof metadatas[number]

export const map: WeakMap<ReturnType<InstanceType<typeof AtHome>["homes"]["get"]>, Record<MetadataKey, any>> = new WeakMap()
105 changes: 105 additions & 0 deletions src/ws.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { Server } from 'ws'
import AtHome from 'athome'
import CState from '../state-center/api'
import { map, metadatas } from './metadata'
import { httpHome, cState, router } from './home'

const keyGen = () => String(Math.random())
const parse = (string: string) => {
try {
let { key, data, query } = JSON.parse(string)
if (typeof data === 'string') {
try {
data = JSON.parse(data)
} catch (_) { }
return { key, data }
}
if (query) {
return { key, query }
}
} catch (_) {
return undefined
}
}

const wss = new Server({ port: 9013 })

const url = new URL('https://cluster.vtbs.moe')

console.log('ws: 9013')

const { log } = cState

wss.on('connection', (ws, request) => {
const resolveTable = new Map<string, (data: any) => void>()
const uuid = httpHome.join((url: string) => {
log('dispatch', { uuid })
const key = keyGen()
ws.send(JSON.stringify({
key,
data: {
type: 'http',
url,
},
}))
return new Promise((resolve, reject) => {
const timeout = setTimeout(reject, 1000 * 15, 'timeout')
resolveTable.set(key, data => {
clearTimeout(timeout)
resolve(data)
})
})
})

console.log('online:', httpHome.homes.size)

const { searchParams } = new URL(request.url, url)
metadatas
map.set(httpHome.homes.get(uuid), Object.fromEntries(metadatas
.map(key => [key, searchParams.get(key)])
.filter(([_, v]) => v)))

log('connect', { uuid })

ws.on('message', (message: string) => {
if (message === 'DDDhttp') {
if (httpHome.pending.length) {
log('pull', { uuid })
httpHome.pull(uuid)
} else {
ws.send('wait')
}
} else if (message === 'DDhttp') {
log('pull', { uuid })
httpHome.pull(uuid)
} else {
const json = parse(message)
if (typeof json === 'object') {
const { key, data, query } = json
if (data) {
if (resolveTable.has(key)) {
resolveTable.get(key)(data)
resolveTable.delete(key)
}
} else if (query) {
const route = router[query as keyof typeof router]
if (route) {
const result = route()
ws.send(JSON.stringify({
key,
data: {
type: 'query',
result,
},
}))
}
}
}
}
})
ws.on('close', n => {
log('close', { n, uuid })
httpHome.quit(uuid)
})
})

2 changes: 1 addition & 1 deletion state-center
Submodule state-center updated 14 files
+9 −0 .gitignore
+10 −0 .nycrc
+0 −91 api.js
+2 −0 api.ts
+0 −3 config.js
+0 −59 index.js
+1 −0 index.ts
+18 −5 package.json
+138 −0 src/api.ts
+1 −0 src/config.ts
+86 −0 src/index.ts
+0 −127 test/test.js
+191 −0 test/test.ts
+17 −0 tsconfig.json
19 changes: 19 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"target": "es2019",
"module": "commonjs",
"esModuleInterop": true,
"moduleResolution": "node",
"noImplicitAny": true,
"composite": true
},
"include": [
"src"
],
"files": [
"index.ts",
],
"references": [
{ "path": "state-center" }
]
}
117 changes: 0 additions & 117 deletions ws.js

This file was deleted.

0 comments on commit f67821a

Please sign in to comment.