Skip to content

Commit

Permalink
fix(remove outvariant): remove it as it doesn't support esm (#800)
Browse files Browse the repository at this point in the history
  • Loading branch information
danilowoz committed Mar 5, 2023
1 parent e910362 commit 3c1faef
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 3 deletions.
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

1 comment on commit 3c1faef

@vercel
Copy link

@vercel vercel bot commented on 3c1faef Mar 5, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

sandpack-docs – ./website/docs

sandpack-docs-codesandbox1.vercel.app
sandpack.vercel.app
sandpack-docs-git-main-codesandbox1.vercel.app

Please sign in to comment.