-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path.pnpmfile.cjs
More file actions
439 lines (382 loc) · 15.4 KB
/
Copy path.pnpmfile.cjs
File metadata and controls
439 lines (382 loc) · 15.4 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/**
* pnpm hook for config-driven local development.
*
* This hook reads `.openzeppelin-dev.json` from the repository root and rewrites
* configured dependency families to either packed tarballs or direct repo paths
* when their corresponding LOCAL_* flags are enabled.
*/
const fs = require('fs');
const path = require('path');
const CONFIG_FILE = '.openzeppelin-dev.json';
const STANDARD_FAMILIES = {
"ui": {
"repoName": "openzeppelin-ui",
"envFlag": "LOCAL_UI",
"envNames": [
"LOCAL_UI_PATH"
],
"defaultPath": "../openzeppelin-ui",
"packageMap": {
"@openzeppelin/ui-types": "packages/types",
"@openzeppelin/ui-utils": "packages/utils",
"@openzeppelin/ui-styles": "packages/styles",
"@openzeppelin/ui-components": "packages/components",
"@openzeppelin/ui-renderer": "packages/renderer",
"@openzeppelin/ui-react": "packages/react",
"@openzeppelin/ui-storage": "packages/storage"
}
},
"adapters": {
"repoName": "openzeppelin-adapters",
"envFlag": "LOCAL_ADAPTERS",
"envNames": [
"LOCAL_ADAPTERS_PATH"
],
"defaultPath": "../openzeppelin-adapters",
"packageMap": {
"@openzeppelin/adapters-vite": "packages/adapters-vite",
"@openzeppelin/adapter-evm": "packages/adapter-evm",
"@openzeppelin/adapter-midnight": "packages/adapter-midnight",
"@openzeppelin/adapter-polkadot": "packages/adapter-polkadot",
"@openzeppelin/adapter-solana": "packages/adapter-solana",
"@openzeppelin/adapter-stellar": "packages/adapter-stellar"
}
}
};
function isObject(value) {
return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
}
function getRealPath(targetPath) {
return typeof fs.realpathSync.native === 'function'
? fs.realpathSync.native(targetPath)
: fs.realpathSync(targetPath);
}
function resolveCacheDir(workspaceRoot, cacheDir) {
const resolvedWorkspaceRoot = path.resolve(workspaceRoot);
const resolvedCacheDir = path.resolve(resolvedWorkspaceRoot, cacheDir);
const relativeCacheDir = path.relative(resolvedWorkspaceRoot, resolvedCacheDir);
if (
relativeCacheDir === '' ||
relativeCacheDir.startsWith('..') ||
path.isAbsolute(relativeCacheDir)
) {
throw new Error(`${CONFIG_FILE} "cacheDir" must be a subdirectory of the workspace root.`);
}
return resolvedCacheDir;
}
function isAnyLocalFamilyEnabled() {
return Object.values(STANDARD_FAMILIES).some((family) => process.env[family.envFlag] === 'true');
}
function readProjectConfig(workspaceRoot) {
const configPath = path.join(workspaceRoot, CONFIG_FILE);
if (!fs.existsSync(configPath)) {
throw new Error(`Missing ${CONFIG_FILE} in ${workspaceRoot}.`);
}
const parsed = JSON.parse(fs.readFileSync(configPath, 'utf8'));
if (!isObject(parsed) || parsed.version !== 1 || !isObject(parsed.families)) {
throw new Error(`${CONFIG_FILE} must declare "version": 1 and a "families" object.`);
}
const families = Object.create(null);
for (const [familyKey, overrides] of Object.entries(parsed.families)) {
if (!Object.prototype.hasOwnProperty.call(STANDARD_FAMILIES, familyKey)) {
throw new Error(`Unsupported family "${familyKey}" in ${CONFIG_FILE}.`);
}
const familyOverrides = isObject(overrides) ? overrides : {};
const baseFamily = STANDARD_FAMILIES[familyKey];
const filteredEnvNames =
Array.isArray(familyOverrides.envNames) && familyOverrides.envNames.length > 0
? familyOverrides.envNames.filter(
(value) => typeof value === 'string' && value.length > 0
)
: null;
families[familyKey] = {
...baseFamily,
defaultPath:
typeof familyOverrides.defaultPath === 'string' && familyOverrides.defaultPath.length > 0
? familyOverrides.defaultPath
: baseFamily.defaultPath,
envNames:
filteredEnvNames && filteredEnvNames.length > 0
? filteredEnvNames
: [...baseFamily.envNames],
};
}
const cacheDirFromConfig =
typeof parsed.cacheDir === 'string' && parsed.cacheDir.trim().length > 0
? parsed.cacheDir
: '.packed-packages/local-dev';
return {
cacheDir: resolveCacheDir(workspaceRoot, cacheDirFromConfig),
families,
};
}
function getConfiguredPath(envNames, defaultPath) {
for (const envName of envNames) {
if (process.env[envName]) {
return {
envName,
relativePath: process.env[envName],
};
}
}
return {
envName: null,
relativePath: defaultPath,
};
}
function resolveRepoRoot(baseDir, family) {
const { envName, relativePath } = getConfiguredPath(family.envNames, family.defaultPath);
const resolvedPath = path.resolve(baseDir, relativePath);
if (!fs.existsSync(resolvedPath)) {
const envHelp = family.envNames.join(' or ');
const envSource = envName ? `${envName}=${relativePath}` : `default path ${family.defaultPath}`;
throw new Error(
`[local-dev] ${family.repoName} checkout not found at ${resolvedPath} (${envSource}). Set ${envHelp} to a valid ${family.repoName} checkout.`
);
}
return getRealPath(resolvedPath);
}
function resolvePackageDirectory(workspaceRoot, family, packageName, packagePath) {
const repoRoot = resolveRepoRoot(workspaceRoot, family);
const resolvedPath = path.resolve(repoRoot, packagePath);
const expectedPackageJsonPath = path.join(resolvedPath, 'package.json');
if (!fs.existsSync(resolvedPath)) {
throw new Error(
`[local-dev] Expected ${packageName} to have a package.json at ${expectedPackageJsonPath}, but it was not found. Check that ${family.repoName} matches a compatible checkout and contains this package.`
);
}
const absolutePath = getRealPath(resolvedPath);
const packageJsonPath = path.join(absolutePath, 'package.json');
if (!fs.existsSync(packageJsonPath)) {
throw new Error(
`[local-dev] Expected ${packageName} to have a package.json at ${packageJsonPath}, but it was not found. Check that ${family.repoName} matches a compatible checkout and contains this package.`
);
}
return absolutePath;
}
function findWorkspacePackage(repoRoot, packageName) {
const packagesDir = path.join(repoRoot, 'packages');
if (!fs.existsSync(packagesDir)) {
return null;
}
for (const entry of fs.readdirSync(packagesDir, { withFileTypes: true })) {
if (!entry.isDirectory()) {
continue;
}
const packageRoot = path.join(packagesDir, entry.name);
const packageJsonPath = path.join(packageRoot, 'package.json');
if (!fs.existsSync(packageJsonPath)) {
continue;
}
try {
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
if (packageJson.name === packageName) {
return getRealPath(packageRoot);
}
} catch {
continue;
}
}
return null;
}
function resolvePackageDirectoryByName(workspaceRoot, family, packageName) {
const explicitPackagePath = family.packageMap[packageName];
if (explicitPackagePath) {
return resolvePackageDirectory(workspaceRoot, family, packageName, explicitPackagePath);
}
const repoRoot = resolveRepoRoot(workspaceRoot, family);
return findWorkspacePackage(repoRoot, packageName);
}
function readPackedManifest(cacheDir, familyKey) {
const manifestPath = path.join(cacheDir, `${familyKey}.json`);
if (!fs.existsSync(manifestPath)) {
return null;
}
try {
const parsed = JSON.parse(fs.readFileSync(manifestPath, 'utf8'));
return isObject(parsed) && isObject(parsed.packages) ? parsed.packages : null;
} catch {
return null;
}
}
function rewriteDependencies(pkg, context, cacheDir, familyKey, family) {
const packedPackages = readPackedManifest(cacheDir, familyKey);
const workspaceRoot = __dirname;
for (const depType of ['dependencies', 'devDependencies']) {
if (!pkg[depType]) continue;
for (const npmName of Object.keys(pkg[depType])) {
if (!npmName.startsWith('@openzeppelin/')) {
continue;
}
const packedTarballPath = packedPackages && packedPackages[npmName];
if (packedTarballPath && fs.existsSync(packedTarballPath)) {
pkg[depType][npmName] = `file:${packedTarballPath}`;
context.log(`[local-dev] ${npmName} → ${packedTarballPath} (packed)`);
continue;
}
const absolutePath = resolvePackageDirectoryByName(workspaceRoot, family, npmName);
if (!absolutePath) {
continue;
}
pkg[depType][npmName] = `file:${absolutePath}`;
const source =
Object.prototype.hasOwnProperty.call(family.packageMap, npmName) ? '' : ' (workspace fallback)';
context.log(`[local-dev] ${npmName} → ${absolutePath}${source}`);
}
}
}
/**
* Widen caret ranges on adapter packages so pnpm can resolve pre-release
* versions (e.g. 2.0.0-rc.1) that a plain ^2.0.0 would exclude.
* Follows standard caret semantics for the upper bound.
* Skips deps already rewritten to file: paths by local-dev mode.
*/
function allowAdapterPrereleases(pkg) {
for (const depType of ['dependencies', 'devDependencies']) {
if (!pkg[depType]) continue;
for (const [name, range] of Object.entries(pkg[depType])) {
if (typeof range !== 'string') continue;
if (!name.startsWith('@openzeppelin/adapter') && !name.startsWith('@openzeppelin/adapters-'))
continue;
if (!range.startsWith('^')) continue;
const m = range.slice(1).match(/^(\d+)\.(\d+)\.(\d+)$/);
if (!m) continue;
const maj = Number(m[1]), min = Number(m[2]), pat = Number(m[3]);
const upper = maj > 0
? `${maj + 1}.0.0`
: min > 0
? `0.${min + 1}.0`
: `0.0.${pat + 1}`;
pkg[depType][name] = `>=${maj}.${min}.${pat}-0 <${upper}`;
}
}
}
// --- License compliance: strip the Trezor (T-RSL) stack --------------------
// @creit.tech/stellar-wallets-kit hard-depends on @trezor/connect-web and
// @trezor/connect-plugin-stellar, which together pull in 22 @trezor/* packages
// licensed under the Trezor Reference Source License (T-RSL). T-RSL grants
// "reference use" within the company only and excludes the right to distribute
// the software outside the company.
//
// Nothing we ship reaches that code: the kit's barrel does not re-export
// modules/trezor.module, allowAllModules() returns only the eight non-Trezor
// modules, and trezor.module is an isolated leaf that nothing else in the kit
// imports. Dropping both deps leaves bundle output byte-identical.
//
// Done here rather than through pnpm.patchedDependencies so it is not pinned to
// one kit version, and so it applies however deep the kit is pulled in (e.g.
// via @openzeppelin/adapter-stellar rather than a direct dependency).
const TREZOR_DEP_HOST = '@creit.tech/stellar-wallets-kit';
const TREZOR_DEPS = ['@trezor/connect-web', '@trezor/connect-plugin-stellar'];
const TREZOR_DEP_FIELDS = ['dependencies', 'optionalDependencies', 'peerDependencies'];
function stripTrezorDependencies(pkg, context) {
if (pkg.name !== TREZOR_DEP_HOST) {
return;
}
for (const field of TREZOR_DEP_FIELDS) {
for (const dep of TREZOR_DEPS) {
if (pkg[field] && dep in pkg[field]) {
delete pkg[field][dep];
context.log(`[license] stripped ${dep} from ${pkg.name}@${pkg.version} (T-RSL)`);
}
}
}
}
// --- License compliance: strip the WalletConnect / Reown stack ---------------
// We no longer register a WalletConnect connector in any ecosystem, so these
// dependencies are dead weight -- and the EVM one drags in @reown/appkit.
//
// Reown moved AppKit to the Reown Community License at 1.8.3 (commercial fees
// above 500 monthly active users, a mandatory-gateway clause and a
// confidentiality clause). The wagmi team have themselves deprecated their
// walletConnect connector over that relicence, noting they cannot patch a known
// downstream vulnerability (pino@7.11.0) because of it.
//
// Both host packages reach WalletConnect through a single isolated module that
// nothing else imports:
// - @wagmi/connectors -> walletConnect.js (dynamic import, unused)
// - @creit.tech/stellar-wallets-kit -> modules/walletconnect.module (not in the
// barrel, not in allowAllModules())
// @wagmi/connectors@8+ makes its WalletConnect dependency an optional peer; until
// we move to wagmi 3 this hook achieves the same on the version we pin.
const WALLETCONNECT_STRIP = {
'@wagmi/connectors': ['@walletconnect/ethereum-provider'],
'@creit.tech/stellar-wallets-kit': ['@walletconnect/modal', '@walletconnect/sign-client'],
};
const WALLETCONNECT_DEP_FIELDS = ['dependencies', 'optionalDependencies', 'peerDependencies'];
function stripWalletConnectDependencies(pkg, context) {
const deps = WALLETCONNECT_STRIP[pkg.name];
if (!deps) {
return;
}
for (const field of WALLETCONNECT_DEP_FIELDS) {
for (const dep of deps) {
if (pkg[field] && dep in pkg[field]) {
delete pkg[field][dep];
context.log(`[license] stripped ${dep} from ${pkg.name}@${pkg.version} (WalletConnect)`);
}
}
}
}
// --- License compliance: strip the MetaMask SDK -----------------------------
// @metamask/sdk ships a proprietary ConsenSys licence, not an open-source one:
// "Copyright ConsenSys Software Inc. 2022. All rights reserved", granting only a
// non-exclusive, non-transferable licence for Non-Commercial Use -- and clause 2
// requires any Resulting Program to carry that same Non-Commercial restriction
// forward.
//
// The OpenZeppelin adapters are AGPL-3.0, which forbids conveying the work under
// added restrictions. A non-commercial-only restriction is exactly such a
// restriction, so the two licences cannot both be satisfied. The conflict is
// structural: it does not depend on a monthly-active-user count, and there is no
// clean version to pin (no published version declares a `license` field at all).
//
// The same 2715-byte licence file ships in @metamask/sdk,
// @metamask/sdk-communication-layer and @metamask/sdk-install-modal-web.
//
// Scoped to that one name on purpose. Most of @metamask/* is MIT or ISC (utils,
// providers, json-rpc-engine, rpc-errors, superstruct, ...) and is legitimately
// needed; a scope-wide strip would break far more than it fixes.
//
// @wagmi/connectors declares @metamask/sdk as a hard dependency, not an optional
// peer, so it installs whether or not a metaMask() connector is registered.
const METAMASK_STRIP = {
'@wagmi/connectors': ['@metamask/sdk'],
};
const METAMASK_DEP_FIELDS = ['dependencies', 'optionalDependencies', 'peerDependencies'];
function stripMetaMaskDependencies(pkg, context) {
const deps = METAMASK_STRIP[pkg.name];
if (!deps) {
return;
}
for (const field of METAMASK_DEP_FIELDS) {
for (const dep of deps) {
if (pkg[field] && dep in pkg[field]) {
delete pkg[field][dep];
context.log(`[license] stripped ${dep} from ${pkg.name}@${pkg.version} (MetaMask SDK)`);
}
}
}
}
function readPackage(pkg, context) {
stripWalletConnectDependencies(pkg, context);
stripTrezorDependencies(pkg, context);
stripMetaMaskDependencies(pkg, context);
if (isAnyLocalFamilyEnabled()) {
const workspaceRoot = __dirname;
const projectConfig = readProjectConfig(workspaceRoot);
for (const [familyKey, family] of Object.entries(projectConfig.families)) {
if (process.env[family.envFlag] !== 'true') {
continue;
}
rewriteDependencies(pkg, context, projectConfig.cacheDir, familyKey, family);
}
}
allowAdapterPrereleases(pkg);
return pkg;
}
module.exports = {
hooks: {
readPackage,
},
};