Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

Commit

Permalink
add last-modified/if-modified-since cache header processing
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksey Smolenchuk authored and othiym23 committed Jan 16, 2015
1 parent 4701a29 commit 07bc335
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Deprecate a version of a package in the registry.
* `cb` {Function}

Fetches data from the registry via a GET request, saving it in the cache folder
with the ETag.
with the ETag or the "Last Modified" timestamp.

### client.publish(uri, params, cb)

Expand Down Expand Up @@ -182,6 +182,7 @@ caching logic directly.
that are not Buffers or Streams are encoded as JSON. Optional – body
only used for write operations.
* `etag` {String} The cached ETag. Optional.
* `lastModified` {String} The cached Last-Modified timestamp. Optional.
* `follow` {Boolean} Follow 302/301 responses. Optional (default: true).
* `auth` {Credentials} Optional.
* `cb` {Function}
Expand Down
10 changes: 10 additions & 0 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ function makeRequest (uri, params, cb_) {
headers[params.method === "GET" ? "if-none-match" : "if-match"] = params.etag
}

if (params.lastModified && params.method === "GET") {
this.log.verbose("lastModified", params.lastModified)
headers["if-modified-since"] = params.lastModified;
}

// figure out wth body is
if (params.body) {
if (Buffer.isBuffer(params.body)) {
Expand All @@ -139,6 +144,7 @@ function makeRequest (uri, params, cb_) {
}
else {
delete params.body._etag
delete params.body._lastModified
opts.json = params.body
}
}
Expand Down Expand Up @@ -220,6 +226,10 @@ function requestDone (method, where, cb) {
parsed._etag = response.headers.etag
}

if (parsed && response.headers['last-modified']) {
parsed._lastModified = response.headers['last-modified']
}

// for the search endpoint, the "error" property can be an object
if (parsed && parsed.error && typeof parsed.error !== "object" ||
response.statusCode >= 400) {
Expand Down
16 changes: 15 additions & 1 deletion test/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ test("request call contract", function (t) {
})

test("run request through its paces", function (t) {
t.plan(24)
t.plan(27)

server.expect("/request-defaults", function (req, res) {
t.equal(req.method, "GET", "uses GET by default")
Expand All @@ -94,6 +94,14 @@ test("run request through its paces", function (t) {
}))
})

server.expect("/last-modified", function (req, res) {
t.equal(req.headers["if-modified-since"], "test-last-modified",
"got test if-modified-since")

res.statusCode = 200
res.json({ fetched : "last-modified" })
})

server.expect("/etag", function (req, res) {
t.equal(req.headers["if-none-match"], "test-etag", "got test etag")

Expand Down Expand Up @@ -169,6 +177,12 @@ test("run request through its paces", function (t) {
}
)

var lastModified = { lastModified : "test-last-modified" }
client.request(common.registry+"/last-modified", lastModified, function (er, data) {
t.ifError(er, "call worked")
t.deepEquals(data, { fetched : "last-modified" }, "last-modified request sent")
})

var etagged = { etag : "test-etag" }
client.request(common.registry+"/etag", etagged, function (er, data) {
t.ifError(er, "call worked")
Expand Down

0 comments on commit 07bc335

Please sign in to comment.