ngx-rendering-service-lib 0.0.15 → 0.0.17
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 +138 -11
- package/package.json +1 -1
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
let sessionId = ""
|
|
2
1
|
const SESSION_HEADER_NAME = "Authentication-Info"
|
|
3
2
|
const RENDER_DATA_PATH = "public/renderdata"
|
|
4
3
|
const JOB_INFO_PATH = "public/job"
|
|
5
4
|
const H5P_PATH = "public/h5p"
|
|
6
5
|
const ASSET_PATH = "public/asset"
|
|
6
|
+
const DB_NAME = "Edu-Sharing-Rendering"
|
|
7
|
+
const DB_STORE = "SessionIds"
|
|
7
8
|
|
|
9
|
+
/** @type {FetchEvent} */
|
|
8
10
|
self.addEventListener('install', function (event) {
|
|
9
11
|
// Skip the 'waiting' lifecycle phase, to go directly from 'installed' to 'activated', even if
|
|
10
12
|
// there are still previous incarnations of this service worker registration active.
|
|
@@ -43,8 +45,21 @@ self.addEventListener('fetch', (event) => {
|
|
|
43
45
|
}
|
|
44
46
|
});
|
|
45
47
|
|
|
48
|
+
/**
|
|
49
|
+
* Function handleSessionRequest
|
|
50
|
+
*
|
|
51
|
+
* @param {Request} request
|
|
52
|
+
* @returns {Promise<Response>}
|
|
53
|
+
*/
|
|
46
54
|
const handleSessionRequest = async (request) => {
|
|
47
|
-
|
|
55
|
+
const dbRequest = await buildDbRequest()
|
|
56
|
+
if (dbRequest === undefined) {
|
|
57
|
+
return handleStandardRequest(request)
|
|
58
|
+
}
|
|
59
|
+
const sessionId = await retrieveSessionIdFromDb(dbRequest)
|
|
60
|
+
if (sessionId === "") {
|
|
61
|
+
return handleStandardRequest(request)
|
|
62
|
+
}
|
|
48
63
|
const modifiedHeaders = new Headers(request.headers)
|
|
49
64
|
modifiedHeaders.set(SESSION_HEADER_NAME, sessionId)
|
|
50
65
|
const modifiedRequest = new Request(request, {
|
|
@@ -55,21 +70,133 @@ const handleSessionRequest = async (request) => {
|
|
|
55
70
|
return await fetch(modifiedRequest)
|
|
56
71
|
}
|
|
57
72
|
|
|
73
|
+
/**
|
|
74
|
+
* Function handleRenderDataRequest
|
|
75
|
+
*
|
|
76
|
+
* @param {Request} request
|
|
77
|
+
* @returns {Promise<Response>}
|
|
78
|
+
*/
|
|
58
79
|
const handleRenderDataRequest = async (request) => {
|
|
59
|
-
console.log("handleRenderDataRequest: " + request.url)
|
|
60
80
|
const response = await fetch(request)
|
|
61
|
-
const
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
81
|
+
const dbRequest = buildDbRequest()
|
|
82
|
+
if (dbRequest !== undefined) {
|
|
83
|
+
const existingSessionId = await retrieveSessionIdFromDb(dbRequest)
|
|
84
|
+
const newSessionId = response.headers.get(SESSION_HEADER_NAME)
|
|
85
|
+
if (newSessionId !== null && newSessionId !== "" && newSessionId !== existingSessionId) {
|
|
86
|
+
await storeSessionIdToDb(dbRequest, newSessionId)
|
|
87
|
+
}
|
|
66
88
|
}
|
|
67
|
-
const newSessionIdAllLowerCase = response.headers.get(SESSION_HEADER_NAME.toLowerCase())
|
|
68
|
-
console.log("All lower case: " + newSessionIdAllLowerCase)
|
|
69
89
|
return response
|
|
70
90
|
}
|
|
71
91
|
|
|
92
|
+
/**
|
|
93
|
+
* Function handleStandardRequest
|
|
94
|
+
*
|
|
95
|
+
* @param {Request} request
|
|
96
|
+
* @returns {Promise<Response>}
|
|
97
|
+
*/
|
|
72
98
|
const handleStandardRequest = async (request) => {
|
|
73
|
-
console.log("handleStandardRequest: " + request.url)
|
|
74
99
|
return await fetch(request)
|
|
75
100
|
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Function buildDbRequest
|
|
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.
|
|
108
|
+
*
|
|
109
|
+
* @return {IDBOpenDBRequest | undefined}
|
|
110
|
+
*/
|
|
111
|
+
const buildDbRequest = () => {
|
|
112
|
+
if (!indexedDB) {
|
|
113
|
+
console.error("IndexedDB is not supported by current browser. Header auth with Edu-Sharing Rendering Service is not available")
|
|
114
|
+
return
|
|
115
|
+
}
|
|
116
|
+
const request = indexedDB.open(DB_NAME, 1)
|
|
117
|
+
request.error = (event) => {
|
|
118
|
+
console.error("Error opening indexedDB")
|
|
119
|
+
console.error(event)
|
|
120
|
+
}
|
|
121
|
+
request.onupgradeneeded = () => {
|
|
122
|
+
const db = request.result
|
|
123
|
+
db.createObjectStore(DB_STORE, { keyPath: "id"})
|
|
124
|
+
}
|
|
125
|
+
return request
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Function storeSessionIdToDb
|
|
130
|
+
*
|
|
131
|
+
* @param {IDBOpenDBRequest} dbRequest
|
|
132
|
+
* @param {string} sessionId
|
|
133
|
+
*/
|
|
134
|
+
const storeSessionIdToDb = (dbRequest, sessionId) => {
|
|
135
|
+
return new Promise((resolve, reject) => {
|
|
136
|
+
dbRequest.onsuccess = () => {
|
|
137
|
+
const db = dbRequest.result
|
|
138
|
+
const transaction = db.transaction(DB_STORE, "readwrite")
|
|
139
|
+
const store = transaction.objectStore(DB_STORE)
|
|
140
|
+
store.put({ id: 1, sessionId: sessionId})
|
|
141
|
+
transaction.oncomplete = () => {
|
|
142
|
+
db.close()
|
|
143
|
+
resolve("Indexed DB transaction completed")
|
|
144
|
+
}
|
|
145
|
+
transaction.onerror = (event) => {
|
|
146
|
+
db.close()
|
|
147
|
+
console.error("DB transaction failed. Cannot store session id.")
|
|
148
|
+
console.error(event)
|
|
149
|
+
reject(new Error("Transaction failed"))
|
|
150
|
+
}
|
|
151
|
+
transaction.onabort = (ev) => {
|
|
152
|
+
db.close()
|
|
153
|
+
console.error("DB transaction aborted. Cannot store session id.")
|
|
154
|
+
console.error(ev)
|
|
155
|
+
reject(new Error("Transaction aborted"))
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
})
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Function retrieveSessionIdFromDb
|
|
163
|
+
*
|
|
164
|
+
* Gets the stored session id from the database. Returns a promise resolving to the result on success
|
|
165
|
+
* and to an empty string on failure
|
|
166
|
+
*
|
|
167
|
+
* @param {IDBOpenDBRequest} dbRequest
|
|
168
|
+
* @return {Promise<String>}
|
|
169
|
+
*/
|
|
170
|
+
const retrieveSessionIdFromDb = (dbRequest) => {
|
|
171
|
+
return new Promise((resolve, reject) => {
|
|
172
|
+
dbRequest.onsuccess = () => {
|
|
173
|
+
let result = ""
|
|
174
|
+
const db = dbRequest.result
|
|
175
|
+
const transaction = db.transaction(DB_STORE, "readwrite")
|
|
176
|
+
const store = transaction.objectStore(DB_STORE)
|
|
177
|
+
const idQuery = store.get(1)
|
|
178
|
+
idQuery.onsuccess = () => {
|
|
179
|
+
result = idQuery.result.sessionId
|
|
180
|
+
}
|
|
181
|
+
idQuery.onerror = () => {
|
|
182
|
+
console.error("Query by id failed")
|
|
183
|
+
}
|
|
184
|
+
transaction.oncomplete = () => {
|
|
185
|
+
db.close()
|
|
186
|
+
resolve(result)
|
|
187
|
+
}
|
|
188
|
+
transaction.onerror = (event) => {
|
|
189
|
+
db.close()
|
|
190
|
+
console.error("DB transaction failed. Cannot retrieve session id.")
|
|
191
|
+
console.error(event)
|
|
192
|
+
reject(new Error("Transaction failed"))
|
|
193
|
+
}
|
|
194
|
+
transaction.onabort = (ev) => {
|
|
195
|
+
db.close()
|
|
196
|
+
console.error("DB transaction aborted. Cannot retrieve session id.")
|
|
197
|
+
console.error(ev)
|
|
198
|
+
reject(new Error("Transaction aborted"))
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
})
|
|
202
|
+
}
|