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

add session key #873

Merged
merged 3 commits into from
Dec 5, 2024
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
47 changes: 36 additions & 11 deletions blocks/matcher.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// deno-lint-ignore-file no-explicit-any
import type { HttpContext } from "../blocks/handler.ts";
import { getCookies, Murmurhash3, setCookie } from "../deps.ts";
import type { Block, BlockModule, InstanceOf } from "../engine/block.ts";
Expand Down Expand Up @@ -70,21 +71,40 @@ const cookieValue = {
},
};

// deno-lint-ignore no-explicit-any
type MatchFunc<TConfig = any> =
| ((config: TConfig) => (ctx: MatchContext) => boolean)
| ((config: TConfig) => boolean)
| ((config: TConfig, ctx: MatchContext) => boolean);

export type MatcherStickiness = "session" | "none";

export interface MatcherModule extends
BlockModule<
MatchFunc,
boolean | ((ctx: MatchContext) => boolean),
(ctx: MatchContext) => boolean
> {
sticky?: MatcherStickiness;
export type MatcherModule<TProps = any> =
| MatcherStickySessionModule<TProps>
| MatcherStickyNoneModule;

const isStickySessionModule = <TProps = any>(
matcher: MatcherModule<TProps>,
): matcher is MatcherStickySessionModule<TProps> => {
return (matcher as MatcherStickySessionModule<TProps>).sticky === "session";
};

export type BlockModuleMatcher = BlockModule<
MatchFunc,
boolean | ((ctx: MatchContext) => boolean),
(ctx: MatchContext) => boolean
>;

export interface MatcherStickyNoneModule extends BlockModuleMatcher {
sticky?: "none";
}

export interface MatcherStickySessionModule<TProps = any>
extends BlockModuleMatcher {
sticky: "session";
sessionKey?: (
props: TProps,
ctx: MatchContext,
) => string | null;
}

const charByType = {
Expand All @@ -100,7 +120,7 @@ const matcherBlock: Block<
> = {
type: "matchers",
adapt: <TConfig = unknown>(
{ default: func, sticky }: MatcherModule,
matcherModule: MatcherModule<TConfig>,
) =>
(
$live: TConfig,
Expand All @@ -111,6 +131,7 @@ const matcherBlock: Block<
unknown
>,
) => {
const { default: func } = matcherModule;
const matcherFunc = (ctx: MatchContext) => {
const fMatcher = func as unknown as
| ((c: TConfig, ctx: MatchContext) => boolean)
Expand All @@ -122,7 +143,7 @@ const matcherBlock: Block<
return matcherFuncOrValue;
};
const respHeaders = httpCtx.context.state.response.headers;
const shouldStickyOnSession = sticky === "session";
const shouldStickyOnSession = isStickySessionModule(matcherModule);
return (ctx: MatchContext) => {
let uniqueId = "";
let isSegment = true;
Expand Down Expand Up @@ -152,7 +173,11 @@ const matcherBlock: Block<
result ??= matcherFunc(ctx);
} else {
hasher.hash(uniqueId);
const cookieName = `${DECO_MATCHER_PREFIX}${hasher.result()}`;
const _sessionKey = matcherModule.sessionKey
? `_${matcherModule.sessionKey?.($live, ctx)}`
: "";
const cookieName =
`${DECO_MATCHER_PREFIX}${hasher.result()}${_sessionKey}`;
hasher.reset();
const isMatchFromCookie = cookieValue.boolean(
getCookies(ctx.request.headers)[cookieName],
Expand Down
2 changes: 1 addition & 1 deletion hooks/useDevice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const useDevice = (): Device => {
}

if (!ctx) {
console.warn("Missing context in rendering tree")
console.warn("Missing context in rendering tree");
}

return ctx?.device || "desktop";
Expand Down
2 changes: 1 addition & 1 deletion runtime/caches/mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const baseTest = async (cacheStorageUT: CacheStorage) => {
const cache = await headersCache(lruCache(cacheStorageUT)).open(CACHE_NAME);
const response = () =>
new Response("Hello, World!", {
headers: { "Content-length": `${MAX_CACHE_SIZE / 2 }` },
headers: { "Content-length": `${MAX_CACHE_SIZE / 2}` },
});
for (let i = 0; i < 5; i++) {
const request = createRequest(i);
Expand Down
1 change: 0 additions & 1 deletion runtime/routes/invoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ function getParsingStrategy(req: Request): keyof typeof propsParsers | null {
return "json";
}


return null;
}

Expand Down
Loading