@pikku/core 0.6.26 → 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.
- package/CHANGELOG.md +17 -1
- package/dist/channel/channel-handler.js +16 -38
- package/dist/channel/channel-runner.d.ts +7 -8
- package/dist/channel/channel-runner.js +58 -7
- package/dist/channel/channel.types.d.ts +14 -27
- package/dist/channel/local/local-channel-runner.js +9 -4
- package/dist/channel/log-channels.js +4 -4
- package/dist/channel/serverless/serverless-channel-runner.js +17 -6
- package/dist/function/function-runner.d.ts +12 -0
- package/dist/function/function-runner.js +38 -0
- package/dist/function/functions.types.d.ts +37 -0
- package/dist/function/functions.types.js +1 -0
- package/dist/function/index.d.ts +2 -0
- package/dist/function/index.js +2 -0
- package/dist/handle-error.d.ts +1 -1
- package/dist/http/http-route-runner.d.ts +3 -3
- package/dist/http/http-route-runner.js +60 -35
- package/dist/http/http-routes.types.d.ts +38 -7
- package/dist/http/http-runner.d.ts +90 -0
- package/dist/http/http-runner.js +336 -0
- package/dist/http/http.types.d.ts +177 -0
- package/dist/http/http.types.js +1 -0
- package/dist/http/index.d.ts +2 -2
- package/dist/http/index.js +1 -1
- package/dist/http/log-http-routes.js +7 -5
- package/dist/http/pikku-fetch-http-request.d.ts +1 -1
- package/dist/http/pikku-fetch-http-response.d.ts +4 -2
- package/dist/http/pikku-fetch-http-response.js +64 -12
- package/dist/http/pikku-http-request.d.ts +56 -0
- package/dist/http/pikku-http-request.js +95 -0
- package/dist/http/pikku-http-response.d.ts +13 -0
- package/dist/http/pikku-http-response.js +60 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.js +3 -2
- package/dist/middleware/auth-apikey.js +5 -4
- package/dist/middleware/auth-bearer.js +5 -4
- package/dist/middleware/auth-cookie.js +10 -9
- package/dist/parse-relative-time-offset.d.ts +12 -0
- package/dist/parse-relative-time-offset.js +20 -0
- package/dist/permissions.d.ts +2 -2
- package/dist/pikku-func.d.ts +12 -0
- package/dist/pikku-func.js +39 -0
- package/dist/pikku-function.d.ts +1 -0
- package/dist/pikku-function.js +1 -0
- package/dist/pikku-state.d.ts +10 -5
- package/dist/pikku-state.js +7 -3
- package/dist/scheduler/scheduler-runner.d.ts +1 -1
- package/dist/scheduler/scheduler-runner.js +24 -6
- package/dist/scheduler/scheduler.types.d.ts +3 -2
- package/dist/services/content-service.d.ts +34 -36
- package/dist/services/index.d.ts +1 -0
- package/dist/services/index.js +1 -0
- package/dist/services/local-content.d.ts +4 -2
- package/dist/services/local-content.js +19 -9
- package/dist/types/core.types.d.ts +15 -1
- package/dist/types/functions.types.d.ts +11 -6
- package/dist/utils.d.ts +2 -0
- package/dist/utils.js +15 -0
- package/package.json +2 -1
- package/lcov.info +0 -4310
- package/src/channel/channel-handler.ts +0 -227
- package/src/channel/channel-runner.ts +0 -98
- package/src/channel/channel-store.ts +0 -29
- package/src/channel/channel.types.ts +0 -171
- package/src/channel/eventhub-service.ts +0 -30
- package/src/channel/eventhub-store.ts +0 -8
- package/src/channel/index.ts +0 -8
- package/src/channel/local/index.ts +0 -3
- package/src/channel/local/local-channel-handler.ts +0 -50
- package/src/channel/local/local-channel-runner.test.ts +0 -158
- package/src/channel/local/local-channel-runner.ts +0 -126
- package/src/channel/local/local-eventhub-service.test.ts +0 -95
- package/src/channel/local/local-eventhub-service.ts +0 -95
- package/src/channel/log-channels.ts +0 -20
- package/src/channel/pikku-abstract-channel-handler.test.ts +0 -52
- package/src/channel/pikku-abstract-channel-handler.ts +0 -38
- package/src/channel/serverless/index.ts +0 -1
- package/src/channel/serverless/serverless-channel-runner.ts +0 -209
- package/src/errors/error-handler.ts +0 -61
- package/src/errors/error.test.ts +0 -54
- package/src/errors/errors.ts +0 -339
- package/src/errors/index.ts +0 -2
- package/src/handle-error.ts +0 -73
- package/src/http/http-route-runner.test.ts +0 -117
- package/src/http/http-route-runner.ts +0 -460
- package/src/http/http-routes.types.ts +0 -211
- package/src/http/incomingmessage-to-request-convertor.ts +0 -0
- package/src/http/index.ts +0 -8
- package/src/http/log-http-routes.ts +0 -20
- package/src/http/pikku-fetch-http-request.test.ts +0 -237
- package/src/http/pikku-fetch-http-request.ts +0 -167
- package/src/http/pikku-fetch-http-response.test.ts +0 -82
- package/src/http/pikku-fetch-http-response.ts +0 -84
- package/src/index.ts +0 -19
- package/src/middleware/auth-apikey.ts +0 -65
- package/src/middleware/auth-bearer.ts +0 -65
- package/src/middleware/auth-cookie.ts +0 -98
- package/src/middleware/index.ts +0 -3
- package/src/middleware/timeout.ts +0 -13
- package/src/middleware-runner.ts +0 -43
- package/src/permissions.test.ts +0 -58
- package/src/permissions.ts +0 -45
- package/src/pikku-request.ts +0 -23
- package/src/pikku-response.ts +0 -5
- package/src/pikku-state.ts +0 -71
- package/src/scheduler/index.ts +0 -5
- package/src/scheduler/log-schedulers.ts +0 -20
- package/src/scheduler/scheduler-runner.ts +0 -87
- package/src/scheduler/scheduler.types.ts +0 -29
- package/src/schema.test.ts +0 -57
- package/src/schema.ts +0 -99
- package/src/services/content-service.ts +0 -69
- package/src/services/index.ts +0 -17
- package/src/services/jwt-service.ts +0 -30
- package/src/services/local-content.ts +0 -91
- package/src/services/local-secrets.ts +0 -43
- package/src/services/local-variables.ts +0 -17
- package/src/services/logger-console.ts +0 -97
- package/src/services/logger.ts +0 -57
- package/src/services/schema-service.ts +0 -26
- package/src/services/secret-service.ts +0 -17
- package/src/services/user-session-service.ts +0 -53
- package/src/services/variables-service.ts +0 -6
- package/src/time-utils.test.ts +0 -56
- package/src/time-utils.ts +0 -32
- package/src/types/core.types.ts +0 -159
- package/src/types/functions.types.ts +0 -63
- package/src/utils.ts +0 -21
- package/tsconfig.json +0 -13
- package/tsconfig.tsbuildinfo +0 -1
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { pikkuState } from '../pikku-state.js'
|
|
2
|
-
import { Logger } from '../services/index.js'
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Logs all the loaded scheduled tasks.
|
|
6
|
-
* @param logger - A logger for logging information.
|
|
7
|
-
*/
|
|
8
|
-
export const logSchedulers = (logger: Logger) => {
|
|
9
|
-
const scheduledTasks = pikkuState('scheduler', 'tasks')
|
|
10
|
-
if (scheduledTasks.size === 0) {
|
|
11
|
-
logger.info('No scheduled tasks added')
|
|
12
|
-
return
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
let scheduledTasksMessage = 'Scheduled tasks:'
|
|
16
|
-
scheduledTasks.forEach(({ schedule }, name) => {
|
|
17
|
-
scheduledTasksMessage += `\n\t- ${name} -> ${schedule}`
|
|
18
|
-
})
|
|
19
|
-
logger.info(scheduledTasksMessage)
|
|
20
|
-
}
|
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
CoreServices,
|
|
3
|
-
CoreSingletonServices,
|
|
4
|
-
CoreUserSession,
|
|
5
|
-
CreateSessionServices,
|
|
6
|
-
} from '../types/core.types.js'
|
|
7
|
-
import type { CoreScheduledTask } from './scheduler.types.js'
|
|
8
|
-
import type { CoreAPIFunctionSessionless } from '../types/functions.types.js'
|
|
9
|
-
import { getErrorResponse } from '../errors/error-handler.js'
|
|
10
|
-
import { closeSessionServices } from '../utils.js'
|
|
11
|
-
import { pikkuState } from '../pikku-state.js'
|
|
12
|
-
|
|
13
|
-
export type RunScheduledTasksParams = {
|
|
14
|
-
name: string
|
|
15
|
-
session?: CoreUserSession
|
|
16
|
-
singletonServices: CoreSingletonServices
|
|
17
|
-
createSessionServices?: CreateSessionServices<
|
|
18
|
-
CoreSingletonServices,
|
|
19
|
-
CoreServices<CoreSingletonServices>,
|
|
20
|
-
CoreUserSession
|
|
21
|
-
>
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export const addScheduledTask = <
|
|
25
|
-
APIFunction extends CoreAPIFunctionSessionless<void, void>,
|
|
26
|
-
>(
|
|
27
|
-
scheduledTask: CoreScheduledTask<APIFunction>
|
|
28
|
-
) => {
|
|
29
|
-
const tasks = pikkuState('scheduler', 'tasks')
|
|
30
|
-
if (tasks.has(scheduledTask.name)) {
|
|
31
|
-
throw new Error(`Scheduled task already exists: ${scheduledTask.name}`)
|
|
32
|
-
}
|
|
33
|
-
tasks.set(scheduledTask.name, scheduledTask)
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
class ScheduledTaskNotFoundError extends Error {
|
|
37
|
-
constructor(title: string) {
|
|
38
|
-
super(`Scheduled task not found: ${title}`)
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export async function runScheduledTask<
|
|
43
|
-
SingletonServices extends CoreSingletonServices = CoreSingletonServices,
|
|
44
|
-
UserSession extends CoreUserSession = CoreUserSession,
|
|
45
|
-
>({
|
|
46
|
-
name,
|
|
47
|
-
session,
|
|
48
|
-
singletonServices,
|
|
49
|
-
createSessionServices,
|
|
50
|
-
}: RunScheduledTasksParams): Promise<void> {
|
|
51
|
-
let sessionServices: CoreServices | undefined
|
|
52
|
-
try {
|
|
53
|
-
const task = pikkuState('scheduler', 'tasks').get(name)
|
|
54
|
-
|
|
55
|
-
if (!task) {
|
|
56
|
-
throw new ScheduledTaskNotFoundError(`Scheduled task not found: ${name}`)
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
singletonServices.logger.info(
|
|
60
|
-
`Running schedule task: ${name} | schedule: ${task.schedule}`
|
|
61
|
-
)
|
|
62
|
-
|
|
63
|
-
let allServices = singletonServices
|
|
64
|
-
if (createSessionServices) {
|
|
65
|
-
const sessionServices = await createSessionServices(
|
|
66
|
-
singletonServices,
|
|
67
|
-
{},
|
|
68
|
-
session
|
|
69
|
-
)
|
|
70
|
-
allServices = { ...singletonServices, ...sessionServices }
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
await task.func(allServices, undefined, session!)
|
|
74
|
-
} catch (e: any) {
|
|
75
|
-
const errorResponse = getErrorResponse(e)
|
|
76
|
-
|
|
77
|
-
if (errorResponse != null) {
|
|
78
|
-
singletonServices.logger.error(e)
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
throw e
|
|
82
|
-
} finally {
|
|
83
|
-
if (sessionServices) {
|
|
84
|
-
await closeSessionServices(singletonServices.logger, sessionServices)
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
}
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { APIDocs, CoreUserSession } from '../types/core.types.js'
|
|
2
|
-
import { CoreAPIFunctionSessionless } from '../types/functions.types.js'
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Represents metadata for scheduled tasks, including title, schedule, and documentation.
|
|
6
|
-
*/
|
|
7
|
-
export type ScheduledTasksMeta<UserSession extends CoreUserSession = any> =
|
|
8
|
-
Array<{
|
|
9
|
-
name: string
|
|
10
|
-
schedule: string
|
|
11
|
-
session?: UserSession
|
|
12
|
-
docs?: APIDocs
|
|
13
|
-
tags?: string[]
|
|
14
|
-
}>
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Represents a core scheduled task.
|
|
18
|
-
*/
|
|
19
|
-
export type CoreScheduledTask<
|
|
20
|
-
APIFunction = CoreAPIFunctionSessionless<void, void>,
|
|
21
|
-
UserSession extends CoreUserSession = CoreUserSession,
|
|
22
|
-
> = {
|
|
23
|
-
name: string
|
|
24
|
-
schedule: string
|
|
25
|
-
func: APIFunction
|
|
26
|
-
docs?: APIDocs
|
|
27
|
-
session?: UserSession
|
|
28
|
-
tags?: string[]
|
|
29
|
-
}
|
package/src/schema.test.ts
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { test, describe, before } from 'node:test'
|
|
2
|
-
import * as assert from 'assert'
|
|
3
|
-
import { addSchema, coerceTopLevelDataFromSchema } from './schema.js'
|
|
4
|
-
|
|
5
|
-
describe('Schema', () => {
|
|
6
|
-
describe('coerceQueryStringToArray', () => {
|
|
7
|
-
before(() => {
|
|
8
|
-
// Add schemas dynamically using addSchema
|
|
9
|
-
addSchema('testSchema', {
|
|
10
|
-
properties: {
|
|
11
|
-
tags: { type: 'array' },
|
|
12
|
-
count: { type: 'number' },
|
|
13
|
-
name: { type: 'string' },
|
|
14
|
-
},
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
addSchema('booleanSchema', {
|
|
18
|
-
properties: {
|
|
19
|
-
isActive: true, // Invalid schema definition, but the function should handle it
|
|
20
|
-
tags: { type: 'array' },
|
|
21
|
-
},
|
|
22
|
-
})
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
test('should split a string into an array for properties of type array', () => {
|
|
26
|
-
const data = { tags: 'a,b,c' }
|
|
27
|
-
coerceTopLevelDataFromSchema('testSchema', data)
|
|
28
|
-
assert.deepStrictEqual(data.tags, ['a', 'b', 'c'])
|
|
29
|
-
})
|
|
30
|
-
|
|
31
|
-
test('should not modify properties of type array if they are already arrays', () => {
|
|
32
|
-
const data = { tags: ['a', 'b', 'c'] }
|
|
33
|
-
coerceTopLevelDataFromSchema('testSchema', data)
|
|
34
|
-
assert.deepStrictEqual(data.tags, ['a', 'b', 'c'])
|
|
35
|
-
})
|
|
36
|
-
|
|
37
|
-
test('should not modify properties that are not type array', () => {
|
|
38
|
-
const data = { count: 5, name: 'example' }
|
|
39
|
-
coerceTopLevelDataFromSchema('testSchema', data)
|
|
40
|
-
assert.strictEqual(data.count, 5)
|
|
41
|
-
assert.strictEqual(data.name, 'example')
|
|
42
|
-
})
|
|
43
|
-
|
|
44
|
-
test('should handle cases where the data object does not have a key present in the schema', () => {
|
|
45
|
-
const data = { unknownKey: 'shouldRemain' }
|
|
46
|
-
coerceTopLevelDataFromSchema('testSchema', data)
|
|
47
|
-
assert.strictEqual(data.unknownKey, 'shouldRemain')
|
|
48
|
-
})
|
|
49
|
-
|
|
50
|
-
test('should handle cases where schema properties contain boolean values', () => {
|
|
51
|
-
const data = { tags: 'a,b,c', isActive: 'true' }
|
|
52
|
-
coerceTopLevelDataFromSchema('booleanSchema', data)
|
|
53
|
-
assert.deepStrictEqual(data.tags, ['a', 'b', 'c'])
|
|
54
|
-
assert.strictEqual(data.isActive, 'true') // No coercion should happen
|
|
55
|
-
})
|
|
56
|
-
})
|
|
57
|
-
})
|
package/src/schema.ts
DELETED
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
import { Logger } from './services/logger.js'
|
|
2
|
-
import { SchemaService } from './services/schema-service.js'
|
|
3
|
-
import { UnprocessableContentError } from './errors/errors.js'
|
|
4
|
-
import { pikkuState } from './pikku-state.js'
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Adds a schema to the global schemas map.
|
|
8
|
-
* @param name - The name of the schema.
|
|
9
|
-
* @param value - The schema value.
|
|
10
|
-
* @ignore
|
|
11
|
-
*/
|
|
12
|
-
export const addSchema = (name: string, value: any) => {
|
|
13
|
-
pikkuState('misc', 'schemas').set(name, value)
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Loads a schema and compiles it into a validator.
|
|
18
|
-
* @param logger - A logger for logging information.
|
|
19
|
-
*/
|
|
20
|
-
export const compileAllSchemas = (
|
|
21
|
-
logger: Logger,
|
|
22
|
-
schemaService?: SchemaService
|
|
23
|
-
) => {
|
|
24
|
-
if (!schemaService) {
|
|
25
|
-
throw new Error('SchemaService needs to be defined to load schemas')
|
|
26
|
-
}
|
|
27
|
-
const schemas = pikkuState('misc', 'schemas')
|
|
28
|
-
for (const [name, schema] of schemas) {
|
|
29
|
-
schemaService.compileSchema(name, schema)
|
|
30
|
-
}
|
|
31
|
-
validateAllSchemasLoaded(logger, schemaService)
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const validateAllSchemasLoaded = (
|
|
35
|
-
logger: Logger,
|
|
36
|
-
schemaService: SchemaService
|
|
37
|
-
) => {
|
|
38
|
-
const routesMeta = pikkuState('http', 'meta')
|
|
39
|
-
const validators = schemaService.getSchemaNames()
|
|
40
|
-
|
|
41
|
-
const missingSchemas: string[] = []
|
|
42
|
-
|
|
43
|
-
for (const route of routesMeta) {
|
|
44
|
-
if (!route.input || validators.has(route.input)) {
|
|
45
|
-
continue
|
|
46
|
-
}
|
|
47
|
-
missingSchemas.push(route.input)
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
if (missingSchemas.length > 0) {
|
|
51
|
-
logger.error(
|
|
52
|
-
`Error: Failed to load schemas:\n.${missingSchemas.join('\n')}`
|
|
53
|
-
)
|
|
54
|
-
logger.error('\tHave you run the schema generation?')
|
|
55
|
-
logger.error('\tnpx @pikku/cli schemas')
|
|
56
|
-
} else {
|
|
57
|
-
logger.info('All schemas loaded')
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export const coerceTopLevelDataFromSchema = (schemaName: string, data: any) => {
|
|
62
|
-
const schema = pikkuState('misc', 'schemas').get(schemaName)
|
|
63
|
-
for (const key in schema.properties) {
|
|
64
|
-
const property = schema.properties[key]
|
|
65
|
-
if (typeof property === 'boolean') {
|
|
66
|
-
continue
|
|
67
|
-
}
|
|
68
|
-
const type = property.type
|
|
69
|
-
if (typeof type === 'boolean') {
|
|
70
|
-
continue
|
|
71
|
-
}
|
|
72
|
-
if (type === 'array' && typeof data[key] === 'string') {
|
|
73
|
-
data[key] = data[key].split(',')
|
|
74
|
-
} else if (type === 'string' && property.format === 'date-time') {
|
|
75
|
-
data[key] = new Date(data[key])
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
export const validateSchema = async (
|
|
81
|
-
logger: Logger,
|
|
82
|
-
schemaService: SchemaService | undefined,
|
|
83
|
-
schemaName: string | undefined | null,
|
|
84
|
-
data: any
|
|
85
|
-
) => {
|
|
86
|
-
if (schemaService) {
|
|
87
|
-
if (!schemaName) {
|
|
88
|
-
if (data && (data.length > 0 || Object.keys(data).length > 0)) {
|
|
89
|
-
logger.warn('No schema provided, but data was passed')
|
|
90
|
-
throw new UnprocessableContentError('No data expected')
|
|
91
|
-
} else {
|
|
92
|
-
return
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
const schema = pikkuState('misc', 'schemas').get(schemaName)
|
|
96
|
-
await schemaService.compileSchema(schemaName, schema)
|
|
97
|
-
await schemaService.validateSchema(schemaName, data)
|
|
98
|
-
}
|
|
99
|
-
}
|
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Interface for handling content operations.
|
|
3
|
-
*/
|
|
4
|
-
export interface ContentService {
|
|
5
|
-
/**
|
|
6
|
-
* Signs a content key to provide secure access.
|
|
7
|
-
* @param contentKey - The key of the content to sign.
|
|
8
|
-
* @returns A promise that resolves to the signed content key URL.
|
|
9
|
-
*/
|
|
10
|
-
signContentKey: (
|
|
11
|
-
contentKey: string,
|
|
12
|
-
dateLessThan: Date,
|
|
13
|
-
dateGreaterThan?: Date
|
|
14
|
-
) => Promise<string>
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Signs a URL to provide secure access.
|
|
18
|
-
* @param url - The URL to sign.
|
|
19
|
-
* @returns A promise that resolves to the signed URL.
|
|
20
|
-
*/
|
|
21
|
-
signURL: (
|
|
22
|
-
url: string,
|
|
23
|
-
dateLessThan: Date,
|
|
24
|
-
dateGreaterThan?: Date
|
|
25
|
-
) => Promise<string>
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Gets an upload URL for a file.
|
|
29
|
-
* @param fileKey - The key of the file to upload.
|
|
30
|
-
* @param contentType - The content type of the file.
|
|
31
|
-
* @returns A promise that resolves to an object containing the upload URL and asset key.
|
|
32
|
-
* This method generates an upload URL for the specified file key and content type, allowing the file to be uploaded.
|
|
33
|
-
*/
|
|
34
|
-
getUploadURL: (
|
|
35
|
-
fileKey: string,
|
|
36
|
-
contentType: string
|
|
37
|
-
) => Promise<{ uploadUrl: string; assetKey: string }>
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Deletes a file.
|
|
41
|
-
* @param fileName - The name of the file to delete.
|
|
42
|
-
* @returns A promise that resolves to a boolean indicating success.
|
|
43
|
-
* This method deletes the specified file and returns a boolean indicating whether the deletion was successful.
|
|
44
|
-
*/
|
|
45
|
-
deleteFile?: (fileName: string) => Promise<boolean>
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Writes a file.
|
|
49
|
-
* @param assetKey - The key of the asset to write.
|
|
50
|
-
* @param buffer - The buffer containing the file data.
|
|
51
|
-
* @returns A promise that resolves to a boolean indicating success.
|
|
52
|
-
*/
|
|
53
|
-
writeFile?: (assetKey: string, buffer: Buffer) => Promise<boolean>
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* This method copies the file from the specified absolute path to the location identified by the asset key and returns a boolean indicating success.
|
|
57
|
-
* @param assetKey - The key of the asset to copy.
|
|
58
|
-
* @param fromAbsolutePath - The absolute path of the source file.
|
|
59
|
-
* @returns A promise that resolves to a boolean indicating success.
|
|
60
|
-
*/
|
|
61
|
-
copyFile?: (assetKey: string, fromAbsolutePath: string) => Promise<boolean>
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Reads a file.
|
|
65
|
-
* @param assetKey - The key of the asset to read.
|
|
66
|
-
* @returns A promise that resolves to a buffer containing the file data.
|
|
67
|
-
*/
|
|
68
|
-
readFile?: (assetKey: string) => Promise<Buffer>
|
|
69
|
-
}
|
package/src/services/index.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @module @pikku/core
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
// Interfaces
|
|
6
|
-
export * from './content-service.js'
|
|
7
|
-
export * from './jwt-service.js'
|
|
8
|
-
export * from './logger.js'
|
|
9
|
-
export * from './logger-console.js'
|
|
10
|
-
export * from './secret-service.js'
|
|
11
|
-
export * from './variables-service.js'
|
|
12
|
-
export * from './schema-service.js'
|
|
13
|
-
export * from './user-session-service.js'
|
|
14
|
-
|
|
15
|
-
// Local implementations
|
|
16
|
-
export * from './local-secrets.js'
|
|
17
|
-
export * from './local-variables.js'
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { RelativeTimeInput } from '../time-utils.js'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Interface for handling JSON Web Tokens (JWT).
|
|
5
|
-
*/
|
|
6
|
-
export interface JWTService {
|
|
7
|
-
/**
|
|
8
|
-
* Encodes a payload into a JWT.
|
|
9
|
-
* @param expiresIn - The expiration time of the token.
|
|
10
|
-
* @param payload - The payload to encode.
|
|
11
|
-
* @returns A promise that resolves to the encoded JWT.
|
|
12
|
-
*/
|
|
13
|
-
encode: <T extends any>(
|
|
14
|
-
expiresIn: RelativeTimeInput,
|
|
15
|
-
payload: T
|
|
16
|
-
) => Promise<string>
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Decodes a JWT into its payload.
|
|
20
|
-
* @param hash - The JWT to decode.
|
|
21
|
-
* @param invalidHashError - An optional error to throw if the hash is invalid.
|
|
22
|
-
* @param debug - An optional flag for debugging.
|
|
23
|
-
* @returns A promise that resolves to the decoded payload.
|
|
24
|
-
*/
|
|
25
|
-
decode: <T>(
|
|
26
|
-
hash: string,
|
|
27
|
-
invalidHashError?: Error,
|
|
28
|
-
debug?: boolean
|
|
29
|
-
) => Promise<T>
|
|
30
|
-
}
|
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
import { promises } from 'fs'
|
|
2
|
-
import { mkdir } from 'fs/promises'
|
|
3
|
-
import { ContentService, Logger } from '@pikku/core/services'
|
|
4
|
-
|
|
5
|
-
export interface LocalContentConfig {
|
|
6
|
-
localFileUploadPath: string
|
|
7
|
-
uploadUrlPrefix: string
|
|
8
|
-
assetUrlPrefix: string
|
|
9
|
-
sizeLimit?: string
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export class LocalContent implements ContentService {
|
|
13
|
-
constructor(
|
|
14
|
-
private config: LocalContentConfig,
|
|
15
|
-
private logger: Logger
|
|
16
|
-
) {}
|
|
17
|
-
|
|
18
|
-
public async init() {}
|
|
19
|
-
|
|
20
|
-
public async signURL(url: string): Promise<string> {
|
|
21
|
-
return `${url}?signed=true`
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
public async signContentKey(assetKey: string): Promise<string> {
|
|
25
|
-
return `${this.config.assetUrlPrefix}/${assetKey}?signed=true`
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
public async getUploadURL(assetKey: string) {
|
|
29
|
-
this.logger.debug(`Going to upload with key: ${assetKey}`)
|
|
30
|
-
return {
|
|
31
|
-
uploadUrl: `${this.config.uploadUrlPrefix}/${assetKey}`,
|
|
32
|
-
assetKey,
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
public async writeFile(assetKey: string, buffer: Buffer): Promise<boolean> {
|
|
37
|
-
this.logger.debug(`Writing file: ${assetKey}`)
|
|
38
|
-
try {
|
|
39
|
-
const path = `${this.config.localFileUploadPath}/${assetKey}`
|
|
40
|
-
await this.createDirectoryForFile(path)
|
|
41
|
-
await promises.writeFile(path, buffer)
|
|
42
|
-
} catch (e) {
|
|
43
|
-
console.error(e)
|
|
44
|
-
this.logger.error(`Error inserting content ${assetKey}`, e)
|
|
45
|
-
}
|
|
46
|
-
return false
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
public async copyFile(
|
|
50
|
-
assetKey: string,
|
|
51
|
-
fromAbsolutePath: string
|
|
52
|
-
): Promise<boolean> {
|
|
53
|
-
this.logger.debug(`Writing file: ${assetKey}`)
|
|
54
|
-
try {
|
|
55
|
-
const path = `${this.config.localFileUploadPath}/${assetKey}`
|
|
56
|
-
await this.createDirectoryForFile(path)
|
|
57
|
-
await promises.copyFile(fromAbsolutePath, path)
|
|
58
|
-
} catch (e) {
|
|
59
|
-
console.error(e)
|
|
60
|
-
this.logger.error(`Error inserting content ${assetKey}`, e)
|
|
61
|
-
}
|
|
62
|
-
return false
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
public async readFile(assetKey: string): Promise<Buffer> {
|
|
66
|
-
this.logger.debug(`getting key: ${assetKey}`)
|
|
67
|
-
try {
|
|
68
|
-
return await promises.readFile(
|
|
69
|
-
`${this.config.localFileUploadPath}/${assetKey}`
|
|
70
|
-
)
|
|
71
|
-
} catch (e) {
|
|
72
|
-
this.logger.error(`Error get content ${assetKey}`)
|
|
73
|
-
throw e
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
public async deleteFile(assetKey: string): Promise<boolean> {
|
|
78
|
-
this.logger.debug(`deleting key: ${assetKey}`)
|
|
79
|
-
try {
|
|
80
|
-
await promises.unlink(`${this.config.localFileUploadPath}/${assetKey}`)
|
|
81
|
-
} catch (e: any) {
|
|
82
|
-
this.logger.error(`Error deleting content ${assetKey}`, e)
|
|
83
|
-
}
|
|
84
|
-
return false
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
private async createDirectoryForFile(path: string): Promise<void> {
|
|
88
|
-
const dir = path.split('/').slice(0, -1).join('/')
|
|
89
|
-
await mkdir(dir, { recursive: true })
|
|
90
|
-
}
|
|
91
|
-
}
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { LocalVariablesService } from './local-variables.js'
|
|
2
|
-
import { SecretService } from './secret-service.js'
|
|
3
|
-
import { VariablesService } from './variables-service.js'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Service for retrieving secrets from environment variables.
|
|
7
|
-
*/
|
|
8
|
-
export class LocalSecretService implements SecretService {
|
|
9
|
-
/**
|
|
10
|
-
* Creates an instance of LocalSecretService.
|
|
11
|
-
*/
|
|
12
|
-
constructor(
|
|
13
|
-
private variables: VariablesService = new LocalVariablesService()
|
|
14
|
-
) {}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Retrieves a secret by key.
|
|
18
|
-
* @param key - The key of the secret to retrieve.
|
|
19
|
-
* @returns A promise that resolves to the secret value.
|
|
20
|
-
* @throws {Error} If the secret is not found.
|
|
21
|
-
*/
|
|
22
|
-
public async getSecretJSON<R>(key: string): Promise<R> {
|
|
23
|
-
const value = await this.variables.get(key)
|
|
24
|
-
if (value) {
|
|
25
|
-
return JSON.parse(value)
|
|
26
|
-
}
|
|
27
|
-
throw new Error(`Secret Not Found: ${key}`)
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Retrieves a secret by key.
|
|
32
|
-
* @param key - The key of the secret to retrieve.
|
|
33
|
-
* @returns A promise that resolves to the secret value.
|
|
34
|
-
* @throws {Error} If the secret is not found.
|
|
35
|
-
*/
|
|
36
|
-
public async getSecret(key: string): Promise<string> {
|
|
37
|
-
const value = await this.variables.get(key)
|
|
38
|
-
if (value) {
|
|
39
|
-
return value
|
|
40
|
-
}
|
|
41
|
-
throw new Error(`Secret Not Found: ${key}`)
|
|
42
|
-
}
|
|
43
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { VariablesService } from './variables-service.js'
|
|
2
|
-
|
|
3
|
-
export class LocalVariablesService implements VariablesService {
|
|
4
|
-
constructor(
|
|
5
|
-
private variables: Record<string, string | undefined> = process.env
|
|
6
|
-
) {}
|
|
7
|
-
|
|
8
|
-
public getAll():
|
|
9
|
-
| Promise<Record<string, string | undefined>>
|
|
10
|
-
| Record<string, string | undefined> {
|
|
11
|
-
return this.variables || {}
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
public get(name: string): Promise<string | undefined> | string | undefined {
|
|
15
|
-
return this.variables[name]
|
|
16
|
-
}
|
|
17
|
-
}
|
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
import { Logger, LogLevel } from './logger.js'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* A logger implementation that logs messages to the console.
|
|
5
|
-
*/
|
|
6
|
-
export class ConsoleLogger implements Logger {
|
|
7
|
-
/**
|
|
8
|
-
* The current logging level.
|
|
9
|
-
*/
|
|
10
|
-
private level: LogLevel = LogLevel.info
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Sets the logging level.
|
|
14
|
-
* @param level - The logging level to set.
|
|
15
|
-
*/
|
|
16
|
-
setLevel(level: LogLevel): void {
|
|
17
|
-
this.level = level
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Logs a trace message.
|
|
22
|
-
* @param message - The message to log.
|
|
23
|
-
* @param meta - Additional metadata to log.
|
|
24
|
-
*/
|
|
25
|
-
trace?(message: string, ...meta: any[]): void {
|
|
26
|
-
if (this.level <= LogLevel.trace) {
|
|
27
|
-
console.trace('TRACE:', message, ...meta)
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Logs a debug message.
|
|
33
|
-
* @param message - The message to log.
|
|
34
|
-
* @param meta - Additional metadata to log.
|
|
35
|
-
*/
|
|
36
|
-
debug(message: string, ...meta: any[]): void {
|
|
37
|
-
if (this.level <= LogLevel.debug) {
|
|
38
|
-
console.debug('DEBUG:', message, ...meta)
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Logs an informational message.
|
|
44
|
-
* @param messageOrObj - The message or object to log.
|
|
45
|
-
* @param meta - Additional metadata to log.
|
|
46
|
-
*/
|
|
47
|
-
info(messageOrObj: string | Record<string, any>, ...meta: any[]): void {
|
|
48
|
-
if (this.level <= LogLevel.info) {
|
|
49
|
-
console.info('INFO:', messageOrObj, ...meta)
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Logs a warning message.
|
|
55
|
-
* @param messageOrObj - The message or object to log.
|
|
56
|
-
* @param meta - Additional metadata to log.
|
|
57
|
-
*/
|
|
58
|
-
warn(messageOrObj: string | Record<string, any>, ...meta: any[]): void {
|
|
59
|
-
if (this.level <= LogLevel.warn) {
|
|
60
|
-
console.warn('WARN:', messageOrObj, ...meta)
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Logs an error message.
|
|
66
|
-
* @param messageOrObj - The message, object, or error to log.
|
|
67
|
-
* @param meta - Additional metadata to log.
|
|
68
|
-
*/
|
|
69
|
-
error(
|
|
70
|
-
messageOrObj: string | Record<string, any> | Error,
|
|
71
|
-
...meta: any[]
|
|
72
|
-
): void {
|
|
73
|
-
if (this.level <= LogLevel.error) {
|
|
74
|
-
console.error(
|
|
75
|
-
'ERROR:',
|
|
76
|
-
messageOrObj instanceof Error ? messageOrObj.message : messageOrObj,
|
|
77
|
-
...meta
|
|
78
|
-
)
|
|
79
|
-
if (messageOrObj instanceof Error) {
|
|
80
|
-
console.error('STACK:', messageOrObj.stack)
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Logs a message at a specified level.
|
|
87
|
-
* @param level - The logging level.
|
|
88
|
-
* @param message - The message to log.
|
|
89
|
-
* @param meta - Additional metadata to log.
|
|
90
|
-
*/
|
|
91
|
-
log(level: string, message: string, ...meta: any[]): void {
|
|
92
|
-
const logLevel = LogLevel[level as keyof typeof LogLevel]
|
|
93
|
-
if (this.level <= logLevel) {
|
|
94
|
-
console.log(`${level.toUpperCase()}:`, message, ...meta)
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|