Skip to content

Commit

Permalink
feat: use native fetch from node (#2381)
Browse files Browse the repository at this point in the history
* chore: remove node-fetch

* chore: update node types to newest version

* fix: vendor types for opentype to prevent polluting global namespace with dom types

* feat: use native fetch

* refactor: use absolute minimum of opentype.js typings
  • Loading branch information
jbroma committed Jun 7, 2024
1 parent 1754348 commit 039f684
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 61 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"@types/glob": "^7.1.1",
"@types/jest": "^26.0.15",
"@types/node": "^18.0.0",
"@types/node-fetch": "^2.3.7",
"babel-jest": "^26.6.2",
"babel-plugin-module-resolver": "^3.2.0",
"chalk": "^4.1.2",
Expand Down
1 change: 0 additions & 1 deletion packages/cli-link-assets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
],
"devDependencies": {
"@react-native-community/cli-types": "14.0.0-alpha.5",
"@types/opentype.js": "^1.3.8",
"@types/plist": "^3.0.5",
"type-fest": "^4.10.2"
},
Expand Down
25 changes: 25 additions & 0 deletions packages/cli-link-assets/src/opentype.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Extracted from @types/opentype.js@1.3.8
// Do not use @types/opentype.js as it references lib-dom

declare module 'opentype.js' {
export interface LocalizedName {
[lang: string]: string;
}
export interface Field {
name: string;
type: string;
value: any;
}
export interface Table {
[propName: string]: any;
encode(): number[];
fields: Field[];
sizeOf(): number;
tables: Table[];
tableName: string;
}
export class Font {
tables: {[tableName: string]: Table};
}
export function parse(buffer: any): Font;
}
2 changes: 0 additions & 2 deletions packages/cli-tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"execa": "^5.0.0",
"find-up": "^5.0.0",
"mime": "^2.4.1",
"node-fetch": "^2.6.0",
"open": "^6.2.0",
"ora": "^5.4.1",
"semver": "^7.5.2",
Expand All @@ -24,7 +23,6 @@
"@types/lodash": "^4.14.149",
"@types/mime": "^2.0.1",
"@types/node": "^18.0.0",
"@types/node-fetch": "^2.5.5",
"@types/shell-quote": "^1.7.1"
},
"files": [
Expand Down
25 changes: 13 additions & 12 deletions packages/cli-tools/src/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import * as os from 'os';
import * as path from 'path';
import * as fs from 'fs';
import * as stream from 'stream';

import nodeFetch, {
RequestInit as FetchOptions,
Response,
Request,
Headers,
} from 'node-fetch';
import {CLIError} from './errors';
import logger from './logger';

Expand All @@ -31,19 +26,25 @@ const fetchToTemp = (url: string): Promise<string> => {
const fileName = path.basename(url);
const tmpDir = path.join(os.tmpdir(), fileName);

nodeFetch(url).then((result) => {
global.fetch(url).then((result) => {
if (result.status >= 400) {
return reject(`Fetch request failed with status ${result.status}`);
}

if (result.body === null) {
return reject('Fetch request failed - empty body');
}

const dest = fs.createWriteStream(tmpDir);
result.body.pipe(dest);
const body = stream.Readable.fromWeb(result.body);

body.pipe(dest);

result.body.on('end', () => {
body.on('end', () => {
resolve(tmpDir);
});

result.body.on('error', reject);
body.on('error', reject);
});
});
} catch (e) {
Expand All @@ -54,9 +55,9 @@ const fetchToTemp = (url: string): Promise<string> => {

const fetch = async (
url: string | Request,
options?: FetchOptions,
options?: RequestInit,
): Promise<{status: number; data: any; headers: Headers}> => {
const result = await nodeFetch(url, options);
const result = await global.fetch(url, options);
const data = await unwrapFetchResult(result);

if (result.status >= 400) {
Expand Down
59 changes: 14 additions & 45 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3071,29 +3071,18 @@
resolved "https://registry.yarnpkg.com/@types/minimist/-/minimist-1.2.0.tgz#69a23a3ad29caf0097f06eda59b361ee2f0639f6"
integrity sha1-aaI6OtKcrwCX8G7aWbNh7i8GOfY=

"@types/node-fetch@^2.3.7", "@types/node-fetch@^2.5.5":
version "2.5.5"
resolved "https://registry.yarnpkg.com/@types/node-fetch/-/node-fetch-2.5.5.tgz#cd264e20a81f4600a6c52864d38e7fef72485e92"
integrity sha512-IWwjsyYjGw+em3xTvWVQi5MgYKbRs0du57klfTaZkv/B24AEQ/p/IopNeqIYNy3EsfHOpg8ieQSDomPcsYMHpA==
dependencies:
"@types/node" "*"
form-data "^3.0.0"

"@types/node@*", "@types/node@^18.0.0":
version "18.17.4"
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.17.4.tgz#bf8ae9875528929cc9930dc3f066cd0481fe1231"
integrity sha512-ATL4WLgr7/W40+Sp1WnNTSKbgVn6Pvhc/2RHAdt8fl6NsQyp4oPCi2eKcGOvA494bwf1K/W6nGgZ9TwDqvpjdw==
version "18.19.31"
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.19.31.tgz#b7d4a00f7cb826b60a543cebdbda5d189aaecdcd"
integrity sha512-ArgCD39YpyyrtFKIqMDvjz79jto5fcI/SVUs2HwB+f0dAzq68yqOdyaSivLiLugSziTpNXLQrVb7RZFmdZzbhA==
dependencies:
undici-types "~5.26.4"

"@types/normalize-package-data@^2.4.0":
version "2.4.0"
resolved "https://registry.yarnpkg.com/@types/normalize-package-data/-/normalize-package-data-2.4.0.tgz#e486d0d97396d79beedd0a6e33f4534ff6b4973e"
integrity sha512-f5j5b/Gf71L+dbqxIpQ4Z2WlmI/mPJ0fOkGGmFgtb6sAu97EPczzbS3/tJKxmcYDj55OX6ssqwDAWOHIYDRDGA==

"@types/opentype.js@^1.3.8":
version "1.3.8"
resolved "https://registry.yarnpkg.com/@types/opentype.js/-/opentype.js-1.3.8.tgz#741be92429d1c2d64b5fa79cf692f74b49d6007f"
integrity sha512-H6qeTp03jrknklSn4bpT1/9+1xCAEIU2CnjcWPkicJy8A1SKuthanbvoHYMiv79/2W3Xn1XE4gfSJFzt2U3JSw==

"@types/plist@^3.0.5":
version "3.0.5"
resolved "https://registry.yarnpkg.com/@types/plist/-/plist-3.0.5.tgz#9a0c49c0f9886c8c8696a7904dd703f6284036e0"
Expand Down Expand Up @@ -8769,7 +8758,7 @@ node-addon-api@^7.0.0:
resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-7.1.0.tgz#71f609369379c08e251c558527a107107b5e0fdb"
integrity sha512-mNcltoe1R8o7STTegSOHdnJNN7s5EUvhoS7ShnTHDyOSd+8H+UdWODq6qSv67PjC8Zc5JRT8+oLAMCr0SIXw7g==

node-fetch@2.6.7, node-fetch@^2.6.0:
node-fetch@2.6.7:
version "2.6.7"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.7.tgz#24de9fba827e3b4ae44dc8b20256a379160052ad"
integrity sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==
Expand Down Expand Up @@ -10774,16 +10763,7 @@ string-natural-compare@^3.0.1:
resolved "https://registry.yarnpkg.com/string-natural-compare/-/string-natural-compare-3.0.1.tgz#7a42d58474454963759e8e8b7ae63d71c1e7fdf4"
integrity sha512-n3sPwynL1nwKi3WJ6AIsClwBMa0zTi54fn2oLU6ndfTSIO05xaznjSf15PcBZU6FNWbmN5Q6cxT4V5hGvB4taw==

"string-width-cjs@npm:string-width@^4.2.0":
version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
dependencies:
emoji-regex "^8.0.0"
is-fullwidth-code-point "^3.0.0"
strip-ansi "^6.0.1"

"string-width@^1.0.2 || 2 || 3 || 4", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
"string-width-cjs@npm:string-width@^4.2.0", "string-width@^1.0.2 || 2 || 3 || 4", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
Expand Down Expand Up @@ -10878,7 +10858,7 @@ string_decoder@~1.1.1:
dependencies:
safe-buffer "~5.1.0"

"strip-ansi-cjs@npm:strip-ansi@^6.0.1":
"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1:
version "6.0.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
Expand All @@ -10899,13 +10879,6 @@ strip-ansi@^5.0.0, strip-ansi@^5.2.0:
dependencies:
ansi-regex "^4.1.0"

strip-ansi@^6.0.0, strip-ansi@^6.0.1:
version "6.0.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
dependencies:
ansi-regex "^5.0.1"

strip-ansi@^7.0.1:
version "7.0.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-7.0.1.tgz#61740a08ce36b61e50e65653f07060d000975fb2"
Expand Down Expand Up @@ -11402,6 +11375,11 @@ unbox-primitive@^1.0.2:
has-symbols "^1.0.3"
which-boxed-primitive "^1.0.2"

undici-types@~5.26.4:
version "5.26.5"
resolved "https://registry.yarnpkg.com/undici-types/-/undici-types-5.26.5.tgz#bcd539893d00b56e964fd2657a4866b221a65617"
integrity sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==

unicode-canonical-property-names-ecmascript@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/unicode-canonical-property-names-ecmascript/-/unicode-canonical-property-names-ecmascript-2.0.0.tgz#301acdc525631670d39f6146e0e77ff6bbdebddc"
Expand Down Expand Up @@ -11779,7 +11757,7 @@ wordwrap@^1.0.0:
resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-1.0.0.tgz#27584810891456a4171c8d0226441ade90cbcaeb"
integrity sha1-J1hIEIkUVqQXHI0CJkQa3pDLyus=

"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0":
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
Expand All @@ -11797,15 +11775,6 @@ wrap-ansi@^6.0.1, wrap-ansi@^6.2.0:
string-width "^4.1.0"
strip-ansi "^6.0.0"

wrap-ansi@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
dependencies:
ansi-styles "^4.0.0"
string-width "^4.1.0"
strip-ansi "^6.0.0"

wrap-ansi@^8.1.0:
version "8.1.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-8.1.0.tgz#56dc22368ee570face1b49819975d9b9a5ead214"
Expand Down

0 comments on commit 039f684

Please sign in to comment.