UNPKG

3.48 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const path_to_regexp_1 = require("path-to-regexp");
4var 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 = {}));
12class 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 // Make this an alias for .onAll, with an empty params object
37 this.onAll((context) => responder({ ...context, params: {} }), priority);
38 }
39 else {
40 // Otherwise compile the route and generate a responder
41 const groups = [];
42 const regexp = path_to_regexp_1.pathToRegexp(route, groups);
43 this.addToPipeline(async (context) => {
44 const { pathname } = context.url;
45 // Only execute if this request matches our route
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}
69exports.FABRouter = FABRouter;
70class 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}
81class 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}
98exports.FABRuntime = FABRuntime;
99//# sourceMappingURL=runtime.js.map
\No newline at end of file