kozz-module-maker 0.1.1 → 0.1.2

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.
Files changed (188) hide show
  1. package/dist/Instance/Basic/index.d.ts +40 -0
  2. package/dist/Instance/Basic/index.js +43 -0
  3. package/dist/Instance/Basic/index.js.map +1 -0
  4. package/dist/Instance/BasicController/index.d.ts +40 -0
  5. package/dist/Instance/BasicController/index.js +43 -0
  6. package/dist/Instance/BasicController/index.js.map +1 -0
  7. package/dist/Instance/CommandHandler/index.d.ts +20 -0
  8. package/dist/Instance/CommandHandler/index.js +26 -0
  9. package/dist/Instance/CommandHandler/index.js.map +1 -0
  10. package/dist/Instance/Common/ResourceMap/index.d.ts +12 -0
  11. package/dist/Instance/Common/ResourceMap/index.js +19 -0
  12. package/dist/Instance/Common/ResourceMap/index.js.map +1 -0
  13. package/dist/Instance/Common/UseFns/index.d.ts +10 -0
  14. package/dist/Instance/Common/UseFns/index.js +16 -0
  15. package/dist/Instance/Common/UseFns/index.js.map +1 -0
  16. package/dist/Instance/Common/index.js +10 -0
  17. package/dist/Instance/Common/index.js.map +1 -0
  18. package/dist/Instance/Common/onEvent/index.d.ts +8 -0
  19. package/dist/Instance/Common/onEvent/index.js +87 -0
  20. package/dist/Instance/Common/onEvent/index.js.map +1 -0
  21. package/dist/Instance/GeneralHandler/index.d.ts +19 -0
  22. package/dist/Instance/GeneralHandler/index.js +49 -0
  23. package/dist/Instance/GeneralHandler/index.js.map +1 -0
  24. package/dist/Instance/GeneralModule/index.d.ts +53 -0
  25. package/dist/Instance/GeneralModule/index.js +55 -0
  26. package/dist/Instance/GeneralModule/index.js.map +1 -0
  27. package/dist/Instance/Handler/index.d.ts +23 -0
  28. package/dist/Instance/Handler/index.js +26 -0
  29. package/dist/Instance/Handler/index.js.map +1 -0
  30. package/dist/Instance/Proxy/index.d.ts +22 -0
  31. package/dist/Instance/Proxy/index.js +36 -0
  32. package/dist/Instance/Proxy/index.js.map +1 -0
  33. package/dist/Instance/index.d.ts +14 -0
  34. package/dist/Instance/index.js +20 -0
  35. package/dist/Instance/index.js.map +1 -0
  36. package/dist/Message/FromTemplate/index.d.ts +33 -0
  37. package/dist/Message/FromTemplate/index.js +62 -0
  38. package/dist/Message/FromTemplate/index.js.map +1 -0
  39. package/dist/Message/PayloadCreation/Media.d.ts +7 -0
  40. package/dist/Message/PayloadCreation/Media.js +44 -0
  41. package/dist/Message/PayloadCreation/Media.js.map +1 -0
  42. package/dist/Message/PayloadCreation/React.d.ts +3 -0
  43. package/dist/Message/PayloadCreation/React.js +13 -0
  44. package/dist/Message/PayloadCreation/React.js.map +1 -0
  45. package/dist/Message/PayloadCreation/Reply.d.ts +5 -0
  46. package/dist/Message/PayloadCreation/Reply.js +47 -0
  47. package/dist/Message/PayloadCreation/Reply.js.map +1 -0
  48. package/dist/Message/PayloadCreation/ReplyWithSticker.d.ts +0 -0
  49. package/dist/Message/PayloadCreation/ReplyWithSticker.js +1 -0
  50. package/dist/Message/PayloadCreation/ReplyWithText.d.ts +2 -0
  51. package/dist/Message/PayloadCreation/ReplyWithText.js +15 -0
  52. package/dist/Message/PayloadCreation/index.js +18 -0
  53. package/dist/Message/PayloadCreation/index.js.map +1 -0
  54. package/dist/Message/PayloadCreation/sendMessage.d.ts +2 -0
  55. package/dist/Message/PayloadCreation/sendMessage.js +24 -0
  56. package/dist/Message/PayloadCreation/sendMessage.js.map +1 -0
  57. package/dist/Message/ProxiedMessage/index.d.ts +42 -0
  58. package/dist/Message/ProxiedMessage/index.js +15 -0
  59. package/dist/Message/ProxiedMessage/index.js.map +1 -0
  60. package/dist/Message/RoutineCreation/AskResource/AskResourceApi/index.d.ts +14 -0
  61. package/dist/Message/RoutineCreation/AskResource/AskResourceApi/index.js +28 -0
  62. package/dist/Message/RoutineCreation/AskResource/AskResourceApi/index.js.map +1 -0
  63. package/dist/Message/RoutineCreation/AskResource/index.d.ts +16 -0
  64. package/dist/Message/RoutineCreation/AskResource/index.js +60 -0
  65. package/dist/Message/RoutineCreation/AskResource/index.js.map +1 -0
  66. package/dist/Message/RoutineCreation/Reply/WithMedia.d.ts +11 -0
  67. package/dist/Message/RoutineCreation/Reply/WithMedia.js +34 -0
  68. package/dist/Message/RoutineCreation/Reply/WithMedia.js.map +1 -0
  69. package/dist/Message/RoutineCreation/Reply/WithSticke.d.ts +3 -0
  70. package/dist/Message/RoutineCreation/Reply/WithSticke.js +9 -0
  71. package/dist/Message/RoutineCreation/Reply/WithSticke.js.map +1 -0
  72. package/dist/Message/RoutineCreation/Reply/WithSticker.d.ts +3 -0
  73. package/dist/Message/RoutineCreation/Reply/WithSticker.js +9 -0
  74. package/dist/Message/RoutineCreation/Reply/WithSticker.js.map +1 -0
  75. package/dist/Message/RoutineCreation/Reply/WithTemplate.d.ts +3 -0
  76. package/dist/Message/RoutineCreation/Reply/WithTemplate.js +20 -0
  77. package/dist/Message/RoutineCreation/Reply/WithTemplate.js.map +1 -0
  78. package/dist/Message/RoutineCreation/Reply/index.js +10 -0
  79. package/dist/Message/RoutineCreation/Reply/index.js.map +1 -0
  80. package/dist/Message/RoutineCreation/SendMessage/index.d.ts +10 -0
  81. package/dist/Message/RoutineCreation/SendMessage/index.js +25 -0
  82. package/dist/Message/RoutineCreation/SendMessage/index.js.map +1 -0
  83. package/dist/Message/RoutineCreation/reply.d.ts +15 -0
  84. package/dist/Message/RoutineCreation/reply.js +18 -0
  85. package/dist/Message/RoutineCreation/reply.js.map +1 -0
  86. package/dist/Message/index.d.ts +42 -0
  87. package/dist/Message/index.js +46 -0
  88. package/dist/Message/index.js.map +1 -0
  89. package/dist/Message/kozz-md.parser/index.d.ts +41 -0
  90. package/dist/Message/kozz-md.parser/index.js +122 -0
  91. package/dist/Message/kozz-md.parser/index.js.map +1 -0
  92. package/dist/Message/zap-md.parser/index.d.ts +1 -0
  93. package/dist/Message/zap-md.parser/index.js +2 -0
  94. package/dist/Schema/index.d.ts +37 -0
  95. package/dist/Schema/index.js +21 -0
  96. package/dist/Schema/index.js.map +1 -0
  97. package/dist/Socket/Events/Command.d.ts +3 -0
  98. package/dist/Socket/Events/Command.js +19 -0
  99. package/dist/Socket/Events/Command.js.map +1 -0
  100. package/dist/Socket/Events/Emit/ForwardEvent.d.ts +6 -0
  101. package/dist/Socket/Events/Emit/ForwardEvent.js +21 -0
  102. package/dist/Socket/Events/Emit/ForwardEvent.js.map +1 -0
  103. package/dist/Socket/Events/Emit/Introduction.d.ts +10 -0
  104. package/dist/Socket/Events/Emit/Introduction.js +21 -0
  105. package/dist/Socket/Events/Emit/Introduction.js.map +1 -0
  106. package/dist/Socket/Events/Emit/ProxyRequest.d.ts +0 -0
  107. package/dist/Socket/Events/Emit/ProxyRequest.js +2 -0
  108. package/dist/Socket/Events/Emit/ProxyRequest.js.map +1 -0
  109. package/dist/Socket/Events/Emit/RequestProxy.d.ts +18 -0
  110. package/dist/Socket/Events/Emit/RequestProxy.js +22 -0
  111. package/dist/Socket/Events/Emit/RequestProxy.js.map +1 -0
  112. package/dist/Socket/Events/Emit/RevokeProxy.d.ts +8 -0
  113. package/dist/Socket/Events/Emit/RevokeProxy.js +16 -0
  114. package/dist/Socket/Events/Emit/RevokeProxy.js.map +1 -0
  115. package/dist/Socket/Events/Handle/AskResource.d.ts +8 -0
  116. package/dist/Socket/Events/Handle/AskResource.js +36 -0
  117. package/dist/Socket/Events/Handle/AskResource.js.map +1 -0
  118. package/dist/Socket/Events/Handle/Command.d.ts +14 -0
  119. package/dist/Socket/Events/Handle/Command.js +48 -0
  120. package/dist/Socket/Events/Handle/Command.js.map +1 -0
  121. package/dist/Socket/Events/Handle/ProxiedMessage.d.ts +10 -0
  122. package/dist/Socket/Events/Handle/ProxiedMessage.js +18 -0
  123. package/dist/Socket/Events/Handle/ProxiedMessage.js.map +1 -0
  124. package/dist/Socket/Events/Introduction.d.ts +3 -0
  125. package/dist/Socket/Events/Introduction.js +15 -0
  126. package/dist/Socket/Events/Introduction.js.map +1 -0
  127. package/dist/Socket/Events/ProxiedMessage.d.ts +2 -0
  128. package/dist/Socket/Events/ProxiedMessage.js +10 -0
  129. package/dist/Socket/Events/ProxiedMessage.js.map +1 -0
  130. package/dist/Socket/Messages/Command.d.ts +2 -0
  131. package/dist/Socket/Messages/Command.js +6 -0
  132. package/dist/Socket/Messages/Command.js.map +1 -0
  133. package/dist/Socket/Messages/Introduction.d.ts +3 -0
  134. package/dist/Socket/Messages/Introduction.js +15 -0
  135. package/dist/Socket/Messages/Introduction.js.map +1 -0
  136. package/dist/Socket/index.d.ts +6 -0
  137. package/dist/Socket/index.js +19 -0
  138. package/dist/Socket/index.js.map +1 -0
  139. package/dist/Validator/index.d.ts +16 -0
  140. package/dist/Validator/index.js +55 -0
  141. package/dist/Validator/index.js.map +1 -0
  142. package/dist/debugIndex.d.ts +1 -0
  143. package/dist/debugIndex.js +21 -0
  144. package/dist/debugIndex.js.map +1 -0
  145. package/dist/index.js +19 -0
  146. package/dist/index.js.map +1 -0
  147. package/dist/util/index.d.ts +25 -0
  148. package/dist/util/index.js +81 -0
  149. package/dist/util/index.js.map +1 -0
  150. package/dist/util.d.ts +8 -0
  151. package/dist/util.js +36 -0
  152. package/dist/util.js.map +1 -0
  153. package/package.json +1 -1
  154. package/src/Instance/Common/ResourceMap/index.ts +0 -26
  155. package/src/Instance/Common/UseFns/index.ts +0 -22
  156. package/src/Instance/Common/onEvent/index.ts +0 -105
  157. package/src/Instance/GeneralModule/index.ts +0 -98
  158. package/src/Message/FromTemplate/index.ts +0 -74
  159. package/src/Message/PayloadCreation/Media.ts +0 -62
  160. package/src/Message/PayloadCreation/React.ts +0 -16
  161. package/src/Message/PayloadCreation/Reply.ts +0 -63
  162. package/src/Message/PayloadCreation/sendMessage.ts +0 -27
  163. package/src/Message/ProxiedMessage/index.ts +0 -27
  164. package/src/Message/RoutineCreation/AskResource/AskResourceApi/index.ts +0 -43
  165. package/src/Message/RoutineCreation/AskResource/index.ts +0 -85
  166. package/src/Message/RoutineCreation/Reply/WithMedia.ts +0 -63
  167. package/src/Message/RoutineCreation/Reply/WithSticker.ts +0 -9
  168. package/src/Message/RoutineCreation/Reply/WithTemplate.ts +0 -28
  169. package/src/Message/RoutineCreation/SendMessage/index.ts +0 -64
  170. package/src/Message/RoutineCreation/reply.ts +0 -23
  171. package/src/Message/index.ts +0 -44
  172. package/src/Message/kozz-md.parser/index.ts +0 -138
  173. package/src/Schema/index.ts +0 -102
  174. package/src/Socket/Events/Emit/ForwardEvent.ts +0 -22
  175. package/src/Socket/Events/Emit/Introduction.ts +0 -28
  176. package/src/Socket/Events/Emit/RequestProxy.ts +0 -38
  177. package/src/Socket/Events/Emit/RevokeProxy.ts +0 -15
  178. package/src/Socket/Events/Handle/AskResource.ts +0 -37
  179. package/src/Socket/Events/Handle/Command.ts +0 -68
  180. package/src/Socket/Events/Handle/ProxiedMessage.ts +0 -28
  181. package/src/Socket/index.ts +0 -40
  182. package/src/Validator/index.ts +0 -81
  183. package/src/messages.kozz.md +0 -52
  184. package/src/util/index.ts +0 -89
  185. /package/{src/Instance/Common/index.ts → dist/Instance/Common/index.d.ts} +0 -0
  186. /package/{src/Message/PayloadCreation/index.ts → dist/Message/PayloadCreation/index.d.ts} +0 -0
  187. /package/{src/Message/RoutineCreation/Reply/index.ts → dist/Message/RoutineCreation/Reply/index.d.ts} +0 -0
  188. /package/{src/index.ts → dist/index.d.ts} +0 -0
