-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Migrate to TypeScript #4
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
cdca80d
init
shigma 369010c
update state-center
shigma 78712b7
migrate
shigma 8816203
Update .gitignore
simon300000 f0208d7
use relative state-center
simon300000 8405972
add ts build command
simon300000 509a993
type fix
simon300000 5b721ec
move to src/
simon300000 4ad6c65
clear @ts-ignore
simon300000 48a686b
噔噔咚 reverse
simon300000 7d5004c
only one router
simon300000 7f4fdd7
use composite
simon300000 0a08f82
type @type
simon300000 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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" } | ||
] | ||
} |
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
必须要这样,不然就