Skip to content

Commit 30260d9

Browse files
committed
undo again
1 parent c93133e commit 30260d9

File tree

2 files changed

+16
-81
lines changed

2 files changed

+16
-81
lines changed

src/hooks/custom/SplitPdfHook.ts

Lines changed: 16 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import async from "async";
22

3+
import { HTTPClient } from "../../lib/http.js";
34
import {
45
AfterErrorContext,
56
AfterErrorHook,
@@ -24,12 +25,10 @@ import {
2425
stringToBoolean,
2526
} from "./utils/index.js";
2627
import {
27-
HTTPClientExtension,
2828
MIN_PAGES_PER_THREAD,
2929
PARTITION_FORM_FILES_KEY,
3030
PARTITION_FORM_SPLIT_PDF_PAGE_KEY,
3131
} from "./common.js";
32-
import {retry, RetryConfig} from "../../lib/retries";
3332

3433
/**
3534
* Represents a hook for splitting and sending PDF files as per page requests.
@@ -40,7 +39,8 @@ export class SplitPdfHook
4039
/**
4140
* The HTTP client used for making requests.
4241
*/
43-
client: HTTPClientExtension | undefined;
42+
client: HTTPClient | undefined;
43+
4444

4545
/**
4646
* Keeps the strict-mode setting for splitPdfPage feature.
@@ -68,16 +68,9 @@ export class SplitPdfHook
6868
* @returns The initialized SDK options.
6969
*/
7070
sdkInit(opts: SDKInitOptions): SDKInitOptions {
71-
const { baseURL } = opts;
72-
this.client = new HTTPClientExtension();
73-
74-
this.client.addHook("response", (res) => {
75-
if (res.status != 200) {
76-
console.error("Request failed with status code", `${res.status}`);
77-
}
78-
});
79-
80-
return { baseURL: baseURL, client: this.client };
71+
const { baseURL, client } = opts;
72+
this.client = client;
73+
return { baseURL: baseURL, client: client };
8174
}
8275

8376
/**
@@ -185,48 +178,23 @@ export class SplitPdfHook
185178
file.name,
186179
firstPageNumber
187180
);
188-
const timeoutInMs = 60 * 10 * 1000;
189181
const req = new Request(requestClone, {
190182
headers,
191183
body,
192-
signal: AbortSignal.timeout(timeoutInMs)
193184
});
194185
requests.push(req);
195186
setIndex+=1;
196187
}
197188

198189
this.partitionSuccessfulResponses[operationID] = new Array(requests.length);
199-
this.partitionFailedResponses[operationID] = new Array(requests.length);
200190

201191
const allowFailed = this.allowFailed;
202192

203-
// These are the retry values from our api spec
204-
// We need to hardcode them here until we're able to reuse the SDK
205-
// from within this hook
206-
const oneSecond = 1000;
207-
const oneMinute = 1000 * 60;
208-
const retryConfig = {
209-
strategy: "backoff",
210-
backoff: {
211-
initialInterval: oneSecond * 3,
212-
maxInterval: oneMinute * 12,
213-
exponent: 1.88,
214-
maxElapsedTime: oneMinute * 30,
215-
},
216-
} as RetryConfig;
217-
218-
const retryCodes = ["502", "503", "504"];
219-
220193
this.partitionRequests[operationID] = async.parallelLimit(
221-
requests.map((req, pageIndex) => async () => {
194+
requests.slice(0, -1).map((req, pageIndex) => async () => {
222195
const pageNumber = pageIndex + startingPageNumber;
223196
try {
224-
const response = await retry(
225-
async () => {
226-
return await this.client!.request(req.clone());
227-
},
228-
{ config: retryConfig, statusCodes: retryCodes }
229-
);
197+
const response = await this.client!.request(req);
230198
if (response.status === 200) {
231199
(this.partitionSuccessfulResponses[operationID] as Response[])[pageIndex] =
232200
response.clone();
@@ -238,7 +206,7 @@ export class SplitPdfHook
238206
}
239207
}
240208
} catch (e) {
241-
console.error(`Failed to send request for page ${pageNumber}.`, e);
209+
console.error(`Failed to send request for page ${pageNumber}.`);
242210
if (!allowFailed) {
243211
throw e;
244212
}
@@ -247,8 +215,7 @@ export class SplitPdfHook
247215
concurrencyLevel
248216
);
249217

250-
const dummyRequest = new Request("https://no-op/");
251-
return dummyRequest;
218+
return requests.at(-1) as Request;
252219
}
253220

254221
/**
@@ -263,39 +230,28 @@ export class SplitPdfHook
263230
successfulResponses: Response[],
264231
failedResponses: Response[]
265232
): Promise<Response> {
266-
let realResponse = response.clone();
267-
const firstSuccessfulResponse = successfulResponses.at(0);
268-
const isFakeResponse = response.headers.has("fake-response");
269-
if (firstSuccessfulResponse !== undefined && isFakeResponse) {
270-
realResponse = firstSuccessfulResponse.clone();
271-
}
272-
273233
let responseBody, responseStatus, responseStatusText;
274234
const numFailedResponses = failedResponses?.length ?? 0;
275-
const headers = prepareResponseHeaders(realResponse);
235+
const headers = prepareResponseHeaders(response);
276236

277237
if (!this.allowFailed && failedResponses && failedResponses.length > 0) {
278238
const failedResponse = failedResponses[0]?.clone();
279239
if (failedResponse) {
280240
responseBody = await failedResponse.text();
241+
responseStatus = failedResponse.status;
281242
responseStatusText = failedResponse.statusText;
282243
} else {
283244
responseBody = JSON.stringify({"details:": "Unknown error"});
245+
responseStatus = 503
284246
responseStatusText = "Unknown error"
285247
}
286-
// if the response status is unknown or was 502, 503, 504, set back to 500 to ensure we don't cause more retries
287-
responseStatus = 500;
288248
console.warn(
289249
`${numFailedResponses} requests failed. The partition operation is cancelled.`
290250
);
291251
} else {
292-
if (isFakeResponse) {
293-
responseBody = await prepareResponseBody([...successfulResponses]);
294-
} else {
295-
responseBody = await prepareResponseBody([...successfulResponses, response]);
296-
}
297-
responseStatus = realResponse.status
298-
responseStatusText = realResponse.statusText
252+
responseBody = await prepareResponseBody([...successfulResponses, response]);
253+
responseStatus = response.status
254+
responseStatusText = response.statusText
299255
if (numFailedResponses > 0) {
300256
console.warn(
301257
`${numFailedResponses} requests failed. The results might miss some pages.`

src/hooks/custom/common.ts

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import {HTTPClient} from "../../lib/http";
2-
31
/**
42
* Regular expression pattern for matching base hostnames in the form of "*.unstructuredapp.io".
53
*/
@@ -25,22 +23,3 @@ export const MAX_NUMBER_OF_PARALLEL_REQUESTS = 15;
2523

2624
export const MIN_PAGES_PER_THREAD = 2;
2725
export const MAX_PAGES_PER_THREAD = 20;
28-
29-
export class HTTPClientExtension extends HTTPClient {
30-
constructor() {
31-
super();
32-
}
33-
34-
override async request(request: Request): Promise<Response> {
35-
if (request.url === "https://no-op/") {
36-
return new Response('{}', {
37-
headers: [
38-
["fake-response", "fake-response"]
39-
],
40-
status: 200,
41-
statusText: 'OK_NO_OP'
42-
});
43-
}
44-
return super.request(request);
45-
}
46-
}

0 commit comments

Comments
 (0)