Skip to content

Commit

Permalink
Showing 37 changed files with 115 additions and 76 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -116,3 +116,19 @@ All internal non-test code, that is files that do not have `test` or `bench` in
the name, must use the assertion functions within `_utils/asserts.ts` and not
`testing/asserts.ts`. This is to create a separation of concerns between
internal and testing assertions.

### Types

Deno is moving away from non-native IO functions and interfaces in favor of the
[Streams API](https://developer.mozilla.org/en-US/docs/Web/API/Streams_API).
These types are to be defined here, in the Standard Library, instead of in the
Deno namespace in the future. As a rule, use the following corresponding and
identical types from `types.d.ts`:

- `Deno.Reader`
- `Deno.Writer`
- `Deno.ReaderSync`
- `Deno.WriterSync`
- `Deno.Closer`

See the tracking issue [here](https://github.com/denoland/deno/issues/9795).
6 changes: 2 additions & 4 deletions archive/_common.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.

import { PartialReadError } from "../io/buffer.ts";

export type Reader = Deno.Reader;
export type Seeker = Deno.Seeker;
import type { Reader } from "../types.d.ts";

export interface TarInfo {
fileMode?: number;
@@ -140,7 +138,7 @@ export const ustarStructure: Array<{ field: string; length: number }> = [
];

export async function readBlock(
reader: Deno.Reader,
reader: Reader,
p: Uint8Array,
): Promise<number | null> {
let bytesRead = 0;
4 changes: 1 addition & 3 deletions archive/tar.ts
Original file line number Diff line number Diff line change
@@ -7,17 +7,15 @@ import {
} from "./untar.ts";
import {
FileTypes,
type Reader,
type TarInfo,
type TarMeta,
type TarOptions,
ustarStructure,
} from "./_common.ts";
import type { Reader } from "../types.d.ts";

export { type TarInfo, type TarMeta, type TarOptions };

export type Seeker = Deno.Seeker;

/*!
* Ported and modified from: https://github.com/beatgammit/tar-js and
* licensed as:
7 changes: 3 additions & 4 deletions archive/untar.ts
Original file line number Diff line number Diff line change
@@ -3,13 +3,12 @@
import {
FileTypes,
readBlock,
type Reader,
recordSize,
type Seeker,
type TarMeta,
ustarStructure,
} from "./_common.ts";
import { readAll } from "../streams/read_all.ts";
import type { Reader } from "../types.d.ts";

/*!
* Ported and modified from: https://github.com/beatgammit/tar-js and
@@ -139,8 +138,8 @@ export class TarEntry implements Reader {
if (this.#consumed) return;
this.#consumed = true;

if (typeof (this.#reader as Seeker).seek === "function") {
await (this.#reader as Seeker).seek(
if (typeof (this.#reader as Deno.Seeker).seek === "function") {
await (this.#reader as Deno.Seeker).seek(
this.#entrySize - this.#read,
Deno.SeekMode.Current,
);
13 changes: 7 additions & 6 deletions encoding/binary.ts
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
*
* @module
*/
import type { Reader, Writer } from "../types.d.ts";

type RawBaseType = "int8" | "int16" | "int32" | "uint8" | "uint16" | "uint32";
type RawNumberType = RawBaseType | "float32" | "float64";
@@ -52,7 +53,7 @@ export function sizeof(dataType: DataType): number {
*
* Throws `Deno.errors.UnexpectedEof` if `n` bytes cannot be read. */
export async function readExact(
r: Deno.Reader,
r: Reader,
b: Uint8Array,
) {
let totalRead = 0;
@@ -69,7 +70,7 @@ export async function readExact(
*
* Resolves it in a `Uint8Array`, or throws `Deno.errors.UnexpectedEof` if `n` bytes cannot be read. */
export async function getNBytes(
r: Deno.Reader,
r: Reader,
n: number,
): Promise<Uint8Array> {
const scratch = new Uint8Array(n);
@@ -219,7 +220,7 @@ export function putVarbig(
*
* `o.dataType` defaults to `"int32"`. */
export async function readVarnum(
r: Deno.Reader,
r: Reader,
o: VarnumOptions = {},
): Promise<number> {
o.dataType = o.dataType ?? "int32";
@@ -231,7 +232,7 @@ export async function readVarnum(
*
* `o.dataType` defaults to `"int64"`. */
export async function readVarbig(
r: Deno.Reader,
r: Reader,
o: VarbigOptions = {},
): Promise<bigint> {
o.dataType = o.dataType ?? "int64";
@@ -243,7 +244,7 @@ export async function readVarbig(
*
* `o.dataType` defaults to `"int32"`. */
export function writeVarnum(
w: Deno.Writer,
w: Writer,
x: number,
o: VarnumOptions = {},
): Promise<number> {
@@ -257,7 +258,7 @@ export function writeVarnum(
*
* `o.dataType` defaults to `"int64"`. */
export function writeVarbig(
w: Deno.Writer,
w: Writer,
x: bigint,
o: VarbigOptions = {},
): Promise<number> {
2 changes: 1 addition & 1 deletion io/_test_common.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.

import type { Reader } from "./types.d.ts";
import type { Reader } from "../types.d.ts";

export const MIN_READ_BUFFER_SIZE = 16;
export const bufsizes: number[] = [
2 changes: 1 addition & 1 deletion io/buf_reader.ts
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@

import { assert } from "../_util/asserts.ts";
import { copy } from "../bytes/copy.ts";
import type { Reader } from "./types.d.ts";
import type { Reader } from "../types.d.ts";

const DEFAULT_BUF_SIZE = 4096;
const MIN_BUF_SIZE = 16;
8 changes: 4 additions & 4 deletions io/buf_reader_test.ts
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ import { BufferFullError, BufReader, PartialReadError } from "./buf_reader.ts";
import { StringReader } from "./string_reader.ts";
import { bufsizes, MIN_READ_BUFFER_SIZE } from "./_test_common.ts";
import { Buffer } from "./buffer.ts";
import type { Reader } from "./types.d.ts";
import type { Reader } from "../types.d.ts";
import { copy } from "../bytes/copy.ts";

/** OneByteReader returns a Reader that implements
@@ -64,11 +64,11 @@ async function readBytes(buf: BufReader): Promise<string> {

interface ReadMaker {
name: string;
fn: (r: Deno.Reader) => Deno.Reader;
fn: (r: Reader) => Reader;
}

const readMakers: ReadMaker[] = [
{ name: "full", fn: (r): Deno.Reader => r },
{ name: "full", fn: (r): Reader => r },
{
name: "byte",
fn: (r): OneByteReader => new OneByteReader(r),
@@ -313,7 +313,7 @@ const testInputrn = encoder.encode(
const testOutput = encoder.encode("0123456789abcdefghijklmnopqrstuvwxy");

// TestReader wraps a Uint8Array and returns reads of a specific length.
class TestReader implements Deno.Reader {
class TestReader implements Reader {
constructor(private data: Uint8Array, private stride: number) {}

read(buf: Uint8Array): Promise<number | null> {
2 changes: 1 addition & 1 deletion io/buf_writer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.

import { copy } from "../bytes/copy.ts";
import type { Writer, WriterSync } from "./types.d.ts";
import type { Writer, WriterSync } from "../types.d.ts";

const DEFAULT_BUF_SIZE = 4096;

5 changes: 3 additions & 2 deletions io/buf_writer_test.ts
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ import { BufWriter, BufWriterSync } from "./buf_writer.ts";
import { Buffer } from "./buffer.ts";
import { StringWriter } from "./string_writer.ts";
import { bufsizes } from "./_test_common.ts";
import type { Writer, WriterSync } from "../types.d.ts";

Deno.test("bufioWriter", async function () {
const data = new Uint8Array(8192);
@@ -112,7 +113,7 @@ Deno.test({
data.fill("a".charCodeAt(0));

const cache: Uint8Array[] = [];
const writer: Deno.Writer = {
const writer: Writer = {
write(p: Uint8Array): Promise<number> {
cache.push(p.subarray(0, 1));

@@ -140,7 +141,7 @@ Deno.test({
data.fill("a".charCodeAt(0));

const cache: Uint8Array[] = [];
const writer: Deno.WriterSync = {
const writer: WriterSync = {
writeSync(p: Uint8Array): number {
cache.push(p.subarray(0, 1));
// Writer that only writes 1 byte at a time
2 changes: 1 addition & 1 deletion io/buffer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { assert } from "../_util/asserts.ts";
import { copy } from "../bytes/copy.ts";
import type { Reader, ReaderSync } from "./types.d.ts";
import type { Reader, ReaderSync } from "../types.d.ts";
import {
BufferFullError as _BufferFullError,
BufReader as _BufReader,
2 changes: 1 addition & 1 deletion io/copy_n.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.

import { assert } from "../_util/asserts.ts";
import type { Reader, Writer } from "./types.d.ts";
import type { Reader, Writer } from "../types.d.ts";

const DEFAULT_BUFFER_SIZE = 32 * 1024;

6 changes: 4 additions & 2 deletions io/limited_reader.ts
Original file line number Diff line number Diff line change
@@ -5,8 +5,10 @@
* `read` returns `null` when `limit` <= `0` or
* when the underlying `reader` returns `null`.
*/
export class LimitedReader implements Deno.Reader {
constructor(public reader: Deno.Reader, public limit: number) {}
import type { Reader } from "../types.d.ts";

export class LimitedReader implements Reader {
constructor(public reader: Reader, public limit: number) {}

async read(p: Uint8Array): Promise<number | null> {
if (this.limit <= 0) {
8 changes: 5 additions & 3 deletions io/multi_reader.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.

import type { Reader } from "../types.d.ts";

/** Reader utility for combining multiple readers */
export class MultiReader implements Deno.Reader {
readonly #readers: Deno.Reader[];
export class MultiReader implements Reader {
readonly #readers: Reader[];
#currentIndex = 0;

constructor(readers: Deno.Reader[]) {
constructor(readers: Reader[]) {
this.#readers = [...readers];
}

2 changes: 1 addition & 1 deletion io/read_delim.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.

import { BytesList } from "../bytes/bytes_list.ts";
import type { Reader } from "./types.d.ts";
import type { Reader } from "../types.d.ts";

/** Generate longest proper prefix which is also suffix array. */
function createLPS(pat: Uint8Array): Uint8Array {
2 changes: 1 addition & 1 deletion io/read_lines.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.

import { type Reader } from "./types.d.ts";
import { type Reader } from "../types.d.ts";
import { BufReader } from "./buf_reader.ts";
import { concat } from "../bytes/concat.ts";

5 changes: 3 additions & 2 deletions io/read_range.ts
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@

import { copy as copyBytes } from "../bytes/copy.ts";
import { assert } from "../_util/asserts.ts";
import type { Reader, ReaderSync } from "../types.d.ts";

const DEFAULT_BUFFER_SIZE = 32 * 1024;

@@ -29,7 +30,7 @@ export interface ByteRange {
* ```
*/
export async function readRange(
r: Deno.Reader & Deno.Seeker,
r: Reader & Deno.Seeker,
range: ByteRange,
): Promise<Uint8Array> {
// byte ranges are inclusive, so we have to add one to the end
@@ -67,7 +68,7 @@ export async function readRange(
* ```
*/
export function readRangeSync(
r: Deno.ReaderSync & Deno.SeekerSync,
r: ReaderSync & Deno.SeekerSync,
range: ByteRange,
): Uint8Array {
// byte ranges are inclusive, so we have to add one to the end
8 changes: 2 additions & 6 deletions io/read_range_test.ts
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ import {
assertThrows,
} from "../testing/asserts.ts";
import { readRange, readRangeSync } from "./read_range.ts";
import type { Closer, Reader, ReaderSync } from "../types.d.ts";

// N controls how many iterations of certain checks are performed.
const N = 100;
@@ -23,12 +24,7 @@ export function init() {
}

class MockFile
implements
Deno.Seeker,
Deno.SeekerSync,
Deno.Reader,
Deno.ReaderSync,
Deno.Closer {
implements Deno.Seeker, Deno.SeekerSync, Reader, ReaderSync, Closer {
#buf: Uint8Array;
#closed = false;
#offset = 0;
2 changes: 1 addition & 1 deletion io/read_string_delim.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.

import { type Reader } from "./types.d.ts";
import { type Reader } from "../types.d.ts";
import { readDelim } from "./read_delim.ts";

/**
2 changes: 1 addition & 1 deletion io/string_writer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.

import type { Writer, WriterSync } from "./types.d.ts";
import type { Writer, WriterSync } from "../types.d.ts";

const decoder = new TextDecoder();

3 changes: 2 additions & 1 deletion log/handlers.ts
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ import type { LogRecord } from "./logger.ts";
import { blue, bold, red, yellow } from "../fmt/colors.ts";
import { exists, existsSync } from "../fs/exists.ts";
import { BufWriterSync } from "../io/buf_writer.ts";
import type { Writer } from "../types.d.ts";

const DEFAULT_FORMATTER = "{levelName} {msg}";
export type FormatterFunction = (logRecord: LogRecord) => string;
@@ -88,7 +89,7 @@ export class ConsoleHandler extends BaseHandler {
}

export abstract class WriterHandler extends BaseHandler {
protected _writer!: Deno.Writer;
protected _writer!: Writer;
#encoder = new TextEncoder();

abstract override log(msg: string): void;
3 changes: 2 additions & 1 deletion node/_fs/_fs_writeFile.ts
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@ import {
validateStringAfterArrayBufferView,
} from "../internal/fs/utils.mjs";
import { promisify } from "../internal/util.mjs";
import type { Writer } from "../../types.d.ts";

export function writeFile(
pathOrRid: string | number | URL,
@@ -156,7 +157,7 @@ interface WriteAllOptions {
signal?: AbortSignal;
}
async function writeAll(
w: Deno.Writer,
w: Writer,
arr: Uint8Array,
options: WriteAllOptions = {},
) {
Loading
Oops, something went wrong.

0 comments on commit 2f9baf6

Please sign in to comment.