pixl-server-storage 3.0.7 → 3.0.11

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 CHANGED
@@ -846,7 +846,7 @@ storage.getStream( 'test1.gif', function(err, readStream) {
846
846
  } );
847
847
  ```
848
848
 
849
- Please note that not all the storage engines support streams natively, so the content may actually be loaded into RAM in the background. Namely, as of this writing, the Couchbase and Redis APIs does not support streams, so they are currently simulated in those engines. Streams *are* supported natively in both the Filesystem and Amazon S3 engines.
849
+ Please note that not all the storage engines support streams natively, so the content may actually be loaded into RAM in the background. Namely, as of this writing, the Couchbase and Redis APIs do not support streams, so they are currently simulated in those engines. Streams *are* supported natively in both the Filesystem and Amazon S3 engines.
850
850
 
851
851
  # Expiring Data
852
852
 
package/docs/API.md CHANGED
@@ -18,6 +18,7 @@ var storage = server.Storage;
18
18
  * [get](#get)
19
19
  * [getMulti](#getmulti)
20
20
  * [getStream](#getstream)
21
+ * [getStreamRange](#getstreamrange)
21
22
  * [head](#head)
22
23
  * [headMulti](#headmulti)
23
24
  * [delete](#delete)
@@ -126,6 +127,8 @@ storage.putMulti( records, function(err) {
126
127
  } );
127
128
  ```
128
129
 
130
+ Note that if any of the individual put operations fail, the entire `putMulti()` function is aborted, and the first error is passed to your callback. At this point the operation may have been partially successful, with some records written, and others not. Due to this uncertainty, you may want to use this method inside of a [transaction](./Transactions.md), which can be safely rolled back upon error.
131
+
129
132
  ## putStream
130
133
 
131
134
  ```javascript
@@ -142,6 +145,8 @@ storage.putStream( 'test1.gif', stream, function(err) {
142
145
  } );
143
146
  ```
144
147
 
148
+ Please note that as of this writing, the `Couchbase`, `Redis` and `RedisCluster` engines have no native stream API, so the `putStream()` method has to load the entire record into memory.
149
+
145
150
  ## get
146
151
 
147
152
  ```javascript
@@ -173,19 +178,46 @@ storage.getMulti( ['test1', 'test2', 'test3'], function(err, values) {
173
178
  } );
174
179
  ```
175
180
 
181
+ Note that if *any* of the records fail, the entire operation fails, and the first error is passed to your callback.
182
+
176
183
  ## getStream
177
184
 
178
185
  ```javascript
179
186
  storage.getStream( KEY, CALLBACK );
180
187
  ```
181
188
 
