0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 09:31:22 -05:00

feat(extensions/crypto): implement randomUUID (#10848)

This commit is contained in:
Leo K 2021-06-05 14:46:24 +02:00 committed by GitHub
parent e8be116ab6
commit cf351f77c2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 38 additions and 6 deletions

1
Cargo.lock generated
View file

@ -645,6 +645,7 @@ version = "0.21.1"
dependencies = [
"deno_core",
"rand 0.8.3",
"uuid",
]
[[package]]

View file

@ -41,10 +41,16 @@
return arrayBufferView;
}
function randomUUID() {
return core.opSync("op_crypto_random_uuid");
}
window.crypto = {
getRandomValues,
randomUUID,
};
window.__bootstrap.crypto = {
getRandomValues,
randomUUID,
};
})(this);

View file

@ -16,3 +16,4 @@ path = "lib.rs"
[dependencies]
deno_core = { version = "0.88.1", path = "../../core" }
rand = "0.8.3"
uuid = { version = "0.8.2", features = ["v4"] }

View file

@ -23,4 +23,5 @@ declare interface Crypto {
>(
array: T,
): T;
randomUUID(): string;
}

View file

@ -21,10 +21,13 @@ pub fn init(maybe_seed: Option<u64>) -> Extension {
prefix "deno:extensions/crypto",
"01_crypto.js",
))
.ops(vec![(
"op_crypto_get_random_values",
op_sync(op_crypto_get_random_values),
)])
.ops(vec![
(
"op_crypto_get_random_values",
op_sync(op_crypto_get_random_values),
),
("op_crypto_random_uuid", op_sync(op_crypto_random_uuid)),
])
.state(move |state| {
if let Some(seed) = maybe_seed {
state.put(StdRng::seed_from_u64(seed));
@ -51,6 +54,25 @@ pub fn op_crypto_get_random_values(
Ok(())
}
pub fn op_crypto_random_uuid(
state: &mut OpState,
_args: (),
_zero_copy: (),
) -> Result<String, AnyError> {
let maybe_seeded_rng = state.try_borrow_mut::<StdRng>();
let uuid = if let Some(seeded_rng) = maybe_seeded_rng {
let mut bytes = [0u8; 16];
seeded_rng.fill(&mut bytes);
uuid::Builder::from_bytes(bytes)
.set_version(uuid::Version::Random)
.build()
} else {
uuid::Uuid::new_v4()
};
Ok(uuid.to_string())
}
pub fn get_declaration() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("lib.deno_crypto.d.ts")
}

View file

@ -146,7 +146,7 @@
"rsa_importKey.https.any.html": false,
"symmetric_importKey.https.any.html": false
},
"randomUUID.https.any.html": false,
"randomUUID.https.any.html": true,
"sign_verify": {
"ecdsa.https.any.html": false,
"hmac.https.any.html": false,
@ -155,7 +155,8 @@
},
"wrapKey_unwrapKey": {
"wrapKey_unwrapKey.https.any.html": false
}
},
"randomUUID.any.html": true
},
"console": {
"console-is-a-namespace.any.html": true,