Skip to content

fix: torii-wasm node import #455

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

Merged
merged 1 commit into from
Jun 16, 2025
Merged

Conversation

MartianGreed
Copy link
Collaborator

@MartianGreed MartianGreed commented Jun 16, 2025

Closes #

Introduced changes

Checklist

  • Linked relevant issue
  • Updated relevant documentation
  • Added relevant tests
  • Add a dedicated CI job for new examples
  • Performed self-review of the code

Summary by CodeRabbit

  • Bug Fixes

    • Resolved an import issue with torii-wasm when used from node-worker environments.
  • Chores

    • Updated package configuration to improve Node.js interoperability and adjusted export paths for better compatibility.

Copy link

coderabbitai bot commented Jun 16, 2025

Walkthrough

This change introduces a new ES module wrapper for the torii-wasm package to fix import issues from Node.js workers. It adds a node.mjs file to re-export the CommonJS module, updates package export mappings, and modifies the package configuration to support the new entry point.

Changes

File(s) Change Summary
packages/torii-wasm/node.mjs Added ES module wrapper that re-exports all named and default exports from the CommonJS dojo_c.js module.
packages/torii-wasm/package.json Removed "type": "module", added "./node.mjs" to files, and updated exports to use the new wrapper file.
.changeset/shaky-wasps-invent.md Added changeset documenting patch updates and the import fix for torii-wasm in Node.js worker environments.

Sequence Diagram(s)

sequenceDiagram
    participant NodeWorker
    participant node.mjs (ESM)
    participant dojo_c.js (CJS)

    NodeWorker->>node.mjs (ESM): import { ... } from 'torii-wasm/node'
    node.mjs (ESM)->>dojo_c.js (CJS): createRequire + require('dojo_c.js')
    node.mjs (ESM)-->>NodeWorker: re-export all named and default exports
Loading

Possibly related PRs

  • chore: bump dojo.c + fix node worker #447: Addresses fixes related to Node.js worker and updates to the torii-wasm package, directly connected to Node.js interoperability and module exports.
  • feat: allow wasm in nodejs #253: Focuses on enabling WebAssembly builds for Node.js and updating package.json for separate web and node targets, related through Node.js integration changes in torii-wasm.

Poem

A wrapper hops in, so neat and spry,
Node and WASM now see eye to eye.
Exports re-shuffled, imports are clear,
No more worker woes to fear!
With .mjs in tow,
The modules all flow—
A patch well done, let’s cheer! 🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (4)
.changeset/shaky-wasps-invent.md (2)

1-13: Align changeset scope with summary
The changeset bumps patch versions for multiple packages (core, SDK, react, etc.) but the summary only mentions torii-wasm. Consider updating the summary to reflect the multi-package patch release or restrict the changeset to only torii-wasm if that was the intent.


15-15: Enhance changeset description
Add more context to the summary line, for example: “add Node.js ESM wrapper via node.mjs for torii-wasm import fix” to improve clarity in release notes.

packages/torii-wasm/node.mjs (2)

1-3: Use explicit node: protocol for built-ins
Node.js recommends using the node: specifier for built-in modules.

-import { createRequire } from 'module';
+import { createRequire } from 'node:module';

5-29: Consider simplifying re-exports
Manually enumerating every export from dojo_c can become brittle as the API evolves. You might replace the manual list with a generic re-export approach:

import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);
const dojo_c = require('./pkg/node/dojo_c.js');

export * from './pkg/node/dojo_c.js';
export { default } from './pkg/node/dojo_c.js';

This automatically surfaces new exports without needing to update this file each time.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1d506e7 and 0c20223.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (3)
  • .changeset/shaky-wasps-invent.md (1 hunks)
  • packages/torii-wasm/node.mjs (1 hunks)
  • packages/torii-wasm/package.json (2 hunks)
🧰 Additional context used
🪛 Biome (1.9.4)
packages/torii-wasm/node.mjs

[error] 1-1: A Node.js builtin module should be imported with the node: protocol.

Using the node: protocol is more explicit and signals that the imported module belongs to Node.js.
Unsafe fix: Add the node: protocol.

(lint/style/useNodejsImportProtocol)

⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: check
  • GitHub Check: build
🔇 Additional comments (2)
packages/torii-wasm/package.json (2)

25-26: ```shell
#!/bin/bash

Re-verify that node.mjs is included by running npm pack in the package directory

cd packages/torii-wasm
npm pack --dry-run | grep node.mjs


---

`34-38`: **Validate dual export for Node subpath**  
The `./node` export now points `"import": "./node.mjs"` and `"require": "./pkg/node/dojo_c.js"`. Confirm both ESM (`import '@dojoengine/torii-wasm/node'`) and CJS (`require('@dojoengine/torii-wasm/node')`) resolutions behave as expected, especially in worker contexts.  

```shell
#!/bin/bash
# Check ESM import
node -e "import('@dojoengine/torii-wasm/node').then(m => console.log('ESM load OK')).catch(err => console.error(err))"
# Check CJS require
node -e "console.log('CJS load OK', require('@dojoengine/torii-wasm/node'))"

@MartianGreed MartianGreed merged commit 5143b5e into main Jun 16, 2025
6 checks passed
@MartianGreed MartianGreed deleted the fix/node-worker-torii-wasm-import branch June 16, 2025 15:26
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.

1 participant