Skip to content

Commit

Permalink
Merge pull request #116 from fullstackedorg/worker-package-install
Browse files Browse the repository at this point in the history
Worker Package Install and Binary Format for IPC
  • Loading branch information
cplepage authored Nov 1, 2024
2 parents 49429c4 + 4ddb4d3 commit 800c00e
Show file tree
Hide file tree
Showing 32 changed files with 1,420 additions and 979 deletions.
3 changes: 2 additions & 1 deletion build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ if (fs.existsSync("editor/build"))

const toBuild = [
["editor/index.ts", "index"],
["editor/typescript/worker.ts", "worker-ts"]
["editor/typescript/worker.ts", "worker-ts"],
["editor/views/packages/worker.ts", "worker-package-install"]
];

const baseJS = await fs.promises.readFile(baseFile, { encoding: "utf-8" });
Expand Down
2 changes: 1 addition & 1 deletion editor/api/connectivity/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ const verifyWSS = async (peerNearby: PeerNearbyBonjour | PeerNearbyWeb) => {

const response = await new Promise((resolve) => {
rpc()
.fetch(url, {
.fetch(url, null, {
encoding: "utf8",
timeout: 500
})
Expand Down
4 changes: 2 additions & 2 deletions editor/api/connectivity/web.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import api from "..";
import rpc from "../../rpc";
import { Browser } from "../../../src/connectivity/browser";
import {
PEER_ADVERSTISING_METHOD,
Expand Down Expand Up @@ -47,8 +48,7 @@ export class BrowseWeb implements Browser {

const promise = new Promise<Peer>((resolve) => {
rpc()
.fetch(url, {
encoding: "utf8",
.fetch(url, null, {
timeout: 2000
})
.then((response) => {
Expand Down
37 changes: 21 additions & 16 deletions editor/api/git/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ export default {
async deviceFlowStart() {
const response = await rpc().fetch(
"https://github.com/login/device/code",
JSON.stringify({
client_id,
scope: "repo,user:email"
}),
{
method: "POST",
headers: {
"content-type": "application/json",
accept: "application/json"
},
body: JSON.stringify({
client_id,
scope: "repo,user:email"
}),
encoding: "utf8"
}
);
Expand All @@ -29,17 +29,17 @@ export default {
async deviceFlowPoll(device_code: string) {
const response = await rpc().fetch(
"https://github.com/login/oauth/access_token",
JSON.stringify({
client_id,
device_code,
grant_type: "urn:ietf:params:oauth:grant-type:device_code"
}),
{
method: "POST",
headers: {
"content-type": "application/json",
accept: "application/json"
},
body: JSON.stringify({
client_id,
device_code,
grant_type: "urn:ietf:params:oauth:grant-type:device_code"
}),
encoding: "utf8"
}
);
Expand All @@ -53,20 +53,25 @@ export default {

const { access_token } = json;

const userResponse = await rpc().fetch("https://api.github.com/user", {
headers: {
authorization: `Bearer ${access_token}`,
accept: "application/json"
},
encoding: "utf8"
});
const userResponse = await rpc().fetch(
"https://api.github.com/user",
null,
{
headers: {
authorization: `Bearer ${access_token}`,
accept: "application/json"
},
encoding: "utf8"
}
);

const user = JSON.parse(userResponse.body as string);

const username = user.login;

const emailsResponse = await rpc().fetch(
"https://api.github.com/user/emails",
null,
{
headers: {
authorization: `Bearer ${access_token}`,
Expand Down
10 changes: 7 additions & 3 deletions editor/api/git/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { CONFIG_TYPE } from "../config/types";
import github from "./github";
import rpc from "../../rpc";
import { GitAuth } from "../../views/project/git/auth";
import { toByteArray } from "base64-js";

// for isomorphic-git
window.Buffer = globalBuffer;
Expand Down Expand Up @@ -113,17 +114,19 @@ const http = {
async request({ url, method, headers, body, onProgress }) {
body = body ? await awaitBody(body) : undefined;

const response = await rpc().fetch(url, {
const response = await rpc().fetch(url, body, {
method,
headers,
body
encoding: "base64"
});

const data = toByteArray(response.body);

return {
...response,
url,
method,
body: [response.body]
body: [data]
};
}
};
Expand Down Expand Up @@ -243,6 +246,7 @@ export default {
});
return true;
} catch (e) {
console.log(e);
return e?.caller !== "git.fetch";
}
},
Expand Down
3 changes: 2 additions & 1 deletion editor/typescript/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { fromByteArray } from "base64-js";
import type { methods } from "./worker";

type OnlyOnePromise<T> = T extends PromiseLike<any> ? T : Promise<T>;
Expand Down Expand Up @@ -74,7 +75,7 @@ function start(workingDirectory: string) {
resolve();
} else if (message.data.body) {
const { id, body } = message.data;
(globalThis as any).Android?.passRequestBody(id, body);
globalThis.Android.passRequestBody(id, fromByteArray(body));
worker.postMessage({ request_id: id });
} else {
const { id, data } = message.data;
Expand Down
Loading

0 comments on commit 800c00e

Please sign in to comment.