ws402
Version:
WebSocket implementation of X402 protocol for pay-as-you-go digital resources with automatic refunds
29 lines (28 loc) • 1.03 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createWS402Middleware = createWS402Middleware;
exports.isWS402Request = isWS402Request;
/**
* Express middleware to serve WS402 schema
*/
function createWS402Middleware(ws402, options = {}) {
const { resourceIdExtractor = (req) => req.params.resourceId || 'default', estimatedDurationExtractor = (req) => parseInt(req.query.duration) || 300, schemaEndpoint = '/ws402/schema/:resourceId', } = options;
return (req, res, next) => {
try {
const resourceId = resourceIdExtractor(req);
const estimatedDuration = estimatedDurationExtractor(req);
const schema = ws402.generateSchema(resourceId, estimatedDuration);
res.json(schema);
}
catch (error) {
next(error);
}
};
}
/**
* Check if request should be handled by WS402
*/
function isWS402Request(req) {
return req.headers['x-protocol'] === 'ws402' ||
req.query.protocol === 'ws402';
}