From 4f019a5bc89a1ee1f9cc58b3128ed72a87c05cab Mon Sep 17 00:00:00 2001 From: raushan-skyflow Date: Tue, 10 Jun 2025 12:30:23 +0530 Subject: [PATCH 1/2] SK-2096 samples for bun as run time environment --- samples/bun-sample/.gitignore | 34 ++++++++ samples/bun-sample/README.md | 13 +++ samples/bun-sample/bun.lock | 25 ++++++ samples/bun-sample/detect-file.ts | 134 ++++++++++++++++++++++++++++++ samples/bun-sample/detect-text.ts | 105 +++++++++++++++++++++++ samples/bun-sample/package.json | 12 +++ samples/bun-sample/upload-file.ts | 95 +++++++++++++++++++++ 7 files changed, 418 insertions(+) create mode 100644 samples/bun-sample/.gitignore create mode 100644 samples/bun-sample/README.md create mode 100644 samples/bun-sample/bun.lock create mode 100644 samples/bun-sample/detect-file.ts create mode 100644 samples/bun-sample/detect-text.ts create mode 100644 samples/bun-sample/package.json create mode 100644 samples/bun-sample/upload-file.ts diff --git a/samples/bun-sample/.gitignore b/samples/bun-sample/.gitignore new file mode 100644 index 0000000..a14702c --- /dev/null +++ b/samples/bun-sample/.gitignore @@ -0,0 +1,34 @@ +# dependencies (bun install) +node_modules + +# output +out +dist +*.tgz + +# code coverage +coverage +*.lcov + +# logs +logs +_.log +report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json + +# dotenv environment variable files +.env +.env.development.local +.env.test.local +.env.production.local +.env.local + +# caches +.eslintcache +.cache +*.tsbuildinfo + +# IntelliJ based IDEs +.idea + +# Finder (MacOS) folder config +.DS_Store diff --git a/samples/bun-sample/README.md b/samples/bun-sample/README.md new file mode 100644 index 0000000..3d326bb --- /dev/null +++ b/samples/bun-sample/README.md @@ -0,0 +1,13 @@ +# bun-sample + +To install dependencies: + +```bash +bun install +``` + +To run the sample: + +```bash +bun run fileName +``` diff --git a/samples/bun-sample/bun.lock b/samples/bun-sample/bun.lock new file mode 100644 index 0000000..7f01d5f --- /dev/null +++ b/samples/bun-sample/bun.lock @@ -0,0 +1,25 @@ +{ + "lockfileVersion": 1, + "workspaces": { + "": { + "name": "bun-sample", + "devDependencies": { + "@types/bun": "latest", + }, + "peerDependencies": { + "typescript": "^5", + }, + }, + }, + "packages": { + "@types/bun": ["@types/bun@1.2.15", "https://prekarilabs.jfrog.io/prekarilabs/api/npm/npm/@types/bun/-/bun-1.2.15.tgz", { "dependencies": { "bun-types": "1.2.15" } }, "sha512-U1ljPdBEphF0nw1MIk0hI7kPg7dFdPyM7EenHsp6W5loNHl7zqy6JQf/RKCgnUn2KDzUpkBwHPnEJEjII594bA=="], + + "@types/node": ["@types/node@24.0.0", "https://prekarilabs.jfrog.io/prekarilabs/api/npm/npm/@types/node/-/node-24.0.0.tgz", { "dependencies": { "undici-types": "~7.8.0" } }, "sha512-yZQa2zm87aRVcqDyH5+4Hv9KYgSdgwX1rFnGvpbzMaC7YAljmhBET93TPiTd3ObwTL+gSpIzPKg5BqVxdCvxKg=="], + + "bun-types": ["bun-types@1.2.15", "https://prekarilabs.jfrog.io/prekarilabs/api/npm/npm/bun-types/-/bun-types-1.2.15.tgz", { "dependencies": { "@types/node": "*" } }, "sha512-NarRIaS+iOaQU1JPfyKhZm4AsUOrwUOqRNHY0XxI8GI8jYxiLXLcdjYMG9UKS+fwWasc1uw1htV9AX24dD+p4w=="], + + "typescript": ["typescript@5.8.3", "https://prekarilabs.jfrog.io/prekarilabs/api/npm/npm/typescript/-/typescript-5.8.3.tgz", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-p1diW6TqL9L07nNxvRMM7hMMw4c5XOo/1ibL4aAIGmSAt9slTE1Xgw5KWuof2uTOvCg9BY7ZRi+GaF+7sfgPeQ=="], + + "undici-types": ["undici-types@7.8.0", "https://prekarilabs.jfrog.io/prekarilabs/api/npm/npm/undici-types/-/undici-types-7.8.0.tgz", {}, "sha512-9UJ2xGDvQ43tYyVMpuHlsgApydB8ZKfVYTsLDhXkFL/6gfkp+U8xTGdh8pMJv1SpZna0zxG1DwsKZsreLbXBxw=="], + } +} diff --git a/samples/bun-sample/detect-file.ts b/samples/bun-sample/detect-file.ts new file mode 100644 index 0000000..c11bc4e --- /dev/null +++ b/samples/bun-sample/detect-file.ts @@ -0,0 +1,134 @@ +import { + DeidentifyFileOptions, + DeidentifyFileRequest, + Env, + LogLevel, + MaskingMethod, + Skyflow, + SkyflowError, + type Credentials, + type SkyflowConfig, + type VaultConfig, +} from "skyflow-node"; +import fs from "fs"; + +/** + * Skyflow Deidentify File Example + * + * This sample demonstrates how to use all available options for deidentifying files. + * Supported file types: images (jpg, png, etc.), pdf, audio (mp3, wav), documents, spreadsheets, presentations, structured text. + * + * Note: File deidentification requires Node.js version 20 or above. + */ + +async function performDeidentifyFile() { + try { + // Step 1: Configure Credentials + const credentials: Credentials = { + path: "path-to-credentials-json", // Path to credentials file + }; + + // Step 2: Configure Vault + const primaryVaultConfig: VaultConfig = { + vaultId: "", // Unique vault identifier + clusterId: "", // From vault URL + env: Env.PROD, // Deployment environment + credentials: credentials, // Authentication method + }; + + // Step 3: Configure Skyflow Client + const skyflowConfig: SkyflowConfig = { + vaultConfigs: [primaryVaultConfig], + logLevel: LogLevel.INFO, // Recommended to use LogLevel.ERROR in production environment. + }; + + // Initialize Skyflow Client + const skyflowClient: Skyflow = new Skyflow(skyflowConfig); + + // Step 4: Prepare Deidentify File Request + // Replace with your file object (e.g., from fs.readFileSync or browser File API) + const buffer = fs.readFileSync(""); // Replace with the path to your file + const file = new File( + [buffer], + "", // Replace with the name of your file + ); + const deidentifyFile = new DeidentifyFileRequest(file); + + // Step 5: Configure DeidentifyFileOptions + const options = new DeidentifyFileOptions(); + + // Entities to detect and deidentify + // options.setEntities([DetectEntities.SSN, DetectEntities.CREDIT_CARD]); + + // Allowlist regex patterns (entities matching these will NOT be deidentified) + // options.setAllowRegexList(['']); + + // Restrict deidentification to entities matching these regex patterns + // options.setRestrictRegexList(['']); + + // Token format for deidentified entities + // const tokenFormat = new TokenFormat(); + // tokenFormat.setDefault(TokenType.ENTITY_ONLY); + // options.setTokenFormat(tokenFormat); + + // Custom transformations for entities + // const transformations = new Transformations(); // Transformations cannot be applied to Documents, Images, or PDFs file formats. + // transformations.setShiftDays({ + // max: 30, + // min: 10, + // entities: [DetectEntities.SSN], + // }); + // options.setTransformations(transformations); + + // Output directory for saving the deidentified file + options.setOutputDirectory( + "", // Replace with your output directory + ); // Replace with your output directory + + // Wait time for response (max 64 seconds) + options.setWaitTime(15); + + // --- Image Options (apply when file is an image) --- + // options.setOutputProcessedImage(true); // Include processed image in output + // options.setOutputOcrText(true); // Include OCR text in response + // options.setMaskingMethod(MaskingMethod.Blackbox); // Masking method for image entities + + // --- PDF Options (apply when file is a PDF) --- + // options.setPixelDensity(1.5); // Pixel density for PDF processing + // options.setMaxResolution(2000); // Max resolution for PDF + + // --- Audio Options (apply when file is audio) --- + // options.setOutputProcessedAudio(true); // Include processed audio in output + // options.setOutputTranscription(DetectOutputTranscription.PLAINTEXT_TRANSCRIPTION); // Type of transcription + + // Bleep audio configuration + // const bleep = new Bleep(); + // bleep.setGain(5); // Loudness in dB + // bleep.setFrequency(1000); // Pitch in Hz + // bleep.setStartPadding(0.1); // Padding at start in seconds + // bleep.setStopPadding(0.2); // Padding at end in seconds + // options.setBleep(bleep); + + // Step 6: Call deidentifyFile API + const response = await skyflowClient + .detect(primaryVaultConfig.vaultId) + .deidentifyFile(deidentifyFile, options); + + // Handle Successful Response + console.log("Deidentify File Response:", response); + } catch (error) { + // Comprehensive Error Handling + if (error instanceof SkyflowError) { + console.error("Skyflow Specific Error:", { + code: error.error?.http_code, + message: error.message, + details: error.error?.details, + }); + } else { + console.error("Unexpected Error:", JSON.stringify(error)); + } + } +} + +// Invoke the deidentify file function +performDeidentifyFile(); \ No newline at end of file diff --git a/samples/bun-sample/detect-text.ts b/samples/bun-sample/detect-text.ts new file mode 100644 index 0000000..fc69e3f --- /dev/null +++ b/samples/bun-sample/detect-text.ts @@ -0,0 +1,105 @@ +import { + DeidentifyTextOptions, + DeidentifyTextRequest, + DetectEntities, + Env, + LogLevel, + Skyflow, + SkyflowError, + TokenFormat, + TokenType, + Transformations, + type Credentials, + type SkyflowConfig, + type VaultConfig, +} from "skyflow-node"; + +/** + * Skyflow Deidentify Text Example + * + * This example demonstrates how to: + * 1. Configure credentials + * 2. Set up vault configuration + * 3. Create a deidentify text request + * 4. Use all available options for deidentification + * 5. Handle response and errors + */ + +async function performDeidentifyText() { + try { + // Step 1: Configure Credentials + const credentials: Credentials = { + path: "path-to-credentials-json", // Path to credentials file + }; + + // Step 2: Configure Vault + const primaryVaultConfig: VaultConfig = { + vaultId: "", // Unique vault identifier + clusterId: "", // From vault URL + env: Env.PROD, // Deployment environment + credentials: credentials, // Authentication method + }; + + // Step 3: Configure Skyflow Client + const skyflowConfig: SkyflowConfig = { + vaultConfigs: [primaryVaultConfig], + logLevel: LogLevel.INFO, // Recommended to use LogLevel.ERROR in production environment. + }; + + // Initialize Skyflow Client + const skyflowClient: Skyflow = new Skyflow(skyflowConfig); + + // Step 4: Prepare Deidentify Text Request + const deidentifyTextRequest = new DeidentifyTextRequest( + "My SSN is 123-45-6789 and my card is 4111 1111 1111 1111.", // Text to be deidentified + ); + + // Step 5: Configure DeidentifyTextOptions + const optionsText = new DeidentifyTextOptions(); + + // setEntities: Specify which entities to deidentify + optionsText.setEntities([DetectEntities.CREDIT_CARD, DetectEntities.SSN]); + + // setAllowRegexList: Allowlist regex patterns (entities matching these will not be deidentified) + // optionsText.setAllowRegexList(['']); + + // setRestrictRegexList: Restrict deidentification to entities matching these regex patterns + // optionsText.setRestrictRegexList(['']); + + // setTokenFormat: Specify the token format for deidentified entities + const tokenFormat = new TokenFormat(); + tokenFormat.setDefault(TokenType.VAULT_TOKEN); + optionsText.setTokenFormat(tokenFormat); + + // setTransformations: Specify custom transformations for entities + const transformations = new Transformations(); + transformations.setShiftDays({ + max: 30, // Maximum shift days + min: 30, // Minimum shift days + entities: [DetectEntities.DOB], // Entities to apply the shift + }); + optionsText.setTransformations(transformations); + + // Step 6: Call deidentifyText API + const response = await skyflowClient + .detect(primaryVaultConfig.vaultId) + .deidentifyText(deidentifyTextRequest, optionsText); + + // Handle Successful Response + console.log("Deidentify Text Response:", response); + } catch (error) { + // Comprehensive Error Handling + if (error instanceof SkyflowError) { + console.error("Skyflow Specific Error:", { + code: error.error?.http_code, + message: error.message, + details: error.error?.details, + }); + } else { + console.error("Unexpected Error:", JSON.stringify(error)); + } + } +} + +// Invoke the deidentify text function +performDeidentifyText(); \ No newline at end of file diff --git a/samples/bun-sample/package.json b/samples/bun-sample/package.json new file mode 100644 index 0000000..2886726 --- /dev/null +++ b/samples/bun-sample/package.json @@ -0,0 +1,12 @@ +{ + "name": "bun-sample", + "module": "index.ts", + "type": "module", + "private": true, + "devDependencies": { + "@types/bun": "latest" + }, + "peerDependencies": { + "typescript": "^5" + } +} diff --git a/samples/bun-sample/upload-file.ts b/samples/bun-sample/upload-file.ts new file mode 100644 index 0000000..b5c6136 --- /dev/null +++ b/samples/bun-sample/upload-file.ts @@ -0,0 +1,95 @@ +import { + Env, + FileUploadOptions, + FileUploadRequest, + FileUploadResponse, + LogLevel, + Skyflow, + SkyflowError, + type Credentials, + type SkyflowConfig, + type VaultConfig, +} from "skyflow-node"; +import * as fs from "fs"; + +/** + * Skyflow File Upload Example + * + * This example demonstrates how to: + * 1. Configure credentials + * 2. Set up vault configuration + * 3. Create a file upload request + * 4. Handle response and errors + * + * Note: File upload requires Node version 20 or above. + */ +async function performFileUpload() { + try { + // Step 1: Configure Credentials + const credentials: Credentials = { + path: "path-to-credentials-json", // Path to credentials file + }; + + // Step 2: Configure Vault + const primaryVaultConfig: VaultConfig = { + vaultId: "", // Unique vault identifier + clusterId: "", // From vault URL + env: Env.PROD, // Deployment environment + credentials: credentials, // Authentication method + }; + + // Step 3: Configure Skyflow Client + const skyflowConfig: SkyflowConfig = { + vaultConfigs: [primaryVaultConfig], + logLevel: LogLevel.INFO, // Recommended to use LogLevel.ERROR in production environment. + }; + + // Initialize Skyflow Client + const skyflowClient: Skyflow = new Skyflow(skyflowConfig); + + // Step 4: Prepare File Upload Data + const tableName = ""; // Table name + const skyflowId = ""; // Skyflow ID of the record + const columnName = ""; // Column name to store file + const filePath = ""; // Path to the file for upload + + // Step 5: Create File Upload Request + const uploadReq: FileUploadRequest = new FileUploadRequest( + tableName, + skyflowId, + columnName, + ); + + // Step 6: Configure FileUpload Options + const uploadOptions: FileUploadOptions = new FileUploadOptions(); + // Set any one of FilePath, Base64 or FileObject in FileUploadOptions + + // uploadOptions.setFilePath(filePath); // Set the file path + // uploadOptions.setBase64('base64-string'); // Set base64 string + // uploadOptions.setFileName('file-name'); // Set the file name when using base64 + const buffer = fs.readFileSync(filePath); + uploadOptions.setFileObject(new File([buffer], filePath)); // Set a File object + + // Step 6: Perform File Upload + const response: FileUploadResponse = await skyflowClient + .vault(primaryVaultConfig.vaultId) + .uploadFile(uploadReq, uploadOptions); + + // Handle Successful Response + console.log("File upload successful:", response); + } catch (error) { + // Comprehensive Error Handling + if (error instanceof SkyflowError) { + console.error("Skyflow Specific Error:", { + code: error.error?.http_code, + message: error.message, + details: error.error?.details, + }); + } else { + console.error("Unexpected Error:", error); + } + } +} + +// Invoke the file upload function +performFileUpload(); \ No newline at end of file From 361947192cac11baf1c3b2939c8621ea2fa8ed7a Mon Sep 17 00:00:00 2001 From: raushan-skyflow Date: Fri, 13 Jun 2025 17:12:23 +0530 Subject: [PATCH 2/2] SK-2096 address the comments --- samples/bun-sample/.gitignore | 2 ++ samples/bun-sample/detect-file.ts | 11 ++++++----- samples/bun-sample/detect-text.ts | 9 +++++---- samples/bun-sample/package.json | 3 +++ samples/bun-sample/upload-file.ts | 16 ++++++++-------- 5 files changed, 24 insertions(+), 17 deletions(-) diff --git a/samples/bun-sample/.gitignore b/samples/bun-sample/.gitignore index a14702c..b3deb60 100644 --- a/samples/bun-sample/.gitignore +++ b/samples/bun-sample/.gitignore @@ -32,3 +32,5 @@ report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json # Finder (MacOS) folder config .DS_Store + +package-lock.json \ No newline at end of file diff --git a/samples/bun-sample/detect-file.ts b/samples/bun-sample/detect-file.ts index c11bc4e..5753829 100644 --- a/samples/bun-sample/detect-file.ts +++ b/samples/bun-sample/detect-file.ts @@ -1,14 +1,15 @@ import { + Credentials, DeidentifyFileOptions, DeidentifyFileRequest, + DeidentifyFileResponse, Env, LogLevel, MaskingMethod, Skyflow, + SkyflowConfig, SkyflowError, - type Credentials, - type SkyflowConfig, - type VaultConfig, + VaultConfig, } from "skyflow-node"; import fs from "fs"; @@ -47,7 +48,7 @@ async function performDeidentifyFile() { // Step 4: Prepare Deidentify File Request // Replace with your file object (e.g., from fs.readFileSync or browser File API) - const buffer = fs.readFileSync(""); // Replace with the path to your file + const buffer: NonSharedBuffer = fs.readFileSync(""); // Replace with the path to your file const file = new File( [buffer], "", // Replace with the name of your file @@ -110,7 +111,7 @@ async function performDeidentifyFile() { // options.setBleep(bleep); // Step 6: Call deidentifyFile API - const response = await skyflowClient + const response: DeidentifyFileResponse = await skyflowClient .detect(primaryVaultConfig.vaultId) .deidentifyFile(deidentifyFile, options); diff --git a/samples/bun-sample/detect-text.ts b/samples/bun-sample/detect-text.ts index fc69e3f..729483b 100644 --- a/samples/bun-sample/detect-text.ts +++ b/samples/bun-sample/detect-text.ts @@ -1,17 +1,18 @@ import { + Credentials, DeidentifyTextOptions, DeidentifyTextRequest, + DeidentifyTextResponse, DetectEntities, Env, LogLevel, Skyflow, + SkyflowConfig, SkyflowError, TokenFormat, TokenType, Transformations, - type Credentials, - type SkyflowConfig, - type VaultConfig, + VaultConfig, } from "skyflow-node"; /** @@ -81,7 +82,7 @@ async function performDeidentifyText() { optionsText.setTransformations(transformations); // Step 6: Call deidentifyText API - const response = await skyflowClient + const response: DeidentifyTextResponse = await skyflowClient .detect(primaryVaultConfig.vaultId) .deidentifyText(deidentifyTextRequest, optionsText); diff --git a/samples/bun-sample/package.json b/samples/bun-sample/package.json index 2886726..0e8e287 100644 --- a/samples/bun-sample/package.json +++ b/samples/bun-sample/package.json @@ -3,6 +3,9 @@ "module": "index.ts", "type": "module", "private": true, + "dependencies": { + "skyflow-node": "2.1.0-beta.1" + }, "devDependencies": { "@types/bun": "latest" }, diff --git a/samples/bun-sample/upload-file.ts b/samples/bun-sample/upload-file.ts index b5c6136..8f7c4e9 100644 --- a/samples/bun-sample/upload-file.ts +++ b/samples/bun-sample/upload-file.ts @@ -1,14 +1,14 @@ import { + Credentials, Env, FileUploadOptions, FileUploadRequest, FileUploadResponse, LogLevel, Skyflow, + SkyflowConfig, SkyflowError, - type Credentials, - type SkyflowConfig, - type VaultConfig, + VaultConfig, } from "skyflow-node"; import * as fs from "fs"; @@ -48,10 +48,10 @@ async function performFileUpload() { const skyflowClient: Skyflow = new Skyflow(skyflowConfig); // Step 4: Prepare File Upload Data - const tableName = ""; // Table name - const skyflowId = ""; // Skyflow ID of the record - const columnName = ""; // Column name to store file - const filePath = ""; // Path to the file for upload + const tableName: string = ""; // Table name + const skyflowId: string = ""; // Skyflow ID of the record + const columnName: string = ""; // Column name to store file + const filePath: string = ""; // Path to the file for upload // Step 5: Create File Upload Request const uploadReq: FileUploadRequest = new FileUploadRequest( @@ -67,7 +67,7 @@ async function performFileUpload() { // uploadOptions.setFilePath(filePath); // Set the file path // uploadOptions.setBase64('base64-string'); // Set base64 string // uploadOptions.setFileName('file-name'); // Set the file name when using base64 - const buffer = fs.readFileSync(filePath); + const buffer: NonSharedBuffer = fs.readFileSync(filePath); uploadOptions.setFileObject(new File([buffer], filePath)); // Set a File object // Step 6: Perform File Upload