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