|
| 1 | +import registerDebug from 'debug' |
| 2 | +import { Source } from '../component' |
| 3 | +import { Readable } from 'stream' |
| 4 | +import { MessageType } from '../message' |
| 5 | + |
| 6 | +const debug = registerDebug('msl:http-source') |
| 7 | + |
| 8 | +interface Headers { |
| 9 | + [key: string]: string |
| 10 | +} |
| 11 | + |
| 12 | +export interface HttpConfig { |
| 13 | + uri: string |
| 14 | + headers?: Headers |
| 15 | +} |
| 16 | + |
| 17 | +export class HttpSource extends Source { |
| 18 | + public uri: string |
| 19 | + public headers?: Headers |
| 20 | + public length?: number |
| 21 | + |
| 22 | + private _reader?: ReadableStreamDefaultReader<Uint8Array> |
| 23 | + |
| 24 | + /** |
| 25 | + * Create an HTTP component. |
| 26 | + * |
| 27 | + * The constructor sets a single readable stream from a fetch. |
| 28 | + */ |
| 29 | + constructor(config: HttpConfig) { |
| 30 | + const { uri, headers } = config |
| 31 | + |
| 32 | + /** |
| 33 | + * Set up an incoming stream and attach it to the socket. |
| 34 | + */ |
| 35 | + const incoming = new Readable({ |
| 36 | + objectMode: true, |
| 37 | + read: function () { |
| 38 | + // |
| 39 | + }, |
| 40 | + }) |
| 41 | + |
| 42 | + // When an error is sent on the incoming stream, close the socket. |
| 43 | + incoming.on('error', (e) => { |
| 44 | + console.warn('closing socket due to incoming error', e) |
| 45 | + this._reader && this._reader.cancel() |
| 46 | + }) |
| 47 | + |
| 48 | + /** |
| 49 | + * initialize the component. |
| 50 | + */ |
| 51 | + super(incoming) |
| 52 | + |
| 53 | + // When a read is requested, continue to pull data |
| 54 | + incoming._read = () => { |
| 55 | + this._pull() |
| 56 | + } |
| 57 | + |
| 58 | + this.uri = uri |
| 59 | + this.headers = headers |
| 60 | + } |
| 61 | + |
| 62 | + play(): void { |
| 63 | + if (this.uri === undefined) { |
| 64 | + throw new Error('cannot start playing when there is no URI') |
| 65 | + } |
| 66 | + |
| 67 | + this.length = 0 |
| 68 | + fetch(this.uri, { headers: this.headers }) |
| 69 | + .then((rsp) => { |
| 70 | + if (rsp.body === null) { |
| 71 | + throw new Error('empty response body') |
| 72 | + } |
| 73 | + |
| 74 | + this._reader = rsp.body.getReader() |
| 75 | + this._pull() |
| 76 | + }) |
| 77 | + .catch((err) => { |
| 78 | + throw new Error(err) |
| 79 | + }) |
| 80 | + } |
| 81 | + |
| 82 | + _pull(): void { |
| 83 | + if (this._reader === undefined) { |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | + this._reader.read().then(({ done, value }) => { |
| 88 | + if (done) { |
| 89 | + debug('fetch completed, total downloaded: ', this.length, ' bytes') |
| 90 | + this.incoming.push(null) |
| 91 | + return |
| 92 | + } |
| 93 | + if (value === undefined) { |
| 94 | + throw new Error('expected value to be defined') |
| 95 | + } |
| 96 | + if (this.length === undefined) { |
| 97 | + throw new Error('expected length to be defined') |
| 98 | + } |
| 99 | + this.length += value.length |
| 100 | + const buffer = Buffer.from(value) |
| 101 | + if (!this.incoming.push({ data: buffer, type: MessageType.RAW })) { |
| 102 | + // Something happened down stream that it is no longer processing the |
| 103 | + // incoming data, and the stream buffer got full. |
| 104 | + // This could be because we are downloading too much data at once, |
| 105 | + // or because the downstream is frozen. The latter is most likely |
| 106 | + // when dealing with a live stream (as in that case we would expect |
| 107 | + // downstream to be able to handle the data). |
| 108 | + debug('downstream back pressure: pausing read') |
| 109 | + } else { |
| 110 | + // It's ok to read more data |
| 111 | + this._pull() |
| 112 | + } |
| 113 | + }) |
| 114 | + } |
| 115 | +} |
0 commit comments