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

[FIX] Resource: Keep stats size up to date #253

Merged
merged 5 commits into from
Jun 26, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion lib/Resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ class Resource {
this._stream = stream || null;
this._buffer = buffer || null;
Copy link
Member

Choose a reason for hiding this comment

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

This line can be removed

Copy link
Member

Choose a reason for hiding this comment

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

Done

if (typeof string === "string" || string instanceof String) {
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't we rather call setString / setBuffer in the constructor?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

this._buffer = Buffer.from(string, "utf8");
this.setString(string);
} else if (this._buffer) {
this.setBuffer(this._buffer);
}

// Tracing:
Expand Down Expand Up @@ -117,6 +119,7 @@ class Resource {
this._buffer = buffer;
this._contentDrained = false;
this._streamDrained = false;
this._setSize(Buffer.byteLength(this._buffer));
Copy link
Member

Choose a reason for hiding this comment

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

Buffer.byteLength just returns the byteLength from the buffer

Suggested change
this._setSize(Buffer.byteLength(this._buffer));
this._setSize(this._buffer.byteLength);

Copy link
Member

Choose a reason for hiding this comment

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

Done

}

/**
Expand Down Expand Up @@ -207,6 +210,17 @@ class Resource {
this._streamDrained = false;
}

/**
* Set the size in bytes
*
* @param {number} size byte size
* @private
* @see https://nodejs.org/api/fs.html#fs_stats_size
*/
_setSize(size) {
this._statInfo.size = size;
}

/**
* Gets the resources path
*
Expand Down
65 changes: 65 additions & 0 deletions test/lib/Resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const test = require("ava");
const Stream = require("stream");
const fs = require("fs");
const path = require("path");
const {promisify} = require("util");
const stat = promisify(fs.stat);
const Resource = require("../../lib/Resource");

function createBasicResource() {
Expand Down Expand Up @@ -142,6 +144,58 @@ test("Resource: setString", (t) => {
});
});

test("Resource: size modification", async (t) => {
const resource = new Resource({
path: "my/path/to/resource"
});
t.falsy(resource.getStatInfo().size, "initial size is not defined");

// string
resource.setString("Content");

t.is(resource.getStatInfo().size, 7, "size after manually setting the string");
t.is(new Resource({
path: "my/path/to/resource",
string: "Content"
}).getStatInfo().size, 7, "size when passing string to constructor");


// buffer
resource.setBuffer(Buffer.from("Super"));

t.is(resource.getStatInfo().size, 5, "size after manually setting the string");

const clonedResource1 = await resource.clone();
t.is(clonedResource1.getStatInfo().size, 5, "size after cloning the resource");


// buffer with alloc
const buf = Buffer.alloc(1234);
buf.write("some string", 0, "utf8");
resource.setBuffer(buf);

t.is(resource.getStatInfo().size, 1234, "buffer with alloc after setting the buffer");
t.is(new Resource({
path: "my/path/to/resource",
buffer: buf
}).getStatInfo().size, 1234, "buffer with alloc when passing buffer to constructor");

const clonedResource2 = await resource.clone();
t.is(clonedResource2.getStatInfo().size, 1234, "buffer with alloc atfer clone");
});

test("Resource: _setSize", (t) => {
t.plan(1);

const resource = new Resource({
path: "my/path/to/resource"
});

resource._setSize(1337);

t.is(resource.getStatInfo().size, 1337);
});

Copy link
Member

Choose a reason for hiding this comment

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

We should also add test cases when passing a real statInfo to the constructor (from fs.stat) and then changing the content. This is the use case when a resource is created by the FsAdapter.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

test("Resource: setStream", (t) => {
t.plan(1);

Expand Down Expand Up @@ -267,3 +321,14 @@ test("getBuffer from Stream content: Subsequent content requests should not thro
// to throw "Content stream of Resource /app/index.html is flagged as drained."
await t.notThrowsAsync(p2);
});

test("integration stat - resource size", async (t) => {
const fsPath = path.join("test", "fixtures", "application.a", "webapp", "index.html");
const statInfo = await stat(fsPath);

const resource = new Resource({path: fsPath, statInfo});
t.is(resource.getStatInfo().size, 91);

resource.setString("myvalue");
t.is(resource.getStatInfo().size, 7);
});