-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayground.mjs
196 lines (173 loc) · 4.48 KB
/
playground.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import {fetch, Response} from 'native-fetch'
import {
ReadableStream,
WritableStream,
TransformStream,
ByteLengthQueuingStrategy,
} from 'node:stream/web'
import fastify from 'fastify'
import stream from 'stream'
import delay from 'delay'
class TimeoutStream extends stream.Readable {
constructor(size, speed, requestTimeout = 0, timeouts) {
super()
this.size = size
this.requestTimeout = requestTimeout
this.timeouts = [...timeouts].sort(
(a, b) => (a.after || 0) - (b.after || 0)
)
this._transferred = 0
this.speed = speed
}
async _read(chunkSize) {
// if (this.requestTimeout) {
// await delay(this.requestTimeout)
// this.requestTimeout = 0
// }
// this.push(Buffer.alloc(0))
const toTransfer = Math.min(this.size - this._transferred, chunkSize)
if (this.speed) await delay((toTransfer / this.speed) * 1000)
if (this.timeouts.length && this.timeouts[0].after <= this._transferred) {
console.log('timeout')
await delay(this.timeouts[0].time)
this.timeouts.splice(0, 1)
}
if (this.aborted) {
this.push(null)
return
}
this.push(Buffer.alloc(toTransfer))
this._transferred += toTransfer
if (this.size <= this._transferred) {
console.log('TimeoutStream finish')
this.push(null)
}
}
_destroy() {
this.aborted = true
this.push(null)
}
}
const server = fastify()
server.route({
method: 'POST',
url: '/:any',
handler: async (req, rep) => {
const {
requestTimeout,
size = 1024 * 1024,
speed,
bodyTimeouts = [],
status,
} = req.body
if (status) {
rep.code(status)
}
if (requestTimeout) {
await delay(requestTimeout)
}
console.log('ready to send')
// return
return new TimeoutStream(size, speed, requestTimeout, bodyTimeouts)
},
})
let port
const start = async () => {
await server.listen(50000)
port = server.server.address().port
console.log('request start')
const res = await fetch(`http://localhost:${port}/asd`, {
method: 'POST',
body: JSON.stringify({
size: 512 * 1024,
requestTimeout: 3000,
speed: 128 * 1024,
bodyTimeouts: [
// {time: 3000, after: 0},
// {time: 1000, after: 256 * 1024},
],
}),
headers: {'content-type': 'application/json'},
})
console.log(res.headers)
if (res?.body) {
/** @type ReadableStreamDefaultReader<Uint8Array> */
let reader
let bytesTransferred
const measureStream = new ReadableStream({
type: 'bytes',
start() {
console.log('original stream locked')
reader = res.body.getReader()
},
async pull(controller) {
if (bytesTransferred === undefined) {
console.log('measure: download start')
bytesTransferred = 0
}
const {done, value} = await reader.read()
if (done) {
console.log(`measure: download finish, ${bytesTransferred} bytes`)
return controller.close()
}
process.stdout.write('M')
bytesTransferred += value.byteLength
controller.enqueue(value)
},
cancel(reason) {
reader.cancel(reason)
},
})
// const modifiedRes = new Proxy(res, {
// get(target, prop, receiver) {
// if (prop === 'body') {
// return measureStream
// }
// return Reflect.get(target, prop, receiver)
// },
// })
const modifiedRes = new Response(measureStream, res)
const clonedRes = modifiedRes.clone()
console.log(await clonedRes.arrayBuffer())
console.log(modifiedRes.headers)
console.log(modifiedRes.statusText)
console.log(modifiedRes.status)
console.log(await modifiedRes.arrayBuffer())
// console.log('actual download: start')
// for await (const chunk of measureStream) {
// process.stdout.write('D')
// }
// console.log('actual download: finish')
}
// const transformed = res.body?.pipeThrough(
// new TransformStream({
// transform(chunk, controller) {
// console.log(`Processed ${chunk.byteLength} bytes`)
// controller.enqueue(chunk)
// },
// flush(controller) {
// console.log('flushed')
// },
// })
// )
// const [measurable, downloadable] = res.body?.tee()
// setTimeout(async () => {
// console.log('Measure start')
// for await (const chunk of res.body) {
// process.stdout.write('M')
// // console.log(`Measurable ${chunk.byteLength}`)
// }
// console.log('Measure finish')
// }, 0)
// setTimeout(async () => {
// console.log('Download start')
// for await (const chunk of downloadable) {
// process.stdout.write('D')
// // console.log(`Downloadable ${chunk.byteLength}`)
// }
// console.log('Download finish')
// }, 60000)
console.log(port)
await server.close()
}
start()