Skip to content

Commit

Permalink
Replaces node-fetch with fetch-h2 on core
Browse files Browse the repository at this point in the history
- `node-fetch` doesn't support HTTP/2 and has had an issue for
  support open since 2017: node-fetch/node-fetch#342
- `fetch-h2` supports HTTP1.1 and HTTP/2 with upgrade.
  • Loading branch information
andreban committed May 7, 2021
1 parent 57d63e1 commit 64c0d36
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 41 deletions.
108 changes: 84 additions & 24 deletions packages/core/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,15 @@
"@types/lodash.template": "^4.5.0",
"@types/mime-types": "^2.1.0",
"@types/node": "^12.20.1",
"@types/node-fetch": "^2.5.8",
"@types/tar": "^4.0.4",
"@types/valid-url": "^1.0.3",
"color": "^3.1.3",
"extract-zip": "^1.7.0",
"fetch-h2": "^2.5.1",
"inquirer": "^7.3.3",
"jimp": "^0.14.0",
"lodash.template": "^4.5.0",
"mime-types": "^2.1.28",
"node-fetch": "^2.6.0",
"svg2img": "^0.8.1",
"tar": "^6.1.0",
"valid-url": "^1.0.9"
Expand Down
9 changes: 4 additions & 5 deletions packages/core/src/lib/ImageHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import * as path from 'path';
import * as fs from 'fs';
import * as Jimp from 'jimp';
import fetch from 'node-fetch';
import {fetch} from 'fetch-h2';
import Color = require('color');
import {promisify} from 'util';
import {svg2img} from './wrappers/svg2img';
Expand Down Expand Up @@ -109,7 +109,7 @@ export class ImageHelper {
* @returns an Object containing the original URL and the icon image data.
*/
async fetchIcon(iconUrl: string): Promise<Icon> {
const response = await fetch(iconUrl);
const response = await fetch(iconUrl, {redirect: 'follow'});
if (response.status !== 200) {
throw new Error(
`Failed to download icon ${iconUrl}. Responded with status ${response.status}`);
Expand All @@ -120,14 +120,13 @@ export class ImageHelper {
throw new Error(`Received icon "${iconUrl}" with invalid Content-Type.` +
` Responded with Content-Type "${contentType}"`);
}
let body;
body = await response.buffer();
let body = await response.arrayBuffer();
if (contentType.startsWith('image/svg')) {
body = await svg2img(iconUrl);
}
return {
url: iconUrl,
data: await Jimp.read(body),
data: await Jimp.read(Buffer.from(body)),
};
}
}
4 changes: 2 additions & 2 deletions packages/core/src/lib/TwaGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import * as path from 'path';
import * as fs from 'fs';
import * as Color from 'color';
import fetch from 'node-fetch';
import {fetch} from 'fetch-h2';
import {template} from 'lodash';
import {promisify} from 'util';
import {TwaManifest} from './TwaManifest';
Expand Down Expand Up @@ -265,7 +265,7 @@ export class TwaGenerator {
'Unable to write the Web Manifest. The TWA Manifest does not have a webManifestUrl');
}

const response = await fetch(twaManifest.webManifestUrl);
const response = await fetch(twaManifest.webManifestUrl.toString(), {redirect: 'follow'});
if (response.status !== 200) {
throw new Error(`Failed to download Web Manifest ${twaManifest.webManifestUrl}.` +
`Responded with status ${response.status}`);
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/lib/TwaManifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
'use strict';

import * as fs from 'fs';
import fetch from 'node-fetch';
import {fetch} from 'fetch-h2';
import {findSuitableIcon, generatePackageId, validateNotEmpty} from './util';
import Color = require('color');
import {ConsoleLog} from './Log';
Expand Down Expand Up @@ -352,7 +352,7 @@ export class TwaManifest {
* @returns {TwaManifest}
*/
static async fromWebManifest(url: string): Promise<TwaManifest> {
const response = await fetch(url);
const response = await fetch(url, {redirect: 'follow'});
const webManifest = await response.json();
const webManifestUrl: URL = new URL(url);
return TwaManifest.fromWebManifestJson(webManifestUrl, webManifest);
Expand Down
14 changes: 8 additions & 6 deletions packages/core/src/lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import * as extractZip from 'extract-zip';
import fetch from 'node-fetch';
import {fetch} from 'fetch-h2';
import * as fs from 'fs';
import {join} from 'path';
import {promisify} from 'util';
Expand Down Expand Up @@ -59,7 +59,7 @@ export async function executeFile(
*/
export async function downloadFile(url: string, path: string,
progressCallback?: (current: number, total: number) => void): Promise<void> {
const result = await fetch(url);
const result = await fetch(url, {redirect: 'follow'});

// Try to determine the file size via the `Content-Length` header. This may not be available
// for all cases.
Expand All @@ -69,20 +69,22 @@ export async function downloadFile(url: string, path: string,
const fileStream = fs.createWriteStream(path);
let received = 0;

const readableStream = await result.readable();
await new Promise((resolve, reject) => {
result.body.pipe(fileStream);
readableStream.pipe(fileStream);

// Even though we're piping the chunks, we intercept them to check for the download progress.
if (progressCallback) {
result.body.on('data', (chunk) => {
readableStream.on('data', (chunk) => {
received = received + chunk.length;
progressCallback(received, fileSize);
});
}

result.body.on('error', (err) => {
readableStream.on('error', (err) => {
reject(err);
});

fileStream.on('finish', () => {
resolve();
});
Expand Down Expand Up @@ -271,7 +273,7 @@ export async function rmdir(path: string): Promise<void> {
* @returns {Promise<WebManifestJson}
*/
export async function getWebManifest(webManifestUrl: URL): Promise<WebManifestJson> {
const response = await fetch(webManifestUrl);
const response = await fetch(webManifestUrl.toString());
if (response.status !== 200) {
throw new Error(`Failed to download Web Manifest ${webManifestUrl}. ` +
`Responded with status ${response.status}`);
Expand Down

0 comments on commit 64c0d36

Please sign in to comment.