@nxtedition/shared 1.0.1 → 1.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +138 -26
- package/lib/index.d.ts +36 -0
- package/lib/index.js +461 -0
- package/package.json +23 -58
- package/.editorconfig +0 -12
- package/.husky/pre-commit +0 -3
- package/index.js +0 -128
- package/package-lock.json +0 -1513
package/package.json
CHANGED
|
@@ -1,66 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nxtedition/shared",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "Ring Buffer for NodeJS cross Worker communication",
|
|
5
|
-
"main": "index.js",
|
|
6
|
-
"repository": {
|
|
7
|
-
"type": "git",
|
|
8
|
-
"url": "git+https://github.com/nxtedition/shared.git"
|
|
9
|
-
},
|
|
10
|
-
"author": "Robert Nagy <ronagy@icloud.com>",
|
|
11
|
-
"license": "MIT License",
|
|
12
|
-
"bugs": {
|
|
13
|
-
"url": "https://github.com/nxtedition/shared/issues"
|
|
14
|
-
},
|
|
3
|
+
"version": "1.0.10",
|
|
15
4
|
"type": "module",
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
"
|
|
24
|
-
|
|
25
|
-
"
|
|
26
|
-
"singleQuote": true,
|
|
27
|
-
"jsxSingleQuote": true
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"types": "lib/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"lib",
|
|
9
|
+
"README.md",
|
|
10
|
+
"LICENSE"
|
|
11
|
+
],
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"publishConfig": {
|
|
14
|
+
"access": "public"
|
|
28
15
|
},
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
},
|
|
36
|
-
"extends": [
|
|
37
|
-
"standard",
|
|
38
|
-
"prettier",
|
|
39
|
-
"prettier/prettier"
|
|
40
|
-
],
|
|
41
|
-
"rules": {
|
|
42
|
-
"quotes": [
|
|
43
|
-
"error",
|
|
44
|
-
"single",
|
|
45
|
-
{
|
|
46
|
-
"avoidEscape": true,
|
|
47
|
-
"allowTemplateLiterals": true
|
|
48
|
-
}
|
|
49
|
-
]
|
|
50
|
-
}
|
|
16
|
+
"scripts": {
|
|
17
|
+
"build": "rimraf lib && tsc && amaroc ./src/index.ts && mv src/index.js lib/",
|
|
18
|
+
"prepublishOnly": "yarn build",
|
|
19
|
+
"typecheck": "tsc --noEmit",
|
|
20
|
+
"test": "node --test",
|
|
21
|
+
"test:ci": "node --test"
|
|
51
22
|
},
|
|
52
23
|
"devDependencies": {
|
|
53
|
-
"
|
|
54
|
-
"
|
|
55
|
-
"
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
"eslint-plugin-promise": "^6.0.0",
|
|
59
|
-
"husky": "^7.0.4",
|
|
60
|
-
"lint-staged": "^12.3.7",
|
|
61
|
-
"prettier": "^2.6.2"
|
|
24
|
+
"@types/node": "^25.2.3",
|
|
25
|
+
"amaroc": "^1.0.1",
|
|
26
|
+
"oxlint-tsgolint": "^0.12.2",
|
|
27
|
+
"rimraf": "^6.1.2",
|
|
28
|
+
"typescript": "^5.9.3"
|
|
62
29
|
},
|
|
63
|
-
"
|
|
64
|
-
"prepare": "husky install"
|
|
65
|
-
}
|
|
30
|
+
"gitHead": "43180e101ef8e6b579c60f24ab5329577627134e"
|
|
66
31
|
}
|
package/.editorconfig
DELETED
package/.husky/pre-commit
DELETED
package/index.js
DELETED
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
const WRITE_INDEX = 0
|
|
2
|
-
const READ_INDEX = 1
|
|
3
|
-
const END_OF_PACKET = -1
|
|
4
|
-
|
|
5
|
-
export function alloc(size) {
|
|
6
|
-
return {
|
|
7
|
-
sharedState: new SharedArrayBuffer(8),
|
|
8
|
-
sharedBuffer: new SharedArrayBuffer(size),
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
async function* _reader({ sharedState, sharedBuffer }, cb) {
|
|
13
|
-
const state = new Int32Array(sharedState)
|
|
14
|
-
const buffer = Buffer.from(sharedBuffer)
|
|
15
|
-
|
|
16
|
-
let readPos = 0
|
|
17
|
-
let writePos = 0
|
|
18
|
-
|
|
19
|
-
while (true) {
|
|
20
|
-
const { async, value } = Atomics.waitAsync(state, WRITE_INDEX, writePos)
|
|
21
|
-
if (async) {
|
|
22
|
-
await value
|
|
23
|
-
}
|
|
24
|
-
writePos = Atomics.load(state, WRITE_INDEX)
|
|
25
|
-
|
|
26
|
-
while (readPos !== writePos) {
|
|
27
|
-
const len = buffer.readInt32LE(readPos)
|
|
28
|
-
|
|
29
|
-
if (len === END_OF_PACKET) {
|
|
30
|
-
readPos = 0
|
|
31
|
-
} else {
|
|
32
|
-
const raw = buffer.slice(readPos + 4, readPos + len)
|
|
33
|
-
readPos += len
|
|
34
|
-
if (cb) {
|
|
35
|
-
const thenable = cb(raw)
|
|
36
|
-
if (thenable && typeof thenable.then === 'function') {
|
|
37
|
-
await thenable
|
|
38
|
-
}
|
|
39
|
-
} else {
|
|
40
|
-
yield raw
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
Atomics.store(state, READ_INDEX, readPos)
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
Atomics.notify(state, READ_INDEX)
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export function reader(options, cb) {
|
|
52
|
-
if (cb) {
|
|
53
|
-
_reader(options, cb).next()
|
|
54
|
-
} else {
|
|
55
|
-
return _reader(options)
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export function writer({ sharedState, sharedBuffer }) {
|
|
60
|
-
const state = new Int32Array(sharedState)
|
|
61
|
-
const buffer = Buffer.from(sharedBuffer)
|
|
62
|
-
const size = buffer.byteLength
|
|
63
|
-
const queue = []
|
|
64
|
-
|
|
65
|
-
let readPos = 0
|
|
66
|
-
let writePos = 0
|
|
67
|
-
let flushing = null
|
|
68
|
-
|
|
69
|
-
function tryWrite(...raw) {
|
|
70
|
-
readPos = Atomics.load(state, READ_INDEX)
|
|
71
|
-
|
|
72
|
-
const len = raw.reduce((len, buf) => len + buf.byteLength, 4)
|
|
73
|
-
|
|
74
|
-
if (size - writePos < len + 4) {
|
|
75
|
-
if (readPos < len + 4) {
|
|
76
|
-
return false
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
buffer.writeInt32LE(-1, writePos)
|
|
80
|
-
writePos = 0
|
|
81
|
-
} else {
|
|
82
|
-
const available = writePos >= readPos ? size - writePos : readPos - writePos
|
|
83
|
-
|
|
84
|
-
if (available < len + 4) {
|
|
85
|
-
return false
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
buffer.writeInt32LE(len, writePos)
|
|
90
|
-
writePos += 4
|
|
91
|
-
|
|
92
|
-
for (const buf of raw) {
|
|
93
|
-
buffer.set(buf, writePos)
|
|
94
|
-
writePos += buf.byteLength
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
Atomics.store(state, WRITE_INDEX, writePos)
|
|
98
|
-
Atomics.notify(state, WRITE_INDEX)
|
|
99
|
-
|
|
100
|
-
return true
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
async function flush() {
|
|
104
|
-
while (queue.length) {
|
|
105
|
-
while (!tryWrite(queue[0])) {
|
|
106
|
-
const { async, value } = Atomics.waitAsync(state, READ_INDEX, readPos)
|
|
107
|
-
if (async) {
|
|
108
|
-
await value
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
queue.shift()
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
flushing = null
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
function write(...raw) {
|
|
118
|
-
if (!queue.length && tryWrite(...raw)) {
|
|
119
|
-
return
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
queue.push(Buffer.concat(raw))
|
|
123
|
-
|
|
124
|
-
return (flushing ??= flush())
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
return write
|
|
128
|
-
}
|