index.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. * @param {import('../file').FileGetContentOptions} [opts]
  82. * @return {string | null | undefined}
  83. */
  84. getContent(opts = {}) {
  85. return null
  86. }
  87. /**
  88. * @see File#isEditable
  89. * @return {boolean | null | undefined} null if it is not currently known
  90. */
  91. isEditable() {
  92. return null
  93. }
  94. /**
  95. * @see File#getByteLength
  96. * @return {number | null | undefined}
  97. */
  98. getByteLength() {
  99. return null
  100. }
  101. /**
  102. * @see File#getStringLength
  103. * @return {number | null | undefined}
  104. */
  105. getStringLength() {
  106. return null
  107. }
  108. /**
  109. * @see File#edit
  110. * @param {EditOperation} editOperation
  111. */
  112. edit(editOperation) {
  113. throw new Error('edit not implemented for ' + JSON.stringify(this))
  114. }
  115. /**
  116. * @function
  117. * @param {BlobStore} blobStore
  118. * @return {Promise<FileData>}
  119. * @abstract
  120. * @see FileData#load
  121. */
  122. async toEager(blobStore) {
  123. throw new Error('toEager not implemented for ' + JSON.stringify(this))
  124. }
  125. /**
  126. * @function
  127. * @param {BlobStore} blobStore
  128. * @return {Promise<FileData>}
  129. * @abstract
  130. * @see FileData#load
  131. */
  132. async toLazy(blobStore) {
  133. throw new Error('toLazy not implemented for ' + JSON.stringify(this))
  134. }
  135. /**
  136. * @function
  137. * @param {BlobStore} blobStore
  138. * @return {Promise<FileData>}
  139. * @abstract
  140. * @see FileData#load
  141. */
  142. async toHollow(blobStore) {
  143. throw new Error('toHollow not implemented for ' + JSON.stringify(this))
  144. }
  145. /**
  146. * @see File#load
  147. * @param {string} kind
  148. * @param {BlobStore} blobStore
  149. * @return {Promise<FileData>}
  150. */
  151. async load(kind, blobStore) {
  152. if (kind === 'eager') return await this.toEager(blobStore)
  153. if (kind === 'lazy') return await this.toLazy(blobStore)
  154. if (kind === 'hollow') return await this.toHollow(blobStore)
  155. throw new Error('bad file data load kind: ' + kind)
  156. }
  157. /**
  158. * @see File#store
  159. * @function
  160. * @param {BlobStore} blobStore
  161. * @return {Promise<Object>} a raw HashFile
  162. * @abstract
  163. */
  164. async store(blobStore) {
  165. throw new Error('store not implemented for ' + JSON.stringify(this))
  166. }
  167. /**
  168. * @see File#getComments
  169. * @function
  170. * @return {CommentRawData[]}
  171. * @abstract
  172. */
  173. getComments() {
  174. throw new Error('getComments not implemented for ' + JSON.stringify(this))
  175. }
  176. }
  177. module.exports = FileData
  178. BinaryFileData = require('./binary_file_data')
  179. HashFileData = require('./hash_file_data')
  180. HollowBinaryFileData = require('./hollow_binary_file_data')
  181. HollowStringFileData = require('./hollow_string_file_data')
  182. LazyStringFileData = require('./lazy_string_file_data')
  183. StringFileData = require('./string_file_data')