UNPKG

6.97 kBJavaScriptView Raw
1var NetworkDomainDebugger_1;
2const inspectorCommands = require('./InspectorBackendCommands');
3import * as debuggerDomains from '.';
4const getApplicationContext = () => require('../utils/android').getApplicationContext();
5const frameId = 'NativeScriptMainFrameIdentifier';
6const loaderId = 'Loader Identifier';
7const resources_datas = [];
8const documentTypeByMimeType = {
9 'text/xml': 'Document',
10 'text/plain': 'Document',
11 'text/html': 'Document',
12 'application/xml': 'Document',
13 'application/xhtml+xml': 'Document',
14 'text/css': 'Stylesheet',
15 'text/javascript': 'Script',
16 'text/ecmascript': 'Script',
17 'application/javascript': 'Script',
18 'application/ecmascript': 'Script',
19 'application/x-javascript': 'Script',
20 'application/json': 'Script',
21 'application/x-json': 'Script',
22 'text/x-javascript': 'Script',
23 'text/x-json': 'Script',
24 'text/typescript': 'Script',
25};
26export class Request {
27 constructor(_networkDomainDebugger, _requestID) {
28 this._networkDomainDebugger = _networkDomainDebugger;
29 this._requestID = _requestID;
30 }
31 get mimeType() {
32 return this._mimeType;
33 }
34 set mimeType(value) {
35 if (this._mimeType !== value) {
36 if (!value) {
37 this._mimeType = 'text/plain';
38 this._resourceType = 'Other';
39 return;
40 }
41 this._mimeType = value;
42 let resourceType = 'Other';
43 if (this._mimeType in documentTypeByMimeType) {
44 resourceType = documentTypeByMimeType[this._mimeType];
45 }
46 if (this._mimeType.indexOf('image/') !== -1) {
47 resourceType = 'Image';
48 }
49 if (this._mimeType.indexOf('font/') !== -1) {
50 resourceType = 'Font';
51 }
52 this._resourceType = resourceType;
53 }
54 }
55 get requestID() {
56 return this._requestID;
57 }
58 get hasTextContent() {
59 return ['Document', 'Stylesheet', 'Script', 'XHR'].indexOf(this._resourceType) !== -1;
60 }
61 get data() {
62 return this._data;
63 }
64 set data(value) {
65 if (this._data !== value) {
66 this._data = value;
67 }
68 }
69 get resourceType() {
70 return this._resourceType;
71 }
72 set resourceType(value) {
73 if (this._resourceType !== value) {
74 this._resourceType = value;
75 }
76 }
77 responseReceived(response) {
78 if (this._networkDomainDebugger.enabled) {
79 this._networkDomainDebugger.events.responseReceived(this.requestID, frameId, loaderId, __inspectorTimestamp(), this.resourceType, response);
80 }
81 }
82 loadingFinished() {
83 if (this._networkDomainDebugger.enabled) {
84 this._networkDomainDebugger.events.loadingFinished(this.requestID, __inspectorTimestamp());
85 }
86 }
87 requestWillBeSent(request) {
88 if (this._networkDomainDebugger.enabled) {
89 this._networkDomainDebugger.events.requestWillBeSent(this.requestID, frameId, loaderId, request.url, request, __inspectorTimestamp(), { type: 'Script' });
90 }
91 }
92}
93let NetworkDomainDebugger = NetworkDomainDebugger_1 = class NetworkDomainDebugger {
94 constructor() {
95 this.events = new inspectorCommands.NetworkDomain.NetworkFrontend();
96 // By default start enabled because we can miss the "enable" event when
97 // running with `--debug-brk` -- the frontend will send it before we've been created
98 this.enable();
99 }
100 get enabled() {
101 return this._enabled;
102 }
103 /**
104 * Enables network tracking, network events will now be delivered to the client.
105 */
106 enable() {
107 if (debuggerDomains.getNetwork()) {
108 throw new Error('One NetworkDomainDebugger may be enabled at a time.');
109 }
110 else {
111 debuggerDomains.setNetwork(this);
112 }
113 this._enabled = true;
114 }
115 /**
116 * Disables network tracking, prevents network events from being sent to the client.
117 */
118 disable() {
119 if (debuggerDomains.getNetwork() === this) {
120 debuggerDomains.setNetwork(null);
121 }
122 this._enabled = false;
123 }
124 /**
125 * Specifies whether to always send extra HTTP headers with the requests from this page.
126 */
127 setExtraHTTPHeaders(params) {
128 //
129 }
130 /**
131 * Returns content served for the given request.
132 */
133 getResponseBody(params) {
134 const resource_data = resources_datas[params.requestId];
135 // java.io.ByteArrayOutputStream
136 const body = resource_data.hasTextContent ? resource_data.data.toString('UTF-8') : android.util.Base64.encodeToString(resource_data.data?.buf?.(), android.util.Base64.NO_WRAP);
137 if (resource_data) {
138 return {
139 body: body,
140 base64Encoded: !resource_data.hasTextContent,
141 };
142 }
143 }
144 /**
145 * Tells whether clearing browser cache is supported.
146 */
147 canClearBrowserCache() {
148 return {
149 result: false,
150 };
151 }
152 /**
153 * Clears browser cache.
154 */
155 clearBrowserCache() {
156 //
157 }
158 /**
159 * Tells whether clearing browser cookies is supported.
160 */
161 canClearBrowserCookies() {
162 return {
163 result: false,
164 };
165 }
166 /**
167 * Clears browser cookies.
168 */
169 clearBrowserCookies() {
170 //
171 }
172 /**
173 * Toggles ignoring cache for each request. If <code>true</code>, cache will not be used.
174 */
175 setCacheDisabled(params) {
176 //
177 }
178 /**
179 * Loads a resource in the context of a frame on the inspected page without cross origin checks.
180 */
181 loadResource(params) {
182 const appPath = getApplicationContext().getFilesDir().getCanonicalPath() + '/app';
183 const pathUrl = params.url.replace('file://', appPath);
184 const file = new java.io.File(pathUrl);
185 const is = file.exists() ? new java.io.FileInputStream(file) : undefined;
186 const data = is ? Array.create('bytes', file.length()) : undefined;
187 const read = data ? is.read(data) : 0;
188 const content = read ? new java.lang.String(data) : '';
189 return {
190 content: content.toString(), // Not sure why however we need to call toString() for NSString
191 mimeType: 'application/octet-stream',
192 status: 200,
193 };
194 }
195 create() {
196 const id = (++NetworkDomainDebugger_1.idSequence).toString();
197 const resourceData = new Request(this, id);
198 resources_datas[id] = resourceData;
199 return resourceData;
200 }
201};
202NetworkDomainDebugger.idSequence = 0;
203NetworkDomainDebugger = NetworkDomainDebugger_1 = __decorate([
204 inspectorCommands.DomainDispatcher('Network'),
205 __metadata("design:paramtypes", [])
206], NetworkDomainDebugger);
207export { NetworkDomainDebugger };
208//# sourceMappingURL=webinspector-network.android.js.map
\No newline at end of file