@integraledger/profile-facilitator 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +1 -1
  2. package/dist/index.js +119 -108
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -384,7 +384,7 @@ The package's entry point exports:
384
384
 
385
385
  | Export | Kind | What it is |
386
386
  |---|---|---|
387
- | `serveProfileFacilitator(config)` | `(c: ProfileFacilitatorConfig) => Promise<{ close(): Promise<void> }>` | Opens the store, starts the HTTP server and resolves once it listens. |
387
+ | `serveProfileFacilitator(config)` | `(c: ProfileFacilitatorConfig) => Promise<{ close(): Promise<void> }>` | Opens the store, starts the HTTP server and resolves once it listens. If it cannot start, it closes the store and rejects with the error that stopped it, such as the listen's `EADDRINUSE`. |
388
388
  | `ProfileFacilitatorConfig` | interface | The configuration above. |
389
389
 
390
390
  ### HTTP API
package/dist/index.js CHANGED
@@ -35,120 +35,131 @@ export async function serveProfileFacilitator(c) {
35
35
  const settleWaitMs = Number.isFinite(c.settleWaitMs) && c.settleWaitMs > 0 ? c.settleWaitMs : DEFAULT_SETTLE_WAIT_MS;
36
36
  const log = guarded(c.operatorLog ?? operatorLine);
37
37
  const store = await openStore(c.store.url, log);
38
- const tron = createTron((c.tron ?? []).map((n) => ({ ...n, fullNode: trimmed(n.fullNode), solidityNode: trimmed(n.solidityNode) })), store, settleWaitMs, log);
39
- const polkadot = createPolkadot((c.polkadot ?? []).map((n) => ({ ...n, rpc: trimmed(n.rpc) })), store, settleWaitMs, log);
40
- const supported = {
41
- kinds: [
42
- ...tron.networks.map((network) => ({
43
- x402Version: 2,
44
- scheme: "exact",
45
- network,
46
- extra: { assetTransferMethod: LCP_TRC20_MEMO },
47
- })),
48
- ...polkadot.networks.map((network) => ({
49
- x402Version: 2,
50
- scheme: "exact",
51
- network,
52
- extra: { assetTransferMethod: LCP_ASSETS_REMARK },
53
- })),
54
- ],
55
- extensions: [],
56
- signers: {},
57
- };
58
- function railOf(r) {
59
- const n = r.paymentRequirements.network;
60
- if (tron.networks.includes(n))
61
- return tron;
62
- if (polkadot.networks.includes(n))
63
- return polkadot;
64
- return undefined;
65
- }
66
- async function verify(body) {
67
- const r = readRequest(body);
68
- if (typeof r === "string")
69
- return { isValid: false, invalidReason: r };
70
- const rail = railOf(r);
71
- if (rail === undefined)
72
- return { isValid: false, invalidReason: "invalid_network" };
73
- try {
74
- return await rail.verify(r);
75
- }
76
- catch (e) {
77
- return invalid(unexpectedVerify(log, { event: "verify-error", network: r.paymentRequirements.network }, e));
38
+ // If the facilitator does not start, what it opened is closed before its error goes to the caller unchanged: the
39
+ // store's pool and sweep, and the Polkadot rail, which opens a node's client only on that node's first request.
40
+ let closeNodes = async () => { };
41
+ try {
42
+ const tron = createTron((c.tron ?? []).map((n) => ({ ...n, fullNode: trimmed(n.fullNode), solidityNode: trimmed(n.solidityNode) })), store, settleWaitMs, log);
43
+ const polkadot = createPolkadot((c.polkadot ?? []).map((n) => ({ ...n, rpc: trimmed(n.rpc) })), store, settleWaitMs, log);
44
+ closeNodes = () => polkadot.close();
45
+ const supported = {
46
+ kinds: [
47
+ ...tron.networks.map((network) => ({
48
+ x402Version: 2,
49
+ scheme: "exact",
50
+ network,
51
+ extra: { assetTransferMethod: LCP_TRC20_MEMO },
52
+ })),
53
+ ...polkadot.networks.map((network) => ({
54
+ x402Version: 2,
55
+ scheme: "exact",
56
+ network,
57
+ extra: { assetTransferMethod: LCP_ASSETS_REMARK },
58
+ })),
59
+ ],
60
+ extensions: [],
61
+ signers: {},
62
+ };
63
+ function railOf(r) {
64
+ const n = r.paymentRequirements.network;
65
+ if (tron.networks.includes(n))
66
+ return tron;
67
+ if (polkadot.networks.includes(n))
68
+ return polkadot;
69
+ return undefined;
78
70
  }
79
- }
80
- async function settle(body) {
81
- const r = readRequest(body);
82
- if (typeof r === "string")
83
- return failed(r, "", networkOf(body));
84
- const network = r.paymentRequirements.network;
85
- const rail = railOf(r);
86
- if (rail === undefined)
87
- return failed("invalid_network", "", network);
88
- try {
89
- return await rail.settle(r);
71
+ async function verify(body) {
72
+ const r = readRequest(body);
73
+ if (typeof r === "string")
74
+ return { isValid: false, invalidReason: r };
75
+ const rail = railOf(r);
76
+ if (rail === undefined)
77
+ return { isValid: false, invalidReason: "invalid_network" };
78
+ try {
79
+ return await rail.verify(r);
80
+ }
81
+ catch (e) {
82
+ return invalid(unexpectedVerify(log, { event: "verify-error", network: r.paymentRequirements.network }, e));
83
+ }
90
84
  }
91
- catch (e) {
92
- return unexpectedSettle(log, { event: "settle-error", network }, e);
85
+ async function settle(body) {
86
+ const r = readRequest(body);
87
+ if (typeof r === "string")
88
+ return failed(r, "", networkOf(body));
89
+ const network = r.paymentRequirements.network;
90
+ const rail = railOf(r);
91
+ if (rail === undefined)
92
+ return failed("invalid_network", "", network);
93
+ try {
94
+ return await rail.settle(r);
95
+ }
96
+ catch (e) {
97
+ return unexpectedSettle(log, { event: "settle-error", network }, e);
98
+ }
93
99
  }
94
- }
95
- async function handle(req, res) {
96
- const path = (req.url ?? "").split("?")[0];
97
- if (path === "/supported") {
98
- if (req.method !== "GET")
100
+ async function handle(req, res) {
101
+ const path = (req.url ?? "").split("?")[0];
102
+ if (path === "/supported") {
103
+ if (req.method !== "GET")
104
+ return sendJson(res, 405, { error: "method not allowed" });
105
+ return sendJson(res, 200, supported);
106
+ }
107
+ if (path !== "/verify" && path !== "/settle")
108
+ return sendJson(res, 404, { error: "not found" });
109
+ if (req.method !== "POST")
99
110
  return sendJson(res, 405, { error: "method not allowed" });
100
- return sendJson(res, 200, supported);
101
- }
102
- if (path !== "/verify" && path !== "/settle")
103
- return sendJson(res, 404, { error: "not found" });
104
- if (req.method !== "POST")
105
- return sendJson(res, 405, { error: "method not allowed" });
106
- const read = await readRequestJson(req);
107
- if (read !== undefined && "tooLarge" in read) {
108
- res.setHeader("connection", "close");
109
- sendJson(res, 413, { error: "the body is larger than 64 KiB" });
110
- req.destroy();
111
- return;
112
- }
113
- if (read !== undefined && "left" in read) {
114
- // The client left before its body was complete: there is no one to answer, and nothing failed here to log.
115
- res.destroy();
116
- return;
117
- }
118
- if (path === "/verify") {
111
+ const read = await readRequestJson(req);
112
+ if (read !== undefined && "tooLarge" in read) {
113
+ res.setHeader("connection", "close");
114
+ sendJson(res, 413, { error: "the body is larger than 64 KiB" });
115
+ req.destroy();
116
+ return;
117
+ }
118
+ if (read !== undefined && "left" in read) {
119
+ // The client left before its body was complete: there is no one to answer, and nothing failed here to log.
120
+ res.destroy();
121
+ return;
122
+ }
123
+ if (path === "/verify") {
124
+ if (read === undefined)
125
+ return sendJson(res, 400, { isValid: false, invalidReason: "invalid_payload" });
126
+ return sendJson(res, 200, await verify(read.json));
127
+ }
119
128
  if (read === undefined)
120
- return sendJson(res, 400, { isValid: false, invalidReason: "invalid_payload" });
121
- return sendJson(res, 200, await verify(read.json));
129
+ return sendJson(res, 400, failed("invalid_payload", "", ""));
130
+ return sendJson(res, 200, await settle(read.json));
122
131
  }
123
- if (read === undefined)
124
- return sendJson(res, 400, failed("invalid_payload", "", ""));
125
- return sendJson(res, 200, await settle(read.json));
126
- }
127
- const server = createServer((req, res) => {
128
- handle(req, res).catch((e) => {
129
- log({ event: "request-error", path: (req.url ?? "").split("?")[0] }, e);
130
- if (!res.headersSent)
131
- sendJson(res, 500, { error: "internal error" });
132
- else
133
- res.destroy();
134
- });
135
- });
136
- server.requestTimeout = settleWaitMs + 60_000;
137
- await new Promise((resolve, reject) => {
138
- server.once("error", reject);
139
- server.listen(port, host, () => {
140
- server.off("error", reject);
141
- resolve();
132
+ const server = createServer((req, res) => {
133
+ handle(req, res).catch((e) => {
134
+ log({ event: "request-error", path: (req.url ?? "").split("?")[0] }, e);
135
+ if (!res.headersSent)
136
+ sendJson(res, 500, { error: "internal error" });
137
+ else
138
+ res.destroy();
139
+ });
142
140
  });
143
- });
144
- return {
145
- async close() {
146
- await new Promise((resolve) => {
147
- server.close(() => resolve());
148
- server.closeAllConnections();
141
+ server.requestTimeout = settleWaitMs + 60_000;
142
+ await new Promise((resolve, reject) => {
143
+ server.once("error", reject);
144
+ server.listen(port, host, () => {
145
+ server.off("error", reject);
146
+ resolve();
149
147
  });
150
- await polkadot.close();
151
- await store.close();
152
- },
153
- };
148
+ });
149
+ return {
150
+ async close() {
151
+ await new Promise((resolve) => {
152
+ server.close(() => resolve());
153
+ server.closeAllConnections();
154
+ });
155
+ await polkadot.close();
156
+ await store.close();
157
+ },
158
+ };
159
+ }
160
+ catch (e) {
161
+ await closeNodes();
162
+ await store.close();
163
+ throw e;
164
+ }
154
165
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@integraledger/profile-facilitator",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "An x402 facilitator for the LCP profiles x402/exact/tron/lcp-trc20-memo and x402/exact/polkadot/lcp-assets-remark: verifies and settles payments whose signed transaction carries the ATR hash.",
5
5
  "license": "Apache-2.0",
6
6
  "engines": {