koishi-plugin-vercel-satori-png-service 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.d.ts ADDED
@@ -0,0 +1,26 @@
1
+ import { Context, Schema, Service } from "koishi";
2
+ import { ReactElement } from "react";
3
+ import { unstable_createNodejsStream } from "@vercel/og";
4
+ import { Readable } from "stream";
5
+ export type ImageOptions = Parameters<typeof unstable_createNodejsStream>[1];
6
+ declare module "koishi" {
7
+ interface Context {
8
+ vercelSatoriPngService: VercelSatoriPngService;
9
+ }
10
+ }
11
+ declare class VercelSatoriPngService extends Service {
12
+ private _ctx;
13
+ private _config;
14
+ constructor(ctx: Context, config: VercelSatoriPngService.Config);
15
+ jsxToReactElement(jsxCode: string, data?: Record<any, any>): Promise<ReactElement<any, any>>;
16
+ jsxToPng(jsxCode: string, options: ImageOptions, data?: Record<any, any>): Promise<Readable>;
17
+ htmlToReactElement(htmlCode: string): ReactElement<any, any>;
18
+ htmlToPng(htmlCode: string, options: ImageOptions): Promise<Readable>;
19
+ }
20
+ declare namespace VercelSatoriPngService {
21
+ const usage: string;
22
+ interface Config {
23
+ }
24
+ const Config: Schema<Config>;
25
+ }
26
+ export default VercelSatoriPngService;
package/lib/index.js ADDED
@@ -0,0 +1,88 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var src_exports = {};
32
+ __export(src_exports, {
33
+ default: () => src_default
34
+ });
35
+ module.exports = __toCommonJS(src_exports);
36
+ var import_koishi = require("koishi");
37
+ var import_sucrase = __toESM(require("sucrase"));
38
+ var import_react = __toESM(require("react"));
39
+ var import_html_react_parser = __toESM(require("html-react-parser"));
40
+ var import_og = require("@vercel/og");
41
+ var serviceName = "vercelSatoriPngService";
42
+ var AsyncFunction = (async () => 0).constructor;
43
+ var VercelSatoriPngService = class extends import_koishi.Service {
44
+ static {
45
+ __name(this, "VercelSatoriPngService");
46
+ }
47
+ _ctx;
48
+ _config;
49
+ constructor(ctx, config) {
50
+ super(ctx, serviceName);
51
+ this._ctx = ctx;
52
+ this._config = config;
53
+ }
54
+ async jsxToReactElement(jsxCode, data) {
55
+ const hCode = import_sucrase.default.transform(jsxCode, {
56
+ transforms: ["jsx"],
57
+ jsxRuntime: "classic",
58
+ production: true
59
+ }).code;
60
+ const fn = AsyncFunction(
61
+ "React",
62
+ "_args_623601",
63
+ "with (_args_623601) {\nreturn " + hCode.replace(/^\s+/, "") + "\n}"
64
+ );
65
+ let res = await fn(import_react.default, data || {});
66
+ let i = 0;
67
+ while (typeof res === "function" && i++ < 999) {
68
+ res = await res();
69
+ }
70
+ return res;
71
+ }
72
+ async jsxToPng(jsxCode, options, data) {
73
+ const reactElement = await this.jsxToReactElement(jsxCode, data);
74
+ return (0, import_og.unstable_createNodejsStream)(reactElement, options);
75
+ }
76
+ htmlToReactElement(htmlCode) {
77
+ return (0, import_html_react_parser.default)(htmlCode);
78
+ }
79
+ htmlToPng(htmlCode, options) {
80
+ const reactElement = this.htmlToReactElement(htmlCode);
81
+ return (0, import_og.unstable_createNodejsStream)(reactElement, options);
82
+ }
83
+ };
84
+ ((VercelSatoriPngService2) => {
85
+ VercelSatoriPngService2.usage = 'html to ReactElement <a target="_blank" href="https://www.npmjs.com/package/html-react-parser">html-react-parser</a> \njsx to ReactElement <a target="_blank" href="https://www.npmjs.com/package/sucrase">sucrase</a> \nReactElement to png <a target="_blank" href="https://www.npmjs.com/package/@vercel/og">@vercel/og</a><a target="_blank" href="https://og-playground.vercel.app/">og-playground</a> \n<a target="_blank" href="https://github.com/vercel/satori#overview">vercel/satori</a> \n';
86
+ VercelSatoriPngService2.Config = import_koishi.Schema.object({});
87
+ })(VercelSatoriPngService || (VercelSatoriPngService = {}));
88
+ var src_default = VercelSatoriPngService;
package/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "name": "koishi-plugin-vercel-satori-png-service",
3
+ "description": "Use Vercel Satori and Resvg.js to convert html to png",
4
+ "version": "0.0.1",
5
+ "main": "lib/index.js",
6
+ "typings": "lib/index.d.ts",
7
+ "scripts": {
8
+ "build": "cd ../../ && yarn build vercel-satori-png-service",
9
+ "pub": "cd ../../ && yarn pub vercel-satori-png-service",
10
+ "fastPub": "cd ../../ && yarn bump -3 vercel-satori-png-service && yarn build vercel-satori-png-service && yarn pub vercel-satori-png-service",
11
+ "test": "node ./test/test.js"
12
+ },
13
+ "files": [
14
+ "lib",
15
+ "dist"
16
+ ],
17
+ "contributors": [
18
+ "pgnqukezrdxmhjso"
19
+ ],
20
+ "homepage": "https://github.com/pgnqukezrdxmhjso/koishi-plugin-vercel-satori-png-service",
21
+ "bugs": {
22
+ "url": "https://github.com/pgnqukezrdxmhjso/koishi-plugin-vercel-satori-png-service/issues"
23
+ },
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "git+https://github.com/pgnqukezrdxmhjso/koishi-plugin-vercel-satori-png-service.git"
27
+ },
28
+ "license": "MIT",
29
+ "keywords": [
30
+ "chatbot",
31
+ "koishi",
32
+ "plugin",
33
+ "render",
34
+ "html to png",
35
+ "jsx to png"
36
+ ],
37
+ "peerDependencies": {
38
+ "koishi": "^4.18.3"
39
+ },
40
+ "dependencies": {
41
+ "@vercel/og": "^0.6.4",
42
+ "html-react-parser": "^5.2.2",
43
+ "react": "^19.0.0",
44
+ "sucrase": "^3.35.0"
45
+ },
46
+ "devDependencies": {
47
+ "@types/react": "^19"
48
+ },
49
+ "koishi": {
50
+ "service": {
51
+ "implements": [
52
+ "vercelSatoriPngService"
53
+ ]
54
+ }
55
+ }
56
+ }
package/readme.md ADDED
@@ -0,0 +1,13 @@
1
+ # koishi-plugin-vercel-satori-png-service
2
+
3
+ [![npm](https://img.shields.io/npm/v/koishi-plugin-vercel-satori-png-service?style=flat-square)](https://www.npmjs.com/package/koishi-plugin-vercel-satori-png-service)
4
+
5
+ Use Vercel Satori and Resvg.js to convert html to png
6
+
7
+ ---
8
+
9
+ html to ReactElement [html-react-parser](https://www.npmjs.com/package/html-react-parser)
10
+ jsx to ReactElement [sucrase](https://www.npmjs.com/package/sucrase)
11
+ ReactElement to png [@vercel/og](https://www.npmjs.com/package/@vercel/og)
12
+ [og-playground](https://og-playground.vercel.app/)
13
+ [vercel/satori](https://github.com/vercel/satori#overview)