@itwin/core-mobile 4.0.0-dev.8 → 4.0.0-dev.80

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/CHANGELOG.md +48 -1
  2. package/README.md +1 -1
  3. package/lib/cjs/MobileBackend.d.ts +7 -7
  4. package/lib/cjs/MobileBackend.js +28 -24
  5. package/lib/cjs/MobileBackend.js.map +1 -1
  6. package/lib/cjs/MobileFrontend.d.ts +5 -5
  7. package/lib/cjs/MobileFrontend.js +25 -21
  8. package/lib/cjs/MobileFrontend.js.map +1 -1
  9. package/lib/cjs/__DOC_ONLY__.d.ts +33 -33
  10. package/lib/cjs/__DOC_ONLY__.js +56 -52
  11. package/lib/cjs/__DOC_ONLY__.js.map +1 -1
  12. package/lib/cjs/backend/MobileAuthorizationBackend.d.ts +17 -17
  13. package/lib/cjs/backend/MobileAuthorizationBackend.js +46 -46
  14. package/lib/cjs/backend/MobileAuthorizationBackend.js.map +1 -1
  15. package/lib/cjs/backend/MobileFileHandler.d.ts +112 -112
  16. package/lib/cjs/backend/MobileFileHandler.d.ts.map +1 -1
  17. package/lib/cjs/backend/MobileFileHandler.js +263 -263
  18. package/lib/cjs/backend/MobileFileHandler.js.map +1 -1
  19. package/lib/cjs/backend/MobileHost.d.ts +72 -72
  20. package/lib/cjs/backend/MobileHost.d.ts.map +1 -1
  21. package/lib/cjs/backend/MobileHost.js +149 -156
  22. package/lib/cjs/backend/MobileHost.js.map +1 -1
  23. package/lib/cjs/backend/MobileRpcServer.d.ts +19 -19
  24. package/lib/cjs/backend/MobileRpcServer.js +156 -156
  25. package/lib/cjs/backend/MobileRpcServer.js.map +1 -1
  26. package/lib/cjs/backend/Request.d.ts +117 -149
  27. package/lib/cjs/backend/Request.d.ts.map +1 -1
  28. package/lib/cjs/backend/Request.js +255 -267
  29. package/lib/cjs/backend/Request.js.map +1 -1
  30. package/lib/cjs/common/MobileAppChannel.d.ts +4 -4
  31. package/lib/cjs/common/MobileAppChannel.js +11 -11
  32. package/lib/cjs/common/MobileAppProps.d.ts +35 -35
  33. package/lib/cjs/common/MobileAppProps.d.ts.map +1 -1
  34. package/lib/cjs/common/MobileAppProps.js +26 -26
  35. package/lib/cjs/common/MobileEventLoop.d.ts +11 -11
  36. package/lib/cjs/common/MobileEventLoop.js +30 -30
  37. package/lib/cjs/common/MobileEventLoop.js.map +1 -1
  38. package/lib/cjs/common/MobileIpc.d.ts +17 -17
  39. package/lib/cjs/common/MobileIpc.js +70 -70
  40. package/lib/cjs/common/MobilePush.d.ts +20 -20
  41. package/lib/cjs/common/MobilePush.js +53 -53
  42. package/lib/cjs/common/MobilePush.js.map +1 -1
  43. package/lib/cjs/common/MobileRpcManager.d.ts +40 -40
  44. package/lib/cjs/common/MobileRpcManager.js +110 -110
  45. package/lib/cjs/common/MobileRpcManager.js.map +1 -1
  46. package/lib/cjs/common/MobileRpcProtocol.d.ts +58 -58
  47. package/lib/cjs/common/MobileRpcProtocol.d.ts.map +1 -1
  48. package/lib/cjs/common/MobileRpcProtocol.js +277 -277
  49. package/lib/cjs/common/MobileRpcProtocol.js.map +1 -1
  50. package/lib/cjs/common/MobileRpcRequest.d.ts +20 -20
  51. package/lib/cjs/common/MobileRpcRequest.js +50 -50
  52. package/lib/cjs/frontend/MobileApp.d.ts +23 -23
  53. package/lib/cjs/frontend/MobileApp.d.ts.map +1 -1
  54. package/lib/cjs/frontend/MobileApp.js +79 -80
  55. package/lib/cjs/frontend/MobileApp.js.map +1 -1
  56. package/lib/cjs/frontend/MobileAuthorizationFrontend.d.ts +17 -17
  57. package/lib/cjs/frontend/MobileAuthorizationFrontend.d.ts.map +1 -1
  58. package/lib/cjs/frontend/MobileAuthorizationFrontend.js +54 -46
  59. package/lib/cjs/frontend/MobileAuthorizationFrontend.js.map +1 -1
  60. package/lib/cjs/test/ios/MobilePlatform.test.d.ts +1 -1
  61. package/lib/cjs/test/ios/MobilePlatform.test.js +84 -84
  62. package/package.json +18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IAiC7D;;;;;OAKG;WACW,WAAW,CAAC,KAAK,EAAE,GAAG,EAAE,QAAQ,EAAE,GAAG,GAAG,OAAO;IAS7D;;OAEG;WACW,eAAe,CAAC,UAAU,EAAE,MAAM,GAAG,UAAU;IAiB7D;;OAEG;IACI,UAAU,IAAI,MAAM;IAI3B;;;OAGG;IACI,GAAG,IAAI,IAAI;CAGnB;AAcD;;;;;;;;;GASG;AACH,wBAAsB,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,QAAQ,CAAC,CAmIrF;AAED;;;;GAIG;AACH,wBAAsB,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,CAOvD"}
