From d42bd36aa780760f6ca59eef1def859b825c0029 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Sun, 2 May 2021 10:10:06 +0100 Subject: [PATCH 1/4] fix: remove node streams from native fetch types They cause errors with powergate --- types/native-fetch/index.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/types/native-fetch/index.d.ts b/types/native-fetch/index.d.ts index 596d04a..eb22d86 100644 --- a/types/native-fetch/index.d.ts +++ b/types/native-fetch/index.d.ts @@ -28,7 +28,7 @@ export class Response extends globalThis.Response { clone: () => Response // Body interface - readonly body: NodeReadableStream | ReadableStream | null + readonly body: ReadableStream | null readonly bodyUsed: boolean arrayBuffer: () => Promise blob: () => Promise @@ -47,7 +47,7 @@ export class Request extends globalThis.Request { constructor (input: RequestInfo, init?: RequestInit) // Body interface - readonly body: NodeReadableStream | ReadableStream | null + readonly body: ReadableStream | null readonly bodyUsed: boolean arrayBuffer: () => Promise blob: () => Promise From f5328e969bcca0006ef3e32226eb627289aa4edc Mon Sep 17 00:00:00 2001 From: achingbrain Date: Tue, 4 May 2021 07:51:10 +0100 Subject: [PATCH 2/4] chore: remove native-fetch retyping These types are incorrect and cause errors in other modules. Fixes #109 --- src/http.js | 9 +-- src/http/error.js | 2 +- src/http/fetch.browser.js | 3 +- src/http/fetch.node.js | 1 - src/types.d.ts | 7 +- types/native-fetch/index.d.ts | 117 ---------------------------------- 6 files changed, 10 insertions(+), 129 deletions(-) delete mode 100644 types/native-fetch/index.d.ts diff --git a/src/http.js b/src/http.js index c08e751..bcf1eb1 100644 --- a/src/http.js +++ b/src/http.js @@ -9,7 +9,7 @@ const { AbortController } = require('native-abort-controller') const anySignal = require('any-signal') /** - * @typedef {import('native-fetch').Response} Response + * @typedef {import('./types').ExtendedResponse} ExtendedResponse * @typedef {import('stream').Readable} NodeReadableStream * @typedef {import('stream').Duplex} NodeDuplexStream * @typedef {import('./types').HTTPOptions} HTTPOptions @@ -88,7 +88,7 @@ class HTTP { * * @param {string | Request} resource * @param {HTTPOptions} options - * @returns {Promise} + * @returns {Promise} */ async fetch (resource, options = {}) { /** @type {HTTPOptions} */ @@ -168,7 +168,6 @@ class HTTP { /** * @param {string | Request} resource * @param {HTTPOptions} options - * @returns {Promise} */ post (resource, options = {}) { return this.fetch(resource, { ...options, method: 'POST' }) @@ -177,7 +176,6 @@ class HTTP { /** * @param {string | Request} resource * @param {HTTPOptions} options - * @returns {Promise} */ get (resource, options = {}) { return this.fetch(resource, { ...options, method: 'GET' }) @@ -186,7 +184,6 @@ class HTTP { /** * @param {string | Request} resource * @param {HTTPOptions} options - * @returns {Promise} */ put (resource, options = {}) { return this.fetch(resource, { ...options, method: 'PUT' }) @@ -195,7 +192,6 @@ class HTTP { /** * @param {string | Request} resource * @param {HTTPOptions} options - * @returns {Promise} */ delete (resource, options = {}) { return this.fetch(resource, { ...options, method: 'DELETE' }) @@ -204,7 +200,6 @@ class HTTP { /** * @param {string | Request} resource * @param {HTTPOptions} options - * @returns {Promise} */ options (resource, options = {}) { return this.fetch(resource, { ...options, method: 'OPTIONS' }) diff --git a/src/http/error.js b/src/http/error.js index aa5c9d6..d839809 100644 --- a/src/http/error.js +++ b/src/http/error.js @@ -18,7 +18,7 @@ exports.AbortError = AbortError class HTTPError extends Error { /** - * @param {import('native-fetch').Response} response + * @param {Response} response */ constructor (response) { super(response.statusText) diff --git a/src/http/fetch.browser.js b/src/http/fetch.browser.js index 4c70938..ee59420 100644 --- a/src/http/fetch.browser.js +++ b/src/http/fetch.browser.js @@ -136,6 +136,5 @@ class ResponseWithURL extends Response { module.exports = { fetch: fetchWith, Request, - Headers, - ResponseWithURL + Headers } diff --git a/src/http/fetch.node.js b/src/http/fetch.node.js index 27d8ee8..ae0ed0f 100644 --- a/src/http/fetch.node.js +++ b/src/http/fetch.node.js @@ -5,7 +5,6 @@ const { Request, Response, Headers, default: nativeFetch } = require('../fetch') const toStream = require('it-to-stream') const { Buffer } = require('buffer') /** - * @typedef {import('native-fetch').BodyInit} BodyInit * @typedef {import('stream').Readable} NodeReadableStream * * @typedef {import('../types').FetchOptions} FetchOptions diff --git a/src/types.d.ts b/src/types.d.ts index 910ec56..2ee6223 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -1,4 +1,3 @@ -import type { RequestInit, Response } from '../types/native-fetch' interface ProgressStatus { total: number loaded: number @@ -51,3 +50,9 @@ export interface HTTPOptions extends FetchOptions { */ handleError?: (rsp: Response) => Promise } + +export interface ExtendedResponse extends Response { + iterator: () => AsyncGenerator + + ndjson: () => AsyncGenerator +} diff --git a/types/native-fetch/index.d.ts b/types/native-fetch/index.d.ts deleted file mode 100644 index eb22d86..0000000 --- a/types/native-fetch/index.d.ts +++ /dev/null @@ -1,117 +0,0 @@ -import { Readable as NodeReadableStream } from 'stream' - -export default function fetch (input: RequestInfo, init?: RequestInit): Promise - -export interface Body { - readonly body: NodeReadableStream | ReadableStream | null - readonly bodyUsed: boolean - arrayBuffer: () => Promise - blob: () => Promise - formData: () => Promise - json: () => Promise - text: () => Promise - buffer: () => Promise -} -export class Headers extends globalThis.Headers {} - -/** This Fetch API interface represents the response to a request. */ -export class Response extends globalThis.Response { - constructor (body?: BodyInit | null, init?: ResponseInit) - readonly headers: Headers - readonly ok: boolean - readonly redirected: boolean - readonly status: number - readonly statusText: string - readonly trailer: Promise - readonly type: ResponseType - readonly url: string - clone: () => Response - - // Body interface - readonly body: ReadableStream | null - readonly bodyUsed: boolean - arrayBuffer: () => Promise - blob: () => Promise - formData: () => Promise - json: () => Promise - text: () => Promise - buffer: () => Promise - iterator: () => AsyncIterable - ndjson: () => AsyncIterable - - static error (): Response - static redirect (url: string, status?: number): Response -} - -export class Request extends globalThis.Request { - constructor (input: RequestInfo, init?: RequestInit) - - // Body interface - readonly body: ReadableStream | null - readonly bodyUsed: boolean - arrayBuffer: () => Promise - blob: () => Promise - formData: () => Promise - json: () => Promise - text: () => Promise - buffer: () => Promise -} - -export type RequestInfo = Request | string - -export type BodyInit = Blob | BufferSource | FormData | URLSearchParams | ReadableStream | string | Buffer | NodeReadableStream - -export interface RequestInit { - /** - * A BodyInit object or null to set request's body. - */ - body?: BodyInit | null - /** - * A string indicating how the request will interact with the browser's cache to set request's cache. - */ - cache?: RequestCache - /** - * A string indicating whether credentials will be sent with the request always, never, or only when sent to a same-origin URL. Sets request's credentials. - */ - credentials?: RequestCredentials - /** - * A Headers object, an object literal, or an array of two-item arrays to set request's headers. - */ - headers?: HeadersInit - /** - * A cryptographic hash of the resource to be fetched by request. Sets request's integrity. - */ - integrity?: string - /** - * A boolean to set request's keepalive. - */ - keepalive?: boolean - /** - * A string to set request's method. - */ - method?: string - /** - * A string to indicate whether the request will use CORS, or will be restricted to same-origin URLs. Sets request's mode. - */ - mode?: RequestMode - /** - * A string indicating whether request follows redirects, results in an error upon encountering a redirect, or returns the redirect (in an opaque fashion). Sets request's redirect. - */ - redirect?: RequestRedirect - /** - * A string whose value is a same-origin URL, "about:client", or the empty string, to set request's referrer. - */ - referrer?: string - /** - * A referrer policy to set request's referrerPolicy. - */ - referrerPolicy?: ReferrerPolicy - /** - * An AbortSignal to set request's signal. - */ - signal?: AbortSignal | null - /** - * Can only be null. Used to disassociate request from any Window. - */ - window?: any -} From eb18a43d4eb4b7af72463c3ab1bb78a8fa995b7a Mon Sep 17 00:00:00 2001 From: achingbrain Date: Tue, 4 May 2021 07:59:44 +0100 Subject: [PATCH 3/4] chore: remove redundant types iso-url and electron-fetch export their own types so do not retype them here. We can remove is-electron too once https://github.com/cheton/is-electron/issues/7 is resolved. --- types/electron-fetch/index.d.ts | 117 -------------------------------- types/iso-url/index.d.ts | 1 - 2 files changed, 118 deletions(-) delete mode 100644 types/electron-fetch/index.d.ts delete mode 100644 types/iso-url/index.d.ts diff --git a/types/electron-fetch/index.d.ts b/types/electron-fetch/index.d.ts deleted file mode 100644 index 596d04a..0000000 --- a/types/electron-fetch/index.d.ts +++ /dev/null @@ -1,117 +0,0 @@ -import { Readable as NodeReadableStream } from 'stream' - -export default function fetch (input: RequestInfo, init?: RequestInit): Promise - -export interface Body { - readonly body: NodeReadableStream | ReadableStream | null - readonly bodyUsed: boolean - arrayBuffer: () => Promise - blob: () => Promise - formData: () => Promise - json: () => Promise - text: () => Promise - buffer: () => Promise -} -export class Headers extends globalThis.Headers {} - -/** This Fetch API interface represents the response to a request. */ -export class Response extends globalThis.Response { - constructor (body?: BodyInit | null, init?: ResponseInit) - readonly headers: Headers - readonly ok: boolean - readonly redirected: boolean - readonly status: number - readonly statusText: string - readonly trailer: Promise - readonly type: ResponseType - readonly url: string - clone: () => Response - - // Body interface - readonly body: NodeReadableStream | ReadableStream | null - readonly bodyUsed: boolean - arrayBuffer: () => Promise - blob: () => Promise - formData: () => Promise - json: () => Promise - text: () => Promise - buffer: () => Promise - iterator: () => AsyncIterable - ndjson: () => AsyncIterable - - static error (): Response - static redirect (url: string, status?: number): Response -} - -export class Request extends globalThis.Request { - constructor (input: RequestInfo, init?: RequestInit) - - // Body interface - readonly body: NodeReadableStream | ReadableStream | null - readonly bodyUsed: boolean - arrayBuffer: () => Promise - blob: () => Promise - formData: () => Promise - json: () => Promise - text: () => Promise - buffer: () => Promise -} - -export type RequestInfo = Request | string - -export type BodyInit = Blob | BufferSource | FormData | URLSearchParams | ReadableStream | string | Buffer | NodeReadableStream - -export interface RequestInit { - /** - * A BodyInit object or null to set request's body. - */ - body?: BodyInit | null - /** - * A string indicating how the request will interact with the browser's cache to set request's cache. - */ - cache?: RequestCache - /** - * A string indicating whether credentials will be sent with the request always, never, or only when sent to a same-origin URL. Sets request's credentials. - */ - credentials?: RequestCredentials - /** - * A Headers object, an object literal, or an array of two-item arrays to set request's headers. - */ - headers?: HeadersInit - /** - * A cryptographic hash of the resource to be fetched by request. Sets request's integrity. - */ - integrity?: string - /** - * A boolean to set request's keepalive. - */ - keepalive?: boolean - /** - * A string to set request's method. - */ - method?: string - /** - * A string to indicate whether the request will use CORS, or will be restricted to same-origin URLs. Sets request's mode. - */ - mode?: RequestMode - /** - * A string indicating whether request follows redirects, results in an error upon encountering a redirect, or returns the redirect (in an opaque fashion). Sets request's redirect. - */ - redirect?: RequestRedirect - /** - * A string whose value is a same-origin URL, "about:client", or the empty string, to set request's referrer. - */ - referrer?: string - /** - * A referrer policy to set request's referrerPolicy. - */ - referrerPolicy?: ReferrerPolicy - /** - * An AbortSignal to set request's signal. - */ - signal?: AbortSignal | null - /** - * Can only be null. Used to disassociate request from any Window. - */ - window?: any -} diff --git a/types/iso-url/index.d.ts b/types/iso-url/index.d.ts deleted file mode 100644 index eac8a7e..0000000 --- a/types/iso-url/index.d.ts +++ /dev/null @@ -1 +0,0 @@ -export { URL, URLSearchParams } From 7239ec832f0a4b31d9ad436747133256ab8f42b8 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Tue, 4 May 2021 08:03:48 +0100 Subject: [PATCH 4/4] chore: derive return type to fix types in tests --- src/http.js | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/http.js b/src/http.js index bcf1eb1..ba88fdf 100644 --- a/src/http.js +++ b/src/http.js @@ -330,35 +330,30 @@ HTTP.streamToAsyncIterator = fromStream /** * @param {string | Request} resource * @param {HTTPOptions} [options] - * @returns {Promise} */ HTTP.post = (resource, options) => new HTTP(options).post(resource, options) /** * @param {string | Request} resource * @param {HTTPOptions} [options] - * @returns {Promise} */ HTTP.get = (resource, options) => new HTTP(options).get(resource, options) /** * @param {string | Request} resource * @param {HTTPOptions} [options] - * @returns {Promise} */ HTTP.put = (resource, options) => new HTTP(options).put(resource, options) /** * @param {string | Request} resource * @param {HTTPOptions} [options] - * @returns {Promise} */ HTTP.delete = (resource, options) => new HTTP(options).delete(resource, options) /** * @param {string | Request} resource * @param {HTTPOptions} [options] - * @returns {Promise} */ HTTP.options = (resource, options) => new HTTP(options).options(resource, options)