This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require("fs"); | |
const http = require("http"); | |
const util = require("util"); | |
const readFile = util.promisify(fs.readFile); | |
const server = http.createServer(async (req, res) => { | |
res.writeHead(200, { | |
"Content-Type": "text/plain;charset=utf-8", | |
}); | |
const buffer = await readFile(__dirname + "/big-file.txt"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type FunctionPropertyNames<T> = { | |
[K in keyof T]: T[K] extends Function ? K : never; | |
}[keyof T]; | |
type FunctionProperties<T> = Pick<T, FunctionPropertyNames<T>>; | |
| |
type NonFunctionPropertyNames<T> = { | |
[K in keyof T]: T[K] extends Function ? never : K; | |
}[keyof T]; | |
type NonFunctionProperties<T> = Pick<T, NonFunctionPropertyNames<T>>; | |
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="zh-cn"> | |
<head> | |
<meta charset="UTF-8" /> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
<title>Concurrent Download Demo</title> | |
<script src="multi-thread-download.js"></script> | |
</head> | |
<body> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function saveAs({ name, buffers, mime = "application/octet-stream" }) { | |
const blob = new Blob([buffers], { type: mime }); | |
const blobUrl = URL.createObjectURL(blob); | |
const a = document.createElement("a"); | |
a.download = name || Math.random(); | |
a.href = blobUrl; | |
a.click(); | |
URL.revokeObjectURL(blob); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function download({ url, chunkSize, poolLimit = 1 }) { | |
const contentLength = await getContentLength(url); | |
const chunks = typeof chunkSize === "number" ? Math.ceil(contentLength / chunkSize) : 1; | |
const results = await asyncPool( | |
poolLimit, | |
[...new Array(chunks).keys()], | |
(i) => { | |
let start = i * chunkSize; | |
let end = i + 1 == chunks ? contentLength - 1 : (i + 1) * chunkSize - 1; | |
return getBinaryContent(url, start, end, i); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getContentLength(url) { | |
return new Promise((resolve, reject) => { | |
let xhr = new XMLHttpRequest(); | |
xhr.open("HEAD", url); | |
xhr.send(); | |
xhr.onload = function () { | |
resolve( | |
~~xhr.getResponseHeader("Content-Length") | |
); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function concatenate(arrays) { | |
if (!arrays.length) return null; | |
let totalLength = arrays.reduce((acc, value) => acc + value.length, 0); | |
let result = new Uint8Array(totalLength); | |
let length = 0; | |
for (let array of arrays) { | |
result.set(array, length); | |
length += array.length; | |
} | |
return result; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getBinaryContent(url, start, end, i) { | |
return new Promise((resolve, reject) => { | |
try { | |
let xhr = new XMLHttpRequest(); | |
xhr.open("GET", url, true); | |
xhr.setRequestHeader("range", `bytes=${start}-${end}`); // Set range request information | |
xhr.responseType = "arraybuffer"; // Set the returned type to arraybuffer | |
xhr.onload = function () { | |
resolve({ | |
index: i, // file block index |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Promise.race = function (iterators) { | |
return new Promise((resolve, reject) => { | |
for (const iter of iterators) { | |
Promise.resolve(iter) | |
.then((res) => { | |
resolve(res); | |
}) | |
.catch((e) => { | |
reject(e); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Promise.all = function (iterators) { | |
return new Promise((resolve, reject) => { | |
if (!iterators || iterators.length === 0) { | |
resolve([]); | |
} else { | |
// used to determine whether all tasks are completed | |
let count = 0; | |
let result = []; // result array | |
for (let i = 0; i < iterators.length; i++) { | |
// Considering that iterators[i] may be ordinary object, |