Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

node:*: fix some typescript errors #10310

Merged
merged 18 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions packages/bun-types/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1068,23 +1068,6 @@ declare global {
*/
canParse(url: string, base?: string): boolean;
}
var URL: typeof globalThis extends {
onerror: any;
URL: infer T;
}
? T
: typeof URL;

interface URLSearchParams {
new (init?: string | string[][] | Record<string, string> | URLSearchParams | undefined): URLSearchParams;
toString(): string;
}
var URLSearchParams: typeof globalThis extends {
onerror: any;
URLSearchParams: infer T;
}
? T
: typeof URLSearchParams;

interface EventListener {
(evt: Event): void;
Expand Down
1 change: 1 addition & 0 deletions src/js/builtins.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ declare function $getSetIteratorInternalField(): TODO;
declare function $getProxyInternalField(): TODO;
declare function $idWithProfile(): TODO;
declare function $isObject(obj: unknown): obj is object;
declare function $isArray(obj: unknown): obj is any[];
declare function $isCallable(fn: unknown): fn is CallableFunction;
declare function $isConstructor(fn: unknown): fn is { new (...args: any[]): any };
declare function $isJSArray(obj: unknown): obj is any[];
Expand Down
4 changes: 4 additions & 0 deletions src/js/node/child_process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1621,6 +1621,10 @@ function validateString(value, name) {
if (typeof value !== "string") throw new ERR_INVALID_ARG_TYPE(name, "string", value);
}

function isInt32(value) {
nektro marked this conversation as resolved.
Show resolved Hide resolved
return value === (value | 0);
}

function nullCheck(path, propName, throwError = true) {
const pathIsString = typeof path === "string";
const pathIsUint8Array = isUint8Array(path);
Expand Down
10 changes: 5 additions & 5 deletions src/js/node/diagnostics_channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
const SafeMap = Map;
const SafeFinalizationRegistry = FinalizationRegistry;

const ArrayPrototypeAt = (array, index) => array[index];
const ArrayPrototypeIndexOf = (array, value) => array.indexOf(value);
const ArrayPrototypePush = (array, value) => array.push(value);
const ArrayPrototypeSplice = (array, start, deleteCount) => array.splice(start, deleteCount);
const ArrayPrototypeAt = Array.prototype.at;
const ArrayPrototypeIndexOf = Array.prototype.indexOf;
const ArrayPrototypePush = Array.prototype.push;
nektro marked this conversation as resolved.
Show resolved Hide resolved
const ArrayPrototypeSplice = Array.prototype.splice;
const ObjectGetPrototypeOf = Object.getPrototypeOf;
const ObjectSetPrototypeOf = Object.setPrototypeOf;
const SymbolHasInstance = Symbol.hasInstance;
Expand All @@ -16,7 +16,7 @@ const PromiseReject = Promise.reject;
const PromisePrototypeThen = (promise, onFulfilled, onRejected) => promise.then(onFulfilled, onRejected);

// TODO: https://github.com/nodejs/node/blob/fb47afc335ef78a8cef7eac52b8ee7f045300696/src/node_util.h#L13
class WeakReference extends WeakRef {
class WeakReference<T extends WeakKey> extends WeakRef<T> {
#refs = 0;

get() {
Expand Down
181 changes: 0 additions & 181 deletions src/js/node/dns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,141 +42,6 @@ function lookup(domain, options, callback) {
}, callback);
}

function resolveSrv(hostname, callback) {
if (typeof callback != "function") {
throw new TypeError("callback must be a function");
}

dns.resolveSrv(hostname, callback).then(
results => {
callback(null, results);
},
error => {
callback(error);
},
);
}

function resolveTxt(hostname, callback) {
if (typeof callback != "function") {
throw new TypeError("callback must be a function");
}

dns.resolveTxt(hostname, callback).then(
results => {
callback(null, results);
},
error => {
callback(error);
},
);
}

function resolveSoa(hostname, callback) {
if (typeof callback != "function") {
throw new TypeError("callback must be a function");
}

dns.resolveSoa(hostname, callback).then(
results => {
callback(null, results);
},
error => {
callback(error);
},
);
}

function resolveNaptr(hostname, callback) {
if (typeof callback != "function") {
throw new TypeError("callback must be a function");
}

dns.resolveNaptr(hostname, callback).then(
results => {
callback(null, results);
},
error => {
callback(error);
},
);
}

function resolveMx(hostname, callback) {
if (typeof callback != "function") {
throw new TypeError("callback must be a function");
}

dns.resolveMx(hostname, callback).then(
results => {
callback(null, results);
},
error => {
callback(error);
},
);
}

function resolveCaa(hostname, callback) {
if (typeof callback != "function") {
throw new TypeError("callback must be a function");
}

dns.resolveCaa(hostname, callback).then(
results => {
callback(null, results);
},
error => {
callback(error);
},
);
}

function resolveNs(hostname, callback) {
if (typeof callback != "function") {
throw new TypeError("callback must be a function");
}

dns.resolveNs(hostname, callback).then(
results => {
callback(null, results);
},
error => {
callback(error);
},
);
}

function resolvePtr(hostname, callback) {
if (typeof callback != "function") {
throw new TypeError("callback must be a function");
}

dns.resolvePtr(hostname, callback).then(
results => {
callback(null, results);
},
error => {
callback(error);
},
);
}

function resolveCname(hostname, callback) {
if (typeof callback != "function") {
throw new TypeError("callback must be a function");
}

dns.resolveCname(hostname, callback).then(
results => {
callback(null, results);
},
error => {
callback(error);
},
);
}

function lookupService(address, port, callback) {
if (typeof callback != "function") {
throw new TypeError("callback must be a function");
Expand All @@ -192,21 +57,6 @@ function lookupService(address, port, callback) {
);
}

function reverse(ip, callback) {
if (typeof callback != "function") {
throw new TypeError("callback must be a function");
}

dns.reverse(ip, callback).then(
results => {
callback(null, results);
},
error => {
callback(error);
},
);
}

var InternalResolver = class Resolver {
constructor(options) {}

Expand Down Expand Up @@ -440,37 +290,6 @@ var InternalResolver = class Resolver {
setServers(servers) {}
};

function resolve(hostname, rrtype, callback) {
if (typeof rrtype == "function") {
callback = rrtype;
}

if (typeof callback != "function") {
throw new TypeError("callback must be a function");
}

dns.resolve(hostname).then(
results => {
switch (rrtype?.toLowerCase()) {
case "a":
case "aaaa":
callback(
null,
hostname,
results.map(({ address }) => address),
);
break;
default:
callback(null, results);
break;
}
},
error => {
callback(error);
},
);
}

function Resolver(options) {
return new InternalResolver(options);
}
Expand Down
17 changes: 10 additions & 7 deletions src/js/node/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ function listenerCount(emitter, type) {
return emitter.listenerCount(type);
}

function eventTargetAgnosticRemoveListener(emitter, name, listener, flags) {
function eventTargetAgnosticRemoveListener(emitter, name, listener, flags?) {
if (typeof emitter.removeListener === "function") {
emitter.removeListener(name, listener);
} else {
Expand All @@ -466,7 +466,7 @@ function eventTargetAgnosticAddListener(emitter, name, listener, flags) {
class AbortError extends Error {
constructor(message = "The operation was aborted", options = undefined) {
if (options !== undefined && typeof options !== "object") {
throw new codes.ERR_INVALID_ARG_TYPE("options", "Object", options);
throw ERR_INVALID_ARG_TYPE("options", "Object", options);
}
super(message, options);
this.code = "ABORT_ERR";
Expand All @@ -488,18 +488,18 @@ function ERR_OUT_OF_RANGE(name, range, value) {

function validateAbortSignal(signal, name) {
if (signal !== undefined && (signal === null || typeof signal !== "object" || !("aborted" in signal))) {
throw new ERR_INVALID_ARG_TYPE(name, "AbortSignal", signal);
throw ERR_INVALID_ARG_TYPE(name, "AbortSignal", signal);
}
}

function validateNumber(value, name, min = undefined, max) {
if (typeof value !== "number") throw new ERR_INVALID_ARG_TYPE(name, "number", value);
function validateNumber(value, name, min?: number, max?: number) {
if (typeof value !== "number") throw ERR_INVALID_ARG_TYPE(name, "number", value);
if (
(min != null && value < min) ||
(max != null && value > max) ||
((min != null || max != null) && Number.isNaN(value))
) {
throw new ERR_OUT_OF_RANGE(
throw ERR_OUT_OF_RANGE(
name,
`${min != null ? `>= ${min}` : ""}${min != null && max != null ? " && " : ""}${max != null ? `<= ${max}` : ""}`,
value,
Expand All @@ -513,6 +513,10 @@ function checkListener(listener) {
}
}

function validateBoolean(value, name) {
if (typeof value !== "boolean") throw ERR_INVALID_ARG_TYPE(name, "boolean", value);
}

let AsyncResource = null;

class EventEmitterAsyncResource extends EventEmitter {
Expand Down Expand Up @@ -584,7 +588,6 @@ Object.assign(EventEmitter, {
captureRejectionSymbol,
EventEmitterAsyncResource,
errorMonitor: kErrorMonitor,
setMaxListeners,
init: EventEmitter,
listenerCount,
});
Expand Down
6 changes: 6 additions & 0 deletions src/js/node/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ var WriteStream;
const EventEmitter = require("node:events");
const promises = require("node:fs/promises");
const Stream = require("node:stream");
const types = require("node:util/types");

const NumberIsFinite = Number.isFinite;
const DateNow = Date.now;
const DatePrototypeGetTime = Date.prototype.getTime;
const isDate = types.isDate;

// Private exports
const { FileHandle, kRef, kUnref, kFd, fs } = promises.$data;
Expand Down
2 changes: 1 addition & 1 deletion src/js/node/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ function validateFunction(callable: any, field: string) {

type FakeSocket = InstanceType<typeof FakeSocket>;
var FakeSocket = class Socket extends Duplex {
[kInternalSocketData]!: [import("bun").Server, OutgoingMessage, Request];
[kInternalSocketData]!: [import("bun").Server, typeof OutgoingMessage, typeof Request];
bytesRead = 0;
bytesWritten = 0;
connecting = false;
Expand Down
4 changes: 0 additions & 4 deletions src/js/node/net.ts
Original file line number Diff line number Diff line change
Expand Up @@ -603,10 +603,6 @@ const Socket = (function (InternalSocket) {
process.nextTick(closeNT, this);
}

get localAddress() {
return "127.0.0.1";
}

get localFamily() {
return "IPv4";
}
Expand Down
Loading
Loading