Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
QuiteAFancyEmerald
GitHub Repository: QuiteAFancyEmerald/Holy-Unblocker
Path: blob/master/lib/rammerhead/src/classes/RammerheadSession.js
5253 views
1
const { Session } = require('testcafe-hammerhead');
2
const UploadStorage = require('testcafe-hammerhead/lib/upload/storage');
3
const generateId = require('../util/generateId');
4
const StrShuffler = require('../util/StrShuffler');
5
6
// disable UploadStorage, a testcafe testing feature we do not need
7
const emptyFunc = () => {};
8
UploadStorage.prototype.copy = emptyFunc;
9
UploadStorage.prototype.get = emptyFunc;
10
UploadStorage.prototype.store = emptyFunc;
11
12
/**
13
* wrapper for initializing Session with saving capabilities
14
*/
15
class RammerheadSession extends Session {
16
data = {};
17
createdAt = Date.now();
18
lastUsed = Date.now();
19
// ID used to generate the global autocomplete session.
20
// This must be a string containing 32 alphanumerical characters.
21
static autocompleteId = 'collectsearchautocompleteresults';
22
23
/**
24
* @param {object} options
25
* @param {string} options.id
26
* @param {boolean} options.dontConnectToData - used when we want to connect to data later (or simply don't want to)
27
* @param {boolean} options.disableShuffling
28
* @param {string[]} options.prependScripts
29
*/
30
constructor({ id = generateId(), dontConnectToData = false, disableShuffling = false, prependScripts = [] } = {}) {
31
super(['blah/blah'], {
32
allowMultipleWindows: true,
33
disablePageCaching: false
34
});
35
36
// necessary abstract methods for Session
37
this.getIframePayloadScript = async () => '';
38
this.getPayloadScript = async () => '';
39
this.getAuthCredentials = () => ({});
40
this.handleFileDownload = () => void 0;
41
this.handlePageError = () => void 0;
42
this.handleAttachment = () => void 0;
43
// this.handlePageError = (ctx, err) => {
44
// console.error(ctx.req.url);
45
// console.error(err);
46
// };
47
48
// intellisense //
49
/**
50
* @type {{ host: string, hostname: string, bypassRules?: string[]; port?: string; proxyAuth?: string, authHeader?: string } | null}
51
*/
52
this.externalProxySettings = null;
53
54
// disable http2. error handling from http2 proxy client to non-http2 user is too complicated to handle
55
// (status code 0, for example, will crash rammerhead)
56
this.isHttp2Disabled = () => true;
57
if (id !== RammerheadSession.autocompleteId) {
58
this.injectable.scripts.push(...prependScripts);
59
this.injectable.scripts.push('/rammer/rammerhead.js');
60
} else {
61
this.injectable.scripts.length = 0;
62
}
63
64
this.id = id;
65
this.shuffleDict = disableShuffling ? null : StrShuffler.generateDictionary();
66
if (!dontConnectToData) {
67
this.connectHammerheadToData();
68
}
69
}
70
/**
71
* @param {boolean} dontCookie - set this to true if the store is using a more reliable approach to
72
* saving the cookies (like in serializeSession)
73
*/
74
connectHammerheadToData(dontCookie = false) {
75
this._connectObjectToHook(this, 'createdAt');
76
this._connectObjectToHook(this, 'lastUsed');
77
this._connectObjectToHook(this, 'injectable');
78
this._connectObjectToHook(this, 'externalProxySettings');
79
this._connectObjectToHook(this, 'shuffleDict');
80
if (!dontCookie) this._connectObjectToHook(this.cookies._cookieJar.store, 'idx', 'cookies');
81
}
82
83
updateLastUsed() {
84
this.lastUsed = Date.now();
85
}
86
serializeSession() {
87
return JSON.stringify({
88
data: this.data,
89
serializedCookieJar: this.cookies.serializeJar()
90
});
91
}
92
// hook system and serializing are for two different store systems
93
static DeserializeSession(id, serializedSession) {
94
const parsed = JSON.parse(serializedSession);
95
if (!parsed.data) throw new Error('expected serializedSession to contain data object');
96
if (!parsed.serializedCookieJar)
97
throw new Error('expected serializedSession to contain serializedCookieJar object');
98
99
const session = new RammerheadSession({ id, dontConnectToData: true });
100
session.data = parsed.data;
101
session.connectHammerheadToData(true);
102
session.cookies.setJar(parsed.serializedCookieJar);
103
return session;
104
}
105
106
hasRequestEventListeners() {
107
// force forceProxySrcForImage to be true
108
// see https://github.com/DevExpress/testcafe-hammerhead/blob/a9fbf7746ff347f7bdafe1f80cf7135eeac21e34/src/session/index.ts#L180
109
return true;
110
}
111
/**
112
* @private
113
*/
114
_connectObjectToHook(obj, prop, dataProp = prop) {
115
const originalValue = obj[prop];
116
Object.defineProperty(obj, prop, {
117
get: () => this.data[dataProp],
118
set: (value) => {
119
this.data[dataProp] = value;
120
}
121
});
122
if (!(dataProp in this.data)) {
123
this.data[dataProp] = originalValue;
124
}
125
}
126
}
127
128
module.exports = RammerheadSession;
129
130