Skip to content

Commit

Permalink
feat: 添加 FakeServer 相关介绍
Browse files Browse the repository at this point in the history
  • Loading branch information
KonghaYao committed Jul 21, 2021
1 parent 05ba4a7 commit e3891ef
Show file tree
Hide file tree
Showing 29 changed files with 186 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ bin-debug/
bin-release/
[Oo]bj/
[Bb]in/
node_modules/*
**/node_modules/*
# Other files and folders
.settings/
localFile/
Expand Down
51 changes: 51 additions & 0 deletions FakeServer/FakeServer.drawio
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<mxfile host="65bd71144e">
<diagram id="GILjV6VJQhHbRAu13tSy" name="第 1 页">
<mxGraphModel dx="926" dy="619" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="1169" pageHeight="827" math="0" shadow="0">
<root>
<mxCell id="0"/>
<mxCell id="1" parent="0"/>
<mxCell id="8" style="edgeStyle=entityRelationEdgeStyle;rounded=0;sketch=1;orthogonalLoop=1;jettySize=auto;html=1;fontFamily=Comic Sans MS;fontSize=24;" edge="1" parent="1" source="2" target="5">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
<mxCell id="9" style="edgeStyle=entityRelationEdgeStyle;rounded=0;sketch=1;orthogonalLoop=1;jettySize=auto;html=1;entryX=0;entryY=0.5;entryDx=0;entryDy=0;fontFamily=Comic Sans MS;fontSize=24;" edge="1" parent="1" source="2" target="6">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
<mxCell id="10" style="edgeStyle=entityRelationEdgeStyle;rounded=0;sketch=1;orthogonalLoop=1;jettySize=auto;html=1;fontFamily=Comic Sans MS;fontSize=24;strokeWidth=3;" edge="1" parent="1" source="2" target="7">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
<mxCell id="2" value="Router" style="rounded=1;whiteSpace=wrap;html=1;strokeWidth=2;fillWeight=4;hachureGap=8;hachureAngle=45;fillColor=#f8cecc;sketch=1;strokeColor=#b85450;fontStyle=1;fontFamily=Comic Sans MS;fontSize=24;" vertex="1" parent="1">
<mxGeometry x="420" y="210" width="120" height="60" as="geometry"/>
</mxCell>
<mxCell id="4" style="edgeStyle=entityRelationEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;entryX=0;entryY=0.5;entryDx=0;entryDy=0;fontFamily=Comic Sans MS;fontSize=24;sketch=1;strokeWidth=3;" edge="1" parent="1" source="3" target="2">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
<mxCell id="3" value="AJAX&lt;br&gt;API" style="rounded=1;whiteSpace=wrap;html=1;strokeWidth=2;fillWeight=4;hachureGap=8;hachureAngle=45;fillColor=#e1d5e7;sketch=1;fontFamily=Comic Sans MS;fontSize=24;strokeColor=#9673a6;" vertex="1" parent="1">
<mxGeometry x="160" y="210" width="120" height="60" as="geometry"/>
</mxCell>
<mxCell id="5" value="Route" style="ellipse;whiteSpace=wrap;html=1;strokeWidth=2;fillWeight=2;hachureGap=8;fillColor=#fff2cc;fillStyle=dots;sketch=1;fontFamily=Comic Sans MS;fontSize=24;strokeColor=#d6b656;" vertex="1" parent="1">
<mxGeometry x="640" y="110" width="120" height="60" as="geometry"/>
</mxCell>
<mxCell id="6" value="Route" style="ellipse;whiteSpace=wrap;html=1;strokeWidth=2;fillWeight=2;hachureGap=8;fillColor=#fff2cc;fillStyle=dots;sketch=1;fontFamily=Comic Sans MS;fontSize=24;strokeColor=#d6b656;" vertex="1" parent="1">
<mxGeometry x="640" y="210" width="120" height="60" as="geometry"/>
</mxCell>
<mxCell id="15" style="edgeStyle=elbowEdgeStyle;rounded=0;sketch=1;orthogonalLoop=1;jettySize=auto;elbow=vertical;html=1;entryX=0;entryY=0.5;entryDx=0;entryDy=0;fontFamily=Comic Sans MS;fontSize=18;startArrow=diamond;startFill=1;strokeWidth=3;" edge="1" parent="1" source="7" target="11">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
<mxCell id="7" value="Route" style="ellipse;whiteSpace=wrap;html=1;strokeWidth=2;fillWeight=2;hachureGap=8;fillColor=#fff2cc;fillStyle=dots;sketch=1;fontFamily=Comic Sans MS;fontSize=24;strokeColor=#d6b656;" vertex="1" parent="1">
<mxGeometry x="640" y="300" width="120" height="60" as="geometry"/>
</mxCell>
<mxCell id="14" style="edgeStyle=elbowEdgeStyle;rounded=0;sketch=1;orthogonalLoop=1;jettySize=auto;html=1;fontFamily=Comic Sans MS;fontSize=18;startArrow=diamond;startFill=1;elbow=vertical;entryX=0.5;entryY=1;entryDx=0;entryDy=0;strokeWidth=3;" edge="1" parent="1" source="11" target="3">
<mxGeometry relative="1" as="geometry">
<mxPoint x="720" y="460" as="targetPoint"/>
<Array as="points">
<mxPoint x="580" y="410"/>
</Array>
</mxGeometry>
</mxCell>
<mxCell id="11" value="Main&lt;br&gt;&amp;nbsp;Callback" style="rhombus;whiteSpace=wrap;html=1;strokeWidth=2;fillWeight=-1;hachureGap=8;fillStyle=cross-hatch;fillColor=#d5e8d4;sketch=1;fontFamily=Comic Sans MS;fontSize=18;strokeColor=#82b366;" vertex="1" parent="1">
<mxGeometry x="860" y="300" width="120" height="60" as="geometry"/>
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>
15 changes: 15 additions & 0 deletions FakeServer/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "@konghayao/fake-server",
"version": "0.0.1",
"description": "",
"main": "parseURL.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "MIT",
"dependencies": {
"path-to-regexp": "^6.2.0"
}
}
73 changes: 73 additions & 0 deletions FakeServer/parseURL.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* @license
* Copyright 2021 KonghaYao 江夏尧 <dongzhongzhidong@qq.com>
* SPDX-License-Identifier: Apache-2.0
*/

