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EAAmC,GACzD,OAAO,EAAO,MAAM,KAAI,GAAQ,SAAS,UAAU,OACjD,EAAK,KAET,CAeA,eAAA,CAAgB,EAAoB,GAElC,KAAU,IADE,SAAS,KAAK,sBAAsB,EAAK,cAAe,IACnD,KAAK,OAKtB,OAAO,EAHL,QAAQ,UAAS,IAAM,KAAK,OAAO,IAIvC,CAeA,UAAA,CAAW,EAAiB,GAC1B,IAAI,EACA,EACJ,MAAM,EAAS,GACf,IAAK,EAAI,EAAG,EAAI,EAAM,OAAQ,EAAE,EAAG,GAAK,EACtC,EAAO,KAAK,EAAM,MAAM,EAAG,EAAI,IAEjC,OAAO,CACT,CAUA,WAAA,CAAY,GAEV,MADA,KAAK,KAAK,QAAS,GACb,CACR,EAGF,OAAO,QAAU","file":"/npm/keyv-dynamodb@1.1.1/lib/index.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/keyv-dynamodb@1.1.1/lib/index.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","//      \n\n             \n               \n                \n               \n                  \n            \n             \n                      \n                \n                                  \n                                                  \n                                                           \n\nconst EventEmitter = require('events');\nconst { DynamoDB } = require('aws-sdk');\nconst Promise = require('bluebird');\n\n/**\n * @var {number}\n *   Number of concurrent requests when clearing DynamoDB.\n */\nconst clearConcurrency = 100;\n\n/**\n * DynamoDB store for the keyv project.\n */\nclass KeyvDynamoDb extends EventEmitter                         {\n                   \n                     \n                    \n\n  /**\n   * Constructs a KeyvDynamoDb object.\n   *\n   * @param {Object} opts\n   *   KeyvDynamoDb options.\n   */\n  constructor(opts                     ) {\n    super();\n    this.dynamo = new DynamoDB(opts.clientOptions);\n    this.tableName = opts.tableName || '';\n    this.needsInit = true;\n    this.tableExists(this.tableName)\n      .then(isCreated => {\n        if (isCreated) {\n          this.needsInit = false;\n          this.emit('isInitialized');\n          return;\n        }\n        throw new Error(`MISSING_TABLE: The DynamoDB table ${this.tableName} does not exist.`);\n      })\n      .catch(error => {\n        this.emit('error', error);\n      });\n  }\n\n  /**\n   * @inheritDoc\n   */\n  set(key        , value     , ttl         )                {\n    try {\n      this.checkIfInitializationIsNeeded();\n    }\n    catch (error) {\n      return Promise.reject(error);\n    }\n    // Calculate the record expiration.\n    const params               = {\n      Item: {\n        Cid: DynamoDB.Converter.input(key),\n        CacheData: DynamoDB.Converter.input(value),\n      },\n      TableName: this.tableName,\n    };\n    if (ttl) {\n      const exp         = parseInt(Date.now() / 1000, 10) + ttl;\n      params.Item.Expiration = DynamoDB.Converter.input(exp);\n    }\n    return Promise.resolve()\n      .then(() => this.dynamo.putItem(params).promise())\n      .then(() => {})\n      .catch(this.handleError.bind(this));\n  }\n\n  /**\n   * @inheritDoc\n   */\n  get(key        )               {\n    try {\n      this.checkIfInitializationIsNeeded();\n    }\n    catch (error) {\n      return Promise.reject(error);\n    }\n    // Query for the data.\n    const params               = {\n      TableName: this.tableName,\n      Key: { Cid: DynamoDB.Converter.input(key) },\n    };\n    return Promise.resolve()\n      .then(() => this.dynamo.getItem(params).promise())\n      // Check if the record is expired.\n      .then(res => this.deleteIfExpired(res, key))\n      .then((res                ) => res\n        ? this.extractOutputProperty(res, 'CacheData')\n        : res\n      )\n      .catch(this.handleError.bind(this));\n  }\n\n  /**\n   * @inheritDoc\n   */\n  delete(key        )                {\n    try {\n      this.checkIfInitializationIsNeeded();\n    }\n    catch (error) {\n      return Promise.reject(error);\n    }\n    const params                  = {\n      Key: { Cid: DynamoDB.Converter.input(key) },\n      TableName: this.tableName,\n    };\n    return Promise.resolve()\n      .then(() => this.dynamo.deleteItem(params).promise())\n      .then(() => {})\n      .catch(this.handleError.bind(this));\n  }\n\n\n  /**\n   * @inheritDoc\n   */\n  clear()                {\n    try {\n      this.checkIfInitializationIsNeeded();\n    }\n    catch (error) {\n      return Promise.reject(error);\n    }\n    const params            = {\n      TableName: this.tableName,\n    };\n    return Promise.resolve()\n      .then(() => this.dynamo.scan(params).promise())\n      .then((res            ) => this.extractOutputProperties(res, 'Cid'))\n      .then((ids               ) => {\n        const deleteRequests                       = ids.map(id => ({\n          DeleteRequest: { Key: { Cid: DynamoDB.Converter.input(id) } },\n        }));\n        // Batch writing only supports 25 deletions at a time in DynamoDB.\n        const batches = this.chunkArray(deleteRequests, 25);\n        return Promise.map(\n          batches,\n          (batch => {\n            const requestItems                      = {\n              RequestItems: {\n                [this.tableName]: batch,\n              }\n            };\n            return this.dynamo.batchWriteItem(requestItems).promise();\n          }),\n          { concurrency: clearConcurrency }\n        );\n      })\n      .then(() => {})\n      .catch(this.handleError.bind(this));\n  }\n\n  /**\n   * Check if initialization is done.\n   *\n   * @throws {Error}\n   *   When the key-value store is used before initialization.\n   *\n   * @protected\n   */\n  checkIfInitializationIsNeeded() {\n    if (this.needsInit) {\n      throw new Error('The table does not exist in DynamoDB');\n    }\n  }\n\n  /**\n   * Checks if a table with a given name exists.\n   *\n   * @param {string} tableName\n   *   Then name of the table in DynamoDB.\n   *\n   * @return {Promise<boolean>}\n   *   TRUE if it exists. FALSE otherwise.\n   *\n   * @protected\n   */\n  tableExists(tableName        )                   {\n    return Promise.resolve()\n      .then(() => this.dynamo.describeTable({ TableName: tableName }).promise())\n      .then(() => true)\n      .catch(this.handleError.bind(this))\n      .catch(() => false);\n  }\n\n  /**\n   * Extracts a property from the first record.\n   *\n   * @param {QueryOutput} output\n   *   The output of a query operation.\n   * @param {string} property\n   *   The name of the property to extract.\n   *\n   * @returns {any}\n   *   The value for that property.\n   *\n   * @protected\n   */\n  extractOutputProperty(output               , property        )      {\n    if (typeof output.Item === 'undefined') {\n      return undefined;\n    }\n    return DynamoDB.Converter.output(output.Item[property]);\n  }\n\n  /**\n   * Extracts all properties from the record.\n   *\n   * @param {QueryOutput} output\n   *   The output of a query operation.\n   * @param {string} property\n   *   The name of the property to extract.\n   *\n   * @returns {any}\n   *   The value for that property.\n   *\n   * @protected\n   */\n  extractOutputProperties(output                           , property        )             {\n    return output.Items.map(item => DynamoDB.Converter.output(\n      item[property]\n    ));\n  }\n\n  /**\n   * Schedules a deletion if the record is expired.\n   *\n   * @param {GetItemOutput} res\n   *   The result of the query.\n   * @param {string} key\n   *   The key of the record to delete.\n   *\n   * @returns {GetItemOutput|undefined}\n   *   The same output if not expired. Undefined otherwise.\n   *\n   * @protected\n   */\n  deleteIfExpired(res               , key        )                 {\n    const exp = parseInt(this.extractOutputProperty(res, 'Expiration'), 10);\n    if (exp * 1000 < Date.now()) {\n      // Schedule the item deletion when the record is expired.\n      process.nextTick(() => this.delete(key));\n      return undefined;\n    }\n    return res;\n  }\n\n  /**\n   * Chunk an array into smaller arrays.\n   *\n   * @param {Array} array\n   *   The array to chunk.\n   * @param {number} chunkSize\n   *   The number of items per chunk.\n   *\n   * @returns {Array[]}\n   *   The array of chunks.\n   *\n   * @protected\n   */\n  chunkArray(array          , chunkSize        )                  {\n    let i;\n    let j;\n    const output = [];\n    for (i = 0, j = array.length; i<j; i += chunkSize) {\n      output.push(array.slice(i, i + chunkSize));\n    }\n    return output;\n  }\n\n  /**\n   * Handles a rejected promise by emitting an error and re-throwing it.\n   *\n   * @param {*} error\n   *   The error.\n   *\n   * @returns {boolean}\n   */\n  handleError(error   )       {\n    this.emit('error', error);\n    throw error;\n  }\n}\n\nmodule.exports = KeyvDynamoDb;\n"]}