patch-node-modules.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. const fs = require('fs');
  2. const path = require('path');
  3. const log = (msg) => console.log(`[OASIS] [PATCH] ${msg}`);
  4. // === Patch ssb-ref ===
  5. const ssbRefPath = path.resolve(__dirname, '../src/server/node_modules/ssb-ref/index.js');
  6. if (fs.existsSync(ssbRefPath)) {
  7. const data = fs.readFileSync(ssbRefPath, 'utf8');
  8. // Check if already in desired state (no deprecate wrapper on parseAddress)
  9. const alreadyClean = /exports\.parseAddress\s*=\s*parseAddress/.test(data);
  10. if (!alreadyClean) {
  11. const patched = data.replace(
  12. /exports\.parseAddress\s*=\s*deprecate\(\s*['"][^'"]*['"]\s*,\s*parseAddress\s*\)/,
  13. 'exports.parseAddress = parseAddress'
  14. );
  15. if (patched !== data) {
  16. fs.writeFileSync(ssbRefPath, patched);
  17. log('Patched ssb-ref to remove deprecated usage of parseAddress');
  18. } else {
  19. log('ssb-ref patch skipped: unexpected parseAddress export format');
  20. }
  21. }
  22. } else {
  23. log('ssb-ref patch skipped: file not found at ' + ssbRefPath);
  24. }
  25. // === Patch ssb-blobs ===
  26. const ssbBlobsPath = path.resolve(__dirname, '../src/server/node_modules/ssb-blobs/inject.js');
  27. if (fs.existsSync(ssbBlobsPath)) {
  28. let data = fs.readFileSync(ssbBlobsPath, 'utf8');
  29. const marker = 'want: function (id, cb)';
  30. const startIndex = data.indexOf(marker);
  31. if (startIndex !== -1) {
  32. const endIndex = data.indexOf('},', startIndex); // end of function block
  33. if (endIndex !== -1) {
  34. const before = data.slice(0, startIndex);
  35. const after = data.slice(endIndex + 2);
  36. const replacement = `
  37. want: function (id, cb) {
  38. id = toBlobId(id);
  39. if (!isBlobId(id)) return cb(new Error('invalid id:' + id));
  40. if (blobStore.isEmptyHash(id)) return cb(null, true);
  41. if (wantCallbacks[id]) {
  42. if (!Array.isArray(wantCallbacks[id])) wantCallbacks[id] = [];
  43. wantCallbacks[id].push(cb);
  44. } else {
  45. wantCallbacks[id] = [cb];
  46. blobStore.size(id, function (err, size) {
  47. if (err) return cb(err);
  48. if (size != null) {
  49. while (wantCallbacks[id].length) {
  50. const fn = wantCallbacks[id].shift();
  51. if (typeof fn === 'function') fn(null, true);
  52. }
  53. delete wantCallbacks[id];
  54. }
  55. });
  56. }
  57. const peerId = findPeerWithBlob(id);
  58. if (peerId) get(peerId, id);
  59. if (wantCallbacks[id]) registerWant(id);
  60. },`;
  61. const finalData = before + replacement + after;
  62. fs.writeFileSync(ssbBlobsPath, finalData);
  63. log('Patched ssb-blobs to fix wantCallbacks handling');
  64. } else {
  65. log('ssb-blobs patch skipped: end of want function not found');
  66. }
  67. } else {
  68. log('ssb-blobs patch skipped: want function not found');
  69. }
  70. }