aws-iot-device-sdk-v2
Version:
NodeJS API for the AWS IoT service
142 lines • 5.68 kB
JavaScript
;
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ServiceError = exports.StreamingOperation = void 0;
const events_1 = require("events");
const aws_crt_1 = require("aws-crt");
const mqtt_request_response_utils = __importStar(require("./mqtt_request_response_utils"));
class StreamingOperation extends events_1.EventEmitter {
constructor(config) {
super();
// validate
let streamingOperationModel = config.serviceModel.streamingOperations.get(config.operationName);
if (!streamingOperationModel) {
throw new aws_crt_1.CrtError("NYI");
}
let validator = config.serviceModel.shapeValidators.get(streamingOperationModel.inputShapeName);
if (!validator) {
throw new aws_crt_1.CrtError("NYI");
}
validator(config.modelConfig);
let streamOptions = {
subscriptionTopicFilter: streamingOperationModel.subscriptionGenerator(config.modelConfig),
};
// create native operation
this.deserializer = streamingOperationModel.deserializer;
this.operation = config.client.createStream(streamOptions);
this.operation.addListener(aws_crt_1.mqtt_request_response.StreamingOperationBase.SUBSCRIPTION_STATUS, this.onSubscriptionStatusChanged.bind(this));
this.operation.addListener(aws_crt_1.mqtt_request_response.StreamingOperationBase.INCOMING_PUBLISH, this.onIncomingPublish.bind(this));
}
static create(config) {
let operation = new StreamingOperation(config);
return operation;
}
open() {
this.operation.open();
}
close() {
this.operation.close();
}
on(event, listener) {
super.on(event, listener);
return this;
}
onSubscriptionStatusChanged(eventData) {
setImmediate(() => __awaiter(this, void 0, void 0, function* () {
this.emit(StreamingOperation.SUBSCRIPTION_STATUS, eventData);
}));
}
onIncomingPublish(eventData) {
try {
let message = this.deserializer(eventData.payload);
setImmediate(() => __awaiter(this, void 0, void 0, function* () {
this.emit(StreamingOperation.INCOMING_PUBLISH, {
message: message,
});
}));
}
catch (error) {
let serviceError = mqtt_request_response_utils.createServiceError(error.toString());
setImmediate(() => __awaiter(this, void 0, void 0, function* () {
this.emit(StreamingOperation.INCOMING_PUBLISH_ERROR, {
payload: eventData.payload,
error: serviceError,
});
}));
}
}
}
exports.StreamingOperation = StreamingOperation;
/**
* Event emitted when the stream's subscription status changes.
*
* Listener type: {@link mqtt_request_response.SubscriptionStatusListener}
*
* @event
*/
StreamingOperation.SUBSCRIPTION_STATUS = 'subscriptionStatus';
/**
* Event emitted when a stream message is received
*
* Listener type: {@link IncomingPublishListener}
*
* @event
*/
StreamingOperation.INCOMING_PUBLISH = 'incomingPublish';
/**
* Event emitted when a stream message is received but handling it resulted in an error
*
* Listener type: {@link IncomingPublishErrorListener}
*
* @event
*/
StreamingOperation.INCOMING_PUBLISH_ERROR = 'incomingPublishError';
class ServiceError extends Error {
/** @internal */
constructor(options) {
super(options.description);
if (options.internalError) {
this.internalError = options.internalError;
}
if (options.modeledError) {
this.modeledError = options.modeledError;
}
}
}
exports.ServiceError = ServiceError;
//# sourceMappingURL=mqtt_request_response.js.map