Skip to content

Commit 1e56fdc

Browse files
committed
Defaults.defaultGetHeaders: extract format calculation
rather than calculate it the same way every time in every call site
1 parent a485135 commit 1e56fdc

File tree

11 files changed

+38
-45
lines changed

11 files changed

+38
-45
lines changed

src/common/lib/client/auth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ class Auth {
588588
return client.baseUri(host) + path;
589589
};
590590

591-
const requestHeaders = Defaults.defaultPostHeaders(this.client.options);
591+
const requestHeaders = Defaults.defaultPostHeaders(this.client.options, { format: Utils.Format.json });
592592
if (resolvedAuthOptions.requestHeaders) Utils.mixin(requestHeaders, resolvedAuthOptions.requestHeaders);
593593
Logger.logAction(
594594
this.logger,

src/common/lib/client/push.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class Admin {
100100
async publish(recipient: any, payload: any): Promise<void> {
101101
const client = this.client;
102102
const format = client.options.useBinaryProtocol ? Utils.Format.msgpack : Utils.Format.json,
103-
headers = Defaults.defaultPostHeaders(client.options, { format }),
103+
headers = Defaults.defaultPostHeaders(client.options),
104104
params = {};
105105
const body = Utils.mixin({ recipient: recipient }, payload);
106106

@@ -124,7 +124,7 @@ class DeviceRegistrations {
124124
const client = this.client;
125125
const body = DeviceDetails.fromValues(device);
126126
const format = client.options.useBinaryProtocol ? Utils.Format.msgpack : Utils.Format.json,
127-
headers = Defaults.defaultPostHeaders(client.options, { format }),
127+
headers = Defaults.defaultPostHeaders(client.options),
128128
params = {};
129129

130130
Utils.mixin(headers, client.options.headers);
@@ -152,7 +152,7 @@ class DeviceRegistrations {
152152
async get(deviceIdOrDetails: any): Promise<DeviceDetails> {
153153
const client = this.client,
154154
format = client.options.useBinaryProtocol ? Utils.Format.msgpack : Utils.Format.json,
155-
headers = Defaults.defaultGetHeaders(client.options, { format }),
155+
headers = Defaults.defaultGetHeaders(client.options),
156156
deviceId = deviceIdOrDetails.id || deviceIdOrDetails;
157157

158158
if (typeof deviceId !== 'string' || !deviceId.length) {
@@ -185,7 +185,7 @@ class DeviceRegistrations {
185185
const client = this.client,
186186
format = client.options.useBinaryProtocol ? Utils.Format.msgpack : Utils.Format.json,
187187
envelope = this.client.http.supportsLinkHeaders ? undefined : format,
188-
headers = Defaults.defaultGetHeaders(client.options, { format });
188+
headers = Defaults.defaultGetHeaders(client.options);
189189

190190
Utils.mixin(headers, client.options.headers);
191191

@@ -204,8 +204,7 @@ class DeviceRegistrations {
204204

205205
async remove(deviceIdOrDetails: any): Promise<void> {
206206
const client = this.client,
207-
format = client.options.useBinaryProtocol ? Utils.Format.msgpack : Utils.Format.json,
208-
headers = Defaults.defaultGetHeaders(client.options, { format }),
207+
headers = Defaults.defaultGetHeaders(client.options),
209208
params = {},
210209
deviceId = deviceIdOrDetails.id || deviceIdOrDetails;
211210

@@ -255,7 +254,7 @@ class ChannelSubscriptions {
255254
const client = this.client;
256255
const body = PushChannelSubscription.fromValues(subscription);
257256
const format = client.options.useBinaryProtocol ? Utils.Format.msgpack : Utils.Format.json,
258-
headers = Defaults.defaultPostHeaders(client.options, { format }),
257+
headers = Defaults.defaultPostHeaders(client.options),
259258
params = {};
260259

261260
Utils.mixin(headers, client.options.headers);
@@ -284,7 +283,7 @@ class ChannelSubscriptions {
284283
const client = this.client,
285284
format = client.options.useBinaryProtocol ? Utils.Format.msgpack : Utils.Format.json,
286285
envelope = this.client.http.supportsLinkHeaders ? undefined : format,
287-
headers = Defaults.defaultGetHeaders(client.options, { format });
286+
headers = Defaults.defaultGetHeaders(client.options);
288287

289288
Utils.mixin(headers, client.options.headers);
290289

@@ -320,7 +319,7 @@ class ChannelSubscriptions {
320319
const client = this.client,
321320
format = client.options.useBinaryProtocol ? Utils.Format.msgpack : Utils.Format.json,
322321
envelope = this.client.http.supportsLinkHeaders ? undefined : format,
323-
headers = Defaults.defaultGetHeaders(client.options, { format });
322+
headers = Defaults.defaultGetHeaders(client.options);
324323

325324
Utils.mixin(headers, client.options.headers);
326325

src/common/lib/client/rest.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ export class Rest {
6363
Utils.mixin(headers, this.client.options.headers);
6464

6565
return new PaginatedResource(this.client, '/stats', headers, envelope, function (body, headers, unpacked) {
66-
const statsValues = unpacked ? body : JSON.parse(body as string);
66+
const statsValues = unpacked ? body : Utils.decodeBody(body, this.client._MsgPack, format);
6767
for (let i = 0; i < statsValues.length; i++) statsValues[i] = Stats.fromValues(statsValues[i]);
6868
return statsValues;
6969
}).get(params as Record<string, string>);
7070
}
7171

7272
async time(params?: RequestParams): Promise<number> {
73-
const headers = Defaults.defaultGetHeaders(this.client.options);
73+
const headers = Defaults.defaultGetHeaders(this.client.options, { format: Utils.Format.json });
7474
if (this.client.options.headers) Utils.mixin(headers, this.client.options.headers);
7575
const timeUri = (host: string) => {
7676
return this.client.baseUri(host) + '/time';
@@ -170,7 +170,7 @@ export class Rest {
170170
}
171171

172172
const format = this.client.options.useBinaryProtocol ? Utils.Format.msgpack : Utils.Format.json,
173-
headers = Defaults.defaultPostHeaders(this.client.options, { format });
173+
headers = Defaults.defaultPostHeaders(this.client.options);
174174

175175
if (this.client.options.headers) Utils.mixin(headers, this.client.options.headers);
176176

@@ -192,7 +192,7 @@ export class Rest {
192192

193193
async batchPresence(channels: string[]): Promise<BatchPresenceResult> {
194194
const format = this.client.options.useBinaryProtocol ? Utils.Format.msgpack : Utils.Format.json,
195-
headers = Defaults.defaultPostHeaders(this.client.options, { format });
195+
headers = Defaults.defaultGetHeaders(this.client.options);
196196

197197
if (this.client.options.headers) Utils.mixin(headers, this.client.options.headers);
198198

@@ -223,7 +223,7 @@ export class Rest {
223223
};
224224

225225
const format = this.client.options.useBinaryProtocol ? Utils.Format.msgpack : Utils.Format.json,
226-
headers = Defaults.defaultPostHeaders(this.client.options, { format });
226+
headers = Defaults.defaultPostHeaders(this.client.options);
227227

228228
if (this.client.options.headers) Utils.mixin(headers, this.client.options.headers);
229229

src/common/lib/client/restannotations.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class RestAnnotations {
7575
const client = this.channel.client,
7676
options = client.options,
7777
format = options.useBinaryProtocol ? Utils.Format.msgpack : Utils.Format.json,
78-
headers = Defaults.defaultPostHeaders(client.options, { format }),
78+
headers = Defaults.defaultPostHeaders(client.options),
7979
params = {};
8080

8181
Utils.mixin(headers, client.options.headers);
@@ -106,7 +106,7 @@ class RestAnnotations {
106106
messageSerial = serialFromMsgOrSerial(msgOrSerial),
107107
format = client.options.useBinaryProtocol ? Utils.Format.msgpack : Utils.Format.json,
108108
envelope = client.http.supportsLinkHeaders ? undefined : format,
109-
headers = Defaults.defaultGetHeaders(client.options, { format });
109+
headers = Defaults.defaultGetHeaders(client.options);
110110

111111
Utils.mixin(headers, client.options.headers);
112112

src/common/lib/client/restchannel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class RestChannel {
108108
options = client.options,
109109
format = options.useBinaryProtocol ? Utils.Format.msgpack : Utils.Format.json,
110110
idempotentRestPublishing = client.options.idempotentRestPublishing,
111-
headers = Defaults.defaultPostHeaders(client.options, { format });
111+
headers = Defaults.defaultPostHeaders(client.options);
112112

113113
Utils.mixin(headers, options.headers);
114114

src/common/lib/client/restchannelmixin.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export class RestChannelMixin {
3838
const client = channel.client,
3939
format = client.options.useBinaryProtocol ? Utils.Format.msgpack : Utils.Format.json,
4040
envelope = channel.client.http.supportsLinkHeaders ? undefined : format,
41-
headers = Defaults.defaultGetHeaders(client.options, { format });
41+
headers = Defaults.defaultGetHeaders(client.options);
4242

4343
Utils.mixin(headers, client.options.headers);
4444

@@ -57,7 +57,7 @@ export class RestChannelMixin {
5757

5858
static async status(channel: RestChannel | RealtimeChannel): Promise<API.ChannelDetails> {
5959
const format = channel.client.options.useBinaryProtocol ? Utils.Format.msgpack : Utils.Format.json;
60-
const headers = Defaults.defaultPostHeaders(channel.client.options, { format });
60+
const headers = Defaults.defaultPostHeaders(channel.client.options);
6161

6262
const response = await Resource.get<API.ChannelDetails>(
6363
channel.client,
@@ -81,9 +81,10 @@ export class RestChannelMixin {
8181
);
8282
}
8383

84-
const format = channel.client.options.useBinaryProtocol ? Utils.Format.msgpack : Utils.Format.json;
85-
const headers = Defaults.defaultGetHeaders(channel.client.options, { format });
86-
Utils.mixin(headers, channel.client.options.headers);
84+
const client = channel.client;
85+
const format = client.options.useBinaryProtocol ? Utils.Format.msgpack : Utils.Format.json;
86+
const headers = Defaults.defaultGetHeaders(client.options);
87+
Utils.mixin(headers, client.options.headers);
8788

8889
const { body, unpacked } = await Resource.get<WireMessage>(
8990
client,
@@ -115,7 +116,7 @@ export class RestChannelMixin {
115116

116117
const client = channel.client;
117118
const format = client.options.useBinaryProtocol ? Utils.Format.msgpack : Utils.Format.json;
118-
const headers = Defaults.defaultPostHeaders(client.options, { format });
119+
const headers = Defaults.defaultPostHeaders(client.options);
119120
Utils.mixin(headers, client.options.headers);
120121

121122
let encoded: WireMessage | null = null;
@@ -167,7 +168,7 @@ export class RestChannelMixin {
167168
const client = channel.client;
168169
const format = client.options.useBinaryProtocol ? Utils.Format.msgpack : Utils.Format.json;
169170
const envelope = channel.client.http.supportsLinkHeaders ? undefined : format;
170-
const headers = Defaults.defaultGetHeaders(client.options, { format });
171+
const headers = Defaults.defaultGetHeaders(client.options);
171172

172173
Utils.mixin(headers, client.options.headers);
173174

src/common/lib/client/restpresence.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class RestPresence {
2121
const client = this.channel.client,
2222
format = client.options.useBinaryProtocol ? Utils.Format.msgpack : Utils.Format.json,
2323
envelope = this.channel.client.http.supportsLinkHeaders ? undefined : format,
24-
headers = Defaults.defaultGetHeaders(client.options, { format });
24+
headers = Defaults.defaultGetHeaders(client.options);
2525

2626
Utils.mixin(headers, client.options.headers);
2727

src/common/lib/client/restpresencemixin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class RestPresenceMixin {
1818
const client = presence.channel.client,
1919
format = client.options.useBinaryProtocol ? Utils.Format.msgpack : Utils.Format.json,
2020
envelope = presence.channel.client.http.supportsLinkHeaders ? undefined : format,
21-
headers = Defaults.defaultGetHeaders(client.options, { format });
21+
headers = Defaults.defaultGetHeaders(client.options);
2222

2323
Utils.mixin(headers, client.options.headers);
2424

src/common/lib/util/defaults.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -410,12 +410,9 @@ const defaultHeadersOptions: Required<HeadersOptions> = {
410410

411411
export function defaultGetHeaders(
412412
options: NormalisedClientOptions,
413-
{
414-
format = defaultHeadersOptions.format,
415-
protocolVersion = defaultHeadersOptions.protocolVersion,
416-
}: HeadersOptions = {},
413+
{ format, protocolVersion = defaultHeadersOptions.protocolVersion }: HeadersOptions = {},
417414
): Record<string, string> {
418-
const accept = contentTypes[format];
415+
const accept = contentTypes[format ?? (options.useBinaryProtocol ? Utils.Format.msgpack : Utils.Format.json)];
419416
return {
420417
accept: accept,
421418
'X-Ably-Version': protocolVersion.toString(),
@@ -425,14 +422,10 @@ export function defaultGetHeaders(
425422

426423
export function defaultPostHeaders(
427424
options: NormalisedClientOptions,
428-
{
429-
format = defaultHeadersOptions.format,
430-
protocolVersion = defaultHeadersOptions.protocolVersion,
431-
}: HeadersOptions = {},
425+
{ format, protocolVersion = defaultHeadersOptions.protocolVersion }: HeadersOptions = {},
432426
): Record<string, string> {
433-
let contentType;
434-
const accept = (contentType = contentTypes[format]);
435-
427+
const accept = contentTypes[format ?? (options.useBinaryProtocol ? Utils.Format.msgpack : Utils.Format.json)];
428+
const contentType = accept;
436429
return {
437430
accept: accept,
438431
'content-type': contentType,

src/plugins/push/pushactivation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export function localDeviceFactory(deviceDetails: typeof DeviceDetails) {
7676
const client = this.rest,
7777
format = client.options.useBinaryProtocol ? client.Utils.Format.msgpack : client.Utils.Format.json,
7878
envelope = client.http.supportsLinkHeaders ? undefined : format,
79-
headers = client.Defaults.defaultGetHeaders(client.options, { format });
79+
headers = client.Defaults.defaultGetHeaders(client.options);
8080

8181
client.Utils.mixin(headers, client.options.headers, { 'X-Ably-DeviceToken': this.deviceIdentityToken });
8282

@@ -301,7 +301,7 @@ export class ActivationStateMachine {
301301
} else {
302302
const rest = this.client;
303303
const format = rest.options.useBinaryProtocol ? this.client.Utils.Format.msgpack : this.client.Utils.Format.json,
304-
headers = this.client.Defaults.defaultPostHeaders(rest.options, { format }),
304+
headers = this.client.Defaults.defaultPostHeaders(rest.options),
305305
params = { deviceId: device.id };
306306

307307
if (rest.options.headers) this.client.Utils.mixin(headers, rest.options.headers);

0 commit comments

Comments
 (0)