From aedee147228f62aa0081de24d0d57956df8ca6ac Mon Sep 17 00:00:00 2001 From: AkiraFukushima Date: Thu, 29 Dec 2022 11:34:45 +0900 Subject: [PATCH] refs #1515 Add encoding header to avoid the axios issue https://github.com/axios/axios/issues/5346 --- example/typescript/src/mastodon/instance.ts | 2 +- megalodon/src/mastodon/api_client.ts | 72 ++++++++++++++++++--- megalodon/src/mastodon/web_socket.ts | 4 +- megalodon/src/misskey/api_client.ts | 9 ++- megalodon/src/misskey/web_socket.ts | 4 +- megalodon/src/pleroma/api_client.ts | 72 ++++++++++++++++++--- megalodon/src/pleroma/web_socket.ts | 4 +- 7 files changed, 146 insertions(+), 21 deletions(-) diff --git a/example/typescript/src/mastodon/instance.ts b/example/typescript/src/mastodon/instance.ts index 410dd12ba..bdd32069e 100644 --- a/example/typescript/src/mastodon/instance.ts +++ b/example/typescript/src/mastodon/instance.ts @@ -1,6 +1,6 @@ import generator, { Entity, Response } from 'megalodon' -const BASE_URL: string = 'http://fedibird.com' +const BASE_URL: string = 'http://mas.to' const client = generator('mastodon', BASE_URL) diff --git a/megalodon/src/mastodon/api_client.ts b/megalodon/src/mastodon/api_client.ts index 3ee7299b4..e6b6dbc14 100644 --- a/megalodon/src/mastodon/api_client.ts +++ b/megalodon/src/mastodon/api_client.ts @@ -78,7 +78,14 @@ namespace MastodonAPI { ): Promise> { let options: AxiosRequestConfig = { params: params, - headers: headers, + headers: Object.assign( + { + 'User-Agent': this.userAgent, + // To avoid: https://github.com/axios/axios/issues/5346 + 'Accept-Encoding': 'gzip,deflate,compress' + }, + headers + ), maxContentLength: Infinity, maxBodyLength: Infinity } @@ -123,7 +130,14 @@ namespace MastodonAPI { */ public async put(path: string, params = {}, headers: { [key: string]: string } = {}): Promise> { let options: AxiosRequestConfig = { - headers: headers, + headers: Object.assign( + { + 'User-Agent': this.userAgent, + // To avoid: https://github.com/axios/axios/issues/5346 + 'Accept-Encoding': 'gzip,deflate,compress' + }, + headers + ), maxContentLength: Infinity, maxBodyLength: Infinity } @@ -168,7 +182,14 @@ namespace MastodonAPI { */ public async putForm(path: string, params = {}, headers: { [key: string]: string } = {}): Promise> { let options: AxiosRequestConfig = { - headers: headers, + headers: Object.assign( + { + 'User-Agent': this.userAgent, + // To avoid: https://github.com/axios/axios/issues/5346 + 'Accept-Encoding': 'gzip,deflate,compress' + }, + headers + ), maxContentLength: Infinity, maxBodyLength: Infinity } @@ -213,7 +234,14 @@ namespace MastodonAPI { */ public async patch(path: string, params = {}, headers: { [key: string]: string } = {}): Promise> { let options: AxiosRequestConfig = { - headers: headers, + headers: Object.assign( + { + 'User-Agent': this.userAgent, + // To avoid: https://github.com/axios/axios/issues/5346 + 'Accept-Encoding': 'gzip,deflate,compress' + }, + headers + ), maxContentLength: Infinity, maxBodyLength: Infinity } @@ -258,7 +286,14 @@ namespace MastodonAPI { */ public async patchForm(path: string, params = {}, headers: { [key: string]: string } = {}): Promise> { let options: AxiosRequestConfig = { - headers: headers, + headers: Object.assign( + { + 'User-Agent': this.userAgent, + // To avoid: https://github.com/axios/axios/issues/5346 + 'Accept-Encoding': 'gzip,deflate,compress' + }, + headers + ), maxContentLength: Infinity, maxBodyLength: Infinity } @@ -303,7 +338,14 @@ namespace MastodonAPI { */ public async post(path: string, params = {}, headers: { [key: string]: string } = {}): Promise> { let options: AxiosRequestConfig = { - headers: headers, + headers: Object.assign( + { + 'User-Agent': this.userAgent, + // To avoid: https://github.com/axios/axios/issues/5346 + 'Accept-Encoding': 'gzip,deflate,compress' + }, + headers + ), maxContentLength: Infinity, maxBodyLength: Infinity } @@ -339,7 +381,14 @@ namespace MastodonAPI { */ public async postForm(path: string, params = {}, headers: { [key: string]: string } = {}): Promise> { let options: AxiosRequestConfig = { - headers: headers, + headers: Object.assign( + { + 'User-Agent': this.userAgent, + // To avoid: https://github.com/axios/axios/issues/5346 + 'Accept-Encoding': 'gzip,deflate,compress' + }, + headers + ), maxContentLength: Infinity, maxBodyLength: Infinity } @@ -376,7 +425,14 @@ namespace MastodonAPI { public async del(path: string, params = {}, headers: { [key: string]: string } = {}): Promise> { let options: AxiosRequestConfig = { data: params, - headers: headers, + headers: Object.assign( + { + 'User-Agent': this.userAgent, + // To avoid: https://github.com/axios/axios/issues/5346 + 'Accept-Encoding': 'gzip,deflate,compress' + }, + headers + ), maxContentLength: Infinity, maxBodyLength: Infinity } diff --git a/megalodon/src/mastodon/web_socket.ts b/megalodon/src/mastodon/web_socket.ts index 2d4b0d087..ce4a89535 100644 --- a/megalodon/src/mastodon/web_socket.ts +++ b/megalodon/src/mastodon/web_socket.ts @@ -52,7 +52,9 @@ export default class WebSocket extends EventEmitter implements WebSocketInterfac } this.parser = new Parser() this.headers = { - 'User-Agent': userAgent + 'User-Agent': userAgent, + // To avoid: https://github.com/axios/axios/issues/5346 + 'Accept-Encoding': 'gzip,deflate,compress' } this.proxyConfig = proxyConfig this._accessToken = accessToken diff --git a/megalodon/src/misskey/api_client.ts b/megalodon/src/misskey/api_client.ts index a9f0cfd03..edec74a10 100644 --- a/megalodon/src/misskey/api_client.ts +++ b/megalodon/src/misskey/api_client.ts @@ -470,7 +470,14 @@ namespace MisskeyAPI { */ public async post(path: string, params: any = {}, headers: { [key: string]: string } = {}): Promise> { let options: AxiosRequestConfig = { - headers: headers, + headers: Object.assign( + { + 'User-Agent': this.userAgent, + // To avoid: https://github.com/axios/axios/issues/5346 + 'Accept-Encoding': 'gzip,deflate,compress' + }, + headers + ), maxContentLength: Infinity, maxBodyLength: Infinity } diff --git a/megalodon/src/misskey/web_socket.ts b/megalodon/src/misskey/web_socket.ts index b0f092c6e..9f16f3e17 100644 --- a/megalodon/src/misskey/web_socket.ts +++ b/megalodon/src/misskey/web_socket.ts @@ -48,7 +48,9 @@ export default class WebSocket extends EventEmitter implements WebSocketInterfac this.parser = new Parser() this.channel = channel this.headers = { - 'User-Agent': userAgent + 'User-Agent': userAgent, + // To avoid: https://github.com/axios/axios/issues/5346 + 'Accept-Encoding': 'gzip,deflate,compress' } if (listId === undefined) { this.listId = null diff --git a/megalodon/src/pleroma/api_client.ts b/megalodon/src/pleroma/api_client.ts index d45bc861b..ee331bd12 100644 --- a/megalodon/src/pleroma/api_client.ts +++ b/megalodon/src/pleroma/api_client.ts @@ -303,7 +303,14 @@ namespace PleromaAPI { public async get(path: string, params = {}, headers: { [key: string]: string } = {}): Promise> { let options: AxiosRequestConfig = { params: params, - headers: headers + headers: Object.assign( + { + 'User-Agent': this.userAgent, + // To avoid: https://github.com/axios/axios/issues/5346 + 'Accept-Encoding': 'gzip,deflate,compress' + }, + headers + ) } if (this.accessToken) { options = objectAssignDeep({}, options, { @@ -346,7 +353,14 @@ namespace PleromaAPI { */ public async put(path: string, params = {}, headers: { [key: string]: string } = {}): Promise> { let options: AxiosRequestConfig = { - headers: headers, + headers: Object.assign( + { + 'User-Agent': this.userAgent, + // To avoid: https://github.com/axios/axios/issues/5346 + 'Accept-Encoding': 'gzip,deflate,compress' + }, + headers + ), maxContentLength: Infinity, maxBodyLength: Infinity } @@ -391,7 +405,14 @@ namespace PleromaAPI { */ public async putForm(path: string, params = {}, headers: { [key: string]: string } = {}): Promise> { let options: AxiosRequestConfig = { - headers: headers, + headers: Object.assign( + { + 'User-Agent': this.userAgent, + // To avoid: https://github.com/axios/axios/issues/5346 + 'Accept-Encoding': 'gzip,deflate,compress' + }, + headers + ), maxContentLength: Infinity, maxBodyLength: Infinity } @@ -436,7 +457,14 @@ namespace PleromaAPI { */ public async patch(path: string, params = {}, headers: { [key: string]: string } = {}): Promise> { let options: AxiosRequestConfig = { - headers: headers, + headers: Object.assign( + { + 'User-Agent': this.userAgent, + // To avoid: https://github.com/axios/axios/issues/5346 + 'Accept-Encoding': 'gzip,deflate,compress' + }, + headers + ), maxContentLength: Infinity, maxBodyLength: Infinity } @@ -481,7 +509,14 @@ namespace PleromaAPI { */ public async patchForm(path: string, params = {}, headers: { [key: string]: string } = {}): Promise> { let options: AxiosRequestConfig = { - headers: headers, + headers: Object.assign( + { + 'User-Agent': this.userAgent, + // To avoid: https://github.com/axios/axios/issues/5346 + 'Accept-Encoding': 'gzip,deflate,compress' + }, + headers + ), maxContentLength: Infinity, maxBodyLength: Infinity } @@ -526,7 +561,14 @@ namespace PleromaAPI { */ public async post(path: string, params = {}, headers: { [key: string]: string } = {}): Promise> { let options: AxiosRequestConfig = { - headers: headers, + headers: Object.assign( + { + 'User-Agent': this.userAgent, + // To avoid: https://github.com/axios/axios/issues/5346 + 'Accept-Encoding': 'gzip,deflate,compress' + }, + headers + ), maxContentLength: Infinity, maxBodyLength: Infinity } @@ -562,7 +604,14 @@ namespace PleromaAPI { */ public async postForm(path: string, params = {}, headers: { [key: string]: string } = {}): Promise> { let options: AxiosRequestConfig = { - headers: headers, + headers: Object.assign( + { + 'User-Agent': this.userAgent, + // To avoid: https://github.com/axios/axios/issues/5346 + 'Accept-Encoding': 'gzip,deflate,compress' + }, + headers + ), maxContentLength: Infinity, maxBodyLength: Infinity } @@ -599,7 +648,14 @@ namespace PleromaAPI { public async del(path: string, params = {}, headers: { [key: string]: string } = {}): Promise> { let options: AxiosRequestConfig = { data: params, - headers: headers, + headers: Object.assign( + { + 'User-Agent': this.userAgent, + // To avoid: https://github.com/axios/axios/issues/5346 + 'Accept-Encoding': 'gzip,deflate,compress' + }, + headers + ), maxContentLength: Infinity, maxBodyLength: Infinity } diff --git a/megalodon/src/pleroma/web_socket.ts b/megalodon/src/pleroma/web_socket.ts index e2aa29990..842d25581 100644 --- a/megalodon/src/pleroma/web_socket.ts +++ b/megalodon/src/pleroma/web_socket.ts @@ -53,7 +53,9 @@ export default class WebSocket extends EventEmitter implements WebSocketInterfac } this.parser = new Parser() this.headers = { - 'User-Agent': userAgent + 'User-Agent': userAgent, + // To avoid: https://github.com/axios/axios/issues/5346 + 'Accept-Encoding': 'gzip,deflate,compress' } this.proxyConfig = proxyConfig this._accessToken = accessToken