bare-fetch
Advanced tools
Comparing version 2.1.1 to 2.1.2
92
index.js
@@ -1,4 +0,4 @@ | ||
const http = require('http') | ||
const https = require('https') | ||
const { ReadableStream, ByteLengthQueuingStrategy } = require('bare-stream/web') | ||
const http = require('bare-http1') | ||
const https = require('bare-https') | ||
const { ReadableStream } = require('bare-stream/web') | ||
const Response = require('./lib/response') | ||
@@ -8,12 +8,21 @@ const Headers = require('./lib/headers') | ||
const defaultStrategy = new ByteLengthQueuingStrategy({ highWaterMark: 65536 }) | ||
// https://fetch.spec.whatwg.org/#dom-global-fetch | ||
module.exports = exports = function fetch(input, init = {}) { | ||
let resolve | ||
let reject | ||
module.exports = exports = function fetch(url, opts = {}) { | ||
let redirects = 0 | ||
const promise = new Promise((res, rej) => { | ||
resolve = res | ||
reject = rej | ||
}) | ||
return process(url) | ||
const result = new Response() | ||
async function process(url) { | ||
if (redirects > 20) { | ||
throw errors.TOO_MANY_REDIRECTS('Redirect count exceeded') | ||
process(input) | ||
return promise | ||
function process(input) { | ||
if (result._urls.length > 20) { | ||
return reject(errors.TOO_MANY_REDIRECTS('Redirect count exceeded')) | ||
} | ||
@@ -23,5 +32,5 @@ | ||
try { | ||
target = new URL(url) | ||
target = new URL(input) | ||
} catch (err) { | ||
throw errors.INVALID_URL('Invalid URL', err) | ||
return reject(errors.INVALID_URL('Invalid URL', err)) | ||
} | ||
@@ -36,51 +45,26 @@ | ||
} else { | ||
throw errors.UNKNOWN_PROTOCOL('Unknown protocol') | ||
return reject(errors.UNKNOWN_PROTOCOL('Unknown protocol')) | ||
} | ||
return new Promise((resolve, reject) => { | ||
const req = protocol.request(target, opts, (res) => { | ||
if (res.headers.location && isRedirectStatus(res.statusCode)) { | ||
redirects++ | ||
result._urls.push(target) | ||
return process(res.headers.location).then(resolve, reject) | ||
} | ||
const req = protocol.request(target, init, (res) => { | ||
if (res.headers.location && isRedirectStatus(res.statusCode)) { | ||
return process(res.headers.location) | ||
} | ||
const body = new ReadableStream( | ||
{ | ||
start(controller) { | ||
res | ||
.on('data', (chunk) => { | ||
controller.enqueue(chunk) | ||
result._body = new ReadableStream(res) | ||
result._status = res.statusCode | ||
result._statusText = res.statusMessage | ||
if (controller.desiredSize <= 0) res.pause() | ||
}) | ||
.on('end', () => { | ||
controller.close() | ||
}) | ||
}, | ||
pull() { | ||
res.resume() | ||
} | ||
}, | ||
defaultStrategy | ||
) | ||
for (const [name, value] of Object.entries(res.headers)) { | ||
result._headers.set(name, value) | ||
} | ||
const result = new Response(body) | ||
resolve(result) | ||
}) | ||
result.status = res.statusCode | ||
result.redirected = redirects > 0 | ||
for (const [name, value] of Object.entries(res.headers)) { | ||
result.headers.set(name, value) | ||
} | ||
resolve(result) | ||
}) | ||
req.on('error', (e) => reject(errors.NETWORK_ERROR('Network error', e))) | ||
if (opts.body) req.write(opts.body) | ||
req.end() | ||
}) | ||
req | ||
.on('error', (e) => reject(errors.NETWORK_ERROR('Network error', e))) | ||
.end(init.body) | ||
} | ||
@@ -87,0 +71,0 @@ } |
@@ -7,3 +7,3 @@ const { ReadableStream, isReadableStreamDisturbed } = require('bare-stream/web') | ||
constructor(body = null) { | ||
this.body = null | ||
this._body = null | ||
@@ -13,3 +13,3 @@ if (typeof body === 'string') body = Buffer.from(body) | ||
if (Buffer.isBuffer(body)) { | ||
this.body = new ReadableStream({ | ||
this._body = new ReadableStream({ | ||
start(controller) { | ||
@@ -21,17 +21,22 @@ controller.enqueue(body) | ||
} else if (body !== null) { | ||
this.body = body | ||
this._body = body | ||
} | ||
} | ||
// https://fetch.spec.whatwg.org/#dom-body-body | ||
get body() { | ||
return this._body | ||
} | ||
// https://fetch.spec.whatwg.org/#dom-body-bodyused | ||
get bodyUsed() { | ||
return this.body !== null && isReadableStreamDisturbed(this.body) | ||
return this._body !== null && isReadableStreamDisturbed(this._body) | ||
} | ||
async buffer() { | ||
if (this.body === null) { | ||
if (this._body === null) { | ||
throw errors.BODY_UNUSABLE('Body is unset') | ||
} | ||
if (this.bodyUsed) { | ||
if (isReadableStreamDisturbed(this._body)) { | ||
throw errors.BODY_UNUSABLE('Body has already been consumed') | ||
@@ -43,3 +48,3 @@ } | ||
for await (const chunk of this.body) { | ||
for await (const chunk of this._body) { | ||
chunks.push(chunk) | ||
@@ -46,0 +51,0 @@ length += chunk.byteLength |
@@ -6,16 +6,44 @@ const Body = require('./body') | ||
module.exports = class Response extends Body { | ||
constructor(body = null, opts = {}) { | ||
const { headers = new Headers(), status = 200 } = opts | ||
constructor(body = null, init = {}) { | ||
const { status = 200, statusText = '', headers } = init | ||
super(body) | ||
this.headers = headers | ||
this.status = status | ||
this.redirected = false | ||
this._urls = [] | ||
this._status = status | ||
this._statusText = statusText | ||
this._headers = new Headers(headers) | ||
} | ||
// https://fetch.spec.whatwg.org/#dom-response-url | ||
get url() { | ||
return this._urls.length === null | ||
? null | ||
: this._urls[this._urls.length - 1].href | ||
} | ||
// https://fetch.spec.whatwg.org/#dom-response-redirected | ||
get redirected() { | ||
return this._urls.length > 1 | ||
} | ||
// https://fetch.spec.whatwg.org/#dom-response-status | ||
get status() { | ||
return this._status | ||
} | ||
// https://fetch.spec.whatwg.org/#dom-response-ok | ||
get ok() { | ||
return this.status >= 200 && this.status <= 299 | ||
return this._status >= 200 && this._status <= 299 | ||
} | ||
// https://fetch.spec.whatwg.org/#dom-response-statustext | ||
get statusText() { | ||
return this._statusText | ||
} | ||
// https://fetch.spec.whatwg.org/#dom-response-headers | ||
get headers() { | ||
return this._headers | ||
} | ||
} |
{ | ||
"name": "bare-fetch", | ||
"version": "2.1.1", | ||
"version": "2.1.2", | ||
"description": "WHATWG Fetch implementation for Bare", | ||
@@ -14,12 +14,2 @@ "exports": { | ||
}, | ||
"imports": { | ||
"http": { | ||
"bare": "bare-http1", | ||
"default": "http" | ||
}, | ||
"https": { | ||
"bare": "bare-https", | ||
"default": "https" | ||
} | ||
}, | ||
"files": [ | ||
@@ -44,5 +34,5 @@ "index.js", | ||
"dependencies": { | ||
"bare-http1": "^4.0.0", | ||
"bare-http1": "^4.0.2", | ||
"bare-https": "^2.0.0", | ||
"bare-stream": "^2.4.1" | ||
"bare-stream": "^2.6.0" | ||
}, | ||
@@ -49,0 +39,0 @@ "devDependencies": { |
Network access
Supply chain riskThis module accesses the network.
Found 2 instances in 1 package
19433
204
3
Updatedbare-http1@^4.0.2
Updatedbare-stream@^2.6.0