1 | import { pathToRegexp } from 'path-to-regexp';
|
2 | export var Priority;
|
3 | (function (Priority) {
|
4 | Priority[Priority["LAST"] = 0] = "LAST";
|
5 | Priority[Priority["LATER"] = 1] = "LATER";
|
6 | Priority[Priority["MIDDLE"] = 2] = "MIDDLE";
|
7 | Priority[Priority["EARLY"] = 3] = "EARLY";
|
8 | Priority[Priority["FIRST"] = 4] = "FIRST";
|
9 | })(Priority || (Priority = {}));
|
10 | export class FABRouter {
|
11 | constructor() {
|
12 | this.pipeline = {
|
13 | [Priority.LAST]: [],
|
14 | [Priority.LATER]: [],
|
15 | [Priority.MIDDLE]: [],
|
16 | [Priority.EARLY]: [],
|
17 | [Priority.FIRST]: [],
|
18 | };
|
19 | }
|
20 | getPipeline() {
|
21 | return [
|
22 | ...this.pipeline[Priority.FIRST],
|
23 | ...this.pipeline[Priority.EARLY],
|
24 | ...this.pipeline[Priority.MIDDLE],
|
25 | ...this.pipeline[Priority.LATER],
|
26 | ...this.pipeline[Priority.LAST],
|
27 | ];
|
28 | }
|
29 | addToPipeline(responder, priority = Priority.MIDDLE) {
|
30 | this.pipeline[priority].push(responder);
|
31 | }
|
32 | on(route, responder, priority) {
|
33 | if (route === '*') {
|
34 |
|
35 | this.onAll((context) => responder({ ...context, params: {} }), priority);
|
36 | }
|
37 | else {
|
38 |
|
39 | const groups = [];
|
40 | const regexp = pathToRegexp(route, groups);
|
41 | this.addToPipeline(async (context) => {
|
42 | const { pathname } = context.url;
|
43 |
|
44 | const match = regexp.exec(pathname);
|
45 | if (match) {
|
46 | const params = {};
|
47 | groups.forEach((group, i) => {
|
48 | params[group.name] = match[i + 1];
|
49 | });
|
50 | return await responder({ ...context, params });
|
51 | }
|
52 | return undefined;
|
53 | }, priority);
|
54 | }
|
55 | }
|
56 | onAll(responder, priority) {
|
57 | this.addToPipeline(responder, priority);
|
58 | }
|
59 | interceptResponse(interceptor, priority = Priority.EARLY) {
|
60 | this.addToPipeline(async (context) => {
|
61 | return {
|
62 | interceptResponse: interceptor,
|
63 | };
|
64 | }, priority);
|
65 | }
|
66 | }
|
67 | class NoopCache {
|
68 | constructor() {
|
69 | this.set = async () => { };
|
70 | this.setJSON = async () => { };
|
71 | this.get = async () => undefined;
|
72 | this.getJSON = async () => undefined;
|
73 | this.getArrayBuffer = async () => undefined;
|
74 | this.getNumber = async () => undefined;
|
75 | this.getStream = async () => undefined;
|
76 | }
|
77 | }
|
78 | export class FABRuntime {
|
79 | constructor(metadata, file_metadata, context) {
|
80 | this.Metadata = metadata;
|
81 | this.FileMetadata = file_metadata;
|
82 | this.Router = new FABRouter();
|
83 | this.Cache = context.cache || new NoopCache();
|
84 | this.ServerContext = context;
|
85 | }
|
86 | static initialize(metadata, plugins, context) {
|
87 | const instance = new FABRuntime(metadata.plugin_metadata, metadata.file_metadata, context);
|
88 | plugins.forEach((plugin) => plugin(instance));
|
89 | return instance;
|
90 | }
|
91 | getPipeline() {
|
92 | return this.Router.getPipeline();
|
93 | }
|
94 | }
|
95 |
|
\ | No newline at end of file |