X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=src%2Fcommunication.js;h=feda5887146c6b70ae242e1daf52ab3fbbe10a44;hb=43898cdd317a127bc45e8b3cb2f160df386760a1;hp=b11237794b7b340da470c7b2740bf3d2b5087c25;hpb=f92c90e92ded661ddd957580e7fbde8aaa24224d;p=websub-hub diff --git a/src/communication.js b/src/communication.js index b112377..feda588 100644 --- a/src/communication.js +++ b/src/communication.js @@ -57,7 +57,7 @@ class Communication { static userAgentString(userAgentConfig) { // eslint-disable-next-line security/detect-object-injection const _conf = (field, def) => (userAgentConfig && field in userAgentConfig) ? userAgentConfig[field] : def; - const product = _conf('product', packageName); + const product = _conf('product', packageName).split('/').pop(); const version = _conf('version', packageVersion); let implementation = _conf('implementation', Enum.Specification); if (implementation) { @@ -196,6 +196,8 @@ class Communication { const acceptPreferred = [topic.contentType, acceptWildcard].filter((x) => x).join(', '); return Communication._axiosConfig('GET', topic.url, undefined, {}, { [Enum.Header.Accept]: acceptPreferred, + ...(topic.httpEtag && { [Enum.Header.IfNoneMatch]: topic.httpEtag }), + ...(topic.httpLastModified && { [Enum.Header.IfModifiedSince]: topic.httpLastModified }), }); } @@ -218,8 +220,8 @@ class Communication { const topic = await this.db.topicGetById(dbCtx, verification.topicId); if (!topic) { - this.logger.error(_scope, 'no such topic id', { verification, requestId }); - throw new Errors.InternalInconsistencyError('no such topic id'); + this.logger.error(_scope, Enum.Message.NoSuchTopicId, { verification, requestId }); + throw new Errors.InternalInconsistencyError(Enum.Message.NoSuchTopicId); } if (!topic.isActive) { @@ -436,8 +438,8 @@ class Communication { const topic = await this.db.topicGetById(dbCtx, topicId); if (topic === undefined) { - this.logger.error(_scope, 'no such topic id', logInfoData); - throw new Errors.InternalInconsistencyError('no such topic id'); + this.logger.error(_scope, Enum.Message.NoSuchTopicId, logInfoData); + throw new Errors.InternalInconsistencyError(Enum.Message.NoSuchTopicId); } // Cull any expired subscriptions @@ -467,6 +469,7 @@ class Communication { switch (common.httpStatusCodeClass(response.status)) { case 2: + case 3: // Fall out of switch on success break; @@ -481,6 +484,12 @@ class Communication { return; } + if (response.status === 304) { + this.logger.info(_scope, 'content has not changed, per server', logInfoData); + await this.db.topicFetchComplete(dbCtx, topicId); + return; + } + const contentHash = Communication.contentHash(response.data, topic.contentHashAlgorithm); logInfoData.contentHash = contentHash; if (topic.contentHash === contentHash) { @@ -505,6 +514,8 @@ class Communication { } const contentType = response.headers[Enum.Header.ContentType.toLowerCase()]; + const httpETag = response.headers[Enum.Header.ETag.toLowerCase()]; + const httpLastModified = response.headers[Enum.Header.LastModified.toLowerCase()]; await this.db.transaction(dbCtx, async (txCtx) => { await this.db.topicSetContent(txCtx, { @@ -512,6 +523,8 @@ class Communication { content: Buffer.from(response.data), contentHash, ...(contentType && { contentType }), + ...(httpETag && { httpETag }), + ...(httpLastModified && { httpLastModified }), }); await this.db.topicFetchComplete(txCtx, topicId); @@ -565,7 +578,7 @@ class Communication { await this.db.transaction(dbCtx, async (txCtx) => { await this.db.verificationInsert(txCtx, verification); - await this.db.subscriptionDeliveryComplete(txCtx, subscription.callback, subscription.topicId); + await this.db.subscriptionDeliveryComplete(txCtx, subscription.callback, subscription.topicId, topic.contentUpdated); }); this.logger.info(_scope, 'update unsubscription for deleted topic', logInfoData); return; @@ -617,7 +630,7 @@ class Communication { return; } - await this.db.subscriptionDeliveryComplete(dbCtx, subscription.callback, subscription.topicId); + await this.db.subscriptionDeliveryComplete(dbCtx, subscription.callback, subscription.topicId, topic.contentUpdated); this.logger.info(_scope, 'update success', logInfoData); }