@hold-baby/bucket-db 0.3.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +503 -19
- package/dist/bucket-db.d.ts +25 -0
- package/dist/bucket-db.js +36 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +133 -0
- package/dist/collection.d.ts +78 -0
- package/dist/collection.js +321 -0
- package/dist/doctor.d.ts +7 -0
- package/dist/doctor.js +100 -0
- package/dist/errors.d.ts +59 -0
- package/dist/errors.js +57 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +6 -0
- package/dist/keys.d.ts +12 -0
- package/dist/keys.js +31 -0
- package/dist/query.d.ts +16 -0
- package/dist/query.js +133 -0
- package/dist/snapshot.d.ts +16 -0
- package/dist/snapshot.js +40 -0
- package/dist/storage/adapter.d.ts +75 -0
- package/dist/storage/adapter.js +27 -0
- package/dist/storage/fs.d.ts +28 -0
- package/dist/storage/fs.js +175 -0
- package/dist/storage/memory.d.ts +18 -0
- package/dist/storage/memory.js +49 -0
- package/dist/storage/s3/adapters.d.ts +38 -0
- package/dist/storage/s3/adapters.js +65 -0
- package/dist/storage/s3/client.d.ts +52 -0
- package/dist/storage/s3/client.js +231 -0
- package/dist/storage/s3/credentials.d.ts +17 -0
- package/dist/storage/s3/credentials.js +42 -0
- package/dist/storage/s3/sigv4.d.ts +32 -0
- package/dist/storage/s3/sigv4.js +75 -0
- package/dist/types.d.ts +55 -0
- package/dist/types.js +1 -0
- package/dist/url.d.ts +16 -0
- package/dist/url.js +66 -0
- package/llms.txt +142 -0
- package/package.json +20 -7
- package/src/cache/lru-cache.ts +0 -131
- package/src/core/bucket-db.ts +0 -47
- package/src/core/collection.ts +0 -278
- package/src/core/wal.ts +0 -106
- package/src/index/shard-manager.ts +0 -197
- package/src/index.ts +0 -37
- package/src/query/evaluator.ts +0 -143
- package/src/storage/ali-oss.d.ts +0 -43
- package/src/storage/fs-adapter.ts +0 -248
- package/src/storage/memory-adapter.ts +0 -105
- package/src/storage/oss-adapter.ts +0 -164
- package/src/storage/s3-adapter.ts +0 -200
- package/src/types/collection.ts +0 -43
- package/src/types/document.ts +0 -22
- package/src/types/errors.ts +0 -49
- package/src/types/index.ts +0 -37
- package/src/types/query.ts +0 -50
- package/src/types/storage.ts +0 -46
- package/src/utils/hash.ts +0 -24
- package/src/utils/path-validator.ts +0 -119
- package/tests/core/bucket-db.test.d.ts +0 -2
- package/tests/core/bucket-db.test.d.ts.map +0 -1
- package/tests/core/bucket-db.test.js +0 -34
- package/tests/core/bucket-db.test.js.map +0 -1
- package/tests/core/bucket-db.test.ts +0 -50
- package/tests/core/collection.test.d.ts +0 -2
- package/tests/core/collection.test.d.ts.map +0 -1
- package/tests/core/collection.test.js +0 -148
- package/tests/core/collection.test.js.map +0 -1
- package/tests/core/collection.test.ts +0 -195
- package/tests/core/dbpath.test.ts +0 -52
- package/tests/index/shard-manager.test.d.ts +0 -2
- package/tests/index/shard-manager.test.d.ts.map +0 -1
- package/tests/index/shard-manager.test.js +0 -106
- package/tests/index/shard-manager.test.js.map +0 -1
- package/tests/index/shard-manager.test.ts +0 -129
- package/tests/query/evaluator.test.d.ts +0 -2
- package/tests/query/evaluator.test.d.ts.map +0 -1
- package/tests/query/evaluator.test.js +0 -87
- package/tests/query/evaluator.test.js.map +0 -1
- package/tests/query/evaluator.test.ts +0 -104
- package/tests/storage/fs-adapter.test.ts +0 -111
- package/tests/storage/memory-adapter.test.d.ts +0 -2
- package/tests/storage/memory-adapter.test.d.ts.map +0 -1
- package/tests/storage/memory-adapter.test.js +0 -65
- package/tests/storage/memory-adapter.test.js.map +0 -1
- package/tests/storage/memory-adapter.test.ts +0 -84
- package/tests/storage/oss-adapter.test.d.ts +0 -2
- package/tests/storage/oss-adapter.test.d.ts.map +0 -1
- package/tests/storage/oss-adapter.test.js +0 -34
- package/tests/storage/oss-adapter.test.js.map +0 -1
- package/tests/storage/oss-adapter.test.ts +0 -40
- package/tests/storage/s3-adapter.test.d.ts +0 -2
- package/tests/storage/s3-adapter.test.d.ts.map +0 -1
- package/tests/storage/s3-adapter.test.js +0 -51
- package/tests/storage/s3-adapter.test.js.map +0 -1
- package/tests/storage/s3-adapter.test.ts +0 -62
- package/tests/utils/hash.test.d.ts +0 -2
- package/tests/utils/hash.test.d.ts.map +0 -1
- package/tests/utils/hash.test.js +0 -36
- package/tests/utils/hash.test.js.map +0 -1
- package/tests/utils/hash.test.ts +0 -42
- package/tsconfig.json +0 -12
package/src/query/evaluator.ts
DELETED
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
import type { QueryValue, QueryFilter } from '../types/index.js';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Allowed query operators (whitelist)
|
|
5
|
-
*/
|
|
6
|
-
const ALLOWED_OPERATORS = new Set([
|
|
7
|
-
'$eq', '$ne', '$gt', '$gte', '$lt', '$lte', '$in', '$nin'
|
|
8
|
-
]);
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Reserved field names that cannot be queried (security)
|
|
12
|
-
*/
|
|
13
|
-
const RESERVED_FIELDS = new Set([
|
|
14
|
-
'__proto__',
|
|
15
|
-
'constructor',
|
|
16
|
-
'prototype',
|
|
17
|
-
'toString',
|
|
18
|
-
'valueOf',
|
|
19
|
-
'hasOwnProperty',
|
|
20
|
-
'isPrototypeOf',
|
|
21
|
-
'propertyIsEnumerable'
|
|
22
|
-
]);
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Validate query filter for security
|
|
26
|
-
*/
|
|
27
|
-
function validateFilter<T extends Record<string, any>>(filter: QueryFilter<T>): void {
|
|
28
|
-
for (const [field, condition] of Object.entries(filter)) {
|
|
29
|
-
// Check for reserved field names
|
|
30
|
-
if (RESERVED_FIELDS.has(field)) {
|
|
31
|
-
throw new Error('Query contains forbidden field name');
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
// Validate operators if condition is an object
|
|
35
|
-
if (typeof condition === 'object' && condition !== null && !Array.isArray(condition)) {
|
|
36
|
-
for (const op of Object.keys(condition)) {
|
|
37
|
-
if (!ALLOWED_OPERATORS.has(op)) {
|
|
38
|
-
throw new Error('Query contains invalid operator');
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Evaluates a single operator condition
|
|
47
|
-
*/
|
|
48
|
-
export function evaluateOperator<T>(
|
|
49
|
-
value: T,
|
|
50
|
-
condition: QueryValue<T>
|
|
51
|
-
): boolean {
|
|
52
|
-
// Direct value comparison (implicit $eq)
|
|
53
|
-
if (typeof condition !== 'object' || condition === null || Array.isArray(condition)) {
|
|
54
|
-
return value === condition;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const operators = condition as Record<string, any>;
|
|
58
|
-
|
|
59
|
-
// Check all operators (AND logic)
|
|
60
|
-
for (const [op, operand] of Object.entries(operators)) {
|
|
61
|
-
switch (op) {
|
|
62
|
-
case '$eq':
|
|
63
|
-
if (value !== operand) return false;
|
|
64
|
-
break;
|
|
65
|
-
|
|
66
|
-
case '$ne':
|
|
67
|
-
if (value === operand) return false;
|
|
68
|
-
break;
|
|
69
|
-
|
|
70
|
-
case '$gt':
|
|
71
|
-
case '$gte':
|
|
72
|
-
case '$lt':
|
|
73
|
-
case '$lte':
|
|
74
|
-
// Type check: only allow comparison for numbers and strings
|
|
75
|
-
if (typeof value !== typeof operand) {
|
|
76
|
-
throw new Error('Type mismatch in comparison operator');
|
|
77
|
-
}
|
|
78
|
-
if (typeof value !== 'number' && typeof value !== 'string') {
|
|
79
|
-
throw new Error('Comparison operators require number or string values');
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
if (op === '$gt' && !(value > operand)) return false;
|
|
83
|
-
if (op === '$gte' && !(value >= operand)) return false;
|
|
84
|
-
if (op === '$lt' && !(value < operand)) return false;
|
|
85
|
-
if (op === '$lte' && !(value <= operand)) return false;
|
|
86
|
-
break;
|
|
87
|
-
|
|
88
|
-
case '$in':
|
|
89
|
-
if (!Array.isArray(operand)) {
|
|
90
|
-
throw new Error('$in operator requires array operand');
|
|
91
|
-
}
|
|
92
|
-
if (operand.length === 0) return false; // Empty array never matches
|
|
93
|
-
if (!operand.includes(value)) return false;
|
|
94
|
-
break;
|
|
95
|
-
|
|
96
|
-
case '$nin':
|
|
97
|
-
if (!Array.isArray(operand)) {
|
|
98
|
-
throw new Error('$nin operator requires array operand');
|
|
99
|
-
}
|
|
100
|
-
if (operand.length === 0) return true; // Empty array means nothing excluded
|
|
101
|
-
if (operand.includes(value)) return false;
|
|
102
|
-
break;
|
|
103
|
-
|
|
104
|
-
default:
|
|
105
|
-
// Should never reach here due to validateFilter check
|
|
106
|
-
throw new Error('Invalid operator');
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
return true;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* Evaluates a complete filter against a document
|
|
115
|
-
*/
|
|
116
|
-
export function evaluateFilter<T extends Record<string, any>>(
|
|
117
|
-
document: T,
|
|
118
|
-
filter: QueryFilter<T>
|
|
119
|
-
): boolean {
|
|
120
|
-
// Empty filter matches all documents
|
|
121
|
-
if (Object.keys(filter).length === 0) {
|
|
122
|
-
return true;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
// Validate filter for security
|
|
126
|
-
validateFilter(filter);
|
|
127
|
-
|
|
128
|
-
// All conditions must match (AND logic)
|
|
129
|
-
for (const [field, condition] of Object.entries(filter)) {
|
|
130
|
-
// Only access own properties, not inherited ones
|
|
131
|
-
if (!Object.prototype.hasOwnProperty.call(document, field)) {
|
|
132
|
-
return false;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
const value = document[field];
|
|
136
|
-
|
|
137
|
-
if (!evaluateOperator(value, condition as QueryValue<any>)) {
|
|
138
|
-
return false;
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
return true;
|
|
143
|
-
}
|
package/src/storage/ali-oss.d.ts
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
declare module 'ali-oss' {
|
|
2
|
-
export default class OSS {
|
|
3
|
-
constructor(options: {
|
|
4
|
-
region: string;
|
|
5
|
-
accessKeyId: string;
|
|
6
|
-
accessKeySecret: string;
|
|
7
|
-
bucket: string;
|
|
8
|
-
endpoint?: string;
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
get(key: string): Promise<{
|
|
12
|
-
content: Buffer;
|
|
13
|
-
res: {
|
|
14
|
-
headers: {
|
|
15
|
-
etag?: string;
|
|
16
|
-
'last-modified': string;
|
|
17
|
-
};
|
|
18
|
-
};
|
|
19
|
-
}>;
|
|
20
|
-
|
|
21
|
-
put(
|
|
22
|
-
key: string,
|
|
23
|
-
data: Buffer,
|
|
24
|
-
options?: {
|
|
25
|
-
headers?: Record<string, string>;
|
|
26
|
-
}
|
|
27
|
-
): Promise<{
|
|
28
|
-
res: {
|
|
29
|
-
headers: {
|
|
30
|
-
etag?: string;
|
|
31
|
-
};
|
|
32
|
-
};
|
|
33
|
-
}>;
|
|
34
|
-
|
|
35
|
-
delete(key: string): Promise<void>;
|
|
36
|
-
|
|
37
|
-
head(key: string): Promise<void>;
|
|
38
|
-
|
|
39
|
-
list(options?: { prefix?: string }): Promise<{
|
|
40
|
-
objects?: Array<{ name: string }>;
|
|
41
|
-
}>;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
@@ -1,248 +0,0 @@
|
|
|
1
|
-
import { readFileSync, writeFileSync, unlinkSync, existsSync, mkdirSync, statSync, readdirSync, renameSync } from 'fs';
|
|
2
|
-
import { join, dirname, relative } from 'path';
|
|
3
|
-
import { createHash } from 'crypto';
|
|
4
|
-
import type {
|
|
5
|
-
StorageAdapter,
|
|
6
|
-
StorageObject,
|
|
7
|
-
PutOptions,
|
|
8
|
-
} from '../types/index.js';
|
|
9
|
-
import { StorageError } from '../types/index.js';
|
|
10
|
-
import { safeKeyPath, validatePathWithinBase } from '../utils/path-validator.js';
|
|
11
|
-
|
|
12
|
-
export interface FileSystemAdapterConfig {
|
|
13
|
-
basePath: string;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
interface FileMetadata {
|
|
17
|
-
etag: string;
|
|
18
|
-
data: any;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Local file system storage adapter
|
|
23
|
-
*/
|
|
24
|
-
export class FileSystemAdapter implements StorageAdapter {
|
|
25
|
-
private basePath: string;
|
|
26
|
-
|
|
27
|
-
constructor(config: FileSystemAdapterConfig) {
|
|
28
|
-
this.basePath = config.basePath;
|
|
29
|
-
|
|
30
|
-
// Ensure base directory exists
|
|
31
|
-
if (!existsSync(this.basePath)) {
|
|
32
|
-
mkdirSync(this.basePath, { recursive: true });
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
private getFilePath(key: string): string {
|
|
37
|
-
return safeKeyPath(this.basePath, key);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
private getMetadataPath(key: string): string {
|
|
41
|
-
return safeKeyPath(this.basePath, key + '.meta');
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
private generateETag(data: any): string {
|
|
45
|
-
const content = JSON.stringify(data);
|
|
46
|
-
return createHash('md5').update(content).digest('hex');
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
private ensureDirectoryExists(filePath: string): void {
|
|
50
|
-
const dir = dirname(filePath);
|
|
51
|
-
if (!existsSync(dir)) {
|
|
52
|
-
mkdirSync(dir, { recursive: true });
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
async get(key: string): Promise<StorageObject> {
|
|
57
|
-
const filePath = this.getFilePath(key);
|
|
58
|
-
const metaPath = this.getMetadataPath(key);
|
|
59
|
-
|
|
60
|
-
try {
|
|
61
|
-
if (!existsSync(filePath)) {
|
|
62
|
-
throw new StorageError(`Object not found: ${key}`);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
const content = readFileSync(filePath, 'utf-8');
|
|
66
|
-
const data = JSON.parse(content);
|
|
67
|
-
|
|
68
|
-
let etag: string;
|
|
69
|
-
if (existsSync(metaPath)) {
|
|
70
|
-
const metadata: FileMetadata = JSON.parse(readFileSync(metaPath, 'utf-8'));
|
|
71
|
-
etag = metadata.etag;
|
|
72
|
-
} else {
|
|
73
|
-
etag = this.generateETag(data);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
const stats = statSync(filePath);
|
|
77
|
-
|
|
78
|
-
return {
|
|
79
|
-
data,
|
|
80
|
-
etag,
|
|
81
|
-
lastModified: stats.mtime,
|
|
82
|
-
};
|
|
83
|
-
} catch (error: any) {
|
|
84
|
-
if (error instanceof StorageError) {
|
|
85
|
-
throw error;
|
|
86
|
-
}
|
|
87
|
-
throw new StorageError(`Failed to get object: ${error.message}`, error);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
async put(key: string, data: any, options?: PutOptions): Promise<{ etag: string }> {
|
|
92
|
-
const filePath = this.getFilePath(key);
|
|
93
|
-
const metaPath = this.getMetadataPath(key);
|
|
94
|
-
|
|
95
|
-
try {
|
|
96
|
-
// Check conditional writes
|
|
97
|
-
if (options?.ifMatch || options?.ifNoneMatch) {
|
|
98
|
-
if (existsSync(metaPath)) {
|
|
99
|
-
const currentMeta: FileMetadata = JSON.parse(readFileSync(metaPath, 'utf-8'));
|
|
100
|
-
|
|
101
|
-
if (options.ifMatch && currentMeta.etag !== options.ifMatch) {
|
|
102
|
-
throw new StorageError('PreconditionFailed: ETag mismatch');
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
if (options.ifNoneMatch && existsSync(filePath)) {
|
|
106
|
-
throw new StorageError('PreconditionFailed: Object already exists');
|
|
107
|
-
}
|
|
108
|
-
} else if (options.ifMatch) {
|
|
109
|
-
// ifMatch provided but no existing metadata
|
|
110
|
-
throw new StorageError('PreconditionFailed: ETag mismatch');
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// Ensure directory exists
|
|
115
|
-
this.ensureDirectoryExists(filePath);
|
|
116
|
-
|
|
117
|
-
// Generate ETag
|
|
118
|
-
const etag = this.generateETag(data);
|
|
119
|
-
|
|
120
|
-
// Use temporary files for atomic writes
|
|
121
|
-
const tmpFilePath = filePath + '.tmp';
|
|
122
|
-
const tmpMetaPath = metaPath + '.tmp';
|
|
123
|
-
|
|
124
|
-
try {
|
|
125
|
-
// Write to temporary files first
|
|
126
|
-
writeFileSync(tmpFilePath, JSON.stringify(data, null, 2), 'utf-8');
|
|
127
|
-
|
|
128
|
-
const metadata: FileMetadata = { etag, data };
|
|
129
|
-
writeFileSync(tmpMetaPath, JSON.stringify(metadata), 'utf-8');
|
|
130
|
-
|
|
131
|
-
// Atomic rename - both files become visible together
|
|
132
|
-
renameSync(tmpFilePath, filePath);
|
|
133
|
-
renameSync(tmpMetaPath, metaPath);
|
|
134
|
-
|
|
135
|
-
return { etag };
|
|
136
|
-
} catch (writeError: any) {
|
|
137
|
-
// Clean up temporary files on failure
|
|
138
|
-
try {
|
|
139
|
-
if (existsSync(tmpFilePath)) unlinkSync(tmpFilePath);
|
|
140
|
-
if (existsSync(tmpMetaPath)) unlinkSync(tmpMetaPath);
|
|
141
|
-
} catch {
|
|
142
|
-
// Ignore cleanup errors
|
|
143
|
-
}
|
|
144
|
-
throw writeError;
|
|
145
|
-
}
|
|
146
|
-
} catch (error: any) {
|
|
147
|
-
if (error instanceof StorageError) {
|
|
148
|
-
throw error;
|
|
149
|
-
}
|
|
150
|
-
throw new StorageError(`Failed to put object: ${error.message}`, error);
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
async delete(key: string): Promise<void> {
|
|
155
|
-
const filePath = this.getFilePath(key);
|
|
156
|
-
const metaPath = this.getMetadataPath(key);
|
|
157
|
-
|
|
158
|
-
try {
|
|
159
|
-
if (existsSync(filePath)) {
|
|
160
|
-
unlinkSync(filePath);
|
|
161
|
-
}
|
|
162
|
-
if (existsSync(metaPath)) {
|
|
163
|
-
unlinkSync(metaPath);
|
|
164
|
-
}
|
|
165
|
-
} catch (error: any) {
|
|
166
|
-
throw new StorageError(`Failed to delete object: ${error.message}`, error);
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
async exists(key: string): Promise<boolean> {
|
|
171
|
-
const filePath = this.getFilePath(key);
|
|
172
|
-
return existsSync(filePath);
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
async listKeys(prefix: string): Promise<string[]> {
|
|
176
|
-
const keys: string[] = [];
|
|
177
|
-
const prefixPath = join(this.basePath, prefix);
|
|
178
|
-
|
|
179
|
-
// Validate that prefix path doesn't escape base directory
|
|
180
|
-
try {
|
|
181
|
-
validatePathWithinBase(this.basePath, prefixPath);
|
|
182
|
-
} catch (error) {
|
|
183
|
-
throw new StorageError(`Invalid prefix path: ${(error as Error).message}`);
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
try {
|
|
187
|
-
const walk = (dir: string): void => {
|
|
188
|
-
if (!existsSync(dir)) {
|
|
189
|
-
return;
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
const files = readdirSync(dir, { withFileTypes: true });
|
|
193
|
-
|
|
194
|
-
for (const file of files) {
|
|
195
|
-
const fullPath = join(dir, file.name);
|
|
196
|
-
|
|
197
|
-
if (file.isDirectory()) {
|
|
198
|
-
walk(fullPath);
|
|
199
|
-
} else if (file.isFile() && !file.name.endsWith('.meta')) {
|
|
200
|
-
// Get relative path from base
|
|
201
|
-
const relativePath = relative(this.basePath, fullPath);
|
|
202
|
-
|
|
203
|
-
// Check if it starts with the prefix
|
|
204
|
-
if (relativePath.startsWith(prefix)) {
|
|
205
|
-
keys.push(relativePath);
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
};
|
|
210
|
-
|
|
211
|
-
// If prefix path exists as a directory, walk it
|
|
212
|
-
if (existsSync(prefixPath) && statSync(prefixPath).isDirectory()) {
|
|
213
|
-
walk(prefixPath);
|
|
214
|
-
} else {
|
|
215
|
-
// Walk from base and filter by prefix
|
|
216
|
-
walk(this.basePath);
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
return keys.sort();
|
|
220
|
-
} catch (error: any) {
|
|
221
|
-
throw new StorageError(`Failed to list keys: ${error.message}`, error);
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
async batchGet(keys: string[]): Promise<Map<string, StorageObject>> {
|
|
226
|
-
const results = new Map<string, StorageObject>();
|
|
227
|
-
|
|
228
|
-
// Fetch in parallel
|
|
229
|
-
const promises = keys.map(async (key) => {
|
|
230
|
-
try {
|
|
231
|
-
const obj = await this.get(key);
|
|
232
|
-
return { key, obj };
|
|
233
|
-
} catch (error) {
|
|
234
|
-
return null;
|
|
235
|
-
}
|
|
236
|
-
});
|
|
237
|
-
|
|
238
|
-
const settled = await Promise.all(promises);
|
|
239
|
-
|
|
240
|
-
for (const result of settled) {
|
|
241
|
-
if (result) {
|
|
242
|
-
results.set(result.key, result.obj);
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
return results;
|
|
247
|
-
}
|
|
248
|
-
}
|
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
StorageAdapter,
|
|
3
|
-
StorageObject,
|
|
4
|
-
PutOptions,
|
|
5
|
-
} from '../types/index.js';
|
|
6
|
-
import { StorageError } from '../types/index.js';
|
|
7
|
-
|
|
8
|
-
interface StorageEntry {
|
|
9
|
-
data: any;
|
|
10
|
-
etag: string;
|
|
11
|
-
lastModified: Date;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* In-memory storage adapter for testing
|
|
16
|
-
*/
|
|
17
|
-
export class MemoryStorageAdapter implements StorageAdapter {
|
|
18
|
-
private storage = new Map<string, StorageEntry>();
|
|
19
|
-
private etagCounter = 0;
|
|
20
|
-
|
|
21
|
-
private generateETag(): string {
|
|
22
|
-
return `etag-${++this.etagCounter}-${Date.now()}`;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
async get(key: string): Promise<StorageObject> {
|
|
26
|
-
const entry = this.storage.get(key);
|
|
27
|
-
if (!entry) {
|
|
28
|
-
throw new StorageError(`Object not found: ${key}`);
|
|
29
|
-
}
|
|
30
|
-
return {
|
|
31
|
-
data: JSON.parse(JSON.stringify(entry.data)), // Deep clone
|
|
32
|
-
etag: entry.etag,
|
|
33
|
-
lastModified: entry.lastModified,
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
async put(
|
|
38
|
-
key: string,
|
|
39
|
-
data: any,
|
|
40
|
-
options?: PutOptions
|
|
41
|
-
): Promise<{ etag: string }> {
|
|
42
|
-
const existing = this.storage.get(key);
|
|
43
|
-
|
|
44
|
-
// Check ifMatch condition
|
|
45
|
-
if (options?.ifMatch) {
|
|
46
|
-
if (!existing || existing.etag !== options.ifMatch) {
|
|
47
|
-
throw new StorageError('PreconditionFailed: ETag mismatch');
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
// Check ifNoneMatch condition
|
|
52
|
-
if (options?.ifNoneMatch && existing) {
|
|
53
|
-
throw new StorageError('PreconditionFailed: Object already exists');
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const etag = this.generateETag();
|
|
57
|
-
this.storage.set(key, {
|
|
58
|
-
data: JSON.parse(JSON.stringify(data)), // Deep clone
|
|
59
|
-
etag,
|
|
60
|
-
lastModified: new Date(),
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
return { etag };
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
async delete(key: string): Promise<void> {
|
|
67
|
-
this.storage.delete(key);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
async exists(key: string): Promise<boolean> {
|
|
71
|
-
return this.storage.has(key);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
async listKeys(prefix: string): Promise<string[]> {
|
|
75
|
-
const keys: string[] = [];
|
|
76
|
-
for (const key of this.storage.keys()) {
|
|
77
|
-
if (key.startsWith(prefix)) {
|
|
78
|
-
keys.push(key);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
return keys.sort();
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
async batchGet(keys: string[]): Promise<Map<string, StorageObject>> {
|
|
85
|
-
const results = new Map<string, StorageObject>();
|
|
86
|
-
for (const key of keys) {
|
|
87
|
-
const entry = this.storage.get(key);
|
|
88
|
-
if (entry) {
|
|
89
|
-
results.set(key, {
|
|
90
|
-
data: JSON.parse(JSON.stringify(entry.data)),
|
|
91
|
-
etag: entry.etag,
|
|
92
|
-
lastModified: entry.lastModified,
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
return results;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* Clear all data (for testing)
|
|
101
|
-
*/
|
|
102
|
-
clear(): void {
|
|
103
|
-
this.storage.clear();
|
|
104
|
-
}
|
|
105
|
-
}
|
|
@@ -1,164 +0,0 @@
|
|
|
1
|
-
import OSS from 'ali-oss';
|
|
2
|
-
import type {
|
|
3
|
-
StorageAdapter,
|
|
4
|
-
StorageObject,
|
|
5
|
-
PutOptions,
|
|
6
|
-
StorageAdapterConfig,
|
|
7
|
-
} from '../types/index.js';
|
|
8
|
-
import { StorageError } from '../types/index.js';
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Alibaba Cloud OSS storage adapter with retry logic and timeout configuration
|
|
12
|
-
*/
|
|
13
|
-
export class OSSAdapter implements StorageAdapter {
|
|
14
|
-
private client: OSS;
|
|
15
|
-
private bucket: string;
|
|
16
|
-
|
|
17
|
-
constructor(config: StorageAdapterConfig) {
|
|
18
|
-
this.bucket = config.bucket;
|
|
19
|
-
this.client = new OSS({
|
|
20
|
-
region: config.region,
|
|
21
|
-
accessKeyId: config.credentials.accessKeyId,
|
|
22
|
-
accessKeySecret: config.credentials.secretAccessKey,
|
|
23
|
-
bucket: config.bucket,
|
|
24
|
-
endpoint: config.endpoint,
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Check if an error is retryable (temporary network issue or rate limit)
|
|
30
|
-
*/
|
|
31
|
-
private isRetryableError(error: any): boolean {
|
|
32
|
-
// OSS error codes for retryable errors
|
|
33
|
-
const retryableCodes = ['RequestTimeout', 'ServiceUnavailable', 'Throttling', 'RequestTimeTooSkewed'];
|
|
34
|
-
const retryableStatusCodes = [429, 500, 502, 503, 504];
|
|
35
|
-
|
|
36
|
-
return (
|
|
37
|
-
retryableCodes.includes(error.code) ||
|
|
38
|
-
retryableStatusCodes.includes(error.status) ||
|
|
39
|
-
error.code === 'ETIMEDOUT' ||
|
|
40
|
-
error.code === 'ECONNRESET'
|
|
41
|
-
);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Retry wrapper with exponential backoff
|
|
46
|
-
*/
|
|
47
|
-
private async withRetry<T>(fn: () => Promise<T>): Promise<T> {
|
|
48
|
-
const maxRetries = 3;
|
|
49
|
-
for (let attempt = 0; attempt < maxRetries; attempt++) {
|
|
50
|
-
try {
|
|
51
|
-
return await fn();
|
|
52
|
-
} catch (error: any) {
|
|
53
|
-
if (this.isRetryableError(error) && attempt < maxRetries - 1) {
|
|
54
|
-
// Exponential backoff with jitter: 100ms * 2^attempt + random(0-50ms)
|
|
55
|
-
const baseDelay = 100 * Math.pow(2, attempt);
|
|
56
|
-
const jitter = Math.random() * 50;
|
|
57
|
-
await new Promise(resolve => setTimeout(resolve, baseDelay + jitter));
|
|
58
|
-
continue;
|
|
59
|
-
}
|
|
60
|
-
throw error;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
throw new Error('Unreachable');
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
async get(key: string): Promise<StorageObject> {
|
|
67
|
-
return this.withRetry(async () => {
|
|
68
|
-
try {
|
|
69
|
-
const result = await this.client.get(key);
|
|
70
|
-
|
|
71
|
-
return {
|
|
72
|
-
data: JSON.parse(result.content.toString()),
|
|
73
|
-
etag: result.res.headers.etag?.replace(/"/g, '') || '',
|
|
74
|
-
lastModified: new Date(result.res.headers['last-modified']),
|
|
75
|
-
};
|
|
76
|
-
} catch (error: any) {
|
|
77
|
-
if (error.code === 'NoSuchKey') {
|
|
78
|
-
throw new StorageError(`Object not found: ${key}`);
|
|
79
|
-
}
|
|
80
|
-
throw new StorageError(`Failed to get object: ${error.message}`, error);
|
|
81
|
-
}
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
async put(key: string, data: any, options?: PutOptions): Promise<{ etag: string }> {
|
|
86
|
-
return this.withRetry(async () => {
|
|
87
|
-
try {
|
|
88
|
-
const headers: Record<string, string> = {};
|
|
89
|
-
|
|
90
|
-
if (options?.ifMatch) {
|
|
91
|
-
headers['If-Match'] = options.ifMatch;
|
|
92
|
-
}
|
|
93
|
-
if (options?.ifNoneMatch) {
|
|
94
|
-
headers['If-None-Match'] = options.ifNoneMatch;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
const result = await this.client.put(key, Buffer.from(JSON.stringify(data)), {
|
|
98
|
-
headers,
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
return {
|
|
102
|
-
etag: result.res.headers.etag?.replace(/"/g, '') || '',
|
|
103
|
-
};
|
|
104
|
-
} catch (error: any) {
|
|
105
|
-
if (error.code === 'PreconditionFailed') {
|
|
106
|
-
throw new StorageError('PreconditionFailed: ETag mismatch');
|
|
107
|
-
}
|
|
108
|
-
throw new StorageError(`Failed to put object: ${error.message}`, error);
|
|
109
|
-
}
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
async delete(key: string): Promise<void> {
|
|
114
|
-
try {
|
|
115
|
-
await this.client.delete(key);
|
|
116
|
-
} catch (error: any) {
|
|
117
|
-
throw new StorageError(`Failed to delete object: ${error.message}`, error);
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
async exists(key: string): Promise<boolean> {
|
|
122
|
-
try {
|
|
123
|
-
await this.client.head(key);
|
|
124
|
-
return true;
|
|
125
|
-
} catch (error: any) {
|
|
126
|
-
if (error.code === 'NoSuchKey') {
|
|
127
|
-
return false;
|
|
128
|
-
}
|
|
129
|
-
throw new StorageError(`Failed to check existence: ${error.message}`, error);
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
async listKeys(prefix: string): Promise<string[]> {
|
|
134
|
-
try {
|
|
135
|
-
const result = await this.client.list({ prefix });
|
|
136
|
-
return (result.objects || []).map(obj => obj.name);
|
|
137
|
-
} catch (error: any) {
|
|
138
|
-
throw new StorageError(`Failed to list keys: ${error.message}`, error);
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
async batchGet(keys: string[]): Promise<Map<string, StorageObject>> {
|
|
143
|
-
const results = new Map<string, StorageObject>();
|
|
144
|
-
|
|
145
|
-
const promises = keys.map(async (key) => {
|
|
146
|
-
try {
|
|
147
|
-
const obj = await this.get(key);
|
|
148
|
-
return { key, obj };
|
|
149
|
-
} catch (error) {
|
|
150
|
-
return null;
|
|
151
|
-
}
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
const settled = await Promise.all(promises);
|
|
155
|
-
|
|
156
|
-
for (const result of settled) {
|
|
157
|
-
if (result) {
|
|
158
|
-
results.set(result.key, result.obj);
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
return results;
|
|
163
|
-
}
|
|
164
|
-
}
|