@anchan828/nest-cloud-run-queue-pubsub-publisher 1.0.3-next.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/LICENSE +21 -0
- package/README.md +49 -0
- package/dist/constants.d.ts +3 -0
- package/dist/constants.js +6 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +10 -0
- package/dist/interfaces.d.ts +15 -0
- package/dist/interfaces.js +2 -0
- package/dist/providers.d.ts +3 -0
- package/dist/providers.js +8 -0
- package/dist/publish.module.d.ts +6 -0
- package/dist/publish.module.js +53 -0
- package/dist/publish.service.d.ts +12 -0
- package/dist/publish.service.js +73 -0
- package/package.json +50 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 anchan828
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# @anchan828/nest-cloud-run-queue-pubsub-publisher
|
|
2
|
+
|
|
3
|
+
## Install
|
|
4
|
+
|
|
5
|
+
```shell
|
|
6
|
+
npm i @anchan828/nest-cloud-run-queue-pubsub-publisher
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## Usage
|
|
10
|
+
|
|
11
|
+
NOTE: You may want to do tutorial for using Pub/Sub with Cloud Run before using them.
|
|
12
|
+
https://cloud.google.com/run/docs/tutorials/pubsub
|
|
13
|
+
|
|
14
|
+
### Import publisher module
|
|
15
|
+
|
|
16
|
+
```ts
|
|
17
|
+
@Module({
|
|
18
|
+
imports: [
|
|
19
|
+
PubSubPublisherModule.register({
|
|
20
|
+
topic: "myRunTopic",
|
|
21
|
+
clientConfig: {
|
|
22
|
+
// If necessary
|
|
23
|
+
projectId: "projectId",
|
|
24
|
+
keyFilename: "path/to/file.json",
|
|
25
|
+
},
|
|
26
|
+
}),
|
|
27
|
+
],
|
|
28
|
+
})
|
|
29
|
+
export class PublisherAppModule {}
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### Publish message to topic
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
export class Service {
|
|
36
|
+
constructor(private readonly pubsubService: PubSubPublisherService) {}
|
|
37
|
+
|
|
38
|
+
public async publishMessage(): Promise<void> {
|
|
39
|
+
await this.pubsubService.publish({
|
|
40
|
+
// Required. this property is used by @anchan828/nest-cloud-run-queue-worker
|
|
41
|
+
name: "Worker name",
|
|
42
|
+
// string or object. ex, { text: "text" }
|
|
43
|
+
data: "text",
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
See more information: [https://github.com/anchan828/nest-cloud-run-queue#readme](https://github.com/anchan828/nest-cloud-run-queue#readme)
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ERROR_TOPIC_NOT_FOUND = exports.PUBSUB = exports.PUBSUB_PUBLISHER_MODULE_OPTIONS = void 0;
|
|
4
|
+
exports.PUBSUB_PUBLISHER_MODULE_OPTIONS = "PUBSUB_PUBLISHER_MODULE_OPTIONS";
|
|
5
|
+
exports.PUBSUB = "PUBSUB";
|
|
6
|
+
exports.ERROR_TOPIC_NOT_FOUND = "Topic not found. Please set topic name.";
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { PUBSUB, PUBSUB_PUBLISHER_MODULE_OPTIONS } from "./constants";
|
|
2
|
+
export { PubSubPublisherModuleAsyncOptions, PubSubPublisherModuleOptions, PubSubPublisherModuleOptionsFactory, PublishData, } from "./interfaces";
|
|
3
|
+
export { PubSubPublisherModule } from "./publish.module";
|
|
4
|
+
export { PubSubPublisherService } from "./publish.service";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PubSubPublisherService = exports.PubSubPublisherModule = exports.PUBSUB_PUBLISHER_MODULE_OPTIONS = exports.PUBSUB = void 0;
|
|
4
|
+
var constants_1 = require("./constants");
|
|
5
|
+
Object.defineProperty(exports, "PUBSUB", { enumerable: true, get: function () { return constants_1.PUBSUB; } });
|
|
6
|
+
Object.defineProperty(exports, "PUBSUB_PUBLISHER_MODULE_OPTIONS", { enumerable: true, get: function () { return constants_1.PUBSUB_PUBLISHER_MODULE_OPTIONS; } });
|
|
7
|
+
var publish_module_1 = require("./publish.module");
|
|
8
|
+
Object.defineProperty(exports, "PubSubPublisherModule", { enumerable: true, get: function () { return publish_module_1.PubSubPublisherModule; } });
|
|
9
|
+
var publish_service_1 = require("./publish.service");
|
|
10
|
+
Object.defineProperty(exports, "PubSubPublisherService", { enumerable: true, get: function () { return publish_service_1.PubSubPublisherService; } });
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Message, ModuleAsyncOptions, ModuleOptions, ModuleOptionsFactory, PublishExtraConfig } from "@anchan828/nest-cloud-run-queue-common";
|
|
2
|
+
import { Attributes } from "@google-cloud/pubsub";
|
|
3
|
+
import { ClientConfig } from "@google-cloud/pubsub/build/src/pubsub";
|
|
4
|
+
import { PublishOptions } from "@google-cloud/pubsub/build/src/topic";
|
|
5
|
+
export interface PubSubPublisherModuleOptions extends ModuleOptions {
|
|
6
|
+
topic?: string;
|
|
7
|
+
clientConfig?: ClientConfig;
|
|
8
|
+
publishConfig?: PublishOptions;
|
|
9
|
+
extraConfig?: PublishExtraConfig<PublishData<any>>;
|
|
10
|
+
}
|
|
11
|
+
export declare type PubSubPublisherModuleAsyncOptions = ModuleAsyncOptions<PubSubPublisherModuleOptions>;
|
|
12
|
+
export declare type PubSubPublisherModuleOptionsFactory = ModuleOptionsFactory<PubSubPublisherModuleOptions>;
|
|
13
|
+
export interface PublishData<T> extends Message<T> {
|
|
14
|
+
attributes?: Attributes;
|
|
15
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createPubSub = void 0;
|
|
4
|
+
const pubsub_1 = require("@google-cloud/pubsub");
|
|
5
|
+
function createPubSub(options) {
|
|
6
|
+
return new pubsub_1.PubSub(options.clientConfig);
|
|
7
|
+
}
|
|
8
|
+
exports.createPubSub = createPubSub;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { DynamicModule } from "@nestjs/common";
|
|
2
|
+
import { PubSubPublisherModuleAsyncOptions, PubSubPublisherModuleOptions } from "./interfaces";
|
|
3
|
+
export declare class PubSubPublisherModule {
|
|
4
|
+
static register(options?: PubSubPublisherModuleOptions): DynamicModule;
|
|
5
|
+
static registerAsync(options: PubSubPublisherModuleAsyncOptions): DynamicModule;
|
|
6
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var PubSubPublisherModule_1;
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.PubSubPublisherModule = void 0;
|
|
11
|
+
const nest_cloud_run_queue_common_1 = require("@anchan828/nest-cloud-run-queue-common");
|
|
12
|
+
const common_1 = require("@nestjs/common");
|
|
13
|
+
const constants_1 = require("./constants");
|
|
14
|
+
const providers_1 = require("./providers");
|
|
15
|
+
const publish_service_1 = require("./publish.service");
|
|
16
|
+
let PubSubPublisherModule = PubSubPublisherModule_1 = class PubSubPublisherModule {
|
|
17
|
+
static register(options = {}) {
|
|
18
|
+
const providers = [
|
|
19
|
+
(0, nest_cloud_run_queue_common_1.createOptionProvider)(constants_1.PUBSUB_PUBLISHER_MODULE_OPTIONS, options),
|
|
20
|
+
publish_service_1.PubSubPublisherService,
|
|
21
|
+
{ provide: constants_1.PUBSUB, useValue: (0, providers_1.createPubSub)(options) },
|
|
22
|
+
];
|
|
23
|
+
return {
|
|
24
|
+
exports: providers,
|
|
25
|
+
global: true,
|
|
26
|
+
module: PubSubPublisherModule_1,
|
|
27
|
+
providers,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
static registerAsync(options) {
|
|
31
|
+
const asyncProviders = [
|
|
32
|
+
...(0, nest_cloud_run_queue_common_1.createAsyncProviders)(constants_1.PUBSUB_PUBLISHER_MODULE_OPTIONS, options),
|
|
33
|
+
publish_service_1.PubSubPublisherService,
|
|
34
|
+
{
|
|
35
|
+
inject: [constants_1.PUBSUB_PUBLISHER_MODULE_OPTIONS],
|
|
36
|
+
provide: constants_1.PUBSUB,
|
|
37
|
+
useFactory: (options) => (0, providers_1.createPubSub)(options),
|
|
38
|
+
},
|
|
39
|
+
];
|
|
40
|
+
const providers = [...asyncProviders];
|
|
41
|
+
return {
|
|
42
|
+
exports: providers,
|
|
43
|
+
global: true,
|
|
44
|
+
imports: [...(options.imports || [])],
|
|
45
|
+
module: PubSubPublisherModule_1,
|
|
46
|
+
providers,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
PubSubPublisherModule = PubSubPublisherModule_1 = __decorate([
|
|
51
|
+
(0, common_1.Module)({})
|
|
52
|
+
], PubSubPublisherModule);
|
|
53
|
+
exports.PubSubPublisherModule = PubSubPublisherModule;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { PubSub } from "@google-cloud/pubsub";
|
|
2
|
+
import { PublishOptions } from "@google-cloud/pubsub/build/src/topic";
|
|
3
|
+
import { PubSubPublisherModuleOptions, PublishData } from "./interfaces";
|
|
4
|
+
export declare class PubSubPublisherService {
|
|
5
|
+
private readonly options;
|
|
6
|
+
private readonly pubsub;
|
|
7
|
+
constructor(options: PubSubPublisherModuleOptions, pubsub: PubSub);
|
|
8
|
+
publish<T>(message: PublishData<T>, options?: PublishOptions & {
|
|
9
|
+
topic?: string;
|
|
10
|
+
}): Promise<string>;
|
|
11
|
+
private getTopicName;
|
|
12
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
15
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
16
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
17
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
18
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
19
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
20
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
24
|
+
var t = {};
|
|
25
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
26
|
+
t[p] = s[p];
|
|
27
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
28
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
29
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
30
|
+
t[p[i]] = s[p[i]];
|
|
31
|
+
}
|
|
32
|
+
return t;
|
|
33
|
+
};
|
|
34
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
35
|
+
exports.PubSubPublisherService = void 0;
|
|
36
|
+
const pubsub_1 = require("@google-cloud/pubsub");
|
|
37
|
+
const common_1 = require("@nestjs/common");
|
|
38
|
+
const constants_1 = require("./constants");
|
|
39
|
+
let PubSubPublisherService = class PubSubPublisherService {
|
|
40
|
+
constructor(options, pubsub) {
|
|
41
|
+
this.options = options;
|
|
42
|
+
this.pubsub = pubsub;
|
|
43
|
+
}
|
|
44
|
+
publish(message, options) {
|
|
45
|
+
var _a, _b, _c, _d;
|
|
46
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
47
|
+
const topicName = this.getTopicName(options);
|
|
48
|
+
const topic = this.pubsub.topic(topicName, Object.assign({}, this.options.publishConfig, options));
|
|
49
|
+
const _e = ((_a = this.options.extraConfig) === null || _a === void 0 ? void 0 : _a.prePublish)
|
|
50
|
+
? yield ((_b = this.options.extraConfig) === null || _b === void 0 ? void 0 : _b.prePublish(message))
|
|
51
|
+
: message, { attributes } = _e, json = __rest(_e, ["attributes"]);
|
|
52
|
+
const messageId = yield topic.publishMessage({ attributes, json });
|
|
53
|
+
if ((_c = this.options.extraConfig) === null || _c === void 0 ? void 0 : _c.postPublish) {
|
|
54
|
+
yield ((_d = this.options.extraConfig) === null || _d === void 0 ? void 0 : _d.postPublish(message, messageId));
|
|
55
|
+
}
|
|
56
|
+
return messageId;
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
getTopicName(options) {
|
|
60
|
+
const topic = this.options.topic || (options === null || options === void 0 ? void 0 : options.topic);
|
|
61
|
+
if (!topic) {
|
|
62
|
+
throw new Error(constants_1.ERROR_TOPIC_NOT_FOUND);
|
|
63
|
+
}
|
|
64
|
+
return topic;
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
PubSubPublisherService = __decorate([
|
|
68
|
+
(0, common_1.Injectable)(),
|
|
69
|
+
__param(0, (0, common_1.Inject)(constants_1.PUBSUB_PUBLISHER_MODULE_OPTIONS)),
|
|
70
|
+
__param(1, (0, common_1.Inject)(constants_1.PUBSUB)),
|
|
71
|
+
__metadata("design:paramtypes", [Object, pubsub_1.PubSub])
|
|
72
|
+
], PubSubPublisherService);
|
|
73
|
+
exports.PubSubPublisherService = PubSubPublisherService;
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@anchan828/nest-cloud-run-queue-pubsub-publisher",
|
|
3
|
+
"version": "1.0.3-next.0",
|
|
4
|
+
"description": "> TODO: description",
|
|
5
|
+
"homepage": "https://github.com/anchan828/nest-cloud-run-queue/tree/master/packages/pubsub-publish#readme",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/anchan828/nest-cloud-run-queue/issues"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/anchan828/nest-cloud-run-queue.git"
|
|
12
|
+
},
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"author": "anchan828 <anchan828@gmail.com>",
|
|
15
|
+
"main": "./dist/index.js",
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"directories": {
|
|
18
|
+
"dist": "dist"
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist"
|
|
22
|
+
],
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build": "tsc -p tsconfig.build.json",
|
|
25
|
+
"build:watch": "tsc --watch",
|
|
26
|
+
"copy:license": "cp ../../LICENSE ./",
|
|
27
|
+
"lint": "eslint --ignore-path ../../.eslintignore '**/*.ts'",
|
|
28
|
+
"lint:fix": "npm run lint -- --fix",
|
|
29
|
+
"prepublishOnly": "rm -rf dist && npm run build && rm -f dist/*.tsbuildinfo && npm run copy:license",
|
|
30
|
+
"test": "jest --coverage --logHeapUsage --runInBand",
|
|
31
|
+
"test:debug": "node --inspect-brk node_modules/.bin/jest --runInBand --logHeapUsage",
|
|
32
|
+
"test:watch": "jest --watch",
|
|
33
|
+
"watch": "tsc -w"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@anchan828/nest-cloud-run-queue-common": "^1.0.3-next.0",
|
|
37
|
+
"@google-cloud/pubsub": "2.19.0"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@nestjs/common": "8.4.4",
|
|
41
|
+
"rxjs": "7.5.5"
|
|
42
|
+
},
|
|
43
|
+
"peerDependencies": {
|
|
44
|
+
"@nestjs/common": "^8.0.0"
|
|
45
|
+
},
|
|
46
|
+
"publishConfig": {
|
|
47
|
+
"access": "public"
|
|
48
|
+
},
|
|
49
|
+
"gitHead": "470341e04eead0d62396245ff1a3fe0c7ad4143e"
|
|
50
|
+
}
|