index.js 5.5 KB

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