node-turbo 1.0.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/.c8rc.json +5 -0
- package/.esdoc.json +83 -0
- package/.eslintrc.json +10 -0
- package/.mocharc.json +7 -0
- package/LICENSE +21 -0
- package/README.md +620 -0
- package/docs/API.md +857 -0
- package/lib/errors/index.js +36 -0
- package/lib/express/express-turbo-stream.js +41 -0
- package/lib/express/index.js +4 -0
- package/lib/express/turbocharge-express.js +108 -0
- package/lib/index.js +8 -0
- package/lib/koa/index.js +4 -0
- package/lib/koa/koa-turbo-stream.js +44 -0
- package/lib/koa/turbocharge-koa.js +122 -0
- package/lib/request-helpers.js +53 -0
- package/lib/sse/index.js +3 -0
- package/lib/sse/sse-turbo-stream.js +137 -0
- package/lib/turbo-element.js +71 -0
- package/lib/turbo-frame.js +79 -0
- package/lib/turbo-readable.js +125 -0
- package/lib/turbo-stream-element.js +67 -0
- package/lib/turbo-stream.js +350 -0
- package/lib/ws/index.js +4 -0
- package/lib/ws/ws-turbo-stream.js +112 -0
- package/package.json +75 -0
- package/test/hooks.js +46 -0
- package/test/integration/express.test.js +137 -0
- package/test/integration/koa.test.js +125 -0
- package/test/integration/sse.test.js +80 -0
- package/test/integration/ws.test.js +155 -0
- package/test/package.test.js +68 -0
- package/test/unit/core/request-helpers.test.js +97 -0
- package/test/unit/core/turbo-element.test.js +15 -0
- package/test/unit/core/turbo-frame.test.js +63 -0
- package/test/unit/core/turbo-readable.test.js +93 -0
- package/test/unit/core/turbo-stream-element.test.js +76 -0
- package/test/unit/core/turbo-stream.test.js +308 -0
- package/test/unit/express/express-turbo-stream.test.js +39 -0
- package/test/unit/express/turbocharge-express.test.js +123 -0
- package/test/unit/koa/koa-turbo-stream.test.js +56 -0
- package/test/unit/koa/turbocharge-koa.test.js +141 -0
- package/test/unit/sse/sse-turbo-stream.test.js +109 -0
- package/test/unit/ws/ws-turbo-stream.test.js +46 -0
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
|
|
2
|
+
import { expect } from 'chai';
|
|
3
|
+
import { SseTurboStream } from '#sse';
|
|
4
|
+
import { Transform } from 'node:stream';
|
|
5
|
+
|
|
6
|
+
describe('SseTurboStream', function() {
|
|
7
|
+
|
|
8
|
+
describe('renderSseEvent()', function() {
|
|
9
|
+
it('returns null if no string', function() {
|
|
10
|
+
const sseSt = new SseTurboStream();
|
|
11
|
+
expect(sseSt.renderSseEvent()).to.be.null;
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it('correctly generates SSE data for each line', function() {
|
|
15
|
+
const result = new SseTurboStream().renderSseEvent('line 1\nline 2');
|
|
16
|
+
expect(result).to.equal('data: line 1\ndata: line 2\n\n');
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('correctly generates SSE message for named events', function() {
|
|
20
|
+
const result = new SseTurboStream('my-event').renderSseEvent('line 1\nline 2');
|
|
21
|
+
expect(result).to.equal('event: my-event\ndata: line 1\ndata: line 2\n\n');
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
describe('render()', function() {
|
|
27
|
+
it('returns null if no elements', function() {
|
|
28
|
+
const sseSt = new SseTurboStream('my-event');
|
|
29
|
+
|
|
30
|
+
expect(sseSt.render()).to.be.null;
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('generates SSE message', function() {
|
|
34
|
+
const sseSt = new SseTurboStream()
|
|
35
|
+
.append('t', 'c');
|
|
36
|
+
|
|
37
|
+
expect(sseSt.render()).to.equal('data: <turbo-stream action="append" target="t"><template>c</template></turbo-stream>\n\n');
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
it('generates SSE message with event name', function() {
|
|
42
|
+
const sseSt = new SseTurboStream('my-event')
|
|
43
|
+
.append('t', 'c');
|
|
44
|
+
|
|
45
|
+
expect(sseSt.render()).to.equal('event: my-event\ndata: <turbo-stream action="append" target="t"><template>c</template></turbo-stream>\n\n');
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
describe('event()', function() {
|
|
51
|
+
it('sets the event name', function() {
|
|
52
|
+
const sseSt = new SseTurboStream().event('my-event');
|
|
53
|
+
expect(sseSt.eventName).to.equal('my-event');
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('is chainable', function() {
|
|
57
|
+
const sseSt = new SseTurboStream().event('my-event');
|
|
58
|
+
expect(sseSt).to.be.an.instanceof(SseTurboStream);
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
it('createReadableStream() returns Transform', function() {
|
|
64
|
+
const sseSt = new SseTurboStream();
|
|
65
|
+
const readable = sseSt.createReadableStream();
|
|
66
|
+
|
|
67
|
+
expect(readable).to.be.an.instanceof(Transform);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
it('SSE message gets written to stream', function() {
|
|
72
|
+
return new Promise((resolve, reject) => {
|
|
73
|
+
const sseSt = new SseTurboStream();
|
|
74
|
+
const readable = sseSt.createReadableStream();
|
|
75
|
+
|
|
76
|
+
readable.on('data', chunk => {
|
|
77
|
+
expect(chunk.toString()).to.equal('data: <turbo-stream action="append" target="t"><template>c</template></turbo-stream>\n\n');
|
|
78
|
+
readable.destroy();
|
|
79
|
+
resolve();
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
readable.on('error', err => {
|
|
83
|
+
reject(err);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
sseSt.append('t', 'c');
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
it('SSE message with event name gets written to stream', function() {
|
|
92
|
+
return new Promise((resolve, reject) => {
|
|
93
|
+
const sseSt = new SseTurboStream('my-event');
|
|
94
|
+
const readable = sseSt.createReadableStream();
|
|
95
|
+
|
|
96
|
+
readable.on('data', chunk => {
|
|
97
|
+
expect(chunk.toString()).to.equal('event: my-event\ndata: <turbo-stream action="append" target="t"><template>c</template></turbo-stream>\n\n');
|
|
98
|
+
resolve();
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
readable.on('error', err => {
|
|
102
|
+
reject(err);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
sseSt.append('t', 'c');
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
});
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
|
|
2
|
+
import chai, { expect } from 'chai';
|
|
3
|
+
import spies from 'chai-spies';
|
|
4
|
+
import { WsTurboStream } from '#ws';
|
|
5
|
+
import { TurboStream } from '#core';
|
|
6
|
+
|
|
7
|
+
chai.use(spies);
|
|
8
|
+
|
|
9
|
+
// Mock WebSocket instance.
|
|
10
|
+
const mockWs = {
|
|
11
|
+
readyState: WsTurboStream.OPEN,
|
|
12
|
+
send() {}
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
describe('WsTurboStream', function() {
|
|
17
|
+
|
|
18
|
+
beforeEach(() => {
|
|
19
|
+
chai.spy.on(mockWs, 'send');
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
afterEach(() => {
|
|
24
|
+
chai.spy.restore(mockWs, 'send');
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
it('Message is sent to WebSocket on \'render\' when buffer === true', function() {
|
|
29
|
+
const wsts = new WsTurboStream(mockWs)
|
|
30
|
+
.append('t', 'c')
|
|
31
|
+
.flush();
|
|
32
|
+
|
|
33
|
+
expect(mockWs.send).to.have.been.called();
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
it('Message is sent to WebSocket on \'element\' when buffer === false', function() {
|
|
38
|
+
const wsts = WsTurboStream
|
|
39
|
+
.use(mockWs)
|
|
40
|
+
.append('t1', 'c1')
|
|
41
|
+
.append('t2', 'c2');
|
|
42
|
+
|
|
43
|
+
expect(mockWs.send).to.have.been.called.twice;
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
});
|