feat: 기능 추가 과정중
This commit is contained in:
@@ -20,7 +20,7 @@ import type {
|
||||
ServiceWorkerUpdatePort,
|
||||
VersionedOfflineRepository,
|
||||
WorkerTaskPort,
|
||||
} from "./contracts.js";
|
||||
} from "./contracts.ts";
|
||||
|
||||
export function success<T>(value: T): CapabilityResult<T> {
|
||||
return Object.freeze({ ok: true, value });
|
||||
@@ -221,11 +221,35 @@ export class FakeFileTransferAdapter implements FileTransferPort {
|
||||
) {
|
||||
return failure("LIMIT_EXCEEDED", false, "File size or type is not allowed.");
|
||||
}
|
||||
input.onProgress({
|
||||
transferredBytes: input.file.size,
|
||||
totalBytes: input.file.size,
|
||||
});
|
||||
return success({ resourceId: `fake:${input.file.name}` });
|
||||
let transferredBytes = 0;
|
||||
for await (const chunk of input.file.content.chunks) {
|
||||
const duringTransfer = aborted(input.signal);
|
||||
if (duringTransfer) return duringTransfer;
|
||||
transferredBytes += chunk.byteLength;
|
||||
if (transferredBytes > input.file.size) {
|
||||
return failure(
|
||||
"INTEGRITY_FAILED",
|
||||
false,
|
||||
"Uploaded bytes exceeded the declared file size.",
|
||||
);
|
||||
}
|
||||
input.onProgress({
|
||||
phase: "TRANSFERRING",
|
||||
transferredBytes,
|
||||
totalBytes: input.file.size,
|
||||
});
|
||||
}
|
||||
if (
|
||||
transferredBytes !== input.file.size ||
|
||||
input.file.content.byteLength !== input.file.size
|
||||
) {
|
||||
return failure(
|
||||
"INTEGRITY_FAILED",
|
||||
false,
|
||||
"Uploaded bytes did not match the declared file size.",
|
||||
);
|
||||
}
|
||||
return success({ resourceId: "fake:opaque-resource" });
|
||||
}
|
||||
|
||||
async download(input: Parameters<FileTransferPort["download"]>[0]) {
|
||||
@@ -235,11 +259,23 @@ export class FakeFileTransferAdapter implements FileTransferPort {
|
||||
return failure("EXPIRED_RESOURCE", true, "The download link expired.");
|
||||
}
|
||||
const bytes = new TextEncoder().encode(input.resourceId);
|
||||
input.onProgress({
|
||||
transferredBytes: bytes.byteLength,
|
||||
totalBytes: bytes.byteLength,
|
||||
const chunks = async function* () {
|
||||
if (input.signal.aborted) return;
|
||||
input.onProgress({
|
||||
phase: "TRANSFERRING" as const,
|
||||
transferredBytes: bytes.byteLength,
|
||||
totalBytes: bytes.byteLength,
|
||||
});
|
||||
yield bytes.slice();
|
||||
};
|
||||
return success({
|
||||
fileName: "download.bin",
|
||||
mediaType: "application/octet-stream",
|
||||
content: {
|
||||
byteLength: bytes.byteLength,
|
||||
chunks: chunks(),
|
||||
},
|
||||
});
|
||||
return success(bytes);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user