@roboflow/inference-sdk 0.1.11 → 0.1.13

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