Skip to content
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

TypeScript inside the package #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions mqttsn.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { EventEmitter } from "events";
import { BaseMQTTSNPacket, MQTTSNPacket } from "./packet";

type OnPacketFn = <T extends MQTTSNPacket = MQTTSNPacket>(packet: T) => void;

interface Parser extends EventEmitter {
parse(buf: Buffer): number;

on(event: "packet", listener: OnPacketFn): this;
off(event: "packet", listener: OnPacketFn): this;
once(event: "packet", listener: OnPacketFn): this;
addListener(event: "packet", listener: OnPacketFn): this;
prependListener(event: "packet", listener: OnPacketFn): this;
prependOnceListener(event: "packet", listener: OnPacketFn): this;
listeners(event: "packet"): Function[];
rawListeners(event: "packet"): Function[];
}

export var parser: {
new(opts?: { isClient?: boolean }): Parser;
};
export function generate<P extends BaseMQTTSNPacket = BaseMQTTSNPacket>(packet: P): Buffer;
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.0.3",
"description": "Parse and generate MQTT-SN packets",
"main": "mqttsn.js",
"typings": "./mqttsn.d.ts",
"scripts": {
"test": "tape test.js | tap-dot"
},
Expand Down Expand Up @@ -30,10 +31,12 @@
},
"homepage": "https://github.com/ithinuel/mqttsn-packet",
"devDependencies": {
"@types/node": "^14.14.22",
"pre-commit": "^1.1.2",
"readable-stream": "^2.0.4",
"tap-dot": "^2.0.0",
"tape": "^4.2.2"
"tape": "^4.2.2",
"typescript": "^4.1.3"
},
"dependencies": {
"bl": "^4.0.0"
Expand Down
198 changes: 198 additions & 0 deletions packet.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
type ReturnCodeString =
| "Accepted"
| "Rejected: congestion"
| "Rejected: invalid topic ID"
| "Rejected: not supported";

type QOS = number;

type TopicType =
| "normal"
| "pre-defined"
| "short topic";

interface BaseMQTTSNPacket {
cmd: "advertise" | "searchgw" | "gwinfo" | "connect" | "connack" | "willtopicresp" | "willmsgresp" | "willtopicreq"
| "willmsgreq" | "pingresp" | "willtopic" | "willtopicupd" | "willmsg" | "willmsgupd" | "register" | "regack"
| "publish" | "puback" | "pubcomp" | "pubrec" | "pubrel" | "unsuback" | "unsubscribe" | "subscribe" | "suback"
| "pingreq" | "disconnect";

length?: number;
}

interface GenericAck extends BaseMQTTSNPacket {
returnCode: ReturnCodeString | null;
}

interface Advertise extends BaseMQTTSNPacket {
cmd: "advertise";
gwId: number;
duration: number;
}

interface SearchGw extends BaseMQTTSNPacket {
cmd: "searchgw";
radius: number;
}

interface GwInfo extends BaseMQTTSNPacket {
cmd: "gwinfo";
gwId: number;
gwAdd?: Buffer;
}

interface Connect extends BaseMQTTSNPacket {
cmd: "connect";
duration: number;
clientId: string;
will: boolean;
cleanSession: boolean;
}

interface ConnAck extends GenericAck {
cmd: "connack";
}

interface WillTopic extends BaseMQTTSNPacket {
cmd: "willtopic";
qos?: QOS;
retain?: boolean;
willTopic?: string;
}

interface WillTopicResp extends GenericAck {
cmd: "willtopicresp";
}

interface WillTopicUpd extends BaseMQTTSNPacket {
cmd: "willtopicupd";
qos?: QOS;
retain?: boolean;
willTopic?: string;
}

interface WillMsg extends BaseMQTTSNPacket {
cmd: "willmsg";
willMsg: string;
}

interface WillMsgResp extends GenericAck {
cmd: "willmsgresp";
}

interface WillMsgUpd extends BaseMQTTSNPacket {
cmd: "willmsgupd";
willMsg: string;
}

interface Register extends BaseMQTTSNPacket {
cmd: "register";
topicId: number;
msgId: number;
topicName: string;
}

interface RegAck extends GenericAck {
cmd: "regack";
topicId: number;
msgId: number;
}

interface Publish<TT extends TopicType, TID> extends BaseMQTTSNPacket {
cmd: "publish";
dup: boolean;
qos: QOS;
topicIdType: TT;
topicId: TID;
msgId: number;
payload: Buffer;
}

type PublishShortTopic = Publish<"short topic", string>;
type PublishPredefined = Publish<"pre-defined", number>;
type PublishRegisteredTopic = Publish<"normal", number>;

interface PubAck extends GenericAck {
cmd: "puback";
topicId: number;
msgId: number;
}

interface PubComp extends BaseMQTTSNPacket {
cmd: "pubcomp";
msgId: number;
}

interface PubRec extends BaseMQTTSNPacket {
cmd: "pubrec";
msgId: number;
}

interface PubRel extends BaseMQTTSNPacket {
cmd: "pubrel";
msgId: number;
}

interface UnsubAck extends BaseMQTTSNPacket {
cmd: "unsuback";
msgId: number;
}

interface Unsubscribe<TT extends TopicType, TN, TID> extends BaseMQTTSNPacket {
cmd: "unsubscribe";
msgId: number;
topicIdType: TT;
topicName: TN;
topicId: TID;
}

type UnsubscribePredefined = Unsubscribe<"pre-defined", undefined, number>;
type UnsubscribeRegisteredTopic = Unsubscribe<"normal", undefined, number>;
type UnsubscribeShortTopic = Unsubscribe<"short topic", string, undefined>;

interface Subscribe<TT extends TopicType, TN, TID> extends BaseMQTTSNPacket {
cmd: "subscribe";
msgId: number;
topicIdType: TT;
topicName: TN;
topicId: TID;
dup: boolean;
qos: QOS;
}

declare type SubscribePredefined = Subscribe<"pre-defined", undefined, number>;
declare type SubscribeRegisteredTopic = Subscribe<"normal", undefined, number>;
declare type SubscribeShortTopic = Subscribe<"short topic", string, undefined>;

interface SubAck extends GenericAck {
cmd: "suback";
topicId: number;
msgId: number;
qos: QOS;
}

interface PingReq extends BaseMQTTSNPacket {
cmd: "pingreq";
clientId?: string;
}

interface Disconnect extends BaseMQTTSNPacket {
cmd: "disconnect";
duration?: number;
}


export type MQTTSNPacket =
| Advertise | SearchGw | GwInfo
| Connect | Disconnect | ConnAck
| WillMsg | WillMsgUpd | WillTopic | WillTopicUpd | WillMsgResp | WillTopicResp
| Register
| RegAck
| PublishPredefined | PublishRegisteredTopic | PublishShortTopic
| PubAck | PubComp | PubRec | PubRel
| SubscribePredefined | SubscribeRegisteredTopic | SubscribeShortTopic
| SubAck
| UnsubscribePredefined | UnsubscribeRegisteredTopic | UnsubscribeShortTopic
| UnsubAck
| PingReq
| BaseMQTTSNPacket;
69 changes: 69 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */

/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "es2019", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
// "declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
// "outDir": "./", /* Redirect output structure to the directory. */
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
// "removeComments": true, /* Do not emit comments to output. */
// "noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */

/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */

/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */

/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */

/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */

/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */

/* Advanced Options */
"skipLibCheck": true, /* Skip type checking of declaration files. */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
}
}