mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-16 11:42:04 +08:00
chore: restore lazy load requires in http module (#7052)
This commit is contained in:
@ -1,14 +1,10 @@
|
||||
/**
|
||||
* Android specific http request implementation.
|
||||
*/
|
||||
import { fromNativeSource } from "../../image-source";
|
||||
import { screen } from "../../platform";
|
||||
import { File } from "../../file-system";
|
||||
// imported for definition purposes only
|
||||
import * as httpModule from "../../http";
|
||||
import * as imageSourceModule from "../../image-source";
|
||||
import * as platformModule from "../../platform";
|
||||
import * as fsModule from "../../file-system";
|
||||
|
||||
import { getFilenameFromUrl } from "./http-request-common";
|
||||
|
||||
// this is imported for definition purposes only
|
||||
import * as http from "../../http";
|
||||
|
||||
import { NetworkAgent } from "../../debugger/debugger";
|
||||
|
||||
export enum HttpResponseEncoding {
|
||||
@ -28,6 +24,27 @@ function parseJSON(source: string): any {
|
||||
let requestIdCounter = 0;
|
||||
const pendingRequests = {};
|
||||
|
||||
let imageSource: typeof imageSourceModule;
|
||||
function ensureImageSource() {
|
||||
if (!imageSource) {
|
||||
imageSource = require("image-source");
|
||||
}
|
||||
}
|
||||
|
||||
let platform: typeof platformModule;
|
||||
function ensurePlatform() {
|
||||
if (!platform) {
|
||||
platform = require("platform");
|
||||
}
|
||||
}
|
||||
|
||||
let fs: typeof fsModule;
|
||||
function ensureFileSystem() {
|
||||
if (!fs) {
|
||||
fs = require("file-system");
|
||||
}
|
||||
}
|
||||
|
||||
let completeCallback: org.nativescript.widgets.Async.CompleteCallback;
|
||||
function ensureCompleteCallback() {
|
||||
if (completeCallback) {
|
||||
@ -55,7 +72,7 @@ function onRequestComplete(requestId: number, result: org.nativescript.widgets.A
|
||||
}
|
||||
|
||||
// read the headers
|
||||
const headers: http.Headers = {};
|
||||
const headers: httpModule.Headers = {};
|
||||
if (result.headers) {
|
||||
const jHeaders = result.headers;
|
||||
const length = jHeaders.size();
|
||||
@ -97,9 +114,11 @@ function onRequestComplete(requestId: number, result: org.nativescript.widgets.A
|
||||
return parseJSON(str);
|
||||
},
|
||||
toImage: () => {
|
||||
ensureImageSource();
|
||||
|
||||
return new Promise<any>((resolveImage, rejectImage) => {
|
||||
if (result.responseAsImage != null) {
|
||||
resolveImage(fromNativeSource(result.responseAsImage));
|
||||
resolveImage(imageSource.fromNativeSource(result.responseAsImage));
|
||||
}
|
||||
else {
|
||||
rejectImage(new Error("Response content may not be converted to an Image"));
|
||||
@ -107,13 +126,15 @@ function onRequestComplete(requestId: number, result: org.nativescript.widgets.A
|
||||
});
|
||||
},
|
||||
toFile: (destinationFilePath: string) => {
|
||||
ensureFileSystem();
|
||||
|
||||
if (!destinationFilePath) {
|
||||
destinationFilePath = getFilenameFromUrl(callbacks.url);
|
||||
}
|
||||
let stream: java.io.FileOutputStream;
|
||||
try {
|
||||
// ensure destination path exists by creating any missing parent directories
|
||||
const file = File.fromPath(destinationFilePath);
|
||||
const file = fs.File.fromPath(destinationFilePath);
|
||||
|
||||
const javaFile = new java.io.File(destinationFilePath);
|
||||
stream = new java.io.FileOutputStream(javaFile);
|
||||
@ -144,7 +165,7 @@ function onRequestError(error: string, requestId: number) {
|
||||
}
|
||||
}
|
||||
|
||||
function buildJavaOptions(options: http.HttpRequestOptions) {
|
||||
function buildJavaOptions(options: httpModule.HttpRequestOptions) {
|
||||
if (typeof options.url !== "string") {
|
||||
throw new Error("Http request must provide a valid url.");
|
||||
}
|
||||
@ -177,20 +198,23 @@ function buildJavaOptions(options: http.HttpRequestOptions) {
|
||||
javaOptions.headers = arrayList;
|
||||
}
|
||||
|
||||
ensurePlatform();
|
||||
|
||||
// pass the maximum available image size to the request options in case we need a bitmap conversion
|
||||
javaOptions.screenWidth = screen.mainScreen.widthPixels;
|
||||
javaOptions.screenHeight = screen.mainScreen.heightPixels;
|
||||
const screen = platform.screen.mainScreen;
|
||||
javaOptions.screenWidth = screen.widthPixels;
|
||||
javaOptions.screenHeight = screen.heightPixels;
|
||||
|
||||
return javaOptions;
|
||||
}
|
||||
|
||||
export function request(options: http.HttpRequestOptions): Promise<http.HttpResponse> {
|
||||
export function request(options: httpModule.HttpRequestOptions): Promise<httpModule.HttpResponse> {
|
||||
if (options === undefined || options === null) {
|
||||
// TODO: Shouldn't we throw an error here - defensive programming
|
||||
return;
|
||||
}
|
||||
|
||||
return new Promise<http.HttpResponse>((resolve, reject) => {
|
||||
return new Promise<httpModule.HttpResponse>((resolve, reject) => {
|
||||
try {
|
||||
// initialize the options
|
||||
const javaOptions = buildJavaOptions(options);
|
||||
@ -228,7 +252,7 @@ function decodeResponse(raw: any, encoding?: HttpResponseEncoding) {
|
||||
return raw.toString(charsetName)
|
||||
}
|
||||
|
||||
export function addHeader(headers: http.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])) {
|
||||
|
@ -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])) {
|
||||
|
Reference in New Issue
Block a user