-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathlibflac_pre.js
95 lines (83 loc) · 2.9 KB
/
libflac_pre.js
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
// libflac.js - port of libflac to JavaScript using emscripten
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['module', 'require'], factory.bind(null, root));
} else if (typeof module === 'object' && module.exports) {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
// use process.env (if available) for reading Flac environment settings:
var env = typeof process !== 'undefined' && process && process.env? process.env : root;
factory(env, module, module.require);
} else {
// Browser globals
root.Flac = factory(root);
}
}(typeof self !== 'undefined' ? self : typeof window !== 'undefined' ? window : this, function (global, expLib, require) {
'use strict';
var Module = Module || {};
var _flac_ready = false;
//in case resources are loaded asynchronously (e.g. *.mem file for minified version): setup "ready" handling
Module["onRuntimeInitialized"] = function(){
_flac_ready = true;
if(!_exported){
//if _exported is not yet set (may happen, in case initialization was strictly synchronously),
// do "pause" until sync initialization has run through
setTimeout(function(){do_fire_event('ready', [{type: 'ready', target: _exported}], true);}, 0);
} else {
do_fire_event('ready', [{type: 'ready', target: _exported}], true);
}
};
if(global && global.FLAC_SCRIPT_LOCATION){
Module["locateFile"] = function(fileName){
var path = global.FLAC_SCRIPT_LOCATION || '';
if(path[fileName]){
return path[fileName];
}
path += path && !/\/$/.test(path)? '/' : '';
return path + fileName;
};
//NOTE will be overwritten if emscripten has env specific implementation for this
var readBinary = function(filePath){
//for Node: use default implementation (copied from generated code):
if(ENVIRONMENT_IS_NODE){
var ret = read_(filePath, true);
if (!ret.buffer) {
ret = new Uint8Array(ret);
}
assert(ret.buffer);
return ret;
}
//otherwise: try "fallback" to AJAX
return new Promise(function(resolve, reject){
var xhr = new XMLHttpRequest();
xhr.responseType = "arraybuffer";
xhr.addEventListener("load", function(evt){
resolve(xhr.response);
});
xhr.addEventListener("error", function(err){
reject(err);
});
xhr.open("GET", filePath);
xhr.send();
});
};
}
//fallback for fetch && support file://-protocol: try read as binary if fetch fails
if(global && typeof global.fetch === 'function'){
var _fetch = global.fetch;
global.fetch = function(url){
return _fetch.apply(null, arguments).catch(function(err){
try{
var result = readBinary(url);
if(result && result.catch){
result.catch(function(_err){throw err});
}
return result;
} catch(_err){
throw err;
}
});
};
}