@panter/cloud-tasks 2.0.2 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/README.md +14 -13
  2. package/dist/base.cjs +1 -0
  3. package/dist/base.d.ts +14 -0
  4. package/dist/base.d.ts.map +1 -0
  5. package/dist/base.mjs +1 -0
  6. package/dist/client.cjs +1 -1
  7. package/dist/client.d.ts +4 -12
  8. package/dist/client.d.ts.map +1 -1
  9. package/dist/client.mjs +1 -1
  10. package/dist/index.cjs +1 -1
  11. package/dist/index.mjs +1 -1
  12. package/dist/server.cjs +1 -1
  13. package/dist/server.d.ts +12 -7
  14. package/dist/server.d.ts.map +1 -1
  15. package/dist/server.mjs +1 -1
  16. package/dist/tasksClients/CommonTasksClient.cjs +1 -0
  17. package/dist/tasksClients/CommonTasksClient.d.ts +19 -0
  18. package/dist/tasksClients/CommonTasksClient.d.ts.map +1 -0
  19. package/dist/tasksClients/CommonTasksClient.mjs +1 -0
  20. package/dist/tasksClients/EmulatorTasksClient.cjs +1 -0
  21. package/dist/tasksClients/EmulatorTasksClient.d.ts +11 -0
  22. package/dist/tasksClients/EmulatorTasksClient.d.ts.map +1 -0
  23. package/dist/tasksClients/EmulatorTasksClient.mjs +1 -0
  24. package/dist/tasksClients/RegularTasksClient.cjs +1 -0
  25. package/dist/tasksClients/RegularTasksClient.d.ts +7 -0
  26. package/dist/tasksClients/RegularTasksClient.d.ts.map +1 -0
  27. package/dist/tasksClients/RegularTasksClient.mjs +1 -0
  28. package/dist/tasksClients/index.cjs +1 -0
  29. package/dist/tasksClients/index.d.ts +4 -0
  30. package/dist/tasksClients/index.d.ts.map +1 -0
  31. package/dist/tasksClients/index.mjs +1 -0
  32. package/dist/tasksClients/types.cjs +1 -0
  33. package/dist/tasksClients/types.d.ts +33 -0
  34. package/dist/tasksClients/types.d.ts.map +1 -0
  35. package/dist/tasksClients/types.mjs +1 -0
  36. package/package.json +11 -1
  37. package/dist/tasksClients.cjs +0 -1
  38. package/dist/tasksClients.d.ts +0 -51
  39. package/dist/tasksClients.d.ts.map +0 -1
  40. package/dist/tasksClients.mjs +0 -1
package/README.md CHANGED
@@ -62,21 +62,22 @@ import { z } from "zod";
62
62
 
63
63
  logger.info("Starting tasks server...");
64
64
 
