diff --git a/README.md b/README.md index 4da3c34..8b45781 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ All methods should be considered as expensive as they may need to do computation #### `source` ``` js -Source.prototype.source() -> String +Source.prototype.source() -> String | ArrayBuffer ``` Returns the represented source code as string. diff --git a/lib/CachedSource.js b/lib/CachedSource.js index 940c4d9..08ca510 100644 --- a/lib/CachedSource.js +++ b/lib/CachedSource.js @@ -30,8 +30,10 @@ class CachedSource extends Source { size() { if(typeof this._cachedSize !== "undefined") return this._cachedSize; - if(typeof this._cachedSource !== "undefined") - return this._cachedSize = this._cachedSource.length; + if(typeof this._cachedSource !== "undefined") { + if(Buffer.from.length === 1) return new Buffer(this._cachedSource).length; + return this._cachedSize = Buffer.byteLength(this._cachedSource); + } return this._cachedSize = this._source.size(); } diff --git a/lib/Source.js b/lib/Source.js index dccf737..f9b8f6b 100644 --- a/lib/Source.js +++ b/lib/Source.js @@ -14,7 +14,8 @@ class Source { } size() { - return this.source().length; + if(Buffer.from.length === 1) return new Buffer(this.source()).length; + return Buffer.byteLength(this.source()) } map(options) { diff --git a/test/CachedSource.js b/test/CachedSource.js new file mode 100644 index 0000000..a622a0c --- /dev/null +++ b/test/CachedSource.js @@ -0,0 +1,56 @@ +var should = require("should"); +var CachedSource = require("../lib/CachedSource"); +var OriginalSource = require('../lib/OriginalSource'); + +describe("CachedSource", function() { + it("should return the correct size for binary files", function() { + var source = new OriginalSource(new ArrayBuffer(256), "file.wasm"); + var cachedSource = new CachedSource(source); + + cachedSource.size().should.be.eql(256); + cachedSource.size().should.be.eql(256); + }); + + it("should return the correct size for cached binary sources", function() { + var source = new OriginalSource(new ArrayBuffer(256), "file.wasm"); + var cachedSource = new CachedSource(source); + + cachedSource.source(); + cachedSource.size().should.be.eql(256); + cachedSource.size().should.be.eql(256); + }); + + it("should return the correct size for text files", function() { + var source = new OriginalSource("TestTestTest", "file.js"); + var cachedSource = new CachedSource(source); + + cachedSource.size().should.be.eql(12); + cachedSource.size().should.be.eql(12); + }); + + it("should return the correct size for cached text files", function() { + var source = new OriginalSource("TestTestTest", "file.js"); + var cachedSource = new CachedSource(source); + + cachedSource.source(); + cachedSource.size().should.be.eql(12); + cachedSource.size().should.be.eql(12); + }); + + it("should return the correct size for unicode files", function() { + var source = new OriginalSource("😋", "file.js"); + var cachedSource = new CachedSource(source); + + cachedSource.size().should.be.eql(4); + cachedSource.size().should.be.eql(4); + }); + + it("should return the correct size for cached unicode files", function() { + var source = new OriginalSource("😋", "file.js"); + var cachedSource = new CachedSource(source); + + cachedSource.source(); + cachedSource.size().should.be.eql(4); + cachedSource.size().should.be.eql(4); + }); +}); diff --git a/test/OriginalSource.js b/test/OriginalSource.js index 9e2a591..08ecf47 100644 --- a/test/OriginalSource.js +++ b/test/OriginalSource.js @@ -56,4 +56,14 @@ describe("OriginalSource", function() { resultMap.mappings.should.be.eql("AAAA;AACA;AACA"); }); + + it("should return the correct size for binary files", function() { + var source = new OriginalSource(new ArrayBuffer(256), "file.wasm"); + source.size().should.be.eql(256); + }); + + it("should return the correct size for unicode files", function() { + var source = new OriginalSource("😋", "file.js"); + source.size().should.be.eql(4); + }); });