From 38fc2da2ba2c6d035120c88d47cceb5320810436 Mon Sep 17 00:00:00 2001 From: Ben Pham Date: Thu, 18 Aug 2022 23:03:07 +1000 Subject: [PATCH 1/2] create mimeTypeOverrides argument to override contentType --- index.d.ts | 6 ++++-- index.js | 16 +++++++++++----- ios/FPStaticServer.h | 19 ++++++++++--------- ios/FPStaticServer.m | 13 ++++++++----- 4 files changed, 33 insertions(+), 21 deletions(-) diff --git a/index.d.ts b/index.d.ts index 96be67a1..7eafc980 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 c3401c2c..e0b839b7 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,30 +18,35 @@ 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: @@ -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 9ca3edba..3e8cd123 100644 --- a/ios/FPStaticServer.h +++ b/ios/FPStaticServer.h @@ -6,17 +6,18 @@ #import "GCDWebServerFileResponse.h" #import "GCDWebServerHTTPStatusCodes.h" -@interface FPStaticServer : NSObject { - GCDWebServer* _webServer; +@interface FPStaticServer : NSObject +{ + 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 *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 d011f5d7..7994cf01 100644 --- a/ios/FPStaticServer.m +++ b/ios/FPStaticServer.m @@ -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]; } } } From fea59e4de2642a5aac84c78278d9ad1076545aaa Mon Sep 17 00:00:00 2001 From: Ben Pham Date: Fri, 19 Aug 2022 09:46:51 +1000 Subject: [PATCH 2/2] fix format --- index.js | 6 +++--- ios/FPStaticServer.h | 17 ++++++++--------- ios/FPStaticServer.m | 4 ++-- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index e0b839b7..2e250ab9 100644 --- a/index.js +++ b/index.js @@ -22,7 +22,7 @@ class StaticServer { 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; @@ -35,7 +35,7 @@ class StaticServer { } break; case 1: - if (typeof (arguments[0]) === 'number') { + if (typeof(arguments[0]) === 'number') { this.port = `${port}`; this.root = ROOT; this.localOnly = false; @@ -64,7 +64,7 @@ class StaticServer { } start() { - if (this.running) { + if( this.running ) { return Promise.resolve(this.origin); } diff --git a/ios/FPStaticServer.h b/ios/FPStaticServer.h index 3e8cd123..e4ea0a72 100644 --- a/ios/FPStaticServer.h +++ b/ios/FPStaticServer.h @@ -6,18 +6,17 @@ #import "GCDWebServerFileResponse.h" #import "GCDWebServerHTTPStatusCodes.h" -@interface FPStaticServer : NSObject -{ +@interface FPStaticServer : NSObject { 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(nonatomic, retain) NSDictionary *mime_type_overrides; -@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 7994cf01..0eee147a 100644 --- a/ios/FPStaticServer.m +++ b/ios/FPStaticServer.m @@ -30,7 +30,7 @@ - (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 @@ -107,7 +107,7 @@ - (dispatch_queue_t)methodQueue response = [GCDWebServerResponse responseWithStatusCode:kGCDWebServerHTTPStatusCode_NotFound]; } } else if ([fileType isEqualToString:NSFileTypeRegular]) { - if (allowRangeRequests) { + if (allowRangeRequests) { response = [[GCDWebServerFileResponse alloc] initWithFile:filePath byteRange:request.byteRange isAttachment:NO mimeTypeOverrides:mime_type_overrides] ; [response setValue:@"bytes" forAdditionalHeader:@"Accept-Ranges"]; } else {