diff --git a/libraries/overleaf-editor-core/lib/blob.js b/libraries/overleaf-editor-core/lib/blob.js index 8acd0b6dcd..49e23fb862 100644 --- a/libraries/overleaf-editor-core/lib/blob.js +++ b/libraries/overleaf-editor-core/lib/blob.js @@ -40,6 +40,11 @@ class Blob { static NotFoundError = NotFoundError + /** + * @param {string} hash + * @param {number} byteLength + * @param {number} [stringLength] + */ constructor(hash, byteLength, stringLength) { this.setHash(hash) this.setByteLength(byteLength) @@ -63,14 +68,14 @@ class Blob { /** * Hex hash. - * @return {?String} + * @return {String} */ getHash() { return this.hash } setHash(hash) { - assert.maybe.match(hash, Blob.HEX_HASH_RX, 'bad hash') + assert.match(hash, Blob.HEX_HASH_RX, 'bad hash') this.hash = hash } @@ -83,7 +88,7 @@ class Blob { } setByteLength(byteLength) { - assert.maybe.integer(byteLength, 'bad byteLength') + assert.integer(byteLength, 'bad byteLength') this.byteLength = byteLength } diff --git a/libraries/overleaf-editor-core/lib/file_data/index.js b/libraries/overleaf-editor-core/lib/file_data/index.js index 0e6f50325f..e7ab920de9 100644 --- a/libraries/overleaf-editor-core/lib/file_data/index.js +++ b/libraries/overleaf-editor-core/lib/file_data/index.js @@ -63,20 +63,14 @@ class FileData { */ static createLazyFromBlobs(blob, rangesBlob) { assert.instance(blob, Blob, 'FileData: bad blob') - if (blob.getStringLength() == null) { - return new BinaryFileData( - // TODO(das7pad): see call-sites - // @ts-ignore - blob.getHash(), - blob.getByteLength() - ) + const stringLength = blob.getStringLength() + if (stringLength == null) { + return new BinaryFileData(blob.getHash(), blob.getByteLength()) } return new LazyStringFileData( - // TODO(das7pad): see call-sites - // @ts-ignore blob.getHash(), rangesBlob?.getHash(), - blob.getStringLength() + stringLength ) } diff --git a/libraries/overleaf-editor-core/lib/file_data/string_file_data.js b/libraries/overleaf-editor-core/lib/file_data/string_file_data.js index 04d486b6b0..2613c30ebc 100644 --- a/libraries/overleaf-editor-core/lib/file_data/string_file_data.js +++ b/libraries/overleaf-editor-core/lib/file_data/string_file_data.js @@ -142,12 +142,8 @@ class StringFileData extends FileData { trackedChanges: this.trackedChanges.toRaw(), } const rangesBlob = await blobStore.putObject(ranges) - // TODO(das7pad): Provide interface that guarantees hash exists? - // @ts-ignore return { hash: blob.getHash(), rangesHash: rangesBlob.getHash() } } - // TODO(das7pad): Provide interface that guarantees hash exists? - // @ts-ignore return { hash: blob.getHash() } } } diff --git a/services/history-v1/storage/lib/blob_store/index.js b/services/history-v1/storage/lib/blob_store/index.js index 6f5632a595..1e9c9c6758 100644 --- a/services/history-v1/storage/lib/blob_store/index.js +++ b/services/history-v1/storage/lib/blob_store/index.js @@ -79,23 +79,12 @@ function getBackend(projectId) { } async function makeBlobForFile(pathname) { - async function getByteLengthOfFile() { - const stat = await fs.promises.stat(pathname) - return stat.size - } - - async function getHashOfFile(blob) { - const stream = fs.createReadStream(pathname) - const hash = await blobHash.fromStream(blob.getByteLength(), stream) - return hash - } - - const blob = new Blob() - const byteLength = await getByteLengthOfFile() - blob.setByteLength(byteLength) - const hash = await getHashOfFile(blob) - blob.setHash(hash) - return blob + const { size: byteLength } = await fs.promises.stat(pathname) + const hash = await blobHash.fromStream( + byteLength, + fs.createReadStream(pathname) + ) + return new Blob(hash, byteLength) } async function getStringLengthOfFile(byteLength, pathname) {