Fix link previews for YouTube Music
This commit is contained in:
parent
0a35489696
commit
172598b354
2 changed files with 10 additions and 8 deletions
|
@ -1,4 +1,4 @@
|
||||||
// Copyright 2020 Signal Messenger, LLC
|
// Copyright 2020-2021 Signal Messenger, LLC
|
||||||
// SPDX-License-Identifier: AGPL-3.0-only
|
// SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
import { RequestInit, Response } from 'node-fetch';
|
import { RequestInit, Response } from 'node-fetch';
|
||||||
|
@ -13,6 +13,8 @@ import {
|
||||||
MIMEType,
|
MIMEType,
|
||||||
} from '../types/MIME';
|
} from '../types/MIME';
|
||||||
|
|
||||||
|
const USER_AGENT = 'WhatsApp/2';
|
||||||
|
|
||||||
const MAX_REQUEST_COUNT_WITH_REDIRECTS = 20;
|
const MAX_REQUEST_COUNT_WITH_REDIRECTS = 20;
|
||||||
|
|
||||||
// Lifted from the `fetch` spec [here][0].
|
// Lifted from the `fetch` spec [here][0].
|
||||||
|
@ -408,7 +410,7 @@ export async function fetchLinkPreviewMetadata(
|
||||||
response = await fetchWithRedirects(fetchFn, href, {
|
response = await fetchWithRedirects(fetchFn, href, {
|
||||||
headers: {
|
headers: {
|
||||||
Accept: 'text/html,application/xhtml+xml',
|
Accept: 'text/html,application/xhtml+xml',
|
||||||
'User-Agent': 'WhatsApp',
|
'User-Agent': USER_AGENT,
|
||||||
},
|
},
|
||||||
signal: abortSignal,
|
signal: abortSignal,
|
||||||
});
|
});
|
||||||
|
@ -504,7 +506,7 @@ export async function fetchLinkPreviewImage(
|
||||||
try {
|
try {
|
||||||
response = await fetchWithRedirects(fetchFn, href, {
|
response = await fetchWithRedirects(fetchFn, href, {
|
||||||
headers: {
|
headers: {
|
||||||
'User-Agent': 'WhatsApp',
|
'User-Agent': USER_AGENT,
|
||||||
},
|
},
|
||||||
size: MAX_IMAGE_CONTENT_LENGTH,
|
size: MAX_IMAGE_CONTENT_LENGTH,
|
||||||
signal: abortSignal,
|
signal: abortSignal,
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
// Copyright 2020 Signal Messenger, LLC
|
// Copyright 2020-2021 Signal Messenger, LLC
|
||||||
// SPDX-License-Identifier: AGPL-3.0-only
|
// SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
import { assert } from 'chai';
|
import { assert } from 'chai';
|
||||||
|
@ -143,7 +143,7 @@ describe('link preview fetching', () => {
|
||||||
sinon.assert.notCalled(warn);
|
sinon.assert.notCalled(warn);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('sends "WhatsApp" as the User-Agent for compatibility', async () => {
|
it('sends WhatsApp as the User-Agent for compatibility', async () => {
|
||||||
const fakeFetch = stub().resolves(makeResponse());
|
const fakeFetch = stub().resolves(makeResponse());
|
||||||
|
|
||||||
await fetchLinkPreviewMetadata(
|
await fetchLinkPreviewMetadata(
|
||||||
|
@ -157,7 +157,7 @@ describe('link preview fetching', () => {
|
||||||
'https://example.com',
|
'https://example.com',
|
||||||
sinon.match({
|
sinon.match({
|
||||||
headers: {
|
headers: {
|
||||||
'User-Agent': 'WhatsApp',
|
'User-Agent': 'WhatsApp/2',
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
@ -1273,7 +1273,7 @@ describe('link preview fetching', () => {
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('sends "WhatsApp" as the User-Agent for compatibility', async () => {
|
it('sends WhatsApp as the User-Agent for compatibility', async () => {
|
||||||
const fakeFetch = stub().resolves(new Response(null));
|
const fakeFetch = stub().resolves(new Response(null));
|
||||||
|
|
||||||
await fetchLinkPreviewImage(
|
await fetchLinkPreviewImage(
|
||||||
|
@ -1287,7 +1287,7 @@ describe('link preview fetching', () => {
|
||||||
'https://example.com/img',
|
'https://example.com/img',
|
||||||
sinon.match({
|
sinon.match({
|
||||||
headers: {
|
headers: {
|
||||||
'User-Agent': 'WhatsApp',
|
'User-Agent': 'WhatsApp/2',
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue