-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile-loader.ts
166 lines (142 loc) · 5.91 KB
/
file-loader.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/// <reference lib="deno.unstable" />
import RequestHandler from "./functions/src/handler/main.ts";
import FileLoader from "./functions/src/file-loader/main.ts";
import server from "./functions/src/server/main.ts";
import getEnv from "./functions/src/utils/environmentVariables.ts";
import { SEPARATOR, basename, extname, join, dirname } from "https://deno.land/std/path/mod.ts";
import Cache from "./functions/src/utils/withCache.ts";
import axionDenoConfig from "./deno.json" with { type: "json" };
self?.addEventListener("unhandledrejection", event => {
event.preventDefault();
console.log('FILE LOADER UNHANDLED ERROR', event)
// self.postMessage({
// message: event.reason.message,
// stack: event.reason.stack,
// });
});
let axionConfigs = new Map<string, string>();
let denoConfigs = new Map<string, any>();
const env = await getEnv();
server({
requestHandler: async (req: Request) => {
const debug = env.DEBUG === 'true';
let useCache;
const authorizationEncoded = req.headers.get('authorization')?.slice(6);
let [username, password] = authorizationEncoded ? atob(authorizationEncoded).split(':') : [];
debug && console.log('Received request in File Loader from', req.url, username, password);
try {
useCache = JSON.parse(env.USE_CACHE || 'true');
} catch (_) {
useCache = true;
}
const [provider, org, repo, branch, environment] = username?.split('--') || [];
if (!provider) {
username = 'local';
}
const fileLoaderWithAxionConfig = async ({ config, modules }) => async (params, res) => {
const axionConfigUrl = new URL('/axion.config.json', params.url);
const denoConfigUrl = new URL('/deno.json', params.url);
const urlWithBasicAuth = new URL(params.url);
if (username) {
axionConfigUrl.username = username;
denoConfigUrl.username = username;
urlWithBasicAuth.username = username;
}
if (password) {
axionConfigUrl.password = password;
denoConfigUrl.password = password;
urlWithBasicAuth.password = password;
};
axionConfigUrl.search = '';
denoConfigUrl.search = '';
axionConfigUrl.pathname = '/axion.config.json';
denoConfigUrl.pathname = '/deno.json';
let axionConfig = axionConfigs.get(axionConfigUrl.origin);
let denoConfig = denoConfigs.get(denoConfigUrl.origin);
const responseMock = Object.entries(res).reduce((acc, [key, value]) => {
acc[key] = (() => { });
return acc;
}, {})
const fileLoader = FileLoader({ config, modules });
if (!axionConfig) {
debug && console.log('axion.config.json not found in cache for', axionConfigUrl.origin, 'fetching from server...')
axionConfig = await fileLoader({
queryParams: {},
headers: { 'content-type': 'text/plain; charset=utf-8' },
pathname: axionConfigUrl.pathname,
url: axionConfigUrl,
}, responseMock);
axionConfig = JSON.parse(axionConfig || '{}')
axionConfigs.set(axionConfigUrl.href, axionConfig);
}
if (!denoConfig) {
debug && console.log('deno.json not found in cache for', denoConfigUrl.origin, 'fetching from server...')
denoConfig = await fileLoader({
queryParams: {},
headers: { 'content-type': 'text/plain; charset=utf-8' },
pathname: '/deno.json',
url: denoConfigUrl,
}, responseMock);
denoConfig = JSON.parse(denoConfig || '{}')
const nodeConfig = await fileLoader({
queryParams: {},
headers: { 'content-type': 'text/plain; charset=utf-8' },
pathname: '/package.json',
url: new URL('/package.json', denoConfigUrl),
}, responseMock);
const nodeConfigJson = JSON.parse(nodeConfig || '{}');
denoConfig.imports = denoConfig?.imports || {};
Object.entries(nodeConfigJson?.dependencies || {}).forEach(([key, value]) => {
if (value.startsWith('http') || value.startsWith('file') || value.startsWith('npm:') || value.startsWith('node:')) {
denoConfig.imports[key] = value;
} else {
denoConfig.imports[key] = `npm:${key}@${value}`;
}
});
denoConfig.imports = { ...axionDenoConfig.imports, ...denoConfig.imports };
denoConfig.scopes = { ...axionDenoConfig.scopes, ...denoConfig.scopes };
denoConfigs.set(denoConfigUrl.href, denoConfig);
}
return FileLoader({
config: { ...config, ...axionConfig },
modules
})({ ...params, url: urlWithBasicAuth, data: { ...params?.data, denoConfig: { ...denoConfig, ...params?.data?.denoConfig, } } }, res);
}
return RequestHandler({
middlewares: {},
pipes: {},
modules: {
path: { SEPARATOR, basename, extname, join, dirname }
},
handlers: {
"/(.*)+": await fileLoaderWithAxionConfig({
config: {
dirEntrypoint: env.DIR_ENTRYPOINT || "index",
debug,
useCache,
bustCacheAfter: env.BUST_CACHE_AFTER,
cachettl: Number(env.CACHE_TTL) || 1000 * 60 * 10,
loaderType: provider || env.DEFAULT_LOADER_TYPE || 'local', //(gitInfo?.owner && gitInfo?.repo) ? 'github' : (env.DEFAULT_LOADER_TYPE || "local"),
owner: org || env.GIT_OWNER,
repo: repo || env.GIT_REPO,
branch: branch || env.GIT_BRANCH, // or any other branch you want to fetch files froM
environment: environment || env.ENV,
apiKey: password || env.GIT_API_KEY,
},
modules: {
path: {
SEPARATOR, basename, extname, join, dirname
},
withCache: await Cache(username, 'data/local')
}
}),
serializers: {}
}
})(req);
},
config: {
PORT: env.FILE_LOADER_PORT || 9000,
verbose: false
}
});
self?.postMessage && self?.postMessage({ message: { 'status': 'ok' } });