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.

metadata based remote esm

+23 -33
+23 -33
src/esm/remote.c
··· 133 133 return cache_path; 134 134 } 135 135 136 - static char *esm_read_cache(const char *cache_path, const char *url, size_t *out_len) { 136 + static char *esm_read_cache(const char *cache_path, size_t *out_len) { 137 137 FILE *fp = fopen(cache_path, "rb"); 138 138 if (!fp) return NULL; 139 139 ··· 151 151 fclose(fp); 152 152 content[size] = '\0'; 153 153 154 - char *newline = strchr(content, '\n'); 155 - if (!newline) { 156 - free(content); 157 - return NULL; 158 - } 159 - 160 - size_t url_len = newline - content; 161 - if (strncmp(content, url, url_len) != 0 || strlen(url) != url_len) { 162 - free(content); 163 - return NULL; 164 - } 165 - 166 - char *body = newline + 1; 167 - size_t body_len = size - (body - content); 168 - 169 - char *result = malloc(body_len + 1); 170 - if (!result) { 171 - free(content); 172 - return NULL; 173 - } 174 - 175 - memcpy(result, body, body_len); 176 - result[body_len] = '\0'; 177 - free(content); 178 - 179 - if (out_len) *out_len = body_len; 180 - return result; 154 + if (out_len) *out_len = (size_t)size; 155 + return content; 181 156 } 182 157 183 158 static void esm_mkdir_recursive(char *path) { ··· 203 178 } 204 179 205 180 esm_mkdir_recursive(dir); 206 - free(dir); 207 181 208 182 FILE *fp = fopen(cache_path, "wb"); 209 - if (!fp) return; 210 - 211 - fprintf(fp, "%s\n", url); 183 + if (!fp) { free(dir); return; } 212 184 fwrite(content, 1, len, fp); 213 185 fclose(fp); 186 + 187 + size_t meta_len = strlen(dir) + 16; 188 + char *meta_path = malloc(meta_len); 189 + if (meta_path) { 190 + snprintf(meta_path, meta_len, "%s/metadata.bin", dir); 191 + FILE *mfp = fopen(meta_path, "ab"); 192 + if (mfp) { 193 + uint64_t hash = hash_key(url, strlen(url)); 194 + uint16_t url_len = (uint16_t)strlen(url); 195 + fwrite(&url_len, sizeof(url_len), 1, mfp); 196 + fwrite(url, 1, url_len, mfp); 197 + fwrite(&hash, sizeof(hash), 1, mfp); 198 + fclose(mfp); 199 + } 200 + free(meta_path); 201 + } 202 + 203 + free(dir); 214 204 } 215 205 216 206 char *esm_fetch_url(const char *url, size_t *out_len, char **out_error) { 217 207 char *cache_path = esm_get_cache_path(url); 218 208 if (cache_path) { 219 - char *cached = esm_read_cache(cache_path, url, out_len); 209 + char *cached = esm_read_cache(cache_path, out_len); 220 210 if (cached) { free(cache_path); return cached; } 221 211 } 222 212