Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions samples/bun-sample/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# 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

package-lock.json
13 changes: 13 additions & 0 deletions samples/bun-sample/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# bun-sample

To install dependencies:

```bash
bun install
```

To run the sample:

```bash
bun run fileName
```
25 changes: 25 additions & 0 deletions samples/bun-sample/bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

135 changes: 135 additions & 0 deletions samples/bun-sample/detect-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import {
Credentials,
DeidentifyFileOptions,
DeidentifyFileRequest,
DeidentifyFileResponse,
Env,
LogLevel,
MaskingMethod,
Skyflow,
SkyflowConfig,
SkyflowError,
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: "<VAULT_ID>", // Unique vault identifier
clusterId: "<CLUSTER_ID>", // 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: NonSharedBuffer = fs.readFileSync("<FILE_PATH>"); // Replace with the path to your file
const file = new File(
[buffer],
"<FILE_PATH>", // 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(['<YOUR_REGEX_PATTERN>']);

// Restrict deidentification to entities matching these regex patterns
// options.setRestrictRegexList(['<YOUR_REGEX_PATTERN>']);

// 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(
"<OUTPUT_DIRECTORY_PATH>", // 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: DeidentifyFileResponse = 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();
106 changes: 106 additions & 0 deletions samples/bun-sample/detect-text.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import {
Credentials,
DeidentifyTextOptions,
DeidentifyTextRequest,
DeidentifyTextResponse,
DetectEntities,
Env,
LogLevel,
Skyflow,
SkyflowConfig,
SkyflowError,
TokenFormat,
TokenType,
Transformations,
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: "<VAULT_ID>", // Unique vault identifier
clusterId: "<CLUSTER_ID>", // 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(['<YOUR_REGEX_PATTERN>']);

// setRestrictRegexList: Restrict deidentification to entities matching these regex patterns
// optionsText.setRestrictRegexList(['<YOUR_REGEX_PATTERN>']);

// 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: DeidentifyTextResponse = 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();
15 changes: 15 additions & 0 deletions samples/bun-sample/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "bun-sample",
"module": "index.ts",
"type": "module",
"private": true,
"dependencies": {
"skyflow-node": "2.1.0-beta.1"
},
"devDependencies": {
"@types/bun": "latest"
},
"peerDependencies": {
"typescript": "^5"
}
}
Loading
Loading