swpp-backends 0.0.1-alpha

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,101 @@
1
+ import { Request, Response } from 'node-fetch';
2
+ import { SwppConfig } from './SwppConfig';
3
+ /**
4
+ * 读取最后一次构建的 rules
5
+ *
6
+ * **执行该函数前必须调用过 [loadRules]**
7
+ */
8
+ export declare function readRules(): SwppRules;
9
+ /**
10
+ * 添加一个 rules 映射事件,这个事件允许用户修改 rules 的内容
11
+ *
12
+ * 执行时按照注册的顺序执行
13
+ */
14
+ export declare function addRulesMapEvent(mapper: (rules: any) => void): void;
15
+ /**
16
+ * 加载 rules 文件
17
+ * @param root 项目根目录
18
+ * @param fileName rules 文件名称
19
+ * @param selects 附加的可选目录,优先级低于 [root]
20
+ */
21
+ export declare function loadRules(root: string, fileName: string, selects: string[]): SwppRules;
22
+ export interface SwppRules {
23
+ /** 配置项 */
24
+ config: SwppConfig;
25
+ /** 缓存规则 */
26
+ cacheRules?: {
27
+ [propName: string]: CacheRules;
28
+ };
29
+ /**
30
+ * 修改 Request
31
+ * @param request 原始 request
32
+ * @param $eject 用于访问通过 [ejectValues] 函数插入的变量,变量名必须为 `$eject`
33
+ * @return 修改后的 Request,不修改的话返回 null 或不返回数据
34
+ */
35
+ modifyRequest?: (request: Request, $eject: any) => Request | undefined;
36
+ /**
37
+ * 获取一个 URL 对应的多个 CDN 的 URL
38
+ *
39
+ * 竞速时除了 URL 外所有参数保持一致
40
+ *
41
+ * @param url 原始 URL
42
+ * @return {?string[]} 返回值不包含则表示去除对原始 URL 地访问。返回 undefined 表示该 URL 不启用竞速
43
+ */
44
+ getRaceUrls?: (url: string) => string[] | undefined;
45
+ /**
46
+ * 获取一个 URL 对应的 URL 列表
47
+ *
48
+ * 访问顺序按列表顺序,所有 URL 访问时参数一致
49
+ *
50
+ * @param url 原始 URL
51
+ * @return {?SpareURLs} 返回 null 或不反悔表示对该 URL 不启用备用 URL 功能
52
+ */
53
+ getSpareUrls?: (url: string) => SpareURLs | undefined;
54
+ /**
55
+ * 判断是否阻塞指定响应
56
+ * @return {boolean} 返回 true 表示阻塞,false 表示不阻塞
57
+ */
58
+ blockRequest?: (url: URL) => boolean;
59
+ /** 插入到 sw 但不在 node 中执行的代码 */
60
+ afterJoin?: VoidFunction;
61
+ /** 插入到 sw 但不在 node 中执行的代码,框架主题禁止覆盖该项 */
62
+ afterTheme?: VoidFunction;
63
+ /** 获取要插入到 sw 中的变量和常量 */
64
+ ejectValues?: (framework: any, rules: SwppRules) => {
65
+ [propName: string]: EjectValue;
66
+ };
67
+ /** 允许插入到 sw 的值 */
68
+ external?: string[];
69
+ /**
70
+ * 向指定的 request 发起网络请求(GET)
71
+ *
72
+ * **注意:声明该项后 swpp 内置的“CDN 竞速”“备用 URL”都将失效**
73
+ *
74
+ * @param request 请求信息
75
+ * @param banCache 是否禁用缓存
76
+ * @param spare 备用 URL
77
+ */
78
+ fetchFile?: (request: Request, banCache: boolean, spare?: SpareURLs) => Promise<Response>;
79
+ }
80
+ export interface CacheRules {
81
+ /** 符合该规则的缓存在进行全局清理时是否清除 */
82
+ clean: boolean;
83
+ /** 是否检查 URL 参数(问号及问号之后的内容) */
84
+ search?: boolean;
85
+ /**
86
+ * 规则匹配器
87
+ * @param url 链接的 URL 对象(对象包括 hash 和 search,但禁止使用 hash,search 为 false 或留空时禁止使用 search)
88
+ * @param $eject 用于访问通过 [ejectValues] 函数插入的变量,变量名必须为 `$eject`
89
+ */
90
+ match: (url: URL, $eject?: any) => boolean;
91
+ }
92
+ export interface SpareURLs {
93
+ /** 超时时间 */
94
+ timeout: number;
95
+ /** URL 列表 */
96
+ list: string[];
97
+ }
98
+ export interface EjectValue {
99
+ prefix: string;
100
+ value: string | number | boolean | bigint | object | string[] | number[] | boolean[] | bigint[] | object[];
101
+ }
@@ -0,0 +1,41 @@
1
+ export declare function error(type: string, message: string): void;
2
+ export declare function warn(type: string, message: string): void;
3
+ export interface EjectCache {
4
+ strValue: string;
5
+ nodeEject: any;
6
+ }
7
+ /**
8
+ * 获取 eject values
9
+ *
10
+ * + **执行该函数前必须调用过 [loadRules]**
11
+ *
12
+ * @param framework 框架对象
13
+ */
14
+ export declare function calcEjectValues(framework: any): void;
15
+ /**
16
+ * 读取最近的已计算的 eject 数据
17
+ *
18
+ * + **执行该函数前必须调用过 [loadRules]**
19
+ * + **执行该函数前必须调用过 [calcEjectValues]**
20
+ */
21
+ export declare function readEjectData(): EjectCache;
22
+ /**
23
+ * 获取指定值的 js 源码表达形式
24
+ * @param obj 要转换的对象
25
+ * @param typeChecker 类型检查器,用于筛除不希望映射的类型
26
+ * @param whiteList 白名单,当 obj 为 Object 时将只转换在白名单中的值(不会传递)
27
+ * @param isTop 是否为顶层元素,为 true 且 obj 为 Object 时将去除最外层的大括号,改为 let(不会传递)
28
+ */
29
+ export declare function getSource(obj: any, typeChecker?: ((name: string) => boolean) | undefined, whiteList?: string[] | undefined, isTop?: boolean): string;
30
+ /**
31
+ * 拉取文件
32
+ *
33
+ * **调用该函数前必须调用过 [loadRules]**
34
+ */
35
+ export declare function fetchFile(link: string): Promise<import("node-fetch").Response>;
36
+ /**
37
+ * 替换编译期的 URL(CDN 竞速)
38
+ *
39
+ * **调用该函数前必须调用过 [loadRules]**
40
+ */
41
+ export declare function replaceDevRequest(link: string): string[] | string;