mirror of
https://github.com/denoland/deno.git
synced 2025-09-28 05:04:48 +00:00
fix(std/node): correct typings for global, globalThis, window (#8363)
This commit is contained in:
parent
e3c3fc58cb
commit
315d889afa
5 changed files with 89 additions and 31 deletions
|
@ -1,19 +1,7 @@
|
||||||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||||
import { assert, assertEquals, assertThrows } from "../testing/asserts.ts";
|
import { assertEquals, assertThrows } from "../testing/asserts.ts";
|
||||||
import { Buffer } from "./buffer.ts";
|
import { Buffer } from "./buffer.ts";
|
||||||
|
|
||||||
Deno.test({
|
|
||||||
name: "Buffer global scope",
|
|
||||||
fn() {
|
|
||||||
// deno-lint-ignore ban-ts-comment
|
|
||||||
// @ts-expect-error
|
|
||||||
assert(window.Buffer === Buffer);
|
|
||||||
// deno-lint-ignore ban-ts-comment
|
|
||||||
// @ts-expect-error
|
|
||||||
assert(globalThis.Buffer === Buffer);
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
Deno.test({
|
Deno.test({
|
||||||
name: "alloc fails on negative numbers",
|
name: "alloc fails on negative numbers",
|
||||||
fn() {
|
fn() {
|
||||||
|
|
28
std/node/global.d.ts
vendored
Normal file
28
std/node/global.d.ts
vendored
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||||
|
import { process as processModule } from "./process.ts";
|
||||||
|
import { Buffer as bufferModule } from "./buffer.ts";
|
||||||
|
|
||||||
|
// d.ts files allow us to declare Buffer as a value and as a type
|
||||||
|
// type something = Buffer | something_else; is quite common
|
||||||
|
|
||||||
|
type GlobalType = {
|
||||||
|
process: typeof processModule;
|
||||||
|
Buffer: typeof bufferModule;
|
||||||
|
};
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
interface Window {
|
||||||
|
global: GlobalType;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface globalThis {
|
||||||
|
global: GlobalType;
|
||||||
|
}
|
||||||
|
|
||||||
|
var global: GlobalType;
|
||||||
|
var process: typeof processModule;
|
||||||
|
var Buffer: typeof bufferModule;
|
||||||
|
type Buffer = bufferModule;
|
||||||
|
}
|
||||||
|
|
||||||
|
export {};
|
|
@ -1,35 +1,24 @@
|
||||||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||||
import process from "./process.ts";
|
/// <reference path="./global.d.ts" />
|
||||||
import { Buffer as buffer } from "./buffer.ts";
|
import { process as processModule } from "./process.ts";
|
||||||
|
import { Buffer as bufferModule } from "./_buffer.ts";
|
||||||
|
|
||||||
Object.defineProperty(globalThis, Symbol.toStringTag, {
|
Object.defineProperty(globalThis, "global", {
|
||||||
value: "global",
|
value: globalThis,
|
||||||
writable: false,
|
writable: false,
|
||||||
enumerable: false,
|
enumerable: false,
|
||||||
configurable: true,
|
configurable: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
// deno-lint-ignore no-explicit-any
|
|
||||||
(globalThis as any)["global"] = globalThis;
|
|
||||||
|
|
||||||
// Define the type for the global declration
|
|
||||||
type Process = typeof process;
|
|
||||||
type Buffer = typeof buffer;
|
|
||||||
|
|
||||||
Object.defineProperty(globalThis, "process", {
|
Object.defineProperty(globalThis, "process", {
|
||||||
value: process,
|
value: processModule,
|
||||||
enumerable: false,
|
enumerable: false,
|
||||||
writable: true,
|
writable: true,
|
||||||
configurable: true,
|
configurable: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
declare global {
|
|
||||||
const process: Process;
|
|
||||||
const Buffer: Buffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
Object.defineProperty(globalThis, "Buffer", {
|
Object.defineProperty(globalThis, "Buffer", {
|
||||||
value: buffer,
|
value: bufferModule,
|
||||||
enumerable: false,
|
enumerable: false,
|
||||||
writable: true,
|
writable: true,
|
||||||
configurable: true,
|
configurable: true,
|
||||||
|
|
52
std/node/global_test.ts
Normal file
52
std/node/global_test.ts
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
import "./global.ts";
|
||||||
|
import { assert, assertStrictEquals } from "../testing/asserts.ts";
|
||||||
|
import { Buffer as BufferModule } from "./buffer.ts";
|
||||||
|
import processModule from "./process.ts";
|
||||||
|
|
||||||
|
// Definitions for this are quite delicate
|
||||||
|
// This ensures modifications to the global namespace don't break on TypeScript
|
||||||
|
|
||||||
|
// TODO
|
||||||
|
// Deno lint marks globals defined by this module as undefined
|
||||||
|
// probably gonna change in the future
|
||||||
|
|
||||||
|
Deno.test("global is correctly defined", () => {
|
||||||
|
// deno-lint-ignore no-undef
|
||||||
|
assertStrictEquals(global, globalThis);
|
||||||
|
// deno-lint-ignore no-undef
|
||||||
|
assertStrictEquals(global.Buffer, BufferModule);
|
||||||
|
// deno-lint-ignore no-undef
|
||||||
|
assertStrictEquals(global.process, process);
|
||||||
|
});
|
||||||
|
|
||||||
|
Deno.test("Buffer is correctly defined", () => {
|
||||||
|
//Check that Buffer is defined as a type as well
|
||||||
|
type x = Buffer;
|
||||||
|
// deno-lint-ignore no-undef
|
||||||
|
assertStrictEquals(Buffer, BufferModule);
|
||||||
|
// deno-lint-ignore no-undef
|
||||||
|
assert(Buffer.from);
|
||||||
|
// deno-lint-ignore no-undef
|
||||||
|
assertStrictEquals(global.Buffer, BufferModule);
|
||||||
|
// deno-lint-ignore no-undef
|
||||||
|
assert(global.Buffer.from);
|
||||||
|
assertStrictEquals(globalThis.Buffer, BufferModule);
|
||||||
|
assert(globalThis.Buffer.from);
|
||||||
|
assertStrictEquals(window.Buffer, BufferModule);
|
||||||
|
assert(window.Buffer.from);
|
||||||
|
});
|
||||||
|
|
||||||
|
Deno.test("process is correctly defined", () => {
|
||||||
|
// deno-lint-ignore no-undef
|
||||||
|
assertStrictEquals(process, processModule);
|
||||||
|
// deno-lint-ignore no-undef
|
||||||
|
assert(process.arch);
|
||||||
|
// deno-lint-ignore no-undef
|
||||||
|
assertStrictEquals(global.process, processModule);
|
||||||
|
// deno-lint-ignore no-undef
|
||||||
|
assert(global.process.arch);
|
||||||
|
assertStrictEquals(globalThis.process, processModule);
|
||||||
|
assert(globalThis.process.arch);
|
||||||
|
assertStrictEquals(window.process, processModule);
|
||||||
|
assert(window.process.arch);
|
||||||
|
});
|
|
@ -6,6 +6,7 @@ import * as path from "../path/mod.ts";
|
||||||
import * as all from "./process.ts";
|
import * as all from "./process.ts";
|
||||||
import { argv, env } from "./process.ts";
|
import { argv, env } from "./process.ts";
|
||||||
import { delay } from "../async/delay.ts";
|
import { delay } from "../async/delay.ts";
|
||||||
|
import "./global.ts";
|
||||||
|
|
||||||
// NOTE: Deno.execPath() (and thus process.argv) currently requires --allow-env
|
// NOTE: Deno.execPath() (and thus process.argv) currently requires --allow-env
|
||||||
// (Also Deno.env.toObject() (and process.env) requires --allow-env but it's more obvious)
|
// (Also Deno.env.toObject() (and process.env) requires --allow-env but it's more obvious)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue