Skip to content

Commit

Permalink
Removed invariant function and used node:assert in its place (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesopstad authored Dec 6, 2024
1 parent 451d487 commit 2b0ec21
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 39 deletions.
17 changes: 9 additions & 8 deletions packages/vite-plugin-cloudflare/src/cloudflare-environment.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import assert from 'node:assert';
import { builtinModules } from 'node:module';
import * as vite from 'vite';
import { getNodeCompatExternals } from './node-js-compat';
import { INIT_PATH, invariant, UNKNOWN_HOST } from './shared';
import { INIT_PATH, UNKNOWN_HOST } from './shared';
import { toMiniflareRequest } from './utils';
import type { ResolvedPluginConfig, WorkerConfig } from './plugin-config';
import type { Fetcher } from '@cloudflare/workers-types/experimental';
Expand All @@ -26,7 +27,7 @@ function createHotChannel(
const client: vite.HotChannelClient = {
send(payload) {
const webSocket = webSocketContainer.webSocket;
invariant(webSocket, webSocketUndefinedError);
assert(webSocket, webSocketUndefinedError);

webSocket.send(JSON.stringify(payload));
},
Expand All @@ -44,7 +45,7 @@ function createHotChannel(
return {
send(payload) {
const webSocket = webSocketContainer.webSocket;
invariant(webSocket, webSocketUndefinedError);
assert(webSocket, webSocketUndefinedError);

webSocket.send(JSON.stringify(payload));
},
Expand All @@ -59,13 +60,13 @@ function createHotChannel(
},
listen() {
const webSocket = webSocketContainer.webSocket;
invariant(webSocket, webSocketUndefinedError);
assert(webSocket, webSocketUndefinedError);

webSocket.addEventListener('message', onMessage);
},
close() {
const webSocket = webSocketContainer.webSocket;
invariant(webSocket, webSocketUndefinedError);
assert(webSocket, webSocketUndefinedError);

webSocket.removeEventListener('message', onMessage);
},
Expand Down Expand Up @@ -98,18 +99,18 @@ export class CloudflareDevEnvironment extends vite.DevEnvironment {
},
);

invariant(response.ok, 'Failed to initialize module runner');
assert(response.ok, 'Failed to initialize module runner');

const webSocket = response.webSocket;
invariant(webSocket, 'Failed to establish WebSocket');
assert(webSocket, 'Failed to establish WebSocket');

webSocket.accept();

this.#webSocketContainer.webSocket = webSocket;
}

async dispatchFetch(request: Request) {
invariant(this.#worker, 'Runner not initialized');
assert(this.#worker, 'Runner not initialized');

return this.#worker.fetch(toMiniflareRequest(request)) as any;
}
Expand Down
7 changes: 2 additions & 5 deletions packages/vite-plugin-cloudflare/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import assert from 'node:assert';
import * as fs from 'node:fs';
import path from 'node:path';
import { createMiddleware } from '@hattip/adapter-node';
Expand All @@ -18,7 +19,6 @@ import {
resolveNodeAliases,
} from './node-js-compat';
import { resolvePluginConfig } from './plugin-config';
import { invariant } from './shared';
import { toMiniflareRequest } from './utils';
import type { PluginConfig, ResolvedPluginConfig } from './plugin-config';
import type { Unstable_RawConfig } from 'wrangler';
Expand Down Expand Up @@ -79,10 +79,7 @@ export function cloudflare(pluginConfig: PluginConfig = {}): vite.Plugin {
).map((environmentName) => {
const environment = builder.environments[environmentName];

invariant(
environment,
`${environmentName} environment not found`,
);
assert(environment, `${environmentName} environment not found`);

return environment;
});
Expand Down
18 changes: 9 additions & 9 deletions packages/vite-plugin-cloudflare/src/miniflare-options.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import assert from 'node:assert';
import * as fs from 'node:fs';
import * as fsp from 'node:fs/promises';
import * as path from 'node:path';
Expand All @@ -13,7 +14,6 @@ import {
ASSET_WORKERS_COMPATIBILITY_DATE,
ROUTER_WORKER_NAME,
} from './assets';
import { invariant } from './shared';
import type { CloudflareDevEnvironment } from './cloudflare-environment';
import type { ResolvedPluginConfig, WorkerConfig } from './plugin-config';
import type { MiniflareOptions, SharedOptions, WorkerOptions } from 'miniflare';
Expand Down Expand Up @@ -67,7 +67,7 @@ function getWorkerToWorkerEntrypointNamesMap(
const entrypointNames = workerToWorkerEntrypointNamesMap.get(
value.name,
);
invariant(entrypointNames, missingWorkerErrorMessage(value.name));
assert(entrypointNames, missingWorkerErrorMessage(value.name));

entrypointNames.add(value.entrypoint);
}
Expand All @@ -88,22 +88,22 @@ function getWorkerToDurableObjectClassNamesMap(
for (const value of Object.values(worker.durableObjects ?? {})) {
if (typeof value === 'string') {
const classNames = workerToDurableObjectClassNamesMap.get(worker.name);
invariant(classNames, missingWorkerErrorMessage(worker.name));
assert(classNames, missingWorkerErrorMessage(worker.name));

classNames.add(value);
} else if (typeof value === 'object') {
if (value.scriptName) {
const classNames = workerToDurableObjectClassNamesMap.get(
value.scriptName,
);
invariant(classNames, missingWorkerErrorMessage(value.scriptName));
assert(classNames, missingWorkerErrorMessage(value.scriptName));

classNames.add(value.className);
} else {
const classNames = workerToDurableObjectClassNamesMap.get(
worker.name,
);
invariant(classNames, missingWorkerErrorMessage(worker.name));
assert(classNames, missingWorkerErrorMessage(worker.name));

classNames.add(value.className);
}
Expand Down Expand Up @@ -271,7 +271,7 @@ export function getDevMiniflareOptions(
data: [string, string, FetchFunctionOptions];
};

invariant(
assert(
invokePayloadData.name === 'fetchModule',
`Invalid invoke event: ${invokePayloadData.name}`,
);
Expand Down Expand Up @@ -342,7 +342,7 @@ export function getDevMiniflareOptions(
const entrypointNames = workerToWorkerEntrypointNamesMap.get(
workerOptions.name,
);
invariant(
assert(
entrypointNames,
`WorkerEntrypoint names not found for worker ${workerOptions.name}`,
);
Expand All @@ -356,7 +356,7 @@ export function getDevMiniflareOptions(
const classNames = workerToDurableObjectClassNamesMap.get(
workerOptions.name,
);
invariant(
assert(
classNames,
`DurableObject class names not found for worker ${workerOptions.name}`,
);
Expand Down Expand Up @@ -390,7 +390,7 @@ export function getDevMiniflareOptions(
}

function getEntryModule(main: string | undefined) {
invariant(
assert(
main,
'Unexpected error: missing main field in miniflareWorkerOptions',
);
Expand Down
12 changes: 6 additions & 6 deletions packages/vite-plugin-cloudflare/src/plugin-config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import assert from 'node:assert';
import * as fs from 'node:fs';
import * as path from 'node:path';
import * as vite from 'vite';
import { unstable_readConfig } from 'wrangler';
import { invariant } from './shared';
import type { Unstable_Config } from 'wrangler';

export interface PluginConfig {
Expand Down Expand Up @@ -66,7 +66,7 @@ function getConfigResult(
wranglerConfigPaths.add(configPath);

if (isEntryWorker && !wranglerConfig.main) {
invariant(
assert(
wranglerConfig.assets,
`No main or assets field provided in ${wranglerConfig.configPath}`,
);
Expand All @@ -77,12 +77,12 @@ function getConfigResult(
};
}

invariant(
assert(
wranglerConfig.main,
`No main field provided in ${wranglerConfig.configPath}`,
);

invariant(
assert(
wranglerConfig.name,
`No name field provided in ${wranglerConfig.configPath}`,
);
Expand Down Expand Up @@ -124,7 +124,7 @@ export function resolvePluginConfig(
? path.join(root, pluginConfig.wranglerConfig)
: findWranglerConfig(root);

invariant(
assert(
configPath,
`Config not found. Have you created a wrangler.json(c) or wrangler.toml file?`,
);
Expand Down Expand Up @@ -155,7 +155,7 @@ export function resolvePluginConfig(
wranglerConfigPaths,
);

invariant(
assert(
configResult.type === 'worker',
'Unexpected error: received AssetsOnlyResult with auxiliary workers.',
);
Expand Down
11 changes: 0 additions & 11 deletions packages/vite-plugin-cloudflare/src/shared.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,2 @@
export const UNKNOWN_HOST = 'http://localhost';
export const INIT_PATH = '/__vite_plugin_cloudflare_init__';

export function invariant(
condition: unknown,
message: string,
): asserts condition {
if (condition) {
return;
}

throw new Error(message);
}

0 comments on commit 2b0ec21

Please sign in to comment.