Source: encrypt/producer-db.js

  1. /**
  2. * Copyright (C) 2015-2016 Regents of the University of California.
  3. * @author: Jeff Thompson <jefft0@remap.ucla.edu>
  4. * @author: From ndn-group-encrypt src/producer-db https://github.com/named-data/ndn-group-encrypt
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. * A copy of the GNU Lesser General Public License is in the file COPYING.
  19. */
  20. /** @ignore */
  21. var SyncPromise = require('../util/sync-promise.js').SyncPromise;
  22. /**
  23. * ProducerDb is a base class the storage of keys for the producer. It contains
  24. * one table that maps time slots (to the nearest hour) to the content key
  25. * created for that time slot. A subclass must implement the methods. For
  26. * example, see Sqlite3ProducerDb (for Nodejs) or IndexedDbProducerDb (for the
  27. * browser).
  28. * @note This class is an experimental feature. The API may change.
  29. * @constructor
  30. */
  31. var ProducerDb = function ProducerDb()
  32. {
  33. };
  34. exports.ProducerDb = ProducerDb;
  35. /**
  36. * Create a new ProducerDb.Error to report an error using ProducerDb
  37. * methods, wrapping the given error object.
  38. * Call with: throw new ProducerDb.Error(new Error("message")).
  39. * @constructor
  40. * @param {Error} error The exception created with new Error.
  41. */
  42. ProducerDb.Error = function ProducerDbError(error)
  43. {
  44. if (error) {
  45. // Copy lineNumber, etc. from where new Error was called.
  46. for (var prop in error)
  47. this[prop] = error[prop];
  48. // Make sure these are copied.
  49. this.message = error.message;
  50. this.stack = error.stack;
  51. }
  52. }
  53. /**
  54. * Check if a content key exists for the hour covering timeSlot.
  55. * @param {number} timeSlot The time slot as milliseconds since Jan 1, 1970 UTC.
  56. * @param {boolean} useSync (optional) If true then return a SyncPromise which
  57. * is already fulfilled. If omitted or false, this may return a SyncPromise or
  58. * an async Promise.
  59. * @return {Promise|SyncPromise} A promise that returns true if there is a
  60. * content key for timeSlot (else false), or that is rejected with
  61. * ProducerDb.Error for a database error.
  62. */
  63. ProducerDb.prototype.hasContentKeyPromise = function(timeSlot, useSync)
  64. {
  65. return SyncPromise.reject(new Error
  66. ("ProducerDb.hasContentKeyPromise is not implemented"));
  67. };
  68. /**
  69. * Get the content key for the hour covering timeSlot.
  70. * @param {number} timeSlot The time slot as milliseconds since Jan 1, 1970 UTC.
  71. * @param {boolean} useSync (optional) If true then return a SyncPromise which
  72. * is already fulfilled. If omitted or false, this may return a SyncPromise or
  73. * an async Promise.
  74. * @return {Promise|SyncPromise} A promise that returns a Blob with the encoded
  75. * key, or that is rejected with ProducerDb.Error if there is no key covering
  76. * timeSlot, or other database error
  77. */
  78. ProducerDb.prototype.getContentKeyPromise = function(timeSlot, useSync)
  79. {
  80. return SyncPromise.reject(new Error
  81. ("ProducerDb.getContentKeyPromise is not implemented"));
  82. };
  83. /**
  84. * Add key as the content key for the hour covering timeSlot.
  85. * @param {number} timeSlot The time slot as milliseconds since Jan 1, 1970 UTC.
  86. * @param {Blob} key The encoded key.
  87. * @param {boolean} useSync (optional) If true then return a SyncPromise which
  88. * is already fulfilled. If omitted or false, this may return a SyncPromise or
  89. * an async Promise.
  90. * @return {Promise|SyncPromise} A promise that fulfills when the key is added,
  91. * or that is rejected with ProducerDb.Error if a key for the same hour already
  92. * exists in the database, or other database error.
  93. */
  94. ProducerDb.prototype.addContentKeyPromise = function
  95. (timeSlot, key, useSync)
  96. {
  97. return SyncPromise.reject(new Error
  98. ("ProducerDb.addContentKeyPromise is not implemented"));
  99. };
  100. /**
  101. * Delete the content key for the hour covering timeSlot. If there is no key for
  102. * the time slot, do nothing.
  103. * @param {number} timeSlot The time slot as milliseconds since Jan 1, 1970 UTC.
  104. * @param {boolean} useSync (optional) If true then return a SyncPromise which
  105. * is already fulfilled. If omitted or false, this may return a SyncPromise or
  106. * an async Promise.
  107. * @return {Promise|SyncPromise} A promise that fulfills when the key is deleted
  108. * (or there is no such key), or that is rejected with ProducerDb.Error for a
  109. * database error.
  110. */
  111. ProducerDb.prototype.deleteContentKeyPromise = function(timeSlot, useSync)
  112. {
  113. return SyncPromise.reject(new Error
  114. ("ProducerDb.deleteContentKeyPromise is not implemented"));
  115. };
  116. /**
  117. * Get the hour-based time slot.
  118. * @param {number} timeSlot The time slot as milliseconds since Jan 1, 1970 UTC.
  119. * @return {number} The hour-based time slot as hours since Jan 1, 1970 UTC.
  120. */
  121. ProducerDb.getFixedTimeSlot = function(timeSlot)
  122. {
  123. return Math.floor(Math.round(timeSlot) / 3600000.0);
  124. };
  125. ProducerDb.Error.prototype = new Error();
  126. ProducerDb.Error.prototype.name = "ProducerDbError";