@central-ticket/queue-sdk 0.0.1 → 0.0.2
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/dist/centralq-C3ukDo0x.d.mts +134 -0
- package/dist/centralq-C3ukDo0x.d.ts +134 -0
- package/dist/{chunk-A4HITWM4.mjs → chunk-4MXU7S6A.mjs} +1 -1
- package/dist/{chunk-42RGFZKP.mjs → chunk-A37B25XO.mjs} +27 -0
- package/dist/chunk-BVCZFNM3.mjs +329 -0
- package/dist/{chunk-JR7BCYB5.mjs → chunk-GWPLLJTU.mjs} +2 -2
- package/dist/chunk-JJ33EJ65.mjs +31 -0
- package/dist/chunk-P73Q2ZIO.mjs +167 -0
- package/dist/chunk-XIQ6LS62.mjs +312 -0
- package/dist/index.d.mts +31 -3
- package/dist/index.d.ts +31 -3
- package/dist/index.js +166 -4
- package/dist/index.mjs +3 -3
- package/dist/react.d.mts +1 -1
- package/dist/react.d.ts +1 -1
- package/dist/react.js +70 -2
- package/dist/react.mjs +2 -2
- package/dist/server.js +1 -1
- package/dist/server.mjs +2 -2
- package/dist/svelte.d.mts +1 -1
- package/dist/svelte.d.ts +1 -1
- package/dist/svelte.js +70 -2
- package/dist/svelte.mjs +2 -2
- package/dist/vue.d.mts +1 -1
- package/dist/vue.d.ts +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
interface CentralQOptions {
|
|
2
|
+
/** URL del queue worker. */
|
|
3
|
+
apiUrl?: string;
|
|
4
|
+
/** Publishable API key (ctq_pub_xxx) */
|
|
5
|
+
apiKey: string;
|
|
6
|
+
/** ID del evento/recurso a proteger */
|
|
7
|
+
eventId: string;
|
|
8
|
+
/** ID de usuario externo. Si no se provee, se autogenera y persiste en sessionStorage */
|
|
9
|
+
userId?: string;
|
|
10
|
+
/** Init token corto emitido por backend (recomendado para producción) */
|
|
11
|
+
queueInitToken?: string;
|
|
12
|
+
/** Intervalo de polling en ms (default: 10000) */
|
|
13
|
+
pollInterval?: number;
|
|
14
|
+
/** Contenedor donde montar el overlay. Default: document.body */
|
|
15
|
+
container?: HTMLElement;
|
|
16
|
+
}
|
|
17
|
+
interface CentralQClientOptions {
|
|
18
|
+
/** URL del queue worker */
|
|
19
|
+
apiUrl?: string;
|
|
20
|
+
/** Publishable API key (ctq_pub_xxx) */
|
|
21
|
+
apiKey: string;
|
|
22
|
+
/** ID de usuario externo por defecto para todas las colas creadas por el client */
|
|
23
|
+
userId?: string;
|
|
24
|
+
/** Init token corto por defecto para colas creadas por el client */
|
|
25
|
+
queueInitToken?: string;
|
|
26
|
+
/** Intervalo de polling por defecto en ms */
|
|
27
|
+
pollInterval?: number;
|
|
28
|
+
/** Contenedor por defecto donde montar el overlay */
|
|
29
|
+
container?: HTMLElement;
|
|
30
|
+
}
|
|
31
|
+
interface CentralQCreateOptions {
|
|
32
|
+
/** ID del evento/recurso a proteger */
|
|
33
|
+
eventId: string;
|
|
34
|
+
/** Override opcional del userId por cola */
|
|
35
|
+
userId?: string;
|
|
36
|
+
/** Override opcional del queue init token por cola */
|
|
37
|
+
queueInitToken?: string;
|
|
38
|
+
/** Override opcional del poll interval por cola */
|
|
39
|
+
pollInterval?: number;
|
|
40
|
+
/** Override opcional del contenedor por cola */
|
|
41
|
+
container?: HTMLElement;
|
|
42
|
+
/** Override opcional del apiUrl por cola */
|
|
43
|
+
apiUrl?: string;
|
|
44
|
+
/** Override opcional del apiKey por cola */
|
|
45
|
+
apiKey?: string;
|
|
46
|
+
}
|
|
47
|
+
interface PassedDetail {
|
|
48
|
+
token: string;
|
|
49
|
+
expiresAt: number;
|
|
50
|
+
userId: string;
|
|
51
|
+
eventId: string;
|
|
52
|
+
}
|
|
53
|
+
interface PositionDetail {
|
|
54
|
+
position: number;
|
|
55
|
+
ahead: number;
|
|
56
|
+
userId: string;
|
|
57
|
+
eventId: string;
|
|
58
|
+
}
|
|
59
|
+
interface ExpiredDetail {
|
|
60
|
+
userId: string;
|
|
61
|
+
eventId: string;
|
|
62
|
+
}
|
|
63
|
+
interface ErrorDetail {
|
|
64
|
+
userId: string;
|
|
65
|
+
eventId: string;
|
|
66
|
+
}
|
|
67
|
+
type EventMap = {
|
|
68
|
+
passed: PassedDetail;
|
|
69
|
+
expired: ExpiredDetail;
|
|
70
|
+
position: PositionDetail;
|
|
71
|
+
error: ErrorDetail;
|
|
72
|
+
};
|
|
73
|
+
type EventName = keyof EventMap;
|
|
74
|
+
type Listener<T> = (detail: T) => void;
|
|
75
|
+
type QueueStatus = "idle" | "joining" | "waiting" | "passed" | "expired" | "error";
|
|
76
|
+
declare class CentralQ {
|
|
77
|
+
private client;
|
|
78
|
+
private element;
|
|
79
|
+
private userId;
|
|
80
|
+
private eventId;
|
|
81
|
+
private pollInterval;
|
|
82
|
+
private container;
|
|
83
|
+
private pollingTimer?;
|
|
84
|
+
private heartbeatTimer?;
|
|
85
|
+
private expiryTimer?;
|
|
86
|
+
private destroyed;
|
|
87
|
+
private listeners;
|
|
88
|
+
private constructor();
|
|
89
|
+
/**
|
|
90
|
+
* Inicializa CentralQ y comienza el flujo de cola automáticamente.
|
|
91
|
+
* Monta el overlay de UI y empieza a hacer polling.
|
|
92
|
+
*/
|
|
93
|
+
static init(options: CentralQOptions): CentralQ;
|
|
94
|
+
/** Suscribirse a un evento del ciclo de vida de la cola */
|
|
95
|
+
on<K extends EventName>(event: K, listener: Listener<EventMap[K]>): this;
|
|
96
|
+
/** Desuscribirse de un evento */
|
|
97
|
+
off<K extends EventName>(event: K, listener: Listener<EventMap[K]>): this;
|
|
98
|
+
private emit;
|
|
99
|
+
/**
|
|
100
|
+
* Libera el slot del usuario inmediatamente.
|
|
101
|
+
* Llamar cuando el usuario completa la compra.
|
|
102
|
+
*/
|
|
103
|
+
leave(): void;
|
|
104
|
+
/**
|
|
105
|
+
* Destruye la instancia: para todos los timers, desmonta el overlay,
|
|
106
|
+
* y libera el slot en el servidor.
|
|
107
|
+
*/
|
|
108
|
+
destroy(): void;
|
|
109
|
+
/** Retorna el userId resuelto (externo o autogenerado) */
|
|
110
|
+
getUserId(): string;
|
|
111
|
+
private resolveUserId;
|
|
112
|
+
private mount;
|
|
113
|
+
private unmount;
|
|
114
|
+
private updateUI;
|
|
115
|
+
private stopTimers;
|
|
116
|
+
private startHeartbeat;
|
|
117
|
+
private scheduleExpiry;
|
|
118
|
+
private handleExpired;
|
|
119
|
+
private checkQueue;
|
|
120
|
+
}
|
|
121
|
+
declare class CentralQClient {
|
|
122
|
+
private defaults;
|
|
123
|
+
constructor(defaults: CentralQClientOptions);
|
|
124
|
+
issueQueueInitToken(eventId: string, userId?: string): Promise<{
|
|
125
|
+
userId: string;
|
|
126
|
+
queueInitToken: string;
|
|
127
|
+
expiresAt: number;
|
|
128
|
+
}>;
|
|
129
|
+
private issueInitToken;
|
|
130
|
+
createQueue(options: CentralQCreateOptions): CentralQ;
|
|
131
|
+
}
|
|
132
|
+
declare function createCentralQClient(options: CentralQClientOptions): CentralQClient;
|
|
133
|
+
|
|
134
|
+
export { CentralQ as C, type ErrorDetail as E, type PassedDetail as P, type QueueStatus as Q, CentralQClient as a, type CentralQClientOptions as b, type CentralQCreateOptions as c, type CentralQOptions as d, type ExpiredDetail as e, type PositionDetail as f, createCentralQClient as g };
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
interface CentralQOptions {
|
|
2
|
+
/** URL del queue worker. */
|
|
3
|
+
apiUrl?: string;
|
|
4
|
+
/** Publishable API key (ctq_pub_xxx) */
|
|
5
|
+
apiKey: string;
|
|
6
|
+
/** ID del evento/recurso a proteger */
|
|
7
|
+
eventId: string;
|
|
8
|
+
/** ID de usuario externo. Si no se provee, se autogenera y persiste en sessionStorage */
|
|
9
|
+
userId?: string;
|
|
10
|
+
/** Init token corto emitido por backend (recomendado para producción) */
|
|
11
|
+
queueInitToken?: string;
|
|
12
|
+
/** Intervalo de polling en ms (default: 10000) */
|
|
13
|
+
pollInterval?: number;
|
|
14
|
+
/** Contenedor donde montar el overlay. Default: document.body */
|
|
15
|
+
container?: HTMLElement;
|
|
16
|
+
}
|
|
17
|
+
interface CentralQClientOptions {
|
|
18
|
+
/** URL del queue worker */
|
|
19
|
+
apiUrl?: string;
|
|
20
|
+
/** Publishable API key (ctq_pub_xxx) */
|
|
21
|
+
apiKey: string;
|
|
22
|
+
/** ID de usuario externo por defecto para todas las colas creadas por el client */
|
|
23
|
+
userId?: string;
|
|
24
|
+
/** Init token corto por defecto para colas creadas por el client */
|
|
25
|
+
queueInitToken?: string;
|
|
26
|
+
/** Intervalo de polling por defecto en ms */
|
|
27
|
+
pollInterval?: number;
|
|
28
|
+
/** Contenedor por defecto donde montar el overlay */
|
|
29
|
+
container?: HTMLElement;
|
|
30
|
+
}
|
|
31
|
+
interface CentralQCreateOptions {
|
|
32
|
+
/** ID del evento/recurso a proteger */
|
|
33
|
+
eventId: string;
|
|
34
|
+
/** Override opcional del userId por cola */
|
|
35
|
+
userId?: string;
|
|
36
|
+
/** Override opcional del queue init token por cola */
|
|
37
|
+
queueInitToken?: string;
|
|
38
|
+
/** Override opcional del poll interval por cola */
|
|
39
|
+
pollInterval?: number;
|
|
40
|
+
/** Override opcional del contenedor por cola */
|
|
41
|
+
container?: HTMLElement;
|
|
42
|
+
/** Override opcional del apiUrl por cola */
|
|
43
|
+
apiUrl?: string;
|
|
44
|
+
/** Override opcional del apiKey por cola */
|
|
45
|
+
apiKey?: string;
|
|
46
|
+
}
|
|
47
|
+
interface PassedDetail {
|
|
48
|
+
token: string;
|
|
49
|
+
expiresAt: number;
|
|
50
|
+
userId: string;
|
|
51
|
+
eventId: string;
|
|
52
|
+
}
|
|
53
|
+
interface PositionDetail {
|
|
54
|
+
position: number;
|
|
55
|
+
ahead: number;
|
|
56
|
+
userId: string;
|
|
57
|
+
eventId: string;
|
|
58
|
+
}
|
|
59
|
+
interface ExpiredDetail {
|
|
60
|
+
userId: string;
|
|
61
|
+
eventId: string;
|
|
62
|
+
}
|
|
63
|
+
interface ErrorDetail {
|
|
64
|
+
userId: string;
|
|
65
|
+
eventId: string;
|
|
66
|
+
}
|
|
67
|
+
type EventMap = {
|
|
68
|
+
passed: PassedDetail;
|
|
69
|
+
expired: ExpiredDetail;
|
|
70
|
+
position: PositionDetail;
|
|
71
|
+
error: ErrorDetail;
|
|
72
|
+
};
|
|
73
|
+
type EventName = keyof EventMap;
|
|
74
|
+
type Listener<T> = (detail: T) => void;
|
|
75
|
+
type QueueStatus = "idle" | "joining" | "waiting" | "passed" | "expired" | "error";
|
|
76
|
+
declare class CentralQ {
|
|
77
|
+
private client;
|
|
78
|
+
private element;
|
|
79
|
+
private userId;
|
|
80
|
+
private eventId;
|
|
81
|
+
private pollInterval;
|
|
82
|
+
private container;
|
|
83
|
+
private pollingTimer?;
|
|
84
|
+
private heartbeatTimer?;
|
|
85
|
+
private expiryTimer?;
|
|
86
|
+
private destroyed;
|
|
87
|
+
private listeners;
|
|
88
|
+
private constructor();
|
|
89
|
+
/**
|
|
90
|
+
* Inicializa CentralQ y comienza el flujo de cola automáticamente.
|
|
91
|
+
* Monta el overlay de UI y empieza a hacer polling.
|
|
92
|
+
*/
|
|
93
|
+
static init(options: CentralQOptions): CentralQ;
|
|
94
|
+
/** Suscribirse a un evento del ciclo de vida de la cola */
|
|
95
|
+
on<K extends EventName>(event: K, listener: Listener<EventMap[K]>): this;
|
|
96
|
+
/** Desuscribirse de un evento */
|
|
97
|
+
off<K extends EventName>(event: K, listener: Listener<EventMap[K]>): this;
|
|
98
|
+
private emit;
|
|
99
|
+
/**
|
|
100
|
+
* Libera el slot del usuario inmediatamente.
|
|
101
|
+
* Llamar cuando el usuario completa la compra.
|
|
102
|
+
*/
|
|
103
|
+
leave(): void;
|
|
104
|
+
/**
|
|
105
|
+
* Destruye la instancia: para todos los timers, desmonta el overlay,
|
|
106
|
+
* y libera el slot en el servidor.
|
|
107
|
+
*/
|
|
108
|
+
destroy(): void;
|
|
109
|
+
/** Retorna el userId resuelto (externo o autogenerado) */
|
|
110
|
+
getUserId(): string;
|
|
111
|
+
private resolveUserId;
|
|
112
|
+
private mount;
|
|
113
|
+
private unmount;
|
|
114
|
+
private updateUI;
|
|
115
|
+
private stopTimers;
|
|
116
|
+
private startHeartbeat;
|
|
117
|
+
private scheduleExpiry;
|
|
118
|
+
private handleExpired;
|
|
119
|
+
private checkQueue;
|
|
120
|
+
}
|
|
121
|
+
declare class CentralQClient {
|
|
122
|
+
private defaults;
|
|
123
|
+
constructor(defaults: CentralQClientOptions);
|
|
124
|
+
issueQueueInitToken(eventId: string, userId?: string): Promise<{
|
|
125
|
+
userId: string;
|
|
126
|
+
queueInitToken: string;
|
|
127
|
+
expiresAt: number;
|
|
128
|
+
}>;
|
|
129
|
+
private issueInitToken;
|
|
130
|
+
createQueue(options: CentralQCreateOptions): CentralQ;
|
|
131
|
+
}
|
|
132
|
+
declare function createCentralQClient(options: CentralQClientOptions): CentralQClient;
|
|
133
|
+
|
|
134
|
+
export { CentralQ as C, type ErrorDetail as E, type PassedDetail as P, type QueueStatus as Q, CentralQClient as a, type CentralQClientOptions as b, type CentralQCreateOptions as c, type CentralQOptions as d, type ExpiredDetail as e, type PositionDetail as f, createCentralQClient as g };
|
|
@@ -91,6 +91,33 @@ var QueueHttpClient = class {
|
|
|
91
91
|
expiresAt: body.expiresAt
|
|
92
92
|
};
|
|
93
93
|
}
|
|
94
|
+
async verifyToken(eventId, token) {
|
|
95
|
+
const res = await fetch(
|
|
96
|
+
`${this.apiUrl}/api/queue/verify/${encodeURIComponent(eventId)}`,
|
|
97
|
+
{
|
|
98
|
+
method: "POST",
|
|
99
|
+
headers: this.headers,
|
|
100
|
+
body: JSON.stringify({ token })
|
|
101
|
+
}
|
|
102
|
+
);
|
|
103
|
+
const body = await res.json().catch(() => ({
|
|
104
|
+
valid: false,
|
|
105
|
+
reason: "Respuesta inv\xE1lida"
|
|
106
|
+
}));
|
|
107
|
+
if (body.valid === true && body.userId && body.eventId && body.expiresAt) {
|
|
108
|
+
return body;
|
|
109
|
+
}
|
|
110
|
+
if (body.valid === false) {
|
|
111
|
+
return { valid: false, reason: body.reason ?? "Token inv\xE1lido" };
|
|
112
|
+
}
|
|
113
|
+
if (!res.ok) {
|
|
114
|
+
return {
|
|
115
|
+
valid: false,
|
|
116
|
+
reason: body.message ?? `Error ${res.status} al validar token de cola`
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
return { valid: false, reason: "Respuesta inv\xE1lida" };
|
|
120
|
+
}
|
|
94
121
|
};
|
|
95
122
|
|
|
96
123
|
export {
|
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CENTRALQ_DEFAULT_API_URL,
|
|
3
|
+
QueueHttpClient
|
|
4
|
+
} from "./chunk-P73Q2ZIO.mjs";
|
|
5
|
+
|
|
6
|
+
// src/admission.ts
|
|
7
|
+
var turnstileScript = null;
|
|
8
|
+
function loadManagedAdmissionDriver() {
|
|
9
|
+
if (typeof window === "undefined" || typeof document === "undefined") {
|
|
10
|
+
return Promise.reject(
|
|
11
|
+
new Error("La admisi\xF3n administrada requiere un navegador")
|
|
12
|
+
);
|
|
13
|
+
}
|
|
14
|
+
if (window.turnstile) return Promise.resolve(window.turnstile);
|
|
15
|
+
if (turnstileScript) return turnstileScript;
|
|
16
|
+
turnstileScript = new Promise((resolve, reject) => {
|
|
17
|
+
const script = document.createElement("script");
|
|
18
|
+
script.src = "https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit";
|
|
19
|
+
script.async = true;
|
|
20
|
+
script.defer = true;
|
|
21
|
+
script.onload = () => {
|
|
22
|
+
if (window.turnstile) resolve(window.turnstile);
|
|
23
|
+
else {
|
|
24
|
+
turnstileScript = null;
|
|
25
|
+
reject(new Error("No se pudo inicializar la admisi\xF3n administrada"));
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
script.onerror = () => {
|
|
29
|
+
turnstileScript = null;
|
|
30
|
+
reject(new Error("No se pudo cargar la admisi\xF3n administrada"));
|
|
31
|
+
};
|
|
32
|
+
document.head.appendChild(script);
|
|
33
|
+
});
|
|
34
|
+
return turnstileScript;
|
|
35
|
+
}
|
|
36
|
+
async function acquireManagedAdmissionProof(publicKey) {
|
|
37
|
+
const driver = await loadManagedAdmissionDriver();
|
|
38
|
+
const container = document.createElement("div");
|
|
39
|
+
container.style.position = "fixed";
|
|
40
|
+
container.style.inset = "0";
|
|
41
|
+
container.style.zIndex = "2147483647";
|
|
42
|
+
container.style.display = "grid";
|
|
43
|
+
container.style.placeItems = "center";
|
|
44
|
+
container.style.pointerEvents = "auto";
|
|
45
|
+
document.body.appendChild(container);
|
|
46
|
+
return new Promise((resolve, reject) => {
|
|
47
|
+
let widgetId = "";
|
|
48
|
+
let timeout;
|
|
49
|
+
const cleanup = () => {
|
|
50
|
+
clearTimeout(timeout);
|
|
51
|
+
if (widgetId) driver.remove(widgetId);
|
|
52
|
+
container.remove();
|
|
53
|
+
};
|
|
54
|
+
timeout = setTimeout(() => {
|
|
55
|
+
cleanup();
|
|
56
|
+
reject(new Error("La validaci\xF3n de admisi\xF3n agot\xF3 el tiempo de espera"));
|
|
57
|
+
}, 15e3);
|
|
58
|
+
try {
|
|
59
|
+
widgetId = driver.render(container, {
|
|
60
|
+
sitekey: publicKey,
|
|
61
|
+
appearance: "interaction-only",
|
|
62
|
+
execution: "execute",
|
|
63
|
+
action: "centralq_admission",
|
|
64
|
+
callback: (token) => {
|
|
65
|
+
cleanup();
|
|
66
|
+
resolve(token);
|
|
67
|
+
},
|
|
68
|
+
"error-callback": () => {
|
|
69
|
+
cleanup();
|
|
70
|
+
reject(new Error("La validaci\xF3n de admisi\xF3n fall\xF3"));
|
|
71
|
+
},
|
|
72
|
+
"expired-callback": () => {
|
|
73
|
+
cleanup();
|
|
74
|
+
reject(new Error("La validaci\xF3n de admisi\xF3n expir\xF3"));
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
driver.execute(widgetId);
|
|
78
|
+
} catch {
|
|
79
|
+
cleanup();
|
|
80
|
+
reject(new Error("No se pudo ejecutar la admisi\xF3n administrada"));
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// src/centralq.ts
|
|
86
|
+
var CentralQ = class _CentralQ {
|
|
87
|
+
constructor(options) {
|
|
88
|
+
this.element = null;
|
|
89
|
+
this.destroyed = false;
|
|
90
|
+
// biome-ignore lint: allow explicit any for generic event map
|
|
91
|
+
this.listeners = /* @__PURE__ */ new Map();
|
|
92
|
+
this.client = new QueueHttpClient(
|
|
93
|
+
options.apiUrl,
|
|
94
|
+
options.apiKey,
|
|
95
|
+
options.queueInitToken
|
|
96
|
+
);
|
|
97
|
+
this.eventId = options.eventId;
|
|
98
|
+
this.pollInterval = options.pollInterval ?? 1e4;
|
|
99
|
+
this.container = options.container ?? document.body;
|
|
100
|
+
this.userId = this.resolveUserId(options.userId);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Inicializa CentralQ y comienza el flujo de cola automáticamente.
|
|
104
|
+
* Monta el overlay de UI y empieza a hacer polling.
|
|
105
|
+
*/
|
|
106
|
+
static init(options) {
|
|
107
|
+
const instance = new _CentralQ(options);
|
|
108
|
+
instance.mount();
|
|
109
|
+
instance.checkQueue();
|
|
110
|
+
return instance;
|
|
111
|
+
}
|
|
112
|
+
// Event API
|
|
113
|
+
/** Suscribirse a un evento del ciclo de vida de la cola */
|
|
114
|
+
on(event, listener) {
|
|
115
|
+
if (!this.listeners.has(event)) {
|
|
116
|
+
this.listeners.set(event, /* @__PURE__ */ new Set());
|
|
117
|
+
}
|
|
118
|
+
this.listeners.get(event).add(listener);
|
|
119
|
+
return this;
|
|
120
|
+
}
|
|
121
|
+
/** Desuscribirse de un evento */
|
|
122
|
+
off(event, listener) {
|
|
123
|
+
this.listeners.get(event)?.delete(listener);
|
|
124
|
+
return this;
|
|
125
|
+
}
|
|
126
|
+
emit(event, detail) {
|
|
127
|
+
this.listeners.get(event)?.forEach((fn) => fn(detail));
|
|
128
|
+
}
|
|
129
|
+
// Métodos públicos
|
|
130
|
+
/**
|
|
131
|
+
* Libera el slot del usuario inmediatamente.
|
|
132
|
+
* Llamar cuando el usuario completa la compra.
|
|
133
|
+
*/
|
|
134
|
+
leave() {
|
|
135
|
+
this.stopTimers();
|
|
136
|
+
this.updateUI("ACTIVE");
|
|
137
|
+
this.client.leaveQueue(this.eventId, this.userId).catch(() => {
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Destruye la instancia: para todos los timers, desmonta el overlay,
|
|
142
|
+
* y libera el slot en el servidor.
|
|
143
|
+
*/
|
|
144
|
+
destroy() {
|
|
145
|
+
this.destroyed = true;
|
|
146
|
+
this.stopTimers();
|
|
147
|
+
this.client.leaveQueue(this.eventId, this.userId).catch(() => {
|
|
148
|
+
});
|
|
149
|
+
this.unmount();
|
|
150
|
+
this.listeners.clear();
|
|
151
|
+
}
|
|
152
|
+
/** Retorna el userId resuelto (externo o autogenerado) */
|
|
153
|
+
getUserId() {
|
|
154
|
+
return this.userId;
|
|
155
|
+
}
|
|
156
|
+
// userId: genera y persiste automáticamente
|
|
157
|
+
resolveUserId(externalId) {
|
|
158
|
+
if (externalId) return externalId;
|
|
159
|
+
const key = `ctq_anon_${this.eventId}`;
|
|
160
|
+
try {
|
|
161
|
+
const stored = sessionStorage.getItem(key);
|
|
162
|
+
if (stored) return stored;
|
|
163
|
+
} catch {
|
|
164
|
+
}
|
|
165
|
+
const generated = `anon_${crypto.randomUUID()}`;
|
|
166
|
+
try {
|
|
167
|
+
sessionStorage.setItem(key, generated);
|
|
168
|
+
} catch {
|
|
169
|
+
}
|
|
170
|
+
return generated;
|
|
171
|
+
}
|
|
172
|
+
// UI: montar/desmontar el Web Component
|
|
173
|
+
mount() {
|
|
174
|
+
import("./queue-element-DXBW64U2.mjs");
|
|
175
|
+
this.element = document.createElement("central-q");
|
|
176
|
+
this.element.onRetry = () => {
|
|
177
|
+
this.updateUI("LOADING");
|
|
178
|
+
this.checkQueue();
|
|
179
|
+
};
|
|
180
|
+
this.container.appendChild(this.element);
|
|
181
|
+
}
|
|
182
|
+
unmount() {
|
|
183
|
+
if (this.element) {
|
|
184
|
+
this.element.remove();
|
|
185
|
+
this.element = null;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
updateUI(status, data) {
|
|
189
|
+
if (!this.element) return;
|
|
190
|
+
this.element.status = status;
|
|
191
|
+
if (data?.position !== void 0) this.element.position = data.position;
|
|
192
|
+
if (data?.ahead !== void 0) this.element.ahead = data.ahead;
|
|
193
|
+
}
|
|
194
|
+
// Timers
|
|
195
|
+
stopTimers() {
|
|
196
|
+
if (this.pollingTimer) {
|
|
197
|
+
clearInterval(this.pollingTimer);
|
|
198
|
+
this.pollingTimer = void 0;
|
|
199
|
+
}
|
|
200
|
+
if (this.heartbeatTimer) {
|
|
201
|
+
clearInterval(this.heartbeatTimer);
|
|
202
|
+
this.heartbeatTimer = void 0;
|
|
203
|
+
}
|
|
204
|
+
if (this.expiryTimer) {
|
|
205
|
+
clearTimeout(this.expiryTimer);
|
|
206
|
+
this.expiryTimer = void 0;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
startHeartbeat() {
|
|
210
|
+
if (this.heartbeatTimer) return;
|
|
211
|
+
this.heartbeatTimer = setInterval(() => {
|
|
212
|
+
this.client.sendHeartbeat(this.eventId, this.userId).catch(() => {
|
|
213
|
+
});
|
|
214
|
+
}, this.pollInterval);
|
|
215
|
+
}
|
|
216
|
+
scheduleExpiry(expiresAt) {
|
|
217
|
+
if (this.expiryTimer) clearTimeout(this.expiryTimer);
|
|
218
|
+
const msLeft = expiresAt * 1e3 - Date.now();
|
|
219
|
+
if (msLeft <= 0) {
|
|
220
|
+
this.handleExpired();
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
this.expiryTimer = setTimeout(() => this.handleExpired(), msLeft);
|
|
224
|
+
}
|
|
225
|
+
handleExpired() {
|
|
226
|
+
this.stopTimers();
|
|
227
|
+
this.updateUI("EXPIRED");
|
|
228
|
+
this.emit("expired", {
|
|
229
|
+
userId: this.userId,
|
|
230
|
+
eventId: this.eventId
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
// Core: polling + join
|
|
234
|
+
async checkQueue() {
|
|
235
|
+
if (this.destroyed) return;
|
|
236
|
+
try {
|
|
237
|
+
const res = await this.client.joinQueue(this.eventId, this.userId);
|
|
238
|
+
if (res.status === "ACTIVE") {
|
|
239
|
+
if (this.pollingTimer) {
|
|
240
|
+
clearInterval(this.pollingTimer);
|
|
241
|
+
this.pollingTimer = void 0;
|
|
242
|
+
}
|
|
243
|
+
this.startHeartbeat();
|
|
244
|
+
this.scheduleExpiry(res.expiresAt);
|
|
245
|
+
this.updateUI("ACTIVE");
|
|
246
|
+
this.emit("passed", {
|
|
247
|
+
token: res.token,
|
|
248
|
+
expiresAt: res.expiresAt,
|
|
249
|
+
userId: this.userId,
|
|
250
|
+
eventId: this.eventId
|
|
251
|
+
});
|
|
252
|
+
} else if (res.status === "WAITING") {
|
|
253
|
+
this.updateUI("WAITING", {
|
|
254
|
+
position: res.position,
|
|
255
|
+
ahead: res.ahead
|
|
256
|
+
});
|
|
257
|
+
this.emit("position", {
|
|
258
|
+
position: res.position,
|
|
259
|
+
ahead: res.ahead,
|
|
260
|
+
userId: this.userId,
|
|
261
|
+
eventId: this.eventId
|
|
262
|
+
});
|
|
263
|
+
if (!this.pollingTimer) {
|
|
264
|
+
this.pollingTimer = setInterval(
|
|
265
|
+
() => this.checkQueue(),
|
|
266
|
+
this.pollInterval
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
} catch {
|
|
271
|
+
this.updateUI("ERROR");
|
|
272
|
+
this.stopTimers();
|
|
273
|
+
this.emit("error", {
|
|
274
|
+
userId: this.userId,
|
|
275
|
+
eventId: this.eventId
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
};
|
|
280
|
+
var CentralQClient = class {
|
|
281
|
+
constructor(defaults) {
|
|
282
|
+
this.defaults = defaults;
|
|
283
|
+
}
|
|
284
|
+
issueQueueInitToken(eventId, userId) {
|
|
285
|
+
const client = new QueueHttpClient(
|
|
286
|
+
this.defaults.apiUrl,
|
|
287
|
+
this.defaults.apiKey
|
|
288
|
+
);
|
|
289
|
+
return this.issueInitToken(client, eventId, userId);
|
|
290
|
+
}
|
|
291
|
+
async issueInitToken(client, eventId, userId) {
|
|
292
|
+
if (typeof window === "undefined") {
|
|
293
|
+
return client.issueQueueInitToken(eventId, userId);
|
|
294
|
+
}
|
|
295
|
+
const protection = await client.getProtection(eventId);
|
|
296
|
+
if (!protection.challengeRequired) {
|
|
297
|
+
return client.issueQueueInitToken(eventId, userId);
|
|
298
|
+
}
|
|
299
|
+
const challenge = await client.getAdmissionChallenge(eventId);
|
|
300
|
+
if (!challenge.required) {
|
|
301
|
+
throw new Error("El evento requiere admisi\xF3n administrada");
|
|
302
|
+
}
|
|
303
|
+
const proof = await acquireManagedAdmissionProof(challenge.publicKey);
|
|
304
|
+
return client.issueQueueInitToken(eventId, userId, proof);
|
|
305
|
+
}
|
|
306
|
+
createQueue(options) {
|
|
307
|
+
return CentralQ.init({
|
|
308
|
+
apiUrl: options.apiUrl ?? this.defaults.apiUrl,
|
|
309
|
+
apiKey: options.apiKey ?? this.defaults.apiKey,
|
|
310
|
+
eventId: options.eventId,
|
|
311
|
+
userId: options.userId ?? this.defaults.userId,
|
|
312
|
+
queueInitToken: options.queueInitToken ?? this.defaults.queueInitToken,
|
|
313
|
+
pollInterval: options.pollInterval ?? this.defaults.pollInterval,
|
|
314
|
+
container: options.container ?? this.defaults.container
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
function createCentralQClient(options) {
|
|
319
|
+
return new CentralQClient({
|
|
320
|
+
...options,
|
|
321
|
+
apiUrl: options.apiUrl ?? CENTRALQ_DEFAULT_API_URL
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
export {
|
|
326
|
+
CentralQ,
|
|
327
|
+
CentralQClient,
|
|
328
|
+
createCentralQClient
|
|
329
|
+
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
CENTRALQ_DEFAULT_API_URL
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-A37B25XO.mjs";
|
|
4
4
|
|
|
5
5
|
// src/server.ts
|
|
6
6
|
function normalizeApiUrl(apiUrl) {
|
|
@@ -9,7 +9,7 @@ function normalizeApiUrl(apiUrl) {
|
|
|
9
9
|
async function validateQueueTokenServer(options) {
|
|
10
10
|
const apiUrl = options.apiUrl ?? CENTRALQ_DEFAULT_API_URL;
|
|
11
11
|
const response = await fetch(
|
|
12
|
-
`${normalizeApiUrl(apiUrl)}/api/verify/${encodeURIComponent(options.eventId)}`,
|
|
12
|
+
`${normalizeApiUrl(apiUrl)}/api/queue/verify/${encodeURIComponent(options.eventId)}`,
|
|
13
13
|
{
|
|
14
14
|
method: "POST",
|
|
15
15
|
headers: {
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CENTRALQ_DEFAULT_API_URL
|
|
3
|
+
} from "./chunk-P73Q2ZIO.mjs";
|
|
4
|
+
|
|
5
|
+
// src/server.ts
|
|
6
|
+
function normalizeApiUrl(apiUrl) {
|
|
7
|
+
return apiUrl.replace(/\/+$/, "");
|
|
8
|
+
}
|
|
9
|
+
async function validateQueueTokenServer(options) {
|
|
10
|
+
const apiUrl = options.apiUrl ?? CENTRALQ_DEFAULT_API_URL;
|
|
11
|
+
const response = await fetch(
|
|
12
|
+
`${normalizeApiUrl(apiUrl)}/api/queue/verify/${encodeURIComponent(options.eventId)}`,
|
|
13
|
+
{
|
|
14
|
+
method: "POST",
|
|
15
|
+
headers: {
|
|
16
|
+
"Content-Type": "application/json",
|
|
17
|
+
"x-api-key": options.secretKey
|
|
18
|
+
},
|
|
19
|
+
body: JSON.stringify({ token: options.token })
|
|
20
|
+
}
|
|
21
|
+
);
|
|
22
|
+
const data = await response.json().catch(() => ({
|
|
23
|
+
valid: false,
|
|
24
|
+
reason: "Respuesta inv\xE1lida"
|
|
25
|
+
}));
|
|
26
|
+
return { status: response.status, data };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export {
|
|
30
|
+
validateQueueTokenServer
|
|
31
|
+
};
|