Skip to content

Commit aace99f

Browse files
committed
chore: sync Lineage stop with current main
2 parents 6237888 + 9b2c03e commit aace99f

114 files changed

Lines changed: 3363 additions & 835 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apps/desktop/src/electron/ElectronProtocol.test.ts‎

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { assert, describe, it } from "@effect/vitest";
22
import * as Cause from "effect/Cause";
33
import * as Effect from "effect/Effect";
4+
import * as Fiber from "effect/Fiber";
45
import * as NodeServices from "@effect/platform-node/NodeServices";
56
import * as FileSystem from "effect/FileSystem";
67
import * as Layer from "effect/Layer";
8+
import * as TestClock from "effect/testing/TestClock";
79
import { beforeEach, vi } from "vite-plus/test";
810

911
const { handleMock, netFetchMock, unhandleMock } = vi.hoisted(() => ({
@@ -174,7 +176,11 @@ describe("ElectronProtocol", () => {
174176
targetOrigin: new URL("http://127.0.0.1:5733/"),
175177
clerkFrontendApiHostname: undefined,
176178
});
177-
return yield* Effect.promise(() => handler!(new Request("t3code-dev://app/")));
179+
const fiber = yield* Effect.forkChild(
180+
Effect.promise(() => handler!(new Request("t3code-dev://app/"))),
181+
);
182+
yield* TestClock.adjust("50 millis");
183+
return yield* Fiber.join(fiber);
178184
}),
179185
);
180186

@@ -183,6 +189,50 @@ describe("ElectronProtocol", () => {
183189
}).pipe(Effect.provide(layerProtocol)),
184190
);
185191

