pixl-server-storage 3.0.8 → 3.0.9

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,7 @@ 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
228
  } );
229
229
  },
230
230
 
@@ -250,7 +250,7 @@ module.exports = Class.create({
250
250
 
251
251
  var range = buf.slice(start, end + 1);
252
252
  var stream = new BufferStream(range);
253
- callback(null, stream);
253
+ callback(null, stream, { mod: 1, len: buf.length });
254
254
  } );
255
255
  },
256
256
 
@@ -449,7 +449,10 @@ module.exports = Class.create({
449
449
  // create read stream
450
450
  var inp = fs.createReadStream( file );
451
451
 
452
- callback( null, inp );
452
+ callback( null, inp, {
453
+ mod: Math.floor(stats.mtime.getTime() / 1000),
454
+ len: stats.size
455
+ } );
453
456
  } );
454
457
  },
455
458
 
@@ -479,7 +482,10 @@ module.exports = Class.create({
479
482
  // create read stream
480
483
  var inp = fs.createReadStream( file, { start, end } );
481
484
 
482
- callback( null, inp );
485
+ callback( null, inp, {
486
+ mod: Math.floor(stats.mtime.getTime() / 1000),
487
+ len: stats.size
488
+ } );
483
489
  } );
484
490
  },
485
491
 
package/engines/Redis.js CHANGED
@@ -227,7 +227,7 @@ 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
231
  } );
232
232
  },
233
233
 
@@ -254,7 +254,7 @@ module.exports = Class.create({
254
254
 
255
255
  var range = buf.slice(start, end + 1);
256
256
  var stream = new BufferStream(range);
257
- callback(null, stream);
257
+ callback(null, stream, { mod: 1, len: buf.length });
258
258
  } );
259
259
  },
260
260
 
@@ -244,7 +244,7 @@ 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
248
  } );
249
249
  },
250
250
 
@@ -271,7 +271,7 @@ module.exports = Class.create({
271
271
 
272
272
  var range = buf.slice(start, end + 1);
273
273
  var stream = new BufferStream(range);
274
- callback(null, stream);
274
+ callback(null, stream, { mod: 1, len: buf.length });
275
275
  } );
276
276
  },
277
277
 
package/engines/S3.js CHANGED
@@ -294,33 +294,38 @@ 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
  } );
322
-
323
- callback( null, download );
324
329
  }); // headObject
325
330
  },
326
331
 
@@ -333,34 +338,38 @@ module.exports = Class.create({
333
338
  this.logDebug(9, "Fetching ranged S3 stream: " + key, { start, end });
334
339
 
335
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
+ } );
336
353
 
337
354
  // double-callback protection (bug in aws-sdk)
338
355
  var done = false;
339
- this.s3.headObject( params, function(err) {
356
+ this.s3.headObject( params, function(err, data) {
340
357
  if (done) return; else done = true;
341
358
 
342
359
  if (err) {
343
360
  if (err.code != 'NoSuchKey') {
344
361
  self.logError('s3', "Failed to head key: " + key + ": " + err.message);
345
362
  }
363
+ download.destroy();
346
364
  callback( err, null );
347
365
  return;
348
366
  }
349
367
 
350
- params.Range = "bytes=" + start + "-" + end;
351
- var download = self.s3.getObject(params).createReadStream();
352
-
353
- download.once('error', function(err) {
354
- self.logError('s3', "Failed to download key: " + key + ": " + err.message);
355
- });
356
- download.once('end', function() {
357
- self.logDebug(9, "S3 stream download complete: " + key);
358
- } );
359
- download.once('close', function() {
360
- self.logDebug(9, "S3 stream download closed: " + key);
368
+ proceed = true;
369
+ callback( null, download, {
370
+ mod: Math.floor((new Date(data.LastModified)).getTime() / 1000),
371
+ len: data.ContentLength
361
372
  } );
362
-
363
- callback( null, download );
364
373
  }); // headObject
365
374
  },
366
375
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pixl-server-storage",
3
- "version": "3.0.8",
3
+ "version": "3.0.9",
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",
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);