@adobe/fetch
Advanced tools
Comparing version 3.5.1 to 3.6.0
{ | ||
"name": "@adobe/fetch", | ||
"version": "3.5.1", | ||
"version": "3.6.0", | ||
"description": "Light-weight Fetch implementation transparently supporting both HTTP/1(.1) and HTTP/2", | ||
"main": "./src/index.js", | ||
"module": "./src/index.js", | ||
"sideEffects": false, | ||
"type": "module", | ||
"main": "src/index.js", | ||
"scripts": { | ||
"test": "nyc mocha", | ||
"lint": "eslint .", | ||
"test": "c8 mocha", | ||
"test-ci": "c8 mocha", | ||
"semantic-release": "semantic-release", | ||
@@ -17,3 +13,3 @@ "prepare": "husky install" | ||
"mocha": { | ||
"timeout": "5000", | ||
"timeout": "10000", | ||
"recursive": "true", | ||
@@ -24,5 +20,9 @@ "reporter": "mocha-multi-reporters", | ||
"engines": { | ||
"node": ">=14.16" | ||
"node": ">=12.0" | ||
}, | ||
"types": "src/index.d.ts", | ||
"exports": { | ||
"import": "./src/index.mjs", | ||
"require": "./src/index.js" | ||
}, | ||
"repository": { | ||
@@ -62,3 +62,2 @@ "type": "git", | ||
"@semantic-release/git": "10.0.1", | ||
"c8": "7.12.0", | ||
"chai": "4.3.7", | ||
@@ -72,3 +71,3 @@ "chai-as-promised": "7.1.1", | ||
"eslint-plugin-import": "2.27.5", | ||
"formdata-node": "5.0.0", | ||
"formdata-node": "4.4.1", | ||
"husky": "8.0.3", | ||
@@ -79,2 +78,3 @@ "lint-staged": "13.1.0", | ||
"nock": "13.3.0", | ||
"nyc": "15.1.0", | ||
"parse-cache-control": "1.0.1", | ||
@@ -81,0 +81,0 @@ "semantic-release": "20.1.0", |
@@ -21,3 +21,2 @@ <div align="center"> | ||
- [Features](#features) | ||
- [ESM/CJS support](#esmcjs-support) | ||
- [Installation](#installation) | ||
@@ -99,7 +98,2 @@ - [API](#api) | ||
## ESM/CJS support | ||
This package is native [ESM](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) and no longer provides CommonJS exports. Use `3.x` version if you still need to use this package with CommonJS. | ||
## Installation | ||
@@ -109,3 +103,3 @@ | ||
> | ||
> As of v4 Node version >= 14.16 is required. | ||
> As of v2 Node version >= 12 is required. | ||
@@ -241,3 +235,3 @@ ```bash | ||
```javascript | ||
import { fetch } from '@adobe/fetch'; | ||
const { fetch } = require('@adobe/fetch'); | ||
@@ -256,3 +250,3 @@ const resp = await fetch('https://httpbin.org/get'); | ||
```javascript | ||
import { fetch } from '@adobe/fetch'; | ||
const { fetch } = require('@adobe/fetch'); | ||
@@ -266,3 +260,3 @@ const resp = await fetch('https://httpbin.org/json'); | ||
```javascript | ||
import { fetch } from '@adobe/fetch'; | ||
const { fetch } = require('@adobe/fetch'); | ||
@@ -276,3 +270,3 @@ const resp = await fetch('https://httpbin.org/'); | ||
```javascript | ||
import { fetch } from '@adobe/fetch'; | ||
const { fetch } = require('@adobe/fetch'); | ||
@@ -288,3 +282,3 @@ const resp = await fetch('https://httpbin.org//stream-bytes/65535'); | ||
```javascript | ||
import { fetch, timeoutSignal, AbortError } from '@adobe/fetch'; | ||
const { fetch, timeoutSignal, AbortError } = require('@adobe/fetch'); | ||
@@ -308,3 +302,3 @@ const signal = timeoutSignal(1000); | ||
```javascript | ||
import { fetch, AbortController, AbortError } from '@adobe/fetch'; | ||
const { fetch, AbortController, AbortError } = require('@adobe/fetch'); | ||
@@ -331,7 +325,7 @@ const controller = new AbortController(); | ||
```javascript | ||
import { createWriteStream } from 'fs'; | ||
import { fetch } from '@adobe/fetch'; | ||
const fs = require('fs'); | ||
const { fetch } = require('@adobe/fetch'); | ||
const resp = await fetch('https://httpbin.org/image/jpeg'); | ||
resp.body.pipe(createWriteStream('saved-image.jpg')); | ||
resp.body.pipe(fs.createWriteStream('saved-image.jpg')); | ||
``` | ||
@@ -342,3 +336,3 @@ | ||
```javascript | ||
import { fetch } from '@adobe/fetch'; | ||
const { fetch } = require('@adobe/fetch'); | ||
@@ -353,7 +347,7 @@ const method = 'POST'; | ||
```javascript | ||
import { createReadStream } from 'fs'; | ||
import { fetch } from '@adobe/fetch'; | ||
const fs = require('fs'); | ||
const { fetch } = require('@adobe/fetch'); | ||
const method = 'POST'; | ||
const body = createReadStream('some-image.jpg'); | ||
const body = fs.createReadStream('some-image.jpg'); | ||
const headers = { 'content-type': 'image/jpeg' }; | ||
@@ -366,6 +360,6 @@ const resp = await fetch('https://httpbin.org/post', { method, body, headers }); | ||
```javascript | ||
import { FormData, Blob, File } from 'formdata-node'; // spec-compliant implementations | ||
import { fileFromPath } from 'formdata-node/file-from-path'; // helper for creating File instance from disk file | ||
const { FormData, Blob, File } = require('formdata-node'); // spec-compliant implementations | ||
const { fileFromPath } = require('formdata-node/file-from-path'); // helper for creating File instance from disk file | ||
import { fetch } from '@adobe/fetch'; | ||
const { fetch } = require('@adobe/fetch'); | ||
@@ -385,3 +379,3 @@ const method = 'POST'; | ||
```javascript | ||
import { createUrl, fetch } from '@adobe/fetch'; | ||
const { createUrl, fetch } = require('@adobe/fetch'); | ||
@@ -400,3 +394,3 @@ const qs = { | ||
```javascript | ||
import { fetch } from '@adobe/fetch'; | ||
const { fetch } = require('@adobe/fetch'); | ||
@@ -417,3 +411,3 @@ const body = new URLSearchParams({ | ||
```javascript | ||
import { fetch } from '@adobe/fetch'; | ||
const { fetch } = require('@adobe/fetch'); | ||
@@ -435,3 +429,3 @@ const url = 'https://httpbin.org/cache/60'; // -> max-age=60 (seconds) | ||
```javascript | ||
import { fetch } from '@adobe/fetch'; | ||
const { fetch } = require('@adobe/fetch'); | ||
@@ -446,4 +440,3 @@ const resp = await fetch('https://httbin.org/', { cache: 'no-store' }); | ||
```javascript | ||
import { noCache } from '@adobe/fetch'; | ||
const { fetch } = noCache(); | ||
const { fetch } = require('@adobe/fetch').noCache(); | ||
``` | ||
@@ -459,3 +452,3 @@ | ||
```javascript | ||
import { fetch, onPush } from '@adobe/fetch'; | ||
const { fetch, onPush } = require('@adobe/fetch'); | ||
@@ -471,4 +464,3 @@ onPush((url, response) => console.log(`received server push: ${url} status ${response.status}`)); | ||
```javascript | ||
import { h1 } from '@adobe/fetch'; | ||
const { fetch } = h1(); | ||
const { fetch } = require('@adobe/fetch').h1(); | ||
@@ -482,4 +474,3 @@ const resp = await fetch('https://nghttp2.org'); | ||
```javascript | ||
import { keepAlive } from '@adobe/fetch'; | ||
const { fetch } = keepAlive(); | ||
const { fetch } = require('@adobe/fetch').keepAlive(); | ||
@@ -495,3 +486,3 @@ const resp = await fetch('https://httpbin.org/status/200'); | ||
```javascript | ||
import { fetch } from '@adobe/fetch'; | ||
const { fetch } = require('@adobe/fetch'); | ||
@@ -506,4 +497,3 @@ const resp = await fetch('https://httpbin.org/cookies/set?a=1&b=2'); | ||
```javascript | ||
import { context } from '@adobe/fetch'; | ||
const { fetch } = context({ rejectUnauthorized: false }); | ||
const { fetch } = require('@adobe/fetch').context({ rejectUnauthorized: false }); | ||
@@ -516,4 +506,3 @@ const resp = await fetch('https://localhost:8443/'); // a server using a self-signed certificate | ||
```javascript | ||
import { context } from '@adobe/fetch'; | ||
const { fetch } = context({ | ||
const { fetch, cacheStats } = require('@adobe/fetch').context({ | ||
maxCacheSize: 100 * 1024, // 100kb (Default: 100mb) | ||
@@ -530,4 +519,3 @@ }); | ||
```javascript | ||
import { noCache } from '@adobe/fetch'; | ||
const { fetch } = noCache(); | ||
const { fetch } = require('@adobe/fetch').noCache(); | ||
@@ -543,4 +531,3 @@ let resp = await fetch('https://httpbin.org/cache/60'); // -> max-age=60 (seconds) | ||
```javascript | ||
import { context } from '@adobe/fetch'; | ||
const { fetch } = context({ | ||
const { fetch } = require('@adobe/fetch').context({ | ||
userAgent: 'custom-fetch' | ||
@@ -547,0 +534,0 @@ }); |
@@ -13,5 +13,7 @@ /* | ||
import { randomBytes } from 'crypto'; | ||
import { Readable } from 'stream'; | ||
'use strict'; | ||
const { randomBytes } = require('crypto'); | ||
const { Readable } = require('stream'); | ||
// Misc. helper functions for dealing with spec-compliant FormData objects | ||
@@ -132,4 +134,4 @@ | ||
export { | ||
module.exports = { | ||
isFormData, FormDataSerializer, | ||
}; |
@@ -13,15 +13,20 @@ /* | ||
'use strict'; | ||
/* eslint-disable guard-for-in */ | ||
import { constants as bufferConstants } from 'buffer'; | ||
import { pipeline, PassThrough } from 'stream'; | ||
import { promisify } from 'util'; | ||
import { | ||
createGunzip, createInflate, createBrotliDecompress, constants as zlibConstants, | ||
} from 'zlib'; | ||
import debugFactory from 'debug'; | ||
const debug = debugFactory('helix-fetch:utils'); | ||
const { MAX_LENGTH: maxBufferLength } = bufferConstants; | ||
const { Z_SYNC_FLUSH } = zlibConstants; | ||
const { constants: { MAX_LENGTH: maxBufferLength } } = require('buffer'); | ||
const { pipeline, PassThrough } = require('stream'); | ||
const { promisify } = require('util'); | ||
const { | ||
createGunzip, | ||
createInflate, | ||
createBrotliDecompress, | ||
constants: { | ||
Z_SYNC_FLUSH, | ||
}, | ||
} = require('zlib'); | ||
const debug = require('debug')('adobe/fetch:utils'); | ||
const asyncPipeline = promisify(pipeline); | ||
@@ -68,3 +73,3 @@ | ||
/* c8 ignore next 4 */ | ||
/* istanbul ignore next */ | ||
default: | ||
@@ -178,3 +183,3 @@ // dead branch since it's covered by shouldDecode already; | ||
passThroughStream.on('data', (chunk) => { | ||
/* c8 ignore next 3 */ | ||
/* istanbul ignore next */ | ||
if ((length + chunk.length) > maxBufferLength) { | ||
@@ -191,4 +196,4 @@ throw new Error('Buffer.constants.MAX_SIZE exceeded'); | ||
export { | ||
module.exports = { | ||
decodeStream, isPlainObject, sizeof, streamToBuffer, | ||
}; |
@@ -13,2 +13,4 @@ /* | ||
'use strict'; | ||
/** | ||
@@ -27,3 +29,2 @@ * Error thrown if a request is aborted via an AbortSignal. | ||
// eslint-disable-next-line import/prefer-default-export | ||
export { RequestAbortedError }; | ||
module.exports = { RequestAbortedError }; |
@@ -13,12 +13,12 @@ /* | ||
import http from 'http'; | ||
import https from 'https'; | ||
import { Readable } from 'stream'; | ||
'use strict'; | ||
import debugFactory from 'debug'; | ||
const http = require('http'); | ||
const https = require('https'); | ||
const { Readable } = require('stream'); | ||
import { RequestAbortedError } from './errors.js'; | ||
import { decodeStream } from '../common/utils.js'; | ||
const debug = require('debug')('adobe/fetch:h1'); | ||
const debug = debugFactory('helix-fetch:h1'); | ||
const { RequestAbortedError } = require('./errors'); | ||
const { decodeStream } = require('../common/utils'); | ||
@@ -109,3 +109,3 @@ const getAgent = (ctx, protocol) => { | ||
delete opts.socket; | ||
/* c8 ignore next 27 */ | ||
/* istanbul ignore next */ | ||
if (!socket.assigned) { | ||
@@ -149,3 +149,3 @@ socket.assigned = true; | ||
signal.removeEventListener('abort', onAbortSignal); | ||
/* c8 ignore next 5 */ | ||
/* istanbul ignore next */ | ||
if (socket && !socket.inUse) { | ||
@@ -157,2 +157,3 @@ // we have no use for the passed socket | ||
reject(new RequestAbortedError()); | ||
/* istanbul ignore else */ | ||
if (req) { | ||
@@ -175,3 +176,3 @@ req.abort(); | ||
} | ||
/* c8 ignore next 5 */ | ||
/* istanbul ignore next */ | ||
if (socket && !socket.inUse) { | ||
@@ -186,6 +187,7 @@ // we have no use for the passed socket | ||
// error occured during the request | ||
/* istanbul ignore else */ | ||
if (signal) { | ||
signal.removeEventListener('abort', onAbortSignal); | ||
} | ||
/* c8 ignore next 5 */ | ||
/* istanbul ignore next */ | ||
if (socket && !socket.inUse) { | ||
@@ -196,3 +198,3 @@ // we have no use for the passed socket | ||
} | ||
/* c8 ignore next 6 */ | ||
/* istanbul ignore next */ | ||
if (!req.aborted) { | ||
@@ -217,2 +219,2 @@ debug(`${opts.method} ${url.href} failed with: ${err.message}`); | ||
export default { request: h1Request, setupContext, resetContext }; | ||
module.exports = { request: h1Request, setupContext, resetContext }; |
@@ -13,11 +13,18 @@ /* | ||
import { connect, constants } from 'http2'; | ||
import { Readable } from 'stream'; | ||
'use strict'; | ||
import debugFactory from 'debug'; | ||
const { | ||
// ClientHttp2Session, | ||
// ClientHttp2Stream, | ||
connect, | ||
constants, | ||
// IncomingHttpHeaders, | ||
// SecureClientSessionOptions, | ||
} = require('http2'); | ||
const { Readable } = require('stream'); | ||
import { RequestAbortedError } from './errors.js'; | ||
import { decodeStream } from '../common/utils.js'; | ||
const debug = require('debug')('adobe/fetch:h2'); | ||
const debug = debugFactory('helix-fetch:h2'); | ||
const { RequestAbortedError } = require('./errors'); | ||
const { decodeStream } = require('../common/utils'); | ||
@@ -48,3 +55,3 @@ const { NGHTTP2_CANCEL } = constants; | ||
decode, | ||
/* c8 ignore next */ onError = () => {}, | ||
/* istanbul ignore next */ onError = () => {}, | ||
) => { | ||
@@ -99,4 +106,3 @@ const hdrs = { ...headers }; | ||
// set timeout to automatically discard pushed streams that aren't consumed for some time | ||
pushedStream.setTimeout(pushedStreamIdleTimeout, () => { | ||
/* c8 ignore next 2 */ | ||
pushedStream.setTimeout(pushedStreamIdleTimeout, /* istanbul ignore next */ () => { | ||
debug(`closing pushed stream #${pushedStream.id} after ${pushedStreamIdleTimeout} ms of inactivity`); | ||
@@ -106,2 +112,3 @@ pushedStream.close(NGHTTP2_CANCEL); | ||
/* istanbul ignore else */ | ||
if (pushHandler) { | ||
@@ -112,12 +119,9 @@ pushHandler(url, requestHeaders, createResponse(responseHeaders, pushedStream, decode)); | ||
// log stream errors | ||
pushedStream.on('aborted', () => { | ||
/* c8 ignore next */ | ||
pushedStream.on('aborted', /* istanbul ignore next */ () => { | ||
debug(`pushed stream #${pushedStream.id} aborted`); | ||
}); | ||
pushedStream.on('error', (err) => { | ||
/* c8 ignore next */ | ||
pushedStream.on('error', /* istanbul ignore next */ (err) => { | ||
debug(`pushed stream #${pushedStream.id} encountered error: ${err}`); | ||
}); | ||
pushedStream.on('frameError', (type, code, id) => { | ||
/* c8 ignore next */ | ||
pushedStream.on('frameError', /* istanbul ignore next */ (type, code, id) => { | ||
debug(`pushed stream #${pushedStream.id} encountered frameError: type: ${type}, code: ${code}, id: ${id}`); | ||
@@ -158,2 +162,3 @@ }); | ||
} | ||
/* istanbul ignore else */ | ||
if (headers.host) { | ||
@@ -203,2 +208,3 @@ headers[':authority'] = headers.host; | ||
debug(`session ${origin} closed`); | ||
/* istanbul ignore else */ | ||
if (sessionCache[origin] === session) { | ||
@@ -209,3 +215,3 @@ debug(`discarding cached session ${origin}`); | ||
}); | ||
session.once('error', (err) => { | ||
session.once('error', /* istanbul ignore next */ (err) => { | ||
debug(`session ${origin} encountered error: ${err}`); | ||
@@ -218,8 +224,7 @@ if (sessionCache[origin] === session) { | ||
}); | ||
session.on('frameError', (type, code, id) => { | ||
/* c8 ignore next */ | ||
session.on('frameError', /* istanbul ignore next */ (type, code, id) => { | ||
debug(`session ${origin} encountered frameError: type: ${type}, code: ${code}, id: ${id}`); | ||
}); | ||
session.once('goaway', (errorCode, lastStreamID, opaqueData) => { | ||
debug(`session ${origin} received GOAWAY frame: errorCode: ${errorCode}, lastStreamID: ${lastStreamID}, opaqueData: ${opaqueData ? /* c8 ignore next */ opaqueData.toString() : undefined}`); | ||
session.once('goaway', /* istanbul ignore next */ (errorCode, lastStreamID, opaqueData) => { | ||
debug(`session ${origin} received GOAWAY frame: errorCode: ${errorCode}, lastStreamID: ${lastStreamID}, opaqueData: ${opaqueData ? opaqueData.toString() : undefined}`); | ||
// session will be closed automatically | ||
@@ -232,3 +237,3 @@ }); | ||
// we have a cached session | ||
/* c8 ignore next 6 */ | ||
/* istanbul ignore next */ | ||
// eslint-disable-next-line no-lonely-if | ||
@@ -250,2 +255,3 @@ if (socket && socket.id !== session.socket.id && !socket.inUse) { | ||
reject(new RequestAbortedError()); | ||
/* istanbul ignore else */ | ||
if (req) { | ||
@@ -263,4 +269,3 @@ req.close(NGHTTP2_CANCEL); | ||
/* c8 ignore next 4 */ | ||
const onSessionError = (err) => { | ||
const onSessionError = /* istanbul ignore next */ (err) => { | ||
debug(`session ${origin} encountered error during ${opts.method} ${url.href}: ${err}`); | ||
@@ -283,2 +288,3 @@ reject(err); | ||
session.off('error', onSessionError); | ||
/* istanbul ignore next */ | ||
if (signal) { | ||
@@ -288,2 +294,3 @@ signal.removeEventListener('abort', onAbortSignal); | ||
// if (!req.aborted) { | ||
/* istanbul ignore else */ | ||
if (req.rstCode !== NGHTTP2_CANCEL) { | ||
@@ -295,9 +302,7 @@ debug(`${opts.method} ${url.href} failed with: ${err.message}`); | ||
}); | ||
req.once('frameError', (type, code, id) => { | ||
/* c8 ignore next 2 */ | ||
req.once('frameError', /* istanbul ignore next */ (type, code, id) => { | ||
session.off('error', onSessionError); | ||
debug(`encountered frameError during ${opts.method} ${url.href}: type: ${type}, code: ${code}, id: ${id}`); | ||
}); | ||
req.on('push', (hdrs, flags) => { | ||
/* c8 ignore next */ | ||
req.on('push', /* istanbul ignore next */ (hdrs, flags) => { | ||
debug(`received 'push' event: headers: ${JSON.stringify(hdrs)}, flags: ${flags}`); | ||
@@ -317,2 +322,2 @@ }); | ||
export default { request, setupContext, resetContext }; | ||
module.exports = { request, setupContext, resetContext }; |
@@ -13,5 +13,7 @@ /* | ||
import debugFactory from 'debug'; | ||
'use strict'; | ||
import { | ||
const debug = require('debug')('adobe/fetch:core'); | ||
const { | ||
request, | ||
@@ -25,6 +27,4 @@ setupContext, | ||
ALPN_HTTP1_0, | ||
} from './request.js'; | ||
} = require('./request'); | ||
const debug = debugFactory('helix-fetch:core'); | ||
class RequestContext { | ||
@@ -89,2 +89,2 @@ constructor(options) { | ||
export default new RequestContext().api(); | ||
module.exports = new RequestContext().api(); |
@@ -13,4 +13,6 @@ /* | ||
import { EventEmitter } from 'events'; | ||
'use strict'; | ||
const { EventEmitter } = require('events'); | ||
/** | ||
@@ -66,2 +68,2 @@ * Creates a lock (mutex) for asynchronous resources. | ||
export default lock; | ||
module.exports = lock; |
@@ -13,24 +13,19 @@ /* | ||
import { types } from 'util'; | ||
import { readFileSync } from 'fs'; | ||
import { Readable } from 'stream'; | ||
import tls from 'tls'; | ||
'use strict'; | ||
import LRU from 'lru-cache'; | ||
import debugFactory from 'debug'; | ||
const { Readable } = require('stream'); | ||
const tls = require('tls'); | ||
const { types: { isAnyArrayBuffer } } = require('util'); | ||
import { RequestAbortedError } from './errors.js'; | ||
import h1 from './h1.js'; | ||
import h2 from './h2.js'; | ||
import lock from './lock.js'; | ||
import { isFormData, FormDataSerializer } from '../common/formData.js'; | ||
import { isPlainObject } from '../common/utils.js'; | ||
// as of node v16 support for importing JSON modules is still experimental | ||
// import pkg from '../../package.json'; | ||
const pkg = JSON.parse(readFileSync(new URL('../../package.json', import.meta.url))); | ||
const { version } = pkg; | ||
const LRU = require('lru-cache'); | ||
const debug = require('debug')('adobe/fetch:core'); | ||
const { isAnyArrayBuffer } = types; | ||
const { RequestAbortedError } = require('./errors'); | ||
const h1 = require('./h1'); | ||
const h2 = require('./h2'); | ||
const lock = require('./lock'); | ||
const { isPlainObject } = require('../common/utils'); | ||
const { isFormData, FormDataSerializer } = require('../common/formData'); | ||
const debug = debugFactory('helix-fetch:core'); | ||
const { version } = require('../../package.json'); | ||
@@ -68,2 +63,3 @@ const ALPN_HTTP2 = 'h2'; | ||
reject(err); | ||
/* istanbul ignore else */ | ||
if (socket) { | ||
@@ -102,2 +98,5 @@ socket.destroy(err); | ||
socket.id = socketIdCounter; | ||
// workaround for node >= 12.17.0 regression | ||
// (see https://github.com/nodejs/node/pull/34859) | ||
socket.secureConnecting = false; | ||
debug(`established TLS connection: #${socket.id} (${socket.servername})`); | ||
@@ -180,3 +179,3 @@ resolve(socket); | ||
protocol = socket.alpnProtocol; | ||
/* c8 ignore next 3 */ | ||
/* istanbul ignore if */ | ||
if (!protocol) { | ||
@@ -204,2 +203,3 @@ protocol = ALPN_HTTP1_1; // default fallback | ||
// sanitze method name | ||
/* istanbul ignore else */ | ||
if (typeof opts.method === 'string') { | ||
@@ -215,2 +215,3 @@ opts.method = opts.method.toUpperCase(); | ||
// User-Agent header | ||
/* istanbul ignore else */ | ||
if (ctx.userAgent) { | ||
@@ -231,2 +232,3 @@ if (opts.headers['user-agent'] === undefined) { | ||
opts.body = fd.stream(); | ||
/* istanbul ignore else */ | ||
if (opts.headers['transfer-encoding'] === undefined | ||
@@ -258,2 +260,3 @@ && opts.headers['content-length'] === undefined) { | ||
// string or buffer body | ||
/* istanbul ignore else */ | ||
if (opts.headers['transfer-encoding'] === undefined | ||
@@ -301,10 +304,8 @@ && opts.headers['content-length'] === undefined) { | ||
new URL(`http://${url.host}${url.pathname}${url.hash}${url.search}`), | ||
/* c8 ignore next */ | ||
socket ? { ...opts, socket } : opts, | ||
socket ? /* istanbul ignore next */ { ...opts, socket } : opts, | ||
); | ||
/* c8 ignore next */ case ALPN_HTTP1_0: | ||
/* istanbul ignore next */ case ALPN_HTTP1_0: | ||
case ALPN_HTTP1_1: | ||
return h1.request(ctx, url, socket ? { ...opts, socket } : opts); | ||
/* c8 ignore next 4 */ | ||
/* istanbul ignore next */ | ||
default: | ||
@@ -343,3 +344,3 @@ // dead branch: only here to make eslint stop complaining | ||
export { | ||
module.exports = { | ||
request, | ||
@@ -346,0 +347,0 @@ setupContext, |
@@ -15,4 +15,6 @@ /* | ||
import { EventEmitter } from 'events'; | ||
'use strict'; | ||
const { EventEmitter } = require('events'); | ||
const SIGNAL_INTERNALS = Symbol('AbortSignal internals'); | ||
@@ -145,2 +147,2 @@ | ||
export { AbortController, AbortSignal, TimeoutSignal }; | ||
module.exports = { AbortController, AbortSignal, TimeoutSignal }; |
@@ -13,9 +13,9 @@ /* | ||
import { PassThrough, Readable } from 'stream'; | ||
import { types } from 'util'; | ||
'use strict'; | ||
import { FetchError, FetchBaseError } from './errors.js'; | ||
import { streamToBuffer } from '../common/utils.js'; | ||
const { PassThrough, Readable } = require('stream'); | ||
const { types: { isAnyArrayBuffer } } = require('util'); | ||
const { isAnyArrayBuffer } = types; | ||
const { FetchError, FetchBaseError } = require('./errors'); | ||
const { streamToBuffer } = require('../common/utils'); | ||
@@ -187,2 +187,3 @@ const EMPTY_BUFFER = Buffer.alloc(0); | ||
/* istanbul ignore else */ | ||
if (stream instanceof Readable) { | ||
@@ -227,2 +228,3 @@ result = new PassThrough(); | ||
/* istanbul ignore else */ | ||
if (body instanceof Readable) { | ||
@@ -236,3 +238,3 @@ return null; | ||
export { | ||
module.exports = { | ||
Body, | ||
@@ -239,0 +241,0 @@ cloneStream, |
@@ -15,7 +15,9 @@ /* | ||
import { Readable } from 'stream'; | ||
'use strict'; | ||
import Headers from './headers.js'; | ||
import Response from './response.js'; | ||
const { Readable } = require('stream'); | ||
const { Headers } = require('./headers'); | ||
const { Response } = require('./response'); | ||
const INTERNALS = Symbol('CacheableResponse internals'); | ||
@@ -135,2 +137,2 @@ | ||
export default cacheableResponse; | ||
module.exports = { cacheableResponse }; |
@@ -15,2 +15,4 @@ /* | ||
'use strict'; | ||
class FetchBaseError extends Error { | ||
@@ -60,2 +62,2 @@ constructor(message, type) { | ||
export { FetchBaseError, FetchError, AbortError }; | ||
module.exports = { FetchBaseError, FetchError, AbortError }; |
@@ -13,7 +13,7 @@ /* | ||
import http from 'http'; | ||
'use strict'; | ||
import { isPlainObject } from '../common/utils.js'; | ||
const { validateHeaderName, validateHeaderValue } = require('http'); | ||
const { validateHeaderName, validateHeaderValue } = http; | ||
const { isPlainObject } = require('../common/utils'); | ||
@@ -24,3 +24,16 @@ const INTERNALS = Symbol('Headers internals'); | ||
const nm = typeof name !== 'string' ? String(name) : name; | ||
validateHeaderName(nm); | ||
/* istanbul ignore next */ | ||
if (typeof validateHeaderName === 'function') { | ||
// since node 14.3.0 | ||
validateHeaderName(nm); | ||
} else { | ||
// eslint-disable-next-line no-lonely-if | ||
if (!/^[\^`\-\w!#$%&'*+.|~]+$/.test(nm)) { | ||
const err = new TypeError(`Header name must be a valid HTTP token [${nm}]`); | ||
Object.defineProperty(err, 'code', { value: 'ERR_INVALID_HTTP_TOKEN' }); | ||
throw err; | ||
} | ||
} | ||
return nm.toLowerCase(); | ||
@@ -31,3 +44,16 @@ }; | ||
const val = typeof value !== 'string' ? String(value) : value; | ||
validateHeaderValue(name, val); | ||
/* istanbul ignore next */ | ||
if (typeof validateHeaderValue === 'function') { | ||
// since node 14.3.0 | ||
validateHeaderValue(name, val); | ||
} else { | ||
// eslint-disable-next-line no-lonely-if | ||
if (/[^\t\u0020-\u007E\u0080-\u00FF]/.test(val)) { | ||
const err = new TypeError(`Invalid character in header content ["${name}"]`); | ||
Object.defineProperty(err, 'code', { value: 'ERR_INVALID_CHAR' }); | ||
throw err; | ||
} | ||
} | ||
return val; | ||
@@ -68,3 +94,3 @@ }; | ||
}); | ||
} else if (isPlainObject(init)) { | ||
} else /* istanbul ignore else */ if (isPlainObject(init)) { | ||
for (const [name, value] of Object.entries(init)) { | ||
@@ -205,2 +231,4 @@ if (Array.isArray(value)) { | ||
export default Headers; | ||
module.exports = { | ||
Headers, | ||
}; |
@@ -13,24 +13,24 @@ /* | ||
import { EventEmitter } from 'events'; | ||
import { Readable } from 'stream'; | ||
'use strict'; | ||
import debugFactory from 'debug'; | ||
import LRU from 'lru-cache'; | ||
const { EventEmitter } = require('events'); | ||
const { Readable } = require('stream'); | ||
import { Body } from './body.js'; | ||
import Headers from './headers.js'; | ||
import Request from './request.js'; | ||
import Response from './response.js'; | ||
import { FetchBaseError, FetchError, AbortError } from './errors.js'; | ||
import { AbortController, AbortSignal, TimeoutSignal } from './abort.js'; | ||
import CachePolicy from './policy.js'; | ||
import cacheableResponse from './cacheableResponse.js'; | ||
import { sizeof } from '../common/utils.js'; | ||
import { isFormData } from '../common/formData.js'; | ||
const debug = require('debug')('adobe/fetch'); | ||
const LRU = require('lru-cache'); | ||
const { Body } = require('./body'); | ||
const { Headers } = require('./headers'); | ||
const { Request } = require('./request'); | ||
const { Response } = require('./response'); | ||
const { FetchBaseError, FetchError, AbortError } = require('./errors'); | ||
const { AbortController, AbortSignal, TimeoutSignal } = require('./abort'); | ||
const CachePolicy = require('./policy'); | ||
const { cacheableResponse } = require('./cacheableResponse'); | ||
const { sizeof } = require('../common/utils'); | ||
const { isFormData } = require('../common/formData'); | ||
// core abstraction layer | ||
import core from '../core/index.js'; | ||
const { context, RequestAbortedError } = require('../core'); | ||
const { context, RequestAbortedError } = core; | ||
const debug = debugFactory('helix-fetch'); | ||
const CACHEABLE_METHODS = ['GET', 'HEAD']; | ||
@@ -53,3 +53,3 @@ const DEFAULT_MAX_CACHE_ITEMS = 500; | ||
const req = url instanceof Request && typeof options === 'undefined' ? url : /* c8 ignore next */ new Request(url, options); | ||
const req = url instanceof Request && typeof options === 'undefined' ? url : /* istanbul ignore next */ new Request(url, options); | ||
@@ -66,2 +66,3 @@ // extract options | ||
// cleanup request | ||
/* istanbul ignore else */ | ||
if (req.init.body instanceof Readable) { | ||
@@ -93,3 +94,3 @@ req.init.body.destroy(err); | ||
} | ||
/* c8 ignore next 3 */ | ||
/* istanbul ignore next */ | ||
if (err instanceof TypeError) { | ||
@@ -111,2 +112,3 @@ throw err; | ||
// cleanup request | ||
/* istanbul ignore else */ | ||
if (req.init.body instanceof Readable) { | ||
@@ -201,3 +203,3 @@ req.init.body.destroy(err); | ||
/* c8 ignore next */ | ||
/* istanbul ignore next */ | ||
default: | ||
@@ -587,2 +589,2 @@ // fall through | ||
export default new FetchContext().api(); | ||
module.exports = new FetchContext().api(); |
@@ -13,6 +13,8 @@ /* | ||
import CachePolicy from 'http-cache-semantics'; | ||
'use strict'; | ||
import Headers from './headers.js'; | ||
const CachePolicy = require('http-cache-semantics'); | ||
const { Headers } = require('./headers'); | ||
/** | ||
@@ -118,2 +120,2 @@ * | ||
export default CachePolicyWrapper; | ||
module.exports = CachePolicyWrapper; |
@@ -13,8 +13,11 @@ /* | ||
import { AbortSignal } from './abort.js'; | ||
import { Body, cloneStream, guessContentType } from './body.js'; | ||
import Headers from './headers.js'; | ||
import { isPlainObject } from '../common/utils.js'; | ||
import { isFormData, FormDataSerializer } from '../common/formData.js'; | ||
'use strict'; | ||
const { AbortSignal } = require('./abort'); | ||
const { Body, cloneStream, guessContentType } = require('./body'); | ||
const { Headers } = require('./headers'); | ||
const { isPlainObject } = require('../common/utils'); | ||
const { isFormData, FormDataSerializer } = require('../common/formData'); | ||
const DEFAULT_FOLLOW = 20; | ||
@@ -57,2 +60,3 @@ | ||
// spec-compliant FormData | ||
/* istanbul ignore else */ | ||
if (!headers.has('content-type')) { | ||
@@ -62,2 +66,3 @@ const fd = new FormDataSerializer(body); | ||
headers.set('content-type', fd.contentType()); | ||
/* istanbul ignore else */ | ||
if (!headers.has('transfer-encoding') | ||
@@ -200,2 +205,4 @@ && !headers.has('content-length')) { | ||
export default Request; | ||
module.exports = { | ||
Request, | ||
}; |
@@ -13,7 +13,10 @@ /* | ||
import { Body, cloneStream, guessContentType } from './body.js'; | ||
import Headers from './headers.js'; | ||
import { isPlainObject } from '../common/utils.js'; | ||
import { isFormData, FormDataSerializer } from '../common/formData.js'; | ||
'use strict'; | ||
const { Body, cloneStream, guessContentType } = require('./body'); | ||
const { Headers } = require('./headers'); | ||
const { isPlainObject } = require('../common/utils'); | ||
const { isFormData, FormDataSerializer } = require('../common/formData'); | ||
const INTERNALS = Symbol('Response internals'); | ||
@@ -41,2 +44,3 @@ | ||
// spec-compliant FormData | ||
/* istanbul ignore else */ | ||
if (!headers.has('content-type')) { | ||
@@ -46,2 +50,3 @@ const fd = new FormDataSerializer(respBody); | ||
headers.set('content-type', fd.contentType()); | ||
/* istanbul ignore else */ | ||
if (!headers.has('transfer-encoding') | ||
@@ -165,2 +170,4 @@ && !headers.has('content-length')) { | ||
export default Response; | ||
module.exports = { | ||
Response, | ||
}; |
/* | ||
* Copyright 2022 Adobe. All rights reserved. | ||
* Copyright 2020 Adobe. All rights reserved. | ||
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
@@ -13,39 +13,4 @@ * you may not use this file except in compliance with the License. You may obtain a copy | ||
import api from './fetch/index.js'; | ||
'use strict'; | ||
export const ALPNProtocol = { | ||
ALPN_HTTP2: api.ALPN_HTTP2, | ||
ALPN_HTTP2C: api.ALPN_HTTP2C, | ||
ALPN_HTTP1_1: api.ALPN_HTTP1_1, | ||
ALPN_HTTP1_0: api.ALPN_HTTP1_0, | ||
}; | ||
export const { | ||
fetch, | ||
context, | ||
reset, | ||
noCache, | ||
h1, | ||
keepAlive, | ||
h1NoCache, | ||
keepAliveNoCache, | ||
cacheStats, | ||
clearCache, | ||
offPush, | ||
onPush, | ||
createUrl, | ||
timeoutSignal, | ||
Body, | ||
Headers, | ||
Request, | ||
Response, | ||
AbortController, | ||
AbortError, | ||
AbortSignal, | ||
FetchBaseError, | ||
FetchError, | ||
ALPN_HTTP2, | ||
ALPN_HTTP2C, | ||
ALPN_HTTP1_1, | ||
ALPN_HTTP1_0, | ||
} = api; | ||
module.exports = require('./fetch'); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
144014
24
3328
0
0
582
No