Skip to content

Commit 7643151

Browse files
authored
Fix encoding during file uploads (#13)
Need to support binary types
1 parent 0500925 commit 7643151

File tree

5 files changed

+16
-4
lines changed

5 files changed

+16
-4
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nightfall-js",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"description": "Build data classification and protection into your application or service with Nightfall.",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/filesScanner.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,15 @@ export class FileScanner extends Base {
5757
// Read the file in chunks
5858
const stream = fs.createReadStream(this.filePath, {
5959
highWaterMark: this.chunkSize,
60-
encoding: 'utf8'
60+
encoding: 'binary'
6161
})
6262

6363
// Upload chunks
6464
let uploadOffset = 0
6565

6666
for await (const chunk of stream) {
67-
await axios.patch(`${this.API_HOST}/v3/upload/${this.fileId}`, chunk, {
67+
let body = Buffer.from(chunk, 'binary')
68+
await axios.patch(`${this.API_HOST}/v3/upload/${this.fileId}`, body, {
6869
headers: {
6970
...this.AXIOS_HEADERS,
7071
'Content-Type': 'application/octet-stream',

src/tests/scanFile.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Nightfall } from '../nightfall'
77
// Mock data
88
const API_HOST = 'https://api.nightfall.ai'
99
const FILE_PATH = path.join(__dirname, "./", "mySecrets.txt");
10+
const IMAGE_FILE_PATH = path.join(__dirname, "./", "test-image.png");
1011
const REQUEST_POLICY: ScanFile.ScanPolicy = {
1112
detectionRuleUUIDs: ['MOCK_RULE_UUID'],
1213
webhookURL: 'https://webhook.com'
@@ -52,6 +53,16 @@ describe('should test the file scanning method', () => {
5253
expect(response.isError).toBe(false)
5354
})
5455

56+
it('test binary upload data', async () => {
57+
const nfClient = new Nightfall()
58+
const scanFileSpy = jest.spyOn(nfClient, 'scanFile')
59+
60+
const response = await nfClient.scanFile(IMAGE_FILE_PATH, REQUEST_POLICY)
61+
expect(scanFileSpy).toHaveBeenCalledWith(IMAGE_FILE_PATH, REQUEST_POLICY)
62+
expect(response.data).toEqual(SCAN_RESPONSE)
63+
expect(response.isError).toBe(false)
64+
})
65+
5566
it('test error path', async () => {
5667
// Reset mocks and force error response
5768
mock.reset()

src/tests/test-image.png

447 Bytes
Loading

0 commit comments

Comments
 (0)