@shgysk8zer0/polyfills 0.3.14 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -6,6 +6,24 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [v0.4.1] - 2024-08-24
10
+
11
+ ### Added
12
+ - Add [`URLPattern`](https://developer.mozilla.org/en-US/docs/Web/API/URLPattern) via [`urlpattern-polyfill`](https://github.com/kenchris/urlpattern-polyfill)
13
+
14
+ ### Fixed
15
+ - Fix error in `Uint8Array.prototype.toHex()`
16
+
17
+ ## [v0.4.0] - 2024-08-19
18
+
19
+ ### Added
20
+ - Add node version of polyfills (`node.js` and `node.min.js`)
21
+ - Add some automated tests (`test/node.js`)
22
+
23
+ ### Changed
24
+ - Create local module from `string-dedent` instead of having as a dependency
25
+ - Update `rollup.config.js` to create all needed bundles
26
+
9
27
  ## [v0.3.14] - 2024-07-16
10
28
 
11
29
  ### Fixed
package/README.md CHANGED
@@ -40,7 +40,7 @@ You can use a CDN to access the library. Add the following script tag to your
40
40
  HTML file to load the latest version:
41
41
 
42
42
  ```html
43
- <script src="https://unpkg.com/@shgysk8zer0/polyfills/all.min.js"></script>
43
+ <script src="https://unpkg.com/@shgysk8zer0/polyfills[@version]/all.min.js"></script>
44
44
  ```
45
45
 
46
46
  #### With version and SRI
@@ -57,6 +57,10 @@ the following command to install the library:
57
57
  ```bash
58
58
  npm install @shgysk8zer0/polyfills
59
59
  ```
60
+ > [!Note]
61
+ > If using this polyfills package in a node environment, you want to use `node.js` instead
62
+ > of `all.js`. The node version omits polyfills that do not make sense in NodeJS, such as DOM.
63
+ > Simply using `import '@shgysk8zer0/polyfills'` or `require('@shgysk8zer0/polyfills')` should work.
60
64
 
61
65
  ### Git Submodule
62
66
 
package/abort.js CHANGED
@@ -1,194 +1,192 @@
1
- (function() {
2
- 'use strict';
3
-
4
- if (! ('AbortSignal' in globalThis)) {
5
- const symbols = {
6
- signal: Symbol('signal'),
7
- aborted: Symbol('aborted'),
8
- reason: Symbol('reason'),
9
- onabort: Symbol('onabort'),
10
- };
11
-
12
- globalThis.AbortError = class AbortError extends Error {};
13
-
14
- globalThis.AbortSignal = class AbortSignal extends EventTarget {
15
- constructor() {
16
- super();
17
-
18
- Object.defineProperties(this,{
19
- [symbols.aborted]: {
20
- enumerable: false,
21
- writable: true,
22
- configurable: false,
23
- value: false,
24
- },
25
- [symbols.reason]: {
26
- enumerable: false,
27
- writable: true,
28
- configurable: false,
29
- value: undefined,
30
- },
31
- [symbols.onabort]: {
32
- enumerable: false,
33
- writable: true,
34
- configurable: false,
35
- value: null,
36
- },
37
- });
38
-
39
- this.addEventListener('abort', event => {
40
- if (this.onabort instanceof Function) {
41
- this.onabort.call(this, event);
42
- }
43
- });
44
- }
45
-
46
- get aborted() {
47
- return this[symbols.aborted];
48
- }
49
-
50
- get onabort() {
51
- return this[symbols.onabort];
52
- }
53
-
54
- set onabort(value) {
55
- if (value instanceof Function) {
56
- this[symbols.onabort] = value;
57
- } else {
58
- this[symbols.onabort] = null;
1
+ 'use strict';
2
+
3
+ if (! ('AbortSignal' in globalThis)) {
4
+ const symbols = {
5
+ signal: Symbol('signal'),
6
+ aborted: Symbol('aborted'),
7
+ reason: Symbol('reason'),
8
+ onabort: Symbol('onabort'),
9
+ };
10
+
11
+ globalThis.AbortError = class AbortError extends Error {};
12
+
13
+ globalThis.AbortSignal = class AbortSignal extends EventTarget {
14
+ constructor() {
15
+ super();
16
+
17
+ Object.defineProperties(this,{
18
+ [symbols.aborted]: {
19
+ enumerable: false,
20
+ writable: true,
21
+ configurable: false,
22
+ value: false,
23
+ },
24
+ [symbols.reason]: {
25
+ enumerable: false,
26
+ writable: true,
27
+ configurable: false,
28
+ value: undefined,
29
+ },
30
+ [symbols.onabort]: {
31
+ enumerable: false,
32
+ writable: true,
33
+ configurable: false,
34
+ value: null,
35
+ },
36
+ });
37
+
38
+ this.addEventListener('abort', event => {
39
+ if (this.onabort instanceof Function) {
40
+ this.onabort.call(this, event);
59
41
  }
60
- }
42
+ });
43
+ }
61
44
 
62
- get reason() {
63
- return this[symbols.reason];
64
- }
45
+ get aborted() {
46
+ return this[symbols.aborted];
47
+ }
65
48
 
66
- throwIfAborted() {
67
- if (this.aborted) {
68
- throw this.reason;
69
- }
70
- }
49
+ get onabort() {
50
+ return this[symbols.onabort];
51
+ }
71
52
 
72
- static abort(reason = new DOMException('Operation aborted')) {
73
- const signal = new AbortSignal();
74
- signal[symbols.aborted] = true;
75
- signal[symbols.reason] = reason;
76
- return signal;
53
+ set onabort(value) {
54
+ if (value instanceof Function) {
55
+ this[symbols.onabort] = value;
56
+ } else {
57
+ this[symbols.onabort] = null;
77
58
  }
78
- };
59
+ }
79
60
 
80
- globalThis.AbortController = class AbortController {
81
- constructor() {
82
- this[symbols.signal] = new AbortSignal();
83
- }
61
+ get reason() {
62
+ return this[symbols.reason];
63
+ }
84
64
 
85
- get signal() {
86
- return this[symbols.signal];
65
+ throwIfAborted() {
66
+ if (this.aborted) {
67
+ throw this.reason;
87
68
  }
69
+ }
88
70
 
89
- abort(reason = new DOMException('Operation aborted')) {
90
- const signal = this.signal;
71
+ static abort(reason = new DOMException('Operation aborted')) {
72
+ const signal = new AbortSignal();
73
+ signal[symbols.aborted] = true;
74
+ signal[symbols.reason] = reason;
75
+ return signal;
76
+ }
77
+ };
91
78
 
92
- if (! signal.aborted) {
93
- signal[symbols.aborted] = true;
94
- signal[symbols.reason] = reason;
95
- signal.dispatchEvent(new Event('abort'));
96
- }
97
- }
98
- };
99
- }
79
+ globalThis.AbortController = class AbortController {
80
+ constructor() {
81
+ this[symbols.signal] = new AbortSignal();
82
+ }
100
83
 
101
- if (! ('reason' in AbortSignal.prototype)) {
102
- const reasons = new WeakMap();
103
- const abort = AbortController.prototype.abort;
84
+ get signal() {
85
+ return this[symbols.signal];
86
+ }
104
87
 
105
- if (AbortSignal.abort instanceof Function) {
106
- const staticAbort = AbortSignal.abort;
88
+ abort(reason = new DOMException('Operation aborted')) {
89
+ const signal = this.signal;
107
90
 
108
- AbortSignal.abort = function(reason = new DOMException('Operation aborted')) {
109
- const signal = staticAbort();
110
- reasons.set(signal, reason);
111
- return signal;
112
- };
113
- } else {
114
- AbortSignal.abort = function abort(reason = new DOMException('Operation aborted')) {
115
- const controller = new AbortController();
116
- controller.abort(reason);
117
- return controller.reason;
118
- };
91
+ if (! signal.aborted) {
92
+ signal[symbols.aborted] = true;
93
+ signal[symbols.reason] = reason;
94
+ signal.dispatchEvent(new Event('abort'));
95
+ }
119
96
  }
97
+ };
98
+ }
120
99
 
121
- Object.defineProperty(AbortSignal.prototype, 'reason', {
122
- enumerable: true,
123
- configurable: true,
124
- get: function() {
125
- if (reasons.has(this)) {
126
- return reasons.get(this);
127
- } else {
128
- return undefined;
129
- }
130
- }
131
- });
100
+ if (! ('reason' in AbortSignal.prototype)) {
101
+ const reasons = new WeakMap();
102
+ const abort = AbortController.prototype.abort;
132
103
 
133
- AbortController.prototype.abort = function(reason = new DOMException('Operation aborted')) {
134
- reasons.set(this.signal, reason);
135
- abort.call(this);
136
- };
137
- }
104
+ if (AbortSignal.abort instanceof Function) {
105
+ const staticAbort = AbortSignal.abort;
138
106
 
139
- if (! (AbortSignal.prototype.throwIfAborted instanceof Function)) {
140
- AbortSignal.prototype.throwIfAborted = function() {
141
- if (this.aborted) {
142
- throw this.reason;
143
- }
107
+ AbortSignal.abort = function(reason = new DOMException('Operation aborted')) {
108
+ const signal = staticAbort();
109
+ reasons.set(signal, reason);
110
+ return signal;
144
111
  };
145
- }
146
-
147
- if (! (AbortSignal.timeout instanceof Function)) {
148
- AbortSignal.timeout = function(ms) {
149
- if (typeof ms === 'undefined') {
150
- throw new TypeError('At least one 1 argument required but only 0 passed');
151
- } else if (! Number.isFinite(ms)) {
152
- throw new TypeError('Argument 1 is not a finite value, so it is out of range for unsigned long long.');
153
- } else if (ms < 0) {
154
- throw new TypeError('Argument 1 is out of range for unsigned long long.');
155
- } else {
156
- const controller = new AbortController();
157
- setTimeout(() => controller.abort(new DOMException('The operation timed out.')), ms);
158
- return controller.signal;
159
- }
112
+ } else {
113
+ AbortSignal.abort = function abort(reason = new DOMException('Operation aborted')) {
114
+ const controller = new AbortController();
115
+ controller.abort(reason);
116
+ return controller.reason;
160
117
  };
161
118
  }
162
119
 
163
- /**
164
- * @see https://chromestatus.com/feature/5202879349522432
165
- * @TODO How do I handle if a signal is already aborted
166
- * @TODO Should controller abort on a TypeError
167
- */
168
- if (! (AbortSignal.any instanceof Function)) {
169
- AbortSignal.any = function(signals) {
170
- if (! Array.isArray(signals)) {
171
- throw new TypeError('Expected an array of signals');
120
+ Object.defineProperty(AbortSignal.prototype, 'reason', {
121
+ enumerable: true,
122
+ configurable: true,
123
+ get: function() {
124
+ if (reasons.has(this)) {
125
+ return reasons.get(this);
126
+ } else {
127
+ return undefined;
172
128
  }
173
-
129
+ }
130
+ });
131
+
132
+ AbortController.prototype.abort = function(reason = new DOMException('Operation aborted')) {
133
+ reasons.set(this.signal, reason);
134
+ abort.call(this);
135
+ };
136
+ }
137
+
138
+ if (! (AbortSignal.prototype.throwIfAborted instanceof Function)) {
139
+ AbortSignal.prototype.throwIfAborted = function() {
140
+ if (this.aborted) {
141
+ throw this.reason;
142
+ }
143
+ };
144
+ }
145
+
146
+ if (! (AbortSignal.timeout instanceof Function)) {
147
+ AbortSignal.timeout = function(ms) {
148
+ if (typeof ms === 'undefined') {
149
+ throw new TypeError('At least one 1 argument required but only 0 passed');
150
+ } else if (! Number.isFinite(ms)) {
151
+ throw new TypeError('Argument 1 is not a finite value, so it is out of range for unsigned long long.');
152
+ } else if (ms < 0) {
153
+ throw new TypeError('Argument 1 is out of range for unsigned long long.');
154
+ } else {
174
155
  const controller = new AbortController();
156
+ setTimeout(() => controller.abort(new DOMException('The operation timed out.')), ms);
157
+ return controller.signal;
158
+ }
159
+ };
160
+ }
161
+
162
+ /**
163
+ * @see https://chromestatus.com/feature/5202879349522432
164
+ * @TODO How do I handle if a signal is already aborted
165
+ * @TODO Should controller abort on a TypeError
166
+ */
167
+ if (! (AbortSignal.any instanceof Function)) {
168
+ AbortSignal.any = function(signals) {
169
+ if (! Array.isArray(signals)) {
170
+ throw new TypeError('Expected an array of signals');
171
+ }
175
172
 
176
- for (const signal of signals) {
177
- if (! (signal instanceof AbortSignal)) {
178
- const err = new TypeError('`signal` is not an `AbortSignal`');
179
- controller.abort(err);
180
- throw err;
181
- } else if (signal.aborted) {
182
- controller.abort(signal.reason || new DOMException('Operation aborted.'));
183
- break;
184
- } else {
185
- signal.addEventListener('abort', ({ target }) => {
186
- controller.abort(target.reason || new DOMException('Operation aborted.'));
187
- }, { signal: controller.signal });
188
- }
173
+ const controller = new AbortController();
174
+
175
+ for (const signal of signals) {
176
+ if (! (signal instanceof AbortSignal)) {
177
+ const err = new TypeError('`signal` is not an `AbortSignal`');
178
+ controller.abort(err);
179
+ throw err;
180
+ } else if (signal.aborted) {
181
+ controller.abort(signal.reason || new DOMException('Operation aborted.'));
182
+ break;
183
+ } else {
184
+ signal.addEventListener('abort', ({ target }) => {
185
+ controller.abort(target.reason || new DOMException('Operation aborted.'));
186
+ }, { signal: controller.signal });
189
187
  }
188
+ }
190
189
 
191
- return controller.signal;
192
- };
193
- }
194
- })();
190
+ return controller.signal;
191
+ };
192
+ }