1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-27 09:39:08 -05:00

fix(std/node): correct typings for global, globalThis, window (#8363)

This commit is contained in:
Steven Guerrero 2020-11-19 07:23:42 -05:00 committed by GitHub
parent e3c3fc58cb
commit 315d889afa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 89 additions and 31 deletions

View file

@ -1,19 +1,7 @@
// 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";
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({
name: "alloc fails on negative numbers",
fn() {

28
std/node/global.d.ts vendored Normal file
View 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 {};

View file

@ -1,35 +1,24 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import process from "./process.ts";
import { Buffer as buffer } from "./buffer.ts";
/// <reference path="./global.d.ts" />
import { process as processModule } from "./process.ts";
import { Buffer as bufferModule } from "./_buffer.ts";
Object.defineProperty(globalThis, Symbol.toStringTag, {
value: "global",
Object.defineProperty(globalThis, "global", {
value: globalThis,
writable: false,
enumerable: false,
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", {
value: process,
value: processModule,
enumerable: false,
writable: true,
configurable: true,
});
declare global {
const process: Process;
const Buffer: Buffer;
}
Object.defineProperty(globalThis, "Buffer", {
value: buffer,
value: bufferModule,
enumerable: false,
writable: true,
configurable: true,

52
std/node/global_test.ts Normal file
View 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);
});

View file

@ -6,6 +6,7 @@ import * as path from "../path/mod.ts";
import * as all from "./process.ts";
import { argv, env } from "./process.ts";
import { delay } from "../async/delay.ts";
import "./global.ts";
// 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)