tero 0.0.12 → 0.1.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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "tero",
3
- "version": "0.0.12",
4
- "description": "Tero is a JSON database that provides ACID transactions, schema validation, automated cloud backup, and automatic cloud recovery",
3
+ "version": "0.1.0",
4
+ "description": "Tero is a JSON database that provides ACID transactions, automated cloud backup, and automatic cloud recovery",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
7
7
  "types": "dist/index.d.ts",
@@ -21,10 +21,6 @@
21
21
  "./recovery": {
22
22
  "types": "./dist/recovery.d.ts",
23
23
  "import": "./dist/recovery.js"
24
- },
25
- "./schema": {
26
- "types": "./dist/schema.d.ts",
27
- "import": "./dist/schema.js"
28
24
  }
29
25
  },
30
26
  "repository": {
@@ -43,17 +39,25 @@
43
39
  "build": "tsc",
44
40
  "dev": "tsc --watch",
45
41
  "test": "node run-tests.js",
46
- "test:production": "node local_tests/prod-test-suite.js",
47
- "test:s3": "node local_tests/s3-live-backup-test.js",
48
- "test:all": "npm run test && npm run test:production",
42
+ "test:sanity": "node tests/sanity-test.js",
43
+ "test:issues": "node --test tests/issue-fixes.test.js",
44
+ "test:hydration": "node --test tests/gradual-hydration.test.js",
45
+ "test:fault": "node --test tests/fault-injection.test.js",
46
+ "test:cloud": "node --test tests/cloud-resilience.test.js",
47
+ "test:concurrency": "node --test tests/concurrency-chaos.test.js",
48
+ "test:fuzz": "node --test tests/fuzz-oracle.test.js",
49
+ "test:chaos": "node tests/chaos-test.js",
50
+ "test:production": "node tests/prod-test-suite.js",
51
+ "test:s3": "node tests/s3-live-backup-test.js",
52
+ "test:all": "node run-tests.js",
49
53
  "test:watch": "npm run test",
50
- "verify": "node local_tests/prod-test-suite.js && npm run test",
51
- "ci": "npm run test && node local_tests/prod-test-suite.js",
52
- "bench": "node bench/run-bench.js --synchronous normal",
53
- "bench:full": "node bench/run-bench.js --synchronous full",
54
- "bench:quick": "node bench/run-bench.js --quick --synchronous normal",
55
- "bench:live": "node bench/run-live-bench.js",
56
- "bench:live:quick": "node bench/run-live-bench.js --quick"
54
+ "verify": "node run-tests.js",
55
+ "ci": "node run-tests.js",
56
+ "bench": "node tests/run-bench.js --synchronous normal",
57
+ "bench:full": "node tests/run-bench.js --synchronous full",
58
+ "bench:quick": "node tests/run-bench.js --quick --synchronous normal",
59
+ "bench:live": "node tests/run-live-bench.js",
60
+ "bench:live:quick": "node tests/run-live-bench.js --quick"
57
61
  },
