Skip to content

Commit

Permalink
add JSDoc
Browse files Browse the repository at this point in the history
  • Loading branch information
tasshi-me committed Oct 28, 2024
1 parent 50a3e72 commit 96c0995
Show file tree
Hide file tree
Showing 13 changed files with 105 additions and 31 deletions.
10 changes: 10 additions & 0 deletions src/plugin/core/contents/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,18 @@ import type { DriverInterface } from "../driver";
import { ZipFileDriver } from "../driver";
import { createContentsZip } from "./zip";

/**
* Contents represents the plugin source files aggregated by Manifest
*/
export interface ContentsInterface extends DriverInterface {
/**
* returns Manifest
*/
manifest(): Promise<ManifestInterface>;

/**
* validate contents
*/
validate(): Promise<void>;
}

Expand Down
32 changes: 18 additions & 14 deletions src/plugin/core/crypto/private-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,28 @@ import RSA from "node-rsa";
import { uuid } from "./uuid";
import { sign } from "./sign";

/**
* PrivateKey represents private key to pack, sign, and verify plugin
*/
export interface PrivateKeyInterface {
/**
* Export private key file
* @return {string} ppk formated private key
*/
exportPrivateKey(): string;
/**
* Export public key file
* @returns {string} der formated public key
*/
exportPublicKey(): Buffer;
/**
* Generate UUID for this key
*/
uuid(): string;
/**
* Generate signature for the contents
* @param contents {Buffer}
*/
sign(contents: Buffer): Buffer;
}

Expand Down Expand Up @@ -35,32 +53,18 @@ export class PrivateKey implements PrivateKeyInterface {
return new PrivateKey(new RSA(key));
}

/**
* Export private key
* @return {string} ppk format string
*/
public exportPrivateKey(): string {
return this.key.exportKey("pkcs1-private") + "\n";
}

/**
* Export public key
*/
public exportPublicKey(): Buffer {
return this.key.exportKey("pkcs8-public-der");
}

/**
* Generate UUID for this key
*/
public uuid(): string {
return uuid(this.exportPublicKey());
}

/**
* Generate signature for the contents
* @param contents {Buffer}
*/
public sign(contents: Buffer): Buffer {
return sign(contents, this.exportPrivateKey());
}
Expand Down
19 changes: 13 additions & 6 deletions src/plugin/core/crypto/public-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,21 @@ import { uuid } from "./uuid";
import crypto from "crypto";

export interface PublicKeyInterface {
/**
* Export public key file
* @returns {string} der formated public key
*/
exportPublicKey(): Buffer;
/**
* Generate UUID for this key
*/
uuid(): string;

/**
* Verify data with signature
* @param data
* @param signature
*/
verify(data: Buffer, signature: Buffer): boolean;
}

Expand Down Expand Up @@ -34,16 +47,10 @@ export class PublicKey implements PublicKeyInterface {
return new PublicKey(rsa);
}

/**
* Export public key
*/
public exportPublicKey(): Buffer {
return this.key.exportKey("pkcs8-public-der");
}

/**
* Generate UUID for this key
*/
public uuid(): string {
return uuid(this.exportPublicKey());
}
Expand Down
27 changes: 27 additions & 0 deletions src/plugin/core/driver/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,40 @@ export type FileStats = {
size: number;
};

/**
* Driver represents filesystem like a local disc, zip file, memory, etc.
*/
export interface DriverInterface {
/**
* Returns files in the filesystem
*/
fileList(): Promise<string[]>;

/**
* Returns file information
* @param fileName
*/
stat(fileName: string): Promise<FileStats>;
// TODO: Delete this function after https://github.com/kintone/js-sdk/pull/3037 merged
statSync(fileName: string): FileStats;

/**
* Read the entire contents of a file.
* @param fileName
*/
readFile(fileName: string): Promise<Buffer>;
readFile(fileName: string, encoding: "utf-8"): Promise<string>;

/**
* Create readable stream to read a file contents.
* @param fileName
*/
openReadStream(fileName: string): Promise<Readable>;

/**
* Write the entire contents to a file.
* @param fileName
* @param data
*/
writeFile(fileName: string, data: string | Buffer): Promise<void>;
}
3 changes: 3 additions & 0 deletions src/plugin/core/driver/local-fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { statSync, createReadStream } from "fs";
import path from "path";
import type { Readable } from "node:stream";

