From 1eecc5c7d0a1f7e8f1ab83fd9263bd3a81185c46 Mon Sep 17 00:00:00 2001 From: Nayeem Rahman Date: Sat, 9 May 2020 13:34:47 +0100 Subject: [PATCH] BREAKING: reorganization (denoland/deno#5087) * Prepend underscores to private modules * Remove collectUint8Arrays() It would be a misuse of Deno.iter()'s result. * Move _util/async.ts to async * Move util/sha*.ts to hash --- README.md | 33 +++-- {util => _util}/deep_assign.ts | 0 {util => _util}/deep_assign_test.ts | 0 {util => _util}/has_own_property.ts | 0 async/deferred.ts | 26 ++++ async/deferred_test.ts | 8 ++ async/delay.ts | 9 ++ async/mod.ts | 4 + async/mux_async_iterator.ts | 58 +++++++++ async/mux_async_iterator_test.ts | 28 +++++ encoding/README.md | 4 +- encoding/{yaml => _yaml}/dumper/dumper.ts | 0 .../{yaml => _yaml}/dumper/dumper_state.ts | 0 encoding/{yaml => _yaml}/error.ts | 0 encoding/{yaml => _yaml}/example/dump.ts | 0 encoding/{yaml => _yaml}/example/inout.ts | 0 encoding/{yaml => _yaml}/example/parse.ts | 0 .../example/sample_document.ts | 0 .../example/sample_document.yml | 0 encoding/{yaml => _yaml}/loader/loader.ts | 0 .../{yaml => _yaml}/loader/loader_state.ts | 0 encoding/{yaml => _yaml}/mark.ts | 0 encoding/{yaml => _yaml}/parse.ts | 0 encoding/{yaml => _yaml}/parse_test.ts | 0 encoding/{yaml => _yaml}/schema.ts | 0 encoding/{yaml => _yaml}/schema/core.ts | 0 encoding/{yaml => _yaml}/schema/default.ts | 0 encoding/{yaml => _yaml}/schema/failsafe.ts | 0 encoding/{yaml => _yaml}/schema/json.ts | 0 encoding/{yaml => _yaml}/schema/mod.ts | 0 encoding/{yaml => _yaml}/state.ts | 0 encoding/{yaml => _yaml}/stringify.ts | 0 encoding/{yaml => _yaml}/stringify_test.ts | 0 encoding/{yaml => _yaml}/type.ts | 0 encoding/{yaml => _yaml}/type/binary.ts | 0 encoding/{yaml => _yaml}/type/bool.ts | 0 encoding/{yaml => _yaml}/type/float.ts | 0 encoding/{yaml => _yaml}/type/int.ts | 0 encoding/{yaml => _yaml}/type/map.ts | 0 encoding/{yaml => _yaml}/type/merge.ts | 0 encoding/{yaml => _yaml}/type/mod.ts | 0 encoding/{yaml => _yaml}/type/nil.ts | 0 encoding/{yaml => _yaml}/type/omap.ts | 0 encoding/{yaml => _yaml}/type/pairs.ts | 0 encoding/{yaml => _yaml}/type/seq.ts | 0 encoding/{yaml => _yaml}/type/set.ts | 0 encoding/{yaml => _yaml}/type/str.ts | 0 encoding/{yaml => _yaml}/type/timestamp.ts | 0 encoding/{yaml => _yaml}/utils.ts | 0 encoding/toml.ts | 2 +- encoding/yaml.ts | 6 +- encoding/yaml_test.ts | 4 +- examples/chat/server_test.ts | 2 +- flags/example.ts => examples/flags.ts | 6 +- flags/README.md | 13 +- flags/mod.ts | 9 +- fs/{utils.ts => _util.ts} | 0 fs/{utils_test.ts => _util_test.ts} | 2 +- fs/copy.ts | 2 +- fs/ensure_dir.ts | 2 +- fs/ensure_file.ts | 2 +- fs/ensure_link.ts | 2 +- fs/ensure_symlink.ts | 2 +- fs/expand_glob.ts | 3 +- fs/move.ts | 2 +- {util => hash}/sha1.ts | 0 {util => hash}/sha1_test.ts | 0 {util => hash}/sha256.ts | 0 {util => hash}/sha256_test.ts | 0 http/{io.ts => _io.ts} | 0 http/{io_test.ts => _io_test.ts} | 6 +- http/{mock.ts => _mock_conn.ts} | 0 http/racing_server.ts | 2 +- http/server.ts | 4 +- http/server_test.ts | 4 +- io/{iotest.ts => _iotest.ts} | 0 io/bufio_test.ts | 2 +- mime/multipart.ts | 2 +- node/module.ts | 2 +- path/{constants.ts => _constants.ts} | 4 +- path/{globrex.ts => _globrex.ts} | 0 path/{globrex_test.ts => _globrex_test.ts} | 2 +- path/{utils.ts => _util.ts} | 2 +- path/common.ts | 2 +- path/glob.ts | 4 +- path/mod.ts | 7 +- path/posix.ts | 4 +- path/separator.ts | 4 + path/win32.ts | 4 +- signal/mod.ts | 2 +- signal/test.ts | 2 +- util/async.ts | 117 ------------------ util/async_test.ts | 76 ------------ uuid/v5.ts | 2 +- ws/example_client.ts | 90 +++++++------- ws/example_server.ts | 80 ++++++------ ws/mod.ts | 8 +- ws/test.ts | 2 +- 98 files changed, 302 insertions(+), 361 deletions(-) rename {util => _util}/deep_assign.ts (100%) rename {util => _util}/deep_assign_test.ts (100%) rename {util => _util}/has_own_property.ts (100%) create mode 100644 async/deferred.ts create mode 100644 async/deferred_test.ts create mode 100644 async/delay.ts create mode 100644 async/mod.ts create mode 100644 async/mux_async_iterator.ts create mode 100644 async/mux_async_iterator_test.ts rename encoding/{yaml => _yaml}/dumper/dumper.ts (100%) rename encoding/{yaml => _yaml}/dumper/dumper_state.ts (100%) rename encoding/{yaml => _yaml}/error.ts (100%) rename encoding/{yaml => _yaml}/example/dump.ts (100%) rename encoding/{yaml => _yaml}/example/inout.ts (100%) rename encoding/{yaml => _yaml}/example/parse.ts (100%) rename encoding/{yaml => _yaml}/example/sample_document.ts (100%) rename encoding/{yaml => _yaml}/example/sample_document.yml (100%) rename encoding/{yaml => _yaml}/loader/loader.ts (100%) rename encoding/{yaml => _yaml}/loader/loader_state.ts (100%) rename encoding/{yaml => _yaml}/mark.ts (100%) rename encoding/{yaml => _yaml}/parse.ts (100%) rename encoding/{yaml => _yaml}/parse_test.ts (100%) rename encoding/{yaml => _yaml}/schema.ts (100%) rename encoding/{yaml => _yaml}/schema/core.ts (100%) rename encoding/{yaml => _yaml}/schema/default.ts (100%) rename encoding/{yaml => _yaml}/schema/failsafe.ts (100%) rename encoding/{yaml => _yaml}/schema/json.ts (100%) rename encoding/{yaml => _yaml}/schema/mod.ts (100%) rename encoding/{yaml => _yaml}/state.ts (100%) rename encoding/{yaml => _yaml}/stringify.ts (100%) rename encoding/{yaml => _yaml}/stringify_test.ts (100%) rename encoding/{yaml => _yaml}/type.ts (100%) rename encoding/{yaml => _yaml}/type/binary.ts (100%) rename encoding/{yaml => _yaml}/type/bool.ts (100%) rename encoding/{yaml => _yaml}/type/float.ts (100%) rename encoding/{yaml => _yaml}/type/int.ts (100%) rename encoding/{yaml => _yaml}/type/map.ts (100%) rename encoding/{yaml => _yaml}/type/merge.ts (100%) rename encoding/{yaml => _yaml}/type/mod.ts (100%) rename encoding/{yaml => _yaml}/type/nil.ts (100%) rename encoding/{yaml => _yaml}/type/omap.ts (100%) rename encoding/{yaml => _yaml}/type/pairs.ts (100%) rename encoding/{yaml => _yaml}/type/seq.ts (100%) rename encoding/{yaml => _yaml}/type/set.ts (100%) rename encoding/{yaml => _yaml}/type/str.ts (100%) rename encoding/{yaml => _yaml}/type/timestamp.ts (100%) rename encoding/{yaml => _yaml}/utils.ts (100%) rename flags/example.ts => examples/flags.ts (51%) rename fs/{utils.ts => _util.ts} (100%) rename fs/{utils_test.ts => _util_test.ts} (96%) rename {util => hash}/sha1.ts (100%) rename {util => hash}/sha1_test.ts (100%) rename {util => hash}/sha256.ts (100%) rename {util => hash}/sha256_test.ts (100%) rename http/{io.ts => _io.ts} (100%) rename http/{io_test.ts => _io_test.ts} (99%) rename http/{mock.ts => _mock_conn.ts} (100%) rename io/{iotest.ts => _iotest.ts} (100%) rename path/{constants.ts => _constants.ts} (95%) rename path/{globrex.ts => _globrex.ts} (100%) rename path/{globrex_test.ts => _globrex_test.ts} (99%) rename path/{utils.ts => _util.ts} (99%) create mode 100644 path/separator.ts delete mode 100644 util/async.ts delete mode 100644 util/async_test.ts diff --git a/README.md b/README.md index 584b7dce9..24ac92ced 100644 --- a/README.md +++ b/README.md @@ -19,28 +19,27 @@ Contributions are welcome! These modules are tagged in accordance with Deno releases. So, for example, the v0.3.0 tag is guaranteed to work with deno v0.3.0. You can link to v0.3.0 using -the URL `https://deno.land/std@v0.3.0/` +the URL `https://deno.land/std@v0.3.0/`. Not specifying a tag will link to the +master branch. -It's strongly recommended that you link to tagged releases rather than the -master branch. The project is still young and we expect disruptive renames in -the future. +It is strongly recommended that you link to tagged releases to avoid unintended +updates. + +Don't link to / import any module whose path: + +- Has a name or parent with an underscore prefix: `_foo.ts`, `_util/bar.ts`. +- Is that of a test module or test data: `test.ts`, `foo_test.ts`, + `testdata/bar.txt`. + +No stability is guaranteed for these files. ## Documentation -Here are the dedicated documentations of modules: +To browse documentation for modules: -- [colors](fmt/colors.ts) -- [datetime](datetime/README.md) -- [encoding](encoding/README.md) -- [examples](examples/README.md) -- [flags](flags/README.md) -- [fs](fs/README.md) -- [http](http/README.md) -- [log](log/README.md) -- [node](node/README.md) -- [testing](testing/README.md) -- [uuid](uuid/README.md) -- [ws](ws/README.md) +- Go to https://deno.land/std/. +- Navigate to any module of interest. +- Click the "DOCUMENTATION" link. ## Contributing diff --git a/util/deep_assign.ts b/_util/deep_assign.ts similarity index 100% rename from util/deep_assign.ts rename to _util/deep_assign.ts diff --git a/util/deep_assign_test.ts b/_util/deep_assign_test.ts similarity index 100% rename from util/deep_assign_test.ts rename to _util/deep_assign_test.ts diff --git a/util/has_own_property.ts b/_util/has_own_property.ts similarity index 100% rename from util/has_own_property.ts rename to _util/has_own_property.ts diff --git a/async/deferred.ts b/async/deferred.ts new file mode 100644 index 000000000..109a1a37e --- /dev/null +++ b/async/deferred.ts @@ -0,0 +1,26 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +// TODO(ry) It'd be better to make Deferred a class that inherits from +// Promise, rather than an interface. This is possible in ES2016, however +// typescript produces broken code when targeting ES5 code. +// See https://github.com/Microsoft/TypeScript/issues/15202 +// At the time of writing, the github issue is closed but the problem remains. +export interface Deferred extends Promise { + resolve: (value?: T | PromiseLike) => void; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + reject: (reason?: any) => void; +} + +/** Creates a Promise with the `reject` and `resolve` functions + * placed as methods on the promise object itself. It allows you to do: + * + * const p = deferred(); + * // ... + * p.resolve(42); + */ +export function deferred(): Deferred { + let methods; + const promise = new Promise((resolve, reject): void => { + methods = { resolve, reject }; + }); + return Object.assign(promise, methods) as Deferred; +} diff --git a/async/deferred_test.ts b/async/deferred_test.ts new file mode 100644 index 000000000..83c317853 --- /dev/null +++ b/async/deferred_test.ts @@ -0,0 +1,8 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +import { deferred } from "./deferred.ts"; + +Deno.test("[async] deferred", function (): Promise { + const d = deferred(); + d.resolve(12); + return Promise.resolve(); +}); diff --git a/async/delay.ts b/async/delay.ts new file mode 100644 index 000000000..e3aec368f --- /dev/null +++ b/async/delay.ts @@ -0,0 +1,9 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +/* Resolves after the given number of milliseconds. */ +export function delay(ms: number): Promise { + return new Promise((res): number => + setTimeout((): void => { + res(); + }, ms) + ); +} diff --git a/async/mod.ts b/async/mod.ts new file mode 100644 index 000000000..9efead91d --- /dev/null +++ b/async/mod.ts @@ -0,0 +1,4 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +export * from "./deferred.ts"; +export * from "./delay.ts"; +export * from "./mux_async_iterator.ts"; diff --git a/async/mux_async_iterator.ts b/async/mux_async_iterator.ts new file mode 100644 index 000000000..b32689a29 --- /dev/null +++ b/async/mux_async_iterator.ts @@ -0,0 +1,58 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +import { Deferred, deferred } from "./deferred.ts"; + +interface TaggedYieldedValue { + iterator: AsyncIterableIterator; + value: T; +} + +/** The MuxAsyncIterator class multiplexes multiple async iterators into a + * single stream. It currently makes a few assumptions: + * - The iterators do not throw. + * - The final result (the value returned and not yielded from the iterator) + * does not matter; if there is any, it is discarded. + */ +export class MuxAsyncIterator implements AsyncIterable { + private iteratorCount = 0; + private yields: Array> = []; + private signal: Deferred = deferred(); + + add(iterator: AsyncIterableIterator): void { + ++this.iteratorCount; + this.callIteratorNext(iterator); + } + + private async callIteratorNext( + iterator: AsyncIterableIterator + ): Promise { + const { value, done } = await iterator.next(); + if (done) { + --this.iteratorCount; + } else { + this.yields.push({ iterator, value }); + } + this.signal.resolve(); + } + + async *iterate(): AsyncIterableIterator { + while (this.iteratorCount > 0) { + // Sleep until any of the wrapped iterators yields. + await this.signal; + + // Note that while we're looping over `yields`, new items may be added. + for (let i = 0; i < this.yields.length; i++) { + const { iterator, value } = this.yields[i]; + yield value; + this.callIteratorNext(iterator); + } + + // Clear the `yields` list and reset the `signal` promise. + this.yields.length = 0; + this.signal = deferred(); + } + } + + [Symbol.asyncIterator](): AsyncIterableIterator { + return this.iterate(); + } +} diff --git a/async/mux_async_iterator_test.ts b/async/mux_async_iterator_test.ts new file mode 100644 index 000000000..7017a4eba --- /dev/null +++ b/async/mux_async_iterator_test.ts @@ -0,0 +1,28 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +import { assertEquals } from "../testing/asserts.ts"; +import { MuxAsyncIterator } from "./mux_async_iterator.ts"; + +// eslint-disable-next-line require-await +async function* gen123(): AsyncIterableIterator { + yield 1; + yield 2; + yield 3; +} + +// eslint-disable-next-line require-await +async function* gen456(): AsyncIterableIterator { + yield 4; + yield 5; + yield 6; +} + +Deno.test("[async] MuxAsyncIterator", async function (): Promise { + const mux = new MuxAsyncIterator(); + mux.add(gen123()); + mux.add(gen456()); + const results = new Set(); + for await (const value of mux) { + results.add(value); + } + assertEquals(results.size, 6); +}); diff --git a/encoding/README.md b/encoding/README.md index 7d9c89117..973713360 100644 --- a/encoding/README.md +++ b/encoding/README.md @@ -243,9 +243,7 @@ Serializes `object` as a YAML document. ### More example -See [`./yaml/example`](./yaml/example) folder and [js-yaml] repository. - -[js-yaml]: https://github.com/nodeca/js-yaml +See https://github.com/nodeca/js-yaml. ## base32 diff --git a/encoding/yaml/dumper/dumper.ts b/encoding/_yaml/dumper/dumper.ts similarity index 100% rename from encoding/yaml/dumper/dumper.ts rename to encoding/_yaml/dumper/dumper.ts diff --git a/encoding/yaml/dumper/dumper_state.ts b/encoding/_yaml/dumper/dumper_state.ts similarity index 100% rename from encoding/yaml/dumper/dumper_state.ts rename to encoding/_yaml/dumper/dumper_state.ts diff --git a/encoding/yaml/error.ts b/encoding/_yaml/error.ts similarity index 100% rename from encoding/yaml/error.ts rename to encoding/_yaml/error.ts diff --git a/encoding/yaml/example/dump.ts b/encoding/_yaml/example/dump.ts similarity index 100% rename from encoding/yaml/example/dump.ts rename to encoding/_yaml/example/dump.ts diff --git a/encoding/yaml/example/inout.ts b/encoding/_yaml/example/inout.ts similarity index 100% rename from encoding/yaml/example/inout.ts rename to encoding/_yaml/example/inout.ts diff --git a/encoding/yaml/example/parse.ts b/encoding/_yaml/example/parse.ts similarity index 100% rename from encoding/yaml/example/parse.ts rename to encoding/_yaml/example/parse.ts diff --git a/encoding/yaml/example/sample_document.ts b/encoding/_yaml/example/sample_document.ts similarity index 100% rename from encoding/yaml/example/sample_document.ts rename to encoding/_yaml/example/sample_document.ts diff --git a/encoding/yaml/example/sample_document.yml b/encoding/_yaml/example/sample_document.yml similarity index 100% rename from encoding/yaml/example/sample_document.yml rename to encoding/_yaml/example/sample_document.yml diff --git a/encoding/yaml/loader/loader.ts b/encoding/_yaml/loader/loader.ts similarity index 100% rename from encoding/yaml/loader/loader.ts rename to encoding/_yaml/loader/loader.ts diff --git a/encoding/yaml/loader/loader_state.ts b/encoding/_yaml/loader/loader_state.ts similarity index 100% rename from encoding/yaml/loader/loader_state.ts rename to encoding/_yaml/loader/loader_state.ts diff --git a/encoding/yaml/mark.ts b/encoding/_yaml/mark.ts similarity index 100% rename from encoding/yaml/mark.ts rename to encoding/_yaml/mark.ts diff --git a/encoding/yaml/parse.ts b/encoding/_yaml/parse.ts similarity index 100% rename from encoding/yaml/parse.ts rename to encoding/_yaml/parse.ts diff --git a/encoding/yaml/parse_test.ts b/encoding/_yaml/parse_test.ts similarity index 100% rename from encoding/yaml/parse_test.ts rename to encoding/_yaml/parse_test.ts diff --git a/encoding/yaml/schema.ts b/encoding/_yaml/schema.ts similarity index 100% rename from encoding/yaml/schema.ts rename to encoding/_yaml/schema.ts diff --git a/encoding/yaml/schema/core.ts b/encoding/_yaml/schema/core.ts similarity index 100% rename from encoding/yaml/schema/core.ts rename to encoding/_yaml/schema/core.ts diff --git a/encoding/yaml/schema/default.ts b/encoding/_yaml/schema/default.ts similarity index 100% rename from encoding/yaml/schema/default.ts rename to encoding/_yaml/schema/default.ts diff --git a/encoding/yaml/schema/failsafe.ts b/encoding/_yaml/schema/failsafe.ts similarity index 100% rename from encoding/yaml/schema/failsafe.ts rename to encoding/_yaml/schema/failsafe.ts diff --git a/encoding/yaml/schema/json.ts b/encoding/_yaml/schema/json.ts similarity index 100% rename from encoding/yaml/schema/json.ts rename to encoding/_yaml/schema/json.ts diff --git a/encoding/yaml/schema/mod.ts b/encoding/_yaml/schema/mod.ts similarity index 100% rename from encoding/yaml/schema/mod.ts rename to encoding/_yaml/schema/mod.ts diff --git a/encoding/yaml/state.ts b/encoding/_yaml/state.ts similarity index 100% rename from encoding/yaml/state.ts rename to encoding/_yaml/state.ts diff --git a/encoding/yaml/stringify.ts b/encoding/_yaml/stringify.ts similarity index 100% rename from encoding/yaml/stringify.ts rename to encoding/_yaml/stringify.ts diff --git a/encoding/yaml/stringify_test.ts b/encoding/_yaml/stringify_test.ts similarity index 100% rename from encoding/yaml/stringify_test.ts rename to encoding/_yaml/stringify_test.ts diff --git a/encoding/yaml/type.ts b/encoding/_yaml/type.ts similarity index 100% rename from encoding/yaml/type.ts rename to encoding/_yaml/type.ts diff --git a/encoding/yaml/type/binary.ts b/encoding/_yaml/type/binary.ts similarity index 100% rename from encoding/yaml/type/binary.ts rename to encoding/_yaml/type/binary.ts diff --git a/encoding/yaml/type/bool.ts b/encoding/_yaml/type/bool.ts similarity index 100% rename from encoding/yaml/type/bool.ts rename to encoding/_yaml/type/bool.ts diff --git a/encoding/yaml/type/float.ts b/encoding/_yaml/type/float.ts similarity index 100% rename from encoding/yaml/type/float.ts rename to encoding/_yaml/type/float.ts diff --git a/encoding/yaml/type/int.ts b/encoding/_yaml/type/int.ts similarity index 100% rename from encoding/yaml/type/int.ts rename to encoding/_yaml/type/int.ts diff --git a/encoding/yaml/type/map.ts b/encoding/_yaml/type/map.ts similarity index 100% rename from encoding/yaml/type/map.ts rename to encoding/_yaml/type/map.ts diff --git a/encoding/yaml/type/merge.ts b/encoding/_yaml/type/merge.ts similarity index 100% rename from encoding/yaml/type/merge.ts rename to encoding/_yaml/type/merge.ts diff --git a/encoding/yaml/type/mod.ts b/encoding/_yaml/type/mod.ts similarity index 100% rename from encoding/yaml/type/mod.ts rename to encoding/_yaml/type/mod.ts diff --git a/encoding/yaml/type/nil.ts b/encoding/_yaml/type/nil.ts similarity index 100% rename from encoding/yaml/type/nil.ts rename to encoding/_yaml/type/nil.ts diff --git a/encoding/yaml/type/omap.ts b/encoding/_yaml/type/omap.ts similarity index 100% rename from encoding/yaml/type/omap.ts rename to encoding/_yaml/type/omap.ts diff --git a/encoding/yaml/type/pairs.ts b/encoding/_yaml/type/pairs.ts similarity index 100% rename from encoding/yaml/type/pairs.ts rename to encoding/_yaml/type/pairs.ts diff --git a/encoding/yaml/type/seq.ts b/encoding/_yaml/type/seq.ts similarity index 100% rename from encoding/yaml/type/seq.ts rename to encoding/_yaml/type/seq.ts diff --git a/encoding/yaml/type/set.ts b/encoding/_yaml/type/set.ts similarity index 100% rename from encoding/yaml/type/set.ts rename to encoding/_yaml/type/set.ts diff --git a/encoding/yaml/type/str.ts b/encoding/_yaml/type/str.ts similarity index 100% rename from encoding/yaml/type/str.ts rename to encoding/_yaml/type/str.ts diff --git a/encoding/yaml/type/timestamp.ts b/encoding/_yaml/type/timestamp.ts similarity index 100% rename from encoding/yaml/type/timestamp.ts rename to encoding/_yaml/type/timestamp.ts diff --git a/encoding/yaml/utils.ts b/encoding/_yaml/utils.ts similarity index 100% rename from encoding/yaml/utils.ts rename to encoding/_yaml/utils.ts diff --git a/encoding/toml.ts b/encoding/toml.ts index ce4519572..7e44bdc18 100644 --- a/encoding/toml.ts +++ b/encoding/toml.ts @@ -1,5 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { deepAssign } from "../util/deep_assign.ts"; +import { deepAssign } from "../_util/deep_assign.ts"; import { assert } from "../testing/asserts.ts"; class KeyValuePair { diff --git a/encoding/yaml.ts b/encoding/yaml.ts index 76b1b8379..abe210d85 100644 --- a/encoding/yaml.ts +++ b/encoding/yaml.ts @@ -3,9 +3,9 @@ // Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -export { ParseOptions, parse, parseAll } from "./yaml/parse.ts"; +export { ParseOptions, parse, parseAll } from "./_yaml/parse.ts"; export { DumpOptions as StringifyOptions, stringify, -} from "./yaml/stringify.ts"; -export * from "./yaml/schema/mod.ts"; +} from "./_yaml/stringify.ts"; +export { Schema, SchemaDefinition, TypeMap } from "./_yaml/schema/mod.ts"; diff --git a/encoding/yaml_test.ts b/encoding/yaml_test.ts index a5a8374cc..d65be604c 100644 --- a/encoding/yaml_test.ts +++ b/encoding/yaml_test.ts @@ -1,4 +1,4 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import "./yaml/parse_test.ts"; -import "./yaml/stringify_test.ts"; +import "./_yaml/parse_test.ts"; +import "./_yaml/stringify_test.ts"; diff --git a/examples/chat/server_test.ts b/examples/chat/server_test.ts index 92eb50f92..7375de47a 100644 --- a/examples/chat/server_test.ts +++ b/examples/chat/server_test.ts @@ -3,7 +3,7 @@ import { assert, assertEquals } from "../../testing/asserts.ts"; import { TextProtoReader } from "../../textproto/mod.ts"; import { BufReader } from "../../io/bufio.ts"; import { connectWebSocket, WebSocket } from "../../ws/mod.ts"; -import { delay } from "../../util/async.ts"; +import { delay } from "../../async/delay.ts"; const { test } = Deno; diff --git a/flags/example.ts b/examples/flags.ts similarity index 51% rename from flags/example.ts rename to examples/flags.ts index ad0317269..4625b8c96 100644 --- a/flags/example.ts +++ b/examples/flags.ts @@ -1,5 +1,7 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. const { args } = Deno; -import { parse } from "./mod.ts"; +import { parse } from "../flags/mod.ts"; -console.dir(parse(args)); +if (import.meta.main) { + console.dir(parse(args)); +} diff --git a/flags/README.md b/flags/README.md index e9764f913..0b7f7fa65 100644 --- a/flags/README.md +++ b/flags/README.md @@ -12,12 +12,12 @@ console.dir(parse(args)); ``` ``` -$ deno run example.ts -a beep -b boop +$ deno run https://deno.land/std/examples/flags.ts -a beep -b boop { _: [], a: 'beep', b: 'boop' } ``` ``` -$ deno run example.ts -x 3 -y 4 -n5 -abc --beep=boop foo bar baz +$ deno run https://deno.land/std/examples/flags.ts -x 3 -y 4 -n5 -abc --beep=boop foo bar baz { _: [ 'foo', 'bar', 'baz' ], x: 3, y: 4, @@ -56,16 +56,13 @@ options can be: the `--` and `parsedArgs['--']` with everything after the `--`. Here's an example: ```ts + // $ deno run example.ts -- a arg1 const { args } = Deno; import { parse } from "https://deno.land/std/flags/mod.ts"; - // options['--'] is now set to false console.dir(parse(args, { "--": false })); - // $ deno run example.ts -- a arg1 - // output: { _: [ "example.ts", "a", "arg1" ] } - // options['--'] is now set to true + // output: { _: [ "a", "arg1" ] } console.dir(parse(args, { "--": true })); - // $ deno run example.ts -- a arg1 - // output: { _: [ "example.ts" ], --: [ "a", "arg1" ] } + // output: { _: [], --: [ "a", "arg1" ] } ``` - `options.unknown` - a function which is invoked with a command line parameter not defined in the `options` configuration object. If the function returns diff --git a/flags/mod.ts b/flags/mod.ts index 0563dab61..e3680087d 100644 --- a/flags/mod.ts +++ b/flags/mod.ts @@ -14,16 +14,13 @@ export interface ArgParsingOptions { /** When `true`, populate the result `_` with everything before the `--` and * the result `['--']` with everything after the `--`. Here's an example: * + * // $ deno run example.ts -- a arg1 * const { args } = Deno; * import { parse } from "https://deno.land/std/flags/mod.ts"; - * // options['--'] is now set to false * console.dir(parse(args, { "--": false })); - * // $ deno run example.ts -- a arg1 - * // output: { _: [ "example.ts", "a", "arg1" ] } - * // options['--'] is now set to true + * // output: { _: [ "a", "arg1" ] } * console.dir(parse(args, { "--": true })); - * // $ deno run example.ts -- a arg1 - * // output: { _: [ "example.ts" ], --: [ "a", "arg1" ] } + * // output: { _: [], --: [ "a", "arg1" ] } * * Defaults to `false`. */ diff --git a/fs/utils.ts b/fs/_util.ts similarity index 100% rename from fs/utils.ts rename to fs/_util.ts diff --git a/fs/utils_test.ts b/fs/_util_test.ts similarity index 96% rename from fs/utils_test.ts rename to fs/_util_test.ts index e104e3e7d..48fc33ecd 100644 --- a/fs/utils_test.ts +++ b/fs/_util_test.ts @@ -2,7 +2,7 @@ import { assertEquals } from "../testing/asserts.ts"; import * as path from "../path/mod.ts"; -import { isSubdir, getFileInfoType, PathType } from "./utils.ts"; +import { isSubdir, getFileInfoType, PathType } from "./_util.ts"; import { ensureFileSync } from "./ensure_file.ts"; import { ensureDirSync } from "./ensure_dir.ts"; diff --git a/fs/copy.ts b/fs/copy.ts index 609a8437b..85558d905 100644 --- a/fs/copy.ts +++ b/fs/copy.ts @@ -1,7 +1,7 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. import * as path from "../path/mod.ts"; import { ensureDir, ensureDirSync } from "./ensure_dir.ts"; -import { isSubdir, getFileInfoType } from "./utils.ts"; +import { isSubdir, getFileInfoType } from "./_util.ts"; import { assert } from "../testing/asserts.ts"; export interface CopyOptions { diff --git a/fs/ensure_dir.ts b/fs/ensure_dir.ts index ecc7356ef..34053b157 100644 --- a/fs/ensure_dir.ts +++ b/fs/ensure_dir.ts @@ -1,5 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { getFileInfoType } from "./utils.ts"; +import { getFileInfoType } from "./_util.ts"; const { lstat, lstatSync, mkdir, mkdirSync } = Deno; /** diff --git a/fs/ensure_file.ts b/fs/ensure_file.ts index de6cab500..2539c71ac 100644 --- a/fs/ensure_file.ts +++ b/fs/ensure_file.ts @@ -1,7 +1,7 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. import * as path from "../path/mod.ts"; import { ensureDir, ensureDirSync } from "./ensure_dir.ts"; -import { getFileInfoType } from "./utils.ts"; +import { getFileInfoType } from "./_util.ts"; const { lstat, lstatSync, writeFile, writeFileSync } = Deno; /** diff --git a/fs/ensure_link.ts b/fs/ensure_link.ts index e43325a25..f446df781 100644 --- a/fs/ensure_link.ts +++ b/fs/ensure_link.ts @@ -2,7 +2,7 @@ import * as path from "../path/mod.ts"; import { ensureDir, ensureDirSync } from "./ensure_dir.ts"; import { exists, existsSync } from "./exists.ts"; -import { getFileInfoType } from "./utils.ts"; +import { getFileInfoType } from "./_util.ts"; /** * Ensures that the hard link exists. diff --git a/fs/ensure_symlink.ts b/fs/ensure_symlink.ts index 03c355b5d..2a184bb4f 100644 --- a/fs/ensure_symlink.ts +++ b/fs/ensure_symlink.ts @@ -2,7 +2,7 @@ import * as path from "../path/mod.ts"; import { ensureDir, ensureDirSync } from "./ensure_dir.ts"; import { exists, existsSync } from "./exists.ts"; -import { getFileInfoType } from "./utils.ts"; +import { getFileInfoType } from "./_util.ts"; /** * Ensures that the link exists. diff --git a/fs/expand_glob.ts b/fs/expand_glob.ts index 803c67cdf..e5abdabcf 100644 --- a/fs/expand_glob.ts +++ b/fs/expand_glob.ts @@ -4,7 +4,6 @@ import { globToRegExp, isAbsolute, isGlob, - isWindows, joinGlobs, normalize, } from "../path/mod.ts"; @@ -19,6 +18,8 @@ import { assert } from "../testing/asserts.ts"; const { cwd } = Deno; type FileInfo = Deno.FileInfo; +const isWindows = Deno.build.os == "windows"; + export interface ExpandGlobOptions extends GlobOptions { root?: string; exclude?: string[]; diff --git a/fs/move.ts b/fs/move.ts index cf1e49193..4a6395365 100644 --- a/fs/move.ts +++ b/fs/move.ts @@ -1,6 +1,6 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. import { exists, existsSync } from "./exists.ts"; -import { isSubdir } from "./utils.ts"; +import { isSubdir } from "./_util.ts"; interface MoveOptions { overwrite?: boolean; diff --git a/util/sha1.ts b/hash/sha1.ts similarity index 100% rename from util/sha1.ts rename to hash/sha1.ts diff --git a/util/sha1_test.ts b/hash/sha1_test.ts similarity index 100% rename from util/sha1_test.ts rename to hash/sha1_test.ts diff --git a/util/sha256.ts b/hash/sha256.ts similarity index 100% rename from util/sha256.ts rename to hash/sha256.ts diff --git a/util/sha256_test.ts b/hash/sha256_test.ts similarity index 100% rename from util/sha256_test.ts rename to hash/sha256_test.ts diff --git a/http/io.ts b/http/_io.ts similarity index 100% rename from http/io.ts rename to http/_io.ts diff --git a/http/io_test.ts b/http/_io_test.ts similarity index 99% rename from http/io_test.ts rename to http/_io_test.ts index c0f57a1b7..c22ebdf07 100644 --- a/http/io_test.ts +++ b/http/_io_test.ts @@ -7,18 +7,18 @@ import { } from "../testing/asserts.ts"; import { bodyReader, + chunkedBodyReader, writeTrailers, readTrailers, parseHTTPVersion, readRequest, writeResponse, -} from "./io.ts"; +} from "./_io.ts"; import { encode, decode } from "../encoding/utf8.ts"; import { BufReader, ReadLineResult } from "../io/bufio.ts"; -import { chunkedBodyReader } from "./io.ts"; import { ServerRequest, Response } from "./server.ts"; import { StringReader } from "../io/readers.ts"; -import { mockConn } from "./mock.ts"; +import { mockConn } from "./_mock_conn.ts"; const { Buffer, test, readAll } = Deno; test("bodyReader", async () => { diff --git a/http/mock.ts b/http/_mock_conn.ts similarity index 100% rename from http/mock.ts rename to http/_mock_conn.ts diff --git a/http/racing_server.ts b/http/racing_server.ts index 0b0e5a8a5..67db029e0 100644 --- a/http/racing_server.ts +++ b/http/racing_server.ts @@ -1,6 +1,6 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. import { serve, ServerRequest } from "./server.ts"; -import { delay } from "../util/async.ts"; +import { delay } from "../async/delay.ts"; const addr = Deno.args[1] || "127.0.0.1:4501"; const server = serve(addr); diff --git a/http/server.ts b/http/server.ts index 9c678ad3d..a372b39a5 100644 --- a/http/server.ts +++ b/http/server.ts @@ -2,14 +2,14 @@ import { encode } from "../encoding/utf8.ts"; import { BufReader, BufWriter } from "../io/bufio.ts"; import { assert } from "../testing/asserts.ts"; -import { deferred, Deferred, MuxAsyncIterator } from "../util/async.ts"; +import { deferred, Deferred, MuxAsyncIterator } from "../async/mod.ts"; import { bodyReader, chunkedBodyReader, emptyReader, writeResponse, readRequest, -} from "./io.ts"; +} from "./_io.ts"; import Listener = Deno.Listener; import Conn = Deno.Conn; import Reader = Deno.Reader; diff --git a/http/server_test.ts b/http/server_test.ts index 03256dd25..807695c6b 100644 --- a/http/server_test.ts +++ b/http/server_test.ts @@ -15,9 +15,9 @@ import { } from "../testing/asserts.ts"; import { Response, ServerRequest, Server, serve } from "./server.ts"; import { BufReader, BufWriter } from "../io/bufio.ts"; -import { delay } from "../util/async.ts"; +import { delay } from "../async/delay.ts"; import { encode, decode } from "../encoding/utf8.ts"; -import { mockConn } from "./mock.ts"; +import { mockConn } from "./_mock_conn.ts"; const { Buffer, test } = Deno; diff --git a/io/iotest.ts b/io/_iotest.ts similarity index 100% rename from io/iotest.ts rename to io/_iotest.ts diff --git a/io/bufio_test.ts b/io/bufio_test.ts index 17cc873fd..c49023814 100644 --- a/io/bufio_test.ts +++ b/io/bufio_test.ts @@ -15,7 +15,7 @@ import { readStringDelim, readLines, } from "./bufio.ts"; -import * as iotest from "./iotest.ts"; +import * as iotest from "./_iotest.ts"; import { charCode, copyBytes, stringsReader } from "./util.ts"; const encoder = new TextEncoder(); diff --git a/mime/multipart.ts b/mime/multipart.ts index 6aadef938..61faebefd 100644 --- a/mime/multipart.ts +++ b/mime/multipart.ts @@ -14,7 +14,7 @@ import { BufReader, BufWriter } from "../io/bufio.ts"; import { encoder } from "../encoding/utf8.ts"; import { assertStrictEq, assert } from "../testing/asserts.ts"; import { TextProtoReader } from "../textproto/mod.ts"; -import { hasOwnProperty } from "../util/has_own_property.ts"; +import { hasOwnProperty } from "../_util/has_own_property.ts"; /** FormFile object */ export interface FormFile { diff --git a/node/module.ts b/node/module.ts index 8969e80d9..f777e658f 100644 --- a/node/module.ts +++ b/node/module.ts @@ -36,7 +36,7 @@ const CHAR_FORWARD_SLASH = "/".charCodeAt(0); const CHAR_BACKWARD_SLASH = "\\".charCodeAt(0); const CHAR_COLON = ":".charCodeAt(0); -const isWindows = path.isWindows; +const isWindows = Deno.build.os == "windows"; const relativeResolveCache = Object.create(null); diff --git a/path/constants.ts b/path/_constants.ts similarity index 95% rename from path/constants.ts rename to path/_constants.ts index 97d3bcf58..ae0aac184 100644 --- a/path/constants.ts +++ b/path/_constants.ts @@ -48,7 +48,7 @@ export const CHAR_EQUAL = 61; /* = */ export const CHAR_0 = 48; /* 0 */ export const CHAR_9 = 57; /* 9 */ -export const isWindows = build.os === "windows"; -export const EOL = isWindows ? "\r\n" : "\n"; +const isWindows = build.os == "windows"; + export const SEP = isWindows ? "\\" : "/"; export const SEP_PATTERN = isWindows ? /[\\/]+/ : /\/+/; diff --git a/path/globrex.ts b/path/_globrex.ts similarity index 100% rename from path/globrex.ts rename to path/_globrex.ts diff --git a/path/globrex_test.ts b/path/_globrex_test.ts similarity index 99% rename from path/globrex_test.ts rename to path/_globrex_test.ts index c52ed108e..2974b4719 100644 --- a/path/globrex_test.ts +++ b/path/_globrex_test.ts @@ -4,7 +4,7 @@ const { test } = Deno; import { assertEquals } from "../testing/asserts.ts"; -import { GlobrexOptions, globrex } from "./globrex.ts"; +import { GlobrexOptions, globrex } from "./_globrex.ts"; const isWin = Deno.build.os === "windows"; const t = { equal: assertEquals, is: assertEquals }; diff --git a/path/utils.ts b/path/_util.ts similarity index 99% rename from path/utils.ts rename to path/_util.ts index fc3dc5be9..2776303cb 100644 --- a/path/utils.ts +++ b/path/_util.ts @@ -10,7 +10,7 @@ import { CHAR_DOT, CHAR_FORWARD_SLASH, CHAR_BACKWARD_SLASH, -} from "./constants.ts"; +} from "./_constants.ts"; export function assertPath(path: string): void { if (typeof path !== "string") { diff --git a/path/common.ts b/path/common.ts index 0a4de3f0c..e0e51ef23 100644 --- a/path/common.ts +++ b/path/common.ts @@ -1,6 +1,6 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { SEP } from "./constants.ts"; +import { SEP } from "./separator.ts"; /** Determines the common path from a set of paths, using an optional separator, * which defaults to the OS default separator. diff --git a/path/glob.ts b/path/glob.ts index a11865c26..80672579d 100644 --- a/path/glob.ts +++ b/path/glob.ts @@ -1,5 +1,5 @@ -import { SEP, SEP_PATTERN } from "./constants.ts"; -import { globrex } from "./globrex.ts"; +import { SEP, SEP_PATTERN } from "./separator.ts"; +import { globrex } from "./_globrex.ts"; import { join, normalize } from "./mod.ts"; import { assert } from "../testing/asserts.ts"; diff --git a/path/mod.ts b/path/mod.ts index 104e0b616..9cb7f1edb 100644 --- a/path/mod.ts +++ b/path/mod.ts @@ -4,7 +4,7 @@ import * as _win32 from "./win32.ts"; import * as _posix from "./posix.ts"; -import { isWindows } from "./constants.ts"; +const isWindows = Deno.build.os == "windows"; const path = isWindows ? _win32 : _posix; @@ -27,8 +27,7 @@ export const { toNamespacedPath, } = path; -export { common } from "./common.ts"; -export { EOL, SEP, SEP_PATTERN, isWindows } from "./constants.ts"; +export * from "./common.ts"; +export { SEP, SEP_PATTERN } from "./separator.ts"; export * from "./interface.ts"; export * from "./glob.ts"; -export * from "./globrex.ts"; diff --git a/path/posix.ts b/path/posix.ts index 156aba796..e88eb3f97 100644 --- a/path/posix.ts +++ b/path/posix.ts @@ -3,14 +3,14 @@ const { cwd } = Deno; import { FormatInputPathObject, ParsedPath } from "./interface.ts"; -import { CHAR_DOT, CHAR_FORWARD_SLASH } from "./constants.ts"; +import { CHAR_DOT, CHAR_FORWARD_SLASH } from "./_constants.ts"; import { assertPath, normalizeString, isPosixPathSeparator, _format, -} from "./utils.ts"; +} from "./_util.ts"; export const sep = "/"; export const delimiter = ":"; diff --git a/path/separator.ts b/path/separator.ts new file mode 100644 index 000000000..fb990b808 --- /dev/null +++ b/path/separator.ts @@ -0,0 +1,4 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +const isWindows = Deno.build.os == "windows"; +export const SEP = isWindows ? "\\" : "/"; +export const SEP_PATTERN = isWindows ? /[\\/]+/ : /\/+/; diff --git a/path/win32.ts b/path/win32.ts index 401e572db..0557b3768 100644 --- a/path/win32.ts +++ b/path/win32.ts @@ -8,7 +8,7 @@ import { CHAR_BACKWARD_SLASH, CHAR_COLON, CHAR_QUESTION_MARK, -} from "./constants.ts"; +} from "./_constants.ts"; import { assertPath, @@ -16,7 +16,7 @@ import { isWindowsDeviceRoot, normalizeString, _format, -} from "./utils.ts"; +} from "./_util.ts"; import { assert } from "../testing/asserts.ts"; export const sep = "\\"; diff --git a/signal/mod.ts b/signal/mod.ts index a463142e6..c15d1b326 100644 --- a/signal/mod.ts +++ b/signal/mod.ts @@ -1,4 +1,4 @@ -import { MuxAsyncIterator } from "../util/async.ts"; +import { MuxAsyncIterator } from "../async/mux_async_iterator.ts"; export type Disposable = { dispose: () => void }; diff --git a/signal/test.ts b/signal/test.ts index c4d1bf3a7..15ebbdcc7 100644 --- a/signal/test.ts +++ b/signal/test.ts @@ -1,6 +1,6 @@ const { test } = Deno; import { assertEquals, assertThrows } from "../testing/asserts.ts"; -import { delay } from "../util/async.ts"; +import { delay } from "../async/delay.ts"; import { signal, onSignal } from "./mod.ts"; test({ diff --git a/util/async.ts b/util/async.ts deleted file mode 100644 index bb50e482d..000000000 --- a/util/async.ts +++ /dev/null @@ -1,117 +0,0 @@ -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. - -// TODO(ry) It'd be better to make Deferred a class that inherits from -// Promise, rather than an interface. This is possible in ES2016, however -// typescript produces broken code when targeting ES5 code. -// See https://github.com/Microsoft/TypeScript/issues/15202 -// At the time of writing, the github issue is closed but the problem remains. -export interface Deferred extends Promise { - resolve: (value?: T | PromiseLike) => void; - // eslint-disable-next-line @typescript-eslint/no-explicit-any - reject: (reason?: any) => void; -} - -/** Creates a Promise with the `reject` and `resolve` functions - * placed as methods on the promise object itself. It allows you to do: - * - * const p = deferred(); - * // ... - * p.resolve(42); - */ -export function deferred(): Deferred { - let methods; - const promise = new Promise((resolve, reject): void => { - methods = { resolve, reject }; - }); - return Object.assign(promise, methods) as Deferred; -} - -interface TaggedYieldedValue { - iterator: AsyncIterableIterator; - value: T; -} - -/** The MuxAsyncIterator class multiplexes multiple async iterators into a - * single stream. It currently makes a few assumptions: - * - The iterators do not throw. - * - The final result (the value returned and not yielded from the iterator) - * does not matter; if there is any, it is discarded. - */ -export class MuxAsyncIterator implements AsyncIterable { - private iteratorCount = 0; - private yields: Array> = []; - private signal: Deferred = deferred(); - - add(iterator: AsyncIterableIterator): void { - ++this.iteratorCount; - this.callIteratorNext(iterator); - } - - private async callIteratorNext( - iterator: AsyncIterableIterator - ): Promise { - const { value, done } = await iterator.next(); - if (done) { - --this.iteratorCount; - } else { - this.yields.push({ iterator, value }); - } - this.signal.resolve(); - } - - async *iterate(): AsyncIterableIterator { - while (this.iteratorCount > 0) { - // Sleep until any of the wrapped iterators yields. - await this.signal; - - // Note that while we're looping over `yields`, new items may be added. - for (let i = 0; i < this.yields.length; i++) { - const { iterator, value } = this.yields[i]; - yield value; - this.callIteratorNext(iterator); - } - - // Clear the `yields` list and reset the `signal` promise. - this.yields.length = 0; - this.signal = deferred(); - } - } - - [Symbol.asyncIterator](): AsyncIterableIterator { - return this.iterate(); - } -} - -/** Collects all Uint8Arrays from an AsyncIterable and retuns a single - * Uint8Array with the concatenated contents of all the collected arrays. - */ -export async function collectUint8Arrays( - it: AsyncIterable -): Promise { - const chunks = []; - let length = 0; - for await (const chunk of it) { - chunks.push(chunk); - length += chunk.length; - } - if (chunks.length === 1) { - // No need to copy. - return chunks[0]; - } - const collected = new Uint8Array(length); - let offset = 0; - for (const chunk of chunks) { - collected.set(chunk, offset); - offset += chunk.length; - } - return collected; -} - -// Delays the given milliseconds and resolves. -export function delay(ms: number): Promise { - return new Promise((res): number => - setTimeout((): void => { - res(); - }, ms) - ); -} diff --git a/util/async_test.ts b/util/async_test.ts deleted file mode 100644 index f3f17312a..000000000 --- a/util/async_test.ts +++ /dev/null @@ -1,76 +0,0 @@ -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -const { test } = Deno; -import { assert, assertEquals, assertStrictEq } from "../testing/asserts.ts"; -import { collectUint8Arrays, deferred, MuxAsyncIterator } from "./async.ts"; - -test("asyncDeferred", function (): Promise { - const d = deferred(); - d.resolve(12); - return Promise.resolve(); -}); - -// eslint-disable-next-line require-await -async function* gen123(): AsyncIterableIterator { - yield 1; - yield 2; - yield 3; -} - -// eslint-disable-next-line require-await -async function* gen456(): AsyncIterableIterator { - yield 4; - yield 5; - yield 6; -} - -test("asyncMuxAsyncIterator", async function (): Promise { - const mux = new MuxAsyncIterator(); - mux.add(gen123()); - mux.add(gen456()); - const results = new Set(); - for await (const value of mux) { - results.add(value); - } - assertEquals(results.size, 6); -}); - -test("collectUint8Arrays0", async function (): Promise { - async function* gen(): AsyncIterableIterator {} - const result = await collectUint8Arrays(gen()); - assert(result instanceof Uint8Array); - assertEquals(result.length, 0); -}); - -test("collectUint8Arrays0", async function (): Promise { - async function* gen(): AsyncIterableIterator {} - const result = await collectUint8Arrays(gen()); - assert(result instanceof Uint8Array); - assertStrictEq(result.length, 0); -}); - -test("collectUint8Arrays1", async function (): Promise { - const buf = new Uint8Array([1, 2, 3]); - // eslint-disable-next-line require-await - async function* gen(): AsyncIterableIterator { - yield buf; - } - const result = await collectUint8Arrays(gen()); - assertStrictEq(result, buf); - assertStrictEq(result.length, 3); -}); - -test("collectUint8Arrays4", async function (): Promise { - // eslint-disable-next-line require-await - async function* gen(): AsyncIterableIterator { - yield new Uint8Array([1, 2, 3]); - yield new Uint8Array([]); - yield new Uint8Array([4, 5]); - yield new Uint8Array([6]); - } - const result = await collectUint8Arrays(gen()); - assert(result instanceof Uint8Array); - assertStrictEq(result.length, 6); - for (let i = 0; i < 6; i++) { - assertStrictEq(result[i], i + 1); - } -}); diff --git a/uuid/v5.ts b/uuid/v5.ts index 1216762dd..3c04873fe 100644 --- a/uuid/v5.ts +++ b/uuid/v5.ts @@ -6,7 +6,7 @@ import { stringToBytes, uuidToBytes, } from "./_common.ts"; -import { Sha1 } from "../util/sha1.ts"; +import { Sha1 } from "../hash/sha1.ts"; import { isString } from "../node/util.ts"; import { assert } from "../testing/asserts.ts"; diff --git a/ws/example_client.ts b/ws/example_client.ts index 4213025f4..93f2f5c7b 100644 --- a/ws/example_client.ts +++ b/ws/example_client.ts @@ -3,57 +3,59 @@ import { isWebSocketCloseEvent, isWebSocketPingEvent, isWebSocketPongEvent, -} from "../ws/mod.ts"; +} from "./mod.ts"; import { encode } from "../encoding/utf8.ts"; import { BufReader } from "../io/bufio.ts"; import { TextProtoReader } from "../textproto/mod.ts"; import { blue, green, red, yellow } from "../fmt/colors.ts"; -const endpoint = Deno.args[0] || "ws://127.0.0.1:8080"; -/** simple websocket cli */ -try { - const sock = await connectWebSocket(endpoint); - console.log(green("ws connected! (type 'close' to quit)")); +if (import.meta.main) { + const endpoint = Deno.args[0] || "ws://127.0.0.1:8080"; + /** simple websocket cli */ + try { + const sock = await connectWebSocket(endpoint); + console.log(green("ws connected! (type 'close' to quit)")); - const messages = async (): Promise => { - for await (const msg of sock) { - if (typeof msg === "string") { - console.log(yellow(`< ${msg}`)); - } else if (isWebSocketPingEvent(msg)) { - console.log(blue("< ping")); - } else if (isWebSocketPongEvent(msg)) { - console.log(blue("< pong")); - } else if (isWebSocketCloseEvent(msg)) { - console.log(red(`closed: code=${msg.code}, reason=${msg.reason}`)); + const messages = async (): Promise => { + for await (const msg of sock) { + if (typeof msg === "string") { + console.log(yellow(`< ${msg}`)); + } else if (isWebSocketPingEvent(msg)) { + console.log(blue("< ping")); + } else if (isWebSocketPongEvent(msg)) { + console.log(blue("< pong")); + } else if (isWebSocketCloseEvent(msg)) { + console.log(red(`closed: code=${msg.code}, reason=${msg.reason}`)); + } } + }; + + const cli = async (): Promise => { + const tpr = new TextProtoReader(new BufReader(Deno.stdin)); + while (true) { + await Deno.stdout.write(encode("> ")); + const line = await tpr.readLine(); + if (line === null) { + break; + } + if (line === "close") { + break; + } else if (line === "ping") { + await sock.ping(); + } else { + await sock.send(line); + } + } + }; + + await Promise.race([messages(), cli()]).catch(console.error); + + if (!sock.isClosed) { + await sock.close(1000).catch(console.error); } - }; - - const cli = async (): Promise => { - const tpr = new TextProtoReader(new BufReader(Deno.stdin)); - while (true) { - await Deno.stdout.write(encode("> ")); - const line = await tpr.readLine(); - if (line === null) { - break; - } - if (line === "close") { - break; - } else if (line === "ping") { - await sock.ping(); - } else { - await sock.send(line); - } - } - }; - - await Promise.race([messages(), cli()]).catch(console.error); - - if (!sock.isClosed) { - await sock.close(1000).catch(console.error); + } catch (err) { + console.error(red(`Could not connect to WebSocket: '${err}'`)); } -} catch (err) { - console.error(red(`Could not connect to WebSocket: '${err}'`)); -} -Deno.exit(0); + Deno.exit(0); +} diff --git a/ws/example_server.ts b/ws/example_server.ts index 947b807ca..3a9815957 100644 --- a/ws/example_server.ts +++ b/ws/example_server.ts @@ -6,50 +6,52 @@ import { isWebSocketPingEvent, } from "./mod.ts"; -/** websocket echo server */ -const port = Deno.args[0] || "8080"; -console.log(`websocket server is running on :${port}`); -for await (const req of serve(`:${port}`)) { - const { conn, r: bufReader, w: bufWriter, headers } = req; - - try { - const sock = await acceptWebSocket({ - conn, - bufReader, - bufWriter, - headers, - }); - - console.log("socket connected!"); +if (import.meta.main) { + /** websocket echo server */ + const port = Deno.args[0] || "8080"; + console.log(`websocket server is running on :${port}`); + for await (const req of serve(`:${port}`)) { + const { conn, r: bufReader, w: bufWriter, headers } = req; try { - for await (const ev of sock) { - if (typeof ev === "string") { - // text message - console.log("ws:Text", ev); - await sock.send(ev); - } else if (ev instanceof Uint8Array) { - // binary message - console.log("ws:Binary", ev); - } else if (isWebSocketPingEvent(ev)) { - const [, body] = ev; - // ping - console.log("ws:Ping", body); - } else if (isWebSocketCloseEvent(ev)) { - // close - const { code, reason } = ev; - console.log("ws:Close", code, reason); + const sock = await acceptWebSocket({ + conn, + bufReader, + bufWriter, + headers, + }); + + console.log("socket connected!"); + + try { + for await (const ev of sock) { + if (typeof ev === "string") { + // text message + console.log("ws:Text", ev); + await sock.send(ev); + } else if (ev instanceof Uint8Array) { + // binary message + console.log("ws:Binary", ev); + } else if (isWebSocketPingEvent(ev)) { + const [, body] = ev; + // ping + console.log("ws:Ping", body); + } else if (isWebSocketCloseEvent(ev)) { + // close + const { code, reason } = ev; + console.log("ws:Close", code, reason); + } + } + } catch (err) { + console.error(`failed to receive frame: ${err}`); + + if (!sock.isClosed) { + await sock.close(1000).catch(console.error); } } } catch (err) { - console.error(`failed to receive frame: ${err}`); - - if (!sock.isClosed) { - await sock.close(1000).catch(console.error); - } + console.error(`failed to accept websocket: ${err}`); + await req.respond({ status: 400 }); } - } catch (err) { - console.error(`failed to accept websocket: ${err}`); - await req.respond({ status: 400 }); } } diff --git a/ws/mod.ts b/ws/mod.ts index 47353d012..324588af0 100644 --- a/ws/mod.ts +++ b/ws/mod.ts @@ -1,13 +1,13 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. import { decode, encode } from "../encoding/utf8.ts"; -import { hasOwnProperty } from "../util/has_own_property.ts"; +import { hasOwnProperty } from "../_util/has_own_property.ts"; import { BufReader, BufWriter } from "../io/bufio.ts"; import { readLong, readShort, sliceLongToBytes } from "../io/ioutil.ts"; -import { Sha1 } from "../util/sha1.ts"; -import { writeResponse } from "../http/io.ts"; +import { Sha1 } from "../hash/sha1.ts"; +import { writeResponse } from "../http/_io.ts"; import { TextProtoReader } from "../textproto/mod.ts"; -import { Deferred, deferred } from "../util/async.ts"; +import { Deferred, deferred } from "../async/deferred.ts"; import { assert } from "../testing/asserts.ts"; import { concat } from "../bytes/mod.ts"; import Conn = Deno.Conn; diff --git a/ws/test.ts b/ws/test.ts index 8e97e6ec2..a1c396b18 100644 --- a/ws/test.ts +++ b/ws/test.ts @@ -21,7 +21,7 @@ import Writer = Deno.Writer; import Reader = Deno.Reader; import Conn = Deno.Conn; import Buffer = Deno.Buffer; -import { delay } from "../util/async.ts"; +import { delay } from "../async/delay.ts"; test("[ws] read unmasked text frame", async () => { // unmasked single text frame with payload "Hello"