Init: Create & Init dayu Project...

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
2019-07-03 15:50:59 +08:00
commit c47137ec4a
92 changed files with 2793 additions and 0 deletions

4
packages/common/.gitignore vendored Normal file
View File

@@ -0,0 +1,4 @@
/node_modules
/dist
/package-lock.json
/yarn.lock

View File

@@ -0,0 +1,22 @@
src
test
typings
bundled
build
coverage
docs
wiki
gulpfile.js
bower.json
karma.conf.js
tsconfig.json
typings.json
CONTRIBUTING.md
ISSUE_TEMPLATE.md
PULL_REQUEST_TEMPLATE.md
tslint.json
wallaby.js
.travis.yml
.gitignore
.vscode
type_definitions

11
packages/common/README.md Normal file
View File

@@ -0,0 +1,11 @@
# `@dayu/common`
> TODO: description
## Usage
```
const core = require('@dayu/common');
// TODO: DEMONSTRATE API
```

View File

@@ -0,0 +1,27 @@
{
"name": "@dayu/common",
"version": "0.0.1",
"description": "> TODO: description",
"author": "MiaoWoo <admin@yumc.pw>",
"homepage": "https://github.com/circlecloud/dayu",
"license": "ISC",
"main": "dist/index.js",
"publishConfig": {
"registry": "https://repo.yumc.pw/repository/npm-hosted/"
},
"scripts": {
"dev": "npx ts-node-dev src/index.ts",
"clean": "rimraf dist",
"watch": "npx tsc --watch",
"build": "yarn clean && npx tsc",
"test": "echo \"Error: run tests from root\" && exit 1"
},
"dependencies": {
"axios": "^0.19.0"
},
"devDependencies": {
"rimraf": "^2.6.3",
"ts-node-dev": "^1.0.0-pre.40",
"typescript": "^3.5.2"
}
}

View File

@@ -0,0 +1,28 @@
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/socket.io-client@2.2.0/dist/socket.io.js"> </script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/xterm@3.12.2/dist/xterm.css" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/xterm@3.12.2/dist/addons/fullscreen/fullscreen.css">
<script src="https://cdn.jsdelivr.net/npm/xterm@3.12.2/dist/xterm.js"></script>
<script src="https://cdn.jsdelivr.net/npm/xterm@3.12.2/dist/addons/fit/fit.js"></script>
<script src="https://cdn.jsdelivr.net/npm/xterm@3.12.2/dist/addons/attach/attach.js"></script>
<script src="https://cdn.jsdelivr.net/npm/xterm@3.12.2/dist/addons/fullscreen/fullscreen.js"></script>
<style>
#terminal-container .terminal.xterm {
height: 100%;
}
#terminal-container .xterm-viewport {
height: 100% !important;
}
</style>
</head>
<body>
<div id="terminal" style="height: 100%;"></div>
<script type="text/javascript" src="js/index.js"></script>
</body>
</html>

View File

@@ -0,0 +1,68 @@
let command = '';
Terminal.applyAddon(fit);
Terminal.applyAddon(attach);
Terminal.applyAddon(fullscreen);
var term = new Terminal({
experimentalCharAtlas: 'dynamic',
cursorBlink: false,
});
term.open(document.getElementById('terminal'));
term.toggleFullScreen();
term.fit();
window.onresize = function() {
term.fit();
}
let query = {}
location.search.substring(1).split("&").forEach(q => {
let qy = q.split("=", 2);
query[qy[0]] = qy[1]
})
console.log(query);
var socket = io('/container', {
path: '/ws',
transports: ['websocket']
});
socket.on('connect', () => {
term.writeln('connect')
if (query.action) {
term.writeln(`Recover Action: ${query.action} Data: ${query.data}`)
switch (query.action) {
case "container":
socket.emit('logs', {
id: query.data,
since: Date.now() / 1000 - 60 * 15,
until: Date.now() / 1000,
stderr: false,
tail: "all"
})
break;
default:
}
}
});
term.on('data', async data => {
if (data == '\t') {
return;
}
term.write(data);
if (data == '\r') {
term.write('\n');
socket.emit('logs', {
id: command
})
command = '';
} else {
command += data;
}
});
socket.on('message', data => {
term.write(data.toString() + '\r\n');
});
socket.on('disconnect', () => {
term.reset();
term.writeln('disconnect');
});

View File

@@ -0,0 +1,68 @@
import * as http from 'http'
import axios, { AxiosResponse, AxiosRequestConfig, Method, AxiosInstance } from 'axios'
class HttpClient {
private api: AxiosInstance;
constructor() {
const instanceConfig: AxiosRequestConfig = {
headers: {
'Content-Type': 'application/json'
},
timeout: 5000
}
if (process.env.DOCKER_HOST.startsWith("/")) {
instanceConfig.socketPath = process.env.DOCKER_HOST
} else {
instanceConfig.baseURL = process.env.DOCKER_HOST
}
this.api = axios.create(instanceConfig)
}
async get<T = any>(path: string, data?: object): Promise<T> {
return await this.handle<T>("GET", path, { params: data });
}
async post<T = any>(path: string, data?: object): Promise<T> {
return await this.handle<T>("POST", path, { data });
}
async stream<T = http.ServerResponse>(path: string, data?: object): Promise<T> {
return await this.handle<T>("GET", path, { params: data, responseType: "stream" });
}
async handle<T>(method: Method, path: string, reqConfig?: AxiosRequestConfig): Promise<T> {
let config: AxiosRequestConfig = {
method,
url: path,
};
let startTime = Date.now();
Object.assign(config, reqConfig)
let response: AxiosResponse;
try {
response = await this.api.request(config);
return response.data as T
} catch (ex) {
if (!ex.response) { throw ex; }
response = ex.response;
if (this.isStream(response)) {
let stream = response.data;
response.data = await new Promise<T>((resolve, reject) => {
let cache = '';
stream.on('data', (chunk: ArrayBuffer) => { cache += chunk.toString() })
stream.on('end', () => { resolve(JSON.parse(cache) as T); })
})
}
throw new Error(JSON.stringify(response.data));
} finally {
if (response) {
console.log(`============== API Invoke ==============
REQUEST METHOD : ${method}
REQUEST PATH : ${axios.getUri(config)}
REQUEST PARAMS : ${JSON.stringify(config.params || {})}
REQUEST BODY : ${JSON.stringify(config.data || {})}
RESPONSE BODY : ${this.isStream(response) ? '<Stream>' : JSON.stringify(response.data)}
HANDLE TIME : ${Date.now() - startTime}ms
========================================`);
}
}
}
isStream(response: AxiosResponse) {
return toString.call(response.data.pipe) === "[object Function]";
}
}

View File

@@ -0,0 +1 @@
export * from './api'

View File

@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"baseUrl": "src",
"outDir": "dist"
}
}