Skip to content

Commit

Permalink
Fixing a regression of hash() such that the mimetype parameter wa…
Browse files Browse the repository at this point in the history
…sn't used in latest iteration, updating README.md, reformatting imports
  • Loading branch information
avoidwork committed Oct 6, 2023
1 parent 93686ab commit ab8f7cf
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 22 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ All files | 100 | 78.57 | 100 | 100 |
### etag({cacheSize: 1000, cacheTTL: 0, seed: random, mimetype: "text/plain"})
Returns an tiny-etag instance. Cache TTL concerns do not spread with a notification.

### create(arg)
Creates a strong ETag value from `arg`; a composite `String` is recommended
### create(arg = ""[, mimetype = "text/plain"])
Creates a strong ETag value from `arg`; a composite `String` is recommended. It's ideal to

### hash(arg[, mimetype="text/plain"])
### hash(arg = ""[, mimetype="text/plain"])
Creates a hash of `arg`, uses `create()`

### keep(arg)
Expand Down
11 changes: 7 additions & 4 deletions dist/tiny-etag.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* @copyright 2023 Jason Mulligan <jason.mulligan@avoidwork.com>
* @license BSD-3-Clause
* @version 4.0.0
* @version 4.0.1
*/
'use strict';

Expand Down Expand Up @@ -33,6 +33,7 @@ const NO_CACHE = "no-cache";
const NO_STORE = "no-store";
const PRIVATE = "private";
const MAX_AGE_0 = "max-age=0";
const STRING = "string";