@@ -1,268 +1,256 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getJson = exports.request = exports.ResponseError = exports.requestIdHeaderName = void 0;
4
- /*---------------------------------------------------------------------------------------------
5
- * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
6
- * See LICENSE.md in the project root for license terms and full copyright notice.
7
- *--------------------------------------------------------------------------------------------*/
8
- /** @packageDocumentation
9
- * @module iTwinServiceClients
10
- */
11
- const deepAssign = require("deep-assign");
12
- const qs_1 = require("qs");
13
- const sarequest = require("superagent");
14
- const core_bentley_1 = require("@itwin/core-bentley");
15
- const loggerCategory = "core-mobile-backend.Request";
16
- /** @internal */
17
- exports.requestIdHeaderName = "X-Correlation-Id";
18
- /** Error object that's thrown/rejected if the Request fails due to a network error, or if the status is *not* in the range of 200-299 (inclusive)
19
- * @internal
20
- */
21
- class ResponseError extends core_bentley_1.BentleyError {
22
- constructor(errorNumber, message, getMetaData) {
23
- super(errorNumber, message, getMetaData);
24
- }
25
- /**
26
- * Parses error from server's response
27
- * @param response Http response from the server.
28
- * @returns Parsed error.
29
- * @internal
30
- */
31
- static parse(response, log = true) {
32
- const error = new ResponseError(ResponseError.parseHttpStatus(response.statusType));
33
- if (!response) {
34
- error.message = "Couldn't get response object.";
35
- return error;
36
- }
37
- if (response.response) {
38
- if (response.response.error) {
39
- error.name = response.response.error.name || error.name;
40
- error.description = response.response.error.message;
41
- }
42
- if (response.response.res) {
43
- error.message = response.response.res.statusMessage;
44
- }
45
- if (response.response.body && Object.keys(response.response.body).length > 0) {
46
- error._data = {};
47
- deepAssign(error._data, response.response.body);
48
- }
49
- else {
50
- error._data = response.response.text;
51
- }
52
- }
53
- error.status = response.status || response.statusCode;
54
- error.name = response.code || response.name || error.name;
55
- error.message = error.message || response.message || response.statusMessage;
56
- if (log)
57
- error.log();
58
- return error;
59
- }
60
- /**
61
- * Decides whether request should be retried or not
62
- * @param error Error returned by request
63
- * @param response Response returned by request
64
- * @internal
65
- */
66
- static shouldRetry(error, response) {
67
- if (error !== undefined && error !== null) {
68
- if ((error.status === undefined || error.status === null) && (error.res === undefined || error.res === null)) {
69
- return true;
70
- }
71
- }
72
- return (response !== undefined && response.statusType === core_bentley_1.HttpStatus.ServerError);
73
- }
74
- /**
75
- * @internal
76
- */
77
- static parseHttpStatus(statusType) {
78
- switch (statusType) {
79
- case 1:
80
- return core_bentley_1.HttpStatus.Info;
81
- case 2:
82
- return core_bentley_1.HttpStatus.Success;
83
- case 3:
84
- return core_bentley_1.HttpStatus.Redirection;
85
- case 4:
86
- return core_bentley_1.HttpStatus.ClientError;
87
- case 5:
88
- return core_bentley_1.HttpStatus.ServerError;
89
- default:
90
- return core_bentley_1.HttpStatus.Success;
91
- }
92
- }
93
- /**
94
- * @internal
95
- */
96
- logMessage() {
97
- return `${this.status} ${this.name}: ${this.message}`;
98
- }
99
- /**
100
- * Logs this error
101
- * @internal
102
- */
103
- log() {
104
- core_bentley_1.Logger.logError(loggerCategory, this.logMessage(), () => this.getMetaData());
105
- }
106
- }
107
- exports.ResponseError = ResponseError;
108
- const logResponse = (req, startTime) => (res) => {
109
- const elapsed = new Date().getTime() - startTime;
110
- const elapsedTime = `${elapsed}ms`;
111
- core_bentley_1.Logger.logTrace(loggerCategory, `${req.method.toUpperCase()} ${res.status} ${req.url} (${elapsedTime})`);
112
- };
113
- // eslint-disable-next-line @typescript-eslint/promise-function-async
114
- const logRequest = (req) => {
115
- const startTime = new Date().getTime();
116
- return req.on("response", logResponse(req, startTime));
117
- };
118
- /** Wrapper around making HTTP requests with the specific options.
119
- *
120
- * Usable in both a browser and node based environment.
121
- *
122
- * @param url Server URL to address the request
123
- * @param options Options to pass to the request
124
- * @returns Resolves to the response from the server
125
- * @throws ResponseError if the request fails due to network issues, or if the returned status is *outside* the range of 200-299 (inclusive)
126
- * @internal
127
- */
128
- async function request(url, options) {
129
- let sareq = sarequest(options.method, url);
130
- if (options.retries)
131
- sareq = sareq.retry(options.retries, options.retryCallback);
132
- if (core_bentley_1.Logger.isEnabled(loggerCategory, core_bentley_1.LogLevel.Trace))
133
- sareq = sareq.use(logRequest);
134
- if (options.headers)
135
- sareq = sareq.set(options.headers);
136
- let queryStr = "";
137
- let fullUrl = "";
138
- if (options.qs && Object.keys(options.qs).length > 0) {
139
- const stringifyOptions = { delimiter: "&", encode: false };
140
- queryStr = (0, qs_1.stringify)(options.qs, stringifyOptions);
141
- sareq = sareq.query(queryStr);
142
- fullUrl = `${url}?${queryStr}`;
143
- }
144
- else {
145
- fullUrl = url;
146
- }
147
- core_bentley_1.Logger.logInfo(loggerCategory, fullUrl);
148
- if (options.accept)
149
- sareq = sareq.accept(options.accept);
150
- if (options.body)
151
- sareq = sareq.send(options.body);
152
- if (options.timeout)
153
- sareq = sareq.timeout(options.timeout);
154
- if (options.responseType)
155
- sareq = sareq.responseType(options.responseType);
156
- if (options.redirects)
157
- sareq = sareq.redirects(options.redirects);
158
- else
159
- sareq = sareq.redirects(0);
160
- if (options.buffer)
161
- sareq = sareq.buffer(options.buffer);
162
- if (options.parser)
163
- sareq = sareq.parse(options.parser);
164
- /** Default to any globally supplied proxy, unless an agent is specified in this call */
165
- if (options.agent)
166
- sareq = sareq.agent(options.agent);
167
- if (options.progressCallback) {
168
- sareq = sareq.on("progress", (event) => {
169
- if (event) {
170
- options.progressCallback({
171
- loaded: event.loaded,
172
- total: event.total,
173
- percent: event.percent,
174
- });
175
- }
176
- });
177
- }
178
- const errorCallback = options.errorCallback ? options.errorCallback : ResponseError.parse;
179
- if (options.readStream) {
180
- if (typeof window !== "undefined")
181
- throw new Error("This option is not supported on browsers");
182
- return new Promise((resolve, reject) => {
183
- sareq = sareq.type("blob");
184
- options
185
- .readStream
186
- .pipe(sareq)
187
- .on("error", (error) => {
188
- const parsedError = errorCallback(error);
189
- reject(parsedError);
190
- })
191
- .on("end", () => {
192
- const retResponse = {
193
- status: 201,
194
- header: undefined,
195
- body: undefined,
196
- text: undefined,
197
- };
198
- resolve(retResponse);
199
- });
200
- });
201
- }
202
- if (options.stream) {
203
- if (typeof window !== "undefined")
204
- throw new Error("This option is not supported on browsers");
205
- return new Promise((resolve, reject) => {
206
- sareq
207
- .on("response", (res) => {
208
- if (res.statusCode !== 200) {
209
- const parsedError = errorCallback(res);
210
- reject(parsedError);
211
- return;
212
- }
213
- })
214
- .pipe(options.stream)
215
- .on("error", (error) => {
216
- const parsedError = errorCallback(error);
217
- reject(parsedError);
218
- })
219
- .on("finish", () => {
220
- const retResponse = {
221
- status: 200,
222
- header: undefined,
223
- body: undefined,
224
- text: undefined,
225
- };
226
- resolve(retResponse);
227
- });
228
- });
229
- }
230
- // console.log("%s %s %s", url, options.method, queryStr);
231
- /**
232
- * Note:
233
- * Javascript's fetch returns status.OK if error is between 200-299 inclusive, and doesn't reject in this case.
234
- * Fetch only rejects if there's some network issue (permissions issue or similar)
235
- * Superagent rejects network issues, and errors outside the range of 200-299. We are currently using
236
- * superagent, but may eventually switch to JavaScript's fetch library.
237
- */
238
- try {
239
- const response = await sareq;
240
- const retResponse = {
241
- body: response.body,
242
- text: response.text,
243
- header: response.header,
244
- status: response.status,
245
- };
246
- return retResponse;
247
- }
248
- catch (error) {
249
- const parsedError = errorCallback(error);
250
- throw parsedError;
251
- }
252
- }
253
- exports.request = request;
254
- /**
255
- * fetch json from HTTP request
256
- * @param url server URL to address the request
257
- * @internal
258
- */
259
- async function getJson(url) {
260
- const options = {
261
- method: "GET",
262
- responseType: "json",
263
- };
264
- const data = await request(url, options);
265
- return data.body;
266
- }
267
- exports.getJson = getJson;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getJson = exports.request = exports.ResponseError = exports.requestIdHeaderName = void 0;
4
+ /*---------------------------------------------------------------------------------------------
5
+ * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
6
+ * See LICENSE.md in the project root for license terms and full copyright notice.
7
+ *--------------------------------------------------------------------------------------------*/
8
+ /** @packageDocumentation
9
+ * @module iTwinServiceClients
10
+ */
11
+ const _ = require("lodash");
12
+ const sarequest = require("superagent");
13
+ const core_bentley_1 = require("@itwin/core-bentley");
14
+ const loggerCategory = "core-mobile-backend.Request";
15
+ /** @internal */
16
+ exports.requestIdHeaderName = "X-Correlation-Id";
17
+ /** Error object that's thrown/rejected if the Request fails due to a network error, or if the status is *not* in the range of 200-299 (inclusive)
18
+ * @internal
19
+ */
20
+ class ResponseError extends core_bentley_1.BentleyError {
21
+ constructor(errorNumber, message, getMetaData) {
22
+ super(errorNumber, message, getMetaData);
23
+ }
24
+ /**
25
+ * Parses error from server's response
26
+ * @param response Http response from the server.
27
+ * @returns Parsed error.
28
+ * @internal
29
+ */
30
+ static parse(response, log = true) {
31
+ const error = new ResponseError(ResponseError.parseHttpStatus(response.statusType));
32
+ if (!response) {
33
+ error.message = "Couldn't get response object.";
34
+ return error;
35
+ }
36
+ if (response.response) {
37
+ if (response.response.error) {
38
+ error.name = response.response.error.name || error.name;
39
+ error.description = response.response.error.message;
40
+ }
41
+ if (response.response.res) {
42
+ error.message = response.response.res.statusMessage;
43
+ }
44
+ if (response.response.body && Object.keys(response.response.body).length > 0) {
45
+ error._data = {};
46
+ _.merge(error._data, response.response.body);
47
+ }
48
+ else {
49
+ error._data = response.response.text;
50
+ }
51
+ }
52
+ error.status = response.status || response.statusCode;
53
+ error.name = response.code || response.name || error.name;
54
+ error.message = error.message || response.message || response.statusMessage;
55
+ if (log)
56
+ error.log();
57
+ return error;
58
+ }
59
+ /**
60
+ * Decides whether request should be retried or not
61
+ * @param error Error returned by request
62
+ * @param response Response returned by request
63
+ * @internal
64
+ */
65
+ static shouldRetry(error, response) {
66
+ if (error !== undefined && error !== null) {
67
+ if ((error.status === undefined || error.status === null) && (error.res === undefined || error.res === null)) {
68
+ return true;
69
+ }
70
+ }
71
+ return (response !== undefined && response.statusType === core_bentley_1.HttpStatus.ServerError);
72
+ }
73
+ /**
74
+ * @internal
75
+ */
76
+ static parseHttpStatus(statusType) {
77
+ switch (statusType) {
78
+ case 1:
79
+ return core_bentley_1.HttpStatus.Info;
80
+ case 2:
81
+ return core_bentley_1.HttpStatus.Success;
82
+ case 3:
83
+ return core_bentley_1.HttpStatus.Redirection;
84
+ case 4:
85
+ return core_bentley_1.HttpStatus.ClientError;
86
+ case 5:
87
+ return core_bentley_1.HttpStatus.ServerError;
88
+ default:
89
+ return core_bentley_1.HttpStatus.Success;
90
+ }
91
+ }
92
+ /**
93
+ * @internal
94
+ */
95
+ logMessage() {
96
+ return `${this.status} ${this.name}: ${this.message}`;
97
+ }
98
+ /**
99
+ * Logs this error
100
+ * @internal
101
+ */
102
+ log() {
103
+ core_bentley_1.Logger.logError(loggerCategory, this.logMessage(), () => this.getMetaData());
104
+ }
105
+ }
106
+ exports.ResponseError = ResponseError;
107
+ const logResponse = (req, startTime) => (res) => {
108
+ const elapsed = new Date().getTime() - startTime;
109
+ const elapsedTime = `${elapsed}ms`;
110
+ core_bentley_1.Logger.logTrace(loggerCategory, `${req.method.toUpperCase()} ${res.status} ${req.url} (${elapsedTime})`);
111
+ };
112
+ // eslint-disable-next-line @typescript-eslint/promise-function-async
113
+ const logRequest = (req) => {
114
+ const startTime = new Date().getTime();
115
+ return req.on("response", logResponse(req, startTime));
116
+ };
117
+ /** Wrapper around making HTTP requests with the specific options.
118
+ *
119
+ * Usable in both a browser and node based environment.
120
+ *
121
+ * @param url Server URL to address the request
122
+ * @param options Options to pass to the request
123
+ * @returns Resolves to the response from the server
124
+ * @throws ResponseError if the request fails due to network issues, or if the returned status is *outside* the range of 200-299 (inclusive)
125
+ * @internal
126
+ */
127
+ async function request(url, options) {
128
+ let sareq = sarequest(options.method, url);
129
+ if (options.retries)
130
+ sareq = sareq.retry(options.retries, options.retryCallback);
131
+ if (core_bentley_1.Logger.isEnabled(loggerCategory, core_bentley_1.LogLevel.Trace))
132
+ sareq = sareq.use(logRequest);
133
+ if (options.headers)
134
+ sareq = sareq.set(options.headers);
135
+ core_bentley_1.Logger.logInfo(loggerCategory, url);
136
+ if (options.accept)
137
+ sareq = sareq.accept(options.accept);
138
+ if (options.body)
139
+ sareq = sareq.send(options.body);
140
+ if (options.timeout)
141
+ sareq = sareq.timeout(options.timeout);
142
+ if (options.responseType)
143
+ sareq = sareq.responseType(options.responseType);
144
+ if (options.redirects)
145
+ sareq = sareq.redirects(options.redirects);
146
+ else
147
+ sareq = sareq.redirects(0);
148
+ if (options.buffer)
149
+ sareq = sareq.buffer(options.buffer);
150
+ if (options.parser)
151
+ sareq = sareq.parse(options.parser);
152
+ /** Default to any globally supplied proxy, unless an agent is specified in this call */
153
+ if (options.agent)
154
+ sareq = sareq.agent(options.agent);
155
+ if (options.progressCallback) {
156
+ sareq = sareq.on("progress", (event) => {
157
+ if (event) {
158
+ options.progressCallback({
159
+ loaded: event.loaded,
160
+ total: event.total,
161
+ percent: event.percent,
162
+ });
163
+ }
164
+ });
165
+ }
166
+ const errorCallback = options.errorCallback ? options.errorCallback : ResponseError.parse;
167
+ if (options.readStream) {
168
+ if (typeof window !== "undefined")
169
+ throw new Error("This option is not supported on browsers");
170
+ return new Promise((resolve, reject) => {
171
+ sareq = sareq.type("blob");
172
+ options
173
+ .readStream
174
+ .pipe(sareq)
175
+ .on("error", (error) => {
176
+ const parsedError = errorCallback(error);
177
+ reject(parsedError);
178
+ })
179
+ .on("end", () => {
180
+ const retResponse = {
181
+ status: 201,
182
+ header: undefined,
183
+ body: undefined,
184
+ text: undefined,
185
+ };
186
+ resolve(retResponse);
187
+ });
188
+ });
189
+ }
190
+ if (options.stream) {
191
+ if (typeof window !== "undefined")
192
+ throw new Error("This option is not supported on browsers");
193
+ return new Promise((resolve, reject) => {
194
+ sareq
195
+ .on("response", (res) => {
196
+ if (res.statusCode !== 200) {
197
+ const parsedError = errorCallback(res);
198
+ reject(parsedError);
199
+ return;
200
+ }
201
+ })
202
+ .pipe(options.stream)
203
+ .on("error", (error) => {
204
+ const parsedError = errorCallback(error);
205
+ reject(parsedError);
206
+ })
207
+ .on("finish", () => {
208
+ const retResponse = {
209
+ status: 200,
210
+ header: undefined,
211
+ body: undefined,
212
+ text: undefined,
213
+ };
214
+ resolve(retResponse);
215
+ });
216
+ });
217
+ }
218
+ // console.log("%s %s %s", url, options.method, queryStr);
219
+ /**
220
+ * Note:
221
+ * Javascript's fetch returns status.OK if error is between 200-299 inclusive, and doesn't reject in this case.
222
+ * Fetch only rejects if there's some network issue (permissions issue or similar)
223
+ * Superagent rejects network issues, and errors outside the range of 200-299. We are currently using
224
+ * superagent, but may eventually switch to JavaScript's fetch library.
225
+ */
226
+ try {
227
+ const response = await sareq;
228
+ const retResponse = {
229
+ body: response.body,
230
+ text: response.text,
231
+ header: response.header,
232
+ status: response.status,
233
+ };
234
+ return retResponse;
235
+ }
236
+ catch (error) {
237
+ const parsedError = errorCallback(error);
238
+ throw parsedError;
239
+ }
240
+ }
241
+ exports.request = request;
242
+ /**
243
+ * fetch json from HTTP request
244
+ * @param url server URL to address the request
245
+ * @internal
246
+ */
247
+ async function getJson(url) {
248
+ const options = {
249
+ method: "GET",
250
+ responseType: "json",
251
+ };
252
+ const data = await request(url, options);
253
+ return data.body;
254
+ }
255
+ exports.getJson = getJson;
268
256
  //# sourceMappingURL=Request.js.map