/
nix-require.js
executable file
·198 lines (178 loc) · 7.45 KB
/
nix-require.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
193
194
195
196
197
198
const Module = require('module');
const {require: originalRequire, _compile: originalCompile} = Module.prototype;
const {existsSync: exists} = require('fs');
const {dirname, join: pathJoin, sep: pathSep} = require('path').posix;
const makeNixResolverFor = requirer => requiredPath => {
const depMap = originalRequire(process.env.NIX_JSON);
const scoped = path => path[0] == '@';
const slicePath = path => a => b => path.split(pathSep).slice(a, b).join(pathSep);
const moduleHead = path => slicePath(path)(0)(scoped(path) ? 2 : 1);
const findUp = (path, file = pathJoin(path, "package.json")) => exists(file) ? file : findUp(dirname(path));
// trimNixPath('/nix/store/abcdefghijklmnopqrstuvwxyz-something/lib/blah/blah') => 'abcdefghijklmnopqrstuvwxyz-something'
const trimNixPath = storePath => storePath.match(/([a-z0-9]{32}-.+?)\//)[1];
const findParent = requirer => {
try {
const requirerNixHash = trimNixPath(requirer.filename);
const parentNixHash = trimNixPath(requirer.parent.filename);
if (requirerNixHash !== parentNixHash) {
return requirer.parent;
}
return findParent(requirer.parent);
} catch (e) {
return null;
}
}
const getRequirerVersion = requirer => {
const {name: requirerName, version: ownVersion} = originalRequire(findUp(requirer.filename));
const findRequirerVersion = parent => {
try {
const versionFromParent = originalRequire(findUp(parent.filename))['dependencies'][requirerName];
if (depMap[requirerName][versionFromParent]) {
return versionFromParent;
}
throw 'SHADE';
} catch (e) {
if (depMap[requirerName][ownVersion]) {
return ownVersion;
}
const nextParent = findParent(parent);
if (nextParent) {
return findRequirerVersion(findParent(nextParent));
}
return null
}
}
return findRequirerVersion(findParent(requirer));
};
const mkVersionFinderFor = requirer => moduleName => {
const packageJson = originalRequire(findUp(requirer.filename));
const {name} = packageJson;
const version = getRequirerVersion(requirer);
const {requires = {}, packageJsonOverride = {}} = depMap[name][version];
const {peerDependencies = {}} = Object.assign({}, packageJson, packageJsonOverride);
const findPeerVersion = requirer => {
const {name} = originalRequire(findUp(requirer.filename));
const version = getRequirerVersion(requirer);
const {requires = {}} = depMap[name][version];
return (requires[moduleName] || findPeerVersion(findParent(requirer)));
};
const isPeer = !!peerDependencies[moduleName];
if (isPeer) {
return findPeerVersion(findParent(requirer));
} else {
return requires[moduleName];
}
};
const requiredVersionOf = mkVersionFinderFor(requirer);
const getNixPathFor = moduleName => depMap[moduleName] && depMap[moduleName][requiredVersionOf(moduleName)].path;
const moduleName = moduleHead(requiredPath);
const nixPath = getNixPathFor(moduleName)
return pathJoin(nixPath, 'lib', 'node_modules');
}
Module.prototype.require = function(request) {
const nixResolve = makeNixResolverFor(this);
const resolveOpts = {};
try {
resolveOpts.paths = [nixResolve(request)];
arguments[0] = require.resolve(request, resolveOpts);
} catch (e) {
if (process.env.NODE_NIX_REQUIRE_VERBOSE) {
console.log(e)
}
} finally {
return originalRequire.call(this, arguments[0]);
}
}
function stripShebang(content) {
// Remove shebang
var contLen = content.length;
if (contLen >= 2) {
if (content.charCodeAt(0) === 35/*#*/ &&
content.charCodeAt(1) === 33/*!*/) {
if (contLen === 2) {
// Exact match
content = '';
} else {
// Find end of shebang line and slice it off
var i = 2;
for (; i < contLen; ++i) {
var code = content.charCodeAt(i);
if (code === 10/*\n*/ || code === 13/*\r*/)
break;
}
if (i === contLen)
content = '';
else {
// Note that this actually includes the newline character(s) in the
// new output. This duplicates the behavior of the regular expression
// that was previously used to replace the shebang line
content = content.slice(i);
}
}
}
}
return content;
}
Module.prototype._compile = function(content, filename) {
originalCompile.call(this, `
const originalRequire = require;
const originalRequireResolve = require.resolve;
const makeNixResolverFor = requiringFile => requiringFileParent => requiredPath => {
const {existsSync: exists} = originalRequire('fs');
const {dirname, join: pathJoin, sep: pathSep} = originalRequire('path').posix;
const depMap = originalRequire(process.env.NIX_JSON);
const scoped = path => path[0] == '@';
const slicePath = path => a => b => path.split(pathSep).slice(a, b).join(pathSep);
const moduleHead = path => slicePath(path)(0)(scoped(path) ? 2 : 1);
const findUp = (path, file = pathJoin(path, "package.json")) => exists(file) ? file : findUp(dirname(path));
const {name, version} = originalRequire(findUp(requiringFile));
const requiredVersionOf = moduleName => {
try {
return depMap[name][version]['requires'][moduleName];
} catch (e) {
if (depMap[name][version]['peers'].includes(moduleName)) {
const {name: parentName, version: parentVersion} = originalRequire(findUp(requiringFileParent));
return depMap[parentName][parentVersion]['requires'][moduleName];
} else {
throw 'SHADE';
}
};
};
const nixPathFor = moduleName => depMap[moduleName] && depMap[moduleName][requiredVersionOf(moduleName)].path;
const moduleName = moduleHead(requiredPath);
// console.error('requiredPath ' + requiredPath + ' moduleName ' + moduleName);
const nixPath = nixPathFor(moduleName)
return pathJoin(nixPath, 'lib', 'node_modules');
}
// const makeNixResolverFor = requiringFile => requiredPath => {
// const {existsSync: exists} = originalRequire('fs');
// const {dirname, join: pathJoin, sep: pathSep} = originalRequire('path').posix;
// const depMap = originalRequire(process.env.NIX_JSON);
// const scoped = path => path[0] == '@';
// const slicePath = path => a => b => path.split(pathSep).slice(a, b).join(pathSep);
// const moduleHead = path => slicePath(path)(0)(scoped(path) ? 2 : 1);
// const findUp = (path, file = pathJoin(path, "package.json")) => exists(file) ? file : findUp(dirname(path));
// const {name, version} = originalRequire(findUp(requiringFile));
// const requiredVersionOf = moduleName => {
// try {
// return depMap[name][version]['requires'][moduleName];
// } catch (e) {
// throw 'SHADE';
// };
// };
// const nixPathFor = moduleName => depMap[moduleName] && depMap[moduleName][requiredVersionOf(moduleName)].path;
// const moduleName = moduleHead(requiredPath);
// const nixPath = nixPathFor(moduleName)
// return pathJoin(nixPath, 'lib', 'node_modules');
// };
require.resolve = function(requiredPath) {
const nixResolve = makeNixResolverFor(module.filename)(module.parent ? module.parent.filename : null);
const resolveOpts = {};
try {
resolveOpts.paths = [nixResolve(requiredPath)];
} finally {
return originalRequireResolve(requiredPath, resolveOpts);
}
};
` + stripShebang(content), filename)
}