Skip to content

Commit 0f990f1

Browse files
committed
Merge branch 'Xmader/feat/atob-btoa' into Xmader/feat/better-setTimeout
2 parents 29b8a7c + fa60ab8 commit 0f990f1

File tree

3 files changed

+43
-93
lines changed

3 files changed

+43
-93
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @file base64.d.ts
3+
* @brief TypeScript type declarations for base64.py
4+
* @author Tom Tang <[email protected]>
5+
* @date July 2023
6+
*/
7+
8+
/**
9+
* Decode base64 string
10+
* @param b64 A string containing base64-encoded data.
11+
* @see https://html.spec.whatwg.org/multipage/webappapis.html#dom-atob-dev
12+
*/
13+
export declare function atob(b64: string): string;
14+
15+
/**
16+
* Create a base64-encoded ASCII string from a binary string
17+
* @param data The binary string to encode.
18+
* @see https://html.spec.whatwg.org/multipage/webappapis.html#dom-btoa-dev
19+
*/
20+
export declare function btoa(data: string): string;

python/pythonmonkey/builtin_modules/base64.js

Lines changed: 0 additions & 93 deletions
This file was deleted.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# @file base64.py
2+
# @author Tom Tang <[email protected]>, Hamada Gasmallah <[email protected]>
3+
# @date July 2023
4+
5+
import base64
6+
7+
atob = lambda b64: str(base64.standard_b64decode(b64), 'latin1')
8+
btoa = lambda data: str(base64.standard_b64encode(bytes(data, 'latin1')), 'latin1')
9+
10+
# Make `atob`/`btoa` globally available
11+
import pythonmonkey as pm
12+
pm.eval(r"""(atob, btoa) => {
13+
if (!globalThis.atob) {
14+
globalThis.atob = atob;
15+
}
16+
if (!globalThis.btoa) {
17+
globalThis.btoa = btoa;
18+
}
19+
}""")(atob, btoa)
20+
21+
# Module exports
22+
exports['atob'] = atob # type: ignore
23+
exports['btoa'] = btoa # type: ignore

0 commit comments

Comments
 (0)