Merge pull request #21664 from overleaf/jpa-blob-hash

[overleaf-editor-core] stricter types for Blob interface

GitOrigin-RevId: 8595fce0d5c98074d2313be5a5634e80f92c68b5
This commit is contained in:
Jakob Ackermann 2024-11-08 09:33:36 +01:00 committed by Copybot
parent 122d89a831
commit a3d8caf87b
4 changed files with 18 additions and 34 deletions

View file

@ -40,6 +40,11 @@ class Blob {
static NotFoundError = NotFoundError static NotFoundError = NotFoundError
/**
* @param {string} hash
* @param {number} byteLength
* @param {number} [stringLength]
*/
constructor(hash, byteLength, stringLength) { constructor(hash, byteLength, stringLength) {
this.setHash(hash) this.setHash(hash)
this.setByteLength(byteLength) this.setByteLength(byteLength)
@ -63,14 +68,14 @@ class Blob {
/** /**
* Hex hash. * Hex hash.
* @return {?String} * @return {String}
*/ */
getHash() { getHash() {
return this.hash return this.hash
} }
setHash(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 this.hash = hash
} }
@ -83,7 +88,7 @@ class Blob {
} }
setByteLength(byteLength) { setByteLength(byteLength) {
assert.maybe.integer(byteLength, 'bad byteLength') assert.integer(byteLength, 'bad byteLength')
this.byteLength = byteLength this.byteLength = byteLength
} }

View file

@ -63,20 +63,14 @@ class FileData {
*/ */
static createLazyFromBlobs(blob, rangesBlob) { static createLazyFromBlobs(blob, rangesBlob) {
assert.instance(blob, Blob, 'FileData: bad blob') assert.instance(blob, Blob, 'FileData: bad blob')
if (blob.getStringLength() == null) { const stringLength = blob.getStringLength()
return new BinaryFileData( if (stringLength == null) {
// TODO(das7pad): see call-sites return new BinaryFileData(blob.getHash(), blob.getByteLength())
// @ts-ignore
blob.getHash(),
blob.getByteLength()
)
} }
return new LazyStringFileData( return new LazyStringFileData(
// TODO(das7pad): see call-sites
// @ts-ignore
blob.getHash(), blob.getHash(),
rangesBlob?.getHash(), rangesBlob?.getHash(),
blob.getStringLength() stringLength
) )
} }

View file

@ -142,12 +142,8 @@ class StringFileData extends FileData {
trackedChanges: this.trackedChanges.toRaw(), trackedChanges: this.trackedChanges.toRaw(),
} }
const rangesBlob = await blobStore.putObject(ranges) const rangesBlob = await blobStore.putObject(ranges)
// TODO(das7pad): Provide interface that guarantees hash exists?
// @ts-ignore
return { hash: blob.getHash(), rangesHash: rangesBlob.getHash() } return { hash: blob.getHash(), rangesHash: rangesBlob.getHash() }
} }
// TODO(das7pad): Provide interface that guarantees hash exists?
// @ts-ignore
return { hash: blob.getHash() } return { hash: blob.getHash() }
} }
} }

View file

@ -79,23 +79,12 @@ function getBackend(projectId) {
} }
async function makeBlobForFile(pathname) { async function makeBlobForFile(pathname) {
async function getByteLengthOfFile() { const { size: byteLength } = await fs.promises.stat(pathname)
const stat = await fs.promises.stat(pathname) const hash = await blobHash.fromStream(
return stat.size byteLength,
} fs.createReadStream(pathname)
)
async function getHashOfFile(blob) { return new Blob(hash, byteLength)
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
} }
async function getStringLengthOfFile(byteLength, pathname) { async function getStringLengthOfFile(byteLength, pathname) {