This repository has been archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 156
/
index.js
83 lines (64 loc) · 2.41 KB
/
index.js
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
import { Buffer } from 'buffer'
globalThis.Buffer = Buffer
import { create } from 'ipfs-core'
import VideoStream from 'videostream'
import toStream from 'it-to-stream'
import {
dragDrop,
statusMessages,
createVideoElement,
log
} from './utils.js'
const App = async () => {
// DOM
const DOM = {
cidInput: () => document.getElementById('cid'),
goButton: () => document.getElementById('gobutton'),
file: () => document.getElementById('file')
}
log('IPFS: Initializing')
const videoElement = createVideoElement()
const ipfs = await create({ repo: 'ipfs-' + Math.random() })
// Allow adding files to IPFS via drag and drop
dragDrop(ipfs, log)
log('IPFS: Ready')
log('IPFS: Drop an .mp4 file into this window to add a file')
log('IPFS: Then press the "Go!" button to start playing a video')
DOM.cidInput().disabled = false
DOM.goButton().disabled = false
let stream
DOM.goButton().addEventListener('click', (event) => {
event.preventDefault()
log(`IPFS: Playing ${DOM.cidInput().value.trim()}`)
// Set up the video stream an attach it to our <video> element
const videoStream = new VideoStream({
createReadStream: function createReadStream (opts) {
const start = opts.start
// The videostream library does not always pass an end byte but when
// it does, it wants bytes between start & end inclusive.
// catReadableStream returns the bytes exclusive so increment the end
// byte if it's been requested
const end = opts.end ? start + opts.end + 1 : undefined
log(`Stream: Asked for data starting at byte ${start} and ending at byte ${end}`)
// If we've streamed before, clean up the existing stream
if (stream && stream.destroy) {
stream.destroy()
}
// This stream will contain the requested bytes
stream = toStream.readable(ipfs.cat(DOM.cidInput().value.trim(), {
offset: start,
length: end && end - start
}))
// Log error messages
stream.on('error', (error) => log(error))
if (start === 0) {
// Show the user some messages while we wait for the data stream to start
statusMessages(stream, log)
}
return stream
}
}, videoElement)
videoElement.addEventListener('error', () => log(videoStream.detailedError))
})
}
document.addEventListener('DOMContentLoaded', async () => App())