bare-ipc 1.1.0 → 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,11 +1,93 @@
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
+
28
+ ## API
29
+
30
+ ### `IPC`
31
+
32
+ #### `const [portA, portB] = IPC.open()`
33
+
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.
35
+
36
+ #### `const ipc = new IPC(port)`
37
+
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.
49
+
50
+ #### `ipc.ref()`
51
+
52
+ Increase the reference count for the IPC to keep the event loop alive.
53
+
54
+ A common pattern is to `ipc.ref()` on `Bare.on('resume')` and `ipc.unref()` on suspend like so:
55
+
56
+ ```js
57
+ Bare.on('suspend', () => ipc.unref()).on('resume', () => ipc.ref())
58
+ ```
59
+
60
+ #### `ipc.unref()`
61
+
62
+ Decrease the reference count for the IPC to allow the event loop to exit.
63
+
64
+ See `ipc.ref()` for the common pattern to keep the event loop alive.
65
+
66
+ ### `IPCPort`
67
+
68
+ #### `const port = new IPCPort(incoming, outgoing)`
69
+
70
+ Constructs a port from a pair of file handles. The arguments are:
71
+
72
+ - `incoming` is the read file handle.
73
+ - `outgoing` is the write file handle.
74
+
75
+ #### `port.connect()`
76
+
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.
86
+
87
+ #### `port.detached`
88
+
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.
90
+
9
91
  ## License
10
92
 
11
93
  Apache-2.0
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() {
@@ -45,6 +56,15 @@ module.exports = exports = class IPC extends Duplex {
45
56
  cb(null)
46
57
  }
47
58
 
59
+ _predestroy() {
60
+ this._incoming.destroy()
61
+ this._outgoing.destroy()
62
+ }
63
+
64
+ _onerror(err) {
65
+ this.destroy(err)
66
+ }
67
+
48
68
  _ondata(data) {
49
69
  if (this.push(data) === false) {
50
70
  this._incoming.pause()
@@ -63,7 +83,7 @@ module.exports = exports = class IPC extends Duplex {
63
83
  }
64
84
  }
65
85
 
66
- const IPC = module.exports
86
+ module.exports = exports = IPC
67
87
 
68
88
  class IPCPort {
69
89
  constructor(incoming, outgoing) {
@@ -80,9 +100,7 @@ class IPCPort {
80
100
 
81
101
  [Symbol.for('bare.detach')]() {
82
102
  if (this.detached) {
83
- throw errors.ALREADY_CONNECTED(
84
- 'Port has already started receiving messages'
85
- )
103
+ throw errors.ALREADY_CONNECTED('Port has already started receiving messages')
86
104
  }
87
105
 
88
106
  this.detached = true
@@ -95,9 +113,9 @@ class IPCPort {
95
113
  }
96
114
  }
97
115
 
98
- exports.open = function open(opts) {
99
- const a = Pipe.pipe(opts)
100
- const b = Pipe.pipe(opts)
116
+ exports.open = function open() {
117
+ const a = Pipe.pipe()
118
+ const b = Pipe.pipe()
101
119
 
102
- return [new IPCPort(a[0], b[1], opts), new IPCPort(b[0], a[1], opts)]
120
+ return [new IPCPort(a[0], b[1]), new IPCPort(b[0], a[1])]
103
121
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-ipc",
3
- "version": "1.1.0",
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
  }