msb-file 0.0.1 → 0.0.2

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/lib/mitm.html DELETED
@@ -1,167 +0,0 @@
1
- <!--
2
- mitm.html is the lite "man in the middle"
3
-
4
- This is only meant to signal the opener's messageChannel to
5
- the service worker - when that is done this mitm can be closed
6
- but it's better to keep it alive since this also stops the sw
7
- from restarting
8
-
9
- The service worker is capable of intercepting all request and fork their
10
- own "fake" response - wish we are going to craft
11
- when the worker then receives a stream then the worker will tell the opener
12
- to open up a link that will start the download
13
- -->
14
- <script>
15
- // This will prevent the sw from restarting
16
- let keepAlive = () => {
17
- keepAlive = () => {}
18
- var ping = location.href.substr(0, location.href.lastIndexOf('/')) + '/ping'
19
- var interval = setInterval(() => {
20
- if (sw) {
21
- sw.postMessage('ping')
22
- } else {
23
- fetch(ping).then(res => res.text(!res.ok && clearInterval(interval)))
24
- }
25
- }, 10000)
26
- }
27
-
28
- // message event is the first thing we need to setup a listner for
29
- // don't want the opener to do a random timeout - instead they can listen for
30
- // the ready event
31
- // but since we need to wait for the Service Worker registration, we store the
32
- // message for later
33
- let messages = []
34
- window.onmessage = evt => messages.push(evt)
35
-
36
- let sw = null
37
- let scope = ''
38
-
39
- function registerWorker() {
40
- return navigator.serviceWorker.getRegistration('./').then(swReg => {
41
- return swReg || navigator.serviceWorker.register('sw.js', { scope: './' })
42
- }).then(swReg => {
43
- const swRegTmp = swReg.installing || swReg.waiting
44
-
45
- scope = swReg.scope
46
-
47
- return (sw = swReg.active) || new Promise(resolve => {
48
- swRegTmp.addEventListener('statechange', fn = () => {
49
- if (swRegTmp.state === 'activated') {
50
- swRegTmp.removeEventListener('statechange', fn)
51
- sw = swReg.active
52
- resolve()
53
- }
54
- })
55
- })
56
- })
57
- }
58
-
59
- // Now that we have the Service Worker registered we can process messages
60
- function onMessage (event) {
61
- let { data, ports, origin } = event
62
-
63
- // It's important to have a messageChannel, don't want to interfere
64
- // with other simultaneous downloads
65
- if (!ports || !ports.length) {
66
- throw new TypeError("[StreamSaver] You didn't send a messageChannel")
67
- }
68
-
69
- if (typeof data !== 'object') {
70
- throw new TypeError("[StreamSaver] You didn't send a object")
71
- }
72
-
73
- // the default public service worker for StreamSaver is shared among others.
74
- // so all download links needs to be prefixed to avoid any other conflict
75
- data.origin = origin
76
-
77
- // if we ever (in some feature versoin of streamsaver) would like to
78
- // redirect back to the page of who initiated a http request
79
- data.referrer = data.referrer || document.referrer || origin
80
-
81
- // pass along version for possible backwards compatibility in sw.js
82
- data.streamSaverVersion = new URLSearchParams(location.search).get('version')
83
-
84
- if (data.streamSaverVersion === '1.2.0') {
85
- console.warn('[StreamSaver] please update streamsaver')
86
- }
87
-
88
- /** @since v2.0.0 */
89
- if (!data.headers) {
90
- console.warn("[StreamSaver] pass `data.headers` that you would like to pass along to the service worker\nit should be a 2D array or a key/val object that fetch's Headers api accepts")
91
- } else {
92
- // test if it's correct
93
- // should thorw a typeError if not
94
- new Headers(data.headers)
95
- }
96
-
97
- /** @since v2.0.0 */
98
- if (typeof data.filename === 'string') {
99
- console.warn("[StreamSaver] You shouldn't send `data.filename` anymore. It should be included in the Content-Disposition header option")
100
- // Do what File constructor do with fileNames
101
- data.filename = data.filename.replace(/\//g, ':')
102
- }
103
-
104
- /** @since v2.0.0 */
105
- if (data.size) {
106
- console.warn("[StreamSaver] You shouldn't send `data.size` anymore. It should be included in the content-length header option")
107
- }
108
-
109
- /** @since v2.0.0 */
110
- if (data.readableStream) {
111
- console.warn("[StreamSaver] You should send the readableStream in the messageChannel, not throught mitm")
112
- }
113
-
114
- /** @since v2.0.0 */
115
- if (!data.pathname) {
116
- console.warn("[StreamSaver] Please send `data.pathname` (eg: /pictures/summer.jpg)")
117
- data.pathname = Math.random().toString().slice(-6) + '/' + data.filename
118
- }
119
-
120
- // remove all leading slashes
121
- data.pathname = data.pathname.replace(/^\/+/g, '')
122
-
123
- // remove protocol
124
- let org = origin.replace(/(^\w+:|^)\/\//, '')
125
-
126
- // set the absolute pathname to the download url.
127
- data.url = new URL(`${scope + org}/${data.pathname}`).toString()
128
-
129
- if (!data.url.startsWith(`${scope + org}/`)) {
130
- throw new TypeError('[StreamSaver] bad `data.pathname`')
131
- }
132
-
133
- // This sends the message data as well as transferring
134
- // messageChannel.port2 to the service worker. The service worker can
135
- // then use the transferred port to reply via postMessage(), which
136
- // will in turn trigger the onmessage handler on messageChannel.port1.
137
-
138
- const transferable = data.readableStream
139
- ? [ ports[0], data.readableStream ]
140
- : [ ports[0] ]
141
-
142
- if (!(data.readableStream || data.transferringReadable)) {
143
- keepAlive()
144
- }
145
-
146
- return sw.postMessage(data, transferable)
147
- }
148
-
149
- if (window.opener) {
150
- // The opener can't listen to onload event, so we need to help em out!
151
- // (telling them that we are ready to accept postMessage's)
152
- window.opener.postMessage('StreamSaver::loadedPopup', '*')
153
- }
154
-
155
- if (navigator.serviceWorker) {
156
- registerWorker().then(() => {
157
- window.onmessage = onMessage
158
- messages.forEach(window.onmessage)
159
- })
160
- }
161
-
162
- // FF v102 just started to supports transferable streams, but still needs to ping sw.js
163
- // even tough the service worker dose not have to do any kind of work and listen to any
164
- // messages... #305
165
- keepAlive()
166
-
167
- </script>
package/lib/sw.js DELETED
@@ -1,130 +0,0 @@
1
- /* global self ReadableStream Response */
2
-
3
- self.addEventListener('install', () => {
4
- self.skipWaiting()
5
- })
6
-
7
- self.addEventListener('activate', event => {
8
- event.waitUntil(self.clients.claim())
9
- })
10
-
11
- const map = new Map()
12
-
13
- // This should be called once per download
14
- // Each event has a dataChannel that the data will be piped through
15
- self.onmessage = event => {
16
- // We send a heartbeat every x second to keep the
17
- // service worker alive if a transferable stream is not sent
18
- if (event.data === 'ping') {
19
- return
20
- }
21
-
22
- const data = event.data
23
- const downloadUrl = data.url || self.registration.scope + Math.random() + '/' + (typeof data === 'string' ? data : data.filename)
24
- const port = event.ports[0]
25
- const metadata = new Array(3) // [stream, data, port]
26
-
27
- metadata[1] = data
28
- metadata[2] = port
29
-
30
- // Note to self:
31
- // old streamsaver v1.2.0 might still use `readableStream`...
32
- // but v2.0.0 will always transfer the stream through MessageChannel #94
33
- if (event.data.readableStream) {
34
- metadata[0] = event.data.readableStream
35
- } else if (event.data.transferringReadable) {
36
- port.onmessage = evt => {
37
- port.onmessage = null
38
- metadata[0] = evt.data.readableStream
39
- }
40
- } else {
41
- metadata[0] = createStream(port)
42
- }
43
-
44
- map.set(downloadUrl, metadata)
45
- port.postMessage({ download: downloadUrl })
46
- }
47
-
48
- function createStream (port) {
49
- // ReadableStream is only supported by chrome 52
50
- return new ReadableStream({
51
- start (controller) {
52
- // When we receive data on the messageChannel, we write
53
- port.onmessage = ({ data }) => {
54
- if (data === 'end') {
55
- return controller.close()
56
- }
57
-
58
- if (data === 'abort') {
59
- controller.error('Aborted the download')
60
- return
61
- }
62
-
63
- controller.enqueue(data)
64
- }
65
- },
66
- cancel (reason) {
67
- console.log('user aborted', reason)
68
- port.postMessage({ abort: true })
69
- }
70
- })
71
- }
72
-
73
- self.onfetch = event => {
74
- const url = event.request.url
75
-
76
- // this only works for Firefox
77
- if (url.endsWith('/ping')) {
78
- return event.respondWith(new Response('pong'))
79
- }
80
-
81
- const hijacke = map.get(url)
82
-
83
- if (!hijacke) return null
84
-
85
- const [ stream, data, port ] = hijacke
86
-
87
- map.delete(url)
88
-
89
- // Not comfortable letting any user control all headers
90
- // so we only copy over the length & disposition
91
- const responseHeaders = new Headers({
92
- 'Content-Type': 'application/octet-stream; charset=utf-8',
93
-
94
- // To be on the safe side, The link can be opened in a iframe.
95
- // but octet-stream should stop it.
96
- 'Content-Security-Policy': "default-src 'none'",
97
- 'X-Content-Security-Policy': "default-src 'none'",
98
- 'X-WebKit-CSP': "default-src 'none'",
99
- 'X-XSS-Protection': '1; mode=block',
100
- 'Cross-Origin-Embedder-Policy': 'require-corp'
101
- })
102
-
103
- let headers = new Headers(data.headers || {})
104
-
105
- if (headers.has('Content-Length')) {
106
- responseHeaders.set('Content-Length', headers.get('Content-Length'))
107
- }
108
-
109
- if (headers.has('Content-Disposition')) {
110
- responseHeaders.set('Content-Disposition', headers.get('Content-Disposition'))
111
- }
112
-
113
- // data, data.filename and size should not be used anymore
114
- if (data.size) {
115
- console.warn('Depricated')
116
- responseHeaders.set('Content-Length', data.size)
117
- }
118
-
119
- let fileName = typeof data === 'string' ? data : data.filename
120
- if (fileName) {
121
- console.warn('Depricated')
122
- // Make filename RFC5987 compatible
123
- fileName = encodeURIComponent(fileName).replace(/['()]/g, escape).replace(/\*/g, '%2A')
124
- responseHeaders.set('Content-Disposition', "attachment; filename*=UTF-8''" + fileName)
125
- }
126
-
127
- event.respondWith(new Response(stream, { headers: responseHeaders }))
128
-
129
- port.postMessage({ debug: 'Download started' })
130
- }