@mediaviz/sdk 1.8.0-dev.92 → 1.8.0-dev.96

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/sdk.umd.js CHANGED
@@ -465,6 +465,448 @@
465
465
  return types;
466
466
  }
467
467
 
468
+ var src$1;
469
+ var hasRequiredSrc$1;
470
+
471
+ function requireSrc$1 () {
472
+ if (hasRequiredSrc$1) return src$1;
473
+ hasRequiredSrc$1 = 1;
474
+
475
+ const { OAuthClient } = requireClient();
476
+ const { OAuthError } = requireErrors();
477
+ const { OAuthErrorCode } = requireTypes();
478
+
479
+ src$1 = { OAuthClient, OAuthError, OAuthErrorCode };
480
+ return src$1;
481
+ }
482
+
483
+ var srcExports$1 = requireSrc$1();
484
+ var _oauth = /*@__PURE__*/getDefaultExportFromCjs(srcExports$1);
485
+
486
+ // Auto-generated — do not edit
487
+ const { OAuthClient, OAuthError, OAuthErrorCode } = _oauth;
488
+
489
+ var signing;
490
+ var hasRequiredSigning;
491
+
492
+ function requireSigning () {
493
+ if (hasRequiredSigning) return signing;
494
+ hasRequiredSigning = 1;
495
+
496
+ // HMAC scheme shared with the MediaViz producer: X-Signature = 'sha256=' +
497
+ // hex(HMAC-SHA256(secret, `${X-Timestamp}.` + rawBody)). The signing string is
498
+ // the timestamp joined to the *raw request body bytes* — never re-serialized
499
+ // JSON. Verification accepts the current and (during rotation) previous
500
+ // secret, rejects timestamps outside the skew window, and compares digests in
501
+ // constant time. Web Crypto only, so the same code runs in Node >= 18 and
502
+ // browsers.
503
+
504
+ const _encoder = new TextEncoder();
505
+
506
+ async function signWebhookPayload(secret, timestamp, rawBody) {
507
+ const key = await globalThis.crypto.subtle.importKey(
508
+ 'raw', _encoder.encode(secret), { name: 'HMAC', hash: 'SHA-256' }, false, ['sign']);
509
+ const payload = _concat(_encoder.encode(`${timestamp}.`), toBytes(rawBody));
510
+ const digest = await globalThis.crypto.subtle.sign('HMAC', key, payload);
511
+ return 'sha256=' + _hex(new Uint8Array(digest));
512
+ }
513
+
514
+ async function verifyWebhookSignature(secretCurrent, secretPrevious, headers, rawBody, options = {}) {
515
+ const h = lowerHeaders(headers);
516
+ const timestamp = h['x-timestamp'];
517
+ const signature = h['x-signature'];
518
+ if (!timestamp || !signature) return false;
519
+ if (!/^-?\d+$/.test(String(timestamp))) return false;
520
+ const skewToleranceS = options.skewToleranceS ?? 300;
521
+ const now = options.now ?? Math.floor(Date.now() / 1000);
522
+ if (Math.abs(now - Number(timestamp)) > skewToleranceS) return false;
523
+ for (const secret of [secretCurrent, secretPrevious]) {
524
+ if (!secret) continue;
525
+ const expected = await signWebhookPayload(secret, String(timestamp), rawBody);
526
+ if (_constantTimeEqual(expected, String(signature))) return true;
527
+ }
528
+ return false;
529
+ }
530
+
531
+ // helpers
532
+ function toBytes(rawBody) {
533
+ if (typeof rawBody === 'string') return _encoder.encode(rawBody);
534
+ if (rawBody instanceof Uint8Array) return rawBody;
535
+ if (rawBody instanceof ArrayBuffer) return new Uint8Array(rawBody);
536
+ throw new TypeError('rawBody must be a string, Uint8Array, or ArrayBuffer');
537
+ }
538
+
539
+ function lowerHeaders(headers) {
540
+ const out = {};
541
+ const entries = typeof headers?.entries === 'function' ? headers.entries() : Object.entries(headers ?? {});
542
+ for (const [k, v] of entries) out[String(k).toLowerCase()] = v;
543
+ return out;
544
+ }
545
+
546
+ function _concat(a, b) {
547
+ const out = new Uint8Array(a.length + b.length);
548
+ out.set(a, 0);
549
+ out.set(b, a.length);
550
+ return out;
551
+ }
552
+
553
+ function _hex(bytes) {
554
+ return Array.from(bytes, (b) => b.toString(16).padStart(2, '0')).join('');
555
+ }
556
+
557
+ function _constantTimeEqual(a, b) {
558
+ if (a.length !== b.length) return false;
559
+ let diff = 0;
560
+ for (let i = 0; i < a.length; i++) diff |= a.charCodeAt(i) ^ b.charCodeAt(i);
561
+ return diff === 0;
562
+ }
563
+
564
+ signing = { signWebhookPayload, verifyWebhookSignature, toBytes, lowerHeaders };
565
+ return signing;
566
+ }
567
+
568
+ var store;
569
+ var hasRequiredStore;
570
+
571
+ function requireStore () {
572
+ if (hasRequiredStore) return store;
573
+ hasRequiredStore = 1;
574
+
575
+ // Persistence seam for the webhook consumer: dedupe ledger, signing secrets,
576
+ // and pull cursors. The default in-memory store suits a single long-lived
577
+ // process (worker, dev server); multi-process consumers supply an object with
578
+ // the same shape backed by Redis or a database.
579
+
580
+ const MAX_SEEN_ENTRIES = 100000;
581
+
582
+ class InMemoryWebhookStore {
583
+ constructor() {
584
+ this._seen = new Map(); // eventId -> expiresAt (ms)
585
+ this._secrets = new Map(); // subscriptionId -> {current, previous, previousExpiresAt}
586
+ this._cursors = new Map(); // subscriptionId -> cursor
587
+ }
588
+
589
+ markSeen(eventId, ttlS) {
590
+ const now = Date.now();
591
+ const expiresAt = this._seen.get(eventId);
592
+ if (expiresAt !== undefined && expiresAt > now) return false;
593
+ this._seen.delete(eventId);
594
+ this._seen.set(eventId, now + ttlS * 1000);
595
+ this._prune(now);
596
+ return true;
597
+ }
598
+
599
+ unmarkSeen(eventId) {
600
+ this._seen.delete(eventId);
601
+ }
602
+
603
+ saveSecret(subscriptionId, secret) {
604
+ this._secrets.set(subscriptionId, { current: secret, previous: null, previousExpiresAt: 0 });
605
+ }
606
+
607
+ rotateSecret(subscriptionId, newSecret, windowS) {
608
+ const entry = this._secrets.get(subscriptionId);
609
+ this._secrets.set(subscriptionId, {
610
+ current: newSecret,
611
+ previous: entry?.current ?? null,
612
+ previousExpiresAt: Date.now() + windowS * 1000,
613
+ });
614
+ }
615
+
616
+ getSecrets(subscriptionId) {
617
+ const entry = this._secrets.get(subscriptionId);
618
+ if (!entry) return [null, null];
619
+ const previous = entry.previous && entry.previousExpiresAt > Date.now() ? entry.previous : null;
620
+ return [entry.current, previous];
621
+ }
622
+
623
+ getCursor(subscriptionId) {
624
+ return this._cursors.get(subscriptionId) ?? null;
625
+ }
626
+
627
+ setCursor(subscriptionId, cursor) {
628
+ this._cursors.set(subscriptionId, cursor);
629
+ }
630
+
631
+ _prune(now) {
632
+ if (this._seen.size <= MAX_SEEN_ENTRIES) return;
633
+ for (const [id, expiresAt] of this._seen) {
634
+ if (expiresAt <= now) this._seen.delete(id);
635
+ if (this._seen.size <= MAX_SEEN_ENTRIES) return;
636
+ }
637
+ // still over the cap: evict oldest insertions
638
+ for (const id of this._seen.keys()) {
639
+ this._seen.delete(id);
640
+ if (this._seen.size <= MAX_SEEN_ENTRIES) return;
641
+ }
642
+ }
643
+ }
644
+
645
+ store = { InMemoryWebhookStore };
646
+ return store;
647
+ }
648
+
649
+ var consumer;
650
+ var hasRequiredConsumer;
651
+
652
+ function requireConsumer () {
653
+ if (hasRequiredConsumer) return consumer;
654
+ hasRequiredConsumer = 1;
655
+
656
+ const { verifyWebhookSignature, toBytes, lowerHeaders } = requireSigning();
657
+ const { InMemoryWebhookStore } = requireStore();
658
+
659
+ // Photo columns each producer model writes — used to select a model's results
660
+ // out of a fetched PhotoDisplay. Mirrors the producer vocabulary; personhood is
661
+ // predicate-based and writes no columns.
662
+ const WEBHOOK_MODEL_COLUMNS = {
663
+ blur: ['blur_value'],
664
+ colors: ['main_color_palette', 'color_averages', 'hue_values', 'light_values', 'saturation_values'],
665
+ face_recognition: ['number_of_faces', 'bounding_boxes_from_faces_model'],
666
+ image_classification: ['labels_from_classifications_model', 'feature_extraction_output'],
667
+ image_comparison: ['average_hash'],
668
+ image_describe: ['topic'],
669
+ ocr: ['ocr_output'],
670
+ similarity: ['similar_photo_ids_high', 'similar_photo_ids_medium', 'similar_photo_ids_low'],
671
+ evidence: ['evidence_score', 'aesthetic_score', 'face_score', 'content_score', 'similarity_set_ranking'],
672
+ personhood: [],
673
+ };
674
+
675
+ const WEBHOOK_OUTCOME_MODELS = {
676
+ curation: ['blur', 'colors', 'face_recognition', 'image_classification', 'image_comparison', 'similarity', 'evidence'],
677
+ similarity: ['image_classification', 'image_comparison', 'similarity'],
678
+ persons: ['face_recognition', 'personhood'],
679
+ };
680
+
681
+ // Client-side counterpart of the MediaViz webhook producer, giving photo-by-photo
682
+ // progress as a project upload is analyzed.
683
+ //
684
+ // Receiver half (no auth needed): classify inbound POSTs, echo the subscription
685
+ // verification challenge, verify signed `target.completed` deliveries (HMAC +
686
+ // timestamp skew), dedupe on event_id, and dispatch each new event to a single
687
+ // onEvent callback. `handleRequest` is framework-agnostic glue — adapt it to
688
+ // express/lambda/etc. in a few lines by feeding it the raw body and headers.
689
+ //
690
+ // API half (requires an authenticated client): subscription lifecycle
691
+ // orchestration — register/confirm/rotate store the signing secret the moment
692
+ // the producer issues it — plus history pulls that share the push dedupe
693
+ // ledger, and out-of-band result fetches (webhooks carry a pointer, never the
694
+ // result itself).
695
+ class WebhookConsumer {
696
+ constructor(ctx, subscriptionApi, options = {}) {
697
+ this._ctx = ctx;
698
+ this._api = subscriptionApi;
699
+ this.store = options.store ?? new InMemoryWebhookStore();
700
+ this.skewToleranceS = options.skewToleranceS ?? 300;
701
+ this.rotationWindowS = options.rotationWindowS ?? 86400;
702
+ this.dedupeTtlS = options.dedupeTtlS ?? 604800;
703
+ this.pageLimit = options.pageLimit ?? 100;
704
+ this._onEvent = options.onEvent ?? null;
705
+ }
706
+
707
+ // ── receiver half ─────────────────────────────────────────────────────────
708
+
709
+ onEvent(fn) {
710
+ this._onEvent = fn;
711
+ return fn;
712
+ }
713
+
714
+ useStore(store) {
715
+ this.store = store;
716
+ }
717
+
718
+ classifyMessage(body) {
719
+ const type = body?.event_type;
720
+ if (type === 'subscription.verification') return 'verification';
721
+ if (type === 'target.completed') return 'delivery';
722
+ return 'unknown';
723
+ }
724
+
725
+ handleChallenge(body) {
726
+ return { challenge: body.challenge };
727
+ }
728
+
729
+ async handleDelivery(headers, rawBody) {
730
+ let event;
731
+ try {
732
+ event = this._parseDelivery(rawBody);
733
+ } catch {
734
+ return 'bad_signature';
735
+ }
736
+ const [current, previous] = this.store.getSecrets(String(event.subscription_id));
737
+ const ok = await verifyWebhookSignature(current, previous, headers, rawBody, { skewToleranceS: this.skewToleranceS });
738
+ if (!ok) return 'bad_signature';
739
+ return this._dispatch(event);
740
+ }
741
+
742
+ async handleRequest(headers, rawBody) {
743
+ let body;
744
+ try {
745
+ body = JSON.parse(_text(rawBody));
746
+ } catch {
747
+ return { status: 400, body: { detail: 'invalid JSON' } };
748
+ }
749
+ switch (this.classifyMessage(body)) {
750
+ case 'verification':
751
+ return { status: 200, body: this.handleChallenge(body) };
752
+ case 'delivery': {
753
+ const ack = await this.handleDelivery(lowerHeaders(headers), rawBody);
754
+ if (ack === 'bad_signature') return { status: 401, body: { detail: 'bad signature' } };
755
+ return { status: 200, body: { status: ack } };
756
+ }
757
+ default:
758
+ return { status: 400, body: { detail: 'unrecognized event_type' } };
759
+ }
760
+ }
761
+
762
+ // ── API half ──────────────────────────────────────────────────────────────
763
+
764
+ async register(projectTableName, callbackUrl, targets) {
765
+ const res = await this._api.createSubscription(projectTableName, callbackUrl, targets);
766
+ this._saveSecretFrom(res);
767
+ return res;
768
+ }
769
+
770
+ async confirm(subscriptionId) {
771
+ const res = await this._api.verifySubscription(subscriptionId);
772
+ this._saveSecretFrom(res);
773
+ return res;
774
+ }
775
+
776
+ async rotateSecret(subscriptionId) {
777
+ const res = await this._api.rotateSubscriptionSecret(subscriptionId);
778
+ this.store.rotateSecret(String(subscriptionId), res.signing_secret, this.rotationWindowS);
779
+ return res.signing_secret;
780
+ }
781
+
782
+ async updateCallback(subscriptionId, callbackUrl) {
783
+ // A callback change resets the subscription to pending_verification and
784
+ // pauses push until the new URL passes the challenge — confirm right away.
785
+ await this._api.updateSubscription(subscriptionId, { callbackUrl });
786
+ return this.confirm(subscriptionId);
787
+ }
788
+
789
+ async disable(subscriptionId) {
790
+ return this._api.deleteSubscription(subscriptionId);
791
+ }
792
+
793
+ async listSubscriptions() {
794
+ return this._api.listSubscriptions();
795
+ }
796
+
797
+ async pullEvents(subscriptionId, { since, limit } = {}) {
798
+ return this._api.listSubscriptionEvents(subscriptionId, { since, limit: limit ?? this.pageLimit });
799
+ }
800
+
801
+ async reconcile(subscriptionId, projectTableName) {
802
+ const id = String(subscriptionId);
803
+ let cursor = this.store.getCursor(id);
804
+ let pulled = 0;
805
+ let dispatched = 0;
806
+ for (;;) {
807
+ const page = await this._api.listSubscriptionEvents(id, { since: cursor ?? undefined, limit: this.pageLimit });
808
+ const events = page?.events ?? [];
809
+ pulled += events.length;
810
+ for (const ev of events) {
811
+ const ack = await this._dispatch({
812
+ event_id: ev.event_id,
813
+ event_type: 'target.completed',
814
+ schema_version: '1',
815
+ subscription_id: id,
816
+ project_table_name: projectTableName,
817
+ photo_id: ev.photo_id,
818
+ target: ev.target,
819
+ completed_at: ev.completed_at,
820
+ result_location: this._ctx.baseUrl + _photoPath(projectTableName, ev.photo_id),
821
+ });
822
+ if (ack === 'handled') dispatched += 1;
823
+ }
824
+ if (page?.next_cursor && page.next_cursor !== cursor) {
825
+ cursor = page.next_cursor;
826
+ this.store.setCursor(id, cursor);
827
+ }
828
+ if (!events.length || events.length < this.pageLimit) break;
829
+ }
830
+ return { pulled, dispatched };
831
+ }
832
+
833
+ async fetchResult(event) {
834
+ this._ctx.requireTokens();
835
+ const path = _photoPath(event.project_table_name, event.photo_id);
836
+ const { data } = await this._ctx.client.request(path, 'GET', this._ctx.accessToken, this._ctx.refreshToken);
837
+ return { photo: data, selected: selectResultColumns(data, event.target) };
838
+ }
839
+
840
+ saveSecret(subscriptionId, secret) {
841
+ this.store.saveSecret(String(subscriptionId), secret);
842
+ }
843
+
844
+ // ── internal ──────────────────────────────────────────────────────────────
845
+
846
+ _saveSecretFrom(res) {
847
+ if (res?.signing_secret && res?.subscription_id != null) {
848
+ this.store.saveSecret(String(res.subscription_id), res.signing_secret);
849
+ }
850
+ }
851
+
852
+ _parseDelivery(rawBody) {
853
+ const event = JSON.parse(_text(rawBody));
854
+ for (const key of ['event_id', 'subscription_id', 'target']) {
855
+ if (typeof event?.[key] !== 'string' || !event[key]) throw new Error(`missing ${key}`);
856
+ }
857
+ if (!Number.isInteger(event.photo_id)) throw new Error('photo_id must be an integer');
858
+ return event;
859
+ }
860
+
861
+ async _dispatch(event) {
862
+ if (!this.store.markSeen(String(event.event_id), this.dedupeTtlS)) return 'duplicate';
863
+ if (this._onEvent) {
864
+ try {
865
+ await this._onEvent(event);
866
+ } catch (e) {
867
+ // release the dedupe claim so the producer's retry redelivers
868
+ this.store.unmarkSeen(String(event.event_id));
869
+ throw e;
870
+ }
871
+ }
872
+ return 'handled';
873
+ }
874
+ }
875
+
876
+ // helpers
877
+ function selectResultColumns(photo, target) {
878
+ if (target === 'all') return null;
879
+ const sep = String(target).indexOf(':');
880
+ const kind = sep === -1 ? String(target) : String(target).slice(0, sep);
881
+ const name = sep === -1 ? '' : String(target).slice(sep + 1);
882
+ let columns;
883
+ if (kind === 'model') {
884
+ columns = WEBHOOK_MODEL_COLUMNS[name];
885
+ if (!columns) throw new Error(`unknown model '${name}'`);
886
+ } else if (kind === 'outcome') {
887
+ const models = WEBHOOK_OUTCOME_MODELS[name];
888
+ if (!models) throw new Error(`unknown outcome '${name}'`);
889
+ columns = [...new Set(models.flatMap((m) => WEBHOOK_MODEL_COLUMNS[m]))];
890
+ } else {
891
+ throw new Error(`unknown target grammar '${target}' (expected 'model:<name>', 'outcome:<name>', or 'all')`);
892
+ }
893
+ const selected = {};
894
+ for (const c of columns) selected[c] = photo?.[c] ?? null;
895
+ return selected;
896
+ }
897
+
898
+ function _photoPath(projectTableName, photoId) {
899
+ return `/api/v1/photos/${encodeURIComponent(projectTableName)}/${encodeURIComponent(String(photoId))}`;
900
+ }
901
+
902
+ function _text(rawBody) {
903
+ return typeof rawBody === 'string' ? rawBody : new TextDecoder().decode(toBytes(rawBody));
904
+ }
905
+
906
+ consumer = { WebhookConsumer, selectResultColumns, WEBHOOK_MODEL_COLUMNS, WEBHOOK_OUTCOME_MODELS };
907
+ return consumer;
908
+ }
909
+
468
910
  var src;