58
62
  "files": [
59
63
  "dist/**/*"
package/dist/schema.d.ts DELETED
@@ -1,61 +0,0 @@
1
- export interface SchemaField {
2
- type: 'string' | 'number' | 'boolean' | 'object' | 'array' | 'date' | 'any';
3
- required?: boolean;
4
- min?: number;
5
- max?: number;
6
- format?: 'email' | 'url' | 'uuid' | 'date' | 'time' | 'datetime' | 'phone' | 'ip';
7
- pattern?: string;
8
- enum?: any[];
9
- default?: any;
10
- properties?: {
11
- [key: string]: SchemaField;
12
- };
13
- items?: SchemaField;
14
- custom?: (value: any) => boolean | string;
15
- }
16
- export interface DocumentSchema {
17
- [fieldName: string]: SchemaField;
18
- }
19
- export interface ValidationError {
20
- field: string;
21
- message: string;
22
- value?: any;
23
- expected?: string;
24
- }
25
- export interface ValidationResult {
26
- valid: boolean;
27
- errors: ValidationError[];
28
- data?: any;
29
- }
30
- export declare class SchemaValidator {
31
- private schemas;
32
- setSchema(collectionName: string, schema: DocumentSchema): void;
33
- getSchema(collectionName: string): DocumentSchema | undefined;
34
- removeSchema(collectionName: string): boolean;
35
- hasSchema(collectionName: string): boolean;
36
- listSchemas(): string[];
37
- private validateSchemaDefinition;
38
- validate(collectionName: string, data: any): ValidationResult;
39
- private validateAndSanitizeData;
40
- private validateField;
41
- private validateType;
42
- private validateString;
43
- private validateNumber;
44
- private validateBoolean;
45
- private validateDate;
46
- private validateArray;
47
- private validateObject;
48
- private validateFormat;
49
- getSchemaStats(): {
50
- totalSchemas: number;
51
- schemaNames: string[];
52
- totalFields: number;
53
- };
54
- exportSchemas(): {
55
- [collectionName: string]: DocumentSchema;
56
- };
57
- importSchemas(schemas: {
58
- [collectionName: string]: DocumentSchema;
59
- }): void;
60
- clearAllSchemas(): void;
61
- }
package/dist/schema.js DELETED
@@ -1,425 +0,0 @@
1
- export class SchemaValidator {
2
- schemas = new Map();
3
- setSchema(collectionName, schema) {
4
- try {
5
- // Validate the schema itself
6
- this.validateSchemaDefinition(schema);
7
- this.schemas.set(collectionName, schema);
8
- }
9
- catch (error) {
10
- throw new Error(`Invalid schema definition: ${error instanceof Error ? error.message : 'Unknown error'}`);
11
- }
12
- }
13
- getSchema(collectionName) {
14
- return this.schemas.get(collectionName);
15
- }
16
- removeSchema(collectionName) {
17
- return this.schemas.delete(collectionName);
18
- }
19
- hasSchema(collectionName) {
20
- return this.schemas.has(collectionName);
21
- }
22
- listSchemas() {
23
- return Array.from(this.schemas.keys());
24
- }
25
- validateSchemaDefinition(schema) {
26
- for (const [fieldName, fieldSchema] of Object.entries(schema)) {
27
- if (!fieldName || typeof fieldName !== 'string') {
28
- throw new Error('Field names must be non-empty strings');
29
- }
30
- if (!fieldSchema || typeof fieldSchema !== 'object') {
31
- throw new Error(`Field '${fieldName}' must have a schema definition`);
32
- }
33
- if (!fieldSchema.type) {
34
- throw new Error(`Field '${fieldName}' must have a type`);
35
- }
36
- const validTypes = ['string', 'number', 'boolean', 'object', 'array', 'date', 'any'];
37
- if (!validTypes.includes(fieldSchema.type)) {
38
- throw new Error(`Field '${fieldName}' has invalid type '${fieldSchema.type}'`);
39
- }
40
- // Validate format if specified
41
- if (fieldSchema.format) {
42
- const validFormats = ['email', 'url', 'uuid', 'date', 'time', 'datetime', 'phone', 'ip'];
43
- if (!validFormats.includes(fieldSchema.format)) {
44
- throw new Error(`Field '${fieldName}' has invalid format '${fieldSchema.format}'`);
45
- }
46
- }
47
- // Validate min/max for numbers
48
- if (fieldSchema.type === 'number' && fieldSchema.min !== undefined && fieldSchema.max !== undefined) {
49
- if (fieldSchema.min > fieldSchema.max) {
50
- throw new Error(`Field '${fieldName}' min value cannot be greater than max value`);
51
- }
52
- }
53
- // Validate nested object schema
54
- if (fieldSchema.type === 'object' && fieldSchema.properties) {
55
- this.validateSchemaDefinition(fieldSchema.properties);
56
- }
57
- // Validate array items schema
58
- if (fieldSchema.type === 'array' && fieldSchema.items) {
59
- this.validateSchemaDefinition({ items: fieldSchema.items });
60
- }
61
- }
62
- }
63
- validate(collectionName, data) {
64
- const schema = this.schemas.get(collectionName);
65
- // If no schema is defined, validation passes
66
- if (!schema) {
67
- return { valid: true, errors: [], data };
68
- }
69
- try {
70
- const errors = [];
71
- const sanitizedData = this.validateAndSanitizeData(data, schema, '', errors);
72
- return {
73
- valid: errors.length === 0,
74
- errors,
75
- data: errors.length === 0 ? sanitizedData : data
76
- };
77
- }
78
- catch (error) {
79
- return {
80
- valid: false,
81
- errors: [{
82
- field: 'root',
83
- message: error instanceof Error ? error.message : 'Validation failed',
84
- value: data
85
- }],
86
- data
87
- };
88
- }
89
- }
90
- validateAndSanitizeData(data, schema, path, errors) {
91
- if (data === null || data === undefined) {
92
- data = {};
93
- }
94
- if (typeof data !== 'object' || Array.isArray(data)) {
95
- errors.push({
96
- field: path || 'root',
97
- message: 'Data must be an object',
98
- value: data,
99
- expected: 'object'
100
- });
101
- return data;
102
- }
103
- const result = {};
104
- // Validate each field in the schema
105
- for (const [fieldName, fieldSchema] of Object.entries(schema)) {
106
- const fieldPath = path ? `${path}.${fieldName}` : fieldName;
107
- const fieldValue = data[fieldName];
108
- // Check if required field is missing
109
- if (fieldSchema.required && (fieldValue === undefined || fieldValue === null)) {
110
- // Use default value if available
111
- if (fieldSchema.default !== undefined) {
112
- result[fieldName] = fieldSchema.default;
113
- continue;
114
- }
115
- errors.push({
116
- field: fieldPath,
117
- message: `Required field '${fieldName}' is missing`,
118
- expected: fieldSchema.type
119
- });
120
- continue;
121
- }
122
- // Skip validation if field is not provided and not required
123
- if (fieldValue === undefined || fieldValue === null) {
124
- if (fieldSchema.default !== undefined) {
125
- result[fieldName] = fieldSchema.default;
126
- }
127
- continue;
128
- }
129
- // Validate the field
130
- const validatedValue = this.validateField(fieldValue, fieldSchema, fieldPath, errors);
131
- if (validatedValue !== undefined) {
132
- result[fieldName] = validatedValue;
133
- }
134
- }
135
- // Copy over any additional fields not in schema (flexible schema)
136
- for (const [key, value] of Object.entries(data)) {
137
- if (!(key in schema)) {
138
- result[key] = value;
139
- }
140
- }
141
- return result;
142
- }
143
- validateField(value, fieldSchema, path, errors) {
144
- // Type validation
145
- if (!this.validateType(value, fieldSchema.type)) {
146
- errors.push({
147
- field: path,
148
- message: `Expected ${fieldSchema.type} but got ${typeof value}`,
149
- value,
150
- expected: fieldSchema.type
151
- });
152
- return value;
153
- }
154
- // Convert and validate based on type
155
- let processedValue = value;
156
- switch (fieldSchema.type) {
157
- case 'string':
158
- processedValue = this.validateString(value, fieldSchema, path, errors);
159
- break;
160
- case 'number':
161
- processedValue = this.validateNumber(value, fieldSchema, path, errors);
162
- break;
163
- case 'boolean':
164
- processedValue = this.validateBoolean(value, fieldSchema, path, errors);
165
- break;
166
- case 'date':
167
- processedValue = this.validateDate(value, fieldSchema, path, errors);
168
- break;
169
- case 'array':
170
- processedValue = this.validateArray(value, fieldSchema, path, errors);
171
- break;
172
- case 'object':
173
- processedValue = this.validateObject(value, fieldSchema, path, errors);
174
- break;
175
- case 'any':
176
- // No specific validation for 'any' type
177
- break;
178
- }
179
- // Enum validation
180
- if (fieldSchema.enum && !fieldSchema.enum.includes(processedValue)) {
181
- errors.push({
182
- field: path,
183
- message: `Value must be one of: ${fieldSchema.enum.join(', ')}`,
184
- value: processedValue,
185
- expected: `enum: [${fieldSchema.enum.join(', ')}]`
186
- });
187
- }
188
- // Custom validation
189
- if (fieldSchema.custom) {
190
- const customResult = fieldSchema.custom(processedValue);
191
- if (customResult !== true) {
192
- errors.push({
193
- field: path,
194
- message: typeof customResult === 'string' ? customResult : 'Custom validation failed',
195
- value: processedValue
196
- });
197
- }
198
- }
199
- return processedValue;
200
- }
201
- validateType(value, expectedType) {
202
- switch (expectedType) {
203
- case 'string':
204
- return typeof value === 'string';
205
- case 'number':
206
- return typeof value === 'number' && !isNaN(value);
207
- case 'boolean':
208
- return typeof value === 'boolean';
209
- case 'object':
210
- return typeof value === 'object' && value !== null && !Array.isArray(value);
211
- case 'array':
212
- return Array.isArray(value);
213
- case 'date':
214
- return value instanceof Date || typeof value === 'string' || typeof value === 'number';
215
- case 'any':
216
- return true;
217
- default:
218
- return false;
219
- }
220
- }
221
- validateString(value, schema, path, errors) {
222
- // Length validation
223
- if (schema.min !== undefined && value.length < schema.min) {
224
- errors.push({
225
- field: path,
226
- message: `String must be at least ${schema.min} characters long`,
227
- value,
228
- expected: `min length: ${schema.min}`
229
- });
230
- }
231
- if (schema.max !== undefined && value.length > schema.max) {
232
- errors.push({
233
- field: path,
234
- message: `String must be at most ${schema.max} characters long`,
235
- value,
236
- expected: `max length: ${schema.max}`
237
- });
238
- }
239
- // Format validation
240
- if (schema.format) {
241
- if (!this.validateFormat(value, schema.format)) {
242
- errors.push({
243
- field: path,
244
- message: `Invalid ${schema.format} format`,
245
- value,
246
- expected: `valid ${schema.format}`
247
- });
248
- }
249
- }
250
- // Pattern validation
251
- if (schema.pattern) {
252
- const regex = new RegExp(schema.pattern);
253
- if (!regex.test(value)) {
254
- errors.push({
255
- field: path,
256
- message: `String does not match required pattern: ${schema.pattern}`,
257
- value,
258
- expected: `pattern: ${schema.pattern}`
259
- });
260
- }
261
- }
262
- return value;
263
- }
264
- validateNumber(value, schema, path, errors) {
265
- // Range validation
266
- if (schema.min !== undefined && value < schema.min) {
267
- errors.push({
268
- field: path,
269
- message: `Number must be at least ${schema.min}`,
270
- value,
271
- expected: `min: ${schema.min}`
272
- });
273
- }
274
- if (schema.max !== undefined && value > schema.max) {
275
- errors.push({
276
- field: path,
277
- message: `Number must be at most ${schema.max}`,
278
- value,
279
- expected: `max: ${schema.max}`
280
- });
281
- }
282
- return value;
283
- }
284
- validateBoolean(value, schema, path, errors) {
285
- return value;
286
- }
287
- validateDate(value, schema, path, errors) {
288
- let date;
289
- if (value instanceof Date) {
290
- date = value;
291
- }
292
- else if (typeof value === 'string' || typeof value === 'number') {
293
- date = new Date(value);
294
- }
295
- else {
296
- errors.push({
297
- field: path,
298
- message: 'Invalid date value',
299
- value,
300
- expected: 'Date, string, or number'
301
- });
302
- return value;
303
- }
304
- if (isNaN(date.getTime())) {
305
- errors.push({
306
- field: path,
307
- message: 'Invalid date value',
308
- value,
309
- expected: 'valid date'
310
- });
311
- return value;
312
- }
313
- return date;
314
- }
315
- validateArray(value, schema, path, errors) {
316
- if (!Array.isArray(value)) {
317
- errors.push({
318
- field: path,
319
- message: 'Expected array',
320
- value,
321
- expected: 'array'
322
- });
323
- return value;
324
- }
325
- // Length validation
326
- if (schema.min !== undefined && value.length < schema.min) {
327
- errors.push({
328
- field: path,
329
- message: `Array must have at least ${schema.min} items`,
330
- value,
331
- expected: `min length: ${schema.min}`
332
- });
333
- }
334
- if (schema.max !== undefined && value.length > schema.max) {
335
- errors.push({
336
- field: path,
337
- message: `Array must have at most ${schema.max} items`,
338
- value,
339
- expected: `max length: ${schema.max}`
340
- });
341
- }
342
- // Validate array items
343
- if (schema.items) {
344
- const validatedArray = value.map((item, index) => {
345
- const itemPath = `${path}[${index}]`;
346
- return this.validateField(item, schema.items, itemPath, errors);
347
- });
348
- return validatedArray;
349
- }
350
- return value;
351
- }
352
- validateObject(value, schema, path, errors) {
353
- if (typeof value !== 'object' || value === null || Array.isArray(value)) {
354
- errors.push({
355
- field: path,
356
- message: 'Expected object',
357
- value,
358
- expected: 'object'
359
- });
360
- return value;
361
- }
362
- // Validate nested object properties
363
- if (schema.properties) {
364
- return this.validateAndSanitizeData(value, schema.properties, path, errors);
365
- }
366
- return value;
367
- }
368
- validateFormat(value, format) {
369
- switch (format) {
370
- case 'email':
371
- return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value);
372
- case 'url':
373
- try {
374
- new URL(value);
375
- return true;
376
- }
377
- catch {
378
- return false;
379
- }
380
- case 'uuid':
381
- return /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(value);
382
- case 'date':
383
- return /^\d{4}-\d{2}-\d{2}$/.test(value) && !isNaN(Date.parse(value));
384
- case 'time':
385
- return /^([01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$/.test(value);
386
- case 'datetime':
387
- return !isNaN(Date.parse(value));
388
- case 'phone':
389
- return /^[\+]?[1-9][\d]{0,15}$/.test(value.replace(/[\s\-\(\)]/g, ''));
390
- case 'ip':
391
- return /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(value) ||
392
- /^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$/.test(value);
393
- default:
394
- return true;
395
- }
396
- }
397
- // Utility methods
398
- getSchemaStats() {
399
- const schemaNames = Array.from(this.schemas.keys());
400
- let totalFields = 0;
401
- for (const schema of this.schemas.values()) {
402
- totalFields += Object.keys(schema).length;
403
- }
404
- return {
405
- totalSchemas: this.schemas.size,
406
- schemaNames,
407
- totalFields
408
- };
409
- }
410
- exportSchemas() {
411
- const exported = {};
412
- for (const [name, schema] of this.schemas.entries()) {
413
- exported[name] = { ...schema };
414
- }
415
- return exported;
416
- }
417
- importSchemas(schemas) {
418
- for (const [name, schema] of Object.entries(schemas)) {
419
- this.setSchema(name, schema);
420
- }
421
- }
422
- clearAllSchemas() {
423
- this.schemas.clear();
424
- }
425
- }