@miniflare/storage-file
Advanced tools
Comparing version 2.6.0-d1.1 to 2.6.0-d1.2
@@ -5,2 +5,4 @@ import { Clock } from '@miniflare/shared'; | ||
import { MiniflareError } from '@miniflare/shared'; | ||
import { Range } from '@miniflare/shared'; | ||
import { RangeStoredValueMeta } from '@miniflare/shared'; | ||
import { StoredKeyMeta } from '@miniflare/shared'; | ||
@@ -10,2 +12,6 @@ import { StoredMeta } from '@miniflare/shared'; | ||
export declare interface FileMeta<Meta = unknown> extends StoredMeta<Meta> { | ||
key?: string; | ||
} | ||
export declare class FileStorage extends LocalStorage { | ||
@@ -18,5 +24,6 @@ private readonly sanitise; | ||
hasMaybeExpired(key: string): Promise<StoredMeta | undefined>; | ||
headMaybeExpired<Meta>(key: string): Promise<FileMeta<Meta> | undefined>; | ||
getMaybeExpired<Meta>(key: string): Promise<StoredValueMeta<Meta> | undefined>; | ||
getSqliteDatabase(): Database.Database; | ||
getRangeMaybeExpired<Meta = unknown>(key: string, start: number, length: number): Promise<StoredValueMeta<Meta> | undefined>; | ||
getRangeMaybeExpired<Meta = unknown>(key: string, { offset: _offset, length: _length, suffix }: Range): Promise<RangeStoredValueMeta<Meta> | undefined>; | ||
put<Meta = unknown>(key: string, { value, expiration, metadata }: StoredValueMeta<Meta>): Promise<void>; | ||
@@ -23,0 +30,0 @@ deleteMaybeExpired(key: string): Promise<boolean>; |
@@ -52,9 +52,32 @@ var __create = Object.create; | ||
} | ||
async function readFileRange(filePath, start, length) { | ||
async function readFileRange(filePath, offset, length, suffix) { | ||
let fd = null; | ||
let res; | ||
try { | ||
const { size } = await import_promises.default.lstat(filePath); | ||
if (suffix !== void 0) { | ||
if (suffix <= 0) { | ||
throw new Error("Suffix must be > 0"); | ||
} | ||
if (suffix > size) | ||
suffix = size; | ||
offset = size - suffix; | ||
length = size - offset; | ||
} | ||
if (offset === void 0) | ||
offset = 0; | ||
if (length === void 0) { | ||
length = size - offset; | ||
} | ||
if (offset < 0) | ||
throw new Error("Offset must be >= 0"); | ||
if (offset >= size) | ||
throw new Error("Offset must be < size"); | ||
if (length <= 0) | ||
throw new Error("Length must be > 0"); | ||
if (offset + length > size) | ||
length = size - offset; | ||
fd = await import_promises.default.open(filePath, "r"); | ||
res = Buffer.alloc(length); | ||
await fd.read(res, 0, length, start); | ||
await fd.read(res, 0, length, offset); | ||
} catch (e) { | ||
@@ -67,3 +90,3 @@ if (e.code === "ENOENT") | ||
} | ||
return res; | ||
return { value: res, offset, length }; | ||
} | ||
@@ -113,3 +136,3 @@ async function writeFile(filePath, data) { | ||
const metaString = await readFile(keyFilePath + metaSuffix, true); | ||
return metaString ? JSON.parse(metaString) : {}; | ||
return metaString ? JSON.parse(metaString) : { expiration: void 0, metadata: void 0 }; | ||
} | ||
@@ -125,2 +148,10 @@ async hasMaybeExpired(key) { | ||
} | ||
async headMaybeExpired(key) { | ||
const [filePath] = this.keyPath(key); | ||
if (!filePath) | ||
return; | ||
if (!(0, import_fs.existsSync)(filePath)) | ||
return; | ||
return await this.meta(filePath); | ||
} | ||
async getMaybeExpired(key) { | ||
@@ -152,3 +183,3 @@ const [filePath] = this.keyPath(key); | ||
} | ||
async getRangeMaybeExpired(key, start, length) { | ||
async getRangeMaybeExpired(key, { offset: _offset, length: _length, suffix }) { | ||
const [filePath] = this.keyPath(key); | ||
@@ -158,5 +189,6 @@ if (!filePath) | ||
try { | ||
const value = await readFileRange(filePath, start, length); | ||
if (value === void 0) | ||
const res = await readFileRange(filePath, _offset, _length, suffix); | ||
if (res === void 0) | ||
return; | ||
const { value, offset, length } = res; | ||
const meta = await this.meta(filePath); | ||
@@ -166,3 +198,4 @@ return { | ||
expiration: meta.expiration, | ||
metadata: meta.metadata | ||
metadata: meta.metadata, | ||
range: { offset, length } | ||
}; | ||
@@ -169,0 +202,0 @@ } catch (e) { |
{ | ||
"name": "@miniflare/storage-file", | ||
"version": "2.6.0-d1.1", | ||
"version": "2.6.0-d1.2", | ||
"description": "File-system storage module for Miniflare: a fun, full-featured, fully-local simulator for Cloudflare Workers", | ||
@@ -38,8 +38,8 @@ "keywords": [ | ||
"dependencies": { | ||
"@miniflare/shared": "2.6.0-d1.1", | ||
"@miniflare/storage-memory": "2.6.0-d1.1" | ||
"@miniflare/shared": "2.6.0-d1.2", | ||
"@miniflare/storage-memory": "2.6.0-d1.2" | ||
}, | ||
"devDependencies": { | ||
"@miniflare/shared-test": "2.6.0-d1.1" | ||
"@miniflare/shared-test": "2.6.0-d1.2" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
15852
285
0
+ Added@miniflare/shared@2.6.0-d1.2(transitive)
+ Added@miniflare/storage-memory@2.6.0-d1.2(transitive)
- Removed@miniflare/shared@2.6.0-d1.1(transitive)
- Removed@miniflare/storage-memory@2.6.0-d1.1(transitive)
Updated@miniflare/shared@2.6.0-d1.2