-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
const {expect} = require("chai"); | ||
const {InMemoryVFS} = require("../vfs"); | ||
const {MemoryWritable, promisePiped} = require("../streams"); | ||
|
||
function abstractVFSBehavior( VFS, name ) { | ||
describe(name + " acts like a VFS", function () { | ||
describe("Given a file which does not exist", function(){ | ||
it("when queried it does not exist", async function(){ | ||
const sut = new VFS(); | ||
expect(await sut.exists("nonexistent-file")).to.eq(false); | ||
}); | ||
}); | ||
|
||
describe("When creating a file as bytes", function(){ | ||
beforeEach(async function(){ | ||
this.sut = new VFS(); | ||
this.fileName = "devision"; | ||
this.fileContents = "where do we go from here, stop and shed a tear"; | ||
await this.sut.putBytes(this.fileName, this.fileContents, this.fileContents.encoding); | ||
}); | ||
|
||
it("is readable as a stream", async function(){ | ||
const istream = await this.sut.createReadableStream(this.fileName); | ||
//TODO: The following should really be abstracted into a "promise to read all bytes" thing | ||
const sink = new MemoryWritable(); | ||
await promisePiped(istream,sink); | ||
expect(sink.bytes.toString("utf-8")).to.deep.eq(this.fileContents); | ||
}); | ||
|
||
it("is readable as bytes", async function(){ | ||
const bytes = await this.sut.asBytes(this.fileName); | ||
expect(bytes.toString("utf-8")).to.deep.eq(this.fileContents); | ||
}); | ||
it("can not mutate returned byte buffer", async function(){ | ||
const bytes1 = await this.sut.asBytes(this.fileName); | ||
bytes1.writeUInt32LE(51); | ||
const bytes2 = await this.sut.asBytes(this.fileName); | ||
expect(bytes2.toString("utf-8")).to.deep.eq(this.fileContents); | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
abstractVFSBehavior(InMemoryVFS, "InMemoryVFS"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* @module vfs | ||
* | ||
* Virtual File System | ||
* | ||
* Provides a framework to abstract out operating against the local file system or a remote file system. This is useful | ||
* in testing, distributed portability, or vendor independence. | ||
*/ | ||
const { | ||
MemoryReadable, | ||
MemoryWritable | ||
} = require("./streams"); | ||
|
||
class InMemoryVFS { | ||
constructor() { | ||
this.files = {}; | ||
} | ||
|
||
async exists( file ){ | ||
return !!this.files[file]; | ||
} | ||
|
||
async unlink( file ){ | ||
if( this.files[file] ){ | ||
delete this.files[file]; | ||
} | ||
} | ||
|
||
async createReadableStream( file ){ | ||
if( !(await this.exists(file)) ){ | ||
throw new Error("No such file "+ file); | ||
} | ||
return new MemoryReadable(this.files[file]); | ||
} | ||
|
||
async asBytes( file ){ | ||
return Buffer.from(this.files[file]); | ||
} | ||
|
||
async putBytes( file, bytes, encoding ){ | ||
this.files[file] = Buffer.from(bytes, encoding); | ||
} | ||
|
||
async createWritableStream( file ){ | ||
const writable = new MemoryWritable(); | ||
writable.on("finish", () => { | ||
this.files[file] = writable.bytes; | ||
}); | ||
return writable; | ||
} | ||
} | ||
|
||
module.exports = { | ||
InMemoryVFS | ||
} |