/* * Copyright 2020 Adobe. All rights reserved. * This file is licensed to you under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. You may obtain a copy * of the License at http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS * OF ANY KIND, either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ "use strict"; const { AdobeIOEvents, AdobeIOEventEmitter } = require("@adobe/asset-compute-events-client"); const EventEmitter = require("events"); /** * Sent for each successfully processed and uploaded rendition. * * @typedef {Object} AssetComputeRenditionCreatedEvent * @property {String} type Event type (rendition_created) * @property {String} date Timestamp when event was sent in simplified extended ISO-8601 format. * @property {String} requestId AssetCompute job ID * @property {AssetComputeSource} source Source passed in to the process request * @property {Object} userData User data passed in to the process request (if provided) * @property {AssetComputeRendition} rendition Rendition passed in to the process request * @property {Object} metadata Rendition metadata (optional, tiff:ImageWidth, tiff:ImageHeight, repo:size) * @property {String} errorReason Rendition failure reason (if any) * @property {String} errorMessage Text giving more detail about the rendition failure (if any) */ /** * Sent for each rendition that failed to process or upload. * * @typedef {Object} AssetComputeRenditionFailedEvent * @property {String} type Event type (rendition_failed) * @property {String} date Timestamp when event was sent in simplified extended ISO-8601 format. * @property {String} requestId AssetCompute job ID * @property {AssetComputeSource} source Source passed in to the process request * @property {Object} userData User data passed in to the process request (if provided) * @property {AssetComputeRendition} rendition Rendition passed in to the process request * @property {String} errorReason Rendition failure reason (if any) * @property {String} errorMessage Text giving more detail about the rendition failure (if any) */ /** * Sent for each successfully processed and uploaded rendition. * * @event AssetCompute#rendition_created * @type {AssetComputeRenditionCreatedEvent} */ /** * Sent for each rendition that failed to process or upload. * * @event AssetCompute#rendition_failed * @type {AssetComputeRenditionFailedEvent} */ /** * Error event fired on polling failure. * * @event AssetCompute#error * @type {Error} */ class AssetComputeEventEmitter extends EventEmitter { /** * @typedef {Object} AssetComputeEventEmitterOptions * @property {String} accessToken JWT access token * @property {String} org IMS organization * @property {String} journal Journal URL * @property {Number} [interval=] Override interval at which to poll I/O events (optional) */ /** * Construct Asset Compute client * * @param {AssetComputeIntegration} integration Asset Compute Integration * @param {AssetComputeEventEmitterOptions} options Options */ constructor(options) { super(); this.accessToken = options.accessToken; this.org = options.org; this.journal = options.journal; this.interval = options.interval; // always latest, because we are listening for events that are generated by us only this.ioEvents = new AdobeIOEvents({ accessToken: this.accessToken, orgId: this.org }); this.ioEventsEmitter = new AdobeIOEventEmitter(this.ioEvents, this.journal, { latest: true, interval: this.interval }); // forward events const self = this; this.ioEventsEmitter.on('event', event => { if (event && event.event) { const assetComputeEvent = event.event; this.emit(assetComputeEvent.type, assetComputeEvent); } else { this.emit( "error", Error(`Event is missing 'event' object: ${JSON.stringify(event)}`) ); } }); this.ioEventsEmitter.on('error', error => self.emit("error", error)); } /** * Stop the AssetComputeEventEmitter */ async close() { return this.ioEventsEmitter.stop(); } } module.exports = { AssetComputeEventEmitter };