growi-plugin.ts 13 KB

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