@miniflare/storage-memory
Advanced tools
Comparing version 2.6.0-d1.1 to 2.6.0
import { Awaitable } from '@miniflare/shared'; | ||
import { Clock } from '@miniflare/shared'; | ||
import Database from 'better-sqlite3'; | ||
import { Range } from '@miniflare/shared'; | ||
import { RangeStoredValueMeta } from '@miniflare/shared'; | ||
import { Storage } from '@miniflare/shared'; | ||
@@ -22,3 +23,5 @@ import { StorageListOptions } from '@miniflare/shared'; | ||
abstract hasMaybeExpired(key: string): Awaitable<StoredMeta | undefined>; | ||
abstract headMaybeExpired<Meta>(key: string): Awaitable<StoredMeta<Meta> | undefined>; | ||
abstract getMaybeExpired<Meta>(key: string): Awaitable<StoredValueMeta<Meta> | undefined>; | ||
abstract getRangeMaybeExpired<Meta>(key: string, range: Range): Awaitable<RangeStoredValueMeta<Meta> | undefined>; | ||
abstract deleteMaybeExpired(key: string): Awaitable<boolean>; | ||
@@ -28,6 +31,7 @@ abstract listAllMaybeExpired<Meta>(): Awaitable<StoredKeyMeta<Meta>[]>; | ||
has(key: string): Promise<boolean>; | ||
head<Meta = unknown>(key: string): Promise<StoredMeta<Meta> | undefined>; | ||
get<Meta = unknown>(key: string): Promise<StoredValueMeta<Meta> | undefined>; | ||
getRange<Meta = unknown>(key: string, range?: Range): Promise<RangeStoredValueMeta<Meta> | undefined>; | ||
delete(key: string): Promise<boolean>; | ||
list<Meta = unknown>(options?: StorageListOptions): Promise<StorageListResult<StoredKeyMeta<Meta>>>; | ||
getSqliteDatabase(): Database.Database; | ||
} | ||
@@ -39,3 +43,5 @@ | ||
hasMaybeExpired(key: string): StoredMeta | undefined; | ||
headMaybeExpired<Meta>(key: string): StoredMeta<Meta> | undefined; | ||
getMaybeExpired<Meta>(key: string): StoredValueMeta<Meta> | undefined; | ||
getRangeMaybeExpired<Meta>(key: string, { offset, length, suffix }: Range): RangeStoredValueMeta<Meta> | undefined; | ||
put<Meta = unknown>(key: string, value: StoredValueMeta<Meta>): void; | ||
@@ -42,0 +48,0 @@ deleteMaybeExpired(key: string): boolean; |
@@ -43,2 +43,3 @@ var __create = Object.create; | ||
function listPaginate(options, keys) { | ||
const resKeys = []; | ||
const direction = options?.reverse ? -1 : 1; | ||
@@ -54,6 +55,39 @@ keys.sort((a, b) => direction * (0, import_shared.lexicographicCompare)(a.name, b.name)); | ||
} | ||
const endIndex = options?.limit === void 0 ? keys.length : startIndex + options.limit; | ||
const nextCursor = endIndex < keys.length ? (0, import_shared.base64Encode)(keys[endIndex - 1].name) : ""; | ||
keys = keys.slice(startIndex, endIndex); | ||
return { keys, cursor: nextCursor }; | ||
let endIndex = startIndex; | ||
const prefix = options?.prefix ?? ""; | ||
const delimitedPrefixes = new Set(); | ||
for (let i = startIndex; i < keys.length; i++) { | ||
const key = keys[i]; | ||
const { name } = key; | ||
endIndex = i; | ||
if (options?.delimiter !== void 0 && name.startsWith(prefix) && name.slice(prefix.length).includes(options.delimiter)) { | ||
const { delimiter } = options; | ||
const objectKey = name.slice(prefix.length); | ||
const delimitedPrefix = prefix + objectKey.split(delimiter)[0] + delimiter; | ||
delimitedPrefixes.add(delimitedPrefix); | ||
while (i < keys.length) { | ||
const nextKey = keys[i]; | ||
const nextName = nextKey.name; | ||
if (!nextName.startsWith(delimitedPrefix)) | ||
break; | ||
endIndex = i; | ||
i++; | ||
} | ||
i--; | ||
} else { | ||
resKeys.push(key); | ||
} | ||
if (options?.limit !== void 0 && resKeys.length + delimitedPrefixes.size >= options.limit) { | ||
break; | ||
} | ||
} | ||
const nextCursor = endIndex < keys.length - 1 ? (0, import_shared.base64Encode)(keys[endIndex].name) : ""; | ||
const res = { | ||
keys: resKeys, | ||
cursor: nextCursor | ||
}; | ||
if (options?.delimiter !== void 0) { | ||
res.delimitedPrefixes = Array.from(delimitedPrefixes); | ||
} | ||
return res; | ||
} | ||
@@ -63,3 +97,2 @@ | ||
var import_shared2 = __toModule(require("@miniflare/shared")); | ||
var import_better_sqlite3 = __toModule(require("better-sqlite3")); | ||
var LocalStorage = class extends import_shared2.Storage { | ||
@@ -83,2 +116,12 @@ constructor(clock = import_shared2.defaultClock) { | ||
} | ||
async head(key) { | ||
const stored = await this.headMaybeExpired(key); | ||
if (stored === void 0) | ||
return void 0; | ||
if (this.expired(stored)) { | ||
await this.deleteMaybeExpired(key); | ||
return void 0; | ||
} | ||
return stored; | ||
} | ||
async get(key) { | ||
@@ -94,2 +137,12 @@ const stored = await this.getMaybeExpired(key); | ||
} | ||
async getRange(key, range = {}) { | ||
const stored = await this.getRangeMaybeExpired(key, range); | ||
if (stored === void 0) | ||
return void 0; | ||
if (this.expired(stored)) { | ||
await this.deleteMaybeExpired(key); | ||
return void 0; | ||
} | ||
return stored; | ||
} | ||
async delete(key) { | ||
@@ -118,8 +171,2 @@ const stored = await this.hasMaybeExpired(key); | ||
} | ||
getSqliteDatabase() { | ||
const location = (0, import_shared2.getSQLiteNativeBindingLocation)(); | ||
return new import_better_sqlite3.default(":memory:", { | ||
nativeBinding: location | ||
}); | ||
} | ||
}; | ||
@@ -141,2 +188,9 @@ | ||
} | ||
headMaybeExpired(key) { | ||
const stored = this.map.get(key); | ||
return stored && { | ||
expiration: stored.expiration, | ||
metadata: cloneMetadata(stored.metadata) | ||
}; | ||
} | ||
getMaybeExpired(key) { | ||
@@ -150,2 +204,39 @@ const stored = this.map.get(key); | ||
} | ||
getRangeMaybeExpired(key, { offset, length, suffix }) { | ||
const stored = this.map.get(key); | ||
if (stored === void 0) | ||
return; | ||
const { value } = stored; | ||
const size = value.byteLength; | ||
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; | ||
return { | ||
value: value.slice(offset, offset + length), | ||
expiration: stored.expiration, | ||
metadata: cloneMetadata(stored.metadata), | ||
range: { | ||
offset, | ||
length | ||
} | ||
}; | ||
} | ||
put(key, value) { | ||
@@ -152,0 +243,0 @@ this.map.set(key, { |
{ | ||
"name": "@miniflare/storage-memory", | ||
"version": "2.6.0-d1.1", | ||
"version": "2.6.0", | ||
"description": "In-memory storage module for Miniflare: a fun, full-featured, fully-local simulator for Cloudflare Workers", | ||
@@ -38,7 +38,7 @@ "keywords": [ | ||
"dependencies": { | ||
"@miniflare/shared": "2.6.0-d1.1" | ||
"@miniflare/shared": "2.6.0" | ||
}, | ||
"devDependencies": { | ||
"@miniflare/shared-test": "2.6.0-d1.1" | ||
"@miniflare/shared-test": "2.6.0" | ||
} | ||
} |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
16901
303
1
0
+ Added@miniflare/shared@2.6.0(transitive)
- Removed@miniflare/shared@2.6.0-d1.1(transitive)
- Removed@types/better-sqlite3@7.6.11(transitive)
- Removed@types/node@22.9.0(transitive)
- Removedbase64-js@1.5.1(transitive)
- Removedbetter-sqlite3@7.6.2(transitive)
- Removedbindings@1.5.0(transitive)
- Removedbl@4.1.0(transitive)
- Removedbuffer@5.7.1(transitive)
- Removedchownr@1.1.4(transitive)
- Removeddecompress-response@6.0.0(transitive)
- Removeddeep-extend@0.6.0(transitive)
- Removeddetect-libc@2.0.3(transitive)
- Removedend-of-stream@1.4.4(transitive)
- Removedexpand-template@2.0.3(transitive)
- Removedfile-uri-to-path@1.0.0(transitive)
- Removedfs-constants@1.0.0(transitive)
- Removedgithub-from-package@0.0.0(transitive)
- Removedieee754@1.2.1(transitive)
- Removedinherits@2.0.4(transitive)
- Removedini@1.3.8(transitive)
- Removedmimic-response@3.1.0(transitive)
- Removedminimist@1.2.8(transitive)
- Removedmkdirp-classic@0.5.3(transitive)
- Removednapi-build-utils@1.0.2(transitive)
- Removednode-abi@3.71.0(transitive)
- Removedonce@1.4.0(transitive)
- Removedprebuild-install@7.1.2(transitive)
- Removedpump@3.0.2(transitive)
- Removedrc@1.2.8(transitive)
- Removedreadable-stream@3.6.2(transitive)
- Removedsafe-buffer@5.2.1(transitive)
- Removedsemver@7.6.3(transitive)
- Removedsimple-concat@1.0.1(transitive)
- Removedsimple-get@4.0.1(transitive)
- Removedstring_decoder@1.3.0(transitive)
- Removedstrip-json-comments@2.0.1(transitive)
- Removedtar-fs@2.1.1(transitive)
- Removedtar-stream@2.2.0(transitive)
- Removedtunnel-agent@0.6.0(transitive)
- Removedundici-types@6.19.8(transitive)
- Removedutil-deprecate@1.0.2(transitive)
- Removedwrappy@1.0.2(transitive)
Updated@miniflare/shared@2.6.0