@@ -1,15 +0,0 @@
1
- import { ProxyRevokePayload, Source } from 'kozz-types';
2
- import { Socket } from 'socket.io-client';
3
-
4
- /**
5
- * Request the revoking of a proxy
6
- * @param socket
7
- * @param source
8
- */
9
- export const revokeProxy = (socket: Socket, source: Source) => {
10
- const payload: ProxyRevokePayload = {
11
- source: source,
12
- };
13
-
14
- socket.emit('revoke_proxy', payload);
15
- };
@@ -1,37 +0,0 @@
1
- import { Socket } from 'socket.io-client';
2
- import { ResourceMap } from '../../../Instance/Common/ResourceMap';
3
- import { AskResourcePayload, ProvideResourcePayload } from 'kozz-types/dist';
4
-
5
- /**
6
- * Provide the resource asked
7
- * @param socket
8
- * @param resourceMap
9
- */
10
- export const onAskResource = (socket: Socket, resourceMap: ResourceMap) => {
11
- socket.on('ask_resource', (payload: AskResourcePayload) => {
12
- const resourceGetter = resourceMap[payload.request.resource];
13
- if (!resourceGetter)
14
- return socket.emit('reply_resource', {
15
- ...payload,
16
- });
17
-
18
- promisify(resourceGetter(payload.request.data)).then(resource => {
19
- const responsePayload: ProvideResourcePayload = {
20
- ...payload,
21
- response: resource,
22
- };
23
-
24
- socket.emit('reply_resource', responsePayload);
25
- });
26
- });
27
- };
28
-
29
- const promisify = (resource: unknown) => {
30
- return new Promise(resolve => {
31
- if (resource instanceof Promise) {
32
- resource.then(resp => resolve(resp));
33
- } else {
34
- resolve(resource);
35
- }
36
- });
37
- };
@@ -1,68 +0,0 @@
1
- import { Socket } from 'socket.io-client';
2
- import { Command } from 'kozz-types';
3
- import { Method } from '../../..';
4
- import { createMessageObject } from '../../../Message';
5
- import { isArgsObjectValid } from '../../../Validator';
6
- import { normalizeString, runUse } from '../../../util';
7
- import { UseFn } from 'src/Instance/Common';
8
-
9
- /**
10
- * Handles the command received. It will check if the boundary should have it's command handled,
11
- * find the correct method, parse, typecheck the args, run the middlewares and try to run the method
12
- * @param socket
13
- * @param methods
14
- * @param moduleUseFns
15
- * @param handlerName
16
- * @param templatePath
17
- * @param boundariesToHandle
18
- */
19
- export const onCommand = <
20
- T extends Record<string, Method<Record<string, any>>>
21
- >(
22
- socket: Socket,
23
- methods: T,
24
- moduleUseFns: UseFn[],
25
- handlerName: string,
26
- templatePath: string,
27
- boundariesToHandle: string[]
28
- ) => {
29
- socket.on('command', (command: Command) => {
30
- // If the handler doesnt want to handle commands for the provided boundary
31
- if (
32
- !boundariesToHandle.includes(command.boundaryName) &&
33
- !boundariesToHandle.includes('*')
34
- ) {
35
- return;
36
- }
37
-
38
- const actualMethod =
39
- methods[normalizeString(normalizeString(command.method))] ||
40
- methods['fallback'];
41
- if (!actualMethod) return;
42
-
43
- if (isArgsObjectValid(command.namedArgs || {}, actualMethod.args)) {
44
- const updatedCommand = runUse(moduleUseFns, command);
45
-
46
- const message = createMessageObject(
47
- socket,
48
- command.message,
49
- handlerName,
50
- templatePath
51
- );
52
-
53
- socket.emit('react_message', {
54
- messageId: command.message.id,
55
- boundaryId: command.boundaryId,
56
- emote: '✔',
57
- });
58
-
59
- actualMethod.func(message, updatedCommand.namedArgs || {});
60
- } else {
61
- socket.emit('react_message', {
62
- messageId: command.message.id,
63
- boundaryId: command.boundaryId,
64
- emote: '❌',
65
- });
66
- }
67
- });
68
- };
@@ -1,28 +0,0 @@
1
- import { Source } from 'kozz-types';
2
- import { Socket } from 'socket.io-client';
3
- import {
4
- ProxiedMessageObject,
5
- createProxiedMessageOject,
6
- } from 'src/Message/ProxiedMessage';
7
-
8
- /**
9
- * Handles the message proxied to the handler
10
- * @param socket
11
- * @param source
12
- * @param cb
13
- */
14
- export const onProxiedMessage = (
15
- socket: Socket,
16
- source: Source,
17
- cb: (message: ProxiedMessageObject) => any
18
- ) => {
19
- socket.on('proxied_message', payload => {
20
- const proxiedMessageObject = createProxiedMessageOject(
21
- socket,
22
- source,
23
- payload,
24
- source.split('/')[0]
25
- );
26
- cb(proxiedMessageObject);
27
- });
28
- };
@@ -1,40 +0,0 @@
1
- import { io } from 'socket.io-client';
2
- import { introduce } from './Events/Emit/Introduction';
3
- import { Method } from '../Schema';
4
- import { UseFn } from '../Instance/Common';
5
- import { onCommand } from './Events/Handle/Command';
6
-
7
- export const connect = <T extends Record<string, any>>(
8
- address: string,
9
- moduleUseFns: UseFn[],
10
- templatePath: string,
11
- handlerName: string,
12
- methods: T,
13
- boundariesToHandle: string[],
14
- signature?: string
15
- ) => {
16
- const socket = io(address);
17
-
18
- socket.on('connect', () => {
19
- introduce(socket, handlerName, methods, signature);
20
- });
21
-
22
- const registerMethods = <
23
- T extends Record<string, Method<Record<string, any>>>
24
- >(
25
- methods: T
26
- ) =>
27
- onCommand(
28
- socket,
29
- methods,
30
- moduleUseFns,
31
- handlerName,
32
- templatePath,
33
- boundariesToHandle
34
- );
35
-
36
- return {
37
- socket,
38
- registerMethods,
39
- };
40
- };
@@ -1,81 +0,0 @@
1
- import {
2
- ArrayTypeString,
3
- OptionalTypeString,
4
- PrimaryTypeString,
5
- TypeString,
6
- } from '../Schema';
7
-
8
- type TypeDescriptor<T extends TypeString> = {
9
- array: T extends ArrayTypeString ? true : false;
10
- optional: T extends OptionalTypeString ? true : false;
11
- type: T extends PrimaryTypeString
12
- ? T
13
- : T extends `${infer Type}[]?`
14
- ? Type
15
- : T extends `${infer Type}?`
16
- ? Type
17
- : T extends `${infer Type}[]`
18
- ? Type
19
- : never;
20
- };
21
-
22
- const typeIsArray = (type: TypeString): type is ArrayTypeString => {
23
- return type.includes('[]');
24
- };
25
-
26
- const typeIsOptional = (type: TypeString): type is OptionalTypeString => {
27
- return type.includes('?');
28
- };
29
-
30
- const toPrimitiveType = (type: TypeString): PrimaryTypeString => {
31
- if (type.includes('boolean')) {
32
- return 'boolean';
33
- }
34
- if (type.includes('number')) {
35
- return 'number';
36
- }
37
- return 'string';
38
- };
39
-
40
- export const describeType = <Type extends TypeString>(type: Type) =>
41
- ({
42
- array: typeIsArray(type),
43
- optional: typeIsOptional(type),
44
- type: toPrimitiveType(type),
45
- } as TypeDescriptor<Type>);
46
-
47
- export const isArgValid = <Type extends TypeString>(
48
- arg: unknown,
49
- typeDescriptor: TypeDescriptor<Type>
50
- ) => {
51
- if (arg === undefined && typeDescriptor.optional) {
52
- return true;
53
- }
54
- if (Array.isArray(arg) !== typeDescriptor.array) {
55
- return false;
56
- }
57
- if (Array.isArray(arg) && typeDescriptor.array && arg.length === 0) {
58
- return true;
59
- }
60
- if (Array.isArray(arg) && typeDescriptor.array && arg.length > 0) {
61
- return arg.reduce((valid, arg) => {
62
- return valid && typeof arg === typeDescriptor.type;
63
- }, true);
64
- }
65
- return typeof arg === typeDescriptor.type;
66
- };
67
-
68
- /**
69
- * Check ifm the arguments of a provided command are valid
70
- * @param args
71
- * @param descriptionMap
72
- * @returns
73
- */
74
- export const isArgsObjectValid = <Type extends TypeString>(
75
- args: Record<string, unknown>,
76
- descriptionMap: Record<string, Type> = {}
77
- ) => {
78
- return Object.entries(descriptionMap).reduce((valid, [argName, argType]) => {
79
- return valid && isArgValid(args[argName], describeType(argType));
80
- }, true);
81
- };
@@ -1,52 +0,0 @@
1
- > @foo
2
-
3
- # This is the foo message
4
-
5
- The foo message says "Hi" to {{name}}
6
-
7
- _Nice to meet you!_
8
-
9
- > ---
10
-
11
- > @bar
12
-
13
- # This is the bar message
14
-
15
- The foo message says "GoodBye" to {{name}}
16
-
17
- _See you soon!_
18
-
19
- > ---
20
-
21
- > @list
22
-
23
- # This is the header. It can't interpolate variables.
24
-
25
- # This is a list. The items can't interpolate variables or styles yet.
26
-
27
- - item 1
28
- - item 2
29
- - item 3
30
- <br>
31
-
32
- # This is a table:
33
-
34
- `| Command | Bot's Response |`
35
- `| ------------- | -------------------------------- |`
36
- `| !ping | pong! |`
37
- `| !ping default | pong! |`
38
- `| !ping blah | Ooops, this method doesn't exist |`
39
- <br>
40
-
41
- _Note:_ Be careful not to make wide tables, most chat's will automatically break lines that become too long.
42
- <br>
43
-
44
- Text _*bold*_ _italic_ and `monospace` can appear in the same line, but you can't mix and match them
45
- In order to skip a line, use a line with nothing but `"<br>"`
46
- <br>
47
- <br>
48
- <br>
49
-
50
- Messages start with `"> @messageName"` and end with `"> ---"`
51
-
52
- > ---
package/src/util/index.ts DELETED
@@ -1,89 +0,0 @@
1
- import { Command } from 'kozz-types';
2
- import { UseFn } from '../Instance/Common';
3
- import fs from 'fs/promises';
4
- import crypto from 'crypto';
5
-
6
- let privateKey: string | undefined;
7
-
8
- export const runUse = (useArr: UseFn[], args: Command) => {
9
- return useArr.reduce((res, fn) => {
10
- return fn(res);
11
- }, args);
12
- };
13
-
14
- export function normalizeString(string: string) {
15
- return string.normalize('NFD').replace(/[\u0300-\u036f]/g, '');
16
- }
17
-
18
- export const randomId = () => crypto.randomUUID();
19
-
20
- export const signPayload = <T extends Record<string, any>>(
21
- payload: T,
22
- signature?: string
23
- ) => {
24
- if (signature) {
25
- return {
26
- ...payload,
27
- signature,
28
- };
29
- }
30
- try {
31
- return signInNodeEnvironment(payload);
32
- } catch (e) {
33
- throw 'Please generate a keypair using the script "scripts/generate_key_pair.sh" and copy the publickey to the "${projetc_root}/keys/privatekey.pem". Leve the key names as it is';
34
- }
35
- };
36
-
37
- /**
38
- * Useful for when you need to authenticate in the web. Still didn't managed to make it work
39
- * @param payload
40
- * @returns
41
- */
42
- export const signInBrowser = <T extends Record<string, any>>(payload: T) => {
43
- try {
44
- // @ts-ignore
45
- return fetch('./keys/privatekey.pem')
46
- .then((privateKey: any) => privateKey.text())
47
- .then((privateKey: string) => {
48
- const signature = crypto.sign(
49
- 'sha256',
50
- Buffer.from(JSON.stringify(payload, undefined, ' ')),
51
- { key: privateKey, padding: crypto.constants.RSA_PKCS1_PSS_PADDING }
52
- );
53
-
54
- return {
55
- ...payload,
56
- signature,
57
- };
58
- });
59
- } catch (e) {
60
- throw 'Please generate a keypair using the script "scripts/generate_key_pair.sh" and copy the publickey to the "${projetc_root}/keys/privatekey.pem". Leve the key names as it is';
61
- }
62
- };
63
- /**
64
- * Sign any payload using fs to retrieve the key
65
- * @param payload
66
- * @returns
67
- */
68
- export const signInNodeEnvironment = async <T extends Record<string, any>>(
69
- payload: T
70
- ) => {
71
- try {
72
- privateKey = await fs.readFile('./keys/privatekey.pem', {
73
- encoding: 'utf-8',
74
- });
75
- } catch (e) {
76
- throw 'Please generate a keypair using the script "scripts/generate_key_pair.sh" and copy the publickey to the "${project_root}/keys/privatekey.pem". Leve the key names as it is';
77
- }
78
-
79
- const signature = crypto.sign(
80
- 'sha256',
81
- Buffer.from(JSON.stringify(payload, undefined, ' ')),
82
- { key: privateKey, padding: crypto.constants.RSA_PKCS1_PSS_PADDING }
83
- );
84
-
85
- return {
86
- ...payload,
87
- signature,
88
- };
89
- };
File without changes