bare-ipc 1.1.1 → 1.1.2

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 CHANGED
@@ -1,30 +1,57 @@
1
1
  # bare-ipc
2
2
 
3
- Lightweight pipe-based IPC for Bare.
3
+ Lightweight pipe-based IPC for Bare. Built on top of `bare-pipe` (<https://github.com/holepunchto/bare-pipe>), it exposes a duplex stream over a pair of file handles whose ports can be transferred between Bare threads and processes.
4
4
 
5
5
  ```
6
6
  npm i bare-ipc
7
7
  ```
8
8
 
9
+ ## Usage
10
+
11
+ ```js
12
+ const IPC = require('bare-ipc')
13
+
14
+ const [portA, portB] = IPC.open()
15
+
16
+ const a = portA.connect()
17
+ const b = portB.connect()
18
+
19
+ a.on('data', (data) => {
20
+ // Handle data received from b
21
+ }).end('hello b')
22
+
23
+ b.on('data', (data) => {
24
+ // Handle data received from a
25
+ }).end('hello a')
26
+ ```
27
+
9
28
  ## API
10
29
 
11
30
  ### `IPC`
12
31
 
13
- #### `const [portA, portB] = IPC.open(opts)`
32
+ #### `const [portA, portB] = IPC.open()`
14
33
 
15
- Returns a pair of `IPCPort`s for use constructing the IPC duplex stream based on
16
- `bare-pipe`.
34
+ Returns a pair of connected `IPCPort`s for constructing the IPC duplex stream based on `bare-pipe`. Each port is transferable and can be sent to another thread or process before being connected.
17
35
 
18
36
  #### `const ipc = new IPC(port)`
19
37
 
20
- Returns a duplex stream using the provided port. See `streamx`'s [`Duplex`](https://github.com/mafintosh/streamx?tab=readme-ov-file#duplex-stream) for Duplex API.
38
+ Returns a duplex stream using the provided `port`. See `bare-stream`'s `Duplex` (<https://github.com/holepunchto/bare-stream>) for the duplex stream API.
39
+
40
+ Errors emitted by the underlying incoming or outgoing pipes are propagated to the stream as `error` events, after which the stream is destroyed.
41
+
42
+ #### `ipc.incoming`
43
+
44
+ The underlying `bare-pipe` `Pipe` used for reading. Read-only.
45
+
46
+ #### `ipc.outgoing`
47
+
48
+ The underlying `bare-pipe` `Pipe` used for writing. Read-only.
21
49
 
22
50
  #### `ipc.ref()`
23
51
 
24
52
  Increase the reference count for the IPC to keep the event loop alive.
25
53
 
26
- A common pattern is to `ipc.ref()` on `Bare.on('resume')` and `ipc.unref()` on
27
- suspend like so:
54
+ A common pattern is to `ipc.ref()` on `Bare.on('resume')` and `ipc.unref()` on suspend like so:
28
55
 
29
56
  ```js
30
57
  Bare.on('suspend', () => ipc.unref()).on('resume', () => ipc.ref())
@@ -34,24 +61,32 @@ Bare.on('suspend', () => ipc.unref()).on('resume', () => ipc.ref())
34
61
 
35
62
  Decrease the reference count for the IPC to allow the event loop to exit.
36
63
 
37
- See [`ipc.ref()`](#ipcref) for common pattern to keep the event loop alive.
64
+ See `ipc.ref()` for the common pattern to keep the event loop alive.
38
65
 
39
66
  ### `IPCPort`
40
67
 
41
68
  #### `const port = new IPCPort(incoming, outgoing)`
42
69
 
43
- The arguments are:
70
+ Constructs a port from a pair of file handles. The arguments are:
44
71
 
45
- - `incoming` is the read file handle
46
- - `outgoing` is the write file handle
72
+ - `incoming` is the read file handle.
73
+ - `outgoing` is the write file handle.
47
74
 
48
75
  #### `port.connect()`
49
76
 
50
- Returns an `IPC` connected to the `port`.
77
+ Returns an `IPC` connected to the `port` and marks the `port` as detached.
78
+
79
+ #### `port.incoming`
80
+
81
+ The read file handle. Read-only.
82
+
83
+ #### `port.outgoing`
84
+
85
+ The write file handle. Read-only.
51
86
 
52
87
  #### `port.detached`
53
88
 
54
- A boolean for whether the `port` is detached.
89
+ A boolean for whether the `port` is detached. A port becomes detached once it is connected or transferred, and a detached port cannot be transferred again.
55
90
 
56
91
  ## License
57
92
 
package/index.d.ts CHANGED
@@ -1,7 +1,11 @@
1
1
  import { Duplex } from 'bare-stream'
2
+ import Pipe from 'bare-pipe'
2
3
  import { Transferable, symbols } from 'bare-structured-clone'
3
4
 
4
5
  interface IPC extends Duplex {
6
+ readonly incoming: Pipe
7
+ readonly outgoing: Pipe
8
+
5
9
  ref(): this
6
10
  unref(): this
7
11
  }
package/index.js CHANGED
@@ -2,7 +2,7 @@ const Pipe = require('bare-pipe')
2
2
  const { Duplex } = require('bare-stream')
3
3
  const errors = require('./lib/errors')
4
4
 
5
- module.exports = exports = class IPC extends Duplex {
5
+ class IPC extends Duplex {
6
6
  constructor(port) {
7
7
  const { incoming, outgoing } = port
8
8
 
@@ -13,12 +13,23 @@ module.exports = exports = class IPC extends Duplex {
13
13
 
14
14
  this._pendingWrite = null
15
15
 
16
+ this._onerror = this._onerror.bind(this)
17
+
16
18
  this._incoming
17
19
  .on('data', this._ondata.bind(this))
18
20
  .on('end', this._onend.bind(this))
21
+ .on('error', this._onerror)
19
22
  .pause()
20
23
 
21
- this._outgoing.on('drain', this._ondrain.bind(this))
24
+ this._outgoing.on('drain', this._ondrain.bind(this)).on('error', this._onerror)
25
+ }
26
+
27
+ get incoming() {
28
+ return this._incoming
29
+ }
30
+
31
+ get outgoing() {
32
+ return this._outgoing
22
33
  }
23
34
 
24
35
  ref() {
@@ -50,6 +61,10 @@ module.exports = exports = class IPC extends Duplex {
50
61
  this._outgoing.destroy()
51
62
  }
52
63
 
64
+ _onerror(err) {
65
+ this.destroy(err)
66
+ }
67
+
53
68
  _ondata(data) {
54
69
  if (this.push(data) === false) {
55
70
  this._incoming.pause()
@@ -68,7 +83,7 @@ module.exports = exports = class IPC extends Duplex {
68
83
  }
69
84
  }
70
85
 
71
- const IPC = module.exports
86
+ module.exports = exports = IPC
72
87
 
73
88
  class IPCPort {
74
89
  constructor(incoming, outgoing) {
@@ -85,9 +100,7 @@ class IPCPort {
85
100
 
86
101
  [Symbol.for('bare.detach')]() {
87
102
  if (this.detached) {
88
- throw errors.ALREADY_CONNECTED(
89
- 'Port has already started receiving messages'
90
- )
103
+ throw errors.ALREADY_CONNECTED('Port has already started receiving messages')
91
104
  }
92
105
 
93
106
  this.detached = true
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-ipc",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
4
4
  "description": "Lightweight pipe-based IPC for Bare",
5
5
  "exports": {
6
6
  ".": {
@@ -19,7 +19,9 @@
19
19
  "lib"
20
20
  ],
21
21
  "scripts": {
22
- "test": "prettier . --check && bare test.js"
22
+ "format": "prettier --write . && lunte --fix",
23
+ "lint": "prettier --check . && lunte",
24
+ "test": "brittle-bare --coverage test.js"
23
25
  },
24
26
  "repository": {
25
27
  "type": "git",
@@ -38,7 +40,8 @@
38
40
  },
39
41
  "devDependencies": {
40
42
  "brittle": "^3.2.1",
43
+ "lunte": "^1.8.0",
41
44
  "prettier": "^3.4.1",
42
- "prettier-config-standard": "^7.0.0"
45
+ "prettier-config-holepunch": "^2.0.0"
43
46
  }
44
47
  }