{"_id":"sync-threads","_rev":"2-37fd5161ee82ddc4c971696d766fe6a1","name":"sync-threads","dist-tags":{"latest":"1.0.1"},"versions":{"1.0.0":{"name":"sync-threads","version":"1.0.0","description":"Perform asynchronous work synchronously using worker threads","author":{"name":"Michael Hart","email":"michael.hart.au@gmail.com","url":"https://github.com/mhart"},"license":"MIT","repository":{"type":"git","url":"git+https://github.com/mhart/aws4fetch.git"},"main":"src/index.js","types":"types/index.d.ts","devDependencies":{"@types/node":"^12.19.3","eslint":"^7.12.1","eslint-config-prettier":"^6.15.0","prettier":"^2.1.2","typescript":"^4.0.5"},"scripts":{"declaration":"tsc -p declaration.tsconfig.json","build":"npm run declaration","prepare":"npm run build","lint":"prettier -c . && eslint ."},"prettier":{"semi":false,"singleQuote":true,"printWidth":120},"eslintConfig":{"extends":["eslint:recommended","prettier"],"env":{"node":true,"es2017":true},"parserOptions":{"ecmaVersion":2019,"sourceType":"module"}},"bugs":{"url":"https://github.com/mhart/aws4fetch/issues"},"homepage":"https://github.com/mhart/aws4fetch#readme","_id":"sync-threads@1.0.0","_nodeVersion":"12.19.0","_npmVersion":"6.14.8","dist":{"integrity":"sha512-NAXRRsFYsNRdvQC1Cd8SgHUWnfddwr2dOauVw1+9dAZuAws7Mpw9hzDbvE5u6vY0ntsaVO+3JB6YW74XPLeHAg==","shasum":"8357e87e86f4f6a45d2c4a24de087982bf44166e","tarball":"https://registry.npmjs.org/sync-threads/-/sync-threads-1.0.0.tgz","fileCount":4,"unpackedSize":4151,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfndXHCRA9TVsSAnZWagAAH3oP/i11jQU/3iICq932eEQV\n3y1CYxyflzjOC2oMPFGHfFqF9WcT8fy1TfHyxRvTqU7n0nukRFV18pGbG/26\nAjrkQFDGHOf8/v1E7QyxltVSdj5ov8WcD+Gy1I94lZJ/fYCupKxWwLmn7VKm\nc2xoBnSDxOmDAhsnI+Eq24L+17xn1mb1LhY1fkUVHXz4d/oUcJxMjXPSY/qw\nBz6NBc500868/QB55BwCtxcDxSocEFwZDYv5sJUf1Lgzuee6JrXuvPWhRX0G\nIAeDDa66tY0zzNmh5kkAwlqZ1TbLxAlRQ+AGW2QGb6KWVYmC7W0aWF4sCeTl\nXpXWAHM93zsON4gdDO1vcXJgWnmgy3jucFfZPDpfTTSAQVNYHt1c+fq4YPxk\nxgVABAwCsrEZVKyH+dkQCWCh/yDb7+jUPEsNH2/YHQtQDotegtGFpKvVuX3C\nvZgitD5KutoAchKmI3hEwHQLspBzjW0R/icri2wuujKcPOpdfYo7wxTO5Lsk\n7sVFtfgSBnP1VdhSH5ZoJalWbiknoTPpH2AVKLQX+kSs0l28hWGyUX5XCzAW\nv7Psu6KHtii/dNPdr9ZLXPGlXVlVaplwex5K1VlQlErYyVvUZHbzNYVwut4V\nfMzC0xxpr4Hig4QXncYhjlkz7uzl4GNgCkjB++sezHHm2ykpvKh+gRN9ezT0\nw1Ig\r\n=5dll\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCHU1rbTPbLq9EgDTFN7UUS5QfZnbyyNeycOUkklJjjbAIgU0BxlhcD6ajS9I5G6dVG6bFJ1H+L6cMjUe8gBJAZ5qs="}]},"maintainers":[{"name":"hichaelmart","email":"michael.hart.au@gmail.com"}],"_npmUser":{"name":"hichaelmart","email":"michael.hart.au@gmail.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/sync-threads_1.0.0_1604179398964_0.44349179809402495"},"_hasShrinkwrap":false},"1.0.1":{"name":"sync-threads","version":"1.0.1","description":"Perform asynchronous work synchronously using worker threads","author":{"name":"Michael Hart","email":"michael.hart.au@gmail.com","url":"https://github.com/mhart"},"license":"MIT","repository":{"type":"git","url":"git+https://github.com/lambci/sync-threads.git"},"main":"src/index.js","types":"types/index.d.ts","devDependencies":{"@types/node":"^12.19.3","eslint":"^7.12.1","eslint-config-prettier":"^6.15.0","prettier":"^2.1.2","typescript":"^4.0.5"},"scripts":{"declaration":"tsc -p declaration.tsconfig.json","build":"npm run declaration","prepare":"npm run build","lint":"prettier -c . && eslint ."},"prettier":{"semi":false,"singleQuote":true,"printWidth":120},"eslintConfig":{"extends":["eslint:recommended","prettier"],"env":{"node":true,"es2017":true},"parserOptions":{"ecmaVersion":2019,"sourceType":"module"}},"gitHead":"e33200a25182c73f55c2c11ded425507ef36fb06","bugs":{"url":"https://github.com/lambci/sync-threads/issues"},"homepage":"https://github.com/lambci/sync-threads#readme","_id":"sync-threads@1.0.1","_nodeVersion":"12.19.0","_npmVersion":"6.14.8","dist":{"integrity":"sha512-hIdwt/c/e1ONnr2RJmfBxEAj/J6KQQWKdToF3Qw8ZNRsTNNteGkOe63rQy9I7J5UNlr8Yl0wkzIr9wgLY94x0Q==","shasum":"1e854ce579eaca0d0f1f0885a40bc2be6237b593","tarball":"https://registry.npmjs.org/sync-threads/-/sync-threads-1.0.1.tgz","fileCount":5,"unpackedSize":7368,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfnfPFCRA9TVsSAnZWagAAFZgQAJpUSaeVJK2bL9J7KHOg\nO9o6lsgES8EgD4KerI8VgsTg/aRhDOA3PPhZJDfatRiJo4q1vwObd/je9d8V\nybe4t0EnCtjjq76OezOht7F78SAnbF5SojLgRozJz99FK0OGrXjWWoMlsup1\n0EqF9GWbqYLp6Q9F6eKpB32NsnjmhZIuUc4LhuxIKW7/Y3x87olRmMvFrrDZ\nCgUw5p2nNUDdFmnvzRDL5ya1kqWtdEE3Vho++Yyx0D/5Ym0jcPyvMqJ2T6vC\nQ9kx5c0vgv4JVi8kxgP+GJJWN4Eu4XCjVyaphUjx6Rw5hOps4yF/4PrRez4T\nlw/oNoP3hsUBvOHK+l3JI9Xfx/h271F3MzjwEz8upEvy4rDw066eXx7jOZz0\nX2tkLs/9mtC6lXRUdyy4situ7SR1YlleMcL/vlYPogq18iTGi++BNrrP7L6N\n0TCH6JmdfuaeB5O2w4opUAj1fC6OfIovnFeDptBPJpplBLY57F9gUl+zW1X7\n/El4bz+3zKxEqs2ycdeLqHMYreGWxlXQaG6eK79ILXOzfX2O81fuKtCu7U23\nzjWH73wQ9DF0cBaZxd1ZIRNMVdvq2eqwiGnKNW+5mtIlJqtWtsAVrMim3PE9\nEZVwHalVr7Dz21SDLP3jbqr3GdMSw6yWzvabbXHQ2+ylmX67VYZ3xf0bz8Oc\nDQbW\r\n=CT/H\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIAlqYvbeTQruEUSIKqE1okAYHkfuRaeMH9j0ZK2izIM3AiBI+cNKZljiOMjd+GwiEddGumzGi3mPETSjec2FezJIVA=="}]},"maintainers":[{"name":"hichaelmart","email":"michael.hart.au@gmail.com"}],"_npmUser":{"name":"hichaelmart","email":"michael.hart.au@gmail.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/sync-threads_1.0.1_1604187077565_0.0035004945898389206"},"_hasShrinkwrap":false}},"time":{"created":"2020-10-31T21:23:18.964Z","1.0.0":"2020-10-31T21:23:19.093Z","modified":"2022-05-19T06:15:35.849Z","1.0.1":"2020-10-31T23:31:17.663Z"},"maintainers":[{"name":"hichaelmart","email":"michael.hart.au@gmail.com"}],"description":"Perform asynchronous work synchronously using worker threads","homepage":"https://github.com/lambci/sync-threads#readme","repository":{"type":"git","url":"git+https://github.com/lambci/sync-threads.git"},"author":{"name":"Michael Hart","email":"michael.hart.au@gmail.com","url":"https://github.com/mhart"},"bugs":{"url":"https://github.com/lambci/sync-threads/issues"},"license":"MIT","readme":"# sync-threads\n\nMake asynchronous calls in Node.js synchronously using [worker threads](https://nodejs.org/api/worker_threads.html) and [Atomics mutexes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics).\n\nEspecially useful when you need to resolve promises at require-time,\nfor example in an AWS Lambda function when using [provisioned concurrency](https://docs.aws.amazon.com/lambda/latest/dg/configuration-concurrency.html#configuration-concurrency-provisioned).\n\nNOTE: you don't need this library if you're happy with the overhead of creating a Node.js subprocess,\nsee the [Appendix](#appendix) for details.\n\n# Example\n\nThis example shows how we can synchronously retrieve secrets from [AWS SSM](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-parameter-store.html)\nat require-time – that is, before the init stage of Lambda has finished.\n\n`index.js`:\n\n```js\nconst { createSyncFn } = require('sync-threads')\n\n// Create our synchronous function\nconst getSsmSecretSync = createSyncFn('./worker.js')\n\n// Call it at init (require) time, no async needed!\nconst secret = getSsmSecretSync('/my-secret')\n\n// Logged at init time\nconsole.log({ secret, source: 'init' })\n\nexports.handler = async () => {\n  // This value can be used in our handler without needing to resolve anything async\n  console.log({ secret, source: 'handler' })\n}\n```\n\n`worker.js`:\n\n```js\nconst SSM = require('aws-sdk/clients/ssm')\nconst { runAsWorker } = require('sync-threads')\n\nconst ssm = new SSM()\n\nrunAsWorker(async (ssmParamName) => {\n  const {\n    Parameter: { Value },\n  } = await ssm.getParameter({ Name: ssmParamName, WithDecryption: true }).promise()\n  return Value\n})\n```\n\nYou can see this example, as well as how you'd bundle it if you're using webpack or similar, in the [`examples`](./examples) directory.\n\n# API\n\n`sync-threads` exports two main functions: `createSyncFn` and `runAsWorker`\n\n## `createSyncFn(filename[, bufferSize])`\n\nReturns a synchronous function that will run the specified file as a worker, pass in any arguments you give it, and wait for the result.\n\n### `runAsWorker(workerAsyncFn)`\n\nTo be called from inside your worker code. It will run the given asynchronous function with the given arguments from the parent and share the result.\n\n# Installation\n\nWith [npm](http://npmjs.org/) do:\n\n```\nnpm install sync-threads\n```\n\n# Appendix\n\nYou can achieve something very similar to this library using the\n`spawnSync`/`execSync` functions from the `child_process` module in Node.js.\n\nThe main difference is that this library performs the async work in a thread, without creating a separate `node` process,\nwhich makes it a little faster.\n\nHere's a simple example of how you'd do it without needing this library:\n\n`index.js`:\n\n```js\nconst { execSync } = require('child_process')\n\nconst { secret } = JSON.parse(execSync('node worker.js /my-secret', 'utf8').trim().split('\\n').pop())\n\nconsole.log({ secret, source: 'init' })\n\nexports.handler = async () => {\n  console.log({ secret, source: 'handler' })\n}\n```\n\n`worker.js`:\n\n```js\nconst SSM = require('aws-sdk/clients/ssm')\n\nconst ssm = new SSM()\n\n;(async () => {\n  const {\n    Parameter: { Value },\n  } = await ssm.getParameter({ Name: process.argv[2], WithDecryption: true }).promise()\n  console.log('%j', { secret: Value })\n})()\n```\n","readmeFilename":"README.md"}