Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to override Content-Type header for responses #119

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
create mimeTypeOverrides argument to override contentType
  • Loading branch information
Ben Pham committed Aug 18, 2022
commit 38fc2da2ba2c6d035120c88d47cceb5320810436
6 changes: 4 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -2,7 +2,8 @@ declare module 'react-native-static-server' {
type Options = {
localOnly?: boolean
keepAlive?: boolean
}
mimeTypeOverrides?: Record<string, string>
};

export default class StaticServer {
constructor(port: number, root?: string, opts?: Options)
@@ -13,10 +14,11 @@ declare module 'react-native-static-server' {
keepAlive: boolean
started: boolean
_origin?: string
mimeTypeOverrides: Record<string, string>

start: () => Promise<string>
stop: () => Promise<any>
isRunning: () => Promise<boolean>
kill: () => void
}
}
}
16 changes: 11 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ import {
NativeModules,
AppState,
Platform
} from 'react-native';
} from 'react-native';

const { FPStaticServer } = NativeModules;

@@ -18,37 +18,43 @@ class StaticServer {
this.root = root || ROOT;
this.localOnly = (opts && opts.localOnly) || false;
this.keepAlive = (opts && opts.keepAlive) || false;
this.mimeTypeOverrides = opts.mimeTypeOverrides || null;
break;
case 2:
this.port = `${port}`;
if (typeof(arguments[1]) === 'string') {
if (typeof (arguments[1]) === 'string') {
this.root = root;
this.localOnly = false;
this.keepAlive = false;
this.mimeTypeOverrides = null;
} else {
this.root = ROOT;
this.localOnly = (arguments[1] && arguments[1].localOnly) || false;
this.keepAlive = (arguments[1] && arguments[1].keepAlive) || false;
this.mimeTypeOverrides = arguments[1].mimeTypeOverrides || null;
}
break;
case 1:
if (typeof(arguments[0]) === 'number') {
if (typeof (arguments[0]) === 'number') {
this.port = `${port}`;
this.root = ROOT;
this.localOnly = false;
this.keepAlive = false;
this.mimeTypeOverrides = null;
} else {
this.port = PORT;
this.root = ROOT;
this.localOnly = (arguments[0] && arguments[0].localOnly) || false;
this.keepAlive = (arguments[0] && arguments[0].keepAlive) || false;
this.mimeTypeOverrides = arguments[0].mimeTypeOverrides || null;
}
break;
default:
this.port = PORT;
this.root = ROOT;
this.localOnly = false;
this.keepAlive = false;
this.mimeTypeOverrides = null;
}


@@ -58,7 +64,7 @@ class StaticServer {
}

start() {
if( this.running ){
if (this.running) {
return Promise.resolve(this.origin);
}

@@ -69,7 +75,7 @@ class StaticServer {
AppState.addEventListener('change', this._handleAppStateChangeFn);
}

return FPStaticServer.start(this.port, this.root, this.localOnly, this.keepAlive)
return FPStaticServer.start(this.port, this.root, this.localOnly, this.keepAlive, this.mimeTypeOverrides)
.then((origin) => {
this._origin = origin;
return origin;
19 changes: 10 additions & 9 deletions ios/FPStaticServer.h
Original file line number Diff line number Diff line change
@@ -6,17 +6,18 @@
#import "GCDWebServerFileResponse.h"
#import "GCDWebServerHTTPStatusCodes.h"

@interface FPStaticServer : NSObject <RCTBridgeModule> {
GCDWebServer* _webServer;
@interface FPStaticServer : NSObject <RCTBridgeModule>
{
GCDWebServer *_webServer;
}

@property(nonatomic, retain) NSString *localPath;
@property(nonatomic, retain) NSString *url;
@property(nonatomic, retain) NSString *localPath;
@property(nonatomic, retain) NSString *url;

@property (nonatomic, retain) NSString* www_root;
@property (nonatomic, retain) NSNumber* port;
@property (assign) BOOL localhost_only;
@property (assign) BOOL keep_alive;
@property(nonatomic, retain) NSString *www_root;
@property(nonatomic, retain) NSNumber *port;
@property(nonatomic, retain) NSDictionary<NSString *, NSString *> *mime_type_overrides;
@property(assign) BOOL localhost_only;
@property(assign) BOOL keep_alive;

@end

13 changes: 8 additions & 5 deletions ios/FPStaticServer.m
Original file line number Diff line number Diff line change
@@ -30,10 +30,11 @@ - (dispatch_queue_t)methodQueue
}


RCT_EXPORT_METHOD(start: (NSString *)port
RCT_EXPORT_METHOD(start:(NSString *)port
root:(NSString *)optroot
localOnly:(BOOL *)localhost_only
keepAlive:(BOOL *)keep_alive
mimeTypeOverrides:(NSDictionary *)mime_type_overrides
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {

@@ -67,12 +68,14 @@ - (dispatch_queue_t)methodQueue

self.localhost_only = localhost_only;

self.mime_type_overrides = mime_type_overrides;

if(_webServer.isRunning != NO) {
NSLog(@"StaticServer already running at %@", self.url);
resolve(self.url);
return;
}

//[_webServer addGETHandlerForBasePath:@"/" directoryPath:self.www_root indexFilename:@"index.html" cacheAge:3600 allowRangeRequests:YES];
NSString *basePath = @"/";
NSString *directoryPath = self.www_root;
@@ -104,11 +107,11 @@ - (dispatch_queue_t)methodQueue
response = [GCDWebServerResponse responseWithStatusCode:kGCDWebServerHTTPStatusCode_NotFound];
}
} else if ([fileType isEqualToString:NSFileTypeRegular]) {
if (allowRangeRequests) {
response = [GCDWebServerFileResponse responseWithFile:filePath byteRange:request.byteRange];
if (allowRangeRequests) {
response = [[GCDWebServerFileResponse alloc] initWithFile:filePath byteRange:request.byteRange isAttachment:NO mimeTypeOverrides:mime_type_overrides] ;
[response setValue:@"bytes" forAdditionalHeader:@"Accept-Ranges"];
} else {
response = [GCDWebServerFileResponse responseWithFile:filePath];
response = [[GCDWebServerFileResponse alloc] initWithFile:filePath byteRange:NSMakeRange(NSUIntegerMax, 0) isAttachment:NO mimeTypeOverrides:mime_type_overrides];
}
}
}