class ETag {
constructor (cacheSize, cacheTTL, mimetype) {
Expand All @@ -44,8 +45,10 @@ class ETag {
return `"${this.hash(arg, mimetype)}"`;
}

hash (arg = "") {
return node_crypto.createHash(SHA1).update(arg).digest(BASE64);
hash (arg = EMPTY, mimetype = this.mimetype) {
const input = `${typeof arg === STRING ? arg : JSON.stringify(arg)}_${mimetype}`;

return node_crypto.createHash(SHA1).update(input).digest(BASE64);
}

keep (arg) {
Expand Down Expand Up @@ -86,7 +89,7 @@ class ETag {
}

parse (arg) {
return new URL(typeof arg === "string" ? arg : `http://${arg.headers.host || `localhost:${arg.socket.server._connectionKey.replace(/.*::/, "")}`}${arg.url}`);
return new URL(typeof arg === STRING ? arg : `http://${arg.headers.host || `localhost:${arg.socket.server._connectionKey.replace(/.*::/, "")}`}${arg.url}`);
}

register (key, arg) {
Expand Down
13 changes: 8 additions & 5 deletions dist/tiny-etag.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* @copyright 2023 Jason Mulligan <jason.mulligan@avoidwork.com>
* @license BSD-3-Clause
* @version 4.0.0
* @version 4.0.1
*/
import {lru}from'tiny-lru';import {createHash}from'node:crypto';const BASE64 = "base64";
const SHA1 = "sha1";
Expand All @@ -27,7 +27,8 @@ const EMPTY = "";
const NO_CACHE = "no-cache";
const NO_STORE = "no-store";
const PRIVATE = "private";
const MAX_AGE_0 = "max-age=0";class ETag {
const MAX_AGE_0 = "max-age=0";
const STRING = "string";class ETag {
constructor (cacheSize, cacheTTL, mimetype) {
this.cache = lru(cacheSize, cacheTTL);
this.mimetype = mimetype;
Expand All @@ -37,8 +38,10 @@ const MAX_AGE_0 = "max-age=0";class ETag {
return `"${this.hash(arg, mimetype)}"`;
}

hash (arg = "") {
return createHash(SHA1).update(arg).digest(BASE64);
hash (arg = EMPTY, mimetype = this.mimetype) {
const input = `${typeof arg === STRING ? arg : JSON.stringify(arg)}_${mimetype}`;

return createHash(SHA1).update(input).digest(BASE64);
}

keep (arg) {
Expand Down Expand Up @@ -79,7 +82,7 @@ const MAX_AGE_0 = "max-age=0";class ETag {
}

parse (arg) {
return new URL(typeof arg === "string" ? arg : `http://${arg.headers.host || `localhost:${arg.socket.server._connectionKey.replace(/.*::/, "")}`}${arg.url}`);
return new URL(typeof arg === STRING ? arg : `http://${arg.headers.host || `localhost:${arg.socket.server._connectionKey.replace(/.*::/, "")}`}${arg.url}`);
}

register (key, arg) {
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tiny-etag",
"version": "4.0.0",
"version": "4.0.1",
"description": "ETag middleware for express.js API compatible routers",
"type": "module",
"types": "types/etag.d.ts",
Expand Down
1 change: 1 addition & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ export const PRIVATE = "private";
export const PUBLIC = "public";
export const CONTENT_TYPE = "content-type";
export const MAX_AGE_0 = "max-age=0";
export const STRING = "string";
20 changes: 15 additions & 5 deletions src/etag.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import {lru} from "tiny-lru";
import {createHash} from "node:crypto";
import {
BASE64,
CACHE_CONTROL,
CONTENT_LOCATION,
DATE,
EMPTY,
ETAG,
EXPIRES,
FINISH,
GET,
IF_NONE_MATCH,
Expand All @@ -12,11 +16,15 @@ import {
INT_200,
INT_304,
INT_DETAULT_CACHE,
PRIVATE,
MAX_AGE_0,
NO_CACHE,
NO_STORE,
PRIVATE,
RANGE,
TEXT_PLAIN, SHA1, BASE64, CONTENT_LOCATION, DATE, EXPIRES, VARY, MAX_AGE_0
SHA1,
STRING,
TEXT_PLAIN,
VARY
} from "./constants.js";

export class ETag {
Expand All @@ -29,8 +37,10 @@ export class ETag {
return `"${this.hash(arg, mimetype)}"`;
}

hash (arg = "") {
return createHash(SHA1).update(arg).digest(BASE64);
hash (arg = EMPTY, mimetype = this.mimetype) {
const input = `${typeof arg === STRING ? arg : JSON.stringify(arg)}_${mimetype}`;

return createHash(SHA1).update(input).digest(BASE64);
}

keep (arg) {
Expand Down Expand Up @@ -71,7 +81,7 @@ export class ETag {
}

parse (arg) {
return new URL(typeof arg === "string" ? arg : `http://${arg.headers.host || `localhost:${arg.socket.server._connectionKey.replace(/.*::/, "")}`}${arg.url}`);
return new URL(typeof arg === STRING ? arg : `http://${arg.headers.host || `localhost:${arg.socket.server._connectionKey.replace(/.*::/, "")}`}${arg.url}`);
}

register (key, arg) {
Expand Down
9 changes: 7 additions & 2 deletions test/etag.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ import assert from "node:assert/strict";
import {httptest} from "tiny-httptest";
import {woodland} from "woodland";
import {etag} from "../dist/tiny-etag.cjs";
import {CONTENT_TYPE, CACHE_CONTROL, INT_1000, INT_200, NO_CACHE, TEXT_PLAIN, PUBLIC} from "../src/constants.js";
import {CACHE_CONTROL, CONTENT_TYPE, INT_1000, INT_200, NO_CACHE, PUBLIC, TEXT_PLAIN} from "../src/constants.js";

const cacheSize = INT_1000,
router = woodland({etags: false, logging: {enabled: false}, defaultHeaders: {[CONTENT_TYPE]: TEXT_PLAIN, [CACHE_CONTROL]: PUBLIC}, cacheSize: cacheSize}),
router = woodland({
etags: false,
logging: {enabled: false},
defaultHeaders: {[CONTENT_TYPE]: TEXT_PLAIN, [CACHE_CONTROL]: PUBLIC},
cacheSize: cacheSize
}),
etagStore = etag({cacheSize: cacheSize}),
msg = "Hello World!",
etagStoreValue = etagStore.create(msg);
Expand Down

0 comments on commit ab8f7cf

Please sign in to comment.