summaryrefslogtreecommitdiff
path: root/cli/js/ops/dispatch_json.ts
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2020-07-19 19:49:44 +0200
committerGitHub <noreply@github.com>2020-07-19 19:49:44 +0200
commitfa61956f03491101b6ef64423ea2f1f73af26a73 (patch)
treec3800702071ca78aa4dd71bdd0a59a9bbe460bdd /cli/js/ops/dispatch_json.ts
parent53adde866dd399aa2509d14508642fce37afb8f5 (diff)
Port internal TS code to JS (#6793)
Co-authored-by: Ryan Dahl <ry@tinyclouds.org>
Diffstat (limited to 'cli/js/ops/dispatch_json.ts')
-rw-r--r--cli/js/ops/dispatch_json.ts94
1 files changed, 0 insertions, 94 deletions
diff --git a/cli/js/ops/dispatch_json.ts b/cli/js/ops/dispatch_json.ts
deleted file mode 100644
index cf6f5c095..000000000
--- a/cli/js/ops/dispatch_json.ts
+++ /dev/null
@@ -1,94 +0,0 @@
-// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
-
-import * as util from "../util.ts";
-import { core } from "../core.ts";
-import { ErrorKind, getErrorClass } from "../errors.ts";
-
-// eslint-disable-next-line @typescript-eslint/no-explicit-any
-type Ok = any;
-
-interface JsonError {
- kind: ErrorKind;
- message: string;
-}
-
-interface JsonResponse {
- ok?: Ok;
- err?: JsonError;
- promiseId?: number; // Only present in async messages.
-}
-
-// Using an object without a prototype because `Map` was causing GC problems.
-const promiseTable: Record<
- number,
- util.Resolvable<JsonResponse>
-> = Object.create(null);
-let _nextPromiseId = 1;
-
-function nextPromiseId(): number {
- return _nextPromiseId++;
-}
-
-function decode(ui8: Uint8Array): JsonResponse {
- return JSON.parse(core.decode(ui8));
-}
-
-function encode(args: object): Uint8Array {
- return core.encode(JSON.stringify(args));
-}
-
-function unwrapResponse(res: JsonResponse): Ok {
- if (res.err != null) {
- throw new (getErrorClass(res.err.kind))(res.err.message);
- }
- util.assert(res.ok != null);
- return res.ok;
-}
-
-export function asyncMsgFromRust(resUi8: Uint8Array): void {
- const res = decode(resUi8);
- util.assert(res.promiseId != null);
-
- const promise = promiseTable[res.promiseId!];
- util.assert(promise != null);
- delete promiseTable[res.promiseId!];
- promise.resolve(res);
-}
-
-export function sendSync(
- opName: string,
- args: object = {},
- ...zeroCopy: Uint8Array[]
-): Ok {
- util.log("sendSync", opName);
- const argsUi8 = encode(args);
- const resUi8 = core.dispatchByName(opName, argsUi8, ...zeroCopy);
- util.assert(resUi8 != null);
- const res = decode(resUi8);
- util.assert(res.promiseId == null);
- return unwrapResponse(res);
-}
-
-export async function sendAsync(
- opName: string,
- args: object = {},
- ...zeroCopy: Uint8Array[]
-): Promise<Ok> {
- util.log("sendAsync", opName);
- const promiseId = nextPromiseId();
- args = Object.assign(args, { promiseId });
- const promise = util.createResolvable<Ok>();
- const argsUi8 = encode(args);
- const buf = core.dispatchByName(opName, argsUi8, ...zeroCopy);
- if (buf != null) {
- // Sync result.
- const res = decode(buf);
- promise.resolve(res);
- } else {
- // Async result.
- promiseTable[promiseId] = promise;
- }
-
- const res = await promise;
- return unwrapResponse(res);
-}