{"name":"make-synchronized","version":"0.7.0","author":{"url":"https://www.fiskercheung.com/","name":"fisker Cheung","email":"lionkay@gmail.com"},"license":"MIT","_id":"make-synchronized@0.7.0","maintainers":[{"name":"fisker","email":"lionkay@gmail.com"}],"homepage":"https://github.com/fisker/make-synchronized#readme","bugs":{"url":"https://github.com/fisker/make-synchronized/issues"},"c8":{"all":true,"exclude":["**/*.d.ts","**/*.test-d.ts"],"include":["source"],"reporter":["lcov","text"]},"dist":{"shasum":"90f2e61ca937587a050c8cf66c2493c9d0e91dc9","tarball":"http://registry-npm.ywwl.com/make-synchronized/download/make-synchronized-0.7.0.tgz","fileCount":6,"integrity":"sha512-Zb1Cb6XN2VMLnpkStwM4eXFLsxEIdDJYCDdFNsNOeuaI4vsSdr9ftQSK4dwNSwckmztiH60D9e2ZLZozwZnJtw==","signatures":[{"sig":"MEQCIHAn7RonLzOOSHmg8ddwH0Em21XUjaso2CnqOj0068yAAiBnI6PAkWnmtprpJQmXYW8EzRiV+fX7AVkpuW56G6fuZg==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":63058,"size":15234,"noattachment":false,"key":"/make-synchronized/-/make-synchronized-0.7.0.tgz"},"main":"index.cjs","type":"module","types":"./index.d.ts","exports":{".":{"types":"./index.d.ts","default":"./index.mjs","require":"./index.cjs","module-sync":"./index.mjs"}},"funding":"https://github.com/fisker/make-synchronized?sponsor=1","gitHead":"a288bae116a1eb6f4c252eddb4eaa299af1f9d96","scripts":{"fix":"run-p --continue-on-error \"fix:*\"","dist":"run-p \"dist:*\"","lint":"run-p --continue-on-error \"lint:*\"","test":"node --test","build":"node scripts/build.js","prepare":"husky install","release":"run-s build lint fix dist","dist:npm":"release-it","test-all":"run-s build test test:production-esm test:production-cjs test:types","fix:eslint":"yarn lint:eslint --fix","lint:types":"tsc","test:types":"tsd --files=./source/index.test-d.ts","lint:eslint":"eslint .","fix:markdown":"yarn lint:markdown --fix","fix:prettier":"prettier . --write","lint:markdown":"markdownlint-cli2","lint:prettier":"prettier . --check","test-coverage":"c8 yarn test-all","fix:package-json":"sort-package-json \"package.json\" \"packages/*/package.json\"","lint:package-json":"yarn run fix:package-json --check","test:production-cjs":"yarn build && cross-env DIST_TEST_TYPE=cjs node --test","test:production-esm":"yarn build && cross-env DIST_TEST_TYPE=esm node --test"},"_npmUser":{"name":"fisker","email":"lionkay@gmail.com"},"repository":{"url":"git+https://github.com/fisker/make-synchronized.git","type":"git"},"_npmVersion":"9.1.2","description":"[![Build Status][github_actions_badge]][github_actions_link] [![Coverage][codecov_badge]][codecov_link] [![Npm Version][package_version_badge]][package_link] [![MIT License][license_badge]][license_link]","directories":{},"sideEffects":false,"_nodeVersion":"22.14.0","publishConfig":{"access":"public","registry":"https://registry.npmjs.org/"},"_hasShrinkwrap":false,"packageManager":"yarn@4.7.0","devDependencies":{"c8":"10.1.3","tsd":"0.31.2","husky":"9.1.7","tempy":"3.1.0","eslint":"9.23.0","del-cli":"6.0.0","esbuild":"0.25.1","prettier":"3.5.3","cross-env":"7.0.3","tinybench":"4.0.1","nano-spawn":"0.2.0","release-it":"18.1.2","strip-ansi":"7.1.0","typescript":"5.8.2","@types/node":"22.13.11","lint-staged":"15.5.0","npm-run-all2":"7.0.2","markdownlint-cli2":"0.17.2","sort-package-json":"3.0.0","@fisker/eslint-config":"14.0.0","@fisker/prettier-config":"5.1.5","@fisker/lint-staged-config":"3.3.5","@fisker/markdownlint-cli2-config":"0.0.3"},"_npmOperationalInternal":{"tmp":"tmp/make-synchronized_0.7.0_1742980103397_0.3537941396268598","host":"s3://npm-registry-packages-npm-production"},"readme":"# make-synchronized\n\n[![Coverage][codecov_badge]][codecov_link]\n[![Npm Version][package_version_badge]][package_link]\n[![MIT License][license_badge]][license_link]\n\n[codecov_badge]: https://img.shields.io/codecov/c/github/fisker/make-synchronized?style=flat-square\n[codecov_link]: https://codecov.io/gh/fisker/make-synchronized\n[license_badge]: https://img.shields.io/npm/l/make-synchronized.svg?style=flat-square\n[license_link]: https://github.com/fisker/make-synchronized/blob/main/license\n[package_version_badge]: https://img.shields.io/npm/v/make-synchronized.svg?style=flat-square\n[package_link]: https://www.npmjs.com/package/make-synchronized\n\n> Make synchronized functions.\n\n## Install\n\n```bash\nyarn add make-synchronized\n```\n\n## Usage\n\nThis module mainly to support three kinds of different purpose of usage:\n\n1. Make asynchronous functions in an existing module into synchronized\n\n   ```js\n   import makeSynchronized from 'make-synchronized'\n\n   const synchronized = makeSynchronized(\n     new URL('./my-asynchronous-function-module.js', import.meta.url),\n   )\n   ```\n\n1. Make a module that turns asynchronous function into synchronized\n\n   ```js\n   import makeSynchronized from 'make-synchronized'\n\n   export default makeSynchronized(import.meta, myAsynchronousFunction)\n   ```\n\n1. Make an inline asynchronous function into synchronized\n\n   ```js\n   import makeSynchronized from 'make-synchronized'\n\n   const synchronized = makeSynchronized(() => Promise.resolve('foo'))\n   ```\n\n## Named exports\n\n```js\nimport {\n  makeSynchronized, // Same as the default export\n  makeDefaultExportSynchronized,\n  makeModuleSynchronized,\n  makeSynchronizedFunction,\n  makeSynchronizedFunctions,\n  makeInlineFunctionSynchronized,\n} from 'make-synchronized'\n```\n\n## Limitation\n\nThis module uses [`MessagePort#postMessage`](https://nodejs.org/api/worker_threads.html#portpostmessagevalue-transferlist) to transfer arguments, return values, errors between the main thread and the [worker](https://nodejs.org/api/worker_threads.html#class-worker). Please make sure the arguments and return values are serializable by [the structured clone algorithm](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm).\n\n## API\n\n### `makeSynchronized(module: string | URL | ImportMeta)`\n\n> Make asynchronous functions in an existing module to be synchronized to call.\n\n- If the passing `module` is a module that contains a function type default export, returns a `Proxy` function, with other specifiers attached.\n\n  ```js\n  // foo.js\n  export default () => Promise.resolve('default export called')\n  export const foo = 'foo'\n  export const bar = () => Promise.resolve('bar called')\n  ```\n\n  ```js\n  const synchronized = makeSynchronized(new URL('./foo.js', import.meta.url))\n\n  synchronized()\n  // -> \"default export called\"\n\n  synchronized.foo\n  // -> \"foo\"\n\n  // This function also synchronized.\n  synchronized.bar()\n  // -> \"bar called\"\n  ```\n\n  [Example](./examples/make-default-export-function-synchronized.js)\n\n- If the passing `module` is a module without default export or default export is not a function, a `Module` object will be returned with all specifiers.\n\n  ```js\n  // foo.js\n  export const foo = 'foo'\n  export const bar = () => Promise.resolve('bar called')\n  ```\n\n  ```js\n  import makeSynchronized from 'make-synchronized'\n\n  const module = makeSynchronized(new URL('./foo.js', import.meta.url))\n\n  module\n  // [Object: null prototype] [Module] { bar: [Getter], foo: [Getter] }\n\n  module.foo\n  // -> \"foo\"\n\n  module.bar()\n  // -> \"bar called\"\n  ```\n\n  [Example](./examples/make-module-specifiers-synchronized.js)\n\n### `makeSynchronized(module: string | URL | ImportMeta, implementation: function | object)`\n\n> Make asynchronous functions to be synchronized for export.\n\n- If `implementation` is a `function`, returns a synchronized version of the passed function.\n\n  > [!IMPORTANT]\n  >\n  > **It MUST be used as the default export**\n\n  ```js\n  // foo.js\n  import makeSynchronized from 'make-synchronized'\n\n  export default makeSynchronized(import.meta, () => Promise.resolve('foo'))\n  ```\n\n  ```js\n  import foo from './foo.js'\n\n  foo()\n  // -> foo\n  ```\n\n  - [Example](./examples/use-module-synchronized-as-default.js)\n\n- If `implementation` is a `object` with multiple functions, returns a `Proxy` object with synchronized functions attached.\n\n  > [!IMPORTANT]\n  >\n  > **Functions MUST exported as the same name as the key in `implementation` object.**\n\n  ```js\n  // foo-and-bar.js\n  import makeSynchronized from 'make-synchronized'\n\n  export const {foo, bar} = makeSynchronized(import.meta, {\n    async foo() {\n      return 'foo'\n    },\n    async bar() {\n      return 'bar'\n    },\n  })\n  ```\n\n  ```js\n  import {foo, bar} from './foo-and-bar.js'\n\n  foo()\n  // -> foo\n\n  bar()\n  // -> bar\n  ```\n\n- [Example](./examples/use-module-synchronized-as-default.js)\n\n### `makeSynchronized(implementation: function)`\n\n```js\nimport makeSynchronized from 'make-synchronized'\n\nconst foo = makeSynchronized(() => Promise.resolve('foo'))\n\nfoo()\n// -> foo\n```\n\n> [!IMPORTANT]\n>\n> The given function is executed in a separate environment, so you cannot use any variables/imports from outside the scope of the function. You can pass in arguments to the function. To import dependencies, use `await import(…)` in the function body.\n\n- [Example](./examples/use-inline-function.js)\n\n### `makeSynchronizedFunction(module: string | URL | ImportMeta, implementation: function, specifier?: string)`\n\n> Make a synchronized function for export.\n\nExplicit version of `makeSynchronized(module, implementation)` that returns the synchronized function for export.\n\n```js\nimport {makeSynchronizedFunction} from 'make-synchronized'\n\nexport default makeSynchronizedFunction(\n  import.meta,\n  async () => 'default export called',\n)\nexport const foo = makeSynchronizedFunction(\n  import.meta,\n  async () => 'foo export called',\n  'foo',\n)\n```\n\n### `makeSynchronizedFunctions(module: string | URL | ImportMeta, implementation: object)`\n\n> Make synchronized functions for export.\n\nExplicit version of `makeSynchronized(module, implementation)` that only returns `Proxy` with synchronized functions for export.\n\n```js\nimport {makeSynchronizedFunctions} from 'make-synchronized'\n\nexport const {\n  // MUST match the key in second argument\n  foo,\n  bar,\n} = makeSynchronizedFunctions(import.meta, {\n  foo: async () => 'foo export called',\n  bar: async () => 'bar export called',\n})\n```\n\n### `makeDefaultExportSynchronized(module: string | URL | ImportMeta)`\n\n> Make an existing module's default export to be a synchronized function.\n\nExplicit version of `makeSynchronized(module)` that only returns the synchronized default export.\n\n```js\nimport {makeDefaultExportSynchronized} from 'make-synchronized'\n\nconst foo = makeModuleSynchronized('foo')\n\nfoo()\n// -> default export of `foo` module is called.\n```\n\n### `makeModuleSynchronized(module: string | URL | ImportMeta)`\n\n> Make an existing module's exports to be synchronized functions.\n\nSynchronize version of `import(module)`, always returns a `Module`.\n\n```diff\n- const {default: foo} = await import('foo')\n+ const {default: foo} = makeModuleSynchronized('foo')\n```\n\n```js\nimport {makeModuleSynchronized} from 'make-synchronized'\n\nconst {default: foo, bar} = makeModuleSynchronized('foo')\n\nfoo()\n// -> default export of `foo` module is called.\n\nbar()\n// -> `bar` function from `foo` module is called.\n```\n\n### `makeInlineFunctionSynchronized(implementOrCode: function | string)`\n\n> Make an inline asynchronous function into synchronized.\n\n> [!IMPORTANT]\n>\n> The given function is executed in a separate environment, so you cannot use any variables/imports from outside the scope of the function. You can pass in arguments to the function. To import dependencies, use `await import(…)` in the function body.\n\nExplicit version of `makeSynchronized(function)`.\n\n```js\nimport makeSynchronized from 'make-synchronized'\n\nconst foo = makeSynchronized(() => Promise.resolve('foo'))\n\nfoo()\n// -> foo\n```\n\n```js\nimport makeSynchronized from 'make-synchronized'\n\nconst foo = makeSynchronized(`\n  () => ${someOtherCode}\n`)\n\nfoo()\n```\n\n```js\nimport makeSynchronized from 'make-synchronized'\n\nconst sleep = makeSynchronized((delay) =>\n  process.getBuiltinModule('node:timers/promises').setTimeout(delay),\n)\n```\n\n```js\nimport makeSynchronized from 'make-synchronized'\n\nconst sleep = makeSynchronized(async (delay) => {\n  const {setTimeout} = await import('node:timers/promises')\n  await setTimeout(delay)\n})\n```\n","publish_time":1742980103568,"_cnpm_publish_time":1742980103568,"dist-tags":{"latest":"0.8.0"}}