Skip to content

Commit

Permalink
move writeRange to lib/range
Browse files Browse the repository at this point in the history
  • Loading branch information
LiviaMedeiros committed Sep 5, 2023
1 parent 81c517f commit 9fdf7bf
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
9 changes: 3 additions & 6 deletions lib/methods.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from './constants.mjs';
import {
readRanges,
writeByteRange,
writeRange,
} from './range.mjs';

const DELETE = async url =>
Expand All @@ -26,7 +26,7 @@ const GET = async (url, { headers, integrity, signal }) => {
if (conditionalResponse)
return conditionalResponse;

return readRanges(url, { headers, integrity, signal }, stats);
return readRanges(url, { integrity, signal }, getRanges(headers), stats);
};

const HEAD = async url =>
Expand All @@ -38,10 +38,7 @@ const POSTorPUT = async (url, { body, headers, method }) => {

const flag = method === 'POST' ? 'r+' : 'w+';

const { unit, ranges: [range] } = getRanges(headers);
return unit === 'none' || unit === 'bytes'
? writeByteRange(url, range ?? '-', body, flag).then(() => genericResponse(201))
: genericResponse(416);
return writeRange(url, { body }, getRanges(headers), flag);
};

const methods = new Map([
Expand Down
13 changes: 7 additions & 6 deletions lib/range.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
} from './sri.mjs';
import {
genericResponse,
getRanges,
statsAsOptions,
} from './http.mjs';

Expand Down Expand Up @@ -48,9 +47,7 @@ const readByteRanges = async (url, ranges, size, fd) => {
}
};

const readRanges = async (url, { headers, integrity, signal }, stats) => {
const { unit, ranges } = getRanges(headers);

const readRanges = async (url, { integrity, signal }, { unit, ranges }, stats) => {
switch (unit) {
case 'bytes':
return readByteRanges(url, ranges, stats.size)
Expand Down Expand Up @@ -101,8 +98,12 @@ const writeByteRange = async (url, range, body, flag) => {
}
};

const writeRange = async (url, { body }, { unit, ranges: [ range ] }, flag) =>
unit === 'none' || unit === 'bytes'
? writeByteRange(url, range ?? '-', body, flag).then(() => genericResponse(201))
: genericResponse(416);

export {
readByteRanges,
readRanges,
writeByteRange,
writeRange,
};

0 comments on commit 9fdf7bf

Please sign in to comment.