@requence/task 1.0.0-alpha.47 → 1.0.0-alpha.49

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/build/index.js CHANGED
@@ -1,11 +1,12 @@
1
1
  import {
2
2
  deobfuscate
3
- } from "./chunk-y4v98p2s.js";
3
+ } from "./chunk-b4h2we9a.js";
4
4
 
5
5
  // ../helpers/src/utils/callbackToAsyncIterator.ts
6
6
  function callbackToAsyncIterator(subscribe) {
7
7
  const pullQueue = [];
8
8
  const pushQueue = [];
9
+ let done = false;
9
10
  const pushValue = (value) => {
10
11
  if (pullQueue.length !== 0) {
11
12
  const resolver = pullQueue.shift();
@@ -21,23 +22,47 @@ function callbackToAsyncIterator(subscribe) {
21
22
  pullQueue.push(resolve);
22
23
  }
23
24
  });
24
- subscribe((value) => {
25
- pushValue({ value });
25
+ const cleanup = subscribe((value) => {
26
+ if (!done) {
27
+ pushValue({ value });
28
+ }
26
29
  }, (error) => {
27
- pushValue({ error });
30
+ if (!done) {
31
+ pushValue({ error });
32
+ }
28
33
  }, () => {
29
- pushValue({ done: true });
34
+ if (!done) {
35
+ pushValue({ done: true });
36
+ }
30
37
  });
31
38
  return {
32
39
  async next() {
40
+ if (done) {
41
+ return { value: undefined, done: true };
42
+ }
33
43
  const value = await pullValue();
34
44
  if (value.done) {
45
+ done = true;
35
46
  return { value: undefined, done: true };
36
47
  }
37
48
  if (value.error) {
49
+ done = true;
38
50
  throw value.error;
39
51
  }
40
52
  return { value: value.value, done: false };
53
+ },
54
+ async return() {
55
+ if (!done) {
56
+ done = true;
57
+ if (typeof cleanup === "function") {
58
+ cleanup();
59
+ }
60
+ while (pullQueue.length > 0) {
61
+ const resolver = pullQueue.shift();
62
+ resolver({ done: true });
63
+ }
64
+ }
65
+ return { value: undefined, done: true };
41
66
  }
42
67
  };
43
68
  }
@@ -485,6 +510,20 @@ async function abortTask(options) {
485
510
  }
486
511
  }
487
512
 
513
+ // src/ackTask.ts
514
+ async function ackTask(options) {
515
+ const { apiBaseUrl, token } = getAccessToken(options.accessToken);
516
+ const response = await fetch(`${apiBaseUrl}/task/ack/${options.taskId}`, {
517
+ method: "POST",
518
+ headers: {
519
+ Authorization: `Bearer ${token}`
520
+ }
521
+ });
522
+ if (!response.ok) {
523
+ throw new Error(`Failed to acknowledge task delivery: ${await response.text()}`);
524
+ }
525
+ }
526
+
488
527
  // src/protectTask.ts
489
528
  class TaskProtectError extends Error {
490
529
  taskId;
@@ -527,16 +566,19 @@ var taskStartUpdateSchema = baseUpdate.extend({
527
566
  });
528
567
  var taskErrorUpdateSchema = baseUpdate.extend({
529
568
  type: z5.literal("taskError"),
530
- reason: z5.string().optional()
569
+ reason: z5.string().optional(),
570
+ requireAck: z5.boolean().optional()
531
571
  });
532
572
  var taskAbortUpdateSchema = baseUpdate.extend({
533
573
  type: z5.literal("taskAborted"),
534
- reason: z5.string().optional()
574
+ reason: z5.string().optional(),
575
+ requireAck: z5.boolean().optional()
535
576
  });
536
577
  var taskEndUpdateSchema = baseUpdate.extend({
537
578
  type: z5.literal("taskEnd"),
538
579
  result: z5.record(z5.string(), z5.any()),
539
- exit: z5.string().nullable()
580
+ exit: z5.string().nullable(),
581
+ requireAck: z5.boolean().optional()
540
582
  });
541
583
  var nodeStartUpdateSchema = baseUpdate.extend({
542
584
  type: z5.literal("nodeStart"),
@@ -624,6 +666,7 @@ function createTask(options, onUpdate) {
624
666
  }
625
667
  const taskStorage = { input: null, data: {}, errors: {}, result: null };
626
668
  let since = null;
669
+ let lastPosition = null;
627
670
  let lastResponse;
628
671
  const customFetch = async (url, init) => {
629
672
  const response = await fetch(url, init);
@@ -639,17 +682,25 @@ function createTask(options, onUpdate) {
639
682
  "Content-Type": "application/json"
640
683
  },
641
684
  get body() {
642
- return JSON.stringify({ since: since ? since.toISOString() : null });
685
+ return JSON.stringify({
686
+ since: since ? since.toISOString() : null,
687
+ lastPosition
688
+ });
643
689
  },
644
690
  onScheduleReconnect() {
645
- since = new Date;
691
+ if (lastPosition === null) {
692
+ since = new Date;
693
+ }
646
694
  },
647
695
  async onDisconnect() {
648
696
  if (lastResponse.status === 200) {
649
697
  eventSource.close();
650
698
  }
651
699
  },
652
- onMessage({ event, data }) {
700
+ onMessage({ id, event, data }) {
701
+ if (id) {
702
+ lastPosition = Number(id);
703
+ }
653
704
  const updateResult = eventSchema.safeParse({ event, data });
654
705
  if (!updateResult.success) {
655
706
  eventSource.close();
@@ -679,11 +730,23 @@ function createTask(options, onUpdate) {
679
730
  case "taskEnd": {
680
731
  taskStorage.result = resolveRequenceTypes(update.result);
681
732
  eventSource.close();
733
+ if (update.requireAck) {
734
+ ackTask({
735
+ taskId: payload.taskId,
736
+ accessToken: options.accessToken
737
+ }).catch(() => {});
738
+ }
682
739
  break;
683
740
  }
684
741
  case "taskError":
685
742
  case "taskAborted": {
686
743
  eventSource.close();
744
+ if (update.requireAck) {
745
+ ackTask({
746
+ taskId: payload.taskId,
747
+ accessToken: options.accessToken
748
+ }).catch(() => {});
749
+ }
687
750
  break;
688
751
  }
689
752
  }
@@ -718,6 +781,8 @@ function createTask(options, onUpdate) {
718
781
  const taskIdPromiseWithResolvers = Promise.withResolvers();
719
782
  const taskUrlPromiseWithResolvers = Promise.withResolvers();
720
783
  const resultPromiseWithResolvers = Promise.withResolvers();
784
+ const finalizedPromiseWithResolvers = Promise.withResolvers();
785
+ finalizedPromiseWithResolvers.promise.catch(() => {});
721
786
  let rejectTaskIdPromise = false;
722
787
  let rejectTaskUrlPromise = false;
723
788
  (async () => {
@@ -729,6 +794,7 @@ function createTask(options, onUpdate) {
729
794
  });
730
795
  if (!prepareResponse.ok) {
731
796
  const error = new TaskPrepareError(await prepareResponse.text(), options.taskTemplate);
797
+ finalizedPromiseWithResolvers.reject(error);
732
798
  if (rejectTaskIdPromise) {
733
799
  taskIdPromiseWithResolvers.reject(error);
734
800
  }
@@ -749,7 +815,8 @@ function createTask(options, onUpdate) {
749
815
  value: {
750
816
  name: options.name,
751
817
  input: options.input,
752
- priority
818
+ priority,
819
+ requireAck: options.requireAck ?? false
753
820
  },
754
821
  options: {
755
822
  uploadUrl: prepareData.fileUrls?.uploadUrl,
@@ -766,6 +833,7 @@ function createTask(options, onUpdate) {
766
833
  });
767
834
  if (!finalizeResponse.ok) {
768
835
  const error = new TaskError(await finalizeResponse.text(), options.taskTemplate, prepareData.taskId);
836
+ finalizedPromiseWithResolvers.reject(error);
769
837
  if (rejectTaskUrlPromise) {
770
838
  taskUrlPromiseWithResolvers.reject(error);
771
839
  }
@@ -775,6 +843,7 @@ function createTask(options, onUpdate) {
775
843
  return;
776
844
  }
777
845
  const payload = await finalizeResponse.json();
846
+ finalizedPromiseWithResolvers.resolve(prepareData.taskId);
778
847
  taskUrlPromiseWithResolvers.resolve(payload.urls?.show ?? null);
779
848
  payloadPromiseWithResolvers.resolve(payload);
780
849
  if (onUpdate) {
@@ -783,14 +852,18 @@ function createTask(options, onUpdate) {
783
852
  }
784
853
  })();
785
854
  const asyncIterable = callbackToAsyncIterable((push, _fail, done) => {
786
- taskUpdateHandlers.add(function handleUpdate(update) {
855
+ const handleUpdate = (update) => {
787
856
  push(update);
788
857
  if (update.type === "taskEnd" || update.type === "taskError" || update.type === "taskAborted") {
789
858
  done();
790
859
  taskUpdateHandlers.delete(handleUpdate);
791
860
  }
792
- });
861
+ };
862
+ taskUpdateHandlers.add(handleUpdate);
793
863
  startMonitoring();
864
+ return () => {
865
+ taskUpdateHandlers.delete(handleUpdate);
866
+ };
794
867
  });
795
868
  const methods = {
796
869
  get catch() {
@@ -827,6 +900,9 @@ function createTask(options, onUpdate) {
827
900
  taskId,
828
901
  accessToken: options.accessToken
829
902
  });
903
+ },
904
+ async finalized() {
905
+ return finalizedPromiseWithResolvers.promise;
830
906
  }
831
907
  };
832
908
  return Object.defineProperties(asyncIterable, Object.getOwnPropertyDescriptors(methods));
@@ -841,6 +917,7 @@ function watchTasks(options) {
841
917
  }
842
918
  const taskStorage = new Map;
843
919
  let startDate = options.since;
920
+ let lastPosition = null;
844
921
  let lastResponse;
845
922
  const customFetch = async (url, init) => {
846
923
  const response = await fetch(url, init);
@@ -857,12 +934,15 @@ function watchTasks(options) {
857
934
  get body() {
858
935
  return JSON.stringify({
859
936
  since: startDate.toISOString(),
937
+ lastPosition,
860
938
  filter: options.filter
861
939
  });
862
940
  },
863
941
  method: "POST",
864
942
  onScheduleReconnect() {
865
- startDate = new Date;
943
+ if (lastPosition === null) {
944
+ startDate = new Date;
945
+ }
866
946
  options.onReconnecting?.();
867
947
  },
868
948
  onConnect() {
@@ -874,6 +954,9 @@ function watchTasks(options) {
874
954
  }
875
955
  },
876
956
  onMessage({ id, event, data }) {
957
+ if (id) {
958
+ lastPosition = Number(id);
959
+ }
877
960
  const updateResult = eventSchema.safeParse({ event, data });
878
961
  if (!updateResult.success) {
879
962
  eventSource.close();
@@ -915,10 +998,23 @@ function watchTasks(options) {
915
998
  case "taskEnd": {
916
999
  storageEntry.result = resolveRequenceTypes(update.result);
917
1000
  taskStorage.delete(update.taskId);
1001
+ if (update.requireAck) {
1002
+ ackTask({
1003
+ taskId: update.taskId,
1004
+ accessToken: options.accessToken
1005
+ }).catch(() => {});
1006
+ }
918
1007
  break;
919
1008
  }
920
- case "taskError": {
1009
+ case "taskError":
1010
+ case "taskAborted": {
921
1011
  taskStorage.delete(update.taskId);
1012
+ if (update.requireAck) {
1013
+ ackTask({
1014
+ taskId: update.taskId,
1015
+ accessToken: options.accessToken
1016
+ }).catch(() => {});
1017
+ }
922
1018
  break;
923
1019
  }
924
1020
  }
@@ -945,6 +1041,9 @@ function watchTasks(options) {
945
1041
  });
946
1042
  const asyncIterable = callbackToAsyncIterable((push) => {
947
1043
  taskUpdateHandlers.add(push);
1044
+ return () => {
1045
+ taskUpdateHandlers.delete(push);
1046
+ };
948
1047
  });
949
1048
  const stopWatching = () => {
950
1049
  eventSource.close();
@@ -960,7 +1059,8 @@ var taskSchema = z6.object({
960
1059
  "IDLE",
961
1060
  "PENDING",
962
1061
  "RUNNING",
963
- "STOPPED"
1062
+ "STOPPED",
1063
+ "AWAITING_DELIVERY"
964
1064
  ]),
965
1065
  statusText: z6.string().nullable(),
966
1066
  input: z6.unknown(),
@@ -1028,5 +1128,5 @@ export {
1028
1128
  RequenceFile
1029
1129
  };
1030
1130
 
1031
- //# debugId=F51ECB56265D093964756E2164756E21
1131
+ //# debugId=8AA062A0F475E65964756E2164756E21
1032
1132
  //# sourceMappingURL=index.js.map