{"_id":"cf-mongodb-polyfills","name":"cf-mongodb-polyfills","dist-tags":{"latest":"1.0.1"},"versions":{"1.0.1":{"name":"cf-mongodb-polyfills","description":"cloudflare worker polyfill for net, tls, and dns for making mongodb connections work in workerd","repository":{"type":"git","url":"git+https://github.com/jchoi2x/cf-mongodb-polyfills.git"},"version":"1.0.1","main":"./dist/index.ts","exports":{".":{"import":"./src/index.ts","require":"./src/index.ts"},"./net":{"import":"./src/net.ts","require":"./src/net.ts"},"./tls":{"import":"./src/tls.ts","require":"./src/tls.ts"},"./dns":{"import":"./src/dns.ts","require":"./src/dns.ts"},"./net-mock":{"import":"./src/net-mock.ts","require":"./src/net-mock.ts"}},"scripts":{},"keywords":["cloudflare","worker","mongodb","polyfill"],"author":{"name":"James Choi","email":"choijjames@gmail.com"},"license":"Apache-2.0","devDependencies":{"@cloudflare/workers-types":"^4.20241205.0","@types/node":"^22.10.2","ts-node":"^10.9.2","typescript":"^5.7.2"},"_id":"cf-mongodb-polyfills@1.0.1","gitHead":"186c92fb0e5248c3e0d80e692a10175a06114a63","bugs":{"url":"https://github.com/jchoi2x/cf-mongodb-polyfills/issues"},"homepage":"https://github.com/jchoi2x/cf-mongodb-polyfills#readme","_nodeVersion":"23.3.0","_npmVersion":"10.9.0","dist":{"integrity":"sha512-9K/bpTWI32zrHGwV1IXRlKN85Q1V1e8DQn+blzbjKBCTUtBpyejll1nt+ZH7zQRuLuBjVnKcqQwhXzuk92BihA==","shasum":"9316267dee9bc67012ada5dd926fa5b175c1225e","tarball":"https://registry.npmjs.org/cf-mongodb-polyfills/-/cf-mongodb-polyfills-1.0.1.tgz","fileCount":8,"unpackedSize":29849,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCZn/BWqty7I/XeGt6l4G//8aoZVNaHRZWNcRoCAq2sgQIhAKtn8zJOZNGwPqXbJ+jWNxeMV+jKP/wD8BPqP8rpsSGz"}]},"_npmUser":{"name":"imtiajbinaoual","email":"imtiajbinaoual@gmail.com"},"directories":{},"maintainers":[{"name":"imtiajbinaoual","email":"imtiajbinaoual@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/cf-mongodb-polyfills_1.0.1_1734977248943_0.6003982058193917"},"_hasShrinkwrap":false}},"time":{"created":"2024-12-23T18:07:28.942Z","1.0.1":"2024-12-23T18:07:29.126Z","modified":"2024-12-23T18:07:29.436Z"},"maintainers":[{"name":"imtiajbinaoual","email":"imtiajbinaoual@gmail.com"}],"description":"cloudflare worker polyfill for net, tls, and dns for making mongodb connections work in workerd","homepage":"https://github.com/jchoi2x/cf-mongodb-polyfills#readme","keywords":["cloudflare","worker","mongodb","polyfill"],"repository":{"type":"git","url":"git+https://github.com/jchoi2x/cf-mongodb-polyfills.git"},"author":{"name":"James Choi","email":"choijjames@gmail.com"},"bugs":{"url":"https://github.com/jchoi2x/cf-mongodb-polyfills/issues"},"license":"Apache-2.0","readme":"# cf-mongodb-polyfills\r\nThis package allows the use of the `mongodb` npm package in a cloudflare worker. It does this by polyfilling the `net` and `tls` modules in the `mongodb` package to use the `cf-mongodb-polyfills` package instead.\r\n\r\n\r\n## Description\r\n\r\nCurrently cloudflare's workerd does not support using `mongodb` package to connect to mongodb. This is due to the fact that `net.createConnection` and `tls.connect` arent\r\nimplemented in the worker runtime ([ref](https://developers.cloudflare.com/workers/runtime-apis/nodejs/#nodejs-api-polyfills)) even with the `nodejs_compat` or `nodejs_compat_v2` compatiblity flags enabled.\r\n\r\nHowever, via the use of the [`module aliasing`](https://developers.cloudflare.com/workers/wrangler/configuration/#module-aliasing) feature in `wrangler.toml`, we can replace the `net` and `tls` modules in the `mongodb` package with polyfills in this package. This package provides polyfills for the `net` and `tls` modules which will use `cloudflare:sockets` npm package to create a tcp connection to the mongodb server.\r\n\r\n\r\n## Installation\r\n\r\nTo install the package, run:\r\n\r\n```sh\r\nnpm install cf-mongodb-polyfills\r\n```\r\n\r\n### Modify `wrangler.toml`\r\n\r\nTo use the package in a cloudflare worker app, you must create [module aliases](https://developers.cloudflare.com/workers/wrangler/configuration/#module-aliasing) in the wrangler.toml to point to the polyfills that are in this package.\r\n\r\n```toml\r\n# wrangler.toml\r\n# ...\r\n\r\n[alias]\r\n\"net\" = \"@jchoi2x/cf-mongodb-polyfills/net\"\r\n\"dns\" = \"@jchoi2x/cf-mongodb-polyfills/dns\"\r\n\"tls\" = \"@jchoi2x/cf-mongodb-polyfills/tls\"\r\n```\r\n\r\n\r\n## Usage\r\n\r\nWith these changes in place, you can now use the `mongodb` package in your cloudflare worker app.\r\n\r\n```typescript\r\nimport { MongoClient } from 'mongodb';\r\n\r\nexport default {\r\n  async fetch(request, env, ctx): Promise<Response> {\r\n\r\n    // connect to atlas mongodb\r\n    const tlsClient = new MongoClient('mongodb+srv://sharedddas:sdfaqwevccfjkjde9ei@dvi.kevpg.mongodb.net/?retryWrites=true&w=majority&appName=dev');\r\n    await tlsClient.connect();\r\n\r\n    // connect to mongo running locally\r\n    const client = new MongoClient('mongodb://localhost:27017');\r\n    await client.connect();\r\n\r\n    const db = tlsClient.db('test');\r\n    const users = await db.collection('users').find({}).toArray().limit(10);\r\n\r\n    return Response.json({\r\n      users\r\n    })\r\n  },\r\n} satisfies ExportedHandler<Env>;\r\n```\r\n\r\n## Contributing\r\n\r\nContributions are welcome! Please open an issue or submit a pull request.\r\n","readmeFilename":"README.md"}