@panter/cloud-tasks 1.1.1 → 1.1.3
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/README.md +1 -1
- package/dist/client.cjs +1 -1
- package/dist/client.d.ts +10 -12
- package/dist/client.d.ts.map +1 -1
- package/dist/client.mjs +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.mjs +1 -1
- package/dist/tasksClients.cjs +1 -1
- package/dist/tasksClients.d.ts +6 -1
- package/dist/tasksClients.d.ts.map +1 -1
- package/dist/tasksClients.mjs +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -143,7 +143,7 @@ const tasks = createTasksClient<Router>({
|
|
|
143
143
|
// special characters are not allowed in queue name
|
|
144
144
|
queueName: "reasonable-queue-name",
|
|
145
145
|
// enable emulator in local development
|
|
146
|
-
|
|
146
|
+
emulator: process.env.ENV_SHORT === "local" ? { port: 6020 } : false,
|
|
147
147
|
// optional: provide a custom logger
|
|
148
148
|
logger,
|
|
149
149
|
});
|
package/dist/client.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var p=Object.defineProperty;var
|
|
1
|
+
"use strict";var p=Object.defineProperty;var q=Object.getOwnPropertyDescriptor;var E=Object.getOwnPropertyNames;var v=Object.prototype.hasOwnProperty;var i=(o,t)=>p(o,"name",{value:t,configurable:!0});var x=(o,t)=>{for(var e in t)p(o,e,{get:t[e],enumerable:!0})},I=(o,t,e,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of E(t))!v.call(o,s)&&s!==e&&p(o,s,{get:()=>t[s],enumerable:!(r=q(t,s))||r.enumerable});return o};var S=o=>I(p({},"__esModule",{value:!0}),o);var L={};x(L,{createTasksClient:()=>O});module.exports=S(L);var f=require("@google-cloud/tasks"),C=require("@grpc/grpc-js"),R=require("google-auth-library"),g=require("google-gax"),a=require("ts-results-es");var h=class{constructor(t,e,r,s){this.client=t;this.location=e;this.queueName=r;this.logger=s;this.projectIdPromise=this.client.getProjectId(),this.serviceAccountEmailPromise=new R.GoogleAuth().getCredentials().then(n=>n.client_email??null)}static{i(this,"CommonTasksClient")}serviceAccountEmailPromise;projectIdPromise;async upsertQueue(t){try{let e=await this.projectIdPromise,r=this.client.queuePath(e,this.location,this.queueName),s=this.client.locationPath(e,this.location);this.logger.info({queuePath:r,parentPath:s},"upserting tasks queue");let n="create";try{await this.client.getQueue({name:r}),n="update"}catch(u){if(k(u)&&u.code===g.Status.NOT_FOUND)n="create";else throw u}return n==="update"?(this.logger.info({queuePath:r},"updating existing queue"),await this.client.updateQueue({queue:{name:r,...t}})):(this.logger.info({queuePath:r},"creating new queue"),await this.client.createQueue({parent:s,queue:{name:r,...t}})),a.Ok.EMPTY}catch(e){return(0,a.Err)(l(e))}}async listTasks(){try{let t=await this.projectIdPromise,e=this.client.queuePath(t,this.location,this.queueName),[r]=await this.client.listTasks({parent:e});return(0,a.Ok)(r)}catch(t){return(0,a.Err)(l(t))}}async createTask(t,e){try{let r=await this.projectIdPromise,s=this.client.queuePath(r,this.location,this.queueName),n=this.client.taskPath(r,this.location,this.queueName,N(t)),u=await this.serviceAccountEmailPromise;return this.logger.info({queuePath:s,taskPath:n,url:e?.httpRequest?.url,payload:e?.httpRequest?.body,serviceAccountEmail:u},"scheduling task"),await this.client.createTask({parent:s,task:{...e,name:n,httpRequest:{...e?.httpRequest,oidcToken:{serviceAccountEmail:u}}}}),(0,a.Ok)(n)}catch(r){return(0,a.Err)(l(r))}}async getTask(t){try{let[e]=await this.client.getTask({name:t});return(0,a.Ok)(e)}catch(e){return k(e)&&e.code===g.Status.NOT_FOUND?(0,a.Ok)(null):(0,a.Err)(l(e))}}},m=class extends h{static{i(this,"RegularTasksClient")}constructor(t,e,r){super(new f.CloudTasksClient({}),t,e,r)}},d=class extends h{static{i(this,"EmulatorTasksClient")}constructor(t,e,r,s){super(new f.CloudTasksClient({sslCreds:C.credentials.createInsecure(),servicePath:s.host??"localhost",port:s.port}),t,e,r)}async upsertQueue(t){try{let e=await this.projectIdPromise,r=this.client.queuePath(e,this.location,this.queueName),s=this.client.locationPath(e,this.location);this.logger.info({queuePath:r,parentPath:s},"upserting simulator tasks queue");try{return await this.client.createQueue({parent:s,queue:{name:r,...t}}),a.Ok.EMPTY}catch(n){return k(n)&&n.code!==g.Status.ALREADY_EXISTS?(0,a.Err)(l(n)):(this.logger.warn({},"dev warning: the queue was not updated due to the emulator not supporting update"),this.logger.warn({},"if you need to update queue options, restart the emulator"),a.Ok.EMPTY)}}catch(e){return(0,a.Err)(l(e))}}async createTask(t,e){return super.createTask(t,{...e,httpRequest:{...e?.httpRequest,url:e?.httpRequest?.url?.replace("//localhost","//host.containers.internal")}})}};function N(o){let t=new Uint8Array(3);crypto.getRandomValues(t);let e=Buffer.from(t).toString("hex");return`${o}-${new Date().getTime()}-${e}`}i(N,"createUniqueName");function k(o){return o instanceof Error&&"code"in o&&Number.isInteger(o.code)}i(k,"isRpcError");function l(o){return k(o)?{...o,type:"rpc"}:{type:"unknown",error:o}}i(l,"toTasksError");function O({queueName:o,emulator:t=!1,location:e="europe-west6",tasksWorkerUrl:r,queueOptions:s,logger:n={error:console.error,info:console.info,warn:console.warn}}){n.info({queueName:o,emulator:t,location:e,tasksWorkerUrl:r},"creating tasks client");let u=t?new d(e,o,n,t):new m(e,o,n);return new Proxy({},{get:i((j,y)=>y==="_manage"?{find:i(c=>u.getTask(c).then(T=>T.unwrap()),"find"),list:i(()=>u.listTasks().then(c=>c.unwrap()),"list")}:{schedule:i(async(c,T)=>{let w=y.toString();await u.upsertQueue(s);let P=new URL(`/trpc/${w}`,r).toString();return n.info({url:P,input:c},"scheduling http request that calls trpc mutation of tasks worker"),(await u.createTask(w,{...T,httpRequest:{httpMethod:"POST",url:P,headers:{"Content-Type":"application/json"},body:c?Buffer.from(JSON.stringify(c)).toString("base64"):null}})).unwrap()},"schedule")},"get")})}i(O,"createTasksClient");
|
package/dist/client.d.ts
CHANGED
|
@@ -1,22 +1,15 @@
|
|
|
1
1
|
import type { google } from "@google-cloud/tasks/build/protos";
|
|
2
2
|
import type { CreateTRPCProxyClient } from "@trpc/client";
|
|
3
3
|
import type { AnyRouter } from "@trpc/server";
|
|
4
|
-
import type { GcpLocation, TaskConfig } from "./tasksClients";
|
|
5
|
-
export
|
|
6
|
-
info: (metadata: Record<string, unknown>, message: string) => void;
|
|
7
|
-
warn: (metadata: Record<string, unknown>, message: string) => void;
|
|
8
|
-
error: (metadata: Record<string, unknown>, message: string) => void;
|
|
9
|
-
};
|
|
10
|
-
export declare function createTasksClient<Router extends AnyRouter>({ queueName, useEmulator, location, tasksWorkerUrl, queueOptions, logger, }: {
|
|
4
|
+
import type { EmulatorConfig, GcpLocation, TaskConfig } from "./tasksClients";
|
|
5
|
+
export declare function createTasksClient<Router extends AnyRouter>({ queueName, emulator, location, tasksWorkerUrl, queueOptions, logger, }: {
|
|
11
6
|
queueName: string;
|
|
12
|
-
useEmulator?: boolean;
|
|
13
7
|
location?: GcpLocation;
|
|
14
8
|
queueOptions?: Omit<google.cloud.tasks.v2.IQueue, "name">;
|
|
15
9
|
tasksWorkerUrl: URL;
|
|
16
10
|
logger?: Logger;
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
} ? CreateTRPCProxyClient<Router>[K] : never; } : never>>;
|
|
11
|
+
emulator?: false | EmulatorConfig;
|
|
12
|
+
}): ScheduleClient<CreateTRPCProxyClient<Router>>;
|
|
20
13
|
type RemoveNeverKeys<T> = {
|
|
21
14
|
[K in keyof T as T[K] extends never ? never : K]: T[K];
|
|
22
15
|
};
|
|
@@ -36,7 +29,7 @@ type ScheduleClientManage = {
|
|
|
36
29
|
/**
|
|
37
30
|
* implementation is in return of `createTasksClient` function
|
|
38
31
|
*/
|
|
39
|
-
type ScheduleClient<T, M = OnlyMutate<T>> = {
|
|
32
|
+
export type ScheduleClient<T, M = OnlyMutate<T>> = {
|
|
40
33
|
[K in keyof M]: {
|
|
41
34
|
/**
|
|
42
35
|
* Schedule a task with given input.
|
|
@@ -56,5 +49,10 @@ type ScheduleClient<T, M = OnlyMutate<T>> = {
|
|
|
56
49
|
*/
|
|
57
50
|
_manage: ScheduleClientManage;
|
|
58
51
|
};
|
|
52
|
+
export type Logger = {
|
|
53
|
+
info: (metadata: Record<string, unknown>, message: string) => void;
|
|
54
|
+
warn: (metadata: Record<string, unknown>, message: string) => void;
|
|
55
|
+
error: (metadata: Record<string, unknown>, message: string) => void;
|
|
56
|
+
};
|
|
59
57
|
export {};
|
|
60
58
|
//# sourceMappingURL=client.d.ts.map
|
package/dist/client.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,kCAAkC,CAAC;AAC/D,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAC;AAC1D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAE9C,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,kCAAkC,CAAC;AAC/D,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAC;AAC1D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAE9C,OAAO,KAAK,EACV,cAAc,EACd,WAAW,EACX,UAAU,EAEX,MAAM,gBAAgB,CAAC;AAGxB,wBAAgB,iBAAiB,CAAC,MAAM,SAAS,SAAS,EAAE,EAC1D,SAAS,EACT,QAAgB,EAChB,QAAyB,EACzB,cAAc,EACd,YAAY,EACZ,MAIC,GACF,EAAE;IACD,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,CAAC,EAAE,WAAW,CAAC;IACvB,YAAY,CAAC,EAAE,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,KAA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AAC,KAAK,EAAE,CAAC,CAAC;CACpD,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,cAAc,CAAC,CAAC,EAAE,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC,IAAI;KAChD,CAAC,IAAI,MAAM,CAAC,GAAG;QACd;;;;;;;;;WASG;QACH,QAAQ,EAAE,mBAAmB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;KACrC;CACF,GAAG;IACF;;OAEG;IACH,OAAO,EAAE,oBAAoB,CAAC;CAC/B,CAAC;AAEF,MAAM,MAAM,MAAM,GAAG;IACnB,IAAI,EAAE,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;IACnE,IAAI,EAAE,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;IACnE,KAAK,EAAE,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;CACrE,CAAC"}
|
package/dist/client.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
1
|
+
var C=Object.defineProperty;var a=(o,t)=>C(o,"name",{value:t,configurable:!0});import{CloudTasksClient as P}from"@google-cloud/tasks";import{credentials as R}from"@grpc/grpc-js";import{GoogleAuth as q}from"google-auth-library";import{Status as T}from"google-gax";import{Err as l,Ok as c}from"ts-results-es";var g=class{constructor(t,e,r,n){this.client=t;this.location=e;this.queueName=r;this.logger=n;this.projectIdPromise=this.client.getProjectId(),this.serviceAccountEmailPromise=new q().getCredentials().then(s=>s.client_email??null)}static{a(this,"CommonTasksClient")}serviceAccountEmailPromise;projectIdPromise;async upsertQueue(t){try{let e=await this.projectIdPromise,r=this.client.queuePath(e,this.location,this.queueName),n=this.client.locationPath(e,this.location);this.logger.info({queuePath:r,parentPath:n},"upserting tasks queue");let s="create";try{await this.client.getQueue({name:r}),s="update"}catch(i){if(d(i)&&i.code===T.NOT_FOUND)s="create";else throw i}return s==="update"?(this.logger.info({queuePath:r},"updating existing queue"),await this.client.updateQueue({queue:{name:r,...t}})):(this.logger.info({queuePath:r},"creating new queue"),await this.client.createQueue({parent:n,queue:{name:r,...t}})),c.EMPTY}catch(e){return l(p(e))}}async listTasks(){try{let t=await this.projectIdPromise,e=this.client.queuePath(t,this.location,this.queueName),[r]=await this.client.listTasks({parent:e});return c(r)}catch(t){return l(p(t))}}async createTask(t,e){try{let r=await this.projectIdPromise,n=this.client.queuePath(r,this.location,this.queueName),s=this.client.taskPath(r,this.location,this.queueName,E(t)),i=await this.serviceAccountEmailPromise;return this.logger.info({queuePath:n,taskPath:s,url:e?.httpRequest?.url,payload:e?.httpRequest?.body,serviceAccountEmail:i},"scheduling task"),await this.client.createTask({parent:n,task:{...e,name:s,httpRequest:{...e?.httpRequest,oidcToken:{serviceAccountEmail:i}}}}),c(s)}catch(r){return l(p(r))}}async getTask(t){try{let[e]=await this.client.getTask({name:t});return c(e)}catch(e){return d(e)&&e.code===T.NOT_FOUND?c(null):l(p(e))}}},h=class extends g{static{a(this,"RegularTasksClient")}constructor(t,e,r){super(new P({}),t,e,r)}},m=class extends g{static{a(this,"EmulatorTasksClient")}constructor(t,e,r,n){super(new P({sslCreds:R.createInsecure(),servicePath:n.host??"localhost",port:n.port}),t,e,r)}async upsertQueue(t){try{let e=await this.projectIdPromise,r=this.client.queuePath(e,this.location,this.queueName),n=this.client.locationPath(e,this.location);this.logger.info({queuePath:r,parentPath:n},"upserting simulator tasks queue");try{return await this.client.createQueue({parent:n,queue:{name:r,...t}}),c.EMPTY}catch(s){return d(s)&&s.code!==T.ALREADY_EXISTS?l(p(s)):(this.logger.warn({},"dev warning: the queue was not updated due to the emulator not supporting update"),this.logger.warn({},"if you need to update queue options, restart the emulator"),c.EMPTY)}}catch(e){return l(p(e))}}async createTask(t,e){return super.createTask(t,{...e,httpRequest:{...e?.httpRequest,url:e?.httpRequest?.url?.replace("//localhost","//host.containers.internal")}})}};function E(o){let t=new Uint8Array(3);crypto.getRandomValues(t);let e=Buffer.from(t).toString("hex");return`${o}-${new Date().getTime()}-${e}`}a(E,"createUniqueName");function d(o){return o instanceof Error&&"code"in o&&Number.isInteger(o.code)}a(d,"isRpcError");function p(o){return d(o)?{...o,type:"rpc"}:{type:"unknown",error:o}}a(p,"toTasksError");function K({queueName:o,emulator:t=!1,location:e="europe-west6",tasksWorkerUrl:r,queueOptions:n,logger:s={error:console.error,info:console.info,warn:console.warn}}){s.info({queueName:o,emulator:t,location:e,tasksWorkerUrl:r},"creating tasks client");let i=t?new m(e,o,s,t):new h(e,o,s);return new Proxy({},{get:a((v,f)=>f==="_manage"?{find:a(u=>i.getTask(u).then(k=>k.unwrap()),"find"),list:a(()=>i.listTasks().then(u=>u.unwrap()),"list")}:{schedule:a(async(u,k)=>{let y=f.toString();await i.upsertQueue(n);let w=new URL(`/trpc/${y}`,r).toString();return s.info({url:w,input:u},"scheduling http request that calls trpc mutation of tasks worker"),(await i.createTask(y,{...k,httpRequest:{httpMethod:"POST",url:w,headers:{"Content-Type":"application/json"},body:u?Buffer.from(JSON.stringify(u)).toString("base64"):null}})).unwrap()},"schedule")},"get")})}a(K,"createTasksClient");export{K as createTasksClient};
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var I=Object.create;var p=Object.defineProperty;var S=Object.getOwnPropertyDescriptor;var N=Object.getOwnPropertyNames;var O=Object.getPrototypeOf,L=Object.prototype.hasOwnProperty;var a=(r,t)=>p(r,"name",{value:t,configurable:!0});var A=(r,t)=>{for(var e in t)p(r,e,{get:t[e],enumerable:!0})},R=(r,t,e,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of N(t))!L.call(r,s)&&s!==e&&p(r,s,{get:()=>t[s],enumerable:!(o=S(t,s))||o.enumerable});return r};var M=(r,t,e)=>(e=r!=null?I(O(r)):{},R(t||!r||!r.__esModule?p(e,"default",{value:r,enumerable:!0}):e,r)),j=r=>R(p({},"__esModule",{value:!0}),r);var _={};A(_,{createTasksClient:()=>K,createTasksServer:()=>b});module.exports=j(_);var
|
|
1
|
+
"use strict";var I=Object.create;var p=Object.defineProperty;var S=Object.getOwnPropertyDescriptor;var N=Object.getOwnPropertyNames;var O=Object.getPrototypeOf,L=Object.prototype.hasOwnProperty;var a=(r,t)=>p(r,"name",{value:t,configurable:!0});var A=(r,t)=>{for(var e in t)p(r,e,{get:t[e],enumerable:!0})},R=(r,t,e,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of N(t))!L.call(r,s)&&s!==e&&p(r,s,{get:()=>t[s],enumerable:!(o=S(t,s))||o.enumerable});return r};var M=(r,t,e)=>(e=r!=null?I(O(r)):{},R(t||!r||!r.__esModule?p(e,"default",{value:r,enumerable:!0}):e,r)),j=r=>R(p({},"__esModule",{value:!0}),r);var _={};A(_,{createTasksClient:()=>K,createTasksServer:()=>b});module.exports=j(_);var T=require("@google-cloud/tasks"),C=require("@grpc/grpc-js"),q=require("google-auth-library"),g=require("google-gax"),i=require("ts-results-es");var m=class{constructor(t,e,o,s){this.client=t;this.location=e;this.queueName=o;this.logger=s;this.projectIdPromise=this.client.getProjectId(),this.serviceAccountEmailPromise=new q.GoogleAuth().getCredentials().then(n=>n.client_email??null)}static{a(this,"CommonTasksClient")}serviceAccountEmailPromise;projectIdPromise;async upsertQueue(t){try{let e=await this.projectIdPromise,o=this.client.queuePath(e,this.location,this.queueName),s=this.client.locationPath(e,this.location);this.logger.info({queuePath:o,parentPath:s},"upserting tasks queue");let n="create";try{await this.client.getQueue({name:o}),n="update"}catch(u){if(k(u)&&u.code===g.Status.NOT_FOUND)n="create";else throw u}return n==="update"?(this.logger.info({queuePath:o},"updating existing queue"),await this.client.updateQueue({queue:{name:o,...t}})):(this.logger.info({queuePath:o},"creating new queue"),await this.client.createQueue({parent:s,queue:{name:o,...t}})),i.Ok.EMPTY}catch(e){return(0,i.Err)(l(e))}}async listTasks(){try{let t=await this.projectIdPromise,e=this.client.queuePath(t,this.location,this.queueName),[o]=await this.client.listTasks({parent:e});return(0,i.Ok)(o)}catch(t){return(0,i.Err)(l(t))}}async createTask(t,e){try{let o=await this.projectIdPromise,s=this.client.queuePath(o,this.location,this.queueName),n=this.client.taskPath(o,this.location,this.queueName,Q(t)),u=await this.serviceAccountEmailPromise;return this.logger.info({queuePath:s,taskPath:n,url:e?.httpRequest?.url,payload:e?.httpRequest?.body,serviceAccountEmail:u},"scheduling task"),await this.client.createTask({parent:s,task:{...e,name:n,httpRequest:{...e?.httpRequest,oidcToken:{serviceAccountEmail:u}}}}),(0,i.Ok)(n)}catch(o){return(0,i.Err)(l(o))}}async getTask(t){try{let[e]=await this.client.getTask({name:t});return(0,i.Ok)(e)}catch(e){return k(e)&&e.code===g.Status.NOT_FOUND?(0,i.Ok)(null):(0,i.Err)(l(e))}}},h=class extends m{static{a(this,"RegularTasksClient")}constructor(t,e,o){super(new T.CloudTasksClient({}),t,e,o)}},d=class extends m{static{a(this,"EmulatorTasksClient")}constructor(t,e,o,s){super(new T.CloudTasksClient({sslCreds:C.credentials.createInsecure(),servicePath:s.host??"localhost",port:s.port}),t,e,o)}async upsertQueue(t){try{let e=await this.projectIdPromise,o=this.client.queuePath(e,this.location,this.queueName),s=this.client.locationPath(e,this.location);this.logger.info({queuePath:o,parentPath:s},"upserting simulator tasks queue");try{return await this.client.createQueue({parent:s,queue:{name:o,...t}}),i.Ok.EMPTY}catch(n){return k(n)&&n.code!==g.Status.ALREADY_EXISTS?(0,i.Err)(l(n)):(this.logger.warn({},"dev warning: the queue was not updated due to the emulator not supporting update"),this.logger.warn({},"if you need to update queue options, restart the emulator"),i.Ok.EMPTY)}}catch(e){return(0,i.Err)(l(e))}}async createTask(t,e){return super.createTask(t,{...e,httpRequest:{...e?.httpRequest,url:e?.httpRequest?.url?.replace("//localhost","//host.containers.internal")}})}};function Q(r){let t=new Uint8Array(3);crypto.getRandomValues(t);let e=Buffer.from(t).toString("hex");return`${r}-${new Date().getTime()}-${e}`}a(Q,"createUniqueName");function k(r){return r instanceof Error&&"code"in r&&Number.isInteger(r.code)}a(k,"isRpcError");function l(r){return k(r)?{...r,type:"rpc"}:{type:"unknown",error:r}}a(l,"toTasksError");function K({queueName:r,emulator:t=!1,location:e="europe-west6",tasksWorkerUrl:o,queueOptions:s,logger:n={error:console.error,info:console.info,warn:console.warn}}){n.info({queueName:r,emulator:t,location:e,tasksWorkerUrl:o},"creating tasks client");let u=t?new d(e,r,n,t):new h(e,r,n);return new Proxy({},{get:a((U,y)=>y==="_manage"?{find:a(c=>u.getTask(c).then(f=>f.unwrap()),"find"),list:a(()=>u.listTasks().then(c=>c.unwrap()),"list")}:{schedule:a(async(c,f)=>{let w=y.toString();await u.upsertQueue(s);let P=new URL(`/trpc/${w}`,o).toString();return n.info({url:P,input:c},"scheduling http request that calls trpc mutation of tasks worker"),(await u.createTask(w,{...f,httpRequest:{httpMethod:"POST",url:P,headers:{"Content-Type":"application/json"},body:c?Buffer.from(JSON.stringify(c)).toString("base64"):null}})).unwrap()},"schedule")},"get")})}a(K,"createTasksClient");var v=require("@trpc/server"),E=require("@trpc/server/adapters/express"),x=M(require("express"));function b(r){let t=v.initTRPC.create(),e=r(t),o=(0,x.default)();return o.use("/trpc",(0,E.createExpressMiddleware)({router:e})),{runServer:a(s=>{o.listen({port:s})},"runServer"),router:e}}a(b,"createTasksServer");
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var R=Object.defineProperty;var
|
|
1
|
+
var R=Object.defineProperty;var a=(o,t)=>R(o,"name",{value:t,configurable:!0});import{CloudTasksClient as P}from"@google-cloud/tasks";import{credentials as C}from"@grpc/grpc-js";import{GoogleAuth as q}from"google-auth-library";import{Status as f}from"google-gax";import{Err as l,Ok as c}from"ts-results-es";var g=class{constructor(t,e,r,n){this.client=t;this.location=e;this.queueName=r;this.logger=n;this.projectIdPromise=this.client.getProjectId(),this.serviceAccountEmailPromise=new q().getCredentials().then(s=>s.client_email??null)}static{a(this,"CommonTasksClient")}serviceAccountEmailPromise;projectIdPromise;async upsertQueue(t){try{let e=await this.projectIdPromise,r=this.client.queuePath(e,this.location,this.queueName),n=this.client.locationPath(e,this.location);this.logger.info({queuePath:r,parentPath:n},"upserting tasks queue");let s="create";try{await this.client.getQueue({name:r}),s="update"}catch(i){if(d(i)&&i.code===f.NOT_FOUND)s="create";else throw i}return s==="update"?(this.logger.info({queuePath:r},"updating existing queue"),await this.client.updateQueue({queue:{name:r,...t}})):(this.logger.info({queuePath:r},"creating new queue"),await this.client.createQueue({parent:n,queue:{name:r,...t}})),c.EMPTY}catch(e){return l(p(e))}}async listTasks(){try{let t=await this.projectIdPromise,e=this.client.queuePath(t,this.location,this.queueName),[r]=await this.client.listTasks({parent:e});return c(r)}catch(t){return l(p(t))}}async createTask(t,e){try{let r=await this.projectIdPromise,n=this.client.queuePath(r,this.location,this.queueName),s=this.client.taskPath(r,this.location,this.queueName,v(t)),i=await this.serviceAccountEmailPromise;return this.logger.info({queuePath:n,taskPath:s,url:e?.httpRequest?.url,payload:e?.httpRequest?.body,serviceAccountEmail:i},"scheduling task"),await this.client.createTask({parent:n,task:{...e,name:s,httpRequest:{...e?.httpRequest,oidcToken:{serviceAccountEmail:i}}}}),c(s)}catch(r){return l(p(r))}}async getTask(t){try{let[e]=await this.client.getTask({name:t});return c(e)}catch(e){return d(e)&&e.code===f.NOT_FOUND?c(null):l(p(e))}}},m=class extends g{static{a(this,"RegularTasksClient")}constructor(t,e,r){super(new P({}),t,e,r)}},h=class extends g{static{a(this,"EmulatorTasksClient")}constructor(t,e,r,n){super(new P({sslCreds:C.createInsecure(),servicePath:n.host??"localhost",port:n.port}),t,e,r)}async upsertQueue(t){try{let e=await this.projectIdPromise,r=this.client.queuePath(e,this.location,this.queueName),n=this.client.locationPath(e,this.location);this.logger.info({queuePath:r,parentPath:n},"upserting simulator tasks queue");try{return await this.client.createQueue({parent:n,queue:{name:r,...t}}),c.EMPTY}catch(s){return d(s)&&s.code!==f.ALREADY_EXISTS?l(p(s)):(this.logger.warn({},"dev warning: the queue was not updated due to the emulator not supporting update"),this.logger.warn({},"if you need to update queue options, restart the emulator"),c.EMPTY)}}catch(e){return l(p(e))}}async createTask(t,e){return super.createTask(t,{...e,httpRequest:{...e?.httpRequest,url:e?.httpRequest?.url?.replace("//localhost","//host.containers.internal")}})}};function v(o){let t=new Uint8Array(3);crypto.getRandomValues(t);let e=Buffer.from(t).toString("hex");return`${o}-${new Date().getTime()}-${e}`}a(v,"createUniqueName");function d(o){return o instanceof Error&&"code"in o&&Number.isInteger(o.code)}a(d,"isRpcError");function p(o){return d(o)?{...o,type:"rpc"}:{type:"unknown",error:o}}a(p,"toTasksError");function U({queueName:o,emulator:t=!1,location:e="europe-west6",tasksWorkerUrl:r,queueOptions:n,logger:s={error:console.error,info:console.info,warn:console.warn}}){s.info({queueName:o,emulator:t,location:e,tasksWorkerUrl:r},"creating tasks client");let i=t?new h(e,o,s,t):new m(e,o,s);return new Proxy({},{get:a((S,T)=>T==="_manage"?{find:a(u=>i.getTask(u).then(k=>k.unwrap()),"find"),list:a(()=>i.listTasks().then(u=>u.unwrap()),"list")}:{schedule:a(async(u,k)=>{let y=T.toString();await i.upsertQueue(n);let w=new URL(`/trpc/${y}`,r).toString();return s.info({url:w,input:u},"scheduling http request that calls trpc mutation of tasks worker"),(await i.createTask(y,{...k,httpRequest:{httpMethod:"POST",url:w,headers:{"Content-Type":"application/json"},body:u?Buffer.from(JSON.stringify(u)).toString("base64"):null}})).unwrap()},"schedule")},"get")})}a(U,"createTasksClient");import{initTRPC as E}from"@trpc/server";import{createExpressMiddleware as x}from"@trpc/server/adapters/express";import I from"express";function B(o){let t=E.create(),e=o(t),r=I();return r.use("/trpc",x({router:e})),{runServer:a(n=>{r.listen({port:n})},"runServer"),router:e}}a(B,"createTasksServer");export{U as createTasksClient,B as createTasksServer};
|
package/dist/tasksClients.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var l=Object.defineProperty;var T=Object.getOwnPropertyDescriptor;var
|
|
1
|
+
"use strict";var l=Object.defineProperty;var T=Object.getOwnPropertyDescriptor;var f=Object.getOwnPropertyNames;var w=Object.prototype.hasOwnProperty;var i=(o,e)=>l(o,"name",{value:e,configurable:!0});var q=(o,e)=>{for(var t in e)l(o,t,{get:e[t],enumerable:!0})},E=(o,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of f(e))!w.call(o,s)&&s!==t&&l(o,s,{get:()=>e[s],enumerable:!(r=T(e,s))||r.enumerable});return o};var I=o=>E(l({},"__esModule",{value:!0}),o);var v={};q(v,{EmulatorTasksClient:()=>d,RegularTasksClient:()=>m});module.exports=I(v);var k=require("@google-cloud/tasks"),y=require("@grpc/grpc-js"),P=require("google-auth-library"),p=require("google-gax"),n=require("ts-results-es");var h=class{constructor(e,t,r,s){this.client=e;this.location=t;this.queueName=r;this.logger=s;this.projectIdPromise=this.client.getProjectId(),this.serviceAccountEmailPromise=new P.GoogleAuth().getCredentials().then(a=>a.client_email??null)}static{i(this,"CommonTasksClient")}serviceAccountEmailPromise;projectIdPromise;async upsertQueue(e){try{let t=await this.projectIdPromise,r=this.client.queuePath(t,this.location,this.queueName),s=this.client.locationPath(t,this.location);this.logger.info({queuePath:r,parentPath:s},"upserting tasks queue");let a="create";try{await this.client.getQueue({name:r}),a="update"}catch(u){if(g(u)&&u.code===p.Status.NOT_FOUND)a="create";else throw u}return a==="update"?(this.logger.info({queuePath:r},"updating existing queue"),await this.client.updateQueue({queue:{name:r,...e}})):(this.logger.info({queuePath:r},"creating new queue"),await this.client.createQueue({parent:s,queue:{name:r,...e}})),n.Ok.EMPTY}catch(t){return(0,n.Err)(c(t))}}async listTasks(){try{let e=await this.projectIdPromise,t=this.client.queuePath(e,this.location,this.queueName),[r]=await this.client.listTasks({parent:t});return(0,n.Ok)(r)}catch(e){return(0,n.Err)(c(e))}}async createTask(e,t){try{let r=await this.projectIdPromise,s=this.client.queuePath(r,this.location,this.queueName),a=this.client.taskPath(r,this.location,this.queueName,R(e)),u=await this.serviceAccountEmailPromise;return this.logger.info({queuePath:s,taskPath:a,url:t?.httpRequest?.url,payload:t?.httpRequest?.body,serviceAccountEmail:u},"scheduling task"),await this.client.createTask({parent:s,task:{...t,name:a,httpRequest:{...t?.httpRequest,oidcToken:{serviceAccountEmail:u}}}}),(0,n.Ok)(a)}catch(r){return(0,n.Err)(c(r))}}async getTask(e){try{let[t]=await this.client.getTask({name:e});return(0,n.Ok)(t)}catch(t){return g(t)&&t.code===p.Status.NOT_FOUND?(0,n.Ok)(null):(0,n.Err)(c(t))}}},m=class extends h{static{i(this,"RegularTasksClient")}constructor(e,t,r){super(new k.CloudTasksClient({}),e,t,r)}},d=class extends h{static{i(this,"EmulatorTasksClient")}constructor(e,t,r,s){super(new k.CloudTasksClient({sslCreds:y.credentials.createInsecure(),servicePath:s.host??"localhost",port:s.port}),e,t,r)}async upsertQueue(e){try{let t=await this.projectIdPromise,r=this.client.queuePath(t,this.location,this.queueName),s=this.client.locationPath(t,this.location);this.logger.info({queuePath:r,parentPath:s},"upserting simulator tasks queue");try{return await this.client.createQueue({parent:s,queue:{name:r,...e}}),n.Ok.EMPTY}catch(a){return g(a)&&a.code!==p.Status.ALREADY_EXISTS?(0,n.Err)(c(a)):(this.logger.warn({},"dev warning: the queue was not updated due to the emulator not supporting update"),this.logger.warn({},"if you need to update queue options, restart the emulator"),n.Ok.EMPTY)}}catch(t){return(0,n.Err)(c(t))}}async createTask(e,t){return super.createTask(e,{...t,httpRequest:{...t?.httpRequest,url:t?.httpRequest?.url?.replace("//localhost","//host.containers.internal")}})}};function R(o){let e=new Uint8Array(3);crypto.getRandomValues(e);let t=Buffer.from(e).toString("hex");return`${o}-${new Date().getTime()}-${t}`}i(R,"createUniqueName");function g(o){return o instanceof Error&&"code"in o&&Number.isInteger(o.code)}i(g,"isRpcError");function c(o){return g(o)?{...o,type:"rpc"}:{type:"unknown",error:o}}i(c,"toTasksError");
|
package/dist/tasksClients.d.ts
CHANGED
|
@@ -37,8 +37,13 @@ declare class CommonTasksClient implements TasksClient {
|
|
|
37
37
|
export declare class RegularTasksClient extends CommonTasksClient {
|
|
38
38
|
constructor(location: GcpLocation, queueName: string, logger: Logger);
|
|
39
39
|
}
|
|
40
|
+
export type EmulatorConfig = {
|
|
41
|
+
/** default: "localhost" */
|
|
42
|
+
host?: string;
|
|
43
|
+
port?: number;
|
|
44
|
+
};
|
|
40
45
|
export declare class EmulatorTasksClient extends CommonTasksClient {
|
|
41
|
-
constructor(location: GcpLocation, queueName: string, logger: Logger);
|
|
46
|
+
constructor(location: GcpLocation, queueName: string, logger: Logger, config: EmulatorConfig);
|
|
42
47
|
upsertQueue(options?: Omit<google.cloud.tasks.v2.IQueue, "name">): Promise<Err<TasksError> | import("ts-results-es").OkImpl<void>>;
|
|
43
48
|
createTask(taskName: string, task: TaskConfig): Promise<Err<TasksError> | Ok<string>>;
|
|
44
49
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tasksClients.d.ts","sourceRoot":"","sources":["../src/tasksClients.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AACvD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,kCAAkC,CAAC;AAG/D,OAAO,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AACpC,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,MAAM,eAAe,CAAC;AAExC,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAEvC,MAAM,MAAM,WAAW,GAAG,cAAc,GAAG,CAAC,MAAM,GAAG,WAAW,CAAC,OAAO,CAAC,CAAC,CAAC;AAE3E,KAAK,QAAQ,GAAG;IACd,IAAI,EAA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C7D,SAAS,CAAC,gBAAgB,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC;gBAGvB,MAAM,EAAE,gBAAgB,EACxB,QAAQ,EAAE,WAAW,EACrB,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,MAAM;IAQ7B,WAAW,CAAC,OAAO,CAAC,EAAE,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC;IAkDhE,SAAS;IAgBT,UAAU,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,UAAU;IA+C7C,OAAO,CAAC,IAAI,EAAE,MAAM;CAW3B;AAED,qBAAa,kBAAmB,SAAQ,iBAAiB;gBAC3C,QAAQ,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM;CAGrE;AAED,MAAM,MAAM,cAAc,GAAG;IAC3B,2BAA2B;IAC3B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,CAAC;CACf,CAAC;AAEF,qBAAa,mBAAoB,SAAQ,iBAAiB;gBAEtD,QAAQ,EAAE,WAAW,EACrB,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,cAAc;IAcT,WAAW,CACxB,OAAO,CAAC,EAAE,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC;IAiDvC,UAAU,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,UAAU;CAY7D"}
|
package/dist/tasksClients.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
1
|
+
var y=Object.defineProperty;var a=(o,e)=>y(o,"name",{value:e,configurable:!0});import{CloudTasksClient as k}from"@google-cloud/tasks";import{credentials as P}from"@grpc/grpc-js";import{GoogleAuth as T}from"google-auth-library";import{Status as g}from"google-gax";import{Err as c,Ok as i}from"ts-results-es";var p=class{constructor(e,t,r,n){this.client=e;this.location=t;this.queueName=r;this.logger=n;this.projectIdPromise=this.client.getProjectId(),this.serviceAccountEmailPromise=new T().getCredentials().then(s=>s.client_email??null)}static{a(this,"CommonTasksClient")}serviceAccountEmailPromise;projectIdPromise;async upsertQueue(e){try{let t=await this.projectIdPromise,r=this.client.queuePath(t,this.location,this.queueName),n=this.client.locationPath(t,this.location);this.logger.info({queuePath:r,parentPath:n},"upserting tasks queue");let s="create";try{await this.client.getQueue({name:r}),s="update"}catch(u){if(h(u)&&u.code===g.NOT_FOUND)s="create";else throw u}return s==="update"?(this.logger.info({queuePath:r},"updating existing queue"),await this.client.updateQueue({queue:{name:r,...e}})):(this.logger.info({queuePath:r},"creating new queue"),await this.client.createQueue({parent:n,queue:{name:r,...e}})),i.EMPTY}catch(t){return c(l(t))}}async listTasks(){try{let e=await this.projectIdPromise,t=this.client.queuePath(e,this.location,this.queueName),[r]=await this.client.listTasks({parent:t});return i(r)}catch(e){return c(l(e))}}async createTask(e,t){try{let r=await this.projectIdPromise,n=this.client.queuePath(r,this.location,this.queueName),s=this.client.taskPath(r,this.location,this.queueName,f(e)),u=await this.serviceAccountEmailPromise;return this.logger.info({queuePath:n,taskPath:s,url:t?.httpRequest?.url,payload:t?.httpRequest?.body,serviceAccountEmail:u},"scheduling task"),await this.client.createTask({parent:n,task:{...t,name:s,httpRequest:{...t?.httpRequest,oidcToken:{serviceAccountEmail:u}}}}),i(s)}catch(r){return c(l(r))}}async getTask(e){try{let[t]=await this.client.getTask({name:e});return i(t)}catch(t){return h(t)&&t.code===g.NOT_FOUND?i(null):c(l(t))}}},m=class extends p{static{a(this,"RegularTasksClient")}constructor(e,t,r){super(new k({}),e,t,r)}},d=class extends p{static{a(this,"EmulatorTasksClient")}constructor(e,t,r,n){super(new k({sslCreds:P.createInsecure(),servicePath:n.host??"localhost",port:n.port}),e,t,r)}async upsertQueue(e){try{let t=await this.projectIdPromise,r=this.client.queuePath(t,this.location,this.queueName),n=this.client.locationPath(t,this.location);this.logger.info({queuePath:r,parentPath:n},"upserting simulator tasks queue");try{return await this.client.createQueue({parent:n,queue:{name:r,...e}}),i.EMPTY}catch(s){return h(s)&&s.code!==g.ALREADY_EXISTS?c(l(s)):(this.logger.warn({},"dev warning: the queue was not updated due to the emulator not supporting update"),this.logger.warn({},"if you need to update queue options, restart the emulator"),i.EMPTY)}}catch(t){return c(l(t))}}async createTask(e,t){return super.createTask(e,{...t,httpRequest:{...t?.httpRequest,url:t?.httpRequest?.url?.replace("//localhost","//host.containers.internal")}})}};function f(o){let e=new Uint8Array(3);crypto.getRandomValues(e);let t=Buffer.from(e).toString("hex");return`${o}-${new Date().getTime()}-${t}`}a(f,"createUniqueName");function h(o){return o instanceof Error&&"code"in o&&Number.isInteger(o.code)}a(h,"isRpcError");function l(o){return h(o)?{...o,type:"rpc"}:{type:"unknown",error:o}}a(l,"toTasksError");export{d as EmulatorTasksClient,m as RegularTasksClient};
|