Skip to content

Commit 17fe873

Browse files
committed
feat: alignment 📐
1 parent ec28497 commit 17fe873

11 files changed

+141
-96
lines changed

.mocharc.yml

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
exit: true
12
ignore:
23
- test/**/fixtures/**
34
require: test/fixtures/index.mjs
5+
recursive: true

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ console.log(res.body);
130130
* `h2` **{boolean}** `Default: false` Forces use of the HTTP2 protocol
131131
* `headers` **{Object}** Headers to add to the request
132132
* `parse` **{boolean}** `Default: true` Parse response body, or simply return a buffer
133-
* `redirect` **{boolean | error | follow}** `Default: 'follow'` Controls redirect flow
133+
* `redirect` **{error | follow | manual}** `Default: 'follow'` Controls redirect flow
134134
* `thenable` **{boolean}** `Default: false` Controls promise resolutions
135135
* **Returns:** Promise that resolves to
136136
extended [http.IncomingMessage](https://nodejs.org/api/http.html#http_class_http_incomingmessage)
@@ -168,6 +168,7 @@ Method with limited functionality to use with streams and pipes
168168
* No automata
169169
* No redirects
170170
* Pass `h2: true` in options to use the HTTP2 protocol
171+
* Or use `ackn({ url: URL })` method in advance to probe the available protocols
171172

172173
---
173174

package-lock.json

+56-56
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
"@babel/eslint-parser": "^7.16.5",
1414
"@babel/preset-env": "^7.16.11",
1515
"c8": "^7.11.0",
16-
"eslint": "^8.7.0",
17-
"eslint-config-ultra-refined": "^2.3.0",
16+
"eslint": "^8.8.0",
17+
"eslint-config-ultra-refined": "^2.4.0",
1818
"mocha": "^9.2.0"
1919
},
2020
"description": "The robust request library that humanity deserves 🌐",
@@ -56,8 +56,9 @@
5656
"lint": "eslint . --ext .cjs,.js,.mjs",
5757
"prepack": "npm run build && sh pony.sh",
5858
"pretest": "rm -rf coverage && npm run cert:gen",
59-
"test": "mocha --exit --recursive",
59+
"test": "mocha",
60+
"test:bail": "mocha --bail",
6061
"test:cover": "c8 --include=src --reporter=lcov --reporter=text npm test"
6162
},
62-
"version": "2.3.6"
63+
"version": "2.4.0"
6364
}

pony.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env bash
2-
# Transforms extensions within source file
2+
# Transforms extensions within source files
33

44
for file in dist/*.js; do
55
echo "$file"
6-
sed -i -e 's/.mjs/.js/g' "$file"
6+
sed -i -e 's/.\bmjs\b/.js/g' "$file"
77
done

src/helpers.mjs

+16-2
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,15 @@ export const preflight = (options) => {
167167

168168
options.method ??= method;
169169
options.parse ??= true;
170-
options.redirect ??= 'follow';
170+
options.redirect ??= redirects.follow;
171+
172+
if (!Object.values(redirects).includes(options.redirect)) {
173+
options.createConnection?.().destroy();
174+
throw new TypeError(`Failed to read the 'redirect' property from 'options': The provided value '${
175+
options.redirect
176+
}' is not a valid enum value.`);
177+
}
178+
171179
options.redirected ??= false;
172180
options.thenable ??= false;
173181

@@ -214,7 +222,7 @@ export const premix = (res, { digest = false, parse = false } = {}) => {
214222
enumerable: true,
215223
value: async function () {
216224
if (this.bodyUsed) {
217-
throw new TypeError('Response stream already read');
225+
throw new TypeError('Response stream already read.');
218226
}
219227

220228
let spool = [];
@@ -262,6 +270,12 @@ export const premix = (res, { digest = false, parse = false } = {}) => {
262270
});
263271
};
264272

273+
export const redirects = {
274+
error: 'error',
275+
follow: 'follow',
276+
manual: 'manual',
277+
};
278+
265279
export async function* tap(value) {
266280
if (Reflect.has(value, Symbol.asyncIterator)) {
267281
yield* value;

src/index.mjs

+7-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
merge,
1010
preflight,
1111
premix,
12+
redirects,
1213
transform,
1314
} from './helpers.mjs';
1415
import { APPLICATION_OCTET_STREAM } from './mediatypes.mjs';
@@ -44,7 +45,7 @@ export default async function rekwest(url, options = {}) {
4445
HTTP2_METHOD_GET,
4546
HTTP2_METHOD_HEAD,
4647
].includes(options.method)) {
47-
throw new TypeError(`Request with ${ HTTP2_METHOD_GET }/${ HTTP2_METHOD_HEAD } method cannot have body`);
48+
throw new TypeError(`Request with ${ HTTP2_METHOD_GET }/${ HTTP2_METHOD_HEAD } method cannot have body.`);
4849
}
4950

5051
if (!options.follow) {
@@ -122,16 +123,16 @@ export default async function rekwest(url, options = {}) {
122123
});
123124

124125
if (follow && /^3\d{2}$/.test(res.statusCode) && res.headers[HTTP2_HEADER_LOCATION]) {
125-
if (redirect === 'error') {
126-
res.emit('error', new RequestError(`Unexpected redirect, redirect mode is set to '${ redirect }'`));
126+
if (redirect === redirects.error) {
127+
res.emit('error', new RequestError(`Unexpected redirect, redirect mode is set to '${ redirect }'.`));
127128
}
128129

129-
if (redirect === 'follow') {
130+
if (redirect === redirects.follow) {
130131
options.url = new URL(res.headers[HTTP2_HEADER_LOCATION], url).href;
131132

132133
if (res.statusCode !== HTTP_STATUS_SEE_OTHER
133134
&& body === Object(body) && body.pipe?.constructor === Function) {
134-
res.emit('error', new RequestError(`Unable to ${ redirect } redirect with body as readable stream`));
135+
res.emit('error', new RequestError(`Unable to ${ redirect } redirect with body as readable stream.`));
135136
}
136137

137138
options.follow--;
@@ -211,6 +212,7 @@ Reflect.defineProperty(rekwest, 'stream', {
211212
...merge(rekwest.defaults, {
212213
headers: { [HTTP2_HEADER_CONTENT_TYPE]: APPLICATION_OCTET_STREAM },
213214
}, options),
215+
redirect: redirects.manual,
214216
});
215217

216218
if (options.h2) {

0 commit comments

Comments
 (0)