Skip to content

Commit

Permalink
Merge pull request #1516 from h3poteto/iss-1515
Browse files Browse the repository at this point in the history
refs #1515 Add encoding header to avoid the axios issue
  • Loading branch information
h3poteto committed Dec 29, 2022
2 parents 3782a42 + 58ea5ab commit adec040
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 23 deletions.
72 changes: 64 additions & 8 deletions megalodon/src/mastodon/api_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,14 @@ namespace MastodonAPI {
): Promise<Response<T>> {
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
}
Expand Down Expand Up @@ -123,7 +130,14 @@ namespace MastodonAPI {
*/
public async put<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
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
}
Expand Down Expand Up @@ -168,7 +182,14 @@ namespace MastodonAPI {
*/
public async putForm<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
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
}
Expand Down Expand Up @@ -213,7 +234,14 @@ namespace MastodonAPI {
*/
public async patch<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
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
}
Expand Down Expand Up @@ -258,7 +286,14 @@ namespace MastodonAPI {
*/
public async patchForm<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
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
}
Expand Down Expand Up @@ -303,7 +338,14 @@ namespace MastodonAPI {
*/
public async post<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
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
}
Expand Down Expand Up @@ -339,7 +381,14 @@ namespace MastodonAPI {
*/
public async postForm<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
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
}
Expand Down Expand Up @@ -376,7 +425,14 @@ namespace MastodonAPI {
public async del<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
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
}
Expand Down
4 changes: 3 additions & 1 deletion megalodon/src/mastodon/web_socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion megalodon/src/megalodon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Mastodon from './mastodon'
import Entity from './entity'
import axios, { AxiosRequestConfig } from 'axios'
import Misskey from './misskey'
import { DEFAULT_UA } from './default'

export interface WebSocketInterface {
start(): void
Expand Down Expand Up @@ -1279,7 +1280,13 @@ type Instance = {
* @return SNS name.
*/
export const detector = async (url: string, proxyConfig: ProxyConfig | false = false): Promise<'mastodon' | 'pleroma' | 'misskey'> => {
let options: AxiosRequestConfig = {}
let options: AxiosRequestConfig = {
headers: {
'User-Agent': DEFAULT_UA,
// To avoid: https://github.com/axios/axios/issues/5346
'Accept-Encoding': 'gzip,deflate,compress'
}
}
if (proxyConfig) {
options = Object.assign(options, {
httpsAgent: proxyAgent(proxyConfig)
Expand Down
9 changes: 8 additions & 1 deletion megalodon/src/misskey/api_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,14 @@ namespace MisskeyAPI {
*/
public async post<T>(path: string, params: any = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
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
}
Expand Down
4 changes: 3 additions & 1 deletion megalodon/src/misskey/web_socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
72 changes: 64 additions & 8 deletions megalodon/src/pleroma/api_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,14 @@ namespace PleromaAPI {
public async get<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
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, {
Expand Down Expand Up @@ -346,7 +353,14 @@ namespace PleromaAPI {
*/
public async put<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
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
}
Expand Down Expand Up @@ -391,7 +405,14 @@ namespace PleromaAPI {
*/
public async putForm<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
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
}
Expand Down Expand Up @@ -436,7 +457,14 @@ namespace PleromaAPI {
*/
public async patch<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
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
}
Expand Down Expand Up @@ -481,7 +509,14 @@ namespace PleromaAPI {
*/
public async patchForm<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
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
}
Expand Down Expand Up @@ -526,7 +561,14 @@ namespace PleromaAPI {
*/
public async post<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
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
}
Expand Down Expand Up @@ -562,7 +604,14 @@ namespace PleromaAPI {
*/
public async postForm<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
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
}
Expand Down Expand Up @@ -599,7 +648,14 @@ namespace PleromaAPI {
public async del<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
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
}
Expand Down
4 changes: 3 additions & 1 deletion megalodon/src/pleroma/web_socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions megalodon/test/integration/megalodon.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ describe('detector', () => {
})
})

describe.skip('misskey', () => {
const url = 'https://sushi.ski'
describe('misskey', () => {
const url = 'https://misskey.io'
it('should be misskey', async () => {
const misskey = await detector(url)
expect(misskey).toEqual('misskey')
Expand Down

0 comments on commit adec040

Please sign in to comment.