Skip to content

Commit 884f8cc

Browse files
committed
Fix
1 parent ca6edf1 commit 884f8cc

4 files changed

Lines changed: 56 additions & 39 deletions

File tree

src/wrapper/mppx.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
1-
import { probe402 } from "./probe402.js";
1+
import { probe402, wsToHttp } from "./util.js";
22

33
export interface MppxClient {
4-
fetch: typeof globalThis.fetch;
5-
transport: {
6-
setCredential(request: Request, credential: string): Request;
7-
};
8-
createCredential(response: Response): Promise<string>;
4+
fetch: typeof globalThis.fetch;
5+
transport: {
6+
setCredential(request: Request, credential: string): Request;
7+
};
8+
createCredential(response: Response): Promise<string>;
99
}
1010

11-
export async function getPaymentCredentials(wsUrl: string, mppx: MppxClient): Promise<Record<string, string>> {
12-
const response = await probe402(wsUrl);
11+
export async function getPaymentCredentials(
12+
wsUrl: string,
13+
mppx: MppxClient
14+
): Promise<Record<string, string>> {
15+
const response = await probe402(wsUrl);
1316

14-
const credential = await mppx.createCredential(response);
15-
const signed = mppx.transport.setCredential(new Request(wsUrl), credential);
17+
const credential = await mppx.createCredential(response);
18+
const signed = mppx.transport.setCredential(
19+
new Request(wsToHttp(wsUrl)),
20+
credential
21+
);
1622

17-
const headers: Record<string, string> = {};
18-
signed.headers.forEach((value: string, key: string) => {
19-
headers[key] = value;
20-
});
21-
return headers;
23+
const headers: Record<string, string> = {};
24+
signed.headers.forEach((value: string, key: string) => {
25+
headers[key] = value;
26+
});
27+
return headers;
2228
}

src/wrapper/probe402.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/wrapper/util.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export function wsToHttp(wsUrl: string): string {
2+
return wsUrl.replace(/^wss:\/\//, "https://").replace(/^ws:\/\//, "http://");
3+
}
4+
5+
export async function probe402(wsUrl: string): Promise<Response> {
6+
const httpUrl = wsToHttp(wsUrl);
7+
8+
const response = await fetch(httpUrl);
9+
if (response.status !== 402) {
10+
throw new Error(`Expected 402 from ${httpUrl} but got ${response.status}`);
11+
}
12+
13+
return response;
14+
}

src/wrapper/x402.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
11
import type { x402Client } from "@x402/fetch";
2-
import { probe402 } from "./probe402.js";
2+
import { probe402 } from "./util.js";
33

4-
export async function getPaymentCredentials(wsUrl: string, client: x402Client): Promise<Record<string, string>> {
5-
const { x402HTTPClient } = await import("@x402/fetch");
6-
const httpClient = new x402HTTPClient(client);
4+
export async function getPaymentCredentials(
5+
wsUrl: string,
6+
client: x402Client
7+
): Promise<Record<string, string>> {
8+
const { x402HTTPClient } = await import("@x402/fetch");
9+
const httpClient = new x402HTTPClient(client);
710

8-
const response = await probe402(wsUrl);
11+
const response = await probe402(wsUrl);
912

10-
let body: unknown;
11-
try {
12-
body = JSON.parse(await response.text());
13-
} catch {
14-
body = undefined;
15-
}
13+
let body: unknown;
14+
try {
15+
body = JSON.parse(await response.text());
16+
} catch {
17+
body = undefined;
18+
}
1619

17-
const getHeader = (name: string) => response.headers.get(name);
18-
const paymentRequired = httpClient.getPaymentRequiredResponse(getHeader, body);
20+
const getHeader = (name: string) => response.headers.get(name);
21+
const paymentRequired = httpClient.getPaymentRequiredResponse(
22+
getHeader,
23+
body
24+
);
1925

20-
const paymentPayload = await httpClient.createPaymentPayload(paymentRequired);
21-
return httpClient.encodePaymentSignatureHeader(paymentPayload);
26+
const paymentPayload = await httpClient.createPaymentPayload(paymentRequired);
27+
return httpClient.encodePaymentSignatureHeader(paymentPayload);
2228
}

0 commit comments

Comments
 (0)