469
911
  var hasRequiredSrc;
470
912
 
@@ -472,19 +914,19 @@
472
914
  if (hasRequiredSrc) return src;
473
915
  hasRequiredSrc = 1;
474
916
 
475
- const { OAuthClient } = requireClient();
476
- const { OAuthError } = requireErrors();
477
- const { OAuthErrorCode } = requireTypes();
917
+ const { WebhookConsumer, selectResultColumns, WEBHOOK_MODEL_COLUMNS, WEBHOOK_OUTCOME_MODELS } = requireConsumer();
918
+ const { InMemoryWebhookStore } = requireStore();
919
+ const { signWebhookPayload, verifyWebhookSignature } = requireSigning();
478
920
 
479
- src = { OAuthClient, OAuthError, OAuthErrorCode };
921
+ src = { WebhookConsumer, InMemoryWebhookStore, signWebhookPayload, verifyWebhookSignature, selectResultColumns, WEBHOOK_MODEL_COLUMNS, WEBHOOK_OUTCOME_MODELS };
480
922
  return src;
481
923
  }
482
924
 
483
925
  var srcExports = requireSrc();
484
- var _oauth = /*@__PURE__*/getDefaultExportFromCjs(srcExports);
926
+ var _webhooks = /*@__PURE__*/getDefaultExportFromCjs(srcExports);
485
927
 
