Skip to content

Commit

Permalink
feat: export all types
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Sep 2, 2023
1 parent 118fe50 commit f561110
Show file tree
Hide file tree
Showing 22 changed files with 116 additions and 60 deletions.
2 changes: 1 addition & 1 deletion src/client/Binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
} from '../types/Binding';
import { convertMapToKVString } from '../utils';

export default class Binding extends RuntimeAPI {
export class Binding extends RuntimeAPI {
async invoke(request: InvokeBindingRequest): Promise<InvokeBindingResponse> {
const req = new InvokeBindingRequestPB();
req.setName(request.name);
Expand Down
24 changes: 12 additions & 12 deletions src/client/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ import { ChannelCredentials } from '@grpc/grpc-js';
import { RuntimeClient } from '../../proto/runtime/v1/runtime_grpc_pb';
import { ObjectStorageServiceClient } from '../../proto/extension/v1/s3/oss_grpc_pb';
import { CryptionServiceClient } from '../../proto/extension/v1/cryption/cryption_grpc_pb';
import State from './State';
import Hello from './Hello';
import Invoker from './Invoker';
import Lock from './Lock';
import Sequencer from './Sequencer';
import Configuration from './Configuration';
import PubSub from './PubSub';
import File from './File';
import Binding from './Binding';
import Oss, { OssOptions } from './Oss';
import Cryption, { CryptionOptions } from './Cryption';
import { State } from './State';
import { Hello } from './Hello';
import { Invoker } from './Invoker';
import { Lock } from './Lock';
import { Sequencer } from './Sequencer';
import { Configuration } from './Configuration';
import { PubSub } from './PubSub';
import { File } from './File';
import { Binding } from './Binding';
import { Oss, OssOptions } from './Oss';
import { Cryption, CryptionOptions } from './Cryption';
import type { CreateMetadataHook } from './API';

const debug = debuglog('layotto:client:main');
Expand All @@ -43,7 +43,7 @@ export interface ClientOptions {
createMetadataHook?: CreateMetadataHook;
}

export default class Client {
export class Client {
readonly host: string;
readonly port: string;
protected readonly localStorage?: AsyncLocalStorage<any>;
Expand Down
2 changes: 1 addition & 1 deletion src/client/Configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { convertArrayToKVString } from '../types/common';

const debug = debuglog('layotto:client:configuration');

export default class Configuration extends RuntimeAPI {
export class Configuration extends RuntimeAPI {
// GetConfiguration gets configuration from configuration store.
async get(request: GetConfigurationRequest): Promise<GetConfigurationItem[]> {
const req = new GetConfigurationRequestPB();
Expand Down
34 changes: 11 additions & 23 deletions src/client/Cryption.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,24 @@
import {
EncryptRequest as EncryptRequestPB,
EncryptResponse,
EncryptResponse as EncryptResponsePB,
DecryptRequest as DecryptRequestPB,
DecryptResponse as DecryptResponsePB,
} from '../../proto/extension/v1/cryption/cryption_pb';
import { CryptionServiceClient } from '../../proto/extension/v1/cryption/cryption_grpc_pb';
import { API, APIOptions } from './API';
import { RequestWithMeta } from '../types/common';

export type EncryptRequest = RequestWithMeta<{
plainText: Uint8Array | string;
keyId?: string;
}>;

export type DecryptRequest = RequestWithMeta<{
cipherText: Uint8Array | string;
}>;

export type DecryptResponse = {
plainText: Uint8Array;
keyId: string,
keyVersionId: string,
requestId: string,
};
import {
EncryptRequest,
DecryptRequest,
DecryptResponse,
} from '../types/Cryption';

export type CryptionOptions = {
export interface CryptionOptions {
componentName: string;
// set default metadata on every request
defaultRequestMeta?: Record<string, string>;
};
}

export default class Cryption extends API {
export class Cryption extends API {
private readonly cryptionClient: CryptionServiceClient;
private readonly options: CryptionOptions;

Expand All @@ -40,7 +28,7 @@ export default class Cryption extends API {
this.cryptionClient = cryptionClient;
}

async encrypt(request: EncryptRequest): Promise<EncryptResponse.AsObject> {
async encrypt(request: EncryptRequest): Promise<EncryptResponsePB.AsObject> {
const req = new EncryptRequestPB();
req.setComponentName(this.options.componentName);
let plainText = request.plainText;
Expand All @@ -54,7 +42,7 @@ export default class Cryption extends API {

const metadata = this.createMetadata(request, this.options.defaultRequestMeta);
return new Promise((resolve, reject) => {
this.cryptionClient.encrypt(req, metadata, (err, res: EncryptResponse) => {
this.cryptionClient.encrypt(req, metadata, (err, res: EncryptResponsePB) => {
if (err) return reject(err);
resolve(res.toObject());
});
Expand Down
2 changes: 1 addition & 1 deletion src/client/File.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { GetFileRequest, ListFileResponse, PutFileRequest } from '../types/File'

const debug = debuglog('layotto:client:file');

export default class File extends RuntimeAPI {
export class File extends RuntimeAPI {
// Get a file stream
async get(request: GetFileRequest): Promise<Readable> {
const req = new GetFileRequestPB();
Expand Down
2 changes: 1 addition & 1 deletion src/client/Hello.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
import { RuntimeAPI } from './RuntimeAPI';
import { SayHelloRequest } from '../types/Hello';

export default class Hello extends RuntimeAPI {
export class Hello extends RuntimeAPI {
async sayHello(request?: SayHelloRequest): Promise<string> {
const req = new SayHelloRequestPB();
if (!request) request = {};
Expand Down
2 changes: 1 addition & 1 deletion src/client/Invoker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
import { RuntimeAPI } from './RuntimeAPI';
import { InvokeServiceRequest, InvokeResponse } from '../types/Invoker';

export default class Invoker extends RuntimeAPI {
export class Invoker extends RuntimeAPI {
async invoke(request: InvokeServiceRequest): Promise<InvokeResponse> {
const message = new CommonInvokeRequestPB();
message.setMethod(request.method);
Expand Down
2 changes: 1 addition & 1 deletion src/client/Lock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
import { RuntimeAPI } from './RuntimeAPI';
import { TryLockRequest, UnlockRequest } from '../types/Lock';

export default class Lock extends RuntimeAPI {
export class Lock extends RuntimeAPI {
// A non-blocking method trying to get a lock with ttl
// expire is the time before expire. The time unit is second.
async tryLock(request: TryLockRequest): Promise<boolean> {
Expand Down
6 changes: 3 additions & 3 deletions src/client/Oss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ import {
import { ObjectStorageServiceClient } from '../../proto/extension/v1/s3/oss_grpc_pb';
import { API, APIOptions } from './API';

export type OssOptions = {
export interface OssOptions {
// set default metadata on every request
defaultRequestMeta?: Record<string, string>;
};
}

export default class Oss extends API {
export class Oss extends API {
private readonly ossClient: ObjectStorageServiceClient;
private readonly options: OssOptions;

Expand Down
2 changes: 1 addition & 1 deletion src/client/PubSub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
import { RuntimeAPI } from './RuntimeAPI';
import { PublishEventRequest } from '../types/PubSub';

export default class PubSub extends RuntimeAPI {
export class PubSub extends RuntimeAPI {
async publish(request: PublishEventRequest): Promise<void> {
const req = new PublishEventRequestPB();
req.setPubsubName(request.pubsubName);
Expand Down
2 changes: 1 addition & 1 deletion src/client/Sequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
import { RuntimeAPI } from './RuntimeAPI';
import { GetNextIdRequest } from '../types/Sequencer';

export default class Sequencer extends RuntimeAPI {
export class Sequencer extends RuntimeAPI {
// Get next unique id with some auto-increment guarantee
async getNextId(request: GetNextIdRequest): Promise<string> {
const req = new GetNextIdRequestPB();
Expand Down
2 changes: 1 addition & 1 deletion src/client/State.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import {
} from '../types/State';
import { isEmptyPBMessage, convertMapToKVString } from '../utils';

export default class State extends RuntimeAPI {
export class State extends RuntimeAPI {
// Saves an array of state objects
async save(request: SaveStateRequest): Promise<void> {
let states = request.states;
Expand Down
14 changes: 14 additions & 0 deletions src/client/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export * from './API';
export * from './Binding';
export * from './Client';
export * from './Configuration';
export * from './Cryption';
export * from './File';
export * from './Hello';
export * from './Invoker';
export * from './Lock';
export * from './Oss';
export * from './PubSub';
export * from './RuntimeAPI';
export * from './Sequencer';
export * from './State';
10 changes: 7 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import Client, { ClientOptions } from './client/Client';
import Server, { ServerOptions } from './server/Server';
import GRPCServerImpl, { GRPCServerOptions } from './server/GRPCServerImpl';
import { Client, ClientOptions } from './client/Client';
import { Server, ServerOptions } from './server/Server';
import { GRPCServerImpl, GRPCServerOptions } from './server/GRPCServerImpl';
import * as utils from './utils';
import * as RuntimeTypes from '../proto/runtime/v1/runtime_pb';
import * as Types from './types';
import * as Clients from './client';
import * as Servers from './server';

export {
Client,
Expand All @@ -29,4 +31,6 @@ export {
utils,
RuntimeTypes,
Types,
Clients,
Servers,
};
2 changes: 1 addition & 1 deletion src/server/GRPCServerImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export interface GRPCServerOptions {

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
export default class GRPCServerImpl implements IAppCallbackServer {
export class GRPCServerImpl implements IAppCallbackServer {
protected readonly handlersTopics: Record<string, PubSubCallback> = {};
protected readonly subscriptionsList: TopicSubscription[] = [];
protected readonly localStorage?: AsyncLocalStorage<any>;
Expand Down
5 changes: 2 additions & 3 deletions src/server/PubSub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import { debuglog } from 'node:util';
import { PubSubCallback } from '../types/PubSub';
import GRPCServerImpl from './GRPCServerImpl';
import { GRPCServerImpl } from './GRPCServerImpl';

const debug = debuglog('layotto:server:pubsub');

export default class PubSub {
export class PubSub {
readonly server: GRPCServerImpl;

constructor(server: GRPCServerImpl) {
Expand Down
6 changes: 3 additions & 3 deletions src/server/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import { debuglog } from 'node:util';
import { AsyncLocalStorage } from 'node:async_hooks';
import { ServerCredentials, Server as GRPCServer } from '@grpc/grpc-js';
import { AppCallbackService } from '../../proto/runtime/v1/appcallback_grpc_pb';
import GRPCServerImpl from './GRPCServerImpl';
import PubSub from './PubSub';
import { GRPCServerImpl } from './GRPCServerImpl';
import { PubSub } from './PubSub';

const debug = debuglog('layotto:server:main');

Expand All @@ -27,7 +27,7 @@ export interface ServerOptions {
localStorage?: AsyncLocalStorage<any>;
}

export default class Server {
export class Server {
readonly port: string;
readonly pubsub: PubSub;
protected readonly localStorage?: AsyncLocalStorage<any>;
Expand Down
3 changes: 3 additions & 0 deletions src/server/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './GRPCServerImpl';
export * from './PubSub';
export * from './Server';
17 changes: 17 additions & 0 deletions src/types/Cryption.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { RequestWithMeta } from './common';

export type EncryptRequest = RequestWithMeta<{
plainText: Uint8Array | string;
keyId?: string;
}>;

export type DecryptRequest = RequestWithMeta<{
cipherText: Uint8Array | string;
}>;

export type DecryptResponse = {
plainText: Uint8Array;
keyId: string,
keyVersionId: string,
requestId: string,
};
2 changes: 1 addition & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ export * as PubSub from './PubSub';
export * as File from './File';
export * as Binding from './Binding';
export * as Oss from './Oss';

export * as Cryption from './Cryption';
31 changes: 31 additions & 0 deletions test/unit/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { strict as assert } from 'node:assert';
import {
Clients, Client,
Servers, Server,
} from '../../src';

describe('index.test.ts', () => {
describe('Clients', () => {
it('should export Cryption Class', async () => {
assert(Clients.Cryption);
assert.equal(typeof Clients.Cryption.prototype.encrypt, 'function');
});

it('should export Client Class', async () => {
assert(Clients.Client);
assert.equal(Clients.Client, Client);
});
});

describe('Servers', () => {
it('should export PubSub Class', async () => {
assert(Servers.PubSub);
assert.equal(typeof Clients.PubSub.prototype.publish, 'function');
});

it('should export Server Class', async () => {
assert(Servers.Server);
assert.equal(Servers.Server, Server);
});
});
});
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"compilerOptions": {
"rootDir": "./src",
"outDir": "./dist",
"module": "CommonJS",
"moduleResolution": "Node"
"module": "NodeNext",
"moduleResolution": "NodeNext"
},
"include": [ "src" ],
"exclude": ["node_modules", "**/test/*"]
Expand Down

0 comments on commit f561110

Please sign in to comment.