/**
* LocalFSDriver is a Driver for the local disc
*/
export class LocalFSDriver implements DriverInterface {
private currentDir: string;

Expand Down
3 changes: 3 additions & 0 deletions src/plugin/core/driver/zip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import consumers from "node:stream/consumers";

type Entries = Map<string, yauzl.Entry>;

/**
* ZipFileDriver is a Driver for a zip file
*/
export class ZipFileDriver implements DriverInterface {
private readonly _buffer: Buffer;

Expand Down
6 changes: 3 additions & 3 deletions src/plugin/core/manifest/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import type { ManifestInterface, ManifestStaticInterface } from "./interface";
import { ManifestV1 } from "./v1";
import { ManifestV2 } from "./v2";

/**
* Factory to instantiate Manifest
*/
export class ManifestFactory {
private constructor() {
/* noop */
Expand Down Expand Up @@ -35,9 +38,6 @@ export class ManifestFactory {
}
}

/**
* Load JSON file without caching
*/
public static async loadJsonFile(
jsonFilePath: string,
driver?: DriverInterface,
Expand Down
23 changes: 23 additions & 0 deletions src/plugin/core/manifest/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,36 @@ import type { ContentsZip } from "../contents";
import type { ValidationResult } from "./validate";

export interface ManifestStaticInterface {
/**
* Parse JSON object to Manifest
* @param manifestJson
*/
parseJson(manifestJson: string): ManifestInterface;
/**
* Load and parse JSON file to Manifest
*/
loadJsonFile(
jsonFilePath: string,
driver?: DriverInterface,
): Promise<ManifestInterface>;
}

export interface ManifestInterface {
/**
* Validate Manifest
* @param driver If set, also validate file existence and file size.
*/
validate(driver?: DriverInterface): Promise<ValidationResult>;

/**
* Returns files described in a Manifest
*/
sourceList(): string[];

/**
* Generate contents.zip from Manifest and Driver
* @param driver
*/
generateContentsZip(driver: DriverInterface): Promise<ContentsZip>;

// Accessor
Expand All @@ -22,5 +42,8 @@ export interface ManifestInterface {
get description(): string | undefined;
get homepageUrl(): string | undefined;

/**
* Returns JSON object represents Manifest raw object.
*/
get json(): object;
}
3 changes: 0 additions & 3 deletions src/plugin/core/manifest/v1/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ export class ManifestV1 implements ManifestInterface {
return new ManifestV1(JSON.parse(manifestJson));
}

/**
* Load JSON file without caching
*/
public static async loadJsonFile(
jsonFilePath: string,
driver?: DriverInterface,
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/core/manifest/v1/sourcelist.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ManifestV1JsonObject } from "./index";

/**
* Create content file list from manifest.json
* Create contents file list from manifest.json
*/
export const sourceList = (manifest: ManifestV1JsonObject): string[] => {
const list = ([] as string[]).concat(
Expand Down
3 changes: 0 additions & 3 deletions src/plugin/core/manifest/v2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ export class ManifestV2 implements ManifestInterface {
return new ManifestV2(JSON.parse(manifestJson));
}

/**
* Load JSON file without caching
*/
public static async loadJsonFile(
jsonFilePath: string,
driver?: DriverInterface,
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/core/manifest/v2/sourcelist.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ManifestV2JsonObject } from "./index";

/**
* Create content file list from manifest.json
* Create contents file list from manifest.json
*/
export const sourceListV2 = (manifest: ManifestV2JsonObject): string[] => {
const list: string[] = [];
Expand Down
3 changes: 3 additions & 0 deletions src/plugin/core/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import type { ManifestInterface } from "../manifest";
import consumers from "node:stream/consumers";
import { logger } from "../../../utils/log";

/**
* Plugin represents plugin includes Contents, public key, and signature.
*/
export interface PluginInterface extends DriverInterface {}

export class PluginZip extends ZipFileDriver implements PluginInterface {
Expand Down

0 comments on commit 96c0995

Please sign in to comment.