Skip to content

Commit

Permalink
removed p-queue from file store
Browse files Browse the repository at this point in the history
  • Loading branch information
fenos committed Dec 8, 2023
1 parent 560852b commit 24071da
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 56 deletions.
25 changes: 10 additions & 15 deletions packages/file-store/configstores/FileConfigstore.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import fs from 'node:fs/promises'
import path from 'node:path'
import {Upload} from '@tus/server'
import PQueue from 'p-queue'

import {Configstore} from './Types'

Expand All @@ -11,40 +10,36 @@ import {Configstore} from './Types'
*/
export class FileConfigstore implements Configstore {
directory: string
queue: PQueue

constructor(path: string) {
this.directory = path
this.queue = new PQueue({concurrency: 1})
}

async get(key: string): Promise<Upload | undefined> {
try {
const buffer = await this.queue.add(() => fs.readFile(this.resolve(key), 'utf8'))
const buffer = await fs.readFile(this.resolve(key), 'utf8')
return JSON.parse(buffer as string)
} catch {
return undefined
}
}

async set(key: string, value: Upload): Promise<void> {
await this.queue.add(() => fs.writeFile(this.resolve(key), JSON.stringify(value)))
await fs.writeFile(this.resolve(key), JSON.stringify(value))
}

async delete(key: string): Promise<void> {
await this.queue.add(() => fs.rm(this.resolve(key)))
await fs.rm(this.resolve(key))
}

async list(): Promise<Array<string>> {
return this.queue.add(async () => {
const files = await fs.readdir(this.directory)
const sorted = files.sort((a, b) => a.localeCompare(b))
const name = (file: string) => path.basename(file, '.json')
// To only return tus file IDs we check if the file has a corresponding JSON info file
return sorted.filter(
(file, idx) => idx < sorted.length - 1 && name(file) === name(sorted[idx + 1])
)
})
const files = await fs.readdir(this.directory)
const sorted = files.sort((a, b) => a.localeCompare(b))
const name = (file: string) => path.basename(file, '.json')
// To only return tus file IDs we check if the file has a corresponding JSON info file
return sorted.filter(
(file, idx) => idx < sorted.length - 1 && name(file) === name(sorted[idx + 1])
)
}

private resolve(key: string): string {
Expand Down
3 changes: 1 addition & 2 deletions packages/file-store/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
"test": "mocha test.ts --exit --extension ts --require ts-node/register"
},
"dependencies": {
"debug": "^4.3.4",
"p-queue": "^6.6.2"
"debug": "^4.3.4"
},
"devDependencies": {
"@tus/server": "workspace:^",
Expand Down
5 changes: 0 additions & 5 deletions packages/server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {
import type stream from 'node:stream'
import type {ServerOptions, RouteHandler} from './types'
import type {DataStore, Upload, CancellationContext} from './models'
import {MemoryLocker} from './lockers'

type Handlers = {
GET: InstanceType<typeof GetHandler>
Expand Down Expand Up @@ -94,10 +93,6 @@ export class Server extends EventEmitter {
throw new Error("'datastore' is not defined; must have a datastore")
}

if (!options.locker) {
options.locker = new MemoryLocker()
}

const {datastore, ...rest} = options
this.options = rest
this.datastore = datastore
Expand Down
34 changes: 0 additions & 34 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1500,7 +1500,6 @@ __metadata:
eslint: ^8.48.0
eslint-config-custom: "workspace:*"
mocha: ^10.2.0
p-queue: ^6.6.2
should: ^13.2.3
typescript: ^5.2.2
peerDependencies:
Expand Down Expand Up @@ -2858,13 +2857,6 @@ __metadata:
languageName: node
linkType: hard

"eventemitter3@npm:^4.0.4":
version: 4.0.7
resolution: "eventemitter3@npm:4.0.7"
checksum: 1875311c42fcfe9c707b2712c32664a245629b42bb0a5a84439762dd0fd637fc54d078155ea83c2af9e0323c9ac13687e03cfba79b03af9f40c89b4960099374
languageName: node
linkType: hard

"events@npm:^3.3.0":
version: 3.3.0
resolution: "events@npm:3.3.0"
Expand Down Expand Up @@ -4217,13 +4209,6 @@ __metadata:
languageName: node
linkType: hard

"p-finally@npm:^1.0.0":
version: 1.0.0
resolution: "p-finally@npm:1.0.0"
checksum: 93a654c53dc805dd5b5891bab16eb0ea46db8f66c4bfd99336ae929323b1af2b70a8b0654f8f1eae924b2b73d037031366d645f1fd18b3d30cbd15950cc4b1d4
languageName: node
linkType: hard

"p-limit@npm:^3.0.1, p-limit@npm:^3.0.2":
version: 3.1.0
resolution: "p-limit@npm:3.1.0"
Expand Down Expand Up @@ -4251,25 +4236,6 @@ __metadata:
languageName: node
linkType: hard

"p-queue@npm:^6.6.2":
version: 6.6.2
resolution: "p-queue@npm:6.6.2"
dependencies:
eventemitter3: ^4.0.4
p-timeout: ^3.2.0
checksum: 832642fcc4ab6477b43e6d7c30209ab10952969ed211c6d6f2931be8a4f9935e3578c72e8cce053dc34f2eb6941a408a2c516a54904e989851a1a209cf19761c
languageName: node
linkType: hard

"p-timeout@npm:^3.2.0":
version: 3.2.0
resolution: "p-timeout@npm:3.2.0"
dependencies:
p-finally: ^1.0.0
checksum: 3dd0eaa048780a6f23e5855df3dd45c7beacff1f820476c1d0d1bcd6648e3298752ba2c877aa1c92f6453c7dd23faaf13d9f5149fc14c0598a142e2c5e8d649c
languageName: node
linkType: hard

"parent-module@npm:^1.0.0":
version: 1.0.1
resolution: "parent-module@npm:1.0.1"
Expand Down

0 comments on commit 24071da

Please sign in to comment.