@ublitzjs/asyncapi 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/index.cjs ADDED
@@ -0,0 +1,79 @@
1
+ var { DeclarativeResponse } = require("@ublitzjs/core"),
2
+ { createWriteStream } = require("node:fs"),
3
+ { Eta } = require("eta"),
4
+ { exit } = require("node:process"),
5
+ { basicSendFile } = require("@ublitzjs/static"),
6
+ { stat } = require("node:fs/promises")
7
+ class AsyncApiBuilder{
8
+ constructor(props){
9
+ this.data = props
10
+ this.data.channels ??= {};
11
+ }
12
+ addChannel(channel, obj){
13
+ this.data.channels[channel] = obj
14
+ }
15
+ getSpecAsJson(){
16
+ return JSON.stringify(this.data);
17
+ }
18
+ }
19
+ function serverExtension(obj){
20
+ return {
21
+ asyncApiBuilder: new AsyncApiBuilder(obj),
22
+ async buildAsyncApi(filePath, exitFromNodejs = false){
23
+ var spec = this.asyncApiBuilder.getSpecAsJson();
24
+ var writeStream = createWriteStream(filePath);
25
+ var offset = 0;
26
+ var chunk = 64 * 1024 * 1024;
27
+ while (spec[offset]) {
28
+ var ok = writeStream.write(spec.slice(offset, (offset += chunk)));
29
+ if (!ok)
30
+ await new Promise((resolve) => writeStream.once("drain", resolve));
31
+ }
32
+ return new Promise((resolve) => {
33
+ writeStream.end(() => {
34
+ if (exitFromNodejs) exit(0);
35
+ resolve(spec.length);
36
+ });
37
+ });
38
+ },
39
+ async serveAsyncApi(prefix, opts){
40
+ var templator = new Eta({ views: opts?.uiPath || "node_modules/@ublitzjs/asyncapi" });
41
+ var asyncapiHTML = new DeclarativeResponse().end(
42
+ templator.render("index", { title: this.asyncApiBuilder.data.info.title })
43
+ );
44
+ this.get(prefix + "/", asyncapiHTML)
45
+ if(!opts?.path) {
46
+ this.get(
47
+ prefix + "/asyncapi.json",
48
+ new DeclarativeResponse().writeHeader("Content-Type", "application/json").end(this.asyncApiBuilder.getSpecAsJson())
49
+ )
50
+ } else this.get(prefix + "/asyncapi.json", basicSendFile({
51
+ maxSize: await stat(opts?.path || "asyncapi.json").then(res => res.size),
52
+ path: opts?.path || "asyncapi.json",
53
+ contentType: "application/json"
54
+ }));
55
+ delete this.asyncApiBuilder;
56
+ }
57
+ }
58
+ }
59
+ function routePlugin(route, server
60
+ ) {
61
+ if (route.asyncapi) {
62
+ server.asyncApiBuilder.addChannel(
63
+ route.path,
64
+ route.asyncapi
65
+ );
66
+ delete route.asyncapi
67
+ }
68
+ }
69
+ function RouterPlugin(methods){
70
+ if(!methods.includes("ws")) return;
71
+ var ws = this.paths[this._currentPath]?.ws;
72
+ if(!ws || !ws.asyncapi)return;
73
+ (this.server).asyncApiBuilder.addChannel(
74
+ this.prefixedPath,
75
+ ws.asyncapi
76
+ )
77
+ delete ws.asyncapi;
78
+ }
79
+ module.exports = { RouterPlugin, routePlugin, serverExtension, }
package/index.d.ts ADDED
@@ -0,0 +1,20 @@
1
+ import type { ChannelObject, AsyncAPIObject } from "asyncapi-types";
2
+ import type { HttpMethods, routeFNOpts, Server } from "@ublitzjs/core";
3
+ import type { ExtendedRouter } from "@ublitzjs/router";
4
+ interface AsyncApiBuilder {
5
+ agetSpecAsJson(): string
6
+ addChannel(channel: string, obj: ChannelObject): void
7
+ }
8
+ export function serverExtension(obj: AsyncAPIObject): {
9
+ readonly asyncApiBuilder: AsyncApiBuilder;
10
+ readonly buildAsyncApi: (filePath: string, exitFromNodejs?: boolean) => Promise<number>;
11
+ readonly serveAsyncApi: (prefix: string, opts?: {
12
+ path?: string;
13
+ uiPath?: string;
14
+ }) => Promise<void>;
15
+ };
16
+ export type methodAddOns = { asyncapi?: ChannelObject };
17
+ export function routePlugin<method extends HttpMethods>(route: routeFNOpts<method> & methodAddOns, server: Server & {
18
+ asyncApiBuilder: AsyncApiBuilder;
19
+ }): void
20
+ export function RouterPlugin(this: ExtendedRouter<any>, methods: string[]): void
package/index.eta ADDED
@@ -0,0 +1,23 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <title><%= it.title %></title>
6
+ <!-- Polyfills (if needed) -->
7
+ <script src="https://unpkg.com/@webcomponents/webcomponentsjs@2.5.0/webcomponents-bundle.js"></script>
8
+
9
+ <!-- AsyncAPI Web Component -->
10
+ <script
11
+ src="https://unpkg.com/@asyncapi/web-component@1.0.0-next.39/lib/asyncapi-web-component.js"
12
+ defer
13
+ ></script>
14
+ </head>
15
+ <body>
16
+ <asyncapi-component
17
+ schemaUrl="./asyncapi.json"
18
+ schemaFetchOptions='{"method":"GET"}'
19
+ config='{"show":{"sidebar":true}}'
20
+ cssImportPath="https://unpkg.com/@asyncapi/react-component@1.0.0-next.39/styles/default.min.css">
21
+ </asyncapi-component>
22
+ </body>
23
+ </html>
package/index.mjs ADDED
@@ -0,0 +1,80 @@
1
+ import { DeclarativeResponse } from "@ublitzjs/core";
2
+ import { createWriteStream } from "node:fs";
3
+ import {Eta} from "eta"
4
+ import { exit } from "node:process";
5
+ import { basicSendFile } from "@ublitzjs/static";
6
+ import { stat } from "node:fs/promises";
7
+
8
+ class AsyncApiBuilder{
9
+ constructor(props){
10
+ this.data = props
11
+ this.data.channels ??= {};
12
+ }
13
+ addChannel(channel, obj){
14
+ this.data.channels[channel] = obj
15
+ }
16
+ getSpecAsJson(){
17
+ return JSON.stringify(this.data);
18
+ }
19
+ }
20
+ function serverExtension(obj){
21
+ return {
22
+ async buildAsyncApi(filePath, exitFromNodejs = false){
23
+ var spec = this.asyncApiBuilder.getSpecAsJson();
24
+ var writeStream = createWriteStream(filePath);
25
+ var offset = 0;
26
+ var chunk = 64 * 1024 * 1024;
27
+ while (spec[offset]) {
28
+ var ok = writeStream.write(spec.slice(offset, (offset += chunk)));
29
+ if (!ok)
30
+ await new Promise((resolve) => writeStream.once("drain", resolve));
31
+ }
32
+ return new Promise((resolve) => {
33
+ writeStream.end(() => {
34
+ if (exitFromNodejs) exit(0);
35
+ resolve(spec.length);
36
+ });
37
+ });
38
+ },
39
+ asyncApiBuilder: new AsyncApiBuilder(obj),
40
+ async serveAsyncApi(prefix, opts){
41
+ var templator = new Eta({ views: opts?.uiPath || "node_modules/@ublitzjs/asyncapi" });
42
+ var asyncapiHTML = new DeclarativeResponse().end(
43
+ templator.render("index", { title: this.asyncApiBuilder.data.info.title })
44
+ );
45
+ this.get(prefix + "/", asyncapiHTML)
46
+ if(!opts?.path) {
47
+ this.get(
48
+ prefix + "/asyncapi.json",
49
+ new DeclarativeResponse().writeHeader("Content-Type", "application/json").end(this.asyncApiBuilder.getSpecAsJson())
50
+ )
51
+ } else this.get(prefix + "/asyncapi.json", basicSendFile({
52
+ maxSize: await stat(opts?.path || "asyncapi.json").then(res => res.size),
53
+ path: opts?.path || "asyncapi.json",
54
+ contentType: "application/json"
55
+ }));
56
+ delete this.asyncApiBuilder;
57
+ }
58
+ }
59
+ }
60
+ function routePlugin(route, server
61
+ ) {
62
+ if (route.asyncapi) {
63
+ server.asyncApiBuilder.addChannel(
64
+ route.path,
65
+ route.asyncapi
66
+ );
67
+ delete route.asyncapi
68
+ }
69
+ }
70
+ function RouterPlugin(methods){
71
+ if(!methods.includes("ws")) return;
72
+ var ws = this.paths[this._currentPath]?.ws;
73
+ if(!ws || !ws.asyncapi)return;
74
+ (this.server).asyncApiBuilder.addChannel(
75
+ this.prefixedPath,
76
+ ws.asyncapi
77
+ )
78
+ delete ws.asyncapi;
79
+ }
80
+ export { RouterPlugin, routePlugin, serverExtension, }
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@ublitzjs/asyncapi",
3
+ "version": "0.0.1",
4
+ "dependencies": {
5
+ "@ublitzjs/core": "^0.1.0",
6
+ "@ublitzjs/static": "^0.1.2",
7
+ "asyncapi-types": "^2",
8
+ "eta": "^3.5.0"
9
+ },
10
+ "devDependencies": {
11
+ "@ublitzjs/router": "^0.1.0",
12
+ "@types/node": "^24.3.1"
13
+ },
14
+ "publishConfig": {
15
+ "access": "public"
16
+ },
17
+ "exports": {
18
+ "types": "./index.d.ts",
19
+ "import": "./index.mjs",
20
+ "require": "./index.cjs"
21
+ },
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "https://github.com/ublitzjs/asyncapi.git"
25
+ },
26
+ "bugs": {
27
+ "url": "https://github.com/ublitzjs/asyncapi/issues",
28
+ "email": "diril656@gmail.com"
29
+ },
30
+ "homepage": "https://github.com/ublitzjs/asyncapi#readme",
31
+ "keywords": [
32
+ "ublitzjs",
33
+ "websockets",
34
+ "uWebSockets.js",
35
+ "asyncapi",
36
+ "documentation",
37
+ "docs",
38
+ "typescript-first",
39
+ "development"
40
+ ]
41
+ }