Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import { readBundledDefinitions } from '../utils/read-bundled-definitions';
let ingestRequests: { body: unknown; headers: Headers }[] = [];

const server = setupServer(
http.head('https://flags.vercel.com/v1/datafile', () => {
return new HttpResponse(null, { status: 200 });
}),
http.post('https://flags.vercel.com/v1/ingest', async ({ request }) => {
ingestRequests.push({
body: await request.json(),
Expand Down Expand Up @@ -2066,4 +2069,114 @@ describe('FlagNetworkDataSource', () => {
await dataSource.shutdown();
});
});

describe('preconnect', () => {
it('should fire HEAD request with correct headers during construction', async () => {
let headRequest: { url: string; headers: Headers } | undefined;

server.use(
http.head('https://flags.vercel.com/v1/datafile', ({ request }) => {
headRequest = { url: request.url, headers: request.headers };
return new HttpResponse(null, { status: 200 });
}),
);

new FlagNetworkDataSource({
sdkKey: 'vf_test_key',
stream: false,
polling: false,
});

await vi.waitFor(() => {
expect(headRequest).toBeDefined();
});

expect(headRequest!.url).toBe('https://flags.vercel.com/v1/datafile');
expect(headRequest!.headers.get('Authorization')).toBe(
'Bearer vf_test_key',
);
expect(headRequest!.headers.get('User-Agent')).toMatch(
/^VercelFlagsCore\//,
);
});

it('should not fire during build step', async () => {
let headRequestFired = false;

server.use(
http.head('https://flags.vercel.com/v1/datafile', () => {
headRequestFired = true;
return new HttpResponse(null, { status: 200 });
}),
);

process.env.CI = '1';

new FlagNetworkDataSource({
sdkKey: 'vf_test_key',
stream: false,
polling: false,
});

await new Promise((r) => setTimeout(r, 100));

expect(headRequestFired).toBe(false);
});

it('should use custom fetch function when provided', async () => {
let customFetchCalled = false;

const customFetch: typeof globalThis.fetch = async (input, init) => {
if (init?.method === 'HEAD') {
customFetchCalled = true;
}
return globalThis.fetch(input, init);
};

new FlagNetworkDataSource({
sdkKey: 'vf_test_key',
stream: false,
polling: false,
fetch: customFetch,
});

await vi.waitFor(() => {
expect(customFetchCalled).toBe(true);
});
});

it('should silently ignore failures', async () => {
server.use(
http.head('https://flags.vercel.com/v1/datafile', () => {
return HttpResponse.error();
}),
);

const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});

const dataSource = new FlagNetworkDataSource({
sdkKey: 'vf_test_key',
stream: false,
polling: false,
datafile: {
projectId: 'test',
definitions: {
testFlag: {
environments: { production: 0 },
variants: [false, true],
},
},
environment: 'production',
},
});

await new Promise((r) => setTimeout(r, 100));

await dataSource.initialize();
const result = await dataSource.read();
expect(result.definitions).toBeDefined();

errorSpy.mockRestore();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,22 @@ export class FlagNetworkDataSource implements DataSource {
sdkKey: this.options.sdkKey,
host: this.host,
});

this.preconnect();
}

private preconnect(): void {
if (this.options.buildStep) return;

void this.options
.fetch(`${this.host}/v1/datafile`, {
method: 'HEAD',
headers: {
Authorization: `Bearer ${this.options.sdkKey}`,
'User-Agent': `VercelFlagsCore/${version}`,
},
})
.catch(() => {});
}

// ---------------------------------------------------------------------------
Expand Down
Loading