mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-15 19:26:42 +08:00
add calls to the network agent (Chrome DevTools) inside http-request.android when making http requests if debugging is enabled
This commit is contained in:
@ -44,3 +44,134 @@ export function getNetwork(): domains.network.NetworkDomainDebugger {
|
||||
export function setNetwork(newNetwork: domains.network.NetworkDomainDebugger) {
|
||||
network = newNetwork;
|
||||
}
|
||||
|
||||
export namespace NetworkAgent {
|
||||
export interface Request {
|
||||
url: string;
|
||||
method: string;
|
||||
headers: any;
|
||||
postData?: string;
|
||||
}
|
||||
|
||||
export interface RequestData {
|
||||
requestId: string;
|
||||
url: string;
|
||||
request: Request;
|
||||
timestamp: number;
|
||||
type: string;
|
||||
}
|
||||
|
||||
export interface Response {
|
||||
url: string;
|
||||
status: number;
|
||||
statusText: string;
|
||||
headers: any;
|
||||
headersText?: string;
|
||||
mimeType: string;
|
||||
fromDiskCache?: boolean;
|
||||
}
|
||||
|
||||
export interface ResponseData {
|
||||
requestId: string;
|
||||
type: string;
|
||||
response: Response;
|
||||
timestamp: number;
|
||||
}
|
||||
|
||||
export interface SuccessfulRequestData {
|
||||
requestId: string;
|
||||
data: string;
|
||||
hasTextContent: boolean;
|
||||
}
|
||||
|
||||
export interface LoadingFinishedData {
|
||||
requestId: string;
|
||||
timestamp: number;
|
||||
}
|
||||
|
||||
export function responseReceived(requestId: number, result: org.nativescript.widgets.Async.Http.RequestResult, headers: any) {
|
||||
let requestIdStr = requestId.toString();
|
||||
// Content-Type and content-type are both common in headers spelling
|
||||
let mimeType: string = <string>headers["Content-Type"] || <string>headers["content-type"];
|
||||
let response: NetworkAgent.Response = {
|
||||
url: result.url || "",
|
||||
status: result.statusCode,
|
||||
statusText: result.statusText || "",
|
||||
headers: headers,
|
||||
mimeType: mimeType,
|
||||
fromDiskCache: false
|
||||
}
|
||||
|
||||
let responseData: NetworkAgent.ResponseData = {
|
||||
requestId: requestIdStr,
|
||||
type: mimeTypeToType(response.mimeType),
|
||||
response: response,
|
||||
timestamp: getTimeStamp()
|
||||
}
|
||||
|
||||
global.__inspector.responseReceived(responseData);
|
||||
global.__inspector.loadingFinished({ requestId: requestIdStr, timestamp: getTimeStamp() });
|
||||
|
||||
let hasTextContent = responseData.type === "Document" || responseData.type === "Script";
|
||||
let data;
|
||||
|
||||
if (!hasTextContent) {
|
||||
if (responseData.type === "Image") {
|
||||
let bitmap = result.responseAsImage;
|
||||
if (bitmap) {
|
||||
let outputStream = new java.io.ByteArrayOutputStream();
|
||||
bitmap.compress(android.graphics.Bitmap.CompressFormat.PNG, 100, outputStream);
|
||||
|
||||
let base64Image = android.util.Base64.encodeToString(outputStream.toByteArray(), android.util.Base64.DEFAULT);
|
||||
data = base64Image;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
data = result.responseAsString;
|
||||
}
|
||||
|
||||
let successfulRequestData: NetworkAgent.SuccessfulRequestData = {
|
||||
requestId: requestIdStr,
|
||||
data: data,
|
||||
hasTextContent: hasTextContent
|
||||
}
|
||||
|
||||
global.__inspector.dataForRequestId(successfulRequestData);
|
||||
}
|
||||
|
||||
export function requestWillBeSent(requestId: number, options: any) {
|
||||
let request: NetworkAgent.Request = {
|
||||
url: options.url,
|
||||
method: options.method,
|
||||
headers: options.headers || {},
|
||||
postData: options.content ? options.content.toString() : ""
|
||||
}
|
||||
|
||||
let requestData: NetworkAgent.RequestData = {
|
||||
requestId: requestId.toString(),
|
||||
url: request.url,
|
||||
request: request,
|
||||
timestamp: getTimeStamp(),
|
||||
type: "Document"
|
||||
}
|
||||
|
||||
global.__inspector.requestWillBeSent(requestData);
|
||||
}
|
||||
|
||||
function getTimeStamp(): number {
|
||||
var d = new Date();
|
||||
return Math.round(d.getTime() / 1000);
|
||||
}
|
||||
|
||||
function mimeTypeToType(mimeType: string): string {
|
||||
let type: string = "Document";
|
||||
|
||||
if (mimeType.indexOf("image") === 0) {
|
||||
type = "Image";
|
||||
} else if (mimeType.indexOf("javascript") !== -1 || mimeType.indexOf("json") !== -1) {
|
||||
type = "Script";
|
||||
}
|
||||
|
||||
return type;
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,8 @@ import * as fsModule from "../../file-system";
|
||||
// this is imported for definition purposes only
|
||||
import * as http from "../../http";
|
||||
|
||||
import { NetworkAgent } from "../../debugger/debugger";
|
||||
|
||||
export const enum HttpResponseEncoding {
|
||||
UTF8,
|
||||
GBK
|
||||
@ -75,6 +77,11 @@ function onRequestComplete(requestId: number, result: org.nativescript.widgets.A
|
||||
}
|
||||
}
|
||||
|
||||
// send response data (for requestId) to network debugger
|
||||
if (global.__inspector && global.__inspector.isConnected) {
|
||||
NetworkAgent.responseReceived(requestId, result, headers);
|
||||
}
|
||||
|
||||
callbacks.resolveCallback({
|
||||
content: {
|
||||
raw: result.raw,
|
||||
@ -194,6 +201,11 @@ export function request(options: http.HttpRequestOptions): Promise<http.HttpResp
|
||||
// initialize the options
|
||||
var javaOptions = buildJavaOptions(options);
|
||||
|
||||
// send request data to network debugger
|
||||
if (global.__inspector && global.__inspector.isConnected) {
|
||||
NetworkAgent.requestWillBeSent(requestIdCounter, options);
|
||||
}
|
||||
|
||||
// remember the callbacks so that we can use them when the CompleteCallback is called
|
||||
var callbacks = {
|
||||
url: options.url,
|
||||
|
1
tns-core-modules/module.d.ts
vendored
1
tns-core-modules/module.d.ts
vendored
@ -14,6 +14,7 @@ declare namespace NodeJS {
|
||||
Deprecated(target: Object, key?: string | symbol, descriptor?: any): any;
|
||||
Experimental(target: Object, key?: string | symbol, descriptor?: any): any;
|
||||
__native?: any;
|
||||
__inspector?: any;
|
||||
__extends: any;
|
||||
__onLiveSync: () => void;
|
||||
__onUncaughtError: (error: NativeScriptError) => void;
|
||||
|
@ -37,6 +37,8 @@
|
||||
public raw: java.io.ByteArrayOutputStream;
|
||||
public headers: java.util.ArrayList<KeyValuePair>;
|
||||
public statusCode: number;
|
||||
public statusText: string;
|
||||
public url: string;
|
||||
public responseAsString: string;
|
||||
public responseAsImage: android.graphics.Bitmap;
|
||||
public error: java.lang.Exception;
|
||||
|
Reference in New Issue
Block a user