Skip to content

Commit afdc6e1

Browse files
committed
fix(mobile): restore thread links in the Hermes build
1 parent 1d626d0 commit afdc6e1

2 files changed

Lines changed: 75 additions & 1 deletion

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import * as NodeModule from "node:module";
2+
import * as NodeURL from "node:url";
3+
import * as NodeVM from "node:vm";
4+
import * as Contracts from "@t3tools/contracts";
5+
import * as Option from "effect/Option";
6+
import * as Schema from "effect/Schema";
7+
import { expect, it } from "vite-plus/test";
8+
9+
const require = NodeModule.createRequire(import.meta.url);
10+
const babel: {
11+
transformFileSync: (
12+
filename: string,
13+
options: Record<string, unknown>,
14+
) => { code?: string | null } | null;
15+
} = require("@babel/core");
16+
17+
it("extracts thread links when Hermes clones Babel's named-group regex for matchAll", () => {
18+
const compiled = babel.transformFileSync(
19+
NodeURL.fileURLToPath(
20+
new URL("../../../../packages/shared/src/threadLinks.ts", import.meta.url),
21+
),
22+
{
23+
babelrc: false,
24+
configFile: false,
25+
presets: [[require.resolve("babel-preset-expo"), { enableBabelRuntime: false }]],
26+
caller: {
27+
name: "metro",
28+
bundler: "metro",
29+
platform: "android",
30+
isDev: false,
31+
isServer: false,
32+
isNodeModule: false,
33+
supportsStaticESM: false,
34+
supportsReactCompiler: true,
35+
},
36+
},
37+
);
38+
if (!compiled?.code) throw new Error("Mobile thread links did not compile");
39+
class CloningRegExp extends RegExp {
40+
// Hermes's matchAll clone loses Babel's wrapper and its named-group mapping.
41+
override [Symbol.matchAll](value: string) {
42+
return new RegExp(this.source, this.flags)[Symbol.matchAll](value);
43+
}
44+
}
45+
const exports = {} as typeof import("@t3tools/shared/threadLinks");
46+
const dependencies: Readonly<Record<string, unknown>> = {
47+
"@t3tools/contracts": Contracts,
48+
"effect/Option": Option,
49+
"effect/Schema": Schema,
50+
};
51+
NodeVM.runInNewContext(compiled.code, {
52+
exports,
53+
RegExp: new Proxy(CloningRegExp, {
54+
apply: (target, _thisArg, args) => Reflect.construct(target, args),
55+
}),
56+
require: (name: string) => dependencies[name],
57+
});
58+
const environmentId = Contracts.EnvironmentId.make("here");
59+
expect(
60+
exports.threadLinkRefs(
61+
"[Dictation](t3-thread://v2/here/thread-1) <t3-thread://v2/there/thread-2> `<t3-thread://v2/code/literal>`",
62+
environmentId,
63+
),
64+
).toEqual([
65+
{ environmentId, threadId: "thread-1" },
66+
{ environmentId: "there", threadId: "thread-2" },
67+
]);
68+
});

‎packages/shared/src/threadLinks.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,13 @@ export function threadLinkRefs(
9797
messageEnvironmentId: EnvironmentId,
9898
): ReadonlyArray<ScopedThreadRef> {
9999
const refs = new Map<string, ScopedThreadRef>();
100-
for (const match of markdown.matchAll(THREAD_LINK_OUTSIDE_CODE)) {
100+
// Hermes's matchAll clones Babel's regex wrapper and loses its named groups.
101+
THREAD_LINK_OUTSIDE_CODE.lastIndex = 0;
102+
for (
103+
let match = THREAD_LINK_OUTSIDE_CODE.exec(markdown);
104+
match !== null;
105+
match = THREAD_LINK_OUTSIDE_CODE.exec(markdown)
106+
) {
101107
const href = match.groups?.href ?? match.groups?.autolink;
102108
const link = href === undefined ? null : parseThreadLinkHref(href);
103109
if (link === null) continue;

0 commit comments

Comments
 (0)