Skip to content

Commit

Permalink
feat: 3.0.0-alpha.0
Browse files Browse the repository at this point in the history
  • Loading branch information
taoyuan committed Jul 28, 2020
1 parent 96c259b commit c6899b6
Show file tree
Hide file tree
Showing 55 changed files with 8,335 additions and 5,315 deletions.
5 changes: 5 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules/
dist/
coverage/
/sandbox
**/*.d.ts
7 changes: 7 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
extends: ['@tib/eslint-config/eslintrc.js'],
rules: {
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/naming-convention': 'off',
},
};
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* text=auto
* text=auto eol=lf
13 changes: 10 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
.idea
node_modules
npm-debug.log
.DS_Store
coverage
.idea
.nyc_output
examples
lib
*.tgz
.sandbox
*.tsbuildinfo
dist

# ESLint cache
.eslintcache
28 changes: 28 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# This file is a template, and might need editing before it works on your project.
# Official framework image. Look for the different tagged releases at:
# https://hub.docker.com/r/library/node/tags/
image: node:latest

# This folder is cached between builds
# http://docs.gitlab.com/ce/ci/yaml/README.html#cache
cache:
paths:
- node_modules/

before_script:
- npm ci

test:
script:
- npm run test:ci

coverage:
script:
- npm run coverage
coverage: /Statements\s+:\s(\d+.?\d+)%/
artifacts:
when: always
paths:
# save coverage results
- coverage
expire_in: 10 days
4 changes: 4 additions & 0 deletions .mocharc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
...require('@tib/build/config/.mocharc.json'),
timeout: 10000,
};
4 changes: 4 additions & 0 deletions .np-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"yarn": false,
"yolo": true
}
3 changes: 3 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package-lock=true
scripts-prepend-node-path=true

13 changes: 13 additions & 0 deletions .nycrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"include": [
"dist"
],
"extension": [
".js",
".ts"
],
"reporter": [
"html"
],
"exclude-after-remap": false
}
7 changes: 7 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.github/
/coverage
**/.sandbox
dist
*.json
CHANGELOG.md
*.yml
8 changes: 8 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"bracketSpacing": false,
"singleQuote": true,
"printWidth": 80,
"trailingComma": "all",
"arrowParens": "avoid",
"useTabs": false
}
62 changes: 62 additions & 0 deletions .renovaterc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"extends": [
":gitSignOff",
"group:monorepos",
"group:recommended",
"helpers:disableTypesNodeMajor"
],

"ignorePaths": [
"node_modules/**",
"**/__tests__/**",
"**/test/**"
],

"automerge": false,
"branchPrefix": "renovate/",
"ignoreUnstable": true,
"rangeStrategy": "bump",
"statusCheckVerify": true,
"updateNotScheduled": true,

"lockFileMaintenance": {
"enabled": true,
"schedule": "before 5am on monday"
},

"prConcurrentLimit": 5,
"prCreation": "immediate",
"prHourlyLimit": 2,

"semanticCommits": true,
"semanticCommitType": "chore",
"semanticCommitScope": null,

"separateMajorMinor": true,
"separateMinorPatch": false,

"packageRules": [{
"packageNames": [
"@microsoft/api-extractor",
"@microsoft/api-documenter"
],
"groupName": "api-extractor packages"
},
{
"packageNames": [
"@types/express",
"@types/express-serve-static-core"
],
"groupName": "@types/express* packages"
}
],

"travis": {
"enabled": true,
"supportPolicy": ["lts", "current"]
},

"masterIssue": true,
"masterIssueApproval": false,
"masterIssueAutoclose": true
}
10 changes: 7 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
language: node_js
node_js:
- v8
- v7
- v6
- v14
- v12
- v10
install:
npm ci
script:
npm run test:ci && npm run coverage:ci
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2015 taoyuan <towyuan@outlook.com>
Copyright (c) 2015-2020 Yvan Tao <towyuan@outlook.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
150 changes: 69 additions & 81 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,101 +1,89 @@
# mqttr [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][daviddm-image]][daviddm-url] [![Coverage percentage][coveralls-image]][coveralls-url]
# mqttr

