Skip to content

Commit d856796

Browse files
Address review comments: UMD exports for Workers, ECONNABORTED on abort, setCookie returns header string
Agent-Logs-Url: https://github.com/Crowdhandler/crowdhandler-javascript-sdk/sessions/c6b7fcba-80a4-4ac9-af1a-0db689f102cd Co-authored-by: luke-owen-crowdhandler <60788788+luke-owen-crowdhandler@users.noreply.github.com>
1 parent 286775e commit d856796

4 files changed

Lines changed: 26 additions & 9 deletions

File tree

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
"exports": {
1818
".": {
1919
"types": "./dist/types/index.d.ts",
20-
"workerd": "./dist/crowdhandler.esm.js",
21-
"worker": "./dist/crowdhandler.esm.js",
20+
"workerd": "./dist/crowdhandler.umd.js",
21+
"worker": "./dist/crowdhandler.umd.js",
2222
"browser": "./dist/crowdhandler.umd.js",
2323
"import": "./dist/crowdhandler.esm.js",
2424
"require": "./dist/crowdhandler.cjs.js",

src/client/base_client.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ export class BaseClient {
108108
// Mirror axios's "no response received" error shape so errorHandler's
109109
// `else if (error.request)` branch fires.
110110
const wrapped: any = new Error(err?.message || "Network request failed");
111+
if (controller.signal.aborted || err?.name === "AbortError") {
112+
wrapped.code = "ECONNABORTED";
113+
}
111114
wrapped.request = { url: finalUrl, method };
112115
wrapped.config = { url: finalUrl, method };
113116
throw wrapped;

src/gatekeeper/gatekeeper.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -962,14 +962,25 @@ export class Gatekeeper {
962962
*
963963
* @param {string} value - The cookie value to set (from result.cookieValue)
964964
* @param {string} domain - Optional domain pattern to determine cookie domain scope
965-
* @returns {boolean} True if the cookie was successfully set, false otherwise
965+
* @returns {boolean | string} In Node.js/Lambda/browser environments returns true on success
966+
* or false on failure. In Cloudflare Workers returns the Set-Cookie header string that
967+
* must be applied to the outgoing Response by the caller.
966968
*
967969
* @example
970+
* // Node.js / Lambda
968971
* if (result.setCookie) {
969972
* gatekeeper.setCookie(result.cookieValue, result.domain);
970973
* }
974+
*
975+
* @example
976+
* // Cloudflare Workers
977+
* if (result.setCookie) {
978+
* const setCookieHeader = gatekeeper.setCookie(result.cookieValue, result.domain);
979+
* // setCookieHeader is the Set-Cookie header value — apply it to the Response:
980+
* // response.headers.append('Set-Cookie', setCookieHeader as string);
981+
* }
971982
*/
972-
public setCookie(value: string, domain?: string): boolean {
983+
public setCookie(value: string, domain?: string): boolean | string {
973984
try {
974985
// Determine cookie domain if domain pattern is provided
975986
let cookieDomain: string | undefined;
@@ -981,9 +992,12 @@ export class Gatekeeper {
981992
}
982993
}
983994

984-
// Set the cookie with the provided value and options
985-
this.REQUEST.setCookie(value, this.STORAGE_NAME, cookieDomain);
986-
return true;
995+
// Set the cookie with the provided value and options.
996+
// CloudflareWorkersHandler returns the Set-Cookie header string because
997+
// Workers are response-out and the caller must apply the header manually.
998+
// All other handlers set the cookie as a side-effect and return void.
999+
const result = this.REQUEST.setCookie(value, this.STORAGE_NAME, cookieDomain);
1000+
return typeof result === 'string' ? result : true;
9871001
} catch (error: any) {
9881002
logger(this.options.debug, "error", error);
9891003
return false;

0 commit comments

Comments
 (0)