Skip to content

Commit 3ff25e0

Browse files
authored
feat(server): support stream and buffers
Supports stream and buffer as single parameter of exposed API.
1 parent 297ecdd commit 3ff25e0

File tree

12 files changed

+5413
-43
lines changed

12 files changed

+5413
-43
lines changed

.npmignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ test/
1111
.editorconfig
1212
.jsdoc.json
1313
proxy-benchmark.js
14-
TODO.md
14+
TODO.md
15+
yarn.lock

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ language: node_js
22
node_js:
33
- node
44
after_script:
5-
- npm run submit-coverage
5+
- yarn submit-coverage

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
# Changelog
33

44
## Unreleased
5+
#### Added
6+
- Support for Buffer and Stream parameters and results
7+
- Validates exposed API metadata such as `validate`, `validateResponse` or `responseSchema`
8+
- Usage of Yarn
9+
510
#### Changed
611
- Reformat CHANGELOG to follow [Keep a Changelog](https://keepachangelog.com) recommandations
712
- New documentation with latest docma v2.0.0

FAQ.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
- [What are deployment modes?](#what-are-deployment-modes-)
44
- [Can exposed API be asynchronous?](#can-exposed-api-be-asynchronous-)
5+
- [Are there limitations on exposed API signatures?](#are-there-limitations-on-exposed-api-signatures-)
56
- [Where to put asynchronous initialization?](#where-to-put-asynchronous-initialization-)
67
- [Can initialization code be configured?](#can-initialization-code-be-configured-)
78
- [How can service definition be more modular?](#how-can-service-definition-be-more-modular-)
@@ -83,6 +84,60 @@ startService({
8384
```
8485

8586

87+
## Are there limitations on exposed API signatures?
88+
89+
Despite all our efforts, yes. Hopefully main cases are covered.
90+
91+
Because parameters will be stringified when sent to server:
92+
- they could be `null` or `undefined`
93+
- they could be booleans, numbers, strings, arrays, and plain object (contains only properties of the previous types)
94+
- they could be of other types (Date, Error, RegExp, custom classes...) but will boils down to the output of their `toString()` method
95+
- they could be `Buffer` or `Stream` (see bellow). All paremeter but the first will be ignored
96+
97+
In particular, don't use functions as parameters.
98+
99+
Same limitations applies to API returned object.
100+
101+
You can use destructuring, rest parameters and even default values:
102+
```js
103+
async withExoticParameters ([a, b], {c: {d}} = {}, ...other) {
104+
return [a, b, d, ...other]
105+
}
106+
```
107+
108+
To use `Buffer` input parameter:
109+
- decorate API with `hasBufferInput`
110+
- use only one parameter (others will be set to `undefined`)
111+
```js
112+
const api = {
113+
async bufferHandling (buffer) {
114+
assert(Buffer.isBuffer(buffer))
115+
return Buffer.concat([buffer, new Uint8Array([3, 4])])
116+
}
117+
}
118+
// decorate
119+
apis.bufferHandling.hasBufferInput = true
120+
```
121+
122+
To use `Stream` input parameter:
123+
- decorate API with `hasStreamInput`
124+
- use only one parameter (others will be set to `undefined`)
125+
```js
126+
const api = {
127+
async streamHandling (stream) {
128+
assert(stream instanceof Readable)
129+
const prefix = new BufferList()
130+
prefix.append('here is a prefix -- ', 'utf8')
131+
return multistream([prefix, stream])
132+
}
133+
}
134+
// decorate
135+
apis.streamHandling.hasStreamInput = true
136+
```
137+
138+
You can return `Stream` and `Buffer` without any decoration, but don't nest them in objects (they will be stringified).
139+
140+
86141
## Where to put asynchronous initialization?
87142
88143
To serve this purpose, the `init()` function can be either synchronous or return a Promise.

docs/content/changelog.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
<hr />
44
<h2 id="unreleased">Unreleased</h2>
55
<hr />
6+
<h4 id="added">Added</h4>
7+
<ul>
8+
<li>Support for Buffer and Stream parameters and results</li>
9+
<li>Validates exposed API metadata such as <code>validate</code>, <code>validateResponse</code> or <code>responseSchema</code></li>
10+
<li>Usage of Yarn </li>
11+
</ul>
612
<h4 id="changed">Changed</h4>
713
<ul>
814
<li>Reformat CHANGELOG to follow <a href="https://keepachangelog.com">Keep a Changelog</a> recommandations</li>

docs/content/faq.html

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<ul>
55
<li><a href="#what-are-deployment-modes-">What are deployment modes?</a></li>
66
<li><a href="#can-exposed-api-be-asynchronous-">Can exposed API be asynchronous?</a></li>
7+
<li><a href="#are-there-limitations-on-exposed-api-signatures-">Are there limitations on exposed API signatures?</a></li>
78
<li><a href="#where-to-put-asynchronous-initialization-">Where to put asynchronous initialization?</a></li>
89
<li><a href="#can-initialization-code-be-configured-">Can initialization code be configured?</a></li>
910
<li><a href="#how-can-service-definition-be-more-modular-">How can service definition be more modular?</a></li>
@@ -77,6 +78,54 @@ <h2 id="can-exposed-api-be-asynchronous-">Can exposed API be asynchronous?</h2>
7778
})
7879
})
7980
</code></pre>
81+
<h2 id="are-there-limitations-on-exposed-api-signatures-">Are there limitations on exposed API signatures?</h2>
82+
<hr />
83+
<p>Despite all our efforts, yes. Hopefully main cases are covered.</p>
84+
<p>Because parameters will be stringified when sent to server:</p>
85+
<ul>
86+
<li>they could be <code>null</code> or <code>undefined</code></li>
87+
<li>they could be booleans, numbers, strings, arrays, and plain object (contains only properties of the previous types)</li>
88+
<li>they could be of other types (Date, Error, RegExp, custom classes...) but will boils down to the output of their <code>toString()</code> method</li>
89+
<li>they could be <code>Buffer</code> or <code>Stream</code> (see bellow). All paremeter but the first will be ignored</li>
90+
</ul>
91+
<p>In particular, don't use functions as parameters.</p>
92+
<p>Same limitations applies to API returned object.</p>
93+
<p>You can use destructuring, rest parameters and even default values:</p>
94+
<pre><code class="lang-js">async withExoticParameters ([a, b], {c: {d}} = {}, ...other) {
95+
return [a, b, d, ...other]
96+
}
97+
</code></pre>
98+
<p>To use <code>Buffer</code> input parameter:</p>
99+
<ul>
100+
<li>decorate API with <code>hasBufferInput</code></li>
101+
<li>use only one parameter (others will be set to <code>undefined</code>)<pre><code class="lang-js"> const api = {
102+
async bufferHandling (buffer) {
103+
assert(Buffer.isBuffer(buffer))
104+
return Buffer.concat([buffer, new Uint8Array([3, 4])])
105+
}
106+
}
107+
// decorate
108+
apis.bufferHandling.hasBufferInput = true
109+
</code></pre>
110+
</li>
111+
</ul>
112+
<p>To use <code>Stream</code> input parameter:</p>
113+
<ul>
114+
<li>decorate API with <code>hasStreamInput</code></li>
115+
<li>use only one parameter (others will be set to <code>undefined</code>)<pre><code class="lang-js"> const api = {
116+
async streamHandling (stream) {
117+
assert(stream instanceof Readable)
118+
const prefix = new BufferList()
119+
prefix.append('here is a prefix -- ', 'utf8')
120+
return multistream([prefix, stream])
121+
}
122+
}
123+
// decorate
124+
apis.streamHandling.hasStreamInput = true
125+
</code></pre>
126+
</li>
127+
</ul>
128+
<p>You can return <code>Stream</code> and <code>Buffer</code> without any decoration, but don't nest them in objects (they will be stringified).</p>
80129
<h2 id="where-to-put-asynchronous-initialization-">Where to put asynchronous initialization?</h2>
81130
<hr />
82131
<p>To serve this purpose, the <code>init()</code> function can be either synchronous or return a Promise.</p>

0 commit comments

Comments
 (0)