Skip to content

Commit

Permalink
Revert "Rename proxy to stub (#19)"
Browse files Browse the repository at this point in the history
This reverts commit a00b16f.
  • Loading branch information
mcandeia authored Dec 5, 2024
1 parent d4d6620 commit 5951fe4
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 55 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class Counter {
}

// Invoking the counter actor
const counter = actors.stub(Counter).id("counter-id");
const counter = actors.proxy(Counter).id("counter-id");
// Increment counter
await counter.increment();
// Get current count
Expand Down
3 changes: 1 addition & 2 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
"exports": {
".": "./src/actors/mod.ts",
"./hono": "./src/actors/hono/middleware.ts",
"./proxy": "./src/actors/stub.ts",
"./stub": "./src/actors/stub.ts",
"./proxy": "./src/actors/proxy.ts",
"./watch": "./src/actors/util/watch.ts",
"./server-run": "./src/actors/server/run.ts",
"./server-gen": "./src/actors/server/gen.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/actors/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
export interface Actor {
}

export type { ActorProxy } from "./proxyutil.ts";
export { ActorRuntime } from "./runtime.ts";
export type { ActorConstructor } from "./runtime.ts";
export { ActorState } from "./state.ts";
export { type ActorStorage } from "./storage.ts";
export type { ActorProxy, ActorProxy as ActorStub } from "./stubutil.ts";
export { actorId } from "./util/id.ts";
27 changes: 12 additions & 15 deletions src/actors/stub.ts → src/actors/proxy.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// deno-lint-ignore-file no-explicit-any
import type { Actor, ActorConstructor } from "./runtime.ts";
import {
type ActorProxy,
create,
createHttpInvoker,
type StubFactory,
} from "./stubutil.ts";
export type { ActorProxy, ActorProxy as ActorStub };
type ProxyFactory,
} from "./proxyutil.ts";
import type { Actor, ActorConstructor } from "./runtime.ts";
export type { ActorProxy };
export interface ActorsServer {
url: string;
credentials?: RequestCredentials;
Expand All @@ -18,19 +18,16 @@ export interface ActorsOptions {
actorIdHeaderName?: string;
errorHandling?: Record<string, new (...args: any[]) => Error>;
}
const stub = <TInstance extends Actor>(
actor: ActorConstructor<TInstance> | string,
options?: ActorsOptions | undefined,
): { id: StubFactory<TInstance> } => {
const factory = (id: string, discriminator?: string) =>
createHttpInvoker(id, discriminator, options);
return create(actor, factory);
};

/**
* utilities to create and manage actors.
*/
export const actors = {
proxy: stub,
stub,
proxy: <TInstance extends Actor>(
actor: ActorConstructor<TInstance> | string,
options?: ActorsOptions | undefined,
): { id: ProxyFactory<TInstance> } => {
const factory = (id: string, discriminator?: string) =>
createHttpInvoker(id, discriminator, options);
return create(actor, factory);
},
};
14 changes: 5 additions & 9 deletions src/actors/stubutil.ts → src/actors/proxyutil.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// deno-lint-ignore-file no-explicit-any
import process from "node:process";
import type { ActorsOptions, ActorsServer } from "./proxy.ts";
import type { InvokeRequest, InvokeResponse } from "./rpc.ts";
import type { Actor, ActorConstructor } from "./runtime.ts";
import { EVENT_STREAM_RESPONSE_HEADER, readFromStream } from "./stream.ts";
import type { ActorsOptions, ActorsServer, ActorStub } from "./stub.ts";
import {
type Channel,
type ChannelUpgrader,
Expand All @@ -20,10 +20,10 @@ export const ACTOR_CONSTRUCTOR_NAME_HEADER = "x-error-constructor-name";
export const ACTOR_DISCRIMINATOR_HEADER_NAME = "x-actor-discriminator";
export const ACTOR_DISCRIMINATOR_QS_NAME = "actor_discriminator";

export type StubFactory<TInstance> = (
export type ProxyFactory<TInstance> = (
id: string,
discriminator?: string,
) => ActorStub<TInstance>;
) => ActorProxy<TInstance>;
/**
* Promise.prototype.then onfufilled callback type.
*/
Expand Down Expand Up @@ -200,8 +200,6 @@ export interface ProxyOptions<TInstance extends Actor> {
server: string;
}

export type StubOptions<TInstance extends Actor> = ProxyOptions<TInstance>;

export type PromisifyKey<Actor, key extends keyof Actor> = Actor[key] extends
(...args: infer Args) => Awaited<infer Return>
? Return extends ChannelUpgrader<infer TSend, infer TReceive>
Expand Down Expand Up @@ -372,11 +370,9 @@ export const createRPCInvoker = <
const resolver: RequestResolver<TResponse> = { response };
pendingRequests.set(id, resolver);
const cleanup = () => {
if (!pendingRequests.has(id)) {
return;
}
const channelClosed = new Error("Channel closed");
response.reject(channelClosed);
resolver.throw?.(channelClosed);
resolver.stream?.close();
resolver.ch?.close();
};
Expand Down Expand Up @@ -514,7 +510,7 @@ export const create = <TInstance extends Actor>(
invokerFactory: (id: string, discriminator?: string) => ActorInvoker,
metadata?: unknown,
disposer?: () => void,
): { id: StubFactory<TInstance> } => {
): { id: ProxyFactory<TInstance> } => {
const name = typeof actor === "string" ? actor : actor.name;
return {
id: (id: string, discriminator?: string): ActorProxy<TInstance> => {
Expand Down
2 changes: 1 addition & 1 deletion src/actors/rpc.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// deno-lint-ignore-file no-explicit-any
import type { ActorInvoker } from "./proxyutil.ts";
import { isEventStreamResponse } from "./stream.ts";
import type { ActorInvoker } from "./stubutil.ts";
import {
type ChannelUpgrader,
type DuplexChannel,
Expand Down
12 changes: 6 additions & 6 deletions src/actors/runtime.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { assertEquals, assertFalse } from "jsr:@std/assert@^1.0.5";
import { actors } from "./proxy.ts";
import { ActorRuntime } from "./runtime.ts";
import type { ActorState } from "./state.ts";
import { actors } from "./stub.ts";
import type { ChannelUpgrader } from "./util/channels/channel.ts";
import { WatchTarget } from "./util/watch.ts";

Expand Down Expand Up @@ -32,7 +32,7 @@ class Counter {
}

callSayHello(): Promise<string> {
const hello = this.state.stub(Hello).id(this.state.id);
const hello = this.state.proxy(Hello).id(this.state.id);
return hello.sayHello();
}

Expand All @@ -59,7 +59,7 @@ class Counter {
}

async *readHelloChan(name: string): AsyncIterableIterator<string> {
const hello = this.state.stub(Hello).id(this.state.id);
const hello = this.state.proxy(Hello).id(this.state.id);
using helloChan = hello.chan(name);
for await (const event of helloChan.recv()) {
yield event;
Expand Down Expand Up @@ -100,10 +100,10 @@ Deno.test("counter tests", async () => {
reqCount++;
});
const actorId = "1234";
const counterStub = actors.stub(Counter);
const counterProxy = actors.proxy(Counter);

const counterActor = counterStub.id(actorId);
using rpcActor = counterStub.id("12345", "1234").rpc();
const counterActor = counterProxy.id(actorId);
using rpcActor = counterProxy.id("12345", "1234").rpc();

for (const actor of [rpcActor, counterActor]) {
const name = `Counter Actor`;
Expand Down
12 changes: 6 additions & 6 deletions src/actors/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
// deno-lint-ignore-file no-explicit-any
import process from "node:process";
import { ActorError } from "./errors.ts";
import {
ACTOR_CONSTRUCTOR_NAME_HEADER,
ACTOR_DISCRIMINATOR_HEADER_NAME,
ACTOR_DISCRIMINATOR_QS_NAME,
ACTOR_ID_HEADER_NAME,
} from "./proxyutil.ts";
import { ActorSilo } from "./silo.ts";
import type { ActorState } from "./state.ts";
import type { ActorStorage } from "./storage.ts";
Expand All @@ -10,12 +16,6 @@ import {
EVENT_STREAM_RESPONSE_HEADER,
isEventStreamResponse,
} from "./stream.ts";
import {
ACTOR_CONSTRUCTOR_NAME_HEADER,
ACTOR_DISCRIMINATOR_HEADER_NAME,
ACTOR_DISCRIMINATOR_QS_NAME,
ACTOR_ID_HEADER_NAME,
} from "./stubutil.ts";
import { serializeUint8Array } from "./util/buffers.ts";
import { isUpgrade, makeWebSocket } from "./util/channels/channel.ts";
import { actorId as getActorId } from "./util/id.ts";
Expand Down
2 changes: 1 addition & 1 deletion src/actors/runtimes/cf/fetcher.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { DurableObjectNamespace } from "@cloudflare/workers-types";
import { ACTOR_ID_HEADER_NAME } from "../../proxyutil.ts";
import type { ActorConstructor, ActorFetcher } from "../../runtime.ts";
import { ACTOR_ID_HEADER_NAME } from "../../stubutil.ts";
import { actorId as getActorId } from "../../util/id.ts";
import { registerActors } from "./actorDO.ts";

Expand Down
16 changes: 8 additions & 8 deletions src/actors/silo.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { ActorError } from "./errors.ts";
import { rpc } from "./rpc.ts";
import type { ActorConstructor, ActorInstance } from "./runtime.ts";
import { ActorState } from "./state.ts";
import type { ActorStorage } from "./storage.ts";
import {
type ActorInvoker,
create,
createHttpInvoker,
WELL_KNOWN_RPC_MEHTOD,
} from "./stubutil.ts";
} from "./proxyutil.ts";
import { rpc } from "./rpc.ts";
import type { ActorConstructor, ActorInstance } from "./runtime.ts";
import { ActorState } from "./state.ts";
import type { ActorStorage } from "./storage.ts";
import { isUpgrade, makeDuplexChannel } from "./util/channels/channel.ts";
// deno-lint-ignore no-explicit-any
type FunctionType = (...args: any) => any;
Expand Down Expand Up @@ -51,7 +51,7 @@ export class ActorSilo<TEnv extends object = object> {
id: this.actorId,
discriminator: this.discriminator,
storage,
stub: (actor) => {
proxy: (actor) => {
const invoker = (id: string) => {
if (id === this.actorId) {
return this.invoker;
Expand Down Expand Up @@ -105,7 +105,7 @@ export class ActorSilo<TEnv extends object = object> {
);
}

const actorStub = new Proxy(actorInstance.actor, {
const actorProxy = new Proxy(actorInstance.actor, {
get(target, prop, receiver) {
if (prop === "metadata") {
return metadata;
Expand All @@ -116,7 +116,7 @@ export class ActorSilo<TEnv extends object = object> {

// deno-lint-ignore ban-types
const result = await (methodImpl as Function).apply(
actorStub,
actorProxy,
Array.isArray(args) ? args : [args],
);

Expand Down
8 changes: 4 additions & 4 deletions src/actors/state.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { create } from "./proxyutil.ts";
import type { Actor, ActorConstructor } from "./runtime.ts";
import type { ActorStorage } from "./storage.ts";
import type { create } from "./stubutil.ts";

export interface ActorStateOptions {
id: string;
discriminator?: string;
storage: ActorStorage;
stub: <TInstance extends Actor>(
proxy: <TInstance extends Actor>(
actor: ActorConstructor<TInstance>,
) => ReturnType<typeof create<TInstance>>;
}
Expand All @@ -17,13 +17,13 @@ export class ActorState {
public id: string;
public discriminator?: string;
public storage: ActorStorage;
public stub: <TInstance extends Actor>(
public proxy: <TInstance extends Actor>(
actor: ActorConstructor<TInstance>,
) => ReturnType<typeof create<TInstance>>;
public initialization: Promise<void> = Promise.resolve();
constructor(options: ActorStateOptions) {
this.storage = options.storage;
this.stub = options.stub;
this.proxy = options.proxy;
this.id = options.id;
this.discriminator = options.discriminator;
}
Expand Down
2 changes: 1 addition & 1 deletion src/actors/util/id.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ACTOR_ID_HEADER_NAME, ACTOR_ID_QS_NAME } from "../stubutil.ts";
import { ACTOR_ID_HEADER_NAME, ACTOR_ID_QS_NAME } from "../proxyutil.ts";

/**
* Retrieves the actor ID from the request.
Expand Down

0 comments on commit 5951fe4

Please sign in to comment.