spooder 6.1.92 → 6.1.94
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 +13 -2
- package/bun.lock +1 -1
- package/package.json +1 -1
- package/src/api.ts +12 -9
package/README.md
CHANGED
|
@@ -599,6 +599,7 @@ log_list(input: any[], delimiter = ', ');
|
|
|
599
599
|
|
|
600
600
|
// http
|
|
601
601
|
http_serve(port: number, hostname?: string): Server;
|
|
602
|
+
server.port: number;
|
|
602
603
|
server.stop(immediate: boolean): Promise<void>;
|
|
603
604
|
|
|
604
605
|
// cookies
|
|
@@ -840,13 +841,14 @@ IPC_TARGET.BROADCAST; // broadcast to all other instances
|
|
|
840
841
|
## API > HTTP
|
|
841
842
|
|
|
842
843
|
### `http_serve(port: number, hostname?: string): Server`
|
|
843
|
-
Bootstrap a server on the specified port (and optional hostname).
|
|
844
|
+
Bootstrap a server on the specified port (and optional hostname). Pass `0` for the port to assign a random available port, which can then be retrieved via `server.port`.
|
|
844
845
|
|
|
845
846
|
```ts
|
|
846
847
|
import { serve } from 'spooder';
|
|
847
848
|
|
|
848
849
|
const server = http_serve(8080); // port only
|
|
849
850
|
const server = http_serve(3000, '0.0.0.0'); // optional hostname
|
|
851
|
+
const server = http_serve(0); // random port, retrieve via server.port
|
|
850
852
|
```
|
|
851
853
|
|
|
852
854
|
By default, the server responds with:
|
|
@@ -879,6 +881,15 @@ await server.stop(false);
|
|
|
879
881
|
// do something now all connections are done
|
|
880
882
|
```
|
|
881
883
|
|
|
884
|
+
### 📖 `server.port`
|
|
885
|
+
|
|
886
|
+
Returns the port the server is listening on. Useful when `0` is passed to `http_serve()` for a random port assignment.
|
|
887
|
+
|
|
888
|
+
```ts
|
|
889
|
+
const server = http_serve(0);
|
|
890
|
+
console.log(`Server listening on port ${server.port}`);
|
|
891
|
+
```
|
|
892
|
+
|
|
882
893
|
### Routing
|
|
883
894
|
|
|
884
895
|
### 🔧 `server.route(path: string, handler: RequestHandler)`
|
|
@@ -1734,7 +1745,7 @@ global_subs: {
|
|
|
1734
1745
|
build_time: async () => {
|
|
1735
1746
|
// Example: fetch build timestamp from git
|
|
1736
1747
|
const process = Bun.spawn(['git', 'log', '-1', '--format=%ct']);
|
|
1737
|
-
const output = await
|
|
1748
|
+
const output = await new Response(process.stdout).text();
|
|
1738
1749
|
return new Date(parseInt(output.trim()) * 1000).toISOString();
|
|
1739
1750
|
},
|
|
1740
1751
|
|
package/bun.lock
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"packages": {
|
|
13
13
|
"@types/bun": ["@types/bun@1.3.5", "", { "dependencies": { "bun-types": "1.3.5" } }, "sha512-RnygCqNrd3srIPEWBd5LFeUYG7plCoH2Yw9WaZGyNmdTEei+gWaHqydbaIRkIkcbXwhBT94q78QljxN0Sk838w=="],
|
|
14
14
|
|
|
15
|
-
"@types/node": ["@types/node@25.0.
|
|
15
|
+
"@types/node": ["@types/node@25.0.6", "", { "dependencies": { "undici-types": "~7.16.0" } }, "sha512-NNu0sjyNxpoiW3YuVFfNz7mxSQ+S4X2G28uqg2s+CzoqoQjLPsWSbsFFyztIAqt2vb8kfEAsJNepMGPTxFDx3Q=="],
|
|
16
16
|
|
|
17
17
|
"bun-types": ["bun-types@1.3.5", "", { "dependencies": { "@types/node": "*" } }, "sha512-inmAYe2PFLs0SUbFOWSVD24sg1jFlMPxOjOSSCYqUgn4Hsc3rDc7dFvfVYjFPNHtov6kgUeulV4SxbuIV/stPw=="],
|
|
18
18
|
|
package/package.json
CHANGED
package/src/api.ts
CHANGED
|
@@ -821,7 +821,7 @@ export class ErrorWithMetadata extends Error {
|
|
|
821
821
|
else if (typeof value === 'function')
|
|
822
822
|
resolved_value = await value();
|
|
823
823
|
else if (value instanceof ReadableStream)
|
|
824
|
-
resolved_value = await
|
|
824
|
+
resolved_value = await new Response(value).text();
|
|
825
825
|
|
|
826
826
|
if (typeof resolved_value === 'string' && resolved_value.includes('\n'))
|
|
827
827
|
resolved_value = resolved_value.split(/\r?\n/);
|
|
@@ -970,12 +970,10 @@ export async function parse_template(template: string, replacements: Replacement
|
|
|
970
970
|
}
|
|
971
971
|
}
|
|
972
972
|
|
|
973
|
-
|
|
974
|
-
return match;
|
|
975
|
-
|
|
973
|
+
// missing/undefined items treated as empty array (no output)
|
|
976
974
|
return '';
|
|
977
975
|
});
|
|
978
|
-
|
|
976
|
+
|
|
979
977
|
// Parse t-if tags
|
|
980
978
|
const if_regex = /<t-if\s+test="([^"]+)"\s*>(.*?)<\/t-if>/gs;
|
|
981
979
|
result = await replace_async(result, if_regex, async (match, condition_key, if_content) => {
|
|
@@ -1119,9 +1117,9 @@ export async function git_get_hashes(length = 7): Promise<Record<string, string>
|
|
|
1119
1117
|
await process.exited;
|
|
1120
1118
|
|
|
1121
1119
|
if (process.exitCode as number > 0)
|
|
1122
|
-
|
|
1120
|
+
return {};
|
|
1123
1121
|
|
|
1124
|
-
const stdout = await
|
|
1122
|
+
const stdout = await new Response(process.stdout).text();
|
|
1125
1123
|
const hash_map: Record<string, string> = {};
|
|
1126
1124
|
|
|
1127
1125
|
const regex = /([^\s]+)\s([^\s]+)\s([^\s]+)\t(.+)/g;
|
|
@@ -1141,7 +1139,7 @@ export function git_get_hashes_sync(length = 7): Record<string, string> {
|
|
|
1141
1139
|
});
|
|
1142
1140
|
|
|
1143
1141
|
if (process.exitCode > 0)
|
|
1144
|
-
|
|
1142
|
+
return {};
|
|
1145
1143
|
|
|
1146
1144
|
const stdout = process.stdout.toString();
|
|
1147
1145
|
const hash_map: Record<string, string> = {};
|
|
@@ -1819,7 +1817,7 @@ export function http_serve(port: number, hostname?: string) {
|
|
|
1819
1817
|
}
|
|
1820
1818
|
});
|
|
1821
1819
|
|
|
1822
|
-
log_spooder(`server started on port {${port}} (host: {${hostname ?? 'unspecified'}})`);
|
|
1820
|
+
log_spooder(`server started on port {${server.port}} (host: {${hostname ?? 'unspecified'}})`);
|
|
1823
1821
|
|
|
1824
1822
|
type ThrottleHandler = {
|
|
1825
1823
|
(delta: number, handler: JSONRequestHandler): JSONRequestHandler;
|
|
@@ -2181,6 +2179,11 @@ export function http_serve(port: number, hostname?: string) {
|
|
|
2181
2179
|
return http_apply_range(file, request);
|
|
2182
2180
|
});
|
|
2183
2181
|
}
|
|
2182
|
+
},
|
|
2183
|
+
|
|
2184
|
+
/** The port the server is listening on. Useful when port 0 is passed to assign a random port. */
|
|
2185
|
+
get port(): number {
|
|
2186
|
+
return server.port!;
|
|
2184
2187
|
}
|
|
2185
2188
|
};
|
|
2186
2189
|
}
|