@shushed/helpers 0.0.84 → 0.0.85
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/index.d.ts +9 -3
- package/dist/index.js +84 -33
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -7,7 +7,6 @@ import { Context } from 'co-body';
|
|
|
7
7
|
import * as _google_cloud_scheduler_build_protos_protos from '@google-cloud/scheduler/build/protos/protos';
|
|
8
8
|
import { CloudSchedulerClient } from '@google-cloud/scheduler';
|
|
9
9
|
import { SecretManagerServiceClient } from '@google-cloud/secret-manager';
|
|
10
|
-
import { CloudTasksClient } from '@google-cloud/tasks';
|
|
11
10
|
import { BigQuery } from '@google-cloud/bigquery';
|
|
12
11
|
|
|
13
12
|
declare const schema$p: {
|
|
@@ -29733,6 +29732,7 @@ declare class Secrets extends Runtime {
|
|
|
29733
29732
|
interface CloudTasksHelperOpts {
|
|
29734
29733
|
projectId: string;
|
|
29735
29734
|
serviceAccount: string;
|
|
29735
|
+
accessToken: string;
|
|
29736
29736
|
envName: string;
|
|
29737
29737
|
workflowId: string;
|
|
29738
29738
|
triggerId: string;
|
|
@@ -29760,16 +29760,22 @@ interface CreateTaskOpts {
|
|
|
29760
29760
|
};
|
|
29761
29761
|
}
|
|
29762
29762
|
declare class CloudTasksHelper extends Runtime {
|
|
29763
|
-
private client;
|
|
29764
29763
|
private projectId;
|
|
29765
29764
|
private serviceAccount;
|
|
29765
|
+
private accessToken;
|
|
29766
29766
|
envName: string;
|
|
29767
29767
|
workflowId: string;
|
|
29768
29768
|
triggerId: string;
|
|
29769
29769
|
private location;
|
|
29770
|
-
constructor(opts: CloudTasksHelperOpts
|
|
29770
|
+
constructor(opts: CloudTasksHelperOpts);
|
|
29771
29771
|
private getQueueName;
|
|
29772
29772
|
private getQueuePath;
|
|
29773
|
+
private getQueueApiUrl;
|
|
29774
|
+
private getLocationApiUrl;
|
|
29775
|
+
private queueExists;
|
|
29776
|
+
private createOrUpdateQueue;
|
|
29777
|
+
private listTasks;
|
|
29778
|
+
private deleteTask;
|
|
29773
29779
|
create(opts: CreateTaskOpts): Promise<false | string[]>;
|
|
29774
29780
|
delete(topicName: string): Promise<boolean>;
|
|
29775
29781
|
}
|
package/dist/index.js
CHANGED
|
@@ -110504,19 +110504,44 @@ var secret_default = Secrets;
|
|
|
110504
110504
|
|
|
110505
110505
|
// src-public/cloudtasks.ts
|
|
110506
110506
|
var import_crypto2 = __toESM(require("crypto"));
|
|
110507
|
+
async function gcpFetch(url, accessToken, options = {}) {
|
|
110508
|
+
let res;
|
|
110509
|
+
try {
|
|
110510
|
+
res = await fetch(url, {
|
|
110511
|
+
...options,
|
|
110512
|
+
headers: {
|
|
110513
|
+
...options.headers || {},
|
|
110514
|
+
"Authorization": `Bearer ${accessToken}`,
|
|
110515
|
+
"Content-Type": "application/json"
|
|
110516
|
+
}
|
|
110517
|
+
});
|
|
110518
|
+
if (!res.ok) {
|
|
110519
|
+
const err = new Error(`GCP API error: ${res.status} ${res.statusText}`);
|
|
110520
|
+
const body = await res.json().catch(() => ({
|
|
110521
|
+
code: res.status
|
|
110522
|
+
}));
|
|
110523
|
+
err.message = body.error?.message || err.message;
|
|
110524
|
+
err.code = body.error?.code || res.status;
|
|
110525
|
+
throw err;
|
|
110526
|
+
}
|
|
110527
|
+
} catch (err) {
|
|
110528
|
+
throw new Error(`GCP API error: ${err.message}`);
|
|
110529
|
+
}
|
|
110530
|
+
return res.json();
|
|
110531
|
+
}
|
|
110507
110532
|
var CloudTasksHelper = class extends Runtime {
|
|
110508
|
-
client;
|
|
110509
110533
|
projectId;
|
|
110510
110534
|
serviceAccount;
|
|
110535
|
+
accessToken;
|
|
110511
110536
|
envName;
|
|
110512
110537
|
workflowId;
|
|
110513
110538
|
triggerId;
|
|
110514
110539
|
location = "europe-west2";
|
|
110515
|
-
constructor(opts
|
|
110540
|
+
constructor(opts) {
|
|
110516
110541
|
super(opts);
|
|
110517
|
-
this.client = cloudTasksClient;
|
|
110518
110542
|
this.projectId = opts.projectId;
|
|
110519
110543
|
this.serviceAccount = opts.serviceAccount;
|
|
110544
|
+
this.accessToken = opts.accessToken;
|
|
110520
110545
|
this.envName = opts.envName;
|
|
110521
110546
|
this.workflowId = opts.workflowId;
|
|
110522
110547
|
this.triggerId = opts.triggerId;
|
|
@@ -110526,36 +110551,60 @@ var CloudTasksHelper = class extends Runtime {
|
|
|
110526
110551
|
return `${infraPrefix}-${shortHash(this.triggerId)}`;
|
|
110527
110552
|
}
|
|
110528
110553
|
getQueuePath(topicName) {
|
|
110529
|
-
return this.
|
|
110554
|
+
return `projects/${this.projectId}/locations/${this.location}/queues/${this.getQueueName(topicName)}`;
|
|
110530
110555
|
}
|
|
110531
|
-
|
|
110532
|
-
|
|
110533
|
-
|
|
110534
|
-
|
|
110556
|
+
getQueueApiUrl(topicName) {
|
|
110557
|
+
return `https://cloudtasks.googleapis.com/v2/${this.getQueuePath(topicName)}`;
|
|
110558
|
+
}
|
|
110559
|
+
getLocationApiUrl() {
|
|
110560
|
+
return `https://cloudtasks.googleapis.com/v2/projects/${this.projectId}/locations/${this.location}`;
|
|
110561
|
+
}
|
|
110562
|
+
async queueExists(topicName) {
|
|
110535
110563
|
try {
|
|
110536
|
-
|
|
110537
|
-
|
|
110564
|
+
await gcpFetch(this.getQueueApiUrl(topicName), this.accessToken, { method: "GET" });
|
|
110565
|
+
return true;
|
|
110538
110566
|
} catch (err) {
|
|
110539
|
-
if (err.code
|
|
110567
|
+
if (err.code === 5 || err.message?.includes("notFound")) return false;
|
|
110568
|
+
throw err;
|
|
110540
110569
|
}
|
|
110570
|
+
}
|
|
110571
|
+
async createOrUpdateQueue(topicName, retryConfig) {
|
|
110572
|
+
const queuePath = this.getQueuePath(topicName);
|
|
110573
|
+
const queueApiUrl = this.getQueueApiUrl(topicName);
|
|
110574
|
+
const locationApiUrl = this.getLocationApiUrl();
|
|
110541
110575
|
const queueConfig = {
|
|
110542
110576
|
name: queuePath,
|
|
110543
|
-
retryConfig:
|
|
110544
|
-
appEngineRoutingOverride: void 0
|
|
110577
|
+
retryConfig: retryConfig || { maxAttempts: 1 }
|
|
110545
110578
|
};
|
|
110546
|
-
if (!queueExists) {
|
|
110547
|
-
await this.
|
|
110548
|
-
|
|
110549
|
-
|
|
110579
|
+
if (!await this.queueExists(topicName)) {
|
|
110580
|
+
await gcpFetch(`${locationApiUrl}/queues`, this.accessToken, {
|
|
110581
|
+
method: "POST",
|
|
110582
|
+
body: JSON.stringify(queueConfig)
|
|
110583
|
+
});
|
|
110584
|
+
} else {
|
|
110585
|
+
await gcpFetch(queueApiUrl, this.accessToken, {
|
|
110586
|
+
method: "PATCH",
|
|
110587
|
+
body: JSON.stringify(queueConfig)
|
|
110550
110588
|
});
|
|
110551
|
-
} else if (queue?.retryConfig?.maxAttempts !== opts.retryConfig?.maxAttempts) {
|
|
110552
|
-
await this.client.updateQueue({ queue: queueConfig });
|
|
110553
110589
|
}
|
|
110590
|
+
}
|
|
110591
|
+
async listTasks(topicName) {
|
|
110592
|
+
const url = `${this.getQueueApiUrl(topicName)}/tasks`;
|
|
110593
|
+
const res = await gcpFetch(url, this.accessToken, { method: "GET" });
|
|
110594
|
+
return res.tasks || [];
|
|
110595
|
+
}
|
|
110596
|
+
async deleteTask(taskName) {
|
|
110597
|
+
const url = `https://cloudtasks.googleapis.com/v2/${taskName}`;
|
|
110598
|
+
await gcpFetch(url, this.accessToken, { method: "DELETE" });
|
|
110599
|
+
}
|
|
110600
|
+
async create(opts) {
|
|
110601
|
+
const queuePath = this.getQueuePath(opts.topicName);
|
|
110602
|
+
await this.createOrUpdateQueue(opts.topicName, opts.retryConfig);
|
|
110554
110603
|
if (opts.deleteExistingTasks) {
|
|
110555
|
-
const
|
|
110604
|
+
const tasks = await this.listTasks(opts.topicName);
|
|
110556
110605
|
await Promise.allSettled(tasks.map(async (task) => {
|
|
110557
110606
|
try {
|
|
110558
|
-
await this.
|
|
110607
|
+
await this.deleteTask(task.name);
|
|
110559
110608
|
} catch (err) {
|
|
110560
110609
|
if (err.code !== 5) throw err;
|
|
110561
110610
|
}
|
|
@@ -110566,32 +110615,34 @@ var CloudTasksHelper = class extends Runtime {
|
|
|
110566
110615
|
const createdTaskNames = [];
|
|
110567
110616
|
for (let i = 0; i < repeat; i++) {
|
|
110568
110617
|
const taskId = opts.doNotCreateIfExists ? import_crypto2.default.createHash("sha256").update(Object.entries(opts.extraQuery || {}).sort().map((x) => x.join("=")).join("&")).digest("base64").slice(0, 16) : null;
|
|
110569
|
-
const scheduleTime =
|
|
110570
|
-
seconds: Math.floor(Date.now() / 1e3) + scheduleSeconds * (i + 1)
|
|
110571
|
-
};
|
|
110618
|
+
const scheduleTime = new Date(Date.now() + 1e3 * scheduleSeconds * (i + 1)).toISOString();
|
|
110572
110619
|
const httpRequest = {
|
|
110573
110620
|
httpMethod: opts.httpTarget.httpMethod || "POST",
|
|
110574
|
-
url: opts.httpTarget.uri || this.runtimeUrl + `/executeWorkflow/${this.workflowId}/${this.triggerId}?cloud_task=${opts.topicName}&schedule_time=${scheduleTime
|
|
110621
|
+
url: opts.httpTarget.uri || this.runtimeUrl + `/executeWorkflow/${this.workflowId}/${this.triggerId}?cloud_task=${opts.topicName}&schedule_time=${scheduleTime}&${new URLSearchParams(opts.extraQuery).toString()}`,
|
|
110575
110622
|
headers: opts.httpTarget.headers || {
|
|
110576
110623
|
"Content-Type": "application/json",
|
|
110577
110624
|
"User-Agent": "CloudTasks-Google"
|
|
110578
110625
|
},
|
|
110579
|
-
body: opts.httpTarget.body ? Buffer.from(JSON.stringify(opts.httpTarget.body)) : void 0,
|
|
110626
|
+
body: opts.httpTarget.body ? Buffer.from(JSON.stringify(opts.httpTarget.body)).toString("base64") : void 0,
|
|
110580
110627
|
oidcToken: {
|
|
110581
110628
|
serviceAccountEmail: this.serviceAccount
|
|
110582
110629
|
}
|
|
110583
110630
|
};
|
|
110584
110631
|
try {
|
|
110585
|
-
const
|
|
110586
|
-
|
|
110632
|
+
const url = `${this.getQueueApiUrl(opts.topicName)}/tasks`;
|
|
110633
|
+
const body = {
|
|
110587
110634
|
task: {
|
|
110588
|
-
name:
|
|
110635
|
+
name: `${queuePath}/tasks/${taskId}`,
|
|
110589
110636
|
scheduleTime,
|
|
110590
110637
|
httpRequest
|
|
110591
110638
|
}
|
|
110639
|
+
};
|
|
110640
|
+
const res = await gcpFetch(url, this.accessToken, {
|
|
110641
|
+
method: "POST",
|
|
110642
|
+
body: JSON.stringify(body)
|
|
110592
110643
|
});
|
|
110593
|
-
if (
|
|
110594
|
-
createdTaskNames.push(
|
|
110644
|
+
if (res.name) {
|
|
110645
|
+
createdTaskNames.push(res.name);
|
|
110595
110646
|
}
|
|
110596
110647
|
} catch (err) {
|
|
110597
110648
|
if (err.code === 5) return false;
|
|
@@ -110602,9 +110653,9 @@ var CloudTasksHelper = class extends Runtime {
|
|
|
110602
110653
|
return createdTaskNames;
|
|
110603
110654
|
}
|
|
110604
110655
|
async delete(topicName) {
|
|
110605
|
-
const
|
|
110656
|
+
const queueApiUrl = this.getQueueApiUrl(topicName);
|
|
110606
110657
|
try {
|
|
110607
|
-
await this.
|
|
110658
|
+
await gcpFetch(queueApiUrl, this.accessToken, { method: "DELETE" });
|
|
110608
110659
|
return true;
|
|
110609
110660
|
} catch (err) {
|
|
110610
110661
|
if (err.code === 5) return false;
|