From 53665b21cc65a6af8af702bab5593f5466073fc5 Mon Sep 17 00:00:00 2001 From: Thomas Chia Date: Thu, 12 Oct 2017 07:12:54 +0800 Subject: [PATCH] [aws-iot-device-sdk] - Fix references to the mqtt types (#20344) * Update index.d.ts Fix mqtt type references. * Make the tests compatible with noImplicitAny: true. * Update version. * Add package.json for mqtt type dependency. * Remove mqtt in favour of original package definitions. * Update notNeededPackages. * Add private flag to package.json. * Trigger travis * Update subscribe options. * Fix subscribe callback. --- notNeededPackages.json | 6 + .../aws-iot-device-sdk-tests.ts | 14 +- types/aws-iot-device-sdk/index.d.ts | 18 +- types/aws-iot-device-sdk/package.json | 6 + types/mqtt/index.d.ts | 448 ------------------ types/mqtt/mqtt-tests.ts | 14 - types/mqtt/tsconfig.json | 23 - 7 files changed, 27 insertions(+), 502 deletions(-) create mode 100644 types/aws-iot-device-sdk/package.json delete mode 100644 types/mqtt/index.d.ts delete mode 100644 types/mqtt/mqtt-tests.ts delete mode 100644 types/mqtt/tsconfig.json diff --git a/notNeededPackages.json b/notNeededPackages.json index 0c6f29e3f4..d3ba44c871 100644 --- a/notNeededPackages.json +++ b/notNeededPackages.json @@ -378,6 +378,12 @@ "sourceRepoURL": "http://www.mendix.com", "asOfVersion": "0.8.1" }, + { + "libraryName": "MQTT", + "typingsPackageName": "mqtt", + "sourceRepoURL": "https://github.com/mqttjs/MQTT.js", + "asOfVersion": "2.5.0" + }, { "libraryName": "mobservable", "typingsPackageName": "mobservable", diff --git a/types/aws-iot-device-sdk/aws-iot-device-sdk-tests.ts b/types/aws-iot-device-sdk/aws-iot-device-sdk-tests.ts index bcb407c352..66655f8dd3 100644 --- a/types/aws-iot-device-sdk/aws-iot-device-sdk-tests.ts +++ b/types/aws-iot-device-sdk/aws-iot-device-sdk-tests.ts @@ -13,7 +13,6 @@ const device = new awsIot.device({ clientId: "", region: "", baseReconnectTimeMs: 1000, - keepalive: 10, protocol: "wss", port: 443, host: "", @@ -39,7 +38,7 @@ device console.log("offline"); }); device - .on("error", function(error) { + .on("error", function(error: Error | string) { console.log("error", error); }); device @@ -55,7 +54,6 @@ const thingShadows = new awsIot.thingShadow({ clientId: "", region: "", baseReconnectTimeMs: 1000, - keepalive: 10, protocol: "mqtts", port: 0, host: "", @@ -65,10 +63,10 @@ const thingShadows = new awsIot.thingShadow({ thingShadows.register( "thingName", { ignoreDeltas: false }, - (err: Error, failedTopics: mqtt.Granted[]) => { } + (err: Error, failedTopics: mqtt.ISubscriptionGrant[]) => { } ); - thingShadows.subscribe("topic", {}, (error: any, granted: mqtt.Granted) => {}); + thingShadows.subscribe("topic", { qos: 1 }, (error: any, granted: mqtt.ISubscriptionGrant[]) => {}); thingShadows.on("connect", function() { console.log("connected to AWS IoT"); @@ -87,7 +85,7 @@ const thingShadows = new awsIot.thingShadow({ console.log("offline"); }); - thingShadows.on("error", function(error) { + thingShadows.on("error", function(error: Error) { console.log("error", error); }); @@ -98,8 +96,8 @@ const thingShadows = new awsIot.thingShadow({ thingShadows.on("status", function(thingName: string, stat: "accepted" | "rejected", clientToken: string, stateObject: any) { }); - thingShadows.on("delta", function(thingName, stateObject) { + thingShadows.on("delta", function(thingName: string, stateObject: any) { }); - thingShadows.on("timeout", function(thingName, clientToken) { + thingShadows.on("timeout", function(thingName: string, clientToken: string) { }); diff --git a/types/aws-iot-device-sdk/index.d.ts b/types/aws-iot-device-sdk/index.d.ts index 4844ae9012..38cf00840d 100644 --- a/types/aws-iot-device-sdk/index.d.ts +++ b/types/aws-iot-device-sdk/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for aws-iot-device-sdk 1.0.13 +// Type definitions for aws-iot-device-sdk 2.1.0 // Project: https://github.com/aws/aws-iot-device-sdk-js // Definitions by: Markus Olsson // Margus Lamp @@ -9,7 +9,7 @@ import * as mqtt from "mqtt"; import * as WebSocket from "ws"; -export interface DeviceOptions extends mqtt.ClientOptions { +export interface DeviceOptions extends mqtt.IClientOptions { /** the AWS IoT region you will operate in (default "us-east-1") */ region?: string; @@ -179,7 +179,7 @@ export class device extends NodeJS.EventEmitter { * @param publish options * @param called when publish succeeds or fails */ - publish(topic: string, message: Buffer | string, options?: mqtt.ClientPublishOptions, callback?: (error?: Error) => void): mqtt.Client; + publish(topic: string, message: Buffer | string, options?: mqtt.IClientPublishOptions, callback?: (error?: Error) => void): mqtt.Client; /** * Subscribe to a topic or topics @@ -187,7 +187,7 @@ export class device extends NodeJS.EventEmitter { * @param the options to subscribe with * @param callback fired on suback */ - subscribe(topic: string | string[] | mqtt.Topic, options?: mqtt.ClientSubscribeOptions, callback?: mqtt.ClientSubscribeCallback): mqtt.Client; + subscribe(topic: string | string[], options?: mqtt.IClientSubscribeOptions, callback?: mqtt.ClientSubscribeCallback): mqtt.Client; /** * Unsubscribe from a topic or topics @@ -196,7 +196,7 @@ export class device extends NodeJS.EventEmitter { * @param options * @param callback fired on unsuback */ - unsubscribe(topic: string | string[], options?: mqtt.ClientSubscribeOptions, callback?: mqtt.ClientSubscribeCallback): mqtt.Client; + unsubscribe(topic: string | string[], options?: mqtt.IClientSubscribeOptions, callback?: mqtt.ClientSubscribeCallback): mqtt.Client; /** * end - close connection @@ -257,7 +257,7 @@ export class thingShadow extends NodeJS.EventEmitter { * for all shadow topics). Applications should wait until shadow * registration is complete before performing update/get/delete operations. */ - register(thingName: string, options?: RegisterOptions, callback?: (error: Error, failedTopics: mqtt.Granted[]) => void): void + register(thingName: string, options?: RegisterOptions, callback?: (error: Error, failedTopics: mqtt.ISubscriptionGrant[]) => void): void /** * Unregister interest in the Thing Shadow named thingName. @@ -334,7 +334,7 @@ export class thingShadow extends NodeJS.EventEmitter { * @param options * @param callback */ - publish(topic: string, message: Buffer | string, options?: mqtt.ClientPublishOptions, callback?: Function): mqtt.Client; + publish(topic: string, message: Buffer | string, options?: mqtt.IClientPublishOptions, callback?: Function): mqtt.Client; /** * Subscribe to a topic or topics @@ -342,7 +342,7 @@ export class thingShadow extends NodeJS.EventEmitter { * @param the options to subscribe with * @param callback fired on suback */ - subscribe(topic: string | string[] | mqtt.Topic, options?: mqtt.ClientSubscribeOptions, callback?: mqtt.ClientSubscribeCallback): mqtt.Client; + subscribe(topic: string | string[], options?: { qos: 0 | 1 }, callback?: mqtt.ClientSubscribeCallback): mqtt.Client; /** * Unsubscribe from a topic or topics @@ -351,7 +351,7 @@ export class thingShadow extends NodeJS.EventEmitter { * @param options * @param callback fired on unsuback */ - unsubscribe(topic: string | string[], options?: mqtt.ClientSubscribeOptions, callback?: mqtt.ClientSubscribeCallback): mqtt.Client; + unsubscribe(topic: string | string[], options?: mqtt.IClientSubscribeOptions, callback?: mqtt.ClientSubscribeCallback): mqtt.Client; /** * end - close connection diff --git a/types/aws-iot-device-sdk/package.json b/types/aws-iot-device-sdk/package.json new file mode 100644 index 0000000000..9aceff7832 --- /dev/null +++ b/types/aws-iot-device-sdk/package.json @@ -0,0 +1,6 @@ +{ + "private": true, + "dependencies": { + "mqtt": "^2.13.0" + } +} diff --git a/types/mqtt/index.d.ts b/types/mqtt/index.d.ts deleted file mode 100644 index 09c04f6236..0000000000 --- a/types/mqtt/index.d.ts +++ /dev/null @@ -1,448 +0,0 @@ -// Type definitions for MQTT -// Project: https://github.com/mqttjs/MQTT.js -// Definitions by: Pekka Leppänen -// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped - -/// - -declare namespace mqtt { - - import ReadableStream = NodeJS.ReadableStream; - import EventEmitter = NodeJS.EventEmitter; - - interface Packet { - messageId: number; - [key: string]: any; - } - - interface Granted { - /** - * is a subscribed to topic - */ - topic: string; - /** - * is the granted qos level on it - */ - qos: number; - } - - interface Topic { - /** - * object which has topic names as object keys and as value the QoS, like {'test1': 0, 'test2': 1}. - */ - [topic: string]: number; - } - - /** - * MQTT CLIENT - */ - - interface ClientOptions extends SecureClientOptions { - /** - * 10 seconds, set to 0 to disable - */ - keepalive?: number; - - /** - * 'mqttjs_' + Math.random().toString(16).substr(2, 8) - */ - clientId?: string; - /** - * 'MQTT' - */ - protocolId?: string; - /** - * 4 - */ - protocolVersion?: number; - /** - * true, set to false to receive QoS 1 and 2 messages while offline - */ - clean?: boolean; - /** - * 1000 milliseconds, interval between two reconnections - */ - reconnectPeriod?: number; - /** - * 30 * 1000 milliseconds, time to wait before a CONNACK is received - */ - connectTimeout?: number; - /** - * the username required by your broker, if any - */ - username?: string; - /** - * the password required by your broker, if any - */ - password?: string; - /** - * a Store for the incoming packets - */ - incomingStore?: Store; - /** - * a Store for the outgoing packets - */ - outgoingStore?: Store; - /** - * a message that will sent by the broker automatically when the client disconnect badly. - */ - will?: { - /** - * the topic to publish - */ - topic: string; - /** - * the message to publish - */ - payload: string; - /** - * the QoS - */ - qos: number; - /** - * the retain flag - */ - retain: boolean; - }; - - } - - interface SecureClientOptions { - /** - * path to private key - */ - keyPath?: string; - /** - * path to corresponding public cert - */ - certPath?: string; - rejectUnauthorized?: boolean; - } - - interface ClientPublishOptions { - /** - * the QoS - */ - qos?: number; - /** - * the retain flag - */ - retain?: boolean; - } - - interface ClientSubscribeOptions { - /** - * the QoS - */ - qos?: number; - } - - interface ClientSubscribeCallback { - (err: any, granted: Granted): void; - } - - /** - * @deprecated use connect() instead - * Create a new IClient (see: IClient) - * - * @param port - broker port (default: 1883) - * @param host - broker host (default: localhost) - * @param options - connect options - */ - function createClient(port?: number, host?: string, options?: ClientOptions): Client; - - /** - * @deprecated use connect() instead - * Create a new secure IClient - * - * @param port - * @param host - * @param options - connection options, must include keys. - */ - function createSecureClient(port?: number, host?: string, options?: SecureClientOptions): Client; - - /** - * Create a new MqttClient (see: IClient) - * - * The brokerUrl supports normal connections using mqtt:// or tcp:// and secure connections using mqtts:// or ssl://. - * - * Passing the clientId is also supported, for example mqtt://user@localhost?clientId=123abc. - * - * @param brokerUrl - * @param options - */ - function connect(brokerUrl: string, options?: ClientOptions): Client; - - /** - * The Client class wraps a client connection to an MQTT broker over an arbitrary transport method (TCP, TLS, WebSocket, ecc). - * - * Client automatically handles the following: - * - Regular server pings - * - QoS flow - * - Automatic reconnections - * - Start publishing before being connected - * - */ - interface Client extends EventEmitter { - (streamBuilder: any, options: ClientOptions): Client; - - /** - * Publish a message to a topic - * - * @param topic - * @param message - * @param options - * @param callback - */ - publish(topic: string, message: Buffer, options?: ClientPublishOptions, callback?: Function): Client; - publish(topic: string, message: string, options?: ClientPublishOptions, callback?: Function): Client; - - /** - * Subscribe to a topic or topics - * @param topic to subscribe to or an Array of topics to subscribe to. It can also be an object. - * @param the options to subscribe with - * @param callback fired on suback - */ - subscribe(topic: string, options?: ClientSubscribeOptions, callback?: ClientSubscribeCallback): Client; - subscribe(topic: string[], options?: ClientSubscribeOptions, callback?: ClientSubscribeCallback): Client; - subscribe(topic: Topic, options?: ClientSubscribeOptions, callback?: ClientSubscribeCallback): Client; - - /** - * Unsubscribe from a topic or topics - * - * @param topic is a String topic or an array of topics to unsubscribe from - * @param options - * @param callback fired on unsuback - */ - unsubscribe(topic: string, options?: ClientSubscribeOptions, callback?: ClientSubscribeCallback): Client; - unsubscribe(topic: string[], options?: ClientSubscribeOptions, callback?: ClientSubscribeCallback): Client; - - /** - * end - close connection - * - * @param force passing it to true will close the client right away, without waiting for the in-flight messages to be acked. - * This parameter is optional. - * @param callback - */ - end(force?: boolean, callback?: Function): Client; - - /** - * Handle messages with backpressure support, one at a time. Override at will, but always call callback, or the client will - * hang. - * - * @param packet - * @param callback - */ - handleMessage(packet: Packet, callback: Function): Client; - - /** - * get last message id. This is for sent messages only. - */ - getLastMessageId(): number; - } - - /** - * STORE - */ - - /** - * In-memory implementation of the message store. - * - * Another implementaion is mqtt-level-store which uses Level-browserify to store the inflight data, - * making it usable both in Node and the Browser. - */ - interface Store { - /** - * Adds a packet to the store, a packet is anything that has a messageId property. The callback is called when the packet has - * been stored. - * @param packet - * @param callback - */ - put(packet: Packet, callback: Function): Store; - - /** - * get a packet from the store - * - * @param packet - * @param callback - */ - get(packet: Packet, callback: Function): Store; - - /** - * Creates a stream with all the packets in the store. - */ - createStream(): ReadableStream; - - /** - * Removes a packet from the store, a packet is anything that has a messageId property. The callback is called when the packet - * has been removed. - * @param packet - * @param callback - */ - del(packet: Packet, callback: Function): Store; - - /** - * Closes the Store. - * @param callback - */ - close(callback: Function): void; - - } - - /** - * CONNECTION - */ - - /** - * @deprecated use mqtt-connect instead - * Create a new MqttConnection (see: MqttConnection) - * - * @param port - broker port (default: 1883) - * @param host - broker host (default: localhost) - * @param callback - fired on underlying stream connect - */ - function createConnection(port?: number, host?: string, callback?: Function): Connection; - - interface ConnectOptions { - - /** - * Protocol ID, usually MQIsdp. - */ - protocolId?: string; - /** - * Protocol version, usually 3. - */ - protocolVersion?: number; - /** - * keepalive period in seconds. - */ - keepalive?: number; - /** - * client ID. - */ - clientId?: string; - /** - * the client's will message options - */ - will?: { - /** - * the topic to publish - */ - topic: string; - /** - * the message to publish - */ - payload: string; - /** - * the QoS - */ - qos: number; - /** - * the retain flag - */ - retain: boolean; - }; - /** - * the 'clean start' flag. - */ - clean?: boolean; - /** - * username for protocol v3.1. - */ - username?: string; - /** - * password for protocol v3.1. - */ - password?: string; - } - - interface ConnectionPublishOptions { - /** - * the message ID of the packet, required if qos > 0. - */ - messageId?: number; - /** - * the topic to publish - */ - topic?: string; - /** - * the message to publish - */ - payload?: string; - /** - * the QoS - */ - qos?: number; - /** - * the retain flag - */ - retain?: boolean; - } - - /** - * The MqttConnection class represents a raw MQTT connection, both on the server and on the client side. For client side - * operations, it is strongly recommended that MqttClient is used, as MqttConnection requires a great deal of additional - * boilerplate such as setting up error handling and ping request/responses. - * - * If such fine grained control is required, MqttConnection can be instantiated using the mqtt.createConnection method. - * - * MqttServerClient is an unaltered subclass of MqttConnection and can be used in exactly the same way. - * - * @link https://github.com/mqttjs/MQTT.js/wiki/connection - * - */ - interface Connection extends EventEmitter { - /** - * Send an MQTT connect packet. - * @param options - */ - connect(options?: ConnectOptions): Connection; - /** - * Send an MQTT connack packet. - * @param options - */ - connack(options?: { returnCode: number; }): Connection; - /** - * Send an MQTT publish packet. - * @param options - */ - publish(options?: ConnectionPublishOptions): Connection; - } - - /** - * SERVER - */ - - /** - * @deprecated use connect instead - * Create a new MqttServer (see : IServer) - * - * @param listener - callback called on server client event - */ - function createServer(listener?: Function): Server; - - /** - * @deprecated use connect instead - * Create a new MqttSecureServer - * @param keyPath - path to private key file - * @param certPath - path to corresponding public cert - * @param listener - callback called on server client event - */ - function createSecureServer(keyPath: string, certPath: string, listener?: Function): Server; - - /** - * The primary methods of instantiating mqtt.js server classes are through the mqtt.createServer and mqtt.createSecureServer - * methods. The former returns an instance of MqttServer and and the latter returns an instance of MqttSecureServer. - * - * While it is possible to instantiate these classes through new MqttServer(), it is strongly recommended to use the factory - * methods - * - * @link https://github.com/mqttjs/MQTT.js/wiki/server - */ - interface Server extends EventEmitter { - - } - -} - -export = mqtt; diff --git a/types/mqtt/mqtt-tests.ts b/types/mqtt/mqtt-tests.ts deleted file mode 100644 index 4022616c03..0000000000 --- a/types/mqtt/mqtt-tests.ts +++ /dev/null @@ -1,14 +0,0 @@ -import mqtt = require('mqtt'); - -var client: mqtt.Client = mqtt.connect('mqtt://test.mosquitto.org'); - -client.on('connect', () => { - client.subscribe('presence'); - client.publish('presence', 'Hello mqtt'); -}); - -client.on('message', (topic: string, message: Buffer) => { - // message is Buffer - console.log(message.toString()); - client.end(); -}); diff --git a/types/mqtt/tsconfig.json b/types/mqtt/tsconfig.json deleted file mode 100644 index 7149db58c4..0000000000 --- a/types/mqtt/tsconfig.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "compilerOptions": { - "module": "commonjs", - "lib": [ - "es6" - ], - "noImplicitAny": true, - "noImplicitThis": true, - "strictNullChecks": false, - "strictFunctionTypes": true, - "baseUrl": "../", - "typeRoots": [ - "../" - ], - "types": [], - "noEmit": true, - "forceConsistentCasingInFileNames": true - }, - "files": [ - "index.d.ts", - "mqtt-tests.ts" - ] -} \ No newline at end of file