-
-
Notifications
You must be signed in to change notification settings - Fork 416
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Fixed image rendering issue in Chatwoot
* refactor: wrap the buffer to stream convert using promise for async control * refactor: add error reject in promise wrapper at buffer to stream method * refactor: refact stream reader * chore: add mocha for tests * Revert "chore: add mocha for tests" This reverts commit 90dee67. * chore: add mocha for tests and add test script * chore: add mocha types * chore: config mocha types in tscondfig * chore!: enable isolatedModules for test modules in isolation * chore: transpile only test files with ts-node for no type checks * chore: setup jest * feat: implement buffer utils to work with buffers * test: ensure that buffer utils will work successfully * refactor: implement AsyncBufferToStream into chatwoot attachments field * refactor: remove test logs * chore!: remove no used dep buffer-to-stream * refactor: Change the bufferUtils filename * fix: unable isolated modules * refactor: change the buffer utils to default exportation * fix: change the file import path for buffer utils. * fix: add event listen to monitore buffer to stream conversion * refactor: change the stream aproach in buffer conversion * refactor: add test to webhook * chore: add pre-push command to husky * fix: remove debug url * refactor: refact the send message attachments to chatwoot * refactor: remove no used variables * refactor: remove debug stuff * chore: add lint check in pre-push hook --------- Co-authored-by: netoeymard <eymard@sysautomacao.com.br> Co-authored-by: Cleiton Carvalho <cleitoncosta83@gmail.com>
- Loading branch information
1 parent
f3ca926
commit ff6df6c
Showing
7 changed files
with
245 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/usr/bin/env sh | ||
. "$(dirname -- "$0")/_/husky.sh" | ||
|
||
yarn test && yarn lint |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/** @type {import('ts-jest').JestConfigWithTsJest} */ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { Readable } from 'stream'; | ||
|
||
import bufferUtils from '../../util/bufferutils'; | ||
|
||
function generateRandomData(length: number): string { | ||
const characters = | ||
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | ||
let randomData = ''; | ||
for (let i = 0; i < length; i++) { | ||
const randomIndex = Math.floor(Math.random() * characters.length); | ||
randomData += characters.charAt(randomIndex); | ||
} | ||
return randomData; | ||
} | ||
|
||
describe('Utils Functions', function () { | ||
describe('Buffer to Stream', function () { | ||
const bodyToBuffer = generateRandomData(100); | ||
const buffer = Buffer.from(bodyToBuffer, 'utf-8'); | ||
|
||
it('Should transform the Buffer in a Readable Stream', function () { | ||
const bufferStream = bufferUtils.bufferToReadableStream(buffer); | ||
|
||
// Assert that the bufferStream is a Readable stream | ||
expect(bufferStream).toBeInstanceOf(Readable); | ||
}); | ||
|
||
it('Should, on data end, checks if the Stream are correct', function () { | ||
const bufferStream = bufferUtils.bufferToReadableStream(buffer); | ||
|
||
let data = ''; | ||
|
||
bufferStream.on('data', (chunck) => { | ||
data += chunck.toString('utf-8'); | ||
}); | ||
|
||
bufferStream.on('end', () => { | ||
expect(data).toStrictEqual(bodyToBuffer); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('Async Buffer to Stream', function () { | ||
const bodyToBuffer = generateRandomData(10000000); | ||
const buffer = Buffer.from(bodyToBuffer, 'utf-8'); | ||
|
||
it('Should await the Buffer convertion and return a instance of readable', async function () { | ||
const bufferStream = await bufferUtils.AsyncBufferToStream(buffer); | ||
|
||
expect(bufferStream).toBeInstanceOf(Readable); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2021 WPPConnect Team | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* 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 { Readable } from 'stream'; | ||
|
||
// type AsyncBufferToStream | ||
|
||
function bufferToReadableStream(buffer: Buffer): Readable { | ||
const readableInstanceStream = new Readable({ | ||
read() { | ||
this.push(buffer); | ||
this.push(null); | ||
}, | ||
}); | ||
|
||
return readableInstanceStream; | ||
} | ||
|
||
async function AsyncBufferToStream(buffer: Buffer): Promise<Readable> { | ||
return new Promise((resolve, reject) => { | ||
const bufferStream = bufferToReadableStream(buffer); | ||
bufferStream.on('data', () => { | ||
// data = chunck; | ||
}); | ||
|
||
bufferStream.on('end', () => { | ||
resolve(bufferStream); | ||
}); | ||
|
||
bufferStream.on('error', (error) => { | ||
reject(error); | ||
}); | ||
}); | ||
} | ||
|
||
export default { bufferToReadableStream, AsyncBufferToStream }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.