-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbrowser.ts
208 lines (191 loc) · 7.02 KB
/
browser.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// #!/usr/bin/env babel-node
// -*- coding: utf-8 -*-
/** @module browser */
'use strict'
/* !
region header
Copyright Torben Sickert (info["~at~"]torben.website) 16.12.2012
License
-------
This library written by Torben Sickert stand under a creative commons
naming 3.0 unported license.
See https://creativecommons.org/licenses/by/3.0/deed.de
endregion
*/
// region imports
import {
CONSOLE_METHODS, ProcedureFunction, SynchronousProcedureFunction, timeout
} from 'clientnode'
import {DOMWindow, VirtualConsole} from 'jsdom'
import {LoaderContext} from 'webpack'
import {LoaderConfiguration} from './ejsLoader'
import {Browser, InitializedBrowser} from './type'
// endregion
// region declaration
declare const NAME: string
declare const TARGET_TECHNOLOGY: string
// endregion
// region variables
const onCreatedListener: Array<ProcedureFunction> = []
export const browser: Browser = {
debug: false,
domContentLoaded: false,
DOM: null,
initialized: false,
instance: null,
window: null,
windowLoaded: false
}
// endregion
// region ensure presence of common browser environment
if (typeof TARGET_TECHNOLOGY === 'undefined' || TARGET_TECHNOLOGY === 'node')
/*
NOTE: We use an asynchronous wrapper method to initialize
"browser.initialized" at module loading time.
*/
(async (): Promise<void> => {
// region mock browser environment
const [{JSDOM, VirtualConsole: VirtualConsoleImplementation}, path] =
await Promise.all([import('jsdom'), import('path')])
const virtualConsole: VirtualConsole =
new VirtualConsoleImplementation()
for (const name of CONSOLE_METHODS)
virtualConsole.on(name, console[name].bind(console))
virtualConsole.on(
'error',
(error: Error & {
detail: string
type: string
}): void => {
if (
!browser.debug &&
['XMLHttpRequest', 'resource loading'].includes(error.type)
)
console.warn(
`Loading resource failed: ${error.toString()}.`
)
else
console.error(error.stack, error.detail)
}
)
const render = (template: string): void => {
browser.DOM = JSDOM
browser.initialized = true
browser.instance = new browser.DOM(
template,
{
beforeParse: (window: DOMWindow): void => {
// We want to use it in a polymorphic way.
browser.window = (
global.window as unknown ??
window as unknown
) as Window
window.document.addEventListener(
'DOMContentLoaded',
() => {
/*
Move template results into global
pre-defined dom.
*/
if (global.window as unknown)
for (const type of [
'head', 'body'
] as const)
global.window.document[type]
.innerHTML =
window.document[type].innerHTML
browser.domContentLoaded = true
}
)
window.addEventListener('load', () => {
/*
NOTE: Maybe we have miss the "DOMContentLoaded"
event caused by a race condition.
*/
browser.domContentLoaded =
browser.windowLoaded =
true
})
for (const callback of onCreatedListener)
(callback as SynchronousProcedureFunction)()
},
resources: 'usable',
runScripts: 'dangerously',
url: 'http://localhost',
virtualConsole
}
)
}
if (typeof NAME === 'undefined' || NAME === 'webOptimizer') {
const filePath: string = path.join(__dirname, 'index.html.ejs')
/*
NOTE: We load dependencies now to avoid having file imports
after test runner has finished to isolate the environment.
*/
const ejsLoader = (await import('./ejsLoader')).default
const content: string = await (await import('fs')).promises
.readFile(filePath, {encoding: 'utf-8'})
render(ejsLoader.bind(
{resourcePath: filePath} as
unknown as
LoaderContext<LoaderConfiguration>
)(content))
} else
render(
await import('webOptimizerDefaultTemplateFilePath') as string
)
// endregion
})().catch((error: unknown) => {
console.error(error)
})
else {
browser.initialized = true
browser.window = window
window.document.addEventListener('DOMContentLoaded', (): void => {
browser.domContentLoaded = true
})
window.addEventListener('load', (): void => {
browser.windowLoaded = true
})
void timeout(() => {
for (const callback of onCreatedListener)
(callback as SynchronousProcedureFunction)()
})
}
// endregion
/**
* Provides a generic browser api in node or web contexts.
* @param replaceWindow - Indicates whether a potential existing window object
* should be replaced or not.
* @returns Determined environment.
*/
export const getInitializedBrowser = async (
replaceWindow = true
): Promise<InitializedBrowser> => {
let resolvePromise: (_browser: InitializedBrowser) => void
const promise = new Promise<InitializedBrowser>(
(resolve: (_browser: InitializedBrowser) => void): void => {
resolvePromise = resolve
}
)
/*
NOTE: We have to define window globally before anything is loaded to
ensure that all future instances share the same window object.
*/
const wrappedCallback = (): void => {
if (
replaceWindow &&
typeof global !== 'undefined' &&
(global as unknown as Window) !== browser.window
)
(global as unknown as {window: Window}).window =
browser.window as Window
resolvePromise(browser as InitializedBrowser)
}
if (browser.initialized)
wrappedCallback()
else
onCreatedListener.push(wrappedCallback)
return promise
}
export default getInitializedBrowser