s3db.js 7.3.10 → 7.4.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "s3db.js",
3
- "version": "7.3.10",
3
+ "version": "7.4.1",
4
4
  "description": "Use AWS S3, the world's most reliable document storage, as a database with this ORM.",
5
5
  "main": "dist/s3db.cjs.js",
6
6
  "module": "dist/s3db.es.js",
@@ -103,7 +103,17 @@ export class Client extends EventEmitter {
103
103
  for (const [k, v] of Object.entries(metadata)) {
104
104
  // Ensure key is a valid string and value is a string
105
105
  const validKey = String(k).replace(/[^a-zA-Z0-9\-_]/g, '_');
106
- stringMetadata[validKey] = String(v);
106
+ const stringValue = String(v);
107
+
108
+ // Check if value contains non-ASCII characters that might be corrupted by HTTP headers
109
+ const hasSpecialChars = /[^\x00-\x7F]/.test(stringValue);
110
+
111
+ if (hasSpecialChars) {
112
+ // Encode as base64 without prefix - we'll detect it intelligently on read
113
+ stringMetadata[validKey] = Buffer.from(stringValue, 'utf8').toString('base64');
114
+ } else {
115
+ stringMetadata[validKey] = stringValue;
116
+ }
107
117
  }
108
118
  }
109
119
 
@@ -141,9 +151,39 @@ export class Client extends EventEmitter {
141
151
  Bucket: this.config.bucket,
142
152
  Key: keyPrefix ? path.join(keyPrefix, key) : key,
143
153
  };
154
+
144
155
  let response, error;
145
156
  try {
146
157
  response = await this.sendCommand(new GetObjectCommand(options));
158
+
159
+ // Smart decode: try to detect base64-encoded UTF-8 values without prefix
160
+ if (response.Metadata) {
161
+ const decodedMetadata = {};
162
+ for (const [key, value] of Object.entries(response.Metadata)) {
163
+ if (typeof value === 'string') {
164
+ // Try to decode as base64 and check if it's valid UTF-8 with special chars
165
+ try {
166
+ const decoded = Buffer.from(value, 'base64').toString('utf8');
167
+ // Check if decoded string is different from original and contains non-ASCII chars
168
+ const hasSpecialChars = /[^\x00-\x7F]/.test(decoded);
169
+ const isValidBase64 = Buffer.from(decoded, 'utf8').toString('base64') === value;
170
+
171
+ if (isValidBase64 && hasSpecialChars && decoded !== value) {
172
+ decodedMetadata[key] = decoded;
173
+ } else {
174
+ decodedMetadata[key] = value;
175
+ }
176
+ } catch (decodeError) {
177
+ // If decode fails, use original value
178
+ decodedMetadata[key] = value;
179
+ }
180
+ } else {
181
+ decodedMetadata[key] = value;
182
+ }
183
+ }
184
+ response.Metadata = decodedMetadata;
185
+ }
186
+
147
187
  return response;
148
188
  } catch (err) {
149
189
  error = err;
@@ -471,7 +471,8 @@ export class Database extends EventEmitter {
471
471
  versioningEnabled: this.versioningEnabled,
472
472
  map: config.map,
473
473
  idGenerator: config.idGenerator,
474
- idSize: config.idSize
474
+ idSize: config.idSize,
475
+ events: config.events || {}
475
476
  });
476
477
  resource.database = this;
477
478
  this.resources[name] = resource;
@@ -40,6 +40,7 @@ export class Resource extends EventEmitter {
40
40
  * @param {Function} [config.idGenerator] - Custom ID generator function
41
41
  * @param {number} [config.idSize=22] - Size for auto-generated IDs
42
42
  * @param {boolean} [config.versioningEnabled=false] - Enable versioning for this resource
43
+ * @param {Object} [config.events={}] - Event listeners to automatically add
43
44
  * @example
44
45
  * const users = new Resource({
45
46
  * name: 'users',
@@ -61,6 +62,14 @@ export class Resource extends EventEmitter {
61
62
  * beforeInsert: [async (data) => {
62
63
  * return data;
63
64
  * }]
65
+ * },
66
+ * events: {
67
+ * insert: (ev) => console.log('Inserted:', ev.id),
68
+ * update: [
69
+ * (ev) => console.warn('Update detected'),
70
+ * (ev) => console.log('Updated:', ev.id)
71
+ * ],
72
+ * delete: (ev) => console.log('Deleted:', ev.id)
64
73
  * }
65
74
  * });
66
75
  *
@@ -117,7 +126,8 @@ export class Resource extends EventEmitter {
117
126
  hooks = {},
118
127
  idGenerator: customIdGenerator,
119
128
  idSize = 22,
120
- versioningEnabled = false
129
+ versioningEnabled = false,
130
+ events = {}
121
131
  } = config;
122
132
 
123
133
  // Set instance properties
@@ -177,6 +187,23 @@ export class Resource extends EventEmitter {
177
187
  }
178
188
  }
179
189
 
190
+ // Setup event listeners
191
+ if (events && Object.keys(events).length > 0) {
192
+ for (const [eventName, listeners] of Object.entries(events)) {
193
+ if (Array.isArray(listeners)) {
194
+ // Multiple listeners for this event
195
+ for (const listener of listeners) {
196
+ if (typeof listener === 'function') {
197
+ this.on(eventName, listener);
198
+ }
199
+ }
200
+ } else if (typeof listeners === 'function') {
201
+ // Single listener for this event
202
+ this.on(eventName, listeners);
203
+ }
204
+ }
205
+ }
206
+
180
207
  // --- MIDDLEWARE SYSTEM ---
181
208
  this._initMiddleware();
182
209
  // Debug: print method names and typeof update at construction
@@ -2617,6 +2644,27 @@ function validateResourceConfig(config) {
2617
2644
  }
2618
2645
  }
2619
2646
 
2647
+ // Validate events
2648
+ if (config.events !== undefined) {
2649
+ if (typeof config.events !== 'object' || Array.isArray(config.events)) {
2650
+ errors.push("Resource 'events' must be an object");
2651
+ } else {
2652
+ for (const [eventName, listeners] of Object.entries(config.events)) {
2653
+ if (Array.isArray(listeners)) {
2654
+ // Multiple listeners for this event
2655
+ for (let i = 0; i < listeners.length; i++) {
2656
+ const listener = listeners[i];
2657
+ if (typeof listener !== 'function') {
2658
+ errors.push(`Resource 'events.${eventName}[${i}]' must be a function`);
2659
+ }
2660
+ }
2661
+ } else if (typeof listeners !== 'function') {
2662
+ errors.push(`Resource 'events.${eventName}' must be a function or array of functions`);
2663
+ }
2664
+ }
2665
+ }
2666
+ }
2667
+
2620
2668
  return {
2621
2669
  isValid: errors.length === 0,
2622
2670
  errors
package/src/s3db.d.ts CHANGED
@@ -47,6 +47,7 @@ declare module 's3db.js' {
47
47
  idSize?: number;
48
48
  versioningEnabled?: boolean;
49
49
  map?: any;
50
+ events?: EventListenerConfig;
50
51
  }
51
52
 
52
53
  /** Partition configuration */
@@ -65,6 +66,11 @@ declare module 's3db.js' {
65
66
  afterDelete?: Function[];
66
67
  }
67
68
 
69
+ /** Event listener configuration */
70
+ export interface EventListenerConfig {
71
+ [eventName: string]: Function | Function[];
72
+ }
73
+
68
74
  /** Query options */
69
75
  export interface QueryOptions {
70
76
  limit?: number;