Skip to content

Commit

Permalink
add UUID fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-nagy committed Aug 1, 2024
1 parent 477c0c7 commit 0089267
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 15 deletions.
3 changes: 2 additions & 1 deletion packages/core/src/Fiber.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as BehaviorSubject from "./BehaviorSubject.js";
import * as UUID from "./UUID.js";

export { Fiber as t };

Expand Down Expand Up @@ -35,7 +36,7 @@ export class Fiber {
/**
* A unique identifier for this fiber.
*/
public readonly id: string = crypto.randomUUID()
public readonly id: string = UUID.v4()
) {
table.set(id, this);
}
Expand Down
9 changes: 5 additions & 4 deletions packages/core/src/Message.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as JsObject from "./JsObject.js";
import * as UUID from "./UUID.js";

/**
* Flattens an intersection type into a single type.
Expand Down Expand Up @@ -89,7 +90,7 @@ export type CallFunction<Args> = FlattenIntersection<
export const CallFunction = <Args>({
address,
args,
id = crypto.randomUUID(),
id = UUID.v4(),
noReply = false,
path
}: {
Expand Down Expand Up @@ -122,7 +123,7 @@ export type Error<Error> = FlattenIntersection<
export const Error = <T>({
address,
error,
id = crypto.randomUUID()
id = UUID.v4()
}: {
address: string;
error: T;
Expand All @@ -147,7 +148,7 @@ export type GarbageCollect = FlattenIntersection<
*/
export const GarbageCollect = ({
address,
id = crypto.randomUUID()
id = UUID.v4()
}: {
address: string;
id?: string;
Expand All @@ -171,7 +172,7 @@ export type SetValue<Value> = FlattenIntersection<
*/
export const SetValue = <T>({
address,
id = crypto.randomUUID(),
id = UUID.v4(),
value
}: {
address: string;
Expand Down
20 changes: 11 additions & 9 deletions packages/core/src/PubSub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,21 @@ export function from<T>(observable: Observable.ObservableLike<T>): PubSub<T> {
observer: AsyncObserver<T> | ((value: T) => Promise<void>)
) => {
const metadata = Metadata.get(observer);
const agent = metadata && Fiber.get(metadata.clientAgentId);
const subscription = observable.subscribe(observer);

if (metadata) {
Fiber.get(metadata.clientAgentId)?.stateChange.subscribe((state) => {
switch (state) {
case Fiber.State.Terminated:
subscription.unsubscribe();
}
});
}
const innerSubscription = agent?.stateChange.subscribe((state) => {
switch (state) {
case Fiber.State.Terminated:
subscription.unsubscribe();
}
});

return {
unsubscribe: async () => subscription.unsubscribe()
unsubscribe: async () => {
innerSubscription?.unsubscribe();
subscription.unsubscribe();
}
};
}
};
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/Session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as ServerAgent from "./ServerAgent.js";
import * as Subject from "./Subject.js";
import * as Subprotocol from "./Subprotocol.js";
import * as Supervisor from "./Supervisor.js";
import * as UUID from "./UUID.js";

export { Session as t };

Expand Down Expand Up @@ -113,7 +114,7 @@ export abstract class Session<
protected createServer(
this: Session,
provide: unknown,
address: string = crypto.randomUUID()
address: string = UUID.v4()
): ServerAgent.t {
const serverAgent = ServerAgent.init({
address,
Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/UUID.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { expect, test } from "bun:test";

import * as UUID from "./UUID.js";

test("generating a v4 UUID", () => {
expect(UUID.v4()).toEqual(
expect.stringMatching(/\w{8}-\w{4}-\w{4}-\w{4}-\w{12}/)
);
});
29 changes: 29 additions & 0 deletions packages/core/src/UUID.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
function hex(bits: number) {
if (bits > 53) throw new Error("bits must be less than or equal to 53");

return Math.floor(Math.random() * (2 ** bits - 1))
.toString(16)
.padStart(Math.ceil(bits / 4), "0");
}

/**
* Generates a v4 UUID. This implementation generates exactly 122 bits of random
* data and concatenates the version and variant.
*
* @see https://datatracker.ietf.org/doc/html/rfc9562#name-uuid-version-4
*/
function v4Fallback(): string {
const a = hex(48);
const b = hex(12);
const c = `${hex(31)}${hex(31)}`;

return `${a.slice(0, 8)}-${a.slice(8)}-4${b}-a${c.slice(0, 3)}-${c.slice(
3,
15
)}`;
}

export const v4 =
typeof crypto !== "undefined" && typeof crypto.randomUUID !== "undefined"
? crypto.randomUUID.bind(crypto)
: v4Fallback;

0 comments on commit 0089267

Please sign in to comment.