spooder 4.2.11 → 4.2.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +26 -2
- package/package.json +1 -1
- package/src/api.ts +16 -7
- package/src/cli.ts +2 -1
package/README.md
CHANGED
|
@@ -62,7 +62,8 @@ The `CLI` component of `spooder` is a global command-line tool for running serve
|
|
|
62
62
|
- [`safe(fn: Callable): Promise<void>`](#api-error-handling-safe)
|
|
63
63
|
- [API > Content](#api-content)
|
|
64
64
|
- [`parse_template(template: string, replacements: Record<string, string>, drop_missing: boolean): string`](#api-content-parse-template)
|
|
65
|
-
- [`generate_hash_subs(length: number, prefix: string): Promise<Record<string, string>>`](#api-content-generate-hash-subs)
|
|
65
|
+
- [`generate_hash_subs(length: number, prefix: string, hashes?: Record<string, string>): Promise<Record<string, string>>`](#api-content-generate-hash-subs)
|
|
66
|
+
- [`get_git_hashes(length: number): Promise<Record<string, string>>`](#api-content-get-git-hashes)
|
|
66
67
|
- [`apply_range(file: BunFile, request: Request): HandlerReturnType`](#api-content-apply-range)
|
|
67
68
|
- [API > State Management](#api-state-management)
|
|
68
69
|
- [`set_cookie(res: Response, name: string, value: string, options?: CookieOptions)`](#api-state-management-set-cookie)
|
|
@@ -1098,7 +1099,7 @@ parse_template(..., {
|
|
|
1098
1099
|
```
|
|
1099
1100
|
|
|
1100
1101
|
<a id="api-content-generate-hash-subs"></a>
|
|
1101
|
-
### 🔧 `generate_hash_subs(prefix: string): Promise<Record<string, string>>`
|
|
1102
|
+
### 🔧 `generate_hash_subs(length: number, prefix: string, hashes?: Record<string, string>): Promise<Record<string, string>>`
|
|
1102
1103
|
|
|
1103
1104
|
Generate a replacement table for mapping file paths to hashes in templates. This is useful for cache-busting static assets.
|
|
1104
1105
|
|
|
@@ -1142,6 +1143,29 @@ server.route('/test', (req, url) => {
|
|
|
1142
1143
|
});
|
|
1143
1144
|
```
|
|
1144
1145
|
|
|
1146
|
+
<a id="api-content-get-git-hashes"></a>
|
|
1147
|
+
### 🔧 ``get_git_hashes(length: number): Promise<Record<string, string>>``
|
|
1148
|
+
|
|
1149
|
+
Internally, `generate_hash_subs()` uses `get_git_hashes()` to retrieve the hash table from git. This function is exposed for convenience.
|
|
1150
|
+
|
|
1151
|
+
> [!IMPORTANT]
|
|
1152
|
+
> Internally `get_git_hashes()` uses `git ls-tree -r HEAD`, so the working directory must be a git repository.
|
|
1153
|
+
|
|
1154
|
+
```ts
|
|
1155
|
+
const hashes = await get_git_hashes(7);
|
|
1156
|
+
// { 'docs/project-logo.png': '754d9ea' }
|
|
1157
|
+
```
|
|
1158
|
+
|
|
1159
|
+
If you're using `generate_hash_subs()` and `get_git_hashes()` at the same time, it is more efficient to pass the result of `get_git_hashes()` directly to `generate_hash_subs()` to prevent redundant calls to git.
|
|
1160
|
+
|
|
1161
|
+
```ts
|
|
1162
|
+
const hashes = await get_git_hashes(7);
|
|
1163
|
+
const subs = await generate_hash_subs(7, undefined, hashes);
|
|
1164
|
+
|
|
1165
|
+
// hashes[0] -> { 'docs/project-logo.png': '754d9ea' }
|
|
1166
|
+
// subs[0] -> { 'hash=docs/project-logo.png': '754d9ea' }
|
|
1167
|
+
```
|
|
1168
|
+
|
|
1145
1169
|
<a id="api-apply-range"></a>
|
|
1146
1170
|
### 🔧 `apply_range(file: BunFile, request: Request): HandlerReturnType`
|
|
1147
1171
|
|
package/package.json
CHANGED
package/src/api.ts
CHANGED
|
@@ -170,7 +170,7 @@ export function parse_template(template: string, replacements: Replacements, dro
|
|
|
170
170
|
return result;
|
|
171
171
|
}
|
|
172
172
|
|
|
173
|
-
export async function
|
|
173
|
+
export async function get_git_hashes(length = 7): Promise<Record<string, string>> {
|
|
174
174
|
const cmd = ['git', 'ls-tree', '-r', 'HEAD'];
|
|
175
175
|
const process = Bun.spawn(cmd, {
|
|
176
176
|
stdout: 'pipe',
|
|
@@ -180,7 +180,7 @@ export async function generate_hash_subs(length = 7, prefix = 'hash='): Promise<
|
|
|
180
180
|
await process.exited;
|
|
181
181
|
|
|
182
182
|
if (process.exitCode as number > 0)
|
|
183
|
-
throw new Error('
|
|
183
|
+
throw new Error('get_git_hashes() failed, `' + cmd.join(' ') + '` exited with non-zero exit code.');
|
|
184
184
|
|
|
185
185
|
const stdout = await Bun.readableStreamToText(process.stdout as ReadableStream);
|
|
186
186
|
const hash_map: Record<string, string> = {};
|
|
@@ -188,11 +188,20 @@ export async function generate_hash_subs(length = 7, prefix = 'hash='): Promise<
|
|
|
188
188
|
const regex = /([^\s]+)\s([^\s]+)\s([^\s]+)\t(.+)/g;
|
|
189
189
|
let match: RegExpExecArray | null;
|
|
190
190
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
191
|
+
while (match = regex.exec(stdout))
|
|
192
|
+
hash_map[match[4]] = match[3].substring(0, length);
|
|
193
|
+
|
|
194
|
+
return hash_map;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export async function generate_hash_subs(length = 7, prefix = 'hash=', hashes?: Record<string, string>): Promise<Record<string, string>> {
|
|
198
|
+
const hash_map: Record<string, string> = {};
|
|
199
|
+
|
|
200
|
+
if (!hashes)
|
|
201
|
+
hashes = await get_git_hashes(length);
|
|
202
|
+
|
|
203
|
+
for (const [file, hash] of Object.entries(hashes))
|
|
204
|
+
hash_map[prefix + file] = hash;
|
|
196
205
|
|
|
197
206
|
return hash_map;
|
|
198
207
|
}
|
package/src/cli.ts
CHANGED
|
@@ -69,7 +69,8 @@ async function start_server() {
|
|
|
69
69
|
return;
|
|
70
70
|
|
|
71
71
|
const chunk_str = text_decoder.decode(chunk.value);
|
|
72
|
-
|
|
72
|
+
for (const chunk of chunk_str.split(/\r?\n/))
|
|
73
|
+
stream_history.push(chunk.trimEnd());
|
|
73
74
|
|
|
74
75
|
if (stream_history.length > crash_console_history)
|
|
75
76
|
stream_history.splice(0, stream_history.length - crash_console_history);
|