@cyphlens/2fa-sse-sdk 1.0.2 → 1.0.3
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.
- package/README.md +2 -1
- package/dist/bundle.min.js +2 -0
- package/dist/bundle.min.js.map +1 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -30,7 +30,7 @@ yarn add @cyphlens/2fa-sse-sdk
|
|
|
30
30
|
### Using CDN (Browser)
|
|
31
31
|
Include the SDK directly in your HTML:
|
|
32
32
|
```html
|
|
33
|
-
<script src="https://cdn.jsdelivr.net/npm/@cyphlens/2fa-sse-sdk/dist/bundle.
|
|
33
|
+
<script src="https://cdn.jsdelivr.net/npm/@cyphlens/2fa-sse-sdk/dist/bundle.min.js"/>
|
|
34
34
|
```
|
|
35
35
|
|
|
36
36
|
---
|
|
@@ -140,3 +140,4 @@ This project is licensed under the Cyphlens License. See the [LICENSE](https://c
|
|
|
140
140
|
|
|
141
141
|
|
|
142
142
|
|
|
143
|
+
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var Cyphlens=function(t){"use strict";var e,s;t.EventType=void 0,(e=t.EventType||(t.EventType={})).MFASwipe="MFA.SWIPE",e.Disconnect="DISCONNECT",t.StatusType=void 0,(s=t.StatusType||(t.StatusType={})).Pending="PENDING",s.Success="SUCCESS",s.Expired="EXPIRED";class i{constructor(t="https://api.cyphme.com/b2c/v1"){this.eventSource=null,this.timeoutId=null,this.onVisibilityChange=()=>{/^((?!chrome|android).)*safari/i.test(navigator.userAgent)&&/iP(ad|od|hone)/i.test(navigator.userAgent)&&"visible"===document.visibilityState&&this.sessionID&&this.start()},this.baseUrl=t}setBaseUrl(t){this.baseUrl=t}start(){return this.sessionID?(this.stop(),this.eventSource=new EventSource(`${this.baseUrl}/cyphlens/status-events?sid=${this.sessionID}`),this.eventSource.addEventListener(t.EventType.MFASwipe,(e=>{var s,i;try{const i=JSON.parse(e.data);this.setupTimeout(i.expiresAt),null===(s=this.onSuccess)||void 0===s||s.call(this,t.EventType.MFASwipe,i)}catch(t){console.error("Error parsing event data:",t);const e=new Event("Something went wrong");null===(i=this.onError)||void 0===i||i.call(this,e)}})),this.eventSource.addEventListener("error",(t=>{var e;null===(e=this.onError)||void 0===e||e.call(this,t),this.stop()})),this.eventSource):null}setupTimeout(t){this.timeoutId&&clearTimeout(this.timeoutId);const e=t-Date.now()+5e3;this.timeoutId=setTimeout((()=>this.stop()),e)}listen(t,e,s){return this.sessionID=t,this.onSuccess=e,this.onError=s,"undefined"!=typeof document&&document.addEventListener("visibilitychange",this.onVisibilityChange),this.start()}stop(){this.eventSource&&(this.eventSource.close(),this.eventSource=null),this.timeoutId&&(clearTimeout(this.timeoutId),this.timeoutId=null)}}return t.Cyphlens=i,t.default=i,Object.defineProperty(t,"__esModule",{value:!0}),t}({});
|
|
2
|
+
//# sourceMappingURL=bundle.min.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bundle.min.js","sources":["../src/types.ts","../src/index.ts"],"sourcesContent":["export enum EventType {\n MFASwipe = \"MFA.SWIPE\",\n Disconnect = \"DISCONNECT\",\n}\n\nexport enum StatusType {\n Pending = \"PENDING\",\n Success = \"SUCCESS\",\n Expired = \"EXPIRED\",\n}\n\nexport interface CyphlensEvent {}\n\nexport interface TwoFactorEvent extends CyphlensEvent { \n sessionId: string;\n status: StatusType;\n timestamp: number;\n expiresAt: number;\n}\n\nexport interface DisconnectEvent extends CyphlensEvent {\n message: string;\n}\n\nexport type EventCallback = (eventType: EventType, data: CyphlensEvent) => void;\nexport type CyphlensErrorCallback = (error: Event) => void;\n","import { EventCallback, TwoFactorEvent, EventType, CyphlensErrorCallback, StatusType, DisconnectEvent, CyphlensEvent } from './types';\nexport { EventCallback, TwoFactorEvent, EventType, CyphlensErrorCallback, StatusType, DisconnectEvent, CyphlensEvent }\nexport class Cyphlens {\n private baseUrl: string;\n private eventSource: EventSource | null = null;\n private sessionID?: string;\n private onSuccess?: EventCallback;\n private onError?: CyphlensErrorCallback;\n private timeoutId: ReturnType<typeof setTimeout> | null = null;\n\n /**\n * Initializes the Cyphlens service with a base URL.\n * @param baseUrl The base API URL for connecting to the Cyphlens service.\n */\n constructor(baseUrl: string = \"https://api.cyphme.com/b2c/v1\") {\n this.baseUrl = baseUrl;\n }\n\n /**\n * Updates the base API URL.\n * @param baseUrl The new base URL.\n */\n setBaseUrl(baseUrl: string): void {\n this.baseUrl = baseUrl;\n }\n\n /**\n * Starts the Server-Sent Events (SSE) connection to listen for authentication events.\n * @returns The EventSource instance if successful, otherwise null.\n */\n private start(): EventSource | null {\n if (!this.sessionID) return null;\n\n this.stop(); // Ensure any existing connections are closed before starting a new one.\n\n this.eventSource = new EventSource(`${this.baseUrl}/cyphlens/status-events?sid=${this.sessionID}`);\n\n // Listen for MFA swipe events\n this.eventSource.addEventListener(EventType.MFASwipe, (event: MessageEvent) => {\n try {\n const data: TwoFactorEvent = JSON.parse(event.data);\n this.setupTimeout(data.expiresAt);\n this.onSuccess?.(EventType.MFASwipe, data);\n } catch (error) {\n console.error(\"Error parsing event data:\", error);\n const errorEvent = new Event('Something went wrong');\n this.onError?.(errorEvent);\n }\n });\n\n // Handle SSE errors\n this.eventSource.addEventListener(\"error\", (event: Event) => {\n this.onError?.(event);\n this.stop();\n });\n\n return this.eventSource;\n }\n\n /**\n * Sets up a timeout to automatically stop the SSE connection when the event expires.\n * @param expiresAt The expiration timestamp (milliseconds).\n */\n private setupTimeout(expiresAt: number): void {\n if (this.timeoutId) {\n clearTimeout(this.timeoutId);\n }\n\n // Calculate timeout duration and add a buffer (5000ms) before stopping the connection.\n const timeoutDuration = expiresAt - Date.now() + 5000;\n this.timeoutId = setTimeout(() => this.stop(), timeoutDuration);\n }\n\n /**\n * Starts listening for authentication events.\n * @param sessionId The session ID used for authentication.\n * @param onData Callback function for handling successful authentication events.\n * @param onError Callback function for handling errors.\n * @returns The EventSource instance if successfully started, otherwise null.\n */\n listen(sessionId: string, onData?: EventCallback, onError?: CyphlensErrorCallback): EventSource | null {\n this.sessionID = sessionId;\n this.onSuccess = onData;\n this.onError = onError;\n\n // Attach visibility change listener to handle browser background issues (especially for iOS Safari).\n if (typeof document !== \"undefined\") {\n document.addEventListener(\"visibilitychange\", this.onVisibilityChange);\n }\n\n return this.start();\n }\n\n /**\n * Stops the SSE connection and clears any active timeouts.\n */\n stop(): void {\n if (this.eventSource) {\n this.eventSource.close();\n this.eventSource = null;\n }\n if (this.timeoutId) {\n clearTimeout(this.timeoutId);\n this.timeoutId = null;\n }\n }\n\n /**\n * Handles browser visibility changes to restart the SSE connection when the page becomes visible.\n * This is primarily to prevent connection loss in Safari when the tab goes to the background.\n * Only applies to Mobile Safari.\n */\n private readonly onVisibilityChange = (): void => {\n const isMobileSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent) &&\n /iP(ad|od|hone)/i.test(navigator.userAgent);\n if (isMobileSafari && document.visibilityState === \"visible\" && this.sessionID) {\n this.start();\n }\n };\n }\n\nexport type CyphlensExports = {\n Cyphlens: typeof Cyphlens;\n EventType: typeof EventType;\n StatusType: typeof StatusType;\n TwoFactorEvent: TwoFactorEvent;\n CyphlensEvent: CyphlensEvent;\n DisconnectEvent: DisconnectEvent;\n EventCallback: EventCallback;\n CyphlensErrorCallback: CyphlensErrorCallback;\n};\n\nexport default Cyphlens;\n"],"names":["EventType","StatusType","Cyphlens","constructor","baseUrl","this","eventSource","timeoutId","onVisibilityChange","test","navigator","userAgent","document","visibilityState","sessionID","start","setBaseUrl","stop","EventSource","addEventListener","MFASwipe","event","data","JSON","parse","setupTimeout","expiresAt","_a","onSuccess","call","error","console","errorEvent","Event","_b","onError","clearTimeout","timeoutDuration","Date","now","setTimeout","listen","sessionId","onData","close"],"mappings":"sCAAA,IAAYA,EAKAC,EALAD,EAAAA,eAAAA,GAAAA,EAAAA,EAASA,YAATA,YAGX,CAAA,IAFC,SAAA,YACAA,EAAA,WAAA,aAGUC,EAAAA,gBAAAA,GAAAA,EAA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KS,UAAY4B,EACjBrC,KAAKuB,UAAYe,EACjBtC,KAAK8B,QAAUA,EAGS,oBAAbvB,UACTA,SAASO,iBAAiB,mBAAoBd,KAAKG,oBAG9CH,KAAKU,QAMd,IAAAE,GACMZ,KAAKC,cACPD,KAAKC,YAAYsC,QACjBvC,KAAKC,YAAc,MAEjBD,KAAKE,YACP6B,aAAa/B,KAAKE,WAClBF,KAAKE,UAAY"}
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cyphlens/2fa-sse-sdk",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Cyphlens SDK for 2FA SSE",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"module": "dist/index.esm.js",
|
|
8
|
-
"browser": "dist/
|
|
8
|
+
"browser": "dist/bundle.min.js",
|
|
9
9
|
"types": "dist/types/index.d.ts",
|
|
10
10
|
"publishConfig": {
|
|
11
11
|
"access": "restricted"
|