Skip to content

Commit 63f3aed

Browse files
committed
refactor: move URL/URLSearchParams APIs into the url builtin_modules
1 parent d2b4725 commit 63f3aed

File tree

4 files changed

+131
-4
lines changed

4 files changed

+131
-4
lines changed

python/pythonmonkey/__init__.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@
1313
require("console")
1414
require("base64")
1515
require("timers")
16-
## npm packages
17-
require("core-js/actual/dom-exception")
18-
require("core-js/actual/url")
19-
require("core-js/actual/url-search-params")
16+
require("url")
2017

2118
# Add the `.keys()` method on `Object.prototype` to get JSObjectProxy dict() conversion working
2219
# Conversion from a dict-subclass to a strict dict by `dict(subclass)` internally calls the .keys() method to read the dictionary keys,
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/**
2+
* @file url.d.ts
3+
* Type definitions for URL/URLSearchParams
4+
*
5+
* @author Tom Tang <[email protected]>
6+
* @date August 2023
7+
*/
8+
9+
/*!
10+
* Modified from https://www.npmjs.com/package/@types/web
11+
* Apache License 2.0
12+
*/
13+
14+
/**
15+
* The URL interface represents an object providing static methods used for creating object URLs.
16+
*
17+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL)
18+
*/
19+
export interface URL {
20+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/hash) */
21+
hash: string;
22+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/host) */
23+
host: string;
24+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/hostname) */
25+
hostname: string;
26+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/href) */
27+
href: string;
28+
toString(): string;
29+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/origin) */
30+
readonly origin: string;
31+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/password) */
32+
password: string;
33+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/pathname) */
34+
pathname: string;
35+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/port) */
36+
port: string;
37+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/protocol) */
38+
protocol: string;
39+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/search) */
40+
search: string;
41+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/searchParams) */
42+
readonly searchParams: URLSearchParams;
43+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/username) */
44+
username: string;
45+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/toJSON) */
46+
toJSON(): string;
47+
}
48+
49+
export declare var URL: {
50+
prototype: URL;
51+
new(url: string | URL, base?: string | URL): URL;
52+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/canParse_static) */
53+
canParse(url: string | URL, base?: string): boolean;
54+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/createObjectURL_static) @deprecated not implemented by polyfill */
55+
// @ts-expect-error types not defined
56+
createObjectURL(obj: Blob | MediaSource): string;
57+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/revokeObjectURL_static) @deprecated not implemented by polyfill */
58+
revokeObjectURL(url: string): void;
59+
};
60+
61+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/URLSearchParams) */
62+
export interface URLSearchParams {
63+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/URLSearchParams/size) */
64+
readonly size: number;
65+
/**
66+
* Appends a specified key/value pair as a new search parameter.
67+
*
68+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/URLSearchParams/append)
69+
*/
70+
append(name: string, value: string): void;
71+
/**
72+
* Deletes the given search parameter, and its associated value, from the list of all search parameters.
73+
*
74+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/URLSearchParams/delete)
75+
*/
76+
delete(name: string, value?: string): void;
77+
/**
78+
* Returns the first value associated to the given search parameter.
79+
*
80+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/URLSearchParams/get)
81+
*/
82+
get(name: string): string | null;
83+
/**
84+
* Returns all the values association with a given search parameter.
85+
*
86+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/URLSearchParams/getAll)
87+
*/
88+
getAll(name: string): string[];
89+
/**
90+
* Returns a Boolean indicating if such a search parameter exists.
91+
*
92+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/URLSearchParams/has)
93+
*/
94+
has(name: string, value?: string): boolean;
95+
/**
96+
* Sets the value associated to a given search parameter to the given value. If there were several values, delete the others.
97+
*
98+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/URLSearchParams/set)
99+
*/
100+
set(name: string, value: string): void;
101+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/URLSearchParams/sort) */
102+
sort(): void;
103+
/** Returns a string containing a query string suitable for use in a URL. Does not include the question mark. */
104+
toString(): string;
105+
forEach(callbackfn: (value: string, key: string, parent: URLSearchParams) => void, thisArg?: any): void;
106+
}
107+
108+
export declare var URLSearchParams: {
109+
prototype: URLSearchParams;
110+
new(init?: string[][] | Record<string, string> | string | URLSearchParams): URLSearchParams;
111+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @file url.js
3+
* Polyfill the URL and URLSearchParams interfaces
4+
*
5+
* @author Tom Tang <[email protected]>
6+
* @date August 2023
7+
*/
8+
9+
// Apply polyfills from core-js
10+
require("core-js/actual/dom-exception")
11+
require("core-js/actual/url")
12+
require("core-js/actual/url-search-params")
13+
14+
exports.URL = globalThis.URL;
15+
exports.URLSearchParams = globalThis.URLSearchParams;

python/pythonmonkey/global.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ declare var btoa: typeof import("base64").btoa;
5555
declare var setTimeout: typeof import("timers").setTimeout;
5656
declare var clearTimeout: typeof import("timers").clearTimeout;
5757

58+
// Expose `URL`/`URLSearchParams` APIs
59+
declare var URL: typeof import("url").URL;
60+
declare var URLSearchParams: typeof import("url").URLSearchParams;
61+
5862
// Keep this in sync with both https://hg.mozilla.org/releases/mozilla-esr102/file/a03fde6/js/public/Promise.h#l331
5963
// and https://github.com/nodejs/node/blob/v20.2.0/deps/v8/include/v8-promise.h#L30
6064
declare enum PromiseState { Pending = 0, Fulfilled = 1, Rejected = 2 }

0 commit comments

Comments
 (0)