mirror of
https://github.com/swc-project/swc.git
synced 2024-12-20 20:22:26 +03:00
246bdd5088
swc_bundler: - Ensure that denoland/deno#10141 is fixed. - Run deno tests on ci. - Support nested `export *`. (denoland/deno#10153, denoland/deno#10174) swc_ecma_codegen: - Remove `,` after rest elements. (#1573, denoland/deno#10167) swc_ecma_transforms_optimization: - Don't drop items used by the discriminant of a switch. swc_ecma_transforms_typescript: - Remove constructors without a body.
64 lines
1.7 KiB
TypeScript
64 lines
1.7 KiB
TypeScript
// Loaded from https://deno.land/std@0.89.0/io/readers.ts
|
|
|
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
|
// Based on https://github.com/golang/go/blob/0452f9460f50f0f0aba18df43dc2b31906fb66cc/src/io/io.go
|
|
// 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.
|
|
|
|
/** Reader utility for strings */
|
|
export class StringReader extends Deno.Buffer {
|
|
constructor(s: string) {
|
|
super(new TextEncoder().encode(s).buffer);
|
|
}
|
|
}
|
|
|
|
/** Reader utility for combining multiple readers */
|
|
export class MultiReader implements Deno.Reader {
|
|
private readonly readers: Deno.Reader[];
|
|
private currentIndex = 0;
|
|
|
|
constructor(...readers: Deno.Reader[]) {
|
|
this.readers = readers;
|
|
}
|
|
|
|
async read(p: Uint8Array): Promise<number | null> {
|
|
const r = this.readers[this.currentIndex];
|
|
if (!r) return null;
|
|
const result = await r.read(p);
|
|
if (result === null) {
|
|
this.currentIndex++;
|
|
return 0;
|
|
}
|
|
return result;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* A `LimitedReader` reads from `reader` but limits the amount of data returned to just `limit` bytes.
|
|
* Each call to `read` updates `limit` to reflect the new amount remaining.
|
|
* `read` returns `null` when `limit` <= `0` or
|
|
* when the underlying `reader` returns `null`.
|
|
*/
|
|
export class LimitedReader implements Deno.Reader {
|
|
constructor(public reader: Deno.Reader, public limit: number) {}
|
|
|
|
async read(p: Uint8Array): Promise<number | null> {
|
|
if (this.limit <= 0) {
|
|
return null;
|
|
}
|
|
|
|
if (p.length > this.limit) {
|
|
p = p.subarray(0, this.limit);
|
|
}
|
|
const n = await this.reader.read(p);
|
|
if (n == null) {
|
|
return null;
|
|
}
|
|
|
|
this.limit -= n;
|
|
return n;
|
|
}
|
|
}
|