Skip to content

Commit

Permalink
refactor(bytes,crypto,encoding,http,io,msgpack,streams,tar): make Uin…
Browse files Browse the repository at this point in the history
…t8Array usages compatible with TS 5.7 (#6372)
  • Loading branch information
kt3k authored Jan 31, 2025
1 parent 61ee836 commit ace6a0e
Show file tree
Hide file tree
Showing 29 changed files with 118 additions and 29 deletions.
11 changes: 11 additions & 0 deletions bytes/_types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright 2018-2025 the Deno authors. MIT license.

/**
* Proxy type of {@code Uint8Array<ArrayBuffer} or {@code Uint8Array} in TypeScript 5.7 or below respectively.
*
* This type is internal utility type and should not be used directly.
*
* @internal @private
*/

export type Uint8Array_ = ReturnType<Uint8Array["slice"]>;
5 changes: 4 additions & 1 deletion bytes/concat.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Copyright 2018-2025 the Deno authors. MIT license.
// This module is browser compatible.

import type { Uint8Array_ } from "./_types.ts";
export type { Uint8Array_ };

/**
* Concatenate an array of byte slices into a single slice.
*
Expand All @@ -18,7 +21,7 @@
* assertEquals(concat([a, b]), new Uint8Array([0, 1, 2, 3, 4, 5]));
* ```
*/
export function concat(buffers: Uint8Array[]): Uint8Array {
export function concat(buffers: Uint8Array[]): Uint8Array_ {
let length = 0;
for (const buffer of buffers) {
length += buffer.length;
Expand Down
4 changes: 3 additions & 1 deletion bytes/repeat.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright 2018-2025 the Deno authors. MIT license.
// This module is browser compatible.
import { copy } from "./copy.ts";
import type { Uint8Array_ } from "./_types.ts";
export type { Uint8Array_ };

/**
* Returns a new byte slice composed of `count` repetitions of the `source`
Expand Down Expand Up @@ -31,7 +33,7 @@ import { copy } from "./copy.ts";
* assertEquals(repeat(source, 0), new Uint8Array());
* ```
*/
export function repeat(source: Uint8Array, count: number): Uint8Array {
export function repeat(source: Uint8Array, count: number): Uint8Array_ {
if (count < 0 || !Number.isInteger(count)) {
throw new RangeError("Count must be a non-negative integer");
}
Expand Down
6 changes: 3 additions & 3 deletions crypto/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ const stdCrypto: StdCrypto = ((x) => x)({
}
context.update(chunkBytes);
}
return context.digestAndDrop(length).buffer;
return context.digestAndDrop(length).buffer as ArrayBuffer;
} else {
throw new TypeError(
"data must be a BufferSource or [Async]Iterable<BufferSource>",
Expand All @@ -265,7 +265,7 @@ const stdCrypto: StdCrypto = ((x) => x)({
const wasmCrypto = instantiateWasm();
if (isBufferSource(data)) {
const bytes = toUint8Array(data)!;
return wasmCrypto.digest(name, bytes, length).buffer;
return wasmCrypto.digest(name, bytes, length).buffer as ArrayBuffer;
}
if (isIterable(data)) {
const context = new wasmCrypto.DigestContext(name);
Expand All @@ -278,7 +278,7 @@ const stdCrypto: StdCrypto = ((x) => x)({
}
context.update(chunkBytes);
}
return context.digestAndDrop(length).buffer;
return context.digestAndDrop(length).buffer as ArrayBuffer;
}
throw new TypeError(
"data must be a BufferSource or Iterable<BufferSource>",
Expand Down
6 changes: 5 additions & 1 deletion encoding/_base32_common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import { validateBinaryLike } from "./_validate_binary_like.ts";
import type { Uint8Array_ } from "./_types.ts";

const placeHolderPadLookup = [0, 1, , 2, 3, , 4];

Expand Down Expand Up @@ -46,7 +47,10 @@ function getByteLength(validLen: number, placeHoldersLen: number): number {
* @param lookup The lookup table
* @returns The encoded string.
*/
export function decode(b32: string, lookup: ReadonlyArray<string>): Uint8Array {
export function decode(
b32: string,
lookup: ReadonlyArray<string>,
): Uint8Array_ {
const revLookup: number[] = [];
lookup.forEach((c, i) => (revLookup[c.charCodeAt(0)] = i));

Expand Down
11 changes: 11 additions & 0 deletions encoding/_types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright 2018-2025 the Deno authors. MIT license.

/**
* Proxy type of {@code Uint8Array<ArrayBuffer} or {@code Uint8Array} in TypeScript 5.7 or below respectively.
*
* This type is internal utility type and should not be used directly.
*
* @internal @private
*/

export type Uint8Array_ = ReturnType<Uint8Array["slice"]>;
5 changes: 4 additions & 1 deletion encoding/ascii85.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Copyright 2018-2025 the Deno authors. MIT license.
// This module is browser compatible.

import type { Uint8Array_ } from "./_types.ts";
export type { Uint8Array_ };

/**
* Utilities for working with {@link https://en.wikipedia.org/wiki/Ascii85 | ascii85} encoding.
*
Expand Down Expand Up @@ -156,7 +159,7 @@ export type DecodeAscii85Options = Omit<EncodeAscii85Options, "delimiter">;
export function decodeAscii85(
ascii85: string,
options: DecodeAscii85Options = {},
): Uint8Array {
): Uint8Array_ {
const { standard = "Adobe" } = options;

// translate all encodings to most basic adobe/btoa one and decompress some special characters ("z" and "y")
Expand Down
5 changes: 4 additions & 1 deletion encoding/base32.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// Copyright (c) 2014 Jameson Little. MIT License.
// This module is browser compatible.

import type { Uint8Array_ } from "./_types.ts";
export type { Uint8Array_ };

/**
* Utilities for
* {@link https://www.rfc-editor.org/rfc/rfc4648.html#section-6 | base32}
Expand Down Expand Up @@ -48,7 +51,7 @@ lookup.forEach((c, i) => (revLookup[c.charCodeAt(0)] = i));
* );
* ```
*/
export function decodeBase32(b32: string): Uint8Array {
export function decodeBase32(b32: string): Uint8Array_ {
return decode(b32, lookup);
}

Expand Down
4 changes: 3 additions & 1 deletion encoding/base58.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/

import { validateBinaryLike } from "./_validate_binary_like.ts";
import type { Uint8Array_ } from "./_types.ts";
export type { Uint8Array_ };

// deno-fmt-ignore
const mapBase58: Record<string, number> = {
Expand Down Expand Up @@ -120,7 +122,7 @@ export function encodeBase58(data: ArrayBuffer | Uint8Array | string): string {
* );
* ```
*/
export function decodeBase58(b58: string): Uint8Array {
export function decodeBase58(b58: string): Uint8Array_ {
const splitInput = b58.trim().split("");

let length = 0;
Expand Down
2 changes: 1 addition & 1 deletion encoding/base58_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Deno.test("encodeBase58() encodes binary", () => {

Deno.test("encodeBase58() encodes binary buffer", () => {
for (const [input, output] of testSetBinary) {
assertEquals(encodeBase58(input.buffer), output);
assertEquals(encodeBase58(input.buffer as ArrayBuffer), output);
}
});

Expand Down
4 changes: 3 additions & 1 deletion encoding/base64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*/

import { validateBinaryLike } from "./_validate_binary_like.ts";
import type { Uint8Array_ } from "./_types.ts";
export type { Uint8Array_ };

const base64abc = [
"A",
Expand Down Expand Up @@ -163,7 +165,7 @@ export function encodeBase64(data: ArrayBuffer | Uint8Array | string): string {
* );
* ```
*/
export function decodeBase64(b64: string): Uint8Array {
export function decodeBase64(b64: string): Uint8Array_ {
const binString = atob(b64);
const size = binString.length;
const bytes = new Uint8Array(size);
Expand Down
2 changes: 1 addition & 1 deletion encoding/base64_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Deno.test("encodeBase64() encodes binary", () => {

Deno.test("encodeBase64() encodes binary buffer", () => {
for (const [input, output] of testsetBinary) {
assertEquals(encodeBase64(input.buffer), output);
assertEquals(encodeBase64(input.buffer as ArrayBuffer), output);
}
});

Expand Down
4 changes: 3 additions & 1 deletion encoding/base64url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*/

import * as base64 from "./base64.ts";
import type { Uint8Array_ } from "./_types.ts";
export type { Uint8Array_ };

/**
* Some variants allow or require omitting the padding '=' signs:
Expand Down Expand Up @@ -83,6 +85,6 @@ export function encodeBase64Url(
* );
* ```
*/
export function decodeBase64Url(b64url: string): Uint8Array {
export function decodeBase64Url(b64url: string): Uint8Array_ {
return base64.decodeBase64(convertBase64urlToBase64(b64url));
}
4 changes: 3 additions & 1 deletion encoding/hex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
*/

import { validateBinaryLike } from "./_validate_binary_like.ts";
import type { Uint8Array_ } from "./_types.ts";
export type { Uint8Array_ };

const hexTable = new TextEncoder().encode("0123456789abcdef");
const textEncoder = new TextEncoder();
Expand Down Expand Up @@ -100,7 +102,7 @@ export function encodeHex(src: string | Uint8Array | ArrayBuffer): string {
* );
* ```
*/
export function decodeHex(src: string): Uint8Array {
export function decodeHex(src: string): Uint8Array_ {
const u8 = textEncoder.encode(src);
const dst = new Uint8Array(u8.length / 2);
for (let i = 0; i < dst.length; i++) {
Expand Down
4 changes: 3 additions & 1 deletion encoding/unstable_base32_stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
*/

import { decodeBase32, encodeBase32 } from "./base32.ts";
import type { Uint8Array_ } from "./_types.ts";
export type { Uint8Array_ };

/**
* Converts a Uint8Array stream into a base32-encoded stream.
Expand Down Expand Up @@ -88,7 +90,7 @@ export class Base32EncoderStream extends TransformStream<Uint8Array, string> {
* assertEquals(await toText(stream), "Hello World!");
* ```
*/
export class Base32DecoderStream extends TransformStream<string, Uint8Array> {
export class Base32DecoderStream extends TransformStream<string, Uint8Array_> {
constructor() {
let push = "";
super({
Expand Down
4 changes: 3 additions & 1 deletion encoding/unstable_base32crockford.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
* @module
*/
import { decode, encode } from "./_base32_common.ts";
import type { Uint8Array_ } from "./_types.ts";
export type { Uint8Array_ };

const lookup: string[] = "0123456789ABCDEFGHJKMNPQRSTVWXYZ".split("");
const revLookup: number[] = [];
Expand All @@ -48,7 +50,7 @@ lookup.forEach((c, i) => (revLookup[c.charCodeAt(0)] = i));
* );
* ```
*/
export function decodeBase32Crockford(b32: string): Uint8Array {
export function decodeBase32Crockford(b32: string): Uint8Array_ {
return decode(b32, lookup);
}

Expand Down
4 changes: 3 additions & 1 deletion encoding/unstable_base32hex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
* @module
*/
import { decode, encode } from "./_base32_common.ts";
import type { Uint8Array_ } from "./_types.ts";
export type { Uint8Array_ };

const lookup: string[] = "0123456789ABCDEFGHIJKLMNOPQRSTUV".split("");
const revLookup: number[] = [];
Expand All @@ -54,7 +56,7 @@ lookup.forEach((c, i) => revLookup[c.charCodeAt(0)] = i);
* );
* ```
*/
export function decodeBase32Hex(b32: string): Uint8Array {
export function decodeBase32Hex(b32: string): Uint8Array_ {
return decode(b32, lookup);
}

Expand Down
4 changes: 3 additions & 1 deletion encoding/unstable_base32hex_stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
*/

import { decodeBase32Hex, encodeBase32Hex } from "./unstable_base32hex.ts";
import type { Uint8Array_ } from "./_types.ts";
export type { Uint8Array_ };

/**
* Converts a Uint8Array stream into a base32hex-encoded stream.
Expand Down Expand Up @@ -90,7 +92,7 @@ export class Base32HexEncoderStream
* ```
*/
export class Base32HexDecoderStream
extends TransformStream<string, Uint8Array> {
extends TransformStream<string, Uint8Array_> {
constructor() {
let push = "";
super({
Expand Down
4 changes: 3 additions & 1 deletion encoding/unstable_base64_stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
*/

import { decodeBase64, encodeBase64 } from "./base64.ts";
import type { Uint8Array_ } from "./_types.ts";
export type { Uint8Array_ };

/**
* Converts a Uint8Array stream into a base64-encoded stream.
Expand Down Expand Up @@ -88,7 +90,7 @@ export class Base64EncoderStream extends TransformStream<Uint8Array, string> {
* assertEquals(await toText(stream), "Hello, world!");
* ```
*/
export class Base64DecoderStream extends TransformStream<string, Uint8Array> {
export class Base64DecoderStream extends TransformStream<string, Uint8Array_> {
constructor() {
let push = "";
super({
Expand Down
4 changes: 3 additions & 1 deletion encoding/unstable_base64url_stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
*/

import { decodeBase64Url, encodeBase64Url } from "./base64url.ts";
import type { Uint8Array_ } from "./_types.ts";
export type { Uint8Array_ };

/**
* Converts a Uint8Array stream into a base64url-encoded stream.
Expand Down Expand Up @@ -91,7 +93,7 @@ export class Base64UrlEncoderStream
* ```
*/
export class Base64UrlDecoderStream
extends TransformStream<string, Uint8Array> {
extends TransformStream<string, Uint8Array_> {
constructor() {
let push = "";
super({
Expand Down
4 changes: 3 additions & 1 deletion encoding/unstable_hex_stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
*/

import { decodeHex, encodeHex } from "./hex.ts";
import type { Uint8Array_ } from "./_types.ts";
export type { Uint8Array_ };

/**
* Converts a Uint8Array stream into a hex-encoded stream.
Expand Down Expand Up @@ -74,7 +76,7 @@ export class HexEncoderStream extends TransformStream<Uint8Array, string> {
* assertEquals(await toText(stream), "Hello, world!");
* ```
*/
export class HexDecoderStream extends TransformStream<string, Uint8Array> {
export class HexDecoderStream extends TransformStream<string, Uint8Array_> {
constructor() {
let push = "";
super({
Expand Down
5 changes: 4 additions & 1 deletion encoding/varint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
// This implementation is a port of https://deno.land/x/varint@v2.0.0 by @keithamus
// This module is browser compatible.

import type { Uint8Array_ } from "./_types.ts";
export type { Uint8Array_ };

/**
* The maximum value of an unsigned 64-bit integer.
* Equivalent to `2n**64n - 1n`
Expand Down Expand Up @@ -216,7 +219,7 @@ export function encodeVarint(
num: bigint | number,
buf: Uint8Array = new Uint8Array(MaxVarintLen64),
offset = 0,
): [Uint8Array, number] {
): [Uint8Array_, number] {
num = BigInt(num);
if (num < 0n) {
throw new RangeError(
Expand Down
2 changes: 1 addition & 1 deletion http/file_server_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1125,7 +1125,7 @@ async function readUntilMatch(
match: string,
) {
const reader = source.getReader();
let buf = new Uint8Array(0);
let buf: Uint8Array = new Uint8Array(0);
const dec = new TextDecoder();
while (!dec.decode(buf).includes(match)) {
const { value } = await reader.read();
Expand Down
Loading

0 comments on commit ace6a0e

Please sign in to comment.