-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from dd-center/shigma/ts
Migrate to TypeScript
- Loading branch information
Showing
10 changed files
with
172 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import "./src" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import './ws' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) | ||
|
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" } | ||
] | ||
} |