I am new in google closure compiler,and after read the documents in the closure tools site,I create a js and make the test.
However I found that even I use the Advanced Compilation level,the compiled codes are still easy to decompilated.
For example,this is the codes:
//2
window["_NameSpace_"]={};
window["_NameSpaceInternal_"]={};
(function() {
_NameSpaceInternal_.Class = function() {
var clazz = function() {
this["init"] && this["init"].apply(this, arguments);
};
var pros = {}, arg;
for(var c = 0, k = arguments.length; c < k; ++c) {
arg = arguments[c];
if( typeof arg === 'function') {
if(c == 0 && k > 1) {
var F = function() {
};
F.prototype = arg.prototype;
pros = new F;
arg = arg.prototype;
}
} else {
if(arg.init) {
clazz = arg["init"];
delete arg["init"]
}
}
}
for(var p in arg)
pros[p] = arg[p]
clazz.prototype = pros;
return clazz;
};
})();
(function(d){
d["Person"]=_NameSpaceInternal_.Class({
"init":function(name){
this.name=name;
},
"whatever":function(aaa){
}
});
})(_NameSpace_);
After compiled(I make a pretty format for human reading):
window.__MapNS__ = {};
window.__MapNSImpl__ = {};
__MapNSImpl__.a = function() {
function c() {
this.init && this.init.apply(this, arguments)
}
for (var b = {}, a, d = 0, e = arguments.length; d < e; ++d) if (a = arguments[d], "function" === typeof a) 0 == d && 1 < e && (b = function() {}, b.prototype = a.prototype, b = new b, a = a.prototype);
else {
a.b && (c = a.init, delete a.init)
}
if (!b || !a) throw Error("You must provide an object to copy from and to");
for (var f in a) b[f] = a[f];
c.prototype = b;
return c
};
(function(c) {
c.Person = __MapNSImpl__.a({
init: function(b) {
this.name = b
},
whatever:function(x){
}
})
})(__MapNS__);
Now,taks the class defination for "Person" for example, after compiled,all the methods for "Person" are clearly for hacker even these method have to be exposed.
But I wonder if I can make the compiled codes like this;
...........
var xx="init",xxx="whatever",xxxx=="Person";
c[xxxx] = __MapNSImpl__.a({
xx: function(b) {
this.name = b
},
xxx:function(x){
}
})
...........
Just like the google map's compress.
Any idea?
The exact answer you are looking for is to enable the AliasStrings pass in the compiler by either using the Java API or by making a custom build of the compiler. The pass is not enabled by default as it tends to produce larger code when accounting for gzip.
To actually get the same effect that most google products achieve, you'll need to make substantial changes to your code.
Define your objects and methods in the global scope. Assign them to namespaces after declaring them globally. You can use the output_wrapper flag to wrap the compiled code in a function to prevent global scope collisions. Example:
function a() {}; window['Namespace']['obj'] = a;
Define your objects directly - don't use a helper method. So instead of
var a = _NameSpaceInternal_.Class({"init":function(name){ this.name=name; });
You would use something like:
function a(){}; a.prototype.name = ""; a.prototype.init = function(name) { this.name=name; };
This avoids using quoted syntax and allows the compiler to rename your properties and methods.
For many more examples of coding style that compiles/renames optimally with Closure-compiler, see the Closure Library.
Related
Because I add a custom AOD clock in the watch file (the code is copied from another open source author, which is an effective code).
But the AOD interface seems to be locked in the original AOD. Did I miss some important settings?
When I set on the normal dial interface, the clock can be displayed in the normal interface, but the AOD clock has not changed. At present, the AOD interface seems to be locked and cannot be modified. However, the official ZEPP development file indicates that AOD display can be set.
https://docs.zepp.com/docs/watchface/api/hmSetting/getScreenType/?_highlight=aod#Screentype-number
Can anyone help me change the AOD clock? Did I miss some important settings? Thank you.
Figure 1: AOD interface, clock is not the style I want to change, it seems to be locked in the official AOD style.
enter image description here
Figure 2: Normal interface. The lowest dotted clock at the bottom is the style I want to change and successfully displayed. But he can only display in the normal interface that the AOD interface has not taken effect.
enter image description here
// aod 显示越少越省电
WatchFace.createWidget(hmUI.widget.IMG_TIME, {
hour_zero: 1,
hour_startX: aodhour_X,
hour_startY: aodhour_Y,
hour_array: aodt_array,
hour_space: 15,
hour_align: hmUI.align.LEFT,
minute_zero: 1,
minute_startX: aodminute_X,
minute_startY: aodminute_Y,
minute_array: aodt_array,
minute_space: 15,
minute_align: hmUI.align.LEFT,
show_level: hmUI.show_level.ONLY_NORMAL | hmUI.show_level.ONAL_AOD,
})
app.js
try {
(() => {
var e = __$$hmAppManager$$__.currentApp;
function t() {
return e.app;
}
const n = {};
function o() {
if ("undefined" != typeof self) return self;
if ("undefined" != typeof window) return window;
if ("undefined" != typeof global) return global;
if ("undefined" != typeof globalThis) return globalThis;
throw new Error("unable to locate global object");
}
t()
? DeviceRuntimeCore.HmUtils.gettextFactory(
n,
t().__globals__.lang,
"en-US"
)
: console.log(n);
let r = o();
r.Logger ||
("undefined" != typeof DeviceRuntimeCore &&
(r.Logger = DeviceRuntimeCore.HmLogger));
let i = o();
i.Buffer ||
("undefined" != typeof Buffer
? (i.Buffer = Buffer)
: (i.Buffer = DeviceRuntimeCore.Buffer));
let u = o();
"undefined" == typeof setTimeout &&
"undefined" != typeof timer &&
((u.clearTimeout = function (e) {
e && timer.stopTimer(e);
}),
(u.setTimeout = function (e, t) {
const n = timer.createTimer(
t || 1,
Number.MAX_SAFE_INTEGER,
function () {
u.clearTimeout(n), e && e();
},
{}
);
return n;
}),
(u.clearImmediate = function (e) {
e && timer.stopTimer(e);
}),
(u.setImmediate = function (e) {
const t = timer.createTimer(
1,
Number.MAX_SAFE_INTEGER,
function () {
u.clearImmediate(t), e && e();
},
{}
);
return t;
}),
(u.clearInterval = function (e) {
e && timer.stopTimer(e);
}),
(u.setInterval = function (e, t) {
return timer.createTimer(
1,
t,
function () {
e && e();
},
{}
);
})),
(e.app = DeviceRuntimeCore.App({
globalData: {},
onCreate(e) { },
onShow(e){},
onHide(e){},
onDestroy(e) { },
onError(e) { },
onPageNotFound(e) { },
onUnhandledRejection(e) { }
})),
(t().__globals__ = {
lang: new DeviceRuntimeCore.HmUtils.Lang(
DeviceRuntimeCore.HmUtils.getLanguage()
),
px: DeviceRuntimeCore.HmUtils.getPx(480)
}),
(t().__globals__.gettext = t()
? DeviceRuntimeCore.HmUtils.gettextFactory(
n,
t().__globals__.lang,
"en-US"
)
: console.log(n));
})();
} catch (e) {
console.log(e);
}
Im migrating my config from another library to node-config.
I have two questions:
The old library uses config.get('a:b'); to get some value, but node-config use a single dot as a delimiter: config.get('a.b');.
Is there is a way to configure it to use : to save my time and refactor my code?
Is there is a way to set a runtime values. e.g. config.set('key', 'val');?
Done it by:
1. wrap node-config in a new js file
2. proxied the get, has and set methods methods
Something like that:
const config = require('config');
const inMemDict = {};
const toNewKey = key => {
return key && key.split(':').join('.');
};
const { get: origGet, has: origHas } = config;
config.get = function (key, ...args) {
key = toNewKey(key);
if(typeof inMemDict[key] !== 'undefined') {
return inMemDict[key];
}
return origGet.apply(config, [key, ...args]);
};
config.has = function (key, ...args) {
key = toNewKey(key);
if(typeof inMemDict[key] !== 'undefined') {
return inMemDict[key];
}
return origHas.apply(config, [key, ...args]);
};
config.set = function (key, val) {
if(!key) return;
inMemDict[toNewKey(key)] = val;
};
module.exports = config;
I am trying to do a simple CRUD app using Ember + Rails and I'm getting the following error when trying to go to the /workouts route.
Error while loading route: TypeError {} ember.js?body=1:415
Uncaught TypeError: Object function () {
if (!wasApplied) {
Class.proto(); // prepare prototype...
}
o_defineProperty(this, GUID_KEY, undefinedDescriptor);
o_defineProperty(this, '_super', undefinedDescriptor);
var m = meta(this), proto = m.proto;
m.proto = this;
if (initMixins) {
// capture locally so we can clear the closed over variable
var mixins = initMixins;
initMixins = null;
this.reopen.apply(this, mixins);
}
if (initProperties) {
// capture locally so we can clear the closed over variable
var props = initProperties;
initProperties = null;
var concatenatedProperties = this.concatenatedProperties;
for (var i = 0, l = props.length; i < l; i++) {
var properties = props[i];
Ember.assert("Ember.Object.create no longer supports mixing in other definitions, use createWithMixins instead.", !(properties instanceof Ember.Mixin));
for (var keyName in properties) {
if (!properties.hasOwnProperty(keyName)) { continue; }
var value = properties[keyName],
IS_BINDING = Ember.IS_BINDING;
if (IS_BINDING.test(keyName)) {
var bindings = m.bindings;
if (!bindings) {
bindings = m.bindings = {};
} else if (!m.hasOwnProperty('bindings')) {
bindings = m.bindings = o_create(m.bindings);
}
bindings[keyName] = value;
}
var desc = m.descs[keyName];
Ember.assert("Ember.Object.create no longer supports defining computed properties.", !(value instanceof Ember.ComputedProperty));
Ember.assert("Ember.Object.create no longer supports defining methods that call _super.", !(typeof value === 'function' && value.toString().indexOf('._super') !== -1));
Ember.assert("`actions` must be provided at extend time, not at create time, when Ember.ActionHandler is used (i.e. views, controllers & routes).", !((keyName === 'actions') && Ember.ActionHandler.detect(this)));
if (concatenatedProperties && indexOf(concatenatedProperties, keyName) >= 0) {
var baseValue = this[keyName];
if (baseValue) {
if ('function' === typeof baseValue.concat) {
value = baseValue.concat(value);
} else {
value = Ember.makeArray(baseValue).concat(value);
}
} else {
value = Ember.makeArray(value);
}
}
if (desc) {
desc.set(this, keyName, value);
} else {
if (typeof this.setUnknownProperty === 'function' && !(keyName in this)) {
this.setUnknownProperty(keyName, value);
} else if (MANDATORY_SETTER) {
Ember.defineProperty(this, keyName, null, value); // setup mandatory setter
} else {
this[keyName] = value;
}
}
}
}
}
finishPartial(this, m);
this.init.apply(this, arguments);
m.proto = proto;
finishChains(this);
sendEvent(this, "init");
} has no method 'find'
My code is located here: https://github.com/ecl1pse/ember-workouts
What am I doing wrong?
Edit: Upon further investigation I believe the culprit is
EmberWorkouts.WorkoutsRoute = Ember.Route.extend(
model: -> EmberWorkouts.Workout.find()
This doesn't actually return anything. How do I debug from there?
If I replace that with this
EmberWorkouts.WorkoutsRoute = Ember.Route.extend
model: -> [{title: 'hi'}, {title: 'damn'}]
The view actually renders content.
How do I get the model to collect from Rails properly?
Ember Data's interface has changed a little with the current release:
You can clear out the store.js file entirely. Ember Data will automatically set up a data store for you using the REST Adapter (unless you tell it otherwise).
Use model: -> #store.find('workout') instead.
I tested this with your app and it works.
If you haven't read through the Ember Data Guide in the last week or two (it's changed a lot), I would spend a few minutes on it.
The fix for this error (as of ember-data 1.0.0.beta.6) for me was to make sure that the JSON returned from the server included an "id" field for each model, BUT not to explicitly declare the id when setting up the Ember DS.Model.
jbuilder template:
json.scans do
json.array! #scans do |scan|
json.id scan.id # This prop has to be there
json.name scan.name
end
end
Ember model:
EmberApp.Scan = DS.Model.extend(
// Don't include the id prop here
name: DS.attr("string")
)
I always have this issue in ember apps that are built on a rails backend. I have a groups.hbs template which lists out a bunch of groups, when you click on a group it loads the group.hbs template next to the groups template and changes the url to /groups/:group_id.
However, when I click the back and forward buttons, or try to manually load a url with a specific :group_id the group template fails to render and the console throws a giant
Uncaught TypeError: Object function () {
...
error.
group.js.coffee
App.Group = Ember.Object.extend()
App.Group.reopenClass
all: ->
App.ajax(
url: App.apiUrl('/groups')
).then (data) ->
console.log data
groups = []
for group in data.response
groups.addObject(App.Group.create(group))
console.log(groups)
groups
router.js.coffee
Mdm.Router.map ->
#resource 'groups', ->
#resource 'group', {path: "/:group_id"}
Mdm.Router.reopen
location: 'history'
I've never experienced this issue when building standalone ember apps. Any idea what would cause this?
EDIT
I should add that I am pulling my data from an api via XHR requests.
EDIT 2
I just explicitly created the GroupRoute and had it load all of the groups, this code is identical to the GroupsRoute. The template is still not rendering, but I no longer get that error.
GroupRoute
App.GroupRoute = Ember.Route.extend(model: ->
App.Group.all()
)
And GroupsRoute:
App.GroupsRoute = Ember.Route.extend(model: ->
App.Group.all()
)
EDIT 3
Here's the whole error if it helps anyone.
Uncaught TypeError: Object function () {
if (!wasApplied) {
Class.proto(); // prepare prototype...
}
o_defineProperty(this, GUID_KEY, undefinedDescriptor);
o_defineProperty(this, '_super', undefinedDescriptor);
var m = meta(this);
m.proto = this;
if (initMixins) {
// capture locally so we can clear the closed over variable
var mixins = initMixins;
initMixins = null;
this.reopen.apply(this, mixins);
}
if (initProperties) {
// capture locally so we can clear the closed over variable
var props = initProperties;
initProperties = null;
var concatenatedProperties = this.concatenatedProperties;
for (var i = 0, l = props.length; i < l; i++) {
var properties = props[i];
Ember.assert("Ember.Object.create no longer supports mixing in other definitions, use createWithMixins instead.", !(properties instanceof Ember.Mixin));
for (var keyName in properties) {
if (!properties.hasOwnProperty(keyName)) { continue; }
var value = properties[keyName],
IS_BINDING = Ember.IS_BINDING;
if (IS_BINDING.test(keyName)) {
var bindings = m.bindings;
if (!bindings) {
bindings = m.bindings = {};
} else if (!m.hasOwnProperty('bindings')) {
bindings = m.bindings = o_create(m.bindings);
}
bindings[keyName] = value;
}
var desc = m.descs[keyName];
Ember.assert("Ember.Object.create no longer supports defining computed properties.", !(value instanceof Ember.ComputedProperty));
Ember.assert("Ember.Object.create no longer supports defining methods that call _super.", !(typeof value === 'function' && value.toString().indexOf('._super') !== -1));
if (concatenatedProperties && indexOf(concatenatedProperties, keyName) >= 0) {
var baseValue = this[keyName];
if (baseValue) {
if ('function' === typeof baseValue.concat) {
value = baseValue.concat(value);
} else {
value = Ember.makeArray(baseValue).concat(value);
}
} else {
value = Ember.makeArray(value);
}
}
if (desc) {
desc.set(this, keyName, value);
} else {
if (typeof this.setUnknownProperty === 'function' && !(keyName in this)) {
this.setUnknownProperty(keyName, value);
} else if (MANDATORY_SETTER) {
Ember.defineProperty(this, keyName, null, value); // setup mandatory setter
} else {
this[keyName] = value;
}
}
}
}
}
finishPartial(this, m);
delete m.proto;
finishChains(this);
this.init.apply(this, arguments);
} has no method 'find'
EDIT
So I think I figured out the problem, when you click the back button or enter a manual url it wasn't finding the obkect based on id. So I added a find() method to the Group model. Not it looks like this:
Mdm.Group = Ember.Object.extend()
Mdm.Group.reopenClass
all: ->
Mdm.ajax(
url: Mdm.apiUrl('/groups')
).then (data) ->
console.log data
groups = []
for group in data.response
groups.addObject(Mdm.Group.create(group))
console.log(groups)
groups
find: (group_id) ->
Mdm.ajax(
url: Mdm.apiUrl("/groups/#{group_id}")
).then (data) ->
renderTemplate: (data)
And my GroupRoute looks like this:
Mdm.GroupRoute = Ember.Route.extend
model: (params) ->
console.log 'oh hai'
Mdm.Group.find(params.group_id)
Now in the console when I click the back button it is loading the data but its not associating the group template with the group_id. What is the best practice way to tell ember to do this?
I'm not a rails developer but try doing something like this for the simple model/route setup you show above
App.Group = Ember.Object.extend().reopenClass
groups: []
find: ->
$.ajax
url: "/api/groups/"
type: "GET"
cache: false
dataType: "json"
beforeSend: =>
#groups.clear()
success: (results) =>
[#groups.addObject(App.Group.create(result)) for result in results]
error: =>
alert "error: failed to load the available groups"
#groups
App.Router.map ->
#resource "groups", path: "/", ->
#route "group", path: "/:group_id"
How do I make a function declared in a closure, global ? This is for a google apps script, hence no window.
There is documentation on how to use closures in google apps scripts, but the example declares an object instead of a function.
http://code.google.com/googleapps/appsscript/articles/appengine.html
var JSON = JSON || {};
// foo = function(){}
(function ()
{
...
foo = function (a, b)
{
...
}
foo.prototype =
{
...
}
// window.foo = foo; // Not Possible
}());
This should work:
var globalFoo;
(function ()
{
...
foo = function (a, b)
{
...
}
foo.prototype =
{
...
}
globalFoo = foo;
// window.foo = foo; // Not Possible
}());
I've made a test in a regular html running on the browser and is works fine. Here is the example:
var globalFoo;
console.log("O1")
console.log(globalFoo);
(function(){
console.log("I1")
console.log(globalFoo);
var x = 13;
var foo = function() {
console.log('foo caled ' + x);
x++;
return true;
}
foo();
globalFoo = foo;
console.log("I2")
console.log(globalFoo);
})();
console.log("O1")
console.log(globalFoo);
console.log(globalFoo());
The firebug output to that is:
O1
undefined
I1
undefined
foo caled 13
I2
function()
O1
function()
foo caled 14
true