{"name":"make-synchronized","version":"0.2.2","author":{"url":"https://www.fiskercheung.com/","name":"fisker Cheung","email":"lionkay@gmail.com"},"license":"MIT","_id":"make-synchronized@0.2.2","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,"include":["index.js","utilities"],"reporter":["lcov","text"]},"dist":{"shasum":"f3eb9735922243905c75c9b2cd6e874cf1151f09","tarball":"http://registry-npm.ywwl.com/make-synchronized/download/make-synchronized-0.2.2.tgz","fileCount":7,"integrity":"sha512-SjG2Ta/pFAStQnSvUcF9RDiJ6LPndbKWigJv5inHrXYYrxhoo0iUQ3koCqJOiW9TeYjoQRYUZCb9vgj8fxUgHw==","signatures":[{"sig":"MEQCIHH0Vl4jy1kLj2+JVtgrvabj5ZnRuyjWAnjXvTzjURgOAiBPjsF3CqoPdgaTYF+DtZ+nGThEoUwiuQWny57BcRE1aQ==","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":32078,"size":6862,"noattachment":false,"key":"/make-synchronized/-/make-synchronized-0.2.2.tgz"},"main":"index.js","type":"module","exports":{".":{"default":"./index.mjs","require":"./index.cjs"}},"funding":"https://github.com/fisker/make-synchronized?sponsor=1","gitHead":"91dabc39d82b25fbad2a3c97af2deabf3d4cd717","scripts":{"dist":"run-p \"dist:*\"","lint":"run-p \"lint:*\"","test":"run-s test:development test:production-esm test:production-cjs","build":"node scripts/build.js","clean":"run-p \"clean:*\"","format":"run-p \"format:*\"","prepare":"husky install","release":"run-s build lint format dist","dist:npm":"np --yolo --no-yarn","clean:dist":"del-cli dist","lint:eslint":"eslint \"**/*.{js,jsx,mjs,cjs,vue}\"","format:eslint":"yarn lint:eslint --fix","lint:markdown":"markdownlint-cli2","lint:prettier":"prettier . --check","test-coverage":"c8 yarn test","format:markdown":"markdownlint-cli2 --fix","format:prettier":"yarn lint:prettier --write","test:development":"node --test","lint:package-json":"yarn run format:package-json --check","format:package-json":"sort-package-json \"package.json\" \"packages/*/package.json\"","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":"10.2.3","description":"[![Build Status][github_actions_badge]][github_actions_link] [![Coverage][coveralls_badge]][coveralls_link] [![Npm Version][package_version_badge]][package_link] [![MIT License][license_badge]][license_link]","directories":{},"sideEffects":false,"_nodeVersion":"20.10.0","publishConfig":{"access":"public","registry":"https://registry.npmjs.org/"},"_hasShrinkwrap":false,"packageManager":"yarn@4.0.2","devDependencies":{"c8":"9.1.0","execa":"8.0.1","husky":"8.0.3","tempy":"3.1.0","eslint":"8.56.0","del-cli":"5.1.0","esbuild":"0.19.11","prettier":"3.2.4","cross-env":"7.0.3","lint-staged":"15.2.0","npm-run-all":"4.1.5","markdownlint-cli2":"0.12.1","sort-package-json":"2.6.0","@fisker/eslint-config":"12.1.0","@fisker/prettier-config":"5.1.1","@fisker/lint-staged-config":"3.3.2","@fisker/markdownlint-cli2-config":"0.0.3"},"_npmOperationalInternal":{"tmp":"tmp/make-synchronized_0.2.2_1705903830092_0.028108214072410043","host":"s3://npm-registry-packages"},"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":1705903830223,"_cnpm_publish_time":1705903830223,"dist-tags":{"latest":"0.8.0"}}