-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
132 lines (112 loc) · 3.7 KB
/
index.js
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
const core = require('@actions/core');
const github = require('@actions/github');
const fs = require('fs-extra');
const semver = require('semver');
const dedent = require('dedent');
const express = require('express');
const app = express();
const path = require('path');
const rateLimit = require('express-rate-limit');
const sanitizeFilename = require('sanitize-filename');
const PORT = process.env.PORT || 3001;
const SLDS_DIR = '/node_modules/@salesforce-ux/design-system/assets';
const ROOT_DIR = '/var/www/'; // Define the root directory for file serving
const limiter = rateLimit({
windowMs: 15 * 60 * 1000,
max: 100,
handler: (req, res, next, options) => {
res.status(options.statusCode).json({
error: 'Too Many Requests',
message: 'Please try again later.'
});
}
});
app.use(limiter);
app.use('/slds', express.static(__dirname + SLDS_DIR));
app.get('/', function (req, res) {
res.sendFile(path.resolve('index.html'), (err) => {
if (err) {
console.error(err);
res.status(500).send('Error serving index.html');
}
});
});
app.get('/:filename', function (req, res) {
let filename = sanitizeFilename(req.params.filename); // Sanitize the filename
const requestedPath = path.resolve(ROOT_DIR, filename); // Normalize the requested path
if (!isValidPath(requestedPath)) {
res.status(400).send('Invalid path');
return;
}
fs.stat(requestedPath, (err, stats) => {
if (err || !stats.isFile()) {
console.error(err);
res.status(404).send('Invalid or non-existent path');
return;
}
res.sendFile(requestedPath, (err) => {
if (err) {
console.error(err);
res.status(500).send('Error serving file');
}
});
});
});
app.listen(PORT, function () {
console.log(`App listening on port ${PORT}`);
});
const main = async () => {
try {
const { draft: isDraft, prerelease: isPrerelease, tag_name: gitTag } = github.context.payload.release;
const gitTagWithoutV = gitTag.slice(1);
const packageJson = await fs.readJson('./package.json');
const packageJsonVersion = packageJson?.version;
if (isDraft) {
core.setFailed('Release is a draft. Skip publish.');
return;
}
if (!packageJsonVersion) {
core.setFailed('Package.json is missing version.');
return;
}
if (!gitTag.startsWith('v')) {
core.setFailed('Release git tag does not start with `v`, ie. `v1.2.3`.');
return;
}
if (gitTagWithoutV !== packageJsonVersion) {
core.setFailed(
dedent(`
Release git tag does not match package.json version.
Release git tag: ${gitTagWithoutV}
Package.json version: ${packageJsonVersion}
`)
);
return;
}
if (!semver.valid(gitTagWithoutV)) {
core.setFailed('Release git tag and package.json versions are not valid semver.');
return;
}
const semverPrerelease = semver.prerelease(gitTagWithoutV);
const hasSemverPrerelease = semverPrerelease !== null;
if (isPrerelease && !hasSemverPrerelease) {
core.setFailed(
'Release in GitHub is marked as `pre-release`, but release git tag and package.json versions do not follow pre-release format, ie. `1.2.3-beta.1`'
);
return;
}
if (!isPrerelease && hasSemverPrerelease) {
core.setFailed(
'Release git tag and package.json versions follow pre-release format, ie. `1.2.3-beta.1`, but release in GitHub is not marked as `pre-release`.'
);
return;
}
} catch (error) {
core.setFailed(error.message);
}
};
const isValidPath = (requestedPath) => {
// Ensure that the requested path is within the root directory
return requestedPath.startsWith(path.resolve(ROOT_DIR));
};
main();