bare-ipc 1.0.1 → 1.1.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 CHANGED
@@ -6,6 +6,53 @@ Lightweight pipe-based IPC for Bare.
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:
28
+
29
+ ```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.
38
+
39
+ ### `IPCPort`
40
+
41
+ #### `const port = new IPCPort(incoming, outgoing)`
42
+
43
+ The arguments are:
44
+
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`.
51
+
52
+ #### `port.detached`
53
+
54
+ A boolean for whether the `port` is detached.
55
+
9
56
  ## License
10
57
 
11
58
  Apache-2.0
package/index.d.ts ADDED
@@ -0,0 +1,33 @@
1
+ import { Duplex } from 'bare-stream'
2
+ import { Transferable, symbols } from 'bare-structured-clone'
3
+
4
+ interface IPC extends Duplex {
5
+ ref(): this
6
+ unref(): this
7
+ }
8
+
9
+ declare class IPC {
10
+ constructor(port: IPCPort)
11
+ }
12
+
13
+ interface IPCPort extends Transferable<[incoming: number, outgoing: number]> {
14
+ readonly incoming: number
15
+ readonly outgoing: number
16
+ readonly detached: boolean
17
+
18
+ connect(): IPC
19
+ }
20
+
21
+ declare class IPCPort {
22
+ constructor(incoming: number, outgoing: number)
23
+
24
+ static [symbols.attach](input: [incoming: number, outgoing: number]): IPCPort
25
+ }
26
+
27
+ declare namespace IPC {
28
+ export { IPCPort }
29
+
30
+ export function open(): [IPCPort, IPCPort]
31
+ }
32
+
33
+ export = IPC
package/index.js CHANGED
@@ -2,52 +2,73 @@ const Pipe = require('bare-pipe')
2
2
  const { Duplex } = require('bare-stream')
3
3
  const errors = require('./lib/errors')
4
4
 
5
- const IPC =
6
- (module.exports =
7
- exports =
8
- class IPC extends Duplex {
9
- constructor(port) {
10
- const { incoming, outgoing } = port
5
+ module.exports = exports = class IPC extends Duplex {
6
+ constructor(port) {
7
+ const { incoming, outgoing } = port
11
8
 
12
- super()
9
+ super()
13
10
 
14
- this._incoming = new Pipe(incoming)
15
- this._outgoing = new Pipe(outgoing)
11
+ this._incoming = new Pipe(incoming)
12
+ this._outgoing = new Pipe(outgoing)
16
13
 
17
- this._pendingWrite = null
14
+ this._pendingWrite = null
18
15
 
19
- this._incoming
20
- .on('data', this._ondata.bind(this))
21
- .on('end', this._onend.bind(this))
16
+ this._incoming
17
+ .on('data', this._ondata.bind(this))
18
+ .on('end', this._onend.bind(this))
19
+ .pause()
22
20
 
23
- this._outgoing.on('drain', this._ondrain.bind(this))
24
- }
21
+ this._outgoing.on('drain', this._ondrain.bind(this))
22
+ }
25
23
 
26
- _write(chunk, encoding, cb) {
27
- if (this._outgoing.write(chunk)) cb(null)
28
- else this._pendingWrite = cb
29
- }
24
+ ref() {
25
+ this._incoming.ref()
26
+ this._outgoing.ref()
27
+ }
30
28
 
31
- _final(cb) {
32
- this._outgoing.end()
33
- cb(null)
34
- }
29
+ unref() {
30
+ this._incoming.unref()
31
+ this._outgoing.unref()
32
+ }
35
33
 
36
- _ondata(data) {
37
- this.push(data)
38
- }
34
+ _read() {
35
+ this._incoming.resume()
36
+ }
39
37
 
40
- _onend() {
41
- this.push(null)
42
- }
38
+ _write(chunk, encoding, cb) {
39
+ if (this._outgoing.write(chunk)) cb(null)
40
+ else this._pendingWrite = cb
41
+ }
42
+
43
+ _final(cb) {
44
+ this._outgoing.end()
45
+ cb(null)
46
+ }
47
+
48
+ _predestroy() {
49
+ this._incoming.destroy()
50
+ this._outgoing.destroy()
51
+ }
52
+
53
+ _ondata(data) {
54
+ if (this.push(data) === false) {
55
+ this._incoming.pause()
56
+ }
57
+ }
58
+
59
+ _onend() {
60
+ this.push(null)
61
+ }
62
+
63
+ _ondrain() {
64
+ if (this._pendingWrite === null) return
65
+ const cb = this._pendingWrite
66
+ this._pendingWrite = null
67
+ cb(null)
68
+ }
69
+ }
43
70
 
44
- _ondrain() {
45
- if (this._pendingWrite === null) return
46
- const cb = this._pendingWrite
47
- this._pendingWrite = null
48
- cb(null)
49
- }
50
- })
71
+ const IPC = module.exports
51
72
 
52
73
  class IPCPort {
53
74
  constructor(incoming, outgoing) {
@@ -79,9 +100,9 @@ class IPCPort {
79
100
  }
80
101
  }
81
102
 
82
- exports.open = function open(opts) {
83
- const a = Pipe.pipe(opts)
84
- const b = Pipe.pipe(opts)
103
+ exports.open = function open() {
104
+ const a = Pipe.pipe()
105
+ const b = Pipe.pipe()
85
106
 
86
- return [new IPCPort(a[0], b[1], opts), new IPCPort(b[0], a[1], opts)]
107
+ return [new IPCPort(a[0], b[1]), new IPCPort(b[0], a[1])]
87
108
  }
@@ -0,0 +1,5 @@
1
+ declare class IPCError extends Error {
2
+ static ALREADY_CONNECTED(msg: string): IPCError
3
+ }
4
+
5
+ export = IPCError
package/package.json CHANGED
@@ -1,14 +1,21 @@
1
1
  {
2
2
  "name": "bare-ipc",
3
- "version": "1.0.1",
3
+ "version": "1.1.1",
4
4
  "description": "Lightweight pipe-based IPC for Bare",
5
5
  "exports": {
6
- ".": "./index.js",
6
+ ".": {
7
+ "types": "./index.d.ts",
8
+ "default": "./index.js"
9
+ },
7
10
  "./package": "./package.json",
8
- "./errors": "./lib/errors.js"
11
+ "./errors": {
12
+ "types": "./lib/errors.d.ts",
13
+ "default": "./lib/errors.js"
14
+ }
9
15
  },
10
16
  "files": [
11
17
  "index.js",
18
+ "index.d.ts",
12
19
  "lib"
13
20
  ],
14
21
  "scripts": {
@@ -26,7 +33,8 @@
26
33
  "homepage": "https://github.com/holepunchto/bare-ipc#readme",
27
34
  "dependencies": {
28
35
  "bare-pipe": "^4.0.0",
29
- "bare-stream": "^2.1.3"
36
+ "bare-stream": "^2.1.3",
37
+ "bare-structured-clone": "^1.4.0"
30
38
  },
31
39
  "devDependencies": {
32
40
  "brittle": "^3.2.1",