Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Sec-ant committed Nov 18, 2022
1 parent f9020f5 commit 2b4506c
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 37 deletions.
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@
"devDependencies": {
"@types/uuid": "^8.3.4",
"@types/wicg-file-system-access": "^2020.9.5",
"@typescript-eslint/eslint-plugin": "^5.42.1",
"@typescript-eslint/parser": "^5.42.1",
"@typescript-eslint/eslint-plugin": "^5.43.0",
"@typescript-eslint/parser": "^5.43.0",
"eslint": "^8.27.0",
"http-server": "^14.1.1",
"mocha": "^10.1.0",
"ts-loader": "^9.4.1",
"tslib": "^2.4.1",
"typescript": "^4.8.4",
"webpack": "^5.74.0",
"webpack-cli": "^4.10.0"
"typescript": "^4.9.3",
"webpack": "^5.75.0",
"webpack-cli": "^5.0.0"
},
"dependencies": {
"@sec-ant/trie-map": "^1.1.4",
"@sec-ant/trie-map": "^1.1.5",
"jssha": "github:Sec-ant/jsSHA#dd69d437ee912b95007a330f9afec7635f83271e",
"uuid": "^9.0.0",
"workbox-streams": "^6.5.4"
Expand Down
53 changes: 34 additions & 19 deletions src/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ interface TorrentOptionsBase {
*/
name?: string;
/**
* piece length: a power of 2 number
* piece length: a power of 2 number,
* will automatically calculate when this value is missing
*/
pieceLength?: number;
Expand Down Expand Up @@ -509,7 +509,7 @@ async function createV1(
iOpts.announce = iOpts.announceList[0]?.[0];
}
// progress hook
const [progressRef, updateProgress] = useProgress(0, onProgress);
const [updateProgress, setProgressTotal] = useProgress(0, onProgress);
// torrent name and common dir
const { commonDir, name } = resolveCommonDirAndTorrentName(
iOpts.name,
Expand All @@ -522,7 +522,7 @@ async function createV1(
if (iOpts.addPaddingFiles) {
// assign progress total (in piece unit)
const totalPieces = getTotalPieces(files, iOpts.pieceLength);
progressRef.total = totalPieces;
await setProgressTotal(totalPieces);

const pieceLayerReadableStreamPromise: Promise<
ReadableStream<Uint8Array>
Expand Down Expand Up @@ -557,7 +557,7 @@ async function createV1(
else {
// assign progress total (in piece unit)
const totalPieces = Math.ceil(totalFileSize / iOpts.pieceLength);
progressRef.total = totalPieces;
await setProgressTotal(totalPieces);

// concatenate all files into a single stream first
const {
Expand Down Expand Up @@ -673,8 +673,10 @@ async function createV2(
) {
iOpts.announce = iOpts.announceList[0]?.[0];
}
// assign progress total (in piece unit)
const totalPieces = getTotalPieces(files, iOpts.pieceLength);
// progress hook
const [, updateProgress] = useProgress(0, onProgress);
const [updateProgress] = useProgress(totalPieces, onProgress);
// torrent name
const { name } = resolveCommonDirAndTorrentName(iOpts.name, fileTree);
iOpts.name = name;
Expand Down Expand Up @@ -777,7 +779,7 @@ async function createHybrid(
iOpts.announce = iOpts.announceList[0]?.[0];
}
// progress hook
const [, updateProgress] = useProgress(
const [updateProgress] = useProgress(
getTotalPieces(files, iOpts.pieceLength) * 2,
onProgress
);
Expand Down Expand Up @@ -910,7 +912,7 @@ async function populatePieceLayersAndFileNodes(
blockLength: number;
pieceLength: number;
blocksPerPiece: number;
updateProgress?: () => void;
updateProgress?: UpdateProgress;
}
) {
// get pieces root and piece layer readable streams
Expand Down Expand Up @@ -949,7 +951,7 @@ function getV1PieceLayerReadableStream(
opts: {
pieceLength: number;
padding: boolean;
updateProgress?: () => void;
updateProgress?: UpdateProgress;
}
): ReadableStream<Uint8Array> {
const pieceLayerReadableStream = stream
Expand All @@ -973,7 +975,7 @@ function getV2PiecesRootAndPieceLayerReadableStreams(
opts: {
blockLength: number;
blocksPerPiece: number;
updateProgress?: () => void;
updateProgress?: UpdateProgress;
}
) {
const pieceLayerReadableStream: ReadableStream<Uint8Array> = stream
Expand Down Expand Up @@ -1019,16 +1021,18 @@ function createPaddingFile(paddingSize: number, commonDir: string | undefined) {
return paddingFile;
}

export type SetProgressTotal = (
totalNumberOrFunction:
| number
| ((total: number, current?: number) => number | Promise<number>)
) => Promise<void>;

export type UpdateProgress = () => Promise<void>;

function useProgress(
initTotal: number,
onProgress?: OnProgress
): [
{
current: number;
total: number;
},
() => void
] {
): [UpdateProgress, SetProgressTotal] {
// init progress parameters
const progressRef = {
// progress current (in piece unit)
Expand All @@ -1037,12 +1041,23 @@ function useProgress(
total: initTotal,
};
// update progress
const updateProgress = () => {
const updateProgress: UpdateProgress = async () => {
if (onProgress) {
void onProgress(progressRef.current++, progressRef.total);
await onProgress(++progressRef.current, progressRef.total);
}
};
// set progress total
const setProgressTotal: SetProgressTotal = async (totalNumberOrFunction) => {
if (typeof totalNumberOrFunction === "number") {
progressRef.total = totalNumberOrFunction;
} else {
progressRef.total = await totalNumberOrFunction(
progressRef.total,
progressRef.current
);
}
};
return [progressRef, updateProgress];
return [updateProgress, setProgressTotal];
}

/**
Expand Down
9 changes: 3 additions & 6 deletions src/encode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,19 +186,16 @@ export function encode(data: BData<false>, hooks?: EncoderHooks) {
* @returns
*/
function addHandler(
controller: ReadableStreamDefaultController<Uint8Array>,
controller: ReadableStreamController<Uint8Array>,
hookHandler: EncodeHookHandler
) {
const newController = new Proxy(controller, {
get: function (target, prop, receiver) {
if (prop !== "enqueue") {
return Reflect.get(target, prop, receiver) as unknown;
}
return ((chunk?: Uint8Array | undefined) => {
return ((chunk: Uint8Array) => {
target.enqueue(chunk);
if (chunk === undefined) {
return;
}
hookHandler({
value: chunk,
done: false,
Expand All @@ -218,7 +215,7 @@ export function useUint8ArrayStreamHook(): [
EncodeHookHandler
] {
const ref = {
controller: null as ReadableStreamDefaultController<Uint8Array> | null,
controller: null as ReadableStreamController<Uint8Array> | null,
};

const hookHandler = ({
Expand Down
7 changes: 4 additions & 3 deletions src/transformers/merkleRootCalculator.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { UpdateProgress } from "../create.js";
import { merkleRoot } from "../utils/misc.js";
/**
* Merkle root calculator transformer class
Expand All @@ -6,7 +7,7 @@ class MerkleRootCalculatorTransformer
implements Transformer<Uint8Array[], Uint8Array>
{
updateProgress;
constructor(updateProgress?: () => void) {
constructor(updateProgress?: UpdateProgress) {
this.updateProgress = updateProgress;
}
async transform(
Expand All @@ -15,7 +16,7 @@ class MerkleRootCalculatorTransformer
) {
controller.enqueue(await merkleRoot(chunk));
if (this.updateProgress) {
this.updateProgress();
await this.updateProgress();
}
}
}
Expand All @@ -24,7 +25,7 @@ export class MerkleRootCalculator extends TransformStream<
Uint8Array[],
Uint8Array
> {
constructor(updateProgress?: () => void) {
constructor(updateProgress?: UpdateProgress) {
const transformer = new MerkleRootCalculatorTransformer(updateProgress);
super(transformer);
}
Expand Down
8 changes: 5 additions & 3 deletions src/transformers/pieceHasher.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { UpdateProgress } from "../create.js";

/**
* Piece hasher transformer class
*/
class PieceHasherTransformer implements Transformer<Uint8Array, Uint8Array> {
updateProgress;
constructor(updateProgress?: () => void) {
constructor(updateProgress?: UpdateProgress) {
this.updateProgress = updateProgress;
}
async transform(
Expand All @@ -21,13 +23,13 @@ class PieceHasherTransformer implements Transformer<Uint8Array, Uint8Array> {
}
controller.enqueue(pieceHash);
if (this.updateProgress) {
this.updateProgress();
await this.updateProgress();
}
}
}

export class PieceHasher extends TransformStream<Uint8Array, Uint8Array> {
constructor(updateProgress?: () => void) {
constructor(updateProgress?: UpdateProgress) {
const transformer = new PieceHasherTransformer(updateProgress);
super(transformer);
}
Expand Down

0 comments on commit 2b4506c

Please sign in to comment.