index.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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("../types").ReadonlyBlobStore} ReadonlyBlobStore
  15. * @typedef {import("../operation/edit_operation")} EditOperation
  16. * @typedef {import("../types").CommentRawData} CommentRawData
  17. */
  18. /**
  19. * Helper to represent the content of a file. This class and its subclasses
  20. * should be used only through {@link File}.
  21. */
  22. class FileData {
  23. /** @see File.fromRaw */
  24. static fromRaw(raw) {
  25. if (Object.prototype.hasOwnProperty.call(raw, 'hash')) {
  26. if (Object.prototype.hasOwnProperty.call(raw, 'byteLength'))
  27. return BinaryFileData.fromRaw(raw)
  28. if (Object.prototype.hasOwnProperty.call(raw, 'stringLength'))
  29. return LazyStringFileData.fromRaw(raw)
  30. return HashFileData.fromRaw(raw)
  31. }
  32. if (Object.prototype.hasOwnProperty.call(raw, 'byteLength'))
  33. return HollowBinaryFileData.fromRaw(raw)
  34. if (Object.prototype.hasOwnProperty.call(raw, 'stringLength'))
  35. return HollowStringFileData.fromRaw(raw)
  36. if (Object.prototype.hasOwnProperty.call(raw, 'content'))
  37. return StringFileData.fromRaw(raw)
  38. throw new Error('FileData: bad raw object ' + JSON.stringify(raw))
  39. }
  40. /** @see File.createHollow */
  41. static createHollow(byteLength, stringLength) {
  42. if (stringLength == null) {
  43. return new HollowBinaryFileData(byteLength)
  44. }
  45. return new HollowStringFileData(stringLength)
  46. }
  47. /**
  48. * @see File.createLazyFromBlob
  49. * @param {Blob} blob
  50. * @param {Blob} [rangesBlob]
  51. */
  52. static createLazyFromBlobs(blob, rangesBlob) {
  53. assert.instance(blob, Blob, 'FileData: bad blob')
  54. if (blob.getStringLength() == null) {
  55. return new BinaryFileData(blob.getHash(), blob.getByteLength())
  56. }
  57. return new LazyStringFileData(
  58. blob.getHash(),
  59. rangesBlob?.getHash(),
  60. blob.getStringLength()
  61. )
  62. }
  63. toRaw() {
  64. throw new Error('FileData: toRaw not implemented')
  65. }
  66. /**
  67. * @see File#getHash
  68. * @return {string | null | undefined}
  69. */
  70. getHash() {
  71. return null
  72. }
  73. /**
  74. * @see File#getHash
  75. * @return {string | null | undefined}
  76. */
  77. getRangesHash() {
  78. return null
  79. }
  80. /**
  81. * @see File#getContent
  82. * @param {import('../file').FileGetContentOptions} [opts]
  83. * @return {string | null | undefined}
  84. */
  85. getContent(opts = {}) {
  86. return null
  87. }
  88. /**
  89. * @see File#isEditable
  90. * @return {boolean | null | undefined} null if it is not currently known
  91. */
  92. isEditable() {
  93. return null
  94. }
  95. /**
  96. * @see File#getByteLength
  97. * @return {number | null | undefined}
  98. */
  99. getByteLength() {
  100. return null
  101. }
  102. /**
  103. * @see File#getStringLength
  104. * @return {number | null | undefined}
  105. */
  106. getStringLength() {
  107. return null
  108. }
  109. /**
  110. * @see File#edit
  111. * @param {EditOperation} editOperation
  112. */
  113. edit(editOperation) {
  114. throw new Error('edit not implemented for ' + JSON.stringify(this))
  115. }
  116. /**
  117. * @function
  118. * @param {ReadonlyBlobStore} blobStore
  119. * @return {Promise<FileData>}
  120. * @abstract
  121. * @see FileData#load
  122. */
  123. async toEager(blobStore) {
  124. throw new Error('toEager not implemented for ' + JSON.stringify(this))
  125. }
  126. /**
  127. * @function
  128. * @param {ReadonlyBlobStore} blobStore
  129. * @return {Promise<FileData>}
  130. * @abstract
  131. * @see FileData#load
  132. */
  133. async toLazy(blobStore) {
  134. throw new Error('toLazy not implemented for ' + JSON.stringify(this))
  135. }
  136. /**
  137. * @function
  138. * @param {ReadonlyBlobStore} blobStore
  139. * @return {Promise<FileData>}
  140. * @abstract
  141. * @see FileData#load
  142. */
  143. async toHollow(blobStore) {
  144. throw new Error('toHollow not implemented for ' + JSON.stringify(this))
  145. }
  146. /**
  147. * @see File#load
  148. * @param {string} kind
  149. * @param {ReadonlyBlobStore} blobStore
  150. * @return {Promise<FileData>}
  151. */
  152. async load(kind, blobStore) {
  153. if (kind === 'eager') return await this.toEager(blobStore)
  154. if (kind === 'lazy') return await this.toLazy(blobStore)
  155. if (kind === 'hollow') return await this.toHollow(blobStore)
  156. throw new Error('bad file data load kind: ' + kind)
  157. }
  158. /**
  159. * @see File#store
  160. * @function
  161. * @param {BlobStore} blobStore
  162. * @return {Promise<Object>} a raw HashFile
  163. * @abstract
  164. */
  165. async store(blobStore) {
  166. throw new Error('store not implemented for ' + JSON.stringify(this))
  167. }
  168. /**
  169. * @see File#getComments
  170. * @function
  171. * @return {CommentRawData[]}
  172. * @abstract
  173. */
  174. getComments() {
  175. throw new Error('getComments not implemented for ' + JSON.stringify(this))
  176. }
  177. }
  178. module.exports = FileData
  179. BinaryFileData = require('./binary_file_data')
  180. HashFileData = require('./hash_file_data')
  181. HollowBinaryFileData = require('./hollow_binary_file_data')
  182. HollowStringFileData = require('./hollow_string_file_data')
  183. LazyStringFileData = require('./lazy_string_file_data')
  184. StringFileData = require('./string_file_data')