mirror of
https://github.com/axios/axios.git
synced 2026-04-11 14:21:59 +08:00
* Added AxiosHeaders class; * Fixed README.md href; * Fixed a potential bug with headers normalization; * Fixed a potential bug with headers normalization; Refactored accessor building routine; Refactored default transforms; Removed `normalizeHeaderName` helper; * Added `Content-Length` accessor; Added missed `has` accessor to TS types; * Added `AxiosTransformStream` class; Added progress capturing ability for node.js environment; Added `maxRate` option to limit the data rate in node.js environment; Refactored event handled by `onUploadProgress` && `onDownloadProgress` listeners in browser environment; Added progress & data rate tests for the http adapter; Added response stream aborting test; Added a manual progress capture test for the browser; Updated TS types; Added TS tests; Refactored request abort logic for the http adapter; Added ability to abort the response stream; * Remove `stream/promises` & `timers/promises` modules usage in tests; * Use `abortcontroller-polyfill`; * Fixed AxiosTransformStream dead-lock in legacy node versions; Fixed CancelError emitting in streams; * Reworked AxiosTransformStream internal logic to optimize memory consumption; Added throwing an error if the request stream was silently destroying (without error) Refers to #3966; * Treat the destruction of the request stream as a cancellation of the request; Fixed tests; * Emit `progress` event in the next tick; * Initial refactoring; * Refactored Mocha tests to use ESM; * Refactored Karma tests to use rollup preprocessor & ESM; Replaced grunt with gulp; Improved dev scripts; Added Babel for rollup build; * Added default commonjs package export for Node build; Added automatic contributors list generator for package.json; Co-authored-by: Jay <jasonsaayman@gmail.com>
146 lines
3.3 KiB
JavaScript
146 lines
3.3 KiB
JavaScript
import fs from 'fs';
|
|
import path from 'path';
|
|
import http from 'http';
|
|
import minimist from 'minimist';
|
|
import url from "url";
|
|
const argv = minimist(process.argv.slice(2));
|
|
let server;
|
|
let dirs;
|
|
|
|
const __filename = url.fileURLToPath(import.meta.url);
|
|
const __dirname = path.dirname(__filename);
|
|
|
|
function listDirs(root) {
|
|
const files = fs.readdirSync(root);
|
|
const dirs = [];
|
|
|
|
for (let i = 0, l = files.length; i < l; i++) {
|
|
const file = files[i];
|
|
if (file[0] !== '.') {
|
|
const stat = fs.statSync(path.join(root, file));
|
|
if (stat.isDirectory()) {
|
|
dirs.push(file);
|
|
}
|
|
}
|
|
}
|
|
|
|
return dirs;
|
|
}
|
|
|
|
function getIndexTemplate() {
|
|
const links = dirs.map(function (dir) {
|
|
const url = '/' + dir;
|
|
return '<li onclick="document.location=\'' + url + '\'"><a href="' + url + '">' + url + '</a></li>';
|
|
});
|
|
|
|
return (
|
|
'<!doctype html>' +
|
|
'<html>' +
|
|
'<head>' +
|
|
'<title>axios examples</title>' +
|
|
'<style>' +
|
|
'body {padding:25px;}' +
|
|
'ul {margin:0; padding:0; list-style:none;}' +
|
|
'li {padding:5px 10px;}' +
|
|
'li:hover {background:#eee; cursor:pointer;}' +
|
|
'a {text-decoration:none; color:#0080ff;}' +
|
|
'</style>' +
|
|
'<body>' +
|
|
'<ul>' +
|
|
links.join('') +
|
|
'</ul>'
|
|
);
|
|
}
|
|
|
|
function sendResponse(res, statusCode, body) {
|
|
res.writeHead(statusCode);
|
|
res.write(body);
|
|
res.end();
|
|
}
|
|
|
|
function send200(res, body) {
|
|
sendResponse(res, 200, body || '<h1>OK</h1>');
|
|
}
|
|
|
|
function send404(res, body) {
|
|
sendResponse(res, 404, body || '<h1>Not Found</h1>');
|
|
}
|
|
|
|
function pipeFileToResponse(res, file, type) {
|
|
if (type) {
|
|
res.writeHead(200, {
|
|
'Content-Type': type
|
|
});
|
|
}
|
|
fs.createReadStream(path.join(__dirname, file)).pipe(res);
|
|
}
|
|
|
|
|
|
dirs = listDirs(__dirname);
|
|
|
|
server = http.createServer(function (req, res) {
|
|
let url = req.url;
|
|
|
|
// Process axios itself
|
|
if (/axios\.min\.js$/.test(url)) {
|
|
pipeFileToResponse(res, '../dist/axios.min.js', 'text/javascript');
|
|
return;
|
|
}
|
|
if (/axios\.min\.map$/.test(url)) {
|
|
pipeFileToResponse(res, '../dist/axios.min.map', 'text/javascript');
|
|
return;
|
|
}
|
|
if (/axios\.amd\.min\.js$/.test(url)) {
|
|
pipeFileToResponse(res, '../dist/axios.amd.min.js', 'text/javascript');
|
|
return;
|
|
}
|
|
if (/axios\.amd\.min\.map$/.test(url)) {
|
|
pipeFileToResponse(res, '../dist/axios.amd.min.map', 'text/javascript');
|
|
return;
|
|
}
|
|
|
|
// Process /
|
|
if (url === '/' || url === '/index.html') {
|
|
send200(res, getIndexTemplate());
|
|
return;
|
|
}
|
|
|
|
// Format request */ -> */index.html
|
|
if (/\/$/.test(url)) {
|
|
url += 'index.html';
|
|
}
|
|
|
|
// Format request /get -> /get/index.html
|
|
const parts = url.split('/');
|
|
if (dirs.indexOf(parts[parts.length - 1]) > -1) {
|
|
url += '/index.html';
|
|
}
|
|
|
|
// Process index.html request
|
|
if (/index\.html$/.test(url)) {
|
|
if (fs.existsSync(path.join(__dirname, url))) {
|
|
pipeFileToResponse(res, url, 'text/html');
|
|
} else {
|
|
send404(res);
|
|
}
|
|
}
|
|
|
|
// Process server request
|
|
else if (new RegExp('(' + dirs.join('|') + ')\/server').test(url)) {
|
|
if (fs.existsSync(path.join(__dirname, url + '.js'))) {
|
|
require(path.join(__dirname, url + '.js'))(req, res);
|
|
} else {
|
|
send404(res);
|
|
}
|
|
}
|
|
else {
|
|
send404(res);
|
|
}
|
|
});
|
|
|
|
const PORT = argv.p || 3000;
|
|
|
|
server.listen(PORT, () => {
|
|
console.log(`Examples running on ${PORT}`);
|
|
});
|