Skip to content

Commit

Permalink
refactor: more strict type checks
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Dec 10, 2024
1 parent a400ba8 commit c8ed5cf
Show file tree
Hide file tree
Showing 16 changed files with 69 additions and 35 deletions.
7 changes: 5 additions & 2 deletions src/drivers/azure-key-vault.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { createError, createRequiredError, defineDriver } from "./utils";
import { SecretClient, SecretClientOptions } from "@azure/keyvault-secrets";
import {
SecretClient,
type SecretClientOptions,
} from "@azure/keyvault-secrets";
import { DefaultAzureCredential } from "@azure/identity";

export interface AzureKeyVaultOptions {
Expand Down Expand Up @@ -118,7 +121,7 @@ function encode(value: string): string {
for (const key in base64Map) {
encoded = encoded.replace(
new RegExp(key.replace(/[$()*+.?[\\\]^{|}]/g, "\\$&"), "g"),
base64Map[key]
base64Map[key]!
);
}
return encoded;
Expand Down
2 changes: 1 addition & 1 deletion src/drivers/azure-storage-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
TableClient,
AzureNamedKeyCredential,
AzureSASCredential,
TableEntity,
type TableEntity,
} from "@azure/data-tables";
import { DefaultAzureCredential } from "@azure/identity";

Expand Down
2 changes: 1 addition & 1 deletion src/drivers/cloudflare-kv-http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export default defineDriver<KVHTTPOptions>((opts) => {
if (i % 10_000 === 0) {
acc.push([]);
}
acc[acc.length - 1].push(key);
acc[acc.length - 1]!.push(key);
return acc;
},
[[]]
Expand Down
6 changes: 5 additions & 1 deletion src/drivers/fs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { existsSync, promises as fsp, Stats } from "node:fs";
import { resolve, relative, join } from "node:path";
import { FSWatcher, WatchOptions as ChokidarOptions, watch } from "chokidar";
import {
FSWatcher,
type WatchOptions as ChokidarOptions,
watch,
} from "chokidar";
import { createError, createRequiredError, defineDriver } from "./utils";
import {
readFile,
Expand Down
6 changes: 3 additions & 3 deletions src/drivers/redis.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { defineDriver, joinKeys } from "./utils";
import Redis, {
Cluster,
ClusterNode,
ClusterOptions,
RedisOptions as _RedisOptions,
type ClusterNode,
type ClusterOptions,
type RedisOptions as _RedisOptions,
} from "ioredis";

export interface RedisOptions extends _RedisOptions {
Expand Down
2 changes: 1 addition & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
getRequestHeader,
setResponseHeader,
readRawBody,
EventHandler,
type EventHandler,
H3Event,
} from "h3";
import type { Storage, TransactionOptions, StorageMeta } from "./types";
Expand Down
14 changes: 7 additions & 7 deletions src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ export function createStorage<T extends StorageValue>(
return {
base,
relativeKey: key.slice(base.length),
driver: context.mounts[base],
driver: context.mounts[base]!,
};
}
}
return {
base: "",
relativeKey: key,
driver: context.mounts[""],
driver: context.mounts[""]!,
};
};

Expand All @@ -65,7 +65,7 @@ export function createStorage<T extends StorageValue>(
? base!.slice(mountpoint.length)
: undefined,
mountpoint,
driver: context.mounts[mountpoint],
driver: context.mounts[mountpoint]!,
}));
};

