Skip to content

Commit 659e13b

Browse files
committed
Prettier format
1 parent 3391da4 commit 659e13b

18 files changed

+1687
-136
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist

.eslintrc.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"env": {
3+
"es2021": true,
4+
"mocha": true,
5+
"node": true
6+
},
7+
"extends": ["standard-with-typescript", "prettier"],
8+
"parserOptions": {
9+
"ecmaVersion": 2021,
10+
"project": "tsconfig.json",
11+
"sourceType": "module"
12+
},
13+
"plugins": ["@typescript-eslint"],
14+
"rules": {
15+
"@typescript-eslint/explicit-function-return-type": "off",
16+
"@typescript-eslint/naming-convention": "off",
17+
"@typescript-eslint/no-misused-promises": "off",
18+
"@typescript-eslint/no-namespace": "off",
19+
"@typescript-eslint/restrict-plus-operands": "off",
20+
"@typescript-eslint/restrict-template-expressions": "off",
21+
"@typescript-eslint/strict-boolean-expressions": "off"
22+
}
23+
}

.github/workflows/build.yml

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
1-
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
2-
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
3-
41
name: Build
52

63
on:
74
push:
85
branches:
96
- '**'
10-
pull_request:
11-
branches:
12-
- '**'
137

148
jobs:
15-
169
build-check:
17-
1810
name: Build check
1911

2012
runs-on: ubuntu-latest
@@ -23,4 +15,5 @@ jobs:
2315
- uses: actions/checkout@v2
2416

2517
- run: yarn
26-
- run: yarn build
18+
- run: yarn lint
19+
- run: yarn build

.github/workflows/release.yml

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
1-
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
2-
# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages
3-
41
name: Release
52

63
on:
74
release:
85
types:
9-
- created
6+
- published
107

118
jobs:
12-
13-
publish-npm:
14-
15-
name: NPM Publish
9+
publish:
10+
name: Publish
1611

1712
if: github.repository_owner == 'coajs'
1813

@@ -26,20 +21,26 @@ jobs:
2621
node-version: 12
2722
registry-url: https://registry.npmjs.org
2823

29-
- name: Build
24+
- name: Yarn
3025
run: |
3126
yarn
32-
npm --no-git-tag-version version ${{ github.event.release.tag_name }}
33-
npm run build
27+
28+
- name: Lint
29+
run: |
30+
yarn lint
31+
32+
- name: Build
33+
run: |
34+
yarn version --no-git-tag-version --new-version ${{ github.event.release.tag_name }}
35+
yarn build
3436
3537
- name: Publish
3638
env:
3739
NODE_AUTH_TOKEN: ${{ secrets.AEX_NPM_TOKEN }}
3840
run: |
39-
cd dist
40-
npm publish
41+
yarn publish dist
4142
4243
- name: Sync
4344
run: |
4445
sleep 5s
45-
npm run sync
46+
yarn sync

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist

.prettierrc.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"printWidth": 160,
3+
"semi": false,
4+
"singleQuote": true
5+
}

README.md

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
[![npm downloads](https://img.shields.io/npm/dm/coa-tcp.svg?style=flat-square)](http://npm-stat.com/charts.html?package=coa-tcp)
66
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://github.com/coajs/coa-tcp/pulls)
77

8-
一个轻量的TCP服务框架,是COA核心库组成之一
8+
一个轻量的 TCP 服务框架,是 COA 核心库组成之一
99

10-
可以快速创建一个TCP服务,支持单片机或物联网设备、跨平台TCP应用的连接
10+
可以快速创建一个 TCP 服务,支持单片机或物联网设备、跨平台 TCP 应用的连接
1111

1212
## 特性
1313

1414
- **简单轻量** 基于 `Node.js` 内置的 [Net](https://nodejs.org/api/net.html) 模块,简单轻量,不依赖于其他第三方库
1515
- **自动管理连接池** 自动维护管理客户端连接池,无需关心连接和释放的问题,专注于收发消息和业务逻辑的开发
16-
- **TypeScript** 全部使用TypeScript书写,类型约束、IDE友好
16+
- **TypeScript** 全部使用 TypeScript 书写,类型约束、IDE 友好
1717

1818
## 快速开始
1919

@@ -38,7 +38,7 @@ const tcpServer = new CoaTcp(clientPool, 5000)
3838
tcpServer.start()
3939
```
4040

41-
当控制台输出类似提示,则说明TCP服务已经正常启动,可以接受客户端的连接了
41+
当控制台输出类似提示,则说明 TCP 服务已经正常启动,可以接受客户端的连接了
4242

4343
```shell
4444
[TCP] Listening on port 5000
@@ -52,30 +52,28 @@ import { CoaClient, CoaClientPool, CoaTcp } from 'coa-tcp'
5252

5353
// 自定义客户端
5454
class CustomClient extends CoaClient {
55-
5655
// 接收到数据
57-
async onData (raw: Buffer) {
56+
async onData(raw: Buffer) {
5857
// 收到数据后要处理的事情
5958
}
6059

6160
// 上线
62-
async onOnline (deviceId: string) {
61+
async onOnline(deviceId: string) {
6362
super.onOnline(deviceId)
6463
// 客户端上线要处理的事情
6564
}
6665

6766
// 下线
68-
async onOffline (deviceId: string) {
67+
async onOffline(deviceId: string) {
6968
super.onOffline(deviceId)
7069
// 客户端下线要处理的事情
7170
}
7271
}
7372

7473
// 自定义客户端连接池
75-
class CustomClientPool extends CoaClientPool<CustomClient>{
76-
74+
class CustomClientPool extends CoaClientPool<CustomClient> {
7775
// 生成一个自定义的客户端
78-
newClient (socket: Socket) {
76+
newClient(socket: Socket) {
7977
return new CustomClient(socket, `custom-id-${++this.increment}`, 'CustomClient')
8078
}
8179
}
@@ -88,4 +86,4 @@ const tcpServer = new CoaTcp(clientPool, 5000)
8886

8987
// 启动服务
9088
tcpServer.start()
91-
```
89+
```

package.json

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,25 @@
1818
"scripts": {
1919
"dev": "tsc -w",
2020
"build": "rm -rf dist && tsc && cp package.json *.md dist && rm -rf dist/test",
21-
"sync": "curl -X PUT 'https://npm.taobao.org/sync/coa-tcp?sync_upstream=true'",
22-
"publish-prerelease": "yarn build && yarn version --prerelease && cp package.json *.md dist && cd dist && rm -rf test && npm publish",
23-
"test": "NODE_PATH=run node dist/test"
21+
"lint": "eslint .",
22+
"prettier": "prettier -w .",
23+
"test": "NODE_PATH=run node dist/test",
24+
"sync": "curl -X PUT 'https://npm.taobao.org/sync/coa-tcp?sync_upstream=true'"
2425
},
2526
"dependencies": {
26-
"coa-echo": "^1.0.8",
27-
"coa-helper": "^1.1.1"
27+
"coa-echo": "^1.1.1",
28+
"coa-helper": "^1.2.1"
2829
},
2930
"devDependencies": {
30-
"@types/node": "^14.14.37",
31-
"typescript": "^4.2.4"
31+
"@types/node": "^15.12.2",
32+
"@typescript-eslint/eslint-plugin": "^4.26.1",
33+
"eslint": "^7.28.0",
34+
"eslint-config-prettier": "^8.3.0",
35+
"eslint-config-standard-with-typescript": "^20.0.0",
36+
"eslint-plugin-import": "^2.23.4",
37+
"eslint-plugin-node": "^11.1.0",
38+
"eslint-plugin-promise": "^5.1.0",
39+
"prettier": "^2.3.1",
40+
"typescript": "^4.3.2"
3241
}
3342
}

src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
export { CoaClient } from './service/CoaClient'
22
export { CoaClientPool } from './service/CoaClientPool'
33
export { CoaTcp } from './service/CoaTcp'
4-

src/service/CoaApplication.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,38 @@ import { CoaClient } from './CoaClient'
55
import { CoaClientPool } from './CoaClientPool'
66

77
export class CoaApplication<T extends CoaClient> {
8-
98
private readonly listenPort: number
109
private readonly clientPool: CoaClientPool<T>
1110
private readonly tcpServer: Server
1211

13-
constructor (clientPool: CoaClientPool<T>, listenPort: number) {
12+
constructor(clientPool: CoaClientPool<T>, listenPort: number) {
1413
this.clientPool = clientPool
1514
this.listenPort = listenPort
16-
this.tcpServer = createServer(socket => this.connectionListener(socket))
15+
this.tcpServer = createServer((socket) => this.connectionListener(socket))
1716
}
1817

1918
// 监听
20-
start () {
19+
start() {
2120
this.tcpServer.listen(this.listenPort, () => {
2221
echo.cyan('[TCP] Listening on port ' + this.listenPort)
2322
})
2423
}
2524

2625
// 监听连接事件
27-
private connectionListener (socket: Socket) {
26+
private connectionListener(socket: Socket) {
2827
let client: T
2928
// 只有首次收到数据,才开始初始化一个客户端
30-
socket.on('data', async raw => {
31-
if (!client)
32-
client = await this.clientPool.connect(socket)
29+
socket.on('data', async (raw) => {
30+
if (!client) client = await this.clientPool.connect(socket)
3331
await client.onData(raw)
3432
})
3533
// 关闭连接的时候销毁数据
3634
socket.on('close', async () => {
37-
if (client)
38-
await this.clientPool.close(client)
35+
if (client) await this.clientPool.close(client)
3936
})
4037
socket.on('error', _.noop)
4138
// 如果超过30秒没有任何响应,则断开连接
4239
socket.on('timeout', () => socket.end(() => socket.destroy()))
4340
socket.setTimeout(30 * 1000)
4441
}
45-
46-
47-
48-
}
42+
}

src/service/CoaClient.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { _ } from 'coa-helper'
33
import { Socket } from 'net'
44

55
export class CoaClient {
6-
76
socket: Socket
87
clientId: string
98
deviceId: string
@@ -12,7 +11,7 @@ export class CoaClient {
1211
isWorking: boolean
1312
live: number
1413

15-
constructor (socket: Socket, clientId: string, type: string) {
14+
constructor(socket: Socket, clientId: string, type: string) {
1615
this.socket = socket
1716
this.type = type
1817
this.clientId = clientId
@@ -23,29 +22,22 @@ export class CoaClient {
2322
}
2423

2524
// 连接
26-
async onConnect () {
27-
}
25+
async onConnect() {}
2826

2927
// 连接关闭
30-
async onClose () {
31-
}
28+
async onClose() {}
3229

3330
// 设备上线
34-
async onOnline (deviceId: string) {
35-
}
31+
async onOnline(deviceId: string) {}
3632

3733
// 设备下线
38-
async onOffline (deviceId: string) {
39-
}
34+
async onOffline(deviceId: string) {}
4035

4136
// 接收到数据
42-
async onData (raw: Buffer) {
43-
44-
}
37+
async onData(raw: Buffer) {}
4538

4639
// 设置设备状态
47-
async setDevice ({ deviceId }: { deviceId: string }) {
48-
40+
async setDevice({ deviceId }: { deviceId: string }) {
4941
// 记录上次的设备ID
5042
const lastDeviceId = this.deviceId + ''
5143

@@ -65,4 +57,4 @@ export class CoaClient {
6557
}
6658
return this.live
6759
}
68-
}
60+
}

0 commit comments

Comments
 (0)