bare-ipc 1.0.0 → 1.1.0

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/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,12 +2,9 @@ const Pipe = require('bare-pipe')
2
2
  const { Duplex } = require('bare-stream')
3
3
  const errors = require('./lib/errors')
4
4
 
5
- const IPC = module.exports = exports = class IPC extends Duplex {
6
- constructor (port) {
7
- const {
8
- incoming,
9
- outgoing
10
- } = port
5
+ module.exports = exports = class IPC extends Duplex {
6
+ constructor(port) {
7
+ const { incoming, outgoing } = port
11
8
 
12
9
  super()
13
10
 
@@ -19,30 +16,46 @@ const IPC = module.exports = exports = class IPC extends Duplex {
19
16
  this._incoming
20
17
  .on('data', this._ondata.bind(this))
21
18
  .on('end', this._onend.bind(this))
19
+ .pause()
22
20
 
23
- this._outgoing
24
- .on('drain', this._ondrain.bind(this))
21
+ this._outgoing.on('drain', this._ondrain.bind(this))
25
22
  }
26
23
 
27
- _write (chunk, encoding, cb) {
24
+ ref() {
25
+ this._incoming.ref()
26
+ this._outgoing.ref()
27
+ }
28
+
29
+ unref() {
30
+ this._incoming.unref()
31
+ this._outgoing.unref()
32
+ }
33
+
34
+ _read() {
35
+ this._incoming.resume()
36
+ }
37
+
38
+ _write(chunk, encoding, cb) {
28
39
  if (this._outgoing.write(chunk)) cb(null)
29
40
  else this._pendingWrite = cb
30
41
  }
31
42
 
32
- _final (cb) {
43
+ _final(cb) {
33
44
  this._outgoing.end()
34
45
  cb(null)
35
46
  }
36
47
 
37
- _ondata (data) {
38
- this.push(data)
48
+ _ondata(data) {
49
+ if (this.push(data) === false) {
50
+ this._incoming.pause()
51
+ }
39
52
  }
40
53
 
41
- _onend () {
54
+ _onend() {
42
55
  this.push(null)
43
56
  }
44
57
 
45
- _ondrain () {
58
+ _ondrain() {
46
59
  if (this._pendingWrite === null) return
47
60
  const cb = this._pendingWrite
48
61
  this._pendingWrite = null
@@ -50,22 +63,26 @@ const IPC = module.exports = exports = class IPC extends Duplex {
50
63
  }
51
64
  }
52
65
 
66
+ const IPC = module.exports
67
+
53
68
  class IPCPort {
54
- constructor (incoming, outgoing) {
69
+ constructor(incoming, outgoing) {
55
70
  this.incoming = incoming
56
71
  this.outgoing = outgoing
57
72
  this.detached = false
58
73
  }
59
74
 
60
- connect () {
75
+ connect() {
61
76
  const ipc = new IPC(this)
62
77
  this.detached = true
63
78
  return ipc
64
79
  }
65
80
 
66
- [Symbol.for('bare.detach')] () {
81
+ [Symbol.for('bare.detach')]() {
67
82
  if (this.detached) {
68
- throw errors.ALREADY_CONNECTED('Port has already started receiving messages')
83
+ throw errors.ALREADY_CONNECTED(
84
+ 'Port has already started receiving messages'
85
+ )
69
86
  }
70
87
 
71
88
  this.detached = true
@@ -73,17 +90,14 @@ class IPCPort {
73
90
  return [this.incoming, this.outgoing]
74
91
  }
75
92
 
76
- static [Symbol.for('bare.attach')] ([incoming, outgoing]) {
93
+ static [Symbol.for('bare.attach')]([incoming, outgoing]) {
77
94
  return new this(incoming, outgoing)
78
95
  }
79
96
  }
80
97
 
81
- exports.open = function open (opts) {
98
+ exports.open = function open(opts) {
82
99
  const a = Pipe.pipe(opts)
83
100
  const b = Pipe.pipe(opts)
84
101
 
85
- return [
86
- new IPCPort(a[0], b[1], opts),
87
- new IPCPort(b[0], a[1], opts)
88
- ]
102
+ return [new IPCPort(a[0], b[1], opts), new IPCPort(b[0], a[1], opts)]
89
103
  }
@@ -0,0 +1,5 @@
1
+ declare class IPCError extends Error {
2
+ static ALREADY_CONNECTED(msg: string): IPCError
3
+ }
4
+
5
+ export = IPCError
package/lib/errors.js CHANGED
@@ -1,5 +1,5 @@
1
1
  module.exports = class IPCError extends Error {
2
- constructor (msg, code, fn = IPCError) {
2
+ constructor(msg, code, fn = IPCError) {
3
3
  super(`${code}: ${msg}`)
4
4
  this.code = code
5
5
 
@@ -8,11 +8,11 @@ module.exports = class IPCError extends Error {
8
8
  }
9
9
  }
10
10
 
11
- get name () {
11
+ get name() {
12
12
  return 'IPCError'
13
13
  }
14
14
 
15
- static ALREADY_CONNECTED (msg) {
15
+ static ALREADY_CONNECTED(msg) {
16
16
  return new IPCError(msg, 'ALREADY_CONNECTED', IPCError.ALREADY_CONNECTED)
17
17
  }
18
18
  }
package/package.json CHANGED
@@ -1,18 +1,25 @@
1
1
  {
2
2
  "name": "bare-ipc",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
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": {
15
- "test": "standard && bare test.js"
22
+ "test": "prettier . --check && bare test.js"
16
23
  },
17
24
  "repository": {
18
25
  "type": "git",
@@ -25,11 +32,13 @@
25
32
  },
26
33
  "homepage": "https://github.com/holepunchto/bare-ipc#readme",
27
34
  "dependencies": {
28
- "bare-pipe": "^3.3.2",
29
- "bare-stream": "^2.1.3"
35
+ "bare-pipe": "^4.0.0",
36
+ "bare-stream": "^2.1.3",
37
+ "bare-structured-clone": "^1.4.0"
30
38
  },
31
39
  "devDependencies": {
32
40
  "brittle": "^3.2.1",
33
- "standard": "^17.0.0"
41
+ "prettier": "^3.4.1",
42
+ "prettier-config-standard": "^7.0.0"
34
43
  }
35
44
  }