Skip to content

Commit f2f76a4

Browse files
committed
Allow tinylicious servers to be connected to within codespaces
1 parent 9097e85 commit f2f76a4

File tree

7 files changed

+71
-9
lines changed

7 files changed

+71
-9
lines changed

examples/benchmarks/tablebench/src/azure.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ import { SharedTree } from "@fluidframework/tree/legacy";
1313
import { generateTable } from "./data.js";
1414
import { Table } from "./tree/index.js";
1515

16+
function getTinyliciousEndpoint(port = 7070): string {
17+
if (typeof window !== "undefined") {
18+
const match = /^(.+)-\d+\.(.+)$/.exec(window.location.hostname);
19+
if (match) {
20+
return `https://${match[1]}-${port}.${match[2]}`;
21+
}
22+
}
23+
return `http://localhost:${port}`;
24+
}
25+
1626
const userId = Math.random().toString(36).slice(2);
1727

1828
const localConnectionConfig: AzureLocalConnectionConfig = {
@@ -21,7 +31,7 @@ const localConnectionConfig: AzureLocalConnectionConfig = {
2131
id: userId,
2232
name: `TestUser-${userId}`,
2333
}),
24-
endpoint: "http://localhost:7070",
34+
endpoint: getTinyliciousEndpoint(),
2535
};
2636

2737
const client = new AzureClient({ connection: localConnectionConfig });

