Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support reading embedded files in compiled executables #3405

Merged
merged 2 commits into from Jun 25, 2023

Conversation

Jarred-Sumner
Copy link
Collaborator

@Jarred-Sumner Jarred-Sumner commented Jun 25, 2023

This adds support for reading files embedded in executables created via bun build --compile.

The following snippet works now:

bun build --compile ./hello.ts
// hello.ts
import index from "./file.html";
import pic from "./favicon.png";
import { readFileSync } from "node:fs";
import { file, serve } from "bun";

serve({
  fetch(req) {
    const { pathname } = new URL(req.url);
    if (pathname === "/") {
      return new Response(file(index));
    }

    if (pathname === "/favicon.png") {
      return new Response(file(pic));
    }

    return new Response("Not found", { status: 404 });
  },
});

// Works with readFileSync() as well.
console.log("index.html is", readFileSync(index).byteLength, "bytes");

This adds support for Bun.file(path) and readFileSync(path). it doesn't add support for the rest of the node:fs functions.

Confusingly, outside of bun build --compile, the snippet above doesn't work without setting .html and .png to be a file loader:

bun --loader=.html:file --loader=.png:file ./hello.ts

@github-actions
Copy link

@Jarred-Sumner 6 files with test failures on linux-x64-baseline:

  • test/cli/install/bunx.test.ts
  • test/js/bun/resolve/import-meta.test.js
  • test/js/bun/websocket/websocket-server.test.ts
  • test/js/third_party/prisma/prisma.test.ts
  • test/js/web/fetch/fetch-leak.test.js
  • test/js/web/websocket/websocket.test.js

View test output

#66d2f4dcd38a2696d9a7d439b3de6888c3dbf5ff

@github-actions
Copy link

@Jarred-Sumner 6 files with test failures on linux-x64:

  • test/cli/install/bunx.test.ts
  • test/js/bun/resolve/import-meta.test.js
  • test/js/bun/websocket/websocket-server.test.ts
  • test/js/third_party/prisma/prisma.test.ts
  • test/js/web/fetch/fetch-leak.test.js
  • test/js/web/websocket/websocket.test.js

View test output

#66d2f4dcd38a2696d9a7d439b3de6888c3dbf5ff

@github-actions
Copy link

@Jarred-Sumner 8 files with test failures on bun-darwin-x64-baseline:

  • test/cli/install/bunx.test.ts
  • test/js/bun/resolve/import-meta.test.js
  • test/js/bun/spawn/spawn-streaming-stdin.test.ts
  • test/js/bun/sqlite/sqlite.test.js
  • test/js/bun/util/sleepSync.test.ts
  • test/js/bun/websocket/websocket-server.test.ts
  • test/js/web/timers/setTimeout.test.js
  • test/js/web/websocket/websocket.test.js

View test output

#66d2f4dcd38a2696d9a7d439b3de6888c3dbf5ff

@github-actions
Copy link

@Jarred-Sumner 7 files with test failures on bun-darwin-aarch64:

  • test/cli/install/bunx.test.ts
  • test/js/bun/net/socket.test.ts
  • test/js/bun/resolve/import-meta.test.js
  • test/js/bun/test/test-test.test.ts
  • test/js/bun/websocket/websocket-server.test.ts
  • test/js/web/fetch/fetch-leak.test.js
  • test/js/web/websocket/websocket.test.js

View test output

#66d2f4dcd38a2696d9a7d439b3de6888c3dbf5ff

Copy link
Contributor

@alexlamsl alexlamsl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

more bun.String goodness 👍

@Jarred-Sumner Jarred-Sumner merged commit fdfbb18 into main Jun 25, 2023
7 of 12 checks passed
@Jarred-Sumner Jarred-Sumner deleted the jarred/standalone-files branch June 25, 2023 23:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants