Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for binary files #42

Merged
merged 12 commits into from
Oct 23, 2018
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 4 additions & 2 deletions lib/CachedSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

- new Buffer
+ Buffer.from

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried that, but that causes the tests to fail for pre node 5.10. Buffer.from.length checks if it supports Buffer.from and if it is equal to 1, it doesn't

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And if it is after 5.10, it supports Buffer.byteLength.

return this._cachedSize = Buffer.byteLength(this._cachedSource);
}
return this._cachedSize = this._source.size();
}

Expand Down
3 changes: 2 additions & 1 deletion lib/Source.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class Source {
}

size() {
return this.source().length;
if(Buffer.from.length === 1) return new Buffer(this.source()).length;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

- new Buffer
+ Buffer.from

return Buffer.byteLength(this.source())
}

map(options) {
Expand Down
56 changes: 56 additions & 0 deletions test/CachedSource.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
10 changes: 10 additions & 0 deletions test/OriginalSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});