Skip to content

Commit ab3eed0

Browse files
base64 decodeString
1 parent f1fd537 commit ab3eed0

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/modules/encoding/mod.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,50 @@ pub(crate) fn create_base64_proxy(_realm: &QuickJsRealmAdapter) -> JsProxy {
117117
)
118118
}
119119
})
120+
.static_method("decodeString", |_runtime, realm, args| {
121+
// todo async
122+
123+
if args.is_empty() || !args[0].is_string() {
124+
Err(JsError::new_str("decodeString expects a single string arg"))
125+
} else {
126+
let s = args[0].to_string()?;
127+
realm.create_resolving_promise(
128+
move || {
129+
let engine = base64::engine::general_purpose::STANDARD;
130+
let decoded = engine
131+
.decode(s)
132+
.map_err(|e| JsError::new_string(format!("{e}")))?;
133+
let s = String::from_utf8_lossy(&decoded);
134+
Ok(s.to_string())
135+
},
136+
|realm, p_res| {
137+
//
138+
realm.create_string(p_res.as_str())
139+
},
140+
)
141+
}
142+
})
143+
.static_method("decodeStringSync", |_runtime, realm, args| {
144+
if args.is_empty() || !args[0].is_string() {
145+
Err(JsError::new_str("decodeString expects a single string arg"))
146+
} else {
147+
let s = args[0].to_string()?;
148+
realm.create_resolving_promise(
149+
move || {
150+
let engine = base64::engine::general_purpose::STANDARD;
151+
let decoded = engine
152+
.decode(s)
153+
.map_err(|e| JsError::new_string(format!("{e}")))?;
154+
let s = String::from_utf8_lossy(&decoded);
155+
Ok(s.to_string())
156+
},
157+
|realm, p_res| {
158+
//
159+
realm.create_string(p_res.as_str())
160+
},
161+
)
162+
}
163+
})
120164
.static_method("decodeSync", |_runtime, realm, args| {
121165
// todo async
122166

0 commit comments

Comments
 (0)