fix: bypass TS errors for missing --unstable (#1819)

Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
This commit is contained in:
Nayeem Rahman 2022-02-01 10:16:37 +00:00 committed by GitHub
parent c331dad1df
commit 06b1791664
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 221 additions and 36 deletions

2
.gitignore vendored
View File

@ -7,3 +7,5 @@ package-lock.json
.vscode/settings.json
**/cov/
/_wasm_crypto/target
# TODO(nayeemrmn): Remove (https://github.com/denoland/deno_std/pull/1819#issuecomment-1011136991).
/_deno_unstable_checked.ts

143
_deno_unstable.ts Normal file
View File

@ -0,0 +1,143 @@
// @ts-nocheck Bypass static errors for missing --unstable.
export type HttpClient = Deno.HttpClient;
export function addSignalListener(
...args: Parameters<typeof Deno.addSignalListener>
): ReturnType<typeof Deno.addSignalListener> {
if (typeof Deno.addSignalListener == "function") {
return Deno.addSignalListener(...args);
} else {
throw new TypeError("Requires --unstable");
}
}
export function createHttpClient(
...args: Parameters<typeof Deno.createHttpClient>
): ReturnType<typeof Deno.createHttpClient> {
if (typeof Deno.createHttpClient == "function") {
return Deno.createHttpClient(...args);
} else {
throw new TypeError("Requires --unstable");
}
}
export function consoleSize(
...args: Parameters<typeof Deno.consoleSize>
): ReturnType<typeof Deno.consoleSize> {
if (typeof Deno.consoleSize == "function") {
return Deno.consoleSize(...args);
} else {
throw new TypeError("Requires --unstable");
}
}
export function futime(
...args: Parameters<typeof Deno.futime>
): ReturnType<typeof Deno.futime> {
if (typeof Deno.futime == "function") {
return Deno.futime(...args);
} else {
return Promise.reject(new TypeError("Requires --unstable"));
}
}
export function futimeSync(
...args: Parameters<typeof Deno.futimeSync>
): ReturnType<typeof Deno.futimeSync> {
if (typeof Deno.futimeSync == "function") {
return Deno.futimeSync(...args);
} else {
throw new TypeError("Requires --unstable");
}
}
export function hostname(
...args: Parameters<typeof Deno.hostname>
): ReturnType<typeof Deno.hostname> {
if (typeof Deno.hostname == "function") {
return Deno.hostname(...args);
} else {
throw new TypeError("Requires --unstable");
}
}
export function loadavg(
...args: Parameters<typeof Deno.loadavg>
): ReturnType<typeof Deno.loadavg> {
if (typeof Deno.loadavg == "function") {
return Deno.loadavg(...args);
} else {
throw new TypeError("Requires --unstable");
}
}
export function osRelease(
...args: Parameters<typeof Deno.osRelease>
): ReturnType<typeof Deno.osRelease> {
if (typeof Deno.osRelease == "function") {
return Deno.osRelease(...args);
} else {
throw new TypeError("Requires --unstable");
}
}
export function removeSignalListener(
...args: Parameters<typeof Deno.removeSignalListener>
): ReturnType<typeof Deno.removeSignalListener> {
if (typeof Deno.removeSignalListener == "function") {
return Deno.removeSignalListener(...args);
} else {
throw new TypeError("Requires --unstable");
}
}
export function setRaw(
...args: Parameters<typeof Deno.setRaw>
): ReturnType<typeof Deno.setRaw> {
if (typeof Deno.setRaw == "function") {
return Deno.setRaw(...args);
} else {
throw new TypeError("Requires --unstable");
}
}
export function systemMemoryInfo(
...args: Parameters<typeof Deno.systemMemoryInfo>
): ReturnType<typeof Deno.systemMemoryInfo> {
if (typeof Deno.systemMemoryInfo == "function") {
return Deno.systemMemoryInfo(...args);
} else {
throw new TypeError("Requires --unstable");
}
}
export function umask(
...args: Parameters<typeof Deno.umask>
): ReturnType<typeof Deno.umask> {
if (typeof Deno.umask == "function") {
return Deno.umask(...args);
} else {
throw new TypeError("Requires --unstable");
}
}
export function utime(
...args: Parameters<typeof Deno.utime>
): ReturnType<typeof Deno.utime> {
if (typeof Deno.utime == "function") {
return Deno.utime(...args);
} else {
return Promise.reject(new TypeError("Requires --unstable"));
}
}
export function utimeSync(
...args: Parameters<typeof Deno.utimeSync>
): ReturnType<typeof Deno.utimeSync> {
if (typeof Deno.utimeSync == "function") {
return Deno.utimeSync(...args);
} else {
throw new TypeError("Requires --unstable");
}
}

32
_deno_unstable_test.ts Normal file
View File

@ -0,0 +1,32 @@
import { assertEquals } from "./testing/asserts.ts";
Deno.test({
name: "_deno_unstable.ts complies with --unstable if type checked",
async fn() {
const denoUnstableUrl = new URL("_deno_unstable.ts", import.meta.url);
const denoUnstableCheckedUrl = new URL(
"_deno_unstable_checked.ts",
import.meta.url,
);
const code = await Deno.readTextFile(denoUnstableUrl);
const checkedCode = `// AUTOGENERATED\n${code.replace("@ts-nocheck ", "")}`;
try {
await Deno.writeTextFile(denoUnstableCheckedUrl, checkedCode);
const process = Deno.run({
cmd: [
Deno.execPath(),
"run",
"--quiet",
"--unstable",
denoUnstableCheckedUrl.href,
],
});
const status = await process.status();
process.close();
assertEquals(status.code, 0);
} finally {
// TODO(nayeemrmn): Uncomment (https://github.com/denoland/deno_std/pull/1819#issuecomment-1011136991).
// await Deno.remove(denoUnstableCheckedUrl, {}).catch(() => {});
}
},
});

View File

@ -1,4 +1,5 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import * as DenoUnstable from "../_deno_unstable.ts";
import * as path from "../path/mod.ts";
import { ensureDir, ensureDirSync } from "./ensure_dir.ts";
import { getFileInfoType, isSubdir } from "./_util.ts";
@ -93,7 +94,7 @@ async function copyFile(
const statInfo = await Deno.stat(src);
assert(statInfo.atime instanceof Date, `statInfo.atime is unavailable`);
assert(statInfo.mtime instanceof Date, `statInfo.mtime is unavailable`);
await Deno.utime(dest, statInfo.atime, statInfo.mtime);
await DenoUnstable.utime(dest, statInfo.atime, statInfo.mtime);
}
}
/* copy file to dest synchronously */
@ -108,7 +109,7 @@ function copyFileSync(
const statInfo = Deno.statSync(src);
assert(statInfo.atime instanceof Date, `statInfo.atime is unavailable`);
assert(statInfo.mtime instanceof Date, `statInfo.mtime is unavailable`);
Deno.utimeSync(dest, statInfo.atime, statInfo.mtime);
DenoUnstable.utimeSync(dest, statInfo.atime, statInfo.mtime);
}
}
@ -132,7 +133,7 @@ async function copySymLink(
const statInfo = await Deno.lstat(src);
assert(statInfo.atime instanceof Date, `statInfo.atime is unavailable`);
assert(statInfo.mtime instanceof Date, `statInfo.mtime is unavailable`);
await Deno.utime(dest, statInfo.atime, statInfo.mtime);
await DenoUnstable.utime(dest, statInfo.atime, statInfo.mtime);
}
}
@ -157,7 +158,7 @@ function copySymlinkSync(
const statInfo = Deno.lstatSync(src);
assert(statInfo.atime instanceof Date, `statInfo.atime is unavailable`);
assert(statInfo.mtime instanceof Date, `statInfo.mtime is unavailable`);
Deno.utimeSync(dest, statInfo.atime, statInfo.mtime);
DenoUnstable.utimeSync(dest, statInfo.atime, statInfo.mtime);
}
}
@ -180,7 +181,7 @@ async function copyDir(
const srcStatInfo = await Deno.stat(src);
assert(srcStatInfo.atime instanceof Date, `statInfo.atime is unavailable`);
assert(srcStatInfo.mtime instanceof Date, `statInfo.mtime is unavailable`);
await Deno.utime(dest, srcStatInfo.atime, srcStatInfo.mtime);
await DenoUnstable.utime(dest, srcStatInfo.atime, srcStatInfo.mtime);
}
for await (const entry of Deno.readDir(src)) {
@ -211,7 +212,7 @@ function copyDirSync(src: string, dest: string, options: CopyOptions): void {
const srcStatInfo = Deno.statSync(src);
assert(srcStatInfo.atime instanceof Date, `statInfo.atime is unavailable`);
assert(srcStatInfo.mtime instanceof Date, `statInfo.mtime is unavailable`);
Deno.utimeSync(dest, srcStatInfo.atime, srcStatInfo.mtime);
DenoUnstable.utimeSync(dest, srcStatInfo.atime, srcStatInfo.mtime);
}
for (const entry of Deno.readDirSync(src)) {

View File

@ -7,8 +7,6 @@ export * from "./ensure_symlink.ts";
export * from "./exists.ts";
export * from "./expand_glob.ts";
export * from "./move.ts";
// TODO(ry) copy.ts depends on unstable Deno.utime API. For now exclude it.
// https://github.com/denoland/deno_std/issues/1539
// export * from "./copy.ts";
export * from "./copy.ts";
export * from "./walk.ts";
export * from "./eol.ts";

View File

@ -1,4 +1,5 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import * as DenoUnstable from "../../_deno_unstable.ts";
import type { CallbackWithError } from "./_fs_common.ts";
function getValidTime(
@ -34,7 +35,7 @@ export function futimes(
atime = getValidTime(atime, "atime");
mtime = getValidTime(mtime, "mtime");
Deno.futime(fd, atime, mtime).then(() => callback(null), callback);
DenoUnstable.futime(fd, atime, mtime).then(() => callback(null), callback);
}
export function futimesSync(
@ -45,5 +46,5 @@ export function futimesSync(
atime = getValidTime(atime, "atime");
mtime = getValidTime(mtime, "mtime");
Deno.futimeSync(fd, atime, mtime);
DenoUnstable.futimeSync(fd, atime, mtime);
}

View File

@ -1,4 +1,5 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import * as DenoUnstable from "../../_deno_unstable.ts";
import type { CallbackWithError } from "./_fs_common.ts";
import { fromFileUrl } from "../path.ts";
@ -37,7 +38,7 @@ export function utimes(
atime = getValidTime(atime, "atime");
mtime = getValidTime(mtime, "mtime");
Deno.utime(path, atime, mtime).then(() => callback(null), callback);
DenoUnstable.utime(path, atime, mtime).then(() => callback(null), callback);
}
export function utimesSync(
@ -49,5 +50,5 @@ export function utimesSync(
atime = getValidTime(atime, "atime");
mtime = getValidTime(mtime, "mtime");
Deno.utimeSync(path, atime, mtime);
DenoUnstable.utimeSync(path, atime, mtime);
}

View File

@ -1,5 +1,6 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
// Copyright Joyent, Inc. and Node.js contributors. All rights reserved. MIT license.
import * as DenoUnstable from "../../_deno_unstable.ts";
import { Buffer } from "../buffer.ts";
import { Readable, Writable } from "../stream.ts";
import { stdio } from "./stdio.js";
@ -107,7 +108,7 @@ Object.defineProperty(stdin, "isTTY", {
});
stdin._isRawMode = false;
stdin.setRawMode = (enable) => {
Deno.setRaw?.(Deno.stdin?.rid, enable);
DenoUnstable.setRaw?.(Deno.stdin?.rid, enable);
stdin._isRawMode = enable;
return stdin;
};

View File

@ -21,17 +21,18 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
// TODO(schwarzkopfb): change this when `Deno.consoleSize()` will be stable
interface DenoUnstable {
consoleSize?(rid: number): { columns: number };
}
function getConsoleWidth(): number {
return (Deno as DenoUnstable).consoleSize?.(Deno.stderr.rid).columns ?? 80;
}
import * as DenoUnstable from "../_deno_unstable.ts";
import { inspect } from "./util.ts";
import { stripColor as removeColors } from "../fmt/colors.ts";
function getConsoleWidth(): number {
try {
return DenoUnstable.consoleSize(Deno.stderr.rid).columns;
} catch {
return 80;
}
}
// TODO(schwarzkopfb): we should implement Node's concept of "primordials"
// Ref: https://github.com/denoland/deno/issues/6040#issuecomment-637305828
const MathMax = Math.max;

View File

@ -1,3 +1,4 @@
import * as DenoUnstable from "../_deno_unstable.ts";
import { core } from "./_core.ts";
import { _normalizeArgs, ListenOptions, Socket } from "./net.ts";
import { Buffer } from "./buffer.ts";
@ -142,7 +143,7 @@ class ClientRequest extends NodeWritable {
this.destroy();
}
_createCustomClient(): Promise<Deno.HttpClient | undefined> {
_createCustomClient(): Promise<DenoUnstable.HttpClient | undefined> {
return Promise.resolve(undefined);
}

View File

@ -1,6 +1,7 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
// Copyright Joyent and Node contributors. All rights reserved. MIT license.
import * as DenoUnstable from "../_deno_unstable.ts";
import { notImplemented } from "./_utils.ts";
import { urlToHttpOptions } from "./internal/url.ts";
import {
@ -58,12 +59,12 @@ export function get(...args: any[]) {
export const globalAgent = undefined;
/** HttpsClientRequest class loosely follows http.ClientRequest class API. */
class HttpsClientRequest extends ClientRequest {
async _createCustomClient(): Promise<Deno.HttpClient | undefined> {
async _createCustomClient(): Promise<DenoUnstable.HttpClient | undefined> {
if (caCerts === null) {
return undefined;
}
if (caCerts !== undefined) {
return Deno.createHttpClient({ caCerts });
return DenoUnstable.createHttpClient({ caCerts });
}
const status = await Deno.permissions.query({
name: "env",
@ -80,7 +81,7 @@ class HttpsClientRequest extends ClientRequest {
}
const caCert = await Deno.readTextFile(certFilename);
caCerts = [caCert];
return Deno.createHttpClient({ caCerts });
return DenoUnstable.createHttpClient({ caCerts });
}
_createSocket(): Socket {

View File

@ -18,6 +18,7 @@
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
import * as DenoUnstable from "../_deno_unstable.ts";
import { notImplemented } from "./_utils.ts";
import { validateIntegerRange } from "./_utils.ts";
import { EOL as fsEOL } from "../fs/eol.ts";
@ -146,7 +147,7 @@ export function endianness(): "BE" | "LE" {
/** Return free memory amount */
export function freemem(): number {
return Deno.systemMemoryInfo().free;
return DenoUnstable.systemMemoryInfo().free;
}
/** Not yet implemented */
@ -173,7 +174,7 @@ export function homedir(): string | null {
/** Returns the host name of the operating system as a string. */
export function hostname(): string {
return Deno.hostname();
return DenoUnstable.hostname();
}
/** Returns an array containing the 1, 5, and 15 minute load averages */
@ -181,7 +182,7 @@ export function loadavg(): number[] {
if (isWindows) {
return [0, 0, 0];
}
return Deno.loadavg();
return DenoUnstable.loadavg();
}
/** Returns an object containing network interfaces that have been assigned a network address.
@ -225,7 +226,7 @@ export function platform(): string {
/** Returns the operating system as a string */
export function release(): string {
return Deno.osRelease();
return DenoUnstable.osRelease();
}
/** Not yet implemented */
@ -271,7 +272,7 @@ export function tmpdir(): string | null {
/** Return total physical memory amount */
export function totalmem(): number {
return Deno.systemMemoryInfo().total;
return DenoUnstable.systemMemoryInfo().total;
}
/** Returns operating system type (i.e. 'Windows_NT', 'Linux', 'Darwin') */

View File

@ -1,5 +1,6 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
// Copyright Joyent, Inc. and Node.js contributors. All rights reserved. MIT license.
import * as DenoUnstable from "../_deno_unstable.ts";
import { warnNotImplemented } from "./_utils.ts";
import { EventEmitter } from "./events.ts";
import { validateString } from "./internal/validators.js";
@ -298,7 +299,7 @@ class Process extends EventEmitter {
if (notImplementedEvents.includes(event)) {
warnNotImplemented(`process.on("${event}")`);
} else if (event.startsWith("SIG")) {
Deno.addSignalListener(event as Deno.Signal, listener);
DenoUnstable.addSignalListener(event as Deno.Signal, listener);
} else {
super.on(event, listener);
}
@ -316,7 +317,7 @@ class Process extends EventEmitter {
if (notImplementedEvents.includes(event)) {
warnNotImplemented(`process.off("${event}")`);
} else if (event.startsWith("SIG")) {
Deno.removeSignalListener(event as Deno.Signal, listener);
DenoUnstable.removeSignalListener(event as Deno.Signal, listener);
} else {
super.off(event, listener);
}
@ -397,7 +398,7 @@ class Process extends EventEmitter {
}
/** https://nodejs.org/api/process.html#processumaskmask */
umask = Deno.umask;
umask = DenoUnstable.umask;
/** https://nodejs.org/api/process.html#processgetuid */
getuid(): number {

View File

@ -1,6 +1,7 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
// deno-lint-ignore-file no-unused-vars
import * as DenoUnstable from "../_deno_unstable.ts";
import { relative, resolve } from "../path/mod.ts";
const CLOCKID_REALTIME = 0;
@ -726,7 +727,7 @@ export default class Context {
mtim = BigInt(Date.now() * 1e6);
}
Deno.utimeSync(entry.path!, Number(atim), Number(mtim));
DenoUnstable.utimeSync(entry.path!, Number(atim), Number(mtim));
return ERRNO_SUCCESS;
}),
@ -1220,7 +1221,7 @@ export default class Context {
mtim = BigInt(Date.now()) * BigInt(1e6);
}
Deno.utimeSync(path, Number(atim), Number(mtim));
DenoUnstable.utimeSync(path, Number(atim), Number(mtim));
return ERRNO_SUCCESS;
}),