-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathapp-protocol.ts
97 lines (82 loc) · 2.81 KB
/
app-protocol.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
/*
Electric Scan
Copyright (C) 2019 Bishop Fox
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-------------------------------------------------------------------------
Implementing a custom protocol achieves two goals:
1) Allows us to use ES6 modules/targets for Angular
2) Avoids running the app in a file:// origin
*/
import * as fs from 'fs';
import * as path from 'path';
type ProtocolCallback = (arg0: { mimeType: string; charset: string; data: Buffer; }) => void;
const DIST_PATH = path.join(__dirname, 'dist');
export const scheme = 'app';
const mimeTypes = {
'.js': 'text/javascript',
'.mjs': 'text/javascript',
'.html': 'text/html',
'.htm': 'text/html',
'.json': 'application/json',
'.css': 'text/css',
'.svg': 'application/svg+xml',
'.ico': 'image/vnd.microsoft.icon',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.map': 'text/plain',
'.woff': 'application/x-font-woff',
'.woff2': 'font/woff2',
'.ttf': 'font/truetype',
'.bin': 'application/octet-stream'
};
function charset(mimeType: string): string {
return ['.html', '.htm', '.js', '.mjs'].some(m => m === mimeType) ? 'utf-8' : null;
}
function mime(filename: string): string {
const type = mimeTypes[path.extname(`${filename || ''}`).toLowerCase()];
return type ? type : null;
}
export function requestHandler(req: Electron.ProtocolRequest, next: ProtocolCallback) {
const reqUrl = new URL(req.url);
console.log(`[req] ${reqUrl.toString()}`);
if (!reqUrl.pathname.startsWith('/')) {
console.error(`[req-err] Request path must start with a '/'`);
return next({
mimeType: null,
charset: null,
data: null,
});
}
let reqPath = path.normalize(reqUrl.pathname);
if (reqPath === '/') {
reqPath = '/index.html';
}
const reqFilename = path.basename(reqPath);
fs.readFile(path.join(DIST_PATH, reqPath), (err, data) => {
const mimeType = mime(reqFilename);
if (!err && mimeType !== null) {
next({
mimeType: mimeType,
charset: charset(mimeType),
data: data
});
} else {
console.error(`[req-err] ${err}`);
next({
mimeType: null,
charset: null,
data: null,
});
}
});
}