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

fix(remove outvariant): remove it as it doesn't support esm #800

Merged
merged 1 commit into from
Mar 5, 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
3 changes: 1 addition & 2 deletions sandpack-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@
"dependencies": {
"@codesandbox/nodebox": "0.1.4",
"buffer": "^6.0.3",
"dequal": "^2.0.2",
"outvariant": "1.3.0"
"dequal": "^2.0.2"
},
"devDependencies": {
"@types/node": "^9.3.0",
Expand Down
144 changes: 144 additions & 0 deletions sandpack-client/src/outvariant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/* eslint-disable @typescript-eslint/no-explicit-any,@typescript-eslint/ban-ts-comment */
const POSITIONALS_EXP = /(%?)(%([sdjo]))/g;

function serializePositional(positional: any, flag: string): any {
switch (flag) {
// Strings.
case "s":
return positional;

// Digits.
case "d":
case "i":
return Number(positional);

// JSON.
case "j":
return JSON.stringify(positional);

// Objects.
case "o": {
// Preserve stings to prevent extra quotes around them.
if (typeof positional === "string") {
return positional;
}

const json = JSON.stringify(positional);

// If the positional isn't serializable, return it as-is.
if (json === "{}" || json === "[]" || /^\[object .+?\]$/.test(json)) {
return positional;
}

return json;
}
}
}

function format(message: string, ...positionals: any[]): string {
if (positionals.length === 0) {
return message;
}

let positionalIndex = 0;
let formattedMessage = message.replace(
POSITIONALS_EXP,
(match, isEscaped, _, flag) => {
const positional = positionals[positionalIndex];
const value = serializePositional(positional, flag);

if (!isEscaped) {
positionalIndex++;
return value;
}

return match;
}
);

// Append unresolved positionals to string as-is.
if (positionalIndex < positionals.length) {
formattedMessage += ` ${positionals.slice(positionalIndex).join(" ")}`;
}

formattedMessage = formattedMessage.replace(/%{2,2}/g, "%");

return formattedMessage;
}

const STACK_FRAMES_TO_IGNORE = 2;

/**
* Remove the "outvariant" package trace from the given error.
* This scopes down the error stack to the relevant parts
* when used in other applications.
*/
function cleanErrorStack(error: Error): void {
if (!error.stack) {
return;
}

const nextStack = error.stack.split("\n");
nextStack.splice(1, STACK_FRAMES_TO_IGNORE);
error.stack = nextStack.join("\n");
}

export class InvariantError extends Error {
name = "Invariant Violation";

constructor(public readonly message: string, ...positionals: any[]) {
super(message);
this.message = format(message, ...positionals);
cleanErrorStack(this);
}
}

export interface CustomErrorConstructor {
new (message: string): Error;
}

export interface CustomErrorFactory {
(message: string): Error;
}

export type CustomError = CustomErrorConstructor | CustomErrorFactory;

interface Invariant {
(
predicate: unknown,
message: string,
...positionals: any[]
): asserts predicate;

as(
ErrorConstructor: CustomError,
predicate: unknown,
message: string,
...positionals: unknown[]
): asserts predicate;
}

export const invariant: Invariant = (
predicate,
message,
...positionals
): asserts predicate => {
if (!predicate) {
// @ts-ignore
throw new InvariantError(message, ...positionals);
}
};

invariant.as = (ErrorConstructor, predicate, message, ...positionals) => {
if (!predicate) {
const isConstructor = ErrorConstructor.prototype.name != null;

const error: Error = isConstructor
? // @ts-ignore
new ErrorConstructor(format(message, positionals))
: // @ts-ignore
ErrorConstructor(format(message, positionals));

throw error;
}
};
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13677,7 +13677,7 @@ osenv@^0.1.4:
os-homedir "^1.0.0"
os-tmpdir "^1.0.0"

outvariant@1.3.0, outvariant@^1.3.0:
outvariant@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/outvariant/-/outvariant-1.3.0.tgz#c39723b1d2cba729c930b74bf962317a81b9b1c9"
integrity sha512-yeWM9k6UPfG/nzxdaPlJkB2p08hCg4xP6Lx99F+vP8YF7xyZVfTmJjrrNalkmzudD4WFvNLVudQikqUmF8zhVQ==
Expand Down