Skip to content

Commit def4c72

Browse files
committed
Stop rolling up trailbase-wasm types to avoid duplicating types per module and confusing tsc.
1 parent ada26e2 commit def4c72

File tree

6 files changed

+82
-81
lines changed

6 files changed

+82
-81
lines changed

examples/wasm-guest-js/src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { defineConfig, addPeriodicCallback } from "trailbase-wasm";
2-
import { HttpHandler, JsonResponse } from "trailbase-wasm/http";
2+
import { HttpHandler, HttpResponse } from "trailbase-wasm/http";
33
import { JobHandler } from "trailbase-wasm/job";
44
import { query } from "trailbase-wasm/db";
55

@@ -43,7 +43,7 @@ export default defineConfig({
4343
});
4444

4545
function jsonHandler(req) {
46-
return JsonResponse.from(
46+
return HttpResponse.json(
4747
req.json() ?? {
4848
int: 5,
4949
real: 4.2,

guests/typescript/package.json

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,55 +8,55 @@
88
"exports": {
99
".": {
1010
"import": "./dist/index.js",
11-
"types": "./dist/index.d.ts"
11+
"types": "./dist/src/index.d.ts"
1212
},
1313
"./db": {
1414
"import": "./dist/db.js",
15-
"types": "./dist/db.d.ts"
15+
"types": "./dist/src/db/index.d.ts"
1616
},
1717
"./fs": {
1818
"import": "./dist/fs.js",
19-
"types": "./dist/fs.d.ts"
19+
"types": "./dist/src/fs/index.d.ts"
2020
},
2121
"./http": {
2222
"import": "./dist/http.js",
23-
"types": "./dist/http.d.ts"
23+
"types": "./dist/src/http/index.d.ts"
2424
},
2525
"./job": {
2626
"import": "./dist/job.js",
27-
"types": "./dist/job.d.ts"
27+
"types": "./dist/src/job/index.d.ts"
2828
},
2929
"./kv": {
3030
"import": "./dist/kv.js",
31-
"types": "./dist/kv.d.ts"
31+
"types": "./dist/src/kv/index.d.ts"
3232
}
3333
},
3434
"publishConfig": {
3535
"access": "public",
3636
"exports": {
3737
".": {
3838
"import": "./dist/index.js",
39-
"types": "./dist/index.d.ts"
39+
"types": "./dist/src/index.d.ts"
4040
},
4141
"./db": {
4242
"import": "./dist/db.js",
43-
"types": "./dist/db.d.ts"
43+
"types": "./dist/src/db/index.d.ts"
4444
},
4545
"./fs": {
4646
"import": "./dist/fs.js",
47-
"types": "./dist/fs.d.ts"
47+
"types": "./dist/src/fs/index.d.ts"
4848
},
4949
"./http": {
5050
"import": "./dist/http.js",
51-
"types": "./dist/http.d.ts"
51+
"types": "./dist/src/http/index.d.ts"
5252
},
5353
"./job": {
5454
"import": "./dist/job.js",
55-
"types": "./dist/job.d.ts"
55+
"types": "./dist/src/job/index.d.ts"
5656
},
5757
"./kv": {
5858
"import": "./dist/kv.js",
59-
"types": "./dist/kv.d.ts"
59+
"types": "./dist/src/kv/index.d.ts"
6060
}
6161
}
6262
},

guests/typescript/src/http/incoming.ts

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@ import {
88
import type { HttpContext } from "@common/HttpContext";
99
import type { HttpHandlerInterface, ResponseType } from "./index";
1010
import { StatusCode } from "./index";
11-
import {
12-
HttpError,
13-
responseToOutgoingResponse,
14-
errorToOutgoingResponse,
15-
} from "./response";
11+
import { HttpError, HttpResponse, buildResponse } from "./response";
1612
import { type Method, HttpRequestImpl } from "./request";
1713
import { JobHandlerInterface } from "../job";
1814
import { awaitPendingTimers } from "../timer";
@@ -85,6 +81,54 @@ export function buildIncomingHttpHandler(args: {
8581
};
8682
}
8783

84+
export function responseToOutgoingResponse(
85+
resp: ResponseType,
86+
): OutgoingResponse {
87+
if (resp instanceof OutgoingResponse) {
88+
return resp;
89+
} else if (resp instanceof HttpResponse) {
90+
return buildResponse({
91+
status: resp.status,
92+
headers: resp.headers,
93+
body: resp.body ?? new Uint8Array(),
94+
});
95+
} else if (resp instanceof Uint8Array) {
96+
return buildResponse({
97+
status: StatusCode.OK,
98+
headers: [],
99+
body: resp,
100+
});
101+
} else if (typeof resp === "string") {
102+
return buildResponse({
103+
status: StatusCode.OK,
104+
headers: [],
105+
body: encodeBytes(resp),
106+
});
107+
} else {
108+
// void case.
109+
return buildResponse({
110+
status: StatusCode.OK,
111+
headers: [],
112+
body: new Uint8Array(),
113+
});
114+
}
115+
}
116+
117+
export function errorToOutgoingResponse(err: unknown): OutgoingResponse {
118+
if (err instanceof HttpError) {
119+
return buildResponse({
120+
status: err.status,
121+
headers: [["Content-Type", encodeBytes("text/plain; charset=utf-8")]],
122+
body: err.message ? encodeBytes(err.message) : new Uint8Array(),
123+
});
124+
}
125+
return buildResponse({
126+
body: encodeBytes(`uncaught: ${err}`),
127+
status: StatusCode.INTERNAL_SERVER_ERROR,
128+
headers: [],
129+
});
130+
}
131+
88132
function writeResponse(
89133
responseOutparam: ResponseOutparam,
90134
response: OutgoingResponse,

guests/typescript/src/http/index.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import { OutgoingResponse } from "wasi:http/types@0.2.3";
12
import { HttpRequest } from "./request";
23
import type { Method } from "./request";
3-
import type { ResponseType } from "./response";
4+
import { HttpResponse } from "./response";
45

56
// Override setInterval/setTimeout.
67
import "../timer";
@@ -10,7 +11,13 @@ export { OutgoingResponse } from "wasi:http/types@0.2.3";
1011
export { StatusCode } from "./status";
1112
export type { Method, HttpRequest, Scheme, User } from "./request";
1213
export { HttpResponse, HttpError } from "./response";
13-
export type { ResponseType } from "./response";
14+
15+
export type ResponseType =
16+
| string
17+
| Uint8Array
18+
| HttpResponse
19+
| OutgoingResponse
20+
| void;
1421

1522
export type HttpHandlerCallback = (
1623
req: HttpRequest,

guests/typescript/src/http/response.ts

Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,6 @@ import { Fields, OutgoingBody, OutgoingResponse } from "wasi:http/types@0.2.3";
22
import { StatusCode } from "./status";
33
import { encodeBytes } from "./incoming";
44

5-
export type ResponseType =
6-
| string
7-
| Uint8Array
8-
| HttpResponse
9-
| OutgoingResponse
10-
| void;
11-
125
export class HttpResponse {
136
protected constructor(
147
public readonly status: StatusCode,
@@ -78,61 +71,13 @@ export class HttpError extends Error {
7871
}
7972
}
8073

81-
export function responseToOutgoingResponse(
82-
resp: ResponseType,
83-
): OutgoingResponse {
84-
if (resp instanceof OutgoingResponse) {
85-
return resp;
86-
} else if (resp instanceof HttpResponse) {
87-
return buildResponse({
88-
status: resp.status,
89-
headers: resp.headers,
90-
body: resp.body ?? new Uint8Array(),
91-
});
92-
} else if (resp instanceof Uint8Array) {
93-
return buildResponse({
94-
status: StatusCode.OK,
95-
headers: [],
96-
body: resp,
97-
});
98-
} else if (typeof resp === "string") {
99-
return buildResponse({
100-
status: StatusCode.OK,
101-
headers: [],
102-
body: encodeBytes(resp),
103-
});
104-
} else {
105-
// void case.
106-
return buildResponse({
107-
status: StatusCode.OK,
108-
headers: [],
109-
body: new Uint8Array(),
110-
});
111-
}
112-
}
113-
114-
export function errorToOutgoingResponse(err: unknown): OutgoingResponse {
115-
if (err instanceof HttpError) {
116-
return buildResponse({
117-
status: err.status,
118-
headers: [["Content-Type", encodeBytes("text/plain; charset=utf-8")]],
119-
body: err.message ? encodeBytes(err.message) : new Uint8Array(),
120-
});
121-
}
122-
return buildResponse({
123-
body: encodeBytes(`uncaught: ${err}`),
124-
status: StatusCode.INTERNAL_SERVER_ERROR,
125-
headers: [],
126-
});
127-
}
128-
12974
type ResponseOptions = {
13075
status: StatusCode;
13176
headers: [string, Uint8Array][];
13277
body: Uint8Array;
13378
};
13479

135-
function buildResponse(opts: ResponseOptions): OutgoingResponse {
80+
export function buildResponse(opts: ResponseOptions): OutgoingResponse {
13681
// NOTE: `outputStream.blockingWriteAndFlush` only writes up to 4kB, see documentation.
13782
if (opts.body.length <= 4096) {
13883
return buildSmallResponse(opts);

guests/typescript/vite.config.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,16 @@ export default defineConfig({
2626
},
2727
rollupOptions: {
2828
plugins: [
29-
// NOTE: Needs to be in `rollupOptions` rather than vite's plugins to apply to each input.
3029
dts({
31-
rollupTypes: true,
30+
// NOTE: We could try to roll-up all types into a single declaration file per module,
31+
// however due to cross-module deps this leads to duplicate type declarations,
32+
// e.g. HttpHandlerInterface and all its dependent types are declared both in the
33+
// "index" and "http" modules.
34+
// This confuses the heck out of tsc for downstream users of this library.
35+
// Maybe there are some rollup(Config|Options) that woudl address this :shrug:.
36+
rollupTypes: false,
3237
// NOTE: Include .d.ts files generated by `jco`.
33-
copyDtsFiles: false,
38+
copyDtsFiles: true,
3439
// NOTE: The .d.ts files generated by `jco` contain dynamic imports.
3540
staticImport: true,
3641
}),
@@ -39,7 +44,7 @@ export default defineConfig({
3944
output: {
4045
entryFileNames: "[name].js",
4146
},
42-
external,
47+
external: external,
4348
},
4449
},
4550
});

0 commit comments

Comments
 (0)