Skip to content

Commit 9905394

Browse files
committed
web: Improve navigator.plugins and navigator.mimeTypes so they can be used with JSON.stringify
1 parent d4b380b commit 9905394

File tree

1 file changed

+62
-35
lines changed

1 file changed

+62
-35
lines changed

web/packages/core/src/plugin-polyfill.ts

Lines changed: 62 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ import {
1818
* plugin emulator is already present.
1919
*/
2020
class RuffleMimeTypeArray implements MimeTypeArray {
21-
private readonly __mimeTypes: MimeType[];
22-
private readonly __namedMimeTypes: Record<string, MimeType>;
21+
readonly #mimeTypes: MimeType[];
22+
readonly #namedMimeTypes: Record<string, MimeType>;
2323

2424
constructor(mimeTypes?: MimeTypeArray) {
25-
this.__mimeTypes = [];
26-
this.__namedMimeTypes = {};
25+
this.#mimeTypes = [];
26+
this.#namedMimeTypes = {};
2727

2828
if (mimeTypes) {
2929
for (let i = 0; i < mimeTypes.length; i++) {
@@ -40,33 +40,37 @@ class RuffleMimeTypeArray implements MimeTypeArray {
4040
install(mimeType: MimeType): void {
4141
const wrapper = new RuffleMimeType(mimeType);
4242

43-
const index = this.__mimeTypes.length;
44-
this.__mimeTypes.push(wrapper);
45-
this.__namedMimeTypes[mimeType.type] = wrapper;
46-
this[wrapper.type] = wrapper;
43+
const index = this.#mimeTypes.length;
44+
this.#mimeTypes.push(wrapper);
45+
this.#namedMimeTypes[mimeType.type] = wrapper;
46+
Object.defineProperty(this, wrapper.type, {
47+
configurable: true,
48+
enumerable: false,
49+
value: wrapper,
50+
});
4751
this[index] = wrapper;
4852
}
4953

5054
item(index: number): MimeType {
5155
// This behavior is done to emulate a 32-bit uint,
5256
// which browsers use.
53-
return this.__mimeTypes[index >>> 0]!;
57+
return this.#mimeTypes[index >>> 0]!;
5458
}
5559

5660
namedItem(name: string): MimeType {
57-
return this.__namedMimeTypes[name]!;
61+
return this.#namedMimeTypes[name]!;
5862
}
5963

6064
get length(): number {
61-
return this.__mimeTypes.length;
65+
return this.#mimeTypes.length;
6266
}
6367

6468
[index: number]: MimeType;
6569

6670
[name: string]: unknown;
6771

6872
[Symbol.iterator](): IterableIterator<MimeType> {
69-
return this.__mimeTypes[Symbol.iterator]();
73+
return this.#mimeTypes[Symbol.iterator]();
7074
}
7175

7276
get [Symbol.toStringTag](): string {
@@ -80,26 +84,26 @@ class RuffleMimeTypeArray implements MimeTypeArray {
8084
* need to spoof `window.MimeType`.
8185
*/
8286
class RuffleMimeType implements MimeType {
83-
private readonly __mimeType: MimeType;
87+
readonly #mimeType: MimeType;
8488

8589
constructor(mimeType: MimeType) {
86-
this.__mimeType = mimeType;
90+
this.#mimeType = mimeType;
8791
}
8892

8993
get type(): string {
90-
return this.__mimeType.type;
94+
return this.#mimeType.type;
9195
}
9296

9397
get description(): string {
94-
return this.__mimeType.description;
98+
return this.#mimeType.description;
9599
}
96100

97101
get suffixes(): string {
98-
return this.__mimeType.suffixes;
102+
return this.#mimeType.suffixes;
99103
}
100104

101105
get enabledPlugin(): Plugin {
102-
return this.__mimeType.enabledPlugin;
106+
return this.#mimeType.enabledPlugin;
103107
}
104108

105109
get [Symbol.toStringTag](): string {
@@ -111,12 +115,31 @@ class RuffleMimeType implements MimeType {
111115
* Equivalent object to `Plugin` that allows us to falsify plugins.
112116
*/
113117
class RufflePlugin extends RuffleMimeTypeArray implements Plugin {
114-
constructor(
115-
readonly name: string,
116-
readonly description: string,
117-
readonly filename: string,
118-
) {
118+
readonly #name: string;
119+
readonly #description: string;
120+
readonly #filename: string;
121+
122+
constructor(name: string, description: string, filename: string) {
119123
super();
124+
this.#name = name;
125+
this.#description = description;
126+
this.#filename = filename;
127+
}
128+
129+
get name(): string {
130+
return this.#name;
131+
}
132+
133+
get description(): string {
134+
return this.#description;
135+
}
136+
137+
get filename(): string {
138+
return this.#filename;
139+
}
140+
141+
override get [Symbol.toStringTag](): string {
142+
return "Plugin";
120143
}
121144
}
122145

@@ -137,35 +160,39 @@ class RufflePlugin extends RuffleMimeTypeArray implements Plugin {
137160
* emulator is already present.
138161
*/
139162
class RufflePluginArray implements PluginArray {
140-
private readonly __plugins: Plugin[];
141-
private readonly __namedPlugins: Record<string, Plugin>;
163+
readonly #plugins: Plugin[];
164+
readonly #namedPlugins: Record<string, Plugin>;
142165

143166
constructor(plugins: PluginArray) {
144-
this.__plugins = [];
145-
this.__namedPlugins = {};
167+
this.#plugins = [];
168+
this.#namedPlugins = {};
146169

147170
for (let i = 0; i < plugins.length; i++) {
148171
this.install(plugins[i]!);
149172
}
150173
}
151174

152175
install(plugin: Plugin): void {
153-
const index = this.__plugins.length;
154-
this.__plugins.push(plugin);
155-
this.__namedPlugins[plugin.name] = plugin;
156-
this[plugin.name] = plugin;
176+
const index = this.#plugins.length;
177+
this.#plugins.push(plugin);
178+
this.#namedPlugins[plugin.name] = plugin;
179+
Object.defineProperty(this, plugin.name, {
180+
configurable: true,
181+
enumerable: false,
182+
value: plugin,
183+
});
157184
this[index] = plugin;
158185
}
159186

160187
item(index: number): Plugin {
161188
// This behavior is done to emulate a 32-bit uint,
162189
// which browsers use. Cloudflare's anti-bot
163190
// checks rely on this.
164-
return this.__plugins[index >>> 0]!;
191+
return this.#plugins[index >>> 0]!;
165192
}
166193

167194
namedItem(name: string): Plugin {
168-
return this.__namedPlugins[name]!;
195+
return this.#namedPlugins[name]!;
169196
}
170197

171198
refresh(): void {
@@ -177,15 +204,15 @@ class RufflePluginArray implements PluginArray {
177204
[name: string]: unknown;
178205

179206
[Symbol.iterator](): IterableIterator<Plugin> {
180-
return this.__plugins[Symbol.iterator]();
207+
return this.#plugins[Symbol.iterator]();
181208
}
182209

183210
get [Symbol.toStringTag](): string {
184211
return "PluginArray";
185212
}
186213

187214
get length(): number {
188-
return this.__plugins.length;
215+
return this.#plugins.length;
189216
}
190217
}
191218

0 commit comments

Comments
 (0)