@pikku/core 0.6.27 → 0.7.0

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 (126) hide show
  1. package/CHANGELOG.md +10 -1
  2. package/dist/channel/channel-handler.js +16 -38
  3. package/dist/channel/channel-runner.d.ts +7 -8
  4. package/dist/channel/channel-runner.js +58 -7
  5. package/dist/channel/channel.types.d.ts +14 -27
  6. package/dist/channel/local/local-channel-runner.js +9 -4
  7. package/dist/channel/log-channels.js +4 -4
  8. package/dist/channel/serverless/serverless-channel-runner.js +17 -6
  9. package/dist/function/function-runner.d.ts +12 -0
  10. package/dist/function/function-runner.js +38 -0
  11. package/dist/function/functions.types.d.ts +37 -0
  12. package/dist/function/functions.types.js +1 -0
  13. package/dist/function/index.d.ts +2 -0
  14. package/dist/function/index.js +2 -0
  15. package/dist/handle-error.d.ts +1 -1
  16. package/dist/http/http-route-runner.d.ts +3 -3
  17. package/dist/http/http-route-runner.js +60 -35
  18. package/dist/http/http-routes.types.d.ts +38 -7
  19. package/dist/http/http-runner.d.ts +90 -0
  20. package/dist/http/http-runner.js +336 -0
  21. package/dist/http/http.types.d.ts +177 -0
  22. package/dist/http/http.types.js +1 -0
  23. package/dist/http/index.d.ts +2 -2
  24. package/dist/http/index.js +1 -1
  25. package/dist/http/log-http-routes.js +7 -5
  26. package/dist/http/pikku-fetch-http-request.d.ts +1 -1
  27. package/dist/http/pikku-fetch-http-response.d.ts +4 -2
  28. package/dist/http/pikku-fetch-http-response.js +64 -12
  29. package/dist/http/pikku-http-request.d.ts +56 -0
  30. package/dist/http/pikku-http-request.js +95 -0
  31. package/dist/http/pikku-http-response.d.ts +13 -0
  32. package/dist/http/pikku-http-response.js +60 -0
  33. package/dist/index.d.ts +3 -2
  34. package/dist/index.js +3 -2
  35. package/dist/middleware/auth-apikey.js +5 -4
  36. package/dist/middleware/auth-bearer.js +5 -4
  37. package/dist/middleware/auth-cookie.js +10 -9
  38. package/dist/parse-relative-time-offset.d.ts +12 -0
  39. package/dist/parse-relative-time-offset.js +20 -0
  40. package/dist/permissions.d.ts +2 -2
  41. package/dist/pikku-func.d.ts +12 -0
  42. package/dist/pikku-func.js +39 -0
  43. package/dist/pikku-function.d.ts +1 -0
  44. package/dist/pikku-function.js +1 -0
  45. package/dist/pikku-state.d.ts +10 -5
  46. package/dist/pikku-state.js +7 -3
  47. package/dist/scheduler/scheduler-runner.d.ts +1 -1
  48. package/dist/scheduler/scheduler-runner.js +24 -6
  49. package/dist/scheduler/scheduler.types.d.ts +3 -2
  50. package/dist/services/content-service.d.ts +33 -37
  51. package/dist/types/core.types.d.ts +15 -1
  52. package/dist/types/functions.types.d.ts +11 -6
  53. package/dist/utils.d.ts +2 -0
  54. package/dist/utils.js +15 -0
  55. package/package.json +2 -1
  56. package/lcov.info +0 -4310
  57. package/src/channel/channel-handler.ts +0 -227
  58. package/src/channel/channel-runner.ts +0 -98
  59. package/src/channel/channel-store.ts +0 -29
  60. package/src/channel/channel.types.ts +0 -171
  61. package/src/channel/eventhub-service.ts +0 -30
  62. package/src/channel/eventhub-store.ts +0 -8
  63. package/src/channel/index.ts +0 -8
  64. package/src/channel/local/index.ts +0 -3
  65. package/src/channel/local/local-channel-handler.ts +0 -50
  66. package/src/channel/local/local-channel-runner.test.ts +0 -158
  67. package/src/channel/local/local-channel-runner.ts +0 -126
  68. package/src/channel/local/local-eventhub-service.test.ts +0 -95
  69. package/src/channel/local/local-eventhub-service.ts +0 -95
  70. package/src/channel/log-channels.ts +0 -20
  71. package/src/channel/pikku-abstract-channel-handler.test.ts +0 -52
  72. package/src/channel/pikku-abstract-channel-handler.ts +0 -38
  73. package/src/channel/serverless/index.ts +0 -1
  74. package/src/channel/serverless/serverless-channel-runner.ts +0 -209
  75. package/src/errors/error-handler.ts +0 -61
  76. package/src/errors/error.test.ts +0 -54
  77. package/src/errors/errors.ts +0 -339
  78. package/src/errors/index.ts +0 -2
  79. package/src/handle-error.ts +0 -73
  80. package/src/http/http-route-runner.test.ts +0 -117
  81. package/src/http/http-route-runner.ts +0 -460
  82. package/src/http/http-routes.types.ts +0 -211
  83. package/src/http/incomingmessage-to-request-convertor.ts +0 -0
  84. package/src/http/index.ts +0 -8
  85. package/src/http/log-http-routes.ts +0 -20
  86. package/src/http/pikku-fetch-http-request.test.ts +0 -237
  87. package/src/http/pikku-fetch-http-request.ts +0 -167
  88. package/src/http/pikku-fetch-http-response.test.ts +0 -82
  89. package/src/http/pikku-fetch-http-response.ts +0 -84
  90. package/src/index.ts +0 -19
  91. package/src/middleware/auth-apikey.ts +0 -65
  92. package/src/middleware/auth-bearer.ts +0 -65
  93. package/src/middleware/auth-cookie.ts +0 -98
  94. package/src/middleware/index.ts +0 -3
  95. package/src/middleware/timeout.ts +0 -13
  96. package/src/middleware-runner.ts +0 -43
  97. package/src/permissions.test.ts +0 -58
  98. package/src/permissions.ts +0 -45
  99. package/src/pikku-request.ts +0 -23
  100. package/src/pikku-response.ts +0 -5
  101. package/src/pikku-state.ts +0 -71
  102. package/src/scheduler/index.ts +0 -5
  103. package/src/scheduler/log-schedulers.ts +0 -20
  104. package/src/scheduler/scheduler-runner.ts +0 -87
  105. package/src/scheduler/scheduler.types.ts +0 -29
  106. package/src/schema.test.ts +0 -57
  107. package/src/schema.ts +0 -99
  108. package/src/services/content-service.ts +0 -72
  109. package/src/services/index.ts +0 -18
  110. package/src/services/jwt-service.ts +0 -30
  111. package/src/services/local-content.ts +0 -105
  112. package/src/services/local-secrets.ts +0 -43
  113. package/src/services/local-variables.ts +0 -17
  114. package/src/services/logger-console.ts +0 -97
  115. package/src/services/logger.ts +0 -57
  116. package/src/services/schema-service.ts +0 -26
  117. package/src/services/secret-service.ts +0 -17
  118. package/src/services/user-session-service.ts +0 -53
  119. package/src/services/variables-service.ts +0 -6
  120. package/src/time-utils.test.ts +0 -56
  121. package/src/time-utils.ts +0 -32
  122. package/src/types/core.types.ts +0 -159
  123. package/src/types/functions.types.ts +0 -63
  124. package/src/utils.ts +0 -21
  125. package/tsconfig.json +0 -13
  126. package/tsconfig.tsbuildinfo +0 -1