examples/service-clients/azure-client/external-controller/src/fluid.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ const userDetails: ICustomUserDetails = {
3333
// Define the server we will be using and initialize Fluid
3434
const useAzure = process.env.FLUID_CLIENT === "azure";
3535

36+
function getTinyliciousEndpoint(port = 7070): string {
37+
if (typeof window !== "undefined") {
38+
const match = /^(.+)-\d+\.(.+)$/.exec(window.location.hostname);
39+
if (match) {
40+
return `https://${match[1]}-${port}.${match[2]}`;
41+
}
42+
}
43+
return `http://localhost:${port}`;
44+
}
45+
3646
const user = {
3747
id: uuid(),
3848
name: uuid(),
@@ -55,7 +65,7 @@ export const connectionConfig: AzureRemoteConnectionConfig | AzureLocalConnectio
5565
: {
5666
type: "local",
5767
tokenProvider: new InsecureTokenProvider("fooBar", user),
58-
endpoint: "http://localhost:7070",
68+
endpoint: getTinyliciousEndpoint(),
5969
};
6070

6171
/**

examples/service-clients/azure-client/todo-list/src/fluid.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ import { TodoItem, TodoList } from "./schema.js";
2424
// Define the server we will be using and initialize Fluid
2525
const useAzure = process.env.FLUID_CLIENT === "azure";
2626

27+
function getTinyliciousEndpoint(port = 7070): string {
28+
if (typeof window !== "undefined") {
29+
const match = /^(.+)-\d+\.(.+)$/.exec(window.location.hostname);
30+
if (match) {
31+
return `https://${match[1]}-${port}.${match[2]}`;
32+
}
33+
}
34+
return `http://localhost:${port}`;
35+
}
36+
2737
const user = {
2838
id: uuid(),
2939
name: uuid(),
@@ -52,7 +62,7 @@ export const connectionConfig: AzureRemoteConnectionConfig | AzureLocalConnectio
5262
: {
5363
type: "local",
5464
tokenProvider: new InsecureTokenProvider("fooBar", user),
55-
endpoint: "http://localhost:7070",
65+
endpoint: getTinyliciousEndpoint(),
5666
};
5767

5868
/**

examples/utils/example-driver/src/tinyliciousDriver.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ import { RouterliciousDocumentServiceFactory } from "@fluidframework/routerlicio
88
import {
99
createTinyliciousCreateNewRequest,
1010
InsecureTinyliciousTokenProvider,
11-
InsecureTinyliciousUrlResolver,
1211
// eslint-disable-next-line import-x/no-internal-modules
1312
} from "@fluidframework/tinylicious-driver/internal";
13+
import { createInsecureTinyliciousTestUrlResolver } from "@fluidframework/tinylicious-driver/test-utils";
1414

1515
import type { ExampleDriver } from "./interfaces.js";
1616

1717
export const createTinyliciousDriver = async (): Promise<ExampleDriver> => {
1818
const tokenProvider = new InsecureTinyliciousTokenProvider();
1919
return {
20-
urlResolver: new InsecureTinyliciousUrlResolver(),
20+
urlResolver: createInsecureTinyliciousTestUrlResolver(),
2121
documentServiceFactory: new RouterliciousDocumentServiceFactory(tokenProvider),
2222
createCreateNewRequest: (id: string) => createTinyliciousCreateNewRequest(),
2323
createLoadExistingRequest: async (id: string) => {

examples/utils/example-utils/src/modelLoader/tinyliciousModelLoader.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class TinyliciousService {
2424

2525
constructor(tinyliciousPort?: number) {
2626
const tokenProvider = createInsecureTinyliciousTestTokenProvider();
27-
this.urlResolver = createInsecureTinyliciousTestUrlResolver();
27+
this.urlResolver = createInsecureTinyliciousTestUrlResolver(tinyliciousPort);
2828
this.documentServiceFactory = createRouterliciousDocumentServiceFactory(tokenProvider);
2929
}
3030
}

examples/utils/webpack-fluid-loader/src/getUrlResolver.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ export const tinyliciousUrls = (
2929
} => {
3030
const port = options.tinyliciousPort ?? defaultTinyliciousPort;
3131

32+
// Detect GitHub Codespaces and use the forwarded port URL
33+
if (typeof window !== "undefined") {
34+
const match = /^(.+)-\d+\.(.+)$/.exec(window.location.hostname);
35+
if (match) {
36+
const url = `https://${match[1]}-${port}.${match[2]}`;
37+
return { deltaStreamUrl: url, hostUrl: url, ordererUrl: url, storageUrl: url };
38+
}
39+
}
40+
3241
return {
3342
deltaStreamUrl: `http://localhost:${port}`,
3443
hostUrl: `http://localhost:${port}`,

packages/drivers/tinylicious-driver/src/insecureTinyliciousUrlResolver.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,33 @@ export class InsecureTinyliciousUrlResolver implements IUrlResolver {
9393
}
9494

9595
/**
96-
* Creates an insecure Tinylicious URL resolver for testing purposes with localhost port 7070.
96+
* Detects the appropriate Tinylicious endpoint based on the environment.
97+
* In GitHub Codespaces, returns the forwarded port URL. Otherwise returns localhost.
9798
*/
98-
export function createInsecureTinyliciousTestUrlResolver(): IUrlResolver {
99-
return new InsecureTinyliciousUrlResolver();
99+
function getTinyliciousEndpoint(port: number = defaultTinyliciousPort): {
100+
endpoint: string;
101+
port: number;
102+
} {
103+
if (typeof window !== "undefined") {
104+
const match = /^(.+)-\d+\.(.+)$/.exec(window.location.hostname);
105+
if (match) {
106+
// In Codespaces, the port is embedded in the hostname, use HTTPS port 443
107+
return { endpoint: `https://${match[1]}-${port}.${match[2]}`, port: 443 };
108+
}
109+
}
110+
return { endpoint: defaultTinyliciousEndpoint, port };
111+
}
112+
113+
/**
114+
* Creates an insecure Tinylicious URL resolver for testing purposes.
115+
* Automatically detects GitHub Codespaces and uses the appropriate endpoint.
116+
* @param tinyliciousPort - Optional port number (defaults to 7070)
117+
*/
118+
export function createInsecureTinyliciousTestUrlResolver(
119+
tinyliciousPort?: number,
120+
): IUrlResolver {
121+
const { endpoint, port } = getTinyliciousEndpoint(tinyliciousPort);
122+
return new InsecureTinyliciousUrlResolver(port, endpoint);
100123
}
101124

102125
/**

0 commit comments

Comments
 (0)