mirror of
https://github.com/oven-sh/bun
synced 2026-02-15 05:12:29 +00:00
@tr1ckydev this fixes the issue you ran into, see the diff for an example usage of a Bun.plugin that makes a network request on import.
This commit is contained in:
@@ -1525,12 +1525,14 @@ pub const ModuleLoader = struct {
|
||||
var slice = slice_;
|
||||
if (slice.len == 0) return slice;
|
||||
var was_http = false;
|
||||
if (strings.hasPrefixComptime(slice, "https://")) {
|
||||
slice = slice["https://".len..];
|
||||
was_http = true;
|
||||
} else if (strings.hasPrefixComptime(slice, "http://")) {
|
||||
slice = slice["http://".len..];
|
||||
was_http = true;
|
||||
if (jsc_vm.bundler.options.serve) {
|
||||
if (strings.hasPrefixComptime(slice, "https://")) {
|
||||
slice = slice["https://".len..];
|
||||
was_http = true;
|
||||
} else if (strings.hasPrefixComptime(slice, "http://")) {
|
||||
slice = slice["http://".len..];
|
||||
was_http = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (strings.hasPrefix(slice, jsc_vm.origin.host)) {
|
||||
|
||||
@@ -13,6 +13,26 @@ declare global {
|
||||
var asyncret: any;
|
||||
}
|
||||
|
||||
plugin({
|
||||
name: "url text file loader",
|
||||
setup(builder) {
|
||||
builder.onResolve({ namespace: "http", filter: /.*/ }, ({ path }) => {
|
||||
return {
|
||||
path,
|
||||
namespace: "url",
|
||||
};
|
||||
});
|
||||
|
||||
builder.onLoad({ filter: /.*/, namespace: "url" }, async ({ path, namespace }) => {
|
||||
const res = await fetch("http://" + path);
|
||||
return {
|
||||
exports: { default: await res.text() },
|
||||
loader: "object",
|
||||
};
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
plugin({
|
||||
name: "boop beep beep",
|
||||
setup(builder) {
|
||||
@@ -313,4 +333,37 @@ describe("errors", () => {
|
||||
throw -1;
|
||||
}).toThrow('Cannot find package "');
|
||||
});
|
||||
|
||||
it("can work with http urls", async () => {
|
||||
const result = `The Mysterious Affair at Styles
|
||||
The Secret Adversary
|
||||
The Murder on the Links
|
||||
The Man in the Brown Suit
|
||||
The Secret of Chimneys
|
||||
The Murder of Roger Ackroyd
|
||||
The Big Four
|
||||
The Mystery of the Blue Train
|
||||
The Seven Dials Mystery
|
||||
The Murder at the Vicarage
|
||||
Giant's Bread
|
||||
The Floating Admiral
|
||||
The Sittaford Mystery
|
||||
Peril at End House
|
||||
Lord Edgware Dies
|
||||
Murder on the Orient Express
|
||||
Unfinished Portrait
|
||||
Why Didn't They Ask Evans?
|
||||
Three Act Tragedy
|
||||
Death in the Clouds`;
|
||||
|
||||
const server = Bun.serve({
|
||||
port: 0,
|
||||
fetch(req, server) {
|
||||
server.stop();
|
||||
return new Response(result);
|
||||
},
|
||||
});
|
||||
const { default: text } = await import(`http://${server.hostname}:${server.port}/hey.txt`);
|
||||
expect(text).toBe(result);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user