mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-17 04:46:05 +00:00
fix tests
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import { afterAll, beforeAll, describe, expect, it } from "bun:test";
|
import { afterAll, beforeAll, describe, expect, it } from "bun:test";
|
||||||
import { makeApp } from "./modes/fresh";
|
import { makeApp } from "./modes/fresh";
|
||||||
import { makeConfig } from "./config";
|
import { makeConfig, type CfMakeConfigArgs } from "./config";
|
||||||
import { disableConsoleLog, enableConsoleLog } from "core/utils";
|
import { disableConsoleLog, enableConsoleLog } from "core/utils";
|
||||||
import { adapterTestSuite } from "adapter/adapter-test-suite";
|
import { adapterTestSuite } from "adapter/adapter-test-suite";
|
||||||
import { bunTestRunner } from "adapter/bun/test";
|
import { bunTestRunner } from "adapter/bun/test";
|
||||||
@@ -23,7 +23,7 @@ describe("cf adapter", () => {
|
|||||||
{
|
{
|
||||||
connection: { url: DB_URL },
|
connection: { url: DB_URL },
|
||||||
},
|
},
|
||||||
{},
|
$ctx({ DB_URL }),
|
||||||
),
|
),
|
||||||
).toEqual({ connection: { url: DB_URL } });
|
).toEqual({ connection: { url: DB_URL } });
|
||||||
|
|
||||||
@@ -34,15 +34,15 @@ describe("cf adapter", () => {
|
|||||||
connection: { url: env.DB_URL },
|
connection: { url: env.DB_URL },
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
{
|
$ctx({ DB_URL }),
|
||||||
DB_URL,
|
|
||||||
},
|
|
||||||
),
|
),
|
||||||
).toEqual({ connection: { url: DB_URL } });
|
).toEqual({ connection: { url: DB_URL } });
|
||||||
});
|
});
|
||||||
|
|
||||||
adapterTestSuite<CloudflareBkndConfig, object>(bunTestRunner, {
|
adapterTestSuite<CloudflareBkndConfig, CfMakeConfigArgs<any>>(bunTestRunner, {
|
||||||
makeApp,
|
makeApp: async (c, a, o) => {
|
||||||
|
return await makeApp(c, { env: a } as any, o);
|
||||||
|
},
|
||||||
makeHandler: (c, a, o) => {
|
makeHandler: (c, a, o) => {
|
||||||
return async (request: any) => {
|
return async (request: any) => {
|
||||||
const app = await makeApp(
|
const app = await makeApp(
|
||||||
@@ -50,7 +50,7 @@ describe("cf adapter", () => {
|
|||||||
c ?? {
|
c ?? {
|
||||||
connection: { url: DB_URL },
|
connection: { url: DB_URL },
|
||||||
},
|
},
|
||||||
a,
|
a!,
|
||||||
o,
|
o,
|
||||||
);
|
);
|
||||||
return app.fetch(request);
|
return app.fetch(request);
|
||||||
|
|||||||
@@ -85,58 +85,61 @@ export function d1SessionHelper(config: CloudflareBkndConfig<any>) {
|
|||||||
let media_registered: boolean = false;
|
let media_registered: boolean = false;
|
||||||
export function makeConfig<Env extends CloudflareEnv = CloudflareEnv>(
|
export function makeConfig<Env extends CloudflareEnv = CloudflareEnv>(
|
||||||
config: CloudflareBkndConfig<Env>,
|
config: CloudflareBkndConfig<Env>,
|
||||||
args: CfMakeConfigArgs<Env>,
|
args?: CfMakeConfigArgs<Env>,
|
||||||
) {
|
) {
|
||||||
if (!media_registered) {
|
if (!media_registered) {
|
||||||
registerMedia(args as any);
|
registerMedia(args as any);
|
||||||
media_registered = true;
|
media_registered = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
const appConfig = makeAdapterConfig(config, args.env);
|
const appConfig = makeAdapterConfig(config, args?.env);
|
||||||
const bindings = config.bindings?.(args.env);
|
|
||||||
|
|
||||||
const sessionHelper = d1SessionHelper(config);
|
if (args?.env) {
|
||||||
const sessionId = sessionHelper.get(args.request);
|
const bindings = config.bindings?.(args?.env);
|
||||||
let session: D1DatabaseSession | undefined;
|
|
||||||
|
|
||||||
if (!appConfig.connection) {
|
const sessionHelper = d1SessionHelper(config);
|
||||||
let db: D1Database | undefined;
|
const sessionId = sessionHelper.get(args.request);
|
||||||
if (bindings?.db) {
|
let session: D1DatabaseSession | undefined;
|
||||||
$console.log("Using database from bindings");
|
|
||||||
db = bindings.db;
|
if (!appConfig.connection) {
|
||||||
} else if (Object.keys(args).length > 0) {
|
let db: D1Database | undefined;
|
||||||
const binding = getBinding(args.env, "D1Database");
|
if (bindings?.db) {
|
||||||
if (binding) {
|
$console.log("Using database from bindings");
|
||||||
$console.log(`Using database from env "${binding.key}"`);
|
db = bindings.db;
|
||||||
db = binding.value;
|
} else if (Object.keys(args).length > 0) {
|
||||||
|
const binding = getBinding(args.env, "D1Database");
|
||||||
|
if (binding) {
|
||||||
|
$console.log(`Using database from env "${binding.key}"`);
|
||||||
|
db = binding.value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (db) {
|
if (db) {
|
||||||
if (config.d1?.session) {
|
if (config.d1?.session) {
|
||||||
session = db.withSession(sessionId ?? config.d1?.first);
|
session = db.withSession(sessionId ?? config.d1?.first);
|
||||||
appConfig.connection = new D1Connection({ binding: session });
|
appConfig.connection = new D1Connection({ binding: session });
|
||||||
|
} else {
|
||||||
|
appConfig.connection = new D1Connection({ binding: db });
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
appConfig.connection = new D1Connection({ binding: db });
|
throw new Error("No database connection given");
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
throw new Error("No database connection given");
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (config.d1?.session) {
|
if (config.d1?.session) {
|
||||||
appConfig.options = {
|
appConfig.options = {
|
||||||
...appConfig.options,
|
...appConfig.options,
|
||||||
manager: {
|
manager: {
|
||||||
...appConfig.options?.manager,
|
...appConfig.options?.manager,
|
||||||
onServerInit: (server) => {
|
onServerInit: (server) => {
|
||||||
server.use(async (c, next) => {
|
server.use(async (c, next) => {
|
||||||
sessionHelper.set(c, session);
|
sessionHelper.set(c, session);
|
||||||
await next();
|
await next();
|
||||||
});
|
});
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
};
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return appConfig;
|
return appConfig;
|
||||||
|
|||||||
@@ -4,10 +4,10 @@ import { makeConfig, registerAsyncsExecutionContext, type CfMakeConfigArgs } fro
|
|||||||
|
|
||||||
export async function makeApp<Env extends CloudflareEnv = CloudflareEnv>(
|
export async function makeApp<Env extends CloudflareEnv = CloudflareEnv>(
|
||||||
config: CloudflareBkndConfig<Env>,
|
config: CloudflareBkndConfig<Env>,
|
||||||
args: CfMakeConfigArgs<Env>,
|
args?: CfMakeConfigArgs<Env>,
|
||||||
opts?: RuntimeOptions,
|
opts?: RuntimeOptions,
|
||||||
) {
|
) {
|
||||||
return await createRuntimeApp<Env>(makeConfig(config, args), args.env, opts);
|
return await createRuntimeApp<Env>(makeConfig(config, args), args?.env, opts);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getFresh<Env extends CloudflareEnv = CloudflareEnv>(
|
export async function getFresh<Env extends CloudflareEnv = CloudflareEnv>(
|
||||||
|
|||||||
Reference in New Issue
Block a user