chore: restore lazy load requires in http module (#7052)

This commit is contained in:
Manol Donev
2019-03-22 11:14:37 +02:00
committed by GitHub
parent 31f5554d6d
commit 32646be19c
2 changed files with 73 additions and 34 deletions

View File

@ -1,18 +1,15 @@
/**
* iOS specific http request implementation.
*/
// imported for definition purposes only
import * as httpModule from "../../http";
import * as imageSourceModule from "../../image-source";
import * as fsModule from "../../file-system";
import { HttpRequestOptions, HttpResponse, Headers } from "../../http";
import * as types from "../../utils/types";
import { fromNativeSource } from "../../image-source";
import { File } from "../../file-system";
import * as utils from "../../utils/utils";
import getter = utils.ios.getter;
import * as domainDebugger from "../../debugger/debugger";
import { getFilenameFromUrl } from "./http-request-common";
const getter = utils.ios.getter;
export enum HttpResponseEncoding {
UTF8,
GBK
@ -59,8 +56,22 @@ function ensureSessionNotFollowingRedirects() {
}
}
export function request(options: HttpRequestOptions): Promise<HttpResponse> {
return new Promise<HttpResponse>((resolve, reject) => {
let imageSource: typeof imageSourceModule;
function ensureImageSource() {
if (!imageSource) {
imageSource = require("image-source");
}
}
let fs: typeof fsModule;
function ensureFileSystem() {
if (!fs) {
fs = require("file-system");
}
}
export function request(options: httpModule.HttpRequestOptions): Promise<httpModule.HttpResponse> {
return new Promise<httpModule.HttpResponse>((resolve, reject) => {
if (!options.url) {
reject(new Error("Request url was empty."));
@ -106,7 +117,7 @@ export function request(options: HttpRequestOptions): Promise<HttpResponse> {
if (error) {
reject(new Error(error.localizedDescription));
} else {
const headers: Headers = {};
const headers: httpModule.Headers = {};
if (response && response.allHeaderFields) {
const headerFields = response.allHeaderFields;
@ -136,10 +147,12 @@ export function request(options: HttpRequestOptions): Promise<HttpResponse> {
toString: (encoding?: any) => NSDataToString(data, encoding),
toJSON: (encoding?: any) => parseJSON(NSDataToString(data, encoding)),
toImage: () => {
ensureImageSource();
return new Promise((resolve, reject) => {
(<any>UIImage).tns_decodeImageWithDataCompletion(data, image => {
if (image) {
resolve(fromNativeSource(image));
resolve(imageSource.fromNativeSource(image));
} else {
reject(new Error("Response content may not be converted to an Image"));
}
@ -147,12 +160,14 @@ export function request(options: HttpRequestOptions): Promise<HttpResponse> {
});
},
toFile: (destinationFilePath?: string) => {
ensureFileSystem();
if (!destinationFilePath) {
destinationFilePath = getFilenameFromUrl(options.url);
}
if (data instanceof NSData) {
// ensure destination path exists by creating any missing parent directories
const file = File.fromPath(destinationFilePath);
const file = fs.File.fromPath(destinationFilePath);
data.writeToFileAtomically(destinationFilePath, true);
@ -192,7 +207,7 @@ function NSDataToString(data: any, encoding?: HttpResponseEncoding): string {
return NSString.alloc().initWithDataEncoding(data, code).toString();
}
export function addHeader(headers: Headers, key: string, value: string): void {
export function addHeader(headers: httpModule.Headers, key: string, value: string): void {
if (!headers[key]) {
headers[key] = value;
} else if (Array.isArray(headers[key])) {