Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@whatwg-node/node-fetch

Package Overview
Dependencies
Maintainers
1
Versions
554
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@whatwg-node/node-fetch - npm Package Compare versions

Comparing version 0.0.1-alpha-20221226141545-8392bc6 to 0.0.1-alpha-20221228080239-7c1fd2a

111

index.js

@@ -57,18 +57,27 @@ 'use strict';

function createController(desiredSize, readable) {
let enqueued = false;
let chunks = [];
return {
desiredSize,
enqueue(chunk) {
enqueued = true;
readable.push(chunk);
chunks.push(Buffer.from(chunk));
},
close() {
if (chunks.length > 0) {
this._flush();
}
readable.push(null);
},
error(error) {
if (chunks.length > 0) {
this._flush();
}
readable.destroy(error);
},
get enqueued() {
return enqueued;
},
_flush() {
if (chunks.length > 0) {
const concatenated = Buffer.concat(chunks);
readable.push(concatenated);
chunks = [];
}
}
};

@@ -79,3 +88,2 @@ }

this.locked = false;
let started = false;
if (underlyingSource instanceof PonyfillReadableStream) {

@@ -88,4 +96,13 @@ this.readable = underlyingSource.readable;

else if (underlyingSource && 'getReader' in underlyingSource) {
const reader = underlyingSource.getReader();
let reader;
this.readable = new stream.Readable({
construct(callback) {
try {
reader = underlyingSource.getReader();
callback(null);
}
catch (err) {
callback(err);
}
},
read() {

@@ -112,24 +129,19 @@ reader

else {
let waitingForPull = false;
this.readable = new stream.Readable({
async read(desiredSize) {
var _a, _b;
if (waitingForPull) {
return;
}
waitingForPull = true;
async construct(callback) {
var _a;
try {
const controller = createController(desiredSize, this);
if (!started) {
started = true;
await ((_a = underlyingSource === null || underlyingSource === void 0 ? void 0 : underlyingSource.start) === null || _a === void 0 ? void 0 : _a.call(underlyingSource, controller));
}
if (!controller.enqueued) {
await ((_b = underlyingSource === null || underlyingSource === void 0 ? void 0 : underlyingSource.pull) === null || _b === void 0 ? void 0 : _b.call(underlyingSource, controller));
}
await ((_a = underlyingSource === null || underlyingSource === void 0 ? void 0 : underlyingSource.start) === null || _a === void 0 ? void 0 : _a.call(underlyingSource, createController(0, this)));
callback(null);
}
finally {
waitingForPull = false;
catch (err) {
callback(err);
}
},
async read(desiredSize) {
var _a;
const controller = createController(desiredSize, this);
await ((_a = underlyingSource === null || underlyingSource === void 0 ? void 0 : underlyingSource.pull) === null || _a === void 0 ? void 0 : _a.call(underlyingSource, controller));
controller._flush();
},
async destroy(err, callback) {

@@ -193,2 +205,10 @@ var _a;

}
// Trick Fastify
pipe(writable) {
return this.readable.pipe(writable);
}
on(event, listener) {
this.readable.on(event, listener);
return this;
}
}

@@ -604,5 +624,20 @@

function getResponseForFile(url$1) {
const path = url.fileURLToPath(url$1);
const readable = fs.createReadStream(path);
return new PonyfillResponse(readable);
}
function getRequestFnForProtocol(protocol) {
switch (protocol) {
case 'http':
return http.request;
case 'https':
return https.request;
}
throw new Error(`Unsupported protocol: ${protocol}`);
}
const fetchPonyfill = (info, init) => {
if (typeof info === 'string' || info instanceof URL) {
return fetchPonyfill(new PonyfillRequest(info, init));
const ponyfillRequest = new PonyfillRequest(info, init);
return fetchPonyfill(ponyfillRequest);
}

@@ -612,7 +647,9 @@ const fetchRequest = info;

try {
if (fetchRequest.url.startsWith('file://')) {
resolve(new PonyfillResponse(fs.createReadStream(url.fileURLToPath(fetchRequest.url))));
const protocol = fetchRequest.url.split('://')[0];
if (protocol === 'file') {
const response = getResponseForFile(fetchRequest.url);
resolve(response);
return;
}
const requestFn = fetchRequest.url.startsWith('https') ? https.request : http.request;
const requestFn = getRequestFnForProtocol(protocol);
const nodeReadable = fetchRequest.readable();

@@ -632,3 +669,4 @@ const nodeHeaders = getHeadersObj(fetchRequest.headers);

if (fetchRequest.redirect === 'error') {
reject(new Error('Redirects are not allowed'));
const redirectError = new Error('Redirects are not allowed');
reject(redirectError);
nodeResponse.resume();

@@ -638,5 +676,7 @@ return;

if (fetchRequest.redirect === 'follow') {
resolve(fetchPonyfill(new URL(nodeResponse.headers.location, info.url), info).then(resp => {
resp.redirected = true;
return resp;
const redirectedUrl = new URL(nodeResponse.headers.location, fetchRequest.url);
const redirectResponse$ = fetchPonyfill(redirectedUrl, info);
resolve(redirectResponse$.then(redirectResponse => {
redirectResponse.redirected = true;
return redirectResponse;
}));

@@ -648,3 +688,3 @@ nodeResponse.resume();

const responseHeaders = nodeResponse.headers;
resolve(new PonyfillResponse(nodeResponse, {
const ponyfillResponse = new PonyfillResponse(nodeResponse, {
status: nodeResponse.statusCode,

@@ -654,3 +694,4 @@ statusText: nodeResponse.statusMessage,

url: info.url,
}));
});
resolve(ponyfillResponse);
});

@@ -657,0 +698,0 @@ nodeRequest.on('error', reject);

{
"name": "@whatwg-node/node-fetch",
"version": "0.0.1-alpha-20221226141545-8392bc6",
"version": "0.0.1-alpha-20221228080239-7c1fd2a",
"description": "Fetch API implementation for Node",

@@ -5,0 +5,0 @@ "sideEffects": false,

/// <reference types="node" />
import { Readable } from 'stream';
import { Readable, Writable } from 'stream';
export declare class PonyfillReadableStream<T> implements ReadableStream<T> {

@@ -19,2 +19,4 @@ readable: Readable;

}): ReadableStream<T2>;
pipe(writable: Writable): Writable;
on(event: string, listener: (...args: any[]) => void): this;
}

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc