index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. 'use strict'
  2. const assert = require('check-types').assert
  3. const BPromise = require('bluebird')
  4. const Blob = require('../blob')
  5. // Dependencies are loaded at the bottom of the file to mitigate circular
  6. // dependency
  7. let BinaryFileData = null
  8. let HashFileData = null
  9. let HollowBinaryFileData = null
  10. let HollowStringFileData = null
  11. let LazyStringFileData = null
  12. let StringFileData = null
  13. /**
  14. * @typedef {import("../types").BlobStore} BlobStore
  15. */
  16. /**
  17. * Helper to represent the content of a file. This class and its subclasses
  18. * should be used only through {@link File}.
  19. */
  20. class FileData {
  21. /** @see File.fromRaw */
  22. static fromRaw(raw) {
  23. if (Object.prototype.hasOwnProperty.call(raw, 'hash')) {
  24. if (Object.prototype.hasOwnProperty.call(raw, 'byteLength'))
  25. return BinaryFileData.fromRaw(raw)
  26. if (Object.prototype.hasOwnProperty.call(raw, 'stringLength'))
  27. return LazyStringFileData.fromRaw(raw)
  28. return HashFileData.fromRaw(raw)
  29. }
  30. if (Object.prototype.hasOwnProperty.call(raw, 'byteLength'))
  31. return HollowBinaryFileData.fromRaw(raw)
  32. if (Object.prototype.hasOwnProperty.call(raw, 'stringLength'))
  33. return HollowStringFileData.fromRaw(raw)
  34. if (Object.prototype.hasOwnProperty.call(raw, 'content'))
  35. return StringFileData.fromRaw(raw)
  36. throw new Error('FileData: bad raw object ' + JSON.stringify(raw))
  37. }
  38. /** @see File.createHollow */
  39. static createHollow(byteLength, stringLength) {
  40. if (stringLength == null) {
  41. return new HollowBinaryFileData(byteLength)
  42. }
  43. return new HollowStringFileData(stringLength)
  44. }
  45. /** @see File.createLazyFromBlob */
  46. static createLazyFromBlob(blob) {
  47. assert.instance(blob, Blob, 'FileData: bad blob')
  48. if (blob.getStringLength() == null) {
  49. return new BinaryFileData(blob.getHash(), blob.getByteLength())
  50. }
  51. return new LazyStringFileData(blob.getHash(), blob.getStringLength())
  52. }
  53. toRaw() {
  54. throw new Error('FileData: toRaw not implemented')
  55. }
  56. /** @see File#getHash */
  57. getHash() {
  58. return null
  59. }
  60. /** @see File#getContent */
  61. getContent() {
  62. return null
  63. }
  64. /** @see File#isEditable */
  65. isEditable() {
  66. return null
  67. }
  68. /** @see File#getByteLength */
  69. getByteLength() {
  70. return null
  71. }
  72. /** @see File#getStringLength */
  73. getStringLength() {
  74. return null
  75. }
  76. /** @see File#edit */
  77. edit(textOperation) {
  78. throw new Error('edit not implemented for ' + JSON.stringify(this))
  79. }
  80. /**
  81. * @function
  82. * @param {BlobStore} blobStore
  83. * @return {BPromise<FileData>}
  84. * @abstract
  85. * @see FileData#load
  86. */
  87. toEager(blobStore) {
  88. return BPromise.reject(
  89. new Error('toEager not implemented for ' + JSON.stringify(this))
  90. )
  91. }
  92. /**
  93. * @function
  94. * @param {BlobStore} blobStore
  95. * @return {BPromise<FileData>}
  96. * @abstract
  97. * @see FileData#load
  98. */
  99. toLazy(blobStore) {
  100. return BPromise.reject(
  101. new Error('toLazy not implemented for ' + JSON.stringify(this))
  102. )
  103. }
  104. /**
  105. * @function
  106. * @param {BlobStore} blobStore
  107. * @return {BPromise<FileData>}
  108. * @abstract
  109. * @see FileData#load
  110. */
  111. toHollow(blobStore) {
  112. return BPromise.reject(
  113. new Error('toHollow not implemented for ' + JSON.stringify(this))
  114. )
  115. }
  116. /**
  117. * @see File#load
  118. * @param {string} kind
  119. * @param {BlobStore} blobStore
  120. * @return {BPromise<FileData>}
  121. */
  122. load(kind, blobStore) {
  123. if (kind === 'eager') return this.toEager(blobStore)
  124. if (kind === 'lazy') return this.toLazy(blobStore)
  125. if (kind === 'hollow') return this.toHollow(blobStore)
  126. throw new Error('bad file data load kind: ' + kind)
  127. }
  128. /**
  129. * @see File#store
  130. * @function
  131. * @param {BlobStore} blobStore
  132. * @return {BPromise<Object>} a raw HashFile
  133. * @abstract
  134. */
  135. store(blobStore) {
  136. return BPromise.reject(
  137. new Error('store not implemented for ' + JSON.stringify(this))
  138. )
  139. }
  140. }
  141. module.exports = FileData
  142. BinaryFileData = require('./binary_file_data')
  143. HashFileData = require('./hash_file_data')
  144. HollowBinaryFileData = require('./hollow_binary_file_data')
  145. HollowStringFileData = require('./hollow_string_file_data')
  146. LazyStringFileData = require('./lazy_string_file_data')
  147. StringFileData = require('./string_file_data')