From d3442ef95f068ed42bc6d2c0d6dc902c2df7a9df Mon Sep 17 00:00:00 2001 From: Lawrence Forooghian Date: Mon, 29 May 2023 10:19:43 -0300 Subject: [PATCH] =?UTF-8?q?Make=20PaginatedResource=E2=80=99s=20bodyHandle?= =?UTF-8?q?r=20async?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Preparation for #1293 (making ICipher.decrypt asynchronous). --- src/common/lib/client/channel.ts | 2 +- src/common/lib/client/paginatedresource.ts | 43 +++++++++++++--------- src/common/lib/client/presence.ts | 4 +- src/common/lib/client/push.ts | 6 +-- src/common/lib/client/rest.ts | 2 +- 5 files changed, 32 insertions(+), 25 deletions(-) diff --git a/src/common/lib/client/channel.ts b/src/common/lib/client/channel.ts index 5c64844f03..a5e156dc24 100644 --- a/src/common/lib/client/channel.ts +++ b/src/common/lib/client/channel.ts @@ -97,7 +97,7 @@ class Channel extends EventEmitter { Utils.mixin(headers, rest.options.headers); const options = this.channelOptions; - new PaginatedResource(rest, this.basePath + '/messages', headers, envelope, function ( + new PaginatedResource(rest, this.basePath + '/messages', headers, envelope, async function ( body: any, headers: Record, unpacked?: boolean diff --git a/src/common/lib/client/paginatedresource.ts b/src/common/lib/client/paginatedresource.ts index 3cadfaba2b..bf54698b23 100644 --- a/src/common/lib/client/paginatedresource.ts +++ b/src/common/lib/client/paginatedresource.ts @@ -5,7 +5,7 @@ import ErrorInfo, { IPartialErrorInfo } from '../types/errorinfo'; import { PaginatedResultCallback } from '../../types/utils'; import Rest from './rest'; -export type BodyHandler = (body: unknown, headers: Record, packed?: boolean) => any; +export type BodyHandler = (body: unknown, headers: Record, packed?: boolean) => Promise; function getRelParams(linkUrl: string) { const urlMatch = linkUrl.match(/^\.\/(\w+)\?(.*)$/); @@ -149,25 +149,32 @@ class PaginatedResource { callback?.(err); return; } - let items, linkHeader, relParams; - try { - items = this.bodyHandler(body, headers || {}, unpacked); - } catch (e) { - /* If we got an error, the failure to parse the body is almost certainly - * due to that, so callback with that in preference over the parse error */ - callback?.(err || e); - return; - } - if (headers && (linkHeader = headers['Link'] || headers['link'])) { - relParams = parseRelLinks(linkHeader); - } + const handleBody = async () => { + let items, linkHeader, relParams; - if (this.useHttpPaginatedResponse) { - callback(null, new HttpPaginatedResponse(this, items, headers || {}, statusCode as number, relParams, err)); - } else { - callback(null, new PaginatedResult(this, items, relParams)); - } + try { + items = await this.bodyHandler(body, headers || {}, unpacked); + } catch (e) { + /* If we got an error, the failure to parse the body is almost certainly + * due to that, so throw that in preference over the parse error */ + throw err || e; + } + + if (headers && (linkHeader = headers['Link'] || headers['link'])) { + relParams = parseRelLinks(linkHeader); + } + + if (this.useHttpPaginatedResponse) { + return new HttpPaginatedResponse(this, items, headers || {}, statusCode as number, relParams, err); + } else { + return new PaginatedResult(this, items, relParams); + } + }; + + handleBody() + .then((result) => callback(null, result)) + .catch((err) => callback(err, null)); } } diff --git a/src/common/lib/client/presence.ts b/src/common/lib/client/presence.ts index 8874bccb85..3f7b9b3309 100644 --- a/src/common/lib/client/presence.ts +++ b/src/common/lib/client/presence.ts @@ -42,7 +42,7 @@ class Presence extends EventEmitter { Utils.mixin(headers, rest.options.headers); const options = this.channel.channelOptions; - new PaginatedResource(rest, this.basePath, headers, envelope, function ( + new PaginatedResource(rest, this.basePath, headers, envelope, async function ( body: any, headers: Record, unpacked?: boolean @@ -84,7 +84,7 @@ class Presence extends EventEmitter { Utils.mixin(headers, rest.options.headers); const options = this.channel.channelOptions; - new PaginatedResource(rest, this.basePath + '/history', headers, envelope, function ( + new PaginatedResource(rest, this.basePath + '/history', headers, envelope, async function ( body: any, headers: Record, unpacked?: boolean diff --git a/src/common/lib/client/push.ts b/src/common/lib/client/push.ts index dca0d72135..fb694eaded 100644 --- a/src/common/lib/client/push.ts +++ b/src/common/lib/client/push.ts @@ -161,7 +161,7 @@ class DeviceRegistrations { Utils.mixin(headers, rest.options.headers); - new PaginatedResource(rest, '/push/deviceRegistrations', headers, envelope, function ( + new PaginatedResource(rest, '/push/deviceRegistrations', headers, envelope, async function ( body: any, headers: Record, unpacked?: boolean @@ -286,7 +286,7 @@ class ChannelSubscriptions { Utils.mixin(headers, rest.options.headers); - new PaginatedResource(rest, '/push/channelSubscriptions', headers, envelope, function ( + new PaginatedResource(rest, '/push/channelSubscriptions', headers, envelope, async function ( body: any, headers: Record, unpacked?: boolean @@ -334,7 +334,7 @@ class ChannelSubscriptions { if (rest.options.pushFullWait) Utils.mixin(params, { fullWait: 'true' }); - new PaginatedResource(rest, '/push/channels', headers, envelope, function ( + new PaginatedResource(rest, '/push/channels', headers, envelope, async function ( body: unknown, headers: Record, unpacked?: boolean diff --git a/src/common/lib/client/rest.ts b/src/common/lib/client/rest.ts index 6f5182e9b5..5629f8f391 100644 --- a/src/common/lib/client/rest.ts +++ b/src/common/lib/client/rest.ts @@ -207,7 +207,7 @@ class Rest { path, headers, envelope, - function (resbody: unknown, headers: Record, unpacked?: boolean) { + async function (resbody: unknown, headers: Record, unpacked?: boolean) { return Utils.ensureArray(unpacked ? resbody : decoder(resbody as string & Buffer)); }, /* useHttpPaginatedResponse: */ true