@@ -2,24 +2,65 @@ import nacl from 'tweetnacl'
22import naclUutil from 'tweetnacl-util'
33import { Base64 } from 'js-base64'
44
5- console . log ( "ass" + nacl + naclUutil + Base64 )
6-
7-
5+ console . log ( "ass " + nacl + naclUutil + Base64 )
6+ // https://stackoverflow.com/questions/51562781/service-worker-fetch-event-is-not-firing
7+ self . addEventListener ( 'activate' , function ( event ) {
8+ console . log ( 'Claiming control' )
9+ return self . clients . claim ( )
10+ } )
811self . addEventListener ( "fetch" , ( event ) => {
12+
913 // Regular requests not related to Web Share Target.
10- if ( event . request . method !== "POST" || new URL ( event . request . url ) . pathname != "/assets/ share-target" ) {
14+ if ( event . request . method !== "POST" || new URL ( event . request . url ) . pathname != "/share-target/ " ) {
1115 event . respondWith ( fetch ( event . request ) )
1216 return
1317 }
1418
1519 // Requests related to Web Share Target.
1620 event . respondWith (
1721 ( async ( ) => {
22+
1823 const formData = await event . request . formData ( )
24+ const nonce = nacl . randomBytes ( nacl . secretbox . nonceLength )
25+ const key = nacl . randomBytes ( nacl . secretbox . keyLength )
1926 // encrypt note or image
2027 // send to fetch api
28+ const imgBase64 = null // todo - base64
29+ const f = await fetch ( '/api/save-note' , {
30+ method : 'POST' ,
31+ headers : {
32+ 'Content-Type' : 'application/json'
33+ } ,
34+ body : JSON . stringify ( {
35+ encryptedText : naclUutil . encodeBase64 (
36+ nacl . secretbox (
37+ naclUutil . decodeUTF8 ( formData . get ( 'text' ) ) ,
38+ nonce ,
39+ key
40+ )
41+ ) ,
42+ encryptedImg : imgBase64 != null ? naclUutil . encodeBase64 (
43+ nacl . secretbox (
44+ naclUutil . decodeUTF8 ( imgBase64 ) ,
45+ nonce ,
46+ key
47+ )
48+ ) : null ,
49+ // code is hard
50+ expiresHours : "4" ,
51+ expiresViews : "1"
52+ } )
53+ } )
54+ const { id } = await f . json ( )
55+
56+ const hash = Base64 . encode (
57+ JSON . stringify ( {
58+ nonce : naclUutil . encodeBase64 ( nonce ) ,
59+ key : naclUutil . encodeBase64 ( key )
60+ } )
61+ )
2162 // respond with saved note page
22- return Response . redirect ( ' /noteSaved#todoshitinthehash' , 303 )
63+ return Response . redirect ( ` /noteSaved?id= ${ id } # ${ hash } ` , 303 )
2364 } ) ( ) ,
2465 )
2566} )
0 commit comments