electron-fetch
Advanced tools
Comparing version 1.7.4 to 1.8.0-0
import { Readable, Stream } from 'stream' | ||
import { Session } from 'electron' | ||
import { AuthInfo, Session } from 'electron' | ||
import { Agent } from 'https' | ||
@@ -12,4 +12,20 @@ | ||
export class FetchError extends Error {} | ||
export enum FetchErrorType { | ||
BodyTimeout = "body-timeout", | ||
System = "system", | ||
MaxSize = "max-size", | ||
Abort = "abort", | ||
RequestTimeout = "request-timeout", | ||
Proxy = "proxy", | ||
NoRedirect = "no-redirect", | ||
MaxRedirect = "max-redirect", | ||
InvalidRedirect = "invalid-redirect", | ||
} | ||
export class FetchError extends Error { | ||
constructor(message: string, type: FetchErrorType, systemError?: { code: number }); | ||
type: string; | ||
code?: number; | ||
} | ||
export type HeadersInit = Headers | string[][] | { [key: string]: string } | ||
@@ -114,2 +130,7 @@ | ||
password?: string | ||
/** | ||
* When running on Electron behind an authenticated HTTP proxy, handler of `electron.ClientRequest`'s `login` event. | ||
* Can be used for acquiring proxy credentials in an async manner (e.g. prompting the user). | ||
*/ | ||
onLogin?: (authInfo: AuthInfo) => Promise<{ username: string, password: string } | undefined> | ||
} | ||
@@ -116,0 +137,0 @@ |
@@ -531,3 +531,4 @@ import { parse, format, resolve } from 'url'; | ||
// istanbul ignore next | ||
if (body._lengthRetrievers && body._lengthRetrievers.length === 0 || body.hasKnownLength && body.hasKnownLength()) { | ||
if (body._lengthRetrievers && body._lengthRetrievers.length === 0 || // 1.x | ||
body.hasKnownLength && body.hasKnownLength()) { | ||
// 2.x | ||
@@ -1297,2 +1298,3 @@ return body.getLengthSync(); | ||
if (opts.agent) options.agent = opts.agent; | ||
if (opts.onLogin) reject(new Error('"onLogin" option is only supported with "useElectronNet" enabled')); | ||
} | ||
@@ -1347,5 +1349,16 @@ | ||
callback(opts.user, opts.password); | ||
} else if (opts.onLogin) { | ||
opts.onLogin(authInfo).then(credentials => { | ||
if (credentials) { | ||
callback(credentials.username, credentials.password); | ||
} else { | ||
callback(); | ||
} | ||
}).catch(error => { | ||
cancelRequest(); | ||
reject(error); | ||
}); | ||
} else { | ||
cancelRequest(); | ||
reject(new FetchError(`login event received from ${authInfo.host} but no credentials provided`, 'proxy', { | ||
reject(new FetchError(`login event received from ${authInfo.host} but no credentials or onLogin handler provided`, 'proxy', { | ||
code: 'PROXY_AUTH_FAILED' | ||
@@ -1352,0 +1365,0 @@ })); |
@@ -23,5 +23,3 @@ 'use strict'; | ||
enumerable: true, | ||
get: function () { | ||
return e[k]; | ||
} | ||
get: function () { return e[k]; } | ||
}); | ||
@@ -31,3 +29,3 @@ } | ||
} | ||
n['default'] = e; | ||
n["default"] = e; | ||
return Object.freeze(n); | ||
@@ -253,3 +251,3 @@ } | ||
body = null; | ||
} else if (typeof body === 'string') ; else if (body instanceof Blob) ; else if (Buffer.isBuffer(body)) ; else if (body instanceof Stream__default['default']) ; else { | ||
} else if (typeof body === 'string') ; else if (body instanceof Blob) ; else if (Buffer.isBuffer(body)) ; else if (body instanceof Stream__default["default"]) ; else { | ||
// none of the above | ||
@@ -378,3 +376,3 @@ // coerce to string | ||
if (!(this.body instanceof Stream__default['default'])) { | ||
if (!(this.body instanceof Stream__default["default"])) { | ||
return Promise.resolve(Buffer.alloc(0)); | ||
@@ -499,3 +497,3 @@ } // body is stream | ||
if (body instanceof Stream__default['default'] && typeof body.getBoundary !== 'function') { | ||
if (body instanceof Stream__default["default"] && typeof body.getBoundary !== 'function') { | ||
// tee instance body | ||
@@ -566,3 +564,4 @@ p1 = new Stream.PassThrough(); | ||
// istanbul ignore next | ||
if (body._lengthRetrievers && body._lengthRetrievers.length === 0 || body.hasKnownLength && body.hasKnownLength()) { | ||
if (body._lengthRetrievers && body._lengthRetrievers.length === 0 || // 1.x | ||
body.hasKnownLength && body.hasKnownLength()) { | ||
// 2.x | ||
@@ -1332,2 +1331,3 @@ return body.getLengthSync(); | ||
if (opts.agent) options.agent = opts.agent; | ||
if (opts.onLogin) reject(new Error('"onLogin" option is only supported with "useElectronNet" enabled')); | ||
} | ||
@@ -1382,5 +1382,16 @@ | ||
callback(opts.user, opts.password); | ||
} else if (opts.onLogin) { | ||
opts.onLogin(authInfo).then(credentials => { | ||
if (credentials) { | ||
callback(credentials.username, credentials.password); | ||
} else { | ||
callback(); | ||
} | ||
}).catch(error => { | ||
cancelRequest(); | ||
reject(error); | ||
}); | ||
} else { | ||
cancelRequest(); | ||
reject(new FetchError(`login event received from ${authInfo.host} but no credentials provided`, 'proxy', { | ||
reject(new FetchError(`login event received from ${authInfo.host} but no credentials or onLogin handler provided`, 'proxy', { | ||
code: 'PROXY_AUTH_FAILED' | ||
@@ -1556,2 +1567,2 @@ })); | ||
exports.Response = Response; | ||
exports['default'] = fetch; | ||
exports["default"] = fetch; |
{ | ||
"name": "electron-fetch", | ||
"version": "1.7.4", | ||
"version": "1.8.0-0", | ||
"description": "A light-weight module that brings window.fetch to electron's background process", | ||
@@ -26,3 +26,3 @@ "main": "lib/index.js", | ||
"coverage": "xvfb-maybe cross-env BABEL_ENV=coverage electron-mocha --require @babel/register test/test.js --reporter test/coverage-reporter.js", | ||
"report": "npm run lint && npm run test:typings && npm run coverage && codecov -f coverage/coverage-final.json" | ||
"report": "npm run coverage && codecov -f coverage/coverage-final.json" | ||
}, | ||
@@ -46,20 +46,20 @@ "repository": { | ||
"devDependencies": { | ||
"@babel/core": "^7.15.0", | ||
"@babel/preset-env": "^7.15.0", | ||
"@babel/register": "^7.15.3", | ||
"@babel/core": "^7.17.8", | ||
"@babel/preset-env": "^7.16.11", | ||
"@babel/register": "^7.17.7", | ||
"abortcontroller-polyfill": "^1.7.3", | ||
"babel-eslint": "^10.1.0", | ||
"babel-plugin-istanbul": "^6.0.0", | ||
"babel-plugin-istanbul": "^6.1.1", | ||
"basic-auth-parser": "0.0.2", | ||
"chai": "^4.3.4", | ||
"chai": "^4.3.6", | ||
"chai-as-promised": "^7.1.1", | ||
"codecov": "^3.8.3", | ||
"cross-env": "^7.0.3", | ||
"electron": "^13.2.1", | ||
"electron-mocha": "^10.1.0", | ||
"electron": "^17.1.2", | ||
"electron-mocha": "^11.0.2", | ||
"form-data": "^4.0.0", | ||
"is-builtin-module": "^3.1.0", | ||
"istanbul-api": "^3.0.0", | ||
"istanbul-lib-coverage": "^3.0.0", | ||
"mocha": "^9.1.0", | ||
"istanbul-lib-coverage": "^3.2.0", | ||
"mocha": "^9.2.2", | ||
"nyc": "^15.1.0", | ||
@@ -70,8 +70,9 @@ "parted": "^0.1.1", | ||
"resumer": "0.0.0", | ||
"rollup": "^2.56.3", | ||
"rollup": "^2.70.1", | ||
"rollup-plugin-babel": "^4.4.0", | ||
"standard": "^16.0.3", | ||
"ts-node": "^10.2.1", | ||
"typescript": "^4.3.5", | ||
"whatwg-url": "^9.1.0", | ||
"standard": "^16.0.4", | ||
"stoppable": "^1.1.0", | ||
"ts-node": "^10.7.0", | ||
"typescript": "^4.6.2", | ||
"whatwg-url": "^11.0.0", | ||
"xvfb-maybe": "^0.2.1" | ||
@@ -78,0 +79,0 @@ }, |
@@ -164,2 +164,10 @@ | ||
})() | ||
// providing proxy credentials (electron-specific) | ||
fetch(url, { | ||
onLogin (authInfo) { // this 'authInfo' is the one received by the 'login' event. See https://www.electronjs.org/docs/latest/api/client-request#event-login | ||
return Promise.resolve({ username: 'testuser', password: 'testpassword' }) | ||
} | ||
}) | ||
``` | ||
@@ -205,3 +213,4 @@ | ||
user: undefined, // When running on Electron behind an authenticated HTTP proxy, username to use to authenticate | ||
password: undefined // When running on Electron behind an authenticated HTTP proxy, password to use to authenticate | ||
password: undefined, // When running on Electron behind an authenticated HTTP proxy, password to use to authenticate | ||
onLogin: undefined // When running on Electron behind an authenticated HTTP proxy, handler of electron.ClientRequest's login event. Can be used for acquiring proxy credentials in an async manner (e.g. prompting the user). Receives an `AuthInfo` object, and must return a `Promise<{ username: string, password: string }>`. | ||
} | ||
@@ -214,9 +223,9 @@ ``` | ||
Header | Value | ||
----------------- | -------------------------------------------------------- | ||
`Accept-Encoding` | `gzip,deflate` | ||
`Accept` | `*/*` | ||
`Connection` | `close` | ||
`Content-Length` | _(automatically calculated, if possible)_ | ||
`User-Agent` | `electron-fetch/1.0 (+https://github.com/arantes555/electron-fetch)` | ||
| Header | Value | | ||
|-------------------|----------------------------------------------------------------------| | ||
| `Accept-Encoding` | `gzip,deflate` | | ||
| `Accept` | `*/*` | | ||
| `Connection` | `close` | | ||
| `Content-Length` | _(automatically calculated, if possible)_ | | ||
| `User-Agent` | `electron-fetch/1.0 (+https://github.com/arantes555/electron-fetch)` | | ||
@@ -395,4 +404,4 @@ <a id="class-request"></a> | ||
[npm-url]: https://www.npmjs.com/package/electron-fetch | ||
[travis-image]: https://img.shields.io/travis/arantes555/electron-fetch.svg?style=flat-square | ||
[travis-url]: https://travis-ci.org/arantes555/electron-fetch | ||
[travis-image]: https://img.shields.io/travis/com/arantes555/electron-fetch.svg?style=flat-square | ||
[travis-url]: https://travis-ci.com/arantes555/electron-fetch | ||
[codecov-image]: https://img.shields.io/codecov/c/github/arantes555/electron-fetch.svg?style=flat-square | ||
@@ -399,0 +408,0 @@ [codecov-url]: https://codecov.io/gh/arantes555/electron-fetch |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
115375
7
2672
410
31
1