@depup/whatwg-url 16.0.1-depup.0

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/lib/URL.js ADDED
@@ -0,0 +1,499 @@
1
+ "use strict";
2
+
3
+ const conversions = require("webidl-conversions");
4
+ const utils = require("./utils.js");
5
+
6
+ const implSymbol = utils.implSymbol;
7
+ const ctorRegistrySymbol = utils.ctorRegistrySymbol;
8
+
9
+ const interfaceName = "URL";
10
+
11
+ exports.is = value => {
12
+ return utils.isObject(value) && Object.hasOwn(value, implSymbol) && value[implSymbol] instanceof Impl.implementation;
13
+ };
14
+ exports.isImpl = value => {
15
+ return utils.isObject(value) && value instanceof Impl.implementation;
16
+ };
17
+ exports.convert = (globalObject, value, { context = "The provided value" } = {}) => {
18
+ if (exports.is(value)) {
19
+ return utils.implForWrapper(value);
20
+ }
21
+ throw new globalObject.TypeError(`${context} is not of type 'URL'.`);
22
+ };
23
+
24
+ function makeWrapper(globalObject, newTarget) {
25
+ let proto;
26
+ if (newTarget !== undefined) {
27
+ proto = newTarget.prototype;
28
+ }
29
+
30
+ if (!utils.isObject(proto)) {
31
+ proto = globalObject[ctorRegistrySymbol]["URL"].prototype;
32
+ }
33
+
34
+ return Object.create(proto);
35
+ }
36
+
37
+ exports.create = (globalObject, constructorArgs, privateData) => {
38
+ const wrapper = makeWrapper(globalObject);
39
+ return exports.setup(wrapper, globalObject, constructorArgs, privateData);
40
+ };
41
+
42
+ exports.createImpl = (globalObject, constructorArgs, privateData) => {
43
+ const wrapper = exports.create(globalObject, constructorArgs, privateData);
44
+ return utils.implForWrapper(wrapper);
45
+ };
46
+
47
+ exports._internalSetup = (wrapper, globalObject) => {};
48
+
49
+ exports.setup = (wrapper, globalObject, constructorArgs = [], privateData = {}) => {
50
+ privateData.wrapper = wrapper;
51
+
52
+ exports._internalSetup(wrapper, globalObject);
53
+ Object.defineProperty(wrapper, implSymbol, {
54
+ value: new Impl.implementation(globalObject, constructorArgs, privateData),
55
+ configurable: true
56
+ });
57
+
58
+ wrapper[implSymbol][utils.wrapperSymbol] = wrapper;
59
+ if (Impl.init) {
60
+ Impl.init(wrapper[implSymbol]);
61
+ }
62
+ return wrapper;
63
+ };
64
+
65
+ exports.new = (globalObject, newTarget) => {
66
+ const wrapper = makeWrapper(globalObject, newTarget);
67
+
68
+ exports._internalSetup(wrapper, globalObject);
69
+ Object.defineProperty(wrapper, implSymbol, {
70
+ value: Object.create(Impl.implementation.prototype),
71
+ configurable: true
72
+ });
73
+
74
+ wrapper[implSymbol][utils.wrapperSymbol] = wrapper;
75
+ if (Impl.init) {
76
+ Impl.init(wrapper[implSymbol]);
77
+ }
78
+ return wrapper[implSymbol];
79
+ };
80
+
81
+ const exposed = new Set(["Window", "Worker"]);
82
+
83
+ exports.install = (globalObject, globalNames) => {
84
+ if (!globalNames.some(globalName => exposed.has(globalName))) {
85
+ return;
86
+ }
87
+
88
+ const ctorRegistry = utils.initCtorRegistry(globalObject);
89
+ class URL {
90
+ constructor(url) {
91
+ if (arguments.length < 1) {
92
+ throw new globalObject.TypeError(
93
+ `Failed to construct 'URL': 1 argument required, but only ${arguments.length} present.`
94
+ );
95
+ }
96
+ const args = [];
97
+ {
98
+ let curArg = arguments[0];
99
+ curArg = conversions["USVString"](curArg, {
100
+ context: "Failed to construct 'URL': parameter 1",
101
+ globals: globalObject
102
+ });
103
+ args.push(curArg);
104
+ }
105
+ {
106
+ let curArg = arguments[1];
107
+ if (curArg !== undefined) {
108
+ curArg = conversions["USVString"](curArg, {
109
+ context: "Failed to construct 'URL': parameter 2",
110
+ globals: globalObject
111
+ });
112
+ }
113
+ args.push(curArg);
114
+ }
115
+ return exports.setup(Object.create(new.target.prototype), globalObject, args);
116
+ }
117
+
118
+ toJSON() {
119
+ const esValue = this !== null && this !== undefined ? this : globalObject;
120
+ if (!exports.is(esValue)) {
121
+ throw new globalObject.TypeError("'toJSON' called on an object that is not a valid instance of URL.");
122
+ }
123
+
124
+ return esValue[implSymbol].toJSON();
125
+ }
126
+
127
+ get href() {
128
+ const esValue = this !== null && this !== undefined ? this : globalObject;
129
+
130
+ if (!exports.is(esValue)) {
131
+ throw new globalObject.TypeError("'get href' called on an object that is not a valid instance of URL.");
132
+ }
133
+
134
+ return esValue[implSymbol]["href"];
135
+ }
136
+
137
+ set href(V) {
138
+ const esValue = this !== null && this !== undefined ? this : globalObject;
139
+
140
+ if (!exports.is(esValue)) {
141
+ throw new globalObject.TypeError("'set href' called on an object that is not a valid instance of URL.");
142
+ }
143
+
144
+ V = conversions["USVString"](V, {
145
+ context: "Failed to set the 'href' property on 'URL': The provided value",
146
+ globals: globalObject
147
+ });
148
+
149
+ esValue[implSymbol]["href"] = V;
150
+ }
151
+
152
+ toString() {
153
+ const esValue = this;
154
+ if (!exports.is(esValue)) {
155
+ throw new globalObject.TypeError("'toString' called on an object that is not a valid instance of URL.");
156
+ }
157
+
158
+ return esValue[implSymbol]["href"];
159
+ }
160
+
161
+ get origin() {
162
+ const esValue = this !== null && this !== undefined ? this : globalObject;
163
+
164
+ if (!exports.is(esValue)) {
165
+ throw new globalObject.TypeError("'get origin' called on an object that is not a valid instance of URL.");
166
+ }
167
+
168
+ return esValue[implSymbol]["origin"];
169
+ }
170
+
171
+ get protocol() {
172
+ const esValue = this !== null && this !== undefined ? this : globalObject;
173
+
174
+ if (!exports.is(esValue)) {
175
+ throw new globalObject.TypeError("'get protocol' called on an object that is not a valid instance of URL.");
176
+ }
177
+
178
+ return esValue[implSymbol]["protocol"];
179
+ }
180
+
181
+ set protocol(V) {
182
+ const esValue = this !== null && this !== undefined ? this : globalObject;
183
+
184
+ if (!exports.is(esValue)) {
185
+ throw new globalObject.TypeError("'set protocol' called on an object that is not a valid instance of URL.");
186
+ }
187
+
188
+ V = conversions["USVString"](V, {
189
+ context: "Failed to set the 'protocol' property on 'URL': The provided value",
190
+ globals: globalObject
191
+ });
192
+
193
+ esValue[implSymbol]["protocol"] = V;
194
+ }
195
+
196
+ get username() {
197
+ const esValue = this !== null && this !== undefined ? this : globalObject;
198
+
199
+ if (!exports.is(esValue)) {
200
+ throw new globalObject.TypeError("'get username' called on an object that is not a valid instance of URL.");
201
+ }
202
+
203
+ return esValue[implSymbol]["username"];
204
+ }
205
+
206
+ set username(V) {
207
+ const esValue = this !== null && this !== undefined ? this : globalObject;
208
+
209
+ if (!exports.is(esValue)) {
210
+ throw new globalObject.TypeError("'set username' called on an object that is not a valid instance of URL.");
211
+ }
212
+
213
+ V = conversions["USVString"](V, {
214
+ context: "Failed to set the 'username' property on 'URL': The provided value",
215
+ globals: globalObject
216
+ });
217
+
218
+ esValue[implSymbol]["username"] = V;
219
+ }
220
+
221
+ get password() {
222
+ const esValue = this !== null && this !== undefined ? this : globalObject;
223
+
224
+ if (!exports.is(esValue)) {
225
+ throw new globalObject.TypeError("'get password' called on an object that is not a valid instance of URL.");
226
+ }
227
+
228
+ return esValue[implSymbol]["password"];
229
+ }
230
+
231
+ set password(V) {
232
+ const esValue = this !== null && this !== undefined ? this : globalObject;
233
+
234
+ if (!exports.is(esValue)) {
235
+ throw new globalObject.TypeError("'set password' called on an object that is not a valid instance of URL.");
236
+ }
237
+
238
+ V = conversions["USVString"](V, {
239
+ context: "Failed to set the 'password' property on 'URL': The provided value",
240
+ globals: globalObject
241
+ });
242
+
243
+ esValue[implSymbol]["password"] = V;
244
+ }
245
+
246
+ get host() {
247
+ const esValue = this !== null && this !== undefined ? this : globalObject;
248
+
249
+ if (!exports.is(esValue)) {
250
+ throw new globalObject.TypeError("'get host' called on an object that is not a valid instance of URL.");
251
+ }
252
+
253
+ return esValue[implSymbol]["host"];
254
+ }
255
+
256
+ set host(V) {
257
+ const esValue = this !== null && this !== undefined ? this : globalObject;
258
+
259
+ if (!exports.is(esValue)) {
260
+ throw new globalObject.TypeError("'set host' called on an object that is not a valid instance of URL.");
261
+ }
262
+
263
+ V = conversions["USVString"](V, {
264
+ context: "Failed to set the 'host' property on 'URL': The provided value",
265
+ globals: globalObject
266
+ });
267
+
268
+ esValue[implSymbol]["host"] = V;
269
+ }
270
+
271
+ get hostname() {
272
+ const esValue = this !== null && this !== undefined ? this : globalObject;
273
+
274
+ if (!exports.is(esValue)) {
275
+ throw new globalObject.TypeError("'get hostname' called on an object that is not a valid instance of URL.");
276
+ }
277
+
278
+ return esValue[implSymbol]["hostname"];
279
+ }
280
+
281
+ set hostname(V) {
282
+ const esValue = this !== null && this !== undefined ? this : globalObject;
283
+
284
+ if (!exports.is(esValue)) {
285
+ throw new globalObject.TypeError("'set hostname' called on an object that is not a valid instance of URL.");
286
+ }
287
+
288
+ V = conversions["USVString"](V, {
289
+ context: "Failed to set the 'hostname' property on 'URL': The provided value",
290
+ globals: globalObject
291
+ });
292
+
293
+ esValue[implSymbol]["hostname"] = V;
294
+ }
295
+
296
+ get port() {
297
+ const esValue = this !== null && this !== undefined ? this : globalObject;
298
+
299
+ if (!exports.is(esValue)) {
300
+ throw new globalObject.TypeError("'get port' called on an object that is not a valid instance of URL.");
301
+ }
302
+
303
+ return esValue[implSymbol]["port"];
304
+ }
305
+
306
+ set port(V) {
307
+ const esValue = this !== null && this !== undefined ? this : globalObject;
308
+
309
+ if (!exports.is(esValue)) {
310
+ throw new globalObject.TypeError("'set port' called on an object that is not a valid instance of URL.");
311
+ }
312
+
313
+ V = conversions["USVString"](V, {
314
+ context: "Failed to set the 'port' property on 'URL': The provided value",
315
+ globals: globalObject
316
+ });
317
+
318
+ esValue[implSymbol]["port"] = V;
319
+ }
320
+
321
+ get pathname() {
322
+ const esValue = this !== null && this !== undefined ? this : globalObject;
323
+
324
+ if (!exports.is(esValue)) {
325
+ throw new globalObject.TypeError("'get pathname' called on an object that is not a valid instance of URL.");
326
+ }
327
+
328
+ return esValue[implSymbol]["pathname"];
329
+ }
330
+
331
+ set pathname(V) {
332
+ const esValue = this !== null && this !== undefined ? this : globalObject;
333
+
334
+ if (!exports.is(esValue)) {
335
+ throw new globalObject.TypeError("'set pathname' called on an object that is not a valid instance of URL.");
336
+ }
337
+
338
+ V = conversions["USVString"](V, {
339
+ context: "Failed to set the 'pathname' property on 'URL': The provided value",
340
+ globals: globalObject
341
+ });
342
+
343
+ esValue[implSymbol]["pathname"] = V;
344
+ }
345
+
346
+ get search() {
347
+ const esValue = this !== null && this !== undefined ? this : globalObject;
348
+
349
+ if (!exports.is(esValue)) {
350
+ throw new globalObject.TypeError("'get search' called on an object that is not a valid instance of URL.");
351
+ }
352
+
353
+ return esValue[implSymbol]["search"];
354
+ }
355
+
356
+ set search(V) {
357
+ const esValue = this !== null && this !== undefined ? this : globalObject;
358
+
359
+ if (!exports.is(esValue)) {
360
+ throw new globalObject.TypeError("'set search' called on an object that is not a valid instance of URL.");
361
+ }
362
+
363
+ V = conversions["USVString"](V, {
364
+ context: "Failed to set the 'search' property on 'URL': The provided value",
365
+ globals: globalObject
366
+ });
367
+
368
+ esValue[implSymbol]["search"] = V;
369
+ }
370
+
371
+ get searchParams() {
372
+ const esValue = this !== null && this !== undefined ? this : globalObject;
373
+
374
+ if (!exports.is(esValue)) {
375
+ throw new globalObject.TypeError("'get searchParams' called on an object that is not a valid instance of URL.");
376
+ }
377
+
378
+ return utils.getSameObject(this, "searchParams", () => {
379
+ return utils.tryWrapperForImpl(esValue[implSymbol]["searchParams"]);
380
+ });
381
+ }
382
+
383
+ get hash() {
384
+ const esValue = this !== null && this !== undefined ? this : globalObject;
385
+
386
+ if (!exports.is(esValue)) {
387
+ throw new globalObject.TypeError("'get hash' called on an object that is not a valid instance of URL.");
388
+ }
389
+
390
+ return esValue[implSymbol]["hash"];
391
+ }
392
+
393
+ set hash(V) {
394
+ const esValue = this !== null && this !== undefined ? this : globalObject;
395
+
396
+ if (!exports.is(esValue)) {
397
+ throw new globalObject.TypeError("'set hash' called on an object that is not a valid instance of URL.");
398
+ }
399
+
400
+ V = conversions["USVString"](V, {
401
+ context: "Failed to set the 'hash' property on 'URL': The provided value",
402
+ globals: globalObject
403
+ });
404
+
405
+ esValue[implSymbol]["hash"] = V;
406
+ }
407
+
408
+ static parse(url) {
409
+ if (arguments.length < 1) {
410
+ throw new globalObject.TypeError(
411
+ `Failed to execute 'parse' on 'URL': 1 argument required, but only ${arguments.length} present.`
412
+ );
413
+ }
414
+ const args = [];
415
+ {
416
+ let curArg = arguments[0];
417
+ curArg = conversions["USVString"](curArg, {
418
+ context: "Failed to execute 'parse' on 'URL': parameter 1",
419
+ globals: globalObject
420
+ });
421
+ args.push(curArg);
422
+ }
423
+ {
424
+ let curArg = arguments[1];
425
+ if (curArg !== undefined) {
426
+ curArg = conversions["USVString"](curArg, {
427
+ context: "Failed to execute 'parse' on 'URL': parameter 2",
428
+ globals: globalObject
429
+ });
430
+ }
431
+ args.push(curArg);
432
+ }
433
+ return utils.tryWrapperForImpl(Impl.implementation.parse(globalObject, ...args));
434
+ }
435
+
436
+ static canParse(url) {
437
+ if (arguments.length < 1) {
438
+ throw new globalObject.TypeError(
439
+ `Failed to execute 'canParse' on 'URL': 1 argument required, but only ${arguments.length} present.`
440
+ );
441
+ }
442
+ const args = [];
443
+ {
444
+ let curArg = arguments[0];
445
+ curArg = conversions["USVString"](curArg, {
446
+ context: "Failed to execute 'canParse' on 'URL': parameter 1",
447
+ globals: globalObject
448
+ });
449
+ args.push(curArg);
450
+ }
451
+ {
452
+ let curArg = arguments[1];
453
+ if (curArg !== undefined) {
454
+ curArg = conversions["USVString"](curArg, {
455
+ context: "Failed to execute 'canParse' on 'URL': parameter 2",
456
+ globals: globalObject
457
+ });
458
+ }
459
+ args.push(curArg);
460
+ }
461
+ return Impl.implementation.canParse(...args);
462
+ }
463
+ }
464
+ Object.defineProperties(URL.prototype, {
465
+ toJSON: { enumerable: true },
466
+ href: { enumerable: true },
467
+ toString: { enumerable: true },
468
+ origin: { enumerable: true },
469
+ protocol: { enumerable: true },
470
+ username: { enumerable: true },
471
+ password: { enumerable: true },
472
+ host: { enumerable: true },
473
+ hostname: { enumerable: true },
474
+ port: { enumerable: true },
475
+ pathname: { enumerable: true },
476
+ search: { enumerable: true },
477
+ searchParams: { enumerable: true },
478
+ hash: { enumerable: true },
479
+ [Symbol.toStringTag]: { value: "URL", configurable: true }
480
+ });
481
+ Object.defineProperties(URL, { parse: { enumerable: true }, canParse: { enumerable: true } });
482
+ ctorRegistry[interfaceName] = URL;
483
+
484
+ Object.defineProperty(globalObject, interfaceName, {
485
+ configurable: true,
486
+ writable: true,
487
+ value: URL
488
+ });
489
+
490
+ if (globalNames.includes("Window")) {
491
+ Object.defineProperty(globalObject, "webkitURL", {
492
+ configurable: true,
493
+ writable: true,
494
+ value: URL
495
+ });
496
+ }
497
+ };
498
+
499
+ const Impl = require("./URL-impl.js");
@@ -0,0 +1,135 @@
1
+ "use strict";
2
+ const urlencoded = require("./urlencoded");
3
+
4
+ exports.implementation = class URLSearchParamsImpl {
5
+ constructor(globalObject, constructorArgs, { doNotStripQMark = false }) {
6
+ let init = constructorArgs[0];
7
+ this._list = [];
8
+ this._url = null;
9
+
10
+ if (!doNotStripQMark && typeof init === "string" && init[0] === "?") {
11
+ init = init.slice(1);
12
+ }
13
+
14
+ if (Array.isArray(init)) {
15
+ for (const pair of init) {
16
+ if (pair.length !== 2) {
17
+ throw new TypeError("Failed to construct 'URLSearchParams': parameter 1 sequence's element does not " +
18
+ "contain exactly two elements.");
19
+ }
20
+ this._list.push([pair[0], pair[1]]);
21
+ }
22
+ } else if (typeof init === "object" && Object.getPrototypeOf(init) === null) {
23
+ for (const name of Object.keys(init)) {
24
+ const value = init[name];
25
+ this._list.push([name, value]);
26
+ }
27
+ } else {
28
+ this._list = urlencoded.parseUrlencodedString(init);
29
+ }
30
+ }
31
+
32
+ _updateSteps() {
33
+ if (this._url !== null) {
34
+ let serializedQuery = urlencoded.serializeUrlencoded(this._list);
35
+ if (serializedQuery === "") {
36
+ serializedQuery = null;
37
+ }
38
+
39
+ this._url._url.query = serializedQuery;
40
+ }
41
+ }
42
+
43
+ get size() {
44
+ return this._list.length;
45
+ }
46
+
47
+ append(name, value) {
48
+ this._list.push([name, value]);
49
+ this._updateSteps();
50
+ }
51
+
52
+ delete(name, value) {
53
+ let i = 0;
54
+ while (i < this._list.length) {
55
+ if (this._list[i][0] === name && (value === undefined || this._list[i][1] === value)) {
56
+ this._list.splice(i, 1);
57
+ } else {
58
+ i++;
59
+ }
60
+ }
61
+ this._updateSteps();
62
+ }
63
+
64
+ get(name) {
65
+ for (const tuple of this._list) {
66
+ if (tuple[0] === name) {
67
+ return tuple[1];
68
+ }
69
+ }
70
+ return null;
71
+ }
72
+
73
+ getAll(name) {
74
+ const output = [];
75
+ for (const tuple of this._list) {
76
+ if (tuple[0] === name) {
77
+ output.push(tuple[1]);
78
+ }
79
+ }
80
+ return output;
81
+ }
82
+
83
+ has(name, value) {
84
+ for (const tuple of this._list) {
85
+ if (tuple[0] === name && (value === undefined || tuple[1] === value)) {
86
+ return true;
87
+ }
88
+ }
89
+ return false;
90
+ }
91
+
92
+ set(name, value) {
93
+ let found = false;
94
+ let i = 0;
95
+ while (i < this._list.length) {
96
+ if (this._list[i][0] === name) {
97
+ if (found) {
98
+ this._list.splice(i, 1);
99
+ } else {
100
+ found = true;
101
+ this._list[i][1] = value;
102
+ i++;
103
+ }
104
+ } else {
105
+ i++;
106
+ }
107
+ }
108
+ if (!found) {
109
+ this._list.push([name, value]);
110
+ }
111
+ this._updateSteps();
112
+ }
113
+
114
+ sort() {
115
+ this._list.sort((a, b) => {
116
+ if (a[0] < b[0]) {
117
+ return -1;
118
+ }
119
+ if (a[0] > b[0]) {
120
+ return 1;
121
+ }
122
+ return 0;
123
+ });
124
+
125
+ this._updateSteps();
126
+ }
127
+
128
+ [Symbol.iterator]() {
129
+ return this._list[Symbol.iterator]();
130
+ }
131
+
132
+ toString() {
133
+ return urlencoded.serializeUrlencoded(this._list);
134
+ }
135
+ };