88 lines
2.5 KiB
JavaScript
88 lines
2.5 KiB
JavaScript
|
|
// egg-compat-patch.js
|
||
|
|
// 批量扫描并修复 egg.js v3 在 Node 22 下的全部缺失依赖
|
||
|
|
const fs = require('fs');
|
||
|
|
const path = require('path');
|
||
|
|
|
||
|
|
const BASE = path.resolve(__dirname, 'node_modules');
|
||
|
|
|
||
|
|
// === 已知缺失依赖 + shim 内容 ===
|
||
|
|
const SHIMS = {
|
||
|
|
'serialize-json': {
|
||
|
|
package: { name: 'serialize-json', version: '2.0.0', main: 'index.js' },
|
||
|
|
index: `'use strict';
|
||
|
|
// shim: serialize-json → native JSON
|
||
|
|
module.exports = JSON;
|
||
|
|
module.exports.parse = JSON.parse;
|
||
|
|
module.exports.stringify = JSON.stringify;
|
||
|
|
`
|
||
|
|
},
|
||
|
|
'sdk-base': {
|
||
|
|
package: { name: 'sdk-base', version: '4.0.0', main: 'index.js' },
|
||
|
|
index: `'use strict';
|
||
|
|
const EventEmitter = require('events');
|
||
|
|
class Base extends EventEmitter {
|
||
|
|
constructor(opts) { super(); this.options = opts || {}; }
|
||
|
|
ready(cb) { if (cb) cb(); return Promise.resolve(); }
|
||
|
|
}
|
||
|
|
module.exports = Base;
|
||
|
|
`
|
||
|
|
},
|
||
|
|
'tcp-base': {
|
||
|
|
package: { name: 'tcp-base', version: '4.0.0', main: 'index.js' },
|
||
|
|
index: `'use strict';
|
||
|
|
const sdkBase = require('sdk-base');
|
||
|
|
class TCPBase extends sdkBase {
|
||
|
|
constructor(opts) { super(opts); }
|
||
|
|
async close() {}
|
||
|
|
async connect() {}
|
||
|
|
}
|
||
|
|
module.exports = TCPBase;
|
||
|
|
`
|
||
|
|
},
|
||
|
|
'await-first': {
|
||
|
|
package: { name: 'await-first', version: '1.0.0', main: 'index.js' },
|
||
|
|
index: `'use strict';
|
||
|
|
module.exports = async function awaitFirst(emitter, event) {
|
||
|
|
return new Promise((resolve) => {
|
||
|
|
emitter.once(event, resolve);
|
||
|
|
});
|
||
|
|
};
|
||
|
|
`
|
||
|
|
},
|
||
|
|
'co': {
|
||
|
|
package: { name: 'co', version: '4.6.0', main: 'index.js' },
|
||
|
|
index: `'use strict';
|
||
|
|
// co shim: use Promise for basic generator support
|
||
|
|
module.exports = function co(gen) {
|
||
|
|
if (typeof gen === 'function') gen = gen();
|
||
|
|
if (!gen || typeof gen.next !== 'function') return Promise.resolve(gen);
|
||
|
|
return new Promise((resolve, reject) => {
|
||
|
|
function step(next) {
|
||
|
|
try {
|
||
|
|
const result = next();
|
||
|
|
if (result.done) return resolve(result.value);
|
||
|
|
Promise.resolve(result.value).then(v => step(() => gen.next(v)), e => step(() => gen.throw(e)));
|
||
|
|
} catch (e) { reject(e); }
|
||
|
|
}
|
||
|
|
step(() => gen.next());
|
||
|
|
});
|
||
|
|
};
|
||
|
|
`
|
||
|
|
},
|
||
|
|
};
|
||
|
|
|
||
|
|
// 创建所有 shim
|
||
|
|
for (const [name, files] of Object.entries(SHIMS)) {
|
||
|
|
const dir = path.join(BASE, name);
|
||
|
|
if (fs.existsSync(dir)) {
|
||
|
|
console.log(`[SKIP] ${name} already exists`);
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
fs.mkdirSync(dir, { recursive: true });
|
||
|
|
fs.writeFileSync(path.join(dir, 'package.json'), JSON.stringify(files.package, null, 2));
|
||
|
|
fs.writeFileSync(path.join(dir, 'index.js'), files.index);
|
||
|
|
console.log(`[CREATED] ${name}`);
|
||
|
|
}
|
||
|
|
|
||
|
|
console.log('\n=== 补丁应用完成 ===');
|