| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535 |
- const crypto = require('node:crypto')
- const os = require('node:os')
- const { expect } = require('chai')
- const fs = require('node:fs')
- const fsPromises = require('node:fs/promises')
- const Path = require('node:path')
- const StreamPromises = require('node:stream/promises')
- const SandboxedModule = require('sandboxed-module')
- const Errors = require('../../src/Errors')
- const MODULE_PATH = '../../src/FSPersistor.js'
- function createTree(base, tree) {
- fs.mkdirSync(base, { recursive: true })
- for (const [name, content] of Object.entries(tree)) {
- const fullPath = Path.join(base, name)
- if (Buffer.isBuffer(content) || typeof content === 'string') {
- fs.writeFileSync(fullPath, content)
- } else if (content && typeof content.symlink === 'string') {
- fs.symlinkSync(content.symlink, fullPath)
- } else {
- createTree(fullPath, content)
- }
- }
- }
- describe('FSPersistorTests', function () {
- const fileContents = {
- 'info.txt': Buffer.from('This information is critical', {
- encoding: 'utf-8',
- }),
- 'other.txt': Buffer.from('Some other content', {
- encoding: 'utf-8',
- }),
- }
- let tmpDir
- let location
- let notADirPath
- const files = {
- wombat: 'animals/wombat.tex',
- giraffe: 'animals/giraffe.tex',
- potato: 'vegetables/potato.tex',
- }
- beforeEach(function () {
- tmpDir = fs.mkdtempSync(Path.join(os.tmpdir(), 'fs-persistor-test-'))
- createTree(tmpDir, {
- uploads: {
- 'info.txt': fileContents['info.txt'],
- 'other.txt': fileContents['other.txt'],
- },
- 'not-a-dir':
- 'This regular file is meant to prevent using this path as a directory',
- directory: {
- subdirectory: {},
- },
- })
- notADirPath = Path.join(tmpDir, 'not-a-dir')
- location = Path.join(tmpDir, 'bucket')
- })
- afterEach(function () {
- fs.rmSync(tmpDir, { recursive: true })
- })
- const scenarios = [
- {
- description: 'default settings',
- settings: {},
- fsPath: key => Path.join(location, key.replaceAll('/', '_')),
- },
- {
- description: 'with useSubdirectories = true',
- settings: { useSubdirectories: true },
- fsPath: key => Path.join(location, key),
- },
- ]
- for (const scenario of scenarios) {
- describe(scenario.description, function () {
- let persistor
- beforeEach(function () {
- const FSPersistor = SandboxedModule.require(MODULE_PATH, {
- requires: {
- 'fs/promises': fsPromises,
- 'stream/promises': StreamPromises,
- './Errors': Errors,
- },
- })
- persistor = new FSPersistor(scenario.settings)
- })
- describe('sendFile', function () {
- it('should copy the file', async function () {
- await persistor.sendFile(
- location,
- files.wombat,
- Path.join(tmpDir, 'uploads', 'info.txt')
- )
- const contents = await fsPromises.readFile(
- scenario.fsPath(files.wombat)
- )
- expect(contents.equals(fileContents['info.txt'])).to.be.true
- })
- it('should return an error if the file cannot be stored', async function () {
- await expect(
- persistor.sendFile(
- notADirPath,
- files.wombat,
- Path.join(tmpDir, 'uploads', 'info.txt')
- )
- ).to.be.rejectedWith(Errors.WriteError)
- })
- })
- describe('sendStream', function () {
- let stream
- describe("when the file doesn't exist", function () {
- beforeEach(function () {
- stream = fs.createReadStream(
- Path.join(tmpDir, 'uploads', 'info.txt')
- )
- })
- it('should write the stream to disk', async function () {
- await persistor.sendStream(location, files.wombat, stream)
- const contents = await fsPromises.readFile(
- scenario.fsPath(files.wombat)
- )
- expect(contents.equals(fileContents['info.txt'])).to.be.true
- })
- it('should delete the temporary file', async function () {
- await persistor.sendStream(location, files.wombat, stream)
- const entries = await fsPromises.readdir(location)
- const tempDirs = entries.filter(dir => dir.startsWith('tmp-'))
- expect(tempDirs).to.be.empty
- })
- describe('on error', function () {
- beforeEach(async function () {
- await expect(
- persistor.sendStream(notADirPath, files.wombat, stream)
- ).to.be.rejectedWith(Errors.WriteError)
- })
- it('should not write the target file', async function () {
- await expect(fsPromises.access(scenario.fsPath(files.wombat))).to
- .be.rejected
- })
- it('should delete the temporary file', async function () {
- await persistor.sendStream(location, files.wombat, stream)
- const entries = await fsPromises.readdir(location)
- const tempDirs = entries.filter(dir => dir.startsWith('tmp-'))
- expect(tempDirs).to.be.empty
- })
- })
- describe('when the md5 hash matches', function () {
- it('should write the stream to disk', async function () {
- await persistor.sendStream(location, files.wombat, stream, {
- sourceMd5: md5(fileContents['info.txt']),
- })
- const contents = await fsPromises.readFile(
- scenario.fsPath(files.wombat)
- )
- expect(contents.equals(fileContents['info.txt'])).to.be.true
- })
- })
- describe('when the md5 hash does not match', function () {
- beforeEach(async function () {
- await expect(
- persistor.sendStream(location, files.wombat, stream, {
- sourceMd5: md5('wrong content'),
- })
- ).to.be.rejectedWith(Errors.WriteError)
- })
- it('should not write the target file', async function () {
- await expect(fsPromises.access(scenario.fsPath(files.wombat))).to
- .be.rejected
- })
- it('should delete the temporary file', async function () {
- await persistor.sendStream(location, files.wombat, stream)
- const entries = await fsPromises.readdir(location)
- const tempDirs = entries.filter(dir => dir.startsWith('tmp-'))
- expect(tempDirs).to.be.empty
- })
- })
- })
- describe('when the file already exists', function () {
- let stream
- beforeEach(async function () {
- await persistor.sendFile(
- location,
- files.wombat,
- Path.join(tmpDir, 'uploads', 'info.txt')
- )
- stream = fs.createReadStream(
- Path.join(tmpDir, 'uploads', 'other.txt')
- )
- })
- it('should write the stream to disk', async function () {
- await persistor.sendStream(location, files.wombat, stream)
- const contents = await fsPromises.readFile(
- scenario.fsPath(files.wombat)
- )
- expect(contents.equals(fileContents['other.txt'])).to.be.true
- })
- it('should delete the temporary file', async function () {
- await persistor.sendStream(location, files.wombat, stream)
- const entries = await fsPromises.readdir(location)
- const tempDirs = entries.filter(dir => dir.startsWith('tmp-'))
- expect(tempDirs).to.be.empty
- })
- describe('on error', function () {
- beforeEach(async function () {
- await expect(
- persistor.sendStream(notADirPath, files.wombat, stream)
- ).to.be.rejectedWith(Errors.WriteError)
- })
- it('should not update the target file', async function () {
- const contents = await fsPromises.readFile(
- scenario.fsPath(files.wombat)
- )
- expect(contents.equals(fileContents['info.txt'])).to.be.true
- })
- it('should delete the temporary file', async function () {
- await persistor.sendStream(location, files.wombat, stream)
- const entries = await fsPromises.readdir(location)
- const tempDirs = entries.filter(dir => dir.startsWith('tmp-'))
- expect(tempDirs).to.be.empty
- })
- })
- describe('when the md5 hash matches', function () {
- it('should write the stream to disk', async function () {
- await persistor.sendStream(location, files.wombat, stream, {
- sourceMd5: md5(fileContents['other.txt']),
- })
- const contents = await fsPromises.readFile(
- scenario.fsPath(files.wombat)
- )
- expect(contents.equals(fileContents['other.txt'])).to.be.true
- })
- })
- describe('when the md5 hash does not match', function () {
- beforeEach(async function () {
- await expect(
- persistor.sendStream(location, files.wombat, stream, {
- sourceMd5: md5('wrong content'),
- })
- ).to.be.rejectedWith(Errors.WriteError)
- })
- it('should not update the target file', async function () {
- const contents = await fsPromises.readFile(
- scenario.fsPath(files.wombat)
- )
- expect(contents.equals(fileContents['info.txt'])).to.be.true
- })
- it('should delete the temporary file', async function () {
- await persistor.sendStream(location, files.wombat, stream)
- const entries = await fsPromises.readdir(location)
- const tempDirs = entries.filter(dir => dir.startsWith('tmp-'))
- expect(tempDirs).to.be.empty
- })
- })
- })
- })
- describe('getObjectStream', function () {
- beforeEach(async function () {
- await persistor.sendFile(
- location,
- files.wombat,
- Path.join(tmpDir, 'uploads', 'info.txt')
- )
- })
- it('should return a string with the object contents', async function () {
- const stream = await persistor.getObjectStream(location, files.wombat)
- const contents = await streamToBuffer(stream)
- expect(contents.equals(fileContents['info.txt'])).to.be.true
- })
- it('should support ranges', async function () {
- const stream = await persistor.getObjectStream(
- location,
- files.wombat,
- {
- start: 5,
- end: 16,
- }
- )
- const contents = await streamToBuffer(stream)
- // end is inclusive in ranges, but exclusive in slice()
- expect(contents.equals(fileContents['info.txt'].slice(5, 17))).to.be
- .true
- })
- it('should give a NotFoundError if the file does not exist', async function () {
- await expect(
- persistor.getObjectStream(location, 'does-not-exist')
- ).to.be.rejectedWith(Errors.NotFoundError)
- })
- })
- describe('getObjectSize', function () {
- beforeEach(async function () {
- await persistor.sendFile(
- location,
- files.wombat,
- Path.join(tmpDir, 'uploads', 'info.txt')
- )
- })
- it('should return the file size', async function () {
- expect(
- await persistor.getObjectSize(location, files.wombat)
- ).to.equal(fileContents['info.txt'].length)
- })
- it('should throw a NotFoundError if the file does not exist', async function () {
- await expect(
- persistor.getObjectSize(location, 'does-not-exist')
- ).to.be.rejectedWith(Errors.NotFoundError)
- })
- })
- describe('copyObject', function () {
- beforeEach(async function () {
- await persistor.sendFile(
- location,
- files.wombat,
- Path.join(tmpDir, 'uploads', 'info.txt')
- )
- })
- it('Should copy the file to the new location', async function () {
- await persistor.copyObject(location, files.wombat, files.potato)
- const contents = await fsPromises.readFile(
- scenario.fsPath(files.potato)
- )
- expect(contents.equals(fileContents['info.txt'])).to.be.true
- })
- })
- describe('deleteObject', function () {
- beforeEach(async function () {
- await persistor.sendFile(
- location,
- files.wombat,
- Path.join(tmpDir, 'uploads', 'info.txt')
- )
- await fsPromises.access(scenario.fsPath(files.wombat))
- })
- it('should delete the file', async function () {
- await persistor.deleteObject(location, files.wombat)
- await expect(fsPromises.access(scenario.fsPath(files.wombat))).to.be
- .rejected
- })
- it("should ignore files that don't exist", async function () {
- await persistor.deleteObject(location, 'does-not-exist')
- })
- })
- describe('deleteDirectory', function () {
- beforeEach(async function () {
- for (const file of Object.values(files)) {
- await persistor.sendFile(
- location,
- file,
- Path.join(tmpDir, 'uploads', 'info.txt')
- )
- await fsPromises.access(scenario.fsPath(file))
- }
- })
- it('should delete all files under the directory', async function () {
- await persistor.deleteDirectory(location, 'animals')
- for (const file of [files.wombat, files.giraffe]) {
- await expect(fsPromises.access(scenario.fsPath(file))).to.be
- .rejected
- }
- })
- it('should not delete files under other directoris', async function () {
- await persistor.deleteDirectory(location, 'animals')
- await fsPromises.access(scenario.fsPath(files.potato))
- })
- it("should ignore directories that don't exist", async function () {
- await persistor.deleteDirectory(location, 'does-not-exist')
- for (const file of Object.values(files)) {
- await fsPromises.access(scenario.fsPath(file))
- }
- })
- })
- describe('checkIfObjectExists', function () {
- beforeEach(async function () {
- await persistor.sendFile(
- location,
- files.wombat,
- Path.join(tmpDir, 'uploads', 'info.txt')
- )
- })
- it('should return true for existing files', async function () {
- expect(
- await persistor.checkIfObjectExists(location, files.wombat)
- ).to.equal(true)
- })
- it('should return false for non-existing files', async function () {
- expect(
- await persistor.checkIfObjectExists(location, 'does-not-exist')
- ).to.equal(false)
- })
- })
- describe('directorySize', function () {
- beforeEach(async function () {
- for (const file of Object.values(files)) {
- await persistor.sendFile(
- location,
- file,
- Path.join(tmpDir, 'uploads', 'info.txt')
- )
- }
- })
- it('should sum directory files size', async function () {
- expect(await persistor.directorySize(location, 'animals')).to.equal(
- 2 * fileContents['info.txt'].length
- )
- })
- it('should return 0 on non-existing directories', async function () {
- expect(
- await persistor.directorySize(location, 'does-not-exist')
- ).to.equal(0)
- })
- })
- describe('listDirectoryKeys', function () {
- beforeEach(async function () {
- for (const file of Object.values(files)) {
- await persistor.sendFile(
- location,
- file,
- Path.join(tmpDir, 'uploads', 'info.txt')
- )
- }
- })
- it('should list directory keys', async function () {
- const keys = await persistor.listDirectoryKeys(location, 'animals')
- expect(keys).to.have.lengthOf(2)
- expect(keys).to.include(scenario.fsPath(files.wombat))
- expect(keys).to.include(scenario.fsPath(files.giraffe))
- })
- it('should return empty array for non-existing directories', async function () {
- const keys = await persistor.listDirectoryKeys(
- location,
- 'does-not-exist'
- )
- expect(keys).to.deep.equal([])
- })
- })
- describe('listDirectoryStats', function () {
- beforeEach(async function () {
- for (const file of Object.values(files)) {
- await persistor.sendFile(
- location,
- file,
- Path.join(tmpDir, 'uploads', 'info.txt')
- )
- }
- })
- it('should list directory stats', async function () {
- const stats = await persistor.listDirectoryStats(location, 'animals')
- expect(stats).to.have.lengthOf(2)
- const keys = stats.map(s => s.key)
- expect(keys).to.include(scenario.fsPath(files.wombat))
- expect(keys).to.include(scenario.fsPath(files.giraffe))
- for (const stat of stats) {
- expect(stat.size).to.equal(fileContents['info.txt'].length)
- }
- })
- it('should return empty array for non-existing directories', async function () {
- const stats = await persistor.listDirectoryStats(
- location,
- 'does-not-exist'
- )
- expect(stats).to.deep.equal([])
- })
- })
- })
- }
- })
- function md5(str) {
- return crypto.createHash('md5').update(str).digest('hex')
- }
- async function streamToBuffer(stream) {
- const chunks = []
- for await (const chunk of stream) {
- chunks.push(chunk)
- }
- return Buffer.concat(chunks)
- }
|