From 2c9a3338bed74132279d24f8e1dbb11950c533a5 Mon Sep 17 00:00:00 2001 From: shivanshtalwar Date: Fri, 16 Aug 2024 14:49:33 +0300 Subject: [PATCH] setup for packages --- .github/workflows/release.yml | 45 +++++++++-------- .gitignore | 2 + package.json | 22 +++++--- src/RediFlow.ts | 91 --------------------------------- src/RediFlowConsumerGroup.ts | 95 ----------------------------------- src/RediFlowList.ts | 44 ---------------- src/RediFlowStream.ts | 56 --------------------- src/index.ts | 5 +- 8 files changed, 44 insertions(+), 316 deletions(-) delete mode 100644 src/RediFlow.ts delete mode 100644 src/RediFlowConsumerGroup.ts delete mode 100644 src/RediFlowList.ts delete mode 100644 src/RediFlowStream.ts diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 6fdd6dc..f9d466f 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,22 +1,27 @@ -name: Release - +name: Build and publish to github packages on: - push: - branches: - - master - + push: + branches: + - prod jobs: - build: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - # Setup .npmrc file to publish to npm - - uses: actions/setup-node@v3 - with: - node-version: '20.x' - registry-url: 'https://registry.npmjs.org' - - run: npm i - - run: npm run build - - run: npm publish - env: - NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Use Node.js + uses: actions/setup-node@v3 + with: + node-version: 20.11.0 + registry-url: https://npm.pkg.github.com/ + - name: Configure .npmrc + run: | + echo "@januscaler:registry=https://npm.pkg.github.com/" > .npmrc + echo "//npm.pkg.github.com/:_authToken=${{ secrets.PUBLISH_GITHUB_PACKAGES }}" >> .npmrc + - name: Install Dependencies + run: npm i + - name: Build Package + run: npm run build + - name: Publish Package + run: npm publish + env: + NODE_AUTH_TOKEN: ${{secrets.PUBLISH_GITHUB_PACKAGES}} \ No newline at end of file diff --git a/.gitignore b/.gitignore index 51c5b10..02607ec 100644 --- a/.gitignore +++ b/.gitignore @@ -113,3 +113,5 @@ dist .yarn/unplugged .yarn/build-state.yml .pnp.* +.npmrc +package-lock.json \ No newline at end of file diff --git a/package.json b/package.json index 1ee29cb..ce39a4c 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "nodejs-sdk", + "name": "@januscaler/nodejs-sdk", "version": "1.0.0", "description": "an intuitive nodejs sdk for januscaler services written on top of core-januscaler-js", "keywords": [ @@ -7,6 +7,16 @@ "webrtc", "engine" ], + "repository": { + "type": "git", + "url": "git+https://github.com/januscaler/nodejs-sdk.git" + }, + "bugs": { + "url": "https://github.com/januscaler/nodejs-sdk/issues" + }, + "publishConfig": { + "@januscaler:registry": "https://npm.pkg.github.com" + }, "license": "MIT", "author": "shivanshtalwar0 ", "exports": { @@ -30,8 +40,8 @@ }, "dependencies": { "ioredis": "5.3.2", - "lodash": "^4.17.21", - "rxjs": "^7.8.1" + "lodash": "4.17.21", + "rxjs": "7.8.1" }, "devDependencies": { "@babel/core": "7.18.2", @@ -49,10 +59,10 @@ "@semantic-release/github": "8.0.4", "@semantic-release/npm": "9.0.1", "@semantic-release/release-notes-generator": "10.0.3", - "@types/lodash": "^4.14.200", - "@types/node": "^20.8.9", + "@types/lodash": "4.14.200", + "@types/node": "20.8.9", "lint-staged": "12.4.2", - "prettier": "^2.6.2", + "prettier": "2.6.2", "rimraf": "3.0.2", "rollup": "2.74.1", "rollup-plugin-terser": "7.0.2", diff --git a/src/RediFlow.ts b/src/RediFlow.ts deleted file mode 100644 index 43c2e92..0000000 --- a/src/RediFlow.ts +++ /dev/null @@ -1,91 +0,0 @@ -import Redis, { RedisOptions } from 'ioredis'; -import { Subject } from 'rxjs'; -import { Buffer } from 'buffer'; -import { RediFlowList } from './RediFlowList'; -import { RediFlowStream } from './RediFlowStream'; - -/** - * My Lib - * @example - * dshhbsd - * - */ -export class RediFlow { - protected connection: Redis; - constructor(public options: RedisOptions) { - this.connection = this.factory(); - this.connection.duplicate().on('message', (channel, message) => { - this.messageObservable.next({ channel, message }); - }); - } - - createList(listName: string) { - return new RediFlowList(listName, this.connection.duplicate()); - } - - createStream(streamName: string) { - return new RediFlowStream(streamName, this.connection.duplicate()); - } - - async setJsonObject(key: string, value: any = {}) { - await this.connection.set(key, JSON.stringify(value)); - } - async getJsonObject(key: string) { - const result = await this.connection.get(key); - return JSON.parse(result ?? '{}'); - } - - async deleteJsonObject(key: string) { - return this.connection.del(key); - } - - protected messageObservable = new Subject<{ - channel: string; - message: string; - }>(); - - async publish(channel: string, message: string | Buffer) { - return this.connection.duplicate().publish(channel, message); - } - - async publishJson(channel: string, message: any) { - return this.connection.duplicate().publish(channel, JSON.stringify(message)); - } - - async subscribe(channels: string[]) { - const currentObservable = new Subject<{ - channel: string; - message: string; - }>(); - await this.connection.duplicate().subscribe(...channels); - const subscription = this.messageObservable.subscribe(({ channel, message }) => { - if (channels.includes(channel)) { - currentObservable.next({ channel, message }); - } - }); - const unsubscribe = async () => { - await this.connection.duplicate().unsubscribe(...channels); - subscription.unsubscribe(); - }; - return { consumer: currentObservable, unsubscribe }; - } - - async subscribeJson(channels: string[]) { - const currentObservable = new Subject<{ channel: string; message: any }>(); - await this.connection.duplicate().subscribe(...channels); - const subscription = this.messageObservable.subscribe(({ channel, message }) => { - if (channels.includes(channel)) { - currentObservable.next({ channel, message: JSON.parse(message) }); - } - }); - const unsubscribe = async () => { - await this.connection.duplicate().unsubscribe(...channels); - subscription.unsubscribe(); - }; - return { consumer: currentObservable, unsubscribe }; - } - - factory() { - return new Redis(this.options); - } -} diff --git a/src/RediFlowConsumerGroup.ts b/src/RediFlowConsumerGroup.ts deleted file mode 100644 index e8da4df..0000000 --- a/src/RediFlowConsumerGroup.ts +++ /dev/null @@ -1,95 +0,0 @@ -import _ from 'lodash'; -import Redis from 'ioredis'; -import { Subject } from 'rxjs'; - -export class RediFlowConsumerGroup { - protected connection: Redis; - protected groupName: string; - protected streamName: string; - protected consumerName: string; - constructor(groupName: string, consumerName: string, streamName: string, connection: Redis) { - this.connection = connection; - this.groupName = groupName; - this.streamName = streamName; - this.consumerName = consumerName; - } - createConsumer(consumer: string) { - return this.connection.duplicate().xgroup('CREATECONSUMER', this.streamName, this.groupName, consumer); - } - deleteConsumer(consumer: string) { - return this.connection.duplicate().xgroup('DELCONSUMER', this.streamName, this.groupName, consumer); - } - destroy() { - return this.connection.duplicate().xgroup('DESTROY', this.streamName, this.groupName); - } - readGroup({ count, block, id }: { id: string; count?: number; block?: number }) { - if (!_.isNil(count) && !_.isNil(block)) { - return this.connection.duplicate().xreadgroup('GROUP', this.groupName, this.consumerName, 'COUNT', count, 'BLOCK', block, 'STREAMS', this.streamName, id); - } else if (_.isNil(count) && !_.isNil(block)) { - return this.connection.duplicate().xreadgroup('GROUP', this.groupName, this.consumerName, 'BLOCK', block, 'STREAMS', this.streamName, id); - } else if (!_.isNil(count) && _.isNil(block)) { - return this.connection.duplicate().xreadgroup('GROUP', this.groupName, this.consumerName, 'COUNT', count, 'STREAMS', this.streamName, id); - } - return this.connection.duplicate().xreadgroup('GROUP', this.groupName, this.consumerName, 'STREAMS', this.streamName, id); - } - protected pairedArrayToObject(array: string[]) { - const newMessage = _.fromPairs(_.chunk(array, 2)); - const keyValueObject = _.isArray(newMessage) ? { [newMessage[0]]: newMessage[1] } : newMessage; - return keyValueObject; - } - async observeStream({ count, block }: { count?: number; block?: number } = { block: 0, count: 1 }) { - const observable = new Subject<{ - key: string; - ids: string[]; - items: Record[]; - }>(); - let isFetching = true; - const self = this; - async function fetchItems() { - if (observable.closed) { - stopObserving(); - } - try { - const streamData: any = await self.connection - .duplicate() - .xreadgroup('GROUP', self.groupName, self.consumerName, 'COUNT', count, 'BLOCK', block, 'STREAMS', self.streamName, '>'); - if (_.isNil(streamData)) { - return; - } - const [key, messages] = streamData[0]; - const ids = _.map(messages, (message) => message[0]); - const messageItems = _.map(messages, (message) => message[1]); - const result = _.map(messageItems, self.pairedArrayToObject); - observable.next({ key, ids, items: result }); - setTimeout(async () => { - if (isFetching) { - await fetchItems(); - } - }, 0); - } catch (error) { - console.log(error); - } - } - const interval = setTimeout(async () => { - fetchItems(); - }, 0); - - process.on('exit', (code) => { - stopObserving(); - process.exit(0); - }); - process.on('SIGINT', () => { - stopObserving(); - process.exit(0); - }); - function stopObserving() { - isFetching = false; - clearTimeout(interval); - observable.complete(); - } - return observable; - } - acknowledge(id: string[]) { - return this.connection.duplicate().xack(this.streamName, this.groupName, ...id); - } -} diff --git a/src/RediFlowList.ts b/src/RediFlowList.ts deleted file mode 100644 index 71713a7..0000000 --- a/src/RediFlowList.ts +++ /dev/null @@ -1,44 +0,0 @@ -import _ from 'lodash'; -import Redis from 'ioredis'; -import { Buffer } from 'buffer'; - -export class RediFlowList { - public listName: string; - protected connection: Redis; - constructor(listName: string, connection: Redis) { - this.listName = listName; - this.connection = connection; - } - pushJson(...elements: any[]) { - return this.push(..._.map(elements, (e) => JSON.stringify(e))); - } - push(...elements: (string | number | Buffer)[]) { - return this.connection.rpush(this.listName, ...elements); - } - unshiftJson(...elements: any[]) { - return this.unshift(..._.map(elements, (e) => JSON.stringify(e))); - } - unshift(...elements: (string | number | Buffer)[]) { - return this.connection.lpush(this.listName, ...elements); - } - async popJson(blocking = false, timeOut = 0) { - return JSON.parse((await this.pop(blocking, timeOut)) ?? '{}'); - } - async pop(blocking = false, timeOut = 0) { - if (blocking) { - const [key, value] = (await this.connection.brpop(this.listName, timeOut)) ?? []; - return value; - } - return this.connection.rpop(this.listName); - } - async shiftJson(blocking = false, timeOut = 0) { - return JSON.parse((await this.shift(blocking, timeOut)) ?? '{}'); - } - async shift(blocking = false, timeOut = 0) { - if (blocking) { - const [key, value] = (await this.connection.blpop(this.listName, timeOut)) ?? []; - return value; - } - return this.connection.lpop(this.listName); - } -} diff --git a/src/RediFlowStream.ts b/src/RediFlowStream.ts deleted file mode 100644 index 5e2d24e..0000000 --- a/src/RediFlowStream.ts +++ /dev/null @@ -1,56 +0,0 @@ -import _ from 'lodash'; -import Redis from 'ioredis'; -import { RediFlowConsumerGroup } from './RediFlowConsumerGroup'; - -export class RediFlowStream { - protected connection: Redis; - protected writeConnection: Redis; - streamName: string; - constructor(streamName: string, connection: Redis) { - this.connection = connection; - this.writeConnection = connection.duplicate(); - this.streamName = streamName; - } - async createConsumerGroup( - groupName: string, - consumerName: string, - { makeStream, startId }: { makeStream?: boolean; startId: string | '$' | '0' } = { - startId: '$', - makeStream: true - } - ) { - const consumerGroup = new RediFlowConsumerGroup(groupName, consumerName, this.streamName, this.connection); - const args: any[] = makeStream ? ['MKSTREAM'] : []; - try { - const createdGroup = await this.writeConnection.xgroup('CREATE', this.streamName, groupName, startId, ...args); - } catch (error) {} - return consumerGroup; - } - getLength() { - return this.writeConnection.xlen(this.streamName); - } - delete(id: string[]) { - return this.writeConnection.xdel(this.streamName, ...id); - } - trimByMaxLength(maxLength: number) { - return this.writeConnection.xtrim(this.streamName, 'MAXLEN', '~', maxLength); - } - trimByMinId(minId: string) { - return this.writeConnection.xtrim(this.streamName, 'MINID', '=', minId); - } - getRange({ start, end, count, reverse }: { start: string; end: string; count?: number; reverse?: boolean }) { - const func = reverse ? 'xrevrange' : 'xrange'; - if (_.isNil(count)) return this.writeConnection[func](this.streamName, start, end); - return this.writeConnection[func](this.streamName, start, end, 'COUNT', count); - } - addToStream(map: Record, id = '*') { - const properties = _.transform( - map, - (properties, value, key) => { - properties.push(key, _.isObjectLike(value) ? JSON.stringify(value) : value); - }, - [] - ); - return this.writeConnection.xadd(this.streamName, id, ...properties); - } -} diff --git a/src/index.ts b/src/index.ts index 306611d..b7de2a2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1 @@ -export * from './RediFlow'; -export * from './RediFlowList'; -export * from './RediFlowStream'; -export * from './RediFlowConsumerGroup'; +export const test = () => console.log('test') \ No newline at end of file