@algolia/ingestion 1.55.1 → 1.55.2

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.
@@ -6,7 +6,7 @@ import {
6
6
  getAlgoliaAgent,
7
7
  validateRequired
8
8
  } from "@algolia/client-common";
9
- var apiClientVersion = "1.55.1";
9
+ var apiClientVersion = "1.55.2";
10
10
  var REGIONS = ["eu", "us"];
11
11
  function getDefaultHosts(region) {
12
12
  const url = "data.{region}.algolia.com".replace("{region}", region);
@@ -115,10 +115,41 @@ function createIngestionClient({
115
115
  referenceIndexName,
116
116
  maxRetries = 100
117
117
  }, requestOptions) {
118
+ if (batchSize < 1) {
119
+ throw new Error("`batchSize` must be at least 1.");
120
+ }
118
121
  let records = [];
119
122
  let offset = 0;
120
123
  const responses = [];
121
124
  const waitBatchSize = Math.floor(batchSize / 10) || batchSize;
125
+ const waitForBatch = async (start, end) => {
126
+ for (const resp of responses.slice(start, end)) {
127
+ if (!resp.eventID) {
128
+ throw new Error("received unexpected response from the push endpoint, eventID must not be undefined");
129
+ }
130
+ let retryCount = 0;
131
+ await createIterablePromise({
132
+ func: async () => {
133
+ if (resp.eventID === void 0 || !resp.eventID) {
134
+ throw new Error("received unexpected response from the push endpoint, eventID must not be undefined");
135
+ }
136
+ return this.getEvent({ runID: resp.runID, eventID: resp.eventID }).catch((error) => {
137
+ if (error.status === 404) {
138
+ return void 0;
139
+ }
140
+ throw error;
141
+ });
142
+ },
143
+ validate: (response) => response !== void 0,
144
+ aggregator: () => retryCount += 1,
145
+ error: {
146
+ validate: () => retryCount >= maxRetries,
147
+ message: () => `Stopped waiting for the task after ${maxRetries} retries. This does not mean the operation failed; it may still complete. If you need to keep polling, retry with a higher maxRetries.`
148
+ },
149
+ timeout: () => Math.min(retryCount * 1500, 5e3)
150
+ });
151
+ }
152
+ };
122
153
  const objectEntries = objects.entries();
123
154
  for (const [i, obj] of objectEntries) {
124
155
  records.push(obj);
@@ -128,36 +159,14 @@ function createIngestionClient({
128
159
  );
129
160
  records = [];
130
161
  }
131
- if (waitForTasks && responses.length > 0 && (responses.length % waitBatchSize === 0 || i === objects.length - 1)) {
132
- for (const resp of responses.slice(offset, offset + waitBatchSize)) {
133
- if (!resp.eventID) {
134
- throw new Error("received unexpected response from the push endpoint, eventID must not be undefined");
135
- }
136
- let retryCount = 0;
137
- await createIterablePromise({
138
- func: async () => {
139
- if (resp.eventID === void 0 || !resp.eventID) {
140
- throw new Error("received unexpected response from the push endpoint, eventID must not be undefined");
141
- }
142
- return this.getEvent({ runID: resp.runID, eventID: resp.eventID }).catch((error) => {
143
- if (error.status === 404) {
144
- return void 0;
145
- }
146
- throw error;
147
- });
148
- },
149
- validate: (response) => response !== void 0,
150
- aggregator: () => retryCount += 1,
151
- error: {
152
- validate: () => retryCount >= maxRetries,
153
- message: () => `Stopped waiting for the task after ${maxRetries} retries. This does not mean the operation failed; it may still complete. If you need to keep polling, retry with a higher maxRetries.`
154
- },
155
- timeout: () => Math.min(retryCount * 1500, 5e3)
156
- });
157
- }
162
+ if (waitForTasks && responses.length - offset >= waitBatchSize) {
163
+ await waitForBatch(offset, offset + waitBatchSize);
158
164
  offset += waitBatchSize;
159
165
  }
160
166
  }
167
+ if (waitForTasks) {
168
+ await waitForBatch(offset, responses.length);
169
+ }
161
170
  return responses;
162
171
  },
163
172
  /**