From 166bfc7fa5b49ddecc8e2b4681b3013e4e691b45 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 8 Aug 2023 11:04:13 +0200 Subject: [PATCH 1/2] feat: expose `__unenv__` flag for mocked classes and functions --- src/runtime/_internal/utils.ts | 4 +++- src/runtime/mock/empty.ts | 6 +++++- src/runtime/mock/noop.ts | 2 +- src/runtime/mock/proxy.ts | 3 +++ src/runtime/node/async_hooks/_async-hook.ts | 2 ++ src/runtime/node/async_hooks/_async-local-storage.ts | 2 ++ src/runtime/node/async_hooks/_async-resource.ts | 2 ++ src/runtime/node/buffer/_file.ts | 2 ++ src/runtime/node/http/_request.ts | 2 ++ src/runtime/node/http/_response.ts | 2 ++ src/runtime/node/net/socket.ts | 4 ++++ src/runtime/node/stream/readable.ts | 2 ++ src/runtime/node/stream/transform.ts | 2 ++ src/runtime/node/stream/writable.ts | 2 ++ src/runtime/node/util/_mime.ts | 4 ++++ 15 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/runtime/_internal/utils.ts b/src/runtime/_internal/utils.ts index 409ddce7..096eeef2 100644 --- a/src/runtime/_internal/utils.ts +++ b/src/runtime/_internal/utils.ts @@ -24,13 +24,15 @@ export function mergeFns(...functions: Fn[]) { } export function notImplemented(name: string) { - return (): any => { + const fn = (): any => { throw new Error(`[unenv] ${name} is not implemented yet!`); }; + return Object.assign(fn, { __unenv__: true }); } export function notImplementedClass(name: string) { return class { + readonly __unenv__ = true; constructor() { throw new Error(`[unenv] ${name} is not implemented yet!`); } diff --git a/src/runtime/mock/empty.ts b/src/runtime/mock/empty.ts index ff8b4c56..9ecb23f1 100644 --- a/src/runtime/mock/empty.ts +++ b/src/runtime/mock/empty.ts @@ -1 +1,5 @@ -export default {}; +export default Object.freeze( + Object.create(null, { + __unenv__: { get: () => true }, + }), +); diff --git a/src/runtime/mock/noop.ts b/src/runtime/mock/noop.ts index 2d1ec238..95beb569 100644 --- a/src/runtime/mock/noop.ts +++ b/src/runtime/mock/noop.ts @@ -1 +1 @@ -export default () => {}; +export default Object.assign(() => {}, { __unenv__: true }); diff --git a/src/runtime/mock/proxy.ts b/src/runtime/mock/proxy.ts index 0418a404..5238881f 100644 --- a/src/runtime/mock/proxy.ts +++ b/src/runtime/mock/proxy.ts @@ -11,6 +11,9 @@ function createMock(name: string, overrides: any = {}): any { if (prop === "__createMock__") { return createMock; } + if (prop === "__unenv__") { + return true; + } if (prop in overrides) { return overrides[prop]; } diff --git a/src/runtime/node/async_hooks/_async-hook.ts b/src/runtime/node/async_hooks/_async-hook.ts index ece4d473..e1ac9a26 100644 --- a/src/runtime/node/async_hooks/_async-hook.ts +++ b/src/runtime/node/async_hooks/_async-hook.ts @@ -3,6 +3,8 @@ import type asyncHooks from "node:async_hooks"; // https://nodejs.org/api/async_hooks.html export class AsyncHook implements asyncHooks.HookCallbacks { + readonly __unenv__ = true; + _enabled: boolean = false; _callbacks: asyncHooks.HookCallbacks = {}; diff --git a/src/runtime/node/async_hooks/_async-local-storage.ts b/src/runtime/node/async_hooks/_async-local-storage.ts index 79039860..b1a298ef 100644 --- a/src/runtime/node/async_hooks/_async-local-storage.ts +++ b/src/runtime/node/async_hooks/_async-local-storage.ts @@ -3,6 +3,8 @@ import type asyncHooks from "node:async_hooks"; // https://nodejs.org/api/async_context.html#class-asynclocalstorage export class AsyncLocalStorage implements asyncHooks.AsyncLocalStorage { + readonly __unenv__ = true; + _currentStore: undefined | T; _enterStore: undefined | T; _enabled: boolean = true; diff --git a/src/runtime/node/async_hooks/_async-resource.ts b/src/runtime/node/async_hooks/_async-resource.ts index b6ac664f..dc620258 100644 --- a/src/runtime/node/async_hooks/_async-resource.ts +++ b/src/runtime/node/async_hooks/_async-resource.ts @@ -6,6 +6,8 @@ import { executionAsyncId } from "./_async-hook"; let _asyncIdCounter = 100; export class AsyncResource implements asyncHooks.AsyncResource { + readonly __unenv__ = true; + type: string; _asyncId: undefined | number; _triggerAsyncId: undefined | number | asyncHooks.AsyncResourceOptions; diff --git a/src/runtime/node/buffer/_file.ts b/src/runtime/node/buffer/_file.ts index d41d1a2a..17e8a694 100644 --- a/src/runtime/node/buffer/_file.ts +++ b/src/runtime/node/buffer/_file.ts @@ -2,6 +2,8 @@ import type buffer from "node:buffer"; import { Buffer } from "./_buffer"; export class File extends Blob implements buffer.File { + readonly __unenv__ = true; + size: number = 0; type: any = ""; name: string = ""; diff --git a/src/runtime/node/http/_request.ts b/src/runtime/node/http/_request.ts index ed4d6273..ab61438b 100644 --- a/src/runtime/node/http/_request.ts +++ b/src/runtime/node/http/_request.ts @@ -7,6 +7,8 @@ import { rawHeaders } from "../../_internal/utils"; // Implementation: https://github.com/nodejs/node/blob/master/lib/_http_incoming.js export class IncomingMessage extends Readable implements http.IncomingMessage { + readonly __unenv__ = true; + public aborted: boolean = false; public httpVersion: string = "1.1"; public httpVersionMajor: number = 1; diff --git a/src/runtime/node/http/_response.ts b/src/runtime/node/http/_response.ts index 00087feb..cc6e0695 100644 --- a/src/runtime/node/http/_response.ts +++ b/src/runtime/node/http/_response.ts @@ -7,6 +7,8 @@ import { Writable } from "../stream/writable"; // Implementation: https://github.com/nodejs/node/blob/master/lib/_http_outgoing.js export class ServerResponse extends Writable implements http.ServerResponse { + readonly __unenv__ = true; + statusCode: number = 200; statusMessage: string = ""; upgrading: boolean = false; diff --git a/src/runtime/node/net/socket.ts b/src/runtime/node/net/socket.ts index b6762e48..ff5cbedc 100644 --- a/src/runtime/node/net/socket.ts +++ b/src/runtime/node/net/socket.ts @@ -4,6 +4,8 @@ import { Duplex } from "../stream/duplex"; // Docs: https://nodejs.org/api/net.html#net_class_net_socket export class Socket extends Duplex implements net.Socket { + readonly __unenv__ = true; + readonly bufferSize: number = 0; readonly bytesRead: number = 0; readonly bytesWritten: number = 0; @@ -90,6 +92,8 @@ export class Socket extends Duplex implements net.Socket { } export class SocketAddress implements net.SocketAddress { + readonly __unenv__ = true; + address: string; family: "ipv4" | "ipv6"; port: number; diff --git a/src/runtime/node/stream/readable.ts b/src/runtime/node/stream/readable.ts index fc61ad0e..a9f7be52 100644 --- a/src/runtime/node/stream/readable.ts +++ b/src/runtime/node/stream/readable.ts @@ -8,6 +8,8 @@ import { EventEmitter } from "../events"; // eslint-disable-next-line unicorn/prefer-event-target export class Readable extends EventEmitter implements stream.Readable { + readonly __unenv__ = true; + readonly readableEncoding: BufferEncoding | null = null; readonly readableEnded: boolean = true; readonly readableFlowing: boolean | null = false; diff --git a/src/runtime/node/stream/transform.ts b/src/runtime/node/stream/transform.ts index be32f01a..6b1c8c67 100644 --- a/src/runtime/node/stream/transform.ts +++ b/src/runtime/node/stream/transform.ts @@ -5,6 +5,8 @@ import { Duplex } from "./duplex"; // Implementation: https://github.com/nodejs/node/blob/master/lib/internal/streams/transform.js export class Transform extends Duplex implements stream.Transform { + readonly __unenv__ = true; + _transform( chunk: any, encoding: globalThis.BufferEncoding, diff --git a/src/runtime/node/stream/writable.ts b/src/runtime/node/stream/writable.ts index 0ee5f616..7c4e7d95 100644 --- a/src/runtime/node/stream/writable.ts +++ b/src/runtime/node/stream/writable.ts @@ -7,6 +7,8 @@ import { EventEmitter } from "../events"; // Implementation: https://github.com/nodejs/node/blob/master/lib/internal/streams/writable.js // eslint-disable-next-line unicorn/prefer-event-target export class Writable extends EventEmitter implements stream.Writable { + readonly __unenv__ = true; + readonly writable: boolean = true; writableEnded: boolean = false; writableFinished: boolean = false; diff --git a/src/runtime/node/util/_mime.ts b/src/runtime/node/util/_mime.ts index 67025fd5..b41bc34d 100644 --- a/src/runtime/node/util/_mime.ts +++ b/src/runtime/node/util/_mime.ts @@ -6,6 +6,8 @@ import type { // https://nodejs.org/api/util.html#class-utilmimetype export class MIMEType implements MIMETypeT { + readonly __unenv__ = true; + params = new MIMEParams(); type: string; subtype: string; @@ -35,6 +37,8 @@ export class MIMEType implements MIMETypeT { // https://nodejs.org/api/util.html#util_class_util_mimeparams export class MIMEParams extends Map implements MIMEParamsT { + readonly __unenv__ = true; + get(name: string) { return (super.get(name) || null) as any; } From 6d2fd0b2c60dd9ebdbccbb352bdc82838d233b37 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 8 Aug 2023 11:12:57 +0200 Subject: [PATCH 2/2] special difference for incoming message --- src/runtime/node/http/_request.ts | 2 +- src/runtime/node/stream/readable.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/runtime/node/http/_request.ts b/src/runtime/node/http/_request.ts index ab61438b..4ac4f6e8 100644 --- a/src/runtime/node/http/_request.ts +++ b/src/runtime/node/http/_request.ts @@ -7,7 +7,7 @@ import { rawHeaders } from "../../_internal/utils"; // Implementation: https://github.com/nodejs/node/blob/master/lib/_http_incoming.js export class IncomingMessage extends Readable implements http.IncomingMessage { - readonly __unenv__ = true; + public __unenv__ = {}; public aborted: boolean = false; public httpVersion: string = "1.1"; diff --git a/src/runtime/node/stream/readable.ts b/src/runtime/node/stream/readable.ts index a9f7be52..94237110 100644 --- a/src/runtime/node/stream/readable.ts +++ b/src/runtime/node/stream/readable.ts @@ -8,7 +8,7 @@ import { EventEmitter } from "../events"; // eslint-disable-next-line unicorn/prefer-event-target export class Readable extends EventEmitter implements stream.Readable { - readonly __unenv__ = true; + __unenv__: unknown = true; readonly readableEncoding: BufferEncoding | null = null; readonly readableEnded: boolean = true;