@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
|
@@ -1,200 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
S3Client,
|
|
3
|
-
GetObjectCommand,
|
|
4
|
-
PutObjectCommand,
|
|
5
|
-
DeleteObjectCommand,
|
|
6
|
-
HeadObjectCommand,
|
|
7
|
-
ListObjectsV2Command,
|
|
8
|
-
} from '@aws-sdk/client-s3';
|
|
9
|
-
import type {
|
|
10
|
-
StorageAdapter,
|
|
11
|
-
StorageObject,
|
|
12
|
-
PutOptions,
|
|
13
|
-
StorageAdapterConfig,
|
|
14
|
-
} from '../types/index.js';
|
|
15
|
-
import { StorageError } from '../types/index.js';
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* S3 storage adapter with retry logic and timeout configuration
|
|
19
|
-
*/
|
|
20
|
-
export class S3Adapter implements StorageAdapter {
|
|
21
|
-
private client: S3Client;
|
|
22
|
-
private bucket: string;
|
|
23
|
-
|
|
24
|
-
constructor(config: StorageAdapterConfig) {
|
|
25
|
-
this.bucket = config.bucket;
|
|
26
|
-
this.client = new S3Client({
|
|
27
|
-
region: config.region,
|
|
28
|
-
credentials: config.credentials,
|
|
29
|
-
endpoint: config.endpoint,
|
|
30
|
-
maxAttempts: 1, // Disable SDK's built-in retry, use our custom retry logic
|
|
31
|
-
requestHandler: {
|
|
32
|
-
connectionTimeout: 3000,
|
|
33
|
-
requestTimeout: 3000,
|
|
34
|
-
},
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Check if an error is retryable (temporary network issue or rate limit)
|
|
40
|
-
*/
|
|
41
|
-
private isRetryableError(error: any): boolean {
|
|
42
|
-
// AWS SDK error codes for retryable errors
|
|
43
|
-
const retryableCodes = ['RequestTimeout', 'ServiceUnavailable', 'ThrottlingException', 'RequestThrottled'];
|
|
44
|
-
const retryableStatusCodes = [429, 500, 502, 503, 504];
|
|
45
|
-
|
|
46
|
-
return (
|
|
47
|
-
retryableCodes.includes(error.name) ||
|
|
48
|
-
retryableStatusCodes.includes(error.$metadata?.httpStatusCode) ||
|
|
49
|
-
error.code === 'ETIMEDOUT' ||
|
|
50
|
-
error.code === 'ECONNRESET'
|
|
51
|
-
);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Retry wrapper with exponential backoff
|
|
56
|
-
*/
|
|
57
|
-
private async withRetry<T>(fn: () => Promise<T>): Promise<T> {
|
|
58
|
-
const maxRetries = 3;
|
|
59
|
-
for (let attempt = 0; attempt < maxRetries; attempt++) {
|
|
60
|
-
try {
|
|
61
|
-
return await fn();
|
|
62
|
-
} catch (error: any) {
|
|
63
|
-
if (this.isRetryableError(error) && attempt < maxRetries - 1) {
|
|
64
|
-
// Exponential backoff with jitter: 100ms * 2^attempt + random(0-50ms)
|
|
65
|
-
const baseDelay = 100 * Math.pow(2, attempt);
|
|
66
|
-
const jitter = Math.random() * 50;
|
|
67
|
-
await new Promise(resolve => setTimeout(resolve, baseDelay + jitter));
|
|
68
|
-
continue;
|
|
69
|
-
}
|
|
70
|
-
throw error;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
throw new Error('Unreachable');
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
async get(key: string): Promise<StorageObject> {
|
|
77
|
-
return this.withRetry(async () => {
|
|
78
|
-
try {
|
|
79
|
-
const command = new GetObjectCommand({
|
|
80
|
-
Bucket: this.bucket,
|
|
81
|
-
Key: key,
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
const response = await this.client.send(command);
|
|
85
|
-
const body = await response.Body?.transformToString();
|
|
86
|
-
|
|
87
|
-
if (!body) {
|
|
88
|
-
throw new StorageError(`Empty response for key: ${key}`);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
return {
|
|
92
|
-
data: JSON.parse(body),
|
|
93
|
-
etag: response.ETag?.replace(/"/g, '') || '',
|
|
94
|
-
lastModified: response.LastModified || new Date(),
|
|
95
|
-
};
|
|
96
|
-
} catch (error: any) {
|
|
97
|
-
if (error.name === 'NoSuchKey') {
|
|
98
|
-
throw new StorageError(`Object not found: ${key}`);
|
|
99
|
-
}
|
|
100
|
-
throw new StorageError(`Failed to get object: ${error.message}`, error);
|
|
101
|
-
}
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
async put(key: string, data: any, options?: PutOptions): Promise<{ etag: string }> {
|
|
106
|
-
return this.withRetry(async () => {
|
|
107
|
-
try {
|
|
108
|
-
const command = new PutObjectCommand({
|
|
109
|
-
Bucket: this.bucket,
|
|
110
|
-
Key: key,
|
|
111
|
-
Body: JSON.stringify(data),
|
|
112
|
-
ContentType: 'application/json',
|
|
113
|
-
IfMatch: options?.ifMatch,
|
|
114
|
-
IfNoneMatch: options?.ifNoneMatch,
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
const response = await this.client.send(command);
|
|
118
|
-
|
|
119
|
-
return {
|
|
120
|
-
etag: response.ETag?.replace(/"/g, '') || '',
|
|
121
|
-
};
|
|
122
|
-
} catch (error: any) {
|
|
123
|
-
if (error.name === 'PreconditionFailed') {
|
|
124
|
-
throw new StorageError('PreconditionFailed: ETag mismatch');
|
|
125
|
-
}
|
|
126
|
-
throw new StorageError(`Failed to put object: ${error.message}`, error);
|
|
127
|
-
}
|
|
128
|
-
});
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
async delete(key: string): Promise<void> {
|
|
132
|
-
try {
|
|
133
|
-
const command = new DeleteObjectCommand({
|
|
134
|
-
Bucket: this.bucket,
|
|
135
|
-
Key: key,
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
await this.client.send(command);
|
|
139
|
-
} catch (error: any) {
|
|
140
|
-
throw new StorageError(`Failed to delete object: ${error.message}`, error);
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
async exists(key: string): Promise<boolean> {
|
|
145
|
-
try {
|
|
146
|
-
const command = new HeadObjectCommand({
|
|
147
|
-
Bucket: this.bucket,
|
|
148
|
-
Key: key,
|
|
149
|
-
});
|
|
150
|
-
|
|
151
|
-
await this.client.send(command);
|
|
152
|
-
return true;
|
|
153
|
-
} catch (error: any) {
|
|
154
|
-
if (error.name === 'NotFound' || error.name === 'NoSuchKey') {
|
|
155
|
-
return false;
|
|
156
|
-
}
|
|
157
|
-
throw new StorageError(`Failed to check existence: ${error.message}`, error);
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
async listKeys(prefix: string): Promise<string[]> {
|
|
162
|
-
return this.withRetry(async () => {
|
|
163
|
-
try {
|
|
164
|
-
const command = new ListObjectsV2Command({
|
|
165
|
-
Bucket: this.bucket,
|
|
166
|
-
Prefix: prefix,
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
const response = await this.client.send(command);
|
|
170
|
-
return (response.Contents || []).map(obj => obj.Key!).filter(Boolean);
|
|
171
|
-
} catch (error: any) {
|
|
172
|
-
throw new StorageError(`Failed to list keys: ${error.message}`, error);
|
|
173
|
-
}
|
|
174
|
-
});
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
async batchGet(keys: string[]): Promise<Map<string, StorageObject>> {
|
|
178
|
-
const results = new Map<string, StorageObject>();
|
|
179
|
-
|
|
180
|
-
// Fetch in parallel
|
|
181
|
-
const promises = keys.map(async (key) => {
|
|
182
|
-
try {
|
|
183
|
-
const obj = await this.get(key);
|
|
184
|
-
return { key, obj };
|
|
185
|
-
} catch (error) {
|
|
186
|
-
return null;
|
|
187
|
-
}
|
|
188
|
-
});
|
|
189
|
-
|
|
190
|
-
const settled = await Promise.all(promises);
|
|
191
|
-
|
|
192
|
-
for (const result of settled) {
|
|
193
|
-
if (result) {
|
|
194
|
-
results.set(result.key, result.obj);
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
return results;
|
|
199
|
-
}
|
|
200
|
-
}
|
package/src/types/collection.ts
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import type { Document, InsertDocument, UpdateDocument } from './document.js';
|
|
2
|
-
import type { QueryFilter, QueryOptions, UpdateOptions } from './query.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Collection interface for type-safe CRUD operations
|
|
6
|
-
*/
|
|
7
|
-
export interface ICollection<T extends Document> {
|
|
8
|
-
insert(data: InsertDocument<T>): Promise<T>;
|
|
9
|
-
findById(id: string): Promise<T | null>;
|
|
10
|
-
find(filter: QueryFilter<T>, options?: QueryOptions): Promise<T[]>;
|
|
11
|
-
update(id: string, data: UpdateDocument<T>, options?: UpdateOptions): Promise<T>;
|
|
12
|
-
delete(id: string): Promise<void>;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Collection metadata
|
|
17
|
-
*/
|
|
18
|
-
export interface CollectionMeta {
|
|
19
|
-
name: string;
|
|
20
|
-
version: number;
|
|
21
|
-
indexShardCount: number;
|
|
22
|
-
indexedFields: string[];
|
|
23
|
-
createdAt: string;
|
|
24
|
-
documentCount: number;
|
|
25
|
-
lastUpdated: string;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Index shard structure
|
|
30
|
-
*/
|
|
31
|
-
export interface IndexShard {
|
|
32
|
-
shardId: string;
|
|
33
|
-
documents: Record<string, any>;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Shard metadata for performance optimization
|
|
38
|
-
*/
|
|
39
|
-
export interface ShardMetadata {
|
|
40
|
-
shardId: string;
|
|
41
|
-
docCount: number;
|
|
42
|
-
lastUpdated: string;
|
|
43
|
-
}
|
package/src/types/document.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Base document type with system fields
|
|
3
|
-
*/
|
|
4
|
-
export interface Document {
|
|
5
|
-
id: string;
|
|
6
|
-
_etag?: string;
|
|
7
|
-
_createdAt?: string;
|
|
8
|
-
_updatedAt?: string;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Type for inserting new documents (omits system fields)
|
|
13
|
-
*/
|
|
14
|
-
export type InsertDocument<T extends Document> = Omit<
|
|
15
|
-
T,
|
|
16
|
-
'id' | '_etag' | '_createdAt' | '_updatedAt'
|
|
17
|
-
>;
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Type for updating documents (partial update without id)
|
|
21
|
-
*/
|
|
22
|
-
export type UpdateDocument<T extends Document> = Partial<Omit<T, 'id'>>;
|
package/src/types/errors.ts
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Base error class for BucketDB
|
|
3
|
-
*/
|
|
4
|
-
export class BucketDBError extends Error {
|
|
5
|
-
constructor(message: string) {
|
|
6
|
-
super(message);
|
|
7
|
-
this.name = 'BucketDBError';
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Document not found error
|
|
13
|
-
*/
|
|
14
|
-
export class DocumentNotFoundError extends BucketDBError {
|
|
15
|
-
constructor(id: string) {
|
|
16
|
-
super(`Document not found: ${id}`);
|
|
17
|
-
this.name = 'DocumentNotFoundError';
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Concurrent update conflict error
|
|
23
|
-
*/
|
|
24
|
-
export class ConcurrentUpdateError extends BucketDBError {
|
|
25
|
-
constructor(message: string = 'Concurrent update conflict, please retry') {
|
|
26
|
-
super(message);
|
|
27
|
-
this.name = 'ConcurrentUpdateError';
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Validation error
|
|
33
|
-
*/
|
|
34
|
-
export class ValidationError extends BucketDBError {
|
|
35
|
-
constructor(message: string) {
|
|
36
|
-
super(message);
|
|
37
|
-
this.name = 'ValidationError';
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Storage error
|
|
43
|
-
*/
|
|
44
|
-
export class StorageError extends BucketDBError {
|
|
45
|
-
constructor(message: string, public readonly cause?: Error) {
|
|
46
|
-
super(message);
|
|
47
|
-
this.name = 'StorageError';
|
|
48
|
-
}
|
|
49
|
-
}
|
package/src/types/index.ts
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
// Document types
|
|
2
|
-
export type { Document, InsertDocument, UpdateDocument } from './document.js';
|
|
3
|
-
|
|
4
|
-
// Query types
|
|
5
|
-
export type {
|
|
6
|
-
QueryOperator,
|
|
7
|
-
QueryValue,
|
|
8
|
-
QueryFilter,
|
|
9
|
-
QueryOptions,
|
|
10
|
-
UpdateOptions,
|
|
11
|
-
} from './query.js';
|
|
12
|
-
|
|
13
|
-
// Storage types
|
|
14
|
-
export type {
|
|
15
|
-
StorageObject,
|
|
16
|
-
PutOptions,
|
|
17
|
-
StorageAdapter,
|
|
18
|
-
StorageCredentials,
|
|
19
|
-
StorageAdapterConfig,
|
|
20
|
-
} from './storage.js';
|
|
21
|
-
|
|
22
|
-
// Collection types
|
|
23
|
-
export type {
|
|
24
|
-
ICollection,
|
|
25
|
-
CollectionMeta,
|
|
26
|
-
IndexShard,
|
|
27
|
-
ShardMetadata,
|
|
28
|
-
} from './collection.js';
|
|
29
|
-
|
|
30
|
-
// Error classes
|
|
31
|
-
export {
|
|
32
|
-
BucketDBError,
|
|
33
|
-
DocumentNotFoundError,
|
|
34
|
-
ConcurrentUpdateError,
|
|
35
|
-
ValidationError,
|
|
36
|
-
StorageError,
|
|
37
|
-
} from './errors.js';
|
package/src/types/query.ts
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Supported query operators
|
|
3
|
-
*/
|
|
4
|
-
export type QueryOperator =
|
|
5
|
-
| '$eq'
|
|
6
|
-
| '$ne'
|
|
7
|
-
| '$gt'
|
|
8
|
-
| '$gte'
|
|
9
|
-
| '$lt'
|
|
10
|
-
| '$lte'
|
|
11
|
-
| '$in'
|
|
12
|
-
| '$nin';
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Query value can be direct value or operator object
|
|
16
|
-
*/
|
|
17
|
-
export type QueryValue<T> =
|
|
18
|
-
| T
|
|
19
|
-
| {
|
|
20
|
-
$eq?: T;
|
|
21
|
-
$ne?: T;
|
|
22
|
-
$gt?: T;
|
|
23
|
-
$gte?: T;
|
|
24
|
-
$lt?: T;
|
|
25
|
-
$lte?: T;
|
|
26
|
-
$in?: T[];
|
|
27
|
-
$nin?: T[];
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Query filter for type-safe queries
|
|
32
|
-
*/
|
|
33
|
-
export type QueryFilter<T> = {
|
|
34
|
-
[K in keyof T]?: QueryValue<T[K]>;
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Query options for pagination and sorting
|
|
39
|
-
*/
|
|
40
|
-
export interface QueryOptions {
|
|
41
|
-
limit?: number;
|
|
42
|
-
offset?: number;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Update options with optimistic locking
|
|
47
|
-
*/
|
|
48
|
-
export interface UpdateOptions {
|
|
49
|
-
etag?: string;
|
|
50
|
-
}
|
package/src/types/storage.ts
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Storage object returned by adapter
|
|
3
|
-
*/
|
|
4
|
-
export interface StorageObject {
|
|
5
|
-
data: any;
|
|
6
|
-
etag: string;
|
|
7
|
-
lastModified: Date;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Put options for conditional writes
|
|
12
|
-
*/
|
|
13
|
-
export interface PutOptions {
|
|
14
|
-
ifMatch?: string;
|
|
15
|
-
ifNoneMatch?: string;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Storage adapter interface (abstraction for S3/OSS)
|
|
20
|
-
*/
|
|
21
|
-
export interface StorageAdapter {
|
|
22
|
-
get(key: string): Promise<StorageObject>;
|
|
23
|
-
put(key: string, data: any, options?: PutOptions): Promise<{ etag: string }>;
|
|
24
|
-
delete(key: string): Promise<void>;
|
|
25
|
-
exists(key: string): Promise<boolean>;
|
|
26
|
-
listKeys(prefix: string): Promise<string[]>;
|
|
27
|
-
batchGet(keys: string[]): Promise<Map<string, StorageObject>>;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Storage credentials
|
|
32
|
-
*/
|
|
33
|
-
export interface StorageCredentials {
|
|
34
|
-
accessKeyId: string;
|
|
35
|
-
secretAccessKey: string;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Storage adapter configuration
|
|
40
|
-
*/
|
|
41
|
-
export interface StorageAdapterConfig {
|
|
42
|
-
bucket: string;
|
|
43
|
-
region: string;
|
|
44
|
-
credentials: StorageCredentials;
|
|
45
|
-
endpoint?: string;
|
|
46
|
-
}
|
package/src/utils/hash.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Simple string hash function (DJB2 algorithm)
|
|
3
|
-
*/
|
|
4
|
-
export function hashString(str: string): number {
|
|
5
|
-
let hash = 5381;
|
|
6
|
-
for (let i = 0; i < str.length; i++) {
|
|
7
|
-
hash = (hash * 33) ^ str.charCodeAt(i);
|
|
8
|
-
}
|
|
9
|
-
return hash >>> 0; // Convert to unsigned 32-bit integer
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Get shard ID for a document ID
|
|
14
|
-
*/
|
|
15
|
-
export function getShardId(docId: string, shardCount: number): number {
|
|
16
|
-
return hashString(docId) % shardCount;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Format shard ID as zero-padded string (e.g., "00", "01", "15")
|
|
21
|
-
*/
|
|
22
|
-
export function formatShardId(shardId: number): string {
|
|
23
|
-
return shardId.toString().padStart(2, '0');
|
|
24
|
-
}
|
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
import { join, resolve, relative } from 'path';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* ValidationError for path validation failures
|
|
5
|
-
*/
|
|
6
|
-
export class PathValidationError extends Error {
|
|
7
|
-
constructor(message: string) {
|
|
8
|
-
super(message);
|
|
9
|
-
this.name = 'PathValidationError';
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Sanitize and validate path components to prevent directory traversal attacks
|
|
15
|
-
* This is for SINGLE path components (like collection names, document IDs)
|
|
16
|
-
*
|
|
17
|
-
* @param input - Path component to sanitize
|
|
18
|
-
* @returns Sanitized path component
|
|
19
|
-
* @throws PathValidationError if validation fails
|
|
20
|
-
*/
|
|
21
|
-
export function sanitizePathComponent(input: string): string {
|
|
22
|
-
if (!input || typeof input !== 'string') {
|
|
23
|
-
throw new PathValidationError('Path component must be a non-empty string');
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
// Remove any path traversal sequences
|
|
27
|
-
let sanitized = input.replace(/\.\./g, '');
|
|
28
|
-
|
|
29
|
-
// Remove directory separators (single components cannot have slashes)
|
|
30
|
-
sanitized = sanitized.replace(/[\/\\]/g, '_');
|
|
31
|
-
|
|
32
|
-
// Only allow alphanumeric, underscore, hyphen, and dot
|
|
33
|
-
if (!/^[a-zA-Z0-9_.-]+$/.test(sanitized)) {
|
|
34
|
-
throw new PathValidationError(
|
|
35
|
-
`Invalid characters in path component. Only alphanumeric, underscore, hyphen, and dot are allowed.`
|
|
36
|
-
);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
// Prevent excessively long names
|
|
40
|
-
if (sanitized.length > 255) {
|
|
41
|
-
throw new PathValidationError('Path component exceeds maximum length of 255 characters');
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
// Prevent hidden files (starting with .)
|
|
45
|
-
if (sanitized.startsWith('.')) {
|
|
46
|
-
throw new PathValidationError('Path component cannot start with a dot');
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
return sanitized;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Validate that a target path is within the allowed base path
|
|
54
|
-
* Prevents directory traversal attacks by ensuring the resolved path
|
|
55
|
-
* doesn't escape the base directory
|
|
56
|
-
*
|
|
57
|
-
* @param basePath - Base directory path
|
|
58
|
-
* @param targetPath - Target path to validate
|
|
59
|
-
* @throws PathValidationError if path escapes base directory
|
|
60
|
-
*/
|
|
61
|
-
export function validatePathWithinBase(basePath: string, targetPath: string): void {
|
|
62
|
-
const normalizedBase = resolve(basePath);
|
|
63
|
-
const normalizedTarget = resolve(targetPath);
|
|
64
|
-
|
|
65
|
-
const relativePath = relative(normalizedBase, normalizedTarget);
|
|
66
|
-
|
|
67
|
-
// If relative path starts with '..', it's outside the base directory
|
|
68
|
-
if (relativePath.startsWith('..') || resolve(normalizedBase, relativePath) !== normalizedTarget) {
|
|
69
|
-
throw new PathValidationError('Path traversal detected: target path is outside base directory');
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Safely join path components with validation
|
|
75
|
-
* This is for joining SINGLE components (not full paths with slashes)
|
|
76
|
-
*
|
|
77
|
-
* @param basePath - Base directory path
|
|
78
|
-
* @param components - Path components to join
|
|
79
|
-
* @returns Validated full path
|
|
80
|
-
* @throws PathValidationError if validation fails
|
|
81
|
-
*/
|
|
82
|
-
export function safePathJoin(basePath: string, ...components: string[]): string {
|
|
83
|
-
// Sanitize each component
|
|
84
|
-
const sanitizedComponents = components.map(sanitizePathComponent);
|
|
85
|
-
|
|
86
|
-
// Join paths
|
|
87
|
-
const fullPath = join(basePath, ...sanitizedComponents);
|
|
88
|
-
|
|
89
|
-
// Validate the result doesn't escape base path
|
|
90
|
-
validatePathWithinBase(basePath, fullPath);
|
|
91
|
-
|
|
92
|
-
return fullPath;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Safely join a key (which may contain slashes) with base path
|
|
97
|
-
* Validates the result doesn't escape base directory
|
|
98
|
-
*
|
|
99
|
-
* @param basePath - Base directory path
|
|
100
|
-
* @param key - Storage key (may contain path separators)
|
|
101
|
-
* @returns Validated full path
|
|
102
|
-
* @throws PathValidationError if validation fails
|
|
103
|
-
*/
|
|
104
|
-
export function safeKeyPath(basePath: string, key: string): string {
|
|
105
|
-
if (!key || typeof key !== 'string') {
|
|
106
|
-
throw new PathValidationError('Key must be a non-empty string');
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
// Check for path traversal patterns
|
|
110
|
-
if (key.includes('..')) {
|
|
111
|
-
throw new PathValidationError('Key contains path traversal sequence');
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// Join and validate
|
|
115
|
-
const fullPath = join(basePath, key);
|
|
116
|
-
validatePathWithinBase(basePath, fullPath);
|
|
117
|
-
|
|
118
|
-
return fullPath;
|
|
119
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"bucket-db.test.d.ts","sourceRoot":"","sources":["bucket-db.test.ts"],"names":[],"mappings":""}
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { describe, test, expect } from 'bun:test';
|
|
2
|
-
import { BucketDB } from '../../src/core/bucket-db';
|
|
3
|
-
import { MemoryStorageAdapter } from '../../src/storage/memory-adapter';
|
|
4
|
-
describe('BucketDB', () => {
|
|
5
|
-
test('creates database instance with memory adapter', () => {
|
|
6
|
-
const adapter = new MemoryStorageAdapter();
|
|
7
|
-
const db = new BucketDB(adapter, 'test-db');
|
|
8
|
-
expect(db).toBeDefined();
|
|
9
|
-
});
|
|
10
|
-
test('creates collection', () => {
|
|
11
|
-
const adapter = new MemoryStorageAdapter();
|
|
12
|
-
const db = new BucketDB(adapter, 'test-db');
|
|
13
|
-
const users = db.collection('users');
|
|
14
|
-
expect(users).toBeDefined();
|
|
15
|
-
});
|
|
16
|
-
test('same collection name returns same instance', () => {
|
|
17
|
-
const adapter = new MemoryStorageAdapter();
|
|
18
|
-
const db = new BucketDB(adapter, 'test-db');
|
|
19
|
-
const users1 = db.collection('users');
|
|
20
|
-
const users2 = db.collection('users');
|
|
21
|
-
expect(users1).toBe(users2);
|
|
22
|
-
});
|
|
23
|
-
test('end-to-end: insert and query across collections', async () => {
|
|
24
|
-
const adapter = new MemoryStorageAdapter();
|
|
25
|
-
const db = new BucketDB(adapter, 'test-db');
|
|
26
|
-
const users = db.collection('users');
|
|
27
|
-
await users.insert({ name: 'Alice', email: 'alice@example.com' });
|
|
28
|
-
await users.insert({ name: 'Bob', email: 'bob@example.com' });
|
|
29
|
-
const results = await users.find({ name: 'Alice' });
|
|
30
|
-
expect(results).toHaveLength(1);
|
|
31
|
-
expect(results[0].email).toBe('alice@example.com');
|
|
32
|
-
});
|
|
33
|
-
});
|
|
34
|
-
//# sourceMappingURL=bucket-db.test.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"bucket-db.test.js","sourceRoot":"","sources":["bucket-db.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAClD,OAAO,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AACpD,OAAO,EAAE,oBAAoB,EAAE,MAAM,kCAAkC,CAAC;AAQxE,QAAQ,CAAC,UAAU,EAAE,GAAG,EAAE;IACxB,IAAI,CAAC,+CAA+C,EAAE,GAAG,EAAE;QACzD,MAAM,OAAO,GAAG,IAAI,oBAAoB,EAAE,CAAC;QAC3C,MAAM,EAAE,GAAG,IAAI,QAAQ,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;QAE5C,MAAM,CAAC,EAAE,CAAC,CAAC,WAAW,EAAE,CAAC;IAC3B,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,oBAAoB,EAAE,GAAG,EAAE;QAC9B,MAAM,OAAO,GAAG,IAAI,oBAAoB,EAAE,CAAC;QAC3C,MAAM,EAAE,GAAG,IAAI,QAAQ,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;QAE5C,MAAM,KAAK,GAAG,EAAE,CAAC,UAAU,CAAO,OAAO,CAAC,CAAC;QAC3C,MAAM,CAAC,KAAK,CAAC,CAAC,WAAW,EAAE,CAAC;IAC9B,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,4CAA4C,EAAE,GAAG,EAAE;QACtD,MAAM,OAAO,GAAG,IAAI,oBAAoB,EAAE,CAAC;QAC3C,MAAM,EAAE,GAAG,IAAI,QAAQ,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;QAE5C,MAAM,MAAM,GAAG,EAAE,CAAC,UAAU,CAAO,OAAO,CAAC,CAAC;QAC5C,MAAM,MAAM,GAAG,EAAE,CAAC,UAAU,CAAO,OAAO,CAAC,CAAC;QAE5C,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IAC9B,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,iDAAiD,EAAE,KAAK,IAAI,EAAE;QACjE,MAAM,OAAO,GAAG,IAAI,oBAAoB,EAAE,CAAC;QAC3C,MAAM,EAAE,GAAG,IAAI,QAAQ,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;QAE5C,MAAM,KAAK,GAAG,EAAE,CAAC,UAAU,CAAO,OAAO,CAAC,CAAC;QAE3C,MAAM,KAAK,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,mBAAmB,EAAE,CAAC,CAAC;QAClE,MAAM,KAAK,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,iBAAiB,EAAE,CAAC,CAAC;QAE9D,MAAM,OAAO,GAAG,MAAM,KAAK,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC;QACpD,MAAM,CAAC,OAAO,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC;QAChC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,mBAAmB,CAAC,CAAC;IACrD,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import { describe, test, expect } from 'bun:test';
|
|
2
|
-
import { BucketDB } from '../../src/core/bucket-db';
|
|
3
|
-
import { MemoryStorageAdapter } from '../../src/storage/memory-adapter';
|
|
4
|
-
import type { Document } from '../../src/index.js';
|
|
5
|
-
|
|
6
|
-
interface User extends Document {
|
|
7
|
-
name: string;
|
|
8
|
-
email: string;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
describe('BucketDB', () => {
|
|
12
|
-
test('creates database instance with memory adapter', () => {
|
|
13
|
-
const adapter = new MemoryStorageAdapter();
|
|
14
|
-
const db = new BucketDB(adapter, 'test-db');
|
|
15
|
-
|
|
16
|
-
expect(db).toBeDefined();
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
test('creates collection', () => {
|
|
20
|
-
const adapter = new MemoryStorageAdapter();
|
|
21
|
-
const db = new BucketDB(adapter, 'test-db');
|
|
22
|
-
|
|
23
|
-
const users = db.collection<User>('users');
|
|
24
|
-
expect(users).toBeDefined();
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
test('same collection name returns same instance', () => {
|
|
28
|
-
const adapter = new MemoryStorageAdapter();
|
|
29
|
-
const db = new BucketDB(adapter, 'test-db');
|
|
30
|
-
|
|
31
|
-
const users1 = db.collection<User>('users');
|
|
32
|
-
const users2 = db.collection<User>('users');
|
|
33
|
-
|
|
34
|
-
expect(users1).toBe(users2);
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
test('end-to-end: insert and query across collections', async () => {
|
|
38
|
-
const adapter = new MemoryStorageAdapter();
|
|
39
|
-
const db = new BucketDB(adapter, 'test-db');
|
|
40
|
-
|
|
41
|
-
const users = db.collection<User>('users');
|
|
42
|
-
|
|
43
|
-
await users.insert({ name: 'Alice', email: 'alice@example.com' });
|
|
44
|
-
await users.insert({ name: 'Bob', email: 'bob@example.com' });
|
|
45
|
-
|
|
46
|
-
const results = await users.find({ name: 'Alice' });
|
|
47
|
-
expect(results).toHaveLength(1);
|
|
48
|
-
expect(results[0].email).toBe('alice@example.com');
|
|
49
|
-
});
|
|
50
|
-
});
|