Skip to content

Commit

Permalink
Reviewer comments round 2
Browse files Browse the repository at this point in the history
  • Loading branch information
anniel-stripe committed Jan 11, 2023
1 parent c6fbd2a commit 6eb1140
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 12 deletions.
5 changes: 2 additions & 3 deletions lib/Webhooks.js

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

8 changes: 4 additions & 4 deletions src/Error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@ class StripeConnectionError extends StripeError {}
* webhook fails
*/
class StripeSignatureVerificationError extends StripeError {
header: string | Buffer;
payload: string | Buffer;
header: string | Uint8Array;
payload: string | Uint8Array;

constructor(
header: string | Buffer,
payload: string | Buffer,
header: string | Uint8Array,
payload: string | Uint8Array,
raw: StripeRawError = {}
) {
super(raw);
Expand Down
5 changes: 2 additions & 3 deletions src/Webhooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const Webhook: WebhookObject = {

const jsonPayload =
payload instanceof Uint8Array
? JSON.parse(new TextDecoder().decode(payload))
? JSON.parse(new TextDecoder('utf8').decode(payload))
: JSON.parse(payload);
return jsonPayload;
},
Expand All @@ -100,10 +100,9 @@ const Webhook: WebhookObject = {
cryptoProvider
);

// @ts-ignore
const jsonPayload =
payload instanceof Uint8Array
? JSON.parse(new TextDecoder().decode(payload))
? JSON.parse(new TextDecoder('utf8').decode(payload))
: JSON.parse(payload);
return jsonPayload;
},
Expand Down
4 changes: 2 additions & 2 deletions test/Webhook.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ describe('Webhooks', () => {

const textEncoder = new TextEncoder();
const event = await constructEventFn(
new Uint8Array(textEncoder.encode(EVENT_PAYLOAD_STRING)),
new Uint8Array(textEncoder.encode(header)),
textEncoder.encode(EVENT_PAYLOAD_STRING),
textEncoder.encode(header),
SECRET
);

Expand Down
21 changes: 21 additions & 0 deletions test/flows.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,27 @@ describe('Flows', function() {
).to.eventually.have.nested.property('size', 739);
});

it('Allows you to upload a file synchronously as a Uint8Array', () => {
const testFilename = path.join(__dirname, 'resources/data/minimal.pdf');
const buf = fs.readFileSync(testFilename);

// Create Uint8Array from Buffer
const f = new Uint8Array(buf.buffer, buf.byteOffset, buf.length);

return expect(
stripe.files
.create({
purpose: 'dispute_evidence',
file: {
data: f,
name: 'minimal.pdf',
type: 'application/octet-stream',
},
})
.then(null, (error) => error)
).to.eventually.have.nested.property('size', 739);
});

it('Surfaces stream errors correctly', (done) => {
const mockedStream = new stream.Readable();
mockedStream._read = () => {};
Expand Down
22 changes: 22 additions & 0 deletions test/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,28 @@ describe('utils', () => {
// whether that's useful is a race between using jest/sinon for these tests and dropping support for node < 14
});
});

describe('concat', () => {
it('should return a joined Uint8Array', () => {
const arr1 = new Uint8Array([1, 2, 3]);
const arr2 = new Uint8Array([4, 5]);
const arrs = [arr1, arr2];
expect(utils.concat(arrs)).to.eql(new Uint8Array([1, 2, 3, 4, 5]));
});

it('should return an empty Uint8Array given an empty array', () => {
expect(utils.concat([])).to.eql(new Uint8Array(0));
});

it('should work with Buffers', () => {
const buf1 = Buffer.from('foo');
const buf2 = Buffer.from('bar');
const mergedBufToString = new TextDecoder('utf8').decode(
utils.concat([buf1, buf2])
);
expect(mergedBufToString).to.equal('foobar');
});
});
});

function handleWarnings(doWithShimmedConsoleWarn, onWarn) {
Expand Down

0 comments on commit 6eb1140

Please sign in to comment.