index.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. */
  15. /**
  16. * Helper to represent the content of a file. This class and its subclasses
  17. * should be used only through {@link File}.
  18. */
  19. class FileData {
  20. /** @see File.fromRaw */
  21. static fromRaw(raw) {
  22. if (Object.prototype.hasOwnProperty.call(raw, 'hash')) {
  23. if (Object.prototype.hasOwnProperty.call(raw, 'byteLength'))
  24. return BinaryFileData.fromRaw(raw)
  25. if (Object.prototype.hasOwnProperty.call(raw, 'stringLength'))
  26. return LazyStringFileData.fromRaw(raw)
  27. return HashFileData.fromRaw(raw)
  28. }
  29. if (Object.prototype.hasOwnProperty.call(raw, 'byteLength'))
  30. return HollowBinaryFileData.fromRaw(raw)
  31. if (Object.prototype.hasOwnProperty.call(raw, 'stringLength'))
  32. return HollowStringFileData.fromRaw(raw)
  33. if (Object.prototype.hasOwnProperty.call(raw, 'content'))
  34. return StringFileData.fromRaw(raw)
  35. throw new Error('FileData: bad raw object ' + JSON.stringify(raw))
  36. }
  37. /** @see File.createHollow */
  38. static createHollow(byteLength, stringLength) {
  39. if (stringLength == null) {
  40. return new HollowBinaryFileData(byteLength)
  41. }
  42. return new HollowStringFileData(stringLength)
  43. }
  44. /** @see File.createLazyFromBlob */
  45. static createLazyFromBlob(blob) {
  46. assert.instance(blob, Blob, 'FileData: bad blob')
  47. if (blob.getStringLength() == null) {
  48. return new BinaryFileData(blob.getHash(), blob.getByteLength())
  49. }
  50. return new LazyStringFileData(blob.getHash(), blob.getStringLength())
  51. }
  52. toRaw() {
  53. throw new Error('FileData: toRaw not implemented')
  54. }
  55. /** @see File#getHash */
  56. getHash() {
  57. return null
  58. }
  59. /** @see File#getContent */
  60. getContent() {
  61. return null
  62. }
  63. /** @see File#isEditable */
  64. isEditable() {
  65. return null
  66. }
  67. /** @see File#getByteLength */
  68. getByteLength() {
  69. return null
  70. }
  71. /** @see File#getStringLength */
  72. getStringLength() {
  73. return null
  74. }
  75. /** @see File#edit */
  76. edit(textOperation) {
  77. throw new Error('edit not implemented for ' + JSON.stringify(this))
  78. }
  79. /**
  80. * @function
  81. * @param {BlobStore} blobStore
  82. * @return {Promise<FileData>}
  83. * @abstract
  84. * @see FileData#load
  85. */
  86. async toEager(blobStore) {
  87. throw new Error('toEager not implemented for ' + JSON.stringify(this))
  88. }
  89. /**
  90. * @function
  91. * @param {BlobStore} blobStore
  92. * @return {Promise<FileData>}
  93. * @abstract
  94. * @see FileData#load
  95. */
  96. async toLazy(blobStore) {
  97. throw new Error('toLazy not implemented for ' + JSON.stringify(this))
  98. }
  99. /**
  100. * @function
  101. * @param {BlobStore} blobStore
  102. * @return {Promise<FileData>}
  103. * @abstract
  104. * @see FileData#load
  105. */
  106. async toHollow(blobStore) {
  107. throw new Error('toHollow not implemented for ' + JSON.stringify(this))
  108. }
  109. /**
  110. * @see File#load
  111. * @param {string} kind
  112. * @param {BlobStore} blobStore
  113. * @return {Promise<FileData>}
  114. */
  115. async load(kind, blobStore) {
  116. if (kind === 'eager') return await this.toEager(blobStore)
  117. if (kind === 'lazy') return await this.toLazy(blobStore)
  118. if (kind === 'hollow') return await this.toHollow(blobStore)
  119. throw new Error('bad file data load kind: ' + kind)
  120. }
  121. /**
  122. * @see File#store
  123. * @function
  124. * @param {BlobStore} blobStore
  125. * @return {Promise<Object>} a raw HashFile
  126. * @abstract
  127. */
  128. async store(blobStore) {
  129. throw new Error('store not implemented for ' + JSON.stringify(this))
  130. }
  131. }
  132. module.exports = FileData
  133. BinaryFileData = require('./binary_file_data')
  134. HashFileData = require('./hash_file_data')
  135. HollowBinaryFileData = require('./hollow_binary_file_data')
  136. HollowStringFileData = require('./hollow_string_file_data')
  137. LazyStringFileData = require('./lazy_string_file_data')
  138. StringFileData = require('./string_file_data')