Skip to content

Commit

Permalink
fix: get rid of undefined symbols from the bundled api (#51)
Browse files Browse the repository at this point in the history
* fix: get rid of undefined symbols from the bundled api

Fixes: #50
Signed-off-by: Darshan Sen <[email protected]>

* fixup! fix: apply suggestions from code review

Signed-off-by: Darshan Sen <[email protected]>
  • Loading branch information
RaisinTen authored Oct 13, 2022
1 parent 52502e6 commit 0ef9ef6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
10 changes: 10 additions & 0 deletions scripts/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ await $`esbuild api.js --bundle --platform=node --outfile=../dist/api.js`;
await fs.copy("../src/cli.js", "../dist/cli.js");
await fs.copy("../postject-api.h", "../dist/postject-api.h");

// Repace all occurrences of `__filename` and `__dirname` with "" because
// Node.js core doesn't support it. These uses are functionally dead when
// `SINGLE_FILE` is enabled anyways.
// Refs: https://github.com/postmanlabs/postject/issues/50
// TODO(RaisinTen): Send a PR to emsdk to get rid of these symbols from the
// affected code paths when `SINGLE_FILE` is enabled.
const contents = await fs.readFile("../dist/api.js", "utf-8");
const replaced = contents.replace(/\b__filename\b|\b__dirname\b/g, "''");
await fs.writeFile("../dist/api.js", replaced);

// Build tests
if (!(await fs.exists("./test"))) {
await $`mkdir test`;
Expand Down
16 changes: 16 additions & 0 deletions test/cli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,19 @@ describe("Inject data into Node.js using API", () => {
}
}).timeout(70000);
});

describe("api.js should not contain __filename and __dirname", () => {
let contents;

before(async () => {
contents = await fs.readFile("./dist/api.js", "utf-8");
});

it("should not contain __filename", () => {
expect(contents).to.not.have.string("__filename");
});

it("should not contain __dirname", () => {
expect(contents).to.not.have.string("__dirname");
});
});

0 comments on commit 0ef9ef6

Please sign in to comment.