@parallel-protocol/mpp 0.2.0 → 0.2.2
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.
- package/README.md +17 -8
- package/dist/{chunk-MEDPOLT4.cjs → chunk-76KPBYGA.cjs} +192 -16
- package/dist/{chunk-CQ62EEHN.js → chunk-BYNIZG5S.js} +190 -17
- package/dist/express.cjs +30 -5
- package/dist/express.d.cts +2 -1
- package/dist/express.d.ts +2 -1
- package/dist/express.js +29 -4
- package/dist/fastify.cjs +70 -7
- package/dist/fastify.d.cts +1 -1
- package/dist/fastify.d.ts +1 -1
- package/dist/fastify.js +67 -4
- package/dist/hono.cjs +20 -9
- package/dist/hono.d.cts +1 -1
- package/dist/hono.d.ts +1 -1
- package/dist/hono.js +19 -8
- package/dist/index.cjs +13 -13
- package/dist/index.d.cts +6 -3
- package/dist/index.d.ts +6 -3
- package/dist/index.js +1 -1
- package/dist/next.cjs +8 -5
- package/dist/next.d.cts +1 -1
- package/dist/next.d.ts +1 -1
- package/dist/next.js +5 -2
- package/dist/{types-CCE-FI8i.d.cts → types-BSzM92sD.d.cts} +5 -0
- package/dist/{types-CCE-FI8i.d.ts → types-BSzM92sD.d.ts} +5 -0
- package/package.json +4 -3
package/dist/express.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var chunkRNM3RDKR_cjs = require('./chunk-RNM3RDKR.cjs');
|
|
4
|
-
var
|
|
4
|
+
var chunk76KPBYGA_cjs = require('./chunk-76KPBYGA.cjs');
|
|
5
5
|
|
|
6
6
|
// src/express/adapter.ts
|
|
7
7
|
var TIMEOUT_BODY = JSON.stringify({
|
|
@@ -27,15 +27,28 @@ var ExpressAdapter = class {
|
|
|
27
27
|
getPath() {
|
|
28
28
|
return this.req.path;
|
|
29
29
|
}
|
|
30
|
+
getFullPath() {
|
|
31
|
+
return this.req.baseUrl + this.req.path;
|
|
32
|
+
}
|
|
30
33
|
getUrl() {
|
|
31
34
|
return `${this.req.protocol}://${this.req.headers.host}${this.req.originalUrl}`;
|
|
32
35
|
}
|
|
33
36
|
async runHandler() {
|
|
34
37
|
const res = this.res;
|
|
35
38
|
const next = this.next;
|
|
39
|
+
chunk76KPBYGA_cjs.dropConditionalHeaders(this.req.headers);
|
|
36
40
|
const originalWriteHead = res.writeHead.bind(res);
|
|
37
41
|
const originalWrite = res.write.bind(res);
|
|
38
42
|
const originalEnd = res.end.bind(res);
|
|
43
|
+
const headersBefore = structuredClone(res.getHeaders());
|
|
44
|
+
const dropHandlerHeaders = () => {
|
|
45
|
+
for (const name of res.getHeaderNames())
|
|
46
|
+
if (!Object.hasOwn(headersBefore, name)) res.removeHeader(name);
|
|
47
|
+
for (const [name, value] of Object.entries(headersBefore))
|
|
48
|
+
if (value !== void 0) res.setHeader(name, value);
|
|
49
|
+
res.statusMessage = "";
|
|
50
|
+
};
|
|
51
|
+
const nodeWriters = Object.getPrototypeOf(res);
|
|
39
52
|
const buffered = [];
|
|
40
53
|
let settled = false;
|
|
41
54
|
let discarded = false;
|
|
@@ -84,8 +97,17 @@ var ExpressAdapter = class {
|
|
|
84
97
|
},
|
|
85
98
|
sendResponse: () => {
|
|
86
99
|
if (res.headersSent) return;
|
|
87
|
-
|
|
88
|
-
|
|
100
|
+
dropHandlerHeaders();
|
|
101
|
+
nodeWriters.writeHead.call(res, 504, {
|
|
102
|
+
"Content-Type": "application/json",
|
|
103
|
+
"Content-Length": Buffer.byteLength(TIMEOUT_BODY)
|
|
104
|
+
});
|
|
105
|
+
nodeWriters.end.call(res, TIMEOUT_BODY, "utf8");
|
|
106
|
+
res.setHeader = (() => res);
|
|
107
|
+
res.setHeaders = (() => res);
|
|
108
|
+
res.appendHeader = (() => res);
|
|
109
|
+
res.removeHeader = () => {
|
|
110
|
+
};
|
|
89
111
|
},
|
|
90
112
|
discardResponse: () => {
|
|
91
113
|
}
|
|
@@ -105,7 +127,9 @@ var ExpressAdapter = class {
|
|
|
105
127
|
},
|
|
106
128
|
discardResponse: () => {
|
|
107
129
|
settled = true;
|
|
108
|
-
|
|
130
|
+
dropHandlerHeaders();
|
|
131
|
+
for (const name of ["writeHead", "write", "end"])
|
|
132
|
+
Reflect.deleteProperty(res, name);
|
|
109
133
|
}
|
|
110
134
|
};
|
|
111
135
|
}
|
|
@@ -113,11 +137,12 @@ var ExpressAdapter = class {
|
|
|
113
137
|
|
|
114
138
|
// src/express/index.ts
|
|
115
139
|
function paymentMiddleware(config) {
|
|
116
|
-
const middleware =
|
|
140
|
+
const middleware = chunk76KPBYGA_cjs.createMppPaymentMiddleware(config);
|
|
117
141
|
return async (req, res, next) => {
|
|
118
142
|
const result = await middleware(new ExpressAdapter(req, res, next));
|
|
119
143
|
if ("pass" in result) {
|
|
120
144
|
if (result.handlerRan) return;
|
|
145
|
+
if (result.preflight) chunk76KPBYGA_cjs.answerAsPreflight(res);
|
|
121
146
|
return next();
|
|
122
147
|
}
|
|
123
148
|
for (const [key, value] of Object.entries(result.headers)) {
|
package/dist/express.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Request, Response, NextFunction } from 'express';
|
|
2
|
-
import { H as HTTPAdapter, R as RunHandlerResult, M as MppMiddlewareConfig } from './types-
|
|
2
|
+
import { H as HTTPAdapter, R as RunHandlerResult, M as MppMiddlewareConfig } from './types-BSzM92sD.cjs';
|
|
3
3
|
import '@parallel-protocol/mpp-types';
|
|
4
4
|
import 'viem';
|
|
5
5
|
|
|
@@ -11,6 +11,7 @@ declare class ExpressAdapter implements HTTPAdapter {
|
|
|
11
11
|
getHeader(name: string): string | undefined;
|
|
12
12
|
getMethod(): string;
|
|
13
13
|
getPath(): string;
|
|
14
|
+
getFullPath(): string;
|
|
14
15
|
getUrl(): string;
|
|
15
16
|
runHandler(): Promise<RunHandlerResult>;
|
|
16
17
|
}
|
package/dist/express.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Request, Response, NextFunction } from 'express';
|
|
2
|
-
import { H as HTTPAdapter, R as RunHandlerResult, M as MppMiddlewareConfig } from './types-
|
|
2
|
+
import { H as HTTPAdapter, R as RunHandlerResult, M as MppMiddlewareConfig } from './types-BSzM92sD.js';
|
|
3
3
|
import '@parallel-protocol/mpp-types';
|
|
4
4
|
import 'viem';
|
|
5
5
|
|
|
@@ -11,6 +11,7 @@ declare class ExpressAdapter implements HTTPAdapter {
|
|
|
11
11
|
getHeader(name: string): string | undefined;
|
|
12
12
|
getMethod(): string;
|
|
13
13
|
getPath(): string;
|
|
14
|
+
getFullPath(): string;
|
|
14
15
|
getUrl(): string;
|
|
15
16
|
runHandler(): Promise<RunHandlerResult>;
|
|
16
17
|
}
|
package/dist/express.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { raceTimeout, HANDLER_TIMEOUT } from './chunk-DXHOWEAQ.js';
|
|
2
|
-
import { createMppPaymentMiddleware } from './chunk-
|
|
2
|
+
import { dropConditionalHeaders, createMppPaymentMiddleware, answerAsPreflight } from './chunk-BYNIZG5S.js';
|
|
3
3
|
|
|
4
4
|
// src/express/adapter.ts
|
|
5
5
|
var TIMEOUT_BODY = JSON.stringify({
|
|
@@ -25,15 +25,28 @@ var ExpressAdapter = class {
|
|
|
25
25
|
getPath() {
|
|
26
26
|
return this.req.path;
|
|
27
27
|
}
|
|
28
|
+
getFullPath() {
|
|
29
|
+
return this.req.baseUrl + this.req.path;
|
|
30
|
+
}
|
|
28
31
|
getUrl() {
|
|
29
32
|
return `${this.req.protocol}://${this.req.headers.host}${this.req.originalUrl}`;
|
|
30
33
|
}
|
|
31
34
|
async runHandler() {
|
|
32
35
|
const res = this.res;
|
|
33
36
|
const next = this.next;
|
|
37
|
+
dropConditionalHeaders(this.req.headers);
|
|
34
38
|
const originalWriteHead = res.writeHead.bind(res);
|
|
35
39
|
const originalWrite = res.write.bind(res);
|
|
36
40
|
const originalEnd = res.end.bind(res);
|
|
41
|
+
const headersBefore = structuredClone(res.getHeaders());
|
|
42
|
+
const dropHandlerHeaders = () => {
|
|
43
|
+
for (const name of res.getHeaderNames())
|
|
44
|
+
if (!Object.hasOwn(headersBefore, name)) res.removeHeader(name);
|
|
45
|
+
for (const [name, value] of Object.entries(headersBefore))
|
|
46
|
+
if (value !== void 0) res.setHeader(name, value);
|
|
47
|
+
res.statusMessage = "";
|
|
48
|
+
};
|
|
49
|
+
const nodeWriters = Object.getPrototypeOf(res);
|
|
37
50
|
const buffered = [];
|
|
38
51
|
let settled = false;
|
|
39
52
|
let discarded = false;
|
|
@@ -82,8 +95,17 @@ var ExpressAdapter = class {
|
|
|
82
95
|
},
|
|
83
96
|
sendResponse: () => {
|
|
84
97
|
if (res.headersSent) return;
|
|
85
|
-
|
|
86
|
-
|
|
98
|
+
dropHandlerHeaders();
|
|
99
|
+
nodeWriters.writeHead.call(res, 504, {
|
|
100
|
+
"Content-Type": "application/json",
|
|
101
|
+
"Content-Length": Buffer.byteLength(TIMEOUT_BODY)
|
|
102
|
+
});
|
|
103
|
+
nodeWriters.end.call(res, TIMEOUT_BODY, "utf8");
|
|
104
|
+
res.setHeader = (() => res);
|
|
105
|
+
res.setHeaders = (() => res);
|
|
106
|
+
res.appendHeader = (() => res);
|
|
107
|
+
res.removeHeader = () => {
|
|
108
|
+
};
|
|
87
109
|
},
|
|
88
110
|
discardResponse: () => {
|
|
89
111
|
}
|
|
@@ -103,7 +125,9 @@ var ExpressAdapter = class {
|
|
|
103
125
|
},
|
|
104
126
|
discardResponse: () => {
|
|
105
127
|
settled = true;
|
|
106
|
-
|
|
128
|
+
dropHandlerHeaders();
|
|
129
|
+
for (const name of ["writeHead", "write", "end"])
|
|
130
|
+
Reflect.deleteProperty(res, name);
|
|
107
131
|
}
|
|
108
132
|
};
|
|
109
133
|
}
|
|
@@ -116,6 +140,7 @@ function paymentMiddleware(config) {
|
|
|
116
140
|
const result = await middleware(new ExpressAdapter(req, res, next));
|
|
117
141
|
if ("pass" in result) {
|
|
118
142
|
if (result.handlerRan) return;
|
|
143
|
+
if (result.preflight) answerAsPreflight(res);
|
|
119
144
|
return next();
|
|
120
145
|
}
|
|
121
146
|
for (const [key, value] of Object.entries(result.headers)) {
|
package/dist/fastify.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var chunk76KPBYGA_cjs = require('./chunk-76KPBYGA.cjs');
|
|
4
4
|
var mppTypes = require('@parallel-protocol/mpp-types');
|
|
5
5
|
|
|
6
6
|
// src/fastify/adapter.ts
|
|
@@ -25,25 +25,85 @@ var FastifyAdapter = class {
|
|
|
25
25
|
};
|
|
26
26
|
|
|
27
27
|
// src/fastify/index.ts
|
|
28
|
+
function refuseRawResponse() {
|
|
29
|
+
throw new Error(
|
|
30
|
+
"mpp: raw responses are not supported on paid routes. Return the body from the handler so the payment can settle"
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
function keepCookiesBefore(reply, cookies) {
|
|
34
|
+
const raw = reply.raw;
|
|
35
|
+
const writeHead = raw.writeHead;
|
|
36
|
+
raw.writeHead = ((...args) => {
|
|
37
|
+
for (const arg of args)
|
|
38
|
+
if (typeof arg === "object" && arg !== null && !Array.isArray(arg))
|
|
39
|
+
delete arg["set-cookie"];
|
|
40
|
+
raw.removeHeader("set-cookie");
|
|
41
|
+
if (cookies !== void 0) raw.setHeader("set-cookie", cookies);
|
|
42
|
+
return writeHead.apply(raw, args);
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
function release(payload) {
|
|
46
|
+
if (payload instanceof ReadableStream) payload.cancel().catch(() => {
|
|
47
|
+
});
|
|
48
|
+
else if (typeof payload?.destroy === "function")
|
|
49
|
+
payload.destroy();
|
|
50
|
+
}
|
|
51
|
+
function restoreReply(reply, before) {
|
|
52
|
+
for (const name of Object.keys(reply.getHeaders())) reply.removeHeader(name);
|
|
53
|
+
for (const [name, value] of Object.entries(before.headers))
|
|
54
|
+
if (value !== void 0) reply.header(name, value);
|
|
55
|
+
for (const name of before.trailers) reply.removeTrailer(name);
|
|
56
|
+
for (const name of ["writeHead", "write", "end"])
|
|
57
|
+
Reflect.deleteProperty(reply.raw, name);
|
|
58
|
+
keepCookiesBefore(reply, before.headers["set-cookie"]);
|
|
59
|
+
reply.raw.statusMessage = "";
|
|
60
|
+
}
|
|
28
61
|
function paymentMiddleware(config) {
|
|
29
62
|
return async (fastify) => {
|
|
30
|
-
const gate =
|
|
63
|
+
const gate = chunk76KPBYGA_cjs.createMppPaymentGate(config);
|
|
31
64
|
const settleMap = /* @__PURE__ */ new WeakMap();
|
|
65
|
+
const replyBefore = /* @__PURE__ */ new WeakMap();
|
|
66
|
+
const rawWriters = /* @__PURE__ */ new WeakMap();
|
|
32
67
|
fastify.addHook("onRequest", async (req, reply) => {
|
|
33
68
|
const gateResult = await gate(new FastifyAdapter(req));
|
|
34
|
-
if (gateResult.type === "pass")
|
|
69
|
+
if (gateResult.type === "pass") {
|
|
70
|
+
if (gateResult.preflight) chunk76KPBYGA_cjs.answerAsPreflight(reply.raw);
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
35
73
|
if (gateResult.type === "error") {
|
|
36
74
|
for (const [key, value] of Object.entries(gateResult.result.headers)) {
|
|
37
75
|
reply.header(key, value);
|
|
38
76
|
}
|
|
39
|
-
reply.status(gateResult.result.status).send(gateResult.result.body);
|
|
40
|
-
return;
|
|
77
|
+
return reply.status(gateResult.result.status).send(gateResult.result.body);
|
|
41
78
|
}
|
|
42
79
|
settleMap.set(req, gateResult.settle);
|
|
80
|
+
chunk76KPBYGA_cjs.dropConditionalHeaders(req.headers);
|
|
81
|
+
const trailers = /* @__PURE__ */ new Set();
|
|
82
|
+
const headers = structuredClone(reply.getHeaders());
|
|
83
|
+
replyBefore.set(req, { headers, trailers });
|
|
84
|
+
const trailer = reply.trailer.bind(reply);
|
|
85
|
+
reply.trailer = ((name, fn) => {
|
|
86
|
+
trailers.add(name.toLowerCase());
|
|
87
|
+
return trailer(name, fn);
|
|
88
|
+
});
|
|
89
|
+
const raw = reply.raw;
|
|
90
|
+
rawWriters.set(req, {
|
|
91
|
+
write: raw.write,
|
|
92
|
+
end: raw.end,
|
|
93
|
+
writeHead: raw.writeHead
|
|
94
|
+
});
|
|
95
|
+
raw.write = refuseRawResponse;
|
|
96
|
+
raw.end = refuseRawResponse;
|
|
97
|
+
raw.writeHead = refuseRawResponse;
|
|
43
98
|
});
|
|
44
99
|
fastify.addHook("onSend", async (req, reply, payload) => {
|
|
100
|
+
const writers = rawWriters.get(req);
|
|
101
|
+
if (writers) {
|
|
102
|
+
Object.assign(reply.raw, writers);
|
|
103
|
+
rawWriters.delete(req);
|
|
104
|
+
}
|
|
45
105
|
const settle = settleMap.get(req);
|
|
46
|
-
if (!settle || !
|
|
106
|
+
if (!settle || !chunk76KPBYGA_cjs.isSuccessStatus(reply.statusCode)) {
|
|
47
107
|
return payload;
|
|
48
108
|
}
|
|
49
109
|
let settled = false;
|
|
@@ -60,7 +120,10 @@ function paymentMiddleware(config) {
|
|
|
60
120
|
} catch {
|
|
61
121
|
}
|
|
62
122
|
if (!settled) {
|
|
63
|
-
|
|
123
|
+
release(payload);
|
|
124
|
+
const before = replyBefore.get(req);
|
|
125
|
+
if (before) restoreReply(reply, before);
|
|
126
|
+
const body = JSON.stringify(chunk76KPBYGA_cjs.SETTLE_FAILURE_BODY);
|
|
64
127
|
reply.status(402);
|
|
65
128
|
reply.header("Content-Type", "application/json");
|
|
66
129
|
reply.header("Content-Length", Buffer.byteLength(body));
|
package/dist/fastify.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { FastifyRequest, FastifyPluginAsync } from 'fastify';
|
|
2
|
-
import { H as HTTPAdapter, M as MppMiddlewareConfig } from './types-
|
|
2
|
+
import { H as HTTPAdapter, M as MppMiddlewareConfig } from './types-BSzM92sD.cjs';
|
|
3
3
|
import '@parallel-protocol/mpp-types';
|
|
4
4
|
import 'viem';
|
|
5
5
|
|
package/dist/fastify.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { FastifyRequest, FastifyPluginAsync } from 'fastify';
|
|
2
|
-
import { H as HTTPAdapter, M as MppMiddlewareConfig } from './types-
|
|
2
|
+
import { H as HTTPAdapter, M as MppMiddlewareConfig } from './types-BSzM92sD.js';
|
|
3
3
|
import '@parallel-protocol/mpp-types';
|
|
4
4
|
import 'viem';
|
|
5
5
|
|
package/dist/fastify.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createMppPaymentGate, isSuccessStatus, SETTLE_FAILURE_BODY } from './chunk-
|
|
1
|
+
import { createMppPaymentGate, answerAsPreflight, dropConditionalHeaders, isSuccessStatus, SETTLE_FAILURE_BODY } from './chunk-BYNIZG5S.js';
|
|
2
2
|
import { encodeBase64Url } from '@parallel-protocol/mpp-types';
|
|
3
3
|
|
|
4
4
|
// src/fastify/adapter.ts
|
|
@@ -23,23 +23,83 @@ var FastifyAdapter = class {
|
|
|
23
23
|
};
|
|
24
24
|
|
|
25
25
|
// src/fastify/index.ts
|
|
26
|
+
function refuseRawResponse() {
|
|
27
|
+
throw new Error(
|
|
28
|
+
"mpp: raw responses are not supported on paid routes. Return the body from the handler so the payment can settle"
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
function keepCookiesBefore(reply, cookies) {
|
|
32
|
+
const raw = reply.raw;
|
|
33
|
+
const writeHead = raw.writeHead;
|
|
34
|
+
raw.writeHead = ((...args) => {
|
|
35
|
+
for (const arg of args)
|
|
36
|
+
if (typeof arg === "object" && arg !== null && !Array.isArray(arg))
|
|
37
|
+
delete arg["set-cookie"];
|
|
38
|
+
raw.removeHeader("set-cookie");
|
|
39
|
+
if (cookies !== void 0) raw.setHeader("set-cookie", cookies);
|
|
40
|
+
return writeHead.apply(raw, args);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
function release(payload) {
|
|
44
|
+
if (payload instanceof ReadableStream) payload.cancel().catch(() => {
|
|
45
|
+
});
|
|
46
|
+
else if (typeof payload?.destroy === "function")
|
|
47
|
+
payload.destroy();
|
|
48
|
+
}
|
|
49
|
+
function restoreReply(reply, before) {
|
|
50
|
+
for (const name of Object.keys(reply.getHeaders())) reply.removeHeader(name);
|
|
51
|
+
for (const [name, value] of Object.entries(before.headers))
|
|
52
|
+
if (value !== void 0) reply.header(name, value);
|
|
53
|
+
for (const name of before.trailers) reply.removeTrailer(name);
|
|
54
|
+
for (const name of ["writeHead", "write", "end"])
|
|
55
|
+
Reflect.deleteProperty(reply.raw, name);
|
|
56
|
+
keepCookiesBefore(reply, before.headers["set-cookie"]);
|
|
57
|
+
reply.raw.statusMessage = "";
|
|
58
|
+
}
|
|
26
59
|
function paymentMiddleware(config) {
|
|
27
60
|
return async (fastify) => {
|
|
28
61
|
const gate = createMppPaymentGate(config);
|
|
29
62
|
const settleMap = /* @__PURE__ */ new WeakMap();
|
|
63
|
+
const replyBefore = /* @__PURE__ */ new WeakMap();
|
|
64
|
+
const rawWriters = /* @__PURE__ */ new WeakMap();
|
|
30
65
|
fastify.addHook("onRequest", async (req, reply) => {
|
|
31
66
|
const gateResult = await gate(new FastifyAdapter(req));
|
|
32
|
-
if (gateResult.type === "pass")
|
|
67
|
+
if (gateResult.type === "pass") {
|
|
68
|
+
if (gateResult.preflight) answerAsPreflight(reply.raw);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
33
71
|
if (gateResult.type === "error") {
|
|
34
72
|
for (const [key, value] of Object.entries(gateResult.result.headers)) {
|
|
35
73
|
reply.header(key, value);
|
|
36
74
|
}
|
|
37
|
-
reply.status(gateResult.result.status).send(gateResult.result.body);
|
|
38
|
-
return;
|
|
75
|
+
return reply.status(gateResult.result.status).send(gateResult.result.body);
|
|
39
76
|
}
|
|
40
77
|
settleMap.set(req, gateResult.settle);
|
|
78
|
+
dropConditionalHeaders(req.headers);
|
|
79
|
+
const trailers = /* @__PURE__ */ new Set();
|
|
80
|
+
const headers = structuredClone(reply.getHeaders());
|
|
81
|
+
replyBefore.set(req, { headers, trailers });
|
|
82
|
+
const trailer = reply.trailer.bind(reply);
|
|
83
|
+
reply.trailer = ((name, fn) => {
|
|
84
|
+
trailers.add(name.toLowerCase());
|
|
85
|
+
return trailer(name, fn);
|
|
86
|
+
});
|
|
87
|
+
const raw = reply.raw;
|
|
88
|
+
rawWriters.set(req, {
|
|
89
|
+
write: raw.write,
|
|
90
|
+
end: raw.end,
|
|
91
|
+
writeHead: raw.writeHead
|
|
92
|
+
});
|
|
93
|
+
raw.write = refuseRawResponse;
|
|
94
|
+
raw.end = refuseRawResponse;
|
|
95
|
+
raw.writeHead = refuseRawResponse;
|
|
41
96
|
});
|
|
42
97
|
fastify.addHook("onSend", async (req, reply, payload) => {
|
|
98
|
+
const writers = rawWriters.get(req);
|
|
99
|
+
if (writers) {
|
|
100
|
+
Object.assign(reply.raw, writers);
|
|
101
|
+
rawWriters.delete(req);
|
|
102
|
+
}
|
|
43
103
|
const settle = settleMap.get(req);
|
|
44
104
|
if (!settle || !isSuccessStatus(reply.statusCode)) {
|
|
45
105
|
return payload;
|
|
@@ -58,6 +118,9 @@ function paymentMiddleware(config) {
|
|
|
58
118
|
} catch {
|
|
59
119
|
}
|
|
60
120
|
if (!settled) {
|
|
121
|
+
release(payload);
|
|
122
|
+
const before = replyBefore.get(req);
|
|
123
|
+
if (before) restoreReply(reply, before);
|
|
61
124
|
const body = JSON.stringify(SETTLE_FAILURE_BODY);
|
|
62
125
|
reply.status(402);
|
|
63
126
|
reply.header("Content-Type", "application/json");
|
package/dist/hono.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var chunkRNM3RDKR_cjs = require('./chunk-RNM3RDKR.cjs');
|
|
4
|
-
var
|
|
4
|
+
var chunk76KPBYGA_cjs = require('./chunk-76KPBYGA.cjs');
|
|
5
5
|
|
|
6
6
|
// src/hono/adapter.ts
|
|
7
7
|
var TIMEOUT_BODY = JSON.stringify({
|
|
@@ -28,18 +28,20 @@ var HonoAdapter = class {
|
|
|
28
28
|
return this.c.req.url;
|
|
29
29
|
}
|
|
30
30
|
async runHandler() {
|
|
31
|
+
const headersBefore = new Headers(this.c.res.headers);
|
|
32
|
+
const replace = (body, status) => {
|
|
33
|
+
const headers = new Headers(headersBefore);
|
|
34
|
+
if (body !== null) headers.set("Content-Type", "application/json");
|
|
35
|
+
this.c.res = void 0;
|
|
36
|
+
this.c.res = new Response(body, { status, headers });
|
|
37
|
+
};
|
|
31
38
|
const outcome = await chunkRNM3RDKR_cjs.raceTimeout(this.next());
|
|
32
39
|
if (outcome === chunkRNM3RDKR_cjs.HANDLER_TIMEOUT) {
|
|
33
40
|
return {
|
|
34
41
|
status: 504,
|
|
35
42
|
setResponseHeader: () => {
|
|
36
43
|
},
|
|
37
|
-
sendResponse: () =>
|
|
38
|
-
this.c.res = new Response(TIMEOUT_BODY, {
|
|
39
|
-
status: 504,
|
|
40
|
-
headers: { "Content-Type": "application/json" }
|
|
41
|
-
});
|
|
42
|
-
},
|
|
44
|
+
sendResponse: () => replace(TIMEOUT_BODY, 504),
|
|
43
45
|
discardResponse: () => {
|
|
44
46
|
}
|
|
45
47
|
};
|
|
@@ -50,7 +52,11 @@ var HonoAdapter = class {
|
|
|
50
52
|
setResponseHeader: (name, value) => res.headers.set(name, value),
|
|
51
53
|
sendResponse: () => {
|
|
52
54
|
},
|
|
55
|
+
// Cancel the body so its source stops, then drop the response.
|
|
53
56
|
discardResponse: () => {
|
|
57
|
+
res.body?.cancel().catch(() => {
|
|
58
|
+
});
|
|
59
|
+
replace(null);
|
|
54
60
|
}
|
|
55
61
|
};
|
|
56
62
|
}
|
|
@@ -58,15 +64,20 @@ var HonoAdapter = class {
|
|
|
58
64
|
|
|
59
65
|
// src/hono/index.ts
|
|
60
66
|
function paymentMiddleware(config) {
|
|
61
|
-
const middleware =
|
|
67
|
+
const middleware = chunk76KPBYGA_cjs.createMppPaymentMiddleware(config);
|
|
62
68
|
return async (c, next) => {
|
|
63
69
|
const result = await middleware(new HonoAdapter(c, next));
|
|
64
70
|
if ("pass" in result) {
|
|
65
71
|
if (result.handlerRan) return;
|
|
66
72
|
await next();
|
|
73
|
+
if (result.preflight) {
|
|
74
|
+
const init = chunk76KPBYGA_cjs.preflightInit(c.res);
|
|
75
|
+
c.res = void 0;
|
|
76
|
+
c.res = new Response(null, init);
|
|
77
|
+
}
|
|
67
78
|
return;
|
|
68
79
|
}
|
|
69
|
-
|
|
80
|
+
c.res = new Response(JSON.stringify(result.body), {
|
|
70
81
|
status: result.status,
|
|
71
82
|
headers: { ...result.headers, "Content-Type": "application/json" }
|
|
72
83
|
});
|
package/dist/hono.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Context, Next, MiddlewareHandler } from 'hono';
|
|
2
|
-
import { H as HTTPAdapter, R as RunHandlerResult, M as MppMiddlewareConfig } from './types-
|
|
2
|
+
import { H as HTTPAdapter, R as RunHandlerResult, M as MppMiddlewareConfig } from './types-BSzM92sD.cjs';
|
|
3
3
|
import '@parallel-protocol/mpp-types';
|
|
4
4
|
import 'viem';
|
|
5
5
|
|
package/dist/hono.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Context, Next, MiddlewareHandler } from 'hono';
|
|
2
|
-
import { H as HTTPAdapter, R as RunHandlerResult, M as MppMiddlewareConfig } from './types-
|
|
2
|
+
import { H as HTTPAdapter, R as RunHandlerResult, M as MppMiddlewareConfig } from './types-BSzM92sD.js';
|
|
3
3
|
import '@parallel-protocol/mpp-types';
|
|
4
4
|
import 'viem';
|
|
5
5
|
|
package/dist/hono.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { raceTimeout, HANDLER_TIMEOUT } from './chunk-DXHOWEAQ.js';
|
|
2
|
-
import { createMppPaymentMiddleware } from './chunk-
|
|
2
|
+
import { createMppPaymentMiddleware, preflightInit } from './chunk-BYNIZG5S.js';
|
|
3
3
|
|
|
4
4
|
// src/hono/adapter.ts
|
|
5
5
|
var TIMEOUT_BODY = JSON.stringify({
|
|
@@ -26,18 +26,20 @@ var HonoAdapter = class {
|
|
|
26
26
|
return this.c.req.url;
|
|
27
27
|
}
|
|
28
28
|
async runHandler() {
|
|
29
|
+
const headersBefore = new Headers(this.c.res.headers);
|
|
30
|
+
const replace = (body, status) => {
|
|
31
|
+
const headers = new Headers(headersBefore);
|
|
32
|
+
if (body !== null) headers.set("Content-Type", "application/json");
|
|
33
|
+
this.c.res = void 0;
|
|
34
|
+
this.c.res = new Response(body, { status, headers });
|
|
35
|
+
};
|
|
29
36
|
const outcome = await raceTimeout(this.next());
|
|
30
37
|
if (outcome === HANDLER_TIMEOUT) {
|
|
31
38
|
return {
|
|
32
39
|
status: 504,
|
|
33
40
|
setResponseHeader: () => {
|
|
34
41
|
},
|
|
35
|
-
sendResponse: () =>
|
|
36
|
-
this.c.res = new Response(TIMEOUT_BODY, {
|
|
37
|
-
status: 504,
|
|
38
|
-
headers: { "Content-Type": "application/json" }
|
|
39
|
-
});
|
|
40
|
-
},
|
|
42
|
+
sendResponse: () => replace(TIMEOUT_BODY, 504),
|
|
41
43
|
discardResponse: () => {
|
|
42
44
|
}
|
|
43
45
|
};
|
|
@@ -48,7 +50,11 @@ var HonoAdapter = class {
|
|
|
48
50
|
setResponseHeader: (name, value) => res.headers.set(name, value),
|
|
49
51
|
sendResponse: () => {
|
|
50
52
|
},
|
|
53
|
+
// Cancel the body so its source stops, then drop the response.
|
|
51
54
|
discardResponse: () => {
|
|
55
|
+
res.body?.cancel().catch(() => {
|
|
56
|
+
});
|
|
57
|
+
replace(null);
|
|
52
58
|
}
|
|
53
59
|
};
|
|
54
60
|
}
|
|
@@ -62,9 +68,14 @@ function paymentMiddleware(config) {
|
|
|
62
68
|
if ("pass" in result) {
|
|
63
69
|
if (result.handlerRan) return;
|
|
64
70
|
await next();
|
|
71
|
+
if (result.preflight) {
|
|
72
|
+
const init = preflightInit(c.res);
|
|
73
|
+
c.res = void 0;
|
|
74
|
+
c.res = new Response(null, init);
|
|
75
|
+
}
|
|
65
76
|
return;
|
|
66
77
|
}
|
|
67
|
-
|
|
78
|
+
c.res = new Response(JSON.stringify(result.body), {
|
|
68
79
|
status: result.status,
|
|
69
80
|
headers: { ...result.headers, "Content-Type": "application/json" }
|
|
70
81
|
});
|
package/dist/index.cjs
CHANGED
|
@@ -1,57 +1,57 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var chunk76KPBYGA_cjs = require('./chunk-76KPBYGA.cjs');
|
|
4
4
|
var mppTypes = require('@parallel-protocol/mpp-types');
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
Object.defineProperty(exports, "MPP_ERROR_CODES", {
|
|
9
9
|
enumerable: true,
|
|
10
|
-
get: function () { return
|
|
10
|
+
get: function () { return chunk76KPBYGA_cjs.MPP_ERROR_CODES; }
|
|
11
11
|
});
|
|
12
12
|
Object.defineProperty(exports, "MppConfigError", {
|
|
13
13
|
enumerable: true,
|
|
14
|
-
get: function () { return
|
|
14
|
+
get: function () { return chunk76KPBYGA_cjs.MppConfigError; }
|
|
15
15
|
});
|
|
16
16
|
Object.defineProperty(exports, "MppFacilitatorClient", {
|
|
17
17
|
enumerable: true,
|
|
18
|
-
get: function () { return
|
|
18
|
+
get: function () { return chunk76KPBYGA_cjs.MppFacilitatorClient; }
|
|
19
19
|
});
|
|
20
20
|
Object.defineProperty(exports, "MppRuntimeError", {
|
|
21
21
|
enumerable: true,
|
|
22
|
-
get: function () { return
|
|
22
|
+
get: function () { return chunk76KPBYGA_cjs.MppRuntimeError; }
|
|
23
23
|
});
|
|
24
24
|
Object.defineProperty(exports, "buildChallenge", {
|
|
25
25
|
enumerable: true,
|
|
26
|
-
get: function () { return
|
|
26
|
+
get: function () { return chunk76KPBYGA_cjs.buildChallenge; }
|
|
27
27
|
});
|
|
28
28
|
Object.defineProperty(exports, "chainIdFromSlug", {
|
|
29
29
|
enumerable: true,
|
|
30
|
-
get: function () { return
|
|
30
|
+
get: function () { return chunk76KPBYGA_cjs.chainIdFromSlug; }
|
|
31
31
|
});
|
|
32
32
|
Object.defineProperty(exports, "createMppPaymentGate", {
|
|
33
33
|
enumerable: true,
|
|
34
|
-
get: function () { return
|
|
34
|
+
get: function () { return chunk76KPBYGA_cjs.createMppPaymentGate; }
|
|
35
35
|
});
|
|
36
36
|
Object.defineProperty(exports, "createMppPaymentMiddleware", {
|
|
37
37
|
enumerable: true,
|
|
38
|
-
get: function () { return
|
|
38
|
+
get: function () { return chunk76KPBYGA_cjs.createMppPaymentMiddleware; }
|
|
39
39
|
});
|
|
40
40
|
Object.defineProperty(exports, "matchRoute", {
|
|
41
41
|
enumerable: true,
|
|
42
|
-
get: function () { return
|
|
42
|
+
get: function () { return chunk76KPBYGA_cjs.matchRoute; }
|
|
43
43
|
});
|
|
44
44
|
Object.defineProperty(exports, "parseAuthorizationHeader", {
|
|
45
45
|
enumerable: true,
|
|
46
|
-
get: function () { return
|
|
46
|
+
get: function () { return chunk76KPBYGA_cjs.parseAuthorizationHeader; }
|
|
47
47
|
});
|
|
48
48
|
Object.defineProperty(exports, "parsePrice", {
|
|
49
49
|
enumerable: true,
|
|
50
|
-
get: function () { return
|
|
50
|
+
get: function () { return chunk76KPBYGA_cjs.parsePrice; }
|
|
51
51
|
});
|
|
52
52
|
Object.defineProperty(exports, "serializeChallenge", {
|
|
53
53
|
enumerable: true,
|
|
54
|
-
get: function () { return
|
|
54
|
+
get: function () { return chunk76KPBYGA_cjs.serializeChallenge; }
|
|
55
55
|
});
|
|
56
56
|
Object.defineProperty(exports, "decodeBase64Url", {
|
|
57
57
|
enumerable: true,
|
package/dist/index.d.cts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { MppChargeCredential } from '@parallel-protocol/mpp-types';
|
|
2
2
|
export { MppChargeCredential, MppReceipt, decodeBase64Url, encodeBase64Url } from '@parallel-protocol/mpp-types';
|
|
3
|
-
import { a as MppRouteConfig, b as MppFacilitatorConfig, c as MppSettleResult, M as MppMiddlewareConfig, H as HTTPAdapter, P as PaymentGateResult, d as MiddlewareResult, e as PassResult } from './types-
|
|
4
|
-
export { f as MppSettleFailure, g as MppSettleSuccess, R as RunHandlerResult } from './types-
|
|
3
|
+
import { a as MppRouteConfig, b as MppFacilitatorConfig, c as MppSettleResult, M as MppMiddlewareConfig, H as HTTPAdapter, P as PaymentGateResult, d as MiddlewareResult, e as PassResult } from './types-BSzM92sD.cjs';
|
|
4
|
+
export { f as MppSettleFailure, g as MppSettleSuccess, R as RunHandlerResult } from './types-BSzM92sD.cjs';
|
|
5
5
|
import 'viem';
|
|
6
6
|
|
|
7
7
|
/** The challenge echoed back inside a credential (derived — mpp-types has no standalone export). */
|
|
@@ -39,6 +39,8 @@ declare const MPP_ERROR_CODES: {
|
|
|
39
39
|
readonly FACILITATOR_UNAVAILABLE: "FACILITATOR_UNAVAILABLE";
|
|
40
40
|
readonly FACILITATOR_INVALID_RESPONSE: "FACILITATOR_INVALID_RESPONSE";
|
|
41
41
|
readonly INVALID_CREDENTIAL: "INVALID_CREDENTIAL";
|
|
42
|
+
/** A path with `.` or `..` segments on a paid route, refused with 400. */
|
|
43
|
+
readonly INVALID_PATH: "INVALID_PATH";
|
|
42
44
|
readonly METHOD_MISMATCH: "METHOD_MISMATCH";
|
|
43
45
|
};
|
|
44
46
|
type MppErrorCode = (typeof MPP_ERROR_CODES)[keyof typeof MPP_ERROR_CODES];
|
|
@@ -50,13 +52,14 @@ declare class MppRuntimeError extends Error {
|
|
|
50
52
|
constructor(code: string);
|
|
51
53
|
}
|
|
52
54
|
|
|
53
|
-
declare function createMppPaymentGate(config: MppMiddlewareConfig): (adapter: Pick<HTTPAdapter, "getHeader" | "getPath" | "getUrl" | "getMethod">) => Promise<PaymentGateResult>;
|
|
55
|
+
declare function createMppPaymentGate(config: MppMiddlewareConfig): (adapter: Pick<HTTPAdapter, "getHeader" | "getPath" | "getFullPath" | "getUrl" | "getMethod">) => Promise<PaymentGateResult>;
|
|
54
56
|
declare function createMppPaymentMiddleware(config: MppMiddlewareConfig): (adapter: HTTPAdapter) => Promise<MiddlewareResult | PassResult>;
|
|
55
57
|
|
|
56
58
|
/** Parse a human price ("0.01") or pass a bigint through. Default 6 decimals (stablecoins). */
|
|
57
59
|
declare function parsePrice(price: string | bigint, decimals?: number): bigint;
|
|
58
60
|
/** Resolve an EVM chain id from a chain slug via the static catalogue. */
|
|
59
61
|
declare function chainIdFromSlug(network: string): number | undefined;
|
|
62
|
+
/** The route a request path falls under, see `findRoute`. */
|
|
60
63
|
declare function matchRoute<T>(path: string, routes: Record<string, T>): T | undefined;
|
|
61
64
|
|
|
62
65
|
export { HTTPAdapter, MPP_ERROR_CODES, MiddlewareResult, type MppChallenge, MppConfigError, type MppErrorCode, MppFacilitatorClient, MppFacilitatorConfig, MppMiddlewareConfig, MppRouteConfig, MppRuntimeError, MppSettleResult, PassResult, PaymentGateResult, buildChallenge, chainIdFromSlug, createMppPaymentGate, createMppPaymentMiddleware, matchRoute, parseAuthorizationHeader, parsePrice, serializeChallenge };
|