65
- export const { runServer, router } = createTasksServer((t) =>
66
- t.router({
67
- createUser: t.procedure
68
- .input(z.object({ name: z.string().min(5) }))
69
- .mutation(async (opts) => {
70
- logger.info(`creating user ${opts.input.name}`);
65
+ export const { runServer, router } = createTasksServer({
66
+ createRouter: (t) =>
67
+ t.router({
68
+ createUser: t.procedure
69
+ .input(z.object({ name: z.string().min(5) }))
70
+ .mutation(async (opts) => {
71
+ logger.info(`creating user ${opts.input.name}`);
72
+ }),
73
+ doNothing: t.procedure.mutation(() => {
74
+ logger.info("doing nothing");
75
+ }),
76
+ sendEmail: t.procedure.mutation(() => {
77
+ logger.info("sending email");
71
78
  }),
72
- doNothing: t.procedure.mutation(() => {
73
- logger.info("doing nothing");
74
- }),
75
- sendEmail: t.procedure.mutation(() => {
76
- logger.info("sending email");
77
79
  }),
78
- }),
79
- );
80
+ });
80
81
 
81
82
  // Export the router type for use in the client
82
83
  export type Router = typeof router;
package/dist/base.cjs ADDED
@@ -0,0 +1 @@
1
+ "use strict";var d=Object.defineProperty;var P=Object.getOwnPropertyDescriptor;var q=Object.getOwnPropertyNames;var R=Object.prototype.hasOwnProperty;var a=(r,e)=>d(r,"name",{value:e,configurable:!0});var x=(r,e)=>{for(var t in e)d(r,t,{get:e[t],enumerable:!0})},I=(r,e,t,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of q(e))!R.call(r,s)&&s!==t&&d(r,s,{get:()=>e[s],enumerable:!(o=P(e,s))||o.enumerable});return r};var L=r=>I(d({},"__esModule",{value:!0}),r);var N={};x(N,{createBaseTaskClient:()=>v});module.exports=L(N);var y=require("@google-cloud/tasks"),w=require("@grpc/grpc-js"),C=require("google-gax"),m=require("ts-results-es");var T=require("google-auth-library"),k=require("google-gax"),i=require("ts-results-es");function p(r){return r instanceof Error&&"code"in r&&Number.isInteger(r.code)}a(p,"isRpcError");function u(r){return p(r)?{...r,type:"rpc"}:{type:"unknown",error:r}}a(u,"toTasksError");function f(r){let e=new Uint8Array(3);crypto.getRandomValues(e);let t=Buffer.from(e).toString("hex");return`${r}-${Date.now()}-${t}`}a(f,"createUniqueName");var l=class{constructor(e,t,o,s){this.client=e;this.location=t;this.queueName=o;this.logger=s;this.projectIdPromise=this.client.getProjectId(),this.serviceAccountEmailPromise=new T.GoogleAuth().getCredentials().then(n=>n.client_email??null)}static{a(this,"CommonTasksClient")}serviceAccountEmailPromise;projectIdPromise;async upsertQueue(e){try{let t=await this.projectIdPromise,o=this.client.queuePath(t,this.location,this.queueName),s=this.client.locationPath(t,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(c){if(p(c)&&c.code===k.Status.NOT_FOUND)n="create";else throw c}return n==="update"?(this.logger.info({queuePath:o},"updating existing queue"),await this.client.updateQueue({queue:{name:o,...e}})):(this.logger.info({queuePath:o},"creating new queue"),await this.client.createQueue({parent:s,queue:{name:o,...e}})),i.Ok.EMPTY}catch(t){return(0,i.Err)(u(t))}}async listTasks(){try{let e=await this.projectIdPromise,t=this.client.queuePath(e,this.location,this.queueName),[o]=await this.client.listTasks({parent:t});return(0,i.Ok)(o)}catch(e){return(0,i.Err)(u(e))}}async createTask(e,t){try{let o=await this.projectIdPromise,s=this.client.queuePath(o,this.location,this.queueName),n=this.client.taskPath(o,this.location,this.queueName,f(e)),c=await this.serviceAccountEmailPromise;return this.logger.info({queuePath:s,taskPath:n,url:t?.httpRequest?.url,payload:t?.httpRequest?.body,serviceAccountEmail:c},"scheduling task"),await this.client.createTask({parent:s,task:{...t,name:n,httpRequest:{...t?.httpRequest,oidcToken:{serviceAccountEmail:c}}}}),(0,i.Ok)(n)}catch(o){return(0,i.Err)(u(o))}}async getTask(e){try{let[t]=await this.client.getTask({name:e});return(0,i.Ok)(t)}catch(t){return p(t)&&t.code===k.Status.NOT_FOUND?(0,i.Ok)(null):(0,i.Err)(u(t))}}};var g=class extends l{static{a(this,"EmulatorTasksClient")}constructor(e,t,o,s){super(new y.CloudTasksClient({sslCreds:w.credentials.createInsecure(),servicePath:s.host??"localhost",port:s.port}),e,t,o)}async upsertQueue(e){try{let t=await this.projectIdPromise,o=this.client.queuePath(t,this.location,this.queueName),s=this.client.locationPath(t,this.location);this.logger.info({queuePath:o,parentPath:s},"upserting simulator tasks queue");try{return await this.client.createQueue({parent:s,queue:{name:o,...e}}),m.Ok.EMPTY}catch(n){return p(n)&&n.code!==C.Status.ALREADY_EXISTS?(0,m.Err)(u(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"),m.Ok.EMPTY)}}catch(t){return(0,m.Err)(u(t))}}async createTask(e,t){return super.createTask(e,{...t,httpRequest:{...t?.httpRequest,url:t?.httpRequest?.url?.replace("//localhost","//host.containers.internal")}})}};var E=require("@google-cloud/tasks");var h=class extends l{static{a(this,"RegularTasksClient")}constructor(e,t,o){super(new E.CloudTasksClient({}),e,t,o)}};function v({queueName:r,emulator:e=!1,location:t="europe-west6",logger:o={error:console.error,info:console.info,warn:console.warn}}){return o.info({queueName:r,emulator:e,location:t},"creating base tasks client"),e?new g(t,r,o,e):new h(t,r,o)}a(v,"createBaseTaskClient");
package/dist/base.d.ts ADDED
@@ -0,0 +1,14 @@
1
+ import type { EmulatorConfig, GcpLocation, TasksClient } from "./tasksClients";
2
+ export type Logger = {
3
+ info: (metadata: Record<string, unknown>, message: string) => void;
4
+ warn: (metadata: Record<string, unknown>, message: string) => void;
5
+ error: (metadata: Record<string, unknown>, message: string) => void;
6
+ };
7
+ export type BaseTaskClientConfig = {
8
+ queueName: string;
9
+ location?: GcpLocation;
10
+ logger?: Logger;
11
+ emulator?: false | EmulatorConfig;
12
+ };
13
+ export declare function createBaseTaskClient({ queueName, emulator, location, logger, }: BaseTaskClientConfig): TasksClient;
14
+ //# sourceMappingURL=base.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../src/base.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAG/E,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;AAEF,MAAM,MAAM,oBAAoB,GAAG;IACjC,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,CAAC,EAAE,WAAW,CAAC;IACvB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,QAAQ,CAAC,EAAE,KAAK,GAAG,cAAc,CAAC;CACnC,CAAC;AAEF,wBAAgB,oBAAoB,CAAC,EACnC,SAAS,EACT,QAAgB,EAChB,QAAyB,EACzB,MAIC,GACF,EAAE,oBAAoB,GAAG,WAAW,CAMpC"}
package/dist/base.mjs ADDED
@@ -0,0 +1 @@
1
+ var y=Object.defineProperty;var a=(r,e)=>y(r,"name",{value:e,configurable:!0});import{CloudTasksClient as C}from"@google-cloud/tasks";import{credentials as E}from"@grpc/grpc-js";import{Status as P}from"google-gax";import{Err as f,Ok as T}from"ts-results-es";import{GoogleAuth as w}from"google-auth-library";import{Status as k}from"google-gax";import{Err as h,Ok as l}from"ts-results-es";function c(r){return r instanceof Error&&"code"in r&&Number.isInteger(r.code)}a(c,"isRpcError");function i(r){return c(r)?{...r,type:"rpc"}:{type:"unknown",error:r}}a(i,"toTasksError");function d(r){let e=new Uint8Array(3);crypto.getRandomValues(e);let t=Buffer.from(e).toString("hex");return`${r}-${Date.now()}-${t}`}a(d,"createUniqueName");var p=class{constructor(e,t,o,n){this.client=e;this.location=t;this.queueName=o;this.logger=n;this.projectIdPromise=this.client.getProjectId(),this.serviceAccountEmailPromise=new w().getCredentials().then(s=>s.client_email??null)}static{a(this,"CommonTasksClient")}serviceAccountEmailPromise;projectIdPromise;async upsertQueue(e){try{let t=await this.projectIdPromise,o=this.client.queuePath(t,this.location,this.queueName),n=this.client.locationPath(t,this.location);this.logger.info({queuePath:o,parentPath:n},"upserting tasks queue");let s="create";try{await this.client.getQueue({name:o}),s="update"}catch(u){if(c(u)&&u.code===k.NOT_FOUND)s="create";else throw u}return s==="update"?(this.logger.info({queuePath:o},"updating existing queue"),await this.client.updateQueue({queue:{name:o,...e}})):(this.logger.info({queuePath:o},"creating new queue"),await this.client.createQueue({parent:n,queue:{name:o,...e}})),l.EMPTY}catch(t){return h(i(t))}}async listTasks(){try{let e=await this.projectIdPromise,t=this.client.queuePath(e,this.location,this.queueName),[o]=await this.client.listTasks({parent:t});return l(o)}catch(e){return h(i(e))}}async createTask(e,t){try{let o=await this.projectIdPromise,n=this.client.queuePath(o,this.location,this.queueName),s=this.client.taskPath(o,this.location,this.queueName,d(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}}}}),l(s)}catch(o){return h(i(o))}}async getTask(e){try{let[t]=await this.client.getTask({name:e});return l(t)}catch(t){return c(t)&&t.code===k.NOT_FOUND?l(null):h(i(t))}}};var m=class extends p{static{a(this,"EmulatorTasksClient")}constructor(e,t,o,n){super(new C({sslCreds:E.createInsecure(),servicePath:n.host??"localhost",port:n.port}),e,t,o)}async upsertQueue(e){try{let t=await this.projectIdPromise,o=this.client.queuePath(t,this.location,this.queueName),n=this.client.locationPath(t,this.location);this.logger.info({queuePath:o,parentPath:n},"upserting simulator tasks queue");try{return await this.client.createQueue({parent:n,queue:{name:o,...e}}),T.EMPTY}catch(s){return c(s)&&s.code!==P.ALREADY_EXISTS?f(i(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"),T.EMPTY)}}catch(t){return f(i(t))}}async createTask(e,t){return super.createTask(e,{...t,httpRequest:{...t?.httpRequest,url:t?.httpRequest?.url?.replace("//localhost","//host.containers.internal")}})}};import{CloudTasksClient as q}from"@google-cloud/tasks";var g=class extends p{static{a(this,"RegularTasksClient")}constructor(e,t,o){super(new q({}),e,t,o)}};function J({queueName:r,emulator:e=!1,location:t="europe-west6",logger:o={error:console.error,info:console.info,warn:console.warn}}){return o.info({queueName:r,emulator:e,location:t},"creating base tasks client"),e?new m(t,r,o,e):new g(t,r,o)}a(J,"createBaseTaskClient");export{J as createBaseTaskClient};
package/dist/client.cjs CHANGED
@@ -1 +1 @@
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 I=(o,t)=>{for(var e in t)p(o,e,{get:t[e],enumerable:!0})},x=(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=>x(p({},"__esModule",{value:!0}),o);var L={};I(L,{createTasksClient:()=>O});module.exports=S(L);var f=require("@google-cloud/tasks"),R=require("@grpc/grpc-js"),C=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 C.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:R.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}-${Date.now()}-${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");
1
+ "use strict";var h=Object.defineProperty;var I=Object.getOwnPropertyDescriptor;var S=Object.getOwnPropertyNames;var L=Object.prototype.hasOwnProperty;var n=(o,t)=>h(o,"name",{value:t,configurable:!0});var N=(o,t)=>{for(var e in t)h(o,e,{get:t[e],enumerable:!0})},O=(o,t,e,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of S(t))!L.call(o,s)&&s!==e&&h(o,s,{get:()=>t[s],enumerable:!(r=I(t,s))||r.enumerable});return o};var b=o=>O(h({},"__esModule",{value:!0}),o);var A={};N(A,{createTasksClient:()=>j});module.exports=b(A);var R=require("@google-cloud/tasks"),E=require("@grpc/grpc-js"),q=require("google-gax"),m=require("ts-results-es");var P=require("google-auth-library"),T=require("google-gax"),i=require("ts-results-es");function p(o){return o instanceof Error&&"code"in o&&Number.isInteger(o.code)}n(p,"isRpcError");function c(o){return p(o)?{...o,type:"rpc"}:{type:"unknown",error:o}}n(c,"toTasksError");function w(o){let t=new Uint8Array(3);crypto.getRandomValues(t);let e=Buffer.from(t).toString("hex");return`${o}-${Date.now()}-${e}`}n(w,"createUniqueName");var g=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 P.GoogleAuth().getCredentials().then(a=>a.client_email??null)}static{n(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 a="create";try{await this.client.getQueue({name:r}),a="update"}catch(u){if(p(u)&&u.code===T.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,...t}})):(this.logger.info({queuePath:r},"creating new queue"),await this.client.createQueue({parent:s,queue:{name:r,...t}})),i.Ok.EMPTY}catch(e){return(0,i.Err)(c(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,i.Ok)(r)}catch(t){return(0,i.Err)(c(t))}}async createTask(t,e){try{let r=await this.projectIdPromise,s=this.client.queuePath(r,this.location,this.queueName),a=this.client.taskPath(r,this.location,this.queueName,w(t)),u=await this.serviceAccountEmailPromise;return this.logger.info({queuePath:s,taskPath:a,url:e?.httpRequest?.url,payload:e?.httpRequest?.body,serviceAccountEmail:u},"scheduling task"),await this.client.createTask({parent:s,task:{...e,name:a,httpRequest:{...e?.httpRequest,oidcToken:{serviceAccountEmail:u}}}}),(0,i.Ok)(a)}catch(r){return(0,i.Err)(c(r))}}async getTask(t){try{let[e]=await this.client.getTask({name:t});return(0,i.Ok)(e)}catch(e){return p(e)&&e.code===T.Status.NOT_FOUND?(0,i.Ok)(null):(0,i.Err)(c(e))}}};var k=class extends g{static{n(this,"EmulatorTasksClient")}constructor(t,e,r,s){super(new R.CloudTasksClient({sslCreds:E.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}}),m.Ok.EMPTY}catch(a){return p(a)&&a.code!==q.Status.ALREADY_EXISTS?(0,m.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"),m.Ok.EMPTY)}}catch(e){return(0,m.Err)(c(e))}}async createTask(t,e){return super.createTask(t,{...e,httpRequest:{...e?.httpRequest,url:e?.httpRequest?.url?.replace("//localhost","//host.containers.internal")}})}};var x=require("@google-cloud/tasks");var d=class extends g{static{n(this,"RegularTasksClient")}constructor(t,e,r){super(new x.CloudTasksClient({}),t,e,r)}};function v({queueName:o,emulator:t=!1,location:e="europe-west6",logger:r={error:console.error,info:console.info,warn:console.warn}}){return r.info({queueName:o,emulator:t,location:e},"creating base tasks client"),t?new k(e,o,r,t):new d(e,o,r)}n(v,"createBaseTaskClient");function j({tasksWorkerUrl:o,queueOptions:t,...e}){let r=v(e),s=e.logger??{error:console.error,info:console.info,warn:console.warn};return new Proxy({},{get:n((a,u)=>u==="_manage"?{find:n(l=>r.getTask(l).then(f=>f.unwrap()),"find"),list:n(()=>r.listTasks().then(l=>l.unwrap()),"list")}:{schedule:n(async(l,f)=>{let y=u.toString();await r.upsertQueue(t);let C=new URL(`/trpc/${y}`,o).toString();return s.info({url:C,input:l},"scheduling http request that calls trpc mutation of tasks worker"),(await r.createTask(y,{...f,httpRequest:{httpMethod:"POST",url:C,headers:{"Content-Type":"application/json"},body:l?Buffer.from(JSON.stringify(l)).toString("base64"):null}})).unwrap()},"schedule")},"get")})}n(j,"createTasksClient");
package/dist/client.d.ts CHANGED
@@ -1,14 +1,12 @@
1
1
  import type { google } from "@google-cloud/tasks/build/protos";
2
2
  import type { TRPCClient } from "@trpc/client";
3
3
  import type { AnyRouter } from "@trpc/server";
4
- import type { EmulatorConfig, GcpLocation, TaskConfig } from "./tasksClients";
5
- export declare function createTasksClient<Router extends AnyRouter>({ queueName, emulator, location, tasksWorkerUrl, queueOptions, logger, }: {
6
- queueName: string;
7
- location?: GcpLocation;
4
+ import type { BaseTaskClientConfig } from "./base";
5
+ import type { TaskConfig } from "./tasksClients";
6
+ export type { Logger } from "./base";
7
+ export declare function createTasksClient<Router extends AnyRouter>({ tasksWorkerUrl, queueOptions, ...baseConfig }: BaseTaskClientConfig & {
8
8
  queueOptions?: Omit<google.cloud.tasks.v2.IQueue, "name">;
9
9
  tasksWorkerUrl: URL;
10
- logger?: Logger;
11
- emulator?: false | EmulatorConfig;
12
10
  }): ScheduleClient<TRPCClient<Router>>;
13
11
  type RemoveNeverKeys<T> = {
14
12
  [K in keyof T as T[K] extends never ? never : K]: T[K];
@@ -49,10 +47,4 @@ export type ScheduleClient<T, M = OnlyMutate<T>> = {
49
47
  */
50
48
  _manage: ScheduleClientManage;
51
49
  };
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
- };
57
- export {};
58
50
  //# sourceMappingURL=client.d.ts.map
@@ -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,UAAU,EAAE,MAAM,cAAc,CAAC;AAC/C,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,KAAK,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC1D,cAAc,EAAE,GAAG,CAAC;IACpB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,QAAQ,CAAC,EAAE,KAAK,GAAG,cAAc,CAAC;CACnC,GAAG,cAAc,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAwDrC;AAED,KAAK,eAAe,CAAC,CAAC,IAAI;KACvB,CAAC,IAAI,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS,KAAK,GAAG,KAAK,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;CACvD,CAAC;AAEF,KAAK,UAAU,CAAC,CAAC,IAAI,eAAe,CAAC;KAClC,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS;QAAE,MAAM,EAAE,CAAC,GAAG,IAAI,EAAE,MAAM,EAAE,KAAK,OAAO,CAAA;KAAE,GACnE,CAAC,CAAC,CAAC,CAAC,GACJ,KAAK;CACV,CAAC,CAAC;AAEH,KAAK,cAAc,GAAG,MAAM,CAAC;AAE7B,KAAK,mBAAmB,CAAC,CAAC,IAAI,CAAC,SAAS;IACtC,MAAM,EAAE,CAAC,GAAG,IAAI,EAAE,MAAM,EAAE,KAAK,OAAO,CAAC;CACxC,GACG,CACE,KAAK,EAAE,UAAU,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,EACjC,UAAU,CAAC,EAAE,UAAU,KACpB,OAAO,CAAC,cAAc,CAAC,GAC5B,KAAK,CAAC;AAEV,KAAK,oBAAoB,GAAG;IAC1B,IAAI,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,GAAG,IAAI,CAAC,CAAC;IACxE,IAAI,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC,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"}
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,UAAU,EAAE,MAAM,cAAc,CAAC;AAC/C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAE9C,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,QAAQ,CAAC;AAEnD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAEjD,YAAY,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAErC,wBAAgB,iBAAiB,CAAC,MAAM,SAAS,SAAS,EAAE,EAC1D,cAAc,EACd,YAAY,EACZ,GAAG,UAAU,EACd,EAAE,oBAAoB,GAAG;IACxB,YAAY,CAAC,EAAE,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC1D,cAAc,EAAE,GAAG,CAAC;CACrB,GAAG,cAAc,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAsDrC;AAED,KAAK,eAAe,CAAC,CAAC,IAAI;KACvB,CAAC,IAAI,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS,KAAK,GAAG,KAAK,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;CACvD,CAAC;AAEF,KAAK,UAAU,CAAC,CAAC,IAAI,eAAe,CAAC;KAClC,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS;QAAE,MAAM,EAAE,CAAC,GAAG,IAAI,EAAE,MAAM,EAAE,KAAK,OAAO,CAAA;KAAE,GACnE,CAAC,CAAC,CAAC,CAAC,GACJ,KAAK;CACV,CAAC,CAAC;AAEH,KAAK,cAAc,GAAG,MAAM,CAAC;AAE7B,KAAK,mBAAmB,CAAC,CAAC,IAAI,CAAC,SAAS;IACtC,MAAM,EAAE,CAAC,GAAG,IAAI,EAAE,MAAM,EAAE,KAAK,OAAO,CAAC;CACxC,GACG,CACE,KAAK,EAAE,UAAU,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,EACjC,UAAU,CAAC,EAAE,UAAU,KACpB,OAAO,CAAC,cAAc,CAAC,GAC5B,KAAK,CAAC;AAEV,KAAK,oBAAoB,GAAG;IAC1B,IAAI,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,GAAG,IAAI,CAAC,CAAC;IACxE,IAAI,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC,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"}
package/dist/client.mjs CHANGED
@@ -1 +1 @@
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 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: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!==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}-${Date.now()}-${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};
1
+ var E=Object.defineProperty;var s=(o,t)=>E(o,"name",{value:t,configurable:!0});import{CloudTasksClient as x}from"@google-cloud/tasks";import{credentials as v}from"@grpc/grpc-js";import{Status as I}from"google-gax";import{Err as w,Ok as P}from"ts-results-es";import{GoogleAuth as q}from"google-auth-library";import{Status as C}from"google-gax";import{Err as d,Ok as g}from"ts-results-es";function l(o){return o instanceof Error&&"code"in o&&Number.isInteger(o.code)}s(l,"isRpcError");function u(o){return l(o)?{...o,type:"rpc"}:{type:"unknown",error:o}}s(u,"toTasksError");function y(o){let t=new Uint8Array(3);crypto.getRandomValues(t);let e=Buffer.from(t).toString("hex");return`${o}-${Date.now()}-${e}`}s(y,"createUniqueName");var p=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(a=>a.client_email??null)}static{s(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 a="create";try{await this.client.getQueue({name:r}),a="update"}catch(i){if(l(i)&&i.code===C.NOT_FOUND)a="create";else throw i}return a==="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}})),g.EMPTY}catch(e){return d(u(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 g(r)}catch(t){return d(u(t))}}async createTask(t,e){try{let r=await this.projectIdPromise,n=this.client.queuePath(r,this.location,this.queueName),a=this.client.taskPath(r,this.location,this.queueName,y(t)),i=await this.serviceAccountEmailPromise;return this.logger.info({queuePath:n,taskPath:a,url:e?.httpRequest?.url,payload:e?.httpRequest?.body,serviceAccountEmail:i},"scheduling task"),await this.client.createTask({parent:n,task:{...e,name:a,httpRequest:{...e?.httpRequest,oidcToken:{serviceAccountEmail:i}}}}),g(a)}catch(r){return d(u(r))}}async getTask(t){try{let[e]=await this.client.getTask({name:t});return g(e)}catch(e){return l(e)&&e.code===C.NOT_FOUND?g(null):d(u(e))}}};var m=class extends p{static{s(this,"EmulatorTasksClient")}constructor(t,e,r,n){super(new x({sslCreds:v.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}}),P.EMPTY}catch(a){return l(a)&&a.code!==I.ALREADY_EXISTS?w(u(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"),P.EMPTY)}}catch(e){return w(u(e))}}async createTask(t,e){return super.createTask(t,{...e,httpRequest:{...e?.httpRequest,url:e?.httpRequest?.url?.replace("//localhost","//host.containers.internal")}})}};import{CloudTasksClient as S}from"@google-cloud/tasks";var k=class extends p{static{s(this,"RegularTasksClient")}constructor(t,e,r){super(new S({}),t,e,r)}};function R({queueName:o,emulator:t=!1,location:e="europe-west6",logger:r={error:console.error,info:console.info,warn:console.warn}}){return r.info({queueName:o,emulator:t,location:e},"creating base tasks client"),t?new m(e,o,r,t):new k(e,o,r)}s(R,"createBaseTaskClient");function ne({tasksWorkerUrl:o,queueOptions:t,...e}){let r=R(e),n=e.logger??{error:console.error,info:console.info,warn:console.warn};return new Proxy({},{get:s((a,i)=>i==="_manage"?{find:s(c=>r.getTask(c).then(h=>h.unwrap()),"find"),list:s(()=>r.listTasks().then(c=>c.unwrap()),"list")}:{schedule:s(async(c,h)=>{let f=i.toString();await r.upsertQueue(t);let T=new URL(`/trpc/${f}`,o).toString();return n.info({url:T,input:c},"scheduling http request that calls trpc mutation of tasks worker"),(await r.createTask(f,{...h,httpRequest:{httpMethod:"POST",url:T,headers:{"Content-Type":"application/json"},body:c?Buffer.from(JSON.stringify(c)).toString("base64"):null}})).unwrap()},"schedule")},"get")})}s(ne,"createTasksClient");export{ne as createTasksClient};
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";var A=Object.create;var p=Object.defineProperty;var N=Object.getOwnPropertyDescriptor;var M=Object.getOwnPropertyNames;var O=Object.getPrototypeOf,L=Object.prototype.hasOwnProperty;var a=(r,t)=>p(r,"name",{value:t,configurable:!0});var j=(r,t)=>{for(var e in t)p(r,e,{get:t[e],enumerable:!0})},P=(r,t,e,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of M(t))!L.call(r,s)&&s!==e&&p(r,s,{get:()=>t[s],enumerable:!(o=N(t,s))||o.enumerable});return r};var Q=(r,t,e)=>(e=r!=null?A(O(r)):{},P(t||!r||!r.__esModule?p(e,"default",{value:r,enumerable:!0}):e,r)),K=r=>P(p({},"__esModule",{value:!0}),r);var G={};j(G,{createExpressTaskServer:()=>I,createExpressTaskServerMiddleware:()=>S,createTasksClient:()=>_,createTasksServer:()=>U});module.exports=K(G);var T=require("@google-cloud/tasks"),C=require("@grpc/grpc-js"),x=require("google-auth-library"),g=require("google-gax"),i=require("ts-results-es");var d=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 x.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,b(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))}}},m=class extends d{static{a(this,"RegularTasksClient")}constructor(t,e,o){super(new T.CloudTasksClient({}),t,e,o)}},h=class extends d{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 b(r){let t=new Uint8Array(3);crypto.getRandomValues(t);let e=Buffer.from(t).toString("hex");return`${r}-${Date.now()}-${e}`}a(b,"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 _({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 h(e,r,n,t):new m(e,r,n);return new Proxy({},{get:a((D,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 R=new URL(`/trpc/${w}`,o).toString();return n.info({url:R,input:c},"scheduling http request that calls trpc mutation of tasks worker"),(await u.createTask(w,{...f,httpRequest:{httpMethod:"POST",url:R,headers:{"Content-Type":"application/json"},body:c?Buffer.from(JSON.stringify(c)).toString("base64"):null}})).unwrap()},"schedule")},"get")})}a(_,"createTasksClient");var v=require("@trpc/server"),q=require("@trpc/server/adapters/express"),E=Q(require("express"));var S=a(r=>{let t=v.initTRPC.create(),e=r(t);return{expressMiddleware:(0,q.createExpressMiddleware)({router:e}),router:e}},"createExpressTaskServerMiddleware");function I(r,t){let e=t??(0,E.default)(),{expressMiddleware:o,router:s}=S(r);return e.use("/trpc",o),{runServer:a(n=>{e.listen({port:n})},"runServer"),router:s}}a(I,"createExpressTaskServer");var U=I;
1
+ "use strict";var O=Object.create;var d=Object.defineProperty;var b=Object.getOwnPropertyDescriptor;var M=Object.getOwnPropertyNames;var j=Object.getPrototypeOf,Q=Object.prototype.hasOwnProperty;var n=(r,t)=>d(r,"name",{value:t,configurable:!0});var G=(r,t)=>{for(var e in t)d(r,e,{get:t[e],enumerable:!0})},x=(r,t,e,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of M(t))!Q.call(r,s)&&s!==e&&d(r,s,{get:()=>t[s],enumerable:!(o=b(t,s))||o.enumerable});return r};var K=(r,t,e)=>(e=r!=null?O(j(r)):{},x(t||!r||!r.__esModule?d(e,"default",{value:r,enumerable:!0}):e,r)),U=r=>x(d({},"__esModule",{value:!0}),r);var F={};G(F,{createExpressTaskServer:()=>_,createExpressTaskServerMiddleware:()=>A,createTasksClient:()=>B});module.exports=U(F);var P=require("@google-cloud/tasks"),E=require("@grpc/grpc-js"),q=require("google-gax"),g=require("ts-results-es");var w=require("google-auth-library"),T=require("google-gax"),i=require("ts-results-es");function l(r){return r instanceof Error&&"code"in r&&Number.isInteger(r.code)}n(l,"isRpcError");function c(r){return l(r)?{...r,type:"rpc"}:{type:"unknown",error:r}}n(c,"toTasksError");function R(r){let t=new Uint8Array(3);crypto.getRandomValues(t);let e=Buffer.from(t).toString("hex");return`${r}-${Date.now()}-${e}`}n(R,"createUniqueName");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 w.GoogleAuth().getCredentials().then(a=>a.client_email??null)}static{n(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 a="create";try{await this.client.getQueue({name:o}),a="update"}catch(u){if(l(u)&&u.code===T.Status.NOT_FOUND)a="create";else throw u}return a==="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)(c(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)(c(t))}}async createTask(t,e){try{let o=await this.projectIdPromise,s=this.client.queuePath(o,this.location,this.queueName),a=this.client.taskPath(o,this.location,this.queueName,R(t)),u=await this.serviceAccountEmailPromise;return this.logger.info({queuePath:s,taskPath:a,url:e?.httpRequest?.url,payload:e?.httpRequest?.body,serviceAccountEmail:u},"scheduling task"),await this.client.createTask({parent:s,task:{...e,name:a,httpRequest:{...e?.httpRequest,oidcToken:{serviceAccountEmail:u}}}}),(0,i.Ok)(a)}catch(o){return(0,i.Err)(c(o))}}async getTask(t){try{let[e]=await this.client.getTask({name:t});return(0,i.Ok)(e)}catch(e){return l(e)&&e.code===T.Status.NOT_FOUND?(0,i.Ok)(null):(0,i.Err)(c(e))}}};var k=class extends m{static{n(this,"EmulatorTasksClient")}constructor(t,e,o,s){super(new P.CloudTasksClient({sslCreds:E.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}}),g.Ok.EMPTY}catch(a){return l(a)&&a.code!==q.Status.ALREADY_EXISTS?(0,g.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"),g.Ok.EMPTY)}}catch(e){return(0,g.Err)(c(e))}}async createTask(t,e){return super.createTask(t,{...e,httpRequest:{...e?.httpRequest,url:e?.httpRequest?.url?.replace("//localhost","//host.containers.internal")}})}};var v=require("@google-cloud/tasks");var f=class extends m{static{n(this,"RegularTasksClient")}constructor(t,e,o){super(new v.CloudTasksClient({}),t,e,o)}};function S({queueName:r,emulator:t=!1,location:e="europe-west6",logger:o={error:console.error,info:console.info,warn:console.warn}}){return o.info({queueName:r,emulator:t,location:e},"creating base tasks client"),t?new k(e,r,o,t):new f(e,r,o)}n(S,"createBaseTaskClient");function B({tasksWorkerUrl:r,queueOptions:t,...e}){let o=S(e),s=e.logger??{error:console.error,info:console.info,warn:console.warn};return new Proxy({},{get:n((a,u)=>u==="_manage"?{find:n(p=>o.getTask(p).then(h=>h.unwrap()),"find"),list:n(()=>o.listTasks().then(p=>p.unwrap()),"list")}:{schedule:n(async(p,h)=>{let C=u.toString();await o.upsertQueue(t);let y=new URL(`/trpc/${C}`,r).toString();return s.info({url:y,input:p},"scheduling http request that calls trpc mutation of tasks worker"),(await o.createTask(C,{...h,httpRequest:{httpMethod:"POST",url:y,headers:{"Content-Type":"application/json"},body:p?Buffer.from(JSON.stringify(p)).toString("base64"):null}})).unwrap()},"schedule")},"get")})}n(B,"createTasksClient");var I=require("@trpc/server"),L=require("@trpc/server/adapters/express"),N=K(require("express"));var A=n(({createRouter:r,createContext:t})=>{let e=I.initTRPC.context().create(),o=r(e);return{expressMiddleware:(0,L.createExpressMiddleware)({router:o,createContext:t}),router:o}},"createExpressTaskServerMiddleware");function _({createRouter:r,createContext:t,app:e=(0,N.default)()}){let{expressMiddleware:o,router:s}=A({createRouter:r,createContext:t});return e.use("/trpc",o),{runServer:n(a=>{e.listen({port:a})},"runServer"),router:s}}n(_,"createExpressTaskServer");
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- var P=Object.defineProperty;var n=(o,t)=>P(o,"name",{value:t,configurable:!0});import{CloudTasksClient as R}from"@google-cloud/tasks";import{credentials as C}from"@grpc/grpc-js";import{GoogleAuth as x}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,a){this.client=t;this.location=e;this.queueName=r;this.logger=a;this.projectIdPromise=this.client.getProjectId(),this.serviceAccountEmailPromise=new x().getCredentials().then(s=>s.client_email??null)}static{n(this,"CommonTasksClient")}serviceAccountEmailPromise;projectIdPromise;async upsertQueue(t){try{let e=await this.projectIdPromise,r=this.client.queuePath(e,this.location,this.queueName),a=this.client.locationPath(e,this.location);this.logger.info({queuePath:r,parentPath:a},"upserting tasks queue");let s="create";try{await this.client.getQueue({name:r}),s="update"}catch(i){if(h(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:a,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,a=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:a,taskPath:s,url:e?.httpRequest?.url,payload:e?.httpRequest?.body,serviceAccountEmail:i},"scheduling task"),await this.client.createTask({parent:a,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 h(e)&&e.code===f.NOT_FOUND?c(null):l(p(e))}}},d=class extends g{static{n(this,"RegularTasksClient")}constructor(t,e,r){super(new R({}),t,e,r)}},m=class extends g{static{n(this,"EmulatorTasksClient")}constructor(t,e,r,a){super(new R({sslCreds:C.createInsecure(),servicePath:a.host??"localhost",port:a.port}),t,e,r)}async upsertQueue(t){try{let e=await this.projectIdPromise,r=this.client.queuePath(e,this.location,this.queueName),a=this.client.locationPath(e,this.location);this.logger.info({queuePath:r,parentPath:a},"upserting simulator tasks queue");try{return await this.client.createQueue({parent:a,queue:{name:r,...t}}),c.EMPTY}catch(s){return h(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}-${Date.now()}-${e}`}n(v,"createUniqueName");function h(o){return o instanceof Error&&"code"in o&&Number.isInteger(o.code)}n(h,"isRpcError");function p(o){return h(o)?{...o,type:"rpc"}:{type:"unknown",error:o}}n(p,"toTasksError");function D({queueName:o,emulator:t=!1,location:e="europe-west6",tasksWorkerUrl:r,queueOptions:a,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 d(e,o,s);return new Proxy({},{get:n((N,T)=>T==="_manage"?{find:n(u=>i.getTask(u).then(k=>k.unwrap()),"find"),list:n(()=>i.listTasks().then(u=>u.unwrap()),"list")}:{schedule:n(async(u,k)=>{let y=T.toString();await i.upsertQueue(a);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")})}n(D,"createTasksClient");import{initTRPC as q}from"@trpc/server";import{createExpressMiddleware as E}from"@trpc/server/adapters/express";import S from"express";var I=n(o=>{let t=q.create(),e=o(t);return{expressMiddleware:E({router:e}),router:e}},"createExpressTaskServerMiddleware");function A(o,t){let e=t??S(),{expressMiddleware:r,router:a}=I(o);return e.use("/trpc",r),{runServer:n(s=>{e.listen({port:s})},"runServer"),router:a}}n(A,"createExpressTaskServer");var V=A;export{A as createExpressTaskServer,I as createExpressTaskServerMiddleware,D as createTasksClient,V as createTasksServer};
1
+ var P=Object.defineProperty;var s=(o,r)=>P(o,"name",{value:r,configurable:!0});import{CloudTasksClient as q}from"@google-cloud/tasks";import{credentials as v}from"@grpc/grpc-js";import{Status as S}from"google-gax";import{Err as x,Ok as R}from"ts-results-es";import{GoogleAuth as E}from"google-auth-library";import{Status as y}from"google-gax";import{Err as k,Ok as m}from"ts-results-es";function p(o){return o instanceof Error&&"code"in o&&Number.isInteger(o.code)}s(p,"isRpcError");function u(o){return p(o)?{...o,type:"rpc"}:{type:"unknown",error:o}}s(u,"toTasksError");function C(o){let r=new Uint8Array(3);crypto.getRandomValues(r);let e=Buffer.from(r).toString("hex");return`${o}-${Date.now()}-${e}`}s(C,"createUniqueName");var l=class{constructor(r,e,t,n){this.client=r;this.location=e;this.queueName=t;this.logger=n;this.projectIdPromise=this.client.getProjectId(),this.serviceAccountEmailPromise=new E().getCredentials().then(a=>a.client_email??null)}static{s(this,"CommonTasksClient")}serviceAccountEmailPromise;projectIdPromise;async upsertQueue(r){try{let e=await this.projectIdPromise,t=this.client.queuePath(e,this.location,this.queueName),n=this.client.locationPath(e,this.location);this.logger.info({queuePath:t,parentPath:n},"upserting tasks queue");let a="create";try{await this.client.getQueue({name:t}),a="update"}catch(i){if(p(i)&&i.code===y.NOT_FOUND)a="create";else throw i}return a==="update"?(this.logger.info({queuePath:t},"updating existing queue"),await this.client.updateQueue({queue:{name:t,...r}})):(this.logger.info({queuePath:t},"creating new queue"),await this.client.createQueue({parent:n,queue:{name:t,...r}})),m.EMPTY}catch(e){return k(u(e))}}async listTasks(){try{let r=await this.projectIdPromise,e=this.client.queuePath(r,this.location,this.queueName),[t]=await this.client.listTasks({parent:e});return m(t)}catch(r){return k(u(r))}}async createTask(r,e){try{let t=await this.projectIdPromise,n=this.client.queuePath(t,this.location,this.queueName),a=this.client.taskPath(t,this.location,this.queueName,C(r)),i=await this.serviceAccountEmailPromise;return this.logger.info({queuePath:n,taskPath:a,url:e?.httpRequest?.url,payload:e?.httpRequest?.body,serviceAccountEmail:i},"scheduling task"),await this.client.createTask({parent:n,task:{...e,name:a,httpRequest:{...e?.httpRequest,oidcToken:{serviceAccountEmail:i}}}}),m(a)}catch(t){return k(u(t))}}async getTask(r){try{let[e]=await this.client.getTask({name:r});return m(e)}catch(e){return p(e)&&e.code===y.NOT_FOUND?m(null):k(u(e))}}};var g=class extends l{static{s(this,"EmulatorTasksClient")}constructor(r,e,t,n){super(new q({sslCreds:v.createInsecure(),servicePath:n.host??"localhost",port:n.port}),r,e,t)}async upsertQueue(r){try{let e=await this.projectIdPromise,t=this.client.queuePath(e,this.location,this.queueName),n=this.client.locationPath(e,this.location);this.logger.info({queuePath:t,parentPath:n},"upserting simulator tasks queue");try{return await this.client.createQueue({parent:n,queue:{name:t,...r}}),R.EMPTY}catch(a){return p(a)&&a.code!==S.ALREADY_EXISTS?x(u(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"),R.EMPTY)}}catch(e){return x(u(e))}}async createTask(r,e){return super.createTask(r,{...e,httpRequest:{...e?.httpRequest,url:e?.httpRequest?.url?.replace("//localhost","//host.containers.internal")}})}};import{CloudTasksClient as I}from"@google-cloud/tasks";var d=class extends l{static{s(this,"RegularTasksClient")}constructor(r,e,t){super(new I({}),r,e,t)}};function w({queueName:o,emulator:r=!1,location:e="europe-west6",logger:t={error:console.error,info:console.info,warn:console.warn}}){return t.info({queueName:o,emulator:r,location:e},"creating base tasks client"),r?new g(e,o,t,r):new d(e,o,t)}s(w,"createBaseTaskClient");function ce({tasksWorkerUrl:o,queueOptions:r,...e}){let t=w(e),n=e.logger??{error:console.error,info:console.info,warn:console.warn};return new Proxy({},{get:s((a,i)=>i==="_manage"?{find:s(c=>t.getTask(c).then(f=>f.unwrap()),"find"),list:s(()=>t.listTasks().then(c=>c.unwrap()),"list")}:{schedule:s(async(c,f)=>{let h=i.toString();await t.upsertQueue(r);let T=new URL(`/trpc/${h}`,o).toString();return n.info({url:T,input:c},"scheduling http request that calls trpc mutation of tasks worker"),(await t.createTask(h,{...f,httpRequest:{httpMethod:"POST",url:T,headers:{"Content-Type":"application/json"},body:c?Buffer.from(JSON.stringify(c)).toString("base64"):null}})).unwrap()},"schedule")},"get")})}s(ce,"createTasksClient");import{initTRPC as L}from"@trpc/server";import{createExpressMiddleware as N}from"@trpc/server/adapters/express";import A from"express";var O=s(({createRouter:o,createContext:r})=>{let e=L.context().create(),t=o(e);return{expressMiddleware:N({router:t,createContext:r}),router:t}},"createExpressTaskServerMiddleware");function ke({createRouter:o,createContext:r,app:e=A()}){let{expressMiddleware:t,router:n}=O({createRouter:o,createContext:r});return e.use("/trpc",t),{runServer:s(a=>{e.listen({port:a})},"runServer"),router:n}}s(ke,"createExpressTaskServer");export{ke as createExpressTaskServer,O as createExpressTaskServerMiddleware,ce as createTasksClient};
package/dist/server.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";var m=Object.create;var n=Object.defineProperty;var y=Object.getOwnPropertyDescriptor;var l=Object.getOwnPropertyNames;var f=Object.getPrototypeOf,A=Object.prototype.hasOwnProperty;var p=(e,r)=>n(e,"name",{value:r,configurable:!0});var T=(e,r)=>{for(var t in r)n(e,t,{get:r[t],enumerable:!0})},u=(e,r,t,s)=>{if(r&&typeof r=="object"||typeof r=="function")for(let o of l(r))!A.call(e,o)&&o!==t&&n(e,o,{get:()=>r[o],enumerable:!(s=y(r,o))||s.enumerable});return e};var v=(e,r,t)=>(t=e!=null?m(f(e)):{},u(r||!e||!e.__esModule?n(t,"default",{value:e,enumerable:!0}):t,e)),w=e=>u(n({},"__esModule",{value:!0}),e);var M={};T(M,{createExpressTaskServer:()=>x,createExpressTaskServerMiddleware:()=>R,createTasksServer:()=>C});module.exports=w(M);var a=require("@trpc/server"),c=require("@trpc/server/adapters/express"),i=v(require("express"));var R=p(e=>{let r=a.initTRPC.create(),t=e(r);return{expressMiddleware:(0,c.createExpressMiddleware)({router:t}),router:t}},"createExpressTaskServerMiddleware");function x(e,r){let t=r??(0,i.default)(),{expressMiddleware:s,router:o}=R(e);return t.use("/trpc",s),{runServer:p(d=>{t.listen({port:d})},"runServer"),router:o}}p(x,"createExpressTaskServer");var C=x;
1
+ "use strict";var i=Object.create;var s=Object.defineProperty;var d=Object.getOwnPropertyDescriptor;var y=Object.getOwnPropertyNames;var T=Object.getPrototypeOf,m=Object.prototype.hasOwnProperty;var x=(e,t)=>s(e,"name",{value:t,configurable:!0});var A=(e,t)=>{for(var r in t)s(e,r,{get:t[r],enumerable:!0})},p=(e,t,r,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of y(t))!m.call(e,o)&&o!==r&&s(e,o,{get:()=>t[o],enumerable:!(n=d(t,o))||n.enumerable});return e};var w=(e,t,r)=>(r=e!=null?i(T(e)):{},p(t||!e||!e.__esModule?s(r,"default",{value:e,enumerable:!0}):r,e)),f=e=>p(s({},"__esModule",{value:!0}),e);var P={};A(P,{createExpressTaskServer:()=>l,createExpressTaskServerMiddleware:()=>R});module.exports=f(P);var a=require("@trpc/server"),C=require("@trpc/server/adapters/express"),u=w(require("express"));var R=x(({createRouter:e,createContext:t})=>{let r=a.initTRPC.context().create(),n=e(r);return{expressMiddleware:(0,C.createExpressMiddleware)({router:n,createContext:t}),router:n}},"createExpressTaskServerMiddleware");function l({createRouter:e,createContext:t,app:r=(0,u.default)()}){let{expressMiddleware:n,router:o}=R({createRouter:e,createContext:t});return r.use("/trpc",n),{runServer:x(c=>{r.listen({port:c})},"runServer"),router:o}}x(l,"createExpressTaskServer");
package/dist/server.d.ts CHANGED
@@ -1,18 +1,23 @@
1
1
  import type { AnyRouter } from "@trpc/server";
2
2
  import { initTRPC } from "@trpc/server";
3
+ import { NodeHTTPCreateContextFnOptions } from "@trpc/server/adapters/node-http";
3
4
  import express from "express";
4
- type CreateRouter<Router extends AnyRouter> = (t: ReturnType<(typeof initTRPC)["create"]>) => Router;
5
- export declare const createExpressTaskServerMiddleware: <Router extends AnyRouter>(createRouter: CreateRouter<Router>) => {
5
+ type TRPCInstance<Context extends object> = ReturnType<ReturnType<typeof initTRPC.context<Context>>["create"]>;
6
+ type CreateRouter<Router extends AnyRouter, Context extends object> = (t: TRPCInstance<Context>) => Router;
7
+ export declare const createExpressTaskServerMiddleware: <Router extends AnyRouter, CreateContext extends (opts: NodeHTTPCreateContextFnOptions<express.Request, express.Response>) => any>({ createRouter, createContext, }: {
8
+ createRouter: CreateRouter<Router, Awaited<ReturnType<CreateContext>>>;
9
+ createContext?: CreateContext;
10
+ }) => {
6
11
  expressMiddleware: express.Handler;
7
12
  router: Router;
8
13
  };
9
- export declare function createExpressTaskServer<Router extends AnyRouter>(createRouter: CreateRouter<Router>, existingApp?: express.Application): {
14
+ export declare function createExpressTaskServer<Router extends AnyRouter, CreateContext extends (opts: NodeHTTPCreateContextFnOptions<express.Request, express.Response>) => any>({ createRouter, createContext, app, }: {
15
+ createRouter: CreateRouter<Router, Awaited<ReturnType<CreateContext>>>;
16
+ createContext: CreateContext;
17
+ app?: express.Application;
18
+ }): {
10
19
  runServer: (port: number) => void;
11
20
  router: Router;
12
21
  };
13
- /**
14
- * @deprecated Use `createExpressTaskServer` instead
15
- */
16
- export declare const createTasksServer: typeof createExpressTaskServer;
17
22
  export {};
18
23
  //# sourceMappingURL=server.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAExC,OAAO,OAAO,MAAM,SAAS,CAAC;AAE9B,KAAK,YAAY,CAAC,MAAM,SAAS,SAAS,IAAI,CAC5C,CAAC,EAAE,UAAU,CAAC,CAAC,OAAO,QAAQ,CAAC,CAAC,QAAQ,CAAC,CAAC,KACvC,MAAM,CAAC;AAEZ,eAAO,MAAM,iCAAiC,GAAI,MAAM,SAAS,SAAS,EACxE,cAAc,YAAY,CAAC,MAAM,CAAC;;;CASnC,CAAC;AACF,wBAAgB,uBAAuB,CAAC,MAAM,SAAS,SAAS,EAC9D,YAAY,EAAE,YAAY,CAAC,MAAM,CAAC,EAClC,WAAW,CAAC,EAAE,OAAO,CAAC,WAAW;sBAQb,MAAM;;EAK3B;AAED;;GAEG;AACH,eAAO,MAAM,iBAAiB,gCAA0B,CAAC"}
1
+ {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAExC,OAAO,EAAE,8BAA8B,EAAE,MAAM,iCAAiC,CAAC;AACjF,OAAO,OAAO,MAAM,SAAS,CAAC;AAE9B,KAAK,YAAY,CAAC,OAAO,SAAS,MAAM,IAAI,UAAU,CACpD,UAAU,CAAC,OAAO,QAAQ,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,QAAQ,CAAC,CACvD,CAAC;AAEF,KAAK,YAAY,CAAC,MAAM,SAAS,SAAS,EAAE,OAAO,SAAS,MAAM,IAAI,CACpE,CAAC,EAAE,YAAY,CAAC,OAAO,CAAC,KACrB,MAAM,CAAC;AAEZ,eAAO,MAAM,iCAAiC,GAC5C,MAAM,SAAS,SAAS,EACxB,aAAa,SAAS,CACpB,IAAI,EAAE,8BAA8B,CAAC,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,QAAQ,CAAC,KACpE,GAAG,EACR,kCAGC;IACD,YAAY,EAAE,YAAY,CAAC,MAAM,EAAE,OAAO,CAAC,UAAU,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC;IACvE,aAAa,CAAC,EAAE,aAAa,CAAC;CAC/B;;;CAaA,CAAC;AACF,wBAAgB,uBAAuB,CACrC,MAAM,SAAS,SAAS,EACxB,aAAa,SAAS,CACpB,IAAI,EAAE,8BAA8B,CAAC,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,QAAQ,CAAC,KACpE,GAAG,EACR,EACA,YAAY,EACZ,aAAa,EACb,GAAe,GAChB,EAAE;IACD,YAAY,EAAE,YAAY,CAAC,MAAM,EAAE,OAAO,CAAC,UAAU,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC;IACvE,aAAa,EAAE,aAAa,CAAC;IAC7B,GAAG,CAAC,EAAE,OAAO,CAAC,WAAW,CAAC;CAC3B;sBAQqB,MAAM;;EAK3B"}
package/dist/server.mjs CHANGED
@@ -1 +1 @@
1
- var u=Object.defineProperty;var o=(e,r)=>u(e,"name",{value:r,configurable:!0});import{initTRPC as a}from"@trpc/server";import{createExpressMiddleware as c}from"@trpc/server/adapters/express";import i from"express";var R=o(e=>{let r=a.create(),t=e(r);return{expressMiddleware:c({router:t}),router:t}},"createExpressTaskServerMiddleware");function x(e,r){let t=r??i(),{expressMiddleware:s,router:n}=R(e);return t.use("/trpc",s),{runServer:o(p=>{t.listen({port:p})},"runServer"),router:n}}o(x,"createExpressTaskServer");var f=x;export{x as createExpressTaskServer,R as createExpressTaskServerMiddleware,f as createTasksServer};
1
+ var p=Object.defineProperty;var n=(e,t)=>p(e,"name",{value:t,configurable:!0});import{initTRPC as a}from"@trpc/server";import{createExpressMiddleware as C}from"@trpc/server/adapters/express";import u from"express";var R=n(({createRouter:e,createContext:t})=>{let r=a.context().create(),o=e(r);return{expressMiddleware:C({router:o,createContext:t}),router:o}},"createExpressTaskServerMiddleware");function T({createRouter:e,createContext:t,app:r=u()}){let{expressMiddleware:o,router:s}=R({createRouter:e,createContext:t});return r.use("/trpc",o),{runServer:n(x=>{r.listen({port:x})},"runServer"),router:s}}n(T,"createExpressTaskServer");export{T as createExpressTaskServer,R as createExpressTaskServerMiddleware};
@@ -0,0 +1 @@
1
+ "use strict";var l=Object.defineProperty;var d=Object.getOwnPropertyDescriptor;var y=Object.getOwnPropertyNames;var f=Object.prototype.hasOwnProperty;var u=(r,t)=>l(r,"name",{value:t,configurable:!0});var T=(r,t)=>{for(var e in t)l(r,e,{get:t[e],enumerable:!0})},P=(r,t,e,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of y(t))!f.call(r,s)&&s!==e&&l(r,s,{get:()=>t[s],enumerable:!(o=d(t,s))||o.enumerable});return r};var w=r=>P(l({},"__esModule",{value:!0}),r);var E={};T(E,{CommonTasksClient:()=>m});module.exports=w(E);var k=require("google-auth-library"),g=require("google-gax"),i=require("ts-results-es");function p(r){return r instanceof Error&&"code"in r&&Number.isInteger(r.code)}u(p,"isRpcError");function c(r){return p(r)?{...r,type:"rpc"}:{type:"unknown",error:r}}u(c,"toTasksError");function h(r){let t=new Uint8Array(3);crypto.getRandomValues(t);let e=Buffer.from(t).toString("hex");return`${r}-${Date.now()}-${e}`}u(h,"createUniqueName");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 k.GoogleAuth().getCredentials().then(n=>n.client_email??null)}static{u(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(a){if(p(a)&&a.code===g.Status.NOT_FOUND)n="create";else throw a}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)(c(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)(c(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,h(t)),a=await this.serviceAccountEmailPromise;return this.logger.info({queuePath:s,taskPath:n,url:e?.httpRequest?.url,payload:e?.httpRequest?.body,serviceAccountEmail:a},"scheduling task"),await this.client.createTask({parent:s,task:{...e,name:n,httpRequest:{...e?.httpRequest,oidcToken:{serviceAccountEmail:a}}}}),(0,i.Ok)(n)}catch(o){return(0,i.Err)(c(o))}}async getTask(t){try{let[e]=await this.client.getTask({name:t});return(0,i.Ok)(e)}catch(e){return p(e)&&e.code===g.Status.NOT_FOUND?(0,i.Ok)(null):(0,i.Err)(c(e))}}};
@@ -0,0 +1,19 @@
1
+ import type { CloudTasksClient } from "@google-cloud/tasks";
2
+ import type { google } from "@google-cloud/tasks/build/protos";
3
+ import { Err, Ok } from "ts-results-es";
4
+ import type { Logger } from "../base";
5
+ import type { GcpLocation, TaskConfig, TasksClient } from "./types";
6
+ export declare class CommonTasksClient implements TasksClient {
7
+ protected readonly client: CloudTasksClient;
8
+ protected readonly location: GcpLocation;
9
+ protected readonly queueName: string;
10
+ protected readonly logger: Logger;
11
+ protected serviceAccountEmailPromise: Promise<string | null>;
12
+ protected projectIdPromise: Promise<string>;
13
+ constructor(client: CloudTasksClient, location: GcpLocation, queueName: string, logger: Logger);
14
+ upsertQueue(options?: Omit<google.cloud.tasks.v2.IQueue, "name">): Promise<Err<import("./types").TasksError> | import("ts-results-es").OkImpl<void>>;
15
+ listTasks(): Promise<Err<import("./types").TasksError> | Ok<google.cloud.tasks.v2.ITask[]>>;
16
+ createTask(taskName: string, task: TaskConfig): Promise<Err<import("./types").TasksError> | Ok<string>>;
17
+ getTask(name: string): Promise<Err<import("./types").TasksError> | Ok<google.cloud.tasks.v2.ITask> | Ok<null>>;
18
+ }
19
+ //# sourceMappingURL=CommonTasksClient.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CommonTasksClient.d.ts","sourceRoot":"","sources":["../../src/tasksClients/CommonTasksClient.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAC5D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,kCAAkC,CAAC;AAG/D,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,MAAM,eAAe,CAAC;AAExC,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACtC,OAAO,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAGpE,qBAAa,iBAAkB,YAAW,WAAW;IAKjD,SAAS,CAAC,QAAQ,CAAC,MAAM,EAAE,gBAAgB;IAC3C,SAAS,CAAC,QAAQ,CAAC,QAAQ,EAAE,WAAW;IACxC,SAAS,CAAC,QAAQ,CAAC,SAAS,EAAE,MAAM;IACpC,SAAS,CAAC,QAAQ,CAAC,MAAM,EAAE,MAAM;IAPnC,SAAS,CAAC,0BAA0B,EAAE,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;IAC7D,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"}
@@ -0,0 +1 @@
1
+ var k=Object.defineProperty;var a=(o,e)=>k(o,"name",{value:e,configurable:!0});import{GoogleAuth as d}from"google-auth-library";import{Status as m}from"google-gax";import{Err as p,Ok as c}from"ts-results-es";function l(o){return o instanceof Error&&"code"in o&&Number.isInteger(o.code)}a(l,"isRpcError");function u(o){return l(o)?{...o,type:"rpc"}:{type:"unknown",error:o}}a(u,"toTasksError");function g(o){let e=new Uint8Array(3);crypto.getRandomValues(e);let t=Buffer.from(e).toString("hex");return`${o}-${Date.now()}-${t}`}a(g,"createUniqueName");var h=class{constructor(e,t,r,i){this.client=e;this.location=t;this.queueName=r;this.logger=i;this.projectIdPromise=this.client.getProjectId(),this.serviceAccountEmailPromise=new d().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),i=this.client.locationPath(t,this.location);this.logger.info({queuePath:r,parentPath:i},"upserting tasks queue");let s="create";try{await this.client.getQueue({name:r}),s="update"}catch(n){if(l(n)&&n.code===m.NOT_FOUND)s="create";else throw n}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:i,queue:{name:r,...e}})),c.EMPTY}catch(t){return p(u(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 c(r)}catch(e){return p(u(e))}}async createTask(e,t){try{let r=await this.projectIdPromise,i=this.client.queuePath(r,this.location,this.queueName),s=this.client.taskPath(r,this.location,this.queueName,g(e)),n=await this.serviceAccountEmailPromise;return this.logger.info({queuePath:i,taskPath:s,url:t?.httpRequest?.url,payload:t?.httpRequest?.body,serviceAccountEmail:n},"scheduling task"),await this.client.createTask({parent:i,task:{...t,name:s,httpRequest:{...t?.httpRequest,oidcToken:{serviceAccountEmail:n}}}}),c(s)}catch(r){return p(u(r))}}async getTask(e){try{let[t]=await this.client.getTask({name:e});return c(t)}catch(t){return l(t)&&t.code===m.NOT_FOUND?c(null):p(u(t))}}};export{h as CommonTasksClient};
@@ -0,0 +1 @@
1
+ "use strict";var m=Object.defineProperty;var q=Object.getOwnPropertyDescriptor;var E=Object.getOwnPropertyNames;var w=Object.prototype.hasOwnProperty;var n=(o,e)=>m(o,"name",{value:e,configurable:!0});var I=(o,e)=>{for(var t in e)m(o,t,{get:e[t],enumerable:!0})},R=(o,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of E(e))!w.call(o,s)&&s!==t&&m(o,s,{get:()=>e[s],enumerable:!(r=q(e,s))||r.enumerable});return o};var C=o=>R(m({},"__esModule",{value:!0}),o);var N={};I(N,{EmulatorTasksClient:()=>d});module.exports=C(N);var y=require("@google-cloud/tasks"),T=require("@grpc/grpc-js"),P=require("google-gax"),p=require("ts-results-es");var f=require("google-auth-library"),h=require("google-gax"),a=require("ts-results-es");function l(o){return o instanceof Error&&"code"in o&&Number.isInteger(o.code)}n(l,"isRpcError");function u(o){return l(o)?{...o,type:"rpc"}:{type:"unknown",error:o}}n(u,"toTasksError");function k(o){let e=new Uint8Array(3);crypto.getRandomValues(e);let t=Buffer.from(e).toString("hex");return`${o}-${Date.now()}-${t}`}n(k,"createUniqueName");var g=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 f.GoogleAuth().getCredentials().then(i=>i.client_email??null)}static{n(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 i="create";try{await this.client.getQueue({name:r}),i="update"}catch(c){if(l(c)&&c.code===h.Status.NOT_FOUND)i="create";else throw c}return i==="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}})),a.Ok.EMPTY}catch(t){return(0,a.Err)(u(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,a.Ok)(r)}catch(e){return(0,a.Err)(u(e))}}async createTask(e,t){try{let r=await this.projectIdPromise,s=this.client.queuePath(r,this.location,this.queueName),i=this.client.taskPath(r,this.location,this.queueName,k(e)),c=await this.serviceAccountEmailPromise;return this.logger.info({queuePath:s,taskPath:i,url:t?.httpRequest?.url,payload:t?.httpRequest?.body,serviceAccountEmail:c},"scheduling task"),await this.client.createTask({parent:s,task:{...t,name:i,httpRequest:{...t?.httpRequest,oidcToken:{serviceAccountEmail:c}}}}),(0,a.Ok)(i)}catch(r){return(0,a.Err)(u(r))}}async getTask(e){try{let[t]=await this.client.getTask({name:e});return(0,a.Ok)(t)}catch(t){return l(t)&&t.code===h.Status.NOT_FOUND?(0,a.Ok)(null):(0,a.Err)(u(t))}}};var d=class extends g{static{n(this,"EmulatorTasksClient")}constructor(e,t,r,s){super(new y.CloudTasksClient({sslCreds:T.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}}),p.Ok.EMPTY}catch(i){return l(i)&&i.code!==P.Status.ALREADY_EXISTS?(0,p.Err)(u(i)):(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"),p.Ok.EMPTY)}}catch(t){return(0,p.Err)(u(t))}}async createTask(e,t){return super.createTask(e,{...t,httpRequest:{...t?.httpRequest,url:t?.httpRequest?.url?.replace("//localhost","//host.containers.internal")}})}};
@@ -0,0 +1,11 @@
1
+ import type { google } from "@google-cloud/tasks/build/protos";
2
+ import { Err, Ok } from "ts-results-es";
3
+ import type { Logger } from "../base";
4
+ import { CommonTasksClient } from "./CommonTasksClient";
5
+ import type { EmulatorConfig, GcpLocation, TaskConfig } from "./types";
6
+ export declare class EmulatorTasksClient extends CommonTasksClient {
7
+ constructor(location: GcpLocation, queueName: string, logger: Logger, config: EmulatorConfig);
8
+ upsertQueue(options?: Omit<google.cloud.tasks.v2.IQueue, "name">): Promise<Err<import("./types").TasksError> | import("ts-results-es").OkImpl<void>>;
9
+ createTask(taskName: string, task: TaskConfig): Promise<Err<import("./types").TasksError> | Ok<string>>;
10
+ }
11
+ //# sourceMappingURL=EmulatorTasksClient.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"EmulatorTasksClient.d.ts","sourceRoot":"","sources":["../../src/tasksClients/EmulatorTasksClient.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,kCAAkC,CAAC;AAG/D,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,MAAM,eAAe,CAAC;AAExC,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACtC,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,KAAK,EAAE,cAAc,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAGvE,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"}
@@ -0,0 +1 @@
1
+ var y=Object.defineProperty;var a=(o,e)=>y(o,"name",{value:e,configurable:!0});import{CloudTasksClient as P}from"@google-cloud/tasks";import{credentials as q}from"@grpc/grpc-js";import{Status as E}from"google-gax";import{Err as d,Ok as k}from"ts-results-es";import{GoogleAuth as T}from"google-auth-library";import{Status as h}from"google-gax";import{Err as p,Ok as l}from"ts-results-es";function c(o){return o instanceof Error&&"code"in o&&Number.isInteger(o.code)}a(c,"isRpcError");function n(o){return c(o)?{...o,type:"rpc"}:{type:"unknown",error:o}}a(n,"toTasksError");function g(o){let e=new Uint8Array(3);crypto.getRandomValues(e);let t=Buffer.from(e).toString("hex");return`${o}-${Date.now()}-${t}`}a(g,"createUniqueName");var m=class{constructor(e,t,r,i){this.client=e;this.location=t;this.queueName=r;this.logger=i;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),i=this.client.locationPath(t,this.location);this.logger.info({queuePath:r,parentPath:i},"upserting tasks queue");let s="create";try{await this.client.getQueue({name:r}),s="update"}catch(u){if(c(u)&&u.code===h.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:i,queue:{name:r,...e}})),l.EMPTY}catch(t){return p(n(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 l(r)}catch(e){return p(n(e))}}async createTask(e,t){try{let r=await this.projectIdPromise,i=this.client.queuePath(r,this.location,this.queueName),s=this.client.taskPath(r,this.location,this.queueName,g(e)),u=await this.serviceAccountEmailPromise;return this.logger.info({queuePath:i,taskPath:s,url:t?.httpRequest?.url,payload:t?.httpRequest?.body,serviceAccountEmail:u},"scheduling task"),await this.client.createTask({parent:i,task:{...t,name:s,httpRequest:{...t?.httpRequest,oidcToken:{serviceAccountEmail:u}}}}),l(s)}catch(r){return p(n(r))}}async getTask(e){try{let[t]=await this.client.getTask({name:e});return l(t)}catch(t){return c(t)&&t.code===h.NOT_FOUND?l(null):p(n(t))}}};var f=class extends m{static{a(this,"EmulatorTasksClient")}constructor(e,t,r,i){super(new P({sslCreds:q.createInsecure(),servicePath:i.host??"localhost",port:i.port}),e,t,r)}async upsertQueue(e){try{let t=await this.projectIdPromise,r=this.client.queuePath(t,this.location,this.queueName),i=this.client.locationPath(t,this.location);this.logger.info({queuePath:r,parentPath:i},"upserting simulator tasks queue");try{return await this.client.createQueue({parent:i,queue:{name:r,...e}}),k.EMPTY}catch(s){return c(s)&&s.code!==E.ALREADY_EXISTS?d(n(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"),k.EMPTY)}}catch(t){return d(n(t))}}async createTask(e,t){return super.createTask(e,{...t,httpRequest:{...t?.httpRequest,url:t?.httpRequest?.url?.replace("//localhost","//host.containers.internal")}})}};export{f as EmulatorTasksClient};
@@ -0,0 +1 @@
1
+ "use strict";var l=Object.defineProperty;var f=Object.getOwnPropertyDescriptor;var T=Object.getOwnPropertyNames;var P=Object.prototype.hasOwnProperty;var a=(r,t)=>l(r,"name",{value:t,configurable:!0});var w=(r,t)=>{for(var e in t)l(r,e,{get:t[e],enumerable:!0})},E=(r,t,e,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of T(t))!P.call(r,s)&&s!==e&&l(r,s,{get:()=>t[s],enumerable:!(o=f(t,s))||o.enumerable});return r};var q=r=>E(l({},"__esModule",{value:!0}),r);var I={};w(I,{RegularTasksClient:()=>h});module.exports=q(I);var y=require("@google-cloud/tasks");var d=require("google-auth-library"),g=require("google-gax"),i=require("ts-results-es");function p(r){return r instanceof Error&&"code"in r&&Number.isInteger(r.code)}a(p,"isRpcError");function u(r){return p(r)?{...r,type:"rpc"}:{type:"unknown",error:r}}a(u,"toTasksError");function k(r){let t=new Uint8Array(3);crypto.getRandomValues(t);let e=Buffer.from(t).toString("hex");return`${r}-${Date.now()}-${e}`}a(k,"createUniqueName");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 d.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(c){if(p(c)&&c.code===g.Status.NOT_FOUND)n="create";else throw c}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)(u(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)(u(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,k(t)),c=await this.serviceAccountEmailPromise;return this.logger.info({queuePath:s,taskPath:n,url:e?.httpRequest?.url,payload:e?.httpRequest?.body,serviceAccountEmail:c},"scheduling task"),await this.client.createTask({parent:s,task:{...e,name:n,httpRequest:{...e?.httpRequest,oidcToken:{serviceAccountEmail:c}}}}),(0,i.Ok)(n)}catch(o){return(0,i.Err)(u(o))}}async getTask(t){try{let[e]=await this.client.getTask({name:t});return(0,i.Ok)(e)}catch(e){return p(e)&&e.code===g.Status.NOT_FOUND?(0,i.Ok)(null):(0,i.Err)(u(e))}}};var h=class extends m{static{a(this,"RegularTasksClient")}constructor(t,e,o){super(new y.CloudTasksClient({}),t,e,o)}};
@@ -0,0 +1,7 @@
1
+ import type { Logger } from "../base";
2
+ import { CommonTasksClient } from "./CommonTasksClient";
3
+ import type { GcpLocation } from "./types";
4
+ export declare class RegularTasksClient extends CommonTasksClient {
5
+ constructor(location: GcpLocation, queueName: string, logger: Logger);
6
+ }
7
+ //# sourceMappingURL=RegularTasksClient.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"RegularTasksClient.d.ts","sourceRoot":"","sources":["../../src/tasksClients/RegularTasksClient.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACtC,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAE3C,qBAAa,kBAAmB,SAAQ,iBAAiB;gBAC3C,QAAQ,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM;CAGrE"}
@@ -0,0 +1 @@
1
+ var d=Object.defineProperty;var i=(o,e)=>d(o,"name",{value:e,configurable:!0});import{CloudTasksClient as f}from"@google-cloud/tasks";import{GoogleAuth as y}from"google-auth-library";import{Status as h}from"google-gax";import{Err as p,Ok as u}from"ts-results-es";function l(o){return o instanceof Error&&"code"in o&&Number.isInteger(o.code)}i(l,"isRpcError");function c(o){return l(o)?{...o,type:"rpc"}:{type:"unknown",error:o}}i(c,"toTasksError");function g(o){let e=new Uint8Array(3);crypto.getRandomValues(e);let t=Buffer.from(e).toString("hex");return`${o}-${Date.now()}-${t}`}i(g,"createUniqueName");var m=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 y().getCredentials().then(s=>s.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),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(a){if(l(a)&&a.code===h.NOT_FOUND)s="create";else throw a}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}})),u.EMPTY}catch(t){return p(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 u(r)}catch(e){return p(c(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,g(e)),a=await this.serviceAccountEmailPromise;return this.logger.info({queuePath:n,taskPath:s,url:t?.httpRequest?.url,payload:t?.httpRequest?.body,serviceAccountEmail:a},"scheduling task"),await this.client.createTask({parent:n,task:{...t,name:s,httpRequest:{...t?.httpRequest,oidcToken:{serviceAccountEmail:a}}}}),u(s)}catch(r){return p(c(r))}}async getTask(e){try{let[t]=await this.client.getTask({name:e});return u(t)}catch(t){return l(t)&&t.code===h.NOT_FOUND?u(null):p(c(t))}}};var k=class extends m{static{i(this,"RegularTasksClient")}constructor(e,t,r){super(new f({}),e,t,r)}};export{k as RegularTasksClient};
@@ -0,0 +1 @@
1
+ "use strict";var g=Object.defineProperty;var w=Object.getOwnPropertyDescriptor;var C=Object.getOwnPropertyNames;var I=Object.prototype.hasOwnProperty;var n=(o,e)=>g(o,"name",{value:e,configurable:!0});var R=(o,e)=>{for(var t in e)g(o,t,{get:e[t],enumerable:!0})},x=(o,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of C(e))!I.call(o,s)&&s!==t&&g(o,s,{get:()=>e[s],enumerable:!(r=w(e,s))||r.enumerable});return o};var N=o=>x(g({},"__esModule",{value:!0}),o);var v={};R(v,{EmulatorTasksClient:()=>h,RegularTasksClient:()=>d});module.exports=N(v);var T=require("@google-cloud/tasks"),P=require("@grpc/grpc-js"),E=require("google-gax"),m=require("ts-results-es");var y=require("google-auth-library"),k=require("google-gax"),a=require("ts-results-es");function p(o){return o instanceof Error&&"code"in o&&Number.isInteger(o.code)}n(p,"isRpcError");function u(o){return p(o)?{...o,type:"rpc"}:{type:"unknown",error:o}}n(u,"toTasksError");function f(o){let e=new Uint8Array(3);crypto.getRandomValues(e);let t=Buffer.from(e).toString("hex");return`${o}-${Date.now()}-${t}`}n(f,"createUniqueName");var l=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 y.GoogleAuth().getCredentials().then(i=>i.client_email??null)}static{n(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 i="create";try{await this.client.getQueue({name:r}),i="update"}catch(c){if(p(c)&&c.code===k.Status.NOT_FOUND)i="create";else throw c}return i==="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}})),a.Ok.EMPTY}catch(t){return(0,a.Err)(u(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,a.Ok)(r)}catch(e){return(0,a.Err)(u(e))}}async createTask(e,t){try{let r=await this.projectIdPromise,s=this.client.queuePath(r,this.location,this.queueName),i=this.client.taskPath(r,this.location,this.queueName,f(e)),c=await this.serviceAccountEmailPromise;return this.logger.info({queuePath:s,taskPath:i,url:t?.httpRequest?.url,payload:t?.httpRequest?.body,serviceAccountEmail:c},"scheduling task"),await this.client.createTask({parent:s,task:{...t,name:i,httpRequest:{...t?.httpRequest,oidcToken:{serviceAccountEmail:c}}}}),(0,a.Ok)(i)}catch(r){return(0,a.Err)(u(r))}}async getTask(e){try{let[t]=await this.client.getTask({name:e});return(0,a.Ok)(t)}catch(t){return p(t)&&t.code===k.Status.NOT_FOUND?(0,a.Ok)(null):(0,a.Err)(u(t))}}};var h=class extends l{static{n(this,"EmulatorTasksClient")}constructor(e,t,r,s){super(new T.CloudTasksClient({sslCreds:P.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}}),m.Ok.EMPTY}catch(i){return p(i)&&i.code!==E.Status.ALREADY_EXISTS?(0,m.Err)(u(i)):(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"),m.Ok.EMPTY)}}catch(t){return(0,m.Err)(u(t))}}async createTask(e,t){return super.createTask(e,{...t,httpRequest:{...t?.httpRequest,url:t?.httpRequest?.url?.replace("//localhost","//host.containers.internal")}})}};var q=require("@google-cloud/tasks");var d=class extends l{static{n(this,"RegularTasksClient")}constructor(e,t,r){super(new q.CloudTasksClient({}),e,t,r)}};
@@ -0,0 +1,4 @@
1
+ export { EmulatorTasksClient } from "./EmulatorTasksClient";
2
+ export { RegularTasksClient } from "./RegularTasksClient";
3
+ export type { EmulatorConfig, GcpLocation, TaskConfig, TasksClient, TasksError, } from "./types";
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/tasksClients/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC1D,YAAY,EACV,cAAc,EACd,WAAW,EACX,UAAU,EACV,WAAW,EACX,UAAU,GACX,MAAM,SAAS,CAAC"}
@@ -0,0 +1 @@
1
+ var T=Object.defineProperty;var a=(o,e)=>T(o,"name",{value:e,configurable:!0});import{CloudTasksClient as E}from"@google-cloud/tasks";import{credentials as q}from"@grpc/grpc-js";import{Status as w}from"google-gax";import{Err as f,Ok as y}from"ts-results-es";import{GoogleAuth as P}from"google-auth-library";import{Status as k}from"google-gax";import{Err as m,Ok as l}from"ts-results-es";function c(o){return o instanceof Error&&"code"in o&&Number.isInteger(o.code)}a(c,"isRpcError");function n(o){return c(o)?{...o,type:"rpc"}:{type:"unknown",error:o}}a(n,"toTasksError");function d(o){let e=new Uint8Array(3);crypto.getRandomValues(e);let t=Buffer.from(e).toString("hex");return`${o}-${Date.now()}-${t}`}a(d,"createUniqueName");var p=class{constructor(e,t,r,i){this.client=e;this.location=t;this.queueName=r;this.logger=i;this.projectIdPromise=this.client.getProjectId(),this.serviceAccountEmailPromise=new P().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),i=this.client.locationPath(t,this.location);this.logger.info({queuePath:r,parentPath:i},"upserting tasks queue");let s="create";try{await this.client.getQueue({name:r}),s="update"}catch(u){if(c(u)&&u.code===k.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:i,queue:{name:r,...e}})),l.EMPTY}catch(t){return m(n(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 l(r)}catch(e){return m(n(e))}}async createTask(e,t){try{let r=await this.projectIdPromise,i=this.client.queuePath(r,this.location,this.queueName),s=this.client.taskPath(r,this.location,this.queueName,d(e)),u=await this.serviceAccountEmailPromise;return this.logger.info({queuePath:i,taskPath:s,url:t?.httpRequest?.url,payload:t?.httpRequest?.body,serviceAccountEmail:u},"scheduling task"),await this.client.createTask({parent:i,task:{...t,name:s,httpRequest:{...t?.httpRequest,oidcToken:{serviceAccountEmail:u}}}}),l(s)}catch(r){return m(n(r))}}async getTask(e){try{let[t]=await this.client.getTask({name:e});return l(t)}catch(t){return c(t)&&t.code===k.NOT_FOUND?l(null):m(n(t))}}};var g=class extends p{static{a(this,"EmulatorTasksClient")}constructor(e,t,r,i){super(new E({sslCreds:q.createInsecure(),servicePath:i.host??"localhost",port:i.port}),e,t,r)}async upsertQueue(e){try{let t=await this.projectIdPromise,r=this.client.queuePath(t,this.location,this.queueName),i=this.client.locationPath(t,this.location);this.logger.info({queuePath:r,parentPath:i},"upserting simulator tasks queue");try{return await this.client.createQueue({parent:i,queue:{name:r,...e}}),y.EMPTY}catch(s){return c(s)&&s.code!==w.ALREADY_EXISTS?f(n(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"),y.EMPTY)}}catch(t){return f(n(t))}}async createTask(e,t){return super.createTask(e,{...t,httpRequest:{...t?.httpRequest,url:t?.httpRequest?.url?.replace("//localhost","//host.containers.internal")}})}};import{CloudTasksClient as C}from"@google-cloud/tasks";var h=class extends p{static{a(this,"RegularTasksClient")}constructor(e,t,r){super(new C({}),e,t,r)}};export{g as EmulatorTasksClient,h as RegularTasksClient};
@@ -0,0 +1 @@
1
+ "use strict";var s=Object.defineProperty;var i=Object.getOwnPropertyDescriptor;var p=Object.getOwnPropertyNames;var k=Object.prototype.hasOwnProperty;var n=(r,o)=>s(r,"name",{value:o,configurable:!0});var g=(r,o)=>{for(var t in o)s(r,t,{get:o[t],enumerable:!0})},l=(r,o,t,a)=>{if(o&&typeof o=="object"||typeof o=="function")for(let e of p(o))!k.call(r,e)&&e!==t&&s(r,e,{get:()=>o[e],enumerable:!(a=i(o,e))||a.enumerable});return r};var c=r=>l(s({},"__esModule",{value:!0}),r);var E={};g(E,{createUniqueName:()=>y,isRpcError:()=>u,toTasksError:()=>m});module.exports=c(E);function u(r){return r instanceof Error&&"code"in r&&Number.isInteger(r.code)}n(u,"isRpcError");function m(r){return u(r)?{...r,type:"rpc"}:{type:"unknown",error:r}}n(m,"toTasksError");function y(r){let o=new Uint8Array(3);crypto.getRandomValues(o);let t=Buffer.from(o).toString("hex");return`${r}-${Date.now()}-${t}`}n(y,"createUniqueName");
@@ -0,0 +1,33 @@
1
+ import type { google } from "@google-cloud/tasks/build/protos";
2
+ import type { Status } from "google-gax";
3
+ import type { Result } from "ts-results-es";
4
+ export type GcpLocation = "europe-west6" | (string & NonNullable<unknown>);
5
+ type RpcError = {
6
+ type: "rpc";
7
+ code: Status;
8
+ } & Error;
9
+ type UnknownError = {
10
+ type: "unknown";
11
+ error: unknown;
12
+ };
13
+ export type TasksError = RpcError | UnknownError;
14
+ export type TaskConfig = Omit<google.cloud.tasks.v2.ITask, "name">;
15
+ export interface TasksClient {
16
+ upsertQueue(options?: Omit<google.cloud.tasks.v2.IQueue, "name">): Promise<Result<void, TasksError>>;
17
+ getTask(taskPath: string): Promise<Result<google.cloud.tasks.v2.ITask | null, TasksError>>;
18
+ listTasks(): Promise<Result<google.cloud.tasks.v2.ITask[], TasksError>>;
19
+ createTask(taskName: string, task: TaskConfig): Promise<Result<string, TasksError>>;
20
+ }
21
+ export type EmulatorConfig = {
22
+ /** default: "localhost" */
23
+ host?: string;
24
+ port?: number;
25
+ };
26
+ export declare function isRpcError(e: unknown): e is RpcError;
27
+ export declare function toTasksError(e: unknown): TasksError;
28
+ /**
29
+ * Create a unique name in a format `{baseName}-{timestamp}-{sixRandomHexChars}`
30
+ */
31
+ export declare function createUniqueName(baseName: string): string;
32
+ export {};
33
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/tasksClients/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,kCAAkC,CAAC;AAC/D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AACzC,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAE5C,MAAM,MAAM,WAAW,GAAG,cAAc,GAAG,CAAC,MAAM,GAAG,WAAW,CAAC,OAAO,CAAC,CAAC,CAAC;AAE3E,KAAK,QAAQ,GAAG;IACd,IAAI,EAAE,KAAK,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;CACd,GAAG,KAAK,CAAC;AAEV,KAAK,YAAY,GAAG;IAClB,IAAI,EAAE,SAAS,CAAC;IAChB,KAAK,EAAE,OAAO,CAAC;CAChB,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG,QAAQ,GAAG,YAAY,CAAC;AAEjD,MAAM,MAAM,UAAU,GAAG,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;AAEnE,MAAM,WAAW,WAAW;IAC1B,WAAW,CACT,OAAO,CAAC,EAAE,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,GACnD,OAAO,CAAC,MAAM,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC;IAErC,OAAO,CACL,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,GAAG,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC;IAEnE,SAAS,IAAI,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,EAAE,EAAE,UAAU,CAAC,CAAC,CAAC;IAExE,UAAU,CACR,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,UAAU,GACf,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC,CAAC;CACxC;AAED,MAAM,MAAM,cAAc,GAAG;IAC3B,2BAA2B;IAC3B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,CAAC;CACf,CAAC;AAEF,wBAAgB,UAAU,CAAC,CAAC,EAAE,OAAO,GAAG,CAAC,IAAI,QAAQ,CAEpD;AAED,wBAAgB,YAAY,CAAC,CAAC,EAAE,OAAO,GAAG,UAAU,CAMnD;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,MAAM,UAKhD"}
@@ -0,0 +1 @@
1
+ var s=Object.defineProperty;var t=(r,o)=>s(r,"name",{value:o,configurable:!0});function n(r){return r instanceof Error&&"code"in r&&Number.isInteger(r.code)}t(n,"isRpcError");function u(r){return n(r)?{...r,type:"rpc"}:{type:"unknown",error:r}}t(u,"toTasksError");function i(r){let o=new Uint8Array(3);crypto.getRandomValues(o);let e=Buffer.from(o).toString("hex");return`${r}-${Date.now()}-${e}`}t(i,"createUniqueName");export{i as createUniqueName,n as isRpcError,u as toTasksError};
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "type": "git",
5
5
  "url": "https://git.panter.ch/open-source/cloud-tasks.git"
6
6
  },
7
- "version": "2.0.2",
7
+ "version": "3.1.0",
8
8
  "sideEffects": false,
9
9
  "exports": {
10
10
  ".": {
@@ -27,6 +27,16 @@
27
27
  "default": "./dist/client.cjs"
28
28
  }
29
29
  },
30
+ "./base": {
31
+ "import": {
32
+ "types": "./dist/base.d.ts",
33
+ "default": "./dist/base.mjs"
34
+ },
35
+ "require": {
36
+ "types": "./dist/base.d.ts",
37
+ "default": "./dist/base.cjs"
38
+ }
39
+ },
30
40
  "./server": {
31
41
  "import": {
32
42
  "types": "./dist/server.d.ts",
@@ -1 +0,0 @@
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}-${Date.now()}-${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");
@@ -1,51 +0,0 @@
1
- import { CloudTasksClient } from "@google-cloud/tasks";
2
- import type { google } from "@google-cloud/tasks/build/protos";
3
- import { Status } from "google-gax";
4
- import type { Result } from "ts-results-es";
5
- import { Err, Ok } from "ts-results-es";
6
- import type { Logger } from "./client";
7
- export type GcpLocation = "europe-west6" | (string & NonNullable<unknown>);
8
- type RpcError = {
9
- type: "rpc";
10
- code: Status;
11
- } & Error;
12
- type UnknownError = {
13
- type: "unknown";
14
- error: unknown;
15
- };
16
- export type TasksError = RpcError | UnknownError;
17
- export type TaskConfig = Omit<google.cloud.tasks.v2.ITask, "name">;
18
- export interface TasksClient {
19
- upsertQueue(options?: Omit<google.cloud.tasks.v2.IQueue, "name">): Promise<Result<void, TasksError>>;
20
- getTask(taskPath: string): Promise<Result<google.cloud.tasks.v2.ITask | null, TasksError>>;
21
- listTasks(): Promise<Result<google.cloud.tasks.v2.ITask[], TasksError>>;
22
- createTask(taskName: string, task: TaskConfig): Promise<Result<string, TasksError>>;
23
- }
24
- declare class CommonTasksClient implements TasksClient {
25
- protected readonly client: CloudTasksClient;
26
- protected readonly location: GcpLocation;
27
- protected readonly queueName: string;
28
- protected readonly logger: Logger;
29
- protected serviceAccountEmailPromise: Promise<string | null>;
30
- protected projectIdPromise: Promise<string>;
31
- constructor(client: CloudTasksClient, location: GcpLocation, queueName: string, logger: Logger);
32
- upsertQueue(options?: Omit<google.cloud.tasks.v2.IQueue, "name">): Promise<Err<TasksError> | import("ts-results-es").OkImpl<void>>;
33
- listTasks(): Promise<Err<TasksError> | Ok<google.cloud.tasks.v2.ITask[]>>;
34
- createTask(taskName: string, task: TaskConfig): Promise<Err<TasksError> | Ok<string>>;
35
- getTask(name: string): Promise<Err<TasksError> | Ok<google.cloud.tasks.v2.ITask> | Ok<null>>;
36
- }
37
- export declare class RegularTasksClient extends CommonTasksClient {
38
- constructor(location: GcpLocation, queueName: string, logger: Logger);
39
- }
40
- export type EmulatorConfig = {
41
- /** default: "localhost" */
42
- host?: string;
43
- port?: number;
44
- };
45
- export declare class EmulatorTasksClient extends CommonTasksClient {
46
- constructor(location: GcpLocation, queueName: string, logger: Logger, config: EmulatorConfig);
47
- upsertQueue(options?: Omit<google.cloud.tasks.v2.IQueue, "name">): Promise<Err<TasksError> | import("ts-results-es").OkImpl<void>>;
48
- createTask(taskName: string, task: TaskConfig): Promise<Err<TasksError> | Ok<string>>;
49
- }
50
- export {};
51
- //# sourceMappingURL=tasksClients.d.ts.map
@@ -1 +0,0 @@
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,EAAE,KAAK,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;CACd,GAAG,KAAK,CAAC;AAEV,KAAK,YAAY,GAAG;IAClB,IAAI,EAAE,SAAS,CAAC;IAChB,KAAK,EAAE,OAAO,CAAC;CAChB,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG,QAAQ,GAAG,YAAY,CAAC;AAEjD,MAAM,MAAM,UAAU,GAAG,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;AAEnE,MAAM,WAAW,WAAW;IAC1B,WAAW,CACT,OAAO,CAAC,EAAE,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,GACnD,OAAO,CAAC,MAAM,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC;IAErC,OAAO,CACL,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,GAAG,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC;IAEnE,SAAS,IAAI,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,EAAE,EAAE,UAAU,CAAC,CAAC,CAAC;IAExE,UAAU,CACR,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,UAAU,GACf,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC,CAAC;CACxC;AAED,cAAM,iBAAkB,YAAW,WAAW;IAK1C,SAAS,CAAC,QAAQ,CAAC,MAAM,EAAE,gBAAgB;IAC3C,SAAS,CAAC,QAAQ,CAAC,QAAQ,EAAE,WAAW;IACxC,SAAS,CAAC,QAAQ,CAAC,SAAS,EAAE,MAAM;IACpC,SAAS,CAAC,QAAQ,CAAC,MAAM,EAAE,MAAM;IAPnC,SAAS,CAAC,0BAA0B,EAAE,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;IAC7D,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"}
@@ -1 +0,0 @@
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}-${Date.now()}-${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};