-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport.js
192 lines (154 loc) · 5.44 KB
/
import.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
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
'use strict';
var fs = require('fs'),
esp = require('esprima'),
path = require('path'),
_ = require('lodash'),
escodegen = require('escodegen');
var SOURCE = process.argv[2],
TARGET = process.argv[3];
var MODULE_NAME = path.basename(TARGET).replace(path.extname(TARGET), '');
function _mkRequire(name) {
return {
type: 'CallExpression',
callee: {
type: 'Identifier',
name: 'require'
},
arguments: [{
type: 'Literal',
value: './' + name,
raw: '\'./' + name + '\''
}]
};
}
function _mkVar(obj, name, right) {
obj.type = 'VariableDeclaration';
obj.kind = 'var';
obj.declarations = [{
type: 'VariableDeclarator',
id: {
type: 'Identifier',
name: name
},
init: right
}];
}
function _mkModuleExports(value) {
value.object.name = 'module';
value.property.name = 'exports';
}
function _transform(obj, moduleName) {
_.each(obj, function(value, key) {
if (typeof value !== 'object' || !value) return;
/**
* 1. First check assignment expressions.
*
* 1.1 if the left side is something like "forge.{something}"
* this becomes var {something} = right side
*
* 1.2 if the left side is something like "forge.{first}.{second}"
* this becomes something like {first}.{second} = right side
* additionally, any {first} that matches the moduleName becomes simply module.exports
*
*/
if (value.type == 'AssignmentExpression') {
if (value.left.type == 'MemberExpression' && value.left.object.name == 'forge') {
if (value.left.property.name !== moduleName) {
_mkVar(obj, value.left.property.name, value.right);
_transform(value.right, moduleName);
return;
}
}
if (value.left.type == 'MemberExpression' && value.left.object.object && value.left.object.object.name == 'forge') {
var name = value.left.object.property.name;
value.left.object = {
type: 'Identifier',
name: name == moduleName ? 'module.exports' : name
};
_transform(value, moduleName);
return;
}
}
/**
* 2. This is not an assignment, if it's not an MemberExpression of forge.{something},
*
* simply walk it's elements and return.
*/
if (value.type !== 'MemberExpression' || (value.object && value.object.name !== 'forge'))
return _transform(value, moduleName);
/**
* 3. This must be a MemberExpression. Check if we're trying to get an object property
*
* This catches everythign that looks like forge.{something} and converts this
* into a require({something}), or module.exports if it's forge.{moduleName}
*/
if (value.property.type === 'Identifier') {
if (key == 'left' || key == 'object') {
if (value.property.name == moduleName)
return _mkModuleExports(value);
}
obj[key] = _mkRequire(value.property.name);
}
});
}
/**
* Find a node matching the "query" and return it as soon as possible.
*/
function search(query, obj, found) {
var keys = _.keys(obj);
for (var i = 0; i < keys.length; i++) {
var value = obj[keys[i]];
if (typeof value !== 'object' || !value) continue;
if (value.type === query.type && value.id && value.id.name === query.name) {
return obj;
}
found = search(query, value, found);
if (found) return found;
}
return found;
}
function _getAst(source, moduleName) {
var src = fs.readFileSync(source);
// hacky fix: remove references to forge.disableNativeCode
src = src.toString().replace(/[!]?forge.disableNativeCode\s(&&|\|\|)/, '');
// another pre-processing hack
if (moduleName == 'pbkdf2') {
src = src.replace('var pkcs5 = forge.pkcs5 = forge.pkcs5 || {};', 'var pkcs5 = {}');
}
return esp.parse(src, {
comment: true
});
}
function run(ast, moduleName, target) {
var code, body, query = {
type: 'FunctionDeclaration',
name: 'initModule'
};
/**
* First, find the function initModule. We'll only use the body of this function.
*/
var initModule = search(query, ast);
if( ! initModule ) return console.log('NO initModule found, cannot transform %s', moduleName );
body = initModule[0].body;
// Force the body type to program
// so escodegen considers this a top-level code block
body.type = 'Program';
// _transform changes the AST IN PLACE
_transform(body, moduleName);
code = escodegen.generate(body);
/**
* cipher.modes is missing due a missing module.export in cipherModes
* and a missing export in cipher
*/
if (moduleName === 'cipher') {
code = code + '\nmodule.exports.modes = require(\'./cipherModes\')';
}
if (moduleName === 'cipherModes') {
code = code + '\nmodule.exports = modes;';
}
// first comment block is the copyright block
// adding it back in.
code = '/' + ast.comments[0].value + '*/\n' + code;
fs.writeFileSync(target, code);
}
run(_getAst(SOURCE, MODULE_NAME), MODULE_NAME, TARGET);