@@ -1,7 +1,14 @@
1
1
  import { getErrorResponse } from '../errors/error-handler.js';
2
2
  import { closeSessionServices } from '../utils.js';
3
3
  import { pikkuState } from '../pikku-state.js';
4
+ import { addFunction, runPikkuFunc } from '../function/function-runner.js';
4
5
  export const addScheduledTask = (scheduledTask) => {
6
+ const meta = pikkuState('scheduler', 'meta');
7
+ const taskMeta = meta[scheduledTask.name];
8
+ if (!taskMeta) {
9
+ throw new Error('Task metadata not found');
10
+ }
11
+ addFunction(taskMeta.pikkuFuncName, scheduledTask.func);
5
12
  const tasks = pikkuState('scheduler', 'tasks');
6
13
  if (tasks.has(scheduledTask.name)) {
7
14
  throw new Error(`Scheduled task already exists: ${scheduledTask.name}`);
@@ -17,16 +24,27 @@ export async function runScheduledTask({ name, session, singletonServices, creat
17
24
  let sessionServices;
18
25
  try {
19
26
  const task = pikkuState('scheduler', 'tasks').get(name);
27
+ const meta = pikkuState('scheduler', 'meta')[name];
20
28
  if (!task) {
21
29
  throw new ScheduledTaskNotFoundError(`Scheduled task not found: ${name}`);
22
30
  }
23
- singletonServices.logger.info(`Running schedule task: ${name} | schedule: ${task.schedule}`);
24
- let allServices = singletonServices;
25
- if (createSessionServices) {
26
- const sessionServices = await createSessionServices(singletonServices, {}, session);
27
- allServices = { ...singletonServices, ...sessionServices };
31
+ if (!meta) {
32
+ throw new ScheduledTaskNotFoundError(`Scheduled task meta not found: ${name}`);
28
33
  }
29
- await task.func(allServices, undefined, session);
34
+ singletonServices.logger.info(`Running schedule task: ${name} | schedule: ${task.schedule}`);
35
+ const getAllServices = async () => {
36
+ if (createSessionServices) {
37
+ const sessionServices = await createSessionServices(singletonServices, {}, session);
38
+ return { ...singletonServices, ...sessionServices };
39
+ }
40
+ return singletonServices;
41
+ };
42
+ return await runPikkuFunc(meta.pikkuFuncName, {
43
+ singletonServices,
44
+ getAllServices,
45
+ session,
46
+ data: undefined,
47
+ });
30
48
  }
31
49
  catch (e) {
32
50
  const errorResponse = getErrorResponse(e);
@@ -1,9 +1,10 @@
1
1
  import { APIDocs, CoreUserSession } from '../types/core.types.js';
2
- import { CoreAPIFunctionSessionless } from '../types/functions.types.js';
2
+ import { CoreAPIFunctionSessionless } from '../function/functions.types.js';
3
3
  /**
4
4
  * Represents metadata for scheduled tasks, including title, schedule, and documentation.
5
5
  */
6
- export type ScheduledTasksMeta<UserSession extends CoreUserSession = any> = Array<{
6
+ export type ScheduledTasksMeta<UserSession extends CoreUserSession = any> = Record<string, {
7
+ pikkuFuncName: string;
7
8
  name: string;
8
9
  schedule: string;
9
10
  session?: UserSession;
@@ -1,57 +1,53 @@
1
1
  import { ReadStream } from 'fs';
2
- import { Readable } from 'stream';
3
- /**
4
- * Interface for handling content operations.
5
- */
6
2
  export interface ContentService {
7
3
  /**
8
- * Signs a content key to provide secure access.
9
- * @param contentKey - The key of the content to sign.
10
- * @returns A promise that resolves to the signed content key URL.
4
+ * Signs a content key to generate a secure, time-limited access URL.
5
+ * @param contentKey - The key representing the content object.
6
+ * @param dateLessThan - The expiration time for the signed URL.
7
+ * @param dateGreaterThan - (Optional) Start time before which access is denied.
11
8
  */
12
- signContentKey: (contentKey: string, dateLessThan: Date, dateGreaterThan?: Date) => Promise<string>;
9
+ signContentKey(contentKey: string, dateLessThan: Date, dateGreaterThan?: Date): Promise<string>;
13
10
  /**
14
- * Signs a URL to provide secure access.
15
- * @param url - The URL to sign.
16
- * @returns A promise that resolves to the signed URL.
11
+ * Signs an arbitrary URL to generate a secure, time-limited access URL.
12
+ * @param url - The full URL that needs signing.
13
+ * @param dateLessThan - The expiration time for the signed URL.
14
+ * @param dateGreaterThan - (Optional) Start time before which access is denied.
17
15
  */
18
- signURL: (url: string, dateLessThan: Date, dateGreaterThan?: Date) => Promise<string>;
16
+ signURL(url: string, dateLessThan: Date, dateGreaterThan?: Date): Promise<string>;
19
17
  /**
20
- * Gets an upload URL for a file.
21
- * @param fileKey - The key of the file to upload.
22
- * @param contentType - The content type of the file.
23
- * @returns A promise that resolves to an object containing the upload URL and asset key.
24
- * This method generates an upload URL for the specified file key and content type, allowing the file to be uploaded.
18
+ * Generates a signed URL for uploading a file directly to storage.
19
+ * @param fileKey - The desired key/location of the uploaded file.
20
+ * @param contentType - The MIME type of the file.
21
+ * @returns A signed upload URL and the finalized asset key.
25
22
  */
26
- getUploadURL: (fileKey: string, contentType: string) => Promise<{
23
+ getUploadURL(fileKey: string, contentType: string): Promise<{
27
24
  uploadUrl: string;
28
25
  assetKey: string;
29
26
  }>;
30
27
  /**
31
- * Deletes a file.
32
- * @param fileName - The name of the file to delete.
33
- * @returns A promise that resolves to a boolean indicating success.
34
- * This method deletes the specified file and returns a boolean indicating whether the deletion was successful.
28
+ * Deletes a file from the storage backend.
29
+ * @param fileName - The name or key of the file to delete.
30
+ * @returns A boolean indicating success.
35
31
  */
36
- deleteFile: (fileName: string) => Promise<boolean>;
32
+ deleteFile(fileName: string): Promise<boolean>;
37
33
  /**
38
- * Writes a file.
39
- * @param assetKey - The key of the asset to write.
40
- * @param stream - The ReadStream of the file to write.
41
- * @returns A promise that resolves to a boolean indicating success.
34
+ * Uploads a file stream to storage under a specified asset key.
35
+ * @param assetKey - The key where the file will be saved.
36
+ * @param stream - A readable stream of the file contents.
37
+ * @returns A boolean indicating success.
42
38
  */
43
- writeFile: (assetKey: string, stream: ReadStream) => Promise<boolean>;
39
+ writeFile(assetKey: string, stream: ReadStream): Promise<boolean>;
44
40
  /**
45
- * This method copies the file from the specified absolute path to the location identified by the asset key and returns a boolean indicating success.
46
- * @param assetKey - The key of the asset to copy.
47
- * @param fromAbsolutePath - The absolute path of the source file.
48
- * @returns A promise that resolves to a boolean indicating success.
41
+ * Copies a file from a local absolute path into storage under a new asset key.
42
+ * @param assetKey - The destination key.
43
+ * @param fromAbsolutePath - The local absolute file path.
44
+ * @returns A boolean indicating success.
49
45
  */
50
- copyFile: (assetKey: string, fromAbsolutePath: string) => Promise<boolean>;
46
+ copyFile(assetKey: string, fromAbsolutePath: string): Promise<boolean>;
51
47
  /**
52
- * Reads a file.
53
- * @param assetKey - The key of the asset to read.
54
- * @returns A promise that resolves to a Readable stream of the file.
48
+ * Reads a file from storage as a readable stream.
49
+ * @param assetKey - The key of the file to read.
50
+ * @returns A readable file stream.
55
51
  */
56
- readFile: (assetKey: string) => Promise<Readable>;
52
+ readFile(assetKey: string): Promise<ReadStream>;
57
53
  }
@@ -2,10 +2,23 @@ import type { Logger, LogLevel } from '../services/logger.js';
2
2
  import { VariablesService } from '../services/variables-service.js';
3
3
  import { EventHubService } from '../channel/eventhub-service.js';
4
4
  import { SchemaService } from '../services/schema-service.js';
5
- import { PikkuHTTP } from '../http/http-routes.types.js';
5
+ import { PikkuHTTP } from '../http/http.types.js';
6
6
  import { UserSessionService } from '../services/user-session-service.js';
7
7
  import { JWTService } from '../services/jwt-service.js';
8
8
  import { SecretService } from '../services/secret-service.js';
9
+ import { PikkuChannel } from '../channel/channel.types.js';
10
+ export interface FunctionServicesMeta {
11
+ optimized: boolean;
12
+ services: string[];
13
+ }
14
+ export type FunctionsMeta = Record<string, {
15
+ pikkuFuncName: string;
16
+ name?: string;
17
+ services: FunctionServicesMeta;
18
+ schemaName: string | null;
19
+ inputs: string[] | null;
20
+ outputs: string[] | null;
21
+ }>;
9
22
  export type MakeRequired<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>;
10
23
  /**
11
24
  * Represents a JSON primitive type which can be a string, number, boolean, null, or undefined.
@@ -81,6 +94,7 @@ export type PikkuMiddleware<SingletonServices extends CoreSingletonServices = Co
81
94
  */
82
95
  export type CoreServices<SingletonServices = CoreSingletonServices, UserSession extends CoreUserSession = CoreUserSession, CoreServices extends Record<string, unknown> = {}> = SingletonServices & PikkuInteraction & {
83
96
  userSession?: UserSessionService<UserSession>;
97
+ channel?: PikkuChannel<unknown, unknown>;
84
98
  } & CoreServices;
85
99
  export type SessionServices<SingletonServices extends CoreSingletonServices = CoreSingletonServices, Services = CoreServices<SingletonServices>> = Omit<Services, keyof SingletonServices | keyof PikkuInteraction | 'session'>;
86
100
  /**
@@ -8,9 +8,11 @@ import type { CoreServices, CoreSingletonServices, CoreUserSession } from './cor
8
8
  * @template Services - The services type, defaults to `CoreServices`.
9
9
  * @template Session - The session type, defaults to `CoreUserSession`.
10
10
  */
11
- export type CoreAPIFunction<In, Out, Services extends CoreSingletonServices = CoreServices & {
12
- channel?: PikkuChannel<unknown, Out>;
13
- }, Session extends CoreUserSession = CoreUserSession, Channel extends boolean = false> = (services: Services, data: In, session: Session) => Channel extends true ? Promise<Out> | Promise<void> : Promise<Out>;
11
+ export type CoreAPIFunction<In, Out, Channel extends boolean = false, Services extends CoreSingletonServices = CoreServices & (Channel extends true ? {
12
+ channel: PikkuChannel<unknown, Out>;
13
+ } : {
14
+ channel?: PikkuChannel<unknown, Out> | undefined;
15
+ }), Session extends CoreUserSession = CoreUserSession> = (services: Services, data: In, session: Session) => Channel extends true ? Promise<Out> | Promise<void> : Promise<Out>;
14
16
  /**
15
17
  * Represents a core API function that can be used without a session.
16
18
  *
@@ -19,9 +21,11 @@ export type CoreAPIFunction<In, Out, Services extends CoreSingletonServices = Co
19
21
  * @template Services - The services type, defaults to `CoreServices`.
20
22
  * @template Session - The session type, defaults to `CoreUserSession`.
21
23
  */
22
- export type CoreAPIFunctionSessionless<In, Out, Services extends CoreSingletonServices = CoreServices & {
23
- channel?: PikkuChannel<unknown, Out>;
24
- }, Session extends CoreUserSession = CoreUserSession, Channel extends boolean = false> = (services: Services, data: In, session?: Session) => Channel extends true ? Promise<Out> | Promise<void> : Promise<Out>;
24
+ export type CoreAPIFunctionSessionless<In, Out, Channel extends boolean = false, Services extends CoreSingletonServices = CoreServices & (Channel extends true ? {
25
+ channel: PikkuChannel<unknown, Out>;
26
+ } : {
27
+ channel?: PikkuChannel<unknown, Out> | undefined;
28
+ }), Session extends CoreUserSession = CoreUserSession> = (services: Services, data: In, session?: Session) => Channel extends true ? Promise<Out> | Promise<void> : Promise<Out>;
25
29
  /**
26
30
  * Represents a function that checks permissions for a given API operation.
27
31
  *
@@ -30,3 +34,4 @@ export type CoreAPIFunctionSessionless<In, Out, Services extends CoreSingletonSe
30
34
  * @template Session - The session type, defaults to `CoreUserSession`.
31
35
  */
32
36
  export type CoreAPIPermission<In = any, Services extends CoreSingletonServices = CoreServices, Session extends CoreUserSession = CoreUserSession> = (services: Services, data: In, session?: Session) => Promise<boolean>;
37
+ export type CorePermissionGroup<APIPermission = CoreAPIPermission<any>> = Record<string, APIPermission | APIPermission[]> | undefined;
package/dist/utils.d.ts CHANGED
@@ -1,2 +1,4 @@
1
1
  import { Logger } from './services/logger.js';
2
2
  export declare const closeSessionServices: (logger: Logger, sessionServices: Record<string, any>) => Promise<void>;
3
+ export declare const createWeakUID: () => string;
4
+ export declare const isSerializable: (data: any) => boolean;
package/dist/utils.js CHANGED
@@ -13,3 +13,18 @@ export const closeSessionServices = async (logger, sessionServices) => {
13
13
  }
14
14
  }));
15
15
  };
16
+ export const createWeakUID = () => {
17
+ return Date.now().toString(36) + Math.random().toString(36).substring(2, 10);
18
+ };
19
+ export const isSerializable = (data) => {
20
+ return !(typeof data === 'string' ||
21
+ data instanceof ArrayBuffer ||
22
+ data instanceof Uint8Array ||
23
+ data instanceof Int8Array ||
24
+ data instanceof Uint16Array ||
25
+ data instanceof Int16Array ||
26
+ data instanceof Uint32Array ||
27
+ data instanceof Int32Array ||
28
+ data instanceof Float32Array ||
29
+ data instanceof Float64Array);
30
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pikku/core",
3
- "version": "0.6.27",
3
+ "version": "0.7.0",
4
4
  "author": "yasser.fadl@gmail.com",
5
5
  "license": "MIT",
6
6
  "module": "dist/index.js",
@@ -19,6 +19,7 @@
19
19
  "exports": {
20
20
  ".": "./dist/index.js",
21
21
  "./middleware": "./dist/middleware/index.js",
22
+ "./function": "./dist/function/index.js",
22
23
  "./channel": "./dist/channel/index.js",
23
24
  "./channel/local": "./dist/channel/local/index.js",
24
25
  "./channel/serverless": "./dist/channel/serverless/index.js",