486
928
  // Auto-generated — do not edit
487
- const { OAuthClient, OAuthError, OAuthErrorCode } = _oauth;
929
+ const { WebhookConsumer, InMemoryWebhookStore, signWebhookPayload, verifyWebhookSignature, selectResultColumns, WEBHOOK_MODEL_COLUMNS, WEBHOOK_OUTCOME_MODELS } = _webhooks;
488
930
 
489
931
  class AiModelCredits {
490
932
  constructor(ctx) { this._ctx = ctx; }
@@ -1214,7 +1656,7 @@
1214
1656
  class Projects {
1215
1657
  constructor(ctx) { this._ctx = ctx; }
1216
1658
 
1217
- async createProjectAndRun(name, private_ = undefined, type = undefined, description = undefined, directory = undefined, photoUploadVector = undefined, thumbnail = undefined, runName = undefined, { outcomes, models } = {}) {
1659
+ async createProjectAndRun(name, type, private_ = undefined, description = undefined, directory = undefined, photoUploadVector = undefined, thumbnail = undefined, runName = undefined, { outcomes, models } = {}) {
1218
1660
  this._ctx.requireTokens();
1219
1661
  let path = `/api/v1/project_outcome/`;
1220
1662
  const query = new URLSearchParams();
@@ -1907,6 +2349,7 @@
1907
2349
  this.search = new Search(_ctx);
1908
2350
  this.subscription = new Subscription(_ctx);
1909
2351
  this.users = new Users(_ctx);
2352
+ this.webhooks = new WebhookConsumer(_ctx, this.subscription);
1910
2353
  }
1911
2354
 
1912
2355
  async authenticate() {
@@ -1944,6 +2387,7 @@
1944
2387
  exports.CustomAlbums = CustomAlbums;
1945
2388
  exports.EmailTokens = EmailTokens;
1946
2389
  exports.Health = Health;
2390
+ exports.InMemoryWebhookStore = InMemoryWebhookStore;
1947
2391
  exports.Keywords = Keywords;
1948
2392
  exports.MediaViz = MediaViz;
1949
2393
  exports.NotFoundError = NotFoundError;
@@ -1963,6 +2407,12 @@
1963
2407
  exports.Subscription = Subscription;
1964
2408
  exports.Users = Users;
1965
2409
  exports.ValidationError = ValidationError;
2410
+ exports.WEBHOOK_MODEL_COLUMNS = WEBHOOK_MODEL_COLUMNS;
2411
+ exports.WEBHOOK_OUTCOME_MODELS = WEBHOOK_OUTCOME_MODELS;
2412
+ exports.WebhookConsumer = WebhookConsumer;
1966
2413
  exports.handleResponse = handleResponse;
2414
+ exports.selectResultColumns = selectResultColumns;
2415
+ exports.signWebhookPayload = signWebhookPayload;
2416
+ exports.verifyWebhookSignature = verifyWebhookSignature;
1967
2417
 
1968
2418
  }));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mediaviz/sdk",
3
- "version": "1.8.0-dev.92",
3
+ "version": "1.8.0-dev.96",
4
4
  "description": "MediaViz JavaScript SDK \u2014 auto-generated public endpoint client.",
5
5
  "license": "MIT",
6
6
  "repository": {