diff --git a/index.d.ts b/index.d.ts index 96be67a..7eafc98 100644 --- a/index.d.ts +++ b/index.d.ts @@ -2,7 +2,8 @@ declare module 'react-native-static-server' { type Options = { localOnly?: boolean keepAlive?: boolean - } + mimeTypeOverrides?: Record + }; 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 start: () => Promise stop: () => Promise isRunning: () => Promise kill: () => void } -} \ No newline at end of file +} diff --git a/index.js b/index.js index c3401c2..2e250ab 100644 --- a/index.js +++ b/index.js @@ -2,7 +2,7 @@ import { NativeModules, AppState, Platform - } from 'react-native'; +} from 'react-native'; const { FPStaticServer } = NativeModules; @@ -18,6 +18,7 @@ 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}`; @@ -25,10 +26,12 @@ class StaticServer { 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: @@ -37,11 +40,13 @@ class StaticServer { 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: @@ -49,6 +54,7 @@ class StaticServer { 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; diff --git a/ios/FPStaticServer.h b/ios/FPStaticServer.h index 9ca3edb..e4ea0a7 100644 --- a/ios/FPStaticServer.h +++ b/ios/FPStaticServer.h @@ -7,16 +7,16 @@ #import "GCDWebServerHTTPStatusCodes.h" @interface FPStaticServer : NSObject { - GCDWebServer* _webServer; + GCDWebServer *_webServer; } @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 *mime_type_overrides; + @property(assign) BOOL localhost_only; + @property(assign) BOOL keep_alive; @end - diff --git a/ios/FPStaticServer.m b/ios/FPStaticServer.m index d011f5d..0eee147 100644 --- a/ios/FPStaticServer.m +++ b/ios/FPStaticServer.m @@ -34,6 +34,7 @@ - (dispatch_queue_t)methodQueue 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; @@ -105,10 +108,10 @@ - (dispatch_queue_t)methodQueue } } else if ([fileType isEqualToString:NSFileTypeRegular]) { if (allowRangeRequests) { - response = [GCDWebServerFileResponse responseWithFile:filePath byteRange:request.byteRange]; + 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]; } } }