Skip to content

Commit

Permalink
feat(std/node/stream): Add Duplex, Transform, Passthrough, pipeline, …
Browse files Browse the repository at this point in the history
…finished and promises (denoland#7940)
  • Loading branch information
Soremwar authored Nov 26, 2020
1 parent 60e980c commit 9042fcc
Show file tree
Hide file tree
Showing 34 changed files with 4,121 additions and 1,109 deletions.
10 changes: 5 additions & 5 deletions std/node/_buffer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import * as hex from "../encoding/hex.ts";
import * as base64 from "../encoding/base64.ts";
import { normalizeEncoding, notImplemented } from "./_utils.ts";
import { Encodings, normalizeEncoding, notImplemented } from "./_utils.ts";

const notImplementedEncodings = [
"ascii",
Expand All @@ -11,7 +11,7 @@ const notImplementedEncodings = [
"utf16le",
];

function checkEncoding(encoding = "utf8", strict = true): string {
function checkEncoding(encoding = "utf8", strict = true): Encodings {
if (typeof encoding !== "string" || (strict && encoding === "")) {
if (!strict) return "utf8";
throw new TypeError(`Unkown encoding: ${encoding}`);
Expand Down Expand Up @@ -93,14 +93,14 @@ export class Buffer extends Uint8Array {

let bufFill;
if (typeof fill === "string") {
encoding = checkEncoding(encoding);
const clearEncoding = checkEncoding(encoding);
if (
typeof fill === "string" &&
fill.length === 1 &&
encoding === "utf8"
clearEncoding === "utf8"
) {
buf.fill(fill.charCodeAt(0));
} else bufFill = Buffer.from(fill, encoding);
} else bufFill = Buffer.from(fill, clearEncoding);
} else if (typeof fill === "number") {
buf.fill(fill);
} else if (fill instanceof Uint8Array) {
Expand Down
2 changes: 1 addition & 1 deletion std/node/_errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1232,7 +1232,7 @@ export class ERR_INVALID_BUFFER_SIZE extends NodeRangeError {
}
}
export class ERR_INVALID_CALLBACK extends NodeTypeError {
constructor(object: { [key: string]: unknown }) {
constructor(object: unknown) {
super(
"ERR_INVALID_CALLBACK",
`Callback must be a function. Received ${JSON.stringify(object)}`,
Expand Down
3 changes: 1 addition & 2 deletions std/node/_fs/_fs_appendFile.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import {
CallbackWithError,
Encodings,
getOpenOptions,
isFileOptions,
WriteFileOptions,
} from "./_fs_common.ts";
import { notImplemented } from "../_utils.ts";
import { Encodings, notImplemented } from "../_utils.ts";
import { fromFileUrl } from "../path.ts";

/**
Expand Down
20 changes: 6 additions & 14 deletions std/node/_fs/_fs_common.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { notImplemented } from "../_utils.ts";
import {
BinaryEncodings,
Encodings,
notImplemented,
TextEncodings,
} from "../_utils.ts";

export type CallbackWithError = (err?: Error | null) => void;

export type TextEncodings =
| "ascii"
| "utf8"
| "utf-8"
| "utf16le"
| "ucs2"
| "ucs-2"
| "base64"
| "latin1"
| "hex";
export type BinaryEncodings = "binary";
export type Encodings = TextEncodings | BinaryEncodings;

export interface FileOptions {
encoding?: Encodings;
flag?: string;
Expand Down
4 changes: 1 addition & 3 deletions std/node/_fs/_fs_readFile.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import {
BinaryEncodings,
BinaryOptionsArgument,
Encodings,
FileOptionsArgument,
getEncoding,
TextEncodings,
TextOptionsArgument,
} from "./_fs_common.ts";
import { Buffer } from "../buffer.ts";
import { fromFileUrl } from "../path.ts";
import { BinaryEncodings, Encodings, TextEncodings } from "../_utils.ts";

function maybeDecode(data: Uint8Array, encoding: TextEncodings): string;
function maybeDecode(
Expand Down
4 changes: 1 addition & 3 deletions std/node/_fs/_fs_writeFile.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { notImplemented } from "../_utils.ts";
import { Encodings, notImplemented } from "../_utils.ts";
import { fromFileUrl } from "../path.ts";
import { Buffer } from "../buffer.ts";

import {
CallbackWithError,
checkEncoding,
Encodings,
getEncoding,
getOpenOptions,
isFileOptions,
Expand Down
2 changes: 1 addition & 1 deletion std/node/_fs/_fs_writeFile_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
assertThrows,
} from "../../testing/asserts.ts";
import { writeFile, writeFileSync } from "./_fs_writeFile.ts";
import type { TextEncodings } from "./_fs_common.ts";
import type { TextEncodings } from "../_utils.ts";
import * as path from "../../path/mod.ts";

const moduleDir = path.dirname(path.fromFileUrl(import.meta.url));
Expand Down
3 changes: 2 additions & 1 deletion std/node/_fs/promises/_fs_writeFile.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import type { Encodings, WriteFileOptions } from "../_fs_common.ts";
import type { WriteFileOptions } from "../_fs_common.ts";
import type { Encodings } from "../../_utils.ts";

import { writeFile as writeFileCallback } from "../_fs_writeFile.ts";

Expand Down
2 changes: 1 addition & 1 deletion std/node/_fs/promises/_fs_writeFile_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
assertThrowsAsync,
} from "../../../testing/asserts.ts";
import { writeFile } from "./_fs_writeFile.ts";
import type { TextEncodings } from "../_fs_common.ts";
import type { TextEncodings } from "../../_utils.ts";

const decoder = new TextDecoder("utf-8");

Expand Down
23 changes: 3 additions & 20 deletions std/node/_stream/async_iterator.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Copyright Node.js contributors. All rights reserved. MIT License.
import type { Buffer } from "../buffer.ts";
import finished from "./end-of-stream.ts";
import finished from "./end_of_stream.ts";
import Readable from "./readable.ts";
import type Stream from "./stream.ts";
import { destroyer } from "./destroy.ts";

const kLastResolve = Symbol("lastResolve");
const kLastReject = Symbol("lastReject");
Expand Down Expand Up @@ -34,24 +35,6 @@ function initIteratorSymbols(
Object.defineProperties(o, properties);
}

// TODO(Soremwar)
// Bring back once requests are implemented
// function isRequest(stream: any) {
// return stream && stream.setHeader && typeof stream.abort === "function";
// }

//TODO(Soremwar)
//Should be any implementation of stream
// deno-lint-ignore no-explicit-any
function destroyer(stream: any, err?: Error | null) {
// TODO(Soremwar)
// Bring back once requests are implemented
// if (isRequest(stream)) return stream.abort();
// if (isRequest(stream.req)) return stream.req.abort();
if (typeof stream.destroy === "function") return stream.destroy(err);
if (typeof stream.close === "function") return stream.close();
}

function createIterResult(
value: IterableItem,
done: boolean,
Expand Down Expand Up @@ -119,7 +102,7 @@ const AsyncIteratorPrototype = Object.getPrototypeOf(
Object.getPrototypeOf(async function* () {}).prototype,
);

class ReadableStreamAsyncIterator
export class ReadableStreamAsyncIterator
implements AsyncIterableIterator<IterableItem> {
[kEnded]: boolean;
[kError]: Error | null = null;
Expand Down
38 changes: 38 additions & 0 deletions std/node/_stream/destroy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright Node.js contributors. All rights reserved. MIT License.
import type Duplex from "./duplex.ts";
import type Readable from "./readable.ts";
import type Stream from "./stream.ts";
import type Writable from "./writable.ts";

//This whole module acts as a 'normalizer'
//Idea behind it is you can pass any kind of streams and functions will execute anyways

//TODO(Soremwar)
//Should be any implementation of stream
//This is a guard to check executed methods exist inside the implementation
type StreamImplementations = Duplex | Readable | Writable;

// TODO(Soremwar)
// Bring back once requests are implemented
// function isRequest(stream: any) {
// return stream && stream.setHeader && typeof stream.abort === "function";
// }

export function destroyer(stream: Stream, err?: Error | null) {
// TODO(Soremwar)
// Bring back once requests are implemented
// if (isRequest(stream)) return stream.abort();
// if (isRequest(stream.req)) return stream.req.abort();
if (
typeof (stream as StreamImplementations).destroy === "function"
) {
return (stream as StreamImplementations).destroy(err);
}
// A test of async iterator mocks an upcoming implementation of stream
// his is casted to any in the meanwhile
// deno-lint-ignore no-explicit-any
if (typeof (stream as any).close === "function") {
// deno-lint-ignore no-explicit-any
return (stream as any).close();
}
}
Loading

0 comments on commit 9042fcc

Please sign in to comment.