@eyepop.ai/eyepop 0.11.0 → 0.13.0

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/index.js CHANGED
@@ -8944,7 +8944,7 @@ var AbstractJob = class {
8944
8944
  return Promise.reject("abstract class");
8945
8945
  }
8946
8946
  start(done, status) {
8947
- this._responseStream = new Promise(async (resolve2, reject) => {
8947
+ this._responseStream = new Promise(async (resolve, reject) => {
8948
8948
  try {
8949
8949
  let retries = 1;
8950
8950
  while (true) {
@@ -8973,7 +8973,7 @@ var AbstractJob = class {
8973
8973
  reject(`response body is empty`);
8974
8974
  break;
8975
8975
  } else {
8976
- resolve2(response.body);
8976
+ resolve(response.body);
8977
8977
  break;
8978
8978
  }
8979
8979
  } catch (error) {
@@ -9083,7 +9083,7 @@ var LoadLiveIngressJob = class extends AbstractJob {
9083
9083
  "Accept": "application/jsonl",
9084
9084
  "Content-Type": "application/json"
9085
9085
  };
9086
- const patchUrl = `${session.baseUrl.replace(/\/+$/, "")}/pipelines/${session.pipelineId}/source?mode=queue&processing=sync`;
9086
+ const patchUrl = `${session.baseUrl.replace(/\/+$/, "")}/pipelines/${session.pipelineId}/source?mode=preempt&processing=sync`;
9087
9087
  this._requestLogger.debug("before PATCH %s with url %s as source", patchUrl, this._ingressId);
9088
9088
  const response = await this._client.fetch(patchUrl, {
9089
9089
  headers,
@@ -9312,17 +9312,91 @@ function v4(options, buf, offset) {
9312
9312
  }
9313
9313
  var v4_default = v4;
9314
9314
 
9315
- // src/web_rtc_base.ts
9316
- var WebRtcBase = class {
9317
- constructor(getSession, client, requestLogger) {
9318
- this._pc = null;
9315
+ // src/webrtc_base.ts
9316
+ var WebrtcBase = class _WebrtcBase {
9317
+ constructor(getSession, client, ingressId, urlBasePath, requestLogger) {
9319
9318
  this._getSession = getSession;
9320
9319
  this._client = client;
9321
9320
  this._requestLogger = requestLogger;
9321
+ this._ingressId = ingressId;
9322
+ this._urlPath = `${urlBasePath}/${this._ingressId}`;
9323
+ this._pc = null;
9322
9324
  this._eTag = "";
9323
9325
  this._queuedCandidates = [];
9324
9326
  this._offerData = null;
9325
9327
  }
9328
+ async close() {
9329
+ const pc = this._pc;
9330
+ this._pc = null;
9331
+ if (pc) {
9332
+ return pc.close();
9333
+ }
9334
+ }
9335
+ ingressId() {
9336
+ return this._ingressId;
9337
+ }
9338
+ async onLocalOffer(offer) {
9339
+ if (!this._pc) {
9340
+ throw new Error("onLocalOffer no peer connection");
9341
+ }
9342
+ if (!offer.sdp) {
9343
+ throw new Error("onLocalOffer no sdp");
9344
+ }
9345
+ this._offerData = _WebrtcBase.parseOffer(offer.sdp);
9346
+ await this._pc.setLocalDescription(offer);
9347
+ const session = await this._getSession();
9348
+ const ingressUrl = new URL(this._urlPath, session.baseUrl);
9349
+ this._requestLogger.debug("before POST: %s", ingressUrl);
9350
+ const headers = {
9351
+ "Authorization": `Bearer ${session.accessToken}`,
9352
+ "Content-Type": "application/sdp"
9353
+ };
9354
+ const response = await this._client.fetch(ingressUrl, {
9355
+ headers,
9356
+ method: "POST",
9357
+ body: offer.sdp
9358
+ });
9359
+ if (response.status != 201) {
9360
+ return Promise.reject(`unknown status code for POST '${ingressUrl}': ${response.status} (${response.statusText})`);
9361
+ }
9362
+ this._eTag = response.headers.get("ETag") ?? "";
9363
+ return this.onRemoteAnswer(new RTCSessionDescription({
9364
+ type: "answer",
9365
+ sdp: await response.text()
9366
+ }));
9367
+ }
9368
+ async sendLocalCandidates(candidates) {
9369
+ if (!this._offerData) {
9370
+ throw new Error("sendLocalCandidates no offerData");
9371
+ }
9372
+ const session = await this._getSession();
9373
+ const ingressUrl = new URL(this._urlPath, session.baseUrl);
9374
+ this._requestLogger.debug("before PATCH: %s", ingressUrl);
9375
+ const headers = {
9376
+ "Authorization": `Bearer ${session.accessToken}`,
9377
+ "Content-Type": "application/trickle-ice-sdpfrag",
9378
+ "If-Match": this._eTag
9379
+ };
9380
+ const response = await this._client.fetch(ingressUrl, {
9381
+ headers,
9382
+ method: "PATCH",
9383
+ body: _WebrtcBase.generateSdpFragment(this._offerData, candidates)
9384
+ });
9385
+ if (response.status != 204) {
9386
+ return Promise.reject(`unknown status code for PATCH '${ingressUrl}': ${response.status} (${response.statusText})`);
9387
+ }
9388
+ return this;
9389
+ }
9390
+ async onLocalCandidate(evt) {
9391
+ if (evt.candidate !== null) {
9392
+ if (this._eTag === "") {
9393
+ this._queuedCandidates.push(evt.candidate);
9394
+ } else {
9395
+ await this.sendLocalCandidates([evt.candidate]);
9396
+ }
9397
+ }
9398
+ return this;
9399
+ }
9326
9400
  static linkToIceServers(links) {
9327
9401
  const servers = links !== null ? links.split(", ").map((link) => {
9328
9402
  const m = link.match(/^<(.+?)>; rel="ice-server"(; username="(.*?)"; credential="(.*?)"; credential-type="password")?/i);
@@ -9331,8 +9405,8 @@ var WebRtcBase = class {
9331
9405
  }
9332
9406
  const iceServer = {
9333
9407
  urls: [m[1]],
9334
- username: Whip.unquoteCredential(m[3]),
9335
- credential: Whip.unquoteCredential(m[4])
9408
+ username: WebrtcWhip.unquoteCredential(m[3]),
9409
+ credential: WebrtcWhip.unquoteCredential(m[4])
9336
9410
  // credentialType: m[3]? "password" : undefined
9337
9411
  };
9338
9412
  return iceServer;
@@ -9346,9 +9420,9 @@ var WebRtcBase = class {
9346
9420
  for (let i = 0; i < sections.length; i++) {
9347
9421
  const section = sections[i];
9348
9422
  if (section.startsWith("video")) {
9349
- sections[i] = Whip.setVideoBitrate(Whip.setCodec(section, videoCodec), videoBitrate);
9423
+ sections[i] = WebrtcWhip.setVideoBitrate(WebrtcWhip.setCodec(section, videoCodec), videoBitrate);
9350
9424
  } else if (section.startsWith("audio")) {
9351
- sections[i] = Whip.setAudioBitrate(Whip.setCodec(section, audioCodec), audioBitrate, audioVoice);
9425
+ sections[i] = WebrtcWhip.setAudioBitrate(WebrtcWhip.setCodec(section, audioCodec), audioBitrate, audioVoice);
9352
9426
  }
9353
9427
  }
9354
9428
  return sections.join("m=");
@@ -9470,22 +9544,15 @@ var WebRtcBase = class {
9470
9544
  }
9471
9545
  };
9472
9546
 
9473
- // src/whip.ts
9474
- var Whip = class _Whip extends WebRtcBase {
9547
+ // src/webrtc_whip.ts
9548
+ var WebrtcWhip = class _WebrtcWhip extends WebrtcBase {
9475
9549
  constructor(stream, getSession, client, requestLogger) {
9476
- super(getSession, client, requestLogger);
9477
- this._ingressId = v4_default().toString();
9550
+ super(getSession, client, v4_default().toString(), "/liveIngress/whip", requestLogger);
9478
9551
  this._stream = stream;
9479
9552
  }
9480
- ingressId() {
9481
- if (!this._ingressId) {
9482
- throw new Error("not initialized");
9483
- }
9484
- return this._ingressId;
9485
- }
9486
9553
  async start() {
9487
9554
  const session = await this._getSession();
9488
- const ingressUrl = new URL("/liveIngress/whip/" + this._ingressId, session.baseUrl);
9555
+ const ingressUrl = new URL(this._urlPath, session.baseUrl);
9489
9556
  this._requestLogger.debug("before GET: %s", ingressUrl);
9490
9557
  const headers = {
9491
9558
  "Authorization": `Bearer ${session.accessToken}`
@@ -9499,17 +9566,46 @@ var Whip = class _Whip extends WebRtcBase {
9499
9566
  }
9500
9567
  return this.onIceServers(response);
9501
9568
  }
9569
+ async stream() {
9570
+ if (this._stream) {
9571
+ return Promise.resolve(this._stream);
9572
+ } else {
9573
+ return Promise.reject("fetching stream failed");
9574
+ }
9575
+ }
9576
+ async onRemoteAnswer(answer) {
9577
+ if (!this._pc) {
9578
+ throw new Error("onLocalOffer no peer connection");
9579
+ }
9580
+ answer = new RTCSessionDescription({
9581
+ type: "answer",
9582
+ sdp: _WebrtcWhip.editAnswer(
9583
+ answer.sdp,
9584
+ "h264/90000",
9585
+ void 0,
9586
+ 5e3,
9587
+ void 0,
9588
+ true
9589
+ )
9590
+ });
9591
+ await this._pc.setRemoteDescription(new RTCSessionDescription(answer));
9592
+ if (this._queuedCandidates.length !== 0) {
9593
+ await this.sendLocalCandidates(this._queuedCandidates);
9594
+ this._queuedCandidates = [];
9595
+ }
9596
+ return this;
9597
+ }
9502
9598
  async onIceServers(response) {
9503
9599
  if (!this._stream) {
9504
9600
  throw new Error("onIceServers no stream");
9505
9601
  }
9506
9602
  const stream = this._stream;
9507
9603
  const pc = new RTCPeerConnection({
9508
- iceServers: _Whip.linkToIceServers(response.headers.get("Link"))
9604
+ iceServers: WebrtcBase.linkToIceServers(response.headers.get("Link"))
9509
9605
  });
9510
9606
  this._pc = pc;
9511
9607
  pc.onicecandidate = (evt) => this.onLocalCandidate(evt);
9512
- return new Promise(async (resolve2, reject) => {
9608
+ return new Promise(async (resolve, reject) => {
9513
9609
  pc.oniceconnectionstatechange = () => {
9514
9610
  switch (pc.iceConnectionState) {
9515
9611
  case "failed":
@@ -9517,7 +9613,7 @@ var Whip = class _Whip extends WebRtcBase {
9517
9613
  reject(`peer connection: ${pc.iceConnectionState}`);
9518
9614
  break;
9519
9615
  case "connected":
9520
- resolve2(this);
9616
+ resolve(this);
9521
9617
  break;
9522
9618
  }
9523
9619
  };
@@ -9532,61 +9628,63 @@ var Whip = class _Whip extends WebRtcBase {
9532
9628
  await this.onLocalOffer(await pc.createOffer());
9533
9629
  });
9534
9630
  }
9535
- async onLocalCandidate(evt) {
9536
- if (evt.candidate !== null) {
9537
- if (this._eTag === "") {
9538
- this._queuedCandidates.push(evt.candidate);
9539
- } else {
9540
- await this.sendLocalCandidates([evt.candidate]);
9541
- }
9631
+ async close() {
9632
+ const stream = this._stream;
9633
+ this._stream = null;
9634
+ if (stream) {
9635
+ const tracks = stream.getTracks();
9636
+ tracks.forEach((track) => {
9637
+ track.stop();
9638
+ });
9542
9639
  }
9543
- return this;
9640
+ return super.close();
9544
9641
  }
9545
- async onLocalOffer(offer) {
9546
- if (!this._pc) {
9547
- throw new Error("onLocalOffer no peer connection");
9642
+ };
9643
+
9644
+ // src/webrtc_whep.ts
9645
+ var WebrtcWhep = class extends WebrtcBase {
9646
+ constructor(ingressId, getSession, client, requestLogger) {
9647
+ super(getSession, client, ingressId, "/liveIngress/whep", requestLogger);
9648
+ this._stream = null;
9649
+ }
9650
+ async close() {
9651
+ const stream = this._stream;
9652
+ this._stream = null;
9653
+ if (stream) {
9654
+ const tracks = stream.getTracks();
9655
+ tracks.forEach((track) => {
9656
+ track.stop();
9657
+ });
9548
9658
  }
9549
- if (!offer.sdp) {
9550
- throw new Error("onLocalOffer no sdp");
9659
+ return super.close();
9660
+ }
9661
+ async stream() {
9662
+ if (this._stream) {
9663
+ return Promise.resolve(this._stream);
9664
+ } else {
9665
+ return Promise.reject("fetching stream failed");
9551
9666
  }
9552
- this._offerData = _Whip.parseOffer(offer.sdp);
9553
- await this._pc.setLocalDescription(offer);
9667
+ }
9668
+ async start() {
9554
9669
  const session = await this._getSession();
9555
- const ingressUrl = new URL("/liveIngress/whip/" + this._ingressId, session.baseUrl);
9556
- this._requestLogger.debug("before GET: %s", ingressUrl);
9670
+ const egressUrl = new URL(this._urlPath, session.baseUrl);
9671
+ this._requestLogger.debug("before GET: %s", egressUrl);
9557
9672
  const headers = {
9558
- "Authorization": `Bearer ${session.accessToken}`,
9559
- "Content-Type": "application/sdp"
9673
+ "Authorization": `Bearer ${session.accessToken}`
9560
9674
  };
9561
- const response = await this._client.fetch(ingressUrl, {
9675
+ const response = await this._client.fetch(egressUrl, {
9562
9676
  headers,
9563
- method: "POST",
9564
- body: offer.sdp
9677
+ method: "GET"
9565
9678
  });
9566
- if (response.status != 201) {
9567
- return Promise.reject(`unknown status code for POST '${ingressUrl}': ${response.status} (${response.statusText})`);
9679
+ if (response.status >= 300) {
9680
+ return Promise.reject(`unknown status code for GET '${egressUrl}': ${response.status} (${response.statusText})`);
9568
9681
  }
9569
- this._eTag = response.headers.get("ETag") ?? "";
9570
- return this.onRemoteAnswer(new RTCSessionDescription({
9571
- type: "answer",
9572
- sdp: await response.text()
9573
- }));
9682
+ return this.onIceServers(response);
9574
9683
  }
9575
9684
  async onRemoteAnswer(answer) {
9576
9685
  if (!this._pc) {
9577
9686
  throw new Error("onLocalOffer no peer connection");
9578
9687
  }
9579
- answer = new RTCSessionDescription({
9580
- type: "answer",
9581
- sdp: _Whip.editAnswer(
9582
- answer.sdp,
9583
- "h264/90000",
9584
- void 0,
9585
- 5e3,
9586
- void 0,
9587
- true
9588
- )
9589
- });
9590
9688
  await this._pc.setRemoteDescription(new RTCSessionDescription(answer));
9591
9689
  if (this._queuedCandidates.length !== 0) {
9592
9690
  await this.sendLocalCandidates(this._queuedCandidates);
@@ -9594,89 +9692,100 @@ var Whip = class _Whip extends WebRtcBase {
9594
9692
  }
9595
9693
  return this;
9596
9694
  }
9597
- async sendLocalCandidates(candidates) {
9598
- if (!this._offerData) {
9599
- throw new Error("sendLocalCandidates no offerData");
9695
+ async onIceServers(response) {
9696
+ if (this._stream) {
9697
+ throw new Error("onIceServers has stream");
9600
9698
  }
9601
- const session = await this._getSession();
9602
- const ingressUrl = new URL("/liveIngress/whip/" + this._ingressId, session.baseUrl);
9603
- this._requestLogger.debug("before PATCH: %s", ingressUrl);
9604
- const headers = {
9605
- "Authorization": `Bearer ${session.accessToken}`,
9606
- "Content-Type": "application/trickle-ice-sdpfrag",
9607
- "If-Match": this._eTag
9608
- };
9609
- const response = await this._client.fetch(ingressUrl, {
9610
- headers,
9611
- method: "PATCH",
9612
- body: _Whip.generateSdpFragment(this._offerData, candidates)
9699
+ const pc = new RTCPeerConnection({
9700
+ iceServers: WebrtcBase.linkToIceServers(response.headers.get("Link"))
9701
+ });
9702
+ const direction = "sendrecv";
9703
+ pc.addTransceiver("video", { direction });
9704
+ pc.onicecandidate = (evt) => this.onLocalCandidate(evt);
9705
+ this._pc = pc;
9706
+ this._requestLogger.info("before", pc.iceConnectionState);
9707
+ return new Promise(async (resolve, reject) => {
9708
+ pc.ontrack = (evt) => {
9709
+ this._requestLogger.debug("new track:", evt.track.kind);
9710
+ this._stream = evt.streams[0];
9711
+ resolve(this);
9712
+ };
9713
+ pc.oniceconnectionstatechange = () => {
9714
+ switch (pc.iceConnectionState) {
9715
+ case "failed":
9716
+ case "disconnected":
9717
+ this._requestLogger.debug("disconnected");
9718
+ reject(`peer connection: ${pc.iceConnectionState}`);
9719
+ break;
9720
+ case "connected":
9721
+ this._requestLogger.debug("connected");
9722
+ break;
9723
+ }
9724
+ };
9725
+ await this.onLocalOffer(await pc.createOffer());
9613
9726
  });
9614
- if (response.status != 204) {
9615
- return Promise.reject(`unknown status code for PATCH '${ingressUrl}': ${response.status} (${response.statusText})`);
9616
- }
9617
- return this;
9618
- }
9619
- async close() {
9620
- const pc = this._pc;
9621
- const stream = this._stream;
9622
- this._stream = null;
9623
- this._pc = null;
9624
- if (stream) {
9625
- const tracks = stream.getTracks();
9626
- tracks.forEach((track) => {
9627
- track.stop();
9628
- });
9629
- }
9630
- if (pc) {
9631
- return pc.close();
9632
- }
9633
9727
  }
9634
9728
  };
9635
9729
 
9636
9730
  // src/shims/local_file.ts
9637
- var resolve;
9731
+ var resolvePath;
9638
9732
  if ("document" in globalThis && "implementation" in globalThis.document) {
9639
- const isFileLike = (value) => value != null && typeof value === "object" && typeof value.name === "string" && typeof value.lastModified === "number";
9640
- resolve = async (params) => {
9641
- if (params.file) {
9642
- if (isFileLike(params.file)) {
9643
- params = {
9644
- mimeType: params.file.type,
9645
- file: params.file
9646
- };
9647
- return params;
9648
- }
9649
- }
9733
+ resolvePath = async (source) => {
9650
9734
  throw new DOMException("resolving a path to a file is not supported in browser");
9651
9735
  };
9652
9736
  } else {
9653
- resolve = async (params) => {
9654
- if (params.file) {
9655
- return params;
9656
- }
9737
+ resolvePath = async (source) => {
9657
9738
  const mime = require_mime_types();
9658
- const fs = require("fs");
9659
- let mimeType;
9660
- let stream;
9661
- if (params.filePath) {
9662
- if (params.mimeType) {
9663
- mimeType = params.mimeType;
9664
- } else {
9665
- mimeType = mime.lookup(params.filePath) || void 0;
9739
+ const filehandle = require("fs/promises");
9740
+ const fd = await filehandle.open(source.path, "r");
9741
+ const stream = fd.createReadStream();
9742
+ const mimeType = mime.lookup(source.path) || "application/octet-stream";
9743
+ return {
9744
+ stream,
9745
+ mimeType
9746
+ };
9747
+ };
9748
+ }
9749
+
9750
+ // src/endpoint.ts
9751
+ var import_pino = require("pino");
9752
+
9753
+ // src/shims/browser_session.ts
9754
+ var import_auth0_spa_js = require("@auth0/auth0-spa-js");
9755
+ var authenticateBrowserSession;
9756
+ if ("document" in globalThis && "implementation" in globalThis.document) {
9757
+ authenticateBrowserSession = async (auth0, eyepopUrl, popId) => {
9758
+ const options = {
9759
+ clientId: auth0.clientId,
9760
+ domain: auth0.domain,
9761
+ authorizationParams: {
9762
+ scope: auth0.scope,
9763
+ audience: auth0.audience
9666
9764
  }
9667
- stream = fs.createReadStream(params.filePath);
9765
+ };
9766
+ const auth0Client = await (0, import_auth0_spa_js.createAuth0Client)(options);
9767
+ if (!await auth0Client.isAuthenticated()) {
9768
+ await auth0Client.loginWithPopup();
9668
9769
  }
9669
- params = {
9670
- filePath: params.filePath,
9671
- mimeType,
9672
- file: stream
9770
+ const accessToken = await auth0Client.getTokenSilently();
9771
+ if (!accessToken) {
9772
+ return Promise.reject("auth0 login failed");
9773
+ }
9774
+ const session = {
9775
+ eyepopUrl,
9776
+ popId,
9777
+ accessToken,
9778
+ validUntil: Date.now() + 60 * 60 * 1e3
9673
9779
  };
9674
- return params;
9780
+ return session;
9781
+ };
9782
+ } else {
9783
+ authenticateBrowserSession = (auth0, eyepopUrl, popId) => {
9784
+ return Promise.reject("auth0 login not supported server-side");
9675
9785
  };
9676
9786
  }
9677
9787
 
9678
9788
  // src/endpoint.ts
9679
- var import_pino = require("pino");
9680
9789
  var Endpoint = class {
9681
9790
  constructor(options) {
9682
9791
  this._options = options;
@@ -9700,54 +9809,12 @@ var Endpoint = class {
9700
9809
  this._logger = rootLogger.child({ module: "eyepop" });
9701
9810
  this._requestLogger = this._logger.child({ module: "requests" });
9702
9811
  }
9703
- onStateChanged(handler) {
9704
- this._stateChangeHandler = handler;
9705
- return this;
9706
- }
9707
- async startIngressWs() {
9708
- if (this._ingressEventWs || !this._baseUrl || !this._client) {
9709
- return;
9812
+ eyepopUrl() {
9813
+ if (this._options.eyepopUrl) {
9814
+ return this._options.eyepopUrl.replace(/\/+$/, "");
9815
+ } else {
9816
+ return "https://api.eyepop.ai";
9710
9817
  }
9711
- try {
9712
- const getTokenUrl = `${this._baseUrl}/liveIngress/events/token`;
9713
- const headers = {
9714
- "Authorization": await this.authorizationHeader()
9715
- };
9716
- this._requestLogger.debug("before GET %s", getTokenUrl);
9717
- let response = await this._client.fetch(getTokenUrl, { headers });
9718
- if (response.status == 401) {
9719
- this._requestLogger.debug("after GET %s: 401, about to retry with fresh access token", getTokenUrl);
9720
- this._token = null;
9721
- const headers2 = {
9722
- "Authorization": await this.authorizationHeader()
9723
- };
9724
- this.updateState("FetchConfig" /* FetchConfig */);
9725
- response = await this._client.fetch(getTokenUrl, { headers: headers2 });
9726
- }
9727
- if (response.status != 200) {
9728
- const message = await response.text();
9729
- return Promise.reject(`Unexpected status ${response.status}: ${message}`);
9730
- }
9731
- const wsAuthToken = await response.json();
9732
- const wsUrl = this._baseUrl.startsWith("https://") ? `${this._baseUrl.replace("https://", "wss://")}/liveIngress/events/${wsAuthToken.token}` : `${this._baseUrl.replace("http://", "ws://")}/liveIngress/events/${wsAuthToken.token}`;
9733
- this._ingressEventWs = new WebSocket(wsUrl);
9734
- this._ingressEventWs.addEventListener("message", (event) => {
9735
- const ingressEvent = JSON.parse(event.data);
9736
- if (this._ingressEventHandler) {
9737
- this._ingressEventHandler(ingressEvent);
9738
- }
9739
- });
9740
- } finally {
9741
- this.updateState();
9742
- }
9743
- }
9744
- onIngressEvent(handler) {
9745
- this._ingressEventHandler = handler;
9746
- this.startIngressWs().then(() => {
9747
- }).catch((reason) => {
9748
- this._logger.warn("unexpected error starting the ingress event handler: %s", reason);
9749
- });
9750
- return this;
9751
9818
  }
9752
9819
  state() {
9753
9820
  return this._state;
@@ -9761,6 +9828,80 @@ var Endpoint = class {
9761
9828
  popId() {
9762
9829
  return this._options.popId || null;
9763
9830
  }
9831
+ onStateChanged(handler) {
9832
+ this._stateChangeHandler = handler;
9833
+ return this;
9834
+ }
9835
+ onIngressEvent(handler) {
9836
+ this._ingressEventHandler = handler;
9837
+ this.startIngressWs().then(() => {
9838
+ }).catch((reason) => {
9839
+ this._logger.warn("unexpected error starting the ingress event handler: %s", reason);
9840
+ });
9841
+ return this;
9842
+ }
9843
+ async connect() {
9844
+ if (this._client) {
9845
+ this._logger.warn("endpoint already connected");
9846
+ return this;
9847
+ }
9848
+ if (!this._options.eyepopUrl) {
9849
+ return Promise.reject("option eyepopUrl or environment variable EYEPOP_URL is required");
9850
+ }
9851
+ if (!this._options.popId) {
9852
+ return Promise.reject("option popId or environment variable EYEPOP_POP_ID is required");
9853
+ }
9854
+ if (this._options.auth === void 0) {
9855
+ return Promise.reject("cannot connect without defined auth option");
9856
+ }
9857
+ if (this._options.auth.session !== void 0) {
9858
+ this._token = this._options.auth.session.accessToken;
9859
+ this._expire_token_time = this._options.auth.session.validUntil / 1e3;
9860
+ } else if (this._options.auth.oAuth2 !== void 0) {
9861
+ } else if (this._options.auth.secretKey !== void 0) {
9862
+ } else {
9863
+ return Promise.reject("option secretKey or environment variable EYEPOP_SECRET_KEY is required");
9864
+ }
9865
+ this._client = await createHttpClient();
9866
+ let result = await this.reconnect();
9867
+ if (this._baseUrl && this._options.stopJobs) {
9868
+ const stop_url = `${this._baseUrl}/pipelines/${this._pipelineId}/source?mode=preempt&processing=sync`;
9869
+ const body = { "sourceType": "NONE" };
9870
+ const headers = {
9871
+ "Authorization": await this.authorizationHeader(),
9872
+ "Content-Type": "application/json"
9873
+ };
9874
+ this._requestLogger.debug("before PATCH %s", stop_url);
9875
+ let response = await this._client.fetch(stop_url, {
9876
+ method: "PATCH",
9877
+ headers,
9878
+ body: JSON.stringify(body)
9879
+ });
9880
+ if (response.status >= 300) {
9881
+ const message = await response.text();
9882
+ return Promise.reject(`Unexpected status ${response.status}: ${message}`);
9883
+ }
9884
+ this._requestLogger.debug("after PATCH %s", stop_url);
9885
+ }
9886
+ if (!this._ingressEventWs && this._ingressEventHandler) {
9887
+ await this.startIngressWs();
9888
+ }
9889
+ return result;
9890
+ }
9891
+ async disconnect(wait = true) {
9892
+ if (wait && this._limit) {
9893
+ for (let j = 0; j < this._options.jobQueueLength; j++) {
9894
+ await this._limit.acquire();
9895
+ }
9896
+ }
9897
+ this._limit = new Semaphore(this._options.jobQueueLength ?? 1024);
9898
+ if (this._client) {
9899
+ await this._client.close();
9900
+ this._client = null;
9901
+ }
9902
+ this._baseUrl = null;
9903
+ this._pipelineId = null;
9904
+ }
9764
9905
  async session() {
9765
9906
  await this.currentAccessToken();
9766
9907
  if (this._token == null || this._expire_token_time == null) {
@@ -9785,101 +9926,172 @@ var Endpoint = class {
9785
9926
  if (!this._baseUrl || !this._client) {
9786
9927
  return Promise.reject("endpoint not connected, use connect() before ingress()");
9787
9928
  }
9788
- const job = new Whip(stream, async () => {
9929
+ const whip = new WebrtcWhip(stream, async () => {
9789
9930
  return this.session();
9790
9931
  }, this._client, this._requestLogger);
9791
- return job.start();
9932
+ return whip.start();
9792
9933
  }
9793
- async upload(params) {
9794
- if (!this._baseUrl || !this._pipelineId || !this._client || !this._limit) {
9795
- return Promise.reject("endpoint not connected, use connect() before upload()");
9796
- }
9797
- params = await resolve(params);
9798
- if (!params.mimeType) {
9799
- return Promise.reject("upload for streams requires a mimeType");
9934
+ async liveEgress(ingressId) {
9935
+ if (!this._baseUrl || !this._client) {
9936
+ return Promise.reject("endpoint not connected, use connect() before ingress()");
9800
9937
  }
9801
- await this._limit.acquire();
9802
- this.updateState();
9803
- const job = new UploadJob(params.file, params.mimeType, async () => {
9938
+ const whep = new WebrtcWhep(ingressId, async () => {
9804
9939
  return this.session();
9805
9940
  }, this._client, this._requestLogger);
9806
- return job.start(() => {
9807
- this.jobDone(job);
9808
- }, (statusCode) => {
9809
- this.jobStatus(job, statusCode);
9810
- });
9941
+ return whep.start();
9942
+ }
9943
+ async process(source) {
9944
+ if (source.file !== void 0) {
9945
+ return this.uploadFile(source);
9946
+ } else if (source.stream !== void 0) {
9947
+ return this.uploadStream(source);
9948
+ } else if (source.path !== void 0) {
9949
+ return this.uploadPath(source);
9950
+ } else if (source.url !== void 0) {
9951
+ return this.loadFrom(source);
9952
+ } else if (source.ingressId !== void 0) {
9953
+ return this.loadLiveIngress(source);
9954
+ } else {
9955
+ return Promise.reject("unknown source type");
9956
+ }
9957
+ }
9958
+ async startIngressWs() {
9959
+ if (this._ingressEventWs || !this._baseUrl || !this._client) {
9960
+ return;
9961
+ }
9962
+ try {
9963
+ const getTokenUrl = `${this._baseUrl}/liveIngress/events/token`;
9964
+ const headers = {
9965
+ "Authorization": await this.authorizationHeader()
9966
+ };
9967
+ this._requestLogger.debug("before GET %s", getTokenUrl);
9968
+ let response = await this._client.fetch(getTokenUrl, { headers });
9969
+ if (response.status == 401) {
9970
+ this._requestLogger.debug("after GET %s: 401, about to retry with fresh access token", getTokenUrl);
9971
+ this._token = null;
9972
+ const headers2 = {
9973
+ "Authorization": await this.authorizationHeader()
9974
+ };
9975
+ this.updateState("FetchConfig" /* FetchConfig */);
9976
+ response = await this._client.fetch(getTokenUrl, { headers: headers2 });
9977
+ }
9978
+ if (response.status != 200) {
9979
+ const message = await response.text();
9980
+ return Promise.reject(`Unexpected status ${response.status}: ${message}`);
9981
+ }
9982
+ const wsAuthToken = await response.json();
9983
+ const wsUrl = this._baseUrl.startsWith("https://") ? `${this._baseUrl.replace("https://", "wss://")}/liveIngress/events/${wsAuthToken.token}` : `${this._baseUrl.replace("http://", "ws://")}/liveIngress/events/${wsAuthToken.token}`;
9984
+ this._ingressEventWs = new WebSocket(wsUrl);
9985
+ this._ingressEventWs.addEventListener("message", (event) => {
9986
+ const ingressEvent = JSON.parse(event.data);
9987
+ if (this._ingressEventHandler) {
9988
+ this._ingressEventHandler(ingressEvent);
9989
+ }
9990
+ });
9991
+ } finally {
9992
+ this.updateState();
9993
+ }
9811
9994
  }
9812
- async loadFrom(location) {
9995
+ async uploadFile(source) {
9813
9996
  if (!this._baseUrl || !this._pipelineId || !this._client || !this._limit) {
9814
- throw new Error("endpoint not connected, use connect() before loadFrom()");
9997
+ return Promise.reject("endpoint not connected, use connect() before upload()");
9815
9998
  }
9816
9999
  await this._limit.acquire();
9817
- this.updateState();
9818
- const job = new LoadFromJob(location, async () => {
9819
- return this.session();
9820
- }, this._client, this._requestLogger);
9821
- return job.start(() => {
9822
- this.jobDone(job);
9823
- }, (statusCode) => {
9824
- this.jobStatus(job, statusCode);
9825
- });
10000
+ try {
10001
+ this.updateState();
10002
+ const job = new UploadJob(source.file, source.file.type, async () => {
10003
+ return this.session();
10004
+ }, this._client, this._requestLogger);
10005
+ return job.start(() => {
10006
+ this.jobDone(job);
10007
+ }, (statusCode) => {
10008
+ this.jobStatus(job, statusCode);
10009
+ });
10010
+ } catch (e) {
10011
+ this._limit.release();
10012
+ throw e;
10013
+ }
9826
10014
  }
9827
- async loadLiveIngress(ingressId) {
10015
+ async uploadStream(source) {
9828
10016
  if (!this._baseUrl || !this._pipelineId || !this._client || !this._limit) {
9829
- throw new Error("endpoint not connected, use connect() before loadLiveIngress()");
10017
+ return Promise.reject("endpoint not connected, use connect() before upload()");
9830
10018
  }
9831
10019
  await this._limit.acquire();
9832
- this.updateState();
9833
- const job = new LoadLiveIngressJob(ingressId, async () => {
9834
- return this.session();
9835
- }, this._client, this._requestLogger);
9836
- return job.start(() => {
9837
- this.jobDone(job);
9838
- }, (statusCode) => {
9839
- this.jobStatus(job, statusCode);
9840
- });
10020
+ try {
10021
+ this.updateState();
10022
+ const job = new UploadJob(source.stream, source.mimeType, async () => {
10023
+ return this.session();
10024
+ }, this._client, this._requestLogger);
10025
+ return job.start(() => {
10026
+ this.jobDone(job);
10027
+ }, (statusCode) => {
10028
+ this.jobStatus(job, statusCode);
10029
+ });
10030
+ } catch (e) {
10031
+ this._limit.release();
10032
+ throw e;
10033
+ }
9841
10034
  }
9842
- // noinspection TypeScriptValidateTypes
9843
- async connect() {
9844
- if (this._client) {
9845
- console.info("endpoint already connected");
9846
- return this;
10035
+ async uploadPath(source) {
10036
+ if (!this._baseUrl || !this._pipelineId || !this._client || !this._limit) {
10037
+ throw new Error("endpoint not connected, use connect() before process()");
9847
10038
  }
9848
- if (!this._options.eyepopUrl) {
9849
- return Promise.reject("option eyepopUrl or environment variable EYEPOP_URL is required");
10039
+ await this._limit.acquire();
10040
+ try {
10041
+ this.updateState();
10042
+ const streamSource = await resolvePath(source);
10043
+ const job = new UploadJob(streamSource.stream, streamSource.mimeType, async () => {
10044
+ return this.session();
10045
+ }, this._client, this._requestLogger);
10046
+ return job.start(() => {
10047
+ this.jobDone(job);
10048
+ }, (statusCode) => {
10049
+ this.jobStatus(job, statusCode);
10050
+ });
10051
+ } catch (e) {
10052
+ this._limit.release();
10053
+ throw e;
9850
10054
  }
9851
- if (!this._options.popId) {
9852
- return Promise.reject("option popId or environment variable EYEPOP_POP_ID is required");
10055
+ }
10056
+ async loadFrom(source) {
10057
+ if (!this._baseUrl || !this._pipelineId || !this._client || !this._limit) {
10058
+ throw new Error("endpoint not connected, use connect() before loadFrom()");
9853
10059
  }
9854
- if (this._options.auth && this._options.auth.session) {
9855
- this._token = this._options.auth.session.accessToken;
9856
- this._expire_token_time = this._options.auth.session.validUntil / 1e3;
9857
- } else {
9858
- if (!this._options.auth || !this._options.auth.secretKey) {
9859
- return Promise.reject("option secretKey or environment variable EYEPOP_SECRET_KEY is required");
9860
- }
10060
+ await this._limit.acquire();
10061
+ try {
10062
+ this.updateState();
10063
+ const job = new LoadFromJob(source.url, async () => {
10064
+ return this.session();
10065
+ }, this._client, this._requestLogger);
10066
+ return job.start(() => {
10067
+ this.jobDone(job);
10068
+ }, (statusCode) => {
10069
+ this.jobStatus(job, statusCode);
10070
+ });
10071
+ } catch (e) {
10072
+ this._limit.release();
10073
+ throw e;
9861
10074
  }
9862
- this._client = await createHttpClient();
9863
- let result = await this.reconnect();
9864
- if (this._baseUrl && this._options.stopJobs) {
9865
- const stop_url = `${this._baseUrl}/pipelines/${this._pipelineId}/source?mode=preempt&processing=sync`;
9866
- const body = { "sourceType": "NONE" };
9867
- const headers = {
9868
- "Authorization": await this.authorizationHeader(),
9869
- "Content-Type": "application/json"
9870
- };
9871
- this._requestLogger.debug("before PATCH %s", stop_url);
9872
- let response = await this._client.fetch(stop_url, { method: "PATCH", headers, body: JSON.stringify(body) });
9873
- if (response.status >= 300) {
9874
- const message = await response.text();
9875
- return Promise.reject(`Unexpected status ${response.status}: ${message}`);
9876
- }
9877
- this._requestLogger.debug("after PATCH %s", stop_url);
10075
+ }
10076
+ async loadLiveIngress(source) {
10077
+ if (!this._baseUrl || !this._pipelineId || !this._client || !this._limit) {
10078
+ throw new Error("endpoint not connected, use connect() before loadLiveIngress()");
9878
10079
  }
9879
- if (!this._ingressEventWs && this._ingressEventHandler) {
9880
- await this.startIngressWs();
10080
+ await this._limit.acquire();
10081
+ try {
10082
+ this.updateState();
10083
+ const job = new LoadLiveIngressJob(source.ingressId, async () => {
10084
+ return this.session();
10085
+ }, this._client, this._requestLogger);
10086
+ return job.start(() => {
10087
+ this.jobDone(job);
10088
+ }, (statusCode) => {
10089
+ this.jobStatus(job, statusCode);
10090
+ });
10091
+ } catch (e) {
10092
+ this._limit.release();
10093
+ throw e;
9881
10094
  }
9882
- return result;
9883
10095
  }
9884
10096
  jobDone(job) {
9885
10097
  this._limit?.release();
@@ -9967,20 +10179,6 @@ var Endpoint = class {
9967
10179
  return Promise.resolve(this);
9968
10180
  }
9969
10181
  }
9970
- async disconnect(wait = true) {
9971
- if (wait && this._limit) {
9972
- for (let j = 0; j < this._options.jobQueueLength; j++) {
9973
- await this._limit.acquire();
9974
- }
9975
- }
9976
- this._limit = new Semaphore(this._options.jobQueueLength ?? 1024);
9977
- if (this._client) {
9978
- await this._client.close();
9979
- this._client = null;
9980
- }
9981
- this._baseUrl = null;
9982
- this._pipelineId = null;
9983
- }
9984
10182
  async authorizationHeader() {
9985
10183
  return `Bearer ${await this.currentAccessToken()}`;
9986
10184
  }
@@ -9988,41 +10186,49 @@ var Endpoint = class {
9988
10186
  const now = Date.now() / 1e3;
9989
10187
  if (!this._token || this._expire_token_time < now) {
9990
10188
  this.updateState("Authenticating" /* Authenticating */);
9991
- if (!this._client) {
9992
- return Promise.reject("endpoint not connected");
9993
- } else if (!this._options.auth || !this._options.auth.secretKey) {
9994
- return Promise.reject("temporary access token expired");
9995
- }
9996
- const body = { "secret_key": this._options.auth.secretKey };
9997
- const headers = { "Content-Type": "application/json" };
9998
- const post_url = `${this.eyepopUrl()}/authentication/token`;
9999
- this._requestLogger.debug("before POST %s", post_url);
10000
- const response = await this._client.fetch(post_url, {
10001
- method: "POST",
10002
- headers,
10003
- body: JSON.stringify(body)
10004
- });
10005
- if (response.status != 200) {
10006
- const message = await response.text();
10007
- return Promise.reject(`Unexpected status ${response.status}: ${message}`);
10189
+ try {
10190
+ if (!this._client) {
10191
+ return Promise.reject("endpoint not connected");
10192
+ } else if (this._options.auth.session !== void 0) {
10193
+ return Promise.reject("temporary access token expired");
10194
+ } else if (this._options.auth.oAuth2 !== void 0 && this._options.eyepopUrl && this._options.popId) {
10195
+ const session = await authenticateBrowserSession(
10196
+ this._options.auth.oAuth2,
10197
+ this._options.eyepopUrl,
10198
+ this._options.popId
10199
+ );
10200
+ this._token = session.accessToken;
10201
+ this._expire_token_time = session.validUntil / 1e3;
10202
+ } else if (this._options.auth.secretKey !== void 0 && this._options.eyepopUrl) {
10203
+ const secretKeyAuth = this._options.auth;
10204
+ const body = { "secret_key": secretKeyAuth.secretKey };
10205
+ const headers = { "Content-Type": "application/json" };
10206
+ const post_url = `${this.eyepopUrl()}/authentication/token`;
10207
+ this._requestLogger.debug("before POST %s", post_url);
10208
+ const response = await this._client.fetch(post_url, {
10209
+ method: "POST",
10210
+ headers,
10211
+ body: JSON.stringify(body)
10212
+ });
10213
+ if (response.status != 200) {
10214
+ const message = await response.text();
10215
+ return Promise.reject(`Unexpected status ${response.status}: ${message}`);
10216
+ }
10217
+ this._requestLogger.debug("after POST %s", post_url);
10218
+ const data = await response.json();
10219
+ const token = data;
10220
+ this._token = token.access_token;
10221
+ this._expire_token_time = now + token.expires_in - 60;
10222
+ } else {
10223
+ return Promise.reject("no valid auth option");
10224
+ }
10225
+ } finally {
10226
+ this.updateState();
10008
10227
  }
10009
- this._requestLogger.debug("after POST %s", post_url);
10010
- const data = await response.json();
10011
- const token = data;
10012
- this._token = token.access_token;
10013
- this._expire_token_time = now + token.expires_in - 60;
10014
- this.updateState();
10015
10228
  }
10016
10229
  this._logger.debug("using access token, valid for at least %d seconds", this._expire_token_time - now);
10017
10230
  return this._token;
10018
10231
  }
10019
- eyepopUrl() {
10020
- if (this._options.eyepopUrl) {
10021
- return this._options.eyepopUrl.replace(/\/+$/, "");
10022
- } else {
10023
- return "https://api.eyepop.ai";
10024
- }
10025
- }
10026
10232
  };
10027
10233
 
10028
10234
  // src/visualize.ts
@@ -10138,40 +10344,6 @@ _EyePopPlot.colors = {
10138
10344
  };
10139
10345
  var EyePopPlot = _EyePopPlot;
10140
10346
 
10141
- // src/shims/browser_session.ts
10142
- var authenticateBrowserSession;
10143
- if ("document" in globalThis && "implementation" in globalThis.document) {
10144
- authenticateBrowserSession = (oauthUrl, eyepopUrl, popId) => {
10145
- const queryString = window.location.search;
10146
- const urlParams = new URLSearchParams(queryString);
10147
- let token = urlParams.get("pop_token");
10148
- if (token) {
10149
- urlParams.delete("pop_token");
10150
- const url = `${window.location.pathname}?${urlParams}`;
10151
- window.history.replaceState({}, document.title, url);
10152
- return {
10153
- eyepopUrl,
10154
- popId,
10155
- accessToken: token,
10156
- validUntil: Date.now() + 60 * 60 * 1e3
10157
- };
10158
- } else {
10159
- const url = `${oauthUrl}?url=${encodeURIComponent(window.location.href)}`;
10160
- window.location.replace(url);
10161
- return {
10162
- eyepopUrl,
10163
- popId,
10164
- accessToken: "in-progress",
10165
- validUntil: 0
10166
- };
10167
- }
10168
- };
10169
- } else {
10170
- authenticateBrowserSession = (oauthUrl, eyepopUrl, popId) => {
10171
- return null;
10172
- };
10173
- }
10174
-
10175
10347
  // src/index.ts
10176
10348
  var readEnv = (env) => {
10177
10349
  if (typeof process !== "undefined") {
@@ -10179,7 +10351,7 @@ var readEnv = (env) => {
10179
10351
  }
10180
10352
  return void 0;
10181
10353
  };
10182
- var EyePopSdk = class {
10354
+ var _EyePopSdk = class _EyePopSdk {
10183
10355
  static endpoint({
10184
10356
  auth = this.defaultAuth,
10185
10357
  popId = readEnv("EYEPOP_POP_ID"),
@@ -10190,17 +10362,30 @@ var EyePopSdk = class {
10190
10362
  logger,
10191
10363
  ...opts
10192
10364
  } = {}) {
10193
- if (auth.oAuth && popId) {
10194
- let oauthUrl = "https://dashboard.eyepop.ai/sdkauth";
10195
- if (typeof auth.oAuth == "string") {
10196
- oauthUrl = auth.oAuth;
10365
+ if (auth.oAuth2 !== void 0 && popId) {
10366
+ if (typeof auth.oAuth2 === "boolean") {
10367
+ auth = {
10368
+ oAuth2: {
10369
+ domain: "eyepop.us.auth0.com",
10370
+ clientId: "Lb9ubA9Hf3jlaqWLUx8XgA0zvotgViCl",
10371
+ audience: "https://api.eyepop.ai",
10372
+ scope: "admin:clouds"
10373
+ }
10374
+ };
10375
+ }
10376
+ }
10377
+ if (auth.session !== void 0) {
10378
+ if (auth.session.popId) {
10379
+ popId = auth.session.popId;
10380
+ }
10381
+ if (auth.session.eyepopUrl) {
10382
+ eyepopUrl = auth.session.eyepopUrl;
10197
10383
  }
10198
- auth.session = authenticateBrowserSession(oauthUrl, eyepopUrl ?? "https://api.eyepop.ai", popId) ?? void 0;
10199
10384
  }
10200
10385
  const options = {
10201
10386
  auth,
10202
- popId: auth.session ? auth.session.popId : popId,
10203
- eyepopUrl: auth.session ? auth.session.eyepopUrl : eyepopUrl ?? "https://api.eyepop.ai",
10387
+ popId,
10388
+ eyepopUrl: eyepopUrl ?? "https://api.eyepop.ai",
10204
10389
  autoStart,
10205
10390
  stopJobs,
10206
10391
  jobQueueLength,
@@ -10213,9 +10398,11 @@ var EyePopSdk = class {
10213
10398
  return new EyePopPlot(context);
10214
10399
  }
10215
10400
  };
10216
- EyePopSdk.defaultAuth = {
10217
- secretKey: readEnv("EYEPOP_SECRET_KEY")
10218
- };
10401
+ _EyePopSdk.envSecretKey = readEnv("EYEPOP_SECRET_KEY");
10402
+ _EyePopSdk.defaultAuth = _EyePopSdk.envSecretKey ? {
10403
+ secretKey: _EyePopSdk.envSecretKey
10404
+ } : void 0;
10405
+ var EyePopSdk = _EyePopSdk;
10219
10406
  var src_default = EyePopSdk;
10220
10407
  // Annotate the CommonJS export names for ESM import in node:
10221
10408
  0 && (module.exports = {