2023-01-13 07:42:29 -05:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const assert = require('check-types').assert
|
|
|
|
const OError = require('@overleaf/o-error')
|
|
|
|
|
2024-09-17 16:50:14 -04:00
|
|
|
const TextOperation = require('./operation/text_operation')
|
|
|
|
|
2023-01-13 07:42:29 -05:00
|
|
|
class NotFoundError extends OError {
|
|
|
|
constructor(hash) {
|
|
|
|
super(`blob ${hash} not found`, { hash })
|
|
|
|
this.hash = hash
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-22 10:48:04 -04:00
|
|
|
/**
|
|
|
|
* Metadata record for the content of a file.
|
|
|
|
*/
|
|
|
|
class Blob {
|
|
|
|
static HEX_HASH_RX_STRING = '^[0-9a-f]{40,40}$'
|
|
|
|
static HEX_HASH_RX = new RegExp(Blob.HEX_HASH_RX_STRING)
|
2023-01-13 07:42:29 -05:00
|
|
|
|
2023-08-22 10:48:04 -04:00
|
|
|
/**
|
|
|
|
* Size of the largest file that we'll read to determine whether we can edit it
|
|
|
|
* or not, in bytes. The final decision on whether a file is editable or not is
|
|
|
|
* based on the number of characters it contains, but we need to read the file
|
|
|
|
* in to determine that; so it is useful to have an upper bound on the byte
|
|
|
|
* length of a file that might be editable.
|
|
|
|
*
|
2024-09-17 16:50:14 -04:00
|
|
|
* The reason for the factor of 3 is as follows. We cannot currently edit files
|
|
|
|
* that contain characters outside of the basic multilingual plane, so we're
|
|
|
|
* limited to characters that can be represented in a single, two-byte UCS-2
|
|
|
|
* code unit. Encoding the largest such value, 0xFFFF (which is not actually
|
|
|
|
* a valid character), takes three bytes in UTF-8: 0xEF 0xBF 0xBF. A file
|
|
|
|
* composed entirely of three-byte UTF-8 codepoints is the worst case; in
|
|
|
|
* practice, this is a very conservative upper bound.
|
|
|
|
*
|
|
|
|
* @type {number}
|
2023-08-22 10:48:04 -04:00
|
|
|
*/
|
2024-09-17 16:50:14 -04:00
|
|
|
static MAX_EDITABLE_BYTE_LENGTH_BOUND = 3 * TextOperation.MAX_STRING_LENGTH
|
2023-08-22 10:48:04 -04:00
|
|
|
|
|
|
|
static NotFoundError = NotFoundError
|
2023-01-13 07:42:29 -05:00
|
|
|
|
2024-11-08 03:33:36 -05:00
|
|
|
/**
|
|
|
|
* @param {string} hash
|
|
|
|
* @param {number} byteLength
|
|
|
|
* @param {number} [stringLength]
|
|
|
|
*/
|
2023-08-22 10:48:04 -04:00
|
|
|
constructor(hash, byteLength, stringLength) {
|
|
|
|
this.setHash(hash)
|
|
|
|
this.setByteLength(byteLength)
|
|
|
|
this.setStringLength(stringLength)
|
2023-01-13 07:42:29 -05:00
|
|
|
}
|
|
|
|
|
2023-08-22 10:48:04 -04:00
|
|
|
static fromRaw(raw) {
|
|
|
|
if (raw) {
|
|
|
|
return new Blob(raw.hash, raw.byteLength, raw.stringLength)
|
|
|
|
}
|
|
|
|
return null
|
2023-01-13 07:42:29 -05:00
|
|
|
}
|
|
|
|
|
2023-08-22 10:48:04 -04:00
|
|
|
toRaw() {
|
|
|
|
return {
|
|
|
|
hash: this.hash,
|
|
|
|
byteLength: this.byteLength,
|
|
|
|
stringLength: this.stringLength,
|
|
|
|
}
|
|
|
|
}
|
2023-01-13 07:42:29 -05:00
|
|
|
|
2023-08-22 10:48:04 -04:00
|
|
|
/**
|
|
|
|
* Hex hash.
|
2024-11-08 03:33:36 -05:00
|
|
|
* @return {String}
|
2023-08-22 10:48:04 -04:00
|
|
|
*/
|
|
|
|
getHash() {
|
|
|
|
return this.hash
|
|
|
|
}
|
2023-01-13 07:42:29 -05:00
|
|
|
|
2023-08-22 10:48:04 -04:00
|
|
|
setHash(hash) {
|
2024-11-08 03:33:36 -05:00
|
|
|
assert.match(hash, Blob.HEX_HASH_RX, 'bad hash')
|
2023-08-22 10:48:04 -04:00
|
|
|
this.hash = hash
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Length of the blob in bytes.
|
|
|
|
* @return {number}
|
|
|
|
*/
|
|
|
|
getByteLength() {
|
|
|
|
return this.byteLength
|
|
|
|
}
|
|
|
|
|
|
|
|
setByteLength(byteLength) {
|
2024-11-08 03:33:36 -05:00
|
|
|
assert.integer(byteLength, 'bad byteLength')
|
2023-08-22 10:48:04 -04:00
|
|
|
this.byteLength = byteLength
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Utf-8 length of the blob content, if it appears to be valid UTF-8.
|
|
|
|
* @return {?number}
|
|
|
|
*/
|
|
|
|
getStringLength() {
|
|
|
|
return this.stringLength
|
|
|
|
}
|
|
|
|
|
|
|
|
setStringLength(stringLength) {
|
|
|
|
assert.maybe.integer(stringLength, 'bad stringLength')
|
|
|
|
this.stringLength = stringLength
|
|
|
|
}
|
2023-01-13 07:42:29 -05:00
|
|
|
}
|
|
|
|
|
2023-08-22 10:48:04 -04:00
|
|
|
module.exports = Blob
|