@hold-baby/bucket-db 0.3.0 → 0.3.1
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/README.md +234 -19
- package/dist/cache/lru-cache.d.ts +47 -0
- package/dist/cache/lru-cache.d.ts.map +1 -0
- package/dist/cache/lru-cache.js +110 -0
- package/dist/cache/lru-cache.js.map +1 -0
- package/dist/core/bucket-db.d.ts +20 -0
- package/dist/core/bucket-db.d.ts.map +1 -0
- package/dist/core/bucket-db.js +32 -0
- package/dist/core/bucket-db.js.map +1 -0
- package/dist/core/collection.d.ts +26 -0
- package/dist/core/collection.d.ts.map +1 -0
- package/dist/core/collection.js +213 -0
- package/dist/core/collection.js.map +1 -0
- package/dist/core/wal.d.ts +45 -0
- package/dist/core/wal.d.ts.map +1 -0
- package/dist/core/wal.js +80 -0
- package/dist/core/wal.js.map +1 -0
- package/dist/index/shard-manager.d.ts +36 -0
- package/dist/index/shard-manager.d.ts.map +1 -0
- package/dist/index/shard-manager.js +177 -0
- package/dist/index/shard-manager.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -0
- package/dist/query/evaluator.d.ts +10 -0
- package/dist/query/evaluator.d.ts.map +1 -0
- package/dist/query/evaluator.js +127 -0
- package/dist/query/evaluator.js.map +1 -0
- package/dist/storage/fs-adapter.d.ts +24 -0
- package/dist/storage/fs-adapter.d.ts.map +1 -0
- package/dist/storage/fs-adapter.js +208 -0
- package/dist/storage/fs-adapter.js.map +1 -0
- package/dist/storage/memory-adapter.d.ts +22 -0
- package/dist/storage/memory-adapter.d.ts.map +1 -0
- package/dist/storage/memory-adapter.js +78 -0
- package/dist/storage/memory-adapter.js.map +1 -0
- package/dist/storage/oss-adapter.d.ts +26 -0
- package/dist/storage/oss-adapter.d.ts.map +1 -0
- package/dist/storage/oss-adapter.js +145 -0
- package/dist/storage/oss-adapter.js.map +1 -0
- package/dist/storage/s3-adapter.d.ts +26 -0
- package/dist/storage/s3-adapter.d.ts.map +1 -0
- package/dist/storage/s3-adapter.js +170 -0
- package/dist/storage/s3-adapter.js.map +1 -0
- package/dist/types/collection.d.ts +40 -0
- package/dist/types/collection.d.ts.map +1 -0
- package/dist/types/collection.js +2 -0
- package/dist/types/collection.js.map +1 -0
- package/{src/types/document.ts → dist/types/document.d.ts} +6 -10
- package/dist/types/document.d.ts.map +1 -0
- package/dist/types/document.js +2 -0
- package/dist/types/document.js.map +1 -0
- package/dist/types/errors.d.ts +32 -0
- package/dist/types/errors.d.ts.map +1 -0
- package/dist/types/errors.js +48 -0
- package/dist/types/errors.js.map +1 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +3 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/query.d.ts +37 -0
- package/dist/types/query.d.ts.map +1 -0
- package/dist/types/query.js +2 -0
- package/dist/types/query.js.map +1 -0
- package/dist/types/storage.d.ts +45 -0
- package/dist/types/storage.d.ts.map +1 -0
- package/dist/types/storage.js +2 -0
- package/dist/types/storage.js.map +1 -0
- package/dist/utils/hash.d.ts +13 -0
- package/dist/utils/hash.d.ts.map +1 -0
- package/dist/utils/hash.js +23 -0
- package/dist/utils/hash.js.map +1 -0
- package/dist/utils/path-validator.d.ts +46 -0
- package/dist/utils/path-validator.d.ts.map +1 -0
- package/dist/utils/path-validator.js +99 -0
- package/dist/utils/path-validator.js.map +1 -0
- package/llms.txt +46 -0
- package/package.json +14 -3
- 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/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/core/wal.ts
DELETED
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
import type { StorageAdapter } from '../types/index.js';
|
|
2
|
-
import { safePathJoin } from '../utils/path-validator.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* WAL operation types
|
|
6
|
-
*/
|
|
7
|
-
export type WALOperation = 'insert' | 'update' | 'delete';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* WAL entry structure
|
|
11
|
-
*/
|
|
12
|
-
export interface WALEntry {
|
|
13
|
-
operation: WALOperation;
|
|
14
|
-
collectionPath: string;
|
|
15
|
-
documentId: string;
|
|
16
|
-
documentData?: any;
|
|
17
|
-
timestamp: string;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Write-Ahead Log for ensuring index-document consistency
|
|
22
|
-
*/
|
|
23
|
-
export class WAL {
|
|
24
|
-
private walPath: string;
|
|
25
|
-
|
|
26
|
-
constructor(
|
|
27
|
-
private adapter: StorageAdapter,
|
|
28
|
-
basePath: string
|
|
29
|
-
) {
|
|
30
|
-
this.walPath = safePathJoin(basePath, 'wal');
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Get WAL entry path for a document
|
|
35
|
-
*/
|
|
36
|
-
private getWALEntryPath(documentId: string): string {
|
|
37
|
-
return `${this.walPath}/${documentId}.wal.json`;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Log an operation before execution
|
|
42
|
-
*/
|
|
43
|
-
async log(entry: WALEntry): Promise<void> {
|
|
44
|
-
const path = this.getWALEntryPath(entry.documentId);
|
|
45
|
-
await this.adapter.put(path, entry);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Clear WAL entry after successful operation
|
|
50
|
-
*/
|
|
51
|
-
async clear(documentId: string): Promise<void> {
|
|
52
|
-
const path = this.getWALEntryPath(documentId);
|
|
53
|
-
try {
|
|
54
|
-
await this.adapter.delete(path);
|
|
55
|
-
} catch {
|
|
56
|
-
// Ignore if entry doesn't exist
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* List all pending WAL entries
|
|
62
|
-
*/
|
|
63
|
-
async listPending(): Promise<WALEntry[]> {
|
|
64
|
-
try {
|
|
65
|
-
const keys = await this.adapter.listKeys(this.walPath);
|
|
66
|
-
const entries: WALEntry[] = [];
|
|
67
|
-
|
|
68
|
-
for (const key of keys) {
|
|
69
|
-
try {
|
|
70
|
-
const { data } = await this.adapter.get(key);
|
|
71
|
-
entries.push(data as WALEntry);
|
|
72
|
-
} catch {
|
|
73
|
-
// Skip corrupted entries
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
return entries;
|
|
78
|
-
} catch {
|
|
79
|
-
return [];
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Recover from pending WAL entries
|
|
85
|
-
* This should be called on collection initialization
|
|
86
|
-
*/
|
|
87
|
-
async recover(
|
|
88
|
-
onRecover: (entry: WALEntry) => Promise<void>
|
|
89
|
-
): Promise<number> {
|
|
90
|
-
const pending = await this.listPending();
|
|
91
|
-
let recovered = 0;
|
|
92
|
-
|
|
93
|
-
for (const entry of pending) {
|
|
94
|
-
try {
|
|
95
|
-
await onRecover(entry);
|
|
96
|
-
await this.clear(entry.documentId);
|
|
97
|
-
recovered++;
|
|
98
|
-
} catch (error) {
|
|
99
|
-
// Log error but continue recovery
|
|
100
|
-
console.error(`Failed to recover WAL entry for ${entry.documentId}:`, error);
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
return recovered;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
@@ -1,197 +0,0 @@
|
|
|
1
|
-
import type { StorageAdapter, IndexShard, QueryFilter, ShardMetadata } from '../types/index.js';
|
|
2
|
-
import { StorageError } from '../types/index.js';
|
|
3
|
-
import { getShardId, formatShardId } from '../utils/hash.js';
|
|
4
|
-
import { evaluateFilter } from '../query/evaluator.js';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Manages index shards for a collection
|
|
8
|
-
*/
|
|
9
|
-
export class ShardManager {
|
|
10
|
-
constructor(
|
|
11
|
-
private adapter: StorageAdapter,
|
|
12
|
-
private collectionPath: string,
|
|
13
|
-
private shardCount: number
|
|
14
|
-
) {}
|
|
15
|
-
|
|
16
|
-
private getShardPath(shardId: number): string {
|
|
17
|
-
return `${this.collectionPath}/index/shard-${formatShardId(shardId)}.json`;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
private getShardMetadataPath(shardId: number): string {
|
|
21
|
-
return `${this.collectionPath}/index/shard-${formatShardId(shardId)}.meta.json`;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
private async readShardMetadata(shardId: number): Promise<ShardMetadata | null> {
|
|
25
|
-
const path = this.getShardMetadataPath(shardId);
|
|
26
|
-
try {
|
|
27
|
-
const { data } = await this.adapter.get(path);
|
|
28
|
-
return data as ShardMetadata;
|
|
29
|
-
} catch (error) {
|
|
30
|
-
return null;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
private async readShard(shardId: number): Promise<{ shard: IndexShard; etag?: string }> {
|
|
35
|
-
const path = this.getShardPath(shardId);
|
|
36
|
-
try {
|
|
37
|
-
const { data, etag } = await this.adapter.get(path);
|
|
38
|
-
return { shard: data as IndexShard, etag };
|
|
39
|
-
} catch (error) {
|
|
40
|
-
// Return empty shard if not found
|
|
41
|
-
return {
|
|
42
|
-
shard: {
|
|
43
|
-
shardId: formatShardId(shardId),
|
|
44
|
-
documents: {},
|
|
45
|
-
},
|
|
46
|
-
etag: undefined,
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
private async writeShard(shardId: number, shard: IndexShard, etag?: string): Promise<void> {
|
|
52
|
-
const path = this.getShardPath(shardId);
|
|
53
|
-
const metaPath = this.getShardMetadataPath(shardId);
|
|
54
|
-
|
|
55
|
-
// Write shard data
|
|
56
|
-
await this.adapter.put(path, shard, etag ? { ifMatch: etag } : undefined);
|
|
57
|
-
|
|
58
|
-
// Write metadata
|
|
59
|
-
const metadata: ShardMetadata = {
|
|
60
|
-
shardId: formatShardId(shardId),
|
|
61
|
-
docCount: Object.keys(shard.documents).length,
|
|
62
|
-
lastUpdated: new Date().toISOString(),
|
|
63
|
-
};
|
|
64
|
-
await this.adapter.put(metaPath, metadata);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Add document to index
|
|
69
|
-
*/
|
|
70
|
-
async addDocument(doc: Record<string, any>): Promise<void> {
|
|
71
|
-
const docId = doc.id;
|
|
72
|
-
const shardId = getShardId(docId, this.shardCount);
|
|
73
|
-
|
|
74
|
-
// Retry logic for optimistic locking with exponential backoff
|
|
75
|
-
const maxRetries = 3;
|
|
76
|
-
for (let attempt = 0; attempt < maxRetries; attempt++) {
|
|
77
|
-
try {
|
|
78
|
-
// Read shard and etag in a single operation
|
|
79
|
-
const { shard, etag } = await this.readShard(shardId);
|
|
80
|
-
|
|
81
|
-
shard.documents[docId] = { ...doc };
|
|
82
|
-
|
|
83
|
-
await this.writeShard(shardId, shard, etag);
|
|
84
|
-
return;
|
|
85
|
-
} catch (error) {
|
|
86
|
-
if (error instanceof StorageError && error.message.includes('PreconditionFailed')) {
|
|
87
|
-
if (attempt < maxRetries - 1) {
|
|
88
|
-
// Exponential backoff with jitter: 100ms * 2^attempt + random(0-50ms)
|
|
89
|
-
const baseDelay = 100 * Math.pow(2, attempt);
|
|
90
|
-
const jitter = Math.random() * 50;
|
|
91
|
-
await new Promise(resolve => setTimeout(resolve, baseDelay + jitter));
|
|
92
|
-
continue;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
throw error;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
throw new StorageError('Failed to add document after retries');
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Update document in index
|
|
103
|
-
*/
|
|
104
|
-
async updateDocument(doc: Record<string, any>): Promise<void> {
|
|
105
|
-
await this.addDocument(doc); // Same as add - upsert behavior
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Remove document from index
|
|
110
|
-
*/
|
|
111
|
-
async removeDocument(docId: string): Promise<void> {
|
|
112
|
-
const shardId = getShardId(docId, this.shardCount);
|
|
113
|
-
|
|
114
|
-
const maxRetries = 3;
|
|
115
|
-
for (let attempt = 0; attempt < maxRetries; attempt++) {
|
|
116
|
-
try {
|
|
117
|
-
// Read shard and etag in a single operation
|
|
118
|
-
const { shard, etag } = await this.readShard(shardId);
|
|
119
|
-
|
|
120
|
-
delete shard.documents[docId];
|
|
121
|
-
|
|
122
|
-
await this.writeShard(shardId, shard, etag);
|
|
123
|
-
return;
|
|
124
|
-
} catch (error) {
|
|
125
|
-
if (error instanceof StorageError && error.message.includes('PreconditionFailed')) {
|
|
126
|
-
if (attempt < maxRetries - 1) {
|
|
127
|
-
// Exponential backoff with jitter
|
|
128
|
-
const baseDelay = 100 * Math.pow(2, attempt);
|
|
129
|
-
const jitter = Math.random() * 50;
|
|
130
|
-
await new Promise(resolve => setTimeout(resolve, baseDelay + jitter));
|
|
131
|
-
continue;
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
// Ignore if shard doesn't exist
|
|
135
|
-
if (error instanceof StorageError && error.message.includes('not found')) {
|
|
136
|
-
return;
|
|
137
|
-
}
|
|
138
|
-
throw error;
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
throw new StorageError('Failed to remove document after retries');
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Find document by ID
|
|
146
|
-
*/
|
|
147
|
-
async findById(docId: string): Promise<Record<string, any> | null> {
|
|
148
|
-
const shardId = getShardId(docId, this.shardCount);
|
|
149
|
-
const { shard } = await this.readShard(shardId);
|
|
150
|
-
return shard.documents[docId] || null;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
|
-
* Query documents matching filter
|
|
155
|
-
*/
|
|
156
|
-
async query(filter: QueryFilter<any>): Promise<Array<Record<string, any>>> {
|
|
157
|
-
// Optimization: If filter contains id equality, target specific shard
|
|
158
|
-
if (filter.id && typeof filter.id === 'string') {
|
|
159
|
-
const targetShardId = getShardId(filter.id, this.shardCount);
|
|
160
|
-
const { shard } = await this.readShard(targetShardId);
|
|
161
|
-
const doc = shard.documents[filter.id];
|
|
162
|
-
if (doc && evaluateFilter(doc, filter)) {
|
|
163
|
-
return [doc];
|
|
164
|
-
}
|
|
165
|
-
return [];
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
// Read all shard metadata first (lightweight)
|
|
169
|
-
const metadataPromises = Array.from(
|
|
170
|
-
{ length: this.shardCount },
|
|
171
|
-
(_, i) => this.readShardMetadata(i)
|
|
172
|
-
);
|
|
173
|
-
const allMetadata = await Promise.all(metadataPromises);
|
|
174
|
-
|
|
175
|
-
// Filter out empty shards
|
|
176
|
-
const nonEmptyShardIds = allMetadata
|
|
177
|
-
.map((meta, index) => ({ meta, index }))
|
|
178
|
-
.filter(({ meta }) => meta && meta.docCount > 0)
|
|
179
|
-
.map(({ index }) => index);
|
|
180
|
-
|
|
181
|
-
// Read only non-empty shards in parallel
|
|
182
|
-
const shardPromises = nonEmptyShardIds.map(i => this.readShard(i));
|
|
183
|
-
const shardResults = await Promise.all(shardPromises);
|
|
184
|
-
|
|
185
|
-
// Filter documents across all shards
|
|
186
|
-
const results: Array<Record<string, any>> = [];
|
|
187
|
-
for (const { shard } of shardResults) {
|
|
188
|
-
for (const doc of Object.values(shard.documents)) {
|
|
189
|
-
if (evaluateFilter(doc, filter)) {
|
|
190
|
-
results.push(doc);
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
return results;
|
|
196
|
-
}
|
|
197
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
// Core classes
|
|
2
|
-
export { BucketDB } from './core/bucket-db.js';
|
|
3
|
-
export { Collection } from './core/collection.js';
|
|
4
|
-
|
|
5
|
-
// Storage adapters
|
|
6
|
-
export { MemoryStorageAdapter } from './storage/memory-adapter.js';
|
|
7
|
-
export { S3Adapter } from './storage/s3-adapter.js';
|
|
8
|
-
export { OSSAdapter } from './storage/oss-adapter.js';
|
|
9
|
-
export { FileSystemAdapter } from './storage/fs-adapter.js';
|
|
10
|
-
|
|
11
|
-
// Re-export types from @bucket-db/types
|
|
12
|
-
export type {
|
|
13
|
-
Document,
|
|
14
|
-
InsertDocument,
|
|
15
|
-
UpdateDocument,
|
|
16
|
-
QueryOperator,
|
|
17
|
-
QueryValue,
|
|
18
|
-
QueryFilter,
|
|
19
|
-
QueryOptions,
|
|
20
|
-
UpdateOptions,
|
|
21
|
-
StorageAdapter,
|
|
22
|
-
StorageObject,
|
|
23
|
-
PutOptions,
|
|
24
|
-
StorageCredentials,
|
|
25
|
-
StorageAdapterConfig,
|
|
26
|
-
ICollection,
|
|
27
|
-
CollectionMeta,
|
|
28
|
-
IndexShard,
|
|
29
|
-
} from './types/index.js';
|
|
30
|
-
|
|
31
|
-
export {
|
|
32
|
-
BucketDBError,
|
|
33
|
-
DocumentNotFoundError,
|
|
34
|
-
ConcurrentUpdateError,
|
|
35
|
-
ValidationError,
|
|
36
|
-
StorageError,
|
|
37
|
-
} from './types/index.js';
|
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
|
-
}
|