abort-polyfill
Advanced tools
Comparing version 0.0.1 to 0.1.0
126
index.js
@@ -1,120 +0,4 @@ | ||
/** | ||
* @fileoverview | ||
* | ||
* @see https://mdn.io/AbortSignal | ||
* @see https://github.com/nodejs/node/blob/master/lib/internal/abort_controller.js | ||
* @see https://github.com/denoland/deno/blob/master/op_crates/web/02_abort_signal.js | ||
*/ | ||
function impl() { | ||
const hasSymbol = typeof Symbol === 'function' | ||
const getSymbol = hasSymbol ? Symbol : (v) => v | ||
const kAborted = getSymbol('kAborted') | ||
const kSignal = getSymbol('kSignal') | ||
import {AbortSignal, AbortController} from './abort' | ||
import {abortable} from './fetch' | ||
// NOTE: not using `class extends`, making it compatible with IE 11 and Firefox 59+ | ||
// see https://caniuse.com/mdn-api_eventtarget_eventtarget | ||
function AbortSignal() { | ||
throw new TypeError('Illegal constructor') | ||
} | ||
AbortSignal.prototype = Object.create(EventTarget.prototype, { | ||
constructor: {value: AbortSignal}, | ||
aborted: { | ||
enumerable: true, | ||
get() { | ||
return this[kAborted] | ||
}, | ||
}, | ||
}) | ||
const kHandlers = getSymbol('kHandlers') | ||
function getHandlers(self) { | ||
if (!self[kHandlers]) { | ||
self[kHandlers] = new Map() | ||
} | ||
return self[kHandlers] | ||
} | ||
function defineEventHandler(emitter, name) { | ||
Object.defineProperty(emitter, 'on' + name, { | ||
get() { | ||
return getHandlers(this).get(name) || null | ||
}, | ||
set(value) { | ||
const handlers = getHandlers(this) | ||
const handler = handlers.get(name) | ||
if (handler) { | ||
this.removeEventListener(name, handler) | ||
} | ||
if (typeof value === 'function') { | ||
const newHandler = value.bind(this) | ||
handlers.set(name, newHandler) | ||
this.addEventListener(name, newHandler) | ||
} else { | ||
handlers.set(name, null) | ||
} | ||
}, | ||
configurable: true, | ||
enumerable: true, | ||
}) | ||
} | ||
defineEventHandler(AbortSignal.prototype, 'abort') | ||
function createAbortSignal() { | ||
let signal | ||
try { | ||
// https://caniuse.com/mdn-api_eventtarget_eventtarget | ||
signal = new EventTarget() | ||
} catch (e) { | ||
signal = new MessageChannel().port1 | ||
} | ||
Object.setPrototypeOf(signal, AbortSignal.prototype) | ||
signal[kAborted] = false | ||
return signal | ||
} | ||
function abortSignal(signal) { | ||
if (signal[kAborted]) return | ||
signal[kAborted] = true | ||
// https://caniuse.com/mdn-api_event_event | ||
const event = new Event('abort') | ||
signal.dispatchEvent(event) | ||
} | ||
function AbortController() { | ||
this[kSignal] = createAbortSignal() | ||
} | ||
Object.defineProperties(AbortController.prototype, { | ||
signal: { | ||
enumerable: true, | ||
get() { | ||
return this[kSignal] | ||
}, | ||
}, | ||
abort: { | ||
enumerable: true, | ||
value() { | ||
abortSignal(this[kSignal]) | ||
}, | ||
}, | ||
}) | ||
function defineStringTag(ctor, tag) { | ||
if (hasSymbol && Symbol.toStringTag) { | ||
Object.defineProperty(ctor.prototype, Symbol.toStringTag, {value: tag}) | ||
} else { | ||
Object.defineProperty(ctor.prototype, 'toString', { | ||
value() { | ||
return '[object ' + tag + ']' | ||
}, | ||
}) | ||
} | ||
} | ||
defineStringTag(AbortSignal, 'AbortSignal') | ||
defineStringTag(AbortController, 'AbortController') | ||
return {AbortController, AbortSignal} | ||
} | ||
const root = | ||
@@ -126,3 +10,7 @@ (typeof globalThis !== 'undefined' && globalThis) || | ||
if (!root.AbortController) { | ||
Object.assign(root, impl()) | ||
Object.assign(root, {AbortSignal, AbortController}) | ||
} | ||
if (root.fetch) { | ||
root.fetch = abortable(root.fetch) | ||
} |
{ | ||
"name": "abort-polyfill", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "Polyfill AbortController and AbortSignal", | ||
"license": "MIT", | ||
"main": "index.js", | ||
"main": "dist/index.js", | ||
"scripts": { | ||
"prepare": "npm test", | ||
"build": "esbuild index.js --bundle --outfile=index.cjs.js --minify-syntax", | ||
"prepare": "npm test && npm run build", | ||
"prebuild": "rm -rf dist", | ||
"build": "esbuild index.js --bundle --outfile=dist/index.js --minify-syntax", | ||
"test": "jest", | ||
@@ -15,3 +16,3 @@ "test:coverage": "jest --coverage", | ||
"files": [ | ||
"index.cjs.js", | ||
"dist", | ||
"index.js" | ||
@@ -18,0 +19,0 @@ ], |
# abort-polyfill | ||
- Polyfill AbortController and AbortSignal | ||
- Polyfill signal options to legacy fetch | ||
[![abort-polyfill](https://badgen.net/bundlephobia/minzip/abort-polyfill)](https://bundlephobia.com/result?p=abort-polyfill) | ||
## Install | ||
@@ -11,4 +16,22 @@ | ||
Any browsers (IE 10+): | ||
```js | ||
import 'cross-fetch/polyfill' | ||
import 'abort-polyfill' | ||
``` | ||
Node < 15: | ||
```js | ||
import 'cross-fetch/polyfill' | ||
import 'event-target-polyfill' | ||
import 'abort-polyfill' | ||
``` | ||
Node >= 15: | ||
```js | ||
import 'cross-fetch/polyfill' | ||
// no need to import polyfill | ||
``` |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
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
6477
5
125
37
4