gcf-common-lib 0.35.0 → 0.36.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/npm-publish.yml +39 -36
- package/.junie/guidelines.md +108 -0
- package/.prettierrc +10 -10
- package/CODE_OF_CONDUCT.md +13 -0
- package/CONTRIBUTING.md +60 -0
- package/LICENSE +21 -0
- package/README.md +110 -1
- package/docs/plan.md +211 -0
- package/docs/tasks.md +88 -0
- package/eslint.config.mjs +31 -31
- package/package.json +63 -52
- package/src/{amqp-helper.ts → amqp-helper.ts.old} +52 -52
- package/src/index.js +191 -188
- package/src/index.ts +177 -178
- package/src/mongo-helper.js +42 -42
- package/src/mongo-helper.ts +51 -51
- package/src/mongo-lock.js +86 -86
- package/src/mongo-lock.ts +99 -99
- package/src/types.js +2 -2
- package/src/types.ts +84 -84
- package/src/utils.js +105 -84
- package/src/utils.ts +110 -88
- package/test/gcf-common.process.test.js +63 -0
- package/test/gcf-common.process.test.ts +65 -0
- package/test/metadata.test.js +77 -0
- package/test/metadata.test.ts +78 -0
- package/test/sample.test.js +12 -0
- package/test/test.js +30 -25
- package/test/test.ts +32 -26
- package/test/ts-tests.test.js +8 -0
- package/test/utils.test.js +43 -0
- package/test/utils.test.ts +47 -0
- package/tsconfig.json +7 -7
- package/src/amqp-helper.js +0 -44
package/src/index.js
CHANGED
|
@@ -1,188 +1,191 @@
|
|
|
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
36
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
37
|
-
};
|
|
38
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
|
-
};
|
|
41
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
-
exports.GcfCommon = exports.storage = exports.pubSub = exports.MongoDb = exports.RxJs = exports.PubSub = exports.Storage = void 0;
|
|
43
|
-
const pubsub_1 = require("@google-cloud/pubsub");
|
|
44
|
-
const storage_1 = require("@google-cloud/storage");
|
|
45
|
-
const isEmpty_1 = __importDefault(require("lodash/isEmpty"));
|
|
46
|
-
const mapValues_1 = __importDefault(require("lodash/mapValues"));
|
|
47
|
-
const noop_1 = __importDefault(require("lodash/noop"));
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
exports.
|
|
51
|
-
exports.
|
|
52
|
-
exports.
|
|
53
|
-
|
|
54
|
-
__exportStar(require("./
|
|
55
|
-
__exportStar(require("./
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
exports.
|
|
59
|
-
exports.
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
.
|
|
78
|
-
await exports.GcfCommon.response({ ...payload },
|
|
79
|
-
|
|
80
|
-
})
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
//
|
|
97
|
-
//
|
|
98
|
-
//
|
|
99
|
-
|
|
100
|
-
//
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
//
|
|
141
|
-
//
|
|
142
|
-
// //
|
|
143
|
-
//
|
|
144
|
-
//
|
|
145
|
-
//
|
|
146
|
-
//
|
|
147
|
-
//
|
|
148
|
-
//
|
|
149
|
-
//
|
|
150
|
-
//
|
|
151
|
-
//
|
|
152
|
-
//
|
|
153
|
-
//
|
|
154
|
-
//
|
|
155
|
-
//
|
|
156
|
-
//
|
|
157
|
-
//
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
return
|
|
187
|
-
},
|
|
188
|
-
|
|
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
36
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
37
|
+
};
|
|
38
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
|
+
};
|
|
41
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
+
exports.GcfCommon = exports.storage = exports.pubSub = exports.MongoDb = exports.RxJs = exports.PubSub = exports.Storage = void 0;
|
|
43
|
+
const pubsub_1 = require("@google-cloud/pubsub");
|
|
44
|
+
const storage_1 = require("@google-cloud/storage");
|
|
45
|
+
const isEmpty_1 = __importDefault(require("lodash/isEmpty"));
|
|
46
|
+
const mapValues_1 = __importDefault(require("lodash/mapValues"));
|
|
47
|
+
const noop_1 = __importDefault(require("lodash/noop"));
|
|
48
|
+
const utils_1 = require("./utils");
|
|
49
|
+
exports.Storage = __importStar(require("@google-cloud/storage"));
|
|
50
|
+
exports.PubSub = __importStar(require("@google-cloud/pubsub"));
|
|
51
|
+
exports.RxJs = __importStar(require("rxjs"));
|
|
52
|
+
exports.MongoDb = __importStar(require("mongodb"));
|
|
53
|
+
__exportStar(require("./types"), exports);
|
|
54
|
+
__exportStar(require("./utils"), exports);
|
|
55
|
+
__exportStar(require("./mongo-helper"), exports);
|
|
56
|
+
// export * from './amqp-helper.ts';
|
|
57
|
+
exports.pubSub = new pubsub_1.PubSub();
|
|
58
|
+
exports.storage = new storage_1.Storage();
|
|
59
|
+
exports.GcfCommon = {
|
|
60
|
+
// amqpOptions: {
|
|
61
|
+
// assertExchange: { durable: true, autoDelete: false },
|
|
62
|
+
// assertOptions: { durable: true, autoDelete: true, expires: ms({ d: 1 }) },
|
|
63
|
+
// publishOptions: { persistent: true },
|
|
64
|
+
// } as {
|
|
65
|
+
// url?: string;
|
|
66
|
+
// assertExchange?: Options.AssertExchange;
|
|
67
|
+
// assertOptions?: Options.AssertQueue;
|
|
68
|
+
// publishOptions?: Options.Publish;
|
|
69
|
+
// },
|
|
70
|
+
async process(payload, handler) {
|
|
71
|
+
const asyncHandler = async (p) => await handler(p);
|
|
72
|
+
// return firstValueFrom(
|
|
73
|
+
// defer(() => from(asyncHandler({ ...payload }))).pipe(first(), timeout({ first: ms({ s: timeoutSec }) })),
|
|
74
|
+
// )
|
|
75
|
+
return asyncHandler({ ...payload })
|
|
76
|
+
.then(async (res) => {
|
|
77
|
+
// console.log('res:', res);
|
|
78
|
+
await exports.GcfCommon.response({ ...payload }, res).catch(noop_1.default);
|
|
79
|
+
return res;
|
|
80
|
+
})
|
|
81
|
+
.catch(async (error) => {
|
|
82
|
+
await exports.GcfCommon.response({ ...payload }, exports.GcfCommon.buildResponse(error), { error: '1' }).catch(noop_1.default);
|
|
83
|
+
throw error;
|
|
84
|
+
});
|
|
85
|
+
},
|
|
86
|
+
buildResponse(error) {
|
|
87
|
+
return {
|
|
88
|
+
error: {
|
|
89
|
+
name: error.name,
|
|
90
|
+
message: `GCF [${process?.env?.K_SERVICE ?? 'UNKNOWN'}]: ${error.message}`,
|
|
91
|
+
stack: error.stack,
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
},
|
|
95
|
+
async response(payload, json, attributes) {
|
|
96
|
+
// console.time('safeGetAttributes');
|
|
97
|
+
// const { topic, exchange, queue, consumer_id, request_id, app_id, env } = await GcfCommon.safeGetAttributes(
|
|
98
|
+
// event,
|
|
99
|
+
// context,
|
|
100
|
+
// ['consumer_id', 'topic', 'exchange', 'queue'],
|
|
101
|
+
// );
|
|
102
|
+
// console.timeEnd('safeGetAttributes');
|
|
103
|
+
const { topic, exchange, queue, consumer_id, request_id, app_id, env } = exports.GcfCommon.getMetadataOrAttribute(payload);
|
|
104
|
+
//
|
|
105
|
+
console.time('publish');
|
|
106
|
+
if (topic && !(0, isEmpty_1.default)(topic)) {
|
|
107
|
+
console.log('send:', topic, app_id, env, json, attributes);
|
|
108
|
+
await exports.pubSub.topic(topic).publishMessage({
|
|
109
|
+
json: json ?? {},
|
|
110
|
+
attributes: {
|
|
111
|
+
...(0, mapValues_1.default)(attributes ?? {}, v => '' + v),
|
|
112
|
+
consumer_id: consumer_id ?? '',
|
|
113
|
+
request_id: request_id ?? '',
|
|
114
|
+
app_id: app_id ?? '',
|
|
115
|
+
env: env ?? '',
|
|
116
|
+
},
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
// if (exchange && !isEmpty(exchange)) {
|
|
120
|
+
// console.log('send:', exchange, queue, app_id, env, json, attributes);
|
|
121
|
+
// await AmqpHelper.withAmqpCh(async ch => {
|
|
122
|
+
// await ch.assertExchange(exchange, 'direct', GcfCommon.amqpOptions.assertExchange);
|
|
123
|
+
// await AmqpHelper.publishAmqp(ch, exchange, queue ?? '', json ?? {}, {
|
|
124
|
+
// ...GcfCommon.amqpOptions.publishOptions,
|
|
125
|
+
// correlationId: request_id,
|
|
126
|
+
// });
|
|
127
|
+
// }, GcfCommon.amqpOptions.url as string);
|
|
128
|
+
// } else if (queue && !isEmpty(queue)) {
|
|
129
|
+
// console.log('send:', queue, app_id, env, json, attributes);
|
|
130
|
+
// await AmqpHelper.withAmqpCh(async ch => {
|
|
131
|
+
// // await ch.assertQueue(queue, GcfCommon.amqpOptions.assertOptions);
|
|
132
|
+
// await AmqpHelper.publishAmqp(ch, undefined, queue, json ?? {}, {
|
|
133
|
+
// ...GcfCommon.amqpOptions.publishOptions,
|
|
134
|
+
// correlationId: request_id,
|
|
135
|
+
// });
|
|
136
|
+
// }, GcfCommon.amqpOptions.url as string);
|
|
137
|
+
// }
|
|
138
|
+
console.timeEnd('publish');
|
|
139
|
+
},
|
|
140
|
+
// async safeGetAttributes<E = TEvent>(event: E, context: TContext, props: string[]) {
|
|
141
|
+
// let metaOrAttr = GcfCommon.getMetadataOrAttribute(event, context);
|
|
142
|
+
// // const everyPropIsNil = props.map(prop => get(metaOrAttr, prop)).every(v => isNil(v));
|
|
143
|
+
//
|
|
144
|
+
// const someProp = props.map(prop => get(metaOrAttr, prop)).some(v => !isNil(v));
|
|
145
|
+
// // if no prop then check file metadata
|
|
146
|
+
// if (!someProp && context?.resource?.type === 'storage#object') {
|
|
147
|
+
// console.log('get metadata from file');
|
|
148
|
+
// if (context?.eventType === 'google.storage.object.finalize') {
|
|
149
|
+
// const gsEvent = event as TGSEvent;
|
|
150
|
+
// const [meta] = await new Storage().bucket(gsEvent.bucket).file(gsEvent.name).getMetadata();
|
|
151
|
+
// metaOrAttr = meta?.metadata ?? {};
|
|
152
|
+
// }
|
|
153
|
+
// }
|
|
154
|
+
//
|
|
155
|
+
// return {
|
|
156
|
+
// ...metaOrAttr,
|
|
157
|
+
// app_id: metaOrAttr.app_id,
|
|
158
|
+
// request_id: metaOrAttr.request_id,
|
|
159
|
+
// } as TMetadataOrAttributes;
|
|
160
|
+
// },
|
|
161
|
+
async getOptions(payload) {
|
|
162
|
+
// const { options } = await GcfCommon.safeGetAttributes(event, context, ['options']);
|
|
163
|
+
const { options } = exports.GcfCommon.getMetadataOrAttribute(payload);
|
|
164
|
+
return (0, utils_1.safeJsonParse)(options, {});
|
|
165
|
+
},
|
|
166
|
+
getMetadataOrAttribute(payload) {
|
|
167
|
+
let metadataOrAttribute;
|
|
168
|
+
if (payload?.context) {
|
|
169
|
+
switch (payload?.context?.eventType) {
|
|
170
|
+
case 'google.storage.object.finalize': {
|
|
171
|
+
const gsEvent = payload?.event;
|
|
172
|
+
metadataOrAttribute = gsEvent?.metadata;
|
|
173
|
+
break;
|
|
174
|
+
}
|
|
175
|
+
case 'google.pubsub.topic.publish': {
|
|
176
|
+
const psEvent = payload?.event;
|
|
177
|
+
metadataOrAttribute = psEvent?.attributes;
|
|
178
|
+
break;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
else if (payload?.request) {
|
|
183
|
+
metadataOrAttribute = exports.GcfCommon.getRequestMessage(payload.request)?.attributes;
|
|
184
|
+
}
|
|
185
|
+
// console.log('metadataOrAttribute:', metadataOrAttribute);
|
|
186
|
+
return metadataOrAttribute ?? {};
|
|
187
|
+
},
|
|
188
|
+
getRequestMessage(request) {
|
|
189
|
+
return request.body.message;
|
|
190
|
+
},
|
|
191
|
+
};
|