mirror of
https://github.com/denoland/deno.git
synced 2024-12-22 23:34:47 -05:00
d47147fb6a
This commit moves "deno_std/node" in "ext/node" crate. The code is transpiled and snapshotted during the build process. During the first pass a minimal amount of work was done to create the snapshot, a lot of code in "ext/node" depends on presence of "Deno" global. This code will be gradually fixed in the follow up PRs to migrate it to import relevant APIs from "internal:" modules. Currently the code from snapshot is not used in any way, and all Node/npm compatibility still uses code from "https://deno.land/std/node" (or from the location specified by "DENO_NODE_COMPAT_URL"). This will also be handled in a follow up PRs. --------- Co-authored-by: crowlkats <crowlkats@toaxl.com> Co-authored-by: Divy Srivastava <dj.srivastava23@gmail.com> Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
49 lines
1.2 KiB
Rust
49 lines
1.2 KiB
Rust
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
|
use deno_core::error::type_error;
|
|
use deno_core::error::AnyError;
|
|
use deno_core::op;
|
|
use deno_core::OpState;
|
|
use deno_core::ResourceId;
|
|
use deno_core::ZeroCopyBuf;
|
|
use std::rc::Rc;
|
|
|
|
mod digest;
|
|
|
|
#[op]
|
|
pub fn op_node_create_hash(
|
|
state: &mut OpState,
|
|
algorithm: String,
|
|
) -> Result<ResourceId, AnyError> {
|
|
Ok(state.resource_table.add(digest::Context::new(&algorithm)?))
|
|
}
|
|
|
|
#[op]
|
|
pub fn op_node_hash_update(
|
|
state: &mut OpState,
|
|
rid: ResourceId,
|
|
data: &[u8],
|
|
) -> Result<(), AnyError> {
|
|
let context = state.resource_table.get::<digest::Context>(rid)?;
|
|
context.update(data);
|
|
Ok(())
|
|
}
|
|
|
|
#[op]
|
|
pub fn op_node_hash_digest(
|
|
state: &mut OpState,
|
|
rid: ResourceId,
|
|
) -> Result<ZeroCopyBuf, AnyError> {
|
|
let context = state.resource_table.take::<digest::Context>(rid)?;
|
|
let context = Rc::try_unwrap(context)
|
|
.map_err(|_| type_error("Hash context is already in use"))?;
|
|
Ok(context.digest()?.into())
|
|
}
|
|
|
|
#[op]
|
|
pub fn op_node_hash_clone(
|
|
state: &mut OpState,
|
|
rid: ResourceId,
|
|
) -> Result<ResourceId, AnyError> {
|
|
let context = state.resource_table.get::<digest::Context>(rid)?;
|
|
Ok(state.resource_table.add(context.as_ref().clone()))
|
|
}
|