proxy design · lesson 10 of 10 · sources checked 2026-11-20
gateway engineering: endpoint function, dialect registry, and stickiness test
This lesson shows how to build tested gateway code that encodes each dialect, loads credentials by key, and proves that a sticky session holds.
status of this lesson: documented in my test systempurpose
A provider can accept a session token in a wrong form and give a new exit for each call. A sweep, a planned series of tests over many tools and pages, then reports numbers for the wrong egress, and no error shows. Tested code between the collector and the provider finds this fault before the sweep.
objectives
- Build an endpoint function that applies each prefix one time only.
- Keep a dialect registry with a parity test that runs the same cases on each dialect.
- Load each proxy credential by key from a secret store, and refuse a default value.
- Explain why a stickiness test must open a new connection for each call.
new terms
- dialect registry
- A table of the dialects that a gateway layer supports, with the encode rule of each dialect.
- endpoint function
- A pure function that builds a proxy endpoint from a dialect, a geo token, and a session token.
- parity test
- A test that runs the same cases on each dialect and compares the results.
- secret by reference
- A configuration value that names a secret in a store and never holds the secret.
how it works
The gateway code sits between the collector, the program that runs the jobs, and the gateway of each provider. It has five parts:
- A dialect registry.
- An endpoint function.
- A credential loader that reads a secret store.
- A local relay for a browser.
- A stickiness test against your own echo.
the endpoint function
The endpoint function builds the proxy user name from a dialect, a geo token, and a session token. It is pure: the same input always gives the same user name. Thus a test can compare its output with a fixed expected value.
The dialects A, B, and C are fictional. They describe no real provider:
| dialect | prefix | user name for the base lab1, geo DE, and token t01 |
|---|---|---|
| A | a- | a-lab1~geo~DE~sid~t01 |
| B | bx. | bx.lab1.DE.t01 |
| C | c_ | c_lab1/c=DE/s=t01 |
The function applies each prefix one time only. For example, the base bx.lab1 stays bx.lab1 in dialect B. A provider can refuse a user name with a double prefix and give no HTTP status. Then the refusal looks like a network fault.
The function also refuses an unknown geo token before any request. A gateway can ignore a geo token that it does not know, and give an exit in a different country.
the registry and the parity test
The dialect registry holds the prefix and the encode rule of each dialect. A parity test runs the same six cases on each dialect:
- no geo and no session token;
- geo only;
- session token only;
- geo and session token;
- a base that has the prefix already;
- an unknown geo token.
A change to one dialect then fails a case before a sweep runs. For example, a version of dialect B that adds its prefix two times fails only the case of the double prefix.
The benchmark platform, my test system, keeps one dialect for each provider and a stickiness test. Its endpoint code applies each prefix one time only.
credentials by key
The collector reads each proxy credential by key from a secret store at start. The job settings hold only the key, never the credential. This is a secret by reference. For example, the key proxy/dialect-a/password names one credential in the store.
A missing value or a default value, for example changeme, stops the start. A test then reads each log line and proves that no line holds a credential.
the local relay
The local relay adds the credential and the session token for a browser that cannot send them. More detail is in The gateway interface: dialects, sessions, geo, authentication (not yet published).
Each local port maps to one session token. For example, port 18001 maps to the token t01. The relay listens only on the loopback address. On a different address, each host of the network can use your credential.
the stickiness test
A sticky session must keep one exit for each request with the same session token. The stickiness test sends three calls with one token to your own egress echo. It expects one exit hash. For example, the token t01 gives three calls and must give one hash. More detail on the echo is in The egress echo: measure what leaves the client (not yet published).
Each call opens a new connection. A real gateway can tie the exit to the connection, not to the token. With one connection for the three calls, such a gateway gives one exit, and the test cannot fail.
the two sides
| automation side | detection side |
|---|---|
| One endpoint function keeps the geo and session token rules in one place. A parity test finds a silent change in one dialect before a sweep reports numbers for the wrong egress. | The site sees only the exit. A gateway that ignores a session token gives an address change inside one page load, and the site can see it. |
design decisions
| option | gain | cost | when it is correct |
|---|---|---|---|
| One endpoint function for all dialects | Each geo and session token rule is in one place. | Each new provider must have a registry entry and its cases. | Two or more providers |
| A user name string in each job | No shared code is necessary. | A change in one dialect breaks jobs with no error. | Never for a sweep |
| Parity test on each change of the registry | A silent change fails before a sweep. | Each case of each dialect must have an expected value. | Each registry |
| Credential by key from a secret store | No file or log holds the credential. | The start depends on the store. | Each credential |
| Local relay for a browser | The browser sends no credential in a flag. | The relay is one more process, and it must listen only on the loopback address. | A browser that cannot send a proxy credential |
| Stickiness test with a new connection for each call | The test finds an exit that the gateway ties to the connection. | Each call costs one more connection. | Each dialect, before the first sweep |
diagram
The first diagram shows the full gateway code.
the gateway code: from the dialect registry to the upstream gateway, and the stickiness test
The second diagram shows the local relay alone.
a local relay adds the credential and the session token for the browser
code example
The four examples use synthetic data only. No part describes a real provider, host, or credential.
example The first example builds the user names of the fictional dialects. It also shows the prefix rule and the refusal of an unknown geo token.
Build a proxy endpoint from a dialect, a geo token, and a session token. Apply each prefix one time only.
// Endpoint function for the fictional dialects A, B, and C. Synthetic data only.
// The function is pure: the same input always gives the same endpoint.
type Dialect = {
name: string;
prefix: string;
encode: (base: string, geo: string, sid: string) => string;
};
type Endpoint = { host: string; port: number; user: string };
const knownGeo = new Set(["", "DE", "FR", "US"]);
const dialects: Record<string, Dialect> = {
A: { name: "A", prefix: "a-", encode: (b, g, s) => [b, g && `geo~${g}`, s && `sid~${s}`].filter(Boolean).join("~") },
B: { name: "B", prefix: "bx.", encode: (b, g, s) => `${b}.${g || "-"}.${s || "-"}` },
C: { name: "C", prefix: "c_", encode: (b, g, s) => [b, g && `c=${g}`, s && `s=${s}`].filter(Boolean).join("/") },
};
// Apply the prefix one time only. A base that has it already stays as it is.
function withPrefix(prefix: string, base: string): string {
return base.startsWith(prefix) ? base : prefix + base;
}
function buildEndpoint(dialectName: string, base: string, geo: string, sid: string): Endpoint {
const d = dialects[dialectName];
if (!d) throw new Error(`unknown dialect ${dialectName}`);
if (!knownGeo.has(geo)) throw new Error(`unknown geo token ${geo}`);
if (!/^[a-z0-9]{0,16}$/.test(sid)) throw new Error("session token has a bad form");
return { host: "gateway.example", port: 8000, user: d.encode(withPrefix(d.prefix, base), geo, sid) };
}
const base = process.argv[2] ?? "lab1";
for (const name of ["A", "B", "C"]) {
const e = buildEndpoint(name, base, "DE", "t01");
console.log(`${name}: user ${e.user}, host ${e.host}, port ${e.port}`);
}
console.log(`B again: ${buildEndpoint("B", "bx." + base, "DE", "t01").user}`);
try {
buildEndpoint("A", base, "ZZ", "t01");
} catch (err) {
console.log(`A refused: ${(err as Error).message}`);
}output
One endpoint for each fictional dialect, the result of a double prefix, and one refusal.
A: user a-lab1~geo~DE~sid~t01, host gateway.example, port 8000
B: user bx.lab1.DE.t01, host gateway.example, port 8000
C: user c_lab1/c=DE/s=t01, host gateway.example, port 8000
B again: bx.lab1.DE.t01
A refused: unknown geo token ZZThe second line of dialect B shows the prefix rule. The base bx.lab1 keeps one prefix.
The second example is the registry and the parity test. The version with the seeded fault adds the prefix of dialect B two times.
Run the same six cases on each fictional dialect, and compare each user name with its expected value.
// Dialect registry and parity test for the fictional dialects A, B, and C.
// Each dialect runs the same six cases. Synthetic data only.
type Encode = (base: string, geo: string, sid: string) => string;
type Entry = { prefix: string; encode: Encode; once: boolean };
type Case = { name: string; base: string; geo: string; sid: string };
const knownGeo = new Set(["", "DE", "FR"]);
function registry(faultyB: boolean): Record<string, Entry> {
return {
A: { prefix: "a-", once: true, encode: (b, g, s) => [b, g && `geo~${g}`, s && `sid~${s}`].filter(Boolean).join("~") },
B: { prefix: "bx.", once: !faultyB, encode: (b, g, s) => `${b}.${g || "-"}.${s || "-"}` },
C: { prefix: "c_", once: true, encode: (b, g, s) => [b, g && `c=${g}`, s && `s=${s}`].filter(Boolean).join("/") },
};
}
function build(e: Entry, c: Case): string {
if (!knownGeo.has(c.geo)) return "refused";
const base = e.once && c.base.startsWith(e.prefix) ? c.base : e.prefix + c.base;
return e.encode(base, c.geo, c.sid);
}
// The six cases. The expected value comes from the plain base with no prefix.
const cases: Case[] = [
{ name: "no geo, no session", base: "lab1", geo: "", sid: "" },
{ name: "geo only", base: "lab1", geo: "DE", sid: "" },
{ name: "session only", base: "lab1", geo: "", sid: "t01" },
{ name: "geo and session", base: "lab1", geo: "FR", sid: "t02" },
{ name: "double prefix", base: "PREFIX", geo: "DE", sid: "t03" },
{ name: "unknown geo token", base: "lab1", geo: "ZZ", sid: "t04" },
];
function expected(e: Entry, c: Case): string {
if (!knownGeo.has(c.geo)) return "refused";
const base = c.base === "PREFIX" ? e.prefix + "lab1" : e.prefix + c.base;
return e.encode(base, c.geo, c.sid);
}
function parity(label: string, reg: Record<string, Entry>): void {
for (const [name, e] of Object.entries(reg)) {
const failed: string[] = [];
for (const c of cases) {
const input = { ...c, base: c.base === "PREFIX" ? e.prefix + "lab1" : c.base };
if (build(e, input) !== expected(e, c)) failed.push(c.name);
}
const passed = cases.length - failed.length;
const tail = failed.length ? `, fails: ${failed.join(", ")}` : "";
console.log(`${label} dialect ${name}: ${passed} of ${cases.length}${tail}`);
}
}
parity("normal", registry(false));
parity("seeded fault", registry(true));output
The passed cases of each dialect, for the normal version and for the version with a seeded fault.
normal dialect A: 6 of 6
normal dialect B: 6 of 6
normal dialect C: 6 of 6
seeded fault dialect A: 6 of 6
seeded fault dialect B: 5 of 6, fails: double prefix
seeded fault dialect C: 6 of 6The third example is the credential loader and its log test.
Read each proxy credential by key at start, refuse a default value, and test that no log line holds a credential.
// Credential loader: read each proxy credential by key at start.
// A missing value or a default value stops the start.
// The test proves that no log line holds a credential. Synthetic data only.
type Store = Record<string, string | undefined>;
const defaults = new Set(["", "changeme", "password", "default", "example"]);
const logLines: string[] = [];
function log(line: string): void {
logLines.push(line);
console.log(line);
}
function loadCredentials(store: Store, keys: string[]): Map<string, string> {
const out = new Map<string, string>();
for (const key of keys) {
const value = store[key];
if (value === undefined) throw new Error(`start refused: key ${key} is missing`);
if (defaults.has(value.toLowerCase())) throw new Error(`start refused: key ${key} has a default value`);
out.set(key, value);
log(`loaded key ${key}`);
}
return out;
}
function noSecretInLog(secrets: Iterable<string>): boolean {
for (const s of secrets) {
if (logLines.some((line) => line.includes(s))) return false;
}
return true;
}
const keys = ["proxy/dialect-a/password", "proxy/dialect-b/password"];
// Case 1: a store with a default value. The start must stop.
try {
loadCredentials({ "proxy/dialect-a/password": "S3c-lab-91", "proxy/dialect-b/password": "changeme" }, keys);
log("case 1: started");
} catch (err) {
log(`case 1: ${(err as Error).message}`);
}
// Case 2: a store with two synthetic values. The start continues.
const creds = loadCredentials({ "proxy/dialect-a/password": "S3c-lab-91", "proxy/dialect-b/password": "Q7-lab-k2x" }, keys);
log("case 2: started");
log(`test: no log line holds a credential: ${noSecretInLog(creds.values()) ? "pass" : "fail"}`);output
One refused start, one start, and the result of the log test.
loaded key proxy/dialect-a/password
case 1: start refused: key proxy/dialect-b/password has a default value
loaded key proxy/dialect-a/password
loaded key proxy/dialect-b/password
case 2: started
test: no log line holds a credential: passThe fourth example is the stickiness test against a synthetic lab gateway. With the seeded fault, the gateway ties the exit to the connection.
Send three calls with one session token through a lab gateway to your own echo, and count the exits of each token.
// Stickiness test against a synthetic lab gateway and your own echo.
// Three calls with one session token must give one exit.
// The lab gateway has 50 exit labels. With the seeded fault, it ties the
// exit to the connection and not to the session token. No network is used.
type Mode = "normal" | "fault";
class LabGateway {
nextConn = 0;
mode: Mode;
constructor(mode: Mode) {
this.mode = mode;
}
connect(): number {
return ++this.nextConn;
}
exitFor(conn: number, token: string): string {
const key = this.mode === "normal" ? token : `${token}#${conn}`;
let h = 0;
for (const ch of key) h = (h * 31 + ch.charCodeAt(0)) % 50;
return `exit-${h}`;
}
}
// The echo returns the exit label that reached it.
function echo(gw: LabGateway, conn: number, token: string): string {
return gw.exitFor(conn, token);
}
function stickiness(gw: LabGateway, token: string, newConnEachCall: boolean): number {
const exits = new Set<string>();
let conn = gw.connect();
for (let call = 1; call <= 3; call++) {
if (newConnEachCall && call > 1) conn = gw.connect();
exits.add(echo(gw, conn, token));
}
return exits.size;
}
const tokens = ["t01", "t02", "t03", "t04", "t05"];
for (const mode of ["normal", "fault"] as Mode[]) {
for (const fresh of [false, true]) {
const gw = new LabGateway(mode);
const counts = tokens.map((t) => stickiness(gw, t, fresh));
const sticky = counts.filter((n) => n === 1).length;
const how = fresh ? "new connection each call" : "one connection";
console.log(`${mode}, ${how}: exits per token ${counts.join(" ")}; sticky ${sticky} of ${tokens.length}`);
}
}output
The exits of each token for each mode of the gateway and each connection rule.
normal, one connection: exits per token 1 1 1 1 1; sticky 5 of 5
normal, new connection each call: exits per token 1 1 1 1 1; sticky 5 of 5
fault, one connection: exits per token 1 1 1 1 1; sticky 5 of 5
fault, new connection each call: exits per token 3 3 3 3 3; sticky 0 of 5I ran the four examples with the Node runtime and the flag --experimental-strip-types. The fourth example shows the connection rule: with one connection, the faulty gateway passes the test. With a new connection for each call, it fails.
data
parity test and stickiness test of a lab gateway
status of this figure: measured by me
source: Lab result of this course: parity test and stickiness test of a lab gateway on one host; date 2026-11-16; sample of 1,000; condition: Lab gateway and lab echo on one host, with 50 exit labels. 3 calls for each token. The exit is a label, not an address.
data table
| series | dialect and build | value | unit |
|---|---|---|---|
| cases passed | dialect A | 6 | cases |
| cases passed | dialect B | 6 | cases |
| cases passed | dialect C | 6 | cases |
| cases passed | dialect B with seeded double prefix | 5 | cases |
| tokens with 1 exit | dialect A | 1000 | tokens |
| tokens with 1 exit | dialect B | 1000 | tokens |
| tokens with 1 exit | dialect C | 1000 | tokens |
| tokens with 1 exit | dialect C with seeded gateway fault | 725 | tokens |
| tokens with 2 exits | dialect A | 0 | tokens |
| tokens with 2 exits | dialect B | 0 | tokens |
| tokens with 2 exits | dialect C | 0 | tokens |
| tokens with 2 exits | dialect C with seeded gateway fault | 251 | tokens |
| tokens with 3 exits | dialect A | 0 | tokens |
| tokens with 3 exits | dialect B | 0 | tokens |
| tokens with 3 exits | dialect C | 0 | tokens |
| tokens with 3 exits | dialect C with seeded gateway fault | 24 | tokens |
The chart is a lab result of this course, on one host with a lab gateway and a lab echo. The lab test has the case of a default credential in place of the case with no geo and no session token. The parity test found the seeded double prefix, and the stickiness test found the seeded gateway fault.
failure modes
| problem | cause | correction |
|---|---|---|
| The provider refuses each request with no HTTP status. | The user name has the prefix two times. | Apply each prefix one time only, and keep the double prefix in the parity test. |
| The exits are in a different country. | The gateway ignored a geo token that it does not know. | Refuse an unknown geo token in the endpoint function. |
| One dialect gives wrong user names after a change. | No test ran the cases of that dialect. | Run the parity test on each change of the registry. |
| The collector starts with a placeholder credential. | The loader accepted a default value. | Refuse a missing value or a default value at start. |
| A log line holds a credential. | The code wrote the value, not the key. | Write only the key, and test each log line. |
| The stickiness test passes, but the sweep shows a new exit for each page. | The test used one connection for the three calls. | Open a new connection for each call of the test. |
check questions
The base bx.lab1 goes into the endpoint function for dialect B. The prefix of dialect B is bx and a dot. Which user name is correct?
- bx.bx.lab1.DE.t01
- bx.lab1.DE.t01
- lab1.DE.t01
answer
b. bx.lab1.DE.t01
The endpoint function applies each prefix one time only. A base that has the prefix already stays as it is.
A version of dialect B adds its prefix two times. Which case of the parity test fails?
- Geo only
- An unknown geo token
- A base that has the prefix already
answer
c. A base that has the prefix already
Only the base with the prefix shows the fault. The other cases have no prefix in the base, so they give the expected user name.
The secret store gives the value changeme for a proxy credential. What does the credential loader do?
- It stops the start.
- It writes the value to the log and continues.
- It sends the value to the gateway one time as a test.
answer
a. It stops the start.
A missing value or a default value stops the start. The log holds only the key.
Explain why the stickiness test must open a new connection for each of its three calls.
sample answer
A real gateway can tie the exit to the connection and not to the session token. With one connection for the three calls, such a gateway gives one exit, and the test cannot fail. A new connection for each call shows the fault.
Name what the site sees when a gateway ignores a session token.
sample answer
The site sees only the exit. It sees an address change inside one page load.
exercise
exercise Do this task in your own lab, with synthetic data only. Do not send a request to a provider or a site.
The change: in the function registry of the second example, change once: true of dialect A to once: !faultyB.
- Save the second code example in an empty folder of your lab.
- Make the change.
- Run the code with Node and the flag
--experimental-strip-types. - Write the result line of dialect A for each version.
- Write the case that fails, and the fault that a provider can then show.
sample answer
The answer uses the synthetic dialects of the second code example.
The normal version prints normal dialect A: 6 of 6. The version with the seeded fault prints seeded fault dialect A: 5 of 6, fails: double prefix. Only the case of the base with the prefix fails, because dialect A now adds its prefix two times. A provider can refuse such a user name with no HTTP status, and the refusal then looks like a network fault.
sources
- My model of the gateway code: the endpoint function, the registry, the credential loader, the local relay, and the stickiness test, 2026-11 (not public).
- Benchmark platform, design (my test system): one dialect for each provider, the prefix rule, and the stickiness test, 2026-11 (not public).