forked from NativeScript/NativeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebugger.ts
More file actions
224 lines (189 loc) · 6.28 KB
/
Copy pathdebugger.ts
File metadata and controls
224 lines (189 loc) · 6.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
export namespace domains {
export namespace network {
export interface NetworkDomainDebugger {
create(): domains.network.NetworkRequest;
}
export interface Headers {
}
export interface Request {
url: string;
method: string;
headers: domains.network.Headers;
postData?: string;
}
export interface Response {
url: string;
status: number;
statusText: string;
headers: Headers;
headersText?: string;
mimeType: string;
requestHeaders?: domains.network.Headers;
requestHeadersText?: string;
fromDiskCache?: boolean;
}
export interface NetworkRequest {
mimeType: string;
data: any;
responseReceived(response: domains.network.Response);
loadingFinished();
requestWillBeSent(request: domains.network.Request);
}
}
}
let network;
export function getNetwork(): domains.network.NetworkDomainDebugger {
return network;
}
export function setNetwork(newNetwork: domains.network.NetworkDomainDebugger) {
network = newNetwork;
}
let dom;
export function getDOM(): any {
return dom;
}
export function setDOM(newDOM) {
dom = newDOM;
}
let css;
export function getCSS(): any {
return css;
}
export function setCSS(newCSS) {
css = newCSS;
}
export namespace NetworkAgent {
export interface Request {
url: string;
method: string;
headers: any;
initialPriority: string;
referrerPolicy: string;
postData?: string;
}
export interface RequestData {
requestId: string;
url: string;
request: Request;
timestamp: number;
type: string;
wallTime: number;
}
export interface Response {
url: string;
status: number;
statusText: string;
headers: any;
headersText?: string;
mimeType: string;
connectionReused: boolean;
connectionId: number;
encodedDataLength: number;
securityState: 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) {
const requestIdStr = requestId.toString();
// Content-Type and content-type are both common in headers spelling
const mimeType: string = <string>headers["Content-Type"] || <string>headers["content-type"] || "application/octet-stream";
const contentLengthHeader: string = <string>headers["Content-Length"] || <string>headers["content-length"];
let contentLength = parseInt(contentLengthHeader, 10);
if (isNaN(contentLength)) {
contentLength = 0;
}
const response: NetworkAgent.Response = {
url: result.url || "",
status: result.statusCode,
statusText: result.statusText || "",
headers: headers,
mimeType: mimeType,
fromDiskCache: false,
connectionReused: true,
connectionId: 0,
encodedDataLength: contentLength,
securityState: "info"
};
const responseData: NetworkAgent.ResponseData = {
requestId: requestIdStr,
type: mimeTypeToType(response.mimeType),
response: response,
timestamp: getTimeStamp()
};
global.__inspector.responseReceived(responseData);
global.__inspector.loadingFinished({
requestId: requestIdStr,
timestamp: getTimeStamp(),
encodedDataLength: contentLength
});
const hasTextContent = responseData.type === "Document" || responseData.type === "Script";
let data;
if (!hasTextContent) {
if (responseData.type === "Image") {
const bitmap = result.responseAsImage;
if (bitmap) {
const outputStream = new java.io.ByteArrayOutputStream();
bitmap.compress(android.graphics.Bitmap.CompressFormat.PNG, 100, outputStream);
const base64Image = android.util.Base64.encodeToString(outputStream.toByteArray(), android.util.Base64.DEFAULT);
data = base64Image;
}
}
} else {
data = result.responseAsString;
}
const successfulRequestData: NetworkAgent.SuccessfulRequestData = {
requestId: requestIdStr,
data: data,
hasTextContent: hasTextContent
};
global.__inspector.dataForRequestId(successfulRequestData);
}
export function requestWillBeSent(requestId: number, options: any) {
const request: NetworkAgent.Request = {
url: options.url,
method: options.method,
headers: options.headers || {},
postData: options.content ? options.content.toString() : "",
initialPriority: "Medium",
referrerPolicy: "no-referrer-when-downgrade"
};
const requestData: NetworkAgent.RequestData = {
requestId: requestId.toString(),
url: request.url,
request: request,
timestamp: getTimeStamp(),
type: "Document",
wallTime: 0
};
global.__inspector.requestWillBeSent(requestData);
}
function getTimeStamp(): number {
const d = new Date();
return Math.round(d.getTime() / 1000);
}
function mimeTypeToType(mimeType: string): string {
let type: string = "Document";
if (mimeType) {
if (mimeType.indexOf("image") === 0) {
type = "Image";
} else if (mimeType.indexOf("javascript") !== -1 || mimeType.indexOf("json") !== -1) {
type = "Script";
}
}
return type;
}
}