Expand All @@ -86,7 +86,7 @@ export function createStorage<T extends StorageValue>(
context.watching = true;
for (const mountpoint in context.mounts) {
context.unwatch[mountpoint] = await watch(
context.mounts[mountpoint],
context.mounts[mountpoint]!,
onChange,
mountpoint
);
Expand All @@ -98,7 +98,7 @@ export function createStorage<T extends StorageValue>(
return;
}
for (const mountpoint in context.unwatch) {
await context.unwatch[mountpoint]();
await context.unwatch[mountpoint]!();
}
context.unwatch = {};
context.watching = false;
Expand Down Expand Up @@ -426,11 +426,11 @@ export function createStorage<T extends StorageValue>(
return;
}
if (context.watching && base in context.unwatch) {
context.unwatch[base]();
context.unwatch[base]?.();
delete context.unwatch[base];
}
if (_dispose) {
await dispose(context.mounts[base]);
await dispose(context.mounts[base]!);
}
context.mountpoints = context.mountpoints.filter((key) => key !== base);
delete context.mounts[base];
Expand Down
12 changes: 7 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ export function normalizeKey(key?: string) {
if (!key) {
return "";
}
return key
.split("?")[0]
.replace(/[/\\]/g, ":")
.replace(/:+/g, ":")
.replace(/^:|:$/g, "");
return (
key
.split("?")[0]
?.replace(/[/\\]/g, ":")
.replace(/:+/g, ":")
.replace(/^:|:$/g, "") || ""
);
}

export function joinKeys(...keys: string[]) {
Expand Down
4 changes: 2 additions & 2 deletions test/drivers/cloudflare-kv-binding.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import { getPlatformProxy } from "wrangler";

describe("drivers: cloudflare-kv", async () => {
const cfProxy = await getPlatformProxy();
globalThis.__env__ = cfProxy.env;
(globalThis as any).__env__ = cfProxy.env;
afterAll(async () => {
globalThis.__env__ = undefined;
(globalThis as any).__env__ = undefined;
await cfProxy.dispose();
});
testDriver({
Expand Down
7 changes: 5 additions & 2 deletions test/drivers/cloudflare-kv-http.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { afterAll, beforeAll, describe, expect, test } from "vitest";
import driver, { KVHTTPOptions } from "../../src/drivers/cloudflare-kv-http";
import driver, {
type KVHTTPOptions,
} from "../../src/drivers/cloudflare-kv-http";
import { testDriver } from "./utils";
import { rest } from "msw";
import { setupServer } from "msw/node";
Expand Down Expand Up @@ -78,7 +80,8 @@ const mockOptions: KVHTTPOptions = {
};

// TODO: Fix msw compatibility with Node 18
const isNode18 = Number.parseInt(process.version.slice(1).split(".")[0]) >= 18;
const isNode18 =
Number.parseInt(process.version.slice(1).split(".")[0] || "") >= 18;
describe.skipIf(isNode18)("drivers: cloudflare-kv-http", () => {
beforeAll(() => {
// Establish requests interception layer before all tests.
Expand Down
4 changes: 2 additions & 2 deletions test/drivers/cloudflare-r2-binding.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import { getPlatformProxy } from "wrangler";

describe("drivers: cloudflare-r2-binding", async () => {
const cfProxy = await getPlatformProxy();
globalThis.__env__ = cfProxy.env;
(globalThis as any).__env__ = cfProxy.env;
afterAll(async () => {
globalThis.__env__ = undefined;
(globalThis as any).__env__ = undefined;
await cfProxy.dispose();
});

Expand Down
2 changes: 1 addition & 1 deletion test/drivers/lru-cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { testDriver } from "./utils";

describe("drivers: lru-cache", () => {
testDriver({
driver: driver(),
driver: driver({}),
});
});

Expand Down
2 changes: 1 addition & 1 deletion test/drivers/redis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe("drivers: redis", () => {
});

it("exposes instance", () => {
expect(driver.getInstance()).toBeInstanceOf(ioredis.default);
expect(driver.getInstance?.()).toBeInstanceOf(ioredis.default);
});
},
});
Expand Down
7 changes: 6 additions & 1 deletion test/drivers/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { it, expect } from "vitest";
import { Storage, Driver, createStorage, restoreSnapshot } from "../../src";
import {
type Storage,
type Driver,
createStorage,
restoreSnapshot,
} from "../../src";

export interface TestContext {
storage: Storage;
Expand Down
18 changes: 13 additions & 5 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "Node",
"module": "preserve",
"moduleDetection": "force",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"allowJs": true,
"resolveJsonModule": true,
"strict": true,
"skipLibCheck": true
},
"include": ["src"]
"isolatedModules": true,
"verbatimModuleSyntax": true,
"noUncheckedIndexedAccess": true,
"forceConsistentCasingInFileNames": true,
"noImplicitOverride": true,
"noEmit": true,
"allowImportingTsExtensions": true,
}
}
9 changes: 9 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineConfig } from "vitest/config";

export default defineConfig({
test: {
typecheck: {
enabled: true,
},
},
});

0 comments on commit c8ed5cf

Please sign in to comment.