Skip to content

Commit da7db22

Browse files
fix(nitro) fix sourcemaps and JSSink closing (#5422)
* fix JSSink progress on sourcemap checking * fix sourcemaps * update JSSink fix * undo + tests --------- Co-authored-by: Jarred Sumner <[email protected]>
1 parent 94e9f8b commit da7db22

File tree

6 files changed

+5696
-33
lines changed

6 files changed

+5696
-33
lines changed

src/bun.js/bindings/JSSink.cpp

+32-24
Original file line numberDiff line numberDiff line change
@@ -798,17 +798,19 @@ void JSReadableArrayBufferSinkController::detach()
798798

799799
auto readableStream = m_weakReadableStream.get();
800800
auto onClose = m_onClose.get();
801-
m_onClose.clear();
802801

803802
if (readableStream && onClose) {
804-
JSC::JSGlobalObject* globalObject = this->globalObject();
805803
auto callData = JSC::getCallData(onClose);
806-
JSC::MarkedArgumentBuffer arguments;
807-
arguments.append(readableStream);
808-
arguments.append(jsUndefined());
809-
call(globalObject, onClose, callData, JSC::jsUndefined(), arguments);
804+
if (callData.type != JSC::CallData::Type::None) {
805+
JSC::JSGlobalObject* globalObject = this->globalObject();
806+
JSC::MarkedArgumentBuffer arguments;
807+
arguments.append(readableStream);
808+
arguments.append(jsUndefined());
809+
call(globalObject, onClose, callData, JSC::jsUndefined(), arguments);
810+
}
810811
}
811812

813+
m_onClose.clear();
812814
m_weakReadableStream.clear();
813815
}
814816

@@ -1052,17 +1054,19 @@ void JSReadableFileSinkController::detach()
10521054

10531055
auto readableStream = m_weakReadableStream.get();
10541056
auto onClose = m_onClose.get();
1055-
m_onClose.clear();
10561057

10571058
if (readableStream && onClose) {
1058-
JSC::JSGlobalObject* globalObject = this->globalObject();
10591059
auto callData = JSC::getCallData(onClose);
1060-
JSC::MarkedArgumentBuffer arguments;
1061-
arguments.append(readableStream);
1062-
arguments.append(jsUndefined());
1063-
call(globalObject, onClose, callData, JSC::jsUndefined(), arguments);
1060+
if (callData.type != JSC::CallData::Type::None) {
1061+
JSC::JSGlobalObject* globalObject = this->globalObject();
1062+
JSC::MarkedArgumentBuffer arguments;
1063+
arguments.append(readableStream);
1064+
arguments.append(jsUndefined());
1065+
call(globalObject, onClose, callData, JSC::jsUndefined(), arguments);
1066+
}
10641067
}
10651068

1069+
m_onClose.clear();
10661070
m_weakReadableStream.clear();
10671071
}
10681072

@@ -1306,17 +1310,19 @@ void JSReadableHTTPResponseSinkController::detach()
13061310

13071311
auto readableStream = m_weakReadableStream.get();
13081312
auto onClose = m_onClose.get();
1309-
m_onClose.clear();
13101313

13111314
if (readableStream && onClose) {
1312-
JSC::JSGlobalObject* globalObject = this->globalObject();
13131315
auto callData = JSC::getCallData(onClose);
1314-
JSC::MarkedArgumentBuffer arguments;
1315-
arguments.append(readableStream);
1316-
arguments.append(jsUndefined());
1317-
call(globalObject, onClose, callData, JSC::jsUndefined(), arguments);
1316+
if (callData.type != JSC::CallData::Type::None) {
1317+
JSC::JSGlobalObject* globalObject = this->globalObject();
1318+
JSC::MarkedArgumentBuffer arguments;
1319+
arguments.append(readableStream);
1320+
arguments.append(jsUndefined());
1321+
call(globalObject, onClose, callData, JSC::jsUndefined(), arguments);
1322+
}
13181323
}
13191324

1325+
m_onClose.clear();
13201326
m_weakReadableStream.clear();
13211327
}
13221328

@@ -1560,17 +1566,19 @@ void JSReadableHTTPSResponseSinkController::detach()
15601566

15611567
auto readableStream = m_weakReadableStream.get();
15621568
auto onClose = m_onClose.get();
1563-
m_onClose.clear();
15641569

15651570
if (readableStream && onClose) {
1566-
JSC::JSGlobalObject* globalObject = this->globalObject();
15671571
auto callData = JSC::getCallData(onClose);
1568-
JSC::MarkedArgumentBuffer arguments;
1569-
arguments.append(readableStream);
1570-
arguments.append(jsUndefined());
1571-
call(globalObject, onClose, callData, JSC::jsUndefined(), arguments);
1572+
if (callData.type != JSC::CallData::Type::None) {
1573+
JSC::JSGlobalObject* globalObject = this->globalObject();
1574+
JSC::MarkedArgumentBuffer arguments;
1575+
arguments.append(readableStream);
1576+
arguments.append(jsUndefined());
1577+
call(globalObject, onClose, callData, JSC::jsUndefined(), arguments);
1578+
}
15721579
}
15731580

1581+
m_onClose.clear();
15741582
m_weakReadableStream.clear();
15751583
}
15761584

src/bun.js/scripts/generate-jssink.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -636,17 +636,19 @@ void JS${controllerName}::detach() {
636636
637637
auto readableStream = m_weakReadableStream.get();
638638
auto onClose = m_onClose.get();
639-
m_onClose.clear();
640639
641640
if (readableStream && onClose) {
642-
JSC::JSGlobalObject *globalObject = this->globalObject();
643641
auto callData = JSC::getCallData(onClose);
644-
JSC::MarkedArgumentBuffer arguments;
645-
arguments.append(readableStream);
646-
arguments.append(jsUndefined());
647-
call(globalObject, onClose, callData, JSC::jsUndefined(), arguments);
642+
if(callData.type != JSC::CallData::Type::None) {
643+
JSC::JSGlobalObject *globalObject = this->globalObject();
644+
JSC::MarkedArgumentBuffer arguments;
645+
arguments.append(readableStream);
646+
arguments.append(jsUndefined());
647+
call(globalObject, onClose, callData, JSC::jsUndefined(), arguments);
648+
}
648649
}
649-
650+
651+
m_onClose.clear();
650652
m_weakReadableStream.clear();
651653
}
652654
`;

src/sourcemap/sourcemap.zig

+2-2
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,7 @@ pub const LineOffsetTable = struct {
940940
contents.ptr,
941941
)) - line_byte_offset),
942942
);
943-
byte_offset_to_first_non_ascii = line_byte_offset;
943+
byte_offset_to_first_non_ascii = column_byte_offset;
944944
}
945945

946946
// Update the per-byte column offsets
@@ -1405,7 +1405,7 @@ pub const Chunk = struct {
14051405
// Use the line to compute the column
14061406
var original_column = loc.start - @as(i32, @intCast(line.byte_offset_to_start_of_line));
14071407
if (line.columns_for_non_ascii.len > 0 and original_column >= @as(i32, @intCast(line.byte_offset_to_first_non_ascii))) {
1408-
original_column = line.columns_for_non_ascii.ptr[@as(u32, @intCast(original_column)) - line.byte_offset_to_first_non_ascii];
1408+
original_column = line.columns_for_non_ascii.slice()[@as(u32, @intCast(original_column)) - line.byte_offset_to_first_non_ascii];
14091409
}
14101410

14111411
b.updateGeneratedLineAndColumn(output);

test/js/bun/http/bun-server.test.ts

+11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, expect, test } from "bun:test";
22
import { bunExe, bunEnv } from "harness";
3+
import path from "path";
34

45
describe("Server", () => {
56
test("normlizes incoming request URLs", async () => {
@@ -368,4 +369,14 @@ describe("Server", () => {
368369
await proc.exited;
369370
expect(proc.exitCode).toBe(0);
370371
});
372+
373+
test("should be able to parse source map and fetch small stream", async () => {
374+
const proc = Bun.spawn({
375+
cmd: [bunExe(), path.join("js-sink-sourmap-fixture", "index.mjs")],
376+
cwd: import.meta.dir,
377+
env: bunEnv,
378+
});
379+
await proc.exited;
380+
expect(proc.exitCode).toBe(0);
381+
});
371382
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { e as eventHandler } from "../index.mjs";
2+
import "fs";
3+
import "path";
4+
import "node:async_hooks";
5+
import "node:fs";
6+
import "node:url";
7+
8+
const stream = eventHandler(() => {
9+
const encoder = new TextEncoder();
10+
const stream = new ReadableStream({
11+
start(controller) {
12+
controller.enqueue(encoder.encode("nitro"));
13+
controller.enqueue(encoder.encode("is"));
14+
controller.enqueue(encoder.encode("awesome"));
15+
controller.close();
16+
},
17+
});
18+
return stream;
19+
});
20+
21+
export { stream as default };
22+
//# sourceMappingURL=stream.mjs.map

0 commit comments

Comments
 (0)