192+
it.effect("rejects with the last renderer target failure after 50ms and 150ms retries", () =>
193+
Effect.gen(function* () {
194+
let handler: ((request: Request) => Promise<Response>) | undefined;
195+
handleMock.mockImplementation((_scheme, nextHandler) => {
196+
handler = nextHandler;
197+
});
198+
const lastFailure = new Error("connect ECONNREFUSED 127.0.0.1:5733 (3)");
199+
netFetchMock
200+
.mockRejectedValueOnce(new Error("connect ECONNREFUSED 127.0.0.1:5733 (1)"))
201+
.mockRejectedValueOnce(new Error("connect ECONNREFUSED 127.0.0.1:5733 (2)"))
202+
.mockRejectedValueOnce(lastFailure);
203+
204+
const rejection = yield* Effect.scoped(
205+
Effect.gen(function* () {
206+
const protocol = yield* ElectronProtocol.ElectronProtocol;
207+
yield* protocol.registerDesktopProtocol({
208+
scheme: "t3code-dev",
209+
targetOrigin: new URL("http://127.0.0.1:5733/"),
210+
clerkFrontendApiHostname: undefined,
211+
});
212+
const fiber = yield* Effect.forkChild(
213+
Effect.promise(() =>
214+
handler!(new Request("t3code-dev://app/")).then(
215+
() => null,
216+
(error: unknown) => error,
217+
),
218+
),
219+
);
220+
yield* TestClock.adjust("49 millis");
221+
assert.equal(netFetchMock.mock.calls.length, 1);
222+
yield* TestClock.adjust("1 millis");
223+
assert.equal(netFetchMock.mock.calls.length, 2);
224+
yield* TestClock.adjust("149 millis");
225+
assert.equal(netFetchMock.mock.calls.length, 2);
226+
yield* TestClock.adjust("1 millis");
227+
return yield* Fiber.join(fiber);
228+
}),
229+
);
230+
231+
assert.strictEqual(rejection, lastFailure);
232+
assert.equal(netFetchMock.mock.calls.length, 3);
233+
}).pipe(Effect.provide(layerProtocol)),
234+
);
235+
186236
it.effect("preserves protocol registration failures", () =>
187237
Effect.gen(function* () {
188238
const cause = new Error("protocol registration failed");

‎apps/desktop/src/electron/ElectronProtocol.ts‎

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import * as Effect from "effect/Effect";
33
import * as FileSystem from "effect/FileSystem";
44
import * as Layer from "effect/Layer";
55
import * as Option from "effect/Option";
6-
import * as NodeTimersPromises from "node:timers/promises";
76
import * as Path from "effect/Path";
87
import * as Mime from "effect/http/Mime";
98
import * as Ref from "effect/Ref";
9+
import * as Schedule from "effect/Schedule";
1010
import * as Schema from "effect/Schema";
1111
import * as Scope from "effect/Scope";
1212

@@ -150,11 +150,29 @@ const registerDesktopSchemePrivileges = Effect.sync(registerDesktopSchemePrivile
150150

151151
export const layerSchemePrivileges = Layer.effectDiscard(registerDesktopSchemePrivileges);
152152

153-
async function proxyRequest(
153+
class ElectronProtocolFetchError extends Schema.TaggedError<ElectronProtocolFetchError>()(
154+
"ElectronProtocolFetchError",
155+
{ cause: Schema.Defect() },
156+
) {}
157+
158+
const netFetch = (url: string, init: RequestInit) =>
159+
Effect.tryPromise({
160+
try: () => Electron.net.fetch(url, init),
161+
catch: (cause) => new ElectronProtocolFetchError({ cause }),
162+
});
163+
164+
// The dev renderer target can briefly refuse connections while Vite restarts:
165+
// retry idempotent requests after 50ms, then 150ms, and keep the last failure.
166+
const fetchWithTransientRetry = (url: string, init: RequestInit) =>
167+
netFetch(url, init).pipe(
168+
Effect.retry({ schedule: Schedule.exponential("50 millis", 3), times: 2 }),
169+
);
170+
171+
const proxyRequest = Effect.fn("desktop.protocol.proxyRequest")(function* (
154172
request: Request,
155173
targetOrigin: URL,
156174
contentSecurityPolicy: string,
157-
): Promise<Response> {
175+
) {
158176
const requestUrl = new URL(request.url);
159177
if (requestUrl.host !== DESKTOP_HOST) {
160178
return new Response(null, { status: 404 });
@@ -190,12 +208,10 @@ async function proxyRequest(
190208
}
191209
const response =
192210
request.method === "GET" || request.method === "HEAD"
193-
? await fetchWithTransientRetry(targetUrl.toString(), init)
194-
: await Electron.net.fetch(targetUrl.toString(), init);
211+
? yield* fetchWithTransientRetry(targetUrl.toString(), init)
212+
: yield* netFetch(targetUrl.toString(), init);
195213
return withContentSecurityPolicy(response, contentSecurityPolicy);
196-
}
197-
198-
const TRANSIENT_FETCH_RETRY_DELAYS_MS = [0, 50, 150] as const;
214+
});
199215

200216
// Serves the packaged web client without a backend: files resolve within the
201217
// asset directory, and any other path falls back to index.html so the SPA
@@ -238,24 +254,6 @@ const serveDesktopAsset = Effect.fn("desktop.protocol.serveAsset")(function* (
238254
});
239255
});
240256

241-
async function fetchWithTransientRetry(url: string, init: RequestInit): Promise<Response> {
242-
let lastError: unknown;
243-
244-
for (const delayMs of TRANSIENT_FETCH_RETRY_DELAYS_MS) {
245-
if (delayMs > 0) {
246-
await NodeTimersPromises.setTimeout(delayMs);
247-
}
248-
249-
try {
250-
return await Electron.net.fetch(url, init);
251-
} catch (error) {
252-
lastError = error;
253-
}
254-
}
255-
256-
throw lastError;
257-
}
258-
259257
/** @public Service construction is part of the canonical Effect module API. */
260258
export const make = Effect.gen(function* () {
261259
const registered = yield* Ref.make(false);
@@ -278,7 +276,14 @@ export const make = Effect.gen(function* () {
278276
contentSecurityPolicy,
279277
);
280278
}
281-
return proxyRequest(request, input.targetOrigin, contentSecurityPolicy);
279+
// Reject with net.fetch's own error, as an unproxied fetch would.
280+
return runPromise(
281+
proxyRequest(request, input.targetOrigin, contentSecurityPolicy).pipe(
282+
Effect.catchTags({
283+
ElectronProtocolFetchError: (error) => Effect.die(error.cause),
284+
}),
285+
),
286+
);
282287
});
283288
},
284289
catch: (cause) => new ElectronProtocolRegistrationError({ scheme: input.scheme, cause }),

‎apps/desktop/src/ipc/channels.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export const SET_WSL_BACKEND_ENABLED_CHANNEL = "desktop:set-wsl-backend-enabled"
6868
export const SET_WSL_DISTRO_CHANNEL = "desktop:set-wsl-distro";
6969
export const SET_WSL_ONLY_CHANNEL = "desktop:set-wsl-only";
7070
export const SSH_PASSWORD_PROMPT_CANCELLED_RESULT = "ssh-password-prompt-cancelled";
71+
export const PREVIEW_SET_FORWARDED_SHORTCUTS_CHANNEL = "desktop:preview-set-forwarded-shortcuts";
7172
export const PREVIEW_CREATE_TAB_CHANNEL = "desktop:preview-create-tab";
7273
export const PREVIEW_CLOSE_TAB_CHANNEL = "desktop:preview-close-tab";
7374
export const PREVIEW_REGISTER_WEBVIEW_CHANNEL = "desktop:preview-register-webview";

‎apps/desktop/src/ipc/methods/preview.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import {
2020
PreviewAnnotationSubmissionResultSchema,
2121
DEFAULT_BROWSER_PROFILE_ID,
2222
INCOGNITO_BROWSER_PROFILE_ID,
23+
PreviewForwardedShortcut,
24+
MAX_KEYBINDINGS_COUNT,
2325
} from "@t3tools/contracts";
2426
import * as Effect from "effect/Effect";
2527
import * as Schema from "effect/Schema";
@@ -53,6 +55,16 @@ export const installPreviewEventForwarding = Effect.fn(
5355
);
5456
});
5557

58+
export const setForwardedShortcuts = DesktopIpc.makeIpcMethod({
59+
channel: IpcChannels.PREVIEW_SET_FORWARDED_SHORTCUTS_CHANNEL,
60+
payload: Schema.Array(PreviewForwardedShortcut).check(Schema.isMaxLength(MAX_KEYBINDINGS_COUNT)),
61+
result: Schema.Void,
62+
handler: Effect.fn("desktop.ipc.preview.setForwardedShortcuts")(function* (shortcuts) {
63+
const manager = yield* PreviewManager.PreviewManager;
64+
yield* manager.setForwardedShortcuts(shortcuts);
65+
}),
66+
});
67+
5668
export const createTab = DesktopIpc.makeIpcMethod({
5769
channel: IpcChannels.PREVIEW_CREATE_TAB_CHANNEL,
5870
payload: DesktopPreviewCreateTabInputSchema,
@@ -406,6 +418,7 @@ export const saveRecording = DesktopIpc.makeIpcMethod({
406418
});
407419

408420
export const methods = [
421+
setForwardedShortcuts,
409422
createTab,
410423
closeTab,
411424
registerWebview,

‎apps/desktop/src/preload.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,8 @@ contextBridge.exposeInMainWorld("desktopBridge", {
299299
},
300300
},
301301
preview: {
302+
setForwardedShortcuts: (shortcuts) =>
303+
ipcRenderer.invoke(IpcChannels.PREVIEW_SET_FORWARDED_SHORTCUTS_CHANNEL, shortcuts),
302304
createTab: (tabId, defaults) =>
303305
ipcRenderer.invoke(IpcChannels.PREVIEW_CREATE_TAB_CHANNEL, {
304306
tabId,

0 commit comments

Comments
 (0)