Skip to content

Commit 40fb7bd

Browse files
Rebuild dist for 2.4.0
1 parent 04405e6 commit 40fb7bd

20 files changed

Lines changed: 1035 additions & 145 deletions

dist/client/base_client.js

Lines changed: 130 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@ var axios_1 = __importDefault(require("axios"));
5555
var zod_1 = require("zod");
5656
var logger_1 = require("../common/logger");
5757
var errors_1 = require("../common/errors");
58+
/**
59+
* Detect if we're running in the Cloudflare Workers (workerd) runtime.
60+
* Workers sets navigator.userAgent to "Cloudflare-Workers" — this is the
61+
* documented and stable detection signal:
62+
* https://developers.cloudflare.com/workers/runtime-apis/web-standards/
63+
*
64+
* axios 0.27.2 has no fetch adapter and requires Node's http module, so it
65+
* crashes inside Workers. When we detect Workers, route HTTP through native
66+
* fetch instead — preserved error shape so errorHandler keeps working.
67+
*/
68+
var isCloudflareWorkers = typeof navigator !== "undefined" &&
69+
navigator.userAgent === "Cloudflare-Workers";
5870
var APIResponse = zod_1.z.object({}).catchall(zod_1.z.any());
5971
var APIErrorResponse = zod_1.z
6072
.object({
@@ -70,8 +82,119 @@ var BaseClient = /** @class */ (function () {
7082
this.apiUrl = options.apiUrl || apiUrl;
7183
this.key = key;
7284
this.timeout = options.timeout || 5000;
73-
axios_1.default.defaults.timeout = this.timeout;
85+
if (!isCloudflareWorkers) {
86+
// axios.defaults is process-global state and is meaningless in Workers
87+
// (we don't use axios there). Skip in Workers to avoid touching axios's
88+
// internal config which can drag in Node-only deps during import.
89+
axios_1.default.defaults.timeout = this.timeout;
90+
}
7491
}
92+
/**
93+
* Issue an HTTP request. Routes through axios in Node/Lambda environments
94+
* and native fetch in Cloudflare Workers. Both paths return / throw
95+
* axios-compatible shapes so errorHandler() and the response.data parsing
96+
* downstream work unchanged.
97+
*/
98+
BaseClient.prototype.httpRequest = function (method, url, options) {
99+
if (options === void 0) { options = {}; }
100+
return __awaiter(this, void 0, void 0, function () {
101+
var response_1, finalUrl, search, _i, _a, _b, k, v, init, hasContentType, controller, timeoutId, response, err_1, wrapped, contentType, data, _c, text, headersObj_1, wrapped, headersObj;
102+
return __generator(this, function (_d) {
103+
switch (_d.label) {
104+
case 0:
105+
if (!!isCloudflareWorkers) return [3 /*break*/, 2];
106+
return [4 /*yield*/, axios_1.default.request({
107+
method: method,
108+
url: url,
109+
params: options.params,
110+
data: options.body,
111+
headers: options.headers,
112+
})];
113+
case 1:
114+
response_1 = _d.sent();
115+
return [2 /*return*/, { data: response_1.data, status: response_1.status, headers: response_1.headers }];
116+
case 2:
117+
finalUrl = url;
118+
if (options.params && Object.keys(options.params).length > 0) {
119+
search = new URLSearchParams();
120+
for (_i = 0, _a = Object.entries(options.params); _i < _a.length; _i++) {
121+
_b = _a[_i], k = _b[0], v = _b[1];
122+
if (v !== undefined && v !== null)
123+
search.append(k, String(v));
124+
}
125+
finalUrl += (finalUrl.includes("?") ? "&" : "?") + search.toString();
126+
}
127+
init = {
128+
method: method,
129+
headers: options.headers,
130+
};
131+
if (options.body !== undefined && method !== "GET" && method !== "DELETE") {
132+
init.body = typeof options.body === "string" ? options.body : JSON.stringify(options.body);
133+
hasContentType = options.headers && Object.keys(options.headers)
134+
.some(function (h) { return h.toLowerCase() === "content-type"; });
135+
if (!hasContentType) {
136+
init.headers = __assign(__assign({}, (options.headers || {})), { "content-type": "application/json" });
137+
}
138+
}
139+
controller = new AbortController();
140+
timeoutId = setTimeout(function () { return controller.abort(); }, this.timeout);
141+
init.signal = controller.signal;
142+
_d.label = 3;
143+
case 3:
144+
_d.trys.push([3, 5, , 6]);
145+
return [4 /*yield*/, fetch(finalUrl, init)];
146+
case 4:
147+
response = _d.sent();
148+
return [3 /*break*/, 6];
149+
case 5:
150+
err_1 = _d.sent();
151+
clearTimeout(timeoutId);
152+
wrapped = new Error((err_1 === null || err_1 === void 0 ? void 0 : err_1.message) || "Network request failed");
153+
wrapped.request = { url: finalUrl, method: method };
154+
wrapped.config = { url: finalUrl, method: method };
155+
throw wrapped;
156+
case 6:
157+
clearTimeout(timeoutId);
158+
contentType = response.headers.get("content-type") || "";
159+
if (!contentType.includes("application/json")) return [3 /*break*/, 11];
160+
_d.label = 7;
161+
case 7:
162+
_d.trys.push([7, 9, , 10]);
163+
return [4 /*yield*/, response.json()];
164+
case 8:
165+
data = _d.sent();
166+
return [3 /*break*/, 10];
167+
case 9:
168+
_c = _d.sent();
169+
data = null;
170+
return [3 /*break*/, 10];
171+
case 10: return [3 /*break*/, 13];
172+
case 11: return [4 /*yield*/, response.text()];
173+
case 12:
174+
text = _d.sent();
175+
try {
176+
data = JSON.parse(text);
177+
}
178+
catch (_e) {
179+
data = text;
180+
}
181+
_d.label = 13;
182+
case 13:
183+
if (response.status < 200 || response.status >= 300) {
184+
headersObj_1 = {};
185+
response.headers.forEach(function (v, k) { headersObj_1[k] = v; });
186+
wrapped = new Error("Request failed with status ".concat(response.status));
187+
wrapped.response = { status: response.status, data: data, headers: headersObj_1 };
188+
wrapped.config = { url: finalUrl, method: method };
189+
throw wrapped;
190+
}
191+
headersObj = {};
192+
response.headers.forEach(function (v, k) { headersObj[k] = v; });
193+
return [2 /*return*/, { data: data, status: response.status, headers: headersObj }];
194+
}
195+
});
196+
});
197+
};
75198
/**
76199
* Wraps any error into a CrowdHandlerError
77200
*/
@@ -168,7 +291,7 @@ var BaseClient = /** @class */ (function () {
168291
switch (_a.label) {
169292
case 0:
170293
_a.trys.push([0, 2, , 4]);
171-
return [4 /*yield*/, axios_1.default.delete(this.apiUrl + path, {
294+
return [4 /*yield*/, this.httpRequest("DELETE", this.apiUrl + path, {
172295
headers: {
173296
"x-api-key": this.key,
174297
},
@@ -200,7 +323,7 @@ var BaseClient = /** @class */ (function () {
200323
switch (_a.label) {
201324
case 0:
202325
_a.trys.push([0, 2, , 4]);
203-
return [4 /*yield*/, axios_1.default.get(this.apiUrl + path, {
326+
return [4 /*yield*/, this.httpRequest("GET", this.apiUrl + path, {
204327
params: params,
205328
headers: {
206329
"x-api-key": this.key,
@@ -234,7 +357,8 @@ var BaseClient = /** @class */ (function () {
234357
switch (_a.label) {
235358
case 0:
236359
_a.trys.push([0, 2, , 4]);
237-
return [4 /*yield*/, axios_1.default.post(this.apiUrl + path, body, {
360+
return [4 /*yield*/, this.httpRequest("POST", this.apiUrl + path, {
361+
body: body,
238362
headers: __assign({ "x-api-key": this.key }, headers),
239363
})];
240364
case 1:
@@ -264,7 +388,8 @@ var BaseClient = /** @class */ (function () {
264388
switch (_a.label) {
265389
case 0:
266390
_a.trys.push([0, 2, , 3]);
267-
return [4 /*yield*/, axios_1.default.put(this.apiUrl + path, body, {
391+
return [4 /*yield*/, this.httpRequest("PUT", this.apiUrl + path, {
392+
body: body,
268393
headers: {
269394
"x-api-key": this.key,
270395
},

dist/common/processURL.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
33
exports.ProcessURL = void 0;
4+
var types_1 = require("./types");
45
var logger_1 = require("./logger");
56
var ProcessURL = /** @class */ (function () {
67
function ProcessURL(request, debug) {
@@ -110,20 +111,11 @@ var ProcessURL = /** @class */ (function () {
110111
ProcessURL.prototype.removeChParams = function (queryString) {
111112
if (!queryString)
112113
return "";
113-
// List of ch-* parameters to remove
114-
var chParams = [
115-
"ch-code",
116-
"ch-fresh",
117-
"ch-id",
118-
"ch-id-signature",
119-
"ch-public-key",
120-
"ch-requested",
121-
];
122114
// Split into individual params, filter out ch-* params, rejoin
123115
var params = queryString.split("&");
124116
var filteredParams = params.filter(function (param) {
125117
var key = param.split("=")[0];
126-
return !chParams.includes(key.toLowerCase());
118+
return !types_1.CH_PARAM_KEYS.includes(key.toLowerCase());
127119
});
128120
return filteredParams.join("&");
129121
};

dist/common/types.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
3-
exports.Modes = exports.RecordPerformanceOptions = exports.SessionStatusWrapper = exports.HttpErrorWrapper = exports.ValidateRequestObject = exports.ValidateRequestParams = exports.TokenObjectConstructor = exports.TokenObject = exports.ExtractTokenOptions = exports.SignatureSourceObject = exports.SignatureResponseObject = exports.SignatureObject = exports.RoomMetaObject = exports.LocalStorageOptions = exports.LocalStorageObject = exports.CookieObject = exports.RequestObject = exports.ProcessURLResultObject = exports.SessionRequestConfig = exports.SpecialParametersObject = exports.GatekeeperKeyPair = exports.GatekeeperOptions = exports.RoomsConfig = exports.RoomConfig = void 0;
3+
exports.Modes = exports.RecordPerformanceOptions = exports.SessionStatusWrapper = exports.HttpErrorWrapper = exports.ValidateRequestObject = exports.ValidateRequestParams = exports.TokenObjectConstructor = exports.TokenObject = exports.ExtractTokenOptions = exports.SignatureSourceObject = exports.SignatureResponseObject = exports.SignatureObject = exports.RoomMetaObject = exports.LocalStorageOptions = exports.LocalStorageObject = exports.CookieObject = exports.RequestObject = exports.ProcessURLResultObject = exports.SessionRequestConfig = exports.SpecialParametersObject = exports.CH_PARAM_KEYS = exports.GatekeeperKeyPair = exports.GatekeeperOptions = exports.RoomsConfig = exports.RoomConfig = void 0;
44
var zod_1 = require("zod");
55
// Lite Validator types
66
exports.RoomConfig = zod_1.z.object({
@@ -32,6 +32,16 @@ exports.GatekeeperKeyPair = zod_1.z.object({
3232
publicKey: zod_1.z.string(),
3333
privateKey: zod_1.z.string().optional(),
3434
});
35+
// Centralised list of CrowdHandler query-string parameter keys.
36+
// Used wherever ch-* params need to be detected or stripped.
37+
exports.CH_PARAM_KEYS = [
38+
'ch-code',
39+
'ch-fresh',
40+
'ch-id',
41+
'ch-id-signature',
42+
'ch-public-key',
43+
'ch-requested',
44+
];
3545
exports.SpecialParametersObject = zod_1.z.object({
3646
chCode: zod_1.z.string(),
3747
chID: zod_1.z.string(),

0 commit comments

Comments
 (0)