182
- The `getStream()` method retrieves a [readable stream](https://nodejs.org/api/stream.html#stream_class_stream_readable) to a given record's data, so it can be read or piped to a writable stream. This is for very large records, so nothing is loaded into memory. Example of spooling to a local file:
189
+ The `getStream()` method retrieves a [readable stream](https://nodejs.org/api/stream.html#class-streamreadable) to a given record's data, so it can be read or piped to a writable stream. This is for very large records, so nothing is loaded into memory. Example of spooling to a local file:
190
+
191
+ ```javascript
192
+ var fs = require('fs');
193
+ var writeStream = fs.createWriteStream('/var/tmp/downloaded.gif');
194
+
195
+ storage.getStream( 'test1.gif', function(err, readStream, info) {
196
+ if (err) throw err;
197
+ writeStream.on('finish', function() {
198
+ // data is completely written
199
+ } );
200
+ readStream.pipe( writeStream );
201
+ } );
202
+ ```
203
+
204
+ As you can see above, your callback is also passed a 3rd argument, which is an object containing the record's full byte length and modification date. These properties match those returned when you call [head()](#head) (i.e. `len` and `mod`).
205
+
206
+ Please note that as of this writing, the `Couchbase`, `Redis` and `RedisCluster` engines have no native stream API, so the `getStream()` method has to load the entire record into memory.
207
+
208
+ ## getStreamRange
209
+
210
+ ```javascript
211
+ storage.getStreamRange( KEY, START, END, CALLBACK );
212
+ ```
213
+
214
+ The `getStreamRange()` method retrieves a [readable stream](https://nodejs.org/api/stream.html#class-streamreadable) to a specific slice of a record's data, so it can be read or piped to a writable stream. The `start` and `end` arguments should be set to the starting and ending byte offset of the slice you want. Both values are *inclusive*. Example of spooling bytes `0-99` of a record to a local file:
183
215
 
184
216
  ```javascript
185
217
  var fs = require('fs');
186
218
  var writeStream = fs.createWriteStream('/var/tmp/downloaded.gif');
187
219
 
188
- storage.getStream( 'test1.gif', function(err, readStream) {
220
+ storage.getStreamRange( 'test1.gif', 0, 99, function(err, readStream, info) {
189
221
  if (err) throw err;
190
222
  writeStream.on('finish', function() {
191
223
  // data is completely written
@@ -194,6 +226,10 @@ storage.getStream( 'test1.gif', function(err, readStream) {
194
226
  } );
195
227
  ```
196
228
 
229
+ As you can see above, your callback is also passed a 3rd argument, which is an object containing the record's full byte length and modification date. These properties match those returned when you call [head()](#head) (i.e. `len` and `mod`). The `len` is not affected by the `start` and `end` range -- it is always the full byte length of the record.
230
+
231
+ Please note that as of this writing, the `Couchbase`, `Redis` and `RedisCluster` engines have no native stream API, so the `getStreamRange()` method has to load the entire record into memory.
232
+
197
233
  ## head
198
234
 
199
235
  ```javascript
@@ -217,7 +253,7 @@ storage.head( 'test1', function(err, data) {
217
253
  } );
218
254
  ```
219
255
 
220
- Please note that as of this writing, the `Couchbase` engine has no native API, so the `head()` method has to load the entire record. It does return the record size in to the `len` property, but there is no way to retrieve the last modified date.
256
+ Please note that as of this writing, the `Couchbase`, `Redis` and `RedisCluster` engines have no native head API, so the `head()` method has to load the entire record. It does return the record size in to the `len` property, but there is no way to retrieve the last modified date.
221
257
 
222
258
  ## headMulti
223
259
 
@@ -224,7 +224,40 @@ module.exports = Class.create({
224
224
  }
225
225
 
226
226
  var stream = new BufferStream(buf);
227
- callback(null, stream);
227
+ callback(null, stream, { mod: 1, len: buf.length });
228
+ } );
229
+ },
230
+
231
+ getStreamRange: function(key, start, end, callback) {
232
+ // get readable stream to record value given key and range
233
+ var self = this;
234
+
235
+ // The Couchbase Node.JS 2.0 API has no stream support.
236
+ // So, we have to do this the RAM-hard way...
237
+ this.get( key, function(err, buf) {
238
+ if (err && (err.code != CouchbaseAPI.errors.keyNotFound)) {
239
+ // some other error
240
+ err.message = "Failed to fetch key: " + key + ": " + err.message;
241
+ self.logError('couchbase', err.message);
242
+ return callback(err);
243
+ }
244
+ else if (!buf) {
245
+ // record not found
246
+ var err = new Error("Failed to fetch key: " + key + ": Not found");
247
+ err.code = "NoSuchKey";
248
+ return callback( err, null );
249
+ }
250
+
251
+ // validate byte range, now that we have the head info
252
+ if ((start < 0) || (start >= buf.length) || (end < start) || (end >= buf.length)) {
253
+ download.destroy();
254
+ callback( new Error("Invalid byte range (" + start + '-' + end + ") for key: " + key + " (len: " + buf.length + ")"), null );
255
+ return;
256
+ }
257
+
258
+ var range = buf.slice(start, end + 1);
259
+ var stream = new BufferStream(range);
260
+ callback(null, stream, { mod: 1, len: buf.length });
228
261
  } );
229
262
  },
230
263
 
@@ -5,7 +5,6 @@
5
5
  var path = require('path');
6
6
  var fs = require('fs');
7
7
  var async = require('async');
8
- var mkdirp = require('mkdirp');
9
8
  var crypto = require('crypto');
10
9
 
11
10
  var Class = require("pixl-class");
@@ -13,6 +12,8 @@ var Component = require("pixl-server/component");
13
12
  var Tools = require("pixl-tools");
14
13
  var Cache = require("pixl-cache");
15
14
 
15
+ var mkdirp = Tools.mkdirp;
16
+
16
17
  module.exports = Class.create({
17
18
 
18
19
  __name: 'Filesystem',
@@ -449,7 +450,50 @@ module.exports = Class.create({
449
450
  // create read stream
450
451
  var inp = fs.createReadStream( file );
451
452
 
452
- callback( null, inp );
453
+ callback( null, inp, {
454
+ mod: Math.floor(stats.mtime.getTime() / 1000),
455
+ len: stats.size
456
+ } );
457
+ } );
458
+ },
459
+
460
+ getStreamRange: function(key, start, end, callback) {
461
+ // get readable stream to record value given key and byte range
462
+ var self = this;
463
+ var file = this.getFilePath(key);
464
+
465
+ this.logDebug(9, "Fetching ranged binary stream: " + key, { file, start, end } );
466
+
467
+ // make sure record exists
468
+ fs.stat(file, function(err, stats) {
469
+ if (err) {
470
+ if (err.message.match(/ENOENT/)) {
471
+ err.message = "File not found";
472
+ err.code = "NoSuchKey";
473
+ }
474
+ else {
475
+ // log fs errors that aren't simple missing files (i.e. I/O errors)
476
+ self.logError('file', "Failed to stat file: " + key + ": " + file + ": " + err.message);
477
+ }
478
+
479
+ err.message = "Failed to head key: " + key + ": " + err.message;
480
+ return callback( err, null );
481
+ }
482
+
483
+ // validate byte range, now that we have the head info
484
+ if ((start < 0) || (start >= stats.size) || (end < start) || (end >= stats.size)) {
485
+ download.destroy();
486
+ callback( new Error("Invalid byte range (" + start + '-' + end + ") for key: " + key + " (len: " + stats.size + ")"), null );
487
+ return;
488
+ }
489
+
490
+ // create read stream
491
+ var inp = fs.createReadStream( file, { start, end } );
492
+
493
+ callback( null, inp, {
494
+ mod: Math.floor(stats.mtime.getTime() / 1000),
495
+ len: stats.size
496
+ } );
453
497
  } );
454
498
  },
455
499
 
package/engines/Hybrid.js CHANGED
@@ -79,6 +79,12 @@ module.exports = Class.create({
79
79
  this.binaryEngine.getStream( key, callback );
80
80
  },
81
81
 
82
+ getStreamRange: function(key, start, end, callback) {
83
+ // get readable stream to record value given key and range
84
+ // streams are binary only!
85
+ this.binaryEngine.getStreamRange( key, start, end, callback );
86
+ },
87
+
82
88
  delete: function(key, callback) {
83
89
  // delete hybrid key given key
84
90
  if (this.storage.isBinaryKey(key)) {
package/engines/Redis.js CHANGED
@@ -227,7 +227,41 @@ module.exports = Class.create({
227
227
  }
228
228
 
229
229
  var stream = new BufferStream(buf);
230
- callback(null, stream);
230
+ callback(null, stream, { mod: 1, len: buf.length });
231
+ } );
232
+ },
233
+
234
+ getStreamRange: function(key, start, end, callback) {
235
+ // get readable stream to record value given key and range
236
+ var self = this;
237
+
238
+ // The Redis API has no stream support.
239
+ // So, we have to do this the RAM-hard way...
240
+
241
+ this.get( key, function(err, buf) {
242
+ if (err) {
243
+ // an actual error
244
+ err.message = "Failed to fetch key: " + key + ": " + err;
245
+ self.logError('redis', ''+err);
246
+ return callback(err);
247
+ }
248
+ else if (!buf) {
249
+ // record not found
250
+ var err = new Error("Failed to fetch key: " + key + ": Not found");
251
+ err.code = "NoSuchKey";
252
+ return callback( err, null );
253
+ }
254
+
255
+ // validate byte range, now that we have the head info
256
+ if ((start < 0) || (start >= buf.length) || (end < start) || (end >= buf.length)) {
257
+ download.destroy();
258
+ callback( new Error("Invalid byte range (" + start + '-' + end + ") for key: " + key + " (len: " + buf.length + ")"), null );
259
+ return;
260
+ }
261
+
262
+ var range = buf.slice(start, end + 1);
263
+ var stream = new BufferStream(range);
264
+ callback(null, stream, { mod: 1, len: buf.length });
231
265
  } );
232
266
  },
233
267
 
@@ -244,7 +244,41 @@ module.exports = Class.create({
244
244
  }
245
245
 
246
246
  var stream = new BufferStream(buf);
247
- callback(null, stream);
247
+ callback(null, stream, { mod: 1, len: buf.length });
248
+ } );
249
+ },
250
+
251
+ getStreamRange: function(key, start, end, callback) {
252
+ // get readable stream to record value given key and range
253
+ var self = this;
254
+
255
+ // The Redis API has no stream support.
256
+ // So, we have to do this the RAM-hard way...
257
+
258
+ this.get( key, function(err, buf) {
259
+ if (err) {
260
+ // an actual error
261
+ err.message = "Failed to fetch key: " + key + ": " + err;
262
+ self.logError('redis', ''+err);
263
+ return callback(err);
264
+ }
265
+ else if (!buf) {
266
+ // record not found
267
+ var err = new Error("Failed to fetch key: " + key + ": Not found");
268
+ err.code = "NoSuchKey";
269
+ return callback( err, null );
270
+ }
271
+
272
+ // validate byte range, now that we have the head info
273
+ if ((start < 0) || (start >= buf.length) || (end < start) || (end >= buf.length)) {
274
+ download.destroy();
275
+ callback( new Error("Invalid byte range (" + start + '-' + end + ") for key: " + key + " (len: " + buf.length + ")"), null );
276
+ return;
277
+ }
278
+
279
+ var range = buf.slice(start, end + 1);
280
+ var stream = new BufferStream(range);
281
+ callback(null, stream, { mod: 1, len: buf.length });
248
282
  } );
249
283
  },
250
284
 
package/engines/S3.js CHANGED
@@ -294,33 +294,89 @@ module.exports = Class.create({
294
294
  this.logDebug(9, "Fetching S3 Stream: " + key);
295
295
 
296
296
  var params = { Key: this.extKey(key, orig_key) };
297
+ var download = self.s3.getObject(params).createReadStream();
298
+ var proceed = false;
299
+
300
+ download.on('error', function(err) {
301
+ if (proceed) self.logError('s3', "Failed to download key: " + key + ": " + err.message);
302
+ });
303
+ download.once('end', function() {
304
+ self.logDebug(9, "S3 stream download complete: " + key);
305
+ } );
306
+ download.once('close', function() {
307
+ self.logDebug(9, "S3 stream download closed: " + key);
308
+ } );
297
309
 
298
310
  // double-callback protection (bug in aws-sdk)
299
311
  var done = false;
300
- this.s3.headObject( params, function(err) {
312
+ this.s3.headObject( params, function(err, data) {
301
313
  if (done) return; else done = true;
302
314
 
303
315
  if (err) {
304
316
  if (err.code != 'NoSuchKey') {
305
317
  self.logError('s3', "Failed to head key: " + key + ": " + err.message);
306
318
  }
319
+ download.destroy();
307
320
  callback( err, null );
308
321
  return;
309
322
  }
310
323
 
311
- var download = self.s3.getObject(params).createReadStream();
312
-
313
- download.once('error', function(err) {
314
- self.logError('s3', "Failed to download key: " + key + ": " + err.message);
315
- });
316
- download.once('end', function() {
317
- self.logDebug(9, "S3 stream download complete: " + key);
318
- } );
319
- download.once('close', function() {
320
- self.logDebug(9, "S3 stream download closed: " + key);
324
+ proceed = true;
325
+ callback( null, download, {
326
+ mod: Math.floor((new Date(data.LastModified)).getTime() / 1000),
327
+ len: data.ContentLength
321
328
  } );
329
+ }); // headObject
330
+ },
331
+
332
+ getStreamRange: function(key, start, end, callback) {
333
+ // get readable stream to record value given key and range
334
+ var self = this;
335
+ var orig_key = key;
336
+ key = this.prepKey(key);
337
+
338
+ this.logDebug(9, "Fetching ranged S3 stream: " + key, { start, end });
339
+
340
+ var params = { Key: this.extKey(key, orig_key) };
341
+ var download = self.s3.getObject( Tools.mergeHashes(params, { Range: "bytes=" + start + "-" + end }) ).createReadStream();
342
+ var proceed = false;
343
+
344
+ download.on('error', function(err) {
345
+ if (proceed) self.logError('s3', "Failed to download key: " + key + ": " + err.message);
346
+ });
347
+ download.once('end', function() {
348
+ self.logDebug(9, "S3 stream download complete: " + key);
349
+ } );
350
+ download.once('close', function() {
351
+ self.logDebug(9, "S3 stream download closed: " + key);
352
+ } );
353
+
354
+ // double-callback protection (bug in aws-sdk)
355
+ var done = false;
356
+ this.s3.headObject( params, function(err, data) {
357
+ if (done) return; else done = true;
322
358
 
323
- callback( null, download );
359
+ if (err) {
360
+ if (err.code != 'NoSuchKey') {
361
+ self.logError('s3', "Failed to head key: " + key + ": " + err.message);
362
+ }
363
+ download.destroy();
364
+ callback( err, null );
365
+ return;
366
+ }
367
+
368
+ // validate byte range, now that we have the head info
369
+ if ((start < 0) || (start >= data.ContentLength) || (end < start) || (end >= data.ContentLength)) {
370
+ download.destroy();
371
+ callback( new Error("Invalid byte range (" + start + '-' + end + ") for key: " + key + " (len: " + data.ContentLength + ")"), null );
372
+ return;
373
+ }
374
+
375
+ proceed = true;
376
+ callback( null, download, {
377
+ mod: Math.floor((new Date(data.LastModified)).getTime() / 1000),
378
+ len: data.ContentLength
379
+ } );
324
380
  }); // headObject
325
381
  },
326
382
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pixl-server-storage",
3
- "version": "3.0.7",
3
+ "version": "3.0.11",
4
4
  "description": "A key/value/list storage component for the pixl-server framework.",
5
5
  "author": "Joseph Huckaby <jhuckaby@gmail.com>",
6
6
  "homepage": "https://github.com/jhuckaby/pixl-server-storage",
@@ -27,8 +27,7 @@
27
27
  "pixl-perf": "^1.0.0",
28
28
  "pixl-cache": "^1.0.1",
29
29
  "async": "2.6.0",
30
- "mkdirp": "0.5.1",
31
- "nearley": "2.11.0",
30
+ "nearley": "2.20.1",
32
31
  "moo": "0.4.3",
33
32
  "porter-stemmer": "0.9.1",
34
33
  "unidecode": "0.1.8",
package/storage.js CHANGED
@@ -354,6 +354,21 @@ module.exports = Class.create({
354
354
  this.engine.getStream( key, callback );
355
355
  },
356
356
 
357
+ getStreamRange: function(key, start, end, callback) {
358
+ // fetch value via stream pipe and range
359
+ // start and end are both inclusive
360
+ var self = this;
361
+ if (!this.started) return callback( new Error("Storage has not completed startup.") );
362
+
363
+ key = this.normalizeKey( key );
364
+
365
+ if (!this.isBinaryKey(key)) {
366
+ return callback( new Error("Stream values are only allowed with keys containing file extensions, e.g. " + key + ".bin") );
367
+ }
368
+
369
+ this.engine.getStreamRange( key, start, end, callback );
370
+ },
371
+
357
372
  getMulti: function(keys, callback) {
358
373
  // fetch multiple records at once, given array of keys
359
374
  // callback is provided an array of values in matching order to keys
package/test/test-main.js CHANGED
@@ -375,7 +375,7 @@ module.exports = {
375
375
  },
376
376
 
377
377
  function testStream(test) {
378
- test.expect(11);
378
+ test.expect(14);
379
379
  var self = this;
380
380
 
381
381
  var key = 'spacer-stream.gif';
@@ -395,10 +395,13 @@ module.exports = {
395
395
  var tempFile = __dirname + '/' + filename + '.streamtemp';
396
396
  var outStream = fs.createWriteStream( tempFile );
397
397
 
398
- self.storage.getStream( key, function(err, storageStream) {
398
+ self.storage.getStream( key, function(err, storageStream, streamInfo) {
399
399
  test.ok( !err, "No error fetching stream: " + err );
400
400
  test.ok( !!storageStream, "Got storage stream as 2nd arg");
401
401
  test.ok( !!storageStream.pipe, "Storage stream has a pipe");
402
+ test.ok( !!streamInfo, "Info was provided as the 3rd arg");
403
+ test.ok( streamInfo.len == 43, "Info has correct data length");
404
+ test.ok( streamInfo.mod > 0, "Info has a non-zero mod date");
402
405
 
403
406
  outStream.on('finish', function() {
404
407
  var newSpacerBuf = fs.readFileSync( tempFile );
@@ -420,6 +423,52 @@ module.exports = {
420
423
  } ); // putStream
421
424
  },
422
425
 
426
+ function testStreamRange(test) {
427
+ test.expect(14);
428
+ var self = this;
429
+
430
+ var key = 'spacer-stream.gif';
431
+ var filename = 'spacer.gif';
432
+ var spacerBuf = fs.readFileSync( __dirname + '/' + filename );
433
+ var spacerStream = fs.createReadStream( __dirname + '/' + filename );
434
+
435
+ test.ok( !!spacerBuf, "Got buffer from file" );
436
+ test.ok( typeof(spacerBuf) == 'object', "Buffer is an object" );
437
+ test.ok( spacerBuf.length > 0, "Buffer has size" );
438
+ test.ok( !!spacerStream, "Got read stream" );
439
+
440
+ this.storage.putStream( key, spacerStream, function(err) {
441
+ test.ok( !err, "No error creating stream: " + err );
442
+
443
+ var tempFile = __dirname + '/' + filename + '.streamtemp';
444
+ var outStream = fs.createWriteStream( tempFile );
445
+
446
+ self.storage.getStreamRange( key, 0, 5, function(err, storageStream, streamInfo) {
447
+ test.ok( !err, "No error fetching stream: " + err );
448
+ test.ok( !!storageStream, "Got storage stream as 2nd arg");
449
+ test.ok( !!storageStream.pipe, "Storage stream has a pipe");
450
+ test.ok( !!streamInfo, "Info was provided as the 3rd arg");
451
+ test.ok( streamInfo.len == 43, "Info has correct data length");
452
+ test.ok( streamInfo.mod > 0, "Info has a non-zero mod date");
453
+
454
+ outStream.on('finish', function() {
455
+ var newSpacerBuf = fs.readFileSync( tempFile );
456
+ test.ok( newSpacerBuf.length == 6, "Stream length is correct" );
457
+ test.ok( newSpacerBuf.toString() == "GIF89a", "Range buffer content is correct" );
458
+
459
+ self.storage.delete( key, function(err) {
460
+ test.ok( !err, "No error deleting stream key: " + err );
461
+ fs.unlinkSync( tempFile );
462
+ test.done();
463
+ } ); // delete
464
+ } ); // stream finish
465
+
466
+ storageStream.pipe( outStream );
467
+
468
+ } ); // getStream
469
+ } ); // putStream
470
+ },
471
+
423
472
  function testPutMulti(test) {
424
473
  // test storing multiple keys at once
425
474
  test.expect(1);
package/transaction.js CHANGED
@@ -8,9 +8,9 @@ var Path = require("path");
8
8
  var cp = require("child_process");
9
9
  var os = require("os");
10
10
  var async = require('async');
11
- var mkdirp = require('mkdirp');
12
11
  var Class = require("pixl-class");
13
12
  var Tools = require("pixl-tools");
13
+ var mkdirp = Tools.mkdirp;
14
14
 
15
15
  // Transaction support is implemented as a mixin to Storage
16
16
  // Config Keys: