@sanity/functions 1.3.1 → 1.5.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/dist/definers.d.ts +9 -3
- package/dist/definers.js +12 -2
- package/dist/definers.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/invoke.d.ts +18 -0
- package/dist/invoke.js +117 -0
- package/dist/invoke.js.map +1 -0
- package/dist/pipelines.d.ts +32 -0
- package/dist/pipelines.js +74 -0
- package/dist/pipelines.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types.d.ts +135 -24
- package/package.json +19 -3
- package/src/aws-lite-plugins.d.ts +4 -0
- package/src/definers.ts +13 -3
- package/src/index.ts +2 -0
- package/src/invoke.ts +135 -0
- package/src/pipelines.ts +107 -0
- package/src/types.ts +145 -24
package/dist/definers.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { DocumentEventHandler, ScheduledEventHandler, SyncTagInvalidateEventHandler } from './types.js';
|
|
1
|
+
import type { DocumentEventHandler, EventHandler, ScheduledEventHandler, SyncTagInvalidateEventHandler } from './types.js';
|
|
2
2
|
/**
|
|
3
3
|
* Defines a "document event" function handler.
|
|
4
4
|
* Returns the handler function as-is, only providing the types and doing basic validation.
|
|
@@ -11,8 +11,6 @@ export declare function documentEventHandler<IData = any>(handler: DocumentEvent
|
|
|
11
11
|
* Defines a "scheduled event" function handler.
|
|
12
12
|
* Returns the handler function as-is, only providing the types and doing basic validation.
|
|
13
13
|
*
|
|
14
|
-
* @alpha
|
|
15
|
-
* @hidden
|
|
16
14
|
* @param handler - The event handler function to use.
|
|
17
15
|
* @returns The handler function, unmodified.
|
|
18
16
|
*/
|
|
@@ -25,3 +23,11 @@ export declare function scheduledEventHandler(handler: ScheduledEventHandler): S
|
|
|
25
23
|
* @returns The handler function, unmodified.
|
|
26
24
|
*/
|
|
27
25
|
export declare function syncTagInvalidateEventHandler(handler: SyncTagInvalidateEventHandler): SyncTagInvalidateEventHandler;
|
|
26
|
+
/**
|
|
27
|
+
* Defines a "generic" function handler.
|
|
28
|
+
* Returns the handler function as-is, only providing the types and doing basic validation.
|
|
29
|
+
*
|
|
30
|
+
* @param handler - The event handler function to use.
|
|
31
|
+
* @returns The handler function, unmodified.
|
|
32
|
+
*/
|
|
33
|
+
export declare function eventHandler<IData = any>(handler: EventHandler<IData>): EventHandler<IData>;
|
package/dist/definers.js
CHANGED
|
@@ -14,8 +14,6 @@ export function documentEventHandler(handler) {
|
|
|
14
14
|
* Defines a "scheduled event" function handler.
|
|
15
15
|
* Returns the handler function as-is, only providing the types and doing basic validation.
|
|
16
16
|
*
|
|
17
|
-
* @alpha
|
|
18
|
-
* @hidden
|
|
19
17
|
* @param handler - The event handler function to use.
|
|
20
18
|
* @returns The handler function, unmodified.
|
|
21
19
|
*/
|
|
@@ -36,4 +34,16 @@ export function syncTagInvalidateEventHandler(handler) {
|
|
|
36
34
|
throw new TypeError('`handler` must be a function');
|
|
37
35
|
return handler;
|
|
38
36
|
}
|
|
37
|
+
/**
|
|
38
|
+
* Defines a "generic" function handler.
|
|
39
|
+
* Returns the handler function as-is, only providing the types and doing basic validation.
|
|
40
|
+
*
|
|
41
|
+
* @param handler - The event handler function to use.
|
|
42
|
+
* @returns The handler function, unmodified.
|
|
43
|
+
*/
|
|
44
|
+
export function eventHandler(handler) {
|
|
45
|
+
if (typeof handler !== 'function')
|
|
46
|
+
throw new TypeError('`handler` must be a function');
|
|
47
|
+
return handler;
|
|
48
|
+
}
|
|
39
49
|
//# sourceMappingURL=definers.js.map
|
package/dist/definers.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"definers.js","sourceRoot":"","sources":["../src/definers.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,MAAM,UAAU,oBAAoB,CAAc,OAAoC;IACpF,IAAI,OAAO,OAAO,KAAK,UAAU;QAAE,MAAM,IAAI,SAAS,CAAC,8BAA8B,CAAC,CAAA;IACtF,OAAO,OAAO,CAAA;AAChB,CAAC;AAED
|
|
1
|
+
{"version":3,"file":"definers.js","sourceRoot":"","sources":["../src/definers.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,MAAM,UAAU,oBAAoB,CAAc,OAAoC;IACpF,IAAI,OAAO,OAAO,KAAK,UAAU;QAAE,MAAM,IAAI,SAAS,CAAC,8BAA8B,CAAC,CAAA;IACtF,OAAO,OAAO,CAAA;AAChB,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,qBAAqB,CAAC,OAA8B;IAClE,IAAI,OAAO,OAAO,KAAK,UAAU;QAAE,MAAM,IAAI,SAAS,CAAC,8BAA8B,CAAC,CAAA;IACtF,OAAO,OAAO,CAAA;AAChB,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,6BAA6B,CAAC,OAAsC;IAClF,IAAI,OAAO,OAAO,KAAK,UAAU;QAAE,MAAM,IAAI,SAAS,CAAC,8BAA8B,CAAC,CAAA;IACtF,OAAO,OAAO,CAAA;AAChB,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,YAAY,CAAc,OAA4B;IACpE,IAAI,OAAO,OAAO,KAAK,UAAU;QAAE,MAAM,IAAI,SAAS,CAAC,8BAA8B,CAAC,CAAA;IACtF,OAAO,OAAO,CAAA;AAChB,CAAC"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAA;AAC7B,cAAc,aAAa,CAAA;AAC3B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,YAAY,CAAA"}
|
package/dist/invoke.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { FunctionPayload, InvokeOptions } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Invokes another Sanity Function.
|
|
4
|
+
*
|
|
5
|
+
* By default the invocation is async: the payload is handed off to the function's
|
|
6
|
+
* event source and nothing is returned. Pass `{sync: true}` to invoke the function and
|
|
7
|
+
* wait for its return value.
|
|
8
|
+
*
|
|
9
|
+
* @param name - Name of the function to invoke
|
|
10
|
+
* @param payload - The `{event, context}` envelope to deliver
|
|
11
|
+
* @param options - Set `sync: true` to wait for and return the function's result
|
|
12
|
+
*/
|
|
13
|
+
export declare function invoke<T = unknown>(name: string, payload: FunctionPayload, options: InvokeOptions & {
|
|
14
|
+
sync: true;
|
|
15
|
+
}): Promise<T>;
|
|
16
|
+
export declare function invoke(name: string, payload: FunctionPayload, options?: InvokeOptions & {
|
|
17
|
+
sync?: false;
|
|
18
|
+
}): Promise<void>;
|
package/dist/invoke.js
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { Buffer } from 'node:buffer';
|
|
2
|
+
import { env } from 'node:process';
|
|
3
|
+
let awsPromise;
|
|
4
|
+
const PARTITION_KEY = 'arc-app-res';
|
|
5
|
+
/**
|
|
6
|
+
* lazy load aws-lite
|
|
7
|
+
*/
|
|
8
|
+
function getAwsLite() {
|
|
9
|
+
if (!awsPromise) {
|
|
10
|
+
awsPromise = (async () => {
|
|
11
|
+
const { default: awsLite } = await import('@aws-lite/client');
|
|
12
|
+
return awsLite({
|
|
13
|
+
plugins: [import('@aws-lite/dynamodb'), import('@aws-lite/lambda'), import('@aws-lite/sns'), import('@aws-lite/sqs')],
|
|
14
|
+
});
|
|
15
|
+
})().catch((err) => {
|
|
16
|
+
awsPromise = undefined;
|
|
17
|
+
throw err;
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
return awsPromise;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Gets a specific resource based off of function name
|
|
24
|
+
* @param {string} name
|
|
25
|
+
*/
|
|
26
|
+
async function getResource(name, aws) {
|
|
27
|
+
const TableName = env['SANITY_DISCO'];
|
|
28
|
+
if (!TableName)
|
|
29
|
+
throw new Error('SANITY_DISCO env var not set');
|
|
30
|
+
const result = await aws.DynamoDB.GetItem({
|
|
31
|
+
TableName,
|
|
32
|
+
Key: {
|
|
33
|
+
PK: PARTITION_KEY,
|
|
34
|
+
SK: name,
|
|
35
|
+
},
|
|
36
|
+
ProjectionExpression: 'resources',
|
|
37
|
+
});
|
|
38
|
+
if (!result?.Item)
|
|
39
|
+
throw new Error(`Function not found: ${name}`);
|
|
40
|
+
if (!result.Item['resources'])
|
|
41
|
+
throw new Error(`Resource record for ${name} is missing resources`);
|
|
42
|
+
return result.Item['resources'];
|
|
43
|
+
}
|
|
44
|
+
export async function invoke(name, payload, options) {
|
|
45
|
+
if (!name)
|
|
46
|
+
throw new Error('Function name was not provided');
|
|
47
|
+
const sync = options?.sync ?? false;
|
|
48
|
+
const stringPayload = JSON.stringify(payload);
|
|
49
|
+
// Check to make sure payload is not over the max we can handle
|
|
50
|
+
checkPayloadSize(stringPayload, sync);
|
|
51
|
+
// Local invoke path for Sanity CLI
|
|
52
|
+
if (payload?.context?.local) {
|
|
53
|
+
if (!payload?.context?.invoke) {
|
|
54
|
+
throw new Error(`No local invoke handler configured for function: ${name}`);
|
|
55
|
+
}
|
|
56
|
+
return await payload.context.invoke(name, payload, options);
|
|
57
|
+
}
|
|
58
|
+
const aws = await getAwsLite();
|
|
59
|
+
if (!aws)
|
|
60
|
+
throw new Error(`Unable to invoke function: ${name}`);
|
|
61
|
+
// Look up the function details
|
|
62
|
+
const resource = await getResource(name, aws);
|
|
63
|
+
// Synchronous invocation
|
|
64
|
+
if (sync === true) {
|
|
65
|
+
if (!resource.function) {
|
|
66
|
+
throw new Error(`Function ${name} cannot be invoked synchronously.`);
|
|
67
|
+
}
|
|
68
|
+
const { Payload, FunctionError } = await aws.Lambda.Invoke({
|
|
69
|
+
FunctionName: resource.function.physicalResourceId,
|
|
70
|
+
Payload: payload,
|
|
71
|
+
InvocationType: 'RequestResponse',
|
|
72
|
+
});
|
|
73
|
+
if (FunctionError) {
|
|
74
|
+
const detail = typeof Payload === 'object' && Payload !== null ? Payload.errorMessage : undefined;
|
|
75
|
+
throw new Error(`Function ${name} failed: ${detail ?? FunctionError}`);
|
|
76
|
+
}
|
|
77
|
+
// TODO: can we not type cast? might be an aws-lite issue, why is the return type UInt8BlobAdapter
|
|
78
|
+
return Payload;
|
|
79
|
+
}
|
|
80
|
+
// Async invocation by type
|
|
81
|
+
if (resource.topic) {
|
|
82
|
+
await aws.SNS.Publish({
|
|
83
|
+
TopicArn: resource.topic.physicalResourceId,
|
|
84
|
+
Message: stringPayload,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
else if (resource.queue) {
|
|
88
|
+
await aws.SQS.SendMessage({
|
|
89
|
+
MessageBody: stringPayload,
|
|
90
|
+
QueueUrl: resource.queue.physicalResourceId,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
else if (resource.function) {
|
|
94
|
+
await aws.Lambda.Invoke({
|
|
95
|
+
FunctionName: resource.function.physicalResourceId,
|
|
96
|
+
Payload: payload,
|
|
97
|
+
InvocationType: 'Event',
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
throw new Error(`No invokeable resource for function: ${name}`);
|
|
102
|
+
}
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
function checkPayloadSize(payload, sync) {
|
|
106
|
+
if (sync) {
|
|
107
|
+
if (Buffer.byteLength(payload, 'utf8') > 6 * 1024 * 1024) {
|
|
108
|
+
throw new Error(`Payload exceeds maximum size of 6MB`);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
if (Buffer.byteLength(payload, 'utf8') > 256 * 1024) {
|
|
113
|
+
throw new Error(`Payload exceeds maximum size of 256KB`);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
//# sourceMappingURL=invoke.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"invoke.js","sourceRoot":"","sources":["../src/invoke.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,MAAM,EAAC,MAAM,aAAa,CAAA;AAClC,OAAO,EAAC,GAAG,EAAC,MAAM,cAAc,CAAA;AAIhC,IAAI,UAAsD,CAAA;AAE1D,MAAM,aAAa,GAAG,aAAa,CAAA;AAEnC;;GAEG;AACH,SAAS,UAAU;IACjB,IAAI,CAAC,UAAU,EAAE,CAAC;QAChB,UAAU,GAAG,CAAC,KAAK,IAAI,EAAE;YACvB,MAAM,EAAC,OAAO,EAAE,OAAO,EAAC,GAAG,MAAM,MAAM,CAAC,kBAAkB,CAAC,CAAA;YAC3D,OAAO,OAAO,CAAC;gBACb,OAAO,EAAE,CAAC,MAAM,CAAC,oBAAoB,CAAC,EAAE,MAAM,CAAC,kBAAkB,CAAC,EAAE,MAAM,CAAC,eAAe,CAAC,EAAE,MAAM,CAAC,eAAe,CAAC,CAAC;aACtH,CAAC,CAAA;QACJ,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;YACjB,UAAU,GAAG,SAAS,CAAA;YACtB,MAAM,GAAG,CAAA;QACX,CAAC,CAAC,CAAA;IACJ,CAAC;IACD,OAAO,UAAU,CAAA;AACnB,CAAC;AAED;;;GAGG;AACH,KAAK,UAAU,WAAW,CAAC,IAAY,EAAE,GAA0B;IACjE,MAAM,SAAS,GAAG,GAAG,CAAC,cAAc,CAAC,CAAA;IACrC,IAAI,CAAC,SAAS;QAAE,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAA;IAE/D,MAAM,MAAM,GAAG,MAAM,GAAG,CAAC,QAAQ,CAAC,OAAO,CAAC;QACxC,SAAS;QACT,GAAG,EAAE;YACH,EA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,MAAM,CAAC;YACtB,YAAY,EAAE,QAAQ,CAAC,QAAQ,CAAC,kBAAkB;YAClD,OAAO,EAAE,OAAO;YAChB,cAAc,EAAE,OAAO;SACxB,CAAC,CAAA;IACJ,CAAC;SAAM,CAAC;QACN,MAAM,IAAI,KAAK,CAAC,wCAAwC,IAAI,EAAE,CAAC,CAAA;IACjE,CAAC;IACD,OAAM;AACR,CAAC;AAED,SAAS,gBAAgB,CAAC,OAAe,EAAE,IAAa;IACtD,IAAI,IAAI,EAAE,CAAC;QACT,IAAI,MAAM,CAAC,UAAU,CAAC,OAAO,EAAE,MAAM,CAAC,GAAG,CAAC,GAAG,IAAI,GAAG,IAAI,EAAE,CAAC;YACzD,MAAM,IAAI,KAAK,CAAC,qCAAqC,CAAC,CAAA;QACxD,CAAC;IACH,CAAC;SAAM,CAAC;QACN,IAAI,MAAM,CAAC,UAAU,CAAC,OAAO,EAAE,MAAM,CAAC,GAAG,GAAG,GAAG,IAAI,EAAE,CAAC;YACpD,MAAM,IAAI,KAAK,CAAC,uCAAuC,CAAC,CAAA;QAC1D,CAAC;IACH,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { PipelineHandler } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Pipeline creation function that can be called with or without a config object.
|
|
4
|
+
* @alpha Pipelines are an experimental feature and may change in the future.
|
|
5
|
+
* @hidden
|
|
6
|
+
* @param handler
|
|
7
|
+
* @returns The handler function, unmodified.
|
|
8
|
+
*/
|
|
9
|
+
export declare function createPipeline(handler: PipelineHandler): PipelineHandler & {
|
|
10
|
+
config?: undefined;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Pipeline creation function that can be called with or without a config object.
|
|
14
|
+
* @alpha Pipelines are an experimental feature and may change in the future.
|
|
15
|
+
* @hidden
|
|
16
|
+
* @param config
|
|
17
|
+
* @param handler
|
|
18
|
+
* @returns The handler function, unmodified and the provided config object.
|
|
19
|
+
*/
|
|
20
|
+
export declare function createPipeline<TConfig extends {
|
|
21
|
+
name: string;
|
|
22
|
+
}>(config: TConfig, handler: PipelineHandler): PipelineHandler & {
|
|
23
|
+
config: TConfig;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* @alpha Pipelines are an experimental feature and may change in the future.
|
|
27
|
+
* @hidden
|
|
28
|
+
* @public
|
|
29
|
+
*/
|
|
30
|
+
export declare const pipeline: {
|
|
31
|
+
createPipeline: typeof createPipeline;
|
|
32
|
+
};
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Determine if `createPipeline({}, () => {})` or `createPipeline(() => {})`.
|
|
3
|
+
* @param configOrHandler
|
|
4
|
+
* @param maybeHandler
|
|
5
|
+
* @returns resolved arguments
|
|
6
|
+
*/
|
|
7
|
+
const resolveFuncArgs = (configOrHandler, maybeHandler) => {
|
|
8
|
+
return typeof configOrHandler === 'function'
|
|
9
|
+
? { config: undefined, handler: configOrHandler, hadConfigArg: false }
|
|
10
|
+
: { config: configOrHandler, handler: maybeHandler, hadConfigArg: true };
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Validates the config to ensure config contains proper elements
|
|
14
|
+
* @param config
|
|
15
|
+
* @returns Errors while validating a config object
|
|
16
|
+
*/
|
|
17
|
+
const validateConfig = (config) => {
|
|
18
|
+
const EVENT_TYPES = ['document', 'media-library', 'cron', 'sync-tag-invalidate'];
|
|
19
|
+
const errors = [];
|
|
20
|
+
if (config === undefined || config === null) {
|
|
21
|
+
errors.push('`config` must be defined');
|
|
22
|
+
return errors;
|
|
23
|
+
}
|
|
24
|
+
if (typeof config !== 'object') {
|
|
25
|
+
errors.push('`config` must be an object');
|
|
26
|
+
return errors;
|
|
27
|
+
}
|
|
28
|
+
const name = 'name' in config ? config.name : undefined;
|
|
29
|
+
const event = 'event' in config ? config.event : undefined;
|
|
30
|
+
if (typeof name !== 'string')
|
|
31
|
+
errors.push('`config.name` must be a string');
|
|
32
|
+
if ('event' in config) {
|
|
33
|
+
if (typeof event !== 'object' || event === null) {
|
|
34
|
+
errors.push('`event` must be an object');
|
|
35
|
+
}
|
|
36
|
+
else if ('type' in event) {
|
|
37
|
+
if (typeof event.type !== 'string') {
|
|
38
|
+
errors.push('`event.type` must be defined');
|
|
39
|
+
}
|
|
40
|
+
else if (!EVENT_TYPES.includes(event.type)) {
|
|
41
|
+
errors.push(`\`event.type\` must be one of: ${EVENT_TYPES.join(', ')}`);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return errors;
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Pipeline creation function that can be called with or without a config object.
|
|
49
|
+
* @alpha Pipelines are an experimental feature and may change in the future.
|
|
50
|
+
* @hidden
|
|
51
|
+
* @public
|
|
52
|
+
* @param configOrHandler
|
|
53
|
+
* @param maybeHandler
|
|
54
|
+
* @returns The handler function, unmodified and the config object if provided
|
|
55
|
+
*/
|
|
56
|
+
export function createPipeline(configOrHandler, maybeHandler) {
|
|
57
|
+
const { config, handler, hadConfigArg } = resolveFuncArgs(configOrHandler, maybeHandler);
|
|
58
|
+
const errors = [
|
|
59
|
+
...(hadConfigArg ? validateConfig(config) : []),
|
|
60
|
+
...(typeof handler !== 'function' ? ['`handler` must be a function'] : []),
|
|
61
|
+
];
|
|
62
|
+
if (errors.length > 0) {
|
|
63
|
+
throw new TypeError(errors.join(', '));
|
|
64
|
+
}
|
|
65
|
+
// Separate config from the handler during build
|
|
66
|
+
return Object.assign(handler, { config });
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* @alpha Pipelines are an experimental feature and may change in the future.
|
|
70
|
+
* @hidden
|
|
71
|
+
* @public
|
|
72
|
+
*/
|
|
73
|
+
export const pipeline = { createPipeline };
|
|
74
|
+
//# sourceMappingURL=pipelines.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pipelines.js","sourceRoot":"","sources":["../src/pipelines.ts"],"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