/**
* The Regexps were copied from Purl (A JavaScript URL parser) v2.3.1
* which developed and maintained by Mark Perkins, mark@allmarkedup.com
* Source repository: https://github.com/allmarkedup/jQuery-URL-Parser
* Licensed under an MIT-style license.
*/
const key = [
"source",
"protocol",
"authority",
"userInfo",
"user",
"password",
"host",
"port",
"relative",
"path",
"directory",
"file",
"query",
"hash",
]; // keys available to query
const parser = {
//less intuitive, more accurate to the specs
strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
// more intuitive, fails on relative paths and deviates from specs
loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/,
};

function parseUri(url, strictMode = false) {
const res = parser[strictMode ? "strict" : "loose"].exec(decodeURI(url));

const uri = key.reduce((col, keyName, index) => {
col[keyName] = res[index] || "";
return col;
}, {});

// build query and hash parameters
uri.queryParams = parseString(uri.query);

// compile a 'base' domain attribute
uri["base"] = uri.host
? (uri.protocol ? uri.protocol + "://" + uri.host : uri.host) +
(uri.port ? ":" + uri.port : "")
: "";

return uri;
}

function parseString(queryString) {
return String(queryString)
.split(/&|;/)
.reduce(function (collection, pair) {
try {
pair = decodeURIComponent(pair.replace(/\+/g, " "));
} catch (e) {
// ignore
}
let [key, value] = pair.split(/=/g);
collection[key] = value;
return collection;
}, {});
}

export function parseURL(url, strictMode = false) {
return parseUri(url, strictMode);
}
13 changes: 13 additions & 0 deletions FakeServer/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions FakeServer/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { map } from 'rxjs/operators';
import { of } from 'rxjs';
import { pathToRegexp } from 'path-to-regexp';
function normalizeRouters(Routers) {
return Object.entries(Routers);
}
function createRegexp(path, config = {}) {
return pathToRegexp(path, [], config);
}
class Route {
constructor(path, cb) {}
use(plugin) {}
}
class Router {
constructor(Routers) {
this.#RouterMap = new Map(normalizeRouters(Routers));
}
#RouterMap;
#RouteMatchers = [];
addRoute(path, mainCallback) {
const route = new Route(path, mainCallback);
const pathRegexp = createRegexp(path);
this.#RouterMap.set(pathRegexp, route);
this.#RouteMatchers.push(pathRegexp);
return route;
}
deleteRouter() {}
enableRouter() {}
disableRouter() {}
receiveMessage(path, message) {}
}
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
import { Spider } from './Spider/index';
import * as plugins from '../plugins/index.js';
import * as tools from './tools/index.js'; // 工具都是 $ 开头的函数
import * as tools from '../tools/index.js'; // 工具都是 $ 开头的函数
import { Plugin } from './Pipeline/PluginSystem';
import { Task, TaskGroup } from './TaskSystem/index';
export default Object.assign(Spider, tools, {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions src/tools/index.js → tools/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
export * from './analysis/index.js';
export * from './loader/loader.js';
export * from './Mock/Mock.js';
export * from '../FakeServer/parseURL.js';
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit e3891ef

Please sign in to comment.