Skip to content

Commit

Permalink
feat(madge): madge 生成时需要 alias 的信息,所以添加了 webpack.config.js
Browse files Browse the repository at this point in the history
  • Loading branch information
KonghaYao committed Aug 2, 2021
1 parent 8ea3697 commit bf4d071
Show file tree
Hide file tree
Showing 23 changed files with 713 additions and 647 deletions.
5 changes: 5 additions & 0 deletions FakeServer/ajax/XMLHttpRequest/constant.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* @license
* Copyright 2021 KonghaYao 江夏尧 <dongzhongzhidong@qq.com>
* SPDX-License-Identifier: Apache-2.0
*/
export default {
100: 'Continue',
101: 'Switching Protocols',
Expand Down
5 changes: 5 additions & 0 deletions FakeServer/ajax/XMLHttpRequest/defineGetterAndSetter.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* @license
* Copyright 2021 KonghaYao 江夏尧 <dongzhongzhidong@qq.com>
* SPDX-License-Identifier: Apache-2.0
*/
// 不可以在原生的 XMLHttpRequest 上直接定义 getter 和 setter,
// 也不可以在 XHR 实例上定义
// 这样的话会导致无法接收到数据
Expand Down
5 changes: 5 additions & 0 deletions FakeServer/ajax/XMLHttpRequest/xhr.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* @license
* Copyright 2021 KonghaYao 江夏尧 <dongzhongzhidong@qq.com>
* SPDX-License-Identifier: Apache-2.0
*/
// 使用不完全覆盖的方式,使用继承方式继承所有的属性
// 只在 send 方式调用的时候对其进行数据返回
import { pick } from 'lodash-es';
Expand Down
5 changes: 5 additions & 0 deletions FakeServer/ajax/fetch/fetch.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* @license
* Copyright 2021 KonghaYao 江夏尧 <dongzhongzhidong@qq.com>
* SPDX-License-Identifier: Apache-2.0
*/
const config = { proxy: null, silent: false };
let realFetch;

Expand Down
5 changes: 5 additions & 0 deletions FakeServer/ajax/fetch/src/INTERNALS.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
/**
* @license
* Copyright 2021 KonghaYao 江夏尧 <dongzhongzhidong@qq.com>
* SPDX-License-Identifier: Apache-2.0
*/
export const BODY = Symbol('Body internals');
export const RESPONSE = Symbol('Response internals');
44 changes: 4 additions & 40 deletions FakeServer/ajax/fetch/src/body.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
/**
* Body.js
*
* Body interface provides common methods for Request and Response
* @license
* Copyright 2021 KonghaYao 江夏尧 <dongzhongzhidong@qq.com>
* SPDX-License-Identifier: Apache-2.0
*/

import { isURLSearchParameters } from './utils/is.js';
import { BODY as INTERNALS } from './INTERNALS.js';

import { consumeBody } from './body/consumeBody.js';
/**
* Body mixin
*
* Ref: https://fetch.spec.whatwg.org/#body
*
* @param Stream body Readable stream
* @param Object opts Response options
* @return Void
*/

export default class Body {
constructor(body, { size = 0 } = {}) {
if (body === null) {
Expand Down Expand Up @@ -49,53 +40,26 @@ export default class Body {
return this[INTERNALS].disturbed;
}

/**
* Decode response as ArrayBuffer
*
* @return Promise
*/
async arrayBuffer() {
const blob = await consumeBody(this[INTERNALS]);
return blob.arrayBuffer();
}

/**
* Return raw response as Blob
*
* @return Promise
*/
async blob() {
return consumeBody(this[INTERNALS]);
}

/**
* Decode response as json
*
* @return Promise
*/
async json() {
const text = await this.text();
return JSON.parse(text || '{}');
}

/**
* Decode response as text
*
* @return Promise
*/
async text() {
const blob = await consumeBody(this[INTERNALS]);
return blob.text();
}

/**
* Decode response as buffer (non-spec api)
*
* @return Promise
*/
}

// In browsers, all properties are enumerable.
Object.defineProperties(Body.prototype, {
body: { enumerable: true },
bodyUsed: { enumerable: true },
Expand Down
8 changes: 3 additions & 5 deletions FakeServer/ajax/fetch/src/body/consumeBody.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/**
* Consume and convert an entire Body to a Buffer.
*
* Ref: https://fetch.spec.whatwg.org/#concept-body-consume-body
*
* @return Promise
* @license
* Copyright 2021 KonghaYao 江夏尧 <dongzhongzhidong@qq.com>
* SPDX-License-Identifier: Apache-2.0
*/
export async function consumeBody(data) {
// 标记为已经使用
Expand Down
21 changes: 5 additions & 16 deletions FakeServer/ajax/fetch/src/body/exactContentType.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,22 @@
import { isURLSearchParameters } from '../utils/is.js';
import { BODY as INTERNALS } from '../INTERNALS';
/**
* Performs the operation "extract a `Content-Type` value from |object|" as
* specified in the specification:
* https://fetch.spec.whatwg.org/#concept-bodyinit-extract
*
* This function assumes that instance.body is present.
*
* @param {any} body Any options.body input
* @return {string | null}
* @license
* Copyright 2021 KonghaYao 江夏尧 <dongzhongzhidong@qq.com>
* SPDX-License-Identifier: Apache-2.0
*/
import { isURLSearchParameters } from '../utils/is.js';
import { BODY as INTERNALS } from '../INTERNALS';
export const extractContentType = (body, request) => {
// Body is null or undefined
if (body === null) {
return null;
}

// Body is string
if (typeof body === 'string') {
return 'text/plain;charset=UTF-8';
}

// Body is a URLSearchParams
if (isURLSearchParameters(body)) {
return 'application/x-www-form-urlencoded;charset=UTF-8';
}

// Body is blob
if (body instanceof Blob) {
return body.type || null;
}
Expand All @@ -35,6 +25,5 @@ export const extractContentType = (body, request) => {
return `multipart/form-data; boundary=${request[INTERNALS].boundary}`;
}

// Body constructor defaults other things to string
return 'text/plain;charset=UTF-8';
};
22 changes: 3 additions & 19 deletions FakeServer/ajax/fetch/src/response.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
/**
* Response.js
*
* Response class provides content decoding
* @license
* Copyright 2021 KonghaYao 江夏尧 <dongzhongzhidong@qq.com>
* SPDX-License-Identifier: Apache-2.0
*/

import Body from './body.js';
import { extractContentType } from './body/exactContentType.js';
import { isRedirect } from './utils/is-redirect.js';
import { RESPONSE as INTERNALS } from './INTERNALS.js';
const Response = (globalThis.window && globalThis.window.Response) || class Null {};
/**
* Response class
*
* @param Stream body Readable stream
* @param Object opts Response options
* @return Void
*/
export default class fakeResponse extends Body {
constructor(body = null, options = {}) {
super(body, options);
Expand Down Expand Up @@ -48,10 +41,6 @@ export default class fakeResponse extends Body {
get status() {
return this[INTERNALS].status;
}

/**
* Convenience property representing if the request ended normally
*/
get ok() {
return this[INTERNALS].status >= 200 && this[INTERNALS].status < 300;
}
Expand Down Expand Up @@ -90,11 +79,6 @@ export default class fakeResponse extends Body {
});
}

/**
* @param {string} url The URL that the new response is to originate from.
* @param {number} status An optional status code for the response (e.g., 302.)
* @return {Response} A Response object.
*/
static redirect(url, status = 302) {
if (!isRedirect(status)) {
throw new RangeError('Failed to execute "redirect" on "response": Invalid status code');
Expand Down
11 changes: 5 additions & 6 deletions FakeServer/ajax/fetch/src/utils/is-redirect.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
const redirectStatus = new Set([301, 302, 303, 307, 308]);

/**
* Redirect code matching
*
* @param {number} code - Status code
* @return {boolean}
* @license
* Copyright 2021 KonghaYao 江夏尧 <dongzhongzhidong@qq.com>
* SPDX-License-Identifier: Apache-2.0
*/
const redirectStatus = new Set([301, 302, 303, 307, 308]);

export const isRedirect = (code) => {
return redirectStatus.has(code);
};
6 changes: 5 additions & 1 deletion FakeServer/ajax/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
/**
* @license
* Copyright 2021 KonghaYao 江夏尧 <dongzhongzhidong@qq.com>
* SPDX-License-Identifier: Apache-2.0
*/
// 在 fetch 的代理中只需要进行 fetch 的数据代理即可
//
export { mockFetch } from './fetch/fetch.js';
export { mockXHR } from './XMLHttpRequest/xhr.js';
5 changes: 5 additions & 0 deletions FakeServer/fakeServer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* @license
* Copyright 2021 KonghaYao 江夏尧 <dongzhongzhidong@qq.com>
* SPDX-License-Identifier: Apache-2.0
*/
import { match } from 'path-to-regexp';
import { Request, Response } from './src/Server.js';
import { Route, RouteMap } from './src/Route.js';
Expand Down
5 changes: 5 additions & 0 deletions FakeServer/src/Route.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* @license
* Copyright 2021 KonghaYao 江夏尧 <dongzhongzhidong@qq.com>
* SPDX-License-Identifier: Apache-2.0
*/
// 每一个 Route 只有一个 main 函数进行数据的返回
export class Route {
constructor({ matcher, name, path, callback, redirect = '' }) {
Expand Down
5 changes: 5 additions & 0 deletions FakeServer/src/Server.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* @license
* Copyright 2021 KonghaYao 江夏尧 <dongzhongzhidong@qq.com>
* SPDX-License-Identifier: Apache-2.0
*/
import { parseURL } from './parseURL.js';
export class Request {
constructor(path, options) {
Expand Down
6 changes: 3 additions & 3 deletions docs/assets/dependencies.drawio
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<mxfile>
<mxfile host="65bd71144e">
<diagram id="Uug-CgO5hbXQp728jUom" name="第 1 页">
<mxGraphModel dx="3087" dy="2497" 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">
<mxGraphModel dx="2906" dy="2258" 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="2" value="" style="shape=image;imageAspect=0;aspect=fixed;verticalLabelPosition=bottom;verticalAlign=top;image=https://cdn.jsdelivr.net/npm/js-spider/docs/assets/dependencies.svg;" parent="1" vertex="1">
<mxGeometry x="80" y="-350" width="2770.29" height="1010" as="geometry"/>
<mxGeometry x="80" y="-350" width="2770" height="1011" as="geometry"/>
</mxCell>
</root>
</mxGraphModel>
Expand Down
Loading

0 comments on commit bf4d071

Please sign in to comment.