UNPKG

1.55 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const log_1 = require("../common/log");
4const log = log_1.create('expire-middleware');
5const IlpPacket = require("ilp-packet");
6const { TransferTimedOutError } = IlpPacket.Errors;
7class ExpireMiddleware {
8 async applyToPipelines(pipelines, accountId) {
9 pipelines.outgoingData.insertLast({
10 name: 'expire',
11 method: async (data, next) => {
12 if (data[0] === IlpPacket.Type.TYPE_ILP_PREPARE) {
13 const { executionCondition, expiresAt } = IlpPacket.deserializeIlpPrepare(data);
14 const duration = expiresAt.getTime() - Date.now();
15 const promise = next(data);
16 let timeout;
17 const timeoutPromise = new Promise((resolve, reject) => {
18 timeout = setTimeout(() => {
19 log.debug('packet expired. cond=%s expiresAt=%s', executionCondition.slice(0, 6).toString('base64'), expiresAt.toISOString());
20 reject(new TransferTimedOutError('packet expired.'));
21 }, duration);
22 });
23 return Promise.race([
24 promise.then((data) => { clearTimeout(timeout); return data; }),
25 timeoutPromise
26 ]);
27 }
28 return next(data);
29 }
30 });
31 }
32}
33exports.default = ExpireMiddleware;
34//# sourceMappingURL=expire.js.map
\No newline at end of file