Skip to content

Commit 0cce5ad

Browse files
committed
add path env variables and ** aliases
1 parent 44a662a commit 0cce5ad

3 files changed

Lines changed: 84 additions & 31 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ in both file types (dynamic served and services), you can use all node/bun metho
118118
- `async data_load_watch(path, callback(content))`: executes callback first and on every change
119119
- `async data_save(path, content)`: writes the content to the file in data directory
120120

121+
## environment variables
122+
- `rtjscomp_path_data`: path to data directory, default is `./data`
123+
- `rtjscomp_path_public`: path to public directory, default is `./public`
124+
121125
## supported environments
122126

123127
- node v10.1.0 or higher

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rtjscomp",
3-
"version": "0.9.18",
3+
"version": "0.10.0",
44
"description": "php-like server but with javascript",
55
"repository": {
66
"type": "git",

rtjscomp.js

Lines changed: 79 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ const IPS_PRIVATE = /^(127\.|10\.|192\.168\.|::1|fc00:|fe80:)/;
2929
const IS_BUN = typeof Bun !== 'undefined';
3030
const LINENUMBER_REG = /:([0-9]+)[\):]/;
3131
const PATH_CONFIG = 'config/';
32-
const PATH_DATA = 'data/';
33-
const PATH_PUBLIC = 'public/';
32+
const PATH_DATA = (process.env.rtjscomp_path_data || 'data') + '/';
33+
const PATH_PUBLIC = (process.env.rtjscomp_path_public || 'public') + '/';
3434
const PLUS_REG = /\+/g;
3535
const RESOLVE_OPTIONS = {paths: [require('path').resolve()]};
3636
const SERVICE_REQUIRE_REG = /\bservice_require\(([^)]*)\)/g;
@@ -1022,14 +1022,24 @@ const request_handle = async (request, response, https) => {
10221022
template: for (const template of templates) {
10231023
const template_path = template.path_split;
10241024
const template_path_length = template_path.length;
1025-
if (template_path_length !== path_split.length) continue;
1025+
if (
1026+
template_path_length > path_split.length ||
1027+
template_path_length < path_split.length &&
1028+
template_path[template_path_length - 1] !== '**'
1029+
) continue;
10261030
const params = {};
10271031
for (let i = 0; i < template_path_length; ++i) {
10281032
if (template_path[i].charCodeAt(0) === 42) {
10291033
if (template_path[i].length > 1) {
1030-
params[
1031-
template_path[i].slice(1)
1032-
] = path_split[i];
1034+
if (template_path[i].charCodeAt(1) === 42) {
1035+
params.path_match = path_split.slice(i).join('/');
1036+
break;
1037+
}
1038+
else {
1039+
params[
1040+
template_path[i].slice(1)
1041+
] = path_split[i];
1042+
}
10331043
}
10341044
}
10351045
else if (template_path[i] !== path_split[i]) {
@@ -1918,6 +1928,7 @@ await file_keep_new('rtjscomp.json', data => {
19181928
throw 'must contain {}';
19191929
}
19201930

1931+
// read+validate
19211932
const compression_enabled_new = get_prop_bool(data, 'compress', true);
19221933
const gzip_level_new = get_prop_uint(data, 'gzip_level', 3);
19231934
const log_verbose_new = get_prop_bool(data, 'log_verbose', log_verbose_flag);
@@ -1934,6 +1945,7 @@ await file_keep_new('rtjscomp.json', data => {
19341945
const upload_limit_new = get_prop_uint(data, 'upload_limit', 10);
19351946
const zstd_level_new = get_prop_uint(data, 'zstd_level', 3);
19361947

1948+
// validate
19371949
if (data) {
19381950
const keys_left = Object.keys(data);
19391951
if (keys_left.length > 0) {
@@ -1943,49 +1955,68 @@ await file_keep_new('rtjscomp.json', data => {
19431955
if (gzip_level_new > 9) {
19441956
throw 'gzip_level > 9';
19451957
}
1958+
if (path_aliases_new) {
1959+
for (const [key, value] of path_aliases_new) {
1960+
config_path_check(key, true);
1961+
config_path_check(value);
1962+
if (key.includes('*')) {
1963+
const path_split = key.split('/');
1964+
if (
1965+
path_split.includes('**') &&
1966+
path_split.indexOf('**') !== path_split.length - 1
1967+
) {
1968+
throw '** only allowed at path end';
1969+
}
1970+
for (const part of path_split) {
1971+
if (
1972+
part !== '**' &&
1973+
part.indexOf('*', 1) > 0
1974+
) {
1975+
throw '* only allowed at path segment start';
1976+
}
1977+
}
1978+
}
1979+
}
1980+
}
1981+
if (path_ghosts_new) {
1982+
for (const key of path_ghosts_new) {
1983+
config_path_check(key, true);
1984+
}
1985+
}
1986+
if (path_hiddens_new) {
1987+
for (const key of path_hiddens_new) {
1988+
config_path_check(key, true);
1989+
}
1990+
}
1991+
if (path_statics_new) {
1992+
for (const key of path_statics_new) {
1993+
config_path_check(key, true);
1994+
}
1995+
}
19461996
if (
19471997
port_http_new > 65535 ||
19481998
port_https_new > 65535
19491999
) {
19502000
throw 'port > 65535';
19512001
}
2002+
for (const key of services_new) {
2003+
config_path_check(key);
2004+
}
19522005
if (zstd_level_new > 19) {
19532006
throw 'zstd_level > 19';
19542007
}
19552008

2009+
// apply
19562010
compression_enabled = compression_enabled_new;
19572011
GZIP_OPTIONS.level = compression_enabled ? gzip_level_new : 0;
19582012
ZSTD_OPTIONS.params[ZSTD_c_compressionLevel] = zstd_level_new;
19592013
log_verbose = log_verbose_new;
19602014
upload_limit = upload_limit_new * 1024 * 1024;
1961-
if (path_ghosts_new) {
1962-
path_ghosts.clear();
1963-
for (const key of path_ghosts_new) {
1964-
config_path_check(key);
1965-
path_ghosts.add(key);
1966-
}
1967-
}
1968-
if (path_hiddens_new) {
1969-
path_hiddens.clear();
1970-
for (const key of path_hiddens_new) {
1971-
config_path_check(key);
1972-
path_hiddens.add(key);
1973-
}
1974-
}
1975-
if (path_statics_new) {
1976-
path_statics.clear();
1977-
for (const key of path_statics_new) {
1978-
config_path_check(key);
1979-
path_statics.add(key);
1980-
}
1981-
}
19822015
if (path_aliases_new) {
19832016
path_aliases.clear();
19842017
path_aliases_reverse.clear();
19852018
path_aliases_templates.clear();
19862019
for (const [key, value] of path_aliases_new) {
1987-
config_path_check(key, true);
1988-
config_path_check(value);
19892020
if (key.includes('*')) {
19902021
const path_split = key.split('/');
19912022
const first = path_split.shift();
@@ -2012,13 +2043,32 @@ await file_keep_new('rtjscomp.json', data => {
20122043
}
20132044
}
20142045
}
2046+
if (path_ghosts_new) {
2047+
path_ghosts.clear();
2048+
for (const key of path_ghosts_new) {
2049+
path_ghosts.add(key);
2050+
}
2051+
}
2052+
if (path_hiddens_new) {
2053+
path_hiddens.clear();
2054+
for (const key of path_hiddens_new) {
2055+
path_hiddens.add(key);
2056+
}
2057+
}
2058+
if (path_statics_new) {
2059+
path_statics.clear();
2060+
for (const key of path_statics_new) {
2061+
path_statics.add(key);
2062+
}
2063+
}
20152064
if (type_dynamics_new) {
20162065
type_dynamics.clear();
20172066
for (const key of type_dynamics_new) {
20182067
type_dynamics.add(key);
20192068
}
20202069
}
20212070
if (type_mimes_new) {
2071+
// we keep old keys, only override
20222072
for (const [key, value] of type_mimes_new) {
20232073
type_mimes.set(key, value);
20242074
}
@@ -2030,7 +2080,6 @@ await file_keep_new('rtjscomp.json', data => {
20302080
}
20312081
}
20322082

2033-
for (const path of services_new) config_path_check(path);
20342083
const promises = [
20352084
services_list_react(
20362085
services_new

0 commit comments

Comments
 (0)