-
Notifications
You must be signed in to change notification settings - Fork 7
SDK: route server-side reads through the vapi RPC cache, with fallback #1614
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0ce85c6
SDK: route server-side reads through the vapi RPC cache, with fallback
feruzm d88c0ca
review: take the node deadline after the proxy attempt, keep aborts a…
feruzm ceb6160
review: breaker for a failing proxy, explicit production switch, no any
feruzm 06a6c14
review: bound the proxy wait by the caller, release a failed body, sn…
feruzm aedc85d
review: align the web's proxy timeout with vapi's lookup budget
feruzm 2894027
Merge origin/develop into feature/sdk-server-rpc-proxy
feruzm 016d369
chore: apply changeset versioning for PR #1614
github-actions[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| // @vitest-environment node | ||
| import { afterEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| /** | ||
| * core/sdk-init.ts switches the server-side RPC proxy on at import time, and | ||
| * only when the deployment asked for it AND both halves of the wiring are | ||
| * present. A module with import-time side effects, so each case gets a fresh | ||
| * module registry and its own environment. | ||
| */ | ||
| const manager = { | ||
| setPrivateApiHost: vi.fn(), | ||
| setImageHost: vi.fn(), | ||
| setHiveNodes: vi.fn(), | ||
| setUserAgent: vi.fn(), | ||
| setResilience: vi.fn(), | ||
| setServerRpcProxy: vi.fn(), | ||
| setDmcaLists: vi.fn() | ||
| }; | ||
| vi.mock("@ecency/sdk", () => ({ ConfigManager: manager })); | ||
|
|
||
| async function load(env: Record<string, string | undefined>): Promise<void> { | ||
| vi.resetModules(); | ||
| for (const [k, v] of Object.entries(env)) { | ||
| vi.stubEnv(k, v ?? ""); | ||
| } | ||
| await import("@/core/sdk-init"); | ||
| } | ||
|
|
||
| afterEach(() => { | ||
| vi.unstubAllEnvs(); | ||
| manager.setServerRpcProxy.mockClear(); | ||
| }); | ||
|
|
||
| describe("sdk-init server rpc proxy", () => { | ||
| it("enables the proxy against the overlay host with the shared secret when switched on", async () => { | ||
| await load({ SSR_RPC_PROXY: "1", SSR_INTERNAL_SECRET: "s3cret", INTERNAL_API_HOST: "http://vapi:4000/" }); | ||
| expect(manager.setServerRpcProxy).toHaveBeenCalledWith({ | ||
| url: "http://vapi:4000/private-api/ssr/rpc", | ||
| headers: { "X-Ecency-Internal": "s3cret" }, | ||
| timeoutMs: 2000 | ||
| }); | ||
| }); | ||
|
|
||
| it.each([ | ||
| ["the switch is off", { SSR_RPC_PROXY: undefined, SSR_INTERNAL_SECRET: "s3cret", INTERNAL_API_HOST: "http://vapi:4000" }], | ||
| ["the secret is missing", { SSR_RPC_PROXY: "1", SSR_INTERNAL_SECRET: undefined, INTERNAL_API_HOST: "http://vapi:4000" }], | ||
| ["the host is missing", { SSR_RPC_PROXY: "1", SSR_INTERNAL_SECRET: "s3cret", INTERNAL_API_HOST: undefined }] | ||
| ])("stays off when %s", async (_label, env) => { | ||
| await load(env); | ||
| expect(manager.setServerRpcProxy).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import { readFileSync } from "node:fs"; | ||
| import { join } from "node:path"; | ||
| import { describe, expect, it } from "vitest"; | ||
|
|
||
| /** | ||
| * The server-side RPC proxy is decided at process start in core/sdk-init.ts | ||
| * from SSR_RPC_PROXY, SSR_INTERNAL_SECRET and INTERNAL_API_HOST, and vapi only | ||
| * switches its side on when it holds the same secret. A variable missing from | ||
| * any one of these places is silent: the SDK simply keeps going to the node | ||
| * pool. This pins the whole chain: both services in both stack files, the | ||
| * deploy jobs forwarding the secret, and the origin proxy hiding the path. | ||
| */ | ||
| const root = join(__dirname, "..", "..", "..", "..", ".."); | ||
| const read = (p: string): string => readFileSync(join(root, p), "utf8"); | ||
|
|
||
| function serviceBlock(compose: string, name: string): string { | ||
| const lines = compose.split("\n"); | ||
| const start = lines.findIndex((l) => l === ` ${name}:`); | ||
| if (start < 0) throw new Error(`service ${name} not found`); | ||
| let end = lines.length; | ||
| for (let i = start + 1; i < lines.length; i++) { | ||
| if (/^ [A-Za-z_-]+:/.test(lines[i]) || /^[A-Za-z_-]+:/.test(lines[i])) { | ||
| end = i; | ||
| break; | ||
| } | ||
| } | ||
| return lines.slice(start, end).join("\n"); | ||
| } | ||
|
|
||
| const envEntries = (block: string): string[] => | ||
| block.split("\n").filter((l) => /^ - [A-Z_]+/.test(l)).map((l) => l.trim().slice(2)); | ||
|
|
||
| describe("ssr rpc proxy deploy wiring", () => { | ||
| it.each(["apps/web/docker-compose.yml", "apps/web/docker-compose.production.yml"])( | ||
| "%s hands the secret to vapi and to web, and web carries the switch", | ||
| (file) => { | ||
| const compose = read(file); | ||
| expect(envEntries(serviceBlock(compose, "vapi")), `${file}: vapi`).toContain("SSR_INTERNAL_SECRET"); | ||
| const web = envEntries(serviceBlock(compose, "web")); | ||
| expect(web, `${file}: web`).toContain("SSR_INTERNAL_SECRET"); | ||
| expect(web.some((e) => e === "SSR_RPC_PROXY" || e === "SSR_RPC_PROXY=1"), `${file}: web switch`).toBe(true); | ||
| } | ||
| ); | ||
|
|
||
| it("alpha has the switch on, production leaves it to the deploy", () => { | ||
| expect(envEntries(serviceBlock(read("apps/web/docker-compose.yml"), "web"))).toContain("SSR_RPC_PROXY=1"); | ||
| expect(envEntries(serviceBlock(read("apps/web/docker-compose.production.yml"), "web"))).toContain("SSR_RPC_PROXY"); | ||
| }); | ||
|
|
||
| it.each(["master.yml", "staging.yml"])(".github/workflows/%s forwards the secret end to end", (file) => { | ||
| const wf = read(`.github/workflows/${file}`); | ||
| const steps = wf.split("appleboy/ssh-action").length - 1; | ||
| expect(steps).toBeGreaterThan(0); | ||
| expect(wf.match(/SSR_INTERNAL_SECRET: \$\{\{secrets\.SSR_INTERNAL_SECRET\}\}/g)?.length, `${file}: env`).toBe(steps); | ||
| expect(wf.match(/envs: [^\n]*SSR_INTERNAL_SECRET/g)?.length, `${file}: envs`).toBe(steps); | ||
| expect(wf.match(/export SSR_INTERNAL_SECRET=\$SSR_INTERNAL_SECRET/g)?.length, `${file}: export`).toBe(steps); | ||
| }); | ||
|
|
||
| it.each(["infra/origin/eu.ecency.com.conf", "infra/origin/us.ecency.com.conf"])( | ||
| "%s hides the proxy path from outside, ahead of the generic private-api location", | ||
| (file) => { | ||
| const conf = read(file); | ||
| const hidden = conf.indexOf("location ~ ^/private-api/ssr/"); | ||
| const generic = conf.indexOf("location ~ ^/(private-api|search-api|wallet-api|auth-api)"); | ||
| expect(hidden, `${file}: location`).toBeGreaterThan(-1); | ||
| expect(hidden, `${file}: order`).toBeLessThan(generic); | ||
| expect(conf.slice(hidden, generic)).toContain("return 404;"); | ||
| } | ||
| ); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.