index.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. 'use strict'
  2. const assert = require('check-types').assert
  3. const Blob = require('../blob')
  4. // Dependencies are loaded at the bottom of the file to mitigate circular
  5. // dependency
  6. let BinaryFileData = null
  7. let HashFileData = null
  8. let HollowBinaryFileData = null
  9. let HollowStringFileData = null
  10. let LazyStringFileData = null
  11. let StringFileData = null
  12. /**
  13. * @typedef {import("../types").BlobStore} BlobStore
  14. * @typedef {import("../operation/edit_operation")} EditOperation
  15. * @typedef {import("../types").CommentRawData} CommentRawData
  16. */
  17. /**
  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. /**
  47. * @see File.createLazyFromBlob
  48. * @param {Blob} blob
  49. * @param {Blob} [rangesBlob]
  50. */
  51. static createLazyFromBlobs(blob, rangesBlob) {
  52. assert.instance(blob, Blob, 'FileData: bad blob')
  53. if (blob.getStringLength() == null) {
  54. return new BinaryFileData(blob.getHash(), blob.getByteLength())
  55. }
  56. return new LazyStringFileData(
  57. blob.getHash(),
  58. rangesBlob?.getHash(),
  59. blob.getStringLength()
  60. )
  61. }
  62. toRaw() {
  63. throw new Error('FileData: toRaw not implemented')
  64. }
  65. /**
  66. * @see File#getHash
  67. * @return {string | null | undefined}
  68. */
  69. getHash() {
  70. return null
  71. }
  72. /**
  73. * @see File#getHash
  74. * @return {string | null | undefined}
  75. */
  76. getRangesHash() {
  77. return null
  78. }
  79. /**
  80. * @see File#getContent
  81. * @return {string | null | undefined}
  82. */
  83. getContent() {
  84. return null
  85. }
  86. /**
  87. * @see File#isEditable
  88. * @return {boolean | null | undefined} null if it is not currently known
  89. */
  90. isEditable() {
  91. return null
  92. }
  93. /**
  94. * @see File#getByteLength
  95. * @return {number | null | undefined}
  96. */
  97. getByteLength() {
  98. return null
  99. }
  100. /**
  101. * @see File#getStringLength
  102. * @return {number | null | undefined}
  103. */
  104. getStringLength() {
  105. return null
  106. }
  107. /**
  108. * @see File#edit
  109. * @param {EditOperation} editOperation
  110. */
  111. edit(editOperation) {
  112. throw new Error('edit not implemented for ' + JSON.stringify(this))
  113. }
  114. /**
  115. * @function
  116. * @param {BlobStore} blobStore
  117. * @return {Promise<FileData>}
  118. * @abstract
  119. * @see FileData#load
  120. */
  121. async toEager(blobStore) {
  122. throw new Error('toEager not implemented for ' + JSON.stringify(this))
  123. }
  124. /**
  125. * @function
  126. * @param {BlobStore} blobStore
  127. * @return {Promise<FileData>}
  128. * @abstract
  129. * @see FileData#load
  130. */
  131. async toLazy(blobStore) {
  132. throw new Error('toLazy not implemented for ' + JSON.stringify(this))
  133. }
  134. /**
  135. * @function
  136. * @param {BlobStore} blobStore
  137. * @return {Promise<FileData>}
  138. * @abstract
  139. * @see FileData#load
  140. */
  141. async toHollow(blobStore) {
  142. throw new Error('toHollow not implemented for ' + JSON.stringify(this))
  143. }
  144. /**
  145. * @see File#load
  146. * @param {string} kind
  147. * @param {BlobStore} blobStore
  148. * @return {Promise<FileData>}
  149. */
  150. async load(kind, blobStore) {
  151. if (kind === 'eager') return await this.toEager(blobStore)
  152. if (kind === 'lazy') return await this.toLazy(blobStore)
  153. if (kind === 'hollow') return await this.toHollow(blobStore)
  154. throw new Error('bad file data load kind: ' + kind)
  155. }
  156. /**
  157. * @see File#store
  158. * @function
  159. * @param {BlobStore} blobStore
  160. * @return {Promise<Object>} a raw HashFile
  161. * @abstract
  162. */
  163. async store(blobStore) {
  164. throw new Error('store not implemented for ' + JSON.stringify(this))
  165. }
  166. /**
  167. * @see File#getComments
  168. * @function
  169. * @return {CommentRawData[]}
  170. * @abstract
  171. */
  172. getComments() {
  173. throw new Error('getComments not implemented for ' + JSON.stringify(this))
  174. }
  175. }
  176. module.exports = FileData
  177. BinaryFileData = require('./binary_file_data')
  178. HashFileData = require('./hash_file_data')
  179. HollowBinaryFileData = require('./hollow_binary_file_data')
  180. HollowStringFileData = require('./hollow_string_file_data')
  181. LazyStringFileData = require('./lazy_string_file_data')
  182. StringFileData = require('./string_file_data')