index.js 5.8 KB

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