Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
birdofpreyru committed Jan 31, 2024
2 parents f77c555 + d01f8a8 commit 87c4961
Show file tree
Hide file tree
Showing 6 changed files with 355 additions and 233 deletions.
70 changes: 41 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
# React Native Static Server

<!-- Status Badges & Important Links -->

[![Latest NPM Release](https://img.shields.io/npm/v/@dr.pogodin/react-native-static-server.svg)](https://www.npmjs.com/package/@dr.pogodin/react-native-static-server)
[![NPM Downloads](https://img.shields.io/npm/dm/@dr.pogodin/react-native-static-server.svg)](https://www.npmjs.com/package/@dr.pogodin/react-native-static-server)
[![CircleCI](https://dl.circleci.com/status-badge/img/gh/birdofpreyru/react-native-static-server/tree/master.svg?style=shield)](https://app.circleci.com/pipelines/github/birdofpreyru/react-native-static-server)
[![GitHub Repo stars](https://img.shields.io/github/stars/birdofpreyru/react-native-static-server?style=social)](https://github.com/birdofpreyru/react-native-static-server)
[![Dr. Pogodin Studio](https://raw.githubusercontent.com/birdofpreyru/react-native-static-server/master/.README/logo-dr-pogodin-studio.svg)](https://dr.pogodin.studio/docs/react-native-static-server)

<!-- Misc links -->
[GCDWebServer]: https://github.com/swisspol/GCDWebServer
[NanoHttpd]: https://github.com/NanoHttpd/nanohttpd
<!-- Misc references -->

[@dr.pogodin/react-native-fs]: https://www.npmjs.com/package/@dr.pogodin/react-native-fs
[copyFileAssets()]: https://github.com/birdofpreyru/react-native-fs?tab=readme-ov-file#copyfileassets
[Error]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error
[Example App]: https://github.com/birdofpreyru/react-native-static-server/tree/master/example
[Expo]: https://expo.dev
[getDeviceType()]: https://www.npmjs.com/package/react-native-device-info#getDeviceType
[Lighttpd]: https://www.lighttpd.net
[MainBundlePath]: https://www.npmjs.com/package/@dr.pogodin/react-native-fs#mainbundlepath
[mod_alias]: https://redmine.lighttpd.net/projects/lighttpd/wiki/Mod_alias
[mod_rewrite]: https://redmine.lighttpd.net/projects/lighttpd/wiki/Mod_rewrite
[mod_webdav]: https://redmine.lighttpd.net/projects/lighttpd/wiki/Mod_webdav
[New Architecture]: https://reactnative.dev/docs/the-new-architecture/landing-page
[Old Architecture]: https://reactnative.dev/docs/native-modules-intro
[Promise]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
[react-native-device-info]: https://www.npmjs.com/package/react-native-device-info
[React Native]: https://reactnative.dev
[TemporaryDirectoryPath]: https://github.com/birdofpreyru/react-native-fs?tab=readme-ov-file#temporarydirectorypath
[WebDAV]: https://en.wikipedia.org/wiki/WebDAV

<!-- The regular README content starts here -->

Embedded HTTP server for [React Native] applications for Android, iOS, Mac (Catalyst),
and Windows platforms. Powered by [Lighttpd] server, supports both [new][New Architecture]
Expand Down Expand Up @@ -42,23 +58,6 @@ and [old][Old Architecture] RN architectures.

[![Logo](https://avatars.githubusercontent.com/u/71085569?s=36)](https://github.com/Lumentric)

<!-- links -->
[@dr.pogodin/react-native-fs]: https://www.npmjs.com/package/@dr.pogodin/react-native-fs
[Error]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error
[example app]: https://github.com/birdofpreyru/react-native-static-server/tree/master/example
[Expo]: https://expo.dev
[OLD-README.md]: https://github.com/birdofpreyru/react-native-static-server/blob/master/OLD-README.md
[getDeviceType()]: https://www.npmjs.com/package/react-native-device-info#getDeviceType
[MainBundlePath]: https://www.npmjs.com/package/@dr.pogodin/react-native-fs#mainbundlepath
[mod_alias]: https://redmine.lighttpd.net/projects/lighttpd/wiki/Mod_alias
[mod_rewrite]: https://redmine.lighttpd.net/projects/lighttpd/wiki/Mod_rewrite
[mod_webdav]: https://redmine.lighttpd.net/projects/lighttpd/wiki/Mod_webdav
[react-native-device-info]: https://www.npmjs.com/package/react-native-device-info
[react-native-fs]: https://www.npmjs.com/package/react-native-fs
[React Native]: https://reactnative.dev
[TemporaryDirectoryPath]: https://www.npmjs.com/package/@dr.pogodin/react-native-fs#temporarydirectorypath
[WebDAV]: https://en.wikipedia.org/wiki/WebDAV

## Content

- [Getting Started](#getting-started)
Expand Down Expand Up @@ -87,8 +86,8 @@ and [old][Old Architecture] RN architectures.
- [.state] &mdash; Holds the current server state.
- [.stopInBackground] &mdash; Holds `stopInBackground` value provided to
[constructor()].
- [extractBundledAssets()] &mdash; Extracts bundled assets into a regular folder
(Android-specific).
- ~~[extractBundledAssets()] &mdash; Extracts bundled assets into a regular folder
(Android-specific).~~
- [getActiveServer()] &mdash; Gets currently active, starting, or stopping
server instance, if any, according to the TS layer data.
- [getActiveServerId()] &mdash; Gets ID of the currently active, starting, or
Expand All @@ -108,7 +107,7 @@ and [old][Old Architecture] RN architectures.

- **Note:**

- This library's repository includes [Example App][example app].
- This library's repository includes [Example App].
Have a look, try to build it, in addition to following the instructions
below.

Expand Down Expand Up @@ -195,7 +194,7 @@ and [old][Old Architecture] RN architectures.
(`com.apple.security.network.server` entitlement).
- If you bundle inside your app the assets to serve by the server,
keep in mind that in Mac Catalyst build they'll end up in a different
path, compared to the regular iOS bundle (see [example app]): \
path, compared to the regular iOS bundle (see [Example App]): \
iOS: "[MainBundlePath]`/webroot`"; \
Mac Catalyst: "[MainBundlePath]`/Content/Resources/webroot`".
Expand Down Expand Up @@ -309,21 +308,24 @@ outside platform-specific sub-folders.
- On Android the server cannot access bundled assets as regular files, thus
before starting the server to serve them, these assets should be extracted
into a folder accessible to the server (_e.g._ app's document folder).
To facilitate it, this library provides [extractBundledAssets()] function.
You want to use it in this manner:
You can use [copyFileAssets()] function from [@dr.pogodin/react-native-fs]
library (v2.24.1 and above):
```jsx
// TODO: To be updated, see a better code inside the example app.
import { Platform } from 'react-native';
import {
copyFileAssets,
DocumentDirectoryPath,
exists,
resolveAssetsPath,
unlink,
} from '@dr.pogodin/react-native-fs';
import {extractBundledAssets} from '@dr.pogodin/react-native-static-server';
async function prepareAssets() {
if (Platform.OS !== 'android') return;
const targetWebrootPathOnDevice = resolveAssetsPath('webroot');
// It is use-case specific, but in general if target webroot path exists
Expand All @@ -348,7 +350,7 @@ outside platform-specific sub-folders.
// This function is a noop on other platforms than Android, thus no need
// to guard against the platform.
await extractBundledAssets(targetWebrootPathOnDevice, 'webroot');
await copyFileAssets('webroot', targetWebrootPathOnDevice);
}
// "webroot" assets have been extracted into the target folder, which now
Expand Down Expand Up @@ -833,6 +835,16 @@ server.stopInBackground: boolean;
Readonly property. It holds `stopInBackground` value provided to [constructor()].
### extractBundledAssets()
**DEPRECATED!** _Use instead [copyFileAssets()] from
the [@dr.pogodin/react-native-fs] library v2.24.1 and above &mdash;
it does the same job in a more efficient way (it is implemented entirely
in the native layer, thus does not incur the overhead of recurrent
communication between the native and JS layers during the operation)._
_The [extractBundledAssets()], with its original implementation, will be kept
around for backward compatibility, but it will be removed in future!_
[extractBundledAssets()]: #extractbundledassets
```ts
import {extractBundledAssets} from '@dr.pogodin/react-native-static-server';
Expand Down
14 changes: 7 additions & 7 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@
"windows": "react-native run-windows"
},
"dependencies": {
"@dr.pogodin/react-native-fs": "^2.22.0",
"@dr.pogodin/react-native-fs": "^2.24.1",
"react": "18.2.0",
"react-native": "0.73.2",
"react-native-webview": "^13.6.4",
"react-native-windows": "^0.73.4"
},
"devDependencies": {
"@babel/core": "^7.23.7",
"@babel/preset-env": "^7.23.8",
"@babel/runtime": "^7.23.8",
"@react-native/babel-preset": "^0.73.19",
"@react-native/metro-config": "^0.73.3",
"@babel/core": "^7.23.9",
"@babel/preset-env": "^7.23.9",
"@babel/runtime": "^7.23.9",
"@react-native/babel-preset": "^0.73.20",
"@react-native/metro-config": "^0.73.4",
"@react-native/typescript-config": "^0.73.1",
"babel-plugin-module-resolver": "^5.0.0",
"metro-config": "^0.80.4",
"metro-config": "^0.80.5",
"pod-install": "^0.2.0"
},
"engines": {
Expand Down
10 changes: 7 additions & 3 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ import {

import { Colors } from 'react-native/Libraries/NewAppScreen';

import { readFile, readFileAssets, unlink } from '@dr.pogodin/react-native-fs';
import {
copyFileAssets,
readFile,
readFileAssets,
unlink,
} from '@dr.pogodin/react-native-fs';

import { WebView } from 'react-native-webview';

import Server, {
STATES,
extractBundledAssets,
resolveAssetsPath,
} from '@dr.pogodin/react-native-static-server';

Expand Down Expand Up @@ -109,7 +113,7 @@ export default function App() {
}
if (extract) {
console.log('Extracting web server assets...');
await extractBundledAssets(fileDir, 'webroot');
await copyFileAssets('webroot', fileDir);
}
}

Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dr.pogodin/react-native-static-server",
"version": "0.10.4",
"version": "0.10.5",
"description": "HTTP static file server for React Native",
"main": "lib/commonjs/index",
"module": "lib/module/index",
Expand Down Expand Up @@ -50,17 +50,17 @@
"registry": "https://registry.npmjs.org/"
},
"devDependencies": {
"@dr.pogodin/react-native-fs": "^2.22.0",
"@dr.pogodin/react-native-fs": "^2.24.1",
"@react-native/eslint-config": "^0.73.2",
"@types/jest": "^29.5.11",
"@types/react": "^18.2.48",
"@types/uuid": "^9.0.7",
"@types/uuid": "^9.0.8",
"del-cli": "^5.1.0",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.3",
"jest": "^29.7.0",
"metro-config": "^0.80.4",
"metro-config": "^0.80.5",
"pod-install": "^0.2.0",
"prettier": "^3.2.4",
"react": "18.2.0",
Expand Down
8 changes: 6 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -434,8 +434,8 @@ class StaticServer {
if (!this._stateChangeEmitter.hasListeners) {
throw Error(
starting
? 'Server auto-start on the app going into foreground failed'
: 'Server auto-stop on the app going into background failed',
? `Server (#${this._id}) auto-start on the app going into foreground failed`
: `Server (#${this._id}) auto-stop on the app going into background failed`,
);
}
}
Expand Down Expand Up @@ -463,6 +463,10 @@ export async function extractBundledAssets(
into = DocumentDirectoryPath,
from = '',
) {
console.warn(
'extractBundledAssets() is deprecated! See: https://github.com/birdofpreyru/react-native-static-server?tab=readme-ov-file#extractbundledassets',
);

if (Platform.OS !== 'android') return;

await mkdir(into);
Expand Down
Loading

0 comments on commit 87c4961

Please sign in to comment.