Installations
npm install shm-typed-array
Developer Guide
Typescript
Yes
Module System
CommonJS
Min. Node Version
>=4.0.0
Node Version
16.20.2
NPM Version
8.19.4
Releases
Contributors
Unable to fetch Contributors
Languages
C++ (65.4%)
JavaScript (30.82%)
TypeScript (3.1%)
Python (0.68%)
Developer
ukrbublik
Download Statistics
Total Downloads
24,973
Last Day
18
Last Week
112
Last Month
424
Last Year
4,839
GitHub Statistics
74 Stars
72 Commits
9 Forks
4 Watching
8 Branches
6 Contributors
Package Meta Information
Latest Version
0.1.1
Package Id
shm-typed-array@0.1.1
Unpacked Size
51.70 kB
Size
13.38 kB
File Count
18
NPM Version
8.19.4
Node Version
16.20.2
Publised On
15 May 2024
Total Downloads
Cumulative downloads
Total Downloads
24,973
Last day
-47.1%
18
Compared to previous day
Last week
-8.9%
112
Compared to previous week
Last month
7.6%
424
Compared to previous month
Last year
125.2%
4,839
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
Dev Dependencies
3
IPC shared memory for Node.js
Use as Buffer
or TypedArray
Supports System V and POSIX shared memory
Install
1$ npm install shm-typed-array
Windows is not supported.
System V vs POSIX
Versions 0.0.* support only System V memory segments.
Starting from version 0.1.0 POSIX memory objects are also supported.
System V is the classic way to use shared memory, stores IPC objects internally in the kernel.
POSIX is newer, but not fully supported in MacOS, uses the file system interface.
To create POSIX memory objects, use string parameter key
in API.
Eg. shm.create(100, 'Buffer', '/test')
will create virtual file /dev/shm/test
in tmpfs for Linix.
To create System V memory segment, use numeric parameter key
in API.
Eg. shm.create(100, 'Buffer', 1234)
or shm.create(100)
to autogenerate key.
API
shm.create (count, typeKey, key?, perm?)
Create shared memory segment/object.
count
- number of elements (not bytes),
typeKey
- type of elements ('Buffer'
by default, see list below),
key
- integer/null to create System V memory segment, or string to create POSIX memory object,
perm
- permissions flag (default is 660
).
Returns shared memory Buffer
or descendant of TypedArray
object, class depends on param typeKey
.
Or returns null
if shm already exists with provided key.
For System V: returned object has property key
- integer key of created System V shared memory segment, to use in shm.get(key)
.
For POSIX: shared memory objects are not automatically destroyed. You should call shm.destroy(key)
manually on process cleanup or if you don't need the object anymore.
shm.get (key, typeKey)
Get created shared memory segment/object by key.
Returns null
if shm not exists with provided key.
shm.detach (key, forceDestroy?)
Detach shared memory segment/object.
For System V: If there are no other attaches for a segment, it will be destroyed automatically (even if forceDestroy
is not true).
For POSIX: Unlike System V segments, POSIX object will not be destroyed automatically. You need to destroy it manually by providing true to forceDestroy
argument or using shm.destroy(key)
.
shm.destroy (key)
Destroy shared memory segment/object.
Same as shm.detach(key, true)
shm.detachAll ()
Detach all created shared memory segments and objects.
Will be automatically called on process exit, see Cleanup.
shm.getTotalSize()
Get total size of all used (mapped) shared memory in bytes.
shm.getTotalCreatedSize()
Get total size of all created shared memory in bytes.
shm.LengthMax
Max length of shared memory segment (count of elements, not bytes)
2^31 for 64bit, 2^30 for 32bit
Types:
1shm.BufferType = { 2 'Buffer': shm.SHMBT_BUFFER, 3 'Int8Array': shm.SHMBT_INT8, 4 'Uint8Array': shm.SHMBT_UINT8, 5 'Uint8ClampedArray': shm.SHMBT_UINT8CLAMPED, 6 'Int16Array': shm.SHMBT_INT16, 7 'Uint16Array': shm.SHMBT_UINT16, 8 'Int32Array': shm.SHMBT_INT32, 9 'Uint32Array': shm.SHMBT_UINT32, 10 'Float32Array': shm.SHMBT_FLOAT32, 11 'Float64Array': shm.SHMBT_FLOAT64, 12};
Cleanup
This library does cleanup of created SHM segments/objects only on normal exit of process, see exit
event.
If you want to do cleanup on terminate signals like SIGINT
, SIGTERM
, please use node-cleanup / node-death and add code to exit handlers:
1shm.detachAll();
Also note that POSIX shared memory objects are not automatically destroyed.
You should call shm.destroy('/your_name')
manually if you don't need it anymore.
Usage
See example.js
Usage of memory segments:
1const cluster = require('cluster'); 2const shm = require('shm-typed-array'); 3 4var buf, arr; 5if (cluster.isMaster) { 6 buf = shm.create(4096); //4KB 7 arr = shm.create(1000000*100, 'Float32Array'); //100M floats 8 buf[0] = 1; 9 arr[0] = 10.0; 10 console.log('[Master] Typeof buf:', buf.constructor.name, 11 'Typeof arr:', arr.constructor.name); 12 13 var worker = cluster.fork(); 14 worker.on('online', function() { 15 this.send({ msg: 'shm', bufKey: buf.key, arrKey: arr.key }); 16 var i = 0; 17 setInterval(function() { 18 buf[0] += 1; 19 arr[0] /= 2; 20 console.log(i + ' [Master] Set buf[0]=', buf[0], 21 ' arr[0]=', arr ? arr[0] : null); 22 i++; 23 if (i == 5) { 24 groupSuicide(); 25 } 26 }, 500); 27 }); 28} else { 29 process.on('message', function(data) { 30 var msg = data.msg; 31 if (msg == 'shm') { 32 buf = shm.get(data.bufKey); 33 arr = shm.get(data.arrKey, 'Float32Array'); 34 console.log('[Worker] Typeof buf:', buf.constructor.name, 35 'Typeof arr:', arr.constructor.name); 36 var i = 0; 37 setInterval(function() { 38 console.log(i + ' [Worker] Get buf[0]=', buf[0], 39 ' arr[0]=', arr ? arr[0] : null); 40 i++; 41 if (i == 2) { 42 shm.detach(data.arrKey); 43 arr = null; //otherwise process will drop 44 } 45 }, 500); 46 } else if (msg == 'exit') { 47 process.exit(); 48 } 49 }); 50} 51 52function groupSuicide() { 53 if (cluster.isMaster) { 54 for (var id in cluster.workers) { 55 cluster.workers[id].send({ msg: 'exit'}); 56 cluster.workers[id].destroy(); 57 } 58 process.exit(); 59 } 60}
Output:
[Master] Typeof buf: Buffer Typeof arr: Float32Array
[Worker] Typeof buf: Buffer Typeof arr: Float32Array
0 [Master] Set buf[0]= 2 arr[0]= 5
0 [Worker] Get buf[0]= 2 arr[0]= 5
1 [Master] Set buf[0]= 3 arr[0]= 2.5
1 [Worker] Get buf[0]= 3 arr[0]= 2.5
2 [Master] Set buf[0]= 4 arr[0]= 1.25
2 [Worker] Get buf[0]= 4 arr[0]= null
3 [Master] Set buf[0]= 5 arr[0]= 0.625
3 [Worker] Get buf[0]= 5 arr[0]= null
4 [Master] Set buf[0]= 6 arr[0]= 0.3125
shm segments destroyed: 2
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 6/24 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/node.js.yml:1
- Info: no jobLevel write permissions found
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/node.js.yml:24: update your workflow using https://app.stepsecurity.io/secureworkflow/ukrbublik/shm-typed-array/node.js.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/node.js.yml:26: update your workflow using https://app.stepsecurity.io/secureworkflow/ukrbublik/shm-typed-array/node.js.yml/master?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/node.js.yml:30
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 npmCommand dependencies pinned
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 17 are checked with a SAST tool
Score
4
/10
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More