bare-ipc 1.1.1 → 1.1.3

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,57 +1,33 @@
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
- ## API
10
-
11
- ### `IPC`
12
-
13
- #### `const [portA, portB] = IPC.open(opts)`
14
-
15
- Returns a pair of `IPCPort`s for use constructing the IPC duplex stream based on
16
- `bare-pipe`.
17
-
18
- #### `const ipc = new IPC(port)`
19
-
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.
21
-
22
- #### `ipc.ref()`
23
-
24
- Increase the reference count for the IPC to keep the event loop alive.
25
-
26
- A common pattern is to `ipc.ref()` on `Bare.on('resume')` and `ipc.unref()` on
27
- suspend like so:
9
+ ## Usage
28
10
 
29
11
  ```js
30
- Bare.on('suspend', () => ipc.unref()).on('resume', () => ipc.ref())
31
- ```
32
-
33
- #### `ipc.unref()`
34
-
35
- Decrease the reference count for the IPC to allow the event loop to exit.
36
-
37
- See [`ipc.ref()`](#ipcref) for common pattern to keep the event loop alive.
12
+ const IPC = require('bare-ipc')
38
13
 
39
- ### `IPCPort`
14
+ const [portA, portB] = IPC.open()
40
15
 
41
- #### `const port = new IPCPort(incoming, outgoing)`
16
+ const a = portA.connect()
17
+ const b = portB.connect()
42
18
 
43
- The arguments are:
19
+ a.on('data', (data) => {
20
+ // Handle data received from b
21
+ }).end('hello b')
44
22
 
45
- - `incoming` is the read file handle
46
- - `outgoing` is the write file handle
47
-
48
- #### `port.connect()`
49
-
50
- Returns an `IPC` connected to the `port`.
23
+ b.on('data', (data) => {
24
+ // Handle data received from a
25
+ }).end('hello a')
26
+ ```
51
27
 
52
- #### `port.detached`
28
+ ## API
53
29
 
54
- A boolean for whether the `port` is detached.
30
+ See the [`bare-ipc` reference](https://docs.pears.com/reference/bare/modules/bare-ipc).
55
31
 
56
32
  ## License
57
33
 
package/index.d.ts CHANGED
@@ -1,24 +1,51 @@
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
+ /** The underlying `bare-pipe` `Pipe` used for reading. Read-only. */
7
+ readonly incoming: Pipe
8
+ /** The underlying `bare-pipe` `Pipe` used for writing. Read-only. */
9
+ readonly outgoing: Pipe
10
+
11
+ /** Increase the reference count for the IPC to keep the event loop alive. */
5
12
  ref(): this
13
+ /** Decrease the reference count for the IPC to allow the event loop to exit. */
6
14
  unref(): this
7
15
  }
8
16
 
9
17
  declare class IPC {
18
+ /**
19
+ * Returns a duplex stream using the provided `port`. See `bare-stream`'s `Duplex`
20
+ * (<https://github.com/holepunchto/bare-stream>) for the duplex stream API.
21
+ * @param port - The port to open the stream over, as returned by `IPC.open()`.
22
+ */
10
23
  constructor(port: IPCPort)
11
24
  }
12
25
 
13
26
  interface IPCPort extends Transferable<[incoming: number, outgoing: number]> {
27
+ /** The file handle used for reading. Read-only. */
14
28
  readonly incoming: number
29
+ /** The file handle used for writing. Read-only. */
15
30
  readonly outgoing: number
31
+ /**
32
+ * A boolean for whether the `port` is detached. A port becomes detached once it is connected or
33
+ * transferred, and a detached port cannot be transferred again.
34
+ */
16
35
  readonly detached: boolean
17
36
 
37
+ /**
38
+ * @returns An `IPC` duplex stream connected to the port.
39
+ */
18
40
  connect(): IPC
19
41
  }
20
42
 
21
43
  declare class IPCPort {
44
+ /**
45
+ * Constructs a port from a pair of file handles, `incoming` and `outgoing`.
46
+ * @param incoming - The file handle used for reading.
47
+ * @param outgoing - The file handle used for writing.
48
+ */
22
49
  constructor(incoming: number, outgoing: number)
23
50
 
24
51
  static [symbols.attach](input: [incoming: number, outgoing: number]): IPCPort
@@ -27,6 +54,12 @@ declare class IPCPort {
27
54
  declare namespace IPC {
28
55
  export { IPCPort }
29
56
 
57
+ /**
58
+ * Returns a pair of connected `IPCPort`s for constructing the IPC duplex stream based on
59
+ * `bare-pipe`. Each port is transferable and can be sent to another thread or process before
60
+ * being connected.
61
+ * @returns A pair of connected ports, one for each end of the IPC channel.
62
+ */
30
63
  export function open(): [IPCPort, IPCPort]
31
64
  }
32
65
 
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/lib/errors.d.ts CHANGED
@@ -1,4 +1,10 @@
1
1
  declare class IPCError extends Error {
2
+ /**
3
+ * Create the error thrown when transferring a port that has already been connected or
4
+ * transferred.
5
+ * @param msg - The error message.
6
+ * @returns An error with code `ALREADY_CONNECTED`.
7
+ */
2
8
  static ALREADY_CONNECTED(msg: string): IPCError
3
9
  }
4
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-ipc",
3
- "version": "1.1.1",
3
+ "version": "1.1.3",
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",
@@ -34,11 +36,12 @@
34
36
  "dependencies": {
35
37
  "bare-pipe": "^4.0.0",
36
38
  "bare-stream": "^2.1.3",
37
- "bare-structured-clone": "^1.4.0"
39
+ "bare-structured-clone": "^2.0.0"
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
  }