Skip to content

feat: Enable Parse.idempotency by default #2164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
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
5 changes: 5 additions & 0 deletions integration/test/IdempotencyTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@ function DuplicateXHR(requestId) {

describe('Idempotency', () => {
beforeEach(() => {
Parse.idempotency = false;
RESTController._setXHR(XHR);
});

afterEach(() => {
Parse.idempotency = true;
});

it('handle duplicate cloud code function request', async () => {
RESTController._setXHR(DuplicateXHR('1234'));
await Parse.Cloud.run('CloudFunctionIdempotency');
Expand Down
2 changes: 1 addition & 1 deletion src/CoreManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ const config: Config & { [key: string]: any } = {
PERFORM_USER_REWRITE: true,
FORCE_REVOCABLE_SESSION: false,
ENCRYPTED_USER: false,
IDEMPOTENCY: false,
IDEMPOTENCY: true,
ALLOW_CUSTOM_OBJECT_ID: false,
PARSE_ERRORS: [],
};
Expand Down
5 changes: 2 additions & 3 deletions src/RESTController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ const RESTController = {
return ajaxIE9(method, url, data, headers, options);
}
const promise = resolvingPromise();
const isIdempotent = CoreManager.get('IDEMPOTENCY') && ['POST', 'PUT'].includes(method);
const requestId = isIdempotent ? uuidv4() : '';
const requestId = CoreManager.get('IDEMPOTENCY') ? uuidv4() : '';
let attempts = 0;

const dispatch = function () {
Expand Down Expand Up @@ -171,7 +170,7 @@ const RESTController = {
headers['User-Agent'] =
'Parse/' + CoreManager.get('VERSION') + ' (NodeJS ' + process.versions.node + ')';
}
if (isIdempotent) {
if (requestId) {
headers['X-Parse-Request-Id'] = requestId;
}
if (CoreManager.get('SERVER_AUTH_TYPE') && CoreManager.get('SERVER_AUTH_TOKEN')) {
Expand Down
6 changes: 3 additions & 3 deletions src/__tests__/Parse-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ describe('Parse module', () => {
});

it('can set idempotency', () => {
expect(Parse.idempotency).toBe(false);
Parse.idempotency = true;
expect(CoreManager.get('IDEMPOTENCY')).toBe(true);
expect(Parse.idempotency).toBe(true);
Parse.idempotency = false;
expect(CoreManager.get('IDEMPOTENCY')).toBe(false);
expect(Parse.idempotency).toBe(false);
Parse.idempotency = true;
expect(Parse.idempotency).toBe(true);
});

it('can set LocalDatastoreController', () => {
Expand Down
39 changes: 10 additions & 29 deletions src/__tests__/RESTController-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,6 @@ describe('RESTController', () => {
});

it('idempotency - sends requestId header', async () => {
CoreManager.set('IDEMPOTENCY', true);
const requestIdHeader = header => 'X-Parse-Request-Id' === header[0];
const xhr = {
setRequestHeader: jest.fn(),
Expand All @@ -364,21 +363,19 @@ describe('RESTController', () => {
});
RESTController.request('POST', 'classes/MyObject', {}, {});
await flushPromises();
expect(xhr.setRequestHeader.mock.calls.filter(requestIdHeader)).toEqual([
['X-Parse-Request-Id', '1000'],
]);
const [header, requestId] = xhr.setRequestHeader.mock.calls.filter(requestIdHeader)[0];
expect(header).toBe('X-Parse-Request-Id');
expect(requestId).toBeDefined();
xhr.setRequestHeader.mockClear();

RESTController.request('PUT', 'classes/MyObject', {}, {});
await flushPromises();
expect(xhr.setRequestHeader.mock.calls.filter(requestIdHeader)).toEqual([
['X-Parse-Request-Id', '1001'],
]);
CoreManager.set('IDEMPOTENCY', false);
const [nextHeader, nextRequestId] = xhr.setRequestHeader.mock.calls.filter(requestIdHeader)[0];
expect(nextHeader).toBe('X-Parse-Request-Id');
expect(nextRequestId).toBe((Number(requestId) + 1).toString());
});

it('idempotency - handle requestId on network retries', done => {
CoreManager.set('IDEMPOTENCY', true);
RESTController._setXHR(
mockXHR([{ status: 500 }, { status: 500 }, { status: 200, response: { success: true } }])
);
Expand All @@ -394,25 +391,6 @@ describe('RESTController', () => {
done();
});
jest.runAllTimers();
CoreManager.set('IDEMPOTENCY', false);
});

it('idempotency - should properly handle url method not POST / PUT', () => {
CoreManager.set('IDEMPOTENCY', true);
const xhr = {
setRequestHeader: jest.fn(),
open: jest.fn(),
send: jest.fn(),
};
RESTController._setXHR(function () {
return xhr;
});
RESTController.ajax('GET', 'users/me', {}, {});
const requestIdHeaders = xhr.setRequestHeader.mock.calls.filter(
header => 'X-Parse-Request-Id' === header[0]
);
expect(requestIdHeaders.length).toBe(0);
CoreManager.set('IDEMPOTENCY', false);
});

it('handles aborted requests', done => {
Expand Down Expand Up @@ -685,7 +663,10 @@ describe('RESTController', () => {
return xhr;
});
RESTController.ajax('GET', 'users/me', {}, { 'X-Parse-Session-Token': '123' });
expect(xhr.setRequestHeader.mock.calls[3]).toEqual(['Cache-Control', 'max-age=3600']);
const cacheHeader = header => 'Cache-Control' === header[0];
const [header, value] = xhr.setRequestHeader.mock.calls.filter(cacheHeader)[0];
expect(header).toBe('Cache-Control');
expect(value).toBe('max-age=3600');
expect(xhr.open.mock.calls[0]).toEqual(['GET', 'users/me', true]);
expect(xhr.send.mock.calls[0][0]).toEqual({});
CoreManager.set('REQUEST_HEADERS', {});
Expand Down
Loading