Skip to content

Commit 7e6f26b

Browse files
committed
Fix crypto.randomUUID error in non-secure contexts (HTTP)
1 parent f7880ab commit 7e6f26b

11 files changed

Lines changed: 85 additions & 10 deletions

File tree

packages/core/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# @spoosh/core
22

3+
## 0.18.1
4+
5+
- Add `generateUUID` utility with fallback for non-secure contexts (HTTP)
6+
37
## 0.18.0
48

59
- Add `matchTag` and `matchTags` utilities for wildcard pattern matching

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@spoosh/core",
3-
"version": "0.18.0",
3+
"version": "0.18.1",
44
"license": "MIT",
55
"description": "Type-safe API toolkit with plugin middleware system",
66
"keywords": [

packages/core/src/utils/buildUrl.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,46 @@ describe("buildUrl", () => {
4040
});
4141
});
4242

43+
describe("IP address URLs", () => {
44+
it("should handle IP address with path", () => {
45+
const result = buildUrl("http://159.223.72.31/api", ["users"]);
46+
expect(result).toBe("http://159.223.72.31/api/users");
47+
});
48+
49+
it("should handle IP address with path and multiple segments", () => {
50+
const result = buildUrl("http://159.223.72.31/api", ["users", "123"]);
51+
expect(result).toBe("http://159.223.72.31/api/users/123");
52+
});
53+
54+
it("should handle IP address with port and path", () => {
55+
const result = buildUrl("http://192.168.1.1:8080/api", ["posts"]);
56+
expect(result).toBe("http://192.168.1.1:8080/api/posts");
57+
});
58+
59+
it("should handle IP address with empty path array", () => {
60+
const result = buildUrl("http://159.223.72.31/api", []);
61+
expect(result).toBe("http://159.223.72.31/api/");
62+
});
63+
64+
it("should handle IP address with query parameters", () => {
65+
const result = buildUrl("http://159.223.72.31/api", ["users"], {
66+
page: 1,
67+
limit: 10,
68+
});
69+
expect(result).toBe("http://159.223.72.31/api/users?page=1&limit=10");
70+
});
71+
72+
it("should handle localhost IP with path", () => {
73+
const result = buildUrl("http://127.0.0.1:3000/api", ["health"]);
74+
expect(result).toBe("http://127.0.0.1:3000/api/health");
75+
});
76+
77+
it("should handle IP address with nested api path", () => {
78+
const result = buildUrl("http://10.0.0.1/v1/api", ["resources", "items"]);
79+
expect(result).toBe("http://10.0.0.1/v1/api/resources/items");
80+
});
81+
});
82+
4383
describe("relative paths", () => {
4484
it("should handle relative base without leading slash", () => {
4585
const result = buildUrl("api", ["users"]);

packages/core/src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ export { matchTag, matchTags, normalizeTag } from "./matchTag";
3030
export { isNetworkError, isAbortError } from "./errors";
3131
export { clone } from "./clone";
3232
export { createTracer } from "./tracer";
33+
export { generateUUID } from "./uuid";

packages/core/src/utils/uuid.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Generates a UUID v4 string.
3+
* Uses crypto.randomUUID() when available (secure contexts),
4+
* falls back to Math.random() for non-secure contexts (HTTP).
5+
*/
6+
export function generateUUID(): string {
7+
if (
8+
typeof crypto !== "undefined" &&
9+
typeof crypto.randomUUID === "function"
10+
) {
11+
return crypto.randomUUID();
12+
}
13+
14+
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
15+
const r = (Math.random() * 16) | 0;
16+
const v = c === "x" ? r : (r & 0x3) | 0x8;
17+
return v.toString(16);
18+
});
19+
}

packages/devtool/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# @spoosh/devtool
22

3+
## 0.7.4
4+
5+
- Fix `crypto.randomUUID` error in non-secure contexts (HTTP)
6+
37
## 0.7.3
48

59
- Fix white spaces and newlines not rendering correctly in import view

packages/devtool/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@spoosh/devtool",
3-
"version": "0.7.3",
3+
"version": "0.7.4",
44
"description": "Visual debugging plugin for Spoosh - floating devtool panel with plugin execution visualization",
55
"license": "MIT",
66
"repository": {
@@ -41,7 +41,7 @@
4141
"format": "prettier --write 'src/**/*.ts' '*.md'"
4242
},
4343
"peerDependencies": {
44-
"@spoosh/core": ">=0.17.0"
44+
"@spoosh/core": ">=0.18.1"
4545
},
4646
"devDependencies": {
4747
"@spoosh/core": "workspace:*",

packages/devtool/src/store/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {
66
StateManager,
77
EventEmitter,
88
} from "@spoosh/core";
9+
import { generateUUID } from "@spoosh/core";
910

1011
import type {
1112
CacheEntryDisplay,
@@ -251,7 +252,7 @@ export class DevToolStore implements DevToolStoreInterface {
251252
const trace: OperationTrace = {
252253
...context,
253254
type: "request",
254-
id: crypto.randomUUID(),
255+
id: generateUUID(),
255256
path: resolvedPath,
256257
startTime: now,
257258
timestamp: Date.now(),

packages/transport-sse/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# @spoosh/transport-sse
22

3+
## 0.1.2
4+
5+
- Fix `crypto.randomUUID` error in non-secure contexts (HTTP)
6+
37
## 0.1.1
48

59
- Attach `resolveParser` and `resolveAccumulator` utilities to transport object under `utils` namespace

packages/transport-sse/package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@spoosh/transport-sse",
3-
"version": "0.1.1",
3+
"version": "0.1.2",
44
"type": "module",
55
"main": "./dist/index.js",
66
"types": "./dist/index.d.ts",
@@ -17,8 +17,10 @@
1717
"format": "prettier --write 'src/**/*.ts' '*.md'"
1818
},
1919
"dependencies": {
20-
"@microsoft/fetch-event-source": "^2.0.1",
21-
"@spoosh/core": "workspace:*"
20+
"@microsoft/fetch-event-source": "^2.0.1"
21+
},
22+
"peerDependencies": {
23+
"@spoosh/core": ">=0.18.1"
2224
},
2325
"devDependencies": {
2426
"tsup": "^8.0.0",

0 commit comments

Comments
 (0)