summaryrefslogtreecommitdiff
path: root/std/node
diff options
context:
space:
mode:
Diffstat (limited to 'std/node')
-rw-r--r--std/node/events.ts33
-rw-r--r--std/node/events_test.ts19
-rw-r--r--std/node/global.ts3
-rw-r--r--std/node/module.ts43
-rw-r--r--std/node/os.ts4
5 files changed, 62 insertions, 40 deletions
diff --git a/std/node/events.ts b/std/node/events.ts
index f4b3e0bc5..b2f1d6026 100644
--- a/std/node/events.ts
+++ b/std/node/events.ts
@@ -22,6 +22,7 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.
import { validateIntegerRange } from "./util.ts";
+import { assert } from "../testing/asserts.ts";
export interface WrappedFunction extends Function {
listener: Function;
@@ -163,7 +164,8 @@ export default class EventEmitter {
private unwrapListeners(arr: Function[]): Function[] {
const unwrappedListeners: Function[] = new Array(arr.length) as Function[];
for (let i = 0; i < arr.length; i++) {
- unwrappedListeners[i] = arr[i]["listener"] || arr[i];
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ unwrappedListeners[i] = (arr[i] as any)["listener"] || arr[i];
}
return unwrappedListeners;
}
@@ -232,10 +234,12 @@ export default class EventEmitter {
const wrapperContext = {
eventName: eventName,
listener: listener,
- rawListener: wrapper,
+ rawListener: (wrapper as unknown) as WrappedFunction,
context: this
};
- const wrapped = wrapper.bind(wrapperContext);
+ const wrapped = (wrapper.bind(
+ wrapperContext
+ ) as unknown) as WrappedFunction;
wrapperContext.rawListener = wrapped;
wrapped.listener = listener;
return wrapped as WrappedFunction;
@@ -275,7 +279,7 @@ export default class EventEmitter {
return this;
}
- if (this._events.has(eventName)) {
+ if (eventName && this._events.has(eventName)) {
const listeners = (this._events.get(eventName) as Array<
Function | WrappedFunction
>).slice(); // Create a copy; We use it AFTER it's deleted.
@@ -299,14 +303,19 @@ export default class EventEmitter {
*/
public removeListener(eventName: string | symbol, listener: Function): this {
if (this._events.has(eventName)) {
- const arr: Array<Function | WrappedFunction> = this._events.get(
- eventName
- );
+ const arr:
+ | Array<Function | WrappedFunction>
+ | undefined = this._events.get(eventName);
+
+ assert(arr);
let listenerIndex = -1;
for (let i = arr.length - 1; i >= 0; i--) {
// arr[i]["listener"] is the reference to the listener inside a bound 'once' wrapper
- if (arr[i] == listener || arr[i]["listener"] == listener) {
+ if (
+ arr[i] == listener ||
+ (arr[i] && (arr[i] as WrappedFunction)["listener"] == listener)
+ ) {
listenerIndex = i;
break;
}
@@ -421,8 +430,10 @@ export function on(
): AsyncInterable {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const unconsumedEventValues: any[] = [];
- const unconsumedPromises = [];
- let error = null;
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ const unconsumedPromises: any[] = [];
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ let error: Error | null = null;
let finished = false;
const iterator = {
@@ -476,7 +487,7 @@ export function on(
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
- [Symbol.asyncIterator](): AsyncIterable<any> {
+ [Symbol.asyncIterator](): any {
return this;
}
};
diff --git a/std/node/events_test.ts b/std/node/events_test.ts
index c89df298a..b0b74e499 100644
--- a/std/node/events_test.ts
+++ b/std/node/events_test.ts
@@ -17,7 +17,7 @@ test({
fn() {
let eventsFired: string[] = [];
const testEmitter = new EventEmitter();
- testEmitter.on("newListener", event => {
+ testEmitter.on("newListener", (event: string) => {
if (event !== "newListener") {
eventsFired.push("newListener");
}
@@ -81,20 +81,23 @@ test({
fn() {
const testEmitter = new EventEmitter();
const eventsFired: string[] = [];
- testEmitter.on("event", oneArg => {
+ testEmitter.on("event", (oneArg: string) => {
eventsFired.push("event(" + oneArg + ")");
});
- testEmitter.on("event", (oneArg, twoArg) => {
+ testEmitter.on("event", (oneArg: string, twoArg: string) => {
eventsFired.push("event(" + oneArg + ", " + twoArg + ")");
});
testEmitter.on("non-event", shouldNeverBeEmitted);
- testEmitter.on("event", (oneArg, twoArg, threeArg) => {
- eventsFired.push(
- "event(" + oneArg + ", " + twoArg + ", " + threeArg + ")"
- );
- });
+ testEmitter.on(
+ "event",
+ (oneArg: string, twoArg: string, threeArg: string) => {
+ eventsFired.push(
+ "event(" + oneArg + ", " + twoArg + ", " + threeArg + ")"
+ );
+ }
+ );
testEmitter.emit("event", 1, 2, 3);
assertEquals(eventsFired, ["event(1)", "event(1, 2)", "event(1, 2, 3)"]);
}
diff --git a/std/node/global.ts b/std/node/global.ts
index c21b0b659..c889e9c8d 100644
--- a/std/node/global.ts
+++ b/std/node/global.ts
@@ -1 +1,2 @@
-window["global"] = window;
+// @ts-ignore
+globalThis["global"] = globalThis;
diff --git a/std/node/module.ts b/std/node/module.ts
index ac436c555..547c76bab 100644
--- a/std/node/module.ts
+++ b/std/node/module.ts
@@ -40,7 +40,7 @@ const isWindows = path.isWindows;
const relativeResolveCache = Object.create(null);
let requireDepth = 0;
-let statCache = null;
+let statCache: Map<string, StatResult> | null = null;
type StatResult = -1 | 0 | 1;
// Returns 0 if the path refers to
@@ -64,7 +64,11 @@ function stat(filename: string): StatResult {
}
}
-function updateChildren(parent: Module, child: Module, scan: boolean): void {
+function updateChildren(
+ parent: Module | null,
+ child: Module,
+ scan: boolean
+): void {
const children = parent && parent.children;
if (children && !(scan && children.includes(child))) {
children.push(child);
@@ -75,17 +79,17 @@ class Module {
id: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
exports: any;
- parent?: Module;
- filename: string;
+ parent: Module | null;
+ filename: string | null;
loaded: boolean;
children: Module[];
paths: string[];
path: string;
- constructor(id = "", parent?: Module) {
+ constructor(id = "", parent?: Module | null) {
this.id = id;
this.exports = {};
- this.parent = parent;
- updateChildren(parent, this, false);
+ this.parent = parent || null;
+ updateChildren(parent || null, this, false);
this.filename = null;
this.loaded = false;
this.children = [];
@@ -229,25 +233,25 @@ class Module {
fakeParent.paths = Module._nodeModulePaths(path);
const lookupPaths = Module._resolveLookupPaths(request, fakeParent);
- for (let j = 0; j < lookupPaths.length; j++) {
- if (!paths.includes(lookupPaths[j])) paths.push(lookupPaths[j]);
+ for (let j = 0; j < lookupPaths!.length; j++) {
+ if (!paths.includes(lookupPaths![j])) paths.push(lookupPaths![j]);
}
}
}
} else if (options.paths === undefined) {
- paths = Module._resolveLookupPaths(request, parent);
+ paths = Module._resolveLookupPaths(request, parent)!;
} else {
throw new Error("options.paths is invalid");
}
} else {
- paths = Module._resolveLookupPaths(request, parent);
+ paths = Module._resolveLookupPaths(request, parent)!;
}
// Look up the filename first, since that's the cache key.
const filename = Module._findPath(request, paths, isMain);
if (!filename) {
const requireStack = [];
- for (let cursor = parent; cursor; cursor = cursor.parent) {
+ for (let cursor: Module | null = parent; cursor; cursor = cursor.parent) {
requireStack.push(cursor.filename || cursor.id);
}
let message = `Cannot find module '${request}'`;
@@ -341,7 +345,7 @@ class Module {
// object.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
static _load(request: string, parent: Module, isMain: boolean): any {
- let relResolveCacheIdentifier;
+ let relResolveCacheIdentifier: string | undefined;
if (parent) {
// Fast path for (lazy loaded) modules in the same directory. The indirect
// caching is required to allow cache invalidation without changing the old
@@ -385,6 +389,7 @@ class Module {
Module._cache[filename] = module;
if (parent !== undefined) {
+ assert(relResolveCacheIdentifier);
relativeResolveCache[relResolveCacheIdentifier] = filename;
}
@@ -397,6 +402,7 @@ class Module {
if (threw) {
delete Module._cache[filename];
if (parent !== undefined) {
+ assert(relResolveCacheIdentifier);
delete relativeResolveCache[relResolveCacheIdentifier];
}
} else if (
@@ -602,7 +608,7 @@ for (const id of nativeModulePolyfill.keys()) {
Module.builtinModules.push(id);
}
-let modulePaths = [];
+let modulePaths: string[] = [];
// Given a module name, and a list of paths to test, returns the first
// matching file in the following precedence.
@@ -664,7 +670,7 @@ function readPackage(requestPath: string): PackageInfo | null {
}
function readPackageScope(
- checkPath
+ checkPath: string
): { path: string; data: PackageInfo } | false {
const rootSeparatorIndex = checkPath.indexOf(path.sep);
let separatorIndex;
@@ -987,6 +993,7 @@ const CircularRequirePrototypeWarningProxy = new Proxy(
{
// eslint-disable-next-line @typescript-eslint/no-explicit-any
get(target, prop): any {
+ // @ts-ignore
if (prop in target) return target[prop];
emitCircularRequireWarning(prop);
return undefined;
@@ -1150,7 +1157,7 @@ function getPathFromURLWin32(url: URL): string {
let pathname = url.pathname;
for (let n = 0; n < pathname.length; n++) {
if (pathname[n] === "%") {
- const third = pathname.codePointAt(n + 2) | 0x20;
+ const third = pathname.codePointAt(n + 2)! | 0x20;
if (
(pathname[n + 1] === "2" && third === 102) || // 2f 2F /
(pathname[n + 1] === "5" && third === 99)
@@ -1165,7 +1172,7 @@ function getPathFromURLWin32(url: URL): string {
pathname = pathname.replace(forwardSlashRegEx, "\\");
pathname = decodeURIComponent(pathname);
// TODO: handle windows hostname case (needs bindings)
- const letter = pathname.codePointAt(1) | 0x20;
+ const letter = pathname.codePointAt(1)! | 0x20;
const sep = pathname[2];
if (
letter < CHAR_LOWERCASE_A ||
@@ -1184,7 +1191,7 @@ function getPathFromURLPosix(url: URL): string {
const pathname = url.pathname;
for (let n = 0; n < pathname.length; n++) {
if (pathname[n] === "%") {
- const third = pathname.codePointAt(n + 2) | 0x20;
+ const third = pathname.codePointAt(n + 2)! | 0x20;
if (pathname[n + 1] === "2" && third === 102) {
throw new Error(
"Invalid file URL path: must not include encoded / characters"
diff --git a/std/node/os.ts b/std/node/os.ts
index e4a00c450..3bff03a69 100644
--- a/std/node/os.ts
+++ b/std/node/os.ts
@@ -123,7 +123,7 @@ export function getPriority(pid = 0): number {
}
/** Returns the string path of the current user's home directory. */
-export function homedir(): string {
+export function homedir(): string | null {
return Deno.dir("home");
}
@@ -157,7 +157,7 @@ export function release(): string {
/** Not yet implemented */
export function setPriority(pid: number, priority?: number): void {
- /* The node API has the 'pid' as the first parameter and as optional.
+ /* The node API has the 'pid' as the first parameter and as optional.
This makes for a problematic implementation in Typescript. */
if (priority === undefined) {
priority = pid;