Skip to content

Commit c615224

Browse files
committed
refactor(preconditions): move file
1 parent f3d1e63 commit c615224

File tree

5 files changed

+49
-41
lines changed

5 files changed

+49
-41
lines changed

etag.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2023-latest the httpland authors. All rights reserved. MIT license.
2+
// This module is browser compatible.
3+
4+
// TODO(miyauci): External Packaging
5+
6+
import { type ETag } from "./deps.ts";
7+
8+
export interface StrongETag extends ETag {
9+
readonly weak: false;
10+
}
11+
12+
export function matchWeak(left: ETag, right: ETag): boolean {
13+
return left.tag === right.tag;
14+
}
15+
16+
export function matchStrong(left: ETag, right: ETag): boolean {
17+
return isStrongETag(left) && isStrongETag(right) && left.tag === right.tag;
18+
}
19+
20+
export function isStrongETag(etag: ETag): etag is StrongETag {
21+
return !etag.weak;
22+
}

preconditions/if_modified_since.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import {
1212
Status,
1313
unsafe,
1414
} from "../deps.ts";
15-
import { ifModifiedSince, isBannedHeader } from "./utils.ts";
15+
import { isBannedHeader } from "../utils.ts";
16+
import { ifModifiedSince } from "./utils.ts";
1617
import type { Precondition } from "../types.ts";
1718

1819
/** `If-Modified-Since` header field precondition. */

preconditions/if_none_match.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import {
1414
unsafe,
1515
} from "../deps.ts";
1616
import type { Precondition } from "../types.ts";
17-
import { ifNoneMatch, isBannedHeader } from "./utils.ts";
17+
import { ifNoneMatch } from "./utils.ts";
18+
import { isBannedHeader } from "../utils.ts";
1819

1920
/** `If-None-Match` header field precondition. */
2021
export class IfNoneMatch implements Precondition {

preconditions/utils.ts

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
// Copyright 2023-latest the httpland authors. All rights reserved. MIT license.
22
// This module is browser compatible.
33

4-
import {
5-
ETag,
6-
isString,
7-
isValidDate,
8-
parseETag,
9-
parseHttpDate,
10-
RepresentationHeader,
11-
} from "../deps.ts";
4+
import { isString, isValidDate, parseETag, parseHttpDate } from "../deps.ts";
125
import { parse } from "../if_match.ts";
6+
import { matchStrong, matchWeak } from "../etag.ts";
7+
8+
const enum Msg {
9+
InvalidField = "field value is invalid <HTTP-date> format.",
10+
InvalidLastModified = "last-modified is invalid <HTTP-date> format.",
11+
}
1312

1413
/** Match `If-Match` field and `ETag` field.
1514
* @throws {SyntaxError} If the input is invalid syntax.
@@ -38,37 +37,23 @@ export function ifNoneMatch(fieldValue: string, etag: string): boolean {
3837
return ifNoneMatch.every((tag) => !matchWeak(tag, etagObj));
3938
}
4039

41-
export function matchWeak(left: ETag, right: ETag): boolean {
42-
return left.tag === right.tag;
43-
}
44-
45-
export function matchStrong(left: ETag, right: ETag): boolean {
46-
return isStrong(left) && isStrong(right) && left.tag === right.tag;
47-
}
48-
49-
export function isStrong(etag: ETag): boolean {
50-
return !etag.weak;
51-
}
52-
5340
/**
5441
* @throws {SyntaxError} If the input is invalid.
5542
*/
5643
export function ifModifiedSince(
5744
fieldValue: string,
5845
lastModified: string,
5946
): boolean {
60-
// A recipient MUST ignore the If-Modified-Since header field if the
61-
// received field-value is not a valid HTTP-date
6247
const date = parseHttpDate(fieldValue.trim());
6348

6449
if (!isValidDate(date)) {
65-
throw TypeError("field value is invalid <HTTP-date> format.");
50+
throw TypeError(Msg.InvalidField);
6651
}
6752

6853
const lastMod = parseHttpDate(lastModified.trim());
6954

7055
if (!isValidDate(lastMod)) {
71-
throw TypeError("last-modified is invalid <HTTP-date> format.");
56+
throw TypeError(Msg.InvalidLastModified);
7257
}
7358

7459
// The origin server SHOULD NOT perform the requested
@@ -84,18 +69,16 @@ export function ifUnmodifiedSince(
8469
fieldValue: string,
8570
lastModified: string,
8671
): boolean {
87-
// A recipient MUST ignore the If-Modified-Since header field if the
88-
// received field-value is not a valid HTTP-date
8972
const date = parseHttpDate(fieldValue.trim());
9073

9174
if (!isValidDate(date)) {
92-
throw TypeError("field value is invalid <HTTP-date> format.");
75+
throw TypeError(Msg.InvalidField);
9376
}
9477

9578
const lastMod = parseHttpDate(lastModified.trim());
9679

9780
if (!isValidDate(lastMod)) {
98-
throw TypeError("last-modified is invalid <HTTP-date> format.");
81+
throw TypeError(Msg.InvalidLastModified);
9982
}
10083

10184
// The origin server MUST NOT perform the requested method
@@ -128,13 +111,13 @@ export function ifRange(fieldValue: string, headers: IfRangeHeaders): boolean {
128111
const left = parseHttpDate(fieldValue);
129112

130113
if (!isValidDate(left)) {
131-
throw TypeError("field value is invalid <HTTP-date> format.");
114+
throw TypeError(Msg.InvalidField);
132115
}
133116

134117
const right = parseHttpDate(lastModified);
135118

136119
if (!isValidDate(right)) {
137-
throw TypeError("last-modified is invalid <HTTP-date> format.");
120+
throw TypeError(Msg.InvalidLastModified);
138121
}
139122

140123
return left.getTime() === right.getTime();
@@ -149,13 +132,4 @@ function isStar(input: unknown): input is Star {
149132
return input === "*";
150133
}
151134

152-
export function isBannedHeader(fieldName: string): boolean {
153-
return ([
154-
RepresentationHeader.ContentEncoding,
155-
RepresentationHeader.ContentLanguage,
156-
RepresentationHeader.ContentLength,
157-
RepresentationHeader.ContentType,
158-
] as string[]).includes(fieldName);
159-
}
160-
161135
export type Star = "*";

utils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
isNull,
99
isSuccessfulStatus,
1010
Method,
11+
RepresentationHeader,
1112
Status,
1213
SuccessfulStatus,
1314
} from "./deps.ts";
@@ -79,3 +80,12 @@ export async function applyPrecondition(
7980

8081
return precondition.respond(request, response, evalResult);
8182
}
83+
84+
export function isBannedHeader(fieldName: string): boolean {
85+
return ([
86+
RepresentationHeader.ContentEncoding,
87+
RepresentationHeader.ContentLanguage,
88+
RepresentationHeader.ContentLength,
89+
RepresentationHeader.ContentType,
90+
] as string[]).includes(fieldName);
91+
}

0 commit comments

Comments
 (0)