opticedge-cloud-utils 1.1.1 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/auth.d.ts +7 -0
- package/dist/auth.js +23 -0
- package/dist/db/mongo.d.ts +3 -0
- package/dist/db/mongo.js +27 -0
- package/dist/db/mongo2.d.ts +4 -0
- package/dist/db/mongo2.js +30 -0
- package/dist/db/mongo3.d.ts +6 -0
- package/dist/db/mongo3.js +39 -0
- package/dist/env.d.ts +1 -0
- package/dist/env.js +12 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +26 -0
- package/dist/regex.d.ts +1 -0
- package/dist/regex.js +6 -0
- package/dist/secrets.d.ts +8 -0
- package/dist/secrets.js +22 -0
- package/dist/task.d.ts +1 -0
- package/dist/task.js +38 -0
- package/dist/tw/utils.d.ts +1 -0
- package/dist/tw/utils.js +19 -0
- package/dist/tw/wallet.d.ts +1 -0
- package/dist/tw/wallet.js +32 -0
- package/package.json +3 -43
- package/src/index.ts +10 -0
package/dist/auth.d.ts
ADDED
package/dist/auth.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.verifyRequest = verifyRequest;
|
|
4
|
+
const google_auth_library_1 = require("google-auth-library");
|
|
5
|
+
async function verifyRequest(req, options) {
|
|
6
|
+
const authHeader = req.headers['authorization'];
|
|
7
|
+
if (!authHeader || !authHeader.startsWith('Bearer ')) {
|
|
8
|
+
return false;
|
|
9
|
+
}
|
|
10
|
+
const token = authHeader.split(' ')[1];
|
|
11
|
+
const client = new google_auth_library_1.OAuth2Client();
|
|
12
|
+
try {
|
|
13
|
+
const ticket = await client.verifyIdToken({
|
|
14
|
+
idToken: token,
|
|
15
|
+
audience: options.allowedAudience
|
|
16
|
+
});
|
|
17
|
+
const payload = ticket.getPayload();
|
|
18
|
+
return payload?.email === options.allowedServiceAccount;
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
}
|
package/dist/db/mongo.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.connectToMongo = connectToMongo;
|
|
4
|
+
exports.getCollection = getCollection;
|
|
5
|
+
const mongodb_1 = require("mongodb");
|
|
6
|
+
const secrets_1 = require("../secrets");
|
|
7
|
+
let client;
|
|
8
|
+
let db;
|
|
9
|
+
let connectPromise;
|
|
10
|
+
async function connectToMongo(projectId, uriSecret, dbName) {
|
|
11
|
+
if (db)
|
|
12
|
+
return; // already connected
|
|
13
|
+
if (!connectPromise) {
|
|
14
|
+
connectPromise = (async () => {
|
|
15
|
+
const uri = await (0, secrets_1.getSecret)(projectId, uriSecret);
|
|
16
|
+
client = new mongodb_1.MongoClient(uri);
|
|
17
|
+
await client.connect();
|
|
18
|
+
db = client.db(dbName);
|
|
19
|
+
})();
|
|
20
|
+
}
|
|
21
|
+
await connectPromise;
|
|
22
|
+
}
|
|
23
|
+
function getCollection(name) {
|
|
24
|
+
if (!db)
|
|
25
|
+
throw new Error('Mongo not initialized');
|
|
26
|
+
return db.collection(name);
|
|
27
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { Db, Collection, Document } from 'mongodb';
|
|
2
|
+
export declare function connectToMongo2(projectId: string, uriSecret: string): Promise<void>;
|
|
3
|
+
export declare function getDb2(dbName: string): Db;
|
|
4
|
+
export declare function getCollection2<T extends Document = Document>(dbName: string, collectionName: string): Collection<T>;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.connectToMongo2 = connectToMongo2;
|
|
4
|
+
exports.getDb2 = getDb2;
|
|
5
|
+
exports.getCollection2 = getCollection2;
|
|
6
|
+
// Multi DB safe util
|
|
7
|
+
const mongodb_1 = require("mongodb");
|
|
8
|
+
const secrets_1 = require("../secrets");
|
|
9
|
+
let client;
|
|
10
|
+
let connectPromise;
|
|
11
|
+
async function connectToMongo2(projectId, uriSecret) {
|
|
12
|
+
if (client)
|
|
13
|
+
return; // already connected
|
|
14
|
+
if (!connectPromise) {
|
|
15
|
+
connectPromise = (async () => {
|
|
16
|
+
const uri = await (0, secrets_1.getSecret)(projectId, uriSecret);
|
|
17
|
+
client = new mongodb_1.MongoClient(uri);
|
|
18
|
+
await client.connect();
|
|
19
|
+
})();
|
|
20
|
+
}
|
|
21
|
+
await connectPromise;
|
|
22
|
+
}
|
|
23
|
+
function getDb2(dbName) {
|
|
24
|
+
if (!client)
|
|
25
|
+
throw new Error('Mongo not initialized');
|
|
26
|
+
return client.db(dbName);
|
|
27
|
+
}
|
|
28
|
+
function getCollection2(dbName, collectionName) {
|
|
29
|
+
return getDb2(dbName).collection(collectionName);
|
|
30
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { MongoClient, Db, Collection, Document } from 'mongodb';
|
|
2
|
+
export declare function connectToMongo3(projectId: string, uriSecret: string): Promise<MongoClient>;
|
|
3
|
+
export declare function getDb3(uriSecret: string, dbName: string): Db;
|
|
4
|
+
export declare function getCollection3<T extends Document = Document>(uriSecret: string, dbName: string, collectionName: string): Collection<T>;
|
|
5
|
+
export declare const __mongoClients: Map<string, MongoClient>;
|
|
6
|
+
export declare const __connectPromises: Map<string, Promise<void>>;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.__connectPromises = exports.__mongoClients = void 0;
|
|
4
|
+
exports.connectToMongo3 = connectToMongo3;
|
|
5
|
+
exports.getDb3 = getDb3;
|
|
6
|
+
exports.getCollection3 = getCollection3;
|
|
7
|
+
// Multi cluster safe util
|
|
8
|
+
const mongodb_1 = require("mongodb");
|
|
9
|
+
const secrets_1 = require("../secrets");
|
|
10
|
+
const clients = new Map();
|
|
11
|
+
const connectPromises = new Map();
|
|
12
|
+
async function connectToMongo3(projectId, uriSecret) {
|
|
13
|
+
if (clients.has(uriSecret)) {
|
|
14
|
+
return clients.get(uriSecret);
|
|
15
|
+
}
|
|
16
|
+
if (!connectPromises.has(uriSecret)) {
|
|
17
|
+
const promise = (async () => {
|
|
18
|
+
const uri = await (0, secrets_1.getSecret)(projectId, uriSecret);
|
|
19
|
+
const client = new mongodb_1.MongoClient(uri);
|
|
20
|
+
await client.connect();
|
|
21
|
+
clients.set(uriSecret, client);
|
|
22
|
+
})();
|
|
23
|
+
connectPromises.set(uriSecret, promise);
|
|
24
|
+
}
|
|
25
|
+
await connectPromises.get(uriSecret);
|
|
26
|
+
return clients.get(uriSecret);
|
|
27
|
+
}
|
|
28
|
+
function getDb3(uriSecret, dbName) {
|
|
29
|
+
const client = clients.get(uriSecret);
|
|
30
|
+
if (!client) {
|
|
31
|
+
throw new Error(`Mongo client for secret "${uriSecret}" not initialized`);
|
|
32
|
+
}
|
|
33
|
+
return client.db(dbName);
|
|
34
|
+
}
|
|
35
|
+
function getCollection3(uriSecret, dbName, collectionName) {
|
|
36
|
+
return getDb3(uriSecret, dbName).collection(collectionName);
|
|
37
|
+
}
|
|
38
|
+
exports.__mongoClients = clients;
|
|
39
|
+
exports.__connectPromises = connectPromises;
|
package/dist/env.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getEnv(name: string, fallback?: string): string;
|
package/dist/env.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getEnv = getEnv;
|
|
4
|
+
function getEnv(name, fallback) {
|
|
5
|
+
const value = process.env[name];
|
|
6
|
+
if (!value) {
|
|
7
|
+
if (fallback !== undefined)
|
|
8
|
+
return fallback;
|
|
9
|
+
throw new Error(`Missing env var ${name}`);
|
|
10
|
+
}
|
|
11
|
+
return value;
|
|
12
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from './db/mongo';
|
|
2
|
+
export * from './db/mongo2';
|
|
3
|
+
export * from './db/mongo3';
|
|
4
|
+
export * from './tw/utils';
|
|
5
|
+
export * from './tw/wallet';
|
|
6
|
+
export * from './auth';
|
|
7
|
+
export * from './env';
|
|
8
|
+
export * from './regex';
|
|
9
|
+
export * from './secrets';
|
|
10
|
+
export * from './task';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./db/mongo"), exports);
|
|
18
|
+
__exportStar(require("./db/mongo2"), exports);
|
|
19
|
+
__exportStar(require("./db/mongo3"), exports);
|
|
20
|
+
__exportStar(require("./tw/utils"), exports);
|
|
21
|
+
__exportStar(require("./tw/wallet"), exports);
|
|
22
|
+
__exportStar(require("./auth"), exports);
|
|
23
|
+
__exportStar(require("./env"), exports);
|
|
24
|
+
__exportStar(require("./regex"), exports);
|
|
25
|
+
__exportStar(require("./secrets"), exports);
|
|
26
|
+
__exportStar(require("./task"), exports);
|
package/dist/regex.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function escapeRegex(input: string): string;
|
package/dist/regex.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns the latest value of a Secret Manager secret.
|
|
3
|
+
*
|
|
4
|
+
* @param projectId – GCP project that owns the secret.
|
|
5
|
+
* @param secretName – Secret name (without version).
|
|
6
|
+
* @returns – UTF-8 string value.
|
|
7
|
+
*/
|
|
8
|
+
export declare function getSecret(projectId: string, secretName: string): Promise<string>;
|
package/dist/secrets.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getSecret = getSecret;
|
|
4
|
+
const secret_manager_1 = require("@google-cloud/secret-manager");
|
|
5
|
+
/**
|
|
6
|
+
* Returns the latest value of a Secret Manager secret.
|
|
7
|
+
*
|
|
8
|
+
* @param projectId – GCP project that owns the secret.
|
|
9
|
+
* @param secretName – Secret name (without version).
|
|
10
|
+
* @returns – UTF-8 string value.
|
|
11
|
+
*/
|
|
12
|
+
async function getSecret(projectId, secretName) {
|
|
13
|
+
if (!projectId)
|
|
14
|
+
throw new Error('projectId is required');
|
|
15
|
+
if (!secretName)
|
|
16
|
+
throw new Error('secretName is required');
|
|
17
|
+
const secretClient = new secret_manager_1.SecretManagerServiceClient();
|
|
18
|
+
const [version] = await secretClient.accessSecretVersion({
|
|
19
|
+
name: `projects/${projectId}/secrets/${secretName}/versions/latest`
|
|
20
|
+
});
|
|
21
|
+
return version.payload?.data?.toString('utf-8') ?? '';
|
|
22
|
+
}
|
package/dist/task.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function createTask(projectId: string, region: string, queueId: string, data: unknown, serviceAccount: string, audience: string, delaySeconds?: number): Promise<string>;
|
package/dist/task.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createTask = createTask;
|
|
4
|
+
const tasks_1 = require("@google-cloud/tasks");
|
|
5
|
+
const tasksClient = new tasks_1.CloudTasksClient();
|
|
6
|
+
async function createTask(projectId, region, queueId, data, serviceAccount, audience, delaySeconds = 0) {
|
|
7
|
+
if (!projectId || !region || !queueId || !serviceAccount || !audience) {
|
|
8
|
+
throw new Error('Missing required parameters for Cloud Tasks setup');
|
|
9
|
+
}
|
|
10
|
+
const parent = tasksClient.queuePath(projectId, region, queueId);
|
|
11
|
+
const now = Date.now() / 1000;
|
|
12
|
+
const scheduledTime = delaySeconds > 0
|
|
13
|
+
? {
|
|
14
|
+
seconds: Math.floor(now + delaySeconds)
|
|
15
|
+
}
|
|
16
|
+
: undefined;
|
|
17
|
+
const task = {
|
|
18
|
+
httpRequest: {
|
|
19
|
+
httpMethod: tasks_1.protos.google.cloud.tasks.v2.HttpMethod.POST,
|
|
20
|
+
url: audience,
|
|
21
|
+
headers: {
|
|
22
|
+
'Content-Type': 'application/json'
|
|
23
|
+
},
|
|
24
|
+
body: Buffer.from(JSON.stringify(data)).toString('base64'),
|
|
25
|
+
oidcToken: {
|
|
26
|
+
serviceAccountEmail: serviceAccount,
|
|
27
|
+
audience
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
scheduleTime: scheduledTime
|
|
31
|
+
};
|
|
32
|
+
const [response] = await tasksClient.createTask({ parent, task });
|
|
33
|
+
if (!response.name) {
|
|
34
|
+
throw new Error('Failed to create task: no name returned');
|
|
35
|
+
}
|
|
36
|
+
console.log(`✅ Created Cloud Task: ${response.name}`);
|
|
37
|
+
return response.name;
|
|
38
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function isValidWebhookSignature(secret: string, body: string, signature: string): boolean;
|
package/dist/tw/utils.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.isValidWebhookSignature = isValidWebhookSignature;
|
|
7
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
8
|
+
const generateSignature = (body, secret) => {
|
|
9
|
+
return crypto_1.default.createHmac('sha256', secret).update(body).digest('hex');
|
|
10
|
+
};
|
|
11
|
+
function isValidWebhookSignature(secret, body, signature) {
|
|
12
|
+
const expectedSignature = generateSignature(body, secret);
|
|
13
|
+
const expectedBuffer = Buffer.from(expectedSignature);
|
|
14
|
+
const signatureBuffer = Buffer.from(signature);
|
|
15
|
+
if (expectedBuffer.length !== signatureBuffer.length) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
return crypto_1.default.timingSafeEqual(expectedBuffer, signatureBuffer);
|
|
19
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function pregenerateInAppWallet(projectId: string, twClientId: string, twSecretKeyName: string, email: string): Promise<string | null>;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.pregenerateInAppWallet = pregenerateInAppWallet;
|
|
7
|
+
const axios_1 = __importDefault(require("axios"));
|
|
8
|
+
const secrets_1 = require("../secrets");
|
|
9
|
+
async function pregenerateInAppWallet(projectId, twClientId, twSecretKeyName, email) {
|
|
10
|
+
const TW_SECRET_KEY = await (0, secrets_1.getSecret)(projectId, twSecretKeyName);
|
|
11
|
+
try {
|
|
12
|
+
const response = await axios_1.default.post('https://in-app-wallet.thirdweb.com/api/v1/pregenerate', { strategy: 'email', email }, {
|
|
13
|
+
headers: {
|
|
14
|
+
'x-secret-key': TW_SECRET_KEY,
|
|
15
|
+
'x-client-id': twClientId,
|
|
16
|
+
'Content-Type': 'application/json'
|
|
17
|
+
},
|
|
18
|
+
timeout: 5000
|
|
19
|
+
});
|
|
20
|
+
const wallet = response.data.wallet;
|
|
21
|
+
if (!wallet || !wallet.address) {
|
|
22
|
+
console.error('Invalid wallet response:', response.data);
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
console.log('Wallet pregeneration response:', wallet.address);
|
|
26
|
+
return wallet.address;
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
console.error('Error pregenerating wallet:', error);
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
}
|
package/package.json
CHANGED
|
@@ -1,49 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "opticedge-cloud-utils",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.2",
|
|
4
4
|
"description": "Common utilities for cloud functions",
|
|
5
|
-
"
|
|
6
|
-
|
|
7
|
-
"import": "./dist/db/mongo.js",
|
|
8
|
-
"types": "./dist/db/mongo.d.ts"
|
|
9
|
-
},
|
|
10
|
-
"./db/mongo2": {
|
|
11
|
-
"import": "./dist/db/mongo2.js",
|
|
12
|
-
"types": "./dist/db/mongo2.d.ts"
|
|
13
|
-
},
|
|
14
|
-
"./db/mongo3": {
|
|
15
|
-
"import": "./dist/db/mongo3.js",
|
|
16
|
-
"types": "./dist/db/mongo3.d.ts"
|
|
17
|
-
},
|
|
18
|
-
"./auth": {
|
|
19
|
-
"import": "./dist/auth/index.js",
|
|
20
|
-
"types": "./dist/auth/index.d.ts"
|
|
21
|
-
},
|
|
22
|
-
"./env": {
|
|
23
|
-
"import": "./dist/env.js",
|
|
24
|
-
"types": "./dist/env.d.ts"
|
|
25
|
-
},
|
|
26
|
-
"./regex": {
|
|
27
|
-
"import": "./dist/regex.js",
|
|
28
|
-
"types": "./dist/regex.d.ts"
|
|
29
|
-
},
|
|
30
|
-
"./secrets": {
|
|
31
|
-
"import": "./dist/secrets.js",
|
|
32
|
-
"types": "./dist/secrets.d.ts"
|
|
33
|
-
},
|
|
34
|
-
"./task": {
|
|
35
|
-
"import": "./dist/task.js",
|
|
36
|
-
"types": "./dist/task.d.ts"
|
|
37
|
-
},
|
|
38
|
-
"./tw/utils": {
|
|
39
|
-
"import": "./dist/tw/tw-utils.js",
|
|
40
|
-
"types": "./dist/tw/tw-utils.d.ts"
|
|
41
|
-
},
|
|
42
|
-
"./tw/wallet": {
|
|
43
|
-
"import": "./dist/tw/wallet.js",
|
|
44
|
-
"types": "./dist/tw/wallet.d.ts"
|
|
45
|
-
}
|
|
46
|
-
},
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
47
7
|
"scripts": {
|
|
48
8
|
"clean": "rimraf dist coverage *.tsbuildinfo",
|
|
49
9
|
"build": "npm run clean && tsc",
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from './db/mongo'
|
|
2
|
+
export * from './db/mongo2'
|
|
3
|
+
export * from './db/mongo3'
|
|
4
|
+
export * from './tw/utils'
|
|
5
|
+
export * from './tw/wallet'
|
|
6
|
+
export * from './auth'
|
|
7
|
+
export * from './env'
|
|
8
|
+
export * from './regex'
|
|
9
|
+
export * from './secrets'
|
|
10
|
+
export * from './task'
|