Skip to content

Commit 290a771

Browse files
committed
add calls to the network agent (Chrome DevTools) inside http-request.android when making http requests if debugging is enabled
1 parent cbbb5c1 commit 290a771

File tree

4 files changed

+147
-1
lines changed

4 files changed

+147
-1
lines changed

tns-core-modules/debugger/debugger.ts

Lines changed: 132 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export namespace domains {
33
export interface NetworkDomainDebugger {
44
create(): domains.network.NetworkRequest;
55
}
6-
6+
77
export interface Headers {
88
}
99

@@ -44,3 +44,134 @@ export function getNetwork(): domains.network.NetworkDomainDebugger {
4444
export function setNetwork(newNetwork: domains.network.NetworkDomainDebugger) {
4545
network = newNetwork;
4646
}
47+
48+
export namespace NetworkAgent {
49+
export interface Request {
50+
url: string;
51+
method: string;
52+
headers: any;
53+
postData?: string;
54+
}
55+
56+
export interface RequestData {
57+
requestId: string;
58+
url: string;
59+
request: Request;
60+
timestamp: number;
61+
type: string;
62+
}
63+
64+
export interface Response {
65+
url: string;
66+
status: number;
67+
statusText: string;
68+
headers: any;
69+
headersText?: string;
70+
mimeType: string;
71+
fromDiskCache?: boolean;
72+
}
73+
74+
export interface ResponseData {
75+
requestId: string;
76+
type: string;
77+
response: Response;
78+
timestamp: number;
79+
}
80+
81+
export interface SuccessfulRequestData {
82+
requestId: string;
83+
data: string;
84+
hasTextContent: boolean;
85+
}
86+
87+
export interface LoadingFinishedData {
88+
requestId: string;
89+
timestamp: number;
90+
}
91+
92+
export function responseReceived(requestId: number, result: org.nativescript.widgets.Async.Http.RequestResult, headers: any) {
93+
let requestIdStr = requestId.toString();
94+
// Content-Type and content-type are both common in headers spelling
95+
let mimeType: string = <string>headers["Content-Type"] || <string>headers["content-type"];
96+
let response: NetworkAgent.Response = {
97+
url: result.url || "",
98+
status: result.statusCode,
99+
statusText: result.statusText || "",
100+
headers: headers,
101+
mimeType: mimeType,
102+
fromDiskCache: false
103+
}
104+
105+
let responseData: NetworkAgent.ResponseData = {
106+
requestId: requestIdStr,
107+
type: mimeTypeToType(response.mimeType),
108+
response: response,
109+
timestamp: getTimeStamp()
110+
}
111+
112+
global.__inspector.responseReceived(responseData);
113+
global.__inspector.loadingFinished({ requestId: requestIdStr, timestamp: getTimeStamp() });
114+
115+
let hasTextContent = responseData.type === "Document" || responseData.type === "Script";
116+
let data;
117+
118+
if (!hasTextContent) {
119+
if (responseData.type === "Image") {
120+
let bitmap = result.responseAsImage;
121+
if (bitmap) {
122+
let outputStream = new java.io.ByteArrayOutputStream();
123+
bitmap.compress(android.graphics.Bitmap.CompressFormat.PNG, 100, outputStream);
124+
125+
let base64Image = android.util.Base64.encodeToString(outputStream.toByteArray(), android.util.Base64.DEFAULT);
126+
data = base64Image;
127+
}
128+
}
129+
} else {
130+
data = result.responseAsString;
131+
}
132+
133+
let successfulRequestData: NetworkAgent.SuccessfulRequestData = {
134+
requestId: requestIdStr,
135+
data: data,
136+
hasTextContent: hasTextContent
137+
}
138+
139+
global.__inspector.dataForRequestId(successfulRequestData);
140+
}
141+
142+
export function requestWillBeSent(requestId: number, options: any) {
143+
let request: NetworkAgent.Request = {
144+
url: options.url,
145+
method: options.method,
146+
headers: options.headers || {},
147+
postData: options.content ? options.content.toString() : ""
148+
}
149+
150+
let requestData: NetworkAgent.RequestData = {
151+
requestId: requestId.toString(),
152+
url: request.url,
153+
request: request,
154+
timestamp: getTimeStamp(),
155+
type: "Document"
156+
}
157+
158+
global.__inspector.requestWillBeSent(requestData);
159+
}
160+
161+
function getTimeStamp(): number {
162+
var d = new Date();
163+
return Math.round(d.getTime() / 1000);
164+
}
165+
166+
function mimeTypeToType(mimeType: string): string {
167+
let type: string = "Document";
168+
169+
if (mimeType.indexOf("image") === 0) {
170+
type = "Image";
171+
} else if (mimeType.indexOf("javascript") !== -1 || mimeType.indexOf("json") !== -1) {
172+
type = "Script";
173+
}
174+
175+
return type;
176+
}
177+
}

tns-core-modules/http/http-request/http-request.android.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import * as fsModule from "../../file-system";
88
// this is imported for definition purposes only
99
import * as http from "../../http";
1010

11+
import { NetworkAgent } from "../../debugger/debugger";
12+
1113
export const enum HttpResponseEncoding {
1214
UTF8,
1315
GBK
@@ -75,6 +77,11 @@ function onRequestComplete(requestId: number, result: org.nativescript.widgets.A
7577
}
7678
}
7779

80+
// send response data (for requestId) to network debugger
81+
if (global.__inspector && global.__inspector.isConnected) {
82+
NetworkAgent.responseReceived(requestId, result, headers);
83+
}
84+
7885
callbacks.resolveCallback({
7986
content: {
8087
raw: result.raw,
@@ -194,6 +201,11 @@ export function request(options: http.HttpRequestOptions): Promise<http.HttpResp
194201
// initialize the options
195202
var javaOptions = buildJavaOptions(options);
196203

204+
// send request data to network debugger
205+
if (global.__inspector && global.__inspector.isConnected) {
206+
NetworkAgent.requestWillBeSent(requestIdCounter, options);
207+
}
208+
197209
// remember the callbacks so that we can use them when the CompleteCallback is called
198210
var callbacks = {
199211
url: options.url,

tns-core-modules/module.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ declare namespace NodeJS {
1414
Deprecated(target: Object, key?: string | symbol, descriptor?: any): any;
1515
Experimental(target: Object, key?: string | symbol, descriptor?: any): any;
1616
__native?: any;
17+
__inspector?: any;
1718
__extends: any;
1819
__onLiveSync: () => void;
1920
__onUncaughtError: (error: NativeScriptError) => void;

tns-platform-declarations/android/org.nativescript.widgets.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
public raw: java.io.ByteArrayOutputStream;
3838
public headers: java.util.ArrayList<KeyValuePair>;
3939
public statusCode: number;
40+
public statusText: string;
41+
public url: string;
4042
public responseAsString: string;
4143
public responseAsImage: android.graphics.Bitmap;
4244
public error: java.lang.Exception;

0 commit comments

Comments
 (0)