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