Skip to content

Commit

Permalink
Refactor before branching off
Browse files Browse the repository at this point in the history
  • Loading branch information
d-gubert committed Oct 10, 2023
1 parent d358a85 commit 1907229
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 57 deletions.
11 changes: 11 additions & 0 deletions deno-runtime/lib/accessors/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

export function proxify(namespace: string) {
return new Proxy({}, {
get(target: unknown, prop: string): unknown {
return (...args: unknown[]) => {
return {};
};
}
})
}

42 changes: 42 additions & 0 deletions deno-runtime/lib/messenger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
export type JSONRPC_Message = {
jsonrpc: '2.0-rc';
};

export type SuccessResponse = JSONRPC_Message & {
id: string;
result: any;
};

export type ErrorResponse = JSONRPC_Message & {
error: {
code: number;
message: string;
data?: Record<string, unknown>;
};
id: string | null;
};

export type JSONRPC_Response = SuccessResponse | ErrorResponse;

const encoder = new TextEncoder();

export async function errorResponse({ error: { message, code = -32000, data }, id }: Omit<ErrorResponse, 'jsonrpc'>): Promise<void> {
const rpc: ErrorResponse = {
jsonrpc: '2.0-rc',
id,
error: { message, code, ...(data && { data }) },
};

const encoded = this.encoder.encode(JSON.stringify(rpc));
Deno.stdout.write(encoded);
}

export async function successResponse(id: string, ...result: unknown[]): Promise<void> {
const rpc: SuccessResponse = {
jsonrpc: '2.0-rc',
id,
result,
};
const encoded = this.encoder.encode(JSON.stringify(rpc));
await Deno.stdout.write(encoded);
}
57 changes: 2 additions & 55 deletions deno-runtime/deno-wrapper.ts → deno-runtime/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ if (!Deno.args.includes('--subprocess')) {

import { createRequire } from 'node:module';
import { sanitizeDeprecatedUsage } from "./lib/sanitizeDeprecatedUsage.ts";
import { proxify } from "./lib/accessors/mod.ts";
import * as Messenger from "./lib/messenger.ts";

const require = createRequire(import.meta.url);

Expand Down Expand Up @@ -58,61 +60,6 @@ function wrapAppCode(code: string): (require: (module: string) => unknown) => Pr
) as (require: (module: string) => unknown) => Promise<Record<string, unknown>>;
}

type JSONRPC_Message = {
jsonrpc: '2.0-rc';
};

type SuccessResponse = JSONRPC_Message & {
id: string;
result: any;
};

type ErrorResponse = JSONRPC_Message & {
error: {
code: number;
message: string;
data?: Record<string, unknown>;
};
id: string | null;
};

type JSONRPC_Response = SuccessResponse | ErrorResponse;

const Messenger = new (class {
private encoder = new TextEncoder();

public async successResponse(id: string, ...result: unknown[]): Promise<void> {
const rpc: SuccessResponse = {
jsonrpc: '2.0-rc',
id,
result,
};
const encoded = this.encoder.encode(JSON.stringify(rpc));
await Deno.stdout.write(encoded);
}

public async errorResponse({ error: { message, code = -32000, data }, id }: Omit<ErrorResponse, 'jsonrpc'>): Promise<void> {
const rpc: ErrorResponse = {
jsonrpc: '2.0-rc',
id,
error: { message, code, ...(data && { data }) },
};

const encoded = this.encoder.encode(JSON.stringify(rpc));
Deno.stdout.write(encoded);
}
})();

function proxify(namespace: string) {
return new Proxy({}, {
get(target: unknown, prop: string): unknown {
return (...args: unknown[]) => {
return {};
};
}
})
}

async function handlInitializeApp({ id, source }: { id: string; source: string }): Promise<Record<string, unknown>> {
source = sanitizeDeprecatedUsage(source);
const require = buildRequire();
Expand Down
4 changes: 2 additions & 2 deletions src/server/runtime/AppsEngineDenoRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ export function getDenoExecutablePath(): string {
export function getDenoWrapperPath(): string {
try {
// This path is relative to the compiled version of the Apps-Engine source
return require.resolve('../../deno-runtime/deno-wrapper.ts');
return require.resolve('../../deno-runtime/main.ts');
} catch {
// This path is relative to the original Apps-Engine files
return require.resolve('../../../deno-runtime/deno-wrapper.ts');
return require.resolve('../../../deno-runtime/main.ts');
}
}

Expand Down

0 comments on commit 1907229

Please sign in to comment.