azure-iot-http-base
Advanced tools
Comparing version 1.1.6 to 1.1.7
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
export import Http = require('./lib/http'); | ||
export { Http } from './lib/http'; |
@@ -13,3 +13,3 @@ // Copyright (c) Microsoft. All rights reserved. | ||
module.exports = { | ||
Http: require('./lib/http.js') | ||
Http: require('./lib/http.js').Http | ||
}; |
@@ -1,33 +0,48 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
import { ServerResponse, ClientRequest, IncomingMessage } from 'http'; | ||
/// <reference types="node" /> | ||
import { ClientRequest, IncomingMessage } from 'http'; | ||
import { Message, X509 } from 'azure-iot-common'; | ||
declare class Http { | ||
// x509Options might be casted to done function in case it's optional and then done parameter should be null | ||
buildRequest( | ||
method: Http.Method, | ||
path: string, | ||
httpHeaders: Http.Dictionary<Http.HeaderLike>, | ||
host: string, | ||
x509Options: X509 | Http.DoneCallback, | ||
done?: Http.DoneCallback): ClientRequest; | ||
export declare type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE'; | ||
export declare type HttpCallback = (err: Error, body?: string, response?: IncomingMessage) => void; | ||
/** | ||
* @class module:azure-iot-http-base.Http | ||
* @classdesc Basic HTTP request/response functionality used by higher-level IoT Hub libraries. | ||
* You generally want to use these higher-level objects (such as [azure-iot-device-http.Http]{@link module:azure-iot-device-http.Http}) rather than this one. | ||
*/ | ||
export declare class Http { | ||
/** | ||
* @method module:azure-iot-http-base.Http.buildRequest | ||
* @description Builds an HTTP request object using the parameters supplied by the caller. | ||
* | ||
* @param {String} method The HTTP verb to use (GET, POST, PUT, DELETE...). | ||
* @param {String} path The section of the URI that should be appended after the hostname. | ||
* @param {Object} httpHeaders An object containing the headers that should be used for the request. | ||
* @param {String} host Fully-Qualified Domain Name of the server to which the request should be sent to. | ||
* @param {Function} done The callback to call when a response or an error is received. | ||
* | ||
* @returns An HTTP request object. | ||
*/ | ||
buildRequest(method: HttpMethod, path: string, httpHeaders: { | ||
[key: string]: string | string[] | number; | ||
}, host: string, x509Options: X509 | HttpCallback, done?: HttpCallback): ClientRequest; | ||
/** | ||
* @method module:azure-iot-http-base.Http.toMessage | ||
* @description Transforms the body of an HTTP response into a {@link module:azure-iot-common.Message} that can be treated by the client. | ||
* | ||
* @param {module:http.IncomingMessage} response A response as returned from the node.js http module | ||
* @param {Object} body The section of the URI that should be appended after the hostname. | ||
* | ||
* @returns {module:azure-iot-common.Message} A Message object. | ||
*/ | ||
toMessage(response: IncomingMessage, body: Message.BufferConvertible): Message; | ||
parseErrorBody(body: string): { code: string, message: string }; | ||
/** | ||
* @method module:azure-iot-http-base.Http#parseErrorBody | ||
* @description Parses the body of an error response and returns it as an object. | ||
* | ||
* @params {String} body The body of the HTTP error response | ||
* @returns {Object} An object with 2 properties: code and message. | ||
*/ | ||
static parseErrorBody(body: string): { | ||
code: string; | ||
message: string; | ||
}; | ||
} | ||
declare namespace Http { | ||
interface Dictionary<T> { | ||
[key: string]: T; | ||
} | ||
// https://nodejs.org/api/http.html#http_message_headers | ||
type HeaderLike = string | string[] | number; | ||
type Method = "GET" | "HEAD" | "POST" | "PUT" | "DELETE" | "CONNECT" | "OPTIONS" | "TRACE"; | ||
type DoneCallback = (err: Error, body?: string, response?: ServerResponse) => void; | ||
} | ||
export = Http; |
303
lib/http.js
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
'use strict'; | ||
var Message = require('azure-iot-common').Message; | ||
var debug = require('debug')('azure-iot-common.Http'); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var https_1 = require("https"); | ||
var azure_iot_common_1 = require("azure-iot-common"); | ||
var dbg = require("debug"); | ||
var debug = dbg('azure-iot-common.Http'); | ||
/** | ||
@@ -14,156 +14,149 @@ * @class module:azure-iot-http-base.Http | ||
*/ | ||
function Http() { | ||
this._https = require('https'); | ||
} | ||
/** | ||
* @method module:azure-iot-http-base.Http.buildRequest | ||
* @description Builds an HTTP request object using the parameters supplied by the caller. | ||
* | ||
* @param {String} method The HTTP verb to use (GET, POST, PUT, DELETE...). | ||
* @param {String} path The section of the URI that should be appended after the hostname. | ||
* @param {Object} httpHeaders An object containing the headers that should be used for the request. | ||
* @param {String} host Fully-Qualified Domain Name of the server to which the request should be sent to. | ||
* @param {Function} done The callback to call when a response or an error is received. | ||
* | ||
* @returns An HTTP request object. | ||
*/ | ||
/*Codes_SRS_NODE_HTTP_05_001: [buildRequest shall accept the following arguments: | ||
method - an HTTP verb, e.g., 'GET', 'POST', 'DELETE' | ||
path - the path to the resource, not including the hostname | ||
httpHeaders - an object whose properties represent the names and values of HTTP headers to include in the request | ||
host - the fully-qualified DNS hostname of the IoT hub | ||
x509Options - [optional] the x509 certificate, key and passphrase that are needed to connect to the service using x509 certificate authentication | ||
done - a callback that will be invoked when a completed response is returned from the server]*/ | ||
Http.prototype.buildRequest = function (method, path, httpHeaders, host, x509Options, done) { | ||
if (!done && (typeof x509Options === 'function')) { | ||
done = x509Options; | ||
x509Options = null; | ||
} | ||
var httpOptions = { | ||
host: host, | ||
path: path, | ||
method: method, | ||
headers: httpHeaders | ||
}; | ||
/*Codes_SRS_NODE_HTTP_16_001: [If `x509Options` is specified, the certificate, key and passphrase in the structure shall be used to authenticate the connection.]*/ | ||
if (x509Options) { | ||
httpOptions.cert = x509Options.cert; | ||
httpOptions.key = x509Options.key; | ||
httpOptions.passphrase = x509Options.passphrase; | ||
} | ||
var request = this._https.request(httpOptions, function onResponse(response) { | ||
var responseBody = ''; | ||
response.on('error', function (err) { | ||
done(err); | ||
}); | ||
response.on('data', function onResponseData(chunk) { | ||
responseBody += chunk; | ||
}); | ||
response.on('end', function onResponseEnd() { | ||
/*Codes_SRS_NODE_HTTP_05_005: [When an HTTP response is received, the callback function indicated by the done argument shall be invoked with the following arguments: | ||
err - the standard JavaScript Error object if status code >= 300, otherwise null | ||
body - the body of the HTTP response as a string | ||
response - the Node.js http.ServerResponse object returned by the transport]*/ | ||
var err = (response.statusCode >= 300) ? | ||
new Error(response.statusMessage) : | ||
null; | ||
done(err, responseBody, response); | ||
}); | ||
}); | ||
/*Codes_SRS_NODE_HTTP_05_003: [If buildRequest encounters an error before it can send the request, it shall invoke the done callback function and pass the standard JavaScript Error object with a text description of the error (err.message).]*/ | ||
request.on('error', done); | ||
/*Codes_SRS_NODE_HTTP_05_002: [buildRequest shall return a Node.js https.ClientRequest object, upon which the caller must invoke the end method in order to actually send the request.]*/ | ||
return request; | ||
}; | ||
/** | ||
* @method module:azure-iot-http-base.Http.toMessage | ||
* @description Transforms the body of an HTTP response into a {@link module:azure-iot-common.Message} that can be treated by the client. | ||
* | ||
* @param {module:http.IncomingMessage} response A response as returned from the node.js http module | ||
* @param {Object} body The section of the URI that should be appended after the hostname. | ||
* | ||
* @returns {module:azure-iot-common.Message} A Message object. | ||
*/ | ||
Http.prototype.toMessage = function toMessage(response, body) { | ||
var msg; | ||
/*Codes_SRS_NODE_HTTP_05_006: [If the status code of the HTTP response < 300, toMessage shall create a new azure-iot-common.Message object with data equal to the body of the HTTP response.]*/ | ||
if (response.statusCode < 300) { | ||
msg = new Message(body); | ||
for (var item in response.headers) { | ||
if (item.search("iothub-") !== -1) { | ||
/*Codes_SRS_NODE_HTTP_05_007: [If the HTTP response has an 'iothub-messageid' header, it shall be saved as the messageId property on the created Message.]*/ | ||
if (item.toLowerCase() === "iothub-messageid") { | ||
msg.messageId = response.headers[item]; | ||
var Http = (function () { | ||
function Http() { | ||
} | ||
/** | ||
* @method module:azure-iot-http-base.Http.buildRequest | ||
* @description Builds an HTTP request object using the parameters supplied by the caller. | ||
* | ||
* @param {String} method The HTTP verb to use (GET, POST, PUT, DELETE...). | ||
* @param {String} path The section of the URI that should be appended after the hostname. | ||
* @param {Object} httpHeaders An object containing the headers that should be used for the request. | ||
* @param {String} host Fully-Qualified Domain Name of the server to which the request should be sent to. | ||
* @param {Function} done The callback to call when a response or an error is received. | ||
* | ||
* @returns An HTTP request object. | ||
*/ | ||
/*Codes_SRS_NODE_HTTP_05_001: [buildRequest shall accept the following arguments: | ||
method - an HTTP verb, e.g., 'GET', 'POST', 'DELETE' | ||
path - the path to the resource, not including the hostname | ||
httpHeaders - an object whose properties represent the names and values of HTTP headers to include in the request | ||
host - the fully-qualified DNS hostname of the IoT hub | ||
x509Options - [optional] the x509 certificate, key and passphrase that are needed to connect to the service using x509 certificate authentication | ||
done - a callback that will be invoked when a completed response is returned from the server]*/ | ||
Http.prototype.buildRequest = function (method, path, httpHeaders, host, x509Options, done) { | ||
if (!done && (typeof x509Options === 'function')) { | ||
done = x509Options; | ||
x509Options = undefined; | ||
} | ||
/*Codes_SRS_NODE_HTTP_05_008: [If the HTTP response has an 'iothub-to' header, it shall be saved as the to property on the created Message.]*/ | ||
else if (item.toLowerCase() === "iothub-to") { | ||
msg.to = response.headers[item]; | ||
var httpOptions = { | ||
host: host, | ||
path: path, | ||
method: method, | ||
headers: httpHeaders | ||
}; | ||
/*Codes_SRS_NODE_HTTP_16_001: [If `x509Options` is specified, the certificate, key and passphrase in the structure shall be used to authenticate the connection.]*/ | ||
if (x509Options) { | ||
httpOptions.cert = x509Options.cert; | ||
httpOptions.key = x509Options.key; | ||
httpOptions.passphrase = x509Options.passphrase; | ||
} | ||
/*Codes_SRS_NODE_HTTP_05_009: [If the HTTP response has an 'iothub-expiry' header, it shall be saved as the expiryTimeUtc property on the created Message.]*/ | ||
else if (item.toLowerCase() === "iothub-expiry") { | ||
msg.expiryTimeUtc = response.headers[item]; | ||
var httpReq = https_1.request(httpOptions, function (response) { | ||
var responseBody = ''; | ||
response.on('error', function (err) { | ||
done(err); | ||
}); | ||
response.on('data', function (chunk) { | ||
responseBody += chunk; | ||
}); | ||
response.on('end', function () { | ||
/*Codes_SRS_NODE_HTTP_05_005: [When an HTTP response is received, the callback function indicated by the done argument shall be invoked with the following arguments: | ||
err - the standard JavaScript Error object if status code >= 300, otherwise null | ||
body - the body of the HTTP response as a string | ||
response - the Node.js http.IncomingMessage object returned by the transport]*/ | ||
var err = (response.statusCode >= 300) ? | ||
new Error(response.statusMessage) : | ||
null; | ||
done(err, responseBody, response); | ||
}); | ||
}); | ||
/*Codes_SRS_NODE_HTTP_05_003: [If buildRequest encounters an error before it can send the request, it shall invoke the done callback function and pass the standard JavaScript Error object with a text description of the error (err.message).]*/ | ||
httpReq.on('error', done); | ||
/*Codes_SRS_NODE_HTTP_05_002: [buildRequest shall return a Node.js https.ClientRequest object, upon which the caller must invoke the end method in order to actually send the request.]*/ | ||
return httpReq; | ||
}; | ||
/** | ||
* @method module:azure-iot-http-base.Http.toMessage | ||
* @description Transforms the body of an HTTP response into a {@link module:azure-iot-common.Message} that can be treated by the client. | ||
* | ||
* @param {module:http.IncomingMessage} response A response as returned from the node.js http module | ||
* @param {Object} body The section of the URI that should be appended after the hostname. | ||
* | ||
* @returns {module:azure-iot-common.Message} A Message object. | ||
*/ | ||
Http.prototype.toMessage = function (response, body) { | ||
var msg; | ||
/*Codes_SRS_NODE_HTTP_05_006: [If the status code of the HTTP response < 300, toMessage shall create a new azure-iot-common.Message object with data equal to the body of the HTTP response.]*/ | ||
if (response.statusCode < 300) { | ||
msg = new azure_iot_common_1.Message(body); | ||
for (var item in response.headers) { | ||
if (item.search('iothub-') !== -1) { | ||
if (item.toLowerCase() === 'iothub-messageid') { | ||
/*Codes_SRS_NODE_HTTP_05_007: [If the HTTP response has an 'iothub-messageid' header, it shall be saved as the messageId property on the created Message.]*/ | ||
msg.messageId = response.headers[item]; | ||
} | ||
else if (item.toLowerCase() === 'iothub-to') { | ||
/*Codes_SRS_NODE_HTTP_05_008: [If the HTTP response has an 'iothub-to' header, it shall be saved as the to property on the created Message.]*/ | ||
msg.to = response.headers[item]; | ||
} | ||
else if (item.toLowerCase() === 'iothub-expiry') { | ||
/*Codes_SRS_NODE_HTTP_05_009: [If the HTTP response has an 'iothub-expiry' header, it shall be saved as the expiryTimeUtc property on the created Message.]*/ | ||
msg.expiryTimeUtc = response.headers[item]; | ||
} | ||
else if (item.toLowerCase() === 'iothub-correlationid') { | ||
/*Codes_SRS_NODE_HTTP_05_010: [If the HTTP response has an 'iothub-correlationid' header, it shall be saved as the correlationId property on the created Message.]*/ | ||
msg.correlationId = response.headers[item]; | ||
} | ||
else if (item.search('iothub-app-') !== -1) { | ||
/*Codes_SRS_NODE_HTTP_13_001: [ If the HTTP response has a header with the prefix iothub-app- then a new property with the header name and value as the key and value shall be added to the message. ]*/ | ||
msg.properties.add(item, response.headers[item]); | ||
} | ||
} | ||
else if (item.toLowerCase() === 'etag') { | ||
/*Codes_SRS_NODE_HTTP_05_011: [If the HTTP response has an 'etag' header, it shall be saved as the lockToken property on the created Message, minus any surrounding quotes.]*/ | ||
// Need to strip the quotes from the string | ||
var len = response.headers[item].length; | ||
msg.lockToken = response.headers[item].substring(1, len - 1); | ||
} | ||
} | ||
} | ||
/*Codes_SRS_NODE_HTTP_05_010: [If the HTTP response has an 'iothub-correlationid' header, it shall be saved as the correlationId property on the created Message.]*/ | ||
else if (item.toLowerCase() === "iothub-correlationid") { | ||
msg.correlationId = response.headers[item]; | ||
return msg; | ||
}; | ||
/** | ||
* @method module:azure-iot-http-base.Http#parseErrorBody | ||
* @description Parses the body of an error response and returns it as an object. | ||
* | ||
* @params {String} body The body of the HTTP error response | ||
* @returns {Object} An object with 2 properties: code and message. | ||
*/ | ||
Http.parseErrorBody = function (body) { | ||
var result = null; | ||
try { | ||
var jsonErr = JSON.parse(body); | ||
var errParts = jsonErr.Message.split(';'); | ||
var errMessage = errParts[1]; | ||
var errCode = errParts[0].split(':')[1]; | ||
if (!!errCode && !!errMessage) { | ||
result = { | ||
message: errMessage, | ||
code: errCode | ||
}; | ||
} | ||
} | ||
/*Codes_SRS_NODE_HTTP_13_001: [ If the HTTP response has a header with the prefix iothub-app- then a new property with the header name and value as the key and value shall be added to the message. ]*/ | ||
else if(item.search("iothub-app-") !== -1) { | ||
msg.properties.add(item, response.headers[item]); | ||
catch (err) { | ||
if (err instanceof SyntaxError) { | ||
debug('Could not parse error body: Invalid JSON'); | ||
} | ||
else if (err instanceof TypeError) { | ||
debug('Could not parse error body: Unknown body format'); | ||
} | ||
else { | ||
throw err; | ||
} | ||
} | ||
} | ||
/*Codes_SRS_NODE_HTTP_05_011: [If the HTTP response has an 'etag' header, it shall be saved as the lockToken property on the created Message, minus any surrounding quotes.]*/ | ||
else if (item.toLowerCase() === "etag") { | ||
// Need to strip the quotes from the string | ||
var len = response.headers[item].length; | ||
msg.lockToken = response.headers[item].substring(1, len - 1); | ||
} | ||
} | ||
} | ||
return msg; | ||
}; | ||
/** | ||
* @method module:azure-iot-http-base.Http#parseErrorBody | ||
* @description Parses the body of an error response and returns it as an object. | ||
* | ||
* @params {String} body The body of the HTTP error response | ||
* @returns {Object} An object with 2 properties: code and message. | ||
*/ | ||
Http.parseErrorBody = function parseError (body) { | ||
var result = null; | ||
try { | ||
var jsonErr = JSON.parse(body); | ||
var errParts = jsonErr.Message.split(';'); | ||
var errMessage = errParts[1]; | ||
var errCode = errParts[0].split(':')[1]; | ||
if(!!errCode && !!errMessage) { | ||
result = { | ||
message: errMessage, | ||
code: errCode | ||
}; | ||
} | ||
} catch (err) { | ||
if (err instanceof SyntaxError) { | ||
debug('Could not parse error body: Invalid JSON'); | ||
} else if (err instanceof TypeError) { | ||
debug('Could not parse error body: Unknown body format'); | ||
} else { | ||
throw err; | ||
} | ||
} | ||
return result; | ||
}; | ||
module.exports = Http; | ||
return result; | ||
}; | ||
return Http; | ||
}()); | ||
exports.Http = Http; | ||
//# sourceMappingURL=http.js.map |
{ | ||
"name": "azure-iot-http-base", | ||
"version": "1.1.6", | ||
"version": "1.1.7", | ||
"description": "HTTP operations used by Azure IoT device and service SDKs", | ||
@@ -10,3 +10,3 @@ "author": "Microsoft Corporation", | ||
"dependencies": { | ||
"azure-iot-common": "1.1.6", | ||
"azure-iot-common": "1.1.7", | ||
"debug": "^2.2.0" | ||
@@ -19,7 +19,9 @@ }, | ||
"mocha": "^3.0.1", | ||
"tslint": "^3.14.0", | ||
"typescript": "^1.8.10" | ||
"tslint": "^5.1.0", | ||
"typescript": "2.2.2", | ||
"@types/node": "^7.0.12" | ||
}, | ||
"scripts": { | ||
"lint": "jshint --show-non-errors . && tslint -c ../../../tslint.json \"{lib,.}/*.ts\"", | ||
"lint": "tslint --type-check --project . -c ../../../tslint.json", | ||
"build": "tsc", | ||
"unittest-min": "istanbul cover --report none node_modules/mocha/bin/_mocha -- --reporter dot test/_*_test.js", | ||
@@ -29,4 +31,4 @@ "alltest-min": "istanbul cover --report none node_modules/mocha/bin/_mocha -- --reporter dot test/_*_test*.js", | ||
"alltest": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter spec test/_*_test*.js", | ||
"ci": "npm -s run lint && npm -s run alltest-min && npm -s run check-cover", | ||
"test": "npm -s run lint && npm -s run unittest", | ||
"ci": "npm -s run lint && npm -s run build && npm -s run alltest-min && npm -s run check-cover", | ||
"test": "npm -s run lint && npm -s run build && npm -s run unittest", | ||
"check-cover": "istanbul check-coverage --statements 51 --branches 37 --functions 37 --lines 51" | ||
@@ -33,0 +35,0 @@ }, |
Sorry, the diff of this file is not supported yet
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
15192
9
239
7
+ Addedazure-iot-common@1.1.7(transitive)
- Removedazure-iot-common@1.1.6(transitive)
Updatedazure-iot-common@1.1.7