2018-12-05 03:36:06 +00:00
|
|
|
const MemoryProvider = require('../../src/providers/memory');
|
|
|
|
const { parseBJSON } = require('../lib/test-utils');
|
|
|
|
|
2018-12-05 15:09:17 +00:00
|
|
|
class SerializableMemoryProvider extends MemoryProvider {
|
2018-12-05 03:36:06 +00:00
|
|
|
|
|
|
|
constructor(name, jsonImage) {
|
|
|
|
super(name);
|
|
|
|
this.unparsedJSONImage = jsonImage;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* In addition to the usual setup of a Memory provider,
|
|
|
|
* also parse and overwrite the internal database.
|
|
|
|
*/
|
|
|
|
open(callback) {
|
|
|
|
super.open(err => {
|
|
|
|
if(err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
2018-12-07 03:22:18 +00:00
|
|
|
// If we don't have an image to import, leave db as is
|
|
|
|
if(!this.unparsedJSONImage) {
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to import the fs image from JSON
|
2018-12-05 03:36:06 +00:00
|
|
|
try {
|
|
|
|
this.db = parseBJSON(this.unparsedJSONImage);
|
|
|
|
this.unparsedJSONImage = null;
|
|
|
|
callback();
|
|
|
|
} catch(e) {
|
|
|
|
callback(new Error(`unable to parse JSON filesystem image: ${e.message}`));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export() {
|
|
|
|
return JSON.stringify(this.db);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 15:09:17 +00:00
|
|
|
module.exports = SerializableMemoryProvider;
|