Skip to content

Commit

Permalink
feat: rename field name to rangeUnit from unit
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed Mar 22, 2023
1 parent 253386f commit 41aa7da
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 33 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,10 @@ protocols.

`Range` is the following structure:

| Name | Type | Description |
| ------- | --------- | ---------------------------------------------------------------------------------------- |
| unit | `string` | Corresponding range unit. |
| respond | `Respond` | Takes the context of a range request and handler response and return a partial response. |
| Name | Type | Description |
| --------- | -------------- | ---------------------------------------------------------------------------------------- |
| rangeUnit | `string` | Corresponding range unit. |
| respond | `RangeRespond` | Takes the context of a range request and handler response and return a partial response. |

The middleware supports the following range request protocols by default:

Expand Down
2 changes: 1 addition & 1 deletion deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ export {
type OtherRange,
parse,
type Range,
type RangeSet,
type RangeSpec,
type RangesSpecifier,
type SuffixRange,
} from "https://deno.land/x/range_parser@1.0.0/mod.ts";
export { concat } from "https://deno.land/std@0.180.0/bytes/concat.ts";
Expand Down
2 changes: 1 addition & 1 deletion middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function rangeRequest(
ranges?: Iterable<Range>,
): Middleware {
const $ranges = ranges ?? DefaultRanges;
const units = Array.from($ranges).map((range) => range.unit);
const units = Array.from($ranges).map((range) => range.rangeUnit);
const unitLike = isNotEmpty(units) ? units : RangeUnit.None;

const contentRangeMiddleware = contentRange($ranges);
Expand Down
4 changes: 2 additions & 2 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ export {
type IntRange,
type Middleware,
type OtherRange,
type RangeSet,
type RangeSpec,
type RangesSpecifier,
type SuffixRange,
} from "./deps.ts";
export {
type BytesContext,
BytesRange,
type ComputeBoundary,
} from "./ranges/bytes.ts";
export type { Range, RangeContext, Respond } from "./types.ts";
export type { Range, RangeContext, RangeRespond } from "./types.ts";
2 changes: 1 addition & 1 deletion ranges/bytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class BytesRange implements Range {
this.#boundary = options?.computeBoundary ?? digestSha1;
}

unit = RangeUnit.Bytes;
rangeUnit = RangeUnit.Bytes;

respond(context: RangeContext): Promise<Response> {
return respondPartial({ ...context, computeBoundary: this.#boundary });
Expand Down
2 changes: 1 addition & 1 deletion ranges/bytes_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,6 @@ describe("BytesRange", () => {
const bytesRange = new BytesRange();

it("should unit is bytes", () => {
assertEquals(bytesRange.unit, "bytes");
assertEquals(bytesRange.rangeUnit, "bytes");
});
});
4 changes: 2 additions & 2 deletions transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ export async function withContentRange(
}

const parsedRange = rangeContainer.value;
const matchedRange = Array.from(context.ranges).find(({ unit }) =>
unit === parsedRange.rangeUnit
const matchedRange = Array.from(context.ranges).find(({ rangeUnit }) =>
rangeUnit === parsedRange.rangeUnit
);
const body = await response.clone().arrayBuffer();

Expand Down
20 changes: 10 additions & 10 deletions transform_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe("withContentRange", () => {

const response = await withContentRange(initResponse, {
rangeValue: "",
ranges: [{ unit: "bytes", respond }],
ranges: [{ rangeUnit: "bytes", respond }],
});

assert(response === initResponse);
Expand All @@ -33,7 +33,7 @@ describe("withContentRange", () => {

const response = await withContentRange(initResponse, {
rangeValue: "",
ranges: [{ unit: "bytes", respond }],
ranges: [{ rangeUnit: "bytes", respond }],
});

assert(response === initResponse);
Expand All @@ -47,7 +47,7 @@ describe("withContentRange", () => {

const response = await withContentRange(initResponse, {
rangeValue: "",
ranges: [{ unit: "bytes", respond }],
ranges: [{ rangeUnit: "bytes", respond }],
});

assert(response === initResponse);
Expand All @@ -63,7 +63,7 @@ describe("withContentRange", () => {

const response = await withContentRange(initResponse, {
rangeValue: "",
ranges: [{ unit: "bytes", respond }],
ranges: [{ rangeUnit: "bytes", respond }],
});

assert(response === initResponse);
Expand All @@ -78,7 +78,7 @@ describe("withContentRange", () => {

const response = await withContentRange(initResponse, {
rangeValue: "",
ranges: [{ unit: "bytes", respond }],
ranges: [{ rangeUnit: "bytes", respond }],
});

assert(response === initResponse);
Expand All @@ -90,7 +90,7 @@ describe("withContentRange", () => {

const response = await withContentRange(initResponse, {
rangeValue: "",
ranges: [{ unit: "bytes", respond }],
ranges: [{ rangeUnit: "bytes", respond }],
});

assert(response === initResponse);
Expand All @@ -105,7 +105,7 @@ describe("withContentRange", () => {
{
rangeValue: "bytes=0-",
ranges: [{
unit: "xxx",
rangeUnit: "xxx",
respond,
}],
},
Expand All @@ -132,7 +132,7 @@ describe("withContentRange", () => {
initResponse,
{
rangeValue: "bytes=0-",
ranges: [{ unit: "bytes", respond }],
ranges: [{ rangeUnit: "bytes", respond }],
},
);

Expand Down Expand Up @@ -175,8 +175,8 @@ describe("withAcceptRanges", () => {
],
];

await Promise.all(table.map(async ([initResponse, unit, expected]) => {
const response = withAcceptRanges(initResponse, unit);
await Promise.all(table.map(async ([initResponse, rangeUnit, expected]) => {
const response = withAcceptRanges(initResponse, rangeUnit);

assert(
await equalsResponse(response, expected, true),
Expand Down
17 changes: 6 additions & 11 deletions types.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
// Copyright 2023-latest the httpland authors. All rights reserved. MIT license.
// This module is browser compatible.

import { type RangeSet } from "./deps.ts";
import { type RangesSpecifier } from "./deps.ts";

/** Range API. */
export interface Range {
/** Corresponding range unit. */
readonly unit: string;
readonly rangeUnit: string;

/** Takes the context of a range request and handler response and return a partial response. */
readonly respond: Respond;
readonly respond: RangeRespond;
}

export interface Respond {
/** Range responding API. */
export interface RangeRespond {
(context: RangeContext): Response | Promise<Response>;
}

/** Context of range request and response. */
export interface RangeContext {
/** Corresponding range unit. */
readonly rangeUnit: string;

/** Representation of [`<range-set>`](https://www.rfc-editor.org/rfc/rfc9110#range.specifiers). */
readonly rangeSet: RangeSet;

export interface RangeContext extends RangesSpecifier {
/** Full response content. */
readonly content: ArrayBuffer;

Expand Down

0 comments on commit 41aa7da

Please sign in to comment.