@roboflow/inference-sdk 0.1.11 → 0.1.12

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.es.js CHANGED
@@ -1,8 +1,8 @@
1
- var K = Object.defineProperty;
2
- var B = (r, e, t) => e in r ? K(r, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[e] = t;
3
- var l = (r, e, t) => B(r, typeof e != "symbol" ? e + "" : e, t);
4
- var L;
5
- const j = typeof process < "u" && ((L = process.env) != null && L.RF_API_BASE_URL) ? process.env.RF_API_BASE_URL : "https://api.roboflow.com", V = [
1
+ var q = Object.defineProperty;
2
+ var K = (r, e, t) => e in r ? q(r, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[e] = t;
3
+ var l = (r, e, t) => K(r, typeof e != "symbol" ? e + "" : e, t);
4
+ var I;
5
+ const j = typeof process < "u" && ((I = process.env) != null && I.RF_API_BASE_URL) ? process.env.RF_API_BASE_URL : "https://api.roboflow.com", V = [
6
6
  "https://serverless.roboflow.com"
7
7
  ];
8
8
  class b {
@@ -10,15 +10,16 @@ class b {
10
10
  * @private
11
11
  * Use InferenceHTTPClient.init() instead
12
12
  */
13
- constructor(e, t = "https://serverless.roboflow.com") {
13
+ constructor(e, t = "https://serverless.roboflow.com", n = j) {
14
14
  l(this, "apiKey");
15
15
  l(this, "serverUrl");
16
- this.apiKey = e, this.serverUrl = t;
16
+ l(this, "apiBaseUrl");
17
+ this.apiKey = e, this.serverUrl = t, this.apiBaseUrl = n;
17
18
  }
18
- static init({ apiKey: e, serverUrl: t }) {
19
+ static init({ apiKey: e, serverUrl: t, apiBaseUrl: n }) {
19
20
  if (!e)
20
21
  throw new Error("apiKey is required");
21
- return new b(e, t);
22
+ return new b(e, t, n);
22
23
  }
23
24
  /**
24
25
  * Initialize a WebRTC worker pipeline
@@ -49,15 +50,15 @@ class b {
49
50
  offer: e,
50
51
  workflowSpec: t,
51
52
  workspaceName: n,
52
- workflowId: i,
53
- config: a = {}
53
+ workflowId: a,
54
+ config: o = {}
54
55
  }) {
55
56
  if (!e || !e.sdp || !e.type)
56
57
  throw new Error("offer with sdp and type is required");
57
- const o = !!t, f = !!(n && i);
58
- if (!o && !f)
58
+ const i = !!t, f = !!(n && a);
59
+ if (!i && !f)
59
60
  throw new Error("Either workflowSpec OR (workspaceName + workflowId) is required");
60
- if (o && f)
61
+ if (i && f)
61
62
  throw new Error("Provide either workflowSpec OR (workspaceName + workflowId), not both");
62
63
  const {
63
64
  imageInputName: d = "image",
@@ -71,7 +72,7 @@ class b {
71
72
  requestedRegion: C,
72
73
  realtimeProcessing: E = !0,
73
74
  rtspUrl: R
74
- } = a, v = {
75
+ } = o, v = {
75
76
  type: "WorkflowConfiguration",
76
77
  image_input_name: d,
77
78
  workflows_parameters: S,
@@ -79,7 +80,7 @@ class b {
79
80
  cancel_thread_pool_tasks_on_exit: !0,
80
81
  video_metadata_input_name: "video_metadata"
81
82
  };
82
- o ? v.workflow_specification = t : (v.workspace_name = n, v.workflow_id = i);
83
+ i ? v.workflow_specification = t : (v.workspace_name = n, v.workflow_id = a);
83
84
  const m = {
84
85
  workflow_configuration: v,
85
86
  api_key: this.apiKey,
@@ -136,7 +137,7 @@ class b {
136
137
  return null;
137
138
  try {
138
139
  const e = await fetch(
139
- `${j}/webrtc_turn_config?api_key=${this.apiKey}`,
140
+ `${this.apiBaseUrl}/webrtc_turn_config?api_key=${this.apiKey}`,
140
141
  {
141
142
  method: "GET",
142
143
  headers: { "Content-Type": "application/json" }
@@ -154,10 +155,10 @@ class b {
154
155
  n = [t];
155
156
  else
156
157
  return console.warn("[RFWebRTC] Invalid TURN config format, using defaults"), null;
157
- return n.map((a) => ({
158
- urls: Array.isArray(a.urls) ? a.urls : [a.urls],
159
- username: a.username,
160
- credential: a.credential
158
+ return n.map((o) => ({
159
+ urls: Array.isArray(o.urls) ? o.urls : [o.urls],
160
+ username: o.username,
161
+ credential: o.credential
161
162
  }));
162
163
  } catch (e) {
163
164
  return console.warn("[RFWebRTC] Error fetching TURN config:", e), null;
@@ -184,35 +185,35 @@ const ie = {
184
185
  * ```
185
186
  */
186
187
  withApiKey(r, e = {}) {
187
- const { serverUrl: t } = e;
188
+ const { serverUrl: t, apiBaseUrl: n } = e;
188
189
  typeof window < "u" && console.warn(
189
190
  "[Security Warning] Using API key directly in browser will expose it. Use connectors.withProxyUrl() for production. See: https://docs.roboflow.com/api-reference/authentication#securing-your-api-key"
190
191
  );
191
- const n = b.init({ apiKey: r, serverUrl: t });
192
+ const a = b.init({ apiKey: r, serverUrl: t, apiBaseUrl: n });
192
193
  return {
193
- connectWrtc: async (i, a) => (console.debug("wrtcParams", a), await n.initializeWebrtcWorker({
194
- offer: i,
195
- workflowSpec: a.workflowSpec,
196
- workspaceName: a.workspaceName,
197
- workflowId: a.workflowId,
194
+ connectWrtc: async (o, i) => (console.debug("wrtcParams", i), await a.initializeWebrtcWorker({
195
+ offer: o,
196
+ workflowSpec: i.workflowSpec,
197
+ workspaceName: i.workspaceName,
198
+ workflowId: i.workflowId,
198
199
  config: {
199
- imageInputName: a.imageInputName,
200
- streamOutputNames: a.streamOutputNames,
201
- dataOutputNames: a.dataOutputNames,
202
- threadPoolWorkers: a.threadPoolWorkers,
203
- workflowsParameters: a.workflowsParameters,
204
- iceServers: a.iceServers,
205
- processingTimeout: a.processingTimeout,
206
- requestedPlan: a.requestedPlan,
207
- requestedRegion: a.requestedRegion,
208
- realtimeProcessing: a.realtimeProcessing,
209
- rtspUrl: a.rtspUrl
200
+ imageInputName: i.imageInputName,
201
+ streamOutputNames: i.streamOutputNames,
202
+ dataOutputNames: i.dataOutputNames,
203
+ threadPoolWorkers: i.threadPoolWorkers,
204
+ workflowsParameters: i.workflowsParameters,
205
+ iceServers: i.iceServers,
206
+ processingTimeout: i.processingTimeout,
207
+ requestedPlan: i.requestedPlan,
208
+ requestedRegion: i.requestedRegion,
209
+ realtimeProcessing: i.realtimeProcessing,
210
+ rtspUrl: i.rtspUrl
210
211
  }
211
212
  })),
212
213
  /**
213
214
  * Fetch TURN server configuration for improved WebRTC connectivity
214
215
  */
215
- getIceServers: async () => await n.fetchTurnConfig(),
216
+ getIceServers: async () => await a.fetchTurnConfig(),
216
217
  // Store apiKey for cleanup
217
218
  _apiKey: r,
218
219
  _serverUrl: t
@@ -284,20 +285,20 @@ const ie = {
284
285
  withProxyUrl(r, e = {}) {
285
286
  const { turnConfigUrl: t } = e;
286
287
  return {
287
- connectWrtc: async (n, i) => {
288
- const a = await fetch(r, {
288
+ connectWrtc: async (n, a) => {
289
+ const o = await fetch(r, {
289
290
  method: "POST",
290
291
  headers: { "Content-Type": "application/json" },
291
292
  body: JSON.stringify({
292
293
  offer: n,
293
- wrtcParams: i
294
+ wrtcParams: a
294
295
  })
295
296
  });
296
- if (!a.ok) {
297
- const o = await a.text().catch(() => "");
298
- throw new Error(`Proxy request failed (${a.status}): ${o}`);
297
+ if (!o.ok) {
298
+ const i = await o.text().catch(() => "");
299
+ throw new Error(`Proxy request failed (${o.status}): ${i}`);
299
300
  }
300
- return await a.json();
301
+ return await o.json();
301
302
  },
302
303
  /**
303
304
  * Fetch TURN server configuration from the proxy backend
@@ -339,7 +340,7 @@ const oe = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
339
340
  function H(r) {
340
341
  return new Promise((e) => setTimeout(e, r));
341
342
  }
342
- class N {
343
+ class D {
343
344
  constructor(e, t) {
344
345
  l(this, "file");
345
346
  l(this, "channel");
@@ -365,37 +366,37 @@ class N {
365
366
  throw new Error("Upload cancelled");
366
367
  if (this.channel.readyState !== "open")
367
368
  throw new Error("Video upload interrupted");
368
- const i = n * k, a = Math.min(i + k, t), o = this.file.slice(i, a), f = new Uint8Array(await o.arrayBuffer()), d = new ArrayBuffer(8 + f.length), c = new DataView(d);
369
+ const a = n * k, o = Math.min(a + k, t), i = this.file.slice(a, o), f = new Uint8Array(await i.arrayBuffer()), d = new ArrayBuffer(8 + f.length), c = new DataView(d);
369
370
  for (c.setUint32(0, n, !0), c.setUint32(4, this.totalChunks, !0), new Uint8Array(d, 8).set(f); this.channel.bufferedAmount > M; ) {
370
371
  if (this.channel.readyState !== "open")
371
372
  throw new Error("Video upload interrupted");
372
373
  await H(z);
373
374
  }
374
- this.channel.send(d), e && e(a, t);
375
+ this.channel.send(d), e && e(o, t);
375
376
  }
376
377
  }
377
378
  }
378
379
  const J = 12;
379
- class D {
380
+ class N {
380
381
  constructor() {
381
382
  l(this, "pendingFrames", /* @__PURE__ */ new Map());
382
383
  }
383
384
  /**
384
385
  * Process an incoming chunk and return the complete message if all chunks received
385
386
  */
386
- processChunk(e, t, n, i) {
387
+ processChunk(e, t, n, a) {
387
388
  if (n === 1)
388
- return i;
389
+ return a;
389
390
  this.pendingFrames.has(e) || this.pendingFrames.set(e, {
390
391
  chunks: /* @__PURE__ */ new Map(),
391
392
  totalChunks: n
392
393
  });
393
- const a = this.pendingFrames.get(e);
394
- if (a.chunks.set(t, i), a.chunks.size === n) {
395
- const o = Array.from(a.chunks.values()).reduce((c, s) => c + s.length, 0), f = new Uint8Array(o);
394
+ const o = this.pendingFrames.get(e);
395
+ if (o.chunks.set(t, a), o.chunks.size === n) {
396
+ const i = Array.from(o.chunks.values()).reduce((c, s) => c + s.length, 0), f = new Uint8Array(i);
396
397
  let d = 0;
397
398
  for (let c = 0; c < n; c++) {
398
- const s = a.chunks.get(c);
399
+ const s = o.chunks.get(c);
399
400
  f.set(s, d), d += s.length;
400
401
  }
401
402
  return this.pendingFrames.delete(e), f;
@@ -410,27 +411,27 @@ class D {
410
411
  }
411
412
  }
412
413
  function A(r) {
413
- const e = new DataView(r), t = e.getUint32(0, !0), n = e.getUint32(4, !0), i = e.getUint32(8, !0), a = new Uint8Array(r, J);
414
- return { frameId: t, chunkIndex: n, totalChunks: i, payload: a };
414
+ const e = new DataView(r), t = e.getUint32(0, !0), n = e.getUint32(4, !0), a = e.getUint32(8, !0), o = new Uint8Array(r, J);
415
+ return { frameId: t, chunkIndex: n, totalChunks: a, payload: o };
415
416
  }
416
417
  async function G(r, e = 6e3) {
417
418
  if (r.iceGatheringState === "complete") return;
418
419
  let t = !1;
419
- const n = (i) => {
420
- i.candidate && i.candidate.type === "srflx" && (t = !0);
420
+ const n = (a) => {
421
+ a.candidate && a.candidate.type === "srflx" && (t = !0);
421
422
  };
422
423
  r.addEventListener("icecandidate", n);
423
424
  try {
424
425
  await Promise.race([
425
- new Promise((i) => {
426
- const a = () => {
427
- r.iceGatheringState === "complete" && (r.removeEventListener("icegatheringstatechange", a), i());
426
+ new Promise((a) => {
427
+ const o = () => {
428
+ r.iceGatheringState === "complete" && (r.removeEventListener("icegatheringstatechange", o), a());
428
429
  };
429
- r.addEventListener("icegatheringstatechange", a);
430
+ r.addEventListener("icegatheringstatechange", o);
430
431
  }),
431
- new Promise((i, a) => {
432
+ new Promise((a, o) => {
432
433
  setTimeout(() => {
433
- t ? i() : (console.error("[ICE] timeout with NO srflx candidate! Connection may fail."), a(new Error("ICE gathering timeout without srflx candidate")));
434
+ t ? a() : (console.error("[ICE] timeout with NO srflx candidate! Connection may fail."), o(new Error("ICE gathering timeout without srflx candidate")));
434
435
  }, e);
435
436
  })
436
437
  ]);
@@ -448,13 +449,13 @@ function Z(r) {
448
449
  const Q = [
449
450
  { urls: ["stun:stun.l.google.com:19302"] }
450
451
  ];
451
- async function X(r, e, t, n, i) {
452
+ async function X(r, e, t, n, a) {
452
453
  if ([!!r, !!e, !!n].filter(Boolean).length !== 1)
453
454
  throw new Error("Exactly one of localStream, file, or rtspUrl must be provided");
454
455
  const c = t ?? Q, s = new RTCPeerConnection({
455
456
  iceServers: c
456
457
  });
457
- i != null && i.onPeerConnectionCreated && await i.onPeerConnectionCreated(s);
458
+ a != null && a.onPeerConnectionCreated && await a.onPeerConnectionCreated(s);
458
459
  try {
459
460
  s.addTransceiver("video", { direction: "recvonly" });
460
461
  } catch (h) {
@@ -463,7 +464,7 @@ async function X(r, e, t, n, i) {
463
464
  if (r)
464
465
  for (const h of r.getVideoTracks()) {
465
466
  const C = s.addTrack(h, r);
466
- i != null && i.onTrackAdded && await i.onTrackAdded(h, C, s);
467
+ a != null && a.onTrackAdded && await a.onTrackAdded(h, C, s);
467
468
  }
468
469
  const y = Z(s), S = s.createDataChannel("inference", {
469
470
  ordered: !0
@@ -471,8 +472,8 @@ async function X(r, e, t, n, i) {
471
472
  let p;
472
473
  e && (p = s.createDataChannel("video_upload"));
473
474
  let u = await s.createOffer();
474
- if (i != null && i.onOfferCreated) {
475
- const h = await i.onOfferCreated(u);
475
+ if (a != null && a.onOfferCreated) {
476
+ const h = await a.onOfferCreated(u);
476
477
  h && (u = h);
477
478
  }
478
479
  return await s.setLocalDescription(u), await G(s), {
@@ -500,19 +501,19 @@ function ee(r, e = 3e4) {
500
501
  t();
501
502
  return;
502
503
  }
503
- const i = () => {
504
- r.removeEventListener("open", i), r.removeEventListener("error", a), clearTimeout(o), t();
505
- }, a = () => {
506
- r.removeEventListener("open", i), r.removeEventListener("error", a), clearTimeout(o), n(new Error("Datachannel error"));
507
- }, o = setTimeout(() => {
508
- r.removeEventListener("open", i), r.removeEventListener("error", a), n(new Error("Datachannel open timeout"));
504
+ const a = () => {
505
+ r.removeEventListener("open", a), r.removeEventListener("error", o), clearTimeout(i), t();
506
+ }, o = () => {
507
+ r.removeEventListener("open", a), r.removeEventListener("error", o), clearTimeout(i), n(new Error("Datachannel error"));
508
+ }, i = setTimeout(() => {
509
+ r.removeEventListener("open", a), r.removeEventListener("error", o), n(new Error("Datachannel open timeout"));
509
510
  }, e);
510
- r.addEventListener("open", i), r.addEventListener("error", a);
511
+ r.addEventListener("open", a), r.addEventListener("error", o);
511
512
  });
512
513
  }
513
514
  class x {
514
515
  /** @private */
515
- constructor(e, t, n, i, a, o) {
516
+ constructor(e, t, n, a, o, i) {
516
517
  /**
517
518
  * The underlying RTCPeerConnection.
518
519
  * Exposed for advanced use cases like getting stats or accessing senders.
@@ -536,8 +537,8 @@ class x {
536
537
  l(this, "uploadChannel");
537
538
  l(this, "uploader");
538
539
  l(this, "onComplete");
539
- this.peerConnection = e, this._localStream = o == null ? void 0 : o.localStream, this.remoteStreamPromise = t, this.pipelineId = n, this.apiKey = i, this.dataChannel = a, this.reassembler = new D(), this.ackPacingEnabled = (o == null ? void 0 : o.ackPacingEnabled) === !0, this.uploadChannel = o == null ? void 0 : o.uploadChannel, this.onComplete = o == null ? void 0 : o.onComplete, this.dataChannel.binaryType = "arraybuffer";
540
- const f = o == null ? void 0 : o.onData;
540
+ this.peerConnection = e, this._localStream = i == null ? void 0 : i.localStream, this.remoteStreamPromise = t, this.pipelineId = n, this.apiKey = a, this.dataChannel = o, this.reassembler = new N(), this.ackPacingEnabled = (i == null ? void 0 : i.ackPacingEnabled) === !0, this.uploadChannel = i == null ? void 0 : i.uploadChannel, this.onComplete = i == null ? void 0 : i.onComplete, this.dataChannel.binaryType = "arraybuffer";
541
+ const f = i == null ? void 0 : i.onData;
541
542
  f && (this.dataChannel.addEventListener("message", (d) => {
542
543
  try {
543
544
  if (d.data instanceof ArrayBuffer) {
@@ -642,7 +643,7 @@ class x {
642
643
  async startUpload(e, t) {
643
644
  if (!this.uploadChannel)
644
645
  throw new Error("No upload channel available. This connection was not created for file uploads.");
645
- await ee(this.uploadChannel), this.uploader = new N(e, this.uploadChannel), await this.uploader.upload(t);
646
+ await ee(this.uploadChannel), this.uploader = new D(e, this.uploadChannel), await this.uploader.upload(t);
646
647
  }
647
648
  /**
648
649
  * Cancel any ongoing file upload
@@ -708,9 +709,9 @@ async function F({
708
709
  rtspUrl: e,
709
710
  connector: t,
710
711
  wrtcParams: n,
711
- onData: i,
712
- onComplete: a,
713
- onFileUploadProgress: o,
712
+ onData: a,
713
+ onComplete: o,
714
+ onFileUploadProgress: i,
714
715
  options: f = {},
715
716
  hooks: d
716
717
  }) {
@@ -744,15 +745,15 @@ async function F({
744
745
  if (!(w != null && w.sdp) || !(w != null && w.type))
745
746
  throw console.error("[RFWebRTC] Invalid answer from server:", m), new Error("connector.connectWrtc must return answer with sdp and type");
746
747
  const U = ((W = m == null ? void 0 : m.context) == null ? void 0 : W.pipeline_id) || null;
747
- await u.setRemoteDescription(w), await new Promise((g, I) => {
748
+ await u.setRemoteDescription(w), await new Promise((g, L) => {
748
749
  const _ = () => {
749
- u.connectionState === "connected" ? (u.removeEventListener("connectionstatechange", _), g()) : u.connectionState === "failed" && (u.removeEventListener("connectionstatechange", _), I(new Error("WebRTC connection failed")));
750
+ u.connectionState === "connected" ? (u.removeEventListener("connectionstatechange", _), g()) : u.connectionState === "failed" && (u.removeEventListener("connectionstatechange", _), L(new Error("WebRTC connection failed")));
750
751
  };
751
752
  u.addEventListener("connectionstatechange", _), _(), setTimeout(() => {
752
- u.removeEventListener("connectionstatechange", _), I(new Error("WebRTC connection timeout after 30s"));
753
+ u.removeEventListener("connectionstatechange", _), L(new Error("WebRTC connection timeout after 30s"));
753
754
  }, 3e4);
754
755
  }), y && f.disableInputStreamDownscaling !== !1 && await Y(u);
755
- const T = t._apiKey || null, q = v.realtimeProcessing === !1, P = new x(
756
+ const T = t._apiKey || null, B = v.realtimeProcessing === !1, P = new x(
756
757
  u,
757
758
  C,
758
759
  U,
@@ -761,12 +762,12 @@ async function F({
761
762
  {
762
763
  localStream: y,
763
764
  uploadChannel: R,
764
- onData: i,
765
- onComplete: a,
766
- ackPacingEnabled: q
765
+ onData: a,
766
+ onComplete: o,
767
+ ackPacingEnabled: B
767
768
  }
768
769
  );
769
- return S && R && P.startUpload(S, o).catch((g) => {
770
+ return S && R && P.startUpload(S, i).catch((g) => {
770
771
  console.error("[RFWebRTC] Upload error:", g);
771
772
  }), P;
772
773
  }
@@ -775,8 +776,8 @@ async function te({
775
776
  connector: e,
776
777
  wrtcParams: t,
777
778
  onData: n,
778
- options: i = {},
779
- hooks: a
779
+ options: a = {},
780
+ hooks: o
780
781
  }) {
781
782
  if (r instanceof File)
782
783
  throw new Error("useStream requires a MediaStream. Use useVideoFile for File uploads.");
@@ -785,8 +786,8 @@ async function te({
785
786
  connector: e,
786
787
  wrtcParams: t,
787
788
  onData: n,
788
- options: i,
789
- hooks: a
789
+ options: a,
790
+ hooks: o
790
791
  });
791
792
  }
792
793
  async function re({
@@ -794,9 +795,9 @@ async function re({
794
795
  connector: e,
795
796
  wrtcParams: t,
796
797
  onData: n,
797
- onUploadProgress: i,
798
- onComplete: a,
799
- hooks: o
798
+ onUploadProgress: a,
799
+ onComplete: o,
800
+ hooks: i
800
801
  }) {
801
802
  return F({
802
803
  source: r,
@@ -806,9 +807,9 @@ async function re({
806
807
  realtimeProcessing: t.realtimeProcessing ?? !0
807
808
  },
808
809
  onData: n,
809
- onComplete: a,
810
- onFileUploadProgress: i,
811
- hooks: o
810
+ onComplete: o,
811
+ onFileUploadProgress: a,
812
+ hooks: i
812
813
  });
813
814
  }
814
815
  async function ne({
@@ -816,7 +817,7 @@ async function ne({
816
817
  connector: e,
817
818
  wrtcParams: t,
818
819
  onData: n,
819
- hooks: i
820
+ hooks: a
820
821
  }) {
821
822
  if (!r.startsWith("rtsp://") && !r.startsWith("rtsps://"))
822
823
  throw new Error("Invalid RTSP URL: must start with rtsp:// or rtsps://");
@@ -825,13 +826,13 @@ async function ne({
825
826
  connector: e,
826
827
  wrtcParams: t,
827
828
  onData: n,
828
- hooks: i
829
+ hooks: a
829
830
  });
830
831
  }
831
832
  const se = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
832
833
  __proto__: null,
833
- ChunkReassembler: D,
834
- FileUploader: N,
834
+ ChunkReassembler: N,
835
+ FileUploader: D,
835
836
  RFWebRTCConnection: x,
836
837
  parseBinaryHeader: A,
837
838
  useRtspStream: ne,