[![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url]
[![Dependency Status][daviddm-image]][daviddm-url]
[![Coverage percentage][coveralls-image]][coveralls-url]

> A routable mqtt library based on mqtt.js
## Installation

```sh
$ npm install --save mqttr
$ npm i mqttr
```

<a name="usage"></a>
## Usage

```js
var mqttr = require('mqttr');

// You should start a mqtt server at 1883 before or after run this script
var client = mqttr.connect('mqtt://localhost');

client.on('connect', function () {
console.log('connect');
});

client.on('reconnect', function () {
console.log('reconnect');
});

client.on('close', function () {
console.log('close');
});

client.on('offline', function () {
console.log('offline');
});

client.on('error', function (err) {
throw err;
});

// full params handler
client.subscribe('/users/:userid/message/:messageid/*', function (topic, payload, message) {
console.log('-------------------------------------------------');
console.log('topic :', topic); // /users/taoyuan/message/4321/ping
console.log('message:', payload); // { hello: 'world' }
console.log('params :', message.params); // { userid: 'taoyuan', messageid: 4321 }
console.log('slats :', message.splats); // [ 'ping' ]
console.log('path :', message.path); // '/users/:userid/message/:messageid/:method'
console.log('packet :', message.packet); // {...} packet received packet, as defined in mqtt-packet
console.log();
});

// one context param handler
client.subscribe('/users/:userid/message/:messageid/*', function (message) {
console.log('-------------------------------------------------');
console.log(message);
console.log();
});

client.ready(function () {
client.publish('/users/taoyuan/message/4321/ping', {hello: 'world'});

setTimeout(function () {
client.end();
}, 10);
});

```


<a name="cli"></a>
## Command Line Tools

`mqttr` bundles a command to interact with a broker.
In order to have it available on your path, you should install `mqttr` globally:

```sh
npm install mqttr -g
```

Then, on one terminal

```
mqttr sub -t 'hello' -h 'test.mosquitto.org' -v
```typescript
import {connect, Message} from 'mqttr';

// eslint-disable-next-line @typescript-eslint/no-floating-promises
(async () => {
// You should start a mqtt server at 1883 before or after run this script
const client = connect('mqtt://localhost');

client.on('connect', function () {
console.log('connect');
});

client.on('reconnect', function () {
console.log('reconnect');
});

client.on('close', function () {
console.log('close');
});

client.on('offline', function () {
console.log('offline');
});

client.on('error', function (err: Error) {
throw err;
});

// full params handler
await client.subscribe(
'/users/:userId/message/:messageId/:splats*',
(topic: string, payload: any, message?: Message) => {
message = message!;
console.log('-------------------------------------------------');
console.log('topic :', topic); // => /users/yvan/message/4321/ping
console.log('message:', payload); // => { hello: '霸王龙' }
console.log('params :', message.params); // => { userId: 'yvan', messageId: 4321, splats: [ 'ping' ] }
console.log('path :', message.path); // => '/users/:userId/message/:messageId/:splats*'
console.log('packet :', message.packet); // => {...} packet received packet, as defined in mqtt-packet
console.log();
},
);

// one context param handler
await client.subscribe(
'/users/:userId/message/:messageId/:splats*',
(message: Message) => {
console.log('-------------------------------------------------');
console.log(message);
console.log();
},
);

await client.ready();

await client.publish('/users/yvan/message/4321/ping', {hello: '霸王龙'});

// eslint-disable-next-line @typescript-eslint/no-misused-promises
setTimeout(() => client.end(true), 10);
})();
```

On another

```
mqttr pub -t 'hello' -h 'test.mosquitto.org' -m 'from mqttr'
```

See `mqttr help <command>` for the command help.
## Topic Patterns

See [path-to-regexp](https://github.com/pillarjs/path-to-regexp)

## License

MIT © [taoyuan](towyuan#outlook.com)


[npm-image]: https://badge.fury.io/js/mqttr.svg
[npm-url]: https://npmjs.org/package/mqttr
[travis-image]: https://travis-ci.org/taoyuan/mqttr.svg?branch=master
Expand Down

0 comments on commit c6899b6

Please sign in to comment.