From f184332c09c851faac50f598d29ebe4426e05464 Mon Sep 17 00:00:00 2001 From: Nayeem Rahman Date: Sat, 9 May 2020 13:34:47 +0100 Subject: BREAKING(std): reorganization (#5087) * Prepend underscores to private modules * Remove collectUint8Arrays() It would be a misuse of Deno.iter()'s result. * Move std/_util/async.ts to std/async * Move std/util/sha*.ts to std/hash --- std/README.md | 35 +- std/_util/deep_assign.ts | 34 + std/_util/deep_assign_test.ts | 24 + std/_util/has_own_property.ts | 30 + std/async/deferred.ts | 26 + std/async/deferred_test.ts | 8 + std/async/delay.ts | 9 + std/async/mod.ts | 4 + std/async/mux_async_iterator.ts | 58 + std/async/mux_async_iterator_test.ts | 28 + std/encoding/README.md | 4 +- std/encoding/_yaml/dumper/dumper.ts | 899 ++++++++++++ std/encoding/_yaml/dumper/dumper_state.ts | 141 ++ std/encoding/_yaml/error.ts | 20 + std/encoding/_yaml/example/dump.ts | 22 + std/encoding/_yaml/example/inout.ts | 27 + std/encoding/_yaml/example/parse.ts | 19 + std/encoding/_yaml/example/sample_document.ts | 23 + std/encoding/_yaml/example/sample_document.yml | 197 +++ std/encoding/_yaml/loader/loader.ts | 1797 ++++++++++++++++++++++++ std/encoding/_yaml/loader/loader_state.ts | 74 + std/encoding/_yaml/mark.ts | 77 + std/encoding/_yaml/parse.ts | 32 + std/encoding/_yaml/parse_test.ts | 56 + std/encoding/_yaml/schema.ts | 101 ++ std/encoding/_yaml/schema/core.ts | 13 + std/encoding/_yaml/schema/default.ts | 16 + std/encoding/_yaml/schema/failsafe.ts | 13 + std/encoding/_yaml/schema/json.ts | 15 + std/encoding/_yaml/schema/mod.ts | 9 + std/encoding/_yaml/state.ts | 11 + std/encoding/_yaml/stringify.ts | 18 + std/encoding/_yaml/stringify_test.ts | 41 + std/encoding/_yaml/type.ts | 55 + std/encoding/_yaml/type/binary.ts | 139 ++ std/encoding/_yaml/type/bool.ts | 39 + std/encoding/_yaml/type/float.ts | 125 ++ std/encoding/_yaml/type/int.ts | 188 +++ std/encoding/_yaml/type/map.ts | 14 + std/encoding/_yaml/type/merge.ts | 15 + std/encoding/_yaml/type/mod.ts | 18 + std/encoding/_yaml/type/nil.ts | 45 + std/encoding/_yaml/type/omap.ts | 46 + std/encoding/_yaml/type/pairs.ts | 49 + std/encoding/_yaml/type/seq.ts | 14 + std/encoding/_yaml/type/set.ts | 31 + std/encoding/_yaml/type/str.ts | 12 + std/encoding/_yaml/type/timestamp.ts | 96 ++ std/encoding/_yaml/utils.ts | 80 ++ std/encoding/toml.ts | 2 +- std/encoding/yaml.ts | 6 +- std/encoding/yaml/dumper/dumper.ts | 899 ------------ std/encoding/yaml/dumper/dumper_state.ts | 141 -- std/encoding/yaml/error.ts | 20 - std/encoding/yaml/example/dump.ts | 22 - std/encoding/yaml/example/inout.ts | 27 - std/encoding/yaml/example/parse.ts | 19 - std/encoding/yaml/example/sample_document.ts | 23 - std/encoding/yaml/example/sample_document.yml | 197 --- std/encoding/yaml/loader/loader.ts | 1797 ------------------------ std/encoding/yaml/loader/loader_state.ts | 74 - std/encoding/yaml/mark.ts | 77 - std/encoding/yaml/parse.ts | 32 - std/encoding/yaml/parse_test.ts | 56 - std/encoding/yaml/schema.ts | 101 -- std/encoding/yaml/schema/core.ts | 13 - std/encoding/yaml/schema/default.ts | 16 - std/encoding/yaml/schema/failsafe.ts | 13 - std/encoding/yaml/schema/json.ts | 15 - std/encoding/yaml/schema/mod.ts | 9 - std/encoding/yaml/state.ts | 11 - std/encoding/yaml/stringify.ts | 18 - std/encoding/yaml/stringify_test.ts | 41 - std/encoding/yaml/type.ts | 55 - std/encoding/yaml/type/binary.ts | 139 -- std/encoding/yaml/type/bool.ts | 39 - std/encoding/yaml/type/float.ts | 125 -- std/encoding/yaml/type/int.ts | 188 --- std/encoding/yaml/type/map.ts | 14 - std/encoding/yaml/type/merge.ts | 15 - std/encoding/yaml/type/mod.ts | 18 - std/encoding/yaml/type/nil.ts | 45 - std/encoding/yaml/type/omap.ts | 46 - std/encoding/yaml/type/pairs.ts | 49 - std/encoding/yaml/type/seq.ts | 14 - std/encoding/yaml/type/set.ts | 31 - std/encoding/yaml/type/str.ts | 12 - std/encoding/yaml/type/timestamp.ts | 96 -- std/encoding/yaml/utils.ts | 80 -- std/encoding/yaml_test.ts | 4 +- std/examples/chat/server_test.ts | 2 +- std/examples/flags.ts | 7 + std/flags/README.md | 13 +- std/flags/example.ts | 5 - std/flags/mod.ts | 9 +- std/fs/_util.ts | 45 + std/fs/_util_test.ts | 63 + std/fs/copy.ts | 2 +- std/fs/ensure_dir.ts | 2 +- std/fs/ensure_file.ts | 2 +- std/fs/ensure_link.ts | 2 +- std/fs/ensure_symlink.ts | 2 +- std/fs/expand_glob.ts | 3 +- std/fs/move.ts | 2 +- std/fs/utils.ts | 45 - std/fs/utils_test.ts | 63 - std/hash/sha1.ts | 374 +++++ std/hash/sha1_test.ts | 24 + std/hash/sha256.ts | 575 ++++++++ std/hash/sha256_test.ts | 296 ++++ std/http/_io.ts | 366 +++++ std/http/_io_test.ts | 476 +++++++ std/http/_mock_conn.ts | 25 + std/http/io.ts | 366 ----- std/http/io_test.ts | 476 ------- std/http/mock.ts | 25 - std/http/racing_server.ts | 2 +- std/http/server.ts | 4 +- std/http/server_test.ts | 4 +- std/io/_iotest.ts | 53 + std/io/bufio_test.ts | 2 +- std/io/iotest.ts | 53 - std/mime/multipart.ts | 2 +- std/node/module.ts | 2 +- std/path/_constants.ts | 54 + std/path/_globrex.ts | 327 +++++ std/path/_globrex_test.ts | 827 +++++++++++ std/path/_util.ts | 116 ++ std/path/common.ts | 2 +- std/path/constants.ts | 54 - std/path/glob.ts | 4 +- std/path/globrex.ts | 327 ----- std/path/globrex_test.ts | 827 ----------- std/path/mod.ts | 7 +- std/path/posix.ts | 4 +- std/path/separator.ts | 4 + std/path/utils.ts | 116 -- std/path/win32.ts | 4 +- std/signal/mod.ts | 2 +- std/signal/test.ts | 2 +- std/util/async.ts | 117 -- std/util/async_test.ts | 76 - std/util/deep_assign.ts | 34 - std/util/deep_assign_test.ts | 24 - std/util/has_own_property.ts | 30 - std/util/sha1.ts | 374 ----- std/util/sha1_test.ts | 24 - std/util/sha256.ts | 575 -------- std/util/sha256_test.ts | 296 ---- std/uuid/v5.ts | 2 +- std/ws/example_client.ts | 84 +- std/ws/example_server.ts | 78 +- std/ws/mod.ts | 8 +- std/ws/test.ts | 2 +- 154 files changed, 8590 insertions(+), 8649 deletions(-) create mode 100644 std/_util/deep_assign.ts create mode 100644 std/_util/deep_assign_test.ts create mode 100644 std/_util/has_own_property.ts create mode 100644 std/async/deferred.ts create mode 100644 std/async/deferred_test.ts create mode 100644 std/async/delay.ts create mode 100644 std/async/mod.ts create mode 100644 std/async/mux_async_iterator.ts create mode 100644 std/async/mux_async_iterator_test.ts create mode 100644 std/encoding/_yaml/dumper/dumper.ts create mode 100644 std/encoding/_yaml/dumper/dumper_state.ts create mode 100644 std/encoding/_yaml/error.ts create mode 100644 std/encoding/_yaml/example/dump.ts create mode 100644 std/encoding/_yaml/example/inout.ts create mode 100644 std/encoding/_yaml/example/parse.ts create mode 100644 std/encoding/_yaml/example/sample_document.ts create mode 100644 std/encoding/_yaml/example/sample_document.yml create mode 100644 std/encoding/_yaml/loader/loader.ts create mode 100644 std/encoding/_yaml/loader/loader_state.ts create mode 100644 std/encoding/_yaml/mark.ts create mode 100644 std/encoding/_yaml/parse.ts create mode 100644 std/encoding/_yaml/parse_test.ts create mode 100644 std/encoding/_yaml/schema.ts create mode 100644 std/encoding/_yaml/schema/core.ts create mode 100644 std/encoding/_yaml/schema/default.ts create mode 100644 std/encoding/_yaml/schema/failsafe.ts create mode 100644 std/encoding/_yaml/schema/json.ts create mode 100644 std/encoding/_yaml/schema/mod.ts create mode 100644 std/encoding/_yaml/state.ts create mode 100644 std/encoding/_yaml/stringify.ts create mode 100644 std/encoding/_yaml/stringify_test.ts create mode 100644 std/encoding/_yaml/type.ts create mode 100644 std/encoding/_yaml/type/binary.ts create mode 100644 std/encoding/_yaml/type/bool.ts create mode 100644 std/encoding/_yaml/type/float.ts create mode 100644 std/encoding/_yaml/type/int.ts create mode 100644 std/encoding/_yaml/type/map.ts create mode 100644 std/encoding/_yaml/type/merge.ts create mode 100644 std/encoding/_yaml/type/mod.ts create mode 100644 std/encoding/_yaml/type/nil.ts create mode 100644 std/encoding/_yaml/type/omap.ts create mode 100644 std/encoding/_yaml/type/pairs.ts create mode 100644 std/encoding/_yaml/type/seq.ts create mode 100644 std/encoding/_yaml/type/set.ts create mode 100644 std/encoding/_yaml/type/str.ts create mode 100644 std/encoding/_yaml/type/timestamp.ts create mode 100644 std/encoding/_yaml/utils.ts delete mode 100644 std/encoding/yaml/dumper/dumper.ts delete mode 100644 std/encoding/yaml/dumper/dumper_state.ts delete mode 100644 std/encoding/yaml/error.ts delete mode 100644 std/encoding/yaml/example/dump.ts delete mode 100644 std/encoding/yaml/example/inout.ts delete mode 100644 std/encoding/yaml/example/parse.ts delete mode 100644 std/encoding/yaml/example/sample_document.ts delete mode 100644 std/encoding/yaml/example/sample_document.yml delete mode 100644 std/encoding/yaml/loader/loader.ts delete mode 100644 std/encoding/yaml/loader/loader_state.ts delete mode 100644 std/encoding/yaml/mark.ts delete mode 100644 std/encoding/yaml/parse.ts delete mode 100644 std/encoding/yaml/parse_test.ts delete mode 100644 std/encoding/yaml/schema.ts delete mode 100644 std/encoding/yaml/schema/core.ts delete mode 100644 std/encoding/yaml/schema/default.ts delete mode 100644 std/encoding/yaml/schema/failsafe.ts delete mode 100644 std/encoding/yaml/schema/json.ts delete mode 100644 std/encoding/yaml/schema/mod.ts delete mode 100644 std/encoding/yaml/state.ts delete mode 100644 std/encoding/yaml/stringify.ts delete mode 100644 std/encoding/yaml/stringify_test.ts delete mode 100644 std/encoding/yaml/type.ts delete mode 100644 std/encoding/yaml/type/binary.ts delete mode 100644 std/encoding/yaml/type/bool.ts delete mode 100644 std/encoding/yaml/type/float.ts delete mode 100644 std/encoding/yaml/type/int.ts delete mode 100644 std/encoding/yaml/type/map.ts delete mode 100644 std/encoding/yaml/type/merge.ts delete mode 100644 std/encoding/yaml/type/mod.ts delete mode 100644 std/encoding/yaml/type/nil.ts delete mode 100644 std/encoding/yaml/type/omap.ts delete mode 100644 std/encoding/yaml/type/pairs.ts delete mode 100644 std/encoding/yaml/type/seq.ts delete mode 100644 std/encoding/yaml/type/set.ts delete mode 100644 std/encoding/yaml/type/str.ts delete mode 100644 std/encoding/yaml/type/timestamp.ts delete mode 100644 std/encoding/yaml/utils.ts create mode 100644 std/examples/flags.ts delete mode 100644 std/flags/example.ts create mode 100644 std/fs/_util.ts create mode 100644 std/fs/_util_test.ts delete mode 100644 std/fs/utils.ts delete mode 100644 std/fs/utils_test.ts create mode 100644 std/hash/sha1.ts create mode 100644 std/hash/sha1_test.ts create mode 100644 std/hash/sha256.ts create mode 100644 std/hash/sha256_test.ts create mode 100644 std/http/_io.ts create mode 100644 std/http/_io_test.ts create mode 100644 std/http/_mock_conn.ts delete mode 100644 std/http/io.ts delete mode 100644 std/http/io_test.ts delete mode 100644 std/http/mock.ts create mode 100644 std/io/_iotest.ts delete mode 100644 std/io/iotest.ts create mode 100644 std/path/_constants.ts create mode 100644 std/path/_globrex.ts create mode 100644 std/path/_globrex_test.ts create mode 100644 std/path/_util.ts delete mode 100644 std/path/constants.ts delete mode 100644 std/path/globrex.ts delete mode 100644 std/path/globrex_test.ts create mode 100644 std/path/separator.ts delete mode 100644 std/path/utils.ts delete mode 100644 std/util/async.ts delete mode 100644 std/util/async_test.ts delete mode 100644 std/util/deep_assign.ts delete mode 100644 std/util/deep_assign_test.ts delete mode 100644 std/util/has_own_property.ts delete mode 100644 std/util/sha1.ts delete mode 100644 std/util/sha1_test.ts delete mode 100644 std/util/sha256.ts delete mode 100644 std/util/sha256_test.ts (limited to 'std') diff --git a/std/README.md b/std/README.md index 76c4b4545..641146902 100644 --- a/std/README.md +++ b/std/README.md @@ -10,28 +10,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: - -- [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) +To browse documentation for modules: + +- Go to https://deno.land/std/. +- Navigate to any module of interest. +- Click the "DOCUMENTATION" link. ## Contributing diff --git a/std/_util/deep_assign.ts b/std/_util/deep_assign.ts new file mode 100644 index 000000000..9034d89bd --- /dev/null +++ b/std/_util/deep_assign.ts @@ -0,0 +1,34 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +import { assert } from "../testing/asserts.ts"; + +export function deepAssign( + target: Record, + ...sources: object[] +): object | undefined { + for (let i = 0; i < sources.length; i++) { + const source = sources[i]; + if (!source || typeof source !== `object`) { + return; + } + Object.entries(source).forEach(([key, value]: [string, unknown]): void => { + if (value instanceof Date) { + target[key] = new Date(value); + return; + } + if (!value || typeof value !== `object`) { + target[key] = value; + return; + } + if (Array.isArray(value)) { + target[key] = []; + } + // value is an Object + if (typeof target[key] !== `object` || !target[key]) { + target[key] = {}; + } + assert(value); + deepAssign(target[key] as Record, value); + }); + } + return target; +} diff --git a/std/_util/deep_assign_test.ts b/std/_util/deep_assign_test.ts new file mode 100644 index 000000000..f1a56e1ad --- /dev/null +++ b/std/_util/deep_assign_test.ts @@ -0,0 +1,24 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +const { test } = Deno; +import { assertEquals, assert } from "../testing/asserts.ts"; +import { deepAssign } from "./deep_assign.ts"; + +test("deepAssignTest", function (): void { + const date = new Date("1979-05-27T07:32:00Z"); + const reg = RegExp(/DENOWOWO/); + const obj1 = { deno: { bar: { deno: ["is", "not", "node"] } } }; + const obj2 = { foo: { deno: date } }; + const obj3 = { foo: { bar: "deno" }, reg: reg }; + const actual = deepAssign(obj1, obj2, obj3); + const expected = { + foo: { + deno: new Date("1979-05-27T07:32:00Z"), + bar: "deno", + }, + deno: { bar: { deno: ["is", "not", "node"] } }, + reg: RegExp(/DENOWOWO/), + }; + assert(date !== expected.foo.deno); + assert(reg !== expected.reg); + assertEquals(actual, expected); +}); diff --git a/std/_util/has_own_property.ts b/std/_util/has_own_property.ts new file mode 100644 index 000000000..351905cec --- /dev/null +++ b/std/_util/has_own_property.ts @@ -0,0 +1,30 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +/** + * Determines whether an object has a property with the specified name. + * Avoid calling prototype builtin `hasOwnProperty` for two reasons: + * + * 1. `hasOwnProperty` is defined on the object as something else: + * + * const options = { + * ending: 'utf8', + * hasOwnProperty: 'foo' + * }; + * options.hasOwnProperty('ending') // throws a TypeError + * + * 2. The object doesn't inherit from `Object.prototype`: + * + * const options = Object.create(null); + * options.ending = 'utf8'; + * options.hasOwnProperty('ending'); // throws a TypeError + * + * @param obj A Object. + * @param v A property name. + * @see https://eslint.org/docs/rules/no-prototype-builtins + */ +export function hasOwnProperty(obj: T, v: PropertyKey): boolean { + if (obj == null) { + return false; + } + return Object.prototype.hasOwnProperty.call(obj, v); +} diff --git a/std/async/deferred.ts b/std/async/deferred.ts new file mode 100644 index 000000000..109a1a37e --- /dev/null +++ b/std/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/std/async/deferred_test.ts b/std/async/deferred_test.ts new file mode 100644 index 000000000..83c317853 --- /dev/null +++ b/std/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/std/async/delay.ts b/std/async/delay.ts new file mode 100644 index 000000000..e3aec368f --- /dev/null +++ b/std/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/std/async/mod.ts b/std/async/mod.ts new file mode 100644 index 000000000..9efead91d --- /dev/null +++ b/std/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/std/async/mux_async_iterator.ts b/std/async/mux_async_iterator.ts new file mode 100644 index 000000000..b32689a29 --- /dev/null +++ b/std/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/std/async/mux_async_iterator_test.ts b/std/async/mux_async_iterator_test.ts new file mode 100644 index 000000000..7017a4eba --- /dev/null +++ b/std/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/std/encoding/README.md b/std/encoding/README.md index 7d9c89117..973713360 100644 --- a/std/encoding/README.md +++ b/std/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/std/encoding/_yaml/dumper/dumper.ts b/std/encoding/_yaml/dumper/dumper.ts new file mode 100644 index 000000000..1280ee757 --- /dev/null +++ b/std/encoding/_yaml/dumper/dumper.ts @@ -0,0 +1,899 @@ +// Ported from js-yaml v3.13.1: +// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da +// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +/* eslint-disable max-len */ + +import { YAMLError } from "../error.ts"; +import { RepresentFn, StyleVariant, Type } from "../type.ts"; +import * as common from "../utils.ts"; +import { DumperState, DumperStateOptions } from "./dumper_state.ts"; + +type Any = common.Any; +type ArrayObject = common.ArrayObject; + +const _toString = Object.prototype.toString; +const _hasOwnProperty = Object.prototype.hasOwnProperty; + +const CHAR_TAB = 0x09; /* Tab */ +const CHAR_LINE_FEED = 0x0a; /* LF */ +const CHAR_SPACE = 0x20; /* Space */ +const CHAR_EXCLAMATION = 0x21; /* ! */ +const CHAR_DOUBLE_QUOTE = 0x22; /* " */ +const CHAR_SHARP = 0x23; /* # */ +const CHAR_PERCENT = 0x25; /* % */ +const CHAR_AMPERSAND = 0x26; /* & */ +const CHAR_SINGLE_QUOTE = 0x27; /* ' */ +const CHAR_ASTERISK = 0x2a; /* * */ +const CHAR_COMMA = 0x2c; /* , */ +const CHAR_MINUS = 0x2d; /* - */ +const CHAR_COLON = 0x3a; /* : */ +const CHAR_GREATER_THAN = 0x3e; /* > */ +const CHAR_QUESTION = 0x3f; /* ? */ +const CHAR_COMMERCIAL_AT = 0x40; /* @ */ +const CHAR_LEFT_SQUARE_BRACKET = 0x5b; /* [ */ +const CHAR_RIGHT_SQUARE_BRACKET = 0x5d; /* ] */ +const CHAR_GRAVE_ACCENT = 0x60; /* ` */ +const CHAR_LEFT_CURLY_BRACKET = 0x7b; /* { */ +const CHAR_VERTICAL_LINE = 0x7c; /* | */ +const CHAR_RIGHT_CURLY_BRACKET = 0x7d; /* } */ + +const ESCAPE_SEQUENCES: { [char: number]: string } = {}; + +ESCAPE_SEQUENCES[0x00] = "\\0"; +ESCAPE_SEQUENCES[0x07] = "\\a"; +ESCAPE_SEQUENCES[0x08] = "\\b"; +ESCAPE_SEQUENCES[0x09] = "\\t"; +ESCAPE_SEQUENCES[0x0a] = "\\n"; +ESCAPE_SEQUENCES[0x0b] = "\\v"; +ESCAPE_SEQUENCES[0x0c] = "\\f"; +ESCAPE_SEQUENCES[0x0d] = "\\r"; +ESCAPE_SEQUENCES[0x1b] = "\\e"; +ESCAPE_SEQUENCES[0x22] = '\\"'; +ESCAPE_SEQUENCES[0x5c] = "\\\\"; +ESCAPE_SEQUENCES[0x85] = "\\N"; +ESCAPE_SEQUENCES[0xa0] = "\\_"; +ESCAPE_SEQUENCES[0x2028] = "\\L"; +ESCAPE_SEQUENCES[0x2029] = "\\P"; + +const DEPRECATED_BOOLEANS_SYNTAX = [ + "y", + "Y", + "yes", + "Yes", + "YES", + "on", + "On", + "ON", + "n", + "N", + "no", + "No", + "NO", + "off", + "Off", + "OFF", +]; + +function encodeHex(character: number): string { + const string = character.toString(16).toUpperCase(); + + let handle: string; + let length: number; + if (character <= 0xff) { + handle = "x"; + length = 2; + } else if (character <= 0xffff) { + handle = "u"; + length = 4; + } else if (character <= 0xffffffff) { + handle = "U"; + length = 8; + } else { + throw new YAMLError( + "code point within a string may not be greater than 0xFFFFFFFF" + ); + } + + return `\\${handle}${common.repeat("0", length - string.length)}${string}`; +} + +// Indents every line in a string. Empty lines (\n only) are not indented. +function indentString(string: string, spaces: number): string { + const ind = common.repeat(" ", spaces), + length = string.length; + let position = 0, + next = -1, + result = "", + line: string; + + while (position < length) { + next = string.indexOf("\n", position); + if (next === -1) { + line = string.slice(position); + position = length; + } else { + line = string.slice(position, next + 1); + position = next + 1; + } + + if (line.length && line !== "\n") result += ind; + + result += line; + } + + return result; +} + +function generateNextLine(state: DumperState, level: number): string { + return `\n${common.repeat(" ", state.indent * level)}`; +} + +function testImplicitResolving(state: DumperState, str: string): boolean { + let type: Type; + for ( + let index = 0, length = state.implicitTypes.length; + index < length; + index += 1 + ) { + type = state.implicitTypes[index]; + + if (type.resolve(str)) { + return true; + } + } + + return false; +} + +// [33] s-white ::= s-space | s-tab +function isWhitespace(c: number): boolean { + return c === CHAR_SPACE || c === CHAR_TAB; +} + +// Returns true if the character can be printed without escaping. +// From YAML 1.2: "any allowed characters known to be non-printable +// should also be escaped. [However,] This isn’t mandatory" +// Derived from nb-char - \t - #x85 - #xA0 - #x2028 - #x2029. +function isPrintable(c: number): boolean { + return ( + (0x00020 <= c && c <= 0x00007e) || + (0x000a1 <= c && c <= 0x00d7ff && c !== 0x2028 && c !== 0x2029) || + (0x0e000 <= c && c <= 0x00fffd && c !== 0xfeff) /* BOM */ || + (0x10000 <= c && c <= 0x10ffff) + ); +} + +// Simplified test for values allowed after the first character in plain style. +function isPlainSafe(c: number): boolean { + // Uses a subset of nb-char - c-flow-indicator - ":" - "#" + // where nb-char ::= c-printable - b-char - c-byte-order-mark. + return ( + isPrintable(c) && + c !== 0xfeff && + // - c-flow-indicator + c !== CHAR_COMMA && + c !== CHAR_LEFT_SQUARE_BRACKET && + c !== CHAR_RIGHT_SQUARE_BRACKET && + c !== CHAR_LEFT_CURLY_BRACKET && + c !== CHAR_RIGHT_CURLY_BRACKET && + // - ":" - "#" + c !== CHAR_COLON && + c !== CHAR_SHARP + ); +} + +// Simplified test for values allowed as the first character in plain style. +function isPlainSafeFirst(c: number): boolean { + // Uses a subset of ns-char - c-indicator + // where ns-char = nb-char - s-white. + return ( + isPrintable(c) && + c !== 0xfeff && + !isWhitespace(c) && // - s-white + // - (c-indicator ::= + // “-” | “?” | “:” | “,” | “[” | “]” | “{” | “}” + c !== CHAR_MINUS && + c !== CHAR_QUESTION && + c !== CHAR_COLON && + c !== CHAR_COMMA && + c !== CHAR_LEFT_SQUARE_BRACKET && + c !== CHAR_RIGHT_SQUARE_BRACKET && + c !== CHAR_LEFT_CURLY_BRACKET && + c !== CHAR_RIGHT_CURLY_BRACKET && + // | “#” | “&” | “*” | “!” | “|” | “>” | “'” | “"” + c !== CHAR_SHARP && + c !== CHAR_AMPERSAND && + c !== CHAR_ASTERISK && + c !== CHAR_EXCLAMATION && + c !== CHAR_VERTICAL_LINE && + c !== CHAR_GREATER_THAN && + c !== CHAR_SINGLE_QUOTE && + c !== CHAR_DOUBLE_QUOTE && + // | “%” | “@” | “`”) + c !== CHAR_PERCENT && + c !== CHAR_COMMERCIAL_AT && + c !== CHAR_GRAVE_ACCENT + ); +} + +// Determines whether block indentation indicator is required. +function needIndentIndicator(string: string): boolean { + const leadingSpaceRe = /^\n* /; + return leadingSpaceRe.test(string); +} + +const STYLE_PLAIN = 1, + STYLE_SINGLE = 2, + STYLE_LITERAL = 3, + STYLE_FOLDED = 4, + STYLE_DOUBLE = 5; + +// Determines which scalar styles are possible and returns the preferred style. +// lineWidth = -1 => no limit. +// Pre-conditions: str.length > 0. +// Post-conditions: +// STYLE_PLAIN or STYLE_SINGLE => no \n are in the string. +// STYLE_LITERAL => no lines are suitable for folding (or lineWidth is -1). +// STYLE_FOLDED => a line > lineWidth and can be folded (and lineWidth != -1). +function chooseScalarStyle( + string: string, + singleLineOnly: boolean, + indentPerLevel: number, + lineWidth: number, + testAmbiguousType: (...args: Any[]) => Any +): number { + const shouldTrackWidth = lineWidth !== -1; + let hasLineBreak = false, + hasFoldableLine = false, // only checked if shouldTrackWidth + previousLineBreak = -1, // count the first line correctly + plain = + isPlainSafeFirst(string.charCodeAt(0)) && + !isWhitespace(string.charCodeAt(string.length - 1)); + + let char: number, i: number; + if (singleLineOnly) { + // Case: no block styles. + // Check for disallowed characters to rule out plain and single. + for (i = 0; i < string.length; i++) { + char = string.charCodeAt(i); + if (!isPrintable(char)) { + return STYLE_DOUBLE; + } + plain = plain && isPlainSafe(char); + } + } else { + // Case: block styles permitted. + for (i = 0; i < string.length; i++) { + char = string.charCodeAt(i); + if (char === CHAR_LINE_FEED) { + hasLineBreak = true; + // Check if any line can be folded. + if (shouldTrackWidth) { + hasFoldableLine = + hasFoldableLine || + // Foldable line = too long, and not more-indented. + (i - previousLineBreak - 1 > lineWidth && + string[previousLineBreak + 1] !== " "); + previousLineBreak = i; + } + } else if (!isPrintable(char)) { + return STYLE_DOUBLE; + } + plain = plain && isPlainSafe(char); + } + // in case the end is missing a \n + hasFoldableLine = + hasFoldableLine || + (shouldTrackWidth && + i - previousLineBreak - 1 > lineWidth && + string[previousLineBreak + 1] !== " "); + } + // Although every style can represent \n without escaping, prefer block styles + // for multiline, since they're more readable and they don't add empty lines. + // Also prefer folding a super-long line. + if (!hasLineBreak && !hasFoldableLine) { + // Strings interpretable as another type have to be quoted; + // e.g. the string 'true' vs. the boolean true. + return plain && !testAmbiguousType(string) ? STYLE_PLAIN : STYLE_SINGLE; + } + // Edge case: block indentation indicator can only have one digit. + if (indentPerLevel > 9 && needIndentIndicator(string)) { + return STYLE_DOUBLE; + } + // At this point we know block styles are valid. + // Prefer literal style unless we want to fold. + return hasFoldableLine ? STYLE_FOLDED : STYLE_LITERAL; +} + +// Greedy line breaking. +// Picks the longest line under the limit each time, +// otherwise settles for the shortest line over the limit. +// NB. More-indented lines *cannot* be folded, as that would add an extra \n. +function foldLine(line: string, width: number): string { + if (line === "" || line[0] === " ") return line; + + // Since a more-indented line adds a \n, breaks can't be followed by a space. + const breakRe = / [^ ]/g; // note: the match index will always be <= length-2. + let match; + // start is an inclusive index. end, curr, and next are exclusive. + let start = 0, + end, + curr = 0, + next = 0; + let result = ""; + + // Invariants: 0 <= start <= length-1. + // 0 <= curr <= next <= max(0, length-2). curr - start <= width. + // Inside the loop: + // A match implies length >= 2, so curr and next are <= length-2. + // tslint:disable-next-line:no-conditional-assignment + while ((match = breakRe.exec(line))) { + next = match.index; + // maintain invariant: curr - start <= width + if (next - start > width) { + end = curr > start ? curr : next; // derive end <= length-2 + result += `\n${line.slice(start, end)}`; + // skip the space that was output as \n + start = end + 1; // derive start <= length-1 + } + curr = next; + } + + // By the invariants, start <= length-1, so there is something left over. + // It is either the whole string or a part starting from non-whitespace. + result += "\n"; + // Insert a break if the remainder is too long and there is a break available. + if (line.length - start > width && curr > start) { + result += `${line.slice(start, curr)}\n${line.slice(curr + 1)}`; + } else { + result += line.slice(start); + } + + return result.slice(1); // drop extra \n joiner +} + +// (See the note for writeScalar.) +function dropEndingNewline(string: string): string { + return string[string.length - 1] === "\n" ? string.slice(0, -1) : string; +} + +// Note: a long line without a suitable break point will exceed the width limit. +// Pre-conditions: every char in str isPrintable, str.length > 0, width > 0. +function foldString(string: string, width: number): string { + // In folded style, $k$ consecutive newlines output as $k+1$ newlines— + // unless they're before or after a more-indented line, or at the very + // beginning or end, in which case $k$ maps to $k$. + // Therefore, parse each chunk as newline(s) followed by a content line. + const lineRe = /(\n+)([^\n]*)/g; + + // first line (possibly an empty line) + let result = ((): string => { + let nextLF = string.indexOf("\n"); + nextLF = nextLF !== -1 ? nextLF : string.length; + lineRe.lastIndex = nextLF; + // eslint-disable-next-line @typescript-eslint/no-use-before-define + return foldLine(string.slice(0, nextLF), width); + })(); + // If we haven't reached the first content line yet, don't add an extra \n. + let prevMoreIndented = string[0] === "\n" || string[0] === " "; + let moreIndented; + + // rest of the lines + let match; + // tslint:disable-next-line:no-conditional-assignment + while ((match = lineRe.exec(string))) { + const prefix = match[1], + line = match[2]; + moreIndented = line[0] === " "; + result += + prefix + + (!prevMoreIndented && !moreIndented && line !== "" ? "\n" : "") + + // eslint-disable-next-line @typescript-eslint/no-use-before-define + foldLine(line, width); + prevMoreIndented = moreIndented; + } + + return result; +} + +// Escapes a double-quoted string. +function escapeString(string: string): string { + let result = ""; + let char, nextChar; + let escapeSeq; + + for (let i = 0; i < string.length; i++) { + char = string.charCodeAt(i); + // Check for surrogate pairs (reference Unicode 3.0 section "3.7 Surrogates"). + if (char >= 0xd800 && char <= 0xdbff /* high surrogate */) { + nextChar = string.charCodeAt(i + 1); + if (nextChar >= 0xdc00 && nextChar <= 0xdfff /* low surrogate */) { + // Combine the surrogate pair and store it escaped. + result += encodeHex( + (char - 0xd800) * 0x400 + nextChar - 0xdc00 + 0x10000 + ); + // Advance index one extra since we already used that char here. + i++; + continue; + } + } + escapeSeq = ESCAPE_SEQUENCES[char]; + result += + !escapeSeq && isPrintable(char) + ? string[i] + : escapeSeq || encodeHex(char); + } + + return result; +} + +// Pre-conditions: string is valid for a block scalar, 1 <= indentPerLevel <= 9. +function blockHeader(string: string, indentPerLevel: number): string { + const indentIndicator = needIndentIndicator(string) + ? String(indentPerLevel) + : ""; + + // note the special case: the string '\n' counts as a "trailing" empty line. + const clip = string[string.length - 1] === "\n"; + const keep = clip && (string[string.length - 2] === "\n" || string === "\n"); + const chomp = keep ? "+" : clip ? "" : "-"; + + return `${indentIndicator}${chomp}\n`; +} + +// Note: line breaking/folding is implemented for only the folded style. +// NB. We drop the last trailing newline (if any) of a returned block scalar +// since the dumper adds its own newline. This always works: +// • No ending newline => unaffected; already using strip "-" chomping. +// • Ending newline => removed then restored. +// Importantly, this keeps the "+" chomp indicator from gaining an extra line. +function writeScalar( + state: DumperState, + string: string, + level: number, + iskey: boolean +): void { + state.dump = ((): string => { + if (string.length === 0) { + return "''"; + } + if ( + !state.noCompatMode && + DEPRECATED_BOOLEANS_SYNTAX.indexOf(string) !== -1 + ) { + return `'${string}'`; + } + + const indent = state.indent * Math.max(1, level); // no 0-indent scalars + // As indentation gets deeper, let the width decrease monotonically + // to the lower bound min(state.lineWidth, 40). + // Note that this implies + // state.lineWidth ≤ 40 + state.indent: width is fixed at the lower bound. + // state.lineWidth > 40 + state.indent: width decreases until the lower + // bound. + // This behaves better than a constant minimum width which disallows + // narrower options, or an indent threshold which causes the width + // to suddenly increase. + const lineWidth = + state.lineWidth === -1 + ? -1 + : Math.max(Math.min(state.lineWidth, 40), state.lineWidth - indent); + + // Without knowing if keys are implicit/explicit, + // assume implicit for safety. + const singleLineOnly = + iskey || + // No block styles in flow mode. + (state.flowLevel > -1 && level >= state.flowLevel); + function testAmbiguity(str: string): boolean { + return testImplicitResolving(state, str); + } + + switch ( + chooseScalarStyle( + string, + singleLineOnly, + state.indent, + lineWidth, + testAmbiguity + ) + ) { + case STYLE_PLAIN: + return string; + case STYLE_SINGLE: + return `'${string.replace(/'/g, "''")}'`; + case STYLE_LITERAL: + return `|${blockHeader(string, state.indent)}${dropEndingNewline( + indentString(string, indent) + )}`; + case STYLE_FOLDED: + return `>${blockHeader(string, state.indent)}${dropEndingNewline( + indentString(foldString(string, lineWidth), indent) + )}`; + case STYLE_DOUBLE: + return `"${escapeString(string)}"`; + default: + throw new YAMLError("impossible error: invalid scalar style"); + } + })(); +} + +function writeFlowSequence( + state: DumperState, + level: number, + object: Any +): void { + let _result = ""; + const _tag = state.tag; + + for (let index = 0, length = object.length; index < length; index += 1) { + // Write only valid elements. + // eslint-disable-next-line @typescript-eslint/no-use-before-define + if (writeNode(state, level, object[index], false, false)) { + if (index !== 0) _result += `,${!state.condenseFlow ? " " : ""}`; + _result += state.dump; + } + } + + state.tag = _tag; + state.dump = `[${_result}]`; +} + +function writeBlockSequence( + state: DumperState, + level: number, + object: Any, + compact = false +): void { + let _result = ""; + const _tag = state.tag; + + for (let index = 0, length = object.length; index < length; index += 1) { + // Write only valid elements. + // eslint-disable-next-line @typescript-eslint/no-use-before-define + if (writeNode(state, level + 1, object[index], true, true)) { + if (!compact || index !== 0) { + _result += generateNextLine(state, level); + } + + if (state.dump && CHAR_LINE_FEED === state.dump.charCodeAt(0)) { + _result += "-"; + } else { + _result += "- "; + } + + _result += state.dump; + } + } + + state.tag = _tag; + state.dump = _result || "[]"; // Empty sequence if no valid values. +} + +function writeFlowMapping( + state: DumperState, + level: number, + object: Any +): void { + let _result = ""; + const _tag = state.tag, + objectKeyList = Object.keys(object); + + let pairBuffer: string, objectKey: string, objectValue: Any; + for ( + let index = 0, length = objectKeyList.length; + index < length; + index += 1 + ) { + pairBuffer = state.condenseFlow ? '"' : ""; + + if (index !== 0) pairBuffer += ", "; + + objectKey = objectKeyList[index]; + objectValue = object[objectKey]; + + // eslint-disable-next-line @typescript-eslint/no-use-before-define + if (!writeNode(state, level, objectKey, false, false)) { + continue; // Skip this pair because of invalid key; + } + + if (state.dump.length > 1024) pairBuffer += "? "; + + pairBuffer += `${state.dump}${state.condenseFlow ? '"' : ""}:${ + state.condenseFlow ? "" : " " + }`; + + // eslint-disable-next-line @typescript-eslint/no-use-before-define + if (!writeNode(state, level, objectValue, false, false)) { + continue; // Skip this pair because of invalid value. + } + + pairBuffer += state.dump; + + // Both key and value are valid. + _result += pairBuffer; + } + + state.tag = _tag; + state.dump = `{${_result}}`; +} + +function writeBlockMapping( + state: DumperState, + level: number, + object: Any, + compact = false +): void { + const _tag = state.tag, + objectKeyList = Object.keys(object); + let _result = ""; + + // Allow sorting keys so that the output file is deterministic + if (state.sortKeys === true) { + // Default sorting + objectKeyList.sort(); + } else if (typeof state.sortKeys === "function") { + // Custom sort function + objectKeyList.sort(state.sortKeys); + } else if (state.sortKeys) { + // Something is wrong + throw new YAMLError("sortKeys must be a boolean or a function"); + } + + let pairBuffer = "", + objectKey: string, + objectValue: Any, + explicitPair: boolean; + for ( + let index = 0, length = objectKeyList.length; + index < length; + index += 1 + ) { + pairBuffer = ""; + + if (!compact || index !== 0) { + pairBuffer += generateNextLine(state, level); + } + + objectKey = objectKeyList[index]; + objectValue = object[objectKey]; + + // eslint-disable-next-line @typescript-eslint/no-use-before-define + if (!writeNode(state, level + 1, objectKey, true, true, true)) { + continue; // Skip this pair because of invalid key. + } + + explicitPair = + (state.tag !== null && state.tag !== "?") || + (state.dump && state.dump.length > 1024); + + if (explicitPair) { + if (state.dump && CHAR_LINE_FEED === state.dump.charCodeAt(0)) { + pairBuffer += "?"; + } else { + pairBuffer += "? "; + } + } + + pairBuffer += state.dump; + + if (explicitPair) { + pairBuffer += generateNextLine(state, level); + } + + // eslint-disable-next-line @typescript-eslint/no-use-before-define + if (!writeNode(state, level + 1, objectValue, true, explicitPair)) { + continue; // Skip this pair because of invalid value. + } + + if (state.dump && CHAR_LINE_FEED === state.dump.charCodeAt(0)) { + pairBuffer += ":"; + } else { + pairBuffer += ": "; + } + + pairBuffer += state.dump; + + // Both key and value are valid. + _result += pairBuffer; + } + + state.tag = _tag; + state.dump = _result || "{}"; // Empty mapping if no valid pairs. +} + +function detectType( + state: DumperState, + object: Any, + explicit = false +): boolean { + const typeList = explicit ? state.explicitTypes : state.implicitTypes; + + let type: Type; + let style: StyleVariant; + let _result: string; + for (let index = 0, length = typeList.length; index < length; index += 1) { + type = typeList[index]; + + if ( + (type.instanceOf || type.predicate) && + (!type.instanceOf || + (typeof object === "object" && object instanceof type.instanceOf)) && + (!type.predicate || type.predicate(object)) + ) { + state.tag = explicit ? type.tag : "?"; + + if (type.represent) { + style = state.styleMap[type.tag] || type.defaultStyle; + + if (_toString.call(type.represent) === "[object Function]") { + _result = (type.represent as RepresentFn)(object, style); + } else if (_hasOwnProperty.call(type.represent, style)) { + _result = (type.represent as ArrayObject)[style]( + object, + style + ); + } else { + throw new YAMLError( + `!<${type.tag}> tag resolver accepts not "${style}" style` + ); + } + + state.dump = _result; + } + + return true; + } + } + + return false; +} + +// Serializes `object` and writes it to global `result`. +// Returns true on success, or false on invalid object. +// +function writeNode( + state: DumperState, + level: number, + object: Any, + block: boolean, + compact: boolean, + iskey = false +): boolean { + state.tag = null; + state.dump = object; + + if (!detectType(state, object, false)) { + detectType(state, object, true); + } + + const type = _toString.call(state.dump); + + if (block) { + block = state.flowLevel < 0 || state.flowLevel > level; + } + + const objectOrArray = type === "[object Object]" || type === "[object Array]"; + + let duplicateIndex = -1; + let duplicate = false; + if (objectOrArray) { + duplicateIndex = state.duplicates.indexOf(object); + duplicate = duplicateIndex !== -1; + } + + if ( + (state.tag !== null && state.tag !== "?") || + duplicate || + (state.indent !== 2 && level > 0) + ) { + compact = false; + } + + if (duplicate && state.usedDuplicates[duplicateIndex]) { + state.dump = `*ref_${duplicateIndex}`; + } else { + if (objectOrArray && duplicate && !state.usedDuplicates[duplicateIndex]) { + state.usedDuplicates[duplicateIndex] = true; + } + if (type === "[object Object]") { + if (block && Object.keys(state.dump).length !== 0) { + writeBlockMapping(state, level, state.dump, compact); + if (duplicate) { + state.dump = `&ref_${duplicateIndex}${state.dump}`; + } + } else { + writeFlowMapping(state, level, state.dump); + if (duplicate) { + state.dump = `&ref_${duplicateIndex} ${state.dump}`; + } + } + } else if (type === "[object Array]") { + const arrayLevel = state.noArrayIndent && level > 0 ? level - 1 : level; + if (block && state.dump.length !== 0) { + writeBlockSequence(state, arrayLevel, state.dump, compact); + if (duplicate) { + state.dump = `&ref_${duplicateIndex}${state.dump}`; + } + } else { + writeFlowSequence(state, arrayLevel, state.dump); + if (duplicate) { + state.dump = `&ref_${duplicateIndex} ${state.dump}`; + } + } + } else if (type === "[object String]") { + if (state.tag !== "?") { + writeScalar(state, state.dump, level, iskey); + } + } else { + if (state.skipInvalid) return false; + throw new YAMLError(`unacceptable kind of an object to dump ${type}`); + } + + if (state.tag !== null && state.tag !== "?") { + state.dump = `!<${state.tag}> ${state.dump}`; + } + } + + return true; +} + +function inspectNode( + object: Any, + objects: Any[], + duplicatesIndexes: number[] +): void { + if (object !== null && typeof object === "object") { + const index = objects.indexOf(object); + if (index !== -1) { + if (duplicatesIndexes.indexOf(index) === -1) { + duplicatesIndexes.push(index); + } + } else { + objects.push(object); + + if (Array.isArray(object)) { + for (let idx = 0, length = object.length; idx < length; idx += 1) { + inspectNode(object[idx], objects, duplicatesIndexes); + } + } else { + const objectKeyList = Object.keys(object); + + for ( + let idx = 0, length = objectKeyList.length; + idx < length; + idx += 1 + ) { + inspectNode(object[objectKeyList[idx]], objects, duplicatesIndexes); + } + } + } + } +} + +function getDuplicateReferences(object: object, state: DumperState): void { + const objects: Any[] = [], + duplicatesIndexes: number[] = []; + + inspectNode(object, objects, duplicatesIndexes); + + const length = duplicatesIndexes.length; + for (let index = 0; index < length; index += 1) { + state.duplicates.push(objects[duplicatesIndexes[index]]); + } + state.usedDuplicates = new Array(length); +} + +export function dump(input: Any, options?: DumperStateOptions): string { + options = options || {}; + + const state = new DumperState(options); + + if (!state.noRefs) getDuplicateReferences(input, state); + + if (writeNode(state, 0, input, true, true)) return `${state.dump}\n`; + + return ""; +} diff --git a/std/encoding/_yaml/dumper/dumper_state.ts b/std/encoding/_yaml/dumper/dumper_state.ts new file mode 100644 index 000000000..94cd84878 --- /dev/null +++ b/std/encoding/_yaml/dumper/dumper_state.ts @@ -0,0 +1,141 @@ +// Ported from js-yaml v3.13.1: +// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da +// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +import { Schema, SchemaDefinition } from "../schema.ts"; +import { State } from "../state.ts"; +import { StyleVariant, Type } from "../type.ts"; +import { ArrayObject, Any } from "../utils.ts"; + +const _hasOwnProperty = Object.prototype.hasOwnProperty; + +function compileStyleMap( + schema: Schema, + map?: ArrayObject | null +): ArrayObject { + if (typeof map === "undefined" || map === null) return {}; + + let type: Type; + const result: ArrayObject = {}; + const keys = Object.keys(map); + let tag: string, style: StyleVariant; + for (let index = 0, length = keys.length; index < length; index += 1) { + tag = keys[index]; + style = String(map[tag]) as StyleVariant; + if (tag.slice(0, 2) === "!!") { + tag = `tag:yaml.org,2002:${tag.slice(2)}`; + } + type = schema.compiledTypeMap.fallback[tag]; + + if ( + type && + typeof type.styleAliases !== "undefined" && + _hasOwnProperty.call(type.styleAliases, style) + ) { + style = type.styleAliases[style]; + } + + result[tag] = style; + } + + return result; +} + +export interface DumperStateOptions { + /** indentation width to use (in spaces). */ + indent?: number; + /** when true, will not add an indentation level to array elements */ + noArrayIndent?: boolean; + /** + * do not throw on invalid types (like function in the safe schema) + * and skip pairs and single values with such types. + */ + skipInvalid?: boolean; + /** + * specifies level of nesting, when to switch from + * block to flow style for collections. -1 means block style everwhere + */ + flowLevel?: number; + /** Each tag may have own set of styles. - "tag" => "style" map. */ + styles?: ArrayObject | null; + /** specifies a schema to use. */ + schema?: SchemaDefinition; + /** + * If true, sort keys when dumping YAML in ascending, ASCII character order. + * If a function, use the function to sort the keys. (default: false) + * If a function is specified, the function must return a negative value + * if first argument is less than second argument, zero if they're equal + * and a positive value otherwise. + */ + sortKeys?: boolean | ((a: string, b: string) => number); + /** set max line width. (default: 80) */ + lineWidth?: number; + /** + * if true, don't convert duplicate objects + * into references (default: false) + */ + noRefs?: boolean; + /** + * if true don't try to be compatible with older yaml versions. + * Currently: don't quote "yes", "no" and so on, + * as required for YAML 1.1 (default: false) + */ + noCompatMode?: boolean; + /** + * if true flow sequences will be condensed, omitting the + * space between `key: value` or `a, b`. Eg. `'[a,b]'` or `{a:{b:c}}`. + * Can be useful when using yaml for pretty URL query params + * as spaces are %-encoded. (default: false). + */ + condenseFlow?: boolean; +} + +export class DumperState extends State { + public indent: number; + public noArrayIndent: boolean; + public skipInvalid: boolean; + public flowLevel: number; + public sortKeys: boolean | ((a: Any, b: Any) => number); + public lineWidth: number; + public noRefs: boolean; + public noCompatMode: boolean; + public condenseFlow: boolean; + public implicitTypes: Type[]; + public explicitTypes: Type[]; + public tag: string | null = null; + public result = ""; + public duplicates: Any[] = []; + public usedDuplicates: Any[] = []; // changed from null to [] + public styleMap: ArrayObject; + public dump: Any; + + constructor({ + schema, + indent = 2, + noArrayIndent = false, + skipInvalid = false, + flowLevel = -1, + styles = null, + sortKeys = false, + lineWidth = 80, + noRefs = false, + noCompatMode = false, + condenseFlow = false, + }: DumperStateOptions) { + super(schema); + this.indent = Math.max(1, indent); + this.noArrayIndent = noArrayIndent; + this.skipInvalid = skipInvalid; + this.flowLevel = flowLevel; + this.styleMap = compileStyleMap(this.schema as Schema, styles); + this.sortKeys = sortKeys; + this.lineWidth = lineWidth; + this.noRefs = noRefs; + this.noCompatMode = noCompatMode; + this.condenseFlow = condenseFlow; + + this.implicitTypes = (this.schema as Schema).compiledImplicit; + this.explicitTypes = (this.schema as Schema).compiledExplicit; + } +} diff --git a/std/encoding/_yaml/error.ts b/std/encoding/_yaml/error.ts new file mode 100644 index 000000000..7f305ccf2 --- /dev/null +++ b/std/encoding/_yaml/error.ts @@ -0,0 +1,20 @@ +// Ported from js-yaml v3.13.1: +// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da +// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +import { Mark } from "./mark.ts"; + +export class YAMLError extends Error { + constructor( + message = "(unknown reason)", + protected mark: Mark | string = "" + ) { + super(`${message} ${mark}`); + this.name = this.constructor.name; + } + + public toString(_compact: boolean): string { + return `${this.name}: ${this.message} ${this.mark}`; + } +} diff --git a/std/encoding/_yaml/example/dump.ts b/std/encoding/_yaml/example/dump.ts new file mode 100644 index 000000000..db3647274 --- /dev/null +++ b/std/encoding/_yaml/example/dump.ts @@ -0,0 +1,22 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +import { stringify } from "../../yaml.ts"; + +console.log( + stringify({ + foo: { + bar: true, + test: [ + "a", + "b", + { + a: false, + }, + { + a: false, + }, + ], + }, + test: "foobar", + }) +); diff --git a/std/encoding/_yaml/example/inout.ts b/std/encoding/_yaml/example/inout.ts new file mode 100644 index 000000000..b0b47e3fe --- /dev/null +++ b/std/encoding/_yaml/example/inout.ts @@ -0,0 +1,27 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +import { parse, stringify } from "../../yaml.ts"; + +const test = { + foo: { + bar: true, + test: [ + "a", + "b", + { + a: false, + }, + { + a: false, + }, + ], + }, + test: "foobar", +}; + +const string = stringify(test); +if (Deno.inspect(test) === Deno.inspect(parse(string))) { + console.log("In-Out as expected."); +} else { + console.log("Someting went wrong."); +} diff --git a/std/encoding/_yaml/example/parse.ts b/std/encoding/_yaml/example/parse.ts new file mode 100644 index 000000000..fc15daf9c --- /dev/null +++ b/std/encoding/_yaml/example/parse.ts @@ -0,0 +1,19 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +import { parse } from "../../yaml.ts"; + +const result = parse(` +test: toto +foo: + bar: True + baz: 1 + qux: ~ +`); +console.log(result); + +const expected = '{ test: "toto", foo: { bar: true, baz: 1, qux: null } }'; +if (Deno.inspect(result) === expected) { + console.log("Output is as expected."); +} else { + console.error("Error during parse. Output is not as expect.", expected); +} diff --git a/std/encoding/_yaml/example/sample_document.ts b/std/encoding/_yaml/example/sample_document.ts new file mode 100644 index 000000000..da969d679 --- /dev/null +++ b/std/encoding/_yaml/example/sample_document.ts @@ -0,0 +1,23 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +/* eslint-disable @typescript-eslint/explicit-function-return-type */ + +import { parse } from "../../yaml.ts"; + +const { readFileSync, cwd } = Deno; + +(() => { + const yml = readFileSync(`${cwd()}/example/sample_document.yml`); + + const document = new TextDecoder().decode(yml); + const obj = parse(document) as object; + console.log(obj); + + let i = 0; + for (const o of Object.values(obj)) { + console.log(`======${i}`); + for (const [key, value] of Object.entries(o)) { + console.log(key, value); + } + i++; + } +})(); diff --git a/std/encoding/_yaml/example/sample_document.yml b/std/encoding/_yaml/example/sample_document.yml new file mode 100644 index 000000000..1f3c2eb3e --- /dev/null +++ b/std/encoding/_yaml/example/sample_document.yml @@ -0,0 +1,197 @@ +--- +# Collection Types ############################################################# +################################################################################ + +# http://yaml.org/type/map.html -----------------------------------------------# + +map: + # Unordered set of key: value pairs. + Block style: !!map + Clark : Evans + Ingy : döt Net + Oren : Ben-Kiki + Flow style: !!map { Clark: Evans, Ingy: döt Net, Oren: Ben-Kiki } + +# http://yaml.org/type/omap.html ----------------------------------------------# + +omap: + # Explicitly typed ordered map (dictionary). + Bestiary: !!omap + - aardvark: African pig-like ant eater. Ugly. + - anteater: South-American ant eater. Two species. + - anaconda: South-American constrictor snake. Scaly. + # Etc. + # Flow style + Numbers: !!omap [ one: 1, two: 2, three : 3 ] + +# http://yaml.org/type/pairs.html ---------------------------------------------# + +pairs: + # Explicitly typed pairs. + Block tasks: !!pairs + - meeting: with team. + - meeting: with boss. + - break: lunch. + - meeting: with client. + Flow tasks: !!pairs [ meeting: with team, meeting: with boss ] + +# http://yaml.org/type/set.html -----------------------------------------------# + +set: + # Explicitly typed set. + baseball players: !!set + ? Mark McGwire + ? Sammy Sosa + ? Ken Griffey + # Flow style + baseball teams: !!set { Boston Red Sox, Detroit Tigers, New York Yankees } + +# http://yaml.org/type/seq.html -----------------------------------------------# + +seq: + # Ordered sequence of nodes + Block style: !!seq + - Mercury # Rotates - no light/dark sides. + - Venus # Deadliest. Aptly named. + - Earth # Mostly dirt. + - Mars # Seems empty. + - Jupiter # The king. + - Saturn # Pretty. + - Uranus # Where the sun hardly shines. + - Neptune # Boring. No rings. + - Pluto # You call this a planet? + Flow style: !!seq [ Mercury, Venus, Earth, Mars, # Rocks + Jupiter, Saturn, Uranus, Neptune, # Gas + Pluto ] # Overrated + + +# Scalar Types ################################################################# +################################################################################ + +# http://yaml.org/type/binary.html --------------------------------------------# + +binary: + canonical: !!binary "\ + R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5\ + OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+\ + +f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC\ + AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs=" + generic: !!binary | + R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5 + OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+ + +f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC + AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs= + description: + The binary value above is a tiny arrow encoded as a gif image. + +# http://yaml.org/type/bool.html ----------------------------------------------# + +bool: + - true + - True + - TRUE + - false + - False + - FALSE + +# http://yaml.org/type/float.html ---------------------------------------------# + +float: + canonical: 6.8523015e+5 + exponential: 685.230_15e+03 + fixed: 685_230.15 + sexagesimal: 190:20:30.15 + negative infinity: -.inf + not a number: .NaN + +# http://yaml.org/type/int.html -----------------------------------------------# + +int: + canonical: 685230 + decimal: +685_230 + octal: 02472256 + hexadecimal: 0x_0A_74_AE + binary: 0b1010_0111_0100_1010_1110 + sexagesimal: 190:20:30 + +# http://yaml.org/type/merge.html ---------------------------------------------# + +merge: + - &CENTER { x: 1, y: 2 } + - &LEFT { x: 0, y: 2 } + - &BIG { r: 10 } + - &SMALL { r: 1 } + + # All the following maps are equal: + + - # Explicit keys + x: 1 + y: 2 + r: 10 + label: nothing + + - # Merge one map + << : *CENTER + r: 10 + label: center + + - # Merge multiple maps + << : [ *CENTER, *BIG ] + label: center/big + + - # Override + << : [ *BIG, *LEFT, *SMALL ] + x: 1 + label: big/left/small + +# http://yaml.org/type/null.html ----------------------------------------------# + +null: + # This mapping has four keys, + # one has a value. + empty: + canonical: ~ + english: null + ~: null key + # This sequence has five + # entries, two have values. + sparse: + - ~ + - 2nd entry + - + - 4th entry + - Null + +# http://yaml.org/type/str.html -----------------------------------------------# + +string: abcd + +# http://yaml.org/type/timestamp.html -----------------------------------------# + +timestamp: + canonical: 2001-12-15T02:59:43.1Z + valid iso8601: 2001-12-14t21:59:43.10-05:00 + space separated: 2001-12-14 21:59:43.10 -5 + no time zone (Z): 2001-12-15 2:59:43.10 + date (00:00:00Z): 2002-12-14 + + +# JavaScript Specific Types #################################################### +################################################################################ + +# https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/RegExp + +# regexp: +# simple: !!js/regexp foobar +# modifiers: !!js/regexp /foobar/mi + +# https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/undefined + +# undefined: !!js/undefined ~ + +# https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function + +# function: !!js/function > +# function foobar() { +# return 'Wow! JS-YAML Rocks!'; +# } diff --git a/std/encoding/_yaml/loader/loader.ts b/std/encoding/_yaml/loader/loader.ts new file mode 100644 index 000000000..f0d535624 --- /dev/null +++ b/std/encoding/_yaml/loader/loader.ts @@ -0,0 +1,1797 @@ +// Ported from js-yaml v3.13.1: +// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da +// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +/* eslint-disable max-len */ + +import { YAMLError } from "../error.ts"; +import { Mark } from "../mark.ts"; +import { Type } from "../type.ts"; +import * as common from "../utils.ts"; +import { LoaderState, LoaderStateOptions, ResultType } from "./loader_state.ts"; + +type Any = common.Any; +type ArrayObject = common.ArrayObject; + +const _hasOwnProperty = Object.prototype.hasOwnProperty; + +const CONTEXT_FLOW_IN = 1; +const CONTEXT_FLOW_OUT = 2; +const CONTEXT_BLOCK_IN = 3; +const CONTEXT_BLOCK_OUT = 4; + +const CHOMPING_CLIP = 1; +const CHOMPING_STRIP = 2; +const CHOMPING_KEEP = 3; + +const PATTERN_NON_PRINTABLE = /[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x84\x86-\x9F\uFFFE\uFFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]/; +const PATTERN_NON_ASCII_LINE_BREAKS = /[\x85\u2028\u2029]/; +const PATTERN_FLOW_INDICATORS = /[,\[\]\{\}]/; +const PATTERN_TAG_HANDLE = /^(?:!|!!|![a-z\-]+!)$/i; +/* eslint-disable-next-line max-len */ +const PATTERN_TAG_URI = /^(?:!|[^,\[\]\{\}])(?:%[0-9a-f]{2}|[0-9a-z\-#;\/\?:@&=\+\$,_\.!~\*'\(\)\[\]])*$/i; + +function _class(obj: unknown): string { + return Object.prototype.toString.call(obj); +} + +function isEOL(c: number): boolean { + return c === 0x0a || /* LF */ c === 0x0d /* CR */; +} + +function isWhiteSpace(c: number): boolean { + return c === 0x09 || /* Tab */ c === 0x20 /* Space */; +} + +function isWsOrEol(c: number): boolean { + return ( + c === 0x09 /* Tab */ || + c === 0x20 /* Space */ || + c === 0x0a /* LF */ || + c === 0x0d /* CR */ + ); +} + +function isFlowIndicator(c: number): boolean { + return ( + c === 0x2c /* , */ || + c === 0x5b /* [ */ || + c === 0x5d /* ] */ || + c === 0x7b /* { */ || + c === 0x7d /* } */ + ); +} + +function fromHexCode(c: number): number { + if (0x30 <= /* 0 */ c && c <= 0x39 /* 9 */) { + return c - 0x30; + } + + const lc = c | 0x20; + + if (0x61 <= /* a */ lc && lc <= 0x66 /* f */) { + return lc - 0x61 + 10; + } + + return -1; +} + +function escapedHexLen(c: number): number { + if (c === 0x78 /* x */) { + return 2; + } + if (c === 0x75 /* u */) { + return 4; + } + if (c === 0x55 /* U */) { + return 8; + } + return 0; +} + +function fromDecimalCode(c: number): number { + if (0x30 <= /* 0 */ c && c <= 0x39 /* 9 */) { + return c - 0x30; + } + + return -1; +} + +function simpleEscapeSequence(c: number): string { + /* eslint:disable:prettier */ + return c === 0x30 /* 0 */ + ? "\x00" + : c === 0x61 /* a */ + ? "\x07" + : c === 0x62 /* b */ + ? "\x08" + : c === 0x74 /* t */ + ? "\x09" + : c === 0x09 /* Tab */ + ? "\x09" + : c === 0x6e /* n */ + ? "\x0A" + : c === 0x76 /* v */ + ? "\x0B" + : c === 0x66 /* f */ + ? "\x0C" + : c === 0x72 /* r */ + ? "\x0D" + : c === 0x65 /* e */ + ? "\x1B" + : c === 0x20 /* Space */ + ? " " + : c === 0x22 /* " */ + ? "\x22" + : c === 0x2f /* / */ + ? "/" + : c === 0x5c /* \ */ + ? "\x5C" + : c === 0x4e /* N */ + ? "\x85" + : c === 0x5f /* _ */ + ? "\xA0" + : c === 0x4c /* L */ + ? "\u2028" + : c === 0x50 /* P */ + ? "\u2029" + : ""; + /* eslint:enable:prettier */ +} + +function charFromCodepoint(c: number): string { + if (c <= 0xffff) { + return String.fromCharCode(c); + } + // Encode UTF-16 surrogate pair + // https://en.wikipedia.org/wiki/UTF-16#Code_points_U.2B010000_to_U.2B10FFFF + return String.fromCharCode( + ((c - 0x010000) >> 10) + 0xd800, + ((c - 0x010000) & 0x03ff) + 0xdc00 + ); +} + +const simpleEscapeCheck = new Array(256); // integer, for fast access +const simpleEscapeMap = new Array(256); +for (let i = 0; i < 256; i++) { + simpleEscapeCheck[i] = simpleEscapeSequence(i) ? 1 : 0; + simpleEscapeMap[i] = simpleEscapeSequence(i); +} + +function generateError(state: LoaderState, message: string): YAMLError { + return new YAMLError( + message, + new Mark( + state.filename as string, + state.input, + state.position, + state.line, + state.position - state.lineStart + ) + ); +} + +function throwError(state: LoaderState, message: string): never { + throw generateError(state, message); +} + +function throwWarning(state: LoaderState, message: string): void { + if (state.onWarning) { + state.onWarning.call(null, generateError(state, message)); + } +} + +interface DirectiveHandlers { + [directive: string]: ( + state: LoaderState, + name: string, + ...args: string[] + ) => void; +} + +const directiveHandlers: DirectiveHandlers = { + YAML(state, _name, ...args: string[]) { + if (state.version !== null) { + return throwError(state, "duplication of %YAML directive"); + } + + if (args.length !== 1) { + return throwError(state, "YAML directive accepts exactly one argument"); + } + + const match = /^([0-9]+)\.([0-9]+)$/.exec(args[0]); + if (match === null) { + return throwError(state, "ill-formed argument of the YAML directive"); + } + + const major = parseInt(match[1], 10); + const minor = parseInt(match[2], 10); + if (major !== 1) { + return throwError(state, "unacceptable YAML version of the document"); + } + + state.version = args[0]; + state.checkLineBreaks = minor < 2; + if (minor !== 1 && minor !== 2) { + return throwWarning(state, "unsupported YAML version of the document"); + } + }, + + TAG(state, _name, ...args: string[]): void { + if (args.length !== 2) { + return throwError(state, "TAG directive accepts exactly two arguments"); + } + + const handle = args[0]; + const prefix = args[1]; + + if (!PATTERN_TAG_HANDLE.test(handle)) { + return throwError( + state, + "ill-formed tag handle (first argument) of the TAG directive" + ); + } + + if (_hasOwnProperty.call(state.tagMap, handle)) { + return throwError( + state, + `there is a previously declared suffix for "${handle}" tag handle` + ); + } + + if (!PATTERN_TAG_URI.test(prefix)) { + return throwError( + state, + "ill-formed tag prefix (second argument) of the TAG directive" + ); + } + + if (typeof state.tagMap === "undefined") { + state.tagMap = {}; + } + state.tagMap[handle] = prefix; + }, +}; + +function captureSegment( + state: LoaderState, + start: number, + end: number, + checkJson: boolean +): void { + let result: string; + if (start < end) { + result = state.input.slice(start, end); + + if (checkJson) { + for ( + let position = 0, length = result.length; + position < length; + position++ + ) { + const character = result.charCodeAt(position); + if ( + !(character === 0x09 || (0x20 <= character && character <= 0x10ffff)) + ) { + return throwError(state, "expected valid JSON character"); + } + } + } else if (PATTERN_NON_PRINTABLE.test(result)) { + return throwError(state, "the stream contains non-printable characters"); + } + + state.result += result; + } +} + +function mergeMappings( + state: LoaderState, + destination: ArrayObject, + source: ArrayObject, + overridableKeys: ArrayObject +): void { + if (!common.isObject(source)) { + return throwError( + state, + "cannot merge mappings; the provided source object is unacceptable" + ); + } + + const keys = Object.keys(source); + for (let i = 0, len = keys.length; i < len; i++) { + const key = keys[i]; + if (!_hasOwnProperty.call(destination, key)) { + destination[key] = (source as ArrayObject)[key]; + overridableKeys[key] = true; + } + } +} + +function storeMappingPair( + state: LoaderState, + result: ArrayObject | null, + overridableKeys: ArrayObject, + keyTag: string | null, + keyNode: Any, + valueNode: unknown, + startLine?: number, + startPos?: number +): ArrayObject { + // The output is a plain object here, so keys can only be strings. + // We need to convert keyNode to a string, but doing so can hang the process + // (deeply nested arrays that explode exponentially using aliases). + if (Array.isArray(keyNode)) { + keyNode = Array.prototype.slice.call(keyNode); + + for (let index = 0, quantity = keyNode.length; index < quantity; index++) { + if (Array.isArray(keyNode[index])) { + return throwError(state, "nested arrays are not supported inside keys"); + } + + if ( + typeof keyNode === "object" && + _class(keyNode[index]) === "[object Object]" + ) { + keyNode[index] = "[object Object]"; + } + } + } + + // Avoid code execution in load() via toString property + // (still use its own toString for arrays, timestamps, + // and whatever user schema extensions happen to have @@toStringTag) + if (typeof keyNode === "object" && _class(keyNode) === "[object Object]") { + keyNode = "[object Object]"; + } + + keyNode = String(keyNode); + + if (result === null) { + result = {}; + } + + if (keyTag === "tag:yaml.org,2002:merge") { + if (Array.isArray(valueNode)) { + for ( + let index = 0, quantity = valueNode.length; + index < quantity; + index++ + ) { + mergeMappings(state, result, valueNode[index], overridableKeys); + } + } else { + mergeMappings(state, result, valueNode as ArrayObject, overridableKeys); + } + } else { + if ( + !state.json && + !_hasOwnProperty.call(overridableKeys, keyNode) && + _hasOwnProperty.call(result, keyNode) + ) { + state.line = startLine || state.line; + state.position = startPos || state.position; + return throwError(state, "duplicated mapping key"); + } + result[keyNode] = valueNode; + delete overridableKeys[keyNode]; + } + + return result; +} + +function readLineBreak(state: LoaderState): void { + const ch = state.input.charCodeAt(state.position); + + if (ch === 0x0a /* LF */) { + state.position++; + } else if (ch === 0x0d /* CR */) { + state.position++; + if (state.input.charCodeAt(state.position) === 0x0a /* LF */) { + state.position++; + } + } else { + return throwError(state, "a line break is expected"); + } + + state.line += 1; + state.lineStart = state.position; +} + +function skipSeparationSpace( + state: LoaderState, + allowComments: boolean, + checkIndent: number +): number { + let lineBreaks = 0, + ch = state.input.charCodeAt(state.position); + + while (ch !== 0) { + while (isWhiteSpace(ch)) { + ch = state.input.charCodeAt(++state.position); + } + + if (allowComments && ch === 0x23 /* # */) { + do { + ch = state.input.charCodeAt(++state.position); + } while (ch !== 0x0a && /* LF */ ch !== 0x0d && /* CR */ ch !== 0); + } + + if (isEOL(ch)) { + readLineBreak(state); + + ch = state.input.charCodeAt(state.position); + lineBreaks++; + state.lineIndent = 0; + + while (ch === 0x20 /* Space */) { + state.lineIndent++; + ch = state.input.charCodeAt(++state.position); + } + } else { + break; + } + } + + if ( + checkIndent !== -1 && + lineBreaks !== 0 && + state.lineIndent < checkIndent + ) { + throwWarning(state, "deficient indentation"); + } + + return lineBreaks; +} + +function testDocumentSeparator(state: LoaderState): boolean { + let _position = state.position; + let ch = state.input.charCodeAt(_position); + + // Condition state.position === state.lineStart is tested + // in parent on each call, for efficiency. No needs to test here again. + if ( + (ch === 0x2d || /* - */ ch === 0x2e) /* . */ && + ch === state.input.charCodeAt(_position + 1) && + ch === state.input.charCodeAt(_position + 2) + ) { + _position += 3; + + ch = state.input.charCodeAt(_position); + + if (ch === 0 || isWsOrEol(ch)) { + return true; + } + } + + return false; +} + +function writeFoldedLines(state: LoaderState, count: number): void { + if (count === 1) { + state.result += " "; + } else if (count > 1) { + state.result += common.repeat("\n", count - 1); + } +} + +function readPlainScalar( + state: LoaderState, + nodeIndent: number, + withinFlowCollection: boolean +): boolean { + const kind = state.kind; + const result = state.result; + let ch = state.input.charCodeAt(state.position); + + if ( + isWsOrEol(ch) || + isFlowIndicator(ch) || + ch === 0x23 /* # */ || + ch === 0x26 /* & */ || + ch === 0x2a /* * */ || + ch === 0x21 /* ! */ || + ch === 0x7c /* | */ || + ch === 0x3e /* > */ || + ch === 0x27 /* ' */ || + ch === 0x22 /* " */ || + ch === 0x25 /* % */ || + ch === 0x40 /* @ */ || + ch === 0x60 /* ` */ + ) { + return false; + } + + let following: number; + if (ch === 0x3f || /* ? */ ch === 0x2d /* - */) { + following = state.input.charCodeAt(state.position + 1); + + if ( + isWsOrEol(following) || + (withinFlowCollection && isFlowIndicator(following)) + ) { + return false; + } + } + + state.kind = "scalar"; + state.result = ""; + let captureEnd: number, + captureStart = (captureEnd = state.position); + let hasPendingContent = false; + let line = 0; + while (ch !== 0) { + if (ch === 0x3a /* : */) { + following = state.input.charCodeAt(state.position + 1); + + if ( + isWsOrEol(following) || + (withinFlowCollection && isFlowIndicator(following)) + ) { + break; + } + } else if (ch === 0x23 /* # */) { + const preceding = state.input.charCodeAt(state.position - 1); + + if (isWsOrEol(preceding)) { + break; + } + } else if ( + (state.position === state.lineStart && testDocumentSeparator(state)) || + (withinFlowCollection && isFlowIndicator(ch)) + ) { + break; + } else if (isEOL(ch)) { + line = state.line; + const lineStart = state.lineStart; + const lineIndent = state.lineIndent; + skipSeparationSpace(state, false, -1); + + if (state.lineIndent >= nodeIndent) { + hasPendingContent = true; + ch = state.input.charCodeAt(state.position); + continue; + } else { + state.position = captureEnd; + state.line = line; + state.lineStart = lineStart; + state.lineIndent = lineIndent; + break; + } + } + + if (hasPendingContent) { + captureSegment(state, captureStart, captureEnd, false); + writeFoldedLines(state, state.line - line); + captureStart = captureEnd = state.position; + hasPendingContent = false; + } + + if (!isWhiteSpace(ch)) { + captureEnd = state.position + 1; + } + + ch = state.input.charCodeAt(++state.position); + } + + captureSegment(state, captureStart, captureEnd, false); + + if (state.result) { + return true; + } + + state.kind = kind; + state.result = result; + return false; +} + +function readSingleQuotedScalar( + state: LoaderState, + nodeIndent: number +): boolean { + let ch, captureStart, captureEnd; + + ch = state.input.charCodeAt(state.position); + + if (ch !== 0x27 /* ' */) { + return false; + } + + state.kind = "scalar"; + state.result = ""; + state.position++; + captureStart = captureEnd = state.position; + + while ((ch = state.input.charCodeAt(state.position)) !== 0) { + if (ch === 0x27 /* ' */) { + captureSegment(state, captureStart, state.position, true); + ch = state.input.charCodeAt(++state.position); + + if (ch === 0x27 /* ' */) { + captureStart = state.position; + state.position++; + captureEnd = state.position; + } else { + return true; + } + } else if (isEOL(ch)) { + captureSegment(state, captureStart, captureEnd, true); + writeFoldedLines(state, skipSeparationSpace(state, false, nodeIndent)); + captureStart = captureEnd = state.position; + } else if ( + state.position === state.lineStart && + testDocumentSeparator(state) + ) { + return throwError( + state, + "unexpected end of the document within a single quoted scalar" + ); + } else { + state.position++; + captureEnd = state.position; + } + } + + return throwError( + state, + "unexpected end of the stream within a single quoted scalar" + ); +} + +function readDoubleQuotedScalar( + state: LoaderState, + nodeIndent: number +): boolean { + let ch = state.input.charCodeAt(state.position); + + if (ch !== 0x22 /* " */) { + return false; + } + + state.kind = "scalar"; + state.result = ""; + state.position++; + let captureEnd: number, + captureStart = (captureEnd = state.position); + let tmp: number; + while ((ch = state.input.charCodeAt(state.position)) !== 0) { + if (ch === 0x22 /* " */) { + captureSegment(state, captureStart, state.position, true); + state.position++; + return true; + } + if (ch === 0x5c /* \ */) { + captureSegment(state, captureStart, state.position, true); + ch = state.input.charCodeAt(++state.position); + + if (isEOL(ch)) { + skipSeparationSpace(state, false, nodeIndent); + + // TODO: rework to inline fn with no type cast? + } else if (ch < 256 && simpleEscapeCheck[ch]) { + state.result += simpleEscapeMap[ch]; + state.position++; + } else if ((tmp = escapedHexLen(ch)) > 0) { + let hexLength = tmp; + let hexResult = 0; + + for (; hexLength > 0; hexLength--) { + ch = state.input.charCodeAt(++state.position); + + if ((tmp = fromHexCode(ch)) >= 0) { + hexResult = (hexResult << 4) + tmp; + } else { + return throwError(state, "expected hexadecimal character"); + } + } + + state.result += charFromCodepoint(hexResult); + + state.position++; + } else { + return throwError(state, "unknown escape sequence"); + } + + captureStart = captureEnd = state.position; + } else if (isEOL(ch)) { + captureSegment(state, captureStart, captureEnd, true); + writeFoldedLines(state, skipSeparationSpace(state, false, nodeIndent)); + captureStart = captureEnd = state.position; + } else if ( + state.position === state.lineStart && + testDocumentSeparator(state) + ) { + return throwError( + state, + "unexpected end of the document within a double quoted scalar" + ); + } else { + state.position++; + captureEnd = state.position; + } + } + + return throwError( + state, + "unexpected end of the stream within a double quoted scalar" + ); +} + +function readFlowCollection(state: LoaderState, nodeIndent: number): boolean { + let ch = state.input.charCodeAt(state.position); + let terminator: number; + let isMapping = true; + let result: ResultType = {}; + if (ch === 0x5b /* [ */) { + terminator = 0x5d; /* ] */ + isMapping = false; + result = []; + } else if (ch === 0x7b /* { */) { + terminator = 0x7d; /* } */ + } else { + return false; + } + + if ( + state.anchor !== null && + typeof state.anchor != "undefined" && + typeof state.anchorMap != "undefined" + ) { + state.anchorMap[state.anchor] = result; + } + + ch = state.input.charCodeAt(++state.position); + + const tag = state.tag, + anchor = state.anchor; + let readNext = true; + let valueNode, + keyNode, + keyTag: string | null = (keyNode = valueNode = null), + isExplicitPair: boolean, + isPair = (isExplicitPair = false); + let following = 0, + line = 0; + const overridableKeys: ArrayObject = {}; + while (ch !== 0) { + skipSeparationSpace(state, true, nodeIndent); + + ch = state.input.charCodeAt(state.position); + + if (ch === terminator) { + state.position++; + state.tag = tag; + state.anchor = anchor; + state.kind = isMapping ? "mapping" : "sequence"; + state.result = result; + return true; + } + if (!readNext) { + return throwError(state, "missed comma between flow collection entries"); + } + + keyTag = keyNode = valueNode = null; + isPair = isExplicitPair = false; + + if (ch === 0x3f /* ? */) { + following = state.input.charCodeAt(state.position + 1); + + if (isWsOrEol(following)) { + isPair = isExplicitPair = true; + state.position++; + skipSeparationSpace(state, true, nodeIndent); + } + } + + line = state.line; + // eslint-disable-next-line @typescript-eslint/no-use-before-define + composeNode(state, nodeIndent, CONTEXT_FLOW_IN, false, true); + keyTag = state.tag || null; + keyNode = state.result; + skipSeparationSpace(state, true, nodeIndent); + + ch = state.input.charCodeAt(state.position); + + if ((isExplicitPair || state.line === line) && ch === 0x3a /* : */) { + isPair = true; + ch = state.input.charCodeAt(++state.position); + skipSeparationSpace(state, true, nodeIndent); + // eslint-disable-next-line @typescript-eslint/no-use-before-define + composeNode(state, nodeIndent, CONTEXT_FLOW_IN, false, true); + valueNode = state.result; + } + + if (isMapping) { + storeMappingPair( + state, + result, + overridableKeys, + keyTag, + keyNode, + valueNode + ); + } else if (isPair) { + (result as Array<{}>).push( + storeMappingPair( + state, + null, + overridableKeys, + keyTag, + keyNode, + valueNode + ) + ); + } else { + (result as ResultType[]).push(keyNode as ResultType); + } + + skipSeparationSpace(state, true, nodeIndent); + + ch = state.input.charCodeAt(state.position); + + if (ch === 0x2c /* , */) { + readNext = true; + ch = state.input.charCodeAt(++state.position); + } else { + readNext = false; + } + } + + return throwError( + state, + "unexpected end of the stream within a flow collection" + ); +} + +function readBlockScalar(state: LoaderState, nodeIndent: number): boolean { + let chomping = CHOMPING_CLIP, + didReadContent = false, + detectedIndent = false, + textIndent = nodeIndent, + emptyLines = 0, + atMoreIndented = false; + + let ch = state.input.charCodeAt(state.position); + + let folding = false; + if (ch === 0x7c /* | */) { + folding = false; + } else if (ch === 0x3e /* > */) { + folding = true; + } else { + return false; + } + + state.kind = "scalar"; + state.result = ""; + + let tmp = 0; + while (ch !== 0) { + ch = state.input.charCodeAt(++state.position); + + if (ch === 0x2b || /* + */ ch === 0x2d /* - */) { + if (CHOMPING_CLIP === chomping) { + chomping = ch === 0x2b /* + */ ? CHOMPING_KEEP : CHOMPING_STRIP; + } else { + return throwError(state, "repeat of a chomping mode identifier"); + } + } else if ((tmp = fromDecimalCode(ch)) >= 0) { + if (tmp === 0) { + return throwError( + state, + "bad explicit indentation width of a block scalar; it cannot be less than one" + ); + } else if (!detectedIndent) { + textIndent = nodeIndent + tmp - 1; + detectedIndent = true; + } else { + return throwError(state, "repeat of an indentation width identifier"); + } + } else { + break; + } + } + + if (isWhiteSpace(ch)) { + do { + ch = state.input.charCodeAt(++state.position); + } while (isWhiteSpace(ch)); + + if (ch === 0x23 /* # */) { + do { + ch = state.input.charCodeAt(++state.position); + } while (!isEOL(ch) && ch !== 0); + } + } + + while (ch !== 0) { + readLineBreak(state); + state.lineIndent = 0; + + ch = state.input.charCodeAt(state.position); + + while ( + (!detectedIndent || state.lineIndent < textIndent) && + ch === 0x20 /* Space */ + ) { + state.lineIndent++; + ch = state.input.charCodeAt(++state.position); + } + + if (!detectedIndent && state.lineIndent > textIndent) { + textIndent = state.lineIndent; + } + + if (isEOL(ch)) { + emptyLines++; + continue; + } + + // End of the scalar. + if (state.lineIndent < textIndent) { + // Perform the chomping. + if (chomping === CHOMPING_KEEP) { + state.result += common.repeat( + "\n", + didReadContent ? 1 + emptyLines : emptyLines + ); + } else if (chomping === CHOMPING_CLIP) { + if (didReadContent) { + // i.e. only if the scalar is not empty. + state.result += "\n"; + } + } + + // Break this `while` cycle and go to the funciton's epilogue. + break; + } + + // Folded style: use fancy rules to handle line breaks. + if (folding) { + // Lines starting with white space characters (more-indented lines) are not folded. + if (isWhiteSpace(ch)) { + atMoreIndented = true; + // except for the first content line (cf. Example 8.1) + state.result += common.repeat( + "\n", + didReadContent ? 1 + emptyLines : emptyLines + ); + + // End of more-indented block. + } else if (atMoreIndented) { + atMoreIndented = false; + state.result += common.repeat("\n", emptyLines + 1); + + // Just one line break - perceive as the same line. + } else if (emptyLines === 0) { + if (didReadContent) { + // i.e. only if we have already read some scalar content. + state.result += " "; + } + + // Several line breaks - perceive as different lines. + } else { + state.result += common.repeat("\n", emptyLines); + } + + // Literal style: just add exact number of line breaks between content lines. + } else { + // Keep all line breaks except the header line break. + state.result += common.repeat( + "\n", + didReadContent ? 1 + emptyLines : emptyLines + ); + } + + didReadContent = true; + detectedIndent = true; + emptyLines = 0; + const captureStart = state.position; + + while (!isEOL(ch) && ch !== 0) { + ch = state.input.charCodeAt(++state.position); + } + + captureSegment(state, captureStart, state.position, false); + } + + return true; +} + +function readBlockSequence(state: LoaderState, nodeIndent: number): boolean { + let line: number, + following: number, + detected = false, + ch: number; + const tag = state.tag, + anchor = state.anchor, + result: unknown[] = []; + + if ( + state.anchor !== null && + typeof state.anchor !== "undefined" && + typeof state.anchorMap !== "undefined" + ) { + state.anchorMap[state.anchor] = result; + } + + ch = state.input.charCodeAt(state.position); + + while (ch !== 0) { + if (ch !== 0x2d /* - */) { + break; + } + + following = state.input.charCodeAt(state.position + 1); + + if (!isWsOrEol(following)) { + break; + } + + detected = true; + state.position++; + + if (skipSeparationSpace(state, true, -1)) { + if (state.lineIndent <= nodeIndent) { + result.push(null); + ch = state.input.charCodeAt(state.position); + continue; + } + } + + line = state.line; + // eslint-disable-next-line @typescript-eslint/no-use-before-define + composeNode(state, nodeIndent, CONTEXT_BLOCK_IN, false, true); + result.push(state.result); + skipSeparationSpace(state, true, -1); + + ch = state.input.charCodeAt(state.position); + + if ((state.line === line || state.lineIndent > nodeIndent) && ch !== 0) { + return throwError(state, "bad indentation of a sequence entry"); + } else if (state.lineIndent < nodeIndent) { + break; + } + } + + if (detected) { + state.tag = tag; + state.anchor = anchor; + state.kind = "sequence"; + state.result = result; + return true; + } + return false; +} + +function readBlockMapping( + state: LoaderState, + nodeIndent: number, + flowIndent: number +): boolean { + const tag = state.tag, + anchor = state.anchor, + result = {}, + overridableKeys = {}; + let following: number, + allowCompact = false, + line: number, + pos: number, + keyTag = null, + keyNode = null, + valueNode = null, + atExplicitKey = false, + detected = false, + ch: number; + + if ( + state.anchor !== null && + typeof state.anchor !== "undefined" && + typeof state.anchorMap !== "undefined" + ) { + state.anchorMap[state.anchor] = result; + } + + ch = state.input.charCodeAt(state.position); + + while (ch !== 0) { + following = state.input.charCodeAt(state.position + 1); + line = state.line; // Save the current line. + pos = state.position; + + // + // Explicit notation case. There are two separate blocks: + // first for the key (denoted by "?") and second for the value (denoted by ":") + // + if ((ch === 0x3f || /* ? */ ch === 0x3a) && /* : */ isWsOrEol(following)) { + if (ch === 0x3f /* ? */) { + if (atExplicitKey) { + storeMappingPair( + state, + result, + overridableKeys, + keyTag as string, + keyNode, + null + ); + keyTag = keyNode = valueNode = null; + } + + detected = true; + atExplicitKey = true; + allowCompact = true; + } else if (atExplicitKey) { + // i.e. 0x3A/* : */ === character after the explicit key. + atExplicitKey = false; + allowCompact = true; + } else { + return throwError( + state, + "incomplete explicit mapping pair; a key node is missed; or followed by a non-tabulated empty line" + ); + } + + state.position += 1; + ch = following; + + // + // Implicit notation case. Flow-style node as the key first, then ":", and the value. + // + // eslint-disable-next-line @typescript-eslint/no-use-before-define + } else if (composeNode(state, flowIndent, CONTEXT_FLOW_OUT, false, true)) { + if (state.line === line) { + ch = state.input.charCodeAt(state.position); + + while (isWhiteSpace(ch)) { + ch = state.input.charCodeAt(++state.position); + } + + if (ch === 0x3a /* : */) { + ch = state.input.charCodeAt(++state.position); + + if (!isWsOrEol(ch)) { + return throwError( + state, + "a whitespace character is expected after the key-value separator within a block mapping" + ); + } + + if (atExplicitKey) { + storeMappingPair( + state, + result, + overridableKeys, + keyTag as string, + keyNode, + null + ); + keyTag = keyNode = valueNode = null; + } + + detected = true; + atExplicitKey = false; + allowCompact = false; + keyTag = state.tag; + keyNode = state.result; + } else if (detected) { + return throwError( + state, + "can not read an implicit mapping pair; a colon is missed" + ); + } else { + state.tag = tag; + state.anchor = anchor; + return true; // Keep the result of `composeNode`. + } + } else if (detected) { + return throwError( + state, + "can not read a block mapping entry; a multiline key may not be an implicit key" + ); + } else { + state.tag = tag; + state.anchor = anchor; + return true; // Keep the result of `composeNode`. + } + } else { + break; // Reading is done. Go to the epilogue. + } + + // + // Common reading code for both explicit and implicit notations. + // + if (state.line === line || state.lineIndent > nodeIndent) { + if ( + // eslint-disable-next-line @typescript-eslint/no-use-before-define + composeNode(state, nodeIndent, CONTEXT_BLOCK_OUT, true, allowCompact) + ) { + if (atExplicitKey) { + keyNode = state.result; + } else { + valueNode = state.result; + } + } + + if (!atExplicitKey) { + storeMappingPair( + state, + result, + overridableKeys, + keyTag as string, + keyNode, + valueNode, + line, + pos + ); + keyTag = keyNode = valueNode = null; + } + + skipSeparationSpace(state, true, -1); + ch = state.input.charCodeAt(state.position); + } + + if (state.lineIndent > nodeIndent && ch !== 0) { + return throwError(state, "bad indentation of a mapping entry"); + } else if (state.lineIndent < nodeIndent) { + break; + } + } + + // + // Epilogue. + // + + // Special case: last mapping's node contains only the key in explicit notation. + if (atExplicitKey) { + storeMappingPair( + state, + result, + overridableKeys, + keyTag as string, + keyNode, + null + ); + } + + // Expose the resulting mapping. + if (detected) { + state.tag = tag; + state.anchor = anchor; + state.kind = "mapping"; + state.result = result; + } + + return detected; +} + +function readTagProperty(state: LoaderState): boolean { + let position: number, + isVerbatim = false, + isNamed = false, + tagHandle = "", + tagName: string, + ch: number; + + ch = state.input.charCodeAt(state.position); + + if (ch !== 0x21 /* ! */) return false; + + if (state.tag !== null) { + return throwError(state, "duplication of a tag property"); + } + + ch = state.input.charCodeAt(++state.position); + + if (ch === 0x3c /* < */) { + isVerbatim = true; + ch = state.input.charCodeAt(++state.position); + } else if (ch === 0x21 /* ! */) { + isNamed = true; + tagHandle = "!!"; + ch = state.input.charCodeAt(++state.position); + } else { + tagHandle = "!"; + } + + position = state.position; + + if (isVerbatim) { + do { + ch = state.input.charCodeAt(++state.position); + } while (ch !== 0 && ch !== 0x3e /* > */); + + if (state.position < state.length) { + tagName = state.input.slice(position, state.position); + ch = state.input.charCodeAt(++state.position); + } else { + return throwError( + state, + "unexpected end of the stream within a verbatim tag" + ); + } + } else { + while (ch !== 0 && !isWsOrEol(ch)) { + if (ch === 0x21 /* ! */) { + if (!isNamed) { + tagHandle = state.input.slice(position - 1, state.position + 1); + + if (!PATTERN_TAG_HANDLE.test(tagHandle)) { + return throwError( + state, + "named tag handle cannot contain such characters" + ); + } + + isNamed = true; + position = state.position + 1; + } else { + return throwError( + state, + "tag suffix cannot contain exclamation marks" + ); + } + } + + ch = state.input.charCodeAt(++state.position); + } + + tagName = state.input.slice(position, state.position); + + if (PATTERN_FLOW_INDICATORS.test(tagName)) { + return throwError( + state, + "tag suffix cannot contain flow indicator characters" + ); + } + } + + if (tagName && !PATTERN_TAG_URI.test(tagName)) { + return throwError( + state, + `tag name cannot contain such characters: ${tagName}` + ); + } + + if (isVerbatim) { + state.tag = tagName; + } else if ( + typeof state.tagMap !== "undefined" && + _hasOwnProperty.call(state.tagMap, tagHandle) + ) { + state.tag = state.tagMap[tagHandle] + tagName; + } else if (tagHandle === "!") { + state.tag = `!${tagName}`; + } else if (tagHandle === "!!") { + state.tag = `tag:yaml.org,2002:${tagName}`; + } else { + return throwError(state, `undeclared tag handle "${tagHandle}"`); + } + + return true; +} + +function readAnchorProperty(state: LoaderState): boolean { + let ch = state.input.charCodeAt(state.position); + if (ch !== 0x26 /* & */) return false; + + if (state.anchor !== null) { + return throwError(state, "duplication of an anchor property"); + } + ch = state.input.charCodeAt(++state.position); + + const position = state.position; + while (ch !== 0 && !isWsOrEol(ch) && !isFlowIndicator(ch)) { + ch = state.input.charCodeAt(++state.position); + } + + if (state.position === position) { + return throwError( + state, + "name of an anchor node must contain at least one character" + ); + } + + state.anchor = state.input.slice(position, state.position); + return true; +} + +function readAlias(state: LoaderState): boolean { + let ch = state.input.charCodeAt(state.position); + + if (ch !== 0x2a /* * */) return false; + + ch = state.input.charCodeAt(++state.position); + const _position = state.position; + + while (ch !== 0 && !isWsOrEol(ch) && !isFlowIndicator(ch)) { + ch = state.input.charCodeAt(++state.position); + } + + if (state.position === _position) { + return throwError( + state, + "name of an alias node must contain at least one character" + ); + } + + const alias = state.input.slice(_position, state.position); + if ( + typeof state.anchorMap !== "undefined" && + !state.anchorMap.hasOwnProperty(alias) + ) { + return throwError(state, `unidentified alias "${alias}"`); + } + + if (typeof state.anchorMap !== "undefined") { + state.result = state.anchorMap[alias]; + } + skipSeparationSpace(state, true, -1); + return true; +} + +function composeNode( + state: LoaderState, + parentIndent: number, + nodeContext: number, + allowToSeek: boolean, + allowCompact: boolean +): boolean { + let allowBlockScalars: boolean, + allowBlockCollections: boolean, + indentStatus = 1, // 1: this>parent, 0: this=parent, -1: this parentIndent) { + indentStatus = 1; + } else if (state.lineIndent === parentIndent) { + indentStatus = 0; + } else if (state.lineIndent < parentIndent) { + indentStatus = -1; + } + } + } + + if (indentStatus === 1) { + while (readTagProperty(state) || readAnchorProperty(state)) { + if (skipSeparationSpace(state, true, -1)) { + atNewLine = true; + allowBlockCollections = allowBlockStyles; + + if (state.lineIndent > parentIndent) { + indentStatus = 1; + } else if (state.lineIndent === parentIndent) { + indentStatus = 0; + } else if (state.lineIndent < parentIndent) { + indentStatus = -1; + } + } else { + allowBlockCollections = false; + } + } + } + + if (allowBlockCollections) { + allowBlockCollections = atNewLine || allowCompact; + } + + if (indentStatus === 1 || CONTEXT_BLOCK_OUT === nodeContext) { + const cond = + CONTEXT_FLOW_IN === nodeContext || CONTEXT_FLOW_OUT === nodeContext; + flowIndent = cond ? parentIndent : parentIndent + 1; + + blockIndent = state.position - state.lineStart; + + if (indentStatus === 1) { + if ( + (allowBlockCollections && + (readBlockSequence(state, blockIndent) || + readBlockMapping(state, blockIndent, flowIndent))) || + readFlowCollection(state, flowIndent) + ) { + hasContent = true; + } else { + if ( + (allowBlockScalars && readBlockScalar(state, flowIndent)) || + readSingleQuotedScalar(state, flowIndent) || + readDoubleQuotedScalar(state, flowIndent) + ) { + hasContent = true; + } else if (readAlias(state)) { + hasContent = true; + + if (state.tag !== null || state.anchor !== null) { + return throwError( + state, + "alias node should not have Any properties" + ); + } + } else if ( + readPlainScalar(state, flowIndent, CONTEXT_FLOW_IN === nodeContext) + ) { + hasContent = true; + + if (state.tag === null) { + state.tag = "?"; + } + } + + if (state.anchor !== null && typeof state.anchorMap !== "undefined") { + state.anchorMap[state.anchor] = state.result; + } + } + } else if (indentStatus === 0) { + // Special case: block sequences are allowed to have same indentation level as the parent. + // http://www.yaml.org/spec/1.2/spec.html#id2799784 + hasContent = + allowBlockCollections && readBlockSequence(state, blockIndent); + } + } + + if (state.tag !== null && state.tag !== "!") { + if (state.tag === "?") { + for ( + let typeIndex = 0, typeQuantity = state.implicitTypes.length; + typeIndex < typeQuantity; + typeIndex++ + ) { + type = state.implicitTypes[typeIndex]; + + // Implicit resolving is not allowed for non-scalar types, and '?' + // non-specific tag is only assigned to plain scalars. So, it isn't + // needed to check for 'kind' conformity. + + if (type.resolve(state.result)) { + // `state.result` updated in resolver if matched + state.result = type.construct(state.result); + state.tag = type.tag; + if (state.anchor !== null && typeof state.anchorMap !== "undefined") { + state.anchorMap[state.anchor] = state.result; + } + break; + } + } + } else if ( + _hasOwnProperty.call(state.typeMap[state.kind || "fallback"], state.tag) + ) { + type = state.typeMap[state.kind || "fallback"][state.tag]; + + if (state.result !== null && type.kind !== state.kind) { + return throwError( + state, + `unacceptable node kind for !<${state.tag}> tag; it should be "${type.kind}", not "${state.kind}"` + ); + } + + if (!type.resolve(state.result)) { + // `state.result` updated in resolver if matched + return throwError( + state, + `cannot resolve a node with !<${state.tag}> explicit tag` + ); + } else { + state.result = type.construct(state.result); + if (state.anchor !== null && typeof state.anchorMap !== "undefined") { + state.anchorMap[state.anchor] = state.result; + } + } + } else { + return throwError(state, `unknown tag !<${state.tag}>`); + } + } + + if (state.listener && state.listener !== null) { + state.listener("close", state); + } + return state.tag !== null || state.anchor !== null || hasContent; +} + +function readDocument(state: LoaderState): void { + const documentStart = state.position; + let position: number, + directiveName: string, + directiveArgs: string[], + hasDirectives = false, + ch: number; + + state.version = null; + state.checkLineBreaks = state.legacy; + state.tagMap = {}; + state.anchorMap = {}; + + while ((ch = state.input.charCodeAt(state.position)) !== 0) { + skipSeparationSpace(state, true, -1); + + ch = state.input.charCodeAt(state.position); + + if (state.lineIndent > 0 || ch !== 0x25 /* % */) { + break; + } + + hasDirectives = true; + ch = state.input.charCodeAt(++state.position); + position = state.position; + + while (ch !== 0 && !isWsOrEol(ch)) { + ch = state.input.charCodeAt(++state.position); + } + + directiveName = state.input.slice(position, state.position); + directiveArgs = []; + + if (directiveName.length < 1) { + return throwError( + state, + "directive name must not be less than one character in length" + ); + } + + while (ch !== 0) { + while (isWhiteSpace(ch)) { + ch = state.input.charCodeAt(++state.position); + } + + if (ch === 0x23 /* # */) { + do { + ch = state.input.charCodeAt(++state.position); + } while (ch !== 0 && !isEOL(ch)); + break; + } + + if (isEOL(ch)) break; + + position = state.position; + + while (ch !== 0 && !isWsOrEol(ch)) { + ch = state.input.charCodeAt(++state.position); + } + + directiveArgs.push(state.input.slice(position, state.position)); + } + + if (ch !== 0) readLineBreak(state); + + if (_hasOwnProperty.call(directiveHandlers, directiveName)) { + directiveHandlers[directiveName](state, directiveName, ...directiveArgs); + } else { + throwWarning(state, `unknown document directive "${directiveName}"`); + } + } + + skipSeparationSpace(state, true, -1); + + if ( + state.lineIndent === 0 && + state.input.charCodeAt(state.position) === 0x2d /* - */ && + state.input.charCodeAt(state.position + 1) === 0x2d /* - */ && + state.input.charCodeAt(state.position + 2) === 0x2d /* - */ + ) { + state.position += 3; + skipSeparationSpace(state, true, -1); + } else if (hasDirectives) { + return throwError(state, "directives end mark is expected"); + } + + composeNode(state, state.lineIndent - 1, CONTEXT_BLOCK_OUT, false, true); + skipSeparationSpace(state, true, -1); + + if ( + state.checkLineBreaks && + PATTERN_NON_ASCII_LINE_BREAKS.test( + state.input.slice(documentStart, state.position) + ) + ) { + throwWarning(state, "non-ASCII line breaks are interpreted as content"); + } + + state.documents.push(state.result); + + if (state.position === state.lineStart && testDocumentSeparator(state)) { + if (state.input.charCodeAt(state.position) === 0x2e /* . */) { + state.position += 3; + skipSeparationSpace(state, true, -1); + } + return; + } + + if (state.position < state.length - 1) { + return throwError( + state, + "end of the stream or a document separator is expected" + ); + } else { + return; + } +} + +function loadDocuments(input: string, options?: LoaderStateOptions): unknown[] { + input = String(input); + options = options || {}; + + if (input.length !== 0) { + // Add tailing `\n` if not exists + if ( + input.charCodeAt(input.length - 1) !== 0x0a /* LF */ && + input.charCodeAt(input.length - 1) !== 0x0d /* CR */ + ) { + input += "\n"; + } + + // Strip BOM + if (input.charCodeAt(0) === 0xfeff) { + input = input.slice(1); + } + } + + const state = new LoaderState(input, options); + + // Use 0 as string terminator. That significantly simplifies bounds check. + state.input += "\0"; + + while (state.input.charCodeAt(state.position) === 0x20 /* Space */) { + state.lineIndent += 1; + state.position += 1; + } + + while (state.position < state.length - 1) { + readDocument(state); + } + + return state.documents; +} + +export type CbFunction = (doc: unknown) => void; +function isCbFunction(fn: unknown): fn is CbFunction { + return typeof fn === "function"; +} + +export function loadAll( + input: string, + iteratorOrOption?: T, + options?: LoaderStateOptions +): T extends CbFunction ? void : unknown[] { + if (!isCbFunction(iteratorOrOption)) { + return loadDocuments(input, iteratorOrOption as LoaderStateOptions) as Any; + } + + const documents = loadDocuments(input, options); + const iterator = iteratorOrOption; + for (let index = 0, length = documents.length; index < length; index++) { + iterator(documents[index]); + } + + return void 0 as Any; +} + +export function load(input: string, options?: LoaderStateOptions): unknown { + const documents = loadDocuments(input, options); + + if (documents.length === 0) { + return; + } + if (documents.length === 1) { + return documents[0]; + } + throw new YAMLError( + "expected a single document in the stream, but found more" + ); +} diff --git a/std/encoding/_yaml/loader/loader_state.ts b/std/encoding/_yaml/loader/loader_state.ts new file mode 100644 index 000000000..ca50fcaf1 --- /dev/null +++ b/std/encoding/_yaml/loader/loader_state.ts @@ -0,0 +1,74 @@ +// Ported from js-yaml v3.13.1: +// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da +// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +import { YAMLError } from "../error.ts"; +import { Schema, SchemaDefinition, TypeMap } from "../schema.ts"; +import { State } from "../state.ts"; +import { Type } from "../type.ts"; +import { Any, ArrayObject } from "../utils.ts"; + +export interface LoaderStateOptions { + legacy?: boolean; + listener?: ((...args: Any[]) => void) | null; + /** string to be used as a file path in error/warning messages. */ + filename?: string; + /** specifies a schema to use. */ + schema?: SchemaDefinition; + /** compatibility with JSON.parse behaviour. */ + json?: boolean; + /** function to call on warning messages. */ + onWarning?(this: null, e?: YAMLError): void; +} + +export type ResultType = [] | {} | string; + +export class LoaderState extends State { + public documents: Any[] = []; + public length: number; + public lineIndent = 0; + public lineStart = 0; + public position = 0; + public line = 0; + public filename?: string; + public onWarning?: (...args: Any[]) => void; + public legacy: boolean; + public json: boolean; + public listener?: ((...args: Any[]) => void) | null; + public implicitTypes: Type[]; + public typeMap: TypeMap; + + public version?: string | null; + public checkLineBreaks?: boolean; + public tagMap?: ArrayObject; + public anchorMap?: ArrayObject; + public tag?: string | null; + public anchor?: string | null; + public kind?: string | null; + public result: ResultType | null = ""; + + constructor( + public input: string, + { + filename, + schema, + onWarning, + legacy = false, + json = false, + listener = null, + }: LoaderStateOptions + ) { + super(schema); + this.filename = filename; + this.onWarning = onWarning; + this.legacy = legacy; + this.json = json; + this.listener = listener; + + this.implicitTypes = (this.schema as Schema).compiledImplicit; + this.typeMap = (this.schema as Schema).compiledTypeMap; + + this.length = input.length; + } +} diff --git a/std/encoding/_yaml/mark.ts b/std/encoding/_yaml/mark.ts new file mode 100644 index 000000000..44cf175a0 --- /dev/null +++ b/std/encoding/_yaml/mark.ts @@ -0,0 +1,77 @@ +// Ported from js-yaml v3.13.1: +// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da +// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +import { repeat } from "./utils.ts"; + +export class Mark { + constructor( + public name: string, + public buffer: string, + public position: number, + public line: number, + public column: number + ) {} + + public getSnippet(indent = 4, maxLength = 75): string | null { + if (!this.buffer) return null; + + let head = ""; + let start = this.position; + + while ( + start > 0 && + "\x00\r\n\x85\u2028\u2029".indexOf(this.buffer.charAt(start - 1)) === -1 + ) { + start -= 1; + if (this.position - start > maxLength / 2 - 1) { + head = " ... "; + start += 5; + break; + } + } + + let tail = ""; + let end = this.position; + + while ( + end < this.buffer.length && + "\x00\r\n\x85\u2028\u2029".indexOf(this.buffer.charAt(end)) === -1 + ) { + end += 1; + if (end - this.position > maxLength / 2 - 1) { + tail = " ... "; + end -= 5; + break; + } + } + + const snippet = this.buffer.slice(start, end); + return `${repeat(" ", indent)}${head}${snippet}${tail}\n${repeat( + " ", + indent + this.position - start + head.length + )}^`; + } + + public toString(compact?: boolean): string { + let snippet, + where = ""; + + if (this.name) { + where += `in "${this.name}" `; + } + + where += `at line ${this.line + 1}, column ${this.column + 1}`; + + if (!compact) { + snippet = this.getSnippet(); + + if (snippet) { + where += `:\n${snippet}`; + } + } + + return where; + } +} diff --git a/std/encoding/_yaml/parse.ts b/std/encoding/_yaml/parse.ts new file mode 100644 index 000000000..2aa0042bd --- /dev/null +++ b/std/encoding/_yaml/parse.ts @@ -0,0 +1,32 @@ +// Ported from js-yaml v3.13.1: +// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da +// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +import { CbFunction, load, loadAll } from "./loader/loader.ts"; +import { LoaderStateOptions } from "./loader/loader_state.ts"; + +export type ParseOptions = LoaderStateOptions; + +/** + * Parses `content` as single YAML document. + * + * Returns a JavaScript object or throws `YAMLException` on error. + * By default, does not support regexps, functions and undefined. This method is safe for untrusted data. + * + */ +export function parse(content: string, options?: ParseOptions): unknown { + return load(content, options); +} + +/** + * Same as `parse()`, but understands multi-document sources. + * Applies iterator to each document if specified, or returns array of documents. + */ +export function parseAll( + content: string, + iterator?: CbFunction, + options?: ParseOptions +): unknown { + return loadAll(content, iterator, options); +} diff --git a/std/encoding/_yaml/parse_test.ts b/std/encoding/_yaml/parse_test.ts new file mode 100644 index 000000000..21f1b893b --- /dev/null +++ b/std/encoding/_yaml/parse_test.ts @@ -0,0 +1,56 @@ +// Ported from js-yaml v3.13.1: +// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da +// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +import { parse, parseAll } from "./parse.ts"; +import { assertEquals } from "../../testing/asserts.ts"; + +Deno.test({ + name: "`parse` parses single document yaml string", + fn(): void { + const yaml = ` + test: toto + foo: + bar: True + baz: 1 + qux: ~ + `; + + const expected = { test: "toto", foo: { bar: true, baz: 1, qux: null } }; + + assertEquals(parse(yaml), expected); + }, +}); + +Deno.test({ + name: "`parseAll` parses the yaml string with multiple documents", + fn(): void { + const yaml = ` +--- +id: 1 +name: Alice +--- +id: 2 +name: Bob +--- +id: 3 +name: Eve + `; + const expected = [ + { + id: 1, + name: "Alice", + }, + { + id: 2, + name: "Bob", + }, + { + id: 3, + name: "Eve", + }, + ]; + assertEquals(parseAll(yaml), expected); + }, +}); diff --git a/std/encoding/_yaml/schema.ts b/std/encoding/_yaml/schema.ts new file mode 100644 index 000000000..579644dbb --- /dev/null +++ b/std/encoding/_yaml/schema.ts @@ -0,0 +1,101 @@ +// Ported from js-yaml v3.13.1: +// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da +// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +import { YAMLError } from "./error.ts"; +import { KindType, Type } from "./type.ts"; +import { ArrayObject, Any } from "./utils.ts"; + +function compileList( + schema: Schema, + name: "implicit" | "explicit", + result: Type[] +): Type[] { + const exclude: number[] = []; + + for (const includedSchema of schema.include) { + result = compileList(includedSchema, name, result); + } + + for (const currentType of schema[name]) { + for ( + let previousIndex = 0; + previousIndex < result.length; + previousIndex++ + ) { + const previousType = result[previousIndex]; + if ( + previousType.tag === currentType.tag && + previousType.kind === currentType.kind + ) { + exclude.push(previousIndex); + } + } + + result.push(currentType); + } + + return result.filter((type, index): unknown => !exclude.includes(index)); +} + +export type TypeMap = { [k in KindType | "fallback"]: ArrayObject }; +function compileMap(...typesList: Type[][]): TypeMap { + const result: TypeMap = { + fallback: {}, + mapping: {}, + scalar: {}, + sequence: {}, + }; + + for (const types of typesList) { + for (const type of types) { + if (type.kind !== null) { + result[type.kind][type.tag] = result["fallback"][type.tag] = type; + } + } + } + return result; +} + +export class Schema implements SchemaDefinition { + public static SCHEMA_DEFAULT?: Schema; + + public implicit: Type[]; + public explicit: Type[]; + public include: Schema[]; + + public compiledImplicit: Type[]; + public compiledExplicit: Type[]; + public compiledTypeMap: TypeMap; + + constructor(definition: SchemaDefinition) { + this.explicit = definition.explicit || []; + this.implicit = definition.implicit || []; + this.include = definition.include || []; + + for (const type of this.implicit) { + if (type.loadKind && type.loadKind !== "scalar") { + throw new YAMLError( + // eslint-disable-next-line max-len + "There is a non-scalar type in the implicit list of a schema. Implicit resolving of such types is not supported." + ); + } + } + + this.compiledImplicit = compileList(this, "implicit", []); + this.compiledExplicit = compileList(this, "explicit", []); + this.compiledTypeMap = compileMap( + this.compiledImplicit, + this.compiledExplicit + ); + } + + public static create(): void {} +} + +export interface SchemaDefinition { + implicit?: Any[]; + explicit?: Type[]; + include?: Schema[]; +} diff --git a/std/encoding/_yaml/schema/core.ts b/std/encoding/_yaml/schema/core.ts new file mode 100644 index 000000000..4fadc9bfe --- /dev/null +++ b/std/encoding/_yaml/schema/core.ts @@ -0,0 +1,13 @@ +// Ported from js-yaml v3.13.1: +// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da +// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +import { Schema } from "../schema.ts"; +import { json } from "./json.ts"; + +// Standard YAML's Core schema. +// http://www.yaml.org/spec/1.2/spec.html#id2804923 +export const core = new Schema({ + include: [json], +}); diff --git a/std/encoding/_yaml/schema/default.ts b/std/encoding/_yaml/schema/default.ts new file mode 100644 index 000000000..4c5ceeba7 --- /dev/null +++ b/std/encoding/_yaml/schema/default.ts @@ -0,0 +1,16 @@ +// Ported from js-yaml v3.13.1: +// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da +// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +import { Schema } from "../schema.ts"; +import { binary, merge, omap, pairs, set, timestamp } from "../type/mod.ts"; +import { core } from "./core.ts"; + +// JS-YAML's default schema for `safeLoad` function. +// It is not described in the YAML specification. +export const def = new Schema({ + explicit: [binary, omap, pairs, set], + implicit: [timestamp, merge], + include: [core], +}); diff --git a/std/encoding/_yaml/schema/failsafe.ts b/std/encoding/_yaml/schema/failsafe.ts new file mode 100644 index 000000000..74e1897be --- /dev/null +++ b/std/encoding/_yaml/schema/failsafe.ts @@ -0,0 +1,13 @@ +// Ported from js-yaml v3.13.1: +// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da +// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +import { Schema } from "../schema.ts"; +import { map, seq, str } from "../type/mod.ts"; + +// Standard YAML's Failsafe schema. +// http://www.yaml.org/spec/1.2/spec.html#id2802346 +export const failsafe = new Schema({ + explicit: [str, seq, map], +}); diff --git a/std/encoding/_yaml/schema/json.ts b/std/encoding/_yaml/schema/json.ts new file mode 100644 index 000000000..c30166fdf --- /dev/null +++ b/std/encoding/_yaml/schema/json.ts @@ -0,0 +1,15 @@ +// Ported from js-yaml v3.13.1: +// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da +// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +import { Schema } from "../schema.ts"; +import { bool, float, int, nil } from "../type/mod.ts"; +import { failsafe } from "./failsafe.ts"; + +// Standard YAML's JSON schema. +// http://www.yaml.org/spec/1.2/spec.html#id2803231 +export const json = new Schema({ + implicit: [nil, bool, int, float], + include: [failsafe], +}); diff --git a/std/encoding/_yaml/schema/mod.ts b/std/encoding/_yaml/schema/mod.ts new file mode 100644 index 000000000..7cbe0c283 --- /dev/null +++ b/std/encoding/_yaml/schema/mod.ts @@ -0,0 +1,9 @@ +// Ported from js-yaml v3.13.1: +// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da +// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +export { core as CORE_SCHEMA } from "./core.ts"; +export { def as DEFAULT_SCHEMA } from "./default.ts"; +export { failsafe as FAILSAFE_SCHEMA } from "./failsafe.ts"; +export { json as JSON_SCHEMA } from "./json.ts"; diff --git a/std/encoding/_yaml/state.ts b/std/encoding/_yaml/state.ts new file mode 100644 index 000000000..6df6dc047 --- /dev/null +++ b/std/encoding/_yaml/state.ts @@ -0,0 +1,11 @@ +// Ported from js-yaml v3.13.1: +// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da +// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +import { SchemaDefinition } from "./schema.ts"; +import { DEFAULT_SCHEMA } from "./schema/mod.ts"; + +export abstract class State { + constructor(public schema: SchemaDefinition = DEFAULT_SCHEMA) {} +} diff --git a/std/encoding/_yaml/stringify.ts b/std/encoding/_yaml/stringify.ts new file mode 100644 index 000000000..f037631d9 --- /dev/null +++ b/std/encoding/_yaml/stringify.ts @@ -0,0 +1,18 @@ +// Ported from js-yaml v3.13.1: +// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da +// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +import { dump } from "./dumper/dumper.ts"; +import { DumperStateOptions } from "./dumper/dumper_state.ts"; + +export type DumpOptions = DumperStateOptions; + +/** + * Serializes `object` as a YAML document. + * + * You can disable exceptions by setting the skipInvalid option to true. + */ +export function stringify(obj: object, options?: DumpOptions): string { + return dump(obj, options); +} diff --git a/std/encoding/_yaml/stringify_test.ts b/std/encoding/_yaml/stringify_test.ts new file mode 100644 index 000000000..03a3090d9 --- /dev/null +++ b/std/encoding/_yaml/stringify_test.ts @@ -0,0 +1,41 @@ +// Ported from js-yaml v3.13.1: +// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da +// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +import { assertEquals } from "../../testing/asserts.ts"; +import { stringify } from "./stringify.ts"; + +Deno.test({ + name: "stringified correctly", + fn(): void { + const FIXTURE = { + foo: { + bar: true, + test: [ + "a", + "b", + { + a: false, + }, + { + a: false, + }, + ], + }, + test: "foobar", + }; + + const ASSERTS = `foo: + bar: true + test: + - a + - b + - a: false + - a: false +test: foobar +`; + + assertEquals(stringify(FIXTURE), ASSERTS); + }, +}); diff --git a/std/encoding/_yaml/type.ts b/std/encoding/_yaml/type.ts new file mode 100644 index 000000000..4a2c6bbac --- /dev/null +++ b/std/encoding/_yaml/type.ts @@ -0,0 +1,55 @@ +// Ported from js-yaml v3.13.1: +// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da +// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +import { ArrayObject, Any } from "./utils.ts"; + +export type KindType = "sequence" | "scalar" | "mapping"; +export type StyleVariant = "lowercase" | "uppercase" | "camelcase" | "decimal"; +export type RepresentFn = (data: Any, style?: StyleVariant) => Any; + +const DEFAULT_RESOLVE = (): boolean => true; +const DEFAULT_CONSTRUCT = (data: Any): Any => data; + +interface TypeOptions { + kind: KindType; + resolve?: (data: Any) => boolean; + construct?: (data: string) => Any; + instanceOf?: Any; + predicate?: (data: object) => boolean; + represent?: RepresentFn | ArrayObject; + defaultStyle?: StyleVariant; + styleAliases?: ArrayObject; +} + +function checkTagFormat(tag: string): string { + return tag; +} + +export class Type { + public tag: string; + public kind: KindType | null = null; + public instanceOf: Any; + public predicate?: (data: object) => boolean; + public represent?: RepresentFn | ArrayObject; + public defaultStyle?: StyleVariant; + public styleAliases?: ArrayObject; + public loadKind?: KindType; + + constructor(tag: string, options?: TypeOptions) { + this.tag = checkTagFormat(tag); + if (options) { + this.kind = options.kind; + this.resolve = options.resolve || DEFAULT_RESOLVE; + this.construct = options.construct || DEFAULT_CONSTRUCT; + this.instanceOf = options.instanceOf; + this.predicate = options.predicate; + this.represent = options.represent; + this.defaultStyle = options.defaultStyle; + this.styleAliases = options.styleAliases; + } + } + public resolve: (data?: Any) => boolean = (): boolean => true; + public construct: (data?: Any) => Any = (data): Any => data; +} diff --git a/std/encoding/_yaml/type/binary.ts b/std/encoding/_yaml/type/binary.ts new file mode 100644 index 000000000..f4823b3f7 --- /dev/null +++ b/std/encoding/_yaml/type/binary.ts @@ -0,0 +1,139 @@ +// Ported from js-yaml v3.13.1: +// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. +// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +import { Type } from "../type.ts"; +import { Any } from "../utils.ts"; + +const { Buffer } = Deno; + +// [ 64, 65, 66 ] -> [ padding, CR, LF ] +const BASE64_MAP = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\n\r"; + +function resolveYamlBinary(data: Any): boolean { + if (data === null) return false; + + let code: number; + let bitlen = 0; + const max = data.length; + const map = BASE64_MAP; + + // Convert one by one. + for (let idx = 0; idx < max; idx++) { + code = map.indexOf(data.charAt(idx)); + + // Skip CR/LF + if (code > 64) continue; + + // Fail on illegal characters + if (code < 0) return false; + + bitlen += 6; + } + + // If there are any bits left, source was corrupted + return bitlen % 8 === 0; +} + +function constructYamlBinary(data: string): Deno.Buffer { + // remove CR/LF & padding to simplify scan + const input = data.replace(/[\r\n=]/g, ""); + const max = input.length; + const map = BASE64_MAP; + + // Collect by 6*4 bits (3 bytes) + + const result = []; + let bits = 0; + for (let idx = 0; idx < max; idx++) { + if (idx % 4 === 0 && idx) { + result.push((bits >> 16) & 0xff); + result.push((bits >> 8) & 0xff); + result.push(bits & 0xff); + } + + bits = (bits << 6) | map.indexOf(input.charAt(idx)); + } + + // Dump tail + + const tailbits = (max % 4) * 6; + + if (tailbits === 0) { + result.push((bits >> 16) & 0xff); + result.push((bits >> 8) & 0xff); + result.push(bits & 0xff); + } else if (tailbits === 18) { + result.push((bits >> 10) & 0xff); + result.push((bits >> 2) & 0xff); + } else if (tailbits === 12) { + result.push((bits >> 4) & 0xff); + } + + return new Buffer(new Uint8Array(result)); +} + +function representYamlBinary(object: Uint8Array): string { + const max = object.length; + const map = BASE64_MAP; + + // Convert every three bytes to 4 ASCII characters. + + let result = ""; + let bits = 0; + for (let idx = 0; idx < max; idx++) { + if (idx % 3 === 0 && idx) { + result += map[(bits >> 18) & 0x3f]; + result += map[(bits >> 12) & 0x3f]; + result += map[(bits >> 6) & 0x3f]; + result += map[bits & 0x3f]; + } + + bits = (bits << 8) + object[idx]; + } + + // Dump tail + + const tail = max % 3; + + if (tail === 0) { + result += map[(bits >> 18) & 0x3f]; + result += map[(bits >> 12) & 0x3f]; + result += map[(bits >> 6) & 0x3f]; + result += map[bits & 0x3f]; + } else if (tail === 2) { + result += map[(bits >> 10) & 0x3f]; + result += map[(bits >> 4) & 0x3f]; + result += map[(bits << 2) & 0x3f]; + result += map[64]; + } else if (tail === 1) { + result += map[(bits >> 2) & 0x3f]; + result += map[(bits << 4) & 0x3f]; + result += map[64]; + result += map[64]; + } + + return result; +} + +function isBinary(obj: Any): obj is Deno.Buffer { + const buf = new Buffer(); + try { + if (0 > buf.readFromSync(obj as Deno.Buffer)) return true; + return false; + } catch { + return false; + } finally { + buf.reset(); + } +} + +export const binary = new Type("tag:yaml.org,2002:binary", { + construct: constructYamlBinary, + kind: "scalar", + predicate: isBinary, + represent: representYamlBinary, + resolve: resolveYamlBinary, +}); diff --git a/std/encoding/_yaml/type/bool.ts b/std/encoding/_yaml/type/bool.ts new file mode 100644 index 000000000..a5a85cf9e --- /dev/null +++ b/std/encoding/_yaml/type/bool.ts @@ -0,0 +1,39 @@ +// Ported from js-yaml v3.13.1: +// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da +// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +import { Type } from "../type.ts"; +import { isBoolean } from "../utils.ts"; + +function resolveYamlBoolean(data: string): boolean { + const max = data.length; + + return ( + (max === 4 && (data === "true" || data === "True" || data === "TRUE")) || + (max === 5 && (data === "false" || data === "False" || data === "FALSE")) + ); +} + +function constructYamlBoolean(data: string): boolean { + return data === "true" || data === "True" || data === "TRUE"; +} + +export const bool = new Type("tag:yaml.org,2002:bool", { + construct: constructYamlBoolean, + defaultStyle: "lowercase", + kind: "scalar", + predicate: isBoolean, + represent: { + lowercase(object: boolean): string { + return object ? "true" : "false"; + }, + uppercase(object: boolean): string { + return object ? "TRUE" : "FALSE"; + }, + camelcase(object: boolean): string { + return object ? "True" : "False"; + }, + }, + resolve: resolveYamlBoolean, +}); diff --git a/std/encoding/_yaml/type/float.ts b/std/encoding/_yaml/type/float.ts new file mode 100644 index 000000000..5ae0689b2 --- /dev/null +++ b/std/encoding/_yaml/type/float.ts @@ -0,0 +1,125 @@ +// Ported from js-yaml v3.13.1: +// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da +// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +import { StyleVariant, Type } from "../type.ts"; +import { isNegativeZero, Any } from "../utils.ts"; + +const YAML_FLOAT_PATTERN = new RegExp( + // 2.5e4, 2.5 and integers + "^(?:[-+]?(?:0|[1-9][0-9_]*)(?:\\.[0-9_]*)?(?:[eE][-+]?[0-9]+)?" + + // .2e4, .2 + // special case, seems not from spec + "|\\.[0-9_]+(?:[eE][-+]?[0-9]+)?" + + // 20:59 + "|[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+\\.[0-9_]*" + + // .inf + "|[-+]?\\.(?:inf|Inf|INF)" + + // .nan + "|\\.(?:nan|NaN|NAN))$" +); + +function resolveYamlFloat(data: string): boolean { + if ( + !YAML_FLOAT_PATTERN.test(data) || + // Quick hack to not allow integers end with `_` + // Probably should update regexp & check speed + data[data.length - 1] === "_" + ) { + return false; + } + + return true; +} + +function constructYamlFloat(data: string): number { + let value = data.replace(/_/g, "").toLowerCase(); + const sign = value[0] === "-" ? -1 : 1; + const digits: number[] = []; + + if ("+-".indexOf(value[0]) >= 0) { + value = value.slice(1); + } + + if (value === ".inf") { + return sign === 1 ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY; + } + if (value === ".nan") { + return NaN; + } + if (value.indexOf(":") >= 0) { + value.split(":").forEach((v): void => { + digits.unshift(parseFloat(v)); + }); + + let valueNb = 0.0; + let base = 1; + + digits.forEach((d): void => { + valueNb += d * base; + base *= 60; + }); + + return sign * valueNb; + } + return sign * parseFloat(value); +} + +const SCIENTIFIC_WITHOUT_DOT = /^[-+]?[0-9]+e/; + +function representYamlFloat(object: Any, style?: StyleVariant): Any { + if (isNaN(object)) { + switch (style) { + case "lowercase": + return ".nan"; + case "uppercase": + return ".NAN"; + case "camelcase": + return ".NaN"; + } + } else if (Number.POSITIVE_INFINITY === object) { + switch (style) { + case "lowercase": + return ".inf"; + case "uppercase": + return ".INF"; + case "camelcase": + return ".Inf"; + } + } else if (Number.NEGATIVE_INFINITY === object) { + switch (style) { + case "lowercase": + return "-.inf"; + case "uppercase": + return "-.INF"; + case "camelcase": + return "-.Inf"; + } + } else if (isNegativeZero(object)) { + return "-0.0"; + } + + const res = object.toString(10); + + // JS stringifier can build scientific format without dots: 5e-100, + // while YAML requres dot: 5.e-100. Fix it with simple hack + + return SCIENTIFIC_WITHOUT_DOT.test(res) ? res.replace("e", ".e") : res; +} + +function isFloat(object: Any): boolean { + return ( + Object.prototype.toString.call(object) === "[object Number]" && + (object % 1 !== 0 || isNegativeZero(object)) + ); +} + +export const float = new Type("tag:yaml.org,2002:float", { + construct: constructYamlFloat, + defaultStyle: "lowercase", + kind: "scalar", + predicate: isFloat, + represent: representYamlFloat, + resolve: resolveYamlFloat, +}); diff --git a/std/encoding/_yaml/type/int.ts b/std/encoding/_yaml/type/int.ts new file mode 100644 index 000000000..6a86aafe9 --- /dev/null +++ b/std/encoding/_yaml/type/int.ts @@ -0,0 +1,188 @@ +// Ported from js-yaml v3.13.1: +// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da +// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +import { Type } from "../type.ts"; +import { isNegativeZero, Any } from "../utils.ts"; + +function isHexCode(c: number): boolean { + return ( + (0x30 <= /* 0 */ c && c <= 0x39) /* 9 */ || + (0x41 <= /* A */ c && c <= 0x46) /* F */ || + (0x61 <= /* a */ c && c <= 0x66) /* f */ + ); +} + +function isOctCode(c: number): boolean { + return 0x30 <= /* 0 */ c && c <= 0x37 /* 7 */; +} + +function isDecCode(c: number): boolean { + return 0x30 <= /* 0 */ c && c <= 0x39 /* 9 */; +} + +function resolveYamlInteger(data: string): boolean { + const max = data.length; + let index = 0; + let hasDigits = false; + + if (!max) return false; + + let ch = data[index]; + + // sign + if (ch === "-" || ch === "+") { + ch = data[++index]; + } + + if (ch === "0") { + // 0 + if (index + 1 === max) return true; + ch = data[++index]; + + // base 2, base 8, base 16 + + if (ch === "b") { + // base 2 + index++; + + for (; index < max; index++) { + ch = data[index]; + if (ch === "_") continue; + if (ch !== "0" && ch !== "1") return false; + hasDigits = true; + } + return hasDigits && ch !== "_"; + } + + if (ch === "x") { + // base 16 + index++; + + for (; index < max; index++) { + ch = data[index]; + if (ch === "_") continue; + if (!isHexCode(data.charCodeAt(index))) return false; + hasDigits = true; + } + return hasDigits && ch !== "_"; + } + + // base 8 + for (; index < max; index++) { + ch = data[index]; + if (ch === "_") continue; + if (!isOctCode(data.charCodeAt(index))) return false; + hasDigits = true; + } + return hasDigits && ch !== "_"; + } + + // base 10 (except 0) or base 60 + + // value should not start with `_`; + if (ch === "_") return false; + + for (; index < max; index++) { + ch = data[index]; + if (ch === "_") continue; + if (ch === ":") break; + if (!isDecCode(data.charCodeAt(index))) { + return false; + } + hasDigits = true; + } + + // Should have digits and should not end with `_` + if (!hasDigits || ch === "_") return false; + + // if !base60 - done; + if (ch !== ":") return true; + + // base60 almost not used, no needs to optimize + return /^(:[0-5]?[0-9])+$/.test(data.slice(index)); +} + +function constructYamlInteger(data: string): number { + let value = data; + const digits: number[] = []; + + if (value.indexOf("_") !== -1) { + value = value.replace(/_/g, ""); + } + + let sign = 1; + let ch = value[0]; + if (ch === "-" || ch === "+") { + if (ch === "-") sign = -1; + value = value.slice(1); + ch = value[0]; + } + + if (value === "0") return 0; + + if (ch === "0") { + if (value[1] === "b") return sign * parseInt(value.slice(2), 2); + if (value[1] === "x") return sign * parseInt(value, 16); + return sign * parseInt(value, 8); + } + + if (value.indexOf(":") !== -1) { + value.split(":").forEach((v): void => { + digits.unshift(parseInt(v, 10)); + }); + + let valueInt = 0; + let base = 1; + + digits.forEach((d): void => { + valueInt += d * base; + base *= 60; + }); + + return sign * valueInt; + } + + return sign * parseInt(value, 10); +} + +function isInteger(object: Any): boolean { + return ( + Object.prototype.toString.call(object) === "[object Number]" && + object % 1 === 0 && + !isNegativeZero(object) + ); +} + +export const int = new Type("tag:yaml.org,2002:int", { + construct: constructYamlInteger, + defaultStyle: "decimal", + kind: "scalar", + predicate: isInteger, + represent: { + binary(obj: number): string { + return obj >= 0 + ? `0b${obj.toString(2)}` + : `-0b${obj.toString(2).slice(1)}`; + }, + octal(obj: number): string { + return obj >= 0 ? `0${obj.toString(8)}` : `-0${obj.toString(8).slice(1)}`; + }, + decimal(obj: number): string { + return obj.toString(10); + }, + hexadecimal(obj: number): string { + return obj >= 0 + ? `0x${obj.toString(16).toUpperCase()}` + : `-0x${obj.toString(16).toUpperCase().slice(1)}`; + }, + }, + resolve: resolveYamlInteger, + styleAliases: { + binary: [2, "bin"], + decimal: [10, "dec"], + hexadecimal: [16, "hex"], + octal: [8, "oct"], + }, +}); diff --git a/std/encoding/_yaml/type/map.ts b/std/encoding/_yaml/type/map.ts new file mode 100644 index 000000000..dcd99abca --- /dev/null +++ b/std/encoding/_yaml/type/map.ts @@ -0,0 +1,14 @@ +// Ported from js-yaml v3.13.1: +// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da +// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +import { Type } from "../type.ts"; +import { Any } from "../utils.ts"; + +export const map = new Type("tag:yaml.org,2002:map", { + construct(data): Any { + return data !== null ? data : {}; + }, + kind: "mapping", +}); diff --git a/std/encoding/_yaml/type/merge.ts b/std/encoding/_yaml/type/merge.ts new file mode 100644 index 000000000..68314bf2e --- /dev/null +++ b/std/encoding/_yaml/type/merge.ts @@ -0,0 +1,15 @@ +// Ported from js-yaml v3.13.1: +// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da +// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +import { Type } from "../type.ts"; + +function resolveYamlMerge(data: string): boolean { + return data === "<<" || data === null; +} + +export const merge = new Type("tag:yaml.org,2002:merge", { + kind: "scalar", + resolve: resolveYamlMerge, +}); diff --git a/std/encoding/_yaml/type/mod.ts b/std/encoding/_yaml/type/mod.ts new file mode 100644 index 000000000..15f33301e --- /dev/null +++ b/std/encoding/_yaml/type/mod.ts @@ -0,0 +1,18 @@ +// Ported from js-yaml v3.13.1: +// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da +// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +export { binary } from "./binary.ts"; +export { bool } from "./bool.ts"; +export { float } from "./float.ts"; +export { int } from "./int.ts"; +export { map } from "./map.ts"; +export { merge } from "./merge.ts"; +export { nil } from "./nil.ts"; +export { omap } from "./omap.ts"; +export { pairs } from "./pairs.ts"; +export { seq } from "./seq.ts"; +export { set } from "./set.ts"; +export { str } from "./str.ts"; +export { timestamp } from "./timestamp.ts"; diff --git a/std/encoding/_yaml/type/nil.ts b/std/encoding/_yaml/type/nil.ts new file mode 100644 index 000000000..8a48d02fb --- /dev/null +++ b/std/encoding/_yaml/type/nil.ts @@ -0,0 +1,45 @@ +// Ported from js-yaml v3.13.1: +// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da +// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +import { Type } from "../type.ts"; + +function resolveYamlNull(data: string): boolean { + const max = data.length; + + return ( + (max === 1 && data === "~") || + (max === 4 && (data === "null" || data === "Null" || data === "NULL")) + ); +} + +function constructYamlNull(): null { + return null; +} + +function isNull(object: unknown): object is null { + return object === null; +} + +export const nil = new Type("tag:yaml.org,2002:null", { + construct: constructYamlNull, + defaultStyle: "lowercase", + kind: "scalar", + predicate: isNull, + represent: { + canonical(): string { + return "~"; + }, + lowercase(): string { + return "null"; + }, + uppercase(): string { + return "NULL"; + }, + camelcase(): string { + return "Null"; + }, + }, + resolve: resolveYamlNull, +}); diff --git a/std/encoding/_yaml/type/omap.ts b/std/encoding/_yaml/type/omap.ts new file mode 100644 index 000000000..d6d751505 --- /dev/null +++ b/std/encoding/_yaml/type/omap.ts @@ -0,0 +1,46 @@ +// Ported from js-yaml v3.13.1: +// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da +// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +import { Type } from "../type.ts"; +import { Any } from "../utils.ts"; + +const _hasOwnProperty = Object.prototype.hasOwnProperty; +const _toString = Object.prototype.toString; + +function resolveYamlOmap(data: Any): boolean { + const objectKeys: string[] = []; + let pairKey = ""; + let pairHasKey = false; + + for (const pair of data) { + pairHasKey = false; + + if (_toString.call(pair) !== "[object Object]") return false; + + for (pairKey in pair) { + if (_hasOwnProperty.call(pair, pairKey)) { + if (!pairHasKey) pairHasKey = true; + else return false; + } + } + + if (!pairHasKey) return false; + + if (objectKeys.indexOf(pairKey) === -1) objectKeys.push(pairKey); + else return false; + } + + return true; +} + +function constructYamlOmap(data: Any): Any { + return data !== null ? data : []; +} + +export const omap = new Type("tag:yaml.org,2002:omap", { + construct: constructYamlOmap, + kind: "sequence", + resolve: resolveYamlOmap, +}); diff --git a/std/encoding/_yaml/type/pairs.ts b/std/encoding/_yaml/type/pairs.ts new file mode 100644 index 000000000..e999748ae --- /dev/null +++ b/std/encoding/_yaml/type/pairs.ts @@ -0,0 +1,49 @@ +// Ported from js-yaml v3.13.1: +// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da +// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +import { Type } from "../type.ts"; +import { Any } from "../utils.ts"; + +const _toString = Object.prototype.toString; + +function resolveYamlPairs(data: Any[][]): boolean { + const result = new Array(data.length); + + for (let index = 0; index < data.length; index++) { + const pair = data[index]; + + if (_toString.call(pair) !== "[object Object]") return false; + + const keys = Object.keys(pair); + + if (keys.length !== 1) return false; + + result[index] = [keys[0], pair[keys[0] as Any]]; + } + + return true; +} + +function constructYamlPairs(data: string): Any[] { + if (data === null) return []; + + const result = new Array(data.length); + + for (let index = 0; index < data.length; index += 1) { + const pair = data[index]; + + const keys = Object.keys(pair); + + result[index] = [keys[0], pair[keys[0] as Any]]; + } + + return result; +} + +export const pairs = new Type("tag:yaml.org,2002:pairs", { + construct: constructYamlPairs, + kind: "sequence", + resolve: resolveYamlPairs, +}); diff --git a/std/encoding/_yaml/type/seq.ts b/std/encoding/_yaml/type/seq.ts new file mode 100644 index 000000000..b19565dbc --- /dev/null +++ b/std/encoding/_yaml/type/seq.ts @@ -0,0 +1,14 @@ +// Ported from js-yaml v3.13.1: +// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da +// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +import { Type } from "../type.ts"; +import { Any } from "../utils.ts"; + +export const seq = new Type("tag:yaml.org,2002:seq", { + construct(data): Any { + return data !== null ? data : []; + }, + kind: "sequence", +}); diff --git a/std/encoding/_yaml/type/set.ts b/std/encoding/_yaml/type/set.ts new file mode 100644 index 000000000..0bfe1c8db --- /dev/null +++ b/std/encoding/_yaml/type/set.ts @@ -0,0 +1,31 @@ +// Ported from js-yaml v3.13.1: +// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da +// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +import { Type } from "../type.ts"; +import { Any } from "../utils.ts"; + +const _hasOwnProperty = Object.prototype.hasOwnProperty; + +function resolveYamlSet(data: Any): boolean { + if (data === null) return true; + + for (const key in data) { + if (_hasOwnProperty.call(data, key)) { + if (data[key] !== null) return false; + } + } + + return true; +} + +function constructYamlSet(data: string): Any { + return data !== null ? data : {}; +} + +export const set = new Type("tag:yaml.org,2002:set", { + construct: constructYamlSet, + kind: "mapping", + resolve: resolveYamlSet, +}); diff --git a/std/encoding/_yaml/type/str.ts b/std/encoding/_yaml/type/str.ts new file mode 100644 index 000000000..cd6e9430f --- /dev/null +++ b/std/encoding/_yaml/type/str.ts @@ -0,0 +1,12 @@ +// Ported from js-yaml v3.13.1: +// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +import { Type } from "../type.ts"; + +export const str = new Type("tag:yaml.org,2002:str", { + construct(data): string { + return data !== null ? data : ""; + }, + kind: "scalar", +}); diff --git a/std/encoding/_yaml/type/timestamp.ts b/std/encoding/_yaml/type/timestamp.ts new file mode 100644 index 000000000..eb03b3825 --- /dev/null +++ b/std/encoding/_yaml/type/timestamp.ts @@ -0,0 +1,96 @@ +// Ported from js-yaml v3.13.1: +// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da +// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +import { Type } from "../type.ts"; + +const YAML_DATE_REGEXP = new RegExp( + "^([0-9][0-9][0-9][0-9])" + // [1] year + "-([0-9][0-9])" + // [2] month + "-([0-9][0-9])$" // [3] day +); + +const YAML_TIMESTAMP_REGEXP = new RegExp( + "^([0-9][0-9][0-9][0-9])" + // [1] year + "-([0-9][0-9]?)" + // [2] month + "-([0-9][0-9]?)" + // [3] day + "(?:[Tt]|[ \\t]+)" + // ... + "([0-9][0-9]?)" + // [4] hour + ":([0-9][0-9])" + // [5] minute + ":([0-9][0-9])" + // [6] second + "(?:\\.([0-9]*))?" + // [7] fraction + "(?:[ \\t]*(Z|([-+])([0-9][0-9]?)" + // [8] tz [9] tz_sign [10] tz_hour + "(?::([0-9][0-9]))?))?$" // [11] tz_minute +); + +function resolveYamlTimestamp(data: string): boolean { + if (data === null) return false; + if (YAML_DATE_REGEXP.exec(data) !== null) return true; + if (YAML_TIMESTAMP_REGEXP.exec(data) !== null) return true; + return false; +} + +function constructYamlTimestamp(data: string): Date { + let match = YAML_DATE_REGEXP.exec(data); + if (match === null) match = YAML_TIMESTAMP_REGEXP.exec(data); + + if (match === null) throw new Error("Date resolve error"); + + // match: [1] year [2] month [3] day + + const year = +match[1]; + const month = +match[2] - 1; // JS month starts with 0 + const day = +match[3]; + + if (!match[4]) { + // no hour + return new Date(Date.UTC(year, month, day)); + } + + // match: [4] hour [5] minute [6] second [7] fraction + + const hour = +match[4]; + const minute = +match[5]; + const second = +match[6]; + + let fraction = 0; + if (match[7]) { + let partFraction = match[7].slice(0, 3); + while (partFraction.length < 3) { + // milli-seconds + partFraction += "0"; + } + fraction = +partFraction; + } + + // match: [8] tz [9] tz_sign [10] tz_hour [11] tz_minute + + let delta = null; + if (match[9]) { + const tzHour = +match[10]; + const tzMinute = +(match[11] || 0); + delta = (tzHour * 60 + tzMinute) * 60000; // delta in mili-seconds + if (match[9] === "-") delta = -delta; + } + + const date = new Date( + Date.UTC(year, month, day, hour, minute, second, fraction) + ); + + if (delta) date.setTime(date.getTime() - delta); + + return date; +} + +function representYamlTimestamp(date: Date): string { + return date.toISOString(); +} + +export const timestamp = new Type("tag:yaml.org,2002:timestamp", { + construct: constructYamlTimestamp, + instanceOf: Date, + kind: "scalar", + represent: representYamlTimestamp, + resolve: resolveYamlTimestamp, +}); diff --git a/std/encoding/_yaml/utils.ts b/std/encoding/_yaml/utils.ts new file mode 100644 index 000000000..4630a45a2 --- /dev/null +++ b/std/encoding/_yaml/utils.ts @@ -0,0 +1,80 @@ +// Ported from js-yaml v3.13.1: +// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da +// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +/* eslint-disable-next-line @typescript-eslint/no-explicit-any */ +export type Any = any; + +export function isNothing(subject: unknown): subject is never { + return typeof subject === "undefined" || subject === null; +} + +export function isArray(value: unknown): value is Any[] { + return Array.isArray(value); +} + +export function isBoolean(value: unknown): value is boolean { + return typeof value === "boolean" || value instanceof Boolean; +} + +export function isNull(value: unknown): value is null { + return value === null; +} + +export function isNumber(value: unknown): value is number { + return typeof value === "number" || value instanceof Number; +} + +export function isString(value: unknown): value is string { + return typeof value === "string" || value instanceof String; +} + +export function isSymbol(value: unknown): value is symbol { + return typeof value === "symbol"; +} + +export function isUndefined(value: unknown): value is undefined { + return value === undefined; +} + +export function isObject(value: unknown): value is object { + return value !== null && typeof value === "object"; +} + +export function isError(e: unknown): boolean { + return e instanceof Error; +} + +export function isFunction(value: unknown): value is () => void { + return typeof value === "function"; +} + +export function isRegExp(value: unknown): value is RegExp { + return value instanceof RegExp; +} + +export function toArray(sequence: T): T | [] | [T] { + if (isArray(sequence)) return sequence; + if (isNothing(sequence)) return []; + + return [sequence]; +} + +export function repeat(str: string, count: number): string { + let result = ""; + + for (let cycle = 0; cycle < count; cycle++) { + result += str; + } + + return result; +} + +export function isNegativeZero(i: number): boolean { + return i === 0 && Number.NEGATIVE_INFINITY === 1 / i; +} + +export interface ArrayObject { + [P: string]: T; +} diff --git a/std/encoding/toml.ts b/std/encoding/toml.ts index ce4519572..7e44bdc18 100644 --- a/std/encoding/toml.ts +++ b/std/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/std/encoding/yaml.ts b/std/encoding/yaml.ts index 76b1b8379..abe210d85 100644 --- a/std/encoding/yaml.ts +++ b/std/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/std/encoding/yaml/dumper/dumper.ts b/std/encoding/yaml/dumper/dumper.ts deleted file mode 100644 index 1280ee757..000000000 --- a/std/encoding/yaml/dumper/dumper.ts +++ /dev/null @@ -1,899 +0,0 @@ -// Ported from js-yaml v3.13.1: -// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da -// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. - -/* eslint-disable max-len */ - -import { YAMLError } from "../error.ts"; -import { RepresentFn, StyleVariant, Type } from "../type.ts"; -import * as common from "../utils.ts"; -import { DumperState, DumperStateOptions } from "./dumper_state.ts"; - -type Any = common.Any; -type ArrayObject = common.ArrayObject; - -const _toString = Object.prototype.toString; -const _hasOwnProperty = Object.prototype.hasOwnProperty; - -const CHAR_TAB = 0x09; /* Tab */ -const CHAR_LINE_FEED = 0x0a; /* LF */ -const CHAR_SPACE = 0x20; /* Space */ -const CHAR_EXCLAMATION = 0x21; /* ! */ -const CHAR_DOUBLE_QUOTE = 0x22; /* " */ -const CHAR_SHARP = 0x23; /* # */ -const CHAR_PERCENT = 0x25; /* % */ -const CHAR_AMPERSAND = 0x26; /* & */ -const CHAR_SINGLE_QUOTE = 0x27; /* ' */ -const CHAR_ASTERISK = 0x2a; /* * */ -const CHAR_COMMA = 0x2c; /* , */ -const CHAR_MINUS = 0x2d; /* - */ -const CHAR_COLON = 0x3a; /* : */ -const CHAR_GREATER_THAN = 0x3e; /* > */ -const CHAR_QUESTION = 0x3f; /* ? */ -const CHAR_COMMERCIAL_AT = 0x40; /* @ */ -const CHAR_LEFT_SQUARE_BRACKET = 0x5b; /* [ */ -const CHAR_RIGHT_SQUARE_BRACKET = 0x5d; /* ] */ -const CHAR_GRAVE_ACCENT = 0x60; /* ` */ -const CHAR_LEFT_CURLY_BRACKET = 0x7b; /* { */ -const CHAR_VERTICAL_LINE = 0x7c; /* | */ -const CHAR_RIGHT_CURLY_BRACKET = 0x7d; /* } */ - -const ESCAPE_SEQUENCES: { [char: number]: string } = {}; - -ESCAPE_SEQUENCES[0x00] = "\\0"; -ESCAPE_SEQUENCES[0x07] = "\\a"; -ESCAPE_SEQUENCES[0x08] = "\\b"; -ESCAPE_SEQUENCES[0x09] = "\\t"; -ESCAPE_SEQUENCES[0x0a] = "\\n"; -ESCAPE_SEQUENCES[0x0b] = "\\v"; -ESCAPE_SEQUENCES[0x0c] = "\\f"; -ESCAPE_SEQUENCES[0x0d] = "\\r"; -ESCAPE_SEQUENCES[0x1b] = "\\e"; -ESCAPE_SEQUENCES[0x22] = '\\"'; -ESCAPE_SEQUENCES[0x5c] = "\\\\"; -ESCAPE_SEQUENCES[0x85] = "\\N"; -ESCAPE_SEQUENCES[0xa0] = "\\_"; -ESCAPE_SEQUENCES[0x2028] = "\\L"; -ESCAPE_SEQUENCES[0x2029] = "\\P"; - -const DEPRECATED_BOOLEANS_SYNTAX = [ - "y", - "Y", - "yes", - "Yes", - "YES", - "on", - "On", - "ON", - "n", - "N", - "no", - "No", - "NO", - "off", - "Off", - "OFF", -]; - -function encodeHex(character: number): string { - const string = character.toString(16).toUpperCase(); - - let handle: string; - let length: number; - if (character <= 0xff) { - handle = "x"; - length = 2; - } else if (character <= 0xffff) { - handle = "u"; - length = 4; - } else if (character <= 0xffffffff) { - handle = "U"; - length = 8; - } else { - throw new YAMLError( - "code point within a string may not be greater than 0xFFFFFFFF" - ); - } - - return `\\${handle}${common.repeat("0", length - string.length)}${string}`; -} - -// Indents every line in a string. Empty lines (\n only) are not indented. -function indentString(string: string, spaces: number): string { - const ind = common.repeat(" ", spaces), - length = string.length; - let position = 0, - next = -1, - result = "", - line: string; - - while (position < length) { - next = string.indexOf("\n", position); - if (next === -1) { - line = string.slice(position); - position = length; - } else { - line = string.slice(position, next + 1); - position = next + 1; - } - - if (line.length && line !== "\n") result += ind; - - result += line; - } - - return result; -} - -function generateNextLine(state: DumperState, level: number): string { - return `\n${common.repeat(" ", state.indent * level)}`; -} - -function testImplicitResolving(state: DumperState, str: string): boolean { - let type: Type; - for ( - let index = 0, length = state.implicitTypes.length; - index < length; - index += 1 - ) { - type = state.implicitTypes[index]; - - if (type.resolve(str)) { - return true; - } - } - - return false; -} - -// [33] s-white ::= s-space | s-tab -function isWhitespace(c: number): boolean { - return c === CHAR_SPACE || c === CHAR_TAB; -} - -// Returns true if the character can be printed without escaping. -// From YAML 1.2: "any allowed characters known to be non-printable -// should also be escaped. [However,] This isn’t mandatory" -// Derived from nb-char - \t - #x85 - #xA0 - #x2028 - #x2029. -function isPrintable(c: number): boolean { - return ( - (0x00020 <= c && c <= 0x00007e) || - (0x000a1 <= c && c <= 0x00d7ff && c !== 0x2028 && c !== 0x2029) || - (0x0e000 <= c && c <= 0x00fffd && c !== 0xfeff) /* BOM */ || - (0x10000 <= c && c <= 0x10ffff) - ); -} - -// Simplified test for values allowed after the first character in plain style. -function isPlainSafe(c: number): boolean { - // Uses a subset of nb-char - c-flow-indicator - ":" - "#" - // where nb-char ::= c-printable - b-char - c-byte-order-mark. - return ( - isPrintable(c) && - c !== 0xfeff && - // - c-flow-indicator - c !== CHAR_COMMA && - c !== CHAR_LEFT_SQUARE_BRACKET && - c !== CHAR_RIGHT_SQUARE_BRACKET && - c !== CHAR_LEFT_CURLY_BRACKET && - c !== CHAR_RIGHT_CURLY_BRACKET && - // - ":" - "#" - c !== CHAR_COLON && - c !== CHAR_SHARP - ); -} - -// Simplified test for values allowed as the first character in plain style. -function isPlainSafeFirst(c: number): boolean { - // Uses a subset of ns-char - c-indicator - // where ns-char = nb-char - s-white. - return ( - isPrintable(c) && - c !== 0xfeff && - !isWhitespace(c) && // - s-white - // - (c-indicator ::= - // “-” | “?” | “:” | “,” | “[” | “]” | “{” | “}” - c !== CHAR_MINUS && - c !== CHAR_QUESTION && - c !== CHAR_COLON && - c !== CHAR_COMMA && - c !== CHAR_LEFT_SQUARE_BRACKET && - c !== CHAR_RIGHT_SQUARE_BRACKET && - c !== CHAR_LEFT_CURLY_BRACKET && - c !== CHAR_RIGHT_CURLY_BRACKET && - // | “#” | “&” | “*” | “!” | “|” | “>” | “'” | “"” - c !== CHAR_SHARP && - c !== CHAR_AMPERSAND && - c !== CHAR_ASTERISK && - c !== CHAR_EXCLAMATION && - c !== CHAR_VERTICAL_LINE && - c !== CHAR_GREATER_THAN && - c !== CHAR_SINGLE_QUOTE && - c !== CHAR_DOUBLE_QUOTE && - // | “%” | “@” | “`”) - c !== CHAR_PERCENT && - c !== CHAR_COMMERCIAL_AT && - c !== CHAR_GRAVE_ACCENT - ); -} - -// Determines whether block indentation indicator is required. -function needIndentIndicator(string: string): boolean { - const leadingSpaceRe = /^\n* /; - return leadingSpaceRe.test(string); -} - -const STYLE_PLAIN = 1, - STYLE_SINGLE = 2, - STYLE_LITERAL = 3, - STYLE_FOLDED = 4, - STYLE_DOUBLE = 5; - -// Determines which scalar styles are possible and returns the preferred style. -// lineWidth = -1 => no limit. -// Pre-conditions: str.length > 0. -// Post-conditions: -// STYLE_PLAIN or STYLE_SINGLE => no \n are in the string. -// STYLE_LITERAL => no lines are suitable for folding (or lineWidth is -1). -// STYLE_FOLDED => a line > lineWidth and can be folded (and lineWidth != -1). -function chooseScalarStyle( - string: string, - singleLineOnly: boolean, - indentPerLevel: number, - lineWidth: number, - testAmbiguousType: (...args: Any[]) => Any -): number { - const shouldTrackWidth = lineWidth !== -1; - let hasLineBreak = false, - hasFoldableLine = false, // only checked if shouldTrackWidth - previousLineBreak = -1, // count the first line correctly - plain = - isPlainSafeFirst(string.charCodeAt(0)) && - !isWhitespace(string.charCodeAt(string.length - 1)); - - let char: number, i: number; - if (singleLineOnly) { - // Case: no block styles. - // Check for disallowed characters to rule out plain and single. - for (i = 0; i < string.length; i++) { - char = string.charCodeAt(i); - if (!isPrintable(char)) { - return STYLE_DOUBLE; - } - plain = plain && isPlainSafe(char); - } - } else { - // Case: block styles permitted. - for (i = 0; i < string.length; i++) { - char = string.charCodeAt(i); - if (char === CHAR_LINE_FEED) { - hasLineBreak = true; - // Check if any line can be folded. - if (shouldTrackWidth) { - hasFoldableLine = - hasFoldableLine || - // Foldable line = too long, and not more-indented. - (i - previousLineBreak - 1 > lineWidth && - string[previousLineBreak + 1] !== " "); - previousLineBreak = i; - } - } else if (!isPrintable(char)) { - return STYLE_DOUBLE; - } - plain = plain && isPlainSafe(char); - } - // in case the end is missing a \n - hasFoldableLine = - hasFoldableLine || - (shouldTrackWidth && - i - previousLineBreak - 1 > lineWidth && - string[previousLineBreak + 1] !== " "); - } - // Although every style can represent \n without escaping, prefer block styles - // for multiline, since they're more readable and they don't add empty lines. - // Also prefer folding a super-long line. - if (!hasLineBreak && !hasFoldableLine) { - // Strings interpretable as another type have to be quoted; - // e.g. the string 'true' vs. the boolean true. - return plain && !testAmbiguousType(string) ? STYLE_PLAIN : STYLE_SINGLE; - } - // Edge case: block indentation indicator can only have one digit. - if (indentPerLevel > 9 && needIndentIndicator(string)) { - return STYLE_DOUBLE; - } - // At this point we know block styles are valid. - // Prefer literal style unless we want to fold. - return hasFoldableLine ? STYLE_FOLDED : STYLE_LITERAL; -} - -// Greedy line breaking. -// Picks the longest line under the limit each time, -// otherwise settles for the shortest line over the limit. -// NB. More-indented lines *cannot* be folded, as that would add an extra \n. -function foldLine(line: string, width: number): string { - if (line === "" || line[0] === " ") return line; - - // Since a more-indented line adds a \n, breaks can't be followed by a space. - const breakRe = / [^ ]/g; // note: the match index will always be <= length-2. - let match; - // start is an inclusive index. end, curr, and next are exclusive. - let start = 0, - end, - curr = 0, - next = 0; - let result = ""; - - // Invariants: 0 <= start <= length-1. - // 0 <= curr <= next <= max(0, length-2). curr - start <= width. - // Inside the loop: - // A match implies length >= 2, so curr and next are <= length-2. - // tslint:disable-next-line:no-conditional-assignment - while ((match = breakRe.exec(line))) { - next = match.index; - // maintain invariant: curr - start <= width - if (next - start > width) { - end = curr > start ? curr : next; // derive end <= length-2 - result += `\n${line.slice(start, end)}`; - // skip the space that was output as \n - start = end + 1; // derive start <= length-1 - } - curr = next; - } - - // By the invariants, start <= length-1, so there is something left over. - // It is either the whole string or a part starting from non-whitespace. - result += "\n"; - // Insert a break if the remainder is too long and there is a break available. - if (line.length - start > width && curr > start) { - result += `${line.slice(start, curr)}\n${line.slice(curr + 1)}`; - } else { - result += line.slice(start); - } - - return result.slice(1); // drop extra \n joiner -} - -// (See the note for writeScalar.) -function dropEndingNewline(string: string): string { - return string[string.length - 1] === "\n" ? string.slice(0, -1) : string; -} - -// Note: a long line without a suitable break point will exceed the width limit. -// Pre-conditions: every char in str isPrintable, str.length > 0, width > 0. -function foldString(string: string, width: number): string { - // In folded style, $k$ consecutive newlines output as $k+1$ newlines— - // unless they're before or after a more-indented line, or at the very - // beginning or end, in which case $k$ maps to $k$. - // Therefore, parse each chunk as newline(s) followed by a content line. - const lineRe = /(\n+)([^\n]*)/g; - - // first line (possibly an empty line) - let result = ((): string => { - let nextLF = string.indexOf("\n"); - nextLF = nextLF !== -1 ? nextLF : string.length; - lineRe.lastIndex = nextLF; - // eslint-disable-next-line @typescript-eslint/no-use-before-define - return foldLine(string.slice(0, nextLF), width); - })(); - // If we haven't reached the first content line yet, don't add an extra \n. - let prevMoreIndented = string[0] === "\n" || string[0] === " "; - let moreIndented; - - // rest of the lines - let match; - // tslint:disable-next-line:no-conditional-assignment - while ((match = lineRe.exec(string))) { - const prefix = match[1], - line = match[2]; - moreIndented = line[0] === " "; - result += - prefix + - (!prevMoreIndented && !moreIndented && line !== "" ? "\n" : "") + - // eslint-disable-next-line @typescript-eslint/no-use-before-define - foldLine(line, width); - prevMoreIndented = moreIndented; - } - - return result; -} - -// Escapes a double-quoted string. -function escapeString(string: string): string { - let result = ""; - let char, nextChar; - let escapeSeq; - - for (let i = 0; i < string.length; i++) { - char = string.charCodeAt(i); - // Check for surrogate pairs (reference Unicode 3.0 section "3.7 Surrogates"). - if (char >= 0xd800 && char <= 0xdbff /* high surrogate */) { - nextChar = string.charCodeAt(i + 1); - if (nextChar >= 0xdc00 && nextChar <= 0xdfff /* low surrogate */) { - // Combine the surrogate pair and store it escaped. - result += encodeHex( - (char - 0xd800) * 0x400 + nextChar - 0xdc00 + 0x10000 - ); - // Advance index one extra since we already used that char here. - i++; - continue; - } - } - escapeSeq = ESCAPE_SEQUENCES[char]; - result += - !escapeSeq && isPrintable(char) - ? string[i] - : escapeSeq || encodeHex(char); - } - - return result; -} - -// Pre-conditions: string is valid for a block scalar, 1 <= indentPerLevel <= 9. -function blockHeader(string: string, indentPerLevel: number): string { - const indentIndicator = needIndentIndicator(string) - ? String(indentPerLevel) - : ""; - - // note the special case: the string '\n' counts as a "trailing" empty line. - const clip = string[string.length - 1] === "\n"; - const keep = clip && (string[string.length - 2] === "\n" || string === "\n"); - const chomp = keep ? "+" : clip ? "" : "-"; - - return `${indentIndicator}${chomp}\n`; -} - -// Note: line breaking/folding is implemented for only the folded style. -// NB. We drop the last trailing newline (if any) of a returned block scalar -// since the dumper adds its own newline. This always works: -// • No ending newline => unaffected; already using strip "-" chomping. -// • Ending newline => removed then restored. -// Importantly, this keeps the "+" chomp indicator from gaining an extra line. -function writeScalar( - state: DumperState, - string: string, - level: number, - iskey: boolean -): void { - state.dump = ((): string => { - if (string.length === 0) { - return "''"; - } - if ( - !state.noCompatMode && - DEPRECATED_BOOLEANS_SYNTAX.indexOf(string) !== -1 - ) { - return `'${string}'`; - } - - const indent = state.indent * Math.max(1, level); // no 0-indent scalars - // As indentation gets deeper, let the width decrease monotonically - // to the lower bound min(state.lineWidth, 40). - // Note that this implies - // state.lineWidth ≤ 40 + state.indent: width is fixed at the lower bound. - // state.lineWidth > 40 + state.indent: width decreases until the lower - // bound. - // This behaves better than a constant minimum width which disallows - // narrower options, or an indent threshold which causes the width - // to suddenly increase. - const lineWidth = - state.lineWidth === -1 - ? -1 - : Math.max(Math.min(state.lineWidth, 40), state.lineWidth - indent); - - // Without knowing if keys are implicit/explicit, - // assume implicit for safety. - const singleLineOnly = - iskey || - // No block styles in flow mode. - (state.flowLevel > -1 && level >= state.flowLevel); - function testAmbiguity(str: string): boolean { - return testImplicitResolving(state, str); - } - - switch ( - chooseScalarStyle( - string, - singleLineOnly, - state.indent, - lineWidth, - testAmbiguity - ) - ) { - case STYLE_PLAIN: - return string; - case STYLE_SINGLE: - return `'${string.replace(/'/g, "''")}'`; - case STYLE_LITERAL: - return `|${blockHeader(string, state.indent)}${dropEndingNewline( - indentString(string, indent) - )}`; - case STYLE_FOLDED: - return `>${blockHeader(string, state.indent)}${dropEndingNewline( - indentString(foldString(string, lineWidth), indent) - )}`; - case STYLE_DOUBLE: - return `"${escapeString(string)}"`; - default: - throw new YAMLError("impossible error: invalid scalar style"); - } - })(); -} - -function writeFlowSequence( - state: DumperState, - level: number, - object: Any -): void { - let _result = ""; - const _tag = state.tag; - - for (let index = 0, length = object.length; index < length; index += 1) { - // Write only valid elements. - // eslint-disable-next-line @typescript-eslint/no-use-before-define - if (writeNode(state, level, object[index], false, false)) { - if (index !== 0) _result += `,${!state.condenseFlow ? " " : ""}`; - _result += state.dump; - } - } - - state.tag = _tag; - state.dump = `[${_result}]`; -} - -function writeBlockSequence( - state: DumperState, - level: number, - object: Any, - compact = false -): void { - let _result = ""; - const _tag = state.tag; - - for (let index = 0, length = object.length; index < length; index += 1) { - // Write only valid elements. - // eslint-disable-next-line @typescript-eslint/no-use-before-define - if (writeNode(state, level + 1, object[index], true, true)) { - if (!compact || index !== 0) { - _result += generateNextLine(state, level); - } - - if (state.dump && CHAR_LINE_FEED === state.dump.charCodeAt(0)) { - _result += "-"; - } else { - _result += "- "; - } - - _result += state.dump; - } - } - - state.tag = _tag; - state.dump = _result || "[]"; // Empty sequence if no valid values. -} - -function writeFlowMapping( - state: DumperState, - level: number, - object: Any -): void { - let _result = ""; - const _tag = state.tag, - objectKeyList = Object.keys(object); - - let pairBuffer: string, objectKey: string, objectValue: Any; - for ( - let index = 0, length = objectKeyList.length; - index < length; - index += 1 - ) { - pairBuffer = state.condenseFlow ? '"' : ""; - - if (index !== 0) pairBuffer += ", "; - - objectKey = objectKeyList[index]; - objectValue = object[objectKey]; - - // eslint-disable-next-line @typescript-eslint/no-use-before-define - if (!writeNode(state, level, objectKey, false, false)) { - continue; // Skip this pair because of invalid key; - } - - if (state.dump.length > 1024) pairBuffer += "? "; - - pairBuffer += `${state.dump}${state.condenseFlow ? '"' : ""}:${ - state.condenseFlow ? "" : " " - }`; - - // eslint-disable-next-line @typescript-eslint/no-use-before-define - if (!writeNode(state, level, objectValue, false, false)) { - continue; // Skip this pair because of invalid value. - } - - pairBuffer += state.dump; - - // Both key and value are valid. - _result += pairBuffer; - } - - state.tag = _tag; - state.dump = `{${_result}}`; -} - -function writeBlockMapping( - state: DumperState, - level: number, - object: Any, - compact = false -): void { - const _tag = state.tag, - objectKeyList = Object.keys(object); - let _result = ""; - - // Allow sorting keys so that the output file is deterministic - if (state.sortKeys === true) { - // Default sorting - objectKeyList.sort(); - } else if (typeof state.sortKeys === "function") { - // Custom sort function - objectKeyList.sort(state.sortKeys); - } else if (state.sortKeys) { - // Something is wrong - throw new YAMLError("sortKeys must be a boolean or a function"); - } - - let pairBuffer = "", - objectKey: string, - objectValue: Any, - explicitPair: boolean; - for ( - let index = 0, length = objectKeyList.length; - index < length; - index += 1 - ) { - pairBuffer = ""; - - if (!compact || index !== 0) { - pairBuffer += generateNextLine(state, level); - } - - objectKey = objectKeyList[index]; - objectValue = object[objectKey]; - - // eslint-disable-next-line @typescript-eslint/no-use-before-define - if (!writeNode(state, level + 1, objectKey, true, true, true)) { - continue; // Skip this pair because of invalid key. - } - - explicitPair = - (state.tag !== null && state.tag !== "?") || - (state.dump && state.dump.length > 1024); - - if (explicitPair) { - if (state.dump && CHAR_LINE_FEED === state.dump.charCodeAt(0)) { - pairBuffer += "?"; - } else { - pairBuffer += "? "; - } - } - - pairBuffer += state.dump; - - if (explicitPair) { - pairBuffer += generateNextLine(state, level); - } - - // eslint-disable-next-line @typescript-eslint/no-use-before-define - if (!writeNode(state, level + 1, objectValue, true, explicitPair)) { - continue; // Skip this pair because of invalid value. - } - - if (state.dump && CHAR_LINE_FEED === state.dump.charCodeAt(0)) { - pairBuffer += ":"; - } else { - pairBuffer += ": "; - } - - pairBuffer += state.dump; - - // Both key and value are valid. - _result += pairBuffer; - } - - state.tag = _tag; - state.dump = _result || "{}"; // Empty mapping if no valid pairs. -} - -function detectType( - state: DumperState, - object: Any, - explicit = false -): boolean { - const typeList = explicit ? state.explicitTypes : state.implicitTypes; - - let type: Type; - let style: StyleVariant; - let _result: string; - for (let index = 0, length = typeList.length; index < length; index += 1) { - type = typeList[index]; - - if ( - (type.instanceOf || type.predicate) && - (!type.instanceOf || - (typeof object === "object" && object instanceof type.instanceOf)) && - (!type.predicate || type.predicate(object)) - ) { - state.tag = explicit ? type.tag : "?"; - - if (type.represent) { - style = state.styleMap[type.tag] || type.defaultStyle; - - if (_toString.call(type.represent) === "[object Function]") { - _result = (type.represent as RepresentFn)(object, style); - } else if (_hasOwnProperty.call(type.represent, style)) { - _result = (type.represent as ArrayObject)[style]( - object, - style - ); - } else { - throw new YAMLError( - `!<${type.tag}> tag resolver accepts not "${style}" style` - ); - } - - state.dump = _result; - } - - return true; - } - } - - return false; -} - -// Serializes `object` and writes it to global `result`. -// Returns true on success, or false on invalid object. -// -function writeNode( - state: DumperState, - level: number, - object: Any, - block: boolean, - compact: boolean, - iskey = false -): boolean { - state.tag = null; - state.dump = object; - - if (!detectType(state, object, false)) { - detectType(state, object, true); - } - - const type = _toString.call(state.dump); - - if (block) { - block = state.flowLevel < 0 || state.flowLevel > level; - } - - const objectOrArray = type === "[object Object]" || type === "[object Array]"; - - let duplicateIndex = -1; - let duplicate = false; - if (objectOrArray) { - duplicateIndex = state.duplicates.indexOf(object); - duplicate = duplicateIndex !== -1; - } - - if ( - (state.tag !== null && state.tag !== "?") || - duplicate || - (state.indent !== 2 && level > 0) - ) { - compact = false; - } - - if (duplicate && state.usedDuplicates[duplicateIndex]) { - state.dump = `*ref_${duplicateIndex}`; - } else { - if (objectOrArray && duplicate && !state.usedDuplicates[duplicateIndex]) { - state.usedDuplicates[duplicateIndex] = true; - } - if (type === "[object Object]") { - if (block && Object.keys(state.dump).length !== 0) { - writeBlockMapping(state, level, state.dump, compact); - if (duplicate) { - state.dump = `&ref_${duplicateIndex}${state.dump}`; - } - } else { - writeFlowMapping(state, level, state.dump); - if (duplicate) { - state.dump = `&ref_${duplicateIndex} ${state.dump}`; - } - } - } else if (type === "[object Array]") { - const arrayLevel = state.noArrayIndent && level > 0 ? level - 1 : level; - if (block && state.dump.length !== 0) { - writeBlockSequence(state, arrayLevel, state.dump, compact); - if (duplicate) { - state.dump = `&ref_${duplicateIndex}${state.dump}`; - } - } else { - writeFlowSequence(state, arrayLevel, state.dump); - if (duplicate) { - state.dump = `&ref_${duplicateIndex} ${state.dump}`; - } - } - } else if (type === "[object String]") { - if (state.tag !== "?") { - writeScalar(state, state.dump, level, iskey); - } - } else { - if (state.skipInvalid) return false; - throw new YAMLError(`unacceptable kind of an object to dump ${type}`); - } - - if (state.tag !== null && state.tag !== "?") { - state.dump = `!<${state.tag}> ${state.dump}`; - } - } - - return true; -} - -function inspectNode( - object: Any, - objects: Any[], - duplicatesIndexes: number[] -): void { - if (object !== null && typeof object === "object") { - const index = objects.indexOf(object); - if (index !== -1) { - if (duplicatesIndexes.indexOf(index) === -1) { - duplicatesIndexes.push(index); - } - } else { - objects.push(object); - - if (Array.isArray(object)) { - for (let idx = 0, length = object.length; idx < length; idx += 1) { - inspectNode(object[idx], objects, duplicatesIndexes); - } - } else { - const objectKeyList = Object.keys(object); - - for ( - let idx = 0, length = objectKeyList.length; - idx < length; - idx += 1 - ) { - inspectNode(object[objectKeyList[idx]], objects, duplicatesIndexes); - } - } - } - } -} - -function getDuplicateReferences(object: object, state: DumperState): void { - const objects: Any[] = [], - duplicatesIndexes: number[] = []; - - inspectNode(object, objects, duplicatesIndexes); - - const length = duplicatesIndexes.length; - for (let index = 0; index < length; index += 1) { - state.duplicates.push(objects[duplicatesIndexes[index]]); - } - state.usedDuplicates = new Array(length); -} - -export function dump(input: Any, options?: DumperStateOptions): string { - options = options || {}; - - const state = new DumperState(options); - - if (!state.noRefs) getDuplicateReferences(input, state); - - if (writeNode(state, 0, input, true, true)) return `${state.dump}\n`; - - return ""; -} diff --git a/std/encoding/yaml/dumper/dumper_state.ts b/std/encoding/yaml/dumper/dumper_state.ts deleted file mode 100644 index 94cd84878..000000000 --- a/std/encoding/yaml/dumper/dumper_state.ts +++ /dev/null @@ -1,141 +0,0 @@ -// Ported from js-yaml v3.13.1: -// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da -// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. - -import { Schema, SchemaDefinition } from "../schema.ts"; -import { State } from "../state.ts"; -import { StyleVariant, Type } from "../type.ts"; -import { ArrayObject, Any } from "../utils.ts"; - -const _hasOwnProperty = Object.prototype.hasOwnProperty; - -function compileStyleMap( - schema: Schema, - map?: ArrayObject | null -): ArrayObject { - if (typeof map === "undefined" || map === null) return {}; - - let type: Type; - const result: ArrayObject = {}; - const keys = Object.keys(map); - let tag: string, style: StyleVariant; - for (let index = 0, length = keys.length; index < length; index += 1) { - tag = keys[index]; - style = String(map[tag]) as StyleVariant; - if (tag.slice(0, 2) === "!!") { - tag = `tag:yaml.org,2002:${tag.slice(2)}`; - } - type = schema.compiledTypeMap.fallback[tag]; - - if ( - type && - typeof type.styleAliases !== "undefined" && - _hasOwnProperty.call(type.styleAliases, style) - ) { - style = type.styleAliases[style]; - } - - result[tag] = style; - } - - return result; -} - -export interface DumperStateOptions { - /** indentation width to use (in spaces). */ - indent?: number; - /** when true, will not add an indentation level to array elements */ - noArrayIndent?: boolean; - /** - * do not throw on invalid types (like function in the safe schema) - * and skip pairs and single values with such types. - */ - skipInvalid?: boolean; - /** - * specifies level of nesting, when to switch from - * block to flow style for collections. -1 means block style everwhere - */ - flowLevel?: number; - /** Each tag may have own set of styles. - "tag" => "style" map. */ - styles?: ArrayObject | null; - /** specifies a schema to use. */ - schema?: SchemaDefinition; - /** - * If true, sort keys when dumping YAML in ascending, ASCII character order. - * If a function, use the function to sort the keys. (default: false) - * If a function is specified, the function must return a negative value - * if first argument is less than second argument, zero if they're equal - * and a positive value otherwise. - */ - sortKeys?: boolean | ((a: string, b: string) => number); - /** set max line width. (default: 80) */ - lineWidth?: number; - /** - * if true, don't convert duplicate objects - * into references (default: false) - */ - noRefs?: boolean; - /** - * if true don't try to be compatible with older yaml versions. - * Currently: don't quote "yes", "no" and so on, - * as required for YAML 1.1 (default: false) - */ - noCompatMode?: boolean; - /** - * if true flow sequences will be condensed, omitting the - * space between `key: value` or `a, b`. Eg. `'[a,b]'` or `{a:{b:c}}`. - * Can be useful when using yaml for pretty URL query params - * as spaces are %-encoded. (default: false). - */ - condenseFlow?: boolean; -} - -export class DumperState extends State { - public indent: number; - public noArrayIndent: boolean; - public skipInvalid: boolean; - public flowLevel: number; - public sortKeys: boolean | ((a: Any, b: Any) => number); - public lineWidth: number; - public noRefs: boolean; - public noCompatMode: boolean; - public condenseFlow: boolean; - public implicitTypes: Type[]; - public explicitTypes: Type[]; - public tag: string | null = null; - public result = ""; - public duplicates: Any[] = []; - public usedDuplicates: Any[] = []; // changed from null to [] - public styleMap: ArrayObject; - public dump: Any; - - constructor({ - schema, - indent = 2, - noArrayIndent = false, - skipInvalid = false, - flowLevel = -1, - styles = null, - sortKeys = false, - lineWidth = 80, - noRefs = false, - noCompatMode = false, - condenseFlow = false, - }: DumperStateOptions) { - super(schema); - this.indent = Math.max(1, indent); - this.noArrayIndent = noArrayIndent; - this.skipInvalid = skipInvalid; - this.flowLevel = flowLevel; - this.styleMap = compileStyleMap(this.schema as Schema, styles); - this.sortKeys = sortKeys; - this.lineWidth = lineWidth; - this.noRefs = noRefs; - this.noCompatMode = noCompatMode; - this.condenseFlow = condenseFlow; - - this.implicitTypes = (this.schema as Schema).compiledImplicit; - this.explicitTypes = (this.schema as Schema).compiledExplicit; - } -} diff --git a/std/encoding/yaml/error.ts b/std/encoding/yaml/error.ts deleted file mode 100644 index 7f305ccf2..000000000 --- a/std/encoding/yaml/error.ts +++ /dev/null @@ -1,20 +0,0 @@ -// Ported from js-yaml v3.13.1: -// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da -// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. - -import { Mark } from "./mark.ts"; - -export class YAMLError extends Error { - constructor( - message = "(unknown reason)", - protected mark: Mark | string = "" - ) { - super(`${message} ${mark}`); - this.name = this.constructor.name; - } - - public toString(_compact: boolean): string { - return `${this.name}: ${this.message} ${this.mark}`; - } -} diff --git a/std/encoding/yaml/example/dump.ts b/std/encoding/yaml/example/dump.ts deleted file mode 100644 index db3647274..000000000 --- a/std/encoding/yaml/example/dump.ts +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. - -import { stringify } from "../../yaml.ts"; - -console.log( - stringify({ - foo: { - bar: true, - test: [ - "a", - "b", - { - a: false, - }, - { - a: false, - }, - ], - }, - test: "foobar", - }) -); diff --git a/std/encoding/yaml/example/inout.ts b/std/encoding/yaml/example/inout.ts deleted file mode 100644 index b0b47e3fe..000000000 --- a/std/encoding/yaml/example/inout.ts +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. - -import { parse, stringify } from "../../yaml.ts"; - -const test = { - foo: { - bar: true, - test: [ - "a", - "b", - { - a: false, - }, - { - a: false, - }, - ], - }, - test: "foobar", -}; - -const string = stringify(test); -if (Deno.inspect(test) === Deno.inspect(parse(string))) { - console.log("In-Out as expected."); -} else { - console.log("Someting went wrong."); -} diff --git a/std/encoding/yaml/example/parse.ts b/std/encoding/yaml/example/parse.ts deleted file mode 100644 index fc15daf9c..000000000 --- a/std/encoding/yaml/example/parse.ts +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. - -import { parse } from "../../yaml.ts"; - -const result = parse(` -test: toto -foo: - bar: True - baz: 1 - qux: ~ -`); -console.log(result); - -const expected = '{ test: "toto", foo: { bar: true, baz: 1, qux: null } }'; -if (Deno.inspect(result) === expected) { - console.log("Output is as expected."); -} else { - console.error("Error during parse. Output is not as expect.", expected); -} diff --git a/std/encoding/yaml/example/sample_document.ts b/std/encoding/yaml/example/sample_document.ts deleted file mode 100644 index da969d679..000000000 --- a/std/encoding/yaml/example/sample_document.ts +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -/* eslint-disable @typescript-eslint/explicit-function-return-type */ - -import { parse } from "../../yaml.ts"; - -const { readFileSync, cwd } = Deno; - -(() => { - const yml = readFileSync(`${cwd()}/example/sample_document.yml`); - - const document = new TextDecoder().decode(yml); - const obj = parse(document) as object; - console.log(obj); - - let i = 0; - for (const o of Object.values(obj)) { - console.log(`======${i}`); - for (const [key, value] of Object.entries(o)) { - console.log(key, value); - } - i++; - } -})(); diff --git a/std/encoding/yaml/example/sample_document.yml b/std/encoding/yaml/example/sample_document.yml deleted file mode 100644 index 1f3c2eb3e..000000000 --- a/std/encoding/yaml/example/sample_document.yml +++ /dev/null @@ -1,197 +0,0 @@ ---- -# Collection Types ############################################################# -################################################################################ - -# http://yaml.org/type/map.html -----------------------------------------------# - -map: - # Unordered set of key: value pairs. - Block style: !!map - Clark : Evans - Ingy : döt Net - Oren : Ben-Kiki - Flow style: !!map { Clark: Evans, Ingy: döt Net, Oren: Ben-Kiki } - -# http://yaml.org/type/omap.html ----------------------------------------------# - -omap: - # Explicitly typed ordered map (dictionary). - Bestiary: !!omap - - aardvark: African pig-like ant eater. Ugly. - - anteater: South-American ant eater. Two species. - - anaconda: South-American constrictor snake. Scaly. - # Etc. - # Flow style - Numbers: !!omap [ one: 1, two: 2, three : 3 ] - -# http://yaml.org/type/pairs.html ---------------------------------------------# - -pairs: - # Explicitly typed pairs. - Block tasks: !!pairs - - meeting: with team. - - meeting: with boss. - - break: lunch. - - meeting: with client. - Flow tasks: !!pairs [ meeting: with team, meeting: with boss ] - -# http://yaml.org/type/set.html -----------------------------------------------# - -set: - # Explicitly typed set. - baseball players: !!set - ? Mark McGwire - ? Sammy Sosa - ? Ken Griffey - # Flow style - baseball teams: !!set { Boston Red Sox, Detroit Tigers, New York Yankees } - -# http://yaml.org/type/seq.html -----------------------------------------------# - -seq: - # Ordered sequence of nodes - Block style: !!seq - - Mercury # Rotates - no light/dark sides. - - Venus # Deadliest. Aptly named. - - Earth # Mostly dirt. - - Mars # Seems empty. - - Jupiter # The king. - - Saturn # Pretty. - - Uranus # Where the sun hardly shines. - - Neptune # Boring. No rings. - - Pluto # You call this a planet? - Flow style: !!seq [ Mercury, Venus, Earth, Mars, # Rocks - Jupiter, Saturn, Uranus, Neptune, # Gas - Pluto ] # Overrated - - -# Scalar Types ################################################################# -################################################################################ - -# http://yaml.org/type/binary.html --------------------------------------------# - -binary: - canonical: !!binary "\ - R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5\ - OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+\ - +f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC\ - AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs=" - generic: !!binary | - R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5 - OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+ - +f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC - AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs= - description: - The binary value above is a tiny arrow encoded as a gif image. - -# http://yaml.org/type/bool.html ----------------------------------------------# - -bool: - - true - - True - - TRUE - - false - - False - - FALSE - -# http://yaml.org/type/float.html ---------------------------------------------# - -float: - canonical: 6.8523015e+5 - exponential: 685.230_15e+03 - fixed: 685_230.15 - sexagesimal: 190:20:30.15 - negative infinity: -.inf - not a number: .NaN - -# http://yaml.org/type/int.html -----------------------------------------------# - -int: - canonical: 685230 - decimal: +685_230 - octal: 02472256 - hexadecimal: 0x_0A_74_AE - binary: 0b1010_0111_0100_1010_1110 - sexagesimal: 190:20:30 - -# http://yaml.org/type/merge.html ---------------------------------------------# - -merge: - - &CENTER { x: 1, y: 2 } - - &LEFT { x: 0, y: 2 } - - &BIG { r: 10 } - - &SMALL { r: 1 } - - # All the following maps are equal: - - - # Explicit keys - x: 1 - y: 2 - r: 10 - label: nothing - - - # Merge one map - << : *CENTER - r: 10 - label: center - - - # Merge multiple maps - << : [ *CENTER, *BIG ] - label: center/big - - - # Override - << : [ *BIG, *LEFT, *SMALL ] - x: 1 - label: big/left/small - -# http://yaml.org/type/null.html ----------------------------------------------# - -null: - # This mapping has four keys, - # one has a value. - empty: - canonical: ~ - english: null - ~: null key - # This sequence has five - # entries, two have values. - sparse: - - ~ - - 2nd entry - - - - 4th entry - - Null - -# http://yaml.org/type/str.html -----------------------------------------------# - -string: abcd - -# http://yaml.org/type/timestamp.html -----------------------------------------# - -timestamp: - canonical: 2001-12-15T02:59:43.1Z - valid iso8601: 2001-12-14t21:59:43.10-05:00 - space separated: 2001-12-14 21:59:43.10 -5 - no time zone (Z): 2001-12-15 2:59:43.10 - date (00:00:00Z): 2002-12-14 - - -# JavaScript Specific Types #################################################### -################################################################################ - -# https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/RegExp - -# regexp: -# simple: !!js/regexp foobar -# modifiers: !!js/regexp /foobar/mi - -# https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/undefined - -# undefined: !!js/undefined ~ - -# https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function - -# function: !!js/function > -# function foobar() { -# return 'Wow! JS-YAML Rocks!'; -# } diff --git a/std/encoding/yaml/loader/loader.ts b/std/encoding/yaml/loader/loader.ts deleted file mode 100644 index f0d535624..000000000 --- a/std/encoding/yaml/loader/loader.ts +++ /dev/null @@ -1,1797 +0,0 @@ -// Ported from js-yaml v3.13.1: -// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da -// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. - -/* eslint-disable max-len */ - -import { YAMLError } from "../error.ts"; -import { Mark } from "../mark.ts"; -import { Type } from "../type.ts"; -import * as common from "../utils.ts"; -import { LoaderState, LoaderStateOptions, ResultType } from "./loader_state.ts"; - -type Any = common.Any; -type ArrayObject = common.ArrayObject; - -const _hasOwnProperty = Object.prototype.hasOwnProperty; - -const CONTEXT_FLOW_IN = 1; -const CONTEXT_FLOW_OUT = 2; -const CONTEXT_BLOCK_IN = 3; -const CONTEXT_BLOCK_OUT = 4; - -const CHOMPING_CLIP = 1; -const CHOMPING_STRIP = 2; -const CHOMPING_KEEP = 3; - -const PATTERN_NON_PRINTABLE = /[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x84\x86-\x9F\uFFFE\uFFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]/; -const PATTERN_NON_ASCII_LINE_BREAKS = /[\x85\u2028\u2029]/; -const PATTERN_FLOW_INDICATORS = /[,\[\]\{\}]/; -const PATTERN_TAG_HANDLE = /^(?:!|!!|![a-z\-]+!)$/i; -/* eslint-disable-next-line max-len */ -const PATTERN_TAG_URI = /^(?:!|[^,\[\]\{\}])(?:%[0-9a-f]{2}|[0-9a-z\-#;\/\?:@&=\+\$,_\.!~\*'\(\)\[\]])*$/i; - -function _class(obj: unknown): string { - return Object.prototype.toString.call(obj); -} - -function isEOL(c: number): boolean { - return c === 0x0a || /* LF */ c === 0x0d /* CR */; -} - -function isWhiteSpace(c: number): boolean { - return c === 0x09 || /* Tab */ c === 0x20 /* Space */; -} - -function isWsOrEol(c: number): boolean { - return ( - c === 0x09 /* Tab */ || - c === 0x20 /* Space */ || - c === 0x0a /* LF */ || - c === 0x0d /* CR */ - ); -} - -function isFlowIndicator(c: number): boolean { - return ( - c === 0x2c /* , */ || - c === 0x5b /* [ */ || - c === 0x5d /* ] */ || - c === 0x7b /* { */ || - c === 0x7d /* } */ - ); -} - -function fromHexCode(c: number): number { - if (0x30 <= /* 0 */ c && c <= 0x39 /* 9 */) { - return c - 0x30; - } - - const lc = c | 0x20; - - if (0x61 <= /* a */ lc && lc <= 0x66 /* f */) { - return lc - 0x61 + 10; - } - - return -1; -} - -function escapedHexLen(c: number): number { - if (c === 0x78 /* x */) { - return 2; - } - if (c === 0x75 /* u */) { - return 4; - } - if (c === 0x55 /* U */) { - return 8; - } - return 0; -} - -function fromDecimalCode(c: number): number { - if (0x30 <= /* 0 */ c && c <= 0x39 /* 9 */) { - return c - 0x30; - } - - return -1; -} - -function simpleEscapeSequence(c: number): string { - /* eslint:disable:prettier */ - return c === 0x30 /* 0 */ - ? "\x00" - : c === 0x61 /* a */ - ? "\x07" - : c === 0x62 /* b */ - ? "\x08" - : c === 0x74 /* t */ - ? "\x09" - : c === 0x09 /* Tab */ - ? "\x09" - : c === 0x6e /* n */ - ? "\x0A" - : c === 0x76 /* v */ - ? "\x0B" - : c === 0x66 /* f */ - ? "\x0C" - : c === 0x72 /* r */ - ? "\x0D" - : c === 0x65 /* e */ - ? "\x1B" - : c === 0x20 /* Space */ - ? " " - : c === 0x22 /* " */ - ? "\x22" - : c === 0x2f /* / */ - ? "/" - : c === 0x5c /* \ */ - ? "\x5C" - : c === 0x4e /* N */ - ? "\x85" - : c === 0x5f /* _ */ - ? "\xA0" - : c === 0x4c /* L */ - ? "\u2028" - : c === 0x50 /* P */ - ? "\u2029" - : ""; - /* eslint:enable:prettier */ -} - -function charFromCodepoint(c: number): string { - if (c <= 0xffff) { - return String.fromCharCode(c); - } - // Encode UTF-16 surrogate pair - // https://en.wikipedia.org/wiki/UTF-16#Code_points_U.2B010000_to_U.2B10FFFF - return String.fromCharCode( - ((c - 0x010000) >> 10) + 0xd800, - ((c - 0x010000) & 0x03ff) + 0xdc00 - ); -} - -const simpleEscapeCheck = new Array(256); // integer, for fast access -const simpleEscapeMap = new Array(256); -for (let i = 0; i < 256; i++) { - simpleEscapeCheck[i] = simpleEscapeSequence(i) ? 1 : 0; - simpleEscapeMap[i] = simpleEscapeSequence(i); -} - -function generateError(state: LoaderState, message: string): YAMLError { - return new YAMLError( - message, - new Mark( - state.filename as string, - state.input, - state.position, - state.line, - state.position - state.lineStart - ) - ); -} - -function throwError(state: LoaderState, message: string): never { - throw generateError(state, message); -} - -function throwWarning(state: LoaderState, message: string): void { - if (state.onWarning) { - state.onWarning.call(null, generateError(state, message)); - } -} - -interface DirectiveHandlers { - [directive: string]: ( - state: LoaderState, - name: string, - ...args: string[] - ) => void; -} - -const directiveHandlers: DirectiveHandlers = { - YAML(state, _name, ...args: string[]) { - if (state.version !== null) { - return throwError(state, "duplication of %YAML directive"); - } - - if (args.length !== 1) { - return throwError(state, "YAML directive accepts exactly one argument"); - } - - const match = /^([0-9]+)\.([0-9]+)$/.exec(args[0]); - if (match === null) { - return throwError(state, "ill-formed argument of the YAML directive"); - } - - const major = parseInt(match[1], 10); - const minor = parseInt(match[2], 10); - if (major !== 1) { - return throwError(state, "unacceptable YAML version of the document"); - } - - state.version = args[0]; - state.checkLineBreaks = minor < 2; - if (minor !== 1 && minor !== 2) { - return throwWarning(state, "unsupported YAML version of the document"); - } - }, - - TAG(state, _name, ...args: string[]): void { - if (args.length !== 2) { - return throwError(state, "TAG directive accepts exactly two arguments"); - } - - const handle = args[0]; - const prefix = args[1]; - - if (!PATTERN_TAG_HANDLE.test(handle)) { - return throwError( - state, - "ill-formed tag handle (first argument) of the TAG directive" - ); - } - - if (_hasOwnProperty.call(state.tagMap, handle)) { - return throwError( - state, - `there is a previously declared suffix for "${handle}" tag handle` - ); - } - - if (!PATTERN_TAG_URI.test(prefix)) { - return throwError( - state, - "ill-formed tag prefix (second argument) of the TAG directive" - ); - } - - if (typeof state.tagMap === "undefined") { - state.tagMap = {}; - } - state.tagMap[handle] = prefix; - }, -}; - -function captureSegment( - state: LoaderState, - start: number, - end: number, - checkJson: boolean -): void { - let result: string; - if (start < end) { - result = state.input.slice(start, end); - - if (checkJson) { - for ( - let position = 0, length = result.length; - position < length; - position++ - ) { - const character = result.charCodeAt(position); - if ( - !(character === 0x09 || (0x20 <= character && character <= 0x10ffff)) - ) { - return throwError(state, "expected valid JSON character"); - } - } - } else if (PATTERN_NON_PRINTABLE.test(result)) { - return throwError(state, "the stream contains non-printable characters"); - } - - state.result += result; - } -} - -function mergeMappings( - state: LoaderState, - destination: ArrayObject, - source: ArrayObject, - overridableKeys: ArrayObject -): void { - if (!common.isObject(source)) { - return throwError( - state, - "cannot merge mappings; the provided source object is unacceptable" - ); - } - - const keys = Object.keys(source); - for (let i = 0, len = keys.length; i < len; i++) { - const key = keys[i]; - if (!_hasOwnProperty.call(destination, key)) { - destination[key] = (source as ArrayObject)[key]; - overridableKeys[key] = true; - } - } -} - -function storeMappingPair( - state: LoaderState, - result: ArrayObject | null, - overridableKeys: ArrayObject, - keyTag: string | null, - keyNode: Any, - valueNode: unknown, - startLine?: number, - startPos?: number -): ArrayObject { - // The output is a plain object here, so keys can only be strings. - // We need to convert keyNode to a string, but doing so can hang the process - // (deeply nested arrays that explode exponentially using aliases). - if (Array.isArray(keyNode)) { - keyNode = Array.prototype.slice.call(keyNode); - - for (let index = 0, quantity = keyNode.length; index < quantity; index++) { - if (Array.isArray(keyNode[index])) { - return throwError(state, "nested arrays are not supported inside keys"); - } - - if ( - typeof keyNode === "object" && - _class(keyNode[index]) === "[object Object]" - ) { - keyNode[index] = "[object Object]"; - } - } - } - - // Avoid code execution in load() via toString property - // (still use its own toString for arrays, timestamps, - // and whatever user schema extensions happen to have @@toStringTag) - if (typeof keyNode === "object" && _class(keyNode) === "[object Object]") { - keyNode = "[object Object]"; - } - - keyNode = String(keyNode); - - if (result === null) { - result = {}; - } - - if (keyTag === "tag:yaml.org,2002:merge") { - if (Array.isArray(valueNode)) { - for ( - let index = 0, quantity = valueNode.length; - index < quantity; - index++ - ) { - mergeMappings(state, result, valueNode[index], overridableKeys); - } - } else { - mergeMappings(state, result, valueNode as ArrayObject, overridableKeys); - } - } else { - if ( - !state.json && - !_hasOwnProperty.call(overridableKeys, keyNode) && - _hasOwnProperty.call(result, keyNode) - ) { - state.line = startLine || state.line; - state.position = startPos || state.position; - return throwError(state, "duplicated mapping key"); - } - result[keyNode] = valueNode; - delete overridableKeys[keyNode]; - } - - return result; -} - -function readLineBreak(state: LoaderState): void { - const ch = state.input.charCodeAt(state.position); - - if (ch === 0x0a /* LF */) { - state.position++; - } else if (ch === 0x0d /* CR */) { - state.position++; - if (state.input.charCodeAt(state.position) === 0x0a /* LF */) { - state.position++; - } - } else { - return throwError(state, "a line break is expected"); - } - - state.line += 1; - state.lineStart = state.position; -} - -function skipSeparationSpace( - state: LoaderState, - allowComments: boolean, - checkIndent: number -): number { - let lineBreaks = 0, - ch = state.input.charCodeAt(state.position); - - while (ch !== 0) { - while (isWhiteSpace(ch)) { - ch = state.input.charCodeAt(++state.position); - } - - if (allowComments && ch === 0x23 /* # */) { - do { - ch = state.input.charCodeAt(++state.position); - } while (ch !== 0x0a && /* LF */ ch !== 0x0d && /* CR */ ch !== 0); - } - - if (isEOL(ch)) { - readLineBreak(state); - - ch = state.input.charCodeAt(state.position); - lineBreaks++; - state.lineIndent = 0; - - while (ch === 0x20 /* Space */) { - state.lineIndent++; - ch = state.input.charCodeAt(++state.position); - } - } else { - break; - } - } - - if ( - checkIndent !== -1 && - lineBreaks !== 0 && - state.lineIndent < checkIndent - ) { - throwWarning(state, "deficient indentation"); - } - - return lineBreaks; -} - -function testDocumentSeparator(state: LoaderState): boolean { - let _position = state.position; - let ch = state.input.charCodeAt(_position); - - // Condition state.position === state.lineStart is tested - // in parent on each call, for efficiency. No needs to test here again. - if ( - (ch === 0x2d || /* - */ ch === 0x2e) /* . */ && - ch === state.input.charCodeAt(_position + 1) && - ch === state.input.charCodeAt(_position + 2) - ) { - _position += 3; - - ch = state.input.charCodeAt(_position); - - if (ch === 0 || isWsOrEol(ch)) { - return true; - } - } - - return false; -} - -function writeFoldedLines(state: LoaderState, count: number): void { - if (count === 1) { - state.result += " "; - } else if (count > 1) { - state.result += common.repeat("\n", count - 1); - } -} - -function readPlainScalar( - state: LoaderState, - nodeIndent: number, - withinFlowCollection: boolean -): boolean { - const kind = state.kind; - const result = state.result; - let ch = state.input.charCodeAt(state.position); - - if ( - isWsOrEol(ch) || - isFlowIndicator(ch) || - ch === 0x23 /* # */ || - ch === 0x26 /* & */ || - ch === 0x2a /* * */ || - ch === 0x21 /* ! */ || - ch === 0x7c /* | */ || - ch === 0x3e /* > */ || - ch === 0x27 /* ' */ || - ch === 0x22 /* " */ || - ch === 0x25 /* % */ || - ch === 0x40 /* @ */ || - ch === 0x60 /* ` */ - ) { - return false; - } - - let following: number; - if (ch === 0x3f || /* ? */ ch === 0x2d /* - */) { - following = state.input.charCodeAt(state.position + 1); - - if ( - isWsOrEol(following) || - (withinFlowCollection && isFlowIndicator(following)) - ) { - return false; - } - } - - state.kind = "scalar"; - state.result = ""; - let captureEnd: number, - captureStart = (captureEnd = state.position); - let hasPendingContent = false; - let line = 0; - while (ch !== 0) { - if (ch === 0x3a /* : */) { - following = state.input.charCodeAt(state.position + 1); - - if ( - isWsOrEol(following) || - (withinFlowCollection && isFlowIndicator(following)) - ) { - break; - } - } else if (ch === 0x23 /* # */) { - const preceding = state.input.charCodeAt(state.position - 1); - - if (isWsOrEol(preceding)) { - break; - } - } else if ( - (state.position === state.lineStart && testDocumentSeparator(state)) || - (withinFlowCollection && isFlowIndicator(ch)) - ) { - break; - } else if (isEOL(ch)) { - line = state.line; - const lineStart = state.lineStart; - const lineIndent = state.lineIndent; - skipSeparationSpace(state, false, -1); - - if (state.lineIndent >= nodeIndent) { - hasPendingContent = true; - ch = state.input.charCodeAt(state.position); - continue; - } else { - state.position = captureEnd; - state.line = line; - state.lineStart = lineStart; - state.lineIndent = lineIndent; - break; - } - } - - if (hasPendingContent) { - captureSegment(state, captureStart, captureEnd, false); - writeFoldedLines(state, state.line - line); - captureStart = captureEnd = state.position; - hasPendingContent = false; - } - - if (!isWhiteSpace(ch)) { - captureEnd = state.position + 1; - } - - ch = state.input.charCodeAt(++state.position); - } - - captureSegment(state, captureStart, captureEnd, false); - - if (state.result) { - return true; - } - - state.kind = kind; - state.result = result; - return false; -} - -function readSingleQuotedScalar( - state: LoaderState, - nodeIndent: number -): boolean { - let ch, captureStart, captureEnd; - - ch = state.input.charCodeAt(state.position); - - if (ch !== 0x27 /* ' */) { - return false; - } - - state.kind = "scalar"; - state.result = ""; - state.position++; - captureStart = captureEnd = state.position; - - while ((ch = state.input.charCodeAt(state.position)) !== 0) { - if (ch === 0x27 /* ' */) { - captureSegment(state, captureStart, state.position, true); - ch = state.input.charCodeAt(++state.position); - - if (ch === 0x27 /* ' */) { - captureStart = state.position; - state.position++; - captureEnd = state.position; - } else { - return true; - } - } else if (isEOL(ch)) { - captureSegment(state, captureStart, captureEnd, true); - writeFoldedLines(state, skipSeparationSpace(state, false, nodeIndent)); - captureStart = captureEnd = state.position; - } else if ( - state.position === state.lineStart && - testDocumentSeparator(state) - ) { - return throwError( - state, - "unexpected end of the document within a single quoted scalar" - ); - } else { - state.position++; - captureEnd = state.position; - } - } - - return throwError( - state, - "unexpected end of the stream within a single quoted scalar" - ); -} - -function readDoubleQuotedScalar( - state: LoaderState, - nodeIndent: number -): boolean { - let ch = state.input.charCodeAt(state.position); - - if (ch !== 0x22 /* " */) { - return false; - } - - state.kind = "scalar"; - state.result = ""; - state.position++; - let captureEnd: number, - captureStart = (captureEnd = state.position); - let tmp: number; - while ((ch = state.input.charCodeAt(state.position)) !== 0) { - if (ch === 0x22 /* " */) { - captureSegment(state, captureStart, state.position, true); - state.position++; - return true; - } - if (ch === 0x5c /* \ */) { - captureSegment(state, captureStart, state.position, true); - ch = state.input.charCodeAt(++state.position); - - if (isEOL(ch)) { - skipSeparationSpace(state, false, nodeIndent); - - // TODO: rework to inline fn with no type cast? - } else if (ch < 256 && simpleEscapeCheck[ch]) { - state.result += simpleEscapeMap[ch]; - state.position++; - } else if ((tmp = escapedHexLen(ch)) > 0) { - let hexLength = tmp; - let hexResult = 0; - - for (; hexLength > 0; hexLength--) { - ch = state.input.charCodeAt(++state.position); - - if ((tmp = fromHexCode(ch)) >= 0) { - hexResult = (hexResult << 4) + tmp; - } else { - return throwError(state, "expected hexadecimal character"); - } - } - - state.result += charFromCodepoint(hexResult); - - state.position++; - } else { - return throwError(state, "unknown escape sequence"); - } - - captureStart = captureEnd = state.position; - } else if (isEOL(ch)) { - captureSegment(state, captureStart, captureEnd, true); - writeFoldedLines(state, skipSeparationSpace(state, false, nodeIndent)); - captureStart = captureEnd = state.position; - } else if ( - state.position === state.lineStart && - testDocumentSeparator(state) - ) { - return throwError( - state, - "unexpected end of the document within a double quoted scalar" - ); - } else { - state.position++; - captureEnd = state.position; - } - } - - return throwError( - state, - "unexpected end of the stream within a double quoted scalar" - ); -} - -function readFlowCollection(state: LoaderState, nodeIndent: number): boolean { - let ch = state.input.charCodeAt(state.position); - let terminator: number; - let isMapping = true; - let result: ResultType = {}; - if (ch === 0x5b /* [ */) { - terminator = 0x5d; /* ] */ - isMapping = false; - result = []; - } else if (ch === 0x7b /* { */) { - terminator = 0x7d; /* } */ - } else { - return false; - } - - if ( - state.anchor !== null && - typeof state.anchor != "undefined" && - typeof state.anchorMap != "undefined" - ) { - state.anchorMap[state.anchor] = result; - } - - ch = state.input.charCodeAt(++state.position); - - const tag = state.tag, - anchor = state.anchor; - let readNext = true; - let valueNode, - keyNode, - keyTag: string | null = (keyNode = valueNode = null), - isExplicitPair: boolean, - isPair = (isExplicitPair = false); - let following = 0, - line = 0; - const overridableKeys: ArrayObject = {}; - while (ch !== 0) { - skipSeparationSpace(state, true, nodeIndent); - - ch = state.input.charCodeAt(state.position); - - if (ch === terminator) { - state.position++; - state.tag = tag; - state.anchor = anchor; - state.kind = isMapping ? "mapping" : "sequence"; - state.result = result; - return true; - } - if (!readNext) { - return throwError(state, "missed comma between flow collection entries"); - } - - keyTag = keyNode = valueNode = null; - isPair = isExplicitPair = false; - - if (ch === 0x3f /* ? */) { - following = state.input.charCodeAt(state.position + 1); - - if (isWsOrEol(following)) { - isPair = isExplicitPair = true; - state.position++; - skipSeparationSpace(state, true, nodeIndent); - } - } - - line = state.line; - // eslint-disable-next-line @typescript-eslint/no-use-before-define - composeNode(state, nodeIndent, CONTEXT_FLOW_IN, false, true); - keyTag = state.tag || null; - keyNode = state.result; - skipSeparationSpace(state, true, nodeIndent); - - ch = state.input.charCodeAt(state.position); - - if ((isExplicitPair || state.line === line) && ch === 0x3a /* : */) { - isPair = true; - ch = state.input.charCodeAt(++state.position); - skipSeparationSpace(state, true, nodeIndent); - // eslint-disable-next-line @typescript-eslint/no-use-before-define - composeNode(state, nodeIndent, CONTEXT_FLOW_IN, false, true); - valueNode = state.result; - } - - if (isMapping) { - storeMappingPair( - state, - result, - overridableKeys, - keyTag, - keyNode, - valueNode - ); - } else if (isPair) { - (result as Array<{}>).push( - storeMappingPair( - state, - null, - overridableKeys, - keyTag, - keyNode, - valueNode - ) - ); - } else { - (result as ResultType[]).push(keyNode as ResultType); - } - - skipSeparationSpace(state, true, nodeIndent); - - ch = state.input.charCodeAt(state.position); - - if (ch === 0x2c /* , */) { - readNext = true; - ch = state.input.charCodeAt(++state.position); - } else { - readNext = false; - } - } - - return throwError( - state, - "unexpected end of the stream within a flow collection" - ); -} - -function readBlockScalar(state: LoaderState, nodeIndent: number): boolean { - let chomping = CHOMPING_CLIP, - didReadContent = false, - detectedIndent = false, - textIndent = nodeIndent, - emptyLines = 0, - atMoreIndented = false; - - let ch = state.input.charCodeAt(state.position); - - let folding = false; - if (ch === 0x7c /* | */) { - folding = false; - } else if (ch === 0x3e /* > */) { - folding = true; - } else { - return false; - } - - state.kind = "scalar"; - state.result = ""; - - let tmp = 0; - while (ch !== 0) { - ch = state.input.charCodeAt(++state.position); - - if (ch === 0x2b || /* + */ ch === 0x2d /* - */) { - if (CHOMPING_CLIP === chomping) { - chomping = ch === 0x2b /* + */ ? CHOMPING_KEEP : CHOMPING_STRIP; - } else { - return throwError(state, "repeat of a chomping mode identifier"); - } - } else if ((tmp = fromDecimalCode(ch)) >= 0) { - if (tmp === 0) { - return throwError( - state, - "bad explicit indentation width of a block scalar; it cannot be less than one" - ); - } else if (!detectedIndent) { - textIndent = nodeIndent + tmp - 1; - detectedIndent = true; - } else { - return throwError(state, "repeat of an indentation width identifier"); - } - } else { - break; - } - } - - if (isWhiteSpace(ch)) { - do { - ch = state.input.charCodeAt(++state.position); - } while (isWhiteSpace(ch)); - - if (ch === 0x23 /* # */) { - do { - ch = state.input.charCodeAt(++state.position); - } while (!isEOL(ch) && ch !== 0); - } - } - - while (ch !== 0) { - readLineBreak(state); - state.lineIndent = 0; - - ch = state.input.charCodeAt(state.position); - - while ( - (!detectedIndent || state.lineIndent < textIndent) && - ch === 0x20 /* Space */ - ) { - state.lineIndent++; - ch = state.input.charCodeAt(++state.position); - } - - if (!detectedIndent && state.lineIndent > textIndent) { - textIndent = state.lineIndent; - } - - if (isEOL(ch)) { - emptyLines++; - continue; - } - - // End of the scalar. - if (state.lineIndent < textIndent) { - // Perform the chomping. - if (chomping === CHOMPING_KEEP) { - state.result += common.repeat( - "\n", - didReadContent ? 1 + emptyLines : emptyLines - ); - } else if (chomping === CHOMPING_CLIP) { - if (didReadContent) { - // i.e. only if the scalar is not empty. - state.result += "\n"; - } - } - - // Break this `while` cycle and go to the funciton's epilogue. - break; - } - - // Folded style: use fancy rules to handle line breaks. - if (folding) { - // Lines starting with white space characters (more-indented lines) are not folded. - if (isWhiteSpace(ch)) { - atMoreIndented = true; - // except for the first content line (cf. Example 8.1) - state.result += common.repeat( - "\n", - didReadContent ? 1 + emptyLines : emptyLines - ); - - // End of more-indented block. - } else if (atMoreIndented) { - atMoreIndented = false; - state.result += common.repeat("\n", emptyLines + 1); - - // Just one line break - perceive as the same line. - } else if (emptyLines === 0) { - if (didReadContent) { - // i.e. only if we have already read some scalar content. - state.result += " "; - } - - // Several line breaks - perceive as different lines. - } else { - state.result += common.repeat("\n", emptyLines); - } - - // Literal style: just add exact number of line breaks between content lines. - } else { - // Keep all line breaks except the header line break. - state.result += common.repeat( - "\n", - didReadContent ? 1 + emptyLines : emptyLines - ); - } - - didReadContent = true; - detectedIndent = true; - emptyLines = 0; - const captureStart = state.position; - - while (!isEOL(ch) && ch !== 0) { - ch = state.input.charCodeAt(++state.position); - } - - captureSegment(state, captureStart, state.position, false); - } - - return true; -} - -function readBlockSequence(state: LoaderState, nodeIndent: number): boolean { - let line: number, - following: number, - detected = false, - ch: number; - const tag = state.tag, - anchor = state.anchor, - result: unknown[] = []; - - if ( - state.anchor !== null && - typeof state.anchor !== "undefined" && - typeof state.anchorMap !== "undefined" - ) { - state.anchorMap[state.anchor] = result; - } - - ch = state.input.charCodeAt(state.position); - - while (ch !== 0) { - if (ch !== 0x2d /* - */) { - break; - } - - following = state.input.charCodeAt(state.position + 1); - - if (!isWsOrEol(following)) { - break; - } - - detected = true; - state.position++; - - if (skipSeparationSpace(state, true, -1)) { - if (state.lineIndent <= nodeIndent) { - result.push(null); - ch = state.input.charCodeAt(state.position); - continue; - } - } - - line = state.line; - // eslint-disable-next-line @typescript-eslint/no-use-before-define - composeNode(state, nodeIndent, CONTEXT_BLOCK_IN, false, true); - result.push(state.result); - skipSeparationSpace(state, true, -1); - - ch = state.input.charCodeAt(state.position); - - if ((state.line === line || state.lineIndent > nodeIndent) && ch !== 0) { - return throwError(state, "bad indentation of a sequence entry"); - } else if (state.lineIndent < nodeIndent) { - break; - } - } - - if (detected) { - state.tag = tag; - state.anchor = anchor; - state.kind = "sequence"; - state.result = result; - return true; - } - return false; -} - -function readBlockMapping( - state: LoaderState, - nodeIndent: number, - flowIndent: number -): boolean { - const tag = state.tag, - anchor = state.anchor, - result = {}, - overridableKeys = {}; - let following: number, - allowCompact = false, - line: number, - pos: number, - keyTag = null, - keyNode = null, - valueNode = null, - atExplicitKey = false, - detected = false, - ch: number; - - if ( - state.anchor !== null && - typeof state.anchor !== "undefined" && - typeof state.anchorMap !== "undefined" - ) { - state.anchorMap[state.anchor] = result; - } - - ch = state.input.charCodeAt(state.position); - - while (ch !== 0) { - following = state.input.charCodeAt(state.position + 1); - line = state.line; // Save the current line. - pos = state.position; - - // - // Explicit notation case. There are two separate blocks: - // first for the key (denoted by "?") and second for the value (denoted by ":") - // - if ((ch === 0x3f || /* ? */ ch === 0x3a) && /* : */ isWsOrEol(following)) { - if (ch === 0x3f /* ? */) { - if (atExplicitKey) { - storeMappingPair( - state, - result, - overridableKeys, - keyTag as string, - keyNode, - null - ); - keyTag = keyNode = valueNode = null; - } - - detected = true; - atExplicitKey = true; - allowCompact = true; - } else if (atExplicitKey) { - // i.e. 0x3A/* : */ === character after the explicit key. - atExplicitKey = false; - allowCompact = true; - } else { - return throwError( - state, - "incomplete explicit mapping pair; a key node is missed; or followed by a non-tabulated empty line" - ); - } - - state.position += 1; - ch = following; - - // - // Implicit notation case. Flow-style node as the key first, then ":", and the value. - // - // eslint-disable-next-line @typescript-eslint/no-use-before-define - } else if (composeNode(state, flowIndent, CONTEXT_FLOW_OUT, false, true)) { - if (state.line === line) { - ch = state.input.charCodeAt(state.position); - - while (isWhiteSpace(ch)) { - ch = state.input.charCodeAt(++state.position); - } - - if (ch === 0x3a /* : */) { - ch = state.input.charCodeAt(++state.position); - - if (!isWsOrEol(ch)) { - return throwError( - state, - "a whitespace character is expected after the key-value separator within a block mapping" - ); - } - - if (atExplicitKey) { - storeMappingPair( - state, - result, - overridableKeys, - keyTag as string, - keyNode, - null - ); - keyTag = keyNode = valueNode = null; - } - - detected = true; - atExplicitKey = false; - allowCompact = false; - keyTag = state.tag; - keyNode = state.result; - } else if (detected) { - return throwError( - state, - "can not read an implicit mapping pair; a colon is missed" - ); - } else { - state.tag = tag; - state.anchor = anchor; - return true; // Keep the result of `composeNode`. - } - } else if (detected) { - return throwError( - state, - "can not read a block mapping entry; a multiline key may not be an implicit key" - ); - } else { - state.tag = tag; - state.anchor = anchor; - return true; // Keep the result of `composeNode`. - } - } else { - break; // Reading is done. Go to the epilogue. - } - - // - // Common reading code for both explicit and implicit notations. - // - if (state.line === line || state.lineIndent > nodeIndent) { - if ( - // eslint-disable-next-line @typescript-eslint/no-use-before-define - composeNode(state, nodeIndent, CONTEXT_BLOCK_OUT, true, allowCompact) - ) { - if (atExplicitKey) { - keyNode = state.result; - } else { - valueNode = state.result; - } - } - - if (!atExplicitKey) { - storeMappingPair( - state, - result, - overridableKeys, - keyTag as string, - keyNode, - valueNode, - line, - pos - ); - keyTag = keyNode = valueNode = null; - } - - skipSeparationSpace(state, true, -1); - ch = state.input.charCodeAt(state.position); - } - - if (state.lineIndent > nodeIndent && ch !== 0) { - return throwError(state, "bad indentation of a mapping entry"); - } else if (state.lineIndent < nodeIndent) { - break; - } - } - - // - // Epilogue. - // - - // Special case: last mapping's node contains only the key in explicit notation. - if (atExplicitKey) { - storeMappingPair( - state, - result, - overridableKeys, - keyTag as string, - keyNode, - null - ); - } - - // Expose the resulting mapping. - if (detected) { - state.tag = tag; - state.anchor = anchor; - state.kind = "mapping"; - state.result = result; - } - - return detected; -} - -function readTagProperty(state: LoaderState): boolean { - let position: number, - isVerbatim = false, - isNamed = false, - tagHandle = "", - tagName: string, - ch: number; - - ch = state.input.charCodeAt(state.position); - - if (ch !== 0x21 /* ! */) return false; - - if (state.tag !== null) { - return throwError(state, "duplication of a tag property"); - } - - ch = state.input.charCodeAt(++state.position); - - if (ch === 0x3c /* < */) { - isVerbatim = true; - ch = state.input.charCodeAt(++state.position); - } else if (ch === 0x21 /* ! */) { - isNamed = true; - tagHandle = "!!"; - ch = state.input.charCodeAt(++state.position); - } else { - tagHandle = "!"; - } - - position = state.position; - - if (isVerbatim) { - do { - ch = state.input.charCodeAt(++state.position); - } while (ch !== 0 && ch !== 0x3e /* > */); - - if (state.position < state.length) { - tagName = state.input.slice(position, state.position); - ch = state.input.charCodeAt(++state.position); - } else { - return throwError( - state, - "unexpected end of the stream within a verbatim tag" - ); - } - } else { - while (ch !== 0 && !isWsOrEol(ch)) { - if (ch === 0x21 /* ! */) { - if (!isNamed) { - tagHandle = state.input.slice(position - 1, state.position + 1); - - if (!PATTERN_TAG_HANDLE.test(tagHandle)) { - return throwError( - state, - "named tag handle cannot contain such characters" - ); - } - - isNamed = true; - position = state.position + 1; - } else { - return throwError( - state, - "tag suffix cannot contain exclamation marks" - ); - } - } - - ch = state.input.charCodeAt(++state.position); - } - - tagName = state.input.slice(position, state.position); - - if (PATTERN_FLOW_INDICATORS.test(tagName)) { - return throwError( - state, - "tag suffix cannot contain flow indicator characters" - ); - } - } - - if (tagName && !PATTERN_TAG_URI.test(tagName)) { - return throwError( - state, - `tag name cannot contain such characters: ${tagName}` - ); - } - - if (isVerbatim) { - state.tag = tagName; - } else if ( - typeof state.tagMap !== "undefined" && - _hasOwnProperty.call(state.tagMap, tagHandle) - ) { - state.tag = state.tagMap[tagHandle] + tagName; - } else if (tagHandle === "!") { - state.tag = `!${tagName}`; - } else if (tagHandle === "!!") { - state.tag = `tag:yaml.org,2002:${tagName}`; - } else { - return throwError(state, `undeclared tag handle "${tagHandle}"`); - } - - return true; -} - -function readAnchorProperty(state: LoaderState): boolean { - let ch = state.input.charCodeAt(state.position); - if (ch !== 0x26 /* & */) return false; - - if (state.anchor !== null) { - return throwError(state, "duplication of an anchor property"); - } - ch = state.input.charCodeAt(++state.position); - - const position = state.position; - while (ch !== 0 && !isWsOrEol(ch) && !isFlowIndicator(ch)) { - ch = state.input.charCodeAt(++state.position); - } - - if (state.position === position) { - return throwError( - state, - "name of an anchor node must contain at least one character" - ); - } - - state.anchor = state.input.slice(position, state.position); - return true; -} - -function readAlias(state: LoaderState): boolean { - let ch = state.input.charCodeAt(state.position); - - if (ch !== 0x2a /* * */) return false; - - ch = state.input.charCodeAt(++state.position); - const _position = state.position; - - while (ch !== 0 && !isWsOrEol(ch) && !isFlowIndicator(ch)) { - ch = state.input.charCodeAt(++state.position); - } - - if (state.position === _position) { - return throwError( - state, - "name of an alias node must contain at least one character" - ); - } - - const alias = state.input.slice(_position, state.position); - if ( - typeof state.anchorMap !== "undefined" && - !state.anchorMap.hasOwnProperty(alias) - ) { - return throwError(state, `unidentified alias "${alias}"`); - } - - if (typeof state.anchorMap !== "undefined") { - state.result = state.anchorMap[alias]; - } - skipSeparationSpace(state, true, -1); - return true; -} - -function composeNode( - state: LoaderState, - parentIndent: number, - nodeContext: number, - allowToSeek: boolean, - allowCompact: boolean -): boolean { - let allowBlockScalars: boolean, - allowBlockCollections: boolean, - indentStatus = 1, // 1: this>parent, 0: this=parent, -1: this parentIndent) { - indentStatus = 1; - } else if (state.lineIndent === parentIndent) { - indentStatus = 0; - } else if (state.lineIndent < parentIndent) { - indentStatus = -1; - } - } - } - - if (indentStatus === 1) { - while (readTagProperty(state) || readAnchorProperty(state)) { - if (skipSeparationSpace(state, true, -1)) { - atNewLine = true; - allowBlockCollections = allowBlockStyles; - - if (state.lineIndent > parentIndent) { - indentStatus = 1; - } else if (state.lineIndent === parentIndent) { - indentStatus = 0; - } else if (state.lineIndent < parentIndent) { - indentStatus = -1; - } - } else { - allowBlockCollections = false; - } - } - } - - if (allowBlockCollections) { - allowBlockCollections = atNewLine || allowCompact; - } - - if (indentStatus === 1 || CONTEXT_BLOCK_OUT === nodeContext) { - const cond = - CONTEXT_FLOW_IN === nodeContext || CONTEXT_FLOW_OUT === nodeContext; - flowIndent = cond ? parentIndent : parentIndent + 1; - - blockIndent = state.position - state.lineStart; - - if (indentStatus === 1) { - if ( - (allowBlockCollections && - (readBlockSequence(state, blockIndent) || - readBlockMapping(state, blockIndent, flowIndent))) || - readFlowCollection(state, flowIndent) - ) { - hasContent = true; - } else { - if ( - (allowBlockScalars && readBlockScalar(state, flowIndent)) || - readSingleQuotedScalar(state, flowIndent) || - readDoubleQuotedScalar(state, flowIndent) - ) { - hasContent = true; - } else if (readAlias(state)) { - hasContent = true; - - if (state.tag !== null || state.anchor !== null) { - return throwError( - state, - "alias node should not have Any properties" - ); - } - } else if ( - readPlainScalar(state, flowIndent, CONTEXT_FLOW_IN === nodeContext) - ) { - hasContent = true; - - if (state.tag === null) { - state.tag = "?"; - } - } - - if (state.anchor !== null && typeof state.anchorMap !== "undefined") { - state.anchorMap[state.anchor] = state.result; - } - } - } else if (indentStatus === 0) { - // Special case: block sequences are allowed to have same indentation level as the parent. - // http://www.yaml.org/spec/1.2/spec.html#id2799784 - hasContent = - allowBlockCollections && readBlockSequence(state, blockIndent); - } - } - - if (state.tag !== null && state.tag !== "!") { - if (state.tag === "?") { - for ( - let typeIndex = 0, typeQuantity = state.implicitTypes.length; - typeIndex < typeQuantity; - typeIndex++ - ) { - type = state.implicitTypes[typeIndex]; - - // Implicit resolving is not allowed for non-scalar types, and '?' - // non-specific tag is only assigned to plain scalars. So, it isn't - // needed to check for 'kind' conformity. - - if (type.resolve(state.result)) { - // `state.result` updated in resolver if matched - state.result = type.construct(state.result); - state.tag = type.tag; - if (state.anchor !== null && typeof state.anchorMap !== "undefined") { - state.anchorMap[state.anchor] = state.result; - } - break; - } - } - } else if ( - _hasOwnProperty.call(state.typeMap[state.kind || "fallback"], state.tag) - ) { - type = state.typeMap[state.kind || "fallback"][state.tag]; - - if (state.result !== null && type.kind !== state.kind) { - return throwError( - state, - `unacceptable node kind for !<${state.tag}> tag; it should be "${type.kind}", not "${state.kind}"` - ); - } - - if (!type.resolve(state.result)) { - // `state.result` updated in resolver if matched - return throwError( - state, - `cannot resolve a node with !<${state.tag}> explicit tag` - ); - } else { - state.result = type.construct(state.result); - if (state.anchor !== null && typeof state.anchorMap !== "undefined") { - state.anchorMap[state.anchor] = state.result; - } - } - } else { - return throwError(state, `unknown tag !<${state.tag}>`); - } - } - - if (state.listener && state.listener !== null) { - state.listener("close", state); - } - return state.tag !== null || state.anchor !== null || hasContent; -} - -function readDocument(state: LoaderState): void { - const documentStart = state.position; - let position: number, - directiveName: string, - directiveArgs: string[], - hasDirectives = false, - ch: number; - - state.version = null; - state.checkLineBreaks = state.legacy; - state.tagMap = {}; - state.anchorMap = {}; - - while ((ch = state.input.charCodeAt(state.position)) !== 0) { - skipSeparationSpace(state, true, -1); - - ch = state.input.charCodeAt(state.position); - - if (state.lineIndent > 0 || ch !== 0x25 /* % */) { - break; - } - - hasDirectives = true; - ch = state.input.charCodeAt(++state.position); - position = state.position; - - while (ch !== 0 && !isWsOrEol(ch)) { - ch = state.input.charCodeAt(++state.position); - } - - directiveName = state.input.slice(position, state.position); - directiveArgs = []; - - if (directiveName.length < 1) { - return throwError( - state, - "directive name must not be less than one character in length" - ); - } - - while (ch !== 0) { - while (isWhiteSpace(ch)) { - ch = state.input.charCodeAt(++state.position); - } - - if (ch === 0x23 /* # */) { - do { - ch = state.input.charCodeAt(++state.position); - } while (ch !== 0 && !isEOL(ch)); - break; - } - - if (isEOL(ch)) break; - - position = state.position; - - while (ch !== 0 && !isWsOrEol(ch)) { - ch = state.input.charCodeAt(++state.position); - } - - directiveArgs.push(state.input.slice(position, state.position)); - } - - if (ch !== 0) readLineBreak(state); - - if (_hasOwnProperty.call(directiveHandlers, directiveName)) { - directiveHandlers[directiveName](state, directiveName, ...directiveArgs); - } else { - throwWarning(state, `unknown document directive "${directiveName}"`); - } - } - - skipSeparationSpace(state, true, -1); - - if ( - state.lineIndent === 0 && - state.input.charCodeAt(state.position) === 0x2d /* - */ && - state.input.charCodeAt(state.position + 1) === 0x2d /* - */ && - state.input.charCodeAt(state.position + 2) === 0x2d /* - */ - ) { - state.position += 3; - skipSeparationSpace(state, true, -1); - } else if (hasDirectives) { - return throwError(state, "directives end mark is expected"); - } - - composeNode(state, state.lineIndent - 1, CONTEXT_BLOCK_OUT, false, true); - skipSeparationSpace(state, true, -1); - - if ( - state.checkLineBreaks && - PATTERN_NON_ASCII_LINE_BREAKS.test( - state.input.slice(documentStart, state.position) - ) - ) { - throwWarning(state, "non-ASCII line breaks are interpreted as content"); - } - - state.documents.push(state.result); - - if (state.position === state.lineStart && testDocumentSeparator(state)) { - if (state.input.charCodeAt(state.position) === 0x2e /* . */) { - state.position += 3; - skipSeparationSpace(state, true, -1); - } - return; - } - - if (state.position < state.length - 1) { - return throwError( - state, - "end of the stream or a document separator is expected" - ); - } else { - return; - } -} - -function loadDocuments(input: string, options?: LoaderStateOptions): unknown[] { - input = String(input); - options = options || {}; - - if (input.length !== 0) { - // Add tailing `\n` if not exists - if ( - input.charCodeAt(input.length - 1) !== 0x0a /* LF */ && - input.charCodeAt(input.length - 1) !== 0x0d /* CR */ - ) { - input += "\n"; - } - - // Strip BOM - if (input.charCodeAt(0) === 0xfeff) { - input = input.slice(1); - } - } - - const state = new LoaderState(input, options); - - // Use 0 as string terminator. That significantly simplifies bounds check. - state.input += "\0"; - - while (state.input.charCodeAt(state.position) === 0x20 /* Space */) { - state.lineIndent += 1; - state.position += 1; - } - - while (state.position < state.length - 1) { - readDocument(state); - } - - return state.documents; -} - -export type CbFunction = (doc: unknown) => void; -function isCbFunction(fn: unknown): fn is CbFunction { - return typeof fn === "function"; -} - -export function loadAll( - input: string, - iteratorOrOption?: T, - options?: LoaderStateOptions -): T extends CbFunction ? void : unknown[] { - if (!isCbFunction(iteratorOrOption)) { - return loadDocuments(input, iteratorOrOption as LoaderStateOptions) as Any; - } - - const documents = loadDocuments(input, options); - const iterator = iteratorOrOption; - for (let index = 0, length = documents.length; index < length; index++) { - iterator(documents[index]); - } - - return void 0 as Any; -} - -export function load(input: string, options?: LoaderStateOptions): unknown { - const documents = loadDocuments(input, options); - - if (documents.length === 0) { - return; - } - if (documents.length === 1) { - return documents[0]; - } - throw new YAMLError( - "expected a single document in the stream, but found more" - ); -} diff --git a/std/encoding/yaml/loader/loader_state.ts b/std/encoding/yaml/loader/loader_state.ts deleted file mode 100644 index ca50fcaf1..000000000 --- a/std/encoding/yaml/loader/loader_state.ts +++ /dev/null @@ -1,74 +0,0 @@ -// Ported from js-yaml v3.13.1: -// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da -// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. - -import { YAMLError } from "../error.ts"; -import { Schema, SchemaDefinition, TypeMap } from "../schema.ts"; -import { State } from "../state.ts"; -import { Type } from "../type.ts"; -import { Any, ArrayObject } from "../utils.ts"; - -export interface LoaderStateOptions { - legacy?: boolean; - listener?: ((...args: Any[]) => void) | null; - /** string to be used as a file path in error/warning messages. */ - filename?: string; - /** specifies a schema to use. */ - schema?: SchemaDefinition; - /** compatibility with JSON.parse behaviour. */ - json?: boolean; - /** function to call on warning messages. */ - onWarning?(this: null, e?: YAMLError): void; -} - -export type ResultType = [] | {} | string; - -export class LoaderState extends State { - public documents: Any[] = []; - public length: number; - public lineIndent = 0; - public lineStart = 0; - public position = 0; - public line = 0; - public filename?: string; - public onWarning?: (...args: Any[]) => void; - public legacy: boolean; - public json: boolean; - public listener?: ((...args: Any[]) => void) | null; - public implicitTypes: Type[]; - public typeMap: TypeMap; - - public version?: string | null; - public checkLineBreaks?: boolean; - public tagMap?: ArrayObject; - public anchorMap?: ArrayObject; - public tag?: string | null; - public anchor?: string | null; - public kind?: string | null; - public result: ResultType | null = ""; - - constructor( - public input: string, - { - filename, - schema, - onWarning, - legacy = false, - json = false, - listener = null, - }: LoaderStateOptions - ) { - super(schema); - this.filename = filename; - this.onWarning = onWarning; - this.legacy = legacy; - this.json = json; - this.listener = listener; - - this.implicitTypes = (this.schema as Schema).compiledImplicit; - this.typeMap = (this.schema as Schema).compiledTypeMap; - - this.length = input.length; - } -} diff --git a/std/encoding/yaml/mark.ts b/std/encoding/yaml/mark.ts deleted file mode 100644 index 44cf175a0..000000000 --- a/std/encoding/yaml/mark.ts +++ /dev/null @@ -1,77 +0,0 @@ -// Ported from js-yaml v3.13.1: -// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da -// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. - -import { repeat } from "./utils.ts"; - -export class Mark { - constructor( - public name: string, - public buffer: string, - public position: number, - public line: number, - public column: number - ) {} - - public getSnippet(indent = 4, maxLength = 75): string | null { - if (!this.buffer) return null; - - let head = ""; - let start = this.position; - - while ( - start > 0 && - "\x00\r\n\x85\u2028\u2029".indexOf(this.buffer.charAt(start - 1)) === -1 - ) { - start -= 1; - if (this.position - start > maxLength / 2 - 1) { - head = " ... "; - start += 5; - break; - } - } - - let tail = ""; - let end = this.position; - - while ( - end < this.buffer.length && - "\x00\r\n\x85\u2028\u2029".indexOf(this.buffer.charAt(end)) === -1 - ) { - end += 1; - if (end - this.position > maxLength / 2 - 1) { - tail = " ... "; - end -= 5; - break; - } - } - - const snippet = this.buffer.slice(start, end); - return `${repeat(" ", indent)}${head}${snippet}${tail}\n${repeat( - " ", - indent + this.position - start + head.length - )}^`; - } - - public toString(compact?: boolean): string { - let snippet, - where = ""; - - if (this.name) { - where += `in "${this.name}" `; - } - - where += `at line ${this.line + 1}, column ${this.column + 1}`; - - if (!compact) { - snippet = this.getSnippet(); - - if (snippet) { - where += `:\n${snippet}`; - } - } - - return where; - } -} diff --git a/std/encoding/yaml/parse.ts b/std/encoding/yaml/parse.ts deleted file mode 100644 index 2aa0042bd..000000000 --- a/std/encoding/yaml/parse.ts +++ /dev/null @@ -1,32 +0,0 @@ -// Ported from js-yaml v3.13.1: -// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da -// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. - -import { CbFunction, load, loadAll } from "./loader/loader.ts"; -import { LoaderStateOptions } from "./loader/loader_state.ts"; - -export type ParseOptions = LoaderStateOptions; - -/** - * Parses `content` as single YAML document. - * - * Returns a JavaScript object or throws `YAMLException` on error. - * By default, does not support regexps, functions and undefined. This method is safe for untrusted data. - * - */ -export function parse(content: string, options?: ParseOptions): unknown { - return load(content, options); -} - -/** - * Same as `parse()`, but understands multi-document sources. - * Applies iterator to each document if specified, or returns array of documents. - */ -export function parseAll( - content: string, - iterator?: CbFunction, - options?: ParseOptions -): unknown { - return loadAll(content, iterator, options); -} diff --git a/std/encoding/yaml/parse_test.ts b/std/encoding/yaml/parse_test.ts deleted file mode 100644 index 21f1b893b..000000000 --- a/std/encoding/yaml/parse_test.ts +++ /dev/null @@ -1,56 +0,0 @@ -// Ported from js-yaml v3.13.1: -// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da -// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. - -import { parse, parseAll } from "./parse.ts"; -import { assertEquals } from "../../testing/asserts.ts"; - -Deno.test({ - name: "`parse` parses single document yaml string", - fn(): void { - const yaml = ` - test: toto - foo: - bar: True - baz: 1 - qux: ~ - `; - - const expected = { test: "toto", foo: { bar: true, baz: 1, qux: null } }; - - assertEquals(parse(yaml), expected); - }, -}); - -Deno.test({ - name: "`parseAll` parses the yaml string with multiple documents", - fn(): void { - const yaml = ` ---- -id: 1 -name: Alice ---- -id: 2 -name: Bob ---- -id: 3 -name: Eve - `; - const expected = [ - { - id: 1, - name: "Alice", - }, - { - id: 2, - name: "Bob", - }, - { - id: 3, - name: "Eve", - }, - ]; - assertEquals(parseAll(yaml), expected); - }, -}); diff --git a/std/encoding/yaml/schema.ts b/std/encoding/yaml/schema.ts deleted file mode 100644 index 579644dbb..000000000 --- a/std/encoding/yaml/schema.ts +++ /dev/null @@ -1,101 +0,0 @@ -// Ported from js-yaml v3.13.1: -// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da -// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. - -import { YAMLError } from "./error.ts"; -import { KindType, Type } from "./type.ts"; -import { ArrayObject, Any } from "./utils.ts"; - -function compileList( - schema: Schema, - name: "implicit" | "explicit", - result: Type[] -): Type[] { - const exclude: number[] = []; - - for (const includedSchema of schema.include) { - result = compileList(includedSchema, name, result); - } - - for (const currentType of schema[name]) { - for ( - let previousIndex = 0; - previousIndex < result.length; - previousIndex++ - ) { - const previousType = result[previousIndex]; - if ( - previousType.tag === currentType.tag && - previousType.kind === currentType.kind - ) { - exclude.push(previousIndex); - } - } - - result.push(currentType); - } - - return result.filter((type, index): unknown => !exclude.includes(index)); -} - -export type TypeMap = { [k in KindType | "fallback"]: ArrayObject }; -function compileMap(...typesList: Type[][]): TypeMap { - const result: TypeMap = { - fallback: {}, - mapping: {}, - scalar: {}, - sequence: {}, - }; - - for (const types of typesList) { - for (const type of types) { - if (type.kind !== null) { - result[type.kind][type.tag] = result["fallback"][type.tag] = type; - } - } - } - return result; -} - -export class Schema implements SchemaDefinition { - public static SCHEMA_DEFAULT?: Schema; - - public implicit: Type[]; - public explicit: Type[]; - public include: Schema[]; - - public compiledImplicit: Type[]; - public compiledExplicit: Type[]; - public compiledTypeMap: TypeMap; - - constructor(definition: SchemaDefinition) { - this.explicit = definition.explicit || []; - this.implicit = definition.implicit || []; - this.include = definition.include || []; - - for (const type of this.implicit) { - if (type.loadKind && type.loadKind !== "scalar") { - throw new YAMLError( - // eslint-disable-next-line max-len - "There is a non-scalar type in the implicit list of a schema. Implicit resolving of such types is not supported." - ); - } - } - - this.compiledImplicit = compileList(this, "implicit", []); - this.compiledExplicit = compileList(this, "explicit", []); - this.compiledTypeMap = compileMap( - this.compiledImplicit, - this.compiledExplicit - ); - } - - public static create(): void {} -} - -export interface SchemaDefinition { - implicit?: Any[]; - explicit?: Type[]; - include?: Schema[]; -} diff --git a/std/encoding/yaml/schema/core.ts b/std/encoding/yaml/schema/core.ts deleted file mode 100644 index 4fadc9bfe..000000000 --- a/std/encoding/yaml/schema/core.ts +++ /dev/null @@ -1,13 +0,0 @@ -// Ported from js-yaml v3.13.1: -// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da -// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. - -import { Schema } from "../schema.ts"; -import { json } from "./json.ts"; - -// Standard YAML's Core schema. -// http://www.yaml.org/spec/1.2/spec.html#id2804923 -export const core = new Schema({ - include: [json], -}); diff --git a/std/encoding/yaml/schema/default.ts b/std/encoding/yaml/schema/default.ts deleted file mode 100644 index 4c5ceeba7..000000000 --- a/std/encoding/yaml/schema/default.ts +++ /dev/null @@ -1,16 +0,0 @@ -// Ported from js-yaml v3.13.1: -// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da -// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. - -import { Schema } from "../schema.ts"; -import { binary, merge, omap, pairs, set, timestamp } from "../type/mod.ts"; -import { core } from "./core.ts"; - -// JS-YAML's default schema for `safeLoad` function. -// It is not described in the YAML specification. -export const def = new Schema({ - explicit: [binary, omap, pairs, set], - implicit: [timestamp, merge], - include: [core], -}); diff --git a/std/encoding/yaml/schema/failsafe.ts b/std/encoding/yaml/schema/failsafe.ts deleted file mode 100644 index 74e1897be..000000000 --- a/std/encoding/yaml/schema/failsafe.ts +++ /dev/null @@ -1,13 +0,0 @@ -// Ported from js-yaml v3.13.1: -// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da -// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. - -import { Schema } from "../schema.ts"; -import { map, seq, str } from "../type/mod.ts"; - -// Standard YAML's Failsafe schema. -// http://www.yaml.org/spec/1.2/spec.html#id2802346 -export const failsafe = new Schema({ - explicit: [str, seq, map], -}); diff --git a/std/encoding/yaml/schema/json.ts b/std/encoding/yaml/schema/json.ts deleted file mode 100644 index c30166fdf..000000000 --- a/std/encoding/yaml/schema/json.ts +++ /dev/null @@ -1,15 +0,0 @@ -// Ported from js-yaml v3.13.1: -// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da -// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. - -import { Schema } from "../schema.ts"; -import { bool, float, int, nil } from "../type/mod.ts"; -import { failsafe } from "./failsafe.ts"; - -// Standard YAML's JSON schema. -// http://www.yaml.org/spec/1.2/spec.html#id2803231 -export const json = new Schema({ - implicit: [nil, bool, int, float], - include: [failsafe], -}); diff --git a/std/encoding/yaml/schema/mod.ts b/std/encoding/yaml/schema/mod.ts deleted file mode 100644 index 7cbe0c283..000000000 --- a/std/encoding/yaml/schema/mod.ts +++ /dev/null @@ -1,9 +0,0 @@ -// Ported from js-yaml v3.13.1: -// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da -// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. - -export { core as CORE_SCHEMA } from "./core.ts"; -export { def as DEFAULT_SCHEMA } from "./default.ts"; -export { failsafe as FAILSAFE_SCHEMA } from "./failsafe.ts"; -export { json as JSON_SCHEMA } from "./json.ts"; diff --git a/std/encoding/yaml/state.ts b/std/encoding/yaml/state.ts deleted file mode 100644 index 6df6dc047..000000000 --- a/std/encoding/yaml/state.ts +++ /dev/null @@ -1,11 +0,0 @@ -// Ported from js-yaml v3.13.1: -// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da -// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. - -import { SchemaDefinition } from "./schema.ts"; -import { DEFAULT_SCHEMA } from "./schema/mod.ts"; - -export abstract class State { - constructor(public schema: SchemaDefinition = DEFAULT_SCHEMA) {} -} diff --git a/std/encoding/yaml/stringify.ts b/std/encoding/yaml/stringify.ts deleted file mode 100644 index f037631d9..000000000 --- a/std/encoding/yaml/stringify.ts +++ /dev/null @@ -1,18 +0,0 @@ -// Ported from js-yaml v3.13.1: -// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da -// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. - -import { dump } from "./dumper/dumper.ts"; -import { DumperStateOptions } from "./dumper/dumper_state.ts"; - -export type DumpOptions = DumperStateOptions; - -/** - * Serializes `object` as a YAML document. - * - * You can disable exceptions by setting the skipInvalid option to true. - */ -export function stringify(obj: object, options?: DumpOptions): string { - return dump(obj, options); -} diff --git a/std/encoding/yaml/stringify_test.ts b/std/encoding/yaml/stringify_test.ts deleted file mode 100644 index 03a3090d9..000000000 --- a/std/encoding/yaml/stringify_test.ts +++ /dev/null @@ -1,41 +0,0 @@ -// Ported from js-yaml v3.13.1: -// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da -// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. - -import { assertEquals } from "../../testing/asserts.ts"; -import { stringify } from "./stringify.ts"; - -Deno.test({ - name: "stringified correctly", - fn(): void { - const FIXTURE = { - foo: { - bar: true, - test: [ - "a", - "b", - { - a: false, - }, - { - a: false, - }, - ], - }, - test: "foobar", - }; - - const ASSERTS = `foo: - bar: true - test: - - a - - b - - a: false - - a: false -test: foobar -`; - - assertEquals(stringify(FIXTURE), ASSERTS); - }, -}); diff --git a/std/encoding/yaml/type.ts b/std/encoding/yaml/type.ts deleted file mode 100644 index 4a2c6bbac..000000000 --- a/std/encoding/yaml/type.ts +++ /dev/null @@ -1,55 +0,0 @@ -// Ported from js-yaml v3.13.1: -// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da -// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. - -import { ArrayObject, Any } from "./utils.ts"; - -export type KindType = "sequence" | "scalar" | "mapping"; -export type StyleVariant = "lowercase" | "uppercase" | "camelcase" | "decimal"; -export type RepresentFn = (data: Any, style?: StyleVariant) => Any; - -const DEFAULT_RESOLVE = (): boolean => true; -const DEFAULT_CONSTRUCT = (data: Any): Any => data; - -interface TypeOptions { - kind: KindType; - resolve?: (data: Any) => boolean; - construct?: (data: string) => Any; - instanceOf?: Any; - predicate?: (data: object) => boolean; - represent?: RepresentFn | ArrayObject; - defaultStyle?: StyleVariant; - styleAliases?: ArrayObject; -} - -function checkTagFormat(tag: string): string { - return tag; -} - -export class Type { - public tag: string; - public kind: KindType | null = null; - public instanceOf: Any; - public predicate?: (data: object) => boolean; - public represent?: RepresentFn | ArrayObject; - public defaultStyle?: StyleVariant; - public styleAliases?: ArrayObject; - public loadKind?: KindType; - - constructor(tag: string, options?: TypeOptions) { - this.tag = checkTagFormat(tag); - if (options) { - this.kind = options.kind; - this.resolve = options.resolve || DEFAULT_RESOLVE; - this.construct = options.construct || DEFAULT_CONSTRUCT; - this.instanceOf = options.instanceOf; - this.predicate = options.predicate; - this.represent = options.represent; - this.defaultStyle = options.defaultStyle; - this.styleAliases = options.styleAliases; - } - } - public resolve: (data?: Any) => boolean = (): boolean => true; - public construct: (data?: Any) => Any = (data): Any => data; -} diff --git a/std/encoding/yaml/type/binary.ts b/std/encoding/yaml/type/binary.ts deleted file mode 100644 index f4823b3f7..000000000 --- a/std/encoding/yaml/type/binary.ts +++ /dev/null @@ -1,139 +0,0 @@ -// Ported from js-yaml v3.13.1: -// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. -// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. - -import { Type } from "../type.ts"; -import { Any } from "../utils.ts"; - -const { Buffer } = Deno; - -// [ 64, 65, 66 ] -> [ padding, CR, LF ] -const BASE64_MAP = - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\n\r"; - -function resolveYamlBinary(data: Any): boolean { - if (data === null) return false; - - let code: number; - let bitlen = 0; - const max = data.length; - const map = BASE64_MAP; - - // Convert one by one. - for (let idx = 0; idx < max; idx++) { - code = map.indexOf(data.charAt(idx)); - - // Skip CR/LF - if (code > 64) continue; - - // Fail on illegal characters - if (code < 0) return false; - - bitlen += 6; - } - - // If there are any bits left, source was corrupted - return bitlen % 8 === 0; -} - -function constructYamlBinary(data: string): Deno.Buffer { - // remove CR/LF & padding to simplify scan - const input = data.replace(/[\r\n=]/g, ""); - const max = input.length; - const map = BASE64_MAP; - - // Collect by 6*4 bits (3 bytes) - - const result = []; - let bits = 0; - for (let idx = 0; idx < max; idx++) { - if (idx % 4 === 0 && idx) { - result.push((bits >> 16) & 0xff); - result.push((bits >> 8) & 0xff); - result.push(bits & 0xff); - } - - bits = (bits << 6) | map.indexOf(input.charAt(idx)); - } - - // Dump tail - - const tailbits = (max % 4) * 6; - - if (tailbits === 0) { - result.push((bits >> 16) & 0xff); - result.push((bits >> 8) & 0xff); - result.push(bits & 0xff); - } else if (tailbits === 18) { - result.push((bits >> 10) & 0xff); - result.push((bits >> 2) & 0xff); - } else if (tailbits === 12) { - result.push((bits >> 4) & 0xff); - } - - return new Buffer(new Uint8Array(result)); -} - -function representYamlBinary(object: Uint8Array): string { - const max = object.length; - const map = BASE64_MAP; - - // Convert every three bytes to 4 ASCII characters. - - let result = ""; - let bits = 0; - for (let idx = 0; idx < max; idx++) { - if (idx % 3 === 0 && idx) { - result += map[(bits >> 18) & 0x3f]; - result += map[(bits >> 12) & 0x3f]; - result += map[(bits >> 6) & 0x3f]; - result += map[bits & 0x3f]; - } - - bits = (bits << 8) + object[idx]; - } - - // Dump tail - - const tail = max % 3; - - if (tail === 0) { - result += map[(bits >> 18) & 0x3f]; - result += map[(bits >> 12) & 0x3f]; - result += map[(bits >> 6) & 0x3f]; - result += map[bits & 0x3f]; - } else if (tail === 2) { - result += map[(bits >> 10) & 0x3f]; - result += map[(bits >> 4) & 0x3f]; - result += map[(bits << 2) & 0x3f]; - result += map[64]; - } else if (tail === 1) { - result += map[(bits >> 2) & 0x3f]; - result += map[(bits << 4) & 0x3f]; - result += map[64]; - result += map[64]; - } - - return result; -} - -function isBinary(obj: Any): obj is Deno.Buffer { - const buf = new Buffer(); - try { - if (0 > buf.readFromSync(obj as Deno.Buffer)) return true; - return false; - } catch { - return false; - } finally { - buf.reset(); - } -} - -export const binary = new Type("tag:yaml.org,2002:binary", { - construct: constructYamlBinary, - kind: "scalar", - predicate: isBinary, - represent: representYamlBinary, - resolve: resolveYamlBinary, -}); diff --git a/std/encoding/yaml/type/bool.ts b/std/encoding/yaml/type/bool.ts deleted file mode 100644 index a5a85cf9e..000000000 --- a/std/encoding/yaml/type/bool.ts +++ /dev/null @@ -1,39 +0,0 @@ -// Ported from js-yaml v3.13.1: -// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da -// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. - -import { Type } from "../type.ts"; -import { isBoolean } from "../utils.ts"; - -function resolveYamlBoolean(data: string): boolean { - const max = data.length; - - return ( - (max === 4 && (data === "true" || data === "True" || data === "TRUE")) || - (max === 5 && (data === "false" || data === "False" || data === "FALSE")) - ); -} - -function constructYamlBoolean(data: string): boolean { - return data === "true" || data === "True" || data === "TRUE"; -} - -export const bool = new Type("tag:yaml.org,2002:bool", { - construct: constructYamlBoolean, - defaultStyle: "lowercase", - kind: "scalar", - predicate: isBoolean, - represent: { - lowercase(object: boolean): string { - return object ? "true" : "false"; - }, - uppercase(object: boolean): string { - return object ? "TRUE" : "FALSE"; - }, - camelcase(object: boolean): string { - return object ? "True" : "False"; - }, - }, - resolve: resolveYamlBoolean, -}); diff --git a/std/encoding/yaml/type/float.ts b/std/encoding/yaml/type/float.ts deleted file mode 100644 index 5ae0689b2..000000000 --- a/std/encoding/yaml/type/float.ts +++ /dev/null @@ -1,125 +0,0 @@ -// Ported from js-yaml v3.13.1: -// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da -// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. - -import { StyleVariant, Type } from "../type.ts"; -import { isNegativeZero, Any } from "../utils.ts"; - -const YAML_FLOAT_PATTERN = new RegExp( - // 2.5e4, 2.5 and integers - "^(?:[-+]?(?:0|[1-9][0-9_]*)(?:\\.[0-9_]*)?(?:[eE][-+]?[0-9]+)?" + - // .2e4, .2 - // special case, seems not from spec - "|\\.[0-9_]+(?:[eE][-+]?[0-9]+)?" + - // 20:59 - "|[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+\\.[0-9_]*" + - // .inf - "|[-+]?\\.(?:inf|Inf|INF)" + - // .nan - "|\\.(?:nan|NaN|NAN))$" -); - -function resolveYamlFloat(data: string): boolean { - if ( - !YAML_FLOAT_PATTERN.test(data) || - // Quick hack to not allow integers end with `_` - // Probably should update regexp & check speed - data[data.length - 1] === "_" - ) { - return false; - } - - return true; -} - -function constructYamlFloat(data: string): number { - let value = data.replace(/_/g, "").toLowerCase(); - const sign = value[0] === "-" ? -1 : 1; - const digits: number[] = []; - - if ("+-".indexOf(value[0]) >= 0) { - value = value.slice(1); - } - - if (value === ".inf") { - return sign === 1 ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY; - } - if (value === ".nan") { - return NaN; - } - if (value.indexOf(":") >= 0) { - value.split(":").forEach((v): void => { - digits.unshift(parseFloat(v)); - }); - - let valueNb = 0.0; - let base = 1; - - digits.forEach((d): void => { - valueNb += d * base; - base *= 60; - }); - - return sign * valueNb; - } - return sign * parseFloat(value); -} - -const SCIENTIFIC_WITHOUT_DOT = /^[-+]?[0-9]+e/; - -function representYamlFloat(object: Any, style?: StyleVariant): Any { - if (isNaN(object)) { - switch (style) { - case "lowercase": - return ".nan"; - case "uppercase": - return ".NAN"; - case "camelcase": - return ".NaN"; - } - } else if (Number.POSITIVE_INFINITY === object) { - switch (style) { - case "lowercase": - return ".inf"; - case "uppercase": - return ".INF"; - case "camelcase": - return ".Inf"; - } - } else if (Number.NEGATIVE_INFINITY === object) { - switch (style) { - case "lowercase": - return "-.inf"; - case "uppercase": - return "-.INF"; - case "camelcase": - return "-.Inf"; - } - } else if (isNegativeZero(object)) { - return "-0.0"; - } - - const res = object.toString(10); - - // JS stringifier can build scientific format without dots: 5e-100, - // while YAML requres dot: 5.e-100. Fix it with simple hack - - return SCIENTIFIC_WITHOUT_DOT.test(res) ? res.replace("e", ".e") : res; -} - -function isFloat(object: Any): boolean { - return ( - Object.prototype.toString.call(object) === "[object Number]" && - (object % 1 !== 0 || isNegativeZero(object)) - ); -} - -export const float = new Type("tag:yaml.org,2002:float", { - construct: constructYamlFloat, - defaultStyle: "lowercase", - kind: "scalar", - predicate: isFloat, - represent: representYamlFloat, - resolve: resolveYamlFloat, -}); diff --git a/std/encoding/yaml/type/int.ts b/std/encoding/yaml/type/int.ts deleted file mode 100644 index 6a86aafe9..000000000 --- a/std/encoding/yaml/type/int.ts +++ /dev/null @@ -1,188 +0,0 @@ -// Ported from js-yaml v3.13.1: -// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da -// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. - -import { Type } from "../type.ts"; -import { isNegativeZero, Any } from "../utils.ts"; - -function isHexCode(c: number): boolean { - return ( - (0x30 <= /* 0 */ c && c <= 0x39) /* 9 */ || - (0x41 <= /* A */ c && c <= 0x46) /* F */ || - (0x61 <= /* a */ c && c <= 0x66) /* f */ - ); -} - -function isOctCode(c: number): boolean { - return 0x30 <= /* 0 */ c && c <= 0x37 /* 7 */; -} - -function isDecCode(c: number): boolean { - return 0x30 <= /* 0 */ c && c <= 0x39 /* 9 */; -} - -function resolveYamlInteger(data: string): boolean { - const max = data.length; - let index = 0; - let hasDigits = false; - - if (!max) return false; - - let ch = data[index]; - - // sign - if (ch === "-" || ch === "+") { - ch = data[++index]; - } - - if (ch === "0") { - // 0 - if (index + 1 === max) return true; - ch = data[++index]; - - // base 2, base 8, base 16 - - if (ch === "b") { - // base 2 - index++; - - for (; index < max; index++) { - ch = data[index]; - if (ch === "_") continue; - if (ch !== "0" && ch !== "1") return false; - hasDigits = true; - } - return hasDigits && ch !== "_"; - } - - if (ch === "x") { - // base 16 - index++; - - for (; index < max; index++) { - ch = data[index]; - if (ch === "_") continue; - if (!isHexCode(data.charCodeAt(index))) return false; - hasDigits = true; - } - return hasDigits && ch !== "_"; - } - - // base 8 - for (; index < max; index++) { - ch = data[index]; - if (ch === "_") continue; - if (!isOctCode(data.charCodeAt(index))) return false; - hasDigits = true; - } - return hasDigits && ch !== "_"; - } - - // base 10 (except 0) or base 60 - - // value should not start with `_`; - if (ch === "_") return false; - - for (; index < max; index++) { - ch = data[index]; - if (ch === "_") continue; - if (ch === ":") break; - if (!isDecCode(data.charCodeAt(index))) { - return false; - } - hasDigits = true; - } - - // Should have digits and should not end with `_` - if (!hasDigits || ch === "_") return false; - - // if !base60 - done; - if (ch !== ":") return true; - - // base60 almost not used, no needs to optimize - return /^(:[0-5]?[0-9])+$/.test(data.slice(index)); -} - -function constructYamlInteger(data: string): number { - let value = data; - const digits: number[] = []; - - if (value.indexOf("_") !== -1) { - value = value.replace(/_/g, ""); - } - - let sign = 1; - let ch = value[0]; - if (ch === "-" || ch === "+") { - if (ch === "-") sign = -1; - value = value.slice(1); - ch = value[0]; - } - - if (value === "0") return 0; - - if (ch === "0") { - if (value[1] === "b") return sign * parseInt(value.slice(2), 2); - if (value[1] === "x") return sign * parseInt(value, 16); - return sign * parseInt(value, 8); - } - - if (value.indexOf(":") !== -1) { - value.split(":").forEach((v): void => { - digits.unshift(parseInt(v, 10)); - }); - - let valueInt = 0; - let base = 1; - - digits.forEach((d): void => { - valueInt += d * base; - base *= 60; - }); - - return sign * valueInt; - } - - return sign * parseInt(value, 10); -} - -function isInteger(object: Any): boolean { - return ( - Object.prototype.toString.call(object) === "[object Number]" && - object % 1 === 0 && - !isNegativeZero(object) - ); -} - -export const int = new Type("tag:yaml.org,2002:int", { - construct: constructYamlInteger, - defaultStyle: "decimal", - kind: "scalar", - predicate: isInteger, - represent: { - binary(obj: number): string { - return obj >= 0 - ? `0b${obj.toString(2)}` - : `-0b${obj.toString(2).slice(1)}`; - }, - octal(obj: number): string { - return obj >= 0 ? `0${obj.toString(8)}` : `-0${obj.toString(8).slice(1)}`; - }, - decimal(obj: number): string { - return obj.toString(10); - }, - hexadecimal(obj: number): string { - return obj >= 0 - ? `0x${obj.toString(16).toUpperCase()}` - : `-0x${obj.toString(16).toUpperCase().slice(1)}`; - }, - }, - resolve: resolveYamlInteger, - styleAliases: { - binary: [2, "bin"], - decimal: [10, "dec"], - hexadecimal: [16, "hex"], - octal: [8, "oct"], - }, -}); diff --git a/std/encoding/yaml/type/map.ts b/std/encoding/yaml/type/map.ts deleted file mode 100644 index dcd99abca..000000000 --- a/std/encoding/yaml/type/map.ts +++ /dev/null @@ -1,14 +0,0 @@ -// Ported from js-yaml v3.13.1: -// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da -// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. - -import { Type } from "../type.ts"; -import { Any } from "../utils.ts"; - -export const map = new Type("tag:yaml.org,2002:map", { - construct(data): Any { - return data !== null ? data : {}; - }, - kind: "mapping", -}); diff --git a/std/encoding/yaml/type/merge.ts b/std/encoding/yaml/type/merge.ts deleted file mode 100644 index 68314bf2e..000000000 --- a/std/encoding/yaml/type/merge.ts +++ /dev/null @@ -1,15 +0,0 @@ -// Ported from js-yaml v3.13.1: -// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da -// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. - -import { Type } from "../type.ts"; - -function resolveYamlMerge(data: string): boolean { - return data === "<<" || data === null; -} - -export const merge = new Type("tag:yaml.org,2002:merge", { - kind: "scalar", - resolve: resolveYamlMerge, -}); diff --git a/std/encoding/yaml/type/mod.ts b/std/encoding/yaml/type/mod.ts deleted file mode 100644 index 15f33301e..000000000 --- a/std/encoding/yaml/type/mod.ts +++ /dev/null @@ -1,18 +0,0 @@ -// Ported from js-yaml v3.13.1: -// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da -// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. - -export { binary } from "./binary.ts"; -export { bool } from "./bool.ts"; -export { float } from "./float.ts"; -export { int } from "./int.ts"; -export { map } from "./map.ts"; -export { merge } from "./merge.ts"; -export { nil } from "./nil.ts"; -export { omap } from "./omap.ts"; -export { pairs } from "./pairs.ts"; -export { seq } from "./seq.ts"; -export { set } from "./set.ts"; -export { str } from "./str.ts"; -export { timestamp } from "./timestamp.ts"; diff --git a/std/encoding/yaml/type/nil.ts b/std/encoding/yaml/type/nil.ts deleted file mode 100644 index 8a48d02fb..000000000 --- a/std/encoding/yaml/type/nil.ts +++ /dev/null @@ -1,45 +0,0 @@ -// Ported from js-yaml v3.13.1: -// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da -// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. - -import { Type } from "../type.ts"; - -function resolveYamlNull(data: string): boolean { - const max = data.length; - - return ( - (max === 1 && data === "~") || - (max === 4 && (data === "null" || data === "Null" || data === "NULL")) - ); -} - -function constructYamlNull(): null { - return null; -} - -function isNull(object: unknown): object is null { - return object === null; -} - -export const nil = new Type("tag:yaml.org,2002:null", { - construct: constructYamlNull, - defaultStyle: "lowercase", - kind: "scalar", - predicate: isNull, - represent: { - canonical(): string { - return "~"; - }, - lowercase(): string { - return "null"; - }, - uppercase(): string { - return "NULL"; - }, - camelcase(): string { - return "Null"; - }, - }, - resolve: resolveYamlNull, -}); diff --git a/std/encoding/yaml/type/omap.ts b/std/encoding/yaml/type/omap.ts deleted file mode 100644 index d6d751505..000000000 --- a/std/encoding/yaml/type/omap.ts +++ /dev/null @@ -1,46 +0,0 @@ -// Ported from js-yaml v3.13.1: -// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da -// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. - -import { Type } from "../type.ts"; -import { Any } from "../utils.ts"; - -const _hasOwnProperty = Object.prototype.hasOwnProperty; -const _toString = Object.prototype.toString; - -function resolveYamlOmap(data: Any): boolean { - const objectKeys: string[] = []; - let pairKey = ""; - let pairHasKey = false; - - for (const pair of data) { - pairHasKey = false; - - if (_toString.call(pair) !== "[object Object]") return false; - - for (pairKey in pair) { - if (_hasOwnProperty.call(pair, pairKey)) { - if (!pairHasKey) pairHasKey = true; - else return false; - } - } - - if (!pairHasKey) return false; - - if (objectKeys.indexOf(pairKey) === -1) objectKeys.push(pairKey); - else return false; - } - - return true; -} - -function constructYamlOmap(data: Any): Any { - return data !== null ? data : []; -} - -export const omap = new Type("tag:yaml.org,2002:omap", { - construct: constructYamlOmap, - kind: "sequence", - resolve: resolveYamlOmap, -}); diff --git a/std/encoding/yaml/type/pairs.ts b/std/encoding/yaml/type/pairs.ts deleted file mode 100644 index e999748ae..000000000 --- a/std/encoding/yaml/type/pairs.ts +++ /dev/null @@ -1,49 +0,0 @@ -// Ported from js-yaml v3.13.1: -// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da -// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. - -import { Type } from "../type.ts"; -import { Any } from "../utils.ts"; - -const _toString = Object.prototype.toString; - -function resolveYamlPairs(data: Any[][]): boolean { - const result = new Array(data.length); - - for (let index = 0; index < data.length; index++) { - const pair = data[index]; - - if (_toString.call(pair) !== "[object Object]") return false; - - const keys = Object.keys(pair); - - if (keys.length !== 1) return false; - - result[index] = [keys[0], pair[keys[0] as Any]]; - } - - return true; -} - -function constructYamlPairs(data: string): Any[] { - if (data === null) return []; - - const result = new Array(data.length); - - for (let index = 0; index < data.length; index += 1) { - const pair = data[index]; - - const keys = Object.keys(pair); - - result[index] = [keys[0], pair[keys[0] as Any]]; - } - - return result; -} - -export const pairs = new Type("tag:yaml.org,2002:pairs", { - construct: constructYamlPairs, - kind: "sequence", - resolve: resolveYamlPairs, -}); diff --git a/std/encoding/yaml/type/seq.ts b/std/encoding/yaml/type/seq.ts deleted file mode 100644 index b19565dbc..000000000 --- a/std/encoding/yaml/type/seq.ts +++ /dev/null @@ -1,14 +0,0 @@ -// Ported from js-yaml v3.13.1: -// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da -// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. - -import { Type } from "../type.ts"; -import { Any } from "../utils.ts"; - -export const seq = new Type("tag:yaml.org,2002:seq", { - construct(data): Any { - return data !== null ? data : []; - }, - kind: "sequence", -}); diff --git a/std/encoding/yaml/type/set.ts b/std/encoding/yaml/type/set.ts deleted file mode 100644 index 0bfe1c8db..000000000 --- a/std/encoding/yaml/type/set.ts +++ /dev/null @@ -1,31 +0,0 @@ -// Ported from js-yaml v3.13.1: -// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da -// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. - -import { Type } from "../type.ts"; -import { Any } from "../utils.ts"; - -const _hasOwnProperty = Object.prototype.hasOwnProperty; - -function resolveYamlSet(data: Any): boolean { - if (data === null) return true; - - for (const key in data) { - if (_hasOwnProperty.call(data, key)) { - if (data[key] !== null) return false; - } - } - - return true; -} - -function constructYamlSet(data: string): Any { - return data !== null ? data : {}; -} - -export const set = new Type("tag:yaml.org,2002:set", { - construct: constructYamlSet, - kind: "mapping", - resolve: resolveYamlSet, -}); diff --git a/std/encoding/yaml/type/str.ts b/std/encoding/yaml/type/str.ts deleted file mode 100644 index cd6e9430f..000000000 --- a/std/encoding/yaml/type/str.ts +++ /dev/null @@ -1,12 +0,0 @@ -// Ported from js-yaml v3.13.1: -// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. - -import { Type } from "../type.ts"; - -export const str = new Type("tag:yaml.org,2002:str", { - construct(data): string { - return data !== null ? data : ""; - }, - kind: "scalar", -}); diff --git a/std/encoding/yaml/type/timestamp.ts b/std/encoding/yaml/type/timestamp.ts deleted file mode 100644 index eb03b3825..000000000 --- a/std/encoding/yaml/type/timestamp.ts +++ /dev/null @@ -1,96 +0,0 @@ -// Ported from js-yaml v3.13.1: -// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da -// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. - -import { Type } from "../type.ts"; - -const YAML_DATE_REGEXP = new RegExp( - "^([0-9][0-9][0-9][0-9])" + // [1] year - "-([0-9][0-9])" + // [2] month - "-([0-9][0-9])$" // [3] day -); - -const YAML_TIMESTAMP_REGEXP = new RegExp( - "^([0-9][0-9][0-9][0-9])" + // [1] year - "-([0-9][0-9]?)" + // [2] month - "-([0-9][0-9]?)" + // [3] day - "(?:[Tt]|[ \\t]+)" + // ... - "([0-9][0-9]?)" + // [4] hour - ":([0-9][0-9])" + // [5] minute - ":([0-9][0-9])" + // [6] second - "(?:\\.([0-9]*))?" + // [7] fraction - "(?:[ \\t]*(Z|([-+])([0-9][0-9]?)" + // [8] tz [9] tz_sign [10] tz_hour - "(?::([0-9][0-9]))?))?$" // [11] tz_minute -); - -function resolveYamlTimestamp(data: string): boolean { - if (data === null) return false; - if (YAML_DATE_REGEXP.exec(data) !== null) return true; - if (YAML_TIMESTAMP_REGEXP.exec(data) !== null) return true; - return false; -} - -function constructYamlTimestamp(data: string): Date { - let match = YAML_DATE_REGEXP.exec(data); - if (match === null) match = YAML_TIMESTAMP_REGEXP.exec(data); - - if (match === null) throw new Error("Date resolve error"); - - // match: [1] year [2] month [3] day - - const year = +match[1]; - const month = +match[2] - 1; // JS month starts with 0 - const day = +match[3]; - - if (!match[4]) { - // no hour - return new Date(Date.UTC(year, month, day)); - } - - // match: [4] hour [5] minute [6] second [7] fraction - - const hour = +match[4]; - const minute = +match[5]; - const second = +match[6]; - - let fraction = 0; - if (match[7]) { - let partFraction = match[7].slice(0, 3); - while (partFraction.length < 3) { - // milli-seconds - partFraction += "0"; - } - fraction = +partFraction; - } - - // match: [8] tz [9] tz_sign [10] tz_hour [11] tz_minute - - let delta = null; - if (match[9]) { - const tzHour = +match[10]; - const tzMinute = +(match[11] || 0); - delta = (tzHour * 60 + tzMinute) * 60000; // delta in mili-seconds - if (match[9] === "-") delta = -delta; - } - - const date = new Date( - Date.UTC(year, month, day, hour, minute, second, fraction) - ); - - if (delta) date.setTime(date.getTime() - delta); - - return date; -} - -function representYamlTimestamp(date: Date): string { - return date.toISOString(); -} - -export const timestamp = new Type("tag:yaml.org,2002:timestamp", { - construct: constructYamlTimestamp, - instanceOf: Date, - kind: "scalar", - represent: representYamlTimestamp, - resolve: resolveYamlTimestamp, -}); diff --git a/std/encoding/yaml/utils.ts b/std/encoding/yaml/utils.ts deleted file mode 100644 index 4630a45a2..000000000 --- a/std/encoding/yaml/utils.ts +++ /dev/null @@ -1,80 +0,0 @@ -// Ported from js-yaml v3.13.1: -// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da -// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. - -/* eslint-disable-next-line @typescript-eslint/no-explicit-any */ -export type Any = any; - -export function isNothing(subject: unknown): subject is never { - return typeof subject === "undefined" || subject === null; -} - -export function isArray(value: unknown): value is Any[] { - return Array.isArray(value); -} - -export function isBoolean(value: unknown): value is boolean { - return typeof value === "boolean" || value instanceof Boolean; -} - -export function isNull(value: unknown): value is null { - return value === null; -} - -export function isNumber(value: unknown): value is number { - return typeof value === "number" || value instanceof Number; -} - -export function isString(value: unknown): value is string { - return typeof value === "string" || value instanceof String; -} - -export function isSymbol(value: unknown): value is symbol { - return typeof value === "symbol"; -} - -export function isUndefined(value: unknown): value is undefined { - return value === undefined; -} - -export function isObject(value: unknown): value is object { - return value !== null && typeof value === "object"; -} - -export function isError(e: unknown): boolean { - return e instanceof Error; -} - -export function isFunction(value: unknown): value is () => void { - return typeof value === "function"; -} - -export function isRegExp(value: unknown): value is RegExp { - return value instanceof RegExp; -} - -export function toArray(sequence: T): T | [] | [T] { - if (isArray(sequence)) return sequence; - if (isNothing(sequence)) return []; - - return [sequence]; -} - -export function repeat(str: string, count: number): string { - let result = ""; - - for (let cycle = 0; cycle < count; cycle++) { - result += str; - } - - return result; -} - -export function isNegativeZero(i: number): boolean { - return i === 0 && Number.NEGATIVE_INFINITY === 1 / i; -} - -export interface ArrayObject { - [P: string]: T; -} diff --git a/std/encoding/yaml_test.ts b/std/encoding/yaml_test.ts index a5a8374cc..d65be604c 100644 --- a/std/encoding/yaml_test.ts +++ b/std/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/std/examples/chat/server_test.ts b/std/examples/chat/server_test.ts index 92eb50f92..7375de47a 100644 --- a/std/examples/chat/server_test.ts +++ b/std/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/std/examples/flags.ts b/std/examples/flags.ts new file mode 100644 index 000000000..4625b8c96 --- /dev/null +++ b/std/examples/flags.ts @@ -0,0 +1,7 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +const { args } = Deno; +import { parse } from "../flags/mod.ts"; + +if (import.meta.main) { + console.dir(parse(args)); +} diff --git a/std/flags/README.md b/std/flags/README.md index e9764f913..0b7f7fa65 100644 --- a/std/flags/README.md +++ b/std/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/std/flags/example.ts b/std/flags/example.ts deleted file mode 100644 index ad0317269..000000000 --- a/std/flags/example.ts +++ /dev/null @@ -1,5 +0,0 @@ -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -const { args } = Deno; -import { parse } from "./mod.ts"; - -console.dir(parse(args)); diff --git a/std/flags/mod.ts b/std/flags/mod.ts index 0563dab61..e3680087d 100644 --- a/std/flags/mod.ts +++ b/std/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/std/fs/_util.ts b/std/fs/_util.ts new file mode 100644 index 000000000..48a98a0b1 --- /dev/null +++ b/std/fs/_util.ts @@ -0,0 +1,45 @@ +import * as path from "../path/mod.ts"; + +/** + * Test whether or not `dest` is a sub-directory of `src` + * @param src src file path + * @param dest dest file path + * @param sep path separator + */ +export function isSubdir( + src: string, + dest: string, + sep: string = path.sep +): boolean { + if (src === dest) { + return false; + } + const srcArray = src.split(sep); + const destArray = dest.split(sep); + // see: https://github.com/Microsoft/TypeScript/issues/30821 + return srcArray.reduce( + // @ts-ignore + (acc: true, current: string, i: number): boolean => { + return acc && destArray[i] === current; + }, + true + ); +} + +export type PathType = "file" | "dir" | "symlink"; + +/** + * Get a human readable file type string. + * + * @param fileInfo A FileInfo describes a file and is returned by `stat`, + * `lstat` + */ +export function getFileInfoType(fileInfo: Deno.FileInfo): PathType | undefined { + return fileInfo.isFile + ? "file" + : fileInfo.isDirectory + ? "dir" + : fileInfo.isSymlink + ? "symlink" + : undefined; +} diff --git a/std/fs/_util_test.ts b/std/fs/_util_test.ts new file mode 100644 index 000000000..48fc33ecd --- /dev/null +++ b/std/fs/_util_test.ts @@ -0,0 +1,63 @@ +// Copyright the Browserify authors. MIT License. + +import { assertEquals } from "../testing/asserts.ts"; +import * as path from "../path/mod.ts"; +import { isSubdir, getFileInfoType, PathType } from "./_util.ts"; +import { ensureFileSync } from "./ensure_file.ts"; +import { ensureDirSync } from "./ensure_dir.ts"; + +const testdataDir = path.resolve("fs", "testdata"); + +Deno.test("_isSubdir", function (): void { + const pairs = [ + ["", "", false, path.posix.sep], + ["/first/second", "/first", false, path.posix.sep], + ["/first", "/first", false, path.posix.sep], + ["/first", "/first/second", true, path.posix.sep], + ["first", "first/second", true, path.posix.sep], + ["../first", "../first/second", true, path.posix.sep], + ["c:\\first", "c:\\first", false, path.win32.sep], + ["c:\\first", "c:\\first\\second", true, path.win32.sep], + ]; + + pairs.forEach(function (p): void { + const src = p[0] as string; + const dest = p[1] as string; + const expected = p[2] as boolean; + const sep = p[3] as string; + assertEquals( + isSubdir(src, dest, sep), + expected, + `'${src}' should ${expected ? "" : "not"} be parent dir of '${dest}'` + ); + }); +}); + +Deno.test("_getFileInfoType", function (): void { + const pairs = [ + [path.join(testdataDir, "file_type_1"), "file"], + [path.join(testdataDir, "file_type_dir_1"), "dir"], + ]; + + pairs.forEach(function (p): void { + const filePath = p[0] as string; + const type = p[1] as PathType; + switch (type) { + case "file": + ensureFileSync(filePath); + break; + case "dir": + ensureDirSync(filePath); + break; + case "symlink": + // TODO(axetroy): test symlink + break; + } + + const stat = Deno.statSync(filePath); + + Deno.removeSync(filePath, { recursive: true }); + + assertEquals(getFileInfoType(stat), type); + }); +}); diff --git a/std/fs/copy.ts b/std/fs/copy.ts index 609a8437b..85558d905 100644 --- a/std/fs/copy.ts +++ b/std/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/std/fs/ensure_dir.ts b/std/fs/ensure_dir.ts index ecc7356ef..34053b157 100644 --- a/std/fs/ensure_dir.ts +++ b/std/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/std/fs/ensure_file.ts b/std/fs/ensure_file.ts index de6cab500..2539c71ac 100644 --- a/std/fs/ensure_file.ts +++ b/std/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/std/fs/ensure_link.ts b/std/fs/ensure_link.ts index e43325a25..f446df781 100644 --- a/std/fs/ensure_link.ts +++ b/std/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/std/fs/ensure_symlink.ts b/std/fs/ensure_symlink.ts index 03c355b5d..2a184bb4f 100644 --- a/std/fs/ensure_symlink.ts +++ b/std/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/std/fs/expand_glob.ts b/std/fs/expand_glob.ts index 803c67cdf..e5abdabcf 100644 --- a/std/fs/expand_glob.ts +++ b/std/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/std/fs/move.ts b/std/fs/move.ts index cf1e49193..4a6395365 100644 --- a/std/fs/move.ts +++ b/std/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/std/fs/utils.ts b/std/fs/utils.ts deleted file mode 100644 index 48a98a0b1..000000000 --- a/std/fs/utils.ts +++ /dev/null @@ -1,45 +0,0 @@ -import * as path from "../path/mod.ts"; - -/** - * Test whether or not `dest` is a sub-directory of `src` - * @param src src file path - * @param dest dest file path - * @param sep path separator - */ -export function isSubdir( - src: string, - dest: string, - sep: string = path.sep -): boolean { - if (src === dest) { - return false; - } - const srcArray = src.split(sep); - const destArray = dest.split(sep); - // see: https://github.com/Microsoft/TypeScript/issues/30821 - return srcArray.reduce( - // @ts-ignore - (acc: true, current: string, i: number): boolean => { - return acc && destArray[i] === current; - }, - true - ); -} - -export type PathType = "file" | "dir" | "symlink"; - -/** - * Get a human readable file type string. - * - * @param fileInfo A FileInfo describes a file and is returned by `stat`, - * `lstat` - */ -export function getFileInfoType(fileInfo: Deno.FileInfo): PathType | undefined { - return fileInfo.isFile - ? "file" - : fileInfo.isDirectory - ? "dir" - : fileInfo.isSymlink - ? "symlink" - : undefined; -} diff --git a/std/fs/utils_test.ts b/std/fs/utils_test.ts deleted file mode 100644 index e104e3e7d..000000000 --- a/std/fs/utils_test.ts +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright the Browserify authors. MIT License. - -import { assertEquals } from "../testing/asserts.ts"; -import * as path from "../path/mod.ts"; -import { isSubdir, getFileInfoType, PathType } from "./utils.ts"; -import { ensureFileSync } from "./ensure_file.ts"; -import { ensureDirSync } from "./ensure_dir.ts"; - -const testdataDir = path.resolve("fs", "testdata"); - -Deno.test("_isSubdir", function (): void { - const pairs = [ - ["", "", false, path.posix.sep], - ["/first/second", "/first", false, path.posix.sep], - ["/first", "/first", false, path.posix.sep], - ["/first", "/first/second", true, path.posix.sep], - ["first", "first/second", true, path.posix.sep], - ["../first", "../first/second", true, path.posix.sep], - ["c:\\first", "c:\\first", false, path.win32.sep], - ["c:\\first", "c:\\first\\second", true, path.win32.sep], - ]; - - pairs.forEach(function (p): void { - const src = p[0] as string; - const dest = p[1] as string; - const expected = p[2] as boolean; - const sep = p[3] as string; - assertEquals( - isSubdir(src, dest, sep), - expected, - `'${src}' should ${expected ? "" : "not"} be parent dir of '${dest}'` - ); - }); -}); - -Deno.test("_getFileInfoType", function (): void { - const pairs = [ - [path.join(testdataDir, "file_type_1"), "file"], - [path.join(testdataDir, "file_type_dir_1"), "dir"], - ]; - - pairs.forEach(function (p): void { - const filePath = p[0] as string; - const type = p[1] as PathType; - switch (type) { - case "file": - ensureFileSync(filePath); - break; - case "dir": - ensureDirSync(filePath); - break; - case "symlink": - // TODO(axetroy): test symlink - break; - } - - const stat = Deno.statSync(filePath); - - Deno.removeSync(filePath, { recursive: true }); - - assertEquals(getFileInfoType(stat), type); - }); -}); diff --git a/std/hash/sha1.ts b/std/hash/sha1.ts new file mode 100644 index 000000000..f47192554 --- /dev/null +++ b/std/hash/sha1.ts @@ -0,0 +1,374 @@ +/* + * [js-sha1]{@link https://github.com/emn178/js-sha1} + * + * @version 0.6.0 + * @author Chen, Yi-Cyuan [emn178@gmail.com] + * @copyright Chen, Yi-Cyuan 2014-2017 + * @license MIT + */ + +const HEX_CHARS = "0123456789abcdef".split(""); +const EXTRA = Uint32Array.of(-2147483648, 8388608, 32768, 128); +const SHIFT = Uint32Array.of(24, 16, 8, 0); + +const blocks = new Uint32Array(80); + +export class Sha1 { + #blocks: Uint32Array; + #block: number; + #start: number; + #bytes: number; + #hBytes: number; + #finalized: boolean; + #hashed: boolean; + + #h0 = 0x67452301; + #h1 = 0xefcdab89; + #h2 = 0x98badcfe; + #h3 = 0x10325476; + #h4 = 0xc3d2e1f0; + #lastByteIndex = 0; + + constructor(sharedMemory = false) { + if (sharedMemory) { + this.#blocks = blocks.fill(0, 0, 17); + } else { + this.#blocks = new Uint32Array(80); + } + + this.#h0 = 0x67452301; + this.#h1 = 0xefcdab89; + this.#h2 = 0x98badcfe; + this.#h3 = 0x10325476; + this.#h4 = 0xc3d2e1f0; + + this.#block = this.#start = this.#bytes = this.#hBytes = 0; + this.#finalized = this.#hashed = false; + } + + update(data: string | ArrayBuffer | ArrayBufferView): Sha1 { + if (this.#finalized) { + return this; + } + let notString = true; + let message; + if (data instanceof ArrayBuffer) { + message = new Uint8Array(data); + } else if (ArrayBuffer.isView(data)) { + message = new Uint8Array(data.buffer); + } else { + notString = false; + message = String(data); + } + let code; + let index = 0; + let i; + const start = this.#start; + const length = message.length || 0; + const blocks = this.#blocks; + + while (index < length) { + if (this.#hashed) { + this.#hashed = false; + blocks[0] = this.#block; + blocks.fill(0, 1, 17); + } + + if (notString) { + for (i = start; index < length && i < 64; ++index) { + blocks[i >> 2] |= (message[index] as number) << SHIFT[i++ & 3]; + } + } else { + for (i = start; index < length && i < 64; ++index) { + code = (message as string).charCodeAt(index); + if (code < 0x80) { + blocks[i >> 2] |= code << SHIFT[i++ & 3]; + } else if (code < 0x800) { + blocks[i >> 2] |= (0xc0 | (code >> 6)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; + } else if (code < 0xd800 || code >= 0xe000) { + blocks[i >> 2] |= (0xe0 | (code >> 12)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; + } else { + code = + 0x10000 + + (((code & 0x3ff) << 10) | + ((message as string).charCodeAt(++index) & 0x3ff)); + blocks[i >> 2] |= (0xf0 | (code >> 18)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | ((code >> 12) & 0x3f)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; + } + } + } + + this.#lastByteIndex = i; + this.#bytes += i - start; + if (i >= 64) { + this.#block = blocks[16]; + this.#start = i - 64; + this.hash(); + this.#hashed = true; + } else { + this.#start = i; + } + } + if (this.#bytes > 4294967295) { + this.#hBytes += (this.#bytes / 4294967296) >>> 0; + this.#bytes = this.#bytes >>> 0; + } + return this; + } + + finalize(): void { + if (this.#finalized) { + return; + } + this.#finalized = true; + const blocks = this.#blocks; + const i = this.#lastByteIndex; + blocks[16] = this.#block; + blocks[i >> 2] |= EXTRA[i & 3]; + this.#block = blocks[16]; + if (i >= 56) { + if (!this.#hashed) { + this.hash(); + } + blocks[0] = this.#block; + blocks.fill(0, 1, 17); + } + blocks[14] = (this.#hBytes << 3) | (this.#bytes >>> 29); + blocks[15] = this.#bytes << 3; + this.hash(); + } + + hash(): void { + let a = this.#h0; + let b = this.#h1; + let c = this.#h2; + let d = this.#h3; + let e = this.#h4; + let f, j, t; + const blocks = this.#blocks; + + for (j = 16; j < 80; ++j) { + t = blocks[j - 3] ^ blocks[j - 8] ^ blocks[j - 14] ^ blocks[j - 16]; + blocks[j] = (t << 1) | (t >>> 31); + } + + for (j = 0; j < 20; j += 5) { + f = (b & c) | (~b & d); + t = (a << 5) | (a >>> 27); + e = (t + f + e + 1518500249 + blocks[j]) >>> 0; + b = (b << 30) | (b >>> 2); + + f = (a & b) | (~a & c); + t = (e << 5) | (e >>> 27); + d = (t + f + d + 1518500249 + blocks[j + 1]) >>> 0; + a = (a << 30) | (a >>> 2); + + f = (e & a) | (~e & b); + t = (d << 5) | (d >>> 27); + c = (t + f + c + 1518500249 + blocks[j + 2]) >>> 0; + e = (e << 30) | (e >>> 2); + + f = (d & e) | (~d & a); + t = (c << 5) | (c >>> 27); + b = (t + f + b + 1518500249 + blocks[j + 3]) >>> 0; + d = (d << 30) | (d >>> 2); + + f = (c & d) | (~c & e); + t = (b << 5) | (b >>> 27); + a = (t + f + a + 1518500249 + blocks[j + 4]) >>> 0; + c = (c << 30) | (c >>> 2); + } + + for (; j < 40; j += 5) { + f = b ^ c ^ d; + t = (a << 5) | (a >>> 27); + e = (t + f + e + 1859775393 + blocks[j]) >>> 0; + b = (b << 30) | (b >>> 2); + + f = a ^ b ^ c; + t = (e << 5) | (e >>> 27); + d = (t + f + d + 1859775393 + blocks[j + 1]) >>> 0; + a = (a << 30) | (a >>> 2); + + f = e ^ a ^ b; + t = (d << 5) | (d >>> 27); + c = (t + f + c + 1859775393 + blocks[j + 2]) >>> 0; + e = (e << 30) | (e >>> 2); + + f = d ^ e ^ a; + t = (c << 5) | (c >>> 27); + b = (t + f + b + 1859775393 + blocks[j + 3]) >>> 0; + d = (d << 30) | (d >>> 2); + + f = c ^ d ^ e; + t = (b << 5) | (b >>> 27); + a = (t + f + a + 1859775393 + blocks[j + 4]) >>> 0; + c = (c << 30) | (c >>> 2); + } + + for (; j < 60; j += 5) { + f = (b & c) | (b & d) | (c & d); + t = (a << 5) | (a >>> 27); + e = (t + f + e - 1894007588 + blocks[j]) >>> 0; + b = (b << 30) | (b >>> 2); + + f = (a & b) | (a & c) | (b & c); + t = (e << 5) | (e >>> 27); + d = (t + f + d - 1894007588 + blocks[j + 1]) >>> 0; + a = (a << 30) | (a >>> 2); + + f = (e & a) | (e & b) | (a & b); + t = (d << 5) | (d >>> 27); + c = (t + f + c - 1894007588 + blocks[j + 2]) >>> 0; + e = (e << 30) | (e >>> 2); + + f = (d & e) | (d & a) | (e & a); + t = (c << 5) | (c >>> 27); + b = (t + f + b - 1894007588 + blocks[j + 3]) >>> 0; + d = (d << 30) | (d >>> 2); + + f = (c & d) | (c & e) | (d & e); + t = (b << 5) | (b >>> 27); + a = (t + f + a - 1894007588 + blocks[j + 4]) >>> 0; + c = (c << 30) | (c >>> 2); + } + + for (; j < 80; j += 5) { + f = b ^ c ^ d; + t = (a << 5) | (a >>> 27); + e = (t + f + e - 899497514 + blocks[j]) >>> 0; + b = (b << 30) | (b >>> 2); + + f = a ^ b ^ c; + t = (e << 5) | (e >>> 27); + d = (t + f + d - 899497514 + blocks[j + 1]) >>> 0; + a = (a << 30) | (a >>> 2); + + f = e ^ a ^ b; + t = (d << 5) | (d >>> 27); + c = (t + f + c - 899497514 + blocks[j + 2]) >>> 0; + e = (e << 30) | (e >>> 2); + + f = d ^ e ^ a; + t = (c << 5) | (c >>> 27); + b = (t + f + b - 899497514 + blocks[j + 3]) >>> 0; + d = (d << 30) | (d >>> 2); + + f = c ^ d ^ e; + t = (b << 5) | (b >>> 27); + a = (t + f + a - 899497514 + blocks[j + 4]) >>> 0; + c = (c << 30) | (c >>> 2); + } + + this.#h0 = (this.#h0 + a) >>> 0; + this.#h1 = (this.#h1 + b) >>> 0; + this.#h2 = (this.#h2 + c) >>> 0; + this.#h3 = (this.#h3 + d) >>> 0; + this.#h4 = (this.#h4 + e) >>> 0; + } + + hex(): string { + this.finalize(); + + const h0 = this.#h0; + const h1 = this.#h1; + const h2 = this.#h2; + const h3 = this.#h3; + const h4 = this.#h4; + + return ( + HEX_CHARS[(h0 >> 28) & 0x0f] + + HEX_CHARS[(h0 >> 24) & 0x0f] + + HEX_CHARS[(h0 >> 20) & 0x0f] + + HEX_CHARS[(h0 >> 16) & 0x0f] + + HEX_CHARS[(h0 >> 12) & 0x0f] + + HEX_CHARS[(h0 >> 8) & 0x0f] + + HEX_CHARS[(h0 >> 4) & 0x0f] + + HEX_CHARS[h0 & 0x0f] + + HEX_CHARS[(h1 >> 28) & 0x0f] + + HEX_CHARS[(h1 >> 24) & 0x0f] + + HEX_CHARS[(h1 >> 20) & 0x0f] + + HEX_CHARS[(h1 >> 16) & 0x0f] + + HEX_CHARS[(h1 >> 12) & 0x0f] + + HEX_CHARS[(h1 >> 8) & 0x0f] + + HEX_CHARS[(h1 >> 4) & 0x0f] + + HEX_CHARS[h1 & 0x0f] + + HEX_CHARS[(h2 >> 28) & 0x0f] + + HEX_CHARS[(h2 >> 24) & 0x0f] + + HEX_CHARS[(h2 >> 20) & 0x0f] + + HEX_CHARS[(h2 >> 16) & 0x0f] + + HEX_CHARS[(h2 >> 12) & 0x0f] + + HEX_CHARS[(h2 >> 8) & 0x0f] + + HEX_CHARS[(h2 >> 4) & 0x0f] + + HEX_CHARS[h2 & 0x0f] + + HEX_CHARS[(h3 >> 28) & 0x0f] + + HEX_CHARS[(h3 >> 24) & 0x0f] + + HEX_CHARS[(h3 >> 20) & 0x0f] + + HEX_CHARS[(h3 >> 16) & 0x0f] + + HEX_CHARS[(h3 >> 12) & 0x0f] + + HEX_CHARS[(h3 >> 8) & 0x0f] + + HEX_CHARS[(h3 >> 4) & 0x0f] + + HEX_CHARS[h3 & 0x0f] + + HEX_CHARS[(h4 >> 28) & 0x0f] + + HEX_CHARS[(h4 >> 24) & 0x0f] + + HEX_CHARS[(h4 >> 20) & 0x0f] + + HEX_CHARS[(h4 >> 16) & 0x0f] + + HEX_CHARS[(h4 >> 12) & 0x0f] + + HEX_CHARS[(h4 >> 8) & 0x0f] + + HEX_CHARS[(h4 >> 4) & 0x0f] + + HEX_CHARS[h4 & 0x0f] + ); + } + + toString(): string { + return this.hex(); + } + + digest(): number[] { + this.finalize(); + + const h0 = this.#h0; + const h1 = this.#h1; + const h2 = this.#h2; + const h3 = this.#h3; + const h4 = this.#h4; + + return [ + (h0 >> 24) & 0xff, + (h0 >> 16) & 0xff, + (h0 >> 8) & 0xff, + h0 & 0xff, + (h1 >> 24) & 0xff, + (h1 >> 16) & 0xff, + (h1 >> 8) & 0xff, + h1 & 0xff, + (h2 >> 24) & 0xff, + (h2 >> 16) & 0xff, + (h2 >> 8) & 0xff, + h2 & 0xff, + (h3 >> 24) & 0xff, + (h3 >> 16) & 0xff, + (h3 >> 8) & 0xff, + h3 & 0xff, + (h4 >> 24) & 0xff, + (h4 >> 16) & 0xff, + (h4 >> 8) & 0xff, + h4 & 0xff, + ]; + } + + array(): number[] { + return this.digest(); + } + + arrayBuffer(): ArrayBuffer { + this.finalize(); + return Uint32Array.of(this.#h0, this.#h1, this.#h2, this.#h3, this.#h4) + .buffer; + } +} diff --git a/std/hash/sha1_test.ts b/std/hash/sha1_test.ts new file mode 100644 index 000000000..159bdd94d --- /dev/null +++ b/std/hash/sha1_test.ts @@ -0,0 +1,24 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +const { test } = Deno; +import { assertEquals } from "../testing/asserts.ts"; +import { Sha1 } from "./sha1.ts"; + +test("[util/sha] test1", () => { + const sha1 = new Sha1(); + sha1.update("abcde"); + assertEquals(sha1.toString(), "03de6c570bfe24bfc328ccd7ca46b76eadaf4334"); +}); + +test("[util/sha] testWithArray", () => { + const data = Uint8Array.of(0x61, 0x62, 0x63, 0x64, 0x65); + const sha1 = new Sha1(); + sha1.update(data); + assertEquals(sha1.toString(), "03de6c570bfe24bfc328ccd7ca46b76eadaf4334"); +}); + +test("[util/sha] testSha1WithBuffer", () => { + const data = Uint8Array.of(0x61, 0x62, 0x63, 0x64, 0x65); + const sha1 = new Sha1(); + sha1.update(data.buffer); + assertEquals(sha1.toString(), "03de6c570bfe24bfc328ccd7ca46b76eadaf4334"); +}); diff --git a/std/hash/sha256.ts b/std/hash/sha256.ts new file mode 100644 index 000000000..02fff94d1 --- /dev/null +++ b/std/hash/sha256.ts @@ -0,0 +1,575 @@ +/* + * Adapted to deno from: + * + * [js-sha256]{@link https://github.com/emn178/js-sha256} + * + * @version 0.9.0 + * @author Chen, Yi-Cyuan [emn178@gmail.com] + * @copyright Chen, Yi-Cyuan 2014-2017 + * @license MIT + */ + +export type Message = string | number[] | ArrayBuffer | Uint8Array; + +const ERROR = "input is invalid type"; +const HEX_CHARS = "0123456789abcdef".split(""); +const EXTRA = [-2147483648, 8388608, 32768, 128] as const; +const SHIFT = [24, 16, 8, 0] as const; +// prettier-ignore +// dprint-ignore +const K = [ + 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, + 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, + 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, + 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, + 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, + 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, + 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, + 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, + 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, + 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, + 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2, +] as const; + +const blocks: number[] = []; + +export class Sha256 { + #block!: number; + #blocks!: number[]; + #bytes!: number; + #finalized!: boolean; + #first!: boolean; + #h0!: number; + #h1!: number; + #h2!: number; + #h3!: number; + #h4!: number; + #h5!: number; + #h6!: number; + #h7!: number; + #hashed!: boolean; + #hBytes!: number; + #is224!: boolean; + #lastByteIndex = 0; + #start!: number; + + constructor(is224 = false, sharedMemory = false) { + this.init(is224, sharedMemory); + } + + protected init(is224: boolean, sharedMemory: boolean): void { + if (sharedMemory) { + blocks[0] = blocks[16] = blocks[1] = blocks[2] = blocks[3] = blocks[4] = blocks[5] = blocks[6] = blocks[7] = blocks[8] = blocks[9] = blocks[10] = blocks[11] = blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0; + this.#blocks = blocks; + } else { + this.#blocks = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; + } + + if (is224) { + this.#h0 = 0xc1059ed8; + this.#h1 = 0x367cd507; + this.#h2 = 0x3070dd17; + this.#h3 = 0xf70e5939; + this.#h4 = 0xffc00b31; + this.#h5 = 0x68581511; + this.#h6 = 0x64f98fa7; + this.#h7 = 0xbefa4fa4; + } else { + // 256 + this.#h0 = 0x6a09e667; + this.#h1 = 0xbb67ae85; + this.#h2 = 0x3c6ef372; + this.#h3 = 0xa54ff53a; + this.#h4 = 0x510e527f; + this.#h5 = 0x9b05688c; + this.#h6 = 0x1f83d9ab; + this.#h7 = 0x5be0cd19; + } + + this.#block = this.#start = this.#bytes = this.#hBytes = 0; + this.#finalized = this.#hashed = false; + this.#first = true; + this.#is224 = is224; + } + + /** Update hash + * + * @param message The message you want to hash. + */ + update(message: Message): this { + if (this.#finalized) { + return this; + } + let msg: string | number[] | Uint8Array | undefined; + if (typeof message !== "string") { + if (typeof message === "object") { + if (message === null) { + throw new Error(ERROR); + } else if (message instanceof ArrayBuffer) { + msg = new Uint8Array(message); + } else if (!Array.isArray(message)) { + if (!ArrayBuffer.isView(message)) { + throw new Error(ERROR); + } + } + } else { + throw new Error(ERROR); + } + } + if (msg === undefined) { + msg = message as string | number[]; + } + let index = 0; + const length = msg.length; + const blocks = this.#blocks; + + while (index < length) { + let i: number; + if (this.#hashed) { + this.#hashed = false; + blocks[0] = this.#block; + blocks[16] = blocks[1] = blocks[2] = blocks[3] = blocks[4] = blocks[5] = blocks[6] = blocks[7] = blocks[8] = blocks[9] = blocks[10] = blocks[11] = blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0; + } + + if (typeof msg !== "string") { + for (i = this.#start; index < length && i < 64; ++index) { + blocks[i >> 2] |= msg[index] << SHIFT[i++ & 3]; + } + } else { + for (i = this.#start; index < length && i < 64; ++index) { + let code = msg.charCodeAt(index); + if (code < 0x80) { + blocks[i >> 2] |= code << SHIFT[i++ & 3]; + } else if (code < 0x800) { + blocks[i >> 2] |= (0xc0 | (code >> 6)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; + } else if (code < 0xd800 || code >= 0xe000) { + blocks[i >> 2] |= (0xe0 | (code >> 12)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; + } else { + code = + 0x10000 + + (((code & 0x3ff) << 10) | (msg.charCodeAt(++index) & 0x3ff)); + blocks[i >> 2] |= (0xf0 | (code >> 18)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | ((code >> 12) & 0x3f)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; + } + } + } + + this.#lastByteIndex = i; + this.#bytes += i - this.#start; + if (i >= 64) { + this.#block = blocks[16]; + this.#start = i - 64; + this.hash(); + this.#hashed = true; + } else { + this.#start = i; + } + } + if (this.#bytes > 4294967295) { + this.#hBytes += (this.#bytes / 4294967296) << 0; + this.#bytes = this.#bytes % 4294967296; + } + return this; + } + + protected finalize(): void { + if (this.#finalized) { + return; + } + this.#finalized = true; + const blocks = this.#blocks; + const i = this.#lastByteIndex; + blocks[16] = this.#block; + blocks[i >> 2] |= EXTRA[i & 3]; + this.#block = blocks[16]; + if (i >= 56) { + if (!this.#hashed) { + this.hash(); + } + blocks[0] = this.#block; + blocks[16] = blocks[1] = blocks[2] = blocks[3] = blocks[4] = blocks[5] = blocks[6] = blocks[7] = blocks[8] = blocks[9] = blocks[10] = blocks[11] = blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0; + } + blocks[14] = (this.#hBytes << 3) | (this.#bytes >>> 29); + blocks[15] = this.#bytes << 3; + this.hash(); + } + + protected hash(): void { + let a = this.#h0; + let b = this.#h1; + let c = this.#h2; + let d = this.#h3; + let e = this.#h4; + let f = this.#h5; + let g = this.#h6; + let h = this.#h7; + const blocks = this.#blocks; + let s0: number; + let s1: number; + let maj: number; + let t1: number; + let t2: number; + let ch: number; + let ab: number; + let da: number; + let cd: number; + let bc: number; + + for (let j = 16; j < 64; ++j) { + // rightrotate + t1 = blocks[j - 15]; + s0 = ((t1 >>> 7) | (t1 << 25)) ^ ((t1 >>> 18) | (t1 << 14)) ^ (t1 >>> 3); + t1 = blocks[j - 2]; + s1 = + ((t1 >>> 17) | (t1 << 15)) ^ ((t1 >>> 19) | (t1 << 13)) ^ (t1 >>> 10); + blocks[j] = (blocks[j - 16] + s0 + blocks[j - 7] + s1) << 0; + } + + bc = b & c; + for (let j = 0; j < 64; j += 4) { + if (this.#first) { + if (this.#is224) { + ab = 300032; + t1 = blocks[0] - 1413257819; + h = (t1 - 150054599) << 0; + d = (t1 + 24177077) << 0; + } else { + ab = 704751109; + t1 = blocks[0] - 210244248; + h = (t1 - 1521486534) << 0; + d = (t1 + 143694565) << 0; + } + this.#first = false; + } else { + s0 = + ((a >>> 2) | (a << 30)) ^ + ((a >>> 13) | (a << 19)) ^ + ((a >>> 22) | (a << 10)); + s1 = + ((e >>> 6) | (e << 26)) ^ + ((e >>> 11) | (e << 21)) ^ + ((e >>> 25) | (e << 7)); + ab = a & b; + maj = ab ^ (a & c) ^ bc; + ch = (e & f) ^ (~e & g); + t1 = h + s1 + ch + K[j] + blocks[j]; + t2 = s0 + maj; + h = (d + t1) << 0; + d = (t1 + t2) << 0; + } + s0 = + ((d >>> 2) | (d << 30)) ^ + ((d >>> 13) | (d << 19)) ^ + ((d >>> 22) | (d << 10)); + s1 = + ((h >>> 6) | (h << 26)) ^ + ((h >>> 11) | (h << 21)) ^ + ((h >>> 25) | (h << 7)); + da = d & a; + maj = da ^ (d & b) ^ ab; + ch = (h & e) ^ (~h & f); + t1 = g + s1 + ch + K[j + 1] + blocks[j + 1]; + t2 = s0 + maj; + g = (c + t1) << 0; + c = (t1 + t2) << 0; + s0 = + ((c >>> 2) | (c << 30)) ^ + ((c >>> 13) | (c << 19)) ^ + ((c >>> 22) | (c << 10)); + s1 = + ((g >>> 6) | (g << 26)) ^ + ((g >>> 11) | (g << 21)) ^ + ((g >>> 25) | (g << 7)); + cd = c & d; + maj = cd ^ (c & a) ^ da; + ch = (g & h) ^ (~g & e); + t1 = f + s1 + ch + K[j + 2] + blocks[j + 2]; + t2 = s0 + maj; + f = (b + t1) << 0; + b = (t1 + t2) << 0; + s0 = + ((b >>> 2) | (b << 30)) ^ + ((b >>> 13) | (b << 19)) ^ + ((b >>> 22) | (b << 10)); + s1 = + ((f >>> 6) | (f << 26)) ^ + ((f >>> 11) | (f << 21)) ^ + ((f >>> 25) | (f << 7)); + bc = b & c; + maj = bc ^ (b & d) ^ cd; + ch = (f & g) ^ (~f & h); + t1 = e + s1 + ch + K[j + 3] + blocks[j + 3]; + t2 = s0 + maj; + e = (a + t1) << 0; + a = (t1 + t2) << 0; + } + + this.#h0 = (this.#h0 + a) << 0; + this.#h1 = (this.#h1 + b) << 0; + this.#h2 = (this.#h2 + c) << 0; + this.#h3 = (this.#h3 + d) << 0; + this.#h4 = (this.#h4 + e) << 0; + this.#h5 = (this.#h5 + f) << 0; + this.#h6 = (this.#h6 + g) << 0; + this.#h7 = (this.#h7 + h) << 0; + } + + /** Return hash in hex string. */ + hex(): string { + this.finalize(); + + const h0 = this.#h0; + const h1 = this.#h1; + const h2 = this.#h2; + const h3 = this.#h3; + const h4 = this.#h4; + const h5 = this.#h5; + const h6 = this.#h6; + const h7 = this.#h7; + + let hex = + HEX_CHARS[(h0 >> 28) & 0x0f] + + HEX_CHARS[(h0 >> 24) & 0x0f] + + HEX_CHARS[(h0 >> 20) & 0x0f] + + HEX_CHARS[(h0 >> 16) & 0x0f] + + HEX_CHARS[(h0 >> 12) & 0x0f] + + HEX_CHARS[(h0 >> 8) & 0x0f] + + HEX_CHARS[(h0 >> 4) & 0x0f] + + HEX_CHARS[h0 & 0x0f] + + HEX_CHARS[(h1 >> 28) & 0x0f] + + HEX_CHARS[(h1 >> 24) & 0x0f] + + HEX_CHARS[(h1 >> 20) & 0x0f] + + HEX_CHARS[(h1 >> 16) & 0x0f] + + HEX_CHARS[(h1 >> 12) & 0x0f] + + HEX_CHARS[(h1 >> 8) & 0x0f] + + HEX_CHARS[(h1 >> 4) & 0x0f] + + HEX_CHARS[h1 & 0x0f] + + HEX_CHARS[(h2 >> 28) & 0x0f] + + HEX_CHARS[(h2 >> 24) & 0x0f] + + HEX_CHARS[(h2 >> 20) & 0x0f] + + HEX_CHARS[(h2 >> 16) & 0x0f] + + HEX_CHARS[(h2 >> 12) & 0x0f] + + HEX_CHARS[(h2 >> 8) & 0x0f] + + HEX_CHARS[(h2 >> 4) & 0x0f] + + HEX_CHARS[h2 & 0x0f] + + HEX_CHARS[(h3 >> 28) & 0x0f] + + HEX_CHARS[(h3 >> 24) & 0x0f] + + HEX_CHARS[(h3 >> 20) & 0x0f] + + HEX_CHARS[(h3 >> 16) & 0x0f] + + HEX_CHARS[(h3 >> 12) & 0x0f] + + HEX_CHARS[(h3 >> 8) & 0x0f] + + HEX_CHARS[(h3 >> 4) & 0x0f] + + HEX_CHARS[h3 & 0x0f] + + HEX_CHARS[(h4 >> 28) & 0x0f] + + HEX_CHARS[(h4 >> 24) & 0x0f] + + HEX_CHARS[(h4 >> 20) & 0x0f] + + HEX_CHARS[(h4 >> 16) & 0x0f] + + HEX_CHARS[(h4 >> 12) & 0x0f] + + HEX_CHARS[(h4 >> 8) & 0x0f] + + HEX_CHARS[(h4 >> 4) & 0x0f] + + HEX_CHARS[h4 & 0x0f] + + HEX_CHARS[(h5 >> 28) & 0x0f] + + HEX_CHARS[(h5 >> 24) & 0x0f] + + HEX_CHARS[(h5 >> 20) & 0x0f] + + HEX_CHARS[(h5 >> 16) & 0x0f] + + HEX_CHARS[(h5 >> 12) & 0x0f] + + HEX_CHARS[(h5 >> 8) & 0x0f] + + HEX_CHARS[(h5 >> 4) & 0x0f] + + HEX_CHARS[h5 & 0x0f] + + HEX_CHARS[(h6 >> 28) & 0x0f] + + HEX_CHARS[(h6 >> 24) & 0x0f] + + HEX_CHARS[(h6 >> 20) & 0x0f] + + HEX_CHARS[(h6 >> 16) & 0x0f] + + HEX_CHARS[(h6 >> 12) & 0x0f] + + HEX_CHARS[(h6 >> 8) & 0x0f] + + HEX_CHARS[(h6 >> 4) & 0x0f] + + HEX_CHARS[h6 & 0x0f]; + if (!this.#is224) { + hex += + HEX_CHARS[(h7 >> 28) & 0x0f] + + HEX_CHARS[(h7 >> 24) & 0x0f] + + HEX_CHARS[(h7 >> 20) & 0x0f] + + HEX_CHARS[(h7 >> 16) & 0x0f] + + HEX_CHARS[(h7 >> 12) & 0x0f] + + HEX_CHARS[(h7 >> 8) & 0x0f] + + HEX_CHARS[(h7 >> 4) & 0x0f] + + HEX_CHARS[h7 & 0x0f]; + } + return hex; + } + + /** Return hash in hex string. */ + toString(): string { + return this.hex(); + } + + /** Return hash in integer array. */ + digest(): number[] { + this.finalize(); + + const h0 = this.#h0; + const h1 = this.#h1; + const h2 = this.#h2; + const h3 = this.#h3; + const h4 = this.#h4; + const h5 = this.#h5; + const h6 = this.#h6; + const h7 = this.#h7; + + const arr = [ + (h0 >> 24) & 0xff, + (h0 >> 16) & 0xff, + (h0 >> 8) & 0xff, + h0 & 0xff, + (h1 >> 24) & 0xff, + (h1 >> 16) & 0xff, + (h1 >> 8) & 0xff, + h1 & 0xff, + (h2 >> 24) & 0xff, + (h2 >> 16) & 0xff, + (h2 >> 8) & 0xff, + h2 & 0xff, + (h3 >> 24) & 0xff, + (h3 >> 16) & 0xff, + (h3 >> 8) & 0xff, + h3 & 0xff, + (h4 >> 24) & 0xff, + (h4 >> 16) & 0xff, + (h4 >> 8) & 0xff, + h4 & 0xff, + (h5 >> 24) & 0xff, + (h5 >> 16) & 0xff, + (h5 >> 8) & 0xff, + h5 & 0xff, + (h6 >> 24) & 0xff, + (h6 >> 16) & 0xff, + (h6 >> 8) & 0xff, + h6 & 0xff, + ]; + if (!this.#is224) { + arr.push( + (h7 >> 24) & 0xff, + (h7 >> 16) & 0xff, + (h7 >> 8) & 0xff, + h7 & 0xff + ); + } + return arr; + } + + /** Return hash in integer array. */ + array(): number[] { + return this.digest(); + } + + /** Return hash in ArrayBuffer. */ + arrayBuffer(): ArrayBuffer { + this.finalize(); + + const buffer = new ArrayBuffer(this.#is224 ? 28 : 32); + const dataView = new DataView(buffer); + dataView.setUint32(0, this.#h0); + dataView.setUint32(4, this.#h1); + dataView.setUint32(8, this.#h2); + dataView.setUint32(12, this.#h3); + dataView.setUint32(16, this.#h4); + dataView.setUint32(20, this.#h5); + dataView.setUint32(24, this.#h6); + if (!this.#is224) { + dataView.setUint32(28, this.#h7); + } + return buffer; + } +} + +export class HmacSha256 extends Sha256 { + #inner: boolean; + #is224: boolean; + #oKeyPad: number[]; + #sharedMemory: boolean; + + constructor(secretKey: Message, is224 = false, sharedMemory = false) { + super(is224, sharedMemory); + + let key: number[] | Uint8Array | undefined; + if (typeof secretKey === "string") { + const bytes: number[] = []; + const length = secretKey.length; + let index = 0; + for (let i = 0; i < length; ++i) { + let code = secretKey.charCodeAt(i); + if (code < 0x80) { + bytes[index++] = code; + } else if (code < 0x800) { + bytes[index++] = 0xc0 | (code >> 6); + bytes[index++] = 0x80 | (code & 0x3f); + } else if (code < 0xd800 || code >= 0xe000) { + bytes[index++] = 0xe0 | (code >> 12); + bytes[index++] = 0x80 | ((code >> 6) & 0x3f); + bytes[index++] = 0x80 | (code & 0x3f); + } else { + code = + 0x10000 + + (((code & 0x3ff) << 10) | (secretKey.charCodeAt(++i) & 0x3ff)); + bytes[index++] = 0xf0 | (code >> 18); + bytes[index++] = 0x80 | ((code >> 12) & 0x3f); + bytes[index++] = 0x80 | ((code >> 6) & 0x3f); + bytes[index++] = 0x80 | (code & 0x3f); + } + } + key = bytes; + } else { + if (typeof secretKey === "object") { + if (secretKey === null) { + throw new Error(ERROR); + } else if (secretKey instanceof ArrayBuffer) { + key = new Uint8Array(secretKey); + } else if (!Array.isArray(secretKey)) { + if (!ArrayBuffer.isView(secretKey)) { + throw new Error(ERROR); + } + } + } else { + throw new Error(ERROR); + } + } + if (key === undefined) { + key = secretKey as number[] | Uint8Array; + } + + if (key.length > 64) { + key = new Sha256(is224, true).update(key).array(); + } + + const oKeyPad: number[] = []; + const iKeyPad: number[] = []; + for (let i = 0; i < 64; ++i) { + const b = key[i] || 0; + oKeyPad[i] = 0x5c ^ b; + iKeyPad[i] = 0x36 ^ b; + } + + this.update(iKeyPad); + this.#oKeyPad = oKeyPad; + this.#inner = true; + this.#is224 = is224; + this.#sharedMemory = sharedMemory; + } + + protected finalize(): void { + super.finalize(); + if (this.#inner) { + this.#inner = false; + const innerHash = this.array(); + super.init(this.#is224, this.#sharedMemory); + this.update(this.#oKeyPad); + this.update(innerHash); + super.finalize(); + } + } +} diff --git a/std/hash/sha256_test.ts b/std/hash/sha256_test.ts new file mode 100644 index 000000000..a38786943 --- /dev/null +++ b/std/hash/sha256_test.ts @@ -0,0 +1,296 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +import { Sha256, HmacSha256, Message } from "./sha256.ts"; +import { assertEquals } from "../testing/asserts.ts"; + +const { test } = Deno; + +/** Handy function to convert an array/array buffer to a string of hex values. */ +function toHexString(value: number[] | ArrayBuffer): string { + const array = new Uint8Array(value); + let hex = ""; + for (const v of array) { + const c = v.toString(16); + hex += c.length === 1 ? `0${c}` : c; + } + return hex; +} + +// prettier-ignore +// dprint-ignore +const fixtures: { + sha256: Record>; + sha224: Record>; + sha256Hmac: Record>; + sha224Hmac: Record>; +} = { + sha256: { + "ascii": { + "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855": "", + "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592": "The quick brown fox jumps over the lazy dog", + "ef537f25c895bfa782526529a9b63d97aa631564d5d789c2b765448c8635fb6c": "The quick brown fox jumps over the lazy dog." + }, + "ascii more than 64 bytes": { + "54e73d89e1924fdcd056390266a983924b6d6d461e9470b6cd50bbaf69b5c54c": "The MD5 message-digest algorithm is a widely used cryptographic hash function producing a 128-bit (16-byte) hash value, typically expressed in text format as a 32 digit hexadecimal number. MD5 has been utilized in a wide variety of cryptographic applications, and is also commonly used to verify data integrity." + }, + "UTF8": { + "72726d8818f693066ceb69afa364218b692e62ea92b385782363780f47529c21": "中文", + "53196d1acfce0c4b264e01e8018c989d571351f59e33f055f76ff15b4f0516c6": "aécio", + "8d10a48685dbc34484696de7ea7434d80a54c1d60100530faccf697463ef19c9": "𠜎" + }, + "UTF8 more than 64 bytes": { + "d691014feebf35b3500ef6f6738d0094cac63628a7a018a980a40292a77703d1": "訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一", + "81a1472ebdeb09406a783d607ff49ee2fde3e9f44ac1cd158ad8d6ad3c4e69fa": "訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一(又譯雜湊演算法、摘要演算法等),主流程式語言普遍已有MD5的實作。" + }, + "special length": { + "5e6b963e2b6444dab8544beab8532850cef2a9d143872a6a5384abe37e61b3db": "0123456780123456780123456780123456780123456780123456780", + "85d240a4a03a0710423fc4f701da51e8785c9eaa96d718ab1c7991d6afd60d62": "01234567801234567801234567801234567801234567801234567801", + "c3ee464d5620eb2dde3dfda4c7955dbd9e9e2e9b113c13983fc67b0dfd892a53": "0123456780123456780123456780123456780123456780123456780123456780", + "74b51c6911f9a8b5e7c499effe7604e43b672166818873c27752c248de434841": "01234567801234567801234567801234567801234567801234567801234567801234567", + "6fba9e623ae6abf028a1b195748814aa95eebfb22e3ec5e15d2444cd6c48186a": "012345678012345678012345678012345678012345678012345678012345678012345678" + }, + "Array": { + "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855": [], + "182889f925ae4e5cc37118ded6ed87f7bdc7cab5ec5e78faef2e50048999473f": [211, 212], + "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592": [84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103], + "74b51c6911f9a8b5e7c499effe7604e43b672166818873c27752c248de434841": [48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55] + } + }, + sha224: { + "ascii": { + "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f": "", + "730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525": "The quick brown fox jumps over the lazy dog", + "619cba8e8e05826e9b8c519c0a5c68f4fb653e8a3d8aa04bb2c8cd4c": "The quick brown fox jumps over the lazy dog." + }, + "ascii more than 64 bytes": { + "4d97e15967391d2e846ea7d21bb480efadbae5868b731e7cc6267006": "The MD5 message-digest algorithm is a widely used cryptographic hash function producing a 128-bit (16-byte) hash value, typically expressed in text format as a 32 digit hexadecimal number. MD5 has been utilized in a wide variety of cryptographic applications, and is also commonly used to verify data integrity." + }, + "UTF8": { + "dfbab71afdf54388af4d55f8bd3de8c9b15e0eb916bf9125f4a959d4": "中文", + "d12841cafd89c534924a839e62bf35a2b5f3717b7802eb19bd8d8e15": "aécio", + "eaa0129b5509f5701db218fb7076b282e4409da52d06363aa3bdd63d": "𠜎" + }, + "UTF8 more than 64 bytes": { + "0dda421f3f81272418e1313673e9d74b7f2d04efc9c52c69458e12c3": "訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一", + "a8cb74a54e6dc6ab6110db3915ba08ffe5e1abafaea78538fa12a626": "訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一(又譯雜湊演算法、摘要演算法等),主流程式語言普遍已有MD5的實作。" + }, + "special length": { + "bc4a354d66f3cff4bc6dd6a88fbb0435cede7fd5fe94da0760cb1924": "0123456780123456780123456780123456780123456780123456780", + "2f148f757d1295784a7c69bf328b8bf827a536669e132234cd6f50e7": "01234567801234567801234567801234567801234567801234567801", + "496275a96bf41aa27ce89c3ae0fc63c3a3eab063887a8ea075bd091b": "0123456780123456780123456780123456780123456780123456780123456780", + "16ee1b101fe0e0d8dd156d598931ec19d75b0f8dc0a0455733c168c8": "01234567801234567801234567801234567801234567801234567801234567801234567", + "04c7a30079c640e440d884cdf0d7ab04fd05501d4498cb21be29ca1f": "012345678012345678012345678012345678012345678012345678012345678012345678" + }, + "Array": { + "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f": [], + "730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525": [84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103], + "16ee1b101fe0e0d8dd156d598931ec19d75b0f8dc0a0455733c168c8": [48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55] + } + }, + sha256Hmac: { + "Test Vectors": { + "b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7": [ + [0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b], + "Hi There" + ], + "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843": [ + "Jefe", + "what do ya want for nothing?" + ], + "773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe": [ + [0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa], + [0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd] + ], + "82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff46729665b": [ + [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19], + [0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd] + ], + "60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54": [ + [0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa], + "Test Using Larger Than Block-Size Key - Hash Key First" + ], + "9b09ffa71b942fcb27635fbcd5b0e944bfdc63644f0713938a7f51535c3a35e2": [ + [0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa], + "This is a test using a larger than block-size key and a larger than block-size data. The key needs to be hashed before being used by the HMAC algorithm." + ] + }, + "UTF8": { + "865cc329d317f6d9fdbd183a3c5cc5fd4c370d11f98abbbb404bceb1e6392c7e": ["中文", "中文"], + "efeef87be5731506b69bb64a9898a456dd12c94834c36a4d8ba99e3db79ad7ed": ["aécio", "aécio"], + "8a6e527049b9cfc7e1c84bcf356a1289c95da68a586c03de3327f3de0d3737fe": ["𠜎", "𠜎"] + } + }, + sha224Hmac: { + "Test Vectors": { + "896fb1128abbdf196832107cd49df33f47b4b1169912ba4f53684b22": [ + [0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b], + "Hi There" + ], + "a30e01098bc6dbbf45690f3a7e9e6d0f8bbea2a39e6148008fd05e44": [ + "Jefe", + "what do ya want for nothing?" + ], + "7fb3cb3588c6c1f6ffa9694d7d6ad2649365b0c1f65d69d1ec8333ea": [ + [0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa], + [0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd] + ], + "6c11506874013cac6a2abc1bb382627cec6a90d86efc012de7afec5a": [ + [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19], + [0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd] + ], + "95e9a0db962095adaebe9b2d6f0dbce2d499f112f2d2b7273fa6870e": [ + [0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa], + "Test Using Larger Than Block-Size Key - Hash Key First" + ], + "3a854166ac5d9f023f54d517d0b39dbd946770db9c2b95c9f6f565d1": [ + [0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa], + "This is a test using a larger than block-size key and a larger than block-size data. The key needs to be hashed before being used by the HMAC algorithm." + ] + }, + "UTF8": { + "e2280928fe813aeb7fa59aa14dd5e589041bfdf91945d19d25b9f3db": ["中文", "中文"], + "86c53dc054b16f6e006a254891bc9ff0da5df8e1a6faee3b0aaa732d": ["aécio", "aécio"], + "e9e5991bfb84506b105f800afac1599ff807bb8e20db8ffda48997b9": ["𠜎", "𠜎"] + } + }, +}; + +// prettier-ignore +// dprint-ignore +fixtures.sha256.Uint8Array = { + '182889f925ae4e5cc37118ded6ed87f7bdc7cab5ec5e78faef2e50048999473f': new Uint8Array([211, 212]), + 'd7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592': new Uint8Array([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103]) +}; +// prettier-ignore +// dprint-ignore +fixtures.sha256.Int8Array = { + 'd7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592': new Int8Array([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103]) +}; +// prettier-ignore +// dprint-ignore +fixtures.sha256.ArrayBuffer = { + 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855': new ArrayBuffer(0), + '6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d': new ArrayBuffer(1) +}; +// prettier-ignore +// dprint-ignore +fixtures.sha224.Uint8Array = { + 'e17541396a3ecd1cd5a2b968b84e597e8eae3b0ea3127963bf48dd3b': new Uint8Array([211, 212]), + '730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525': new Uint8Array([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103]) +}; +// prettier-ignore +// dprint-ignore +fixtures.sha224.Int8Array = { + '730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525': new Int8Array([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103]) +}; +// prettier-ignore +// dprint-ignore +fixtures.sha224.ArrayBuffer = { + 'd14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f': new ArrayBuffer(0), + 'fff9292b4201617bdc4d3053fce02734166a683d7d858a7f5f59b073': new ArrayBuffer(1), +}; +fixtures.sha256Hmac.Uint8Array = { + e48411262715c8370cd5e7bf8e82bef53bd53712d007f3429351843b77c7bb9b: [ + new Uint8Array(0), + "Hi There", + ], +}; +fixtures.sha256Hmac.ArrayBuffer = { + e48411262715c8370cd5e7bf8e82bef53bd53712d007f3429351843b77c7bb9b: [ + new ArrayBuffer(0), + "Hi There", + ], +}; +fixtures.sha224Hmac.Uint8Array = { + da8f94de91d62154b55ea4e8d6eb133f6d553bcd1f1ba205b9488945: [ + new Uint8Array(0), + "Hi There", + ], +}; +fixtures.sha224Hmac.ArrayBuffer = { + da8f94de91d62154b55ea4e8d6eb133f6d553bcd1f1ba205b9488945: [ + new ArrayBuffer(0), + "Hi There", + ], +}; + +const methods = ["array", "arrayBuffer", "digest", "hex"] as const; + +for (const method of methods) { + for (const [name, tests] of Object.entries(fixtures.sha256)) { + let i = 1; + for (const [expected, message] of Object.entries(tests)) { + test({ + name: `sha256.${method}() - ${name} - #${i++}`, + fn() { + const algorithm = new Sha256(); + algorithm.update(message); + const actual = + method === "hex" + ? algorithm[method]() + : toHexString(algorithm[method]()); + assertEquals(actual, expected); + }, + }); + } + } +} + +for (const method of methods) { + for (const [name, tests] of Object.entries(fixtures.sha224)) { + let i = 1; + for (const [expected, message] of Object.entries(tests)) { + test({ + name: `sha224.${method}() - ${name} - #${i++}`, + fn() { + const algorithm = new Sha256(true); + algorithm.update(message); + const actual = + method === "hex" + ? algorithm[method]() + : toHexString(algorithm[method]()); + assertEquals(actual, expected); + }, + }); + } + } +} + +for (const method of methods) { + for (const [name, tests] of Object.entries(fixtures.sha256Hmac)) { + let i = 1; + for (const [expected, [key, message]] of Object.entries(tests)) { + test({ + name: `hmacSha256.${method}() - ${name} - #${i++}`, + fn() { + const algorithm = new HmacSha256(key); + algorithm.update(message); + const actual = + method === "hex" + ? algorithm[method]() + : toHexString(algorithm[method]()); + assertEquals(actual, expected); + }, + }); + } + } +} + +for (const method of methods) { + for (const [name, tests] of Object.entries(fixtures.sha224Hmac)) { + let i = 1; + for (const [expected, [key, message]] of Object.entries(tests)) { + test({ + name: `hmacSha224.${method}() - ${name} - #${i++}`, + fn() { + const algorithm = new HmacSha256(key, true); + algorithm.update(message); + const actual = + method === "hex" + ? algorithm[method]() + : toHexString(algorithm[method]()); + assertEquals(actual, expected); + }, + }); + } + } +} diff --git a/std/http/_io.ts b/std/http/_io.ts new file mode 100644 index 000000000..631adafd0 --- /dev/null +++ b/std/http/_io.ts @@ -0,0 +1,366 @@ +import { BufReader, BufWriter } from "../io/bufio.ts"; +import { TextProtoReader } from "../textproto/mod.ts"; +import { assert } from "../testing/asserts.ts"; +import { encoder } from "../encoding/utf8.ts"; +import { ServerRequest, Response } from "./server.ts"; +import { STATUS_TEXT } from "./http_status.ts"; + +export function emptyReader(): Deno.Reader { + return { + read(_: Uint8Array): Promise { + return Promise.resolve(null); + }, + }; +} + +export function bodyReader(contentLength: number, r: BufReader): Deno.Reader { + let totalRead = 0; + let finished = false; + async function read(buf: Uint8Array): Promise { + if (finished) return null; + let result: number | null; + const remaining = contentLength - totalRead; + if (remaining >= buf.byteLength) { + result = await r.read(buf); + } else { + const readBuf = buf.subarray(0, remaining); + result = await r.read(readBuf); + } + if (result !== null) { + totalRead += result; + } + finished = totalRead === contentLength; + return result; + } + return { read }; +} + +export function chunkedBodyReader(h: Headers, r: BufReader): Deno.Reader { + // Based on https://tools.ietf.org/html/rfc2616#section-19.4.6 + const tp = new TextProtoReader(r); + let finished = false; + const chunks: Array<{ + offset: number; + data: Uint8Array; + }> = []; + async function read(buf: Uint8Array): Promise { + if (finished) return null; + const [chunk] = chunks; + if (chunk) { + const chunkRemaining = chunk.data.byteLength - chunk.offset; + const readLength = Math.min(chunkRemaining, buf.byteLength); + for (let i = 0; i < readLength; i++) { + buf[i] = chunk.data[chunk.offset + i]; + } + chunk.offset += readLength; + if (chunk.offset === chunk.data.byteLength) { + chunks.shift(); + // Consume \r\n; + if ((await tp.readLine()) === null) { + throw new Deno.errors.UnexpectedEof(); + } + } + return readLength; + } + const line = await tp.readLine(); + if (line === null) throw new Deno.errors.UnexpectedEof(); + // TODO: handle chunk extension + const [chunkSizeString] = line.split(";"); + const chunkSize = parseInt(chunkSizeString, 16); + if (Number.isNaN(chunkSize) || chunkSize < 0) { + throw new Error("Invalid chunk size"); + } + if (chunkSize > 0) { + if (chunkSize > buf.byteLength) { + let eof = await r.readFull(buf); + if (eof === null) { + throw new Deno.errors.UnexpectedEof(); + } + const restChunk = new Uint8Array(chunkSize - buf.byteLength); + eof = await r.readFull(restChunk); + if (eof === null) { + throw new Deno.errors.UnexpectedEof(); + } else { + chunks.push({ + offset: 0, + data: restChunk, + }); + } + return buf.byteLength; + } else { + const bufToFill = buf.subarray(0, chunkSize); + const eof = await r.readFull(bufToFill); + if (eof === null) { + throw new Deno.errors.UnexpectedEof(); + } + // Consume \r\n + if ((await tp.readLine()) === null) { + throw new Deno.errors.UnexpectedEof(); + } + return chunkSize; + } + } else { + assert(chunkSize === 0); + // Consume \r\n + if ((await r.readLine()) === null) { + throw new Deno.errors.UnexpectedEof(); + } + await readTrailers(h, r); + finished = true; + return null; + } + } + return { read }; +} + +const kProhibitedTrailerHeaders = [ + "transfer-encoding", + "content-length", + "trailer", +]; + +/** + * Read trailer headers from reader and append values to headers. + * "trailer" field will be deleted. + * */ +export async function readTrailers( + headers: Headers, + r: BufReader +): Promise { + const keys = parseTrailer(headers.get("trailer")); + if (!keys) return; + const tp = new TextProtoReader(r); + const result = await tp.readMIMEHeader(); + assert(result !== null, "trailer must be set"); + for (const [k, v] of result) { + if (!keys.has(k)) { + throw new Error("Undeclared trailer field"); + } + keys.delete(k); + headers.append(k, v); + } + assert(keys.size === 0, "Missing trailers"); + headers.delete("trailer"); +} + +function parseTrailer(field: string | null): Set | undefined { + if (field == null) { + return undefined; + } + const keys = field.split(",").map((v) => v.trim()); + if (keys.length === 0) { + throw new Error("Empty trailer"); + } + for (const invalid of kProhibitedTrailerHeaders) { + if (keys.includes(invalid)) { + throw new Error(`Prohibited field for trailer`); + } + } + return new Set(keys); +} + +export async function writeChunkedBody( + w: Deno.Writer, + r: Deno.Reader +): Promise { + const writer = BufWriter.create(w); + for await (const chunk of Deno.iter(r)) { + if (chunk.byteLength <= 0) continue; + const start = encoder.encode(`${chunk.byteLength.toString(16)}\r\n`); + const end = encoder.encode("\r\n"); + await writer.write(start); + await writer.write(chunk); + await writer.write(end); + } + + const endChunk = encoder.encode("0\r\n\r\n"); + await writer.write(endChunk); +} + +/** write trailer headers to writer. it mostly should be called after writeResponse */ +export async function writeTrailers( + w: Deno.Writer, + headers: Headers, + trailers: Headers +): Promise { + const trailer = headers.get("trailer"); + if (trailer === null) { + throw new Error('response headers must have "trailer" header field'); + } + const transferEncoding = headers.get("transfer-encoding"); + if (transferEncoding === null || !transferEncoding.match(/^chunked/)) { + throw new Error( + `trailer headers is only allowed for "transfer-encoding: chunked": got "${transferEncoding}"` + ); + } + const writer = BufWriter.create(w); + const trailerHeaderFields = trailer + .split(",") + .map((s) => s.trim().toLowerCase()); + for (const f of trailerHeaderFields) { + assert( + !kProhibitedTrailerHeaders.includes(f), + `"${f}" is prohibited for trailer header` + ); + } + for (const [key, value] of trailers) { + assert( + trailerHeaderFields.includes(key), + `Not trailer header field: ${key}` + ); + await writer.write(encoder.encode(`${key}: ${value}\r\n`)); + } + await writer.write(encoder.encode("\r\n")); + await writer.flush(); +} + +export async function writeResponse( + w: Deno.Writer, + r: Response +): Promise { + const protoMajor = 1; + const protoMinor = 1; + const statusCode = r.status || 200; + const statusText = STATUS_TEXT.get(statusCode); + const writer = BufWriter.create(w); + if (!statusText) { + throw new Deno.errors.InvalidData("Bad status code"); + } + if (!r.body) { + r.body = new Uint8Array(); + } + if (typeof r.body === "string") { + r.body = encoder.encode(r.body); + } + + let out = `HTTP/${protoMajor}.${protoMinor} ${statusCode} ${statusText}\r\n`; + + const headers = r.headers ?? new Headers(); + + if (r.body && !headers.get("content-length")) { + if (r.body instanceof Uint8Array) { + out += `content-length: ${r.body.byteLength}\r\n`; + } else if (!headers.get("transfer-encoding")) { + out += "transfer-encoding: chunked\r\n"; + } + } + + for (const [key, value] of headers) { + out += `${key}: ${value}\r\n`; + } + + out += `\r\n`; + + const header = encoder.encode(out); + const n = await writer.write(header); + assert(n === header.byteLength); + + if (r.body instanceof Uint8Array) { + const n = await writer.write(r.body); + assert(n === r.body.byteLength); + } else if (headers.has("content-length")) { + const contentLength = headers.get("content-length"); + assert(contentLength != null); + const bodyLength = parseInt(contentLength); + const n = await Deno.copy(r.body, writer); + assert(n === bodyLength); + } else { + await writeChunkedBody(writer, r.body); + } + if (r.trailers) { + const t = await r.trailers(); + await writeTrailers(writer, headers, t); + } + await writer.flush(); +} + +/** + * ParseHTTPVersion parses a HTTP version string. + * "HTTP/1.0" returns (1, 0). + * Ported from https://github.com/golang/go/blob/f5c43b9/src/net/http/request.go#L766-L792 + */ +export function parseHTTPVersion(vers: string): [number, number] { + switch (vers) { + case "HTTP/1.1": + return [1, 1]; + + case "HTTP/1.0": + return [1, 0]; + + default: { + const Big = 1000000; // arbitrary upper bound + + if (!vers.startsWith("HTTP/")) { + break; + } + + const dot = vers.indexOf("."); + if (dot < 0) { + break; + } + + const majorStr = vers.substring(vers.indexOf("/") + 1, dot); + const major = Number(majorStr); + if (!Number.isInteger(major) || major < 0 || major > Big) { + break; + } + + const minorStr = vers.substring(dot + 1); + const minor = Number(minorStr); + if (!Number.isInteger(minor) || minor < 0 || minor > Big) { + break; + } + + return [major, minor]; + } + } + + throw new Error(`malformed HTTP version ${vers}`); +} + +export async function readRequest( + conn: Deno.Conn, + bufr: BufReader +): Promise { + const tp = new TextProtoReader(bufr); + const firstLine = await tp.readLine(); // e.g. GET /index.html HTTP/1.0 + if (firstLine === null) return null; + const headers = await tp.readMIMEHeader(); + if (headers === null) throw new Deno.errors.UnexpectedEof(); + + const req = new ServerRequest(); + req.conn = conn; + req.r = bufr; + [req.method, req.url, req.proto] = firstLine.split(" ", 3); + [req.protoMinor, req.protoMajor] = parseHTTPVersion(req.proto); + req.headers = headers; + fixLength(req); + return req; +} + +function fixLength(req: ServerRequest): void { + const contentLength = req.headers.get("Content-Length"); + if (contentLength) { + const arrClen = contentLength.split(","); + if (arrClen.length > 1) { + const distinct = [...new Set(arrClen.map((e): string => e.trim()))]; + if (distinct.length > 1) { + throw Error("cannot contain multiple Content-Length headers"); + } else { + req.headers.set("Content-Length", distinct[0]); + } + } + const c = req.headers.get("Content-Length"); + if (req.method === "HEAD" && c && c !== "0") { + throw Error("http: method cannot contain a Content-Length"); + } + if (c && req.headers.has("transfer-encoding")) { + // A sender MUST NOT send a Content-Length header field in any message + // that contains a Transfer-Encoding header field. + // rfc: https://tools.ietf.org/html/rfc7230#section-3.3.2 + throw new Error( + "http: Transfer-Encoding and Content-Length cannot be send together" + ); + } + } +} diff --git a/std/http/_io_test.ts b/std/http/_io_test.ts new file mode 100644 index 000000000..c22ebdf07 --- /dev/null +++ b/std/http/_io_test.ts @@ -0,0 +1,476 @@ +import { + AssertionError, + assertThrowsAsync, + assertEquals, + assert, + assertNotEquals, +} from "../testing/asserts.ts"; +import { + bodyReader, + chunkedBodyReader, + writeTrailers, + readTrailers, + parseHTTPVersion, + readRequest, + writeResponse, +} from "./_io.ts"; +import { encode, decode } from "../encoding/utf8.ts"; +import { BufReader, ReadLineResult } from "../io/bufio.ts"; +import { ServerRequest, Response } from "./server.ts"; +import { StringReader } from "../io/readers.ts"; +import { mockConn } from "./_mock_conn.ts"; +const { Buffer, test, readAll } = Deno; + +test("bodyReader", async () => { + const text = "Hello, Deno"; + const r = bodyReader(text.length, new BufReader(new Buffer(encode(text)))); + assertEquals(decode(await Deno.readAll(r)), text); +}); +function chunkify(n: number, char: string): string { + const v = Array.from({ length: n }) + .map(() => `${char}`) + .join(""); + return `${n.toString(16)}\r\n${v}\r\n`; +} +test("chunkedBodyReader", async () => { + const body = [ + chunkify(3, "a"), + chunkify(5, "b"), + chunkify(11, "c"), + chunkify(22, "d"), + chunkify(0, ""), + ].join(""); + const h = new Headers(); + const r = chunkedBodyReader(h, new BufReader(new Buffer(encode(body)))); + let result: number | null; + // Use small buffer as some chunks exceed buffer size + const buf = new Uint8Array(5); + const dest = new Buffer(); + while ((result = await r.read(buf)) !== null) { + const len = Math.min(buf.byteLength, result); + await dest.write(buf.subarray(0, len)); + } + const exp = "aaabbbbbcccccccccccdddddddddddddddddddddd"; + assertEquals(new TextDecoder().decode(dest.bytes()), exp); +}); + +test("chunkedBodyReader with trailers", async () => { + const body = [ + chunkify(3, "a"), + chunkify(5, "b"), + chunkify(11, "c"), + chunkify(22, "d"), + chunkify(0, ""), + "deno: land\r\n", + "node: js\r\n", + "\r\n", + ].join(""); + const h = new Headers({ + trailer: "deno,node", + }); + const r = chunkedBodyReader(h, new BufReader(new Buffer(encode(body)))); + assertEquals(h.has("trailer"), true); + assertEquals(h.has("deno"), false); + assertEquals(h.has("node"), false); + const act = decode(await Deno.readAll(r)); + const exp = "aaabbbbbcccccccccccdddddddddddddddddddddd"; + assertEquals(act, exp); + assertEquals(h.has("trailer"), false); + assertEquals(h.get("deno"), "land"); + assertEquals(h.get("node"), "js"); +}); + +test("readTrailers", async () => { + const h = new Headers({ + trailer: "deno,node", + }); + const trailer = ["deno: land", "node: js", "", ""].join("\r\n"); + await readTrailers(h, new BufReader(new Buffer(encode(trailer)))); + assertEquals(h.has("trailer"), false); + assertEquals(h.get("deno"), "land"); + assertEquals(h.get("node"), "js"); +}); + +test("readTrailer should throw if undeclared headers found in trailer", async () => { + const patterns = [ + ["deno,node", "deno: land\r\nnode: js\r\ngo: lang\r\n\r\n"], + ["deno", "node: js\r\n\r\n"], + ["deno", "node:js\r\ngo: lang\r\n\r\n"], + ]; + for (const [header, trailer] of patterns) { + const h = new Headers({ + trailer: header, + }); + await assertThrowsAsync( + async () => { + await readTrailers(h, new BufReader(new Buffer(encode(trailer)))); + }, + Error, + "Undeclared trailer field" + ); + } +}); + +test("readTrailer should throw if trailer contains prohibited fields", async () => { + for (const f of ["content-length", "trailer", "transfer-encoding"]) { + const h = new Headers({ + trailer: f, + }); + await assertThrowsAsync( + async () => { + await readTrailers(h, new BufReader(new Buffer())); + }, + Error, + "Prohibited field for trailer" + ); + } +}); + +test("writeTrailer", async () => { + const w = new Buffer(); + await writeTrailers( + w, + new Headers({ "transfer-encoding": "chunked", trailer: "deno,node" }), + new Headers({ deno: "land", node: "js" }) + ); + assertEquals( + new TextDecoder().decode(w.bytes()), + "deno: land\r\nnode: js\r\n\r\n" + ); +}); + +test("writeTrailer should throw", async () => { + const w = new Buffer(); + await assertThrowsAsync( + () => { + return writeTrailers(w, new Headers(), new Headers()); + }, + Error, + 'must have "trailer"' + ); + await assertThrowsAsync( + () => { + return writeTrailers(w, new Headers({ trailer: "deno" }), new Headers()); + }, + Error, + "only allowed" + ); + for (const f of ["content-length", "trailer", "transfer-encoding"]) { + await assertThrowsAsync( + () => { + return writeTrailers( + w, + new Headers({ "transfer-encoding": "chunked", trailer: f }), + new Headers({ [f]: "1" }) + ); + }, + AssertionError, + "prohibited" + ); + } + await assertThrowsAsync( + () => { + return writeTrailers( + w, + new Headers({ "transfer-encoding": "chunked", trailer: "deno" }), + new Headers({ node: "js" }) + ); + }, + AssertionError, + "Not trailer" + ); +}); + +// Ported from https://github.com/golang/go/blob/f5c43b9/src/net/http/request_test.go#L535-L565 +test("parseHttpVersion", (): void => { + const testCases = [ + { in: "HTTP/0.9", want: [0, 9] }, + { in: "HTTP/1.0", want: [1, 0] }, + { in: "HTTP/1.1", want: [1, 1] }, + { in: "HTTP/3.14", want: [3, 14] }, + { in: "HTTP", err: true }, + { in: "HTTP/one.one", err: true }, + { in: "HTTP/1.1/", err: true }, + { in: "HTTP/-1.0", err: true }, + { in: "HTTP/0.-1", err: true }, + { in: "HTTP/", err: true }, + { in: "HTTP/1,0", err: true }, + { in: "HTTP/1.1000001", err: true }, + ]; + for (const t of testCases) { + let r, err; + try { + r = parseHTTPVersion(t.in); + } catch (e) { + err = e; + } + if (t.err) { + assert(err instanceof Error, t.in); + } else { + assertEquals(err, undefined); + assertEquals(r, t.want, t.in); + } + } +}); + +test("writeUint8ArrayResponse", async function (): Promise { + const shortText = "Hello"; + + const body = new TextEncoder().encode(shortText); + const res: Response = { body }; + + const buf = new Deno.Buffer(); + await writeResponse(buf, res); + + const decoder = new TextDecoder("utf-8"); + const reader = new BufReader(buf); + + let r: ReadLineResult | null = await reader.readLine(); + assert(r !== null); + assertEquals(decoder.decode(r.line), "HTTP/1.1 200 OK"); + assertEquals(r.more, false); + + r = await reader.readLine(); + assert(r !== null); + assertEquals(decoder.decode(r.line), `content-length: ${shortText.length}`); + assertEquals(r.more, false); + + r = await reader.readLine(); + assert(r !== null); + assertEquals(r.line.byteLength, 0); + assertEquals(r.more, false); + + r = await reader.readLine(); + assert(r !== null); + assertEquals(decoder.decode(r.line), shortText); + assertEquals(r.more, false); + + const eof = await reader.readLine(); + assertEquals(eof, null); +}); + +test("writeStringResponse", async function (): Promise { + const body = "Hello"; + + const res: Response = { body }; + + const buf = new Deno.Buffer(); + await writeResponse(buf, res); + + const decoder = new TextDecoder("utf-8"); + const reader = new BufReader(buf); + + let r: ReadLineResult | null = await reader.readLine(); + assert(r !== null); + assertEquals(decoder.decode(r.line), "HTTP/1.1 200 OK"); + assertEquals(r.more, false); + + r = await reader.readLine(); + assert(r !== null); + assertEquals(decoder.decode(r.line), `content-length: ${body.length}`); + assertEquals(r.more, false); + + r = await reader.readLine(); + assert(r !== null); + assertEquals(r.line.byteLength, 0); + assertEquals(r.more, false); + + r = await reader.readLine(); + assert(r !== null); + assertEquals(decoder.decode(r.line), body); + assertEquals(r.more, false); + + const eof = await reader.readLine(); + assertEquals(eof, null); +}); + +test("writeStringReaderResponse", async function (): Promise { + const shortText = "Hello"; + + const body = new StringReader(shortText); + const res: Response = { body }; + + const buf = new Deno.Buffer(); + await writeResponse(buf, res); + + const decoder = new TextDecoder("utf-8"); + const reader = new BufReader(buf); + + let r: ReadLineResult | null = await reader.readLine(); + assert(r !== null); + assertEquals(decoder.decode(r.line), "HTTP/1.1 200 OK"); + assertEquals(r.more, false); + + r = await reader.readLine(); + assert(r !== null); + assertEquals(decoder.decode(r.line), "transfer-encoding: chunked"); + assertEquals(r.more, false); + + r = await reader.readLine(); + assert(r !== null); + assertEquals(r.line.byteLength, 0); + assertEquals(r.more, false); + + r = await reader.readLine(); + assert(r !== null); + assertEquals(decoder.decode(r.line), shortText.length.toString()); + assertEquals(r.more, false); + + r = await reader.readLine(); + assert(r !== null); + assertEquals(decoder.decode(r.line), shortText); + assertEquals(r.more, false); + + r = await reader.readLine(); + assert(r !== null); + assertEquals(decoder.decode(r.line), "0"); + assertEquals(r.more, false); +}); + +test("writeResponse with trailer", async () => { + const w = new Buffer(); + const body = new StringReader("Hello"); + await writeResponse(w, { + status: 200, + headers: new Headers({ + "transfer-encoding": "chunked", + trailer: "deno,node", + }), + body, + trailers: () => new Headers({ deno: "land", node: "js" }), + }); + const ret = new TextDecoder().decode(w.bytes()); + const exp = [ + "HTTP/1.1 200 OK", + "transfer-encoding: chunked", + "trailer: deno,node", + "", + "5", + "Hello", + "0", + "", + "deno: land", + "node: js", + "", + "", + ].join("\r\n"); + assertEquals(ret, exp); +}); + +test("writeResponseShouldNotModifyOriginHeaders", async () => { + const headers = new Headers(); + const buf = new Deno.Buffer(); + + await writeResponse(buf, { body: "foo", headers }); + assert(decode(await readAll(buf)).includes("content-length: 3")); + + await writeResponse(buf, { body: "hello", headers }); + assert(decode(await readAll(buf)).includes("content-length: 5")); +}); + +test("readRequestError", async function (): Promise { + const input = `GET / HTTP/1.1 +malformedHeader +`; + const reader = new BufReader(new StringReader(input)); + let err; + try { + await readRequest(mockConn(), reader); + } catch (e) { + err = e; + } + assert(err instanceof Error); + assertEquals(err.message, "malformed MIME header line: malformedHeader"); +}); + +// Ported from Go +// https://github.com/golang/go/blob/go1.12.5/src/net/http/request_test.go#L377-L443 +// TODO(zekth) fix tests +test("testReadRequestError", async function (): Promise { + const testCases = [ + { + in: "GET / HTTP/1.1\r\nheader: foo\r\n\r\n", + headers: [{ key: "header", value: "foo" }], + }, + { + in: "GET / HTTP/1.1\r\nheader:foo\r\n", + err: Deno.errors.UnexpectedEof, + }, + { in: "", eof: true }, + { + in: "HEAD / HTTP/1.1\r\nContent-Length:4\r\n\r\n", + err: "http: method cannot contain a Content-Length", + }, + { + in: "HEAD / HTTP/1.1\r\n\r\n", + headers: [], + }, + // Multiple Content-Length values should either be + // deduplicated if same or reject otherwise + // See Issue 16490. + { + in: + "POST / HTTP/1.1\r\nContent-Length: 10\r\nContent-Length: 0\r\n\r\n" + + "Gopher hey\r\n", + err: "cannot contain multiple Content-Length headers", + }, + { + in: + "POST / HTTP/1.1\r\nContent-Length: 10\r\nContent-Length: 6\r\n\r\n" + + "Gopher\r\n", + err: "cannot contain multiple Content-Length headers", + }, + { + in: + "PUT / HTTP/1.1\r\nContent-Length: 6 \r\nContent-Length: 6\r\n" + + "Content-Length:6\r\n\r\nGopher\r\n", + headers: [{ key: "Content-Length", value: "6" }], + }, + { + in: "PUT / HTTP/1.1\r\nContent-Length: 1\r\nContent-Length: 6 \r\n\r\n", + err: "cannot contain multiple Content-Length headers", + }, + // Setting an empty header is swallowed by textproto + // see: readMIMEHeader() + // { + // in: "POST / HTTP/1.1\r\nContent-Length:\r\nContent-Length: 3\r\n\r\n", + // err: "cannot contain multiple Content-Length headers" + // }, + { + in: "HEAD / HTTP/1.1\r\nContent-Length:0\r\nContent-Length: 0\r\n\r\n", + headers: [{ key: "Content-Length", value: "0" }], + }, + { + in: + "POST / HTTP/1.1\r\nContent-Length:0\r\ntransfer-encoding: " + + "chunked\r\n\r\n", + headers: [], + err: "http: Transfer-Encoding and Content-Length cannot be send together", + }, + ]; + for (const test of testCases) { + const reader = new BufReader(new StringReader(test.in)); + let err; + let req: ServerRequest | null = null; + try { + req = await readRequest(mockConn(), reader); + } catch (e) { + err = e; + } + if (test.eof) { + assertEquals(req, null); + } else if (typeof test.err === "string") { + assertEquals(err.message, test.err); + } else if (test.err) { + assert(err instanceof (test.err as typeof Deno.errors.UnexpectedEof)); + } else { + assert(req instanceof ServerRequest); + assert(test.headers); + assertEquals(err, undefined); + assertNotEquals(req, null); + for (const h of test.headers) { + assertEquals(req.headers.get(h.key), h.value); + } + } + } +}); diff --git a/std/http/_mock_conn.ts b/std/http/_mock_conn.ts new file mode 100644 index 000000000..be07ede24 --- /dev/null +++ b/std/http/_mock_conn.ts @@ -0,0 +1,25 @@ +/** Create dummy Deno.Conn object with given base properties */ +export function mockConn(base: Partial = {}): Deno.Conn { + return { + localAddr: { + transport: "tcp", + hostname: "", + port: 0, + }, + remoteAddr: { + transport: "tcp", + hostname: "", + port: 0, + }, + rid: -1, + closeWrite: (): void => {}, + read: (): Promise => { + return Promise.resolve(0); + }, + write: (): Promise => { + return Promise.resolve(-1); + }, + close: (): void => {}, + ...base, + }; +} diff --git a/std/http/io.ts b/std/http/io.ts deleted file mode 100644 index 631adafd0..000000000 --- a/std/http/io.ts +++ /dev/null @@ -1,366 +0,0 @@ -import { BufReader, BufWriter } from "../io/bufio.ts"; -import { TextProtoReader } from "../textproto/mod.ts"; -import { assert } from "../testing/asserts.ts"; -import { encoder } from "../encoding/utf8.ts"; -import { ServerRequest, Response } from "./server.ts"; -import { STATUS_TEXT } from "./http_status.ts"; - -export function emptyReader(): Deno.Reader { - return { - read(_: Uint8Array): Promise { - return Promise.resolve(null); - }, - }; -} - -export function bodyReader(contentLength: number, r: BufReader): Deno.Reader { - let totalRead = 0; - let finished = false; - async function read(buf: Uint8Array): Promise { - if (finished) return null; - let result: number | null; - const remaining = contentLength - totalRead; - if (remaining >= buf.byteLength) { - result = await r.read(buf); - } else { - const readBuf = buf.subarray(0, remaining); - result = await r.read(readBuf); - } - if (result !== null) { - totalRead += result; - } - finished = totalRead === contentLength; - return result; - } - return { read }; -} - -export function chunkedBodyReader(h: Headers, r: BufReader): Deno.Reader { - // Based on https://tools.ietf.org/html/rfc2616#section-19.4.6 - const tp = new TextProtoReader(r); - let finished = false; - const chunks: Array<{ - offset: number; - data: Uint8Array; - }> = []; - async function read(buf: Uint8Array): Promise { - if (finished) return null; - const [chunk] = chunks; - if (chunk) { - const chunkRemaining = chunk.data.byteLength - chunk.offset; - const readLength = Math.min(chunkRemaining, buf.byteLength); - for (let i = 0; i < readLength; i++) { - buf[i] = chunk.data[chunk.offset + i]; - } - chunk.offset += readLength; - if (chunk.offset === chunk.data.byteLength) { - chunks.shift(); - // Consume \r\n; - if ((await tp.readLine()) === null) { - throw new Deno.errors.UnexpectedEof(); - } - } - return readLength; - } - const line = await tp.readLine(); - if (line === null) throw new Deno.errors.UnexpectedEof(); - // TODO: handle chunk extension - const [chunkSizeString] = line.split(";"); - const chunkSize = parseInt(chunkSizeString, 16); - if (Number.isNaN(chunkSize) || chunkSize < 0) { - throw new Error("Invalid chunk size"); - } - if (chunkSize > 0) { - if (chunkSize > buf.byteLength) { - let eof = await r.readFull(buf); - if (eof === null) { - throw new Deno.errors.UnexpectedEof(); - } - const restChunk = new Uint8Array(chunkSize - buf.byteLength); - eof = await r.readFull(restChunk); - if (eof === null) { - throw new Deno.errors.UnexpectedEof(); - } else { - chunks.push({ - offset: 0, - data: restChunk, - }); - } - return buf.byteLength; - } else { - const bufToFill = buf.subarray(0, chunkSize); - const eof = await r.readFull(bufToFill); - if (eof === null) { - throw new Deno.errors.UnexpectedEof(); - } - // Consume \r\n - if ((await tp.readLine()) === null) { - throw new Deno.errors.UnexpectedEof(); - } - return chunkSize; - } - } else { - assert(chunkSize === 0); - // Consume \r\n - if ((await r.readLine()) === null) { - throw new Deno.errors.UnexpectedEof(); - } - await readTrailers(h, r); - finished = true; - return null; - } - } - return { read }; -} - -const kProhibitedTrailerHeaders = [ - "transfer-encoding", - "content-length", - "trailer", -]; - -/** - * Read trailer headers from reader and append values to headers. - * "trailer" field will be deleted. - * */ -export async function readTrailers( - headers: Headers, - r: BufReader -): Promise { - const keys = parseTrailer(headers.get("trailer")); - if (!keys) return; - const tp = new TextProtoReader(r); - const result = await tp.readMIMEHeader(); - assert(result !== null, "trailer must be set"); - for (const [k, v] of result) { - if (!keys.has(k)) { - throw new Error("Undeclared trailer field"); - } - keys.delete(k); - headers.append(k, v); - } - assert(keys.size === 0, "Missing trailers"); - headers.delete("trailer"); -} - -function parseTrailer(field: string | null): Set | undefined { - if (field == null) { - return undefined; - } - const keys = field.split(",").map((v) => v.trim()); - if (keys.length === 0) { - throw new Error("Empty trailer"); - } - for (const invalid of kProhibitedTrailerHeaders) { - if (keys.includes(invalid)) { - throw new Error(`Prohibited field for trailer`); - } - } - return new Set(keys); -} - -export async function writeChunkedBody( - w: Deno.Writer, - r: Deno.Reader -): Promise { - const writer = BufWriter.create(w); - for await (const chunk of Deno.iter(r)) { - if (chunk.byteLength <= 0) continue; - const start = encoder.encode(`${chunk.byteLength.toString(16)}\r\n`); - const end = encoder.encode("\r\n"); - await writer.write(start); - await writer.write(chunk); - await writer.write(end); - } - - const endChunk = encoder.encode("0\r\n\r\n"); - await writer.write(endChunk); -} - -/** write trailer headers to writer. it mostly should be called after writeResponse */ -export async function writeTrailers( - w: Deno.Writer, - headers: Headers, - trailers: Headers -): Promise { - const trailer = headers.get("trailer"); - if (trailer === null) { - throw new Error('response headers must have "trailer" header field'); - } - const transferEncoding = headers.get("transfer-encoding"); - if (transferEncoding === null || !transferEncoding.match(/^chunked/)) { - throw new Error( - `trailer headers is only allowed for "transfer-encoding: chunked": got "${transferEncoding}"` - ); - } - const writer = BufWriter.create(w); - const trailerHeaderFields = trailer - .split(",") - .map((s) => s.trim().toLowerCase()); - for (const f of trailerHeaderFields) { - assert( - !kProhibitedTrailerHeaders.includes(f), - `"${f}" is prohibited for trailer header` - ); - } - for (const [key, value] of trailers) { - assert( - trailerHeaderFields.includes(key), - `Not trailer header field: ${key}` - ); - await writer.write(encoder.encode(`${key}: ${value}\r\n`)); - } - await writer.write(encoder.encode("\r\n")); - await writer.flush(); -} - -export async function writeResponse( - w: Deno.Writer, - r: Response -): Promise { - const protoMajor = 1; - const protoMinor = 1; - const statusCode = r.status || 200; - const statusText = STATUS_TEXT.get(statusCode); - const writer = BufWriter.create(w); - if (!statusText) { - throw new Deno.errors.InvalidData("Bad status code"); - } - if (!r.body) { - r.body = new Uint8Array(); - } - if (typeof r.body === "string") { - r.body = encoder.encode(r.body); - } - - let out = `HTTP/${protoMajor}.${protoMinor} ${statusCode} ${statusText}\r\n`; - - const headers = r.headers ?? new Headers(); - - if (r.body && !headers.get("content-length")) { - if (r.body instanceof Uint8Array) { - out += `content-length: ${r.body.byteLength}\r\n`; - } else if (!headers.get("transfer-encoding")) { - out += "transfer-encoding: chunked\r\n"; - } - } - - for (const [key, value] of headers) { - out += `${key}: ${value}\r\n`; - } - - out += `\r\n`; - - const header = encoder.encode(out); - const n = await writer.write(header); - assert(n === header.byteLength); - - if (r.body instanceof Uint8Array) { - const n = await writer.write(r.body); - assert(n === r.body.byteLength); - } else if (headers.has("content-length")) { - const contentLength = headers.get("content-length"); - assert(contentLength != null); - const bodyLength = parseInt(contentLength); - const n = await Deno.copy(r.body, writer); - assert(n === bodyLength); - } else { - await writeChunkedBody(writer, r.body); - } - if (r.trailers) { - const t = await r.trailers(); - await writeTrailers(writer, headers, t); - } - await writer.flush(); -} - -/** - * ParseHTTPVersion parses a HTTP version string. - * "HTTP/1.0" returns (1, 0). - * Ported from https://github.com/golang/go/blob/f5c43b9/src/net/http/request.go#L766-L792 - */ -export function parseHTTPVersion(vers: string): [number, number] { - switch (vers) { - case "HTTP/1.1": - return [1, 1]; - - case "HTTP/1.0": - return [1, 0]; - - default: { - const Big = 1000000; // arbitrary upper bound - - if (!vers.startsWith("HTTP/")) { - break; - } - - const dot = vers.indexOf("."); - if (dot < 0) { - break; - } - - const majorStr = vers.substring(vers.indexOf("/") + 1, dot); - const major = Number(majorStr); - if (!Number.isInteger(major) || major < 0 || major > Big) { - break; - } - - const minorStr = vers.substring(dot + 1); - const minor = Number(minorStr); - if (!Number.isInteger(minor) || minor < 0 || minor > Big) { - break; - } - - return [major, minor]; - } - } - - throw new Error(`malformed HTTP version ${vers}`); -} - -export async function readRequest( - conn: Deno.Conn, - bufr: BufReader -): Promise { - const tp = new TextProtoReader(bufr); - const firstLine = await tp.readLine(); // e.g. GET /index.html HTTP/1.0 - if (firstLine === null) return null; - const headers = await tp.readMIMEHeader(); - if (headers === null) throw new Deno.errors.UnexpectedEof(); - - const req = new ServerRequest(); - req.conn = conn; - req.r = bufr; - [req.method, req.url, req.proto] = firstLine.split(" ", 3); - [req.protoMinor, req.protoMajor] = parseHTTPVersion(req.proto); - req.headers = headers; - fixLength(req); - return req; -} - -function fixLength(req: ServerRequest): void { - const contentLength = req.headers.get("Content-Length"); - if (contentLength) { - const arrClen = contentLength.split(","); - if (arrClen.length > 1) { - const distinct = [...new Set(arrClen.map((e): string => e.trim()))]; - if (distinct.length > 1) { - throw Error("cannot contain multiple Content-Length headers"); - } else { - req.headers.set("Content-Length", distinct[0]); - } - } - const c = req.headers.get("Content-Length"); - if (req.method === "HEAD" && c && c !== "0") { - throw Error("http: method cannot contain a Content-Length"); - } - if (c && req.headers.has("transfer-encoding")) { - // A sender MUST NOT send a Content-Length header field in any message - // that contains a Transfer-Encoding header field. - // rfc: https://tools.ietf.org/html/rfc7230#section-3.3.2 - throw new Error( - "http: Transfer-Encoding and Content-Length cannot be send together" - ); - } - } -} diff --git a/std/http/io_test.ts b/std/http/io_test.ts deleted file mode 100644 index c0f57a1b7..000000000 --- a/std/http/io_test.ts +++ /dev/null @@ -1,476 +0,0 @@ -import { - AssertionError, - assertThrowsAsync, - assertEquals, - assert, - assertNotEquals, -} from "../testing/asserts.ts"; -import { - bodyReader, - writeTrailers, - readTrailers, - parseHTTPVersion, - readRequest, - writeResponse, -} 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"; -const { Buffer, test, readAll } = Deno; - -test("bodyReader", async () => { - const text = "Hello, Deno"; - const r = bodyReader(text.length, new BufReader(new Buffer(encode(text)))); - assertEquals(decode(await Deno.readAll(r)), text); -}); -function chunkify(n: number, char: string): string { - const v = Array.from({ length: n }) - .map(() => `${char}`) - .join(""); - return `${n.toString(16)}\r\n${v}\r\n`; -} -test("chunkedBodyReader", async () => { - const body = [ - chunkify(3, "a"), - chunkify(5, "b"), - chunkify(11, "c"), - chunkify(22, "d"), - chunkify(0, ""), - ].join(""); - const h = new Headers(); - const r = chunkedBodyReader(h, new BufReader(new Buffer(encode(body)))); - let result: number | null; - // Use small buffer as some chunks exceed buffer size - const buf = new Uint8Array(5); - const dest = new Buffer(); - while ((result = await r.read(buf)) !== null) { - const len = Math.min(buf.byteLength, result); - await dest.write(buf.subarray(0, len)); - } - const exp = "aaabbbbbcccccccccccdddddddddddddddddddddd"; - assertEquals(new TextDecoder().decode(dest.bytes()), exp); -}); - -test("chunkedBodyReader with trailers", async () => { - const body = [ - chunkify(3, "a"), - chunkify(5, "b"), - chunkify(11, "c"), - chunkify(22, "d"), - chunkify(0, ""), - "deno: land\r\n", - "node: js\r\n", - "\r\n", - ].join(""); - const h = new Headers({ - trailer: "deno,node", - }); - const r = chunkedBodyReader(h, new BufReader(new Buffer(encode(body)))); - assertEquals(h.has("trailer"), true); - assertEquals(h.has("deno"), false); - assertEquals(h.has("node"), false); - const act = decode(await Deno.readAll(r)); - const exp = "aaabbbbbcccccccccccdddddddddddddddddddddd"; - assertEquals(act, exp); - assertEquals(h.has("trailer"), false); - assertEquals(h.get("deno"), "land"); - assertEquals(h.get("node"), "js"); -}); - -test("readTrailers", async () => { - const h = new Headers({ - trailer: "deno,node", - }); - const trailer = ["deno: land", "node: js", "", ""].join("\r\n"); - await readTrailers(h, new BufReader(new Buffer(encode(trailer)))); - assertEquals(h.has("trailer"), false); - assertEquals(h.get("deno"), "land"); - assertEquals(h.get("node"), "js"); -}); - -test("readTrailer should throw if undeclared headers found in trailer", async () => { - const patterns = [ - ["deno,node", "deno: land\r\nnode: js\r\ngo: lang\r\n\r\n"], - ["deno", "node: js\r\n\r\n"], - ["deno", "node:js\r\ngo: lang\r\n\r\n"], - ]; - for (const [header, trailer] of patterns) { - const h = new Headers({ - trailer: header, - }); - await assertThrowsAsync( - async () => { - await readTrailers(h, new BufReader(new Buffer(encode(trailer)))); - }, - Error, - "Undeclared trailer field" - ); - } -}); - -test("readTrailer should throw if trailer contains prohibited fields", async () => { - for (const f of ["content-length", "trailer", "transfer-encoding"]) { - const h = new Headers({ - trailer: f, - }); - await assertThrowsAsync( - async () => { - await readTrailers(h, new BufReader(new Buffer())); - }, - Error, - "Prohibited field for trailer" - ); - } -}); - -test("writeTrailer", async () => { - const w = new Buffer(); - await writeTrailers( - w, - new Headers({ "transfer-encoding": "chunked", trailer: "deno,node" }), - new Headers({ deno: "land", node: "js" }) - ); - assertEquals( - new TextDecoder().decode(w.bytes()), - "deno: land\r\nnode: js\r\n\r\n" - ); -}); - -test("writeTrailer should throw", async () => { - const w = new Buffer(); - await assertThrowsAsync( - () => { - return writeTrailers(w, new Headers(), new Headers()); - }, - Error, - 'must have "trailer"' - ); - await assertThrowsAsync( - () => { - return writeTrailers(w, new Headers({ trailer: "deno" }), new Headers()); - }, - Error, - "only allowed" - ); - for (const f of ["content-length", "trailer", "transfer-encoding"]) { - await assertThrowsAsync( - () => { - return writeTrailers( - w, - new Headers({ "transfer-encoding": "chunked", trailer: f }), - new Headers({ [f]: "1" }) - ); - }, - AssertionError, - "prohibited" - ); - } - await assertThrowsAsync( - () => { - return writeTrailers( - w, - new Headers({ "transfer-encoding": "chunked", trailer: "deno" }), - new Headers({ node: "js" }) - ); - }, - AssertionError, - "Not trailer" - ); -}); - -// Ported from https://github.com/golang/go/blob/f5c43b9/src/net/http/request_test.go#L535-L565 -test("parseHttpVersion", (): void => { - const testCases = [ - { in: "HTTP/0.9", want: [0, 9] }, - { in: "HTTP/1.0", want: [1, 0] }, - { in: "HTTP/1.1", want: [1, 1] }, - { in: "HTTP/3.14", want: [3, 14] }, - { in: "HTTP", err: true }, - { in: "HTTP/one.one", err: true }, - { in: "HTTP/1.1/", err: true }, - { in: "HTTP/-1.0", err: true }, - { in: "HTTP/0.-1", err: true }, - { in: "HTTP/", err: true }, - { in: "HTTP/1,0", err: true }, - { in: "HTTP/1.1000001", err: true }, - ]; - for (const t of testCases) { - let r, err; - try { - r = parseHTTPVersion(t.in); - } catch (e) { - err = e; - } - if (t.err) { - assert(err instanceof Error, t.in); - } else { - assertEquals(err, undefined); - assertEquals(r, t.want, t.in); - } - } -}); - -test("writeUint8ArrayResponse", async function (): Promise { - const shortText = "Hello"; - - const body = new TextEncoder().encode(shortText); - const res: Response = { body }; - - const buf = new Deno.Buffer(); - await writeResponse(buf, res); - - const decoder = new TextDecoder("utf-8"); - const reader = new BufReader(buf); - - let r: ReadLineResult | null = await reader.readLine(); - assert(r !== null); - assertEquals(decoder.decode(r.line), "HTTP/1.1 200 OK"); - assertEquals(r.more, false); - - r = await reader.readLine(); - assert(r !== null); - assertEquals(decoder.decode(r.line), `content-length: ${shortText.length}`); - assertEquals(r.more, false); - - r = await reader.readLine(); - assert(r !== null); - assertEquals(r.line.byteLength, 0); - assertEquals(r.more, false); - - r = await reader.readLine(); - assert(r !== null); - assertEquals(decoder.decode(r.line), shortText); - assertEquals(r.more, false); - - const eof = await reader.readLine(); - assertEquals(eof, null); -}); - -test("writeStringResponse", async function (): Promise { - const body = "Hello"; - - const res: Response = { body }; - - const buf = new Deno.Buffer(); - await writeResponse(buf, res); - - const decoder = new TextDecoder("utf-8"); - const reader = new BufReader(buf); - - let r: ReadLineResult | null = await reader.readLine(); - assert(r !== null); - assertEquals(decoder.decode(r.line), "HTTP/1.1 200 OK"); - assertEquals(r.more, false); - - r = await reader.readLine(); - assert(r !== null); - assertEquals(decoder.decode(r.line), `content-length: ${body.length}`); - assertEquals(r.more, false); - - r = await reader.readLine(); - assert(r !== null); - assertEquals(r.line.byteLength, 0); - assertEquals(r.more, false); - - r = await reader.readLine(); - assert(r !== null); - assertEquals(decoder.decode(r.line), body); - assertEquals(r.more, false); - - const eof = await reader.readLine(); - assertEquals(eof, null); -}); - -test("writeStringReaderResponse", async function (): Promise { - const shortText = "Hello"; - - const body = new StringReader(shortText); - const res: Response = { body }; - - const buf = new Deno.Buffer(); - await writeResponse(buf, res); - - const decoder = new TextDecoder("utf-8"); - const reader = new BufReader(buf); - - let r: ReadLineResult | null = await reader.readLine(); - assert(r !== null); - assertEquals(decoder.decode(r.line), "HTTP/1.1 200 OK"); - assertEquals(r.more, false); - - r = await reader.readLine(); - assert(r !== null); - assertEquals(decoder.decode(r.line), "transfer-encoding: chunked"); - assertEquals(r.more, false); - - r = await reader.readLine(); - assert(r !== null); - assertEquals(r.line.byteLength, 0); - assertEquals(r.more, false); - - r = await reader.readLine(); - assert(r !== null); - assertEquals(decoder.decode(r.line), shortText.length.toString()); - assertEquals(r.more, false); - - r = await reader.readLine(); - assert(r !== null); - assertEquals(decoder.decode(r.line), shortText); - assertEquals(r.more, false); - - r = await reader.readLine(); - assert(r !== null); - assertEquals(decoder.decode(r.line), "0"); - assertEquals(r.more, false); -}); - -test("writeResponse with trailer", async () => { - const w = new Buffer(); - const body = new StringReader("Hello"); - await writeResponse(w, { - status: 200, - headers: new Headers({ - "transfer-encoding": "chunked", - trailer: "deno,node", - }), - body, - trailers: () => new Headers({ deno: "land", node: "js" }), - }); - const ret = new TextDecoder().decode(w.bytes()); - const exp = [ - "HTTP/1.1 200 OK", - "transfer-encoding: chunked", - "trailer: deno,node", - "", - "5", - "Hello", - "0", - "", - "deno: land", - "node: js", - "", - "", - ].join("\r\n"); - assertEquals(ret, exp); -}); - -test("writeResponseShouldNotModifyOriginHeaders", async () => { - const headers = new Headers(); - const buf = new Deno.Buffer(); - - await writeResponse(buf, { body: "foo", headers }); - assert(decode(await readAll(buf)).includes("content-length: 3")); - - await writeResponse(buf, { body: "hello", headers }); - assert(decode(await readAll(buf)).includes("content-length: 5")); -}); - -test("readRequestError", async function (): Promise { - const input = `GET / HTTP/1.1 -malformedHeader -`; - const reader = new BufReader(new StringReader(input)); - let err; - try { - await readRequest(mockConn(), reader); - } catch (e) { - err = e; - } - assert(err instanceof Error); - assertEquals(err.message, "malformed MIME header line: malformedHeader"); -}); - -// Ported from Go -// https://github.com/golang/go/blob/go1.12.5/src/net/http/request_test.go#L377-L443 -// TODO(zekth) fix tests -test("testReadRequestError", async function (): Promise { - const testCases = [ - { - in: "GET / HTTP/1.1\r\nheader: foo\r\n\r\n", - headers: [{ key: "header", value: "foo" }], - }, - { - in: "GET / HTTP/1.1\r\nheader:foo\r\n", - err: Deno.errors.UnexpectedEof, - }, - { in: "", eof: true }, - { - in: "HEAD / HTTP/1.1\r\nContent-Length:4\r\n\r\n", - err: "http: method cannot contain a Content-Length", - }, - { - in: "HEAD / HTTP/1.1\r\n\r\n", - headers: [], - }, - // Multiple Content-Length values should either be - // deduplicated if same or reject otherwise - // See Issue 16490. - { - in: - "POST / HTTP/1.1\r\nContent-Length: 10\r\nContent-Length: 0\r\n\r\n" + - "Gopher hey\r\n", - err: "cannot contain multiple Content-Length headers", - }, - { - in: - "POST / HTTP/1.1\r\nContent-Length: 10\r\nContent-Length: 6\r\n\r\n" + - "Gopher\r\n", - err: "cannot contain multiple Content-Length headers", - }, - { - in: - "PUT / HTTP/1.1\r\nContent-Length: 6 \r\nContent-Length: 6\r\n" + - "Content-Length:6\r\n\r\nGopher\r\n", - headers: [{ key: "Content-Length", value: "6" }], - }, - { - in: "PUT / HTTP/1.1\r\nContent-Length: 1\r\nContent-Length: 6 \r\n\r\n", - err: "cannot contain multiple Content-Length headers", - }, - // Setting an empty header is swallowed by textproto - // see: readMIMEHeader() - // { - // in: "POST / HTTP/1.1\r\nContent-Length:\r\nContent-Length: 3\r\n\r\n", - // err: "cannot contain multiple Content-Length headers" - // }, - { - in: "HEAD / HTTP/1.1\r\nContent-Length:0\r\nContent-Length: 0\r\n\r\n", - headers: [{ key: "Content-Length", value: "0" }], - }, - { - in: - "POST / HTTP/1.1\r\nContent-Length:0\r\ntransfer-encoding: " + - "chunked\r\n\r\n", - headers: [], - err: "http: Transfer-Encoding and Content-Length cannot be send together", - }, - ]; - for (const test of testCases) { - const reader = new BufReader(new StringReader(test.in)); - let err; - let req: ServerRequest | null = null; - try { - req = await readRequest(mockConn(), reader); - } catch (e) { - err = e; - } - if (test.eof) { - assertEquals(req, null); - } else if (typeof test.err === "string") { - assertEquals(err.message, test.err); - } else if (test.err) { - assert(err instanceof (test.err as typeof Deno.errors.UnexpectedEof)); - } else { - assert(req instanceof ServerRequest); - assert(test.headers); - assertEquals(err, undefined); - assertNotEquals(req, null); - for (const h of test.headers) { - assertEquals(req.headers.get(h.key), h.value); - } - } - } -}); diff --git a/std/http/mock.ts b/std/http/mock.ts deleted file mode 100644 index be07ede24..000000000 --- a/std/http/mock.ts +++ /dev/null @@ -1,25 +0,0 @@ -/** Create dummy Deno.Conn object with given base properties */ -export function mockConn(base: Partial = {}): Deno.Conn { - return { - localAddr: { - transport: "tcp", - hostname: "", - port: 0, - }, - remoteAddr: { - transport: "tcp", - hostname: "", - port: 0, - }, - rid: -1, - closeWrite: (): void => {}, - read: (): Promise => { - return Promise.resolve(0); - }, - write: (): Promise => { - return Promise.resolve(-1); - }, - close: (): void => {}, - ...base, - }; -} diff --git a/std/http/racing_server.ts b/std/http/racing_server.ts index 0b0e5a8a5..67db029e0 100644 --- a/std/http/racing_server.ts +++ b/std/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/std/http/server.ts b/std/http/server.ts index 9c678ad3d..a372b39a5 100644 --- a/std/http/server.ts +++ b/std/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/std/http/server_test.ts b/std/http/server_test.ts index 03256dd25..807695c6b 100644 --- a/std/http/server_test.ts +++ b/std/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/std/io/_iotest.ts b/std/io/_iotest.ts new file mode 100644 index 000000000..a309fb5e1 --- /dev/null +++ b/std/io/_iotest.ts @@ -0,0 +1,53 @@ +// Ported to Deno from +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. +type Reader = Deno.Reader; + +/** OneByteReader returns a Reader that implements + * each non-empty Read by reading one byte from r. + */ +export class OneByteReader implements Reader { + constructor(readonly r: Reader) {} + + read(p: Uint8Array): Promise { + if (p.byteLength === 0) { + return Promise.resolve(0); + } + if (!(p instanceof Uint8Array)) { + throw Error("expected Uint8Array"); + } + return Promise.resolve(this.r.read(p.subarray(0, 1))); + } +} + +/** HalfReader returns a Reader that implements Read + * by reading half as many requested bytes from r. + */ +export class HalfReader implements Reader { + constructor(readonly r: Reader) {} + + read(p: Uint8Array): Promise { + if (!(p instanceof Uint8Array)) { + throw Error("expected Uint8Array"); + } + const half = Math.floor((p.byteLength + 1) / 2); + return Promise.resolve(this.r.read(p.subarray(0, half))); + } +} + +/** TimeoutReader returns `Deno.errors.TimedOut` on the second read + * with no data. Subsequent calls to read succeed. + */ +export class TimeoutReader implements Reader { + count = 0; + constructor(readonly r: Reader) {} + + read(p: Uint8Array): Promise { + this.count++; + if (this.count === 2) { + throw new Deno.errors.TimedOut(); + } + return Promise.resolve(this.r.read(p)); + } +} diff --git a/std/io/bufio_test.ts b/std/io/bufio_test.ts index 17cc873fd..c49023814 100644 --- a/std/io/bufio_test.ts +++ b/std/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/std/io/iotest.ts b/std/io/iotest.ts deleted file mode 100644 index a309fb5e1..000000000 --- a/std/io/iotest.ts +++ /dev/null @@ -1,53 +0,0 @@ -// Ported to Deno from -// Copyright 2009 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. -type Reader = Deno.Reader; - -/** OneByteReader returns a Reader that implements - * each non-empty Read by reading one byte from r. - */ -export class OneByteReader implements Reader { - constructor(readonly r: Reader) {} - - read(p: Uint8Array): Promise { - if (p.byteLength === 0) { - return Promise.resolve(0); - } - if (!(p instanceof Uint8Array)) { - throw Error("expected Uint8Array"); - } - return Promise.resolve(this.r.read(p.subarray(0, 1))); - } -} - -/** HalfReader returns a Reader that implements Read - * by reading half as many requested bytes from r. - */ -export class HalfReader implements Reader { - constructor(readonly r: Reader) {} - - read(p: Uint8Array): Promise { - if (!(p instanceof Uint8Array)) { - throw Error("expected Uint8Array"); - } - const half = Math.floor((p.byteLength + 1) / 2); - return Promise.resolve(this.r.read(p.subarray(0, half))); - } -} - -/** TimeoutReader returns `Deno.errors.TimedOut` on the second read - * with no data. Subsequent calls to read succeed. - */ -export class TimeoutReader implements Reader { - count = 0; - constructor(readonly r: Reader) {} - - read(p: Uint8Array): Promise { - this.count++; - if (this.count === 2) { - throw new Deno.errors.TimedOut(); - } - return Promise.resolve(this.r.read(p)); - } -} diff --git a/std/mime/multipart.ts b/std/mime/multipart.ts index 6aadef938..61faebefd 100644 --- a/std/mime/multipart.ts +++ b/std/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/std/node/module.ts b/std/node/module.ts index 8969e80d9..f777e658f 100644 --- a/std/node/module.ts +++ b/std/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/std/path/_constants.ts b/std/path/_constants.ts new file mode 100644 index 000000000..ae0aac184 --- /dev/null +++ b/std/path/_constants.ts @@ -0,0 +1,54 @@ +// Copyright the Browserify authors. MIT License. +// Ported from https://github.com/browserify/path-browserify/ + +const { build } = Deno; + +// Alphabet chars. +export const CHAR_UPPERCASE_A = 65; /* A */ +export const CHAR_LOWERCASE_A = 97; /* a */ +export const CHAR_UPPERCASE_Z = 90; /* Z */ +export const CHAR_LOWERCASE_Z = 122; /* z */ + +// Non-alphabetic chars. +export const CHAR_DOT = 46; /* . */ +export const CHAR_FORWARD_SLASH = 47; /* / */ +export const CHAR_BACKWARD_SLASH = 92; /* \ */ +export const CHAR_VERTICAL_LINE = 124; /* | */ +export const CHAR_COLON = 58; /* : */ +export const CHAR_QUESTION_MARK = 63; /* ? */ +export const CHAR_UNDERSCORE = 95; /* _ */ +export const CHAR_LINE_FEED = 10; /* \n */ +export const CHAR_CARRIAGE_RETURN = 13; /* \r */ +export const CHAR_TAB = 9; /* \t */ +export const CHAR_FORM_FEED = 12; /* \f */ +export const CHAR_EXCLAMATION_MARK = 33; /* ! */ +export const CHAR_HASH = 35; /* # */ +export const CHAR_SPACE = 32; /* */ +export const CHAR_NO_BREAK_SPACE = 160; /* \u00A0 */ +export const CHAR_ZERO_WIDTH_NOBREAK_SPACE = 65279; /* \uFEFF */ +export const CHAR_LEFT_SQUARE_BRACKET = 91; /* [ */ +export const CHAR_RIGHT_SQUARE_BRACKET = 93; /* ] */ +export const CHAR_LEFT_ANGLE_BRACKET = 60; /* < */ +export const CHAR_RIGHT_ANGLE_BRACKET = 62; /* > */ +export const CHAR_LEFT_CURLY_BRACKET = 123; /* { */ +export const CHAR_RIGHT_CURLY_BRACKET = 125; /* } */ +export const CHAR_HYPHEN_MINUS = 45; /* - */ +export const CHAR_PLUS = 43; /* + */ +export const CHAR_DOUBLE_QUOTE = 34; /* " */ +export const CHAR_SINGLE_QUOTE = 39; /* ' */ +export const CHAR_PERCENT = 37; /* % */ +export const CHAR_SEMICOLON = 59; /* ; */ +export const CHAR_CIRCUMFLEX_ACCENT = 94; /* ^ */ +export const CHAR_GRAVE_ACCENT = 96; /* ` */ +export const CHAR_AT = 64; /* @ */ +export const CHAR_AMPERSAND = 38; /* & */ +export const CHAR_EQUAL = 61; /* = */ + +// Digits +export const CHAR_0 = 48; /* 0 */ +export const CHAR_9 = 57; /* 9 */ + +const isWindows = build.os == "windows"; + +export const SEP = isWindows ? "\\" : "/"; +export const SEP_PATTERN = isWindows ? /[\\/]+/ : /\/+/; diff --git a/std/path/_globrex.ts b/std/path/_globrex.ts new file mode 100644 index 000000000..3fc69dd6c --- /dev/null +++ b/std/path/_globrex.ts @@ -0,0 +1,327 @@ +// This file is ported from globrex@0.1.2 +// MIT License +// Copyright (c) 2018 Terkel Gjervig Nielsen + +const isWin = Deno.build.os === "windows"; +const SEP = isWin ? `(?:\\\\|\\/)` : `\\/`; +const SEP_ESC = isWin ? `\\\\` : `/`; +const SEP_RAW = isWin ? `\\` : `/`; +const GLOBSTAR = `(?:(?:[^${SEP_ESC}/]*(?:${SEP_ESC}|\/|$))*)`; +const WILDCARD = `(?:[^${SEP_ESC}/]*)`; +const GLOBSTAR_SEGMENT = `((?:[^${SEP_ESC}/]*(?:${SEP_ESC}|\/|$))*)`; +const WILDCARD_SEGMENT = `(?:[^${SEP_ESC}/]*)`; + +export interface GlobrexOptions { + /** Allow ExtGlob features. + * @default false */ + extended?: boolean; + /** Support globstar. + * @remarks When globstar is `true`, '/foo/**' is equivelant + * to '/foo/*' when globstar is `false`. + * Having globstar set to `true` is the same usage as + * using wildcards in bash. + * @default false */ + globstar?: boolean; + /** Be laissez-faire about mutiple slashes. + * @default true */ + strict?: boolean; + /** Parse as filepath for extra path related features. + * @default false */ + filepath?: boolean; + /** Flag to use in the generated RegExp. */ + flags?: string; +} + +export interface GlobrexResult { + regex: RegExp; + path?: { + regex: RegExp; + segments: RegExp[]; + globstar?: RegExp; + }; +} + +/** + * Convert any glob pattern to a JavaScript Regexp object + * @param glob Glob pattern to convert + * @param opts Configuration object + * @returns Converted object with string, segments and RegExp object + */ +export function globrex( + glob: string, + { + extended = false, + globstar = false, + strict = false, + filepath = false, + flags = "", + }: GlobrexOptions = {} +): GlobrexResult { + const sepPattern = new RegExp(`^${SEP}${strict ? "" : "+"}$`); + let regex = ""; + let segment = ""; + let pathRegexStr = ""; + const pathSegments = []; + + // If we are doing extended matching, this boolean is true when we are inside + // a group (eg {*.html,*.js}), and false otherwise. + let inGroup = false; + let inRange = false; + + // extglob stack. Keep track of scope + const ext = []; + + interface AddOptions { + split?: boolean; + last?: boolean; + only?: string; + } + + // Helper function to build string and segments + function add( + str: string, + options: AddOptions = { split: false, last: false, only: "" } + ): void { + const { split, last, only } = options; + if (only !== "path") regex += str; + if (filepath && only !== "regex") { + pathRegexStr += str.match(sepPattern) ? SEP : str; + if (split) { + if (last) segment += str; + if (segment !== "") { + // change it 'includes' + if (!flags.includes("g")) segment = `^${segment}$`; + pathSegments.push(new RegExp(segment, flags)); + } + segment = ""; + } else { + segment += str; + } + } + } + + let c, n; + for (let i = 0; i < glob.length; i++) { + c = glob[i]; + n = glob[i + 1]; + + if (["\\", "$", "^", ".", "="].includes(c)) { + add(`\\${c}`); + continue; + } + + if (c.match(sepPattern)) { + add(SEP, { split: true }); + if (n != null && n.match(sepPattern) && !strict) regex += "?"; + continue; + } + + if (c === "(") { + if (ext.length) { + add(`${c}?:`); + continue; + } + add(`\\${c}`); + continue; + } + + if (c === ")") { + if (ext.length) { + add(c); + const type: string | undefined = ext.pop(); + if (type === "@") { + add("{1}"); + } else if (type === "!") { + add(WILDCARD); + } else { + add(type as string); + } + continue; + } + add(`\\${c}`); + continue; + } + + if (c === "|") { + if (ext.length) { + add(c); + continue; + } + add(`\\${c}`); + continue; + } + + if (c === "+") { + if (n === "(" && extended) { + ext.push(c); + continue; + } + add(`\\${c}`); + continue; + } + + if (c === "@" && extended) { + if (n === "(") { + ext.push(c); + continue; + } + } + + if (c === "!") { + if (extended) { + if (inRange) { + add("^"); + continue; + } + if (n === "(") { + ext.push(c); + add("(?!"); + i++; + continue; + } + add(`\\${c}`); + continue; + } + add(`\\${c}`); + continue; + } + + if (c === "?") { + if (extended) { + if (n === "(") { + ext.push(c); + } else { + add("."); + } + continue; + } + add(`\\${c}`); + continue; + } + + if (c === "[") { + if (inRange && n === ":") { + i++; // skip [ + let value = ""; + while (glob[++i] !== ":") value += glob[i]; + if (value === "alnum") add("(?:\\w|\\d)"); + else if (value === "space") add("\\s"); + else if (value === "digit") add("\\d"); + i++; // skip last ] + continue; + } + if (extended) { + inRange = true; + add(c); + continue; + } + add(`\\${c}`); + continue; + } + + if (c === "]") { + if (extended) { + inRange = false; + add(c); + continue; + } + add(`\\${c}`); + continue; + } + + if (c === "{") { + if (extended) { + inGroup = true; + add("(?:"); + continue; + } + add(`\\${c}`); + continue; + } + + if (c === "}") { + if (extended) { + inGroup = false; + add(")"); + continue; + } + add(`\\${c}`); + continue; + } + + if (c === ",") { + if (inGroup) { + add("|"); + continue; + } + add(`\\${c}`); + continue; + } + + if (c === "*") { + if (n === "(" && extended) { + ext.push(c); + continue; + } + // Move over all consecutive "*"'s. + // Also store the previous and next characters + const prevChar = glob[i - 1]; + let starCount = 1; + while (glob[i + 1] === "*") { + starCount++; + i++; + } + const nextChar = glob[i + 1]; + if (!globstar) { + // globstar is disabled, so treat any number of "*" as one + add(".*"); + } else { + // globstar is enabled, so determine if this is a globstar segment + const isGlobstar = + starCount > 1 && // multiple "*"'s + // from the start of the segment + [SEP_RAW, "/", undefined].includes(prevChar) && + // to the end of the segment + [SEP_RAW, "/", undefined].includes(nextChar); + if (isGlobstar) { + // it's a globstar, so match zero or more path segments + add(GLOBSTAR, { only: "regex" }); + add(GLOBSTAR_SEGMENT, { only: "path", last: true, split: true }); + i++; // move over the "/" + } else { + // it's not a globstar, so only match one path segment + add(WILDCARD, { only: "regex" }); + add(WILDCARD_SEGMENT, { only: "path" }); + } + } + continue; + } + + add(c); + } + + // When regexp 'g' flag is specified don't + // constrain the regular expression with ^ & $ + if (!flags.includes("g")) { + regex = `^${regex}$`; + segment = `^${segment}$`; + if (filepath) pathRegexStr = `^${pathRegexStr}$`; + } + + const result: GlobrexResult = { regex: new RegExp(regex, flags) }; + + // Push the last segment + if (filepath) { + pathSegments.push(new RegExp(segment, flags)); + result.path = { + regex: new RegExp(pathRegexStr, flags), + segments: pathSegments, + globstar: new RegExp( + !flags.includes("g") ? `^${GLOBSTAR_SEGMENT}$` : GLOBSTAR_SEGMENT, + flags + ), + }; + } + + return result; +} diff --git a/std/path/_globrex_test.ts b/std/path/_globrex_test.ts new file mode 100644 index 000000000..2974b4719 --- /dev/null +++ b/std/path/_globrex_test.ts @@ -0,0 +1,827 @@ +// This file is ported from globrex@0.1.2 +// MIT License +// Copyright (c) 2018 Terkel Gjervig Nielsen + +const { test } = Deno; +import { assertEquals } from "../testing/asserts.ts"; +import { GlobrexOptions, globrex } from "./_globrex.ts"; + +const isWin = Deno.build.os === "windows"; +const t = { equal: assertEquals, is: assertEquals }; + +function match( + glob: string, + strUnix: string, + strWin?: string | object, + opts: GlobrexOptions = {} +): boolean { + if (typeof strWin === "object") { + opts = strWin; + strWin = ""; + } + const { regex } = globrex(glob, opts); + const match = (isWin && strWin ? strWin : strUnix).match(regex); + if (match && !regex.flags.includes("g")) { + assertEquals(match.length, 1); + } + return !!match; +} + +test({ + name: "globrex: standard", + fn(): void { + const res = globrex("*.js"); + t.equal(typeof globrex, "function", "constructor is a typeof function"); + t.equal(res instanceof Object, true, "returns object"); + t.equal(res.regex.toString(), "/^.*\\.js$/", "returns regex object"); + }, +}); + +test({ + name: "globrex: Standard * matching", + fn(): void { + t.equal(match("*", "foo"), true, "match everything"); + t.equal(match("*", "foo", { flags: "g" }), true, "match everything"); + t.equal(match("f*", "foo"), true, "match the end"); + t.equal(match("f*", "foo", { flags: "g" }), true, "match the end"); + t.equal(match("*o", "foo"), true, "match the start"); + t.equal(match("*o", "foo", { flags: "g" }), true, "match the start"); + t.equal(match("u*orn", "unicorn"), true, "match the middle"); + t.equal( + match("u*orn", "unicorn", { flags: "g" }), + true, + "match the middle" + ); + t.equal(match("ico", "unicorn"), false, "do not match without g"); + t.equal( + match("ico", "unicorn", { flags: "g" }), + true, + 'match anywhere with RegExp "g"' + ); + t.equal(match("u*nicorn", "unicorn"), true, "match zero characters"); + t.equal( + match("u*nicorn", "unicorn", { flags: "g" }), + true, + "match zero characters" + ); + }, +}); + +test({ + name: "globrex: advance * matching", + fn(): void { + t.equal( + match("*.min.js", "http://example.com/jquery.min.js", { + globstar: false, + }), + true, + "complex match" + ); + t.equal( + match("*.min.*", "http://example.com/jquery.min.js", { globstar: false }), + true, + "complex match" + ); + t.equal( + match("*/js/*.js", "http://example.com/js/jquery.min.js", { + globstar: false, + }), + true, + "complex match" + ); + t.equal( + match("*.min.*", "http://example.com/jquery.min.js", { flags: "g" }), + true, + "complex match global" + ); + t.equal( + match("*.min.js", "http://example.com/jquery.min.js", { flags: "g" }), + true, + "complex match global" + ); + t.equal( + match("*/js/*.js", "http://example.com/js/jquery.min.js", { flags: "g" }), + true, + "complex match global" + ); + + const str = "\\/$^+?.()=!|{},[].*"; + t.equal(match(str, str), true, "battle test complex string - strict"); + t.equal( + match(str, str, { flags: "g" }), + true, + "battle test complex string - strict" + ); + + t.equal( + match(".min.", "http://example.com/jquery.min.js"), + false, + 'matches without/with using RegExp "g"' + ); + t.equal( + match("*.min.*", "http://example.com/jquery.min.js"), + true, + 'matches without/with using RegExp "g"' + ); + t.equal( + match(".min.", "http://example.com/jquery.min.js", { flags: "g" }), + true, + 'matches without/with using RegExp "g"' + ); + t.equal( + match("http:", "http://example.com/jquery.min.js"), + false, + 'matches without/with using RegExp "g"' + ); + t.equal( + match("http:*", "http://example.com/jquery.min.js"), + true, + 'matches without/with using RegExp "g"' + ); + t.equal( + match("http:", "http://example.com/jquery.min.js", { flags: "g" }), + true, + 'matches without/with using RegExp "g"' + ); + t.equal( + match("min.js", "http://example.com/jquery.min.js"), + false, + 'matches without/with using RegExp "g"' + ); + t.equal( + match("*.min.js", "http://example.com/jquery.min.js"), + true, + 'matches without/with using RegExp "g"' + ); + t.equal( + match("min.js", "http://example.com/jquery.min.js", { flags: "g" }), + true, + 'matches without/with using RegExp "g"' + ); + t.equal( + match("min", "http://example.com/jquery.min.js", { flags: "g" }), + true, + 'match anywhere (globally) using RegExp "g"' + ); + t.equal( + match("/js/", "http://example.com/js/jquery.min.js", { flags: "g" }), + true, + 'match anywhere (globally) using RegExp "g"' + ); + t.equal(match("/js*jq*.js", "http://example.com/js/jquery.min.js"), false); + t.equal( + match("/js*jq*.js", "http://example.com/js/jquery.min.js", { + flags: "g", + }), + true + ); + }, +}); + +test({ + name: "globrex: ? match one character, no more and no less", + fn(): void { + t.equal(match("f?o", "foo", { extended: true }), true); + t.equal(match("f?o", "fooo", { extended: true }), false); + t.equal(match("f?oo", "foo", { extended: true }), false); + + const tester = (globstar: boolean): void => { + t.equal( + match("f?o", "foo", { extended: true, globstar, flags: "g" }), + true + ); + t.equal( + match("f?o", "fooo", { extended: true, globstar, flags: "g" }), + true + ); + t.equal( + match("f?o?", "fooo", { extended: true, globstar, flags: "g" }), + true + ); + + t.equal( + match("?fo", "fooo", { extended: true, globstar, flags: "g" }), + false + ); + t.equal( + match("f?oo", "foo", { extended: true, globstar, flags: "g" }), + false + ); + t.equal( + match("foo?", "foo", { extended: true, globstar, flags: "g" }), + false + ); + }; + + tester(true); + tester(false); + }, +}); + +test({ + name: "globrex: [] match a character range", + fn(): void { + t.equal(match("fo[oz]", "foo", { extended: true }), true); + t.equal(match("fo[oz]", "foz", { extended: true }), true); + t.equal(match("fo[oz]", "fog", { extended: true }), false); + t.equal(match("fo[a-z]", "fob", { extended: true }), true); + t.equal(match("fo[a-d]", "fot", { extended: true }), false); + t.equal(match("fo[!tz]", "fot", { extended: true }), false); + t.equal(match("fo[!tz]", "fob", { extended: true }), true); + + const tester = (globstar: boolean): void => { + t.equal( + match("fo[oz]", "foo", { extended: true, globstar, flags: "g" }), + true + ); + t.equal( + match("fo[oz]", "foz", { extended: true, globstar, flags: "g" }), + true + ); + t.equal( + match("fo[oz]", "fog", { extended: true, globstar, flags: "g" }), + false + ); + }; + + tester(true); + tester(false); + }, +}); + +test({ + name: "globrex: [] extended character ranges", + fn(): void { + t.equal( + match("[[:alnum:]]/bar.txt", "a/bar.txt", { extended: true }), + true + ); + t.equal( + match("@([[:alnum:]abc]|11)/bar.txt", "11/bar.txt", { extended: true }), + true + ); + t.equal( + match("@([[:alnum:]abc]|11)/bar.txt", "a/bar.txt", { extended: true }), + true + ); + t.equal( + match("@([[:alnum:]abc]|11)/bar.txt", "b/bar.txt", { extended: true }), + true + ); + t.equal( + match("@([[:alnum:]abc]|11)/bar.txt", "c/bar.txt", { extended: true }), + true + ); + t.equal( + match("@([[:alnum:]abc]|11)/bar.txt", "abc/bar.txt", { extended: true }), + false + ); + t.equal( + match("@([[:alnum:]abc]|11)/bar.txt", "3/bar.txt", { extended: true }), + true + ); + t.equal( + match("[[:digit:]]/bar.txt", "1/bar.txt", { extended: true }), + true + ); + t.equal( + match("[[:digit:]b]/bar.txt", "b/bar.txt", { extended: true }), + true + ); + t.equal( + match("[![:digit:]b]/bar.txt", "a/bar.txt", { extended: true }), + true + ); + t.equal( + match("[[:alnum:]]/bar.txt", "!/bar.txt", { extended: true }), + false + ); + t.equal( + match("[[:digit:]]/bar.txt", "a/bar.txt", { extended: true }), + false + ); + t.equal( + match("[[:digit:]b]/bar.txt", "a/bar.txt", { extended: true }), + false + ); + }, +}); + +test({ + name: "globrex: {} match a choice of different substrings", + fn(): void { + t.equal(match("foo{bar,baaz}", "foobaaz", { extended: true }), true); + t.equal(match("foo{bar,baaz}", "foobar", { extended: true }), true); + t.equal(match("foo{bar,baaz}", "foobuzz", { extended: true }), false); + t.equal(match("foo{bar,b*z}", "foobuzz", { extended: true }), true); + + const tester = (globstar: boolean): void => { + t.equal( + match("foo{bar,baaz}", "foobaaz", { + extended: true, + globstar, + flag: "g", + }), + true + ); + t.equal( + match("foo{bar,baaz}", "foobar", { + extended: true, + globstar, + flag: "g", + }), + true + ); + t.equal( + match("foo{bar,baaz}", "foobuzz", { + extended: true, + globstar, + flag: "g", + }), + false + ); + t.equal( + match("foo{bar,b*z}", "foobuzz", { + extended: true, + globstar, + flag: "g", + }), + true + ); + }; + + tester(true); + tester(false); + }, +}); + +test({ + name: "globrex: complex extended matches", + fn(): void { + t.equal( + match( + "http://?o[oz].b*z.com/{*.js,*.html}", + "http://foo.baaz.com/jquery.min.js", + { extended: true } + ), + true + ); + t.equal( + match( + "http://?o[oz].b*z.com/{*.js,*.html}", + "http://moz.buzz.com/index.html", + { extended: true } + ), + true + ); + t.equal( + match( + "http://?o[oz].b*z.com/{*.js,*.html}", + "http://moz.buzz.com/index.htm", + { extended: true } + ), + false + ); + t.equal( + match( + "http://?o[oz].b*z.com/{*.js,*.html}", + "http://moz.bar.com/index.html", + { extended: true } + ), + false + ); + t.equal( + match( + "http://?o[oz].b*z.com/{*.js,*.html}", + "http://flozz.buzz.com/index.html", + { extended: true } + ), + false + ); + + const tester = (globstar: boolean): void => { + t.equal( + match( + "http://?o[oz].b*z.com/{*.js,*.html}", + "http://foo.baaz.com/jquery.min.js", + { extended: true, globstar, flags: "g" } + ), + true + ); + t.equal( + match( + "http://?o[oz].b*z.com/{*.js,*.html}", + "http://moz.buzz.com/index.html", + { extended: true, globstar, flags: "g" } + ), + true + ); + t.equal( + match( + "http://?o[oz].b*z.com/{*.js,*.html}", + "http://moz.buzz.com/index.htm", + { extended: true, globstar, flags: "g" } + ), + false + ); + t.equal( + match( + "http://?o[oz].b*z.com/{*.js,*.html}", + "http://moz.bar.com/index.html", + { extended: true, globstar, flags: "g" } + ), + false + ); + t.equal( + match( + "http://?o[oz].b*z.com/{*.js,*.html}", + "http://flozz.buzz.com/index.html", + { extended: true, globstar, flags: "g" } + ), + false + ); + }; + + tester(true); + tester(false); + }, +}); + +test({ + name: "globrex: standard globstar", + fn(): void { + const tester = (globstar: boolean): void => { + t.equal( + match( + "http://foo.com/**/{*.js,*.html}", + "http://foo.com/bar/jquery.min.js", + { extended: true, globstar, flags: "g" } + ), + true + ); + t.equal( + match( + "http://foo.com/**/{*.js,*.html}", + "http://foo.com/bar/baz/jquery.min.js", + { extended: true, globstar, flags: "g" } + ), + true + ); + t.equal( + match("http://foo.com/**", "http://foo.com/bar/baz/jquery.min.js", { + extended: true, + globstar, + flags: "g", + }), + true + ); + }; + + tester(true); + tester(false); + }, +}); + +test({ + name: "globrex: remaining chars should match themself", + fn(): void { + const tester = (globstar: boolean): void => { + const testExtStr = "\\/$^+.()=!|,.*"; + t.equal(match(testExtStr, testExtStr, { extended: true }), true); + t.equal( + match(testExtStr, testExtStr, { extended: true, globstar, flags: "g" }), + true + ); + }; + + tester(true); + tester(false); + }, +}); + +test({ + name: "globrex: globstar advance testing", + fn(): void { + t.equal(match("/foo/*", "/foo/bar.txt", { globstar: true }), true); + t.equal(match("/foo/**", "/foo/bar.txt", { globstar: true }), true); + t.equal(match("/foo/**", "/foo/bar/baz.txt", { globstar: true }), true); + t.equal(match("/foo/**", "/foo/bar/baz.txt", { globstar: true }), true); + t.equal( + match("/foo/*/*.txt", "/foo/bar/baz.txt", { globstar: true }), + true + ); + t.equal( + match("/foo/**/*.txt", "/foo/bar/baz.txt", { globstar: true }), + true + ); + t.equal( + match("/foo/**/*.txt", "/foo/bar/baz/qux.txt", { globstar: true }), + true + ); + t.equal(match("/foo/**/bar.txt", "/foo/bar.txt", { globstar: true }), true); + t.equal( + match("/foo/**/**/bar.txt", "/foo/bar.txt", { globstar: true }), + true + ); + t.equal( + match("/foo/**/*/baz.txt", "/foo/bar/baz.txt", { globstar: true }), + true + ); + t.equal(match("/foo/**/*.txt", "/foo/bar.txt", { globstar: true }), true); + t.equal( + match("/foo/**/**/*.txt", "/foo/bar.txt", { globstar: true }), + true + ); + t.equal( + match("/foo/**/*/*.txt", "/foo/bar/baz.txt", { globstar: true }), + true + ); + t.equal( + match("**/*.txt", "/foo/bar/baz/qux.txt", { globstar: true }), + true + ); + t.equal(match("**/foo.txt", "foo.txt", { globstar: true }), true); + t.equal(match("**/*.txt", "foo.txt", { globstar: true }), true); + t.equal(match("/foo/*", "/foo/bar/baz.txt", { globstar: true }), false); + t.equal(match("/foo/*.txt", "/foo/bar/baz.txt", { globstar: true }), false); + t.equal( + match("/foo/*/*.txt", "/foo/bar/baz/qux.txt", { globstar: true }), + false + ); + t.equal(match("/foo/*/bar.txt", "/foo/bar.txt", { globstar: true }), false); + t.equal( + match("/foo/*/*/baz.txt", "/foo/bar/baz.txt", { globstar: true }), + false + ); + t.equal( + match("/foo/**.txt", "/foo/bar/baz/qux.txt", { globstar: true }), + false + ); + t.equal( + match("/foo/bar**/*.txt", "/foo/bar/baz/qux.txt", { globstar: true }), + false + ); + t.equal(match("/foo/bar**", "/foo/bar/baz.txt", { globstar: true }), false); + t.equal( + match("**/.txt", "/foo/bar/baz/qux.txt", { globstar: true }), + false + ); + t.equal( + match("*/*.txt", "/foo/bar/baz/qux.txt", { globstar: true }), + false + ); + t.equal(match("*/*.txt", "foo.txt", { globstar: true }), false); + t.equal( + match("http://foo.com/*", "http://foo.com/bar/baz/jquery.min.js", { + extended: true, + globstar: true, + }), + false + ); + t.equal( + match("http://foo.com/*", "http://foo.com/bar/baz/jquery.min.js", { + globstar: true, + }), + false + ); + t.equal( + match("http://foo.com/*", "http://foo.com/bar/baz/jquery.min.js", { + globstar: false, + }), + true + ); + t.equal( + match("http://foo.com/**", "http://foo.com/bar/baz/jquery.min.js", { + globstar: true, + }), + true + ); + t.equal( + match( + "http://foo.com/*/*/jquery.min.js", + "http://foo.com/bar/baz/jquery.min.js", + { globstar: true } + ), + true + ); + t.equal( + match( + "http://foo.com/**/jquery.min.js", + "http://foo.com/bar/baz/jquery.min.js", + { globstar: true } + ), + true + ); + t.equal( + match( + "http://foo.com/*/*/jquery.min.js", + "http://foo.com/bar/baz/jquery.min.js", + { globstar: false } + ), + true + ); + t.equal( + match( + "http://foo.com/*/jquery.min.js", + "http://foo.com/bar/baz/jquery.min.js", + { globstar: false } + ), + true + ); + t.equal( + match( + "http://foo.com/*/jquery.min.js", + "http://foo.com/bar/baz/jquery.min.js", + { globstar: true } + ), + false + ); + }, +}); + +test({ + name: "globrex: extended extglob ?", + fn(): void { + t.equal(match("(foo).txt", "(foo).txt", { extended: true }), true); + t.equal(match("?(foo).txt", "foo.txt", { extended: true }), true); + t.equal(match("?(foo).txt", ".txt", { extended: true }), true); + t.equal(match("?(foo|bar)baz.txt", "foobaz.txt", { extended: true }), true); + t.equal( + match("?(ba[zr]|qux)baz.txt", "bazbaz.txt", { extended: true }), + true + ); + t.equal( + match("?(ba[zr]|qux)baz.txt", "barbaz.txt", { extended: true }), + true + ); + t.equal( + match("?(ba[zr]|qux)baz.txt", "quxbaz.txt", { extended: true }), + true + ); + t.equal( + match("?(ba[!zr]|qux)baz.txt", "batbaz.txt", { extended: true }), + true + ); + t.equal(match("?(ba*|qux)baz.txt", "batbaz.txt", { extended: true }), true); + t.equal( + match("?(ba*|qux)baz.txt", "batttbaz.txt", { extended: true }), + true + ); + t.equal(match("?(ba*|qux)baz.txt", "quxbaz.txt", { extended: true }), true); + t.equal( + match("?(ba?(z|r)|qux)baz.txt", "bazbaz.txt", { extended: true }), + true + ); + t.equal( + match("?(ba?(z|?(r))|qux)baz.txt", "bazbaz.txt", { extended: true }), + true + ); + t.equal(match("?(foo).txt", "foo.txt", { extended: false }), false); + t.equal( + match("?(foo|bar)baz.txt", "foobarbaz.txt", { extended: true }), + false + ); + t.equal( + match("?(ba[zr]|qux)baz.txt", "bazquxbaz.txt", { extended: true }), + false + ); + t.equal( + match("?(ba[!zr]|qux)baz.txt", "bazbaz.txt", { extended: true }), + false + ); + }, +}); + +test({ + name: "globrex: extended extglob *", + fn(): void { + t.equal(match("*(foo).txt", "foo.txt", { extended: true }), true); + t.equal(match("*foo.txt", "bofoo.txt", { extended: true }), true); + t.equal(match("*(foo).txt", "foofoo.txt", { extended: true }), true); + t.equal(match("*(foo).txt", ".txt", { extended: true }), true); + t.equal(match("*(fooo).txt", ".txt", { extended: true }), true); + t.equal(match("*(fooo).txt", "foo.txt", { extended: true }), false); + t.equal(match("*(foo|bar).txt", "foobar.txt", { extended: true }), true); + t.equal(match("*(foo|bar).txt", "barbar.txt", { extended: true }), true); + t.equal(match("*(foo|bar).txt", "barfoobar.txt", { extended: true }), true); + t.equal(match("*(foo|bar).txt", ".txt", { extended: true }), true); + t.equal(match("*(foo|ba[rt]).txt", "bat.txt", { extended: true }), true); + t.equal(match("*(foo|b*[rt]).txt", "blat.txt", { extended: true }), true); + t.equal(match("*(foo|b*[rt]).txt", "tlat.txt", { extended: true }), false); + t.equal( + match("*(*).txt", "whatever.txt", { extended: true, globstar: true }), + true + ); + t.equal( + match("*(foo|bar)/**/*.txt", "foo/hello/world/bar.txt", { + extended: true, + globstar: true, + }), + true + ); + t.equal( + match("*(foo|bar)/**/*.txt", "foo/world/bar.txt", { + extended: true, + globstar: true, + }), + true + ); + }, +}); + +test({ + name: "globrex: extended extglob +", + fn(): void { + t.equal(match("+(foo).txt", "foo.txt", { extended: true }), true); + t.equal(match("+foo.txt", "+foo.txt", { extended: true }), true); + t.equal(match("+(foo).txt", ".txt", { extended: true }), false); + t.equal(match("+(foo|bar).txt", "foobar.txt", { extended: true }), true); + }, +}); + +test({ + name: "globrex: extended extglob @", + fn(): void { + t.equal(match("@(foo).txt", "foo.txt", { extended: true }), true); + t.equal(match("@foo.txt", "@foo.txt", { extended: true }), true); + t.equal(match("@(foo|baz)bar.txt", "foobar.txt", { extended: true }), true); + t.equal( + match("@(foo|baz)bar.txt", "foobazbar.txt", { extended: true }), + false + ); + t.equal( + match("@(foo|baz)bar.txt", "foofoobar.txt", { extended: true }), + false + ); + t.equal( + match("@(foo|baz)bar.txt", "toofoobar.txt", { extended: true }), + false + ); + }, +}); + +test({ + name: "globrex: extended extglob !", + fn(): void { + t.equal(match("!(boo).txt", "foo.txt", { extended: true }), true); + t.equal(match("!(foo|baz)bar.txt", "buzbar.txt", { extended: true }), true); + t.equal(match("!bar.txt", "!bar.txt", { extended: true }), true); + t.equal( + match("!({foo,bar})baz.txt", "notbaz.txt", { extended: true }), + true + ); + t.equal( + match("!({foo,bar})baz.txt", "foobaz.txt", { extended: true }), + false + ); + }, +}); + +test({ + name: "globrex: strict", + fn(): void { + t.equal(match("foo//bar.txt", "foo/bar.txt"), true); + t.equal(match("foo///bar.txt", "foo/bar.txt"), true); + t.equal(match("foo///bar.txt", "foo/bar.txt", { strict: true }), false); + }, +}); + +test({ + name: "globrex: stress testing", + fn(): void { + t.equal( + match("**/*/?yfile.{md,js,txt}", "foo/bar/baz/myfile.md", { + extended: true, + }), + true + ); + t.equal( + match("**/*/?yfile.{md,js,txt}", "foo/baz/myfile.md", { extended: true }), + true + ); + t.equal( + match("**/*/?yfile.{md,js,txt}", "foo/baz/tyfile.js", { extended: true }), + true + ); + t.equal( + match("[[:digit:]_.]/file.js", "1/file.js", { extended: true }), + true + ); + t.equal( + match("[[:digit:]_.]/file.js", "2/file.js", { extended: true }), + true + ); + t.equal( + match("[[:digit:]_.]/file.js", "_/file.js", { extended: true }), + true + ); + t.equal( + match("[[:digit:]_.]/file.js", "./file.js", { extended: true }), + true + ); + t.equal( + match("[[:digit:]_.]/file.js", "z/file.js", { extended: true }), + false + ); + }, +}); diff --git a/std/path/_util.ts b/std/path/_util.ts new file mode 100644 index 000000000..2776303cb --- /dev/null +++ b/std/path/_util.ts @@ -0,0 +1,116 @@ +// Copyright the Browserify authors. MIT License. +// Ported from https://github.com/browserify/path-browserify/ + +import { FormatInputPathObject } from "./interface.ts"; +import { + CHAR_UPPERCASE_A, + CHAR_LOWERCASE_A, + CHAR_UPPERCASE_Z, + CHAR_LOWERCASE_Z, + CHAR_DOT, + CHAR_FORWARD_SLASH, + CHAR_BACKWARD_SLASH, +} from "./_constants.ts"; + +export function assertPath(path: string): void { + if (typeof path !== "string") { + throw new TypeError( + `Path must be a string. Received ${JSON.stringify(path)}` + ); + } +} + +export function isPosixPathSeparator(code: number): boolean { + return code === CHAR_FORWARD_SLASH; +} + +export function isPathSeparator(code: number): boolean { + return isPosixPathSeparator(code) || code === CHAR_BACKWARD_SLASH; +} + +export function isWindowsDeviceRoot(code: number): boolean { + return ( + (code >= CHAR_LOWERCASE_A && code <= CHAR_LOWERCASE_Z) || + (code >= CHAR_UPPERCASE_A && code <= CHAR_UPPERCASE_Z) + ); +} + +// Resolves . and .. elements in a path with directory names +export function normalizeString( + path: string, + allowAboveRoot: boolean, + separator: string, + isPathSeparator: (code: number) => boolean +): string { + let res = ""; + let lastSegmentLength = 0; + let lastSlash = -1; + let dots = 0; + let code: number | undefined; + for (let i = 0, len = path.length; i <= len; ++i) { + if (i < len) code = path.charCodeAt(i); + else if (isPathSeparator(code!)) break; + else code = CHAR_FORWARD_SLASH; + + if (isPathSeparator(code!)) { + if (lastSlash === i - 1 || dots === 1) { + // NOOP + } else if (lastSlash !== i - 1 && dots === 2) { + if ( + res.length < 2 || + lastSegmentLength !== 2 || + res.charCodeAt(res.length - 1) !== CHAR_DOT || + res.charCodeAt(res.length - 2) !== CHAR_DOT + ) { + if (res.length > 2) { + const lastSlashIndex = res.lastIndexOf(separator); + if (lastSlashIndex === -1) { + res = ""; + lastSegmentLength = 0; + } else { + res = res.slice(0, lastSlashIndex); + lastSegmentLength = res.length - 1 - res.lastIndexOf(separator); + } + lastSlash = i; + dots = 0; + continue; + } else if (res.length === 2 || res.length === 1) { + res = ""; + lastSegmentLength = 0; + lastSlash = i; + dots = 0; + continue; + } + } + if (allowAboveRoot) { + if (res.length > 0) res += `${separator}..`; + else res = ".."; + lastSegmentLength = 2; + } + } else { + if (res.length > 0) res += separator + path.slice(lastSlash + 1, i); + else res = path.slice(lastSlash + 1, i); + lastSegmentLength = i - lastSlash - 1; + } + lastSlash = i; + dots = 0; + } else if (code === CHAR_DOT && dots !== -1) { + ++dots; + } else { + dots = -1; + } + } + return res; +} + +export function _format( + sep: string, + pathObject: FormatInputPathObject +): string { + const dir: string | undefined = pathObject.dir || pathObject.root; + const base: string = + pathObject.base || (pathObject.name || "") + (pathObject.ext || ""); + if (!dir) return base; + if (dir === pathObject.root) return dir + base; + return dir + sep + base; +} diff --git a/std/path/common.ts b/std/path/common.ts index 0a4de3f0c..e0e51ef23 100644 --- a/std/path/common.ts +++ b/std/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/std/path/constants.ts b/std/path/constants.ts deleted file mode 100644 index 97d3bcf58..000000000 --- a/std/path/constants.ts +++ /dev/null @@ -1,54 +0,0 @@ -// Copyright the Browserify authors. MIT License. -// Ported from https://github.com/browserify/path-browserify/ - -const { build } = Deno; - -// Alphabet chars. -export const CHAR_UPPERCASE_A = 65; /* A */ -export const CHAR_LOWERCASE_A = 97; /* a */ -export const CHAR_UPPERCASE_Z = 90; /* Z */ -export const CHAR_LOWERCASE_Z = 122; /* z */ - -// Non-alphabetic chars. -export const CHAR_DOT = 46; /* . */ -export const CHAR_FORWARD_SLASH = 47; /* / */ -export const CHAR_BACKWARD_SLASH = 92; /* \ */ -export const CHAR_VERTICAL_LINE = 124; /* | */ -export const CHAR_COLON = 58; /* : */ -export const CHAR_QUESTION_MARK = 63; /* ? */ -export const CHAR_UNDERSCORE = 95; /* _ */ -export const CHAR_LINE_FEED = 10; /* \n */ -export const CHAR_CARRIAGE_RETURN = 13; /* \r */ -export const CHAR_TAB = 9; /* \t */ -export const CHAR_FORM_FEED = 12; /* \f */ -export const CHAR_EXCLAMATION_MARK = 33; /* ! */ -export const CHAR_HASH = 35; /* # */ -export const CHAR_SPACE = 32; /* */ -export const CHAR_NO_BREAK_SPACE = 160; /* \u00A0 */ -export const CHAR_ZERO_WIDTH_NOBREAK_SPACE = 65279; /* \uFEFF */ -export const CHAR_LEFT_SQUARE_BRACKET = 91; /* [ */ -export const CHAR_RIGHT_SQUARE_BRACKET = 93; /* ] */ -export const CHAR_LEFT_ANGLE_BRACKET = 60; /* < */ -export const CHAR_RIGHT_ANGLE_BRACKET = 62; /* > */ -export const CHAR_LEFT_CURLY_BRACKET = 123; /* { */ -export const CHAR_RIGHT_CURLY_BRACKET = 125; /* } */ -export const CHAR_HYPHEN_MINUS = 45; /* - */ -export const CHAR_PLUS = 43; /* + */ -export const CHAR_DOUBLE_QUOTE = 34; /* " */ -export const CHAR_SINGLE_QUOTE = 39; /* ' */ -export const CHAR_PERCENT = 37; /* % */ -export const CHAR_SEMICOLON = 59; /* ; */ -export const CHAR_CIRCUMFLEX_ACCENT = 94; /* ^ */ -export const CHAR_GRAVE_ACCENT = 96; /* ` */ -export const CHAR_AT = 64; /* @ */ -export const CHAR_AMPERSAND = 38; /* & */ -export const CHAR_EQUAL = 61; /* = */ - -// Digits -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"; -export const SEP = isWindows ? "\\" : "/"; -export const SEP_PATTERN = isWindows ? /[\\/]+/ : /\/+/; diff --git a/std/path/glob.ts b/std/path/glob.ts index a11865c26..80672579d 100644 --- a/std/path/glob.ts +++ b/std/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/std/path/globrex.ts b/std/path/globrex.ts deleted file mode 100644 index 3fc69dd6c..000000000 --- a/std/path/globrex.ts +++ /dev/null @@ -1,327 +0,0 @@ -// This file is ported from globrex@0.1.2 -// MIT License -// Copyright (c) 2018 Terkel Gjervig Nielsen - -const isWin = Deno.build.os === "windows"; -const SEP = isWin ? `(?:\\\\|\\/)` : `\\/`; -const SEP_ESC = isWin ? `\\\\` : `/`; -const SEP_RAW = isWin ? `\\` : `/`; -const GLOBSTAR = `(?:(?:[^${SEP_ESC}/]*(?:${SEP_ESC}|\/|$))*)`; -const WILDCARD = `(?:[^${SEP_ESC}/]*)`; -const GLOBSTAR_SEGMENT = `((?:[^${SEP_ESC}/]*(?:${SEP_ESC}|\/|$))*)`; -const WILDCARD_SEGMENT = `(?:[^${SEP_ESC}/]*)`; - -export interface GlobrexOptions { - /** Allow ExtGlob features. - * @default false */ - extended?: boolean; - /** Support globstar. - * @remarks When globstar is `true`, '/foo/**' is equivelant - * to '/foo/*' when globstar is `false`. - * Having globstar set to `true` is the same usage as - * using wildcards in bash. - * @default false */ - globstar?: boolean; - /** Be laissez-faire about mutiple slashes. - * @default true */ - strict?: boolean; - /** Parse as filepath for extra path related features. - * @default false */ - filepath?: boolean; - /** Flag to use in the generated RegExp. */ - flags?: string; -} - -export interface GlobrexResult { - regex: RegExp; - path?: { - regex: RegExp; - segments: RegExp[]; - globstar?: RegExp; - }; -} - -/** - * Convert any glob pattern to a JavaScript Regexp object - * @param glob Glob pattern to convert - * @param opts Configuration object - * @returns Converted object with string, segments and RegExp object - */ -export function globrex( - glob: string, - { - extended = false, - globstar = false, - strict = false, - filepath = false, - flags = "", - }: GlobrexOptions = {} -): GlobrexResult { - const sepPattern = new RegExp(`^${SEP}${strict ? "" : "+"}$`); - let regex = ""; - let segment = ""; - let pathRegexStr = ""; - const pathSegments = []; - - // If we are doing extended matching, this boolean is true when we are inside - // a group (eg {*.html,*.js}), and false otherwise. - let inGroup = false; - let inRange = false; - - // extglob stack. Keep track of scope - const ext = []; - - interface AddOptions { - split?: boolean; - last?: boolean; - only?: string; - } - - // Helper function to build string and segments - function add( - str: string, - options: AddOptions = { split: false, last: false, only: "" } - ): void { - const { split, last, only } = options; - if (only !== "path") regex += str; - if (filepath && only !== "regex") { - pathRegexStr += str.match(sepPattern) ? SEP : str; - if (split) { - if (last) segment += str; - if (segment !== "") { - // change it 'includes' - if (!flags.includes("g")) segment = `^${segment}$`; - pathSegments.push(new RegExp(segment, flags)); - } - segment = ""; - } else { - segment += str; - } - } - } - - let c, n; - for (let i = 0; i < glob.length; i++) { - c = glob[i]; - n = glob[i + 1]; - - if (["\\", "$", "^", ".", "="].includes(c)) { - add(`\\${c}`); - continue; - } - - if (c.match(sepPattern)) { - add(SEP, { split: true }); - if (n != null && n.match(sepPattern) && !strict) regex += "?"; - continue; - } - - if (c === "(") { - if (ext.length) { - add(`${c}?:`); - continue; - } - add(`\\${c}`); - continue; - } - - if (c === ")") { - if (ext.length) { - add(c); - const type: string | undefined = ext.pop(); - if (type === "@") { - add("{1}"); - } else if (type === "!") { - add(WILDCARD); - } else { - add(type as string); - } - continue; - } - add(`\\${c}`); - continue; - } - - if (c === "|") { - if (ext.length) { - add(c); - continue; - } - add(`\\${c}`); - continue; - } - - if (c === "+") { - if (n === "(" && extended) { - ext.push(c); - continue; - } - add(`\\${c}`); - continue; - } - - if (c === "@" && extended) { - if (n === "(") { - ext.push(c); - continue; - } - } - - if (c === "!") { - if (extended) { - if (inRange) { - add("^"); - continue; - } - if (n === "(") { - ext.push(c); - add("(?!"); - i++; - continue; - } - add(`\\${c}`); - continue; - } - add(`\\${c}`); - continue; - } - - if (c === "?") { - if (extended) { - if (n === "(") { - ext.push(c); - } else { - add("."); - } - continue; - } - add(`\\${c}`); - continue; - } - - if (c === "[") { - if (inRange && n === ":") { - i++; // skip [ - let value = ""; - while (glob[++i] !== ":") value += glob[i]; - if (value === "alnum") add("(?:\\w|\\d)"); - else if (value === "space") add("\\s"); - else if (value === "digit") add("\\d"); - i++; // skip last ] - continue; - } - if (extended) { - inRange = true; - add(c); - continue; - } - add(`\\${c}`); - continue; - } - - if (c === "]") { - if (extended) { - inRange = false; - add(c); - continue; - } - add(`\\${c}`); - continue; - } - - if (c === "{") { - if (extended) { - inGroup = true; - add("(?:"); - continue; - } - add(`\\${c}`); - continue; - } - - if (c === "}") { - if (extended) { - inGroup = false; - add(")"); - continue; - } - add(`\\${c}`); - continue; - } - - if (c === ",") { - if (inGroup) { - add("|"); - continue; - } - add(`\\${c}`); - continue; - } - - if (c === "*") { - if (n === "(" && extended) { - ext.push(c); - continue; - } - // Move over all consecutive "*"'s. - // Also store the previous and next characters - const prevChar = glob[i - 1]; - let starCount = 1; - while (glob[i + 1] === "*") { - starCount++; - i++; - } - const nextChar = glob[i + 1]; - if (!globstar) { - // globstar is disabled, so treat any number of "*" as one - add(".*"); - } else { - // globstar is enabled, so determine if this is a globstar segment - const isGlobstar = - starCount > 1 && // multiple "*"'s - // from the start of the segment - [SEP_RAW, "/", undefined].includes(prevChar) && - // to the end of the segment - [SEP_RAW, "/", undefined].includes(nextChar); - if (isGlobstar) { - // it's a globstar, so match zero or more path segments - add(GLOBSTAR, { only: "regex" }); - add(GLOBSTAR_SEGMENT, { only: "path", last: true, split: true }); - i++; // move over the "/" - } else { - // it's not a globstar, so only match one path segment - add(WILDCARD, { only: "regex" }); - add(WILDCARD_SEGMENT, { only: "path" }); - } - } - continue; - } - - add(c); - } - - // When regexp 'g' flag is specified don't - // constrain the regular expression with ^ & $ - if (!flags.includes("g")) { - regex = `^${regex}$`; - segment = `^${segment}$`; - if (filepath) pathRegexStr = `^${pathRegexStr}$`; - } - - const result: GlobrexResult = { regex: new RegExp(regex, flags) }; - - // Push the last segment - if (filepath) { - pathSegments.push(new RegExp(segment, flags)); - result.path = { - regex: new RegExp(pathRegexStr, flags), - segments: pathSegments, - globstar: new RegExp( - !flags.includes("g") ? `^${GLOBSTAR_SEGMENT}$` : GLOBSTAR_SEGMENT, - flags - ), - }; - } - - return result; -} diff --git a/std/path/globrex_test.ts b/std/path/globrex_test.ts deleted file mode 100644 index c52ed108e..000000000 --- a/std/path/globrex_test.ts +++ /dev/null @@ -1,827 +0,0 @@ -// This file is ported from globrex@0.1.2 -// MIT License -// Copyright (c) 2018 Terkel Gjervig Nielsen - -const { test } = Deno; -import { assertEquals } from "../testing/asserts.ts"; -import { GlobrexOptions, globrex } from "./globrex.ts"; - -const isWin = Deno.build.os === "windows"; -const t = { equal: assertEquals, is: assertEquals }; - -function match( - glob: string, - strUnix: string, - strWin?: string | object, - opts: GlobrexOptions = {} -): boolean { - if (typeof strWin === "object") { - opts = strWin; - strWin = ""; - } - const { regex } = globrex(glob, opts); - const match = (isWin && strWin ? strWin : strUnix).match(regex); - if (match && !regex.flags.includes("g")) { - assertEquals(match.length, 1); - } - return !!match; -} - -test({ - name: "globrex: standard", - fn(): void { - const res = globrex("*.js"); - t.equal(typeof globrex, "function", "constructor is a typeof function"); - t.equal(res instanceof Object, true, "returns object"); - t.equal(res.regex.toString(), "/^.*\\.js$/", "returns regex object"); - }, -}); - -test({ - name: "globrex: Standard * matching", - fn(): void { - t.equal(match("*", "foo"), true, "match everything"); - t.equal(match("*", "foo", { flags: "g" }), true, "match everything"); - t.equal(match("f*", "foo"), true, "match the end"); - t.equal(match("f*", "foo", { flags: "g" }), true, "match the end"); - t.equal(match("*o", "foo"), true, "match the start"); - t.equal(match("*o", "foo", { flags: "g" }), true, "match the start"); - t.equal(match("u*orn", "unicorn"), true, "match the middle"); - t.equal( - match("u*orn", "unicorn", { flags: "g" }), - true, - "match the middle" - ); - t.equal(match("ico", "unicorn"), false, "do not match without g"); - t.equal( - match("ico", "unicorn", { flags: "g" }), - true, - 'match anywhere with RegExp "g"' - ); - t.equal(match("u*nicorn", "unicorn"), true, "match zero characters"); - t.equal( - match("u*nicorn", "unicorn", { flags: "g" }), - true, - "match zero characters" - ); - }, -}); - -test({ - name: "globrex: advance * matching", - fn(): void { - t.equal( - match("*.min.js", "http://example.com/jquery.min.js", { - globstar: false, - }), - true, - "complex match" - ); - t.equal( - match("*.min.*", "http://example.com/jquery.min.js", { globstar: false }), - true, - "complex match" - ); - t.equal( - match("*/js/*.js", "http://example.com/js/jquery.min.js", { - globstar: false, - }), - true, - "complex match" - ); - t.equal( - match("*.min.*", "http://example.com/jquery.min.js", { flags: "g" }), - true, - "complex match global" - ); - t.equal( - match("*.min.js", "http://example.com/jquery.min.js", { flags: "g" }), - true, - "complex match global" - ); - t.equal( - match("*/js/*.js", "http://example.com/js/jquery.min.js", { flags: "g" }), - true, - "complex match global" - ); - - const str = "\\/$^+?.()=!|{},[].*"; - t.equal(match(str, str), true, "battle test complex string - strict"); - t.equal( - match(str, str, { flags: "g" }), - true, - "battle test complex string - strict" - ); - - t.equal( - match(".min.", "http://example.com/jquery.min.js"), - false, - 'matches without/with using RegExp "g"' - ); - t.equal( - match("*.min.*", "http://example.com/jquery.min.js"), - true, - 'matches without/with using RegExp "g"' - ); - t.equal( - match(".min.", "http://example.com/jquery.min.js", { flags: "g" }), - true, - 'matches without/with using RegExp "g"' - ); - t.equal( - match("http:", "http://example.com/jquery.min.js"), - false, - 'matches without/with using RegExp "g"' - ); - t.equal( - match("http:*", "http://example.com/jquery.min.js"), - true, - 'matches without/with using RegExp "g"' - ); - t.equal( - match("http:", "http://example.com/jquery.min.js", { flags: "g" }), - true, - 'matches without/with using RegExp "g"' - ); - t.equal( - match("min.js", "http://example.com/jquery.min.js"), - false, - 'matches without/with using RegExp "g"' - ); - t.equal( - match("*.min.js", "http://example.com/jquery.min.js"), - true, - 'matches without/with using RegExp "g"' - ); - t.equal( - match("min.js", "http://example.com/jquery.min.js", { flags: "g" }), - true, - 'matches without/with using RegExp "g"' - ); - t.equal( - match("min", "http://example.com/jquery.min.js", { flags: "g" }), - true, - 'match anywhere (globally) using RegExp "g"' - ); - t.equal( - match("/js/", "http://example.com/js/jquery.min.js", { flags: "g" }), - true, - 'match anywhere (globally) using RegExp "g"' - ); - t.equal(match("/js*jq*.js", "http://example.com/js/jquery.min.js"), false); - t.equal( - match("/js*jq*.js", "http://example.com/js/jquery.min.js", { - flags: "g", - }), - true - ); - }, -}); - -test({ - name: "globrex: ? match one character, no more and no less", - fn(): void { - t.equal(match("f?o", "foo", { extended: true }), true); - t.equal(match("f?o", "fooo", { extended: true }), false); - t.equal(match("f?oo", "foo", { extended: true }), false); - - const tester = (globstar: boolean): void => { - t.equal( - match("f?o", "foo", { extended: true, globstar, flags: "g" }), - true - ); - t.equal( - match("f?o", "fooo", { extended: true, globstar, flags: "g" }), - true - ); - t.equal( - match("f?o?", "fooo", { extended: true, globstar, flags: "g" }), - true - ); - - t.equal( - match("?fo", "fooo", { extended: true, globstar, flags: "g" }), - false - ); - t.equal( - match("f?oo", "foo", { extended: true, globstar, flags: "g" }), - false - ); - t.equal( - match("foo?", "foo", { extended: true, globstar, flags: "g" }), - false - ); - }; - - tester(true); - tester(false); - }, -}); - -test({ - name: "globrex: [] match a character range", - fn(): void { - t.equal(match("fo[oz]", "foo", { extended: true }), true); - t.equal(match("fo[oz]", "foz", { extended: true }), true); - t.equal(match("fo[oz]", "fog", { extended: true }), false); - t.equal(match("fo[a-z]", "fob", { extended: true }), true); - t.equal(match("fo[a-d]", "fot", { extended: true }), false); - t.equal(match("fo[!tz]", "fot", { extended: true }), false); - t.equal(match("fo[!tz]", "fob", { extended: true }), true); - - const tester = (globstar: boolean): void => { - t.equal( - match("fo[oz]", "foo", { extended: true, globstar, flags: "g" }), - true - ); - t.equal( - match("fo[oz]", "foz", { extended: true, globstar, flags: "g" }), - true - ); - t.equal( - match("fo[oz]", "fog", { extended: true, globstar, flags: "g" }), - false - ); - }; - - tester(true); - tester(false); - }, -}); - -test({ - name: "globrex: [] extended character ranges", - fn(): void { - t.equal( - match("[[:alnum:]]/bar.txt", "a/bar.txt", { extended: true }), - true - ); - t.equal( - match("@([[:alnum:]abc]|11)/bar.txt", "11/bar.txt", { extended: true }), - true - ); - t.equal( - match("@([[:alnum:]abc]|11)/bar.txt", "a/bar.txt", { extended: true }), - true - ); - t.equal( - match("@([[:alnum:]abc]|11)/bar.txt", "b/bar.txt", { extended: true }), - true - ); - t.equal( - match("@([[:alnum:]abc]|11)/bar.txt", "c/bar.txt", { extended: true }), - true - ); - t.equal( - match("@([[:alnum:]abc]|11)/bar.txt", "abc/bar.txt", { extended: true }), - false - ); - t.equal( - match("@([[:alnum:]abc]|11)/bar.txt", "3/bar.txt", { extended: true }), - true - ); - t.equal( - match("[[:digit:]]/bar.txt", "1/bar.txt", { extended: true }), - true - ); - t.equal( - match("[[:digit:]b]/bar.txt", "b/bar.txt", { extended: true }), - true - ); - t.equal( - match("[![:digit:]b]/bar.txt", "a/bar.txt", { extended: true }), - true - ); - t.equal( - match("[[:alnum:]]/bar.txt", "!/bar.txt", { extended: true }), - false - ); - t.equal( - match("[[:digit:]]/bar.txt", "a/bar.txt", { extended: true }), - false - ); - t.equal( - match("[[:digit:]b]/bar.txt", "a/bar.txt", { extended: true }), - false - ); - }, -}); - -test({ - name: "globrex: {} match a choice of different substrings", - fn(): void { - t.equal(match("foo{bar,baaz}", "foobaaz", { extended: true }), true); - t.equal(match("foo{bar,baaz}", "foobar", { extended: true }), true); - t.equal(match("foo{bar,baaz}", "foobuzz", { extended: true }), false); - t.equal(match("foo{bar,b*z}", "foobuzz", { extended: true }), true); - - const tester = (globstar: boolean): void => { - t.equal( - match("foo{bar,baaz}", "foobaaz", { - extended: true, - globstar, - flag: "g", - }), - true - ); - t.equal( - match("foo{bar,baaz}", "foobar", { - extended: true, - globstar, - flag: "g", - }), - true - ); - t.equal( - match("foo{bar,baaz}", "foobuzz", { - extended: true, - globstar, - flag: "g", - }), - false - ); - t.equal( - match("foo{bar,b*z}", "foobuzz", { - extended: true, - globstar, - flag: "g", - }), - true - ); - }; - - tester(true); - tester(false); - }, -}); - -test({ - name: "globrex: complex extended matches", - fn(): void { - t.equal( - match( - "http://?o[oz].b*z.com/{*.js,*.html}", - "http://foo.baaz.com/jquery.min.js", - { extended: true } - ), - true - ); - t.equal( - match( - "http://?o[oz].b*z.com/{*.js,*.html}", - "http://moz.buzz.com/index.html", - { extended: true } - ), - true - ); - t.equal( - match( - "http://?o[oz].b*z.com/{*.js,*.html}", - "http://moz.buzz.com/index.htm", - { extended: true } - ), - false - ); - t.equal( - match( - "http://?o[oz].b*z.com/{*.js,*.html}", - "http://moz.bar.com/index.html", - { extended: true } - ), - false - ); - t.equal( - match( - "http://?o[oz].b*z.com/{*.js,*.html}", - "http://flozz.buzz.com/index.html", - { extended: true } - ), - false - ); - - const tester = (globstar: boolean): void => { - t.equal( - match( - "http://?o[oz].b*z.com/{*.js,*.html}", - "http://foo.baaz.com/jquery.min.js", - { extended: true, globstar, flags: "g" } - ), - true - ); - t.equal( - match( - "http://?o[oz].b*z.com/{*.js,*.html}", - "http://moz.buzz.com/index.html", - { extended: true, globstar, flags: "g" } - ), - true - ); - t.equal( - match( - "http://?o[oz].b*z.com/{*.js,*.html}", - "http://moz.buzz.com/index.htm", - { extended: true, globstar, flags: "g" } - ), - false - ); - t.equal( - match( - "http://?o[oz].b*z.com/{*.js,*.html}", - "http://moz.bar.com/index.html", - { extended: true, globstar, flags: "g" } - ), - false - ); - t.equal( - match( - "http://?o[oz].b*z.com/{*.js,*.html}", - "http://flozz.buzz.com/index.html", - { extended: true, globstar, flags: "g" } - ), - false - ); - }; - - tester(true); - tester(false); - }, -}); - -test({ - name: "globrex: standard globstar", - fn(): void { - const tester = (globstar: boolean): void => { - t.equal( - match( - "http://foo.com/**/{*.js,*.html}", - "http://foo.com/bar/jquery.min.js", - { extended: true, globstar, flags: "g" } - ), - true - ); - t.equal( - match( - "http://foo.com/**/{*.js,*.html}", - "http://foo.com/bar/baz/jquery.min.js", - { extended: true, globstar, flags: "g" } - ), - true - ); - t.equal( - match("http://foo.com/**", "http://foo.com/bar/baz/jquery.min.js", { - extended: true, - globstar, - flags: "g", - }), - true - ); - }; - - tester(true); - tester(false); - }, -}); - -test({ - name: "globrex: remaining chars should match themself", - fn(): void { - const tester = (globstar: boolean): void => { - const testExtStr = "\\/$^+.()=!|,.*"; - t.equal(match(testExtStr, testExtStr, { extended: true }), true); - t.equal( - match(testExtStr, testExtStr, { extended: true, globstar, flags: "g" }), - true - ); - }; - - tester(true); - tester(false); - }, -}); - -test({ - name: "globrex: globstar advance testing", - fn(): void { - t.equal(match("/foo/*", "/foo/bar.txt", { globstar: true }), true); - t.equal(match("/foo/**", "/foo/bar.txt", { globstar: true }), true); - t.equal(match("/foo/**", "/foo/bar/baz.txt", { globstar: true }), true); - t.equal(match("/foo/**", "/foo/bar/baz.txt", { globstar: true }), true); - t.equal( - match("/foo/*/*.txt", "/foo/bar/baz.txt", { globstar: true }), - true - ); - t.equal( - match("/foo/**/*.txt", "/foo/bar/baz.txt", { globstar: true }), - true - ); - t.equal( - match("/foo/**/*.txt", "/foo/bar/baz/qux.txt", { globstar: true }), - true - ); - t.equal(match("/foo/**/bar.txt", "/foo/bar.txt", { globstar: true }), true); - t.equal( - match("/foo/**/**/bar.txt", "/foo/bar.txt", { globstar: true }), - true - ); - t.equal( - match("/foo/**/*/baz.txt", "/foo/bar/baz.txt", { globstar: true }), - true - ); - t.equal(match("/foo/**/*.txt", "/foo/bar.txt", { globstar: true }), true); - t.equal( - match("/foo/**/**/*.txt", "/foo/bar.txt", { globstar: true }), - true - ); - t.equal( - match("/foo/**/*/*.txt", "/foo/bar/baz.txt", { globstar: true }), - true - ); - t.equal( - match("**/*.txt", "/foo/bar/baz/qux.txt", { globstar: true }), - true - ); - t.equal(match("**/foo.txt", "foo.txt", { globstar: true }), true); - t.equal(match("**/*.txt", "foo.txt", { globstar: true }), true); - t.equal(match("/foo/*", "/foo/bar/baz.txt", { globstar: true }), false); - t.equal(match("/foo/*.txt", "/foo/bar/baz.txt", { globstar: true }), false); - t.equal( - match("/foo/*/*.txt", "/foo/bar/baz/qux.txt", { globstar: true }), - false - ); - t.equal(match("/foo/*/bar.txt", "/foo/bar.txt", { globstar: true }), false); - t.equal( - match("/foo/*/*/baz.txt", "/foo/bar/baz.txt", { globstar: true }), - false - ); - t.equal( - match("/foo/**.txt", "/foo/bar/baz/qux.txt", { globstar: true }), - false - ); - t.equal( - match("/foo/bar**/*.txt", "/foo/bar/baz/qux.txt", { globstar: true }), - false - ); - t.equal(match("/foo/bar**", "/foo/bar/baz.txt", { globstar: true }), false); - t.equal( - match("**/.txt", "/foo/bar/baz/qux.txt", { globstar: true }), - false - ); - t.equal( - match("*/*.txt", "/foo/bar/baz/qux.txt", { globstar: true }), - false - ); - t.equal(match("*/*.txt", "foo.txt", { globstar: true }), false); - t.equal( - match("http://foo.com/*", "http://foo.com/bar/baz/jquery.min.js", { - extended: true, - globstar: true, - }), - false - ); - t.equal( - match("http://foo.com/*", "http://foo.com/bar/baz/jquery.min.js", { - globstar: true, - }), - false - ); - t.equal( - match("http://foo.com/*", "http://foo.com/bar/baz/jquery.min.js", { - globstar: false, - }), - true - ); - t.equal( - match("http://foo.com/**", "http://foo.com/bar/baz/jquery.min.js", { - globstar: true, - }), - true - ); - t.equal( - match( - "http://foo.com/*/*/jquery.min.js", - "http://foo.com/bar/baz/jquery.min.js", - { globstar: true } - ), - true - ); - t.equal( - match( - "http://foo.com/**/jquery.min.js", - "http://foo.com/bar/baz/jquery.min.js", - { globstar: true } - ), - true - ); - t.equal( - match( - "http://foo.com/*/*/jquery.min.js", - "http://foo.com/bar/baz/jquery.min.js", - { globstar: false } - ), - true - ); - t.equal( - match( - "http://foo.com/*/jquery.min.js", - "http://foo.com/bar/baz/jquery.min.js", - { globstar: false } - ), - true - ); - t.equal( - match( - "http://foo.com/*/jquery.min.js", - "http://foo.com/bar/baz/jquery.min.js", - { globstar: true } - ), - false - ); - }, -}); - -test({ - name: "globrex: extended extglob ?", - fn(): void { - t.equal(match("(foo).txt", "(foo).txt", { extended: true }), true); - t.equal(match("?(foo).txt", "foo.txt", { extended: true }), true); - t.equal(match("?(foo).txt", ".txt", { extended: true }), true); - t.equal(match("?(foo|bar)baz.txt", "foobaz.txt", { extended: true }), true); - t.equal( - match("?(ba[zr]|qux)baz.txt", "bazbaz.txt", { extended: true }), - true - ); - t.equal( - match("?(ba[zr]|qux)baz.txt", "barbaz.txt", { extended: true }), - true - ); - t.equal( - match("?(ba[zr]|qux)baz.txt", "quxbaz.txt", { extended: true }), - true - ); - t.equal( - match("?(ba[!zr]|qux)baz.txt", "batbaz.txt", { extended: true }), - true - ); - t.equal(match("?(ba*|qux)baz.txt", "batbaz.txt", { extended: true }), true); - t.equal( - match("?(ba*|qux)baz.txt", "batttbaz.txt", { extended: true }), - true - ); - t.equal(match("?(ba*|qux)baz.txt", "quxbaz.txt", { extended: true }), true); - t.equal( - match("?(ba?(z|r)|qux)baz.txt", "bazbaz.txt", { extended: true }), - true - ); - t.equal( - match("?(ba?(z|?(r))|qux)baz.txt", "bazbaz.txt", { extended: true }), - true - ); - t.equal(match("?(foo).txt", "foo.txt", { extended: false }), false); - t.equal( - match("?(foo|bar)baz.txt", "foobarbaz.txt", { extended: true }), - false - ); - t.equal( - match("?(ba[zr]|qux)baz.txt", "bazquxbaz.txt", { extended: true }), - false - ); - t.equal( - match("?(ba[!zr]|qux)baz.txt", "bazbaz.txt", { extended: true }), - false - ); - }, -}); - -test({ - name: "globrex: extended extglob *", - fn(): void { - t.equal(match("*(foo).txt", "foo.txt", { extended: true }), true); - t.equal(match("*foo.txt", "bofoo.txt", { extended: true }), true); - t.equal(match("*(foo).txt", "foofoo.txt", { extended: true }), true); - t.equal(match("*(foo).txt", ".txt", { extended: true }), true); - t.equal(match("*(fooo).txt", ".txt", { extended: true }), true); - t.equal(match("*(fooo).txt", "foo.txt", { extended: true }), false); - t.equal(match("*(foo|bar).txt", "foobar.txt", { extended: true }), true); - t.equal(match("*(foo|bar).txt", "barbar.txt", { extended: true }), true); - t.equal(match("*(foo|bar).txt", "barfoobar.txt", { extended: true }), true); - t.equal(match("*(foo|bar).txt", ".txt", { extended: true }), true); - t.equal(match("*(foo|ba[rt]).txt", "bat.txt", { extended: true }), true); - t.equal(match("*(foo|b*[rt]).txt", "blat.txt", { extended: true }), true); - t.equal(match("*(foo|b*[rt]).txt", "tlat.txt", { extended: true }), false); - t.equal( - match("*(*).txt", "whatever.txt", { extended: true, globstar: true }), - true - ); - t.equal( - match("*(foo|bar)/**/*.txt", "foo/hello/world/bar.txt", { - extended: true, - globstar: true, - }), - true - ); - t.equal( - match("*(foo|bar)/**/*.txt", "foo/world/bar.txt", { - extended: true, - globstar: true, - }), - true - ); - }, -}); - -test({ - name: "globrex: extended extglob +", - fn(): void { - t.equal(match("+(foo).txt", "foo.txt", { extended: true }), true); - t.equal(match("+foo.txt", "+foo.txt", { extended: true }), true); - t.equal(match("+(foo).txt", ".txt", { extended: true }), false); - t.equal(match("+(foo|bar).txt", "foobar.txt", { extended: true }), true); - }, -}); - -test({ - name: "globrex: extended extglob @", - fn(): void { - t.equal(match("@(foo).txt", "foo.txt", { extended: true }), true); - t.equal(match("@foo.txt", "@foo.txt", { extended: true }), true); - t.equal(match("@(foo|baz)bar.txt", "foobar.txt", { extended: true }), true); - t.equal( - match("@(foo|baz)bar.txt", "foobazbar.txt", { extended: true }), - false - ); - t.equal( - match("@(foo|baz)bar.txt", "foofoobar.txt", { extended: true }), - false - ); - t.equal( - match("@(foo|baz)bar.txt", "toofoobar.txt", { extended: true }), - false - ); - }, -}); - -test({ - name: "globrex: extended extglob !", - fn(): void { - t.equal(match("!(boo).txt", "foo.txt", { extended: true }), true); - t.equal(match("!(foo|baz)bar.txt", "buzbar.txt", { extended: true }), true); - t.equal(match("!bar.txt", "!bar.txt", { extended: true }), true); - t.equal( - match("!({foo,bar})baz.txt", "notbaz.txt", { extended: true }), - true - ); - t.equal( - match("!({foo,bar})baz.txt", "foobaz.txt", { extended: true }), - false - ); - }, -}); - -test({ - name: "globrex: strict", - fn(): void { - t.equal(match("foo//bar.txt", "foo/bar.txt"), true); - t.equal(match("foo///bar.txt", "foo/bar.txt"), true); - t.equal(match("foo///bar.txt", "foo/bar.txt", { strict: true }), false); - }, -}); - -test({ - name: "globrex: stress testing", - fn(): void { - t.equal( - match("**/*/?yfile.{md,js,txt}", "foo/bar/baz/myfile.md", { - extended: true, - }), - true - ); - t.equal( - match("**/*/?yfile.{md,js,txt}", "foo/baz/myfile.md", { extended: true }), - true - ); - t.equal( - match("**/*/?yfile.{md,js,txt}", "foo/baz/tyfile.js", { extended: true }), - true - ); - t.equal( - match("[[:digit:]_.]/file.js", "1/file.js", { extended: true }), - true - ); - t.equal( - match("[[:digit:]_.]/file.js", "2/file.js", { extended: true }), - true - ); - t.equal( - match("[[:digit:]_.]/file.js", "_/file.js", { extended: true }), - true - ); - t.equal( - match("[[:digit:]_.]/file.js", "./file.js", { extended: true }), - true - ); - t.equal( - match("[[:digit:]_.]/file.js", "z/file.js", { extended: true }), - false - ); - }, -}); diff --git a/std/path/mod.ts b/std/path/mod.ts index 104e0b616..9cb7f1edb 100644 --- a/std/path/mod.ts +++ b/std/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/std/path/posix.ts b/std/path/posix.ts index 156aba796..e88eb3f97 100644 --- a/std/path/posix.ts +++ b/std/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/std/path/separator.ts b/std/path/separator.ts new file mode 100644 index 000000000..fb990b808 --- /dev/null +++ b/std/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/std/path/utils.ts b/std/path/utils.ts deleted file mode 100644 index fc3dc5be9..000000000 --- a/std/path/utils.ts +++ /dev/null @@ -1,116 +0,0 @@ -// Copyright the Browserify authors. MIT License. -// Ported from https://github.com/browserify/path-browserify/ - -import { FormatInputPathObject } from "./interface.ts"; -import { - CHAR_UPPERCASE_A, - CHAR_LOWERCASE_A, - CHAR_UPPERCASE_Z, - CHAR_LOWERCASE_Z, - CHAR_DOT, - CHAR_FORWARD_SLASH, - CHAR_BACKWARD_SLASH, -} from "./constants.ts"; - -export function assertPath(path: string): void { - if (typeof path !== "string") { - throw new TypeError( - `Path must be a string. Received ${JSON.stringify(path)}` - ); - } -} - -export function isPosixPathSeparator(code: number): boolean { - return code === CHAR_FORWARD_SLASH; -} - -export function isPathSeparator(code: number): boolean { - return isPosixPathSeparator(code) || code === CHAR_BACKWARD_SLASH; -} - -export function isWindowsDeviceRoot(code: number): boolean { - return ( - (code >= CHAR_LOWERCASE_A && code <= CHAR_LOWERCASE_Z) || - (code >= CHAR_UPPERCASE_A && code <= CHAR_UPPERCASE_Z) - ); -} - -// Resolves . and .. elements in a path with directory names -export function normalizeString( - path: string, - allowAboveRoot: boolean, - separator: string, - isPathSeparator: (code: number) => boolean -): string { - let res = ""; - let lastSegmentLength = 0; - let lastSlash = -1; - let dots = 0; - let code: number | undefined; - for (let i = 0, len = path.length; i <= len; ++i) { - if (i < len) code = path.charCodeAt(i); - else if (isPathSeparator(code!)) break; - else code = CHAR_FORWARD_SLASH; - - if (isPathSeparator(code!)) { - if (lastSlash === i - 1 || dots === 1) { - // NOOP - } else if (lastSlash !== i - 1 && dots === 2) { - if ( - res.length < 2 || - lastSegmentLength !== 2 || - res.charCodeAt(res.length - 1) !== CHAR_DOT || - res.charCodeAt(res.length - 2) !== CHAR_DOT - ) { - if (res.length > 2) { - const lastSlashIndex = res.lastIndexOf(separator); - if (lastSlashIndex === -1) { - res = ""; - lastSegmentLength = 0; - } else { - res = res.slice(0, lastSlashIndex); - lastSegmentLength = res.length - 1 - res.lastIndexOf(separator); - } - lastSlash = i; - dots = 0; - continue; - } else if (res.length === 2 || res.length === 1) { - res = ""; - lastSegmentLength = 0; - lastSlash = i; - dots = 0; - continue; - } - } - if (allowAboveRoot) { - if (res.length > 0) res += `${separator}..`; - else res = ".."; - lastSegmentLength = 2; - } - } else { - if (res.length > 0) res += separator + path.slice(lastSlash + 1, i); - else res = path.slice(lastSlash + 1, i); - lastSegmentLength = i - lastSlash - 1; - } - lastSlash = i; - dots = 0; - } else if (code === CHAR_DOT && dots !== -1) { - ++dots; - } else { - dots = -1; - } - } - return res; -} - -export function _format( - sep: string, - pathObject: FormatInputPathObject -): string { - const dir: string | undefined = pathObject.dir || pathObject.root; - const base: string = - pathObject.base || (pathObject.name || "") + (pathObject.ext || ""); - if (!dir) return base; - if (dir === pathObject.root) return dir + base; - return dir + sep + base; -} diff --git a/std/path/win32.ts b/std/path/win32.ts index 401e572db..0557b3768 100644 --- a/std/path/win32.ts +++ b/std/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/std/signal/mod.ts b/std/signal/mod.ts index a463142e6..c15d1b326 100644 --- a/std/signal/mod.ts +++ b/std/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/std/signal/test.ts b/std/signal/test.ts index c4d1bf3a7..15ebbdcc7 100644 --- a/std/signal/test.ts +++ b/std/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/std/util/async.ts b/std/util/async.ts deleted file mode 100644 index bb50e482d..000000000 --- a/std/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/std/util/async_test.ts b/std/util/async_test.ts deleted file mode 100644 index f3f17312a..000000000 --- a/std/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/std/util/deep_assign.ts b/std/util/deep_assign.ts deleted file mode 100644 index 9034d89bd..000000000 --- a/std/util/deep_assign.ts +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { assert } from "../testing/asserts.ts"; - -export function deepAssign( - target: Record, - ...sources: object[] -): object | undefined { - for (let i = 0; i < sources.length; i++) { - const source = sources[i]; - if (!source || typeof source !== `object`) { - return; - } - Object.entries(source).forEach(([key, value]: [string, unknown]): void => { - if (value instanceof Date) { - target[key] = new Date(value); - return; - } - if (!value || typeof value !== `object`) { - target[key] = value; - return; - } - if (Array.isArray(value)) { - target[key] = []; - } - // value is an Object - if (typeof target[key] !== `object` || !target[key]) { - target[key] = {}; - } - assert(value); - deepAssign(target[key] as Record, value); - }); - } - return target; -} diff --git a/std/util/deep_assign_test.ts b/std/util/deep_assign_test.ts deleted file mode 100644 index f1a56e1ad..000000000 --- a/std/util/deep_assign_test.ts +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -const { test } = Deno; -import { assertEquals, assert } from "../testing/asserts.ts"; -import { deepAssign } from "./deep_assign.ts"; - -test("deepAssignTest", function (): void { - const date = new Date("1979-05-27T07:32:00Z"); - const reg = RegExp(/DENOWOWO/); - const obj1 = { deno: { bar: { deno: ["is", "not", "node"] } } }; - const obj2 = { foo: { deno: date } }; - const obj3 = { foo: { bar: "deno" }, reg: reg }; - const actual = deepAssign(obj1, obj2, obj3); - const expected = { - foo: { - deno: new Date("1979-05-27T07:32:00Z"), - bar: "deno", - }, - deno: { bar: { deno: ["is", "not", "node"] } }, - reg: RegExp(/DENOWOWO/), - }; - assert(date !== expected.foo.deno); - assert(reg !== expected.reg); - assertEquals(actual, expected); -}); diff --git a/std/util/has_own_property.ts b/std/util/has_own_property.ts deleted file mode 100644 index 351905cec..000000000 --- a/std/util/has_own_property.ts +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. - -/** - * Determines whether an object has a property with the specified name. - * Avoid calling prototype builtin `hasOwnProperty` for two reasons: - * - * 1. `hasOwnProperty` is defined on the object as something else: - * - * const options = { - * ending: 'utf8', - * hasOwnProperty: 'foo' - * }; - * options.hasOwnProperty('ending') // throws a TypeError - * - * 2. The object doesn't inherit from `Object.prototype`: - * - * const options = Object.create(null); - * options.ending = 'utf8'; - * options.hasOwnProperty('ending'); // throws a TypeError - * - * @param obj A Object. - * @param v A property name. - * @see https://eslint.org/docs/rules/no-prototype-builtins - */ -export function hasOwnProperty(obj: T, v: PropertyKey): boolean { - if (obj == null) { - return false; - } - return Object.prototype.hasOwnProperty.call(obj, v); -} diff --git a/std/util/sha1.ts b/std/util/sha1.ts deleted file mode 100644 index f47192554..000000000 --- a/std/util/sha1.ts +++ /dev/null @@ -1,374 +0,0 @@ -/* - * [js-sha1]{@link https://github.com/emn178/js-sha1} - * - * @version 0.6.0 - * @author Chen, Yi-Cyuan [emn178@gmail.com] - * @copyright Chen, Yi-Cyuan 2014-2017 - * @license MIT - */ - -const HEX_CHARS = "0123456789abcdef".split(""); -const EXTRA = Uint32Array.of(-2147483648, 8388608, 32768, 128); -const SHIFT = Uint32Array.of(24, 16, 8, 0); - -const blocks = new Uint32Array(80); - -export class Sha1 { - #blocks: Uint32Array; - #block: number; - #start: number; - #bytes: number; - #hBytes: number; - #finalized: boolean; - #hashed: boolean; - - #h0 = 0x67452301; - #h1 = 0xefcdab89; - #h2 = 0x98badcfe; - #h3 = 0x10325476; - #h4 = 0xc3d2e1f0; - #lastByteIndex = 0; - - constructor(sharedMemory = false) { - if (sharedMemory) { - this.#blocks = blocks.fill(0, 0, 17); - } else { - this.#blocks = new Uint32Array(80); - } - - this.#h0 = 0x67452301; - this.#h1 = 0xefcdab89; - this.#h2 = 0x98badcfe; - this.#h3 = 0x10325476; - this.#h4 = 0xc3d2e1f0; - - this.#block = this.#start = this.#bytes = this.#hBytes = 0; - this.#finalized = this.#hashed = false; - } - - update(data: string | ArrayBuffer | ArrayBufferView): Sha1 { - if (this.#finalized) { - return this; - } - let notString = true; - let message; - if (data instanceof ArrayBuffer) { - message = new Uint8Array(data); - } else if (ArrayBuffer.isView(data)) { - message = new Uint8Array(data.buffer); - } else { - notString = false; - message = String(data); - } - let code; - let index = 0; - let i; - const start = this.#start; - const length = message.length || 0; - const blocks = this.#blocks; - - while (index < length) { - if (this.#hashed) { - this.#hashed = false; - blocks[0] = this.#block; - blocks.fill(0, 1, 17); - } - - if (notString) { - for (i = start; index < length && i < 64; ++index) { - blocks[i >> 2] |= (message[index] as number) << SHIFT[i++ & 3]; - } - } else { - for (i = start; index < length && i < 64; ++index) { - code = (message as string).charCodeAt(index); - if (code < 0x80) { - blocks[i >> 2] |= code << SHIFT[i++ & 3]; - } else if (code < 0x800) { - blocks[i >> 2] |= (0xc0 | (code >> 6)) << SHIFT[i++ & 3]; - blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; - } else if (code < 0xd800 || code >= 0xe000) { - blocks[i >> 2] |= (0xe0 | (code >> 12)) << SHIFT[i++ & 3]; - blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3]; - blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; - } else { - code = - 0x10000 + - (((code & 0x3ff) << 10) | - ((message as string).charCodeAt(++index) & 0x3ff)); - blocks[i >> 2] |= (0xf0 | (code >> 18)) << SHIFT[i++ & 3]; - blocks[i >> 2] |= (0x80 | ((code >> 12) & 0x3f)) << SHIFT[i++ & 3]; - blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3]; - blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; - } - } - } - - this.#lastByteIndex = i; - this.#bytes += i - start; - if (i >= 64) { - this.#block = blocks[16]; - this.#start = i - 64; - this.hash(); - this.#hashed = true; - } else { - this.#start = i; - } - } - if (this.#bytes > 4294967295) { - this.#hBytes += (this.#bytes / 4294967296) >>> 0; - this.#bytes = this.#bytes >>> 0; - } - return this; - } - - finalize(): void { - if (this.#finalized) { - return; - } - this.#finalized = true; - const blocks = this.#blocks; - const i = this.#lastByteIndex; - blocks[16] = this.#block; - blocks[i >> 2] |= EXTRA[i & 3]; - this.#block = blocks[16]; - if (i >= 56) { - if (!this.#hashed) { - this.hash(); - } - blocks[0] = this.#block; - blocks.fill(0, 1, 17); - } - blocks[14] = (this.#hBytes << 3) | (this.#bytes >>> 29); - blocks[15] = this.#bytes << 3; - this.hash(); - } - - hash(): void { - let a = this.#h0; - let b = this.#h1; - let c = this.#h2; - let d = this.#h3; - let e = this.#h4; - let f, j, t; - const blocks = this.#blocks; - - for (j = 16; j < 80; ++j) { - t = blocks[j - 3] ^ blocks[j - 8] ^ blocks[j - 14] ^ blocks[j - 16]; - blocks[j] = (t << 1) | (t >>> 31); - } - - for (j = 0; j < 20; j += 5) { - f = (b & c) | (~b & d); - t = (a << 5) | (a >>> 27); - e = (t + f + e + 1518500249 + blocks[j]) >>> 0; - b = (b << 30) | (b >>> 2); - - f = (a & b) | (~a & c); - t = (e << 5) | (e >>> 27); - d = (t + f + d + 1518500249 + blocks[j + 1]) >>> 0; - a = (a << 30) | (a >>> 2); - - f = (e & a) | (~e & b); - t = (d << 5) | (d >>> 27); - c = (t + f + c + 1518500249 + blocks[j + 2]) >>> 0; - e = (e << 30) | (e >>> 2); - - f = (d & e) | (~d & a); - t = (c << 5) | (c >>> 27); - b = (t + f + b + 1518500249 + blocks[j + 3]) >>> 0; - d = (d << 30) | (d >>> 2); - - f = (c & d) | (~c & e); - t = (b << 5) | (b >>> 27); - a = (t + f + a + 1518500249 + blocks[j + 4]) >>> 0; - c = (c << 30) | (c >>> 2); - } - - for (; j < 40; j += 5) { - f = b ^ c ^ d; - t = (a << 5) | (a >>> 27); - e = (t + f + e + 1859775393 + blocks[j]) >>> 0; - b = (b << 30) | (b >>> 2); - - f = a ^ b ^ c; - t = (e << 5) | (e >>> 27); - d = (t + f + d + 1859775393 + blocks[j + 1]) >>> 0; - a = (a << 30) | (a >>> 2); - - f = e ^ a ^ b; - t = (d << 5) | (d >>> 27); - c = (t + f + c + 1859775393 + blocks[j + 2]) >>> 0; - e = (e << 30) | (e >>> 2); - - f = d ^ e ^ a; - t = (c << 5) | (c >>> 27); - b = (t + f + b + 1859775393 + blocks[j + 3]) >>> 0; - d = (d << 30) | (d >>> 2); - - f = c ^ d ^ e; - t = (b << 5) | (b >>> 27); - a = (t + f + a + 1859775393 + blocks[j + 4]) >>> 0; - c = (c << 30) | (c >>> 2); - } - - for (; j < 60; j += 5) { - f = (b & c) | (b & d) | (c & d); - t = (a << 5) | (a >>> 27); - e = (t + f + e - 1894007588 + blocks[j]) >>> 0; - b = (b << 30) | (b >>> 2); - - f = (a & b) | (a & c) | (b & c); - t = (e << 5) | (e >>> 27); - d = (t + f + d - 1894007588 + blocks[j + 1]) >>> 0; - a = (a << 30) | (a >>> 2); - - f = (e & a) | (e & b) | (a & b); - t = (d << 5) | (d >>> 27); - c = (t + f + c - 1894007588 + blocks[j + 2]) >>> 0; - e = (e << 30) | (e >>> 2); - - f = (d & e) | (d & a) | (e & a); - t = (c << 5) | (c >>> 27); - b = (t + f + b - 1894007588 + blocks[j + 3]) >>> 0; - d = (d << 30) | (d >>> 2); - - f = (c & d) | (c & e) | (d & e); - t = (b << 5) | (b >>> 27); - a = (t + f + a - 1894007588 + blocks[j + 4]) >>> 0; - c = (c << 30) | (c >>> 2); - } - - for (; j < 80; j += 5) { - f = b ^ c ^ d; - t = (a << 5) | (a >>> 27); - e = (t + f + e - 899497514 + blocks[j]) >>> 0; - b = (b << 30) | (b >>> 2); - - f = a ^ b ^ c; - t = (e << 5) | (e >>> 27); - d = (t + f + d - 899497514 + blocks[j + 1]) >>> 0; - a = (a << 30) | (a >>> 2); - - f = e ^ a ^ b; - t = (d << 5) | (d >>> 27); - c = (t + f + c - 899497514 + blocks[j + 2]) >>> 0; - e = (e << 30) | (e >>> 2); - - f = d ^ e ^ a; - t = (c << 5) | (c >>> 27); - b = (t + f + b - 899497514 + blocks[j + 3]) >>> 0; - d = (d << 30) | (d >>> 2); - - f = c ^ d ^ e; - t = (b << 5) | (b >>> 27); - a = (t + f + a - 899497514 + blocks[j + 4]) >>> 0; - c = (c << 30) | (c >>> 2); - } - - this.#h0 = (this.#h0 + a) >>> 0; - this.#h1 = (this.#h1 + b) >>> 0; - this.#h2 = (this.#h2 + c) >>> 0; - this.#h3 = (this.#h3 + d) >>> 0; - this.#h4 = (this.#h4 + e) >>> 0; - } - - hex(): string { - this.finalize(); - - const h0 = this.#h0; - const h1 = this.#h1; - const h2 = this.#h2; - const h3 = this.#h3; - const h4 = this.#h4; - - return ( - HEX_CHARS[(h0 >> 28) & 0x0f] + - HEX_CHARS[(h0 >> 24) & 0x0f] + - HEX_CHARS[(h0 >> 20) & 0x0f] + - HEX_CHARS[(h0 >> 16) & 0x0f] + - HEX_CHARS[(h0 >> 12) & 0x0f] + - HEX_CHARS[(h0 >> 8) & 0x0f] + - HEX_CHARS[(h0 >> 4) & 0x0f] + - HEX_CHARS[h0 & 0x0f] + - HEX_CHARS[(h1 >> 28) & 0x0f] + - HEX_CHARS[(h1 >> 24) & 0x0f] + - HEX_CHARS[(h1 >> 20) & 0x0f] + - HEX_CHARS[(h1 >> 16) & 0x0f] + - HEX_CHARS[(h1 >> 12) & 0x0f] + - HEX_CHARS[(h1 >> 8) & 0x0f] + - HEX_CHARS[(h1 >> 4) & 0x0f] + - HEX_CHARS[h1 & 0x0f] + - HEX_CHARS[(h2 >> 28) & 0x0f] + - HEX_CHARS[(h2 >> 24) & 0x0f] + - HEX_CHARS[(h2 >> 20) & 0x0f] + - HEX_CHARS[(h2 >> 16) & 0x0f] + - HEX_CHARS[(h2 >> 12) & 0x0f] + - HEX_CHARS[(h2 >> 8) & 0x0f] + - HEX_CHARS[(h2 >> 4) & 0x0f] + - HEX_CHARS[h2 & 0x0f] + - HEX_CHARS[(h3 >> 28) & 0x0f] + - HEX_CHARS[(h3 >> 24) & 0x0f] + - HEX_CHARS[(h3 >> 20) & 0x0f] + - HEX_CHARS[(h3 >> 16) & 0x0f] + - HEX_CHARS[(h3 >> 12) & 0x0f] + - HEX_CHARS[(h3 >> 8) & 0x0f] + - HEX_CHARS[(h3 >> 4) & 0x0f] + - HEX_CHARS[h3 & 0x0f] + - HEX_CHARS[(h4 >> 28) & 0x0f] + - HEX_CHARS[(h4 >> 24) & 0x0f] + - HEX_CHARS[(h4 >> 20) & 0x0f] + - HEX_CHARS[(h4 >> 16) & 0x0f] + - HEX_CHARS[(h4 >> 12) & 0x0f] + - HEX_CHARS[(h4 >> 8) & 0x0f] + - HEX_CHARS[(h4 >> 4) & 0x0f] + - HEX_CHARS[h4 & 0x0f] - ); - } - - toString(): string { - return this.hex(); - } - - digest(): number[] { - this.finalize(); - - const h0 = this.#h0; - const h1 = this.#h1; - const h2 = this.#h2; - const h3 = this.#h3; - const h4 = this.#h4; - - return [ - (h0 >> 24) & 0xff, - (h0 >> 16) & 0xff, - (h0 >> 8) & 0xff, - h0 & 0xff, - (h1 >> 24) & 0xff, - (h1 >> 16) & 0xff, - (h1 >> 8) & 0xff, - h1 & 0xff, - (h2 >> 24) & 0xff, - (h2 >> 16) & 0xff, - (h2 >> 8) & 0xff, - h2 & 0xff, - (h3 >> 24) & 0xff, - (h3 >> 16) & 0xff, - (h3 >> 8) & 0xff, - h3 & 0xff, - (h4 >> 24) & 0xff, - (h4 >> 16) & 0xff, - (h4 >> 8) & 0xff, - h4 & 0xff, - ]; - } - - array(): number[] { - return this.digest(); - } - - arrayBuffer(): ArrayBuffer { - this.finalize(); - return Uint32Array.of(this.#h0, this.#h1, this.#h2, this.#h3, this.#h4) - .buffer; - } -} diff --git a/std/util/sha1_test.ts b/std/util/sha1_test.ts deleted file mode 100644 index 159bdd94d..000000000 --- a/std/util/sha1_test.ts +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -const { test } = Deno; -import { assertEquals } from "../testing/asserts.ts"; -import { Sha1 } from "./sha1.ts"; - -test("[util/sha] test1", () => { - const sha1 = new Sha1(); - sha1.update("abcde"); - assertEquals(sha1.toString(), "03de6c570bfe24bfc328ccd7ca46b76eadaf4334"); -}); - -test("[util/sha] testWithArray", () => { - const data = Uint8Array.of(0x61, 0x62, 0x63, 0x64, 0x65); - const sha1 = new Sha1(); - sha1.update(data); - assertEquals(sha1.toString(), "03de6c570bfe24bfc328ccd7ca46b76eadaf4334"); -}); - -test("[util/sha] testSha1WithBuffer", () => { - const data = Uint8Array.of(0x61, 0x62, 0x63, 0x64, 0x65); - const sha1 = new Sha1(); - sha1.update(data.buffer); - assertEquals(sha1.toString(), "03de6c570bfe24bfc328ccd7ca46b76eadaf4334"); -}); diff --git a/std/util/sha256.ts b/std/util/sha256.ts deleted file mode 100644 index 02fff94d1..000000000 --- a/std/util/sha256.ts +++ /dev/null @@ -1,575 +0,0 @@ -/* - * Adapted to deno from: - * - * [js-sha256]{@link https://github.com/emn178/js-sha256} - * - * @version 0.9.0 - * @author Chen, Yi-Cyuan [emn178@gmail.com] - * @copyright Chen, Yi-Cyuan 2014-2017 - * @license MIT - */ - -export type Message = string | number[] | ArrayBuffer | Uint8Array; - -const ERROR = "input is invalid type"; -const HEX_CHARS = "0123456789abcdef".split(""); -const EXTRA = [-2147483648, 8388608, 32768, 128] as const; -const SHIFT = [24, 16, 8, 0] as const; -// prettier-ignore -// dprint-ignore -const K = [ - 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, - 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, - 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, - 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, - 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, - 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, - 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, - 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, - 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, - 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, - 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2, -] as const; - -const blocks: number[] = []; - -export class Sha256 { - #block!: number; - #blocks!: number[]; - #bytes!: number; - #finalized!: boolean; - #first!: boolean; - #h0!: number; - #h1!: number; - #h2!: number; - #h3!: number; - #h4!: number; - #h5!: number; - #h6!: number; - #h7!: number; - #hashed!: boolean; - #hBytes!: number; - #is224!: boolean; - #lastByteIndex = 0; - #start!: number; - - constructor(is224 = false, sharedMemory = false) { - this.init(is224, sharedMemory); - } - - protected init(is224: boolean, sharedMemory: boolean): void { - if (sharedMemory) { - blocks[0] = blocks[16] = blocks[1] = blocks[2] = blocks[3] = blocks[4] = blocks[5] = blocks[6] = blocks[7] = blocks[8] = blocks[9] = blocks[10] = blocks[11] = blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0; - this.#blocks = blocks; - } else { - this.#blocks = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; - } - - if (is224) { - this.#h0 = 0xc1059ed8; - this.#h1 = 0x367cd507; - this.#h2 = 0x3070dd17; - this.#h3 = 0xf70e5939; - this.#h4 = 0xffc00b31; - this.#h5 = 0x68581511; - this.#h6 = 0x64f98fa7; - this.#h7 = 0xbefa4fa4; - } else { - // 256 - this.#h0 = 0x6a09e667; - this.#h1 = 0xbb67ae85; - this.#h2 = 0x3c6ef372; - this.#h3 = 0xa54ff53a; - this.#h4 = 0x510e527f; - this.#h5 = 0x9b05688c; - this.#h6 = 0x1f83d9ab; - this.#h7 = 0x5be0cd19; - } - - this.#block = this.#start = this.#bytes = this.#hBytes = 0; - this.#finalized = this.#hashed = false; - this.#first = true; - this.#is224 = is224; - } - - /** Update hash - * - * @param message The message you want to hash. - */ - update(message: Message): this { - if (this.#finalized) { - return this; - } - let msg: string | number[] | Uint8Array | undefined; - if (typeof message !== "string") { - if (typeof message === "object") { - if (message === null) { - throw new Error(ERROR); - } else if (message instanceof ArrayBuffer) { - msg = new Uint8Array(message); - } else if (!Array.isArray(message)) { - if (!ArrayBuffer.isView(message)) { - throw new Error(ERROR); - } - } - } else { - throw new Error(ERROR); - } - } - if (msg === undefined) { - msg = message as string | number[]; - } - let index = 0; - const length = msg.length; - const blocks = this.#blocks; - - while (index < length) { - let i: number; - if (this.#hashed) { - this.#hashed = false; - blocks[0] = this.#block; - blocks[16] = blocks[1] = blocks[2] = blocks[3] = blocks[4] = blocks[5] = blocks[6] = blocks[7] = blocks[8] = blocks[9] = blocks[10] = blocks[11] = blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0; - } - - if (typeof msg !== "string") { - for (i = this.#start; index < length && i < 64; ++index) { - blocks[i >> 2] |= msg[index] << SHIFT[i++ & 3]; - } - } else { - for (i = this.#start; index < length && i < 64; ++index) { - let code = msg.charCodeAt(index); - if (code < 0x80) { - blocks[i >> 2] |= code << SHIFT[i++ & 3]; - } else if (code < 0x800) { - blocks[i >> 2] |= (0xc0 | (code >> 6)) << SHIFT[i++ & 3]; - blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; - } else if (code < 0xd800 || code >= 0xe000) { - blocks[i >> 2] |= (0xe0 | (code >> 12)) << SHIFT[i++ & 3]; - blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3]; - blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; - } else { - code = - 0x10000 + - (((code & 0x3ff) << 10) | (msg.charCodeAt(++index) & 0x3ff)); - blocks[i >> 2] |= (0xf0 | (code >> 18)) << SHIFT[i++ & 3]; - blocks[i >> 2] |= (0x80 | ((code >> 12) & 0x3f)) << SHIFT[i++ & 3]; - blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3]; - blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; - } - } - } - - this.#lastByteIndex = i; - this.#bytes += i - this.#start; - if (i >= 64) { - this.#block = blocks[16]; - this.#start = i - 64; - this.hash(); - this.#hashed = true; - } else { - this.#start = i; - } - } - if (this.#bytes > 4294967295) { - this.#hBytes += (this.#bytes / 4294967296) << 0; - this.#bytes = this.#bytes % 4294967296; - } - return this; - } - - protected finalize(): void { - if (this.#finalized) { - return; - } - this.#finalized = true; - const blocks = this.#blocks; - const i = this.#lastByteIndex; - blocks[16] = this.#block; - blocks[i >> 2] |= EXTRA[i & 3]; - this.#block = blocks[16]; - if (i >= 56) { - if (!this.#hashed) { - this.hash(); - } - blocks[0] = this.#block; - blocks[16] = blocks[1] = blocks[2] = blocks[3] = blocks[4] = blocks[5] = blocks[6] = blocks[7] = blocks[8] = blocks[9] = blocks[10] = blocks[11] = blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0; - } - blocks[14] = (this.#hBytes << 3) | (this.#bytes >>> 29); - blocks[15] = this.#bytes << 3; - this.hash(); - } - - protected hash(): void { - let a = this.#h0; - let b = this.#h1; - let c = this.#h2; - let d = this.#h3; - let e = this.#h4; - let f = this.#h5; - let g = this.#h6; - let h = this.#h7; - const blocks = this.#blocks; - let s0: number; - let s1: number; - let maj: number; - let t1: number; - let t2: number; - let ch: number; - let ab: number; - let da: number; - let cd: number; - let bc: number; - - for (let j = 16; j < 64; ++j) { - // rightrotate - t1 = blocks[j - 15]; - s0 = ((t1 >>> 7) | (t1 << 25)) ^ ((t1 >>> 18) | (t1 << 14)) ^ (t1 >>> 3); - t1 = blocks[j - 2]; - s1 = - ((t1 >>> 17) | (t1 << 15)) ^ ((t1 >>> 19) | (t1 << 13)) ^ (t1 >>> 10); - blocks[j] = (blocks[j - 16] + s0 + blocks[j - 7] + s1) << 0; - } - - bc = b & c; - for (let j = 0; j < 64; j += 4) { - if (this.#first) { - if (this.#is224) { - ab = 300032; - t1 = blocks[0] - 1413257819; - h = (t1 - 150054599) << 0; - d = (t1 + 24177077) << 0; - } else { - ab = 704751109; - t1 = blocks[0] - 210244248; - h = (t1 - 1521486534) << 0; - d = (t1 + 143694565) << 0; - } - this.#first = false; - } else { - s0 = - ((a >>> 2) | (a << 30)) ^ - ((a >>> 13) | (a << 19)) ^ - ((a >>> 22) | (a << 10)); - s1 = - ((e >>> 6) | (e << 26)) ^ - ((e >>> 11) | (e << 21)) ^ - ((e >>> 25) | (e << 7)); - ab = a & b; - maj = ab ^ (a & c) ^ bc; - ch = (e & f) ^ (~e & g); - t1 = h + s1 + ch + K[j] + blocks[j]; - t2 = s0 + maj; - h = (d + t1) << 0; - d = (t1 + t2) << 0; - } - s0 = - ((d >>> 2) | (d << 30)) ^ - ((d >>> 13) | (d << 19)) ^ - ((d >>> 22) | (d << 10)); - s1 = - ((h >>> 6) | (h << 26)) ^ - ((h >>> 11) | (h << 21)) ^ - ((h >>> 25) | (h << 7)); - da = d & a; - maj = da ^ (d & b) ^ ab; - ch = (h & e) ^ (~h & f); - t1 = g + s1 + ch + K[j + 1] + blocks[j + 1]; - t2 = s0 + maj; - g = (c + t1) << 0; - c = (t1 + t2) << 0; - s0 = - ((c >>> 2) | (c << 30)) ^ - ((c >>> 13) | (c << 19)) ^ - ((c >>> 22) | (c << 10)); - s1 = - ((g >>> 6) | (g << 26)) ^ - ((g >>> 11) | (g << 21)) ^ - ((g >>> 25) | (g << 7)); - cd = c & d; - maj = cd ^ (c & a) ^ da; - ch = (g & h) ^ (~g & e); - t1 = f + s1 + ch + K[j + 2] + blocks[j + 2]; - t2 = s0 + maj; - f = (b + t1) << 0; - b = (t1 + t2) << 0; - s0 = - ((b >>> 2) | (b << 30)) ^ - ((b >>> 13) | (b << 19)) ^ - ((b >>> 22) | (b << 10)); - s1 = - ((f >>> 6) | (f << 26)) ^ - ((f >>> 11) | (f << 21)) ^ - ((f >>> 25) | (f << 7)); - bc = b & c; - maj = bc ^ (b & d) ^ cd; - ch = (f & g) ^ (~f & h); - t1 = e + s1 + ch + K[j + 3] + blocks[j + 3]; - t2 = s0 + maj; - e = (a + t1) << 0; - a = (t1 + t2) << 0; - } - - this.#h0 = (this.#h0 + a) << 0; - this.#h1 = (this.#h1 + b) << 0; - this.#h2 = (this.#h2 + c) << 0; - this.#h3 = (this.#h3 + d) << 0; - this.#h4 = (this.#h4 + e) << 0; - this.#h5 = (this.#h5 + f) << 0; - this.#h6 = (this.#h6 + g) << 0; - this.#h7 = (this.#h7 + h) << 0; - } - - /** Return hash in hex string. */ - hex(): string { - this.finalize(); - - const h0 = this.#h0; - const h1 = this.#h1; - const h2 = this.#h2; - const h3 = this.#h3; - const h4 = this.#h4; - const h5 = this.#h5; - const h6 = this.#h6; - const h7 = this.#h7; - - let hex = - HEX_CHARS[(h0 >> 28) & 0x0f] + - HEX_CHARS[(h0 >> 24) & 0x0f] + - HEX_CHARS[(h0 >> 20) & 0x0f] + - HEX_CHARS[(h0 >> 16) & 0x0f] + - HEX_CHARS[(h0 >> 12) & 0x0f] + - HEX_CHARS[(h0 >> 8) & 0x0f] + - HEX_CHARS[(h0 >> 4) & 0x0f] + - HEX_CHARS[h0 & 0x0f] + - HEX_CHARS[(h1 >> 28) & 0x0f] + - HEX_CHARS[(h1 >> 24) & 0x0f] + - HEX_CHARS[(h1 >> 20) & 0x0f] + - HEX_CHARS[(h1 >> 16) & 0x0f] + - HEX_CHARS[(h1 >> 12) & 0x0f] + - HEX_CHARS[(h1 >> 8) & 0x0f] + - HEX_CHARS[(h1 >> 4) & 0x0f] + - HEX_CHARS[h1 & 0x0f] + - HEX_CHARS[(h2 >> 28) & 0x0f] + - HEX_CHARS[(h2 >> 24) & 0x0f] + - HEX_CHARS[(h2 >> 20) & 0x0f] + - HEX_CHARS[(h2 >> 16) & 0x0f] + - HEX_CHARS[(h2 >> 12) & 0x0f] + - HEX_CHARS[(h2 >> 8) & 0x0f] + - HEX_CHARS[(h2 >> 4) & 0x0f] + - HEX_CHARS[h2 & 0x0f] + - HEX_CHARS[(h3 >> 28) & 0x0f] + - HEX_CHARS[(h3 >> 24) & 0x0f] + - HEX_CHARS[(h3 >> 20) & 0x0f] + - HEX_CHARS[(h3 >> 16) & 0x0f] + - HEX_CHARS[(h3 >> 12) & 0x0f] + - HEX_CHARS[(h3 >> 8) & 0x0f] + - HEX_CHARS[(h3 >> 4) & 0x0f] + - HEX_CHARS[h3 & 0x0f] + - HEX_CHARS[(h4 >> 28) & 0x0f] + - HEX_CHARS[(h4 >> 24) & 0x0f] + - HEX_CHARS[(h4 >> 20) & 0x0f] + - HEX_CHARS[(h4 >> 16) & 0x0f] + - HEX_CHARS[(h4 >> 12) & 0x0f] + - HEX_CHARS[(h4 >> 8) & 0x0f] + - HEX_CHARS[(h4 >> 4) & 0x0f] + - HEX_CHARS[h4 & 0x0f] + - HEX_CHARS[(h5 >> 28) & 0x0f] + - HEX_CHARS[(h5 >> 24) & 0x0f] + - HEX_CHARS[(h5 >> 20) & 0x0f] + - HEX_CHARS[(h5 >> 16) & 0x0f] + - HEX_CHARS[(h5 >> 12) & 0x0f] + - HEX_CHARS[(h5 >> 8) & 0x0f] + - HEX_CHARS[(h5 >> 4) & 0x0f] + - HEX_CHARS[h5 & 0x0f] + - HEX_CHARS[(h6 >> 28) & 0x0f] + - HEX_CHARS[(h6 >> 24) & 0x0f] + - HEX_CHARS[(h6 >> 20) & 0x0f] + - HEX_CHARS[(h6 >> 16) & 0x0f] + - HEX_CHARS[(h6 >> 12) & 0x0f] + - HEX_CHARS[(h6 >> 8) & 0x0f] + - HEX_CHARS[(h6 >> 4) & 0x0f] + - HEX_CHARS[h6 & 0x0f]; - if (!this.#is224) { - hex += - HEX_CHARS[(h7 >> 28) & 0x0f] + - HEX_CHARS[(h7 >> 24) & 0x0f] + - HEX_CHARS[(h7 >> 20) & 0x0f] + - HEX_CHARS[(h7 >> 16) & 0x0f] + - HEX_CHARS[(h7 >> 12) & 0x0f] + - HEX_CHARS[(h7 >> 8) & 0x0f] + - HEX_CHARS[(h7 >> 4) & 0x0f] + - HEX_CHARS[h7 & 0x0f]; - } - return hex; - } - - /** Return hash in hex string. */ - toString(): string { - return this.hex(); - } - - /** Return hash in integer array. */ - digest(): number[] { - this.finalize(); - - const h0 = this.#h0; - const h1 = this.#h1; - const h2 = this.#h2; - const h3 = this.#h3; - const h4 = this.#h4; - const h5 = this.#h5; - const h6 = this.#h6; - const h7 = this.#h7; - - const arr = [ - (h0 >> 24) & 0xff, - (h0 >> 16) & 0xff, - (h0 >> 8) & 0xff, - h0 & 0xff, - (h1 >> 24) & 0xff, - (h1 >> 16) & 0xff, - (h1 >> 8) & 0xff, - h1 & 0xff, - (h2 >> 24) & 0xff, - (h2 >> 16) & 0xff, - (h2 >> 8) & 0xff, - h2 & 0xff, - (h3 >> 24) & 0xff, - (h3 >> 16) & 0xff, - (h3 >> 8) & 0xff, - h3 & 0xff, - (h4 >> 24) & 0xff, - (h4 >> 16) & 0xff, - (h4 >> 8) & 0xff, - h4 & 0xff, - (h5 >> 24) & 0xff, - (h5 >> 16) & 0xff, - (h5 >> 8) & 0xff, - h5 & 0xff, - (h6 >> 24) & 0xff, - (h6 >> 16) & 0xff, - (h6 >> 8) & 0xff, - h6 & 0xff, - ]; - if (!this.#is224) { - arr.push( - (h7 >> 24) & 0xff, - (h7 >> 16) & 0xff, - (h7 >> 8) & 0xff, - h7 & 0xff - ); - } - return arr; - } - - /** Return hash in integer array. */ - array(): number[] { - return this.digest(); - } - - /** Return hash in ArrayBuffer. */ - arrayBuffer(): ArrayBuffer { - this.finalize(); - - const buffer = new ArrayBuffer(this.#is224 ? 28 : 32); - const dataView = new DataView(buffer); - dataView.setUint32(0, this.#h0); - dataView.setUint32(4, this.#h1); - dataView.setUint32(8, this.#h2); - dataView.setUint32(12, this.#h3); - dataView.setUint32(16, this.#h4); - dataView.setUint32(20, this.#h5); - dataView.setUint32(24, this.#h6); - if (!this.#is224) { - dataView.setUint32(28, this.#h7); - } - return buffer; - } -} - -export class HmacSha256 extends Sha256 { - #inner: boolean; - #is224: boolean; - #oKeyPad: number[]; - #sharedMemory: boolean; - - constructor(secretKey: Message, is224 = false, sharedMemory = false) { - super(is224, sharedMemory); - - let key: number[] | Uint8Array | undefined; - if (typeof secretKey === "string") { - const bytes: number[] = []; - const length = secretKey.length; - let index = 0; - for (let i = 0; i < length; ++i) { - let code = secretKey.charCodeAt(i); - if (code < 0x80) { - bytes[index++] = code; - } else if (code < 0x800) { - bytes[index++] = 0xc0 | (code >> 6); - bytes[index++] = 0x80 | (code & 0x3f); - } else if (code < 0xd800 || code >= 0xe000) { - bytes[index++] = 0xe0 | (code >> 12); - bytes[index++] = 0x80 | ((code >> 6) & 0x3f); - bytes[index++] = 0x80 | (code & 0x3f); - } else { - code = - 0x10000 + - (((code & 0x3ff) << 10) | (secretKey.charCodeAt(++i) & 0x3ff)); - bytes[index++] = 0xf0 | (code >> 18); - bytes[index++] = 0x80 | ((code >> 12) & 0x3f); - bytes[index++] = 0x80 | ((code >> 6) & 0x3f); - bytes[index++] = 0x80 | (code & 0x3f); - } - } - key = bytes; - } else { - if (typeof secretKey === "object") { - if (secretKey === null) { - throw new Error(ERROR); - } else if (secretKey instanceof ArrayBuffer) { - key = new Uint8Array(secretKey); - } else if (!Array.isArray(secretKey)) { - if (!ArrayBuffer.isView(secretKey)) { - throw new Error(ERROR); - } - } - } else { - throw new Error(ERROR); - } - } - if (key === undefined) { - key = secretKey as number[] | Uint8Array; - } - - if (key.length > 64) { - key = new Sha256(is224, true).update(key).array(); - } - - const oKeyPad: number[] = []; - const iKeyPad: number[] = []; - for (let i = 0; i < 64; ++i) { - const b = key[i] || 0; - oKeyPad[i] = 0x5c ^ b; - iKeyPad[i] = 0x36 ^ b; - } - - this.update(iKeyPad); - this.#oKeyPad = oKeyPad; - this.#inner = true; - this.#is224 = is224; - this.#sharedMemory = sharedMemory; - } - - protected finalize(): void { - super.finalize(); - if (this.#inner) { - this.#inner = false; - const innerHash = this.array(); - super.init(this.#is224, this.#sharedMemory); - this.update(this.#oKeyPad); - this.update(innerHash); - super.finalize(); - } - } -} diff --git a/std/util/sha256_test.ts b/std/util/sha256_test.ts deleted file mode 100644 index a38786943..000000000 --- a/std/util/sha256_test.ts +++ /dev/null @@ -1,296 +0,0 @@ -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { Sha256, HmacSha256, Message } from "./sha256.ts"; -import { assertEquals } from "../testing/asserts.ts"; - -const { test } = Deno; - -/** Handy function to convert an array/array buffer to a string of hex values. */ -function toHexString(value: number[] | ArrayBuffer): string { - const array = new Uint8Array(value); - let hex = ""; - for (const v of array) { - const c = v.toString(16); - hex += c.length === 1 ? `0${c}` : c; - } - return hex; -} - -// prettier-ignore -// dprint-ignore -const fixtures: { - sha256: Record>; - sha224: Record>; - sha256Hmac: Record>; - sha224Hmac: Record>; -} = { - sha256: { - "ascii": { - "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855": "", - "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592": "The quick brown fox jumps over the lazy dog", - "ef537f25c895bfa782526529a9b63d97aa631564d5d789c2b765448c8635fb6c": "The quick brown fox jumps over the lazy dog." - }, - "ascii more than 64 bytes": { - "54e73d89e1924fdcd056390266a983924b6d6d461e9470b6cd50bbaf69b5c54c": "The MD5 message-digest algorithm is a widely used cryptographic hash function producing a 128-bit (16-byte) hash value, typically expressed in text format as a 32 digit hexadecimal number. MD5 has been utilized in a wide variety of cryptographic applications, and is also commonly used to verify data integrity." - }, - "UTF8": { - "72726d8818f693066ceb69afa364218b692e62ea92b385782363780f47529c21": "中文", - "53196d1acfce0c4b264e01e8018c989d571351f59e33f055f76ff15b4f0516c6": "aécio", - "8d10a48685dbc34484696de7ea7434d80a54c1d60100530faccf697463ef19c9": "𠜎" - }, - "UTF8 more than 64 bytes": { - "d691014feebf35b3500ef6f6738d0094cac63628a7a018a980a40292a77703d1": "訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一", - "81a1472ebdeb09406a783d607ff49ee2fde3e9f44ac1cd158ad8d6ad3c4e69fa": "訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一(又譯雜湊演算法、摘要演算法等),主流程式語言普遍已有MD5的實作。" - }, - "special length": { - "5e6b963e2b6444dab8544beab8532850cef2a9d143872a6a5384abe37e61b3db": "0123456780123456780123456780123456780123456780123456780", - "85d240a4a03a0710423fc4f701da51e8785c9eaa96d718ab1c7991d6afd60d62": "01234567801234567801234567801234567801234567801234567801", - "c3ee464d5620eb2dde3dfda4c7955dbd9e9e2e9b113c13983fc67b0dfd892a53": "0123456780123456780123456780123456780123456780123456780123456780", - "74b51c6911f9a8b5e7c499effe7604e43b672166818873c27752c248de434841": "01234567801234567801234567801234567801234567801234567801234567801234567", - "6fba9e623ae6abf028a1b195748814aa95eebfb22e3ec5e15d2444cd6c48186a": "012345678012345678012345678012345678012345678012345678012345678012345678" - }, - "Array": { - "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855": [], - "182889f925ae4e5cc37118ded6ed87f7bdc7cab5ec5e78faef2e50048999473f": [211, 212], - "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592": [84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103], - "74b51c6911f9a8b5e7c499effe7604e43b672166818873c27752c248de434841": [48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55] - } - }, - sha224: { - "ascii": { - "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f": "", - "730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525": "The quick brown fox jumps over the lazy dog", - "619cba8e8e05826e9b8c519c0a5c68f4fb653e8a3d8aa04bb2c8cd4c": "The quick brown fox jumps over the lazy dog." - }, - "ascii more than 64 bytes": { - "4d97e15967391d2e846ea7d21bb480efadbae5868b731e7cc6267006": "The MD5 message-digest algorithm is a widely used cryptographic hash function producing a 128-bit (16-byte) hash value, typically expressed in text format as a 32 digit hexadecimal number. MD5 has been utilized in a wide variety of cryptographic applications, and is also commonly used to verify data integrity." - }, - "UTF8": { - "dfbab71afdf54388af4d55f8bd3de8c9b15e0eb916bf9125f4a959d4": "中文", - "d12841cafd89c534924a839e62bf35a2b5f3717b7802eb19bd8d8e15": "aécio", - "eaa0129b5509f5701db218fb7076b282e4409da52d06363aa3bdd63d": "𠜎" - }, - "UTF8 more than 64 bytes": { - "0dda421f3f81272418e1313673e9d74b7f2d04efc9c52c69458e12c3": "訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一", - "a8cb74a54e6dc6ab6110db3915ba08ffe5e1abafaea78538fa12a626": "訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一(又譯雜湊演算法、摘要演算法等),主流程式語言普遍已有MD5的實作。" - }, - "special length": { - "bc4a354d66f3cff4bc6dd6a88fbb0435cede7fd5fe94da0760cb1924": "0123456780123456780123456780123456780123456780123456780", - "2f148f757d1295784a7c69bf328b8bf827a536669e132234cd6f50e7": "01234567801234567801234567801234567801234567801234567801", - "496275a96bf41aa27ce89c3ae0fc63c3a3eab063887a8ea075bd091b": "0123456780123456780123456780123456780123456780123456780123456780", - "16ee1b101fe0e0d8dd156d598931ec19d75b0f8dc0a0455733c168c8": "01234567801234567801234567801234567801234567801234567801234567801234567", - "04c7a30079c640e440d884cdf0d7ab04fd05501d4498cb21be29ca1f": "012345678012345678012345678012345678012345678012345678012345678012345678" - }, - "Array": { - "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f": [], - "730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525": [84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103], - "16ee1b101fe0e0d8dd156d598931ec19d75b0f8dc0a0455733c168c8": [48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55, 56, 48, 49, 50, 51, 52, 53, 54, 55] - } - }, - sha256Hmac: { - "Test Vectors": { - "b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7": [ - [0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b], - "Hi There" - ], - "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843": [ - "Jefe", - "what do ya want for nothing?" - ], - "773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe": [ - [0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa], - [0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd] - ], - "82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff46729665b": [ - [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19], - [0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd] - ], - "60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54": [ - [0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa], - "Test Using Larger Than Block-Size Key - Hash Key First" - ], - "9b09ffa71b942fcb27635fbcd5b0e944bfdc63644f0713938a7f51535c3a35e2": [ - [0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa], - "This is a test using a larger than block-size key and a larger than block-size data. The key needs to be hashed before being used by the HMAC algorithm." - ] - }, - "UTF8": { - "865cc329d317f6d9fdbd183a3c5cc5fd4c370d11f98abbbb404bceb1e6392c7e": ["中文", "中文"], - "efeef87be5731506b69bb64a9898a456dd12c94834c36a4d8ba99e3db79ad7ed": ["aécio", "aécio"], - "8a6e527049b9cfc7e1c84bcf356a1289c95da68a586c03de3327f3de0d3737fe": ["𠜎", "𠜎"] - } - }, - sha224Hmac: { - "Test Vectors": { - "896fb1128abbdf196832107cd49df33f47b4b1169912ba4f53684b22": [ - [0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b], - "Hi There" - ], - "a30e01098bc6dbbf45690f3a7e9e6d0f8bbea2a39e6148008fd05e44": [ - "Jefe", - "what do ya want for nothing?" - ], - "7fb3cb3588c6c1f6ffa9694d7d6ad2649365b0c1f65d69d1ec8333ea": [ - [0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa], - [0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd] - ], - "6c11506874013cac6a2abc1bb382627cec6a90d86efc012de7afec5a": [ - [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19], - [0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd] - ], - "95e9a0db962095adaebe9b2d6f0dbce2d499f112f2d2b7273fa6870e": [ - [0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa], - "Test Using Larger Than Block-Size Key - Hash Key First" - ], - "3a854166ac5d9f023f54d517d0b39dbd946770db9c2b95c9f6f565d1": [ - [0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa], - "This is a test using a larger than block-size key and a larger than block-size data. The key needs to be hashed before being used by the HMAC algorithm." - ] - }, - "UTF8": { - "e2280928fe813aeb7fa59aa14dd5e589041bfdf91945d19d25b9f3db": ["中文", "中文"], - "86c53dc054b16f6e006a254891bc9ff0da5df8e1a6faee3b0aaa732d": ["aécio", "aécio"], - "e9e5991bfb84506b105f800afac1599ff807bb8e20db8ffda48997b9": ["𠜎", "𠜎"] - } - }, -}; - -// prettier-ignore -// dprint-ignore -fixtures.sha256.Uint8Array = { - '182889f925ae4e5cc37118ded6ed87f7bdc7cab5ec5e78faef2e50048999473f': new Uint8Array([211, 212]), - 'd7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592': new Uint8Array([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103]) -}; -// prettier-ignore -// dprint-ignore -fixtures.sha256.Int8Array = { - 'd7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592': new Int8Array([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103]) -}; -// prettier-ignore -// dprint-ignore -fixtures.sha256.ArrayBuffer = { - 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855': new ArrayBuffer(0), - '6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d': new ArrayBuffer(1) -}; -// prettier-ignore -// dprint-ignore -fixtures.sha224.Uint8Array = { - 'e17541396a3ecd1cd5a2b968b84e597e8eae3b0ea3127963bf48dd3b': new Uint8Array([211, 212]), - '730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525': new Uint8Array([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103]) -}; -// prettier-ignore -// dprint-ignore -fixtures.sha224.Int8Array = { - '730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525': new Int8Array([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103]) -}; -// prettier-ignore -// dprint-ignore -fixtures.sha224.ArrayBuffer = { - 'd14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f': new ArrayBuffer(0), - 'fff9292b4201617bdc4d3053fce02734166a683d7d858a7f5f59b073': new ArrayBuffer(1), -}; -fixtures.sha256Hmac.Uint8Array = { - e48411262715c8370cd5e7bf8e82bef53bd53712d007f3429351843b77c7bb9b: [ - new Uint8Array(0), - "Hi There", - ], -}; -fixtures.sha256Hmac.ArrayBuffer = { - e48411262715c8370cd5e7bf8e82bef53bd53712d007f3429351843b77c7bb9b: [ - new ArrayBuffer(0), - "Hi There", - ], -}; -fixtures.sha224Hmac.Uint8Array = { - da8f94de91d62154b55ea4e8d6eb133f6d553bcd1f1ba205b9488945: [ - new Uint8Array(0), - "Hi There", - ], -}; -fixtures.sha224Hmac.ArrayBuffer = { - da8f94de91d62154b55ea4e8d6eb133f6d553bcd1f1ba205b9488945: [ - new ArrayBuffer(0), - "Hi There", - ], -}; - -const methods = ["array", "arrayBuffer", "digest", "hex"] as const; - -for (const method of methods) { - for (const [name, tests] of Object.entries(fixtures.sha256)) { - let i = 1; - for (const [expected, message] of Object.entries(tests)) { - test({ - name: `sha256.${method}() - ${name} - #${i++}`, - fn() { - const algorithm = new Sha256(); - algorithm.update(message); - const actual = - method === "hex" - ? algorithm[method]() - : toHexString(algorithm[method]()); - assertEquals(actual, expected); - }, - }); - } - } -} - -for (const method of methods) { - for (const [name, tests] of Object.entries(fixtures.sha224)) { - let i = 1; - for (const [expected, message] of Object.entries(tests)) { - test({ - name: `sha224.${method}() - ${name} - #${i++}`, - fn() { - const algorithm = new Sha256(true); - algorithm.update(message); - const actual = - method === "hex" - ? algorithm[method]() - : toHexString(algorithm[method]()); - assertEquals(actual, expected); - }, - }); - } - } -} - -for (const method of methods) { - for (const [name, tests] of Object.entries(fixtures.sha256Hmac)) { - let i = 1; - for (const [expected, [key, message]] of Object.entries(tests)) { - test({ - name: `hmacSha256.${method}() - ${name} - #${i++}`, - fn() { - const algorithm = new HmacSha256(key); - algorithm.update(message); - const actual = - method === "hex" - ? algorithm[method]() - : toHexString(algorithm[method]()); - assertEquals(actual, expected); - }, - }); - } - } -} - -for (const method of methods) { - for (const [name, tests] of Object.entries(fixtures.sha224Hmac)) { - let i = 1; - for (const [expected, [key, message]] of Object.entries(tests)) { - test({ - name: `hmacSha224.${method}() - ${name} - #${i++}`, - fn() { - const algorithm = new HmacSha256(key, true); - algorithm.update(message); - const actual = - method === "hex" - ? algorithm[method]() - : toHexString(algorithm[method]()); - assertEquals(actual, expected); - }, - }); - } - } -} diff --git a/std/uuid/v5.ts b/std/uuid/v5.ts index 1216762dd..3c04873fe 100644 --- a/std/uuid/v5.ts +++ b/std/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/std/ws/example_client.ts b/std/ws/example_client.ts index 4213025f4..93f2f5c7b 100644 --- a/std/ws/example_client.ts +++ b/std/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); + 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); + await Promise.race([messages(), cli()]).catch(console.error); - if (!sock.isClosed) { - await sock.close(1000).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/std/ws/example_server.ts b/std/ws/example_server.ts index 947b807ca..3a9815957 100644 --- a/std/ws/example_server.ts +++ b/std/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; +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 { - const sock = await acceptWebSocket({ - conn, - bufReader, - bufWriter, - headers, - }); + try { + const sock = await acceptWebSocket({ + conn, + bufReader, + bufWriter, + headers, + }); - console.log("socket connected!"); + 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); + 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}`); + } catch (err) { + console.error(`failed to receive frame: ${err}`); - if (!sock.isClosed) { - await sock.close(1000).catch(console.error); + if (!sock.isClosed) { + await sock.close(1000).catch(console.error); + } } + } catch (err) { + 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/std/ws/mod.ts b/std/ws/mod.ts index 47353d012..324588af0 100644 --- a/std/ws/mod.ts +++ b/std/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/std/ws/test.ts b/std/ws/test.ts index 8e97e6ec2..a1c396b18 100644 --- a/std/ws/test.ts +++ b/std/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" -- cgit v1.2.3