Skip to content

Commit

Permalink
Net Fetch in Electron (#3069)
Browse files Browse the repository at this point in the history
  • Loading branch information
jameskerr authored Jun 6, 2024
1 parent 64f50eb commit cc740ad
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 27 deletions.
47 changes: 26 additions & 21 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,35 @@
"ignorePatterns": ["**/*"],
"plugins": ["@nrwl/nx"],
"overrides": [
{
"files": ["*.ts", "*.tsx"],
"extends": ["plugin:@nrwl/nx/typescript"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"extends": ["plugin:@nrwl/nx/javascript"],
"rules": {}
},
{
"files": ["*.spec.ts", "*.spec.tsx", "*.spec.js", "*.spec.jsx"],
"env": {
"jest": true
},
"rules": {}
},
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {
"@typescript-eslint/no-unused-vars": ["error", {
"varsIgnorePattern": "^_",
"argsIgnorePattern": "^_"
}],
"@typescript-eslint/no-unused-vars": [
"error",
{
"varsIgnorePattern": "^_",
"argsIgnorePattern": "^_"
}
],
"@typescript-eslint/no-explicit-any": ["off", {}],
"@typescript-eslint/ban-ts-comment": ["off", {}],
"@nrwl/nx/enforce-module-boundaries": [
"error",
{
Expand All @@ -24,23 +46,6 @@
}
]
}
},
{
"files": ["*.ts", "*.tsx"],
"extends": ["plugin:@nrwl/nx/typescript"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"extends": ["plugin:@nrwl/nx/javascript"],
"rules": {}
},
{
"files": ["*.spec.ts", "*.spec.tsx", "*.spec.js", "*.spec.jsx"],
"env": {
"jest": true
},
"rules": {}
}
]
}
6 changes: 6 additions & 0 deletions apps/zui/src/core/electron-zed-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {Client} from "@brimdata/zed-node"
import {net} from "electron"

export class ElectronZedClient extends Client {
fetch = net.fetch
}
6 changes: 6 additions & 0 deletions apps/zui/src/core/electron-zed-lake.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {Lake} from "@brimdata/zed-node"
import {net} from "electron"

export class ElectronZedLake extends Lake {
fetch = net.fetch
}
9 changes: 5 additions & 4 deletions apps/zui/src/core/main/main-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {app} from "electron"
import keytar from "keytar"
import {EventEmitter} from "events"
import os from "os"
import {Client, Lake} from "@brimdata/zed-node"
import {Store as ReduxStore} from "redux"
import url from "url"
import {
Expand Down Expand Up @@ -30,6 +29,8 @@ import {getAuthToken} from "../../js/api/core/get-zealot"
import {Abortables} from "src/app/core/models/abortables"
import * as zui from "src/zui"
import log from "electron-log"
import {ElectronZedClient} from "../electron-zed-client"
import {ElectronZedLake} from "../electron-zed-lake"

export class MainObject {
public isQuitting = false
Expand All @@ -43,7 +44,7 @@ export class MainObject {
const windows = new WindowManager(data)
const store = createMainStore(data?.globalState)
const appMeta = await getAppMeta()
const lake = new Lake({
const lake = new ElectronZedLake({
root: args.lakeRoot,
port: args.lakePort,
logs: args.lakeLogs,
Expand All @@ -55,7 +56,7 @@ export class MainObject {

// Only call this from boot
constructor(
readonly lake: Lake,
readonly lake: ElectronZedLake,
readonly windows: WindowManager,
readonly store: ReduxStore<State, any>,
readonly session: Session,
Expand Down Expand Up @@ -135,7 +136,7 @@ export class MainObject {
const lakeData = Lakes.id(lakeId)(this.store.getState())
const lake = createLake(lakeData)
const auth = await this.dispatch(getAuthToken(lake))
return new Client(lake.getAddress(), {auth})
return new ElectronZedClient(lake.getAddress(), {auth})
}

async createDefaultClient() {
Expand Down
5 changes: 5 additions & 0 deletions apps/zui/src/test/shared/__mocks__/electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ export const dialog = {
showSaveDialog: jest.fn(),
}

export const net = {
// @ts-ignore
fetch: (...args) => globalThis.fetch(...args),
}

class WebContents extends EventEmitter {
send(channel, ...args) {
ipcRenderer.emitter.emit("receive-from-main", channel, ...args)
Expand Down
2 changes: 1 addition & 1 deletion packages/zed-node/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from '@brimdata/zed-js';

export class Client extends BaseClient {
public fetch = globalThis.fetch;
public fetch = globalThis.fetch.bind(globalThis);

async load(
data: string | NodeJS.ReadableStream,
Expand Down
3 changes: 2 additions & 1 deletion packages/zed-node/src/lake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type ConstructorOpts = {
corsOrigins?: string[];
};
export class Lake {
fetch = globalThis.fetch;
lake?: ChildProcess;
root: string;
port: number;
Expand Down Expand Up @@ -82,7 +83,7 @@ export class Lake {

async isUp() {
try {
const response = await globalThis.fetch(`http://${this.addr()}/status`);
const response = await this.fetch(`http://${this.addr()}/status`);
const text = await response.text();
return text === 'ok';
} catch (e) {
Expand Down

0 comments on commit cc740ad

Please sign in to comment.