MIRROR: javascript for ๐Ÿœ's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

file prefix res

+49
+1
include/utils.h
··· 15 15 uint64_t hash_key(const char *key, size_t len); 16 16 17 17 int is_typescript_file(const char *filename); 18 + char *resolve_js_file(const char *filename); 18 19 int ant_version(void *argtable[]); 19 20 20 21 void *try_oom(size_t size);
+4
src/main.c
··· 362 362 struct stat path_stat; 363 363 char *resolved_file = NULL; 364 364 365 + resolved_file = resolve_js_file(module_file); 366 + if (resolved_file) module_file = resolved_file; 367 + 365 368 if (stat(module_file, &path_stat) == 0 && S_ISDIR(path_stat.st_mode)) { 366 369 size_t len = strlen(module_file); 367 370 int has_slash = (len > 0 && module_file[len - 1] == '/'); 371 + if (resolved_file) free(resolved_file); 368 372 resolved_file = try_oom(len + 10 + (has_slash ? 0 : 1)); 369 373 sprintf(resolved_file, "%s%sindex.js", module_file, has_slash ? "" : "/"); 370 374 module_file = resolved_file;
+44
src/utils.c
··· 3 3 4 4 #include <string.h> 5 5 #include <stdint.h> 6 + #include <stdbool.h> 6 7 #include <pthread.h> 7 8 #include <argtable3.h> 9 + #include <sys/stat.h> 8 10 9 11 static char ant_semver_buf[32]; 10 12 static pthread_once_t ant_semver_once = PTHREAD_ONCE_INIT; 13 + 14 + static const char *const js_extensions[] = { 15 + ".js", ".ts", 16 + ".cjs", ".mjs", 17 + ".jsx", ".tsx", NULL 18 + }; 11 19 12 20 static void ant_semver_init(void) { 13 21 const char *s = ANT_VERSION; ··· 54 62 ext--; 55 63 56 64 return (strcmp(ext, ".ts") == 0 || strcmp(ext, ".mts") == 0 || strcmp(ext, ".cts") == 0); 65 + } 66 + 67 + static bool has_js_extension(const char *filename) { 68 + const char *dot = strrchr(filename, '.'); 69 + if (!dot) return false; 70 + for (const char *const *ext = js_extensions; *ext; ext++) { 71 + if (strcmp(dot, *ext) == 0) return true; 72 + } 73 + return false; 74 + } 75 + 76 + char *resolve_js_file(const char *filename) { 77 + extern bool esm_is_url(const char *path); 78 + if (esm_is_url(filename)) return strdup(filename); 79 + 80 + struct stat st; 81 + if (stat(filename, &st) == 0 && S_ISREG(st.st_mode)) { 82 + return strdup(filename); 83 + } 84 + 85 + if (has_js_extension(filename)) return NULL; 86 + size_t base_len = strlen(filename); 87 + 88 + for (const char *const *ext = js_extensions; *ext; ext++) { 89 + size_t ext_len = strlen(*ext); 90 + char *test_path = try_oom(base_len + ext_len + 1); 91 + 92 + memcpy(test_path, filename, base_len); 93 + memcpy(test_path + base_len, *ext, ext_len + 1); 94 + 95 + if (stat(test_path, &st) == 0 && S_ISREG(st.st_mode)) { 96 + return test_path; 97 + } free(test_path); 98 + } 99 + 100 + return NULL; 57 101 } 58 102 59 103 int ant_version(void *argtable[]) {