index.js 4.2 KB

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