Skip to content

Legacy Brotli #114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ $(DIST_DIR)/lib/libexpat.a: build/lib/expat/configured
emmake make install

build/lib/brotli/js/decode.js: build/lib/brotli/configured
build/lib/brotli/js/polyfill.js: build/lib/brotli/configured
build/lib/brotli/configured: lib/brotli $(wildcard $(BASE_DIR)build/patches/brotli/*.patch)
rm -rf build/lib/brotli
cp -r lib/brotli build/lib/brotli
Expand Down Expand Up @@ -325,9 +326,11 @@ dist/js/subtitles-octopus-worker.js: src/subtitles-octopus-worker.bc src/pre-wor
-s WASM=1 \
$(EMCC_COMMON_ARGS)

dist/js/subtitles-octopus-worker-legacy.js: src/subtitles-octopus-worker.bc src/pre-worker.js src/SubOctpInterface.js src/post-worker.js build/lib/brotli/js/decode.js
dist/js/subtitles-octopus-worker-legacy.js: src/subtitles-octopus-worker.bc src/polyfill.js src/pre-worker.js src/SubOctpInterface.js src/post-worker.js build/lib/brotli/js/decode.js build/lib/brotli/js/polyfill.js
mkdir -p dist/js
emcc src/subtitles-octopus-worker.bc $(OCTP_DEPS) \
--pre-js src/polyfill.js \
--pre-js build/lib/brotli/js/polyfill.js \
--pre-js src/pre-worker.js \
--pre-js build/lib/brotli/js/decode.js \
--post-js src/SubOctpInterface.js \
Expand Down
94 changes: 94 additions & 0 deletions src/polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
if (!String.prototype.startsWith) {
String.prototype.startsWith = function (search, pos) {
if (pos === undefined) {
pos = 0;
}
return this.substring(pos, search.length) === search;
};
}

if (!String.prototype.endsWith) {
String.prototype.endsWith = function (search, this_len) {
if (this_len === undefined || this_len > this.length) {
this_len = this.length;
}
return this.substring(this_len - search.length, this_len) === search;
};
}

if (!String.prototype.includes) {
String.prototype.includes = function (search, pos) {
return this.indexOf(search, pos) !== -1;
};
}

if (!ArrayBuffer.isView) {
var typedArrays = [
Int8Array,
Uint8Array,
Uint8ClampedArray,
Int16Array,
Uint16Array,
Int32Array,
Uint32Array,
Float32Array,
Float64Array
];

ArrayBuffer.isView = function (obj) {
return obj && obj.constructor && typedArrays.indexOf(obj.constructor) !== -1;
};
}

if (!Int8Array.prototype.slice) {
Object.defineProperty(Int8Array.prototype, 'slice', {
value: function (begin, end)
{
return new Int8Array(this.subarray(begin, end));
}
});
}

if (!Uint8Array.prototype.slice) {
Object.defineProperty(Uint8Array.prototype, 'slice', {
value: function (begin, end)
{
return new Uint8Array(this.subarray(begin, end));
}
});
}

if (!Int16Array.from) {
// Doesn't work for String
Int16Array.from = function (source) {
var arr = new Int16Array(source.length);
arr.set(source, 0);
return arr;
};
}

if (!Int32Array.from) {
// Doesn't work for String
Int32Array.from = function (source) {
var arr = new Int32Array(source.length);
arr.set(source, 0);
return arr;
};
}

// performance.now() polyfill
if ("performance" in self === false) {
self.performance = {};
}
Date.now = (Date.now || function () {
return new Date().getTime();
});
if ("now" in self.performance === false) {
var nowOffset = Date.now();
if (performance.timing && performance.timing.navigationStart) {
nowOffset = performance.timing.navigationStart
}
self.performance.now = function now() {
return Date.now() - nowOffset;
}
}
2 changes: 1 addition & 1 deletion src/post-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ self.freeTrack = function () {
*/
self.setTrackByUrl = function (url) {
var content = "";
if (url.endsWith(".br")) {
if (isBrotliFile(url)) {
content = Module["BrotliDecode"](readBinary(url))
} else {
content = read_(url);
Expand Down
87 changes: 17 additions & 70 deletions src/pre-worker.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,3 @@
if (!String.prototype.startsWith) {
String.prototype.startsWith = function (search, pos) {
if (pos === undefined) {
pos = 0;
}
return this.substring(pos, search.length) === search;
};
}

if (!String.prototype.endsWith) {
String.prototype.endsWith = function(search, this_len) {
if (this_len === undefined || this_len > this.length) {
this_len = this.length;
}
return this.substring(this_len - search.length, this_len) === search;
};
}

if (!String.prototype.includes) {
String.prototype.includes = function (search, pos) {
return this.indexOf(search, pos) !== -1;
};
}

if (!Uint8Array.prototype.slice) {
Object.defineProperty(Uint8Array.prototype, 'slice', {
value: function (begin, end)
{
return new Uint8Array(this.subarray(begin, end));
}
});
}

if (!Int16Array.from) {
// Doesn't work for String
Int16Array.from = function (source) {
var arr = new Int16Array(source.length);
arr.set(source, 0);
return arr;
};
}

if (!Int32Array.from) {
// Doesn't work for String
Int32Array.from = function (source) {
var arr = new Int32Array(source.length);
arr.set(source, 0);
return arr;
};
}


var hasNativeConsole = typeof console !== "undefined";

// implement console methods if they're missing
Expand Down Expand Up @@ -84,6 +32,22 @@ function makeCustomConsole() {
return console;
}

/**
* Test the subtitle file for Brotli compression.
* @param {!string} url the URL of the subtitle file.
* @returns {boolean} Brotli compression found or not.
*/
function isBrotliFile(url) {
// Search for parameters
var len = url.indexOf("?");

if (len === -1) {
len = url.length;
}

return url.endsWith(".br", len);
}

Module = Module || {};

Module["preRun"] = Module["preRun"] || [];
Expand All @@ -96,7 +60,7 @@ Module["preRun"].push(function () {

if (!self.subContent) {
// We can use sync xhr cause we're inside Web Worker
if (self.subUrl.endsWith(".br")) {
if (isBrotliFile(self.subUrl)) {
self.subContent = Module["BrotliDecode"](readBinary(self.subUrl))
} else {
self.subContent = read_(self.subUrl);
Expand Down Expand Up @@ -184,20 +148,3 @@ if (!hasNativeConsole) {
},
};
}

// performance.now() polyfill
if ("performance" in self === false) {
self.performance = {};
}
Date.now = (Date.now || function () {
return new Date().getTime();
});
if ("now" in self.performance === false) {
var nowOffset = Date.now();
if (performance.timing && performance.timing.navigationStart) {
nowOffset = performance.timing.navigationStart
}
self.performance.now = function now() {
return Date.now() - nowOffset;
}
}