ngx-rendering-service-lib 0.0.20 → 0.0.21
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/assets/edu-service-worker.js +86 -99
- package/package.json +1 -1
|
@@ -5,8 +5,10 @@ const H5P_PATH = "public/h5p"
|
|
|
5
5
|
const ASSET_PATH = "public/asset"
|
|
6
6
|
const DB_NAME = "Edu-Sharing-Rendering"
|
|
7
7
|
const DB_STORE = "SessionIds"
|
|
8
|
+
/** @type {IDBDatabase} */
|
|
9
|
+
let db
|
|
8
10
|
|
|
9
|
-
self.addEventListener('install', function (event) {
|
|
11
|
+
self.addEventListener('install', async function (event) {
|
|
10
12
|
// Skip the 'waiting' lifecycle phase, to go directly from 'installed' to 'activated', even if
|
|
11
13
|
// there are still previous incarnations of this service worker registration active.
|
|
12
14
|
console.log('install')
|
|
@@ -28,17 +30,17 @@ self.addEventListener('message', (event) => {
|
|
|
28
30
|
}
|
|
29
31
|
});
|
|
30
32
|
|
|
31
|
-
self.addEventListener('fetch', (event) => {
|
|
33
|
+
self.addEventListener('fetch', async (event) => {
|
|
34
|
+
await getDb().catch(error => {
|
|
35
|
+
console.error(error)
|
|
36
|
+
event.respondWith(handleStandardRequest(request))
|
|
37
|
+
})
|
|
32
38
|
const request = event.request
|
|
33
39
|
const url = request.url
|
|
34
40
|
if (url.includes(RENDER_DATA_PATH)) {
|
|
35
|
-
event.respondWith(
|
|
36
|
-
|
|
37
|
-
)
|
|
38
|
-
} else if(url.includes(JOB_INFO_PATH) || url.includes(H5P_PATH) || url.includes(ASSET_PATH)) {
|
|
39
|
-
event.respondWith(
|
|
40
|
-
handleSessionRequest(request)
|
|
41
|
-
)
|
|
41
|
+
event.respondWith(handleRenderDataRequest(request))
|
|
42
|
+
} else if (url.includes(JOB_INFO_PATH) || url.includes(H5P_PATH) || url.includes(ASSET_PATH)) {
|
|
43
|
+
event.respondWith(handleSessionRequest(request))
|
|
42
44
|
} else {
|
|
43
45
|
event.respondWith(handleStandardRequest(request))
|
|
44
46
|
}
|
|
@@ -51,20 +53,14 @@ self.addEventListener('fetch', (event) => {
|
|
|
51
53
|
* @returns {Promise<Response>}
|
|
52
54
|
*/
|
|
53
55
|
const handleSessionRequest = async (request) => {
|
|
54
|
-
const
|
|
55
|
-
if (dbRequest === undefined) {
|
|
56
|
-
return handleStandardRequest(request)
|
|
57
|
-
}
|
|
58
|
-
const sessionId = await retrieveSessionIdFromDb(dbRequest)
|
|
56
|
+
const sessionId = await retrieveSessionIdFromDb()
|
|
59
57
|
if (sessionId === "") {
|
|
60
58
|
return handleStandardRequest(request)
|
|
61
59
|
}
|
|
62
60
|
const modifiedHeaders = new Headers(request.headers)
|
|
63
61
|
modifiedHeaders.set(SESSION_HEADER_NAME, sessionId)
|
|
64
62
|
const modifiedRequest = new Request(request, {
|
|
65
|
-
mode: 'cors',
|
|
66
|
-
credentials: 'include',
|
|
67
|
-
headers: modifiedHeaders,
|
|
63
|
+
mode: 'cors', credentials: 'include', headers: modifiedHeaders,
|
|
68
64
|
});
|
|
69
65
|
return await fetch(modifiedRequest)
|
|
70
66
|
}
|
|
@@ -77,14 +73,10 @@ const handleSessionRequest = async (request) => {
|
|
|
77
73
|
*/
|
|
78
74
|
const handleRenderDataRequest = async (request) => {
|
|
79
75
|
const response = await fetch(request)
|
|
80
|
-
|
|
81
|
-
const
|
|
82
|
-
if (
|
|
83
|
-
|
|
84
|
-
const newSessionId = response.headers.get(SESSION_HEADER_NAME)
|
|
85
|
-
if (newSessionId !== null && newSessionId !== "") {
|
|
86
|
-
await storeSessionIdToDb(dbRequestWrite, newSessionId)
|
|
87
|
-
}
|
|
76
|
+
const existingSessionId = await retrieveSessionIdFromDb()
|
|
77
|
+
const newSessionId = response.headers.get(SESSION_HEADER_NAME)
|
|
78
|
+
if (newSessionId !== null && newSessionId !== "" && newSessionId !== existingSessionId) {
|
|
79
|
+
await storeSessionIdToDb(newSessionId)
|
|
88
80
|
}
|
|
89
81
|
return response
|
|
90
82
|
}
|
|
@@ -100,60 +92,59 @@ const handleStandardRequest = async (request) => {
|
|
|
100
92
|
}
|
|
101
93
|
|
|
102
94
|
/**
|
|
103
|
-
* Function
|
|
104
|
-
*
|
|
105
|
-
* Prepares an IDBOpenDBRequest and assigns callbacks for onupgradeneeded and error events
|
|
106
|
-
* In order to apply changes to the db scheme, increment the version and modify the onupgradeneeded callback
|
|
107
|
-
* function according to your needs.
|
|
95
|
+
* Function getDb
|
|
108
96
|
*
|
|
109
|
-
* @
|
|
97
|
+
* @returns {Promise<String>}
|
|
110
98
|
*/
|
|
111
|
-
const
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
99
|
+
const getDb = () => {
|
|
100
|
+
return new Promise((resolve, reject) => {
|
|
101
|
+
if (db !== undefined) {
|
|
102
|
+
resolve("success")
|
|
103
|
+
}
|
|
104
|
+
if (!indexedDB) {
|
|
105
|
+
reject(new Error("IndexedDB is not supported by current browser. Header auth with Edu-Sharing Rendering Service is not available"))
|
|
106
|
+
}
|
|
107
|
+
const request = indexedDB.open(DB_NAME, 1)
|
|
108
|
+
request.error = (event) => {
|
|
109
|
+
console.error(event)
|
|
110
|
+
reject(new Error("Error opening indexedDB"))
|
|
111
|
+
}
|
|
112
|
+
request.onupgradeneeded = () => {
|
|
113
|
+
const newDb = request.result
|
|
114
|
+
newDb.createObjectStore(DB_STORE, {keyPath: "id"})
|
|
115
|
+
}
|
|
116
|
+
request.onsuccess = () => {
|
|
117
|
+
db = request.result
|
|
118
|
+
resolve("success")
|
|
119
|
+
}
|
|
120
|
+
})
|
|
126
121
|
}
|
|
127
122
|
|
|
128
123
|
/**
|
|
129
124
|
* Function storeSessionIdToDb
|
|
130
125
|
*
|
|
131
|
-
* @param {IDBOpenDBRequest} dbRequest
|
|
132
126
|
* @param {string} sessionId
|
|
133
127
|
*/
|
|
134
|
-
const storeSessionIdToDb = (
|
|
128
|
+
const storeSessionIdToDb = (sessionId) => {
|
|
135
129
|
return new Promise((resolve, reject) => {
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
transaction
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
transaction.
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
transaction.
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
console.error(ev)
|
|
155
|
-
reject(new Error("Transaction aborted"))
|
|
156
|
-
}
|
|
130
|
+
const transaction = db.transaction(DB_STORE, "readwrite")
|
|
131
|
+
const store = transaction.objectStore(DB_STORE)
|
|
132
|
+
store.put({id: 1, sessionId: sessionId})
|
|
133
|
+
transaction.oncomplete = () => {
|
|
134
|
+
db.close()
|
|
135
|
+
resolve("Indexed DB transaction completed")
|
|
136
|
+
}
|
|
137
|
+
transaction.onerror = (event) => {
|
|
138
|
+
db.close()
|
|
139
|
+
console.error("DB transaction failed. Cannot store session id.")
|
|
140
|
+
console.error(event)
|
|
141
|
+
reject(new Error("Transaction failed"))
|
|
142
|
+
}
|
|
143
|
+
transaction.onabort = (ev) => {
|
|
144
|
+
db.close()
|
|
145
|
+
console.error("DB transaction aborted. Cannot store session id.")
|
|
146
|
+
console.error(ev)
|
|
147
|
+
reject(new Error("Transaction aborted"))
|
|
157
148
|
}
|
|
158
149
|
})
|
|
159
150
|
}
|
|
@@ -164,39 +155,35 @@ const storeSessionIdToDb = (dbRequest, sessionId) => {
|
|
|
164
155
|
* Gets the stored session id from the database. Returns a promise resolving to the result on success
|
|
165
156
|
* and to an empty string on failure
|
|
166
157
|
*
|
|
167
|
-
* @param {IDBOpenDBRequest} dbRequest
|
|
168
158
|
* @return {Promise<String>}
|
|
169
159
|
*/
|
|
170
|
-
const retrieveSessionIdFromDb = (
|
|
160
|
+
const retrieveSessionIdFromDb = () => {
|
|
171
161
|
return new Promise((resolve, reject) => {
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
transaction.
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
transaction.
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
console.error(ev)
|
|
198
|
-
reject(new Error("Transaction aborted"))
|
|
199
|
-
}
|
|
162
|
+
let result = ""
|
|
163
|
+
const transaction = db.transaction(DB_STORE, "readonly")
|
|
164
|
+
const store = transaction.objectStore(DB_STORE)
|
|
165
|
+
const idQuery = store.get(1)
|
|
166
|
+
idQuery.onsuccess = () => {
|
|
167
|
+
result = idQuery.result !== undefined ? idQuery.result.sessionId : ""
|
|
168
|
+
}
|
|
169
|
+
idQuery.onerror = () => {
|
|
170
|
+
console.error("Query by id failed")
|
|
171
|
+
}
|
|
172
|
+
transaction.oncomplete = () => {
|
|
173
|
+
db.close()
|
|
174
|
+
resolve(result)
|
|
175
|
+
}
|
|
176
|
+
transaction.onerror = (event) => {
|
|
177
|
+
db.close()
|
|
178
|
+
console.error("DB transaction failed. Cannot retrieve session id.")
|
|
179
|
+
console.error(event)
|
|
180
|
+
reject(new Error("Transaction failed"))
|
|
181
|
+
}
|
|
182
|
+
transaction.onabort = (ev) => {
|
|
183
|
+
db.close()
|
|
184
|
+
console.error("DB transaction aborted. Cannot retrieve session id.")
|
|
185
|
+
console.error(ev)
|
|
186
|
+
reject(new Error("Transaction aborted"))
|
|
200
187
|
}
|
|
201
188
|
})
|
|
202
189
|
}
|