1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const log_1 = require("../common/log");
|
4 | const log = log_1.create('expire-middleware');
|
5 | const IlpPacket = require("ilp-packet");
|
6 | const { TransferTimedOutError } = IlpPacket.Errors;
|
7 | class 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 | }
|
33 | exports.default = ExpireMiddleware;
|
34 |
|
\ | No newline at end of file |