Skip to content

Commit 05ca733

Browse files
authored
Merge pull request GoogleChromeLabs#453 from felixfbecker/throw-scalar-bug
2 parents cda7b24 + a88f600 commit 05ca733

File tree

2 files changed

+32
-20
lines changed

2 files changed

+32
-20
lines changed

src/comlink.ts

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export { Endpoint };
2525
export const proxyMarker = Symbol("Comlink.proxy");
2626
export const createEndpoint = Symbol("Comlink.endpoint");
2727
export const releaseProxy = Symbol("Comlink.releaseProxy");
28-
const throwSet = new WeakSet();
28+
const throwMarker = Symbol("Comlink.thrown");
2929

3030
// prettier-ignore
3131
type Promisify<T> =
@@ -100,24 +100,23 @@ export const transferHandlers = new Map<string, TransferHandler>([
100100
[
101101
"throw",
102102
{
103-
canHandle: obj => throwSet.has(obj),
104-
serialize(obj) {
105-
const isError = obj instanceof Error;
106-
let serialized = obj;
103+
canHandle: obj => typeof obj === "object" && throwMarker in obj,
104+
serialize({ value }) {
105+
const isError = value instanceof Error;
106+
let serialized = { isError, value };
107107
if (isError) {
108-
serialized = {
109-
isError,
110-
message: obj.message,
111-
stack: obj.stack
108+
serialized.value = {
109+
message: value.message,
110+
stack: value.stack
112111
};
113112
}
114113
return [serialized, []];
115114
},
116-
deserialize(obj) {
117-
if ((obj as any).isError) {
118-
throw Object.assign(new Error(), obj);
115+
deserialize(serialized) {
116+
if (serialized.isError) {
117+
throw Object.assign(new Error(), serialized.value);
119118
}
120-
throw obj;
119+
throw serialized.value;
121120
}
122121
}
123122
]
@@ -173,14 +172,12 @@ export function expose(obj: any, ep: Endpoint = self as any) {
173172
}
174173
break;
175174
}
176-
} catch (e) {
177-
returnValue = e;
178-
throwSet.add(e);
175+
} catch (value) {
176+
returnValue = { value, [throwMarker]: 0 };
179177
}
180178
Promise.resolve(returnValue)
181-
.catch(e => {
182-
throwSet.add(e);
183-
return e;
179+
.catch(value => {
180+
return { value, [throwMarker]: 0 };
184181
})
185182
.then(returnValue => {
186183
const [wireValue, transferables] = toWireValue(returnValue);

tests/same_window.comlink.test.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,26 @@ describe("Comlink in the same realm", function() {
142142
await thing();
143143
throw "Should have thrown";
144144
} catch (err) {
145-
expect(err).to.not.eq("Should have thrown");
145+
expect(err).to.not.equal("Should have thrown");
146146
expect(err.test).to.equal(true);
147147
}
148148
});
149149

150+
it("can rethrow scalars", async function() {
151+
const thing = Comlink.wrap(this.port1);
152+
Comlink.expose(_ => {
153+
throw "oops";
154+
}, this.port2);
155+
try {
156+
await thing();
157+
throw "Should have thrown";
158+
} catch (err) {
159+
expect(err).to.not.equal("Should have thrown");
160+
expect(err).to.equal("oops");
161+
expect(typeof err).to.equal("string");
162+
}
163+
});
164+
150165
it("can work with parameterized functions", async function() {
151166
const thing = Comlink.wrap(this.port1);
152167
Comlink.expose((a, b) => a + b, this.port2);

0 commit comments

Comments
 (0)