Skip to content

Commit

Permalink
Configure react-native specific build
Browse files Browse the repository at this point in the history
  • Loading branch information
perry-mitchell committed Feb 24, 2024
1 parent d10e467 commit 0f53c7c
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 272 deletions.
213 changes: 35 additions & 178 deletions package-lock.json

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

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
"exports": {
".": {
"browser": "./dist/web/index.js",
"react-native": "./dist/react-native/index.js",
"default": "./dist/node/index.js"
},
"./web": "./dist/web/index.js",
"./dist/*": "./dist/*"
},
"react-native": "./dist/web/index.js",
"react-native": "./dist/react-native/index.js",
"type": "module",
"types": "./dist/node/index.d.ts",
"scripts": {
Expand Down Expand Up @@ -118,12 +119,12 @@
"resolve-typescript-plugin": "^2.0.0",
"rimraf": "^3.0.2",
"sinon": "^14.0.2",
"speed-measure-webpack-plugin": "^1.5.0",
"ts-node": "^10.9.1",
"typescript": "^4.9.3",
"wait-on": "^6.0.1",
"webdav-server": "^2.6.2",
"webpack": "^5.75.0",
"webpack-cli": "^5.0.0"
"webpack-cli": "^5.0.0",
"webpack-merge": "^5.10.0"
}
}
11 changes: 6 additions & 5 deletions source/compat/env.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
declare var WEB: boolean;
declare var TARGET: "web" | "react-native" | undefined;

export function isReactNative(): boolean {
return typeof TARGET === "string" && TARGET === "react-native";
}

export function isWeb(): boolean {
if (typeof WEB === "boolean" && WEB === true) {
return true;
}
return false;
return typeof TARGET === "string" && TARGET === "web";
}
4 changes: 2 additions & 2 deletions source/operations/getFileContents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Layerr } from "layerr";
import { joinURL } from "../tools/url.js";
import { encodePath } from "../tools/path.js";
import { fromBase64 } from "../tools/encode.js";
import { isWeb } from "../compat/env.js";
import { isReactNative, isWeb } from "../compat/env.js";
import { request, prepareRequestOptions } from "../request.js";
import { handleResponseCode, processResponsePayload } from "../response.js";
import {
Expand Down Expand Up @@ -53,7 +53,7 @@ async function getFileContentsBuffer(
const response = await request(requestOptions);
handleResponseCode(context, response);
let body: BufferLike;
if (isWeb()) {
if (isWeb() || isReactNative()) {
body = await response.arrayBuffer();
} else {
body = Buffer.from(await response.arrayBuffer());
Expand Down
3 changes: 2 additions & 1 deletion source/operations/putFileContents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { fromBase64 } from "../tools/encode.js";
import { joinURL } from "../tools/url.js";
import { encodePath } from "../tools/path.js";
import { calculateDataLength } from "../tools/size.js";
import { isWeb } from "../compat/env.js";
import { isReactNative, isWeb } from "../compat/env.js";
import { request, prepareRequestOptions } from "../request.js";
import { handleResponseCode } from "../response.js";
import {
Expand All @@ -29,6 +29,7 @@ export async function putFileContents(
};
if (
!isWeb() &&
!isReactNative() &&
typeof Stream !== "undefined" &&
typeof Stream?.Readable !== "undefined" &&
data instanceof Stream.Readable
Expand Down
4 changes: 2 additions & 2 deletions source/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Agent as HTTPSAgent } from "https";
import { fetch } from "@buttercup/fetch";
import type { RequestInit as RequestInitNF } from "node-fetch";
import { getPatcher } from "./compat/patcher.js";
import { isWeb } from "./compat/env.js";
import { isReactNative, isWeb } from "./compat/env.js";
import { generateDigestAuthHeader, parseDigestAuth } from "./auth/digest.js";
import { cloneShallow, merge } from "./tools/merge.js";
import { mergeHeaders } from "./tools/headers.js";
Expand Down Expand Up @@ -54,7 +54,7 @@ function getFetchOptions(requestOptions: RequestOptions): RequestInit | RequestI
(opts as RequestInit).credentials = "include";
}
// Check for node-specific options
if (!isWeb()) {
if (!isWeb() && !isReactNative()) {
if (requestOptions.httpAgent || requestOptions.httpsAgent) {
(opts as RequestInitNF).agent = (parsedURL: URL) => {
if (parsedURL.protocol === "http:") {
Expand Down
4 changes: 2 additions & 2 deletions source/tools/body.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import Stream from "stream";
import { isArrayBuffer } from "../compat/arrayBuffer.js";
import { isBuffer } from "../compat/buffer.js";
import { isWeb } from "../compat/env.js";
import { isReactNative, isWeb } from "../compat/env.js";
import { Headers, RequestDataPayload } from "../types.js";

export function requestDataToFetchBody(data: RequestDataPayload): [BodyInit, Headers] {
if (!isWeb() && data instanceof Stream.Readable) {
if (!isWeb() && !isReactNative() && data instanceof Stream.Readable) {
// @ts-ignore
return [data, {}];
}
Expand Down
Loading

0 comments on commit 0f53c7c

Please sign in to comment.