fs-blob-storage
Advanced tools
Comparing version 4.0.0 to 4.1.0
@@ -25,10 +25,2 @@ import * as fs from "node:fs"; | ||
} | ||
interface FsPromises { | ||
close: typeof fs.close.__promisify__; | ||
mkdir: typeof fs.mkdir.__promisify__; | ||
open: typeof fs.open.__promisify__; | ||
rename: typeof fs.rename.__promisify__; | ||
stat: typeof fs.stat.__promisify__; | ||
unlink: typeof fs.unlink.__promisify__; | ||
} | ||
export declare const DEFAULT_EXT = ""; | ||
@@ -42,3 +34,2 @@ export declare const DEFAULT_PART = ".part"; | ||
protected path: string; | ||
protected fsPromises: FsPromises; | ||
constructor(options?: FsBlobStorageOptions); | ||
@@ -45,0 +36,0 @@ createWriteStream(key: string, options?: FsBlobStorageWriteStreamOptions): Promise<fs.WriteStream>; |
/// <reference types="node" /> | ||
import * as fs from "node:fs"; | ||
import * as path from "node:path"; | ||
import * as util from "node:util"; | ||
export const DEFAULT_EXT = ""; | ||
@@ -14,6 +13,2 @@ export const DEFAULT_PART = ".part"; | ||
this.path = options.path || "."; | ||
this.fsPromises = {}; | ||
for (const method of ["close", "mkdir", "open", "rename", "stat", "unlink"]) { | ||
this.fsPromises[method] = util.promisify(this.fs[method]); | ||
} | ||
} | ||
@@ -24,12 +19,12 @@ async createWriteStream(key, options = {}) { | ||
const dirpath = path.dirname(filepath); | ||
await this.fsPromises.mkdir(dirpath, { recursive: true }); | ||
await this.fs.promises.mkdir(dirpath, { recursive: true }); | ||
// for exclusive mode it will reject if file already exist | ||
const fd = await this.fsPromises.open(filepath, this.writeFlags); | ||
const fd = await this.fs.promises.open(filepath, this.writeFlags); | ||
if (part) { | ||
// do `open` instead of `stat` to prevent race condition | ||
const fdPart = await this.fsPromises.open(filepath + part, this.writeFlags); | ||
const fdPart = await this.fs.promises.open(filepath + part, this.writeFlags); | ||
// `close` before `rename` just for Windows | ||
await this.fsPromises.close(fdPart); | ||
await fdPart.close(); | ||
// `rename` overwrites quietly the file | ||
await this.fsPromises.rename(filepath, filepath + part); | ||
await this.fs.promises.rename(filepath, filepath + part); | ||
} | ||
@@ -42,4 +37,4 @@ // first argument is ignored | ||
const filepath = path.join(this.path, key + ext); | ||
const fd = await this.fsPromises.open(filepath, "r"); | ||
const stats = await this.fsPromises.stat(filepath); | ||
const fd = await this.fs.promises.open(filepath, "r"); | ||
const stats = await this.fs.promises.stat(filepath); | ||
if (!stats.size) { | ||
@@ -57,3 +52,3 @@ throw Object.assign(new Error(`ENOENT: empty file, open '${filepath}'`), { | ||
const filepath = path.join(this.path, key + ext); | ||
return this.fsPromises.rename(filepath + part, filepath); | ||
return this.fs.promises.rename(filepath + part, filepath); | ||
} | ||
@@ -64,5 +59,5 @@ } | ||
const filepath = path.join(this.path, key + ext); | ||
return this.fsPromises.unlink(filepath); | ||
return this.fs.promises.unlink(filepath); | ||
} | ||
} | ||
export default FsBlobStorage; |
{ | ||
"name": "fs-blob-storage", | ||
"version": "4.0.0", | ||
"version": "4.1.0", | ||
"description": "Blob storage on filesystem, with streams and promises API", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -5,3 +5,2 @@ /// <reference types="node" /> | ||
import * as path from "node:path" | ||
import * as util from "node:util" | ||
@@ -36,11 +35,2 @@ export interface FsBlobStorageOptions { | ||
interface FsPromises { | ||
close: typeof fs.close.__promisify__ | ||
mkdir: typeof fs.mkdir.__promisify__ | ||
open: typeof fs.open.__promisify__ | ||
rename: typeof fs.rename.__promisify__ | ||
stat: typeof fs.stat.__promisify__ | ||
unlink: typeof fs.unlink.__promisify__ | ||
} | ||
export const DEFAULT_EXT = "" | ||
@@ -56,4 +46,2 @@ export const DEFAULT_PART = ".part" | ||
protected fsPromises: FsPromises | ||
constructor(options: FsBlobStorageOptions = {}) { | ||
@@ -65,7 +53,2 @@ this.ext = options.ext !== undefined ? options.ext : DEFAULT_EXT | ||
this.path = options.path || "." | ||
this.fsPromises = {} as FsPromises | ||
for (const method of ["close", "mkdir", "open", "rename", "stat", "unlink"] as Array<keyof FsPromises>) { | ||
this.fsPromises[method] = util.promisify(this.fs[method]) as any | ||
} | ||
} | ||
@@ -78,16 +61,16 @@ | ||
await this.fsPromises.mkdir(dirpath, {recursive: true}) | ||
await this.fs.promises.mkdir(dirpath, {recursive: true}) | ||
// for exclusive mode it will reject if file already exist | ||
const fd = await this.fsPromises.open(filepath, this.writeFlags) | ||
const fd = await this.fs.promises.open(filepath, this.writeFlags) | ||
if (part) { | ||
// do `open` instead of `stat` to prevent race condition | ||
const fdPart = await this.fsPromises.open(filepath + part, this.writeFlags) | ||
const fdPart = await this.fs.promises.open(filepath + part, this.writeFlags) | ||
// `close` before `rename` just for Windows | ||
await this.fsPromises.close(fdPart) | ||
await fdPart.close() | ||
// `rename` overwrites quietly the file | ||
await this.fsPromises.rename(filepath, filepath + part) | ||
await this.fs.promises.rename(filepath, filepath + part) | ||
} | ||
@@ -103,5 +86,5 @@ | ||
const fd = await this.fsPromises.open(filepath, "r") | ||
const fd = await this.fs.promises.open(filepath, "r") | ||
const stats = await this.fsPromises.stat(filepath) | ||
const stats = await this.fs.promises.stat(filepath) | ||
@@ -122,3 +105,3 @@ if (!stats.size) { | ||
const filepath = path.join(this.path, key + ext) | ||
return this.fsPromises.rename(filepath + part, filepath) | ||
return this.fs.promises.rename(filepath + part, filepath) | ||
} | ||
@@ -130,3 +113,3 @@ } | ||
const filepath = path.join(this.path, key + ext) | ||
return this.fsPromises.unlink(filepath) | ||
return this.fs.promises.unlink(filepath) | ||
} | ||
@@ -133,0 +116,0 @@ } |
15624
200