BREAKING(bytes): deprecate concat() signatures that don't use Uint8Array[] argument (#3784)

* BREAKING(bytes): deprecate spreaded `concat()`

* fix

* cleanup
This commit is contained in:
Asher Gomez 2023-11-13 16:34:32 +11:00 committed by GitHub
parent 192c8c8e8c
commit 029a3edc7d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 43 additions and 19 deletions

View File

@ -1,18 +1,42 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
/** Concatenate the given arrays into a new Uint8Array.
/**
* Concatenate an array of {@linkcode Uint8Array}s.
*
* @example
* ```ts
* import { concat } from "https://deno.land/std@$STD_VERSION/bytes/concat.ts";
*
* const a = new Uint8Array([0, 1, 2]);
* const b = new Uint8Array([3, 4, 5]);
* console.log(concat(a, b)); // [0, 1, 2, 3, 4, 5]
* concat([a, b]); // Uint8Array(6) [ 0, 1, 2, 3, 4, 5 ]
* ```
*/
export function concat(buf: Uint8Array[]): Uint8Array;
/**
* @deprecated (will be removed in 0.209.0) Pass in an array instead of a
* spread of arguments.
*
* Concatenate an array of {@linkcode Uint8Array}s.
*
* @example
* ```ts
* import { concat } from "https://deno.land/std@$STD_VERSION/bytes/concat.ts";
*
* const a = new Uint8Array([0, 1, 2]);
* const b = new Uint8Array([3, 4, 5]);
* concat(a, b); // Uint8Array(6) [ 0, 1, 2, 3, 4, 5 ]
* ```
*/
export function concat(...buf: Uint8Array[]): Uint8Array;
export function concat(...buf: (Uint8Array | Uint8Array[])[]): Uint8Array {
/**
* @todo(iuioiua): Revert to the old implementation upon removal of the
* spread signatures.
*
* @see {@link https://github.com/denoland/deno_std/blob/e6c61ba64d547b60076422bbc1f6ad33184cc10a/bytes/concat.ts}
*/
// No need to concatenate if there is only one element in array or sub-array
if (buf.length === 1) {
if (!Array.isArray(buf[0])) {

View File

@ -6,7 +6,7 @@ Deno.test("[bytes] concat", () => {
const encoder = new TextEncoder();
const u1 = encoder.encode("Hello ");
const u2 = encoder.encode("World");
const joined = concat(u1, u2);
const joined = concat([u1, u2]);
assertEquals(new TextDecoder().decode(joined), "Hello World");
assert(u1 !== joined);
assert(u2 !== joined);
@ -15,7 +15,7 @@ Deno.test("[bytes] concat", () => {
Deno.test("[bytes] concat empty arrays", () => {
const u1 = new Uint8Array();
const u2 = new Uint8Array();
const joined = concat(u1, u2);
const joined = concat([u1, u2]);
assertEquals(joined.byteLength, 0);
assert(u1 !== joined);
assert(u2 !== joined);
@ -29,7 +29,7 @@ Deno.test("[bytes] concat multiple Uint8Array", () => {
const u4 = encoder.encode("r");
const u5 = encoder.encode("l");
const u6 = encoder.encode("d");
const joined = concat(u1, u2, u3, u4, u5, u6);
const joined = concat([u1, u2, u3, u4, u5, u6]);
assertEquals(new TextDecoder().decode(joined), "Hello World");
assert(u1 !== joined);
assert(u2 !== joined);

View File

@ -54,7 +54,7 @@ export async function* readDelim(
// Discard all remaining and silently fail.
return;
}
chunks = concat(chunks, inspectArr.slice(0, result));
chunks = concat([chunks, inspectArr.slice(0, result)]);
let localIndex = 0;
while (inspectIndex < chunks.length) {
if (inspectArr[localIndex] === delim[matchIndex]) {

View File

@ -38,13 +38,13 @@ export async function* readLines(
const res = await bufReader.readLine();
if (!res) {
if (chunks.length > 0) {
yield decoder.decode(concat(...chunks));
yield decoder.decode(concat(chunks));
}
break;
}
chunks.push(res.line);
if (!res.more) {
yield decoder.decode(concat(...chunks));
yield decoder.decode(concat(chunks));
chunks = [];
}
}

View File

@ -120,7 +120,7 @@ export class DelimiterStream extends TransformStream<Uint8Array, Uint8Array> {
// Concat not needed when a single buffer is passed.
controller.enqueue(bufs[0]);
} else {
controller.enqueue(concat(...bufs));
controller.enqueue(concat(bufs));
}
// Drop all previous chunks.
bufs.length = 0;
@ -143,7 +143,7 @@ export class DelimiterStream extends TransformStream<Uint8Array, Uint8Array> {
controller.enqueue(lastSliced);
} else {
bufs[lastIndex] = lastSliced;
controller.enqueue(concat(...bufs));
controller.enqueue(concat(bufs));
}
bufs.length = 0;
if (disposition === "prefix") {
@ -156,7 +156,7 @@ export class DelimiterStream extends TransformStream<Uint8Array, Uint8Array> {
} else if (delimitedChunkEnd > 0 && bufs.length > 0) {
// Previous chunks and current chunk together form a delimited chunk.
const chunkSliced = chunk.subarray(chunkStart, delimitedChunkEnd);
const result = concat(...bufs, chunkSliced);
const result = concat([...bufs, chunkSliced]);
bufs.length = 0;
controller.enqueue(result);
chunkStart = disposition === "prefix"
@ -279,7 +279,7 @@ export class DelimiterStream extends TransformStream<Uint8Array, Uint8Array> {
} else if (length === 1) {
controller.enqueue(bufs[0]);
} else {
controller.enqueue(concat(...bufs));
controller.enqueue(concat(bufs));
}
}
}

View File

@ -40,7 +40,7 @@ Deno.test("[streams] readableStreamFromReader()", async function () {
const stream = readableStreamFromReader(reader);
const actual = await Array.fromAsync(stream);
const decoder = new TextDecoder();
assertEquals(decoder.decode(concat(...actual)), "hello deno land");
assertEquals(decoder.decode(concat(actual)), "hello deno land");
});
Deno.test({
@ -59,7 +59,7 @@ Deno.test("[streams] readableStreamFromReader() - calls close", async function (
const stream = readableStreamFromReader(reader);
const actual = await Array.fromAsync(stream);
const decoder = new TextDecoder();
assertEquals(decoder.decode(concat(...actual)), "hello deno land");
assertEquals(decoder.decode(concat(actual)), "hello deno land");
assertEquals(reader.closeCall, 1);
});
@ -74,7 +74,7 @@ Deno.test("[streams] readableStreamFromReader() - doesn't call close with autoCl
const stream = readableStreamFromReader(reader, { autoClose: false });
const actual = await Array.fromAsync(stream);
const decoder = new TextDecoder();
assertEquals(decoder.decode(concat(...actual)), "hello deno land");
assertEquals(decoder.decode(concat(actual)), "hello deno land");
assertEquals(reader.closeCall, 0);
});
@ -90,6 +90,6 @@ Deno.test("[streams] readableStreamFromReader() - chunkSize", async function ()
const actual = await Array.fromAsync(stream);
const decoder = new TextDecoder();
assertEquals(actual.length, 8);
assertEquals(decoder.decode(concat(...actual)), "hello deno land");
assertEquals(decoder.decode(concat(actual)), "hello deno land");
assertEquals(reader.closeCall, 1);
});

View File

@ -19,5 +19,5 @@ export async function toArrayBuffer(
chunks.push(value);
}
return concat(...chunks).buffer;
return concat(chunks).buffer;
}

View File

@ -50,7 +50,7 @@ export async function generate(
const space = uuidToBytes(namespace);
assert(space.length === 16, "namespace must be a valid UUID");
const toHash = concat(new Uint8Array(space), data);
const toHash = concat([new Uint8Array(space), data]);
const buffer = await crypto.subtle.digest("MD5", toHash);
const bytes = new Uint8Array(buffer);

View File

@ -49,7 +49,7 @@ export async function generate(
const space = uuidToBytes(namespace);
assert(space.length === 16, "namespace must be a valid UUID");
const toHash = concat(new Uint8Array(space), data);
const toHash = concat([new Uint8Array(space), data]);
const buffer = await crypto.subtle.digest("sha-1", toHash);
const bytes = new Uint8Array(buffer);