Skip to content

Commit be963e0

Browse files
committed
Force mtlsFetch binding.
1 parent 48a42d3 commit be963e0

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

src/__tests__/getMtlsFetch.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ describe('getMtlsFetch', () => {
2525
it('should return the MTLS fetch function when the binding exists', () => {
2626
// Arrange
2727
mockEnv[MTLS_BINDING] = { fetch: mockFetch };
28+
const boundMockFetch = mockFetch.bind(mockEnv[MTLS_BINDING]);
29+
mockFetch.bind = vi.fn().mockReturnValue(boundMockFetch);
2830

2931
// Act
3032
const result = getMtlsFetch(mockEnv);
3133

3234
// Assert
33-
expect(result).toBe(mockFetch);
35+
expect(result).toBe(boundMockFetch);
3436
});
3537

3638
it('should return a function that throws an error when the binding does not exist', () => {

src/getMtlsFetch.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import { MTLS_BINDING } from './constants.js';
1313

1414
export default (env) => {
1515
if (env && env[MTLS_BINDING]) {
16-
return env[MTLS_BINDING].fetch;
16+
// We need the binding here, otherwise the worker will throw an error:
17+
// TypeError: Illegal invocation: function called with incorrect `this` reference.
18+
return env[MTLS_BINDING].fetch.bind(env[MTLS_BINDING]);
1719
}
1820

1921
return () => {

0 commit comments

Comments
 (0)