@@ -5,21 +5,15 @@ import stream from 'node:stream'
55import http from 'node:http'
66
77import debug from 'debug'
8- import Configstore from 'configstore'
98
9+ import { Configstore , FileConfigstore } from './configstores'
1010import { DataStore , Upload , ERRORS } from '@tus/server'
11- import pkg from './package.json'
1211
13- type Store = {
14- get ( key : string ) : Upload | undefined
15- set ( key : string , value : Upload ) : void
16- delete ( key : string ) : void
17- all : Record < string , Upload >
18- }
12+ export * from './configstores'
1913
2014type Options = {
2115 directory : string
22- configstore ?: Store
16+ configstore ?: Configstore
2317 expirationPeriodInMilliseconds ?: number
2418}
2519
@@ -30,13 +24,13 @@ const log = debug('tus-node-server:stores:filestore')
3024
3125export class FileStore extends DataStore {
3226 directory : string
33- configstore : Store
27+ configstore : Configstore
3428 expirationPeriodInMilliseconds : number
3529
3630 constructor ( { directory, configstore, expirationPeriodInMilliseconds} : Options ) {
3731 super ( )
3832 this . directory = directory
39- this . configstore = configstore ?? new Configstore ( ` ${ pkg . name } - ${ pkg . version } ` )
33+ this . configstore = configstore ?? new FileConfigstore ( directory )
4034 this . expirationPeriodInMilliseconds = expirationPeriodInMilliseconds ?? 0
4135 this . extensions = [
4236 'creation' ,
@@ -65,13 +59,13 @@ export class FileStore extends DataStore {
6559 */
6660 create ( file : Upload ) : Promise < Upload > {
6761 return new Promise ( ( resolve , reject ) => {
68- fs . open ( path . join ( this . directory , file . id ) , 'w' , ( err , fd ) => {
62+ fs . open ( path . join ( this . directory , file . id ) , 'w' , async ( err , fd ) => {
6963 if ( err ) {
7064 log ( '[FileStore] create: Error' , err )
7165 return reject ( err )
7266 }
7367
74- this . configstore . set ( file . id , file )
68+ await this . configstore . set ( file . id , file )
7569
7670 return fs . close ( fd , ( exception ) => {
7771 if ( exception ) {
@@ -143,7 +137,7 @@ export class FileStore extends DataStore {
143137 }
144138
145139 async getUpload ( id : string ) : Promise < Upload > {
146- const file = this . configstore . get ( id )
140+ const file = await this . configstore . get ( id )
147141
148142 if ( ! file ) {
149143 throw ERRORS . FILE_NOT_FOUND
@@ -188,25 +182,29 @@ export class FileStore extends DataStore {
188182 }
189183
190184 async declareUploadLength ( id : string , upload_length : number ) {
191- const file = this . configstore . get ( id )
185+ const file = await this . configstore . get ( id )
192186
193187 if ( ! file ) {
194188 throw ERRORS . FILE_NOT_FOUND
195189 }
196190
197191 file . size = upload_length
198192
199- this . configstore . set ( id , file )
193+ await this . configstore . set ( id , file )
200194 }
201195
202196 async deleteExpired ( ) : Promise < number > {
203197 const now = new Date ( )
204198 const toDelete : Promise < void > [ ] = [ ]
205199
206- const uploadInfos = this . configstore . all
207- for ( const file_id of Object . keys ( uploadInfos ) ) {
200+ if ( ! this . configstore . list ) {
201+ throw ERRORS . UNSUPPORTED_EXPIRATION_EXTENSION
202+ }
203+
204+ const uploadKeys = await this . configstore . list ( )
205+ for ( const file_id of uploadKeys ) {
208206 try {
209- const info = uploadInfos [ file_id ]
207+ const info = await this . configstore . get ( file_id )
210208 if (
211209 info &&
212210 'creation_date' in info &&
@@ -227,7 +225,8 @@ export class FileStore extends DataStore {
227225 }
228226 }
229227
230- return Promise . all ( toDelete ) . then ( ( ) => toDelete . length )
228+ await Promise . all ( toDelete )
229+ return toDelete . length
231230 }
232231
233232 getExpiration ( ) : number {
0 commit comments