(config: Mirror)
| 60 | let since = Math.floor(Date.now() / 1000) - 60 * 10 |
| 61 | |
| 62 | const createMirror = (config: Mirror) => { |
| 63 | const subscriptionId = `mirror-${randomUUID()}` |
| 64 | |
| 65 | logger('connecting to %s', config.address) |
| 66 | |
| 67 | return new WebSocket(config.address, { timeout: 5000 }) |
| 68 | .on('open', function () { |
| 69 | logger('connected to %s', config.address) |
| 70 | |
| 71 | if (Array.isArray(config.filters) && config.filters?.length) { |
| 72 | const filters = config.filters.map((filter) => ({ ...filter, since })) |
| 73 | |
| 74 | logger('subscribing with %s: %o', subscriptionId, filters) |
| 75 | |
| 76 | this.send(JSON.stringify(createSubscriptionMessage(subscriptionId, filters))) |
| 77 | } |
| 78 | }) |
| 79 | .on('message', async (raw: RawData) => { |
| 80 | try { |
| 81 | const message = JSON.parse(raw.toString('utf8')) as OutgoingEventMessage |
| 82 | |
| 83 | if (!Array.isArray(message)) { |
| 84 | return |
| 85 | } |
| 86 | |
| 87 | if (message[0] !== 'EVENT' || message[1] !== subscriptionId) { |
| 88 | logger('%s >> local: %o', config.address, message) |
| 89 | return |
| 90 | } |
| 91 | |
| 92 | let event = message[2] |
| 93 | |
| 94 | if (!anyPass(map(isEventMatchingFilter, config.filters))(event)) { |
| 95 | return |
| 96 | } |
| 97 | |
| 98 | if (!(await isEventIdValid(event)) || !(await isEventSignatureValid(event))) { |
| 99 | return |
| 100 | } |
| 101 | |
| 102 | if (isExpiredEvent(event)) { |
| 103 | return |
| 104 | } |
| 105 | |
| 106 | const eventExpiration = getEventExpiration(event) |
| 107 | if (eventExpiration) { |
| 108 | event = { |
| 109 | ...event, |
| 110 | [EventExpirationTimeMetadataKey]: eventExpiration, |
| 111 | } as any |
| 112 | } |
| 113 | |
| 114 | if (!this.canAcceptEvent(event)) { |
| 115 | return |
| 116 | } |
| 117 | |
| 118 | if (!(await this.isUserAdmitted(event))) { |
| 119 | return |
nothing calls this directly
no test coverage detected