aws-crt
Version:
NodeJS bindings to the aws-c-* libraries
194 lines • 7.99 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const io = __importStar(require("./io"));
const platform = __importStar(require("../common/platform"));
const auth_1 = require("./auth");
/** Creates a MqttConnectionConfig to simplify configuring a connection to IoT services */
class AwsIotMqttConnectionConfigBuilder {
constructor(tls_ctx_options) {
this.tls_ctx_options = tls_ctx_options;
this.params = {
client_id: '',
host_name: '',
socket_options: new io.SocketOptions(),
port: 8883,
use_websocket: false,
clean_session: false,
keep_alive: undefined,
will: undefined,
username: `?SDK=NodeJSv2&Version=${platform.crt_version()}`,
password: undefined,
tls_ctx: undefined,
};
}
/**
* Create a new builder with mTLS file paths
* @param cert_path - Path to certificate, in PEM format
* @param key_path - Path to private key, in PEM format
*/
static new_mtls_builder_from_path(cert_path, key_path) {
let builder = new AwsIotMqttConnectionConfigBuilder(io.TlsContextOptions.create_client_with_mtls_from_path(cert_path, key_path));
builder.params.port = 8883;
if (io.is_alpn_available()) {
builder.tls_ctx_options.alpn_list.unshift('x-amzn-mqtt-ca');
}
return builder;
}
/**
* Create a new builder with mTLS cert pair in memory
* @param cert - Certificate, in PEM format
* @param private_key - Private key, in PEM format
*/
static new_mtls_builder(cert, private_key) {
let builder = new AwsIotMqttConnectionConfigBuilder(io.TlsContextOptions.create_client_with_mtls(cert, private_key));
builder.params.port = 8883;
if (io.is_alpn_available()) {
builder.tls_ctx_options.alpn_list.unshift('x-amzn-mqtt-ca');
}
return builder;
}
/**
* Configures the connection to use MQTT over websockets. Forces the port to 443.
*/
static new_with_websockets(options) {
var _a;
let builder = new AwsIotMqttConnectionConfigBuilder(new io.TlsContextOptions());
builder.params.use_websocket = true;
builder.params.proxy_options = (_a = options) === null || _a === void 0 ? void 0 : _a.proxy_options;
if (builder.tls_ctx_options) {
builder.tls_ctx_options.alpn_list = [];
builder.params.port = 443;
}
if (options) {
builder.params.websocket_handshake_transform = (request, done) => __awaiter(this, void 0, void 0, function* () {
var _b, _c, _d, _e;
const signing_config = (_d = (_c = (_b = options).create_signing_config) === null || _c === void 0 ? void 0 : _c.call(_b), (_d !== null && _d !== void 0 ? _d : {
algorithm: auth_1.AwsSigningAlgorithm.SigV4QueryParam,
provider: options.credentials_provider,
region: options.region,
service: (_e = options.service, (_e !== null && _e !== void 0 ? _e : "iotdevicegateway")),
param_blacklist: ["x-amz-date", "x-amz-security-token"],
}));
try {
yield auth_1.aws_sign_request(request, signing_config);
done();
}
catch (error) {
done(error);
}
});
}
return builder;
}
/**
* Overrides the default system trust store.
* @param ca_dirpath - Only used on Unix-style systems where all trust anchors are
* stored in a directory (e.g. /etc/ssl/certs).
* @param ca_filepath - Single file containing all trust CAs, in PEM format
*/
with_certificate_authority_from_path(ca_dirpath, ca_filepath) {
this.tls_ctx_options.override_default_trust_store_from_path(ca_dirpath, ca_filepath);
return this;
}
/**
* Overrides the default system trust store.
* @param ca - Buffer containing all trust CAs, in PEM format
*/
with_certificate_authority(ca) {
this.tls_ctx_options.override_default_trust_store(ca);
return this;
}
/**
* Configures the IoT endpoint for this connection
* @param endpoint The IoT endpoint to connect to
*/
with_endpoint(endpoint) {
this.params.host_name = endpoint;
return this;
}
/**
* The port to connect to on the IoT endpoint
* @param port The port to connect to on the IoT endpoint. Usually 8883 for MQTT, or 443 for websockets
*/
with_port(port) {
this.params.port = port;
return this;
}
/**
* Configures the client_id to use to connect to the IoT Core service
* @param client_id The client id for this connection. Needs to be unique across all devices/clients.
*/
with_client_id(client_id) {
this.params.client_id = client_id;
return this;
}
/**
* Determines whether or not the service should try to resume prior subscriptions, if it has any
* @param clean_session true if the session should drop prior subscriptions when this client connects, false to resume the session
*/
with_clean_session(clean_session) {
this.params.clean_session = clean_session;
return this;
}
/**
* Configures MQTT keep-alive via PING messages. Note that this is not TCP keepalive.
* @param keep_alive How often in seconds to send an MQTT PING message to the service to keep the connection alive
*/
with_keep_alive_seconds(keep_alive) {
this.params.keep_alive = keep_alive;
return this;
}
/**
* Configures the TCP socket timeout (in milliseconds)
* @param timeout_ms TCP socket timeout
*/
with_timeout_ms(timeout_ms) {
this.params.timeout = timeout_ms;
return this;
}
/**
* Configures the will message to be sent when this client disconnects
* @param will The will topic, qos, and message
*/
with_will(will) {
this.params.will = will;
return this;
}
/**
* Configures the common settings for the socket to use when opening a connection to the server
* @param socket_options The socket settings
*/
with_socket_options(socket_options) {
this.params.socket_options = socket_options;
return this;
}
/**
* Returns the configured MqttConnectionConfig
* @returns The configured MqttConnectionConfig
*/
build() {
if (this.params.client_id === undefined || this.params.host_name === undefined) {
throw 'client_id and endpoint are required';
}
this.params.tls_ctx = new io.ClientTlsContext(this.tls_ctx_options);
return this.params;
}
}
exports.AwsIotMqttConnectionConfigBuilder = AwsIotMqttConnectionConfigBuilder;
//# sourceMappingURL=aws_iot.js.map