Skip to content

Commit

Permalink
feat: Add Deno.core.encodeBinaryString() (denoland#558)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju committed Feb 6, 2024
1 parent 81ee417 commit 33bed3f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
2 changes: 2 additions & 0 deletions core/01_core.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@
op_destructure_error,
op_dispatch_exception,
op_encode,
op_encode_binary_string,
op_eval_context,
op_event_loop_has_more_work,
op_get_promise_details,
Expand Down Expand Up @@ -591,6 +592,7 @@
) => op_eval_context(source, specifier),
hostObjectBrand,
encode: (text) => op_encode(text),
encodeBinaryString: (buffer) => op_encode_binary_string(buffer),
decode: (buffer) => op_decode(buffer),
serialize: (
value,
Expand Down
2 changes: 2 additions & 0 deletions core/lib.deno_core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,8 @@ declare namespace Deno {

function createCancelHandle(): number;

function encodeBinaryString(data: Uint8Array): string;

const build: {
target: string;
arch: string;
Expand Down
8 changes: 8 additions & 0 deletions core/ops_builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::OpState;
use crate::Resource;
use anyhow::Error;
use bytes::BytesMut;
use serde_v8::ByteString;
use std::cell::RefCell;
use std::io::stderr;
use std::io::stdout;
Expand Down Expand Up @@ -56,6 +57,7 @@ builtin_ops! {
op_str_byte_length,
op_panic,
op_cancel_handle,
op_encode_binary_string,
ops_builtin_types::op_is_any_array_buffer,
ops_builtin_types::op_is_arguments_object,
ops_builtin_types::op_is_array_buffer,
Expand Down Expand Up @@ -381,3 +383,9 @@ fn op_str_byte_length(
pub fn op_cancel_handle(state: &mut OpState) -> u32 {
state.resource_table.add(CancelHandle::new())
}

#[op2]
#[serde]
fn op_encode_binary_string(#[buffer] s: &[u8]) -> ByteString {
ByteString::from(s)
}
36 changes: 36 additions & 0 deletions testing/unit/encode_decode_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,39 @@ test(function testStringTooLarge() {
}
assert(thrown);
});

test(function binaryEncode() {
function asBinaryString(bytes: Uint8Array): string {
return Array.from(bytes).map(
(v: number) => String.fromCodePoint(v),
).join("");
}

function decodeBinary(binaryString: string) {
const chars: string[] = Array.from(binaryString);
return chars.map((v: string): number | undefined => v.codePointAt(0));
}

// invalid utf-8 code points
const invalid = new Uint8Array([0xC0]);
assertArrayEquals(
Deno.core.encodeBinaryString(invalid),
asBinaryString(invalid),
);

const invalid2 = new Uint8Array([0xC1]);
assertArrayEquals(
Deno.core.encodeBinaryString(invalid2),
asBinaryString(invalid2),
);

for (let i = 0, j = 255; i <= 255; i++, j--) {
const bytes = new Uint8Array([i, j]);
const binaryString = Deno.core.encodeBinaryString(bytes);
assertArrayEquals(
binaryString,
asBinaryString(bytes),
);
assertArrayEquals(Array.from(bytes), decodeBinary(binaryString));
}
});

0 comments on commit 33bed3f

Please sign in to comment.