Skip to content

Commit b477549

Browse files
authored
Merge pull request #45 from chipmk/fix/http-wasm-url
fix(http): resolve parser wasm from package root
2 parents 6e76304 + f6754fc commit b477549

3 files changed

Lines changed: 15 additions & 21 deletions

File tree

packages/http/src/wasm-url.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const httpParserWasmUrl = new URL(
2+
'../http_parser.wasm',
3+
import.meta.url
4+
);

packages/http/src/wasm/bindings.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, expect, test } from 'vitest';
22
import { HttpParser } from '../parser.js';
3+
import { httpParserWasmUrl } from '../wasm-url.js';
34
import { LlhttpBindings } from './bindings.js';
45

56
const encoder = new TextEncoder();
@@ -29,6 +30,14 @@ async function readText(readable: ReadableStream<Uint8Array>) {
2930
}
3031

3132
describe('LlhttpBindings', () => {
33+
test('uses the package wasm asset URL', () => {
34+
expect(httpParserWasmUrl.pathname).toMatch(/\/http_parser\.wasm$/);
35+
expect(httpParserWasmUrl.pathname).not.toContain('/src/');
36+
expect(httpParserWasmUrl.pathname).not.toMatch(
37+
/\/@tcpip\/http_parser\.wasm$/
38+
);
39+
});
40+
3241
test('parses a response with streamed body bytes', async () => {
3342
const runtime = new LlhttpBindings();
3443
await runtime.ready();

packages/http/src/wasm/bindings.ts

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type {
55
HttpParserRuntime,
66
HttpParserType,
77
} from '../types.js';
8+
import { httpParserWasmUrl } from '../wasm-url.js';
89
import { fetchFile } from './fetch-file.js';
910
import type { LlhttpExports, LlhttpImports, Pointer } from './types.js';
1011

@@ -98,27 +99,7 @@ export class LlhttpBindings implements HttpParserRuntime {
9899
}
99100

100101
async #source() {
101-
// Source tests run from src/wasm; published builds run from dist. Try both
102-
// relative paths back to the package-root wasm asset.
103-
const urls = [
104-
new URL('../../http_parser.wasm', import.meta.url),
105-
new URL('../http_parser.wasm', import.meta.url),
106-
];
107-
108-
let lastError: unknown;
109-
for (const url of urls) {
110-
try {
111-
const response = await fetchFile(url, 'application/wasm');
112-
if (response.ok) {
113-
return response;
114-
}
115-
lastError = new Error(`failed to fetch ${url}: ${response.status}`);
116-
} catch (error) {
117-
lastError = error;
118-
}
119-
}
120-
121-
throw lastError;
102+
return fetchFile(httpParserWasmUrl, 'application/wasm');
122103
}
123104

124105
createParser(type: HttpParserType, callbacks: HttpParserCallbacks) {

0 commit comments

Comments
 (0)