@adobe/fetch
Advanced tools
Comparing version 3.3.1 to 3.4.0
{ | ||
"name": "@adobe/fetch", | ||
"version": "3.3.1", | ||
"version": "3.4.0", | ||
"description": "Light-weight Fetch implementation transparently supporting both HTTP/1(.1) and HTTP/2", | ||
"main": "src/index.js", | ||
"main": "./src/index.js", | ||
"module": "./src/index.js", | ||
"sideEffects": false, | ||
"type": "module", | ||
"scripts": { | ||
"test": "nyc mocha", | ||
"lint": "eslint .", | ||
"test": "c8 mocha", | ||
"test-ci": "c8 mocha", | ||
"semantic-release": "semantic-release", | ||
@@ -13,3 +17,3 @@ "prepare": "husky install" | ||
"mocha": { | ||
"timeout": "10000", | ||
"timeout": "5000", | ||
"recursive": "true", | ||
@@ -20,9 +24,5 @@ "reporter": "mocha-multi-reporters", | ||
"engines": { | ||
"node": ">=12.0" | ||
"node": ">=14.16" | ||
}, | ||
"types": "src/index.d.ts", | ||
"exports": { | ||
"import": "./src/index.mjs", | ||
"require": "./src/index.js" | ||
}, | ||
"repository": { | ||
@@ -56,3 +56,3 @@ "type": "git", | ||
"debug": "4.3.4", | ||
"http-cache-semantics": "4.1.0", | ||
"http-cache-semantics": "4.1.1", | ||
"lru-cache": "7.14.1" | ||
@@ -63,2 +63,3 @@ }, | ||
"@semantic-release/git": "10.0.1", | ||
"c8": "7.12.0", | ||
"chai": "4.3.7", | ||
@@ -68,15 +69,14 @@ "chai-as-promised": "7.1.1", | ||
"chai-iterator": "3.0.2", | ||
"eslint": "8.30.0", | ||
"eslint": "8.32.0", | ||
"eslint-config-airbnb-base": "15.0.0", | ||
"eslint-plugin-header": "3.1.1", | ||
"eslint-plugin-import": "2.26.0", | ||
"formdata-node": "4.4.1", | ||
"husky": "8.0.2", | ||
"eslint-plugin-import": "2.27.5", | ||
"formdata-node": "5.0.0", | ||
"husky": "8.0.3", | ||
"lint-staged": "13.1.0", | ||
"mocha": "10.2.0", | ||
"mocha-multi-reporters": "1.5.1", | ||
"nock": "13.2.9", | ||
"nyc": "15.1.0", | ||
"nock": "13.3.0", | ||
"parse-cache-control": "1.0.1", | ||
"semantic-release": "19.0.5", | ||
"semantic-release": "20.1.0", | ||
"sinon": "15.0.1", | ||
@@ -83,0 +83,0 @@ "stream-buffers": "3.0.2" |
@@ -21,2 +21,3 @@ <div align="center"> | ||
- [Features](#features) | ||
- [ESM/CJS support](#esmcjs-support) | ||
- [Installation](#installation) | ||
@@ -98,2 +99,7 @@ - [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 | ||
@@ -103,3 +109,3 @@ | ||
> | ||
> As of v2 Node version >= 12 is required. | ||
> As of v4 Node version >= 14.16 is required. | ||
@@ -235,3 +241,3 @@ ```bash | ||
```javascript | ||
const { fetch } = require('@adobe/fetch'); | ||
import { fetch } from '@adobe/fetch'; | ||
@@ -250,3 +256,3 @@ const resp = await fetch('https://httpbin.org/get'); | ||
```javascript | ||
const { fetch } = require('@adobe/fetch'); | ||
import { fetch } from '@adobe/fetch'; | ||
@@ -260,3 +266,3 @@ const resp = await fetch('https://httpbin.org/json'); | ||
```javascript | ||
const { fetch } = require('@adobe/fetch'); | ||
import { fetch } from '@adobe/fetch'; | ||
@@ -270,3 +276,3 @@ const resp = await fetch('https://httpbin.org/'); | ||
```javascript | ||
const { fetch } = require('@adobe/fetch'); | ||
import { fetch } from '@adobe/fetch'; | ||
@@ -282,3 +288,3 @@ const resp = await fetch('https://httpbin.org//stream-bytes/65535'); | ||
```javascript | ||
const { fetch, timeoutSignal, AbortError } = require('@adobe/fetch'); | ||
import { fetch, timeoutSignal, AbortError } from '@adobe/fetch'; | ||
@@ -302,3 +308,3 @@ const signal = timeoutSignal(1000); | ||
```javascript | ||
const { fetch, AbortController, AbortError } = require('@adobe/fetch'); | ||
import { fetch, AbortController, AbortError } from '@adobe/fetch'; | ||
@@ -325,7 +331,7 @@ const controller = new AbortController(); | ||
```javascript | ||
const fs = require('fs'); | ||
const { fetch } = require('@adobe/fetch'); | ||
import { createWriteStream } from 'fs'; | ||
import { fetch } from '@adobe/fetch'; | ||
const resp = await fetch('https://httpbin.org/image/jpeg'); | ||
resp.body.pipe(fs.createWriteStream('saved-image.jpg')); | ||
resp.body.pipe(createWriteStream('saved-image.jpg')); | ||
``` | ||
@@ -336,3 +342,3 @@ | ||
```javascript | ||
const { fetch } = require('@adobe/fetch'); | ||
import { fetch } from '@adobe/fetch'; | ||
@@ -347,7 +353,7 @@ const method = 'POST'; | ||
```javascript | ||
const fs = require('fs'); | ||
const { fetch } = require('@adobe/fetch'); | ||
import { createReadStream } from 'fs'; | ||
import { fetch } from '@adobe/fetch'; | ||
const method = 'POST'; | ||
const body = fs.createReadStream('some-image.jpg'); | ||
const body = createReadStream('some-image.jpg'); | ||
const headers = { 'content-type': 'image/jpeg' }; | ||
@@ -360,6 +366,6 @@ const resp = await fetch('https://httpbin.org/post', { method, body, headers }); | ||
```javascript | ||
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 { 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 { fetch } = require('@adobe/fetch'); | ||
import { fetch } from '@adobe/fetch'; | ||
@@ -379,3 +385,3 @@ const method = 'POST'; | ||
```javascript | ||
const { createUrl, fetch } = require('@adobe/fetch'); | ||
import { createUrl, fetch } from '@adobe/fetch'; | ||
@@ -394,3 +400,3 @@ const qs = { | ||
```javascript | ||
const { fetch } = require('@adobe/fetch'); | ||
import { fetch } from '@adobe/fetch'; | ||
@@ -411,3 +417,3 @@ const body = new URLSearchParams({ | ||
```javascript | ||
const { fetch } = require('@adobe/fetch'); | ||
import { fetch } from '@adobe/fetch'; | ||
@@ -429,3 +435,3 @@ const url = 'https://httpbin.org/cache/60'; // -> max-age=60 (seconds) | ||
```javascript | ||
const { fetch } = require('@adobe/fetch'); | ||
import { fetch } from '@adobe/fetch'; | ||
@@ -440,3 +446,4 @@ const resp = await fetch('https://httbin.org/', { cache: 'no-store' }); | ||
```javascript | ||
const { fetch } = require('@adobe/fetch').noCache(); | ||
import { noCache } from '@adobe/fetch'; | ||
const { fetch } = noCache(); | ||
``` | ||
@@ -452,3 +459,3 @@ | ||
```javascript | ||
const { fetch, onPush } = require('@adobe/fetch'); | ||
import { fetch, onPush } from '@adobe/fetch'; | ||
@@ -464,3 +471,4 @@ onPush((url, response) => console.log(`received server push: ${url} status ${response.status}`)); | ||
```javascript | ||
const { fetch } = require('@adobe/fetch').h1(); | ||
import { h1 } from '@adobe/fetch'; | ||
const { fetch } = h1(); | ||
@@ -474,3 +482,4 @@ const resp = await fetch('https://nghttp2.org'); | ||
```javascript | ||
const { fetch } = require('@adobe/fetch').keepAlive(); | ||
import { keepAlive } from '@adobe/fetch'; | ||
const { fetch } = keepAlive(); | ||
@@ -486,3 +495,3 @@ const resp = await fetch('https://httpbin.org/status/200'); | ||
```javascript | ||
const { fetch } = require('@adobe/fetch'); | ||
import { fetch } from '@adobe/fetch'; | ||
@@ -497,3 +506,4 @@ const resp = await fetch('https://httpbin.org/cookies/set?a=1&b=2'); | ||
```javascript | ||
const { fetch } = require('@adobe/fetch').context({ rejectUnauthorized: false }); | ||
import { context } from '@adobe/fetch'; | ||
const { fetch } = context({ rejectUnauthorized: false }); | ||
@@ -506,3 +516,4 @@ const resp = await fetch('https://localhost:8443/'); // a server using a self-signed certificate | ||
```javascript | ||
const { fetch, cacheStats } = require('@adobe/fetch').context({ | ||
import { context } from '@adobe/fetch'; | ||
const { fetch } = context({ | ||
maxCacheSize: 100 * 1024, // 100kb (Default: 100mb) | ||
@@ -519,3 +530,4 @@ }); | ||
```javascript | ||
const { fetch } = require('@adobe/fetch').noCache(); | ||
import { noCache } from '@adobe/fetch'; | ||
const { fetch } = noCache(); | ||
@@ -531,3 +543,4 @@ let resp = await fetch('https://httpbin.org/cache/60'); // -> max-age=60 (seconds) | ||
```javascript | ||
const { fetch } = require('@adobe/fetch').context({ | ||
import { context } from '@adobe/fetch'; | ||
const { fetch } = context({ | ||
userAgent: 'custom-fetch' | ||
@@ -534,0 +547,0 @@ }); |
@@ -13,7 +13,5 @@ /* | ||
'use strict'; | ||
import { randomBytes } from 'crypto'; | ||
import { Readable } from 'stream'; | ||
const { randomBytes } = require('crypto'); | ||
const { Readable } = require('stream'); | ||
// Misc. helper functions for dealing with spec-compliant FormData objects | ||
@@ -134,4 +132,4 @@ | ||
module.exports = { | ||
export { | ||
isFormData, FormDataSerializer, | ||
}; |
@@ -13,20 +13,15 @@ /* | ||
'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 { 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 = debugFactory('helix-fetch:utils'); | ||
const { MAX_LENGTH: maxBufferLength } = bufferConstants; | ||
const { Z_SYNC_FLUSH } = zlibConstants; | ||
const debug = require('debug')('adobe/fetch:utils'); | ||
const asyncPipeline = promisify(pipeline); | ||
@@ -73,3 +68,3 @@ | ||
/* istanbul ignore next */ | ||
/* c8 ignore next 4 */ | ||
default: | ||
@@ -183,3 +178,3 @@ // dead branch since it's covered by shouldDecode already; | ||
passThroughStream.on('data', (chunk) => { | ||
/* istanbul ignore next */ | ||
/* c8 ignore next 3 */ | ||
if ((length + chunk.length) > maxBufferLength) { | ||
@@ -196,4 +191,4 @@ throw new Error('Buffer.constants.MAX_SIZE exceeded'); | ||
module.exports = { | ||
export { | ||
decodeStream, isPlainObject, sizeof, streamToBuffer, | ||
}; |
@@ -13,4 +13,2 @@ /* | ||
'use strict'; | ||
/** | ||
@@ -29,2 +27,3 @@ * Error thrown if a request is aborted via an AbortSignal. | ||
module.exports = { RequestAbortedError }; | ||
// eslint-disable-next-line import/prefer-default-export | ||
export { RequestAbortedError }; |
@@ -13,12 +13,12 @@ /* | ||
'use strict'; | ||
import http from 'http'; | ||
import https from 'https'; | ||
import { Readable } from 'stream'; | ||
const http = require('http'); | ||
const https = require('https'); | ||
const { Readable } = require('stream'); | ||
import debugFactory from 'debug'; | ||
const debug = require('debug')('adobe/fetch:h1'); | ||
import { RequestAbortedError } from './errors.js'; | ||
import { decodeStream } from '../common/utils.js'; | ||
const { RequestAbortedError } = require('./errors'); | ||
const { decodeStream } = require('../common/utils'); | ||
const debug = debugFactory('helix-fetch:h1'); | ||
@@ -109,3 +109,3 @@ const getAgent = (ctx, protocol) => { | ||
delete opts.socket; | ||
/* istanbul ignore next */ | ||
/* c8 ignore next 27 */ | ||
if (!socket.assigned) { | ||
@@ -149,3 +149,3 @@ socket.assigned = true; | ||
signal.removeEventListener('abort', onAbortSignal); | ||
/* istanbul ignore next */ | ||
/* c8 ignore next 5 */ | ||
if (socket && !socket.inUse) { | ||
@@ -157,3 +157,2 @@ // we have no use for the passed socket | ||
reject(new RequestAbortedError()); | ||
/* istanbul ignore else */ | ||
if (req) { | ||
@@ -176,3 +175,3 @@ req.abort(); | ||
} | ||
/* istanbul ignore next */ | ||
/* c8 ignore next 5 */ | ||
if (socket && !socket.inUse) { | ||
@@ -187,7 +186,6 @@ // we have no use for the passed socket | ||
// error occured during the request | ||
/* istanbul ignore else */ | ||
if (signal) { | ||
signal.removeEventListener('abort', onAbortSignal); | ||
} | ||
/* istanbul ignore next */ | ||
/* c8 ignore next 5 */ | ||
if (socket && !socket.inUse) { | ||
@@ -198,3 +196,3 @@ // we have no use for the passed socket | ||
} | ||
/* istanbul ignore next */ | ||
/* c8 ignore next 6 */ | ||
if (!req.aborted) { | ||
@@ -219,2 +217,2 @@ debug(`${opts.method} ${url.href} failed with: ${err.message}`); | ||
module.exports = { request: h1Request, setupContext, resetContext }; | ||
export default { request: h1Request, setupContext, resetContext }; |
@@ -13,18 +13,11 @@ /* | ||
'use strict'; | ||
import { connect, constants } from 'http2'; | ||
import { Readable } from 'stream'; | ||
const { | ||
// ClientHttp2Session, | ||
// ClientHttp2Stream, | ||
connect, | ||
constants, | ||
// IncomingHttpHeaders, | ||
// SecureClientSessionOptions, | ||
} = require('http2'); | ||
const { Readable } = require('stream'); | ||
import debugFactory from 'debug'; | ||
const debug = require('debug')('adobe/fetch:h2'); | ||
import { RequestAbortedError } from './errors.js'; | ||
import { decodeStream } from '../common/utils.js'; | ||
const { RequestAbortedError } = require('./errors'); | ||
const { decodeStream } = require('../common/utils'); | ||
const debug = debugFactory('helix-fetch:h2'); | ||
@@ -55,3 +48,3 @@ const { NGHTTP2_CANCEL } = constants; | ||
decode, | ||
/* istanbul ignore next */ onError = () => {}, | ||
/* c8 ignore next */ onError = () => {}, | ||
) => { | ||
@@ -106,3 +99,4 @@ const hdrs = { ...headers }; | ||
// set timeout to automatically discard pushed streams that aren't consumed for some time | ||
pushedStream.setTimeout(pushedStreamIdleTimeout, /* istanbul ignore next */ () => { | ||
pushedStream.setTimeout(pushedStreamIdleTimeout, () => { | ||
/* c8 ignore next 2 */ | ||
debug(`closing pushed stream #${pushedStream.id} after ${pushedStreamIdleTimeout} ms of inactivity`); | ||
@@ -112,3 +106,2 @@ pushedStream.close(NGHTTP2_CANCEL); | ||
/* istanbul ignore else */ | ||
if (pushHandler) { | ||
@@ -119,9 +112,12 @@ pushHandler(url, requestHeaders, createResponse(responseHeaders, pushedStream, decode)); | ||
// log stream errors | ||
pushedStream.on('aborted', /* istanbul ignore next */ () => { | ||
pushedStream.on('aborted', () => { | ||
/* c8 ignore next */ | ||
debug(`pushed stream #${pushedStream.id} aborted`); | ||
}); | ||
pushedStream.on('error', /* istanbul ignore next */ (err) => { | ||
pushedStream.on('error', (err) => { | ||
/* c8 ignore next */ | ||
debug(`pushed stream #${pushedStream.id} encountered error: ${err}`); | ||
}); | ||
pushedStream.on('frameError', /* istanbul ignore next */ (type, code, id) => { | ||
pushedStream.on('frameError', (type, code, id) => { | ||
/* c8 ignore next */ | ||
debug(`pushed stream #${pushedStream.id} encountered frameError: type: ${type}, code: ${code}, id: ${id}`); | ||
@@ -162,3 +158,2 @@ }); | ||
} | ||
/* istanbul ignore else */ | ||
if (headers.host) { | ||
@@ -208,3 +203,2 @@ headers[':authority'] = headers.host; | ||
debug(`session ${origin} closed`); | ||
/* istanbul ignore else */ | ||
if (sessionCache[origin] === session) { | ||
@@ -215,3 +209,3 @@ debug(`discarding cached session ${origin}`); | ||
}); | ||
session.once('error', /* istanbul ignore next */ (err) => { | ||
session.once('error', (err) => { | ||
debug(`session ${origin} encountered error: ${err}`); | ||
@@ -224,7 +218,8 @@ if (sessionCache[origin] === session) { | ||
}); | ||
session.on('frameError', /* istanbul ignore next */ (type, code, id) => { | ||
session.on('frameError', (type, code, id) => { | ||
/* c8 ignore next */ | ||
debug(`session ${origin} encountered frameError: type: ${type}, code: ${code}, id: ${id}`); | ||
}); | ||
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.once('goaway', (errorCode, lastStreamID, opaqueData) => { | ||
debug(`session ${origin} received GOAWAY frame: errorCode: ${errorCode}, lastStreamID: ${lastStreamID}, opaqueData: ${opaqueData ? /* c8 ignore next */ opaqueData.toString() : undefined}`); | ||
// session will be closed automatically | ||
@@ -237,3 +232,3 @@ }); | ||
// we have a cached session | ||
/* istanbul ignore next */ | ||
/* c8 ignore next 6 */ | ||
// eslint-disable-next-line no-lonely-if | ||
@@ -255,3 +250,2 @@ if (socket && socket.id !== session.socket.id && !socket.inUse) { | ||
reject(new RequestAbortedError()); | ||
/* istanbul ignore else */ | ||
if (req) { | ||
@@ -269,3 +263,4 @@ req.close(NGHTTP2_CANCEL); | ||
const onSessionError = /* istanbul ignore next */ (err) => { | ||
/* c8 ignore next 4 */ | ||
const onSessionError = (err) => { | ||
debug(`session ${origin} encountered error during ${opts.method} ${url.href}: ${err}`); | ||
@@ -288,3 +283,2 @@ reject(err); | ||
session.off('error', onSessionError); | ||
/* istanbul ignore next */ | ||
if (signal) { | ||
@@ -294,3 +288,2 @@ signal.removeEventListener('abort', onAbortSignal); | ||
// if (!req.aborted) { | ||
/* istanbul ignore else */ | ||
if (req.rstCode !== NGHTTP2_CANCEL) { | ||
@@ -302,7 +295,9 @@ debug(`${opts.method} ${url.href} failed with: ${err.message}`); | ||
}); | ||
req.once('frameError', /* istanbul ignore next */ (type, code, id) => { | ||
req.once('frameError', (type, code, id) => { | ||
/* c8 ignore next 2 */ | ||
session.off('error', onSessionError); | ||
debug(`encountered frameError during ${opts.method} ${url.href}: type: ${type}, code: ${code}, id: ${id}`); | ||
}); | ||
req.on('push', /* istanbul ignore next */ (hdrs, flags) => { | ||
req.on('push', (hdrs, flags) => { | ||
/* c8 ignore next */ | ||
debug(`received 'push' event: headers: ${JSON.stringify(hdrs)}, flags: ${flags}`); | ||
@@ -322,2 +317,2 @@ }); | ||
module.exports = { request, setupContext, resetContext }; | ||
export default { request, setupContext, resetContext }; |
@@ -13,7 +13,5 @@ /* | ||
'use strict'; | ||
import debugFactory from 'debug'; | ||
const debug = require('debug')('adobe/fetch:core'); | ||
const { | ||
import { | ||
request, | ||
@@ -27,4 +25,6 @@ setupContext, | ||
ALPN_HTTP1_0, | ||
} = require('./request'); | ||
} from './request.js'; | ||
const debug = debugFactory('helix-fetch:core'); | ||
class RequestContext { | ||
@@ -89,2 +89,2 @@ constructor(options) { | ||
module.exports = new RequestContext().api(); | ||
export default new RequestContext().api(); |
@@ -13,6 +13,4 @@ /* | ||
'use strict'; | ||
import { EventEmitter } from 'events'; | ||
const { EventEmitter } = require('events'); | ||
/** | ||
@@ -68,2 +66,2 @@ * Creates a lock (mutex) for asynchronous resources. | ||
module.exports = lock; | ||
export default lock; |
@@ -13,19 +13,24 @@ /* | ||
'use strict'; | ||
import { types } from 'util'; | ||
import { readFileSync } from 'fs'; | ||
import { Readable } from 'stream'; | ||
import tls from 'tls'; | ||
const { Readable } = require('stream'); | ||
const tls = require('tls'); | ||
const { types: { isAnyArrayBuffer } } = require('util'); | ||
import LRU from 'lru-cache'; | ||
import debugFactory from 'debug'; | ||
const LRU = require('lru-cache'); | ||
const debug = require('debug')('adobe/fetch:core'); | ||
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 { 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 { isAnyArrayBuffer } = types; | ||
const { version } = require('../../package.json'); | ||
const debug = debugFactory('helix-fetch:core'); | ||
@@ -63,3 +68,2 @@ const ALPN_HTTP2 = 'h2'; | ||
reject(err); | ||
/* istanbul ignore else */ | ||
if (socket) { | ||
@@ -98,5 +102,2 @@ 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})`); | ||
@@ -179,3 +180,3 @@ resolve(socket); | ||
protocol = socket.alpnProtocol; | ||
/* istanbul ignore if */ | ||
/* c8 ignore next 3 */ | ||
if (!protocol) { | ||
@@ -203,3 +204,2 @@ protocol = ALPN_HTTP1_1; // default fallback | ||
// sanitze method name | ||
/* istanbul ignore else */ | ||
if (typeof opts.method === 'string') { | ||
@@ -215,3 +215,2 @@ opts.method = opts.method.toUpperCase(); | ||
// User-Agent header | ||
/* istanbul ignore else */ | ||
if (ctx.userAgent) { | ||
@@ -232,3 +231,2 @@ if (opts.headers['user-agent'] === undefined) { | ||
opts.body = fd.stream(); | ||
/* istanbul ignore else */ | ||
if (opts.headers['transfer-encoding'] === undefined | ||
@@ -260,3 +258,2 @@ && opts.headers['content-length'] === undefined) { | ||
// string or buffer body | ||
/* istanbul ignore else */ | ||
if (opts.headers['transfer-encoding'] === undefined | ||
@@ -304,8 +301,10 @@ && opts.headers['content-length'] === undefined) { | ||
new URL(`http://${url.host}${url.pathname}${url.hash}${url.search}`), | ||
socket ? /* istanbul ignore next */ { ...opts, socket } : opts, | ||
/* c8 ignore next */ | ||
socket ? { ...opts, socket } : opts, | ||
); | ||
/* istanbul ignore next */ case ALPN_HTTP1_0: | ||
/* c8 ignore next */ case ALPN_HTTP1_0: | ||
case ALPN_HTTP1_1: | ||
return h1.request(ctx, url, socket ? { ...opts, socket } : opts); | ||
/* istanbul ignore next */ | ||
/* c8 ignore next 4 */ | ||
default: | ||
@@ -344,3 +343,3 @@ // dead branch: only here to make eslint stop complaining | ||
module.exports = { | ||
export { | ||
request, | ||
@@ -347,0 +346,0 @@ setupContext, |
@@ -15,6 +15,4 @@ /* | ||
'use strict'; | ||
import { EventEmitter } from 'events'; | ||
const { EventEmitter } = require('events'); | ||
const SIGNAL_INTERNALS = Symbol('AbortSignal internals'); | ||
@@ -147,2 +145,2 @@ | ||
module.exports = { AbortController, AbortSignal, TimeoutSignal }; | ||
export { AbortController, AbortSignal, TimeoutSignal }; |
@@ -13,9 +13,9 @@ /* | ||
'use strict'; | ||
import { PassThrough, Readable } from 'stream'; | ||
import { types } from 'util'; | ||
const { PassThrough, Readable } = require('stream'); | ||
const { types: { isAnyArrayBuffer } } = require('util'); | ||
import { FetchError, FetchBaseError } from './errors.js'; | ||
import { streamToBuffer } from '../common/utils.js'; | ||
const { FetchError, FetchBaseError } = require('./errors'); | ||
const { streamToBuffer } = require('../common/utils'); | ||
const { isAnyArrayBuffer } = types; | ||
@@ -187,3 +187,2 @@ const EMPTY_BUFFER = Buffer.alloc(0); | ||
/* istanbul ignore else */ | ||
if (stream instanceof Readable) { | ||
@@ -228,3 +227,2 @@ result = new PassThrough(); | ||
/* istanbul ignore else */ | ||
if (body instanceof Readable) { | ||
@@ -238,3 +236,3 @@ return null; | ||
module.exports = { | ||
export { | ||
Body, | ||
@@ -241,0 +239,0 @@ cloneStream, |
@@ -15,9 +15,7 @@ /* | ||
'use strict'; | ||
import { Readable } from 'stream'; | ||
const { Readable } = require('stream'); | ||
import Headers from './headers.js'; | ||
import Response from './response.js'; | ||
const { Headers } = require('./headers'); | ||
const { Response } = require('./response'); | ||
const INTERNALS = Symbol('CacheableResponse internals'); | ||
@@ -137,2 +135,2 @@ | ||
module.exports = { cacheableResponse }; | ||
export default cacheableResponse; |
@@ -15,4 +15,2 @@ /* | ||
'use strict'; | ||
class FetchBaseError extends Error { | ||
@@ -62,2 +60,2 @@ constructor(message, type) { | ||
module.exports = { FetchBaseError, FetchError, AbortError }; | ||
export { FetchBaseError, FetchError, AbortError }; |
@@ -13,7 +13,7 @@ /* | ||
'use strict'; | ||
import http from 'http'; | ||
const { validateHeaderName, validateHeaderValue } = require('http'); | ||
import { isPlainObject } from '../common/utils.js'; | ||
const { isPlainObject } = require('../common/utils'); | ||
const { validateHeaderName, validateHeaderValue } = http; | ||
@@ -24,16 +24,3 @@ const INTERNALS = Symbol('Headers internals'); | ||
const nm = typeof name !== 'string' ? String(name) : name; | ||
/* 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; | ||
} | ||
} | ||
validateHeaderName(nm); | ||
return nm.toLowerCase(); | ||
@@ -44,16 +31,3 @@ }; | ||
const val = typeof value !== 'string' ? String(value) : value; | ||
/* 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; | ||
} | ||
} | ||
validateHeaderValue(name, val); | ||
return val; | ||
@@ -94,3 +68,3 @@ }; | ||
}); | ||
} else /* istanbul ignore else */ if (isPlainObject(init)) { | ||
} else if (isPlainObject(init)) { | ||
for (const [name, value] of Object.entries(init)) { | ||
@@ -231,4 +205,2 @@ if (Array.isArray(value)) { | ||
module.exports = { | ||
Headers, | ||
}; | ||
export default Headers; |
@@ -13,24 +13,24 @@ /* | ||
'use strict'; | ||
import { EventEmitter } from 'events'; | ||
import { Readable } from 'stream'; | ||
const { EventEmitter } = require('events'); | ||
const { Readable } = require('stream'); | ||
import debugFactory from 'debug'; | ||
import LRU from 'lru-cache'; | ||
const debug = require('debug')('adobe/fetch'); | ||
const LRU = require('lru-cache'); | ||
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'; | ||
// core abstraction layer | ||
import core from '../core/index.js'; | ||
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'); | ||
const { context, RequestAbortedError } = core; | ||
const debug = debugFactory('helix-fetch'); | ||
// core abstraction layer | ||
const { context, RequestAbortedError } = require('../core'); | ||
const CACHEABLE_METHODS = ['GET', 'HEAD']; | ||
@@ -53,3 +53,3 @@ const DEFAULT_MAX_CACHE_ITEMS = 500; | ||
const req = url instanceof Request && typeof options === 'undefined' ? url : /* istanbul ignore next */ new Request(url, options); | ||
const req = url instanceof Request && typeof options === 'undefined' ? url : /* c8 ignore next */ new Request(url, options); | ||
@@ -66,3 +66,2 @@ // extract options | ||
// cleanup request | ||
/* istanbul ignore else */ | ||
if (req.init.body instanceof Readable) { | ||
@@ -94,3 +93,3 @@ req.init.body.destroy(err); | ||
} | ||
/* istanbul ignore next */ | ||
/* c8 ignore next 3 */ | ||
if (err instanceof TypeError) { | ||
@@ -112,3 +111,2 @@ throw err; | ||
// cleanup request | ||
/* istanbul ignore else */ | ||
if (req.init.body instanceof Readable) { | ||
@@ -203,3 +201,3 @@ req.init.body.destroy(err); | ||
/* istanbul ignore next */ | ||
/* c8 ignore next */ | ||
default: | ||
@@ -589,2 +587,2 @@ // fall through | ||
module.exports = new FetchContext().api(); | ||
export default new FetchContext().api(); |
@@ -13,8 +13,6 @@ /* | ||
'use strict'; | ||
import CachePolicy from 'http-cache-semantics'; | ||
const CachePolicy = require('http-cache-semantics'); | ||
import Headers from './headers.js'; | ||
const { Headers } = require('./headers'); | ||
/** | ||
@@ -120,2 +118,2 @@ * | ||
module.exports = CachePolicyWrapper; | ||
export default CachePolicyWrapper; |
@@ -13,11 +13,8 @@ /* | ||
'use strict'; | ||
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'; | ||
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; | ||
@@ -60,3 +57,2 @@ | ||
// spec-compliant FormData | ||
/* istanbul ignore else */ | ||
if (!headers.has('content-type')) { | ||
@@ -66,3 +62,2 @@ const fd = new FormDataSerializer(body); | ||
headers.set('content-type', fd.contentType()); | ||
/* istanbul ignore else */ | ||
if (!headers.has('transfer-encoding') | ||
@@ -205,4 +200,2 @@ && !headers.has('content-length')) { | ||
module.exports = { | ||
Request, | ||
}; | ||
export default Request; |
@@ -13,10 +13,7 @@ /* | ||
'use strict'; | ||
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'; | ||
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'); | ||
@@ -44,3 +41,2 @@ | ||
// spec-compliant FormData | ||
/* istanbul ignore else */ | ||
if (!headers.has('content-type')) { | ||
@@ -50,3 +46,2 @@ const fd = new FormDataSerializer(respBody); | ||
headers.set('content-type', fd.contentType()); | ||
/* istanbul ignore else */ | ||
if (!headers.has('transfer-encoding') | ||
@@ -170,4 +165,2 @@ && !headers.has('content-length')) { | ||
module.exports = { | ||
Response, | ||
}; | ||
export default Response; |
/* | ||
* Copyright 2020 Adobe. All rights reserved. | ||
* Copyright 2022 Adobe. All rights reserved. | ||
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
@@ -13,4 +13,39 @@ * you may not use this file except in compliance with the License. You may obtain a copy | ||
'use strict'; | ||
import api from './fetch/index.js'; | ||
module.exports = require('./fetch'); | ||
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; |
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
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
595
Yes
141812
23
3260
1
1
+ Addedhttp-cache-semantics@4.1.1(transitive)
- Removedhttp-cache-semantics@4.1.0(transitive)
Updatedhttp-cache-semantics@4.1.1