pear-opwait 1.0.0 → 1.0.1
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/README.md +11 -10
- package/index.js +10 -4
- package/package.json +23 -5
package/README.md
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
> Pear operation stream promise wrapper
|
|
4
4
|
|
|
5
|
+
See [pear-opstream](https://github.com/holepunchto/pear-opstream).
|
|
6
|
+
|
|
5
7
|
## API
|
|
6
8
|
|
|
7
9
|
### `opwait(stream[, onstatus])`
|
|
@@ -13,19 +15,18 @@ Returns `Promise<any>` of the `final` data.
|
|
|
13
15
|
|
|
14
16
|
**Arguments**
|
|
15
17
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
+
- `stream`: `Readable` emitting `{ tag, data }`
|
|
19
|
+
- `onstatus`: `(status) => void|Promise<void>` optional observer for each status
|
|
18
20
|
|
|
19
21
|
**Behavior**
|
|
20
22
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
23
|
+
- Listens to `data`, `end`, `error`
|
|
24
|
+
- `tag === 'final'` → stores `data` as resolution value
|
|
25
|
+
- `tag === 'error'` → destroys stream with `ERR_OPERATION_FAILED(message, data)`
|
|
26
|
+
- `onstatus` may be async; rejection throws into the stream via `destroy(err)`
|
|
27
|
+
- Promise **resolves** on `end` with last seen `final` (or `null` if none)
|
|
28
|
+
- Promise **rejects** on `error` from the stream (including wrapped operation errors)
|
|
28
29
|
|
|
29
30
|
## License
|
|
30
31
|
|
|
31
|
-
Apache-2.0
|
|
32
|
+
Apache-2.0
|
package/index.js
CHANGED
|
@@ -2,22 +2,28 @@
|
|
|
2
2
|
const noop = () => {}
|
|
3
3
|
const { ERR_OPERATION_FAILED } = require('pear-errors')
|
|
4
4
|
|
|
5
|
-
module.exports = function opwait
|
|
5
|
+
module.exports = function opwait(stream, onstatus) {
|
|
6
6
|
if (typeof onstatus !== 'function') onstatus = noop
|
|
7
7
|
return new Promise((resolve, reject) => {
|
|
8
8
|
let final = null
|
|
9
9
|
stream.once('error', reject)
|
|
10
|
-
stream.on('end', () => {
|
|
10
|
+
stream.on('end', () => {
|
|
11
|
+
resolve(final)
|
|
12
|
+
})
|
|
11
13
|
stream.on('data', (status) => {
|
|
12
14
|
const { tag, data } = status
|
|
13
15
|
if (tag === 'error') {
|
|
14
|
-
stream.destroy(
|
|
16
|
+
stream.destroy(
|
|
17
|
+
ERR_OPERATION_FAILED(data.stack || data.message || 'Unknown', data)
|
|
18
|
+
)
|
|
15
19
|
return
|
|
16
20
|
}
|
|
17
21
|
if (tag === 'final') final = data
|
|
18
22
|
try {
|
|
19
23
|
const p = onstatus(status)
|
|
20
|
-
if (typeof p?.catch === 'function')
|
|
24
|
+
if (typeof p?.catch === 'function') {
|
|
25
|
+
p.catch((err) => stream.destroy(err))
|
|
26
|
+
}
|
|
21
27
|
} catch (err) {
|
|
22
28
|
stream.destroy(err)
|
|
23
29
|
}
|
package/package.json
CHANGED
|
@@ -1,22 +1,40 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pear-opwait",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"description": "Pear operation stream promise wrapper",
|
|
7
7
|
"license": "Apache-2.0",
|
|
8
8
|
"files": [],
|
|
9
9
|
"scripts": {
|
|
10
|
+
"format": "prettier --write .",
|
|
11
|
+
"lint": "prettier --check . && lunte",
|
|
10
12
|
"test:gen": "brittle -r test/all.js test/*.test.js",
|
|
11
|
-
"test": "
|
|
12
|
-
"
|
|
13
|
+
"test": "npm run test:node && npm run test:bare",
|
|
14
|
+
"test:node": "brittle-node --coverage test/all.js",
|
|
15
|
+
"test:bare": "brittle-bare --coverage test/all.js"
|
|
13
16
|
},
|
|
14
17
|
"devDependencies": {
|
|
15
18
|
"brittle": "^3.0.0",
|
|
16
|
-
"
|
|
19
|
+
"lunte": "^1.3.0",
|
|
20
|
+
"prettier": "^3.6.2",
|
|
21
|
+
"prettier-config-holepunch": "^1.0.0",
|
|
17
22
|
"streamx": "^2.22.1"
|
|
18
23
|
},
|
|
19
24
|
"dependencies": {
|
|
20
25
|
"pear-errors": "^1.0.0"
|
|
21
|
-
}
|
|
26
|
+
},
|
|
27
|
+
"directories": {
|
|
28
|
+
"test": "test"
|
|
29
|
+
},
|
|
30
|
+
"repository": {
|
|
31
|
+
"type": "git",
|
|
32
|
+
"url": "git+https://github.com/holepunchto/pear-opwait.git"
|
|
33
|
+
},
|
|
34
|
+
"keywords": [],
|
|
35
|
+
"author": "",
|
|
36
|
+
"bugs": {
|
|
37
|
+
"url": "https://github.com/holepunchto/pear-opwait/issues"
|
|
38
|
+
},
|
|
39
|
+
"homepage": "https://github.com/holepunchto/pear-opwait#readme"
|
|
22
40
|
}
|