Skip to content

Commit

Permalink
Import from internal
Browse files Browse the repository at this point in the history
  • Loading branch information
mhart committed Oct 31, 2020
0 parents commit 350fd25
Show file tree
Hide file tree
Showing 18 changed files with 419 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

node_modules
package-lock.json
dist
samconfig.toml
9 changes: 9 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"eslint.validate": ["javascript"],
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"typescript.tsdk": "node_modules/typescript/lib"
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Michael Hart

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
111 changes: 111 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# sync-threads

Make 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).

Especially useful when you need to resolve promises at require-time,
for example in an AWS Lambda function when using [provisioned concurrency](https://docs.aws.amazon.com/lambda/latest/dg/configuration-concurrency.html#configuration-concurrency-provisioned).

NOTE: you don't need this library if you're happy with the overhead of creating a Node.js subprocess,
see the [Appendix](#appendix) for details.

# Example

This 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)
at require-time – that is, before the init stage of Lambda has finished.

`index.js`:

```js
const { createSyncFn } = require('sync-threads')

// Create our synchronous function
const getSsmSecretSync = createSyncFn('./worker.js')

// Call it at init (require) time, no async needed!
const secret = getSsmSecretSync('/my-secret')

// Logged at init time
console.log({ secret, source: 'init' })

exports.handler = async () => {
// This value can be used in our handler without needing to resolve anything async
console.log({ secret, source: 'handler' })
}
```

`worker.js`:

```js
const SSM = require('aws-sdk/clients/ssm')
const { runAsWorker } = require('sync-threads')

const ssm = new SSM()

runAsWorker(async (ssmParamName) => {
const {
Parameter: { Value },
} = await ssm.getParameter({ Name: ssmParamName, WithDecryption: true }).promise()
return Value
})
```

You can see this example, as well as how you'd bundle it if you're using webpack or similar, in the [`examples`](./examples) directory.

# API

`sync-threads` exports two main functions: `createSyncFn` and `runAsWorker`

## `createSyncFn(filename[, bufferSize])`

Returns a synchronous function that will run the specified file as a worker, pass in any arguments you give it, and wait for the result.

### `runAsWorker(workerAsyncFn)`

To 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.

# Installation

With [npm](http://npmjs.org/) do:

```
npm install sync-threads
```

# Appendix

You can achieve something very similar to this library using the
`spawnSync`/`execSync` functions from the `child_process` module in Node.js.

The main difference is that this library performs the async work in a thread, without creating a separate `node` process,
which makes it a little faster.

Here's a simple example of how you'd do it without needing this library:

`index.js`:

```js
const { execSync } = require('child_process')

const { secret } = JSON.parse(execSync('node worker.js /my-secret', 'utf8').trim().split('\n').pop())

console.log({ secret, source: 'init' })

exports.handler = async () => {
console.log({ secret, source: 'handler' })
}
```

`worker.js`:

```js
const SSM = require('aws-sdk/clients/ssm')

const ssm = new SSM()

;(async () => {
const {
Parameter: { Value },
} = await ssm.getParameter({ Name: process.argv[2], WithDecryption: true }).promise()
console.log('%j', { secret: Value })
})()
```
10 changes: 10 additions & 0 deletions declaration.tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "./tsconfig",
"compilerOptions": {
"outDir": "types",
"declaration": true,
"noEmit": false,
"emitDeclarationOnly": true,
"removeComments": true
}
}
15 changes: 15 additions & 0 deletions examples/bare/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const { createSyncFn } = require('sync-threads')

const getSsmSecretSync = createSyncFn('./worker.js')

// Happens at init (require) time
const secret = getSsmSecretSync('/my-secret')

console.log({ secret, source: 'init' })

exports.handler = async () => {
// Happens every invoke
const secret = getSsmSecretSync('/my-secret')

console.log({ secret, source: 'handler' })
}
11 changes: 11 additions & 0 deletions examples/bare/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "syncworker-bare-example",
"version": "1.0.0",
"dependencies": {
"aws-sdk": "^2.783.0",
"sync-threads": "^1.0.0"
},
"scripts": {
"deploy": "sam deploy"
}
}
18 changes: 18 additions & 0 deletions examples/bare/template.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31
Resources:
WorkerTestFunction:
Type: AWS::Serverless::Function
Properties:
Runtime: nodejs12.x
CodeUri: .
Handler: index.handler
MemorySize: 1024
AutoPublishAlias: live
ProvisionedConcurrencyConfig:
ProvisionedConcurrentExecutions: 1
DeploymentPreference:
Type: AllAtOnce
Policies:
- SSMParameterReadPolicy:
ParameterName: my-secret
11 changes: 11 additions & 0 deletions examples/bare/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const SSM = require('aws-sdk/clients/ssm')
const { runAsWorker } = require('sync-threads')

const ssm = new SSM()

runAsWorker(async (ssmParamName) => {
const {
Parameter: { Value },
} = await ssm.getParameter({ Name: ssmParamName, WithDecryption: true }).promise()
return Value
})
16 changes: 16 additions & 0 deletions examples/webpack/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "syncworker-webpack-example",
"version": "1.0.0",
"dependencies": {
"aws-sdk": "^2.783.0",
"sync-threads": "^1.0.0"
},
"devDependencies": {
"webpack": "^4.44.2",
"webpack-cli": "^4.1.0"
},
"scripts": {
"build": "webpack",
"deploy": "npm run build && sam deploy"
}
}
15 changes: 15 additions & 0 deletions examples/webpack/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const { createSyncFn } = require('sync-threads')

const getSsmSecretSync = createSyncFn('./worker.js')

// Happens at init (require) time
const secret = getSsmSecretSync('/my-secret')

console.log({ secret, source: 'init' })

exports.handler = async () => {
// Happens every invoke
const secret = getSsmSecretSync('/my-secret')

console.log({ secret, source: 'handler' })
}
11 changes: 11 additions & 0 deletions examples/webpack/src/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const SSM = require('aws-sdk/clients/ssm')
const { runAsWorker } = require('sync-threads')

const ssm = new SSM()

runAsWorker(async (ssmParamName) => {
const {
Parameter: { Value },
} = await ssm.getParameter({ Name: ssmParamName, WithDecryption: true }).promise()
return Value
})
18 changes: 18 additions & 0 deletions examples/webpack/template.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31
Resources:
WorkerTestFunction:
Type: AWS::Serverless::Function
Properties:
Runtime: nodejs12.x
CodeUri: dist
Handler: index.handler
MemorySize: 1024
AutoPublishAlias: live
ProvisionedConcurrencyConfig:
ProvisionedConcurrentExecutions: 1
DeploymentPreference:
Type: AllAtOnce
Policies:
- SSMParameterReadPolicy:
ParameterName: my-secret
21 changes: 21 additions & 0 deletions examples/webpack/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const path = require('path')

module.exports = {
mode: 'production',
target: 'node',
node: { __filename: false }, // false, // counterintuitively, this turns off any messing with __dirname, __filename, etc
entry: {
index: './src/index.js',
worker: './src/worker.js',
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].js',
libraryTarget: 'commonjs2',
},
externals: {
'./worker': 'commonjs2 ./worker',
},
// These are only to make the output easier to read
optimization: { minimize: false, namedModules: true },
}
46 changes: 46 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "sync-threads",
"version": "1.0.0",
"description": "Perform asynchronous work synchronously using worker threads",
"author": "Michael Hart <[email protected]> (https://github.com/mhart)",
"license": "MIT",
"repository": "github:lambci/sync-threads",
"main": "src/index.js",
"types": "types/index.d.ts",
"files": [
"src",
"types"
],
"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"
}
}
}
Loading

0 comments on commit 350fd25

Please sign in to comment.