judge: allow unlimited access & fix file corruption when errored (#726)

Co-authored-by: undefined <i@undefined.moe>
This commit is contained in:
Yang Gaoandundefined authored and GitHub committed 2024-02-20 07:39:56 +08:00
1 parent 1f2100b9d1
commit 7ea3c91f5f
6 files changed
+42 -37

No files matched your search

+25 -3
View File
@@ -8,7 +8,7 @@ import fs from 'fs-extra';
import moment, { isMoment, Moment } from 'moment-timezone';
import { ObjectId } from 'mongodb';
import Logger from 'reggol';
import type { Request } from 'superagent';
import type * as superagent from 'superagent';
export * as yaml from 'js-yaml';
export * as fs from 'fs-extra';
@@ -82,10 +82,10 @@ export function isClass(obj: any, strict = false): obj is new (...args: any) =>
return false;
}
function isSuperagentRequest(t: NodeJS.ReadableStream | Request): t is Request {
function isSuperagentRequest(t: NodeJS.ReadableStream | superagent.Request): t is superagent.Request {
return 'req' in t;
}
export function streamToBuffer(input: NodeJS.ReadableStream | Request, maxSize = 0): Promise<Buffer> {
export function streamToBuffer(input: NodeJS.ReadableStream | superagent.Request, maxSize = 0): Promise<Buffer> {
let stream: NodeJS.ReadableStream;
if (isSuperagentRequest(input)) {
const s = new PassThrough();
@@ -323,4 +323,26 @@ export async function extractZip(zip: AdmZip, dest: string, overwrite = false, s
}
}
export async function pipeRequest(req: superagent.Request, w: fs.WriteStream, timeout?: number, name?: string) {
try {
await req.buffer(false).timeout({
response: Math.min(10000, timeout),
deadline: timeout,
}).parse((resp, cb) => {
if (resp.statusCode !== 200) throw new Error(`${resp.statusCode}`);
else {
resp.pipe(w);
resp.on('end', () => {
cb(null, undefined);
});
resp.on('error', (err) => {
cb(err, undefined);
});
}
});
} catch (e) {
throw new Error(`Download${e.errno === 'ETIMEDOUT' ? 'Timedout' : 'Error'}(${name ? `${name}, ` : ''}${e.message})`);
}
}
export * from '@hydrooj/utils/lib/common';