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

feat: expose __unenv__ flag for unenv classes and functions #125

Merged
merged 2 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion src/runtime/_internal/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!`);
}
Expand Down
6 changes: 5 additions & 1 deletion src/runtime/mock/empty.ts
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
export default {};
export default Object.freeze(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: freezing is not related to this PR but quickly included as fix

Object.create(null, {
__unenv__: { get: () => true },
}),
);
2 changes: 1 addition & 1 deletion src/runtime/mock/noop.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default () => {};
export default Object.assign(() => {}, { __unenv__: true });
3 changes: 3 additions & 0 deletions src/runtime/mock/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down
2 changes: 2 additions & 0 deletions src/runtime/node/async_hooks/_async-hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};

Expand Down
2 changes: 2 additions & 0 deletions src/runtime/node/async_hooks/_async-local-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import type asyncHooks from "node:async_hooks";
// https://nodejs.org/api/async_context.html#class-asynclocalstorage

export class AsyncLocalStorage<T> implements asyncHooks.AsyncLocalStorage<T> {
readonly __unenv__ = true;

_currentStore: undefined | T;
_enterStore: undefined | T;
_enabled: boolean = true;
Expand Down
2 changes: 2 additions & 0 deletions src/runtime/node/async_hooks/_async-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/runtime/node/buffer/_file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand Down
2 changes: 2 additions & 0 deletions src/runtime/node/http/_request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
public __unenv__ = {};

public aborted: boolean = false;
public httpVersion: string = "1.1";
public httpVersionMajor: number = 1;
Expand Down
2 changes: 2 additions & 0 deletions src/runtime/node/http/_response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/runtime/node/net/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/runtime/node/stream/readable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { EventEmitter } from "../events";

// eslint-disable-next-line unicorn/prefer-event-target
export class Readable extends EventEmitter implements stream.Readable {
__unenv__: unknown = true;

readonly readableEncoding: BufferEncoding | null = null;
readonly readableEnded: boolean = true;
readonly readableFlowing: boolean | null = false;
Expand Down
2 changes: 2 additions & 0 deletions src/runtime/node/stream/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions src/runtime/node/stream/writable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/runtime/node/util/_mime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -35,6 +37,8 @@ export class MIMEType implements MIMETypeT {
// https://nodejs.org/api/util.html#util_class_util_mimeparams

export class MIMEParams extends Map<string, string> implements MIMEParamsT {
readonly __unenv__ = true;

get(name: string) {
return (super.get(name) || null) as any;
}
Expand Down