Skip to content

Class: FileStore ​

Defined in: src/file-store.ts:15

A verified content-addressed file store.

This example requires Node.js.

ts
import { FileStore } from 'osu-files'

const store = new FileStore('./files')
const stored = store.put(Buffer.from('data'))

Constructors ​

new FileStore(basePath, readOnly?): FileStore

Defined in: src/file-store.ts:16

Parameters ​

ParameterTypeDefault value
basePathstringundefined
readOnlybooleanfalse

Returns ​

FileStore

This example requires Node.js.

ts
import { FileStore } from 'osu-files'

const store = new FileStore('./files')

return store

Methods ​

beginTransaction() ​

beginTransaction(): FileStoreTransaction

Defined in: src/file-store.ts:87

Starts a temporary file transaction for a compound operation.

Returns ​

FileStoreTransaction

A transaction that must be committed and finalised by its owner.

See ​

FileStoreTransaction

This example requires Node.js.

ts
import { FileStore } from 'osu-files'

const store = new FileStore('./files')
const transaction = store.beginTransaction()

transaction.rollback()

return transaction

hasPath() ​

hasPath(hash): boolean

Defined in: src/file-store.ts:135

Checks for a stored path without hashing its contents.

Parameters ​

ParameterType
hashstring

Returns ​

boolean

This example requires Node.js.

ts
import { FileStore } from 'osu-files'

const store = new FileStore('./files')
const stored = store.put(Buffer.from('data'))

return store.hasPath(stored.hash)

path() ​

path(hash): string

Defined in: src/file-store.ts:24

Returns the canonical path for a SHA-256 file hash.

Parameters ​

ParameterType
hashstring

Returns ​

string

This example requires Node.js.

ts
import { FileStore } from 'osu-files'

const store = new FileStore('./files')

return store.path('a'.repeat(64))

promote() ​

promote(temporary, hash): string

Defined in: src/file-store.ts:162

Parameters ​

ParameterType
temporarystring
hashstring

Returns ​

string

This example requires Node.js.

ts
import { FileStore } from 'osu-files'

const store = new FileStore('./files')
const hash = 'a'.repeat(64)
const temporary = store.temporaryPath(hash)

return store.promote(temporary, hash)

put() ​

put(content, expectedHash?): object

Defined in: src/file-store.ts:44

Stores content after checking or calculating its SHA-256 hash.

Parameters ​

ParameterType
contentBuffer
expectedHash?string

Returns ​

object

created ​

created: boolean

hash ​

hash: string

This example requires Node.js.

ts
import { FileStore } from 'osu-files'

const store = new FileStore('./files')
const content = Buffer.from('data')
const stored = store.put(content)

return stored.hash

putFile() ​

putFile(source, hash): object

Defined in: src/file-store.ts:72

Promotes an already hashed temporary file without buffering it.

Parameters ​

ParameterType
sourcestring
hashstring

Returns ​

object

created ​

created: boolean

hash ​

hash: string

This example requires Node.js.

ts
import { FileStore } from 'osu-files'

const store = new FileStore('./files')
const hash = 'a'.repeat(64)
const temporary = store.temporaryPath(hash)

return store.putFile(temporary, hash)

read() ​

read(hash, verify?): Buffer

Defined in: src/file-store.ts:106

Reads a file and optionally verifies its content hash.

Parameters ​

ParameterTypeDefault valueDescription
hashstringundefinedSHA-256 content address.
verifybooleantrueRecompute the hash before returning.

Returns ​

Buffer

The stored file contents.

Default ​

ts
true

Throws ​

If the file is missing, invalid, or fails verification.

This example requires Node.js.

ts
import { FileStore } from 'osu-files'

const store = new FileStore('./files', true)
const hash = 'a'.repeat(64)
const content = store.read(hash)

return content.length

recoveryBasePath() ​

recoveryBasePath(): string

Defined in: src/file-store.ts:30

Returns the resolved root used by transaction recovery.

Returns ​

string

This example requires Node.js.

ts
import { FileStore } from 'osu-files'

const store = new FileStore('./files')

return store.recoveryBasePath()

remove() ​

remove(hash): boolean

Defined in: src/file-store.ts:144

Removes a stored file after checking that the session is writable.

Parameters ​

ParameterTypeDescription
hashstringSHA-256 content address.

Returns ​

boolean

Whether a file was removed.

Throws ​

If the store is read-only or the hash is invalid.

This example requires Node.js.

ts
import { FileStore } from 'osu-files'

const store = new FileStore('./files')
const stored = store.put(Buffer.from('data'))

return store.remove(stored.hash)

temporaryPath() ​

temporaryPath(hash): string

Defined in: src/file-store.ts:184

Parameters ​

ParameterType
hashstring

Returns ​

string

This example requires Node.js.

ts
import { FileStore } from 'osu-files'

const store = new FileStore('./files')

return store.temporaryPath('a'.repeat(64))

verify() ​

verify(hash): boolean

Defined in: src/file-store.ts:125

Verifies a stored file without returning its content.

Parameters ​

ParameterTypeDescription
hashstringSHA-256 content address.

Returns ​

boolean

true only when the file exists and matches its address.

This example requires Node.js.

ts
import { FileStore } from 'osu-files'

const store = new FileStore('./files', true)
const hash = 'a'.repeat(64)

return store.verify(hash)

Released under the MIT License.