mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-05-23 09:09:36 +02:00
[object-persistor] import ProjectKey helper from history-v1 (#30600)
GitOrigin-RevId: c72aa4bf91569904a2072c74d6ed2f3c764d97bb
This commit is contained in:
22
libraries/object-persistor/src/ProjectKey.js
Normal file
22
libraries/object-persistor/src/ProjectKey.js
Normal file
@@ -0,0 +1,22 @@
|
||||
const path = require('node:path')
|
||||
|
||||
//
|
||||
// The advice in http://docs.aws.amazon.com/AmazonS3/latest/dev/
|
||||
// request-rate-perf-considerations.html is to avoid sequential key prefixes,
|
||||
// so we reverse the project ID part of the key as they suggest.
|
||||
//
|
||||
function format(projectId) {
|
||||
const prefix = naiveReverse(pad(projectId))
|
||||
return path.join(prefix.slice(0, 3), prefix.slice(3, 6), prefix.slice(6))
|
||||
}
|
||||
|
||||
function pad(number) {
|
||||
return (number || 0).toString().padStart(9, '0')
|
||||
}
|
||||
|
||||
function naiveReverse(string) {
|
||||
return string.split('').reverse().join('')
|
||||
}
|
||||
|
||||
exports.format = format
|
||||
exports.pad = pad
|
||||
21
libraries/object-persistor/test/unit/ProjectKeyTests.js
Normal file
21
libraries/object-persistor/test/unit/ProjectKeyTests.js
Normal file
@@ -0,0 +1,21 @@
|
||||
const { expect } = require('chai')
|
||||
|
||||
const { format, pad } = require('../../src/ProjectKey.js')
|
||||
|
||||
describe('projectKey', function () {
|
||||
it('reverses padded keys', function () {
|
||||
expect(format(1)).to.equal('100/000/000')
|
||||
expect(format(12)).to.equal('210/000/000')
|
||||
expect(format(123456789)).to.equal('987/654/321')
|
||||
expect(format(9123456789)).to.equal('987/654/3219')
|
||||
})
|
||||
|
||||
it('pads numbers with zeros to length 9', function () {
|
||||
expect(pad(undefined)).to.equal('000000000')
|
||||
expect(pad(null)).to.equal('000000000')
|
||||
expect(pad(1)).to.equal('000000001')
|
||||
expect(pad(10)).to.equal('000000010')
|
||||
expect(pad(100000000)).to.equal('100000000')
|
||||
expect(pad(1000000000)).to.equal('1000000000')
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user