growi-plugin.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. import fs, { readFileSync } from 'fs';
  2. import path from 'path';
  3. import { GrowiPluginType, type GrowiThemeMetadata, type ViteManifest } from '@growi/core';
  4. import type { GrowiPluginPackageData } from '@growi/pluginkit';
  5. import { importPackageJson, validatePackageJson } from '@growi/pluginkit/dist/v4/server';
  6. // eslint-disable-next-line no-restricted-imports
  7. import axios from 'axios';
  8. import mongoose from 'mongoose';
  9. import streamToPromise from 'stream-to-promise';
  10. import unzipper from 'unzipper';
  11. import loggerFactory from '~/utils/logger';
  12. import { resolveFromRoot } from '~/utils/project-dir-utils';
  13. import type {
  14. IGrowiPlugin, IGrowiPluginOrigin, IGrowiThemePluginMeta, IGrowiPluginMeta,
  15. } from '../../interfaces';
  16. import { GrowiPlugin } from '../models';
  17. import { GitHubUrl } from '../models/vo/github-url';
  18. const logger = loggerFactory('growi:plugins:plugin-utils');
  19. const pluginStoringPath = resolveFromRoot('tmp/plugins');
  20. const PLUGINS_STATIC_DIR = '/static/plugins'; // configured by express.static
  21. export type GrowiPluginResourceEntries = [installedPath: string, href: string][];
  22. function retrievePluginManifest(growiPlugin: IGrowiPlugin): ViteManifest {
  23. const manifestPath = resolveFromRoot(path.join('tmp/plugins', growiPlugin.installedPath, 'dist/manifest.json'));
  24. const manifestStr: string = readFileSync(manifestPath, 'utf-8');
  25. return JSON.parse(manifestStr);
  26. }
  27. type FindThemePluginResult = {
  28. growiPlugin: IGrowiPlugin,
  29. themeMetadata: GrowiThemeMetadata,
  30. themeHref: string,
  31. }
  32. export interface IGrowiPluginService {
  33. install(origin: IGrowiPluginOrigin): Promise<string>
  34. findThemePlugin(theme: string): Promise<FindThemePluginResult | null>
  35. retrieveAllPluginResourceEntries(): Promise<GrowiPluginResourceEntries>
  36. downloadNotExistPluginRepositories(): Promise<void>
  37. }
  38. export class GrowiPluginService implements IGrowiPluginService {
  39. /*
  40. * Downloading a non-existent repository to the file system
  41. */
  42. async downloadNotExistPluginRepositories(): Promise<void> {
  43. try {
  44. // find all growi plugin documents
  45. const growiPlugins = await GrowiPlugin.find({});
  46. // if not exists repository in file system, download latest plugin repository
  47. for await (const growiPlugin of growiPlugins) {
  48. const pluginPath = path.join(pluginStoringPath, growiPlugin.installedPath);
  49. const organizationName = path.join(pluginStoringPath, growiPlugin.organizationName);
  50. if (fs.existsSync(pluginPath)) {
  51. continue;
  52. }
  53. else {
  54. if (!fs.existsSync(organizationName)) {
  55. fs.mkdirSync(organizationName);
  56. }
  57. // TODO: imprv Document version and repository version possibly different.
  58. const ghUrl = new GitHubUrl(growiPlugin.origin.url, growiPlugin.origin.branchName);
  59. const { reposName, branchName, archiveUrl } = ghUrl;
  60. const zipFilePath = path.join(pluginStoringPath, `${branchName}.zip`);
  61. const unzippedPath = pluginStoringPath;
  62. const unzippedReposPath = path.join(pluginStoringPath, `${reposName}-${branchName}`);
  63. try {
  64. // download github repository to local file system
  65. await this.download(archiveUrl, zipFilePath);
  66. await this.unzip(zipFilePath, unzippedPath);
  67. fs.renameSync(unzippedReposPath, pluginPath);
  68. }
  69. catch (err) {
  70. // clean up, documents are not operated
  71. if (fs.existsSync(unzippedReposPath)) await fs.promises.rm(unzippedReposPath, { recursive: true });
  72. if (fs.existsSync(pluginPath)) await fs.promises.rm(pluginPath, { recursive: true });
  73. logger.error(err);
  74. }
  75. continue;
  76. }
  77. }
  78. }
  79. catch (err) {
  80. logger.error(err);
  81. }
  82. }
  83. /*
  84. * Install a plugin from URL and save it in the DB and file system.
  85. */
  86. async install(origin: IGrowiPluginOrigin): Promise<string> {
  87. const ghUrl = new GitHubUrl(origin.url, origin.ghBranch);
  88. const {
  89. organizationName, reposName, branchName, archiveUrl,
  90. } = ghUrl;
  91. const installedPath = `${organizationName}/${reposName}`;
  92. const zipFilePath = path.join(pluginStoringPath, `${branchName}.zip`);
  93. const unzippedPath = pluginStoringPath;
  94. const unzippedReposPath = path.join(pluginStoringPath, `${reposName}-${branchName}`);
  95. const temporaryReposPath = path.join(pluginStoringPath, reposName);
  96. const reposStoringPath = path.join(pluginStoringPath, `${installedPath}`);
  97. const organizationPath = path.join(pluginStoringPath, organizationName);
  98. let plugins: IGrowiPlugin<IGrowiPluginMeta>[];
  99. try {
  100. // download github repository to file system's temporary path
  101. await this.download(archiveUrl, zipFilePath);
  102. await this.unzip(zipFilePath, unzippedPath);
  103. fs.renameSync(unzippedReposPath, temporaryReposPath);
  104. // detect plugins
  105. plugins = await GrowiPluginService.detectPlugins(origin, organizationName, reposName);
  106. if (!fs.existsSync(organizationPath)) fs.mkdirSync(organizationPath);
  107. // remove the old repository from the storing path
  108. if (fs.existsSync(reposStoringPath)) await fs.promises.rm(reposStoringPath, { recursive: true });
  109. // move new repository from temporary path to storing path.
  110. fs.renameSync(temporaryReposPath, reposStoringPath);
  111. }
  112. catch (err) {
  113. // clean up
  114. if (fs.existsSync(zipFilePath)) await fs.promises.rm(zipFilePath);
  115. if (fs.existsSync(unzippedReposPath)) await fs.promises.rm(unzippedReposPath, { recursive: true });
  116. if (fs.existsSync(temporaryReposPath)) await fs.promises.rm(temporaryReposPath, { recursive: true });
  117. logger.error(err);
  118. throw err;
  119. }
  120. try {
  121. // delete plugin documents if these exist
  122. await this.deleteOldPluginDocument(installedPath);
  123. // save new plugins metadata
  124. await this.savePluginMetaData(plugins);
  125. return plugins[0].meta.name;
  126. }
  127. catch (err) {
  128. // clean up
  129. if (fs.existsSync(reposStoringPath)) await fs.promises.rm(reposStoringPath, { recursive: true });
  130. await this.deleteOldPluginDocument(installedPath);
  131. logger.error(err);
  132. throw err;
  133. }
  134. }
  135. private async deleteOldPluginDocument(path: string): Promise<void> {
  136. await GrowiPlugin.deleteMany({ installedPath: path });
  137. }
  138. // !! DO NOT USE WHERE NOT SSRF GUARDED !! -- 2022.12.26 ryoji-s
  139. private async download(requestUrl: string, filePath: string): Promise<void> {
  140. return new Promise<void>((resolve, rejects) => {
  141. axios({
  142. method: 'GET',
  143. url: requestUrl,
  144. responseType: 'stream',
  145. })
  146. .then((res) => {
  147. if (res.status === 200) {
  148. const file = fs.createWriteStream(filePath);
  149. res.data.pipe(file)
  150. .on('close', () => file.close())
  151. .on('finish', () => {
  152. return resolve();
  153. });
  154. }
  155. else {
  156. rejects(res.status);
  157. }
  158. }).catch((err) => {
  159. logger.error(err);
  160. // eslint-disable-next-line prefer-promise-reject-errors
  161. rejects('Filed to download file.');
  162. });
  163. });
  164. }
  165. private async unzip(zipFilePath: fs.PathLike, unzippedPath: fs.PathLike): Promise<void> {
  166. try {
  167. const stream = fs.createReadStream(zipFilePath);
  168. const unzipStream = stream.pipe(unzipper.Extract({ path: unzippedPath }));
  169. await streamToPromise(unzipStream);
  170. await fs.promises.rm(zipFilePath);
  171. }
  172. catch (err) {
  173. logger.error(err);
  174. throw new Error('Filed to unzip.');
  175. }
  176. }
  177. private async savePluginMetaData(plugins: IGrowiPlugin[]): Promise<void> {
  178. await GrowiPlugin.insertMany(plugins);
  179. }
  180. // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, max-len
  181. private static async detectPlugins(
  182. origin: IGrowiPluginOrigin, ghOrganizationName: string, ghReposName: string,
  183. opts?: {
  184. packageRootPath: string,
  185. parentPackageData: GrowiPluginPackageData,
  186. },
  187. ): Promise<IGrowiPlugin[]> {
  188. const packageRootPath = opts?.packageRootPath ?? path.resolve(pluginStoringPath, ghOrganizationName, ghReposName);
  189. // validate
  190. await validatePackageJson(packageRootPath);
  191. const packageData = importPackageJson(packageRootPath);
  192. const { growiPlugin } = packageData;
  193. const {
  194. name: packageName, description: packageDesc, author: packageAuthor,
  195. } = opts?.parentPackageData ?? packageData;
  196. if (growiPlugin == null) {
  197. throw new Error('This package does not include \'growiPlugin\' section.');
  198. }
  199. // detect sub plugins for monorepo
  200. if (growiPlugin.isMonorepo && growiPlugin.packages != null) {
  201. const plugins = await Promise.all(
  202. growiPlugin.packages.map(async(subPackagePath) => {
  203. return this.detectPlugins(origin, ghOrganizationName, ghReposName, {
  204. packageRootPath: path.join(packageRootPath, subPackagePath),
  205. parentPackageData: packageData,
  206. });
  207. }),
  208. );
  209. return plugins.flat();
  210. }
  211. const plugin = {
  212. isEnabled: true,
  213. installedPath: `${ghOrganizationName}/${ghReposName}`,
  214. organizationName: ghOrganizationName,
  215. origin,
  216. meta: {
  217. name: growiPlugin.name ?? packageName,
  218. desc: growiPlugin.desc ?? packageDesc,
  219. author: growiPlugin.author ?? packageAuthor,
  220. types: growiPlugin.types,
  221. },
  222. };
  223. // add theme metadata
  224. if (growiPlugin.types.includes(GrowiPluginType.Theme)) {
  225. (plugin as IGrowiPlugin<IGrowiThemePluginMeta>).meta = {
  226. ...plugin.meta,
  227. themes: growiPlugin.themes,
  228. };
  229. }
  230. logger.info('Plugin detected => ', plugin);
  231. return [plugin];
  232. }
  233. async listPlugins(): Promise<IGrowiPlugin[]> {
  234. return [];
  235. }
  236. /**
  237. * Delete plugin
  238. */
  239. async deletePlugin(pluginId: mongoose.Types.ObjectId): Promise<string> {
  240. const deleteFolder = (path: fs.PathLike): Promise<void> => {
  241. return fs.promises.rm(path, { recursive: true });
  242. };
  243. const growiPlugins = await GrowiPlugin.findById(pluginId);
  244. if (growiPlugins == null) {
  245. throw new Error('No plugin found for this ID.');
  246. }
  247. try {
  248. const growiPluginsPath = path.join(pluginStoringPath, growiPlugins.installedPath);
  249. await deleteFolder(growiPluginsPath);
  250. }
  251. catch (err) {
  252. logger.error(err);
  253. throw new Error('Filed to delete plugin repository.');
  254. }
  255. try {
  256. await GrowiPlugin.deleteOne({ _id: pluginId });
  257. }
  258. catch (err) {
  259. logger.error(err);
  260. throw new Error('Filed to delete plugin from GrowiPlugin documents.');
  261. }
  262. return growiPlugins.meta.name;
  263. }
  264. async findThemePlugin(theme: string): Promise<FindThemePluginResult | null> {
  265. let matchedPlugin: IGrowiPlugin | undefined;
  266. let matchedThemeMetadata: GrowiThemeMetadata | undefined;
  267. try {
  268. // retrieve plugin manifests
  269. const growiPlugins = await GrowiPlugin.findEnabledPluginsIncludingAnyTypes([GrowiPluginType.Theme]) as IGrowiPlugin<IGrowiThemePluginMeta>[];
  270. growiPlugins
  271. .forEach(async(growiPlugin) => {
  272. const themeMetadatas = growiPlugin.meta.themes;
  273. const themeMetadata = themeMetadatas.find(t => t.name === theme);
  274. // found
  275. if (themeMetadata != null) {
  276. matchedPlugin = growiPlugin;
  277. matchedThemeMetadata = themeMetadata;
  278. }
  279. });
  280. }
  281. catch (e) {
  282. logger.error(`Could not find the theme '${theme}' from GrowiPlugin documents.`, e);
  283. }
  284. if (matchedPlugin == null || matchedThemeMetadata == null) {
  285. return null;
  286. }
  287. let themeHref;
  288. try {
  289. const manifest = retrievePluginManifest(matchedPlugin);
  290. themeHref = `${PLUGINS_STATIC_DIR}/${matchedPlugin.installedPath}/dist/${manifest[matchedThemeMetadata.manifestKey].file}`;
  291. }
  292. catch (e) {
  293. logger.error(`Could not read manifest file for the theme '${theme}'`, e);
  294. }
  295. return { growiPlugin: matchedPlugin, themeMetadata: matchedThemeMetadata, themeHref };
  296. }
  297. async retrieveAllPluginResourceEntries(): Promise<GrowiPluginResourceEntries> {
  298. const entries: GrowiPluginResourceEntries = [];
  299. try {
  300. const growiPlugins = await GrowiPlugin.findEnabledPlugins();
  301. growiPlugins.forEach(async(growiPlugin) => {
  302. try {
  303. const { types } = growiPlugin.meta;
  304. const manifest = await retrievePluginManifest(growiPlugin);
  305. // add script
  306. if (types.includes(GrowiPluginType.Script)) {
  307. const href = `${PLUGINS_STATIC_DIR}/${growiPlugin.installedPath}/dist/${manifest['client-entry.tsx'].file}`;
  308. entries.push([growiPlugin.installedPath, href]);
  309. }
  310. // add link
  311. if (types.includes(GrowiPluginType.Script) || types.includes(GrowiPluginType.Style)) {
  312. const href = `${PLUGINS_STATIC_DIR}/${growiPlugin.installedPath}/dist/${manifest['client-entry.tsx'].css}`;
  313. entries.push([growiPlugin.installedPath, href]);
  314. }
  315. }
  316. catch (e) {
  317. logger.warn(e);
  318. }
  319. });
  320. }
  321. catch (e) {
  322. logger.error('Could not retrieve GrowiPlugin documents.', e);
  323. }
  324. return entries;
  325. }
  326. }
  327. export const growiPluginService = new GrowiPluginService();