Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,17 +146,13 @@
"any-signal": "^3.0.0",
"browser-readablestream-to-it": "^1.0.0",
"buffer": "^6.0.1",
"electron-fetch": "^1.7.2",
"err-code": "^3.0.1",
"is-electron": "^2.2.0",
"iso-url": "^1.1.5",
"it-all": "^1.0.4",
"it-glob": "^1.0.1",
"it-to-stream": "^1.0.0",
"merge-options": "^3.0.4",
"nanoid": "^3.1.20",
"native-fetch": "^3.0.0",
"node-fetch": "^2.6.8",
"react-native-fetch-api": "^3.0.0",
"stream-to-it": "^0.2.2"
},
Expand All @@ -174,10 +170,8 @@
"util": "^0.12.3"
},
"browser": {
"./src/http/fetch.js": "./src/http/fetch.browser.js",
"./src/temp-dir.js": "./src/temp-dir.browser.js",
"./src/path-join.js": "./src/path-join.browser.js",
"./src/fetch.js": "./src/fetch.browser.js",
"./src/files/glob-source.js": false,
"./test/files/glob-source.spec.js": false,
"electron-fetch": false,
Expand Down
2 changes: 1 addition & 1 deletion rn-test.config.js → rn-test.config.cjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

module.exports = {
require: require.resolve('./rn-test.require.js'),
require: require.resolve('./rn-test.require.cjs'),
runner: 'mocha',
modules: [
'react-native-url-polyfill',
Expand Down
6 changes: 3 additions & 3 deletions rn-test.require.js → rn-test.require.cjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict'

const { polyfill: polyfillReadableStream } = require('react-native-polyfill-globals/src/readable-stream')
const { polyfill: polyfillURL } = require('react-native-polyfill-globals/src/url')
const { polyfill: polyfillEncoding } = require('react-native-polyfill-globals/src/encoding')
const { polyfill: polyfillReadableStream } = require('react-native-polyfill-globals/src/readable-stream.js')
const { polyfill: polyfillURL } = require('react-native-polyfill-globals/src/url.js')
const { polyfill: polyfillEncoding } = require('react-native-polyfill-globals/src/encoding.js')

polyfillURL()
polyfillReadableStream()
Expand Down
2 changes: 1 addition & 1 deletion src/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const IS_ELECTRON = isElectron()
const IS_BROWSER = IS_ENV_WITH_DOM && !IS_ELECTRON
const IS_ELECTRON_MAIN = IS_ELECTRON && !IS_ENV_WITH_DOM
const IS_ELECTRON_RENDERER = IS_ELECTRON && IS_ENV_WITH_DOM
const IS_NODE = typeof require === 'function' && typeof process !== 'undefined' && typeof process.release !== 'undefined' && process.release.name === 'node' && !IS_ELECTRON
const IS_NODE = typeof process !== 'undefined' && typeof process.release !== 'undefined' && process.release.name === 'node' && !IS_ELECTRON
// @ts-ignore - we either ignore worker scope or dom scope
const IS_WEBWORKER = typeof importScripts === 'function' && typeof self !== 'undefined' && typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope
const IS_TEST = typeof process !== 'undefined' && typeof process.env !== 'undefined' && process.env.NODE_ENV === 'test'
Expand Down
10 changes: 0 additions & 10 deletions src/fetch.browser.js

This file was deleted.

14 changes: 5 additions & 9 deletions src/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@
* @typedef {globalThis.Response} Response
*/

const { isElectronMain } = require('./env')

// use window.fetch if it is available, fall back to node-fetch if not
let impl = 'native-fetch'

if (isElectronMain) {
impl = 'electron-fetch'
module.exports = {
Headers: globalThis.Headers,
Request: globalThis.Request,
Response: globalThis.Response,
default: globalThis.fetch
}

module.exports = require(impl)
22 changes: 12 additions & 10 deletions src/fetch.rn.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
// @ts-nocheck
'use strict'
// @ts-ignore
const { Headers, Request, Response, fetch } = require('react-native-fetch-api')
const { Headers: rnHeaders, Request: rnRequest, Response: rnResponse, fetch: rnFetch } = require('react-native-fetch-api')

/** @type {import('electron-fetch').default} */
const rnFetch = fetch
const fetch = rnFetch
/** @type {import('electron-fetch').Headers} */
const rnHeaders = Headers
const Headers = rnHeaders
/** @type {import('electron-fetch').Request} */
const rnRequest = Request
const Request = rnRequest
/** @type {import('electron-fetch').Response} */
const rnResponse = Response
module.exports = rnFetch
module.exports.Headers = rnHeaders
module.exports.Request = rnRequest
module.exports.Response = rnResponse
module.exports.default = rnFetch
const Response = rnResponse

module.exports = {
Headers,
Request,
Response,
default: fetch
}
6 changes: 3 additions & 3 deletions src/files/glob-source.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const fsp = require('fs').promises
const fs = require('fs')
const { promises: fsp } = fs
const glob = require('it-glob')
const Path = require('path')
const errCode = require('err-code')
Expand All @@ -11,8 +11,8 @@ const errCode = require('err-code')
*
* @param {string} cwd - The directory to start matching the pattern in
* @param {string} pattern - Glob pattern to match
* @param {import('../types').GlobSourceOptions} [options] - Optional options
* @returns {AsyncGenerator<import('../types').GlobSourceResult, void, unknown>} File objects that match glob
* @param {import('../types.d.ts').GlobSourceOptions} [options] - Optional options
* @returns {AsyncGenerator<import('../types.d.ts').GlobSourceResult, void, unknown>} File objects that match glob
*/
module.exports = async function * globSource (cwd, pattern, options) {
options = options || {}
Expand Down
6 changes: 3 additions & 3 deletions src/files/url-source.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict'

const HTTP = require('../http')
const HTTP = require('../http.js')

/**
*
* @param {string} url
* @param {import("../types").HTTPOptions} [options]
* @param {import('../types.d.ts').HTTPOptions} [options]
* @returns {{ path: string; content?: AsyncIterable<Uint8Array> }}
*/
const urlSource = (url, options) => {
Expand All @@ -18,7 +18,7 @@ const urlSource = (url, options) => {
/**
*
* @param {string} url
* @param {import("../types").HTTPOptions} [options]
* @param {import('../types.d.ts').HTTPOptions} [options]
* @returns {AsyncIterable<Uint8Array>}
*/
async function * readURLContent (url, options) {
Expand Down
75 changes: 8 additions & 67 deletions src/http.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
/* eslint-disable no-undef */
'use strict'

const { fetch, Request, Headers } = require('./http/fetch')
const { TimeoutError, HTTPError } = require('./http/error')
const merge = require('merge-options').bind({ ignoreUndefined: true })
const { fetch, Request, Headers } = require('./http/fetch.js')
const { TimeoutError, HTTPError } = require('./http/error.js')
const mergeOptions = require('merge-options')
const merge = mergeOptions.bind({ ignoreUndefined: true })
const { URL, URLSearchParams } = require('iso-url')
const anySignal = require('any-signal')
const browserReableStreamToIt = require('browser-readablestream-to-it')
const { isBrowser, isWebWorker } = require('./env')
const { isBrowser, isWebWorker } = require('./env.js')
const all = require('it-all')

/**
* @typedef {import('stream').Readable} NodeReadableStream
* @typedef {import('./types').HTTPOptions} HTTPOptions
* @typedef {import('./types').ExtendedResponse} ExtendedResponse
* @typedef {import('./types.d.ts').HTTPOptions} HTTPOptions
* @typedef {import('./types.d.ts').ExtendedResponse} ExtendedResponse
*/

/**
Expand Down Expand Up @@ -254,53 +254,14 @@ const ndjson = async function * (source) {
* Stream to AsyncIterable
*
* @template TChunk
* @param {ReadableStream<TChunk> | NodeReadableStream | null} source
* @param {ReadableStream<TChunk> | AsyncIterable<TChunk> | null} source
* @returns {AsyncIterable<TChunk>}
*/
const fromStream = (source) => {
if (isAsyncIterable(source)) {
return source
}

// Workaround for https://github.com/node-fetch/node-fetch/issues/766
if (isNodeReadableStream(source)) {
const iter = source[Symbol.asyncIterator]()
return {
[Symbol.asyncIterator] () {
return {
next: iter.next.bind(iter),
return (value) {
source.destroy()
if (typeof iter.return === 'function') {
return iter.return()
}
return Promise.resolve({ done: true, value })
}
}
}
}
}

if (isWebReadableStream(source)) {
const reader = source.getReader()
return (async function * () {
try {
while (true) {
// Read from the stream
const { done, value } = await reader.read()
// Exit if we're done
if (done) return
// Else yield the chunk
if (value) {
yield value
}
}
} finally {
reader.releaseLock()
}
})()
}

throw new TypeError('Body can\'t be converted to AsyncIterable')
}

Expand All @@ -318,26 +279,6 @@ const isAsyncIterable = (value) => {
typeof /** @type {any} */(value)[Symbol.asyncIterator] === 'function'
}

/**
* Check for web readable stream
*
* @template {unknown} TChunk
* @template {any} Other
* @param {Other|ReadableStream<TChunk>} value
* @returns {value is ReadableStream<TChunk>}
*/
const isWebReadableStream = (value) => {
return value && typeof /** @type {any} */(value).getReader === 'function'
}

/**
* @param {any} value
* @returns {value is NodeReadableStream}
*/
const isNodeReadableStream = (value) =>
Object.prototype.hasOwnProperty.call(value, 'readable') &&
Object.prototype.hasOwnProperty.call(value, 'writable')

HTTP.HTTPError = HTTPError
HTTP.TimeoutError = TimeoutError
HTTP.streamToAsyncIterator = fromStream
Expand Down
9 changes: 6 additions & 3 deletions src/http/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ class TimeoutError extends Error {
this.name = 'TimeoutError'
}
}
exports.TimeoutError = TimeoutError

class AbortError extends Error {
constructor (message = 'The operation was aborted.') {
super(message)
this.name = 'AbortError'
}
}
exports.AbortError = AbortError

class HTTPError extends Error {
/**
Expand All @@ -26,4 +24,9 @@ class HTTPError extends Error {
this.response = response
}
}
exports.HTTPError = HTTPError

module.exports = {
AbortError,
HTTPError,
TimeoutError
}
Loading