Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
LiviaMedeiros committed Aug 19, 2023
1 parent 235dec5 commit 586ae3b
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 42 deletions.
4 changes: 2 additions & 2 deletions bin/poteto-cat.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import poteto from '../index.mjs?persistCwd=true';
const printResponse = async ({ ok, body }) => {
for await (const $ of body)
process[ok ? 'stdout' : 'stderr'].write($);
}
};

const [,,...urls] = process.argv;

Expand All @@ -24,7 +24,7 @@ const catOrder = async urls => {
for (const url of urls)
await poteto(url).then(printResponse);
//await printResponse(await poteto(url));
}
};

await catOrder(urls);
//await catConcurrent(urls);
65 changes: 32 additions & 33 deletions index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -96,37 +96,37 @@ const GET = async (url, { headers, integrity, signal }) => {

return ranges.length
? Promise.all(ranges.map(range => readRange(range, size, url)))
.then($ => validatedBody(integrity, $))
.then(
async body => [
body,
await statsAsOptions(
Object.assign(stats, { size: BigInt(body.byteLength) }),
{ status: 206 }
),
]
)
.then(responseConstructor)
.catch($ => $ instanceof RangeError
? genericResponse(416)
: Promise.reject($)
)
.then($ => validatedBody(integrity, $))
.then(
async body => [
body,
await statsAsOptions(
Object.assign(stats, { size: BigInt(body.byteLength) }),
{ status: 206 }
),
]
)
.then(responseConstructor)
.catch($ => $ instanceof RangeError
? genericResponse(416)
: Promise.reject($)
)
: Promise.all([
fs.readFile(url, { signal }).then($ => validatedBody(integrity, [$])),
statsAsOptions(stats),
]).then(responseConstructor);
fs.readFile(url, { signal }).then($ => validatedBody(integrity, [$])),
statsAsOptions(stats),
]).then(responseConstructor);
};

const HEAD = async url =>
statsAsOptions(fs.stat(url, STAT_OPTS)).then($ => new Response(null, $));

const PUT = async (url, { body, headers, signal }) => {
const [range] = getRanges(headers);
const [range] = getRanges(headers);

return range
? writeRange(range, url, body)
: fs.writeFile(url, body, { signal });
}
return range
? writeRange(range, url, body)
: fs.writeFile(url, body, { signal });
};

const DELETE = async url =>
fs.rm(url).then(() => genericResponse(204));
Expand Down Expand Up @@ -154,8 +154,8 @@ const methods = new Map([
['WRITE', async (url, { body, signal }) =>
body instanceof ReadableStream
? fs.open(url, 'w')
.then(fd => body.pipeTo(Writable.toWeb(fd.createWriteStream()), { signal }))
.then(() => genericResponse(201))
.then(fd => body.pipeTo(Writable.toWeb(fd.createWriteStream()), { signal }))
.then(() => genericResponse(201))
: genericResponse(422)],
['APPEND', async (url, { body, signal }) =>
body
Expand Down Expand Up @@ -187,14 +187,13 @@ const handler = {

return url.protocol === 'file:'
? executeRequest(url, request).catch(err =>
statsAsOptions(err).then(opts =>
opts.status
? Promise.resolve(/application\/json/.test(request.headers.get('Accept'))
? Response.json(err, opts)
: new Response(err.message, opts))
: Promise.reject(err)
)
)
statsAsOptions(err).then(opts =>
opts.status
? Promise.resolve(/application\/json/.test(request.headers.get('Accept'))
? Response.json(err, opts)
: new Response(err.message, opts))
: Promise.reject(err)
))
: Reflect.apply(target, thisArg, argumentsList);
},
};
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,8 @@
"bugs": {
"url": "https://github.com/LiviaMedeiros/poteto/issues"
},
"homepage": "https://github.com/LiviaMedeiros/poteto#readme"
"homepage": "https://github.com/LiviaMedeiros/poteto#readme",
"devDependencies": {
"eslint": "*"
}
}
6 changes: 3 additions & 3 deletions test/redirect.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ test('redirect', async () => {
text = await resp.text();
assert.strictEqual(text, '');
location = resp.headers.get('Location');
assert.strictEqual(location, new URL(`../testdir/redirect/target`, import.meta.url).href);
assert.strictEqual(location, new URL('../testdir/redirect/target', import.meta.url).href);
resp = await poteto(location, { redirect: 'manual' });
text = await resp.text();
assert.strictEqual(text, 'target content');
Expand All @@ -56,12 +56,12 @@ test('redirect', async () => {
text = await resp.text();
assert.strictEqual(text, '');
location = resp.headers.get('Location');
assert.strictEqual(location, new URL(`../testdir/redirect/link1`, import.meta.url).href);
assert.strictEqual(location, new URL('../testdir/redirect/link1', import.meta.url).href);
resp = await poteto(location, { redirect: 'manual' });
text = await resp.text();
assert.strictEqual(text, '');
location = resp.headers.get('Location');
assert.strictEqual(location, new URL(`../testdir/redirect/target`, import.meta.url).href);
assert.strictEqual(location, new URL('../testdir/redirect/target', import.meta.url).href);
resp = await poteto(location, { redirect: 'manual' });
text = await resp.text();
assert.strictEqual(text, 'target content');
Expand Down
6 changes: 3 additions & 3 deletions test/sequence.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const bodygen = async function*(n = 10, pause = 9) {
yield `${_i++}`;
await new Promise(_ => setTimeout(_, pause));
}
}
};

const validate = (actual, expected = {}, headers = {}) => {
assert.ok(actual instanceof Response, `${actual} is not Response`);
Expand All @@ -27,10 +27,10 @@ const validate = (actual, expected = {}, headers = {}) => {
assert.strictEqual(actual[key], value)
);

Object.entries(headers).forEach(([key, value]) =>
Object.entries(headers).forEach(([key, value]) =>
assert.strictEqual(actual.headers.get(key), value)
);
}
};

const filename = `deleteme-${Math.random()}`;

Expand Down

0 comments on commit 586ae3b

Please sign in to comment.