Skip to content
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

ajaxerror not serialized properly #4211

Merged
merged 18 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

- ⚠️ Allow breaking lines in labels before a left parenthesis ([#4138](https://github.com/maplibre/maplibre-gl-js/pull/4138))
- ⚠️ Fix ignoring embedded line breaks when `symbol-placement` is `line` or `line-center` ([#4124](https://github.com/maplibre/maplibre-gl-js/pull/4124))
- Fix AjaxError not properly serialized ([#4024](https://github.com/maplibre/maplibre-gl-js/pull/4211))
- _...Add new stuff here..._

## 4.3.2
Expand Down
2 changes: 1 addition & 1 deletion jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const config: Config = {
projects: [
{
displayName: 'unit',
testEnvironment: 'jsdom',
testEnvironment: './jest.jsdom.environment.ts',
oberhamsi marked this conversation as resolved.
Show resolved Hide resolved
setupFiles: [
'jest-webgl-canvas-mock',
'./test/unit/lib/web_worker_mock.ts'
Expand Down
10 changes: 10 additions & 0 deletions jest.jsdom.environment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import JSDOMEnvironment from 'jest-environment-jsdom';

export default class FixJSDOMEnvironment extends JSDOMEnvironment {
constructor(...args: ConstructorParameters<typeof JSDOMEnvironment>) {
super(...args);

// https://github.com/jsdom/jsdom/issues/3363
this.global.structuredClone = structuredClone;
}
}
43 changes: 33 additions & 10 deletions src/util/web_worker_transfer.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import {AJAXError} from './ajax';
import {register, serialize, deserialize} from './web_worker_transfer';

const mockTransfer = (input, transferables?) => {
const serialized = serialize(input, transferables);
return deserialize(structuredClone(serialized, {transfer: transferables}));
HarelM marked this conversation as resolved.
Show resolved Hide resolved
};

describe('web worker transfer', () => {
test('round trip', () => {
class SerializableMock {
Expand All @@ -11,7 +17,7 @@ describe('web worker transfer', () => {
constructor(n) {
this.n = n;
this.buffer = new ArrayBuffer(100);
this.blob = new Blob();
this.blob = new Blob(['Test'], {type: 'application/text'});
this.squared();
}

Expand All @@ -28,15 +34,20 @@ describe('web worker transfer', () => {

const serializableMock = new SerializableMock(10);
const transferables = [];
const deserialized = deserialize(serialize(serializableMock, transferables)) as SerializableMock;
expect(deserialize(serialize(serializableMock, transferables)) instanceof SerializableMock).toBeTruthy();

const deserialized = mockTransfer(serializableMock, transferables) as SerializableMock;
expect(deserialized instanceof SerializableMock).toBeTruthy();
expect(transferables[0] === serializableMock.buffer).toBeTruthy();
expect(serializableMock.buffer.byteLength).toBe(0);
expect(deserialized.buffer.byteLength).toBe(100);
expect(serializableMock !== deserialized).toBeTruthy();
expect(deserialized.constructor === SerializableMock).toBeTruthy();
expect(deserialized.n === 10).toBeTruthy();
expect(deserialized.buffer === serializableMock.buffer).toBeTruthy();
expect(deserialized.blob === serializableMock.blob).toBeTruthy();
expect(transferables[0] === serializableMock.buffer).toBeTruthy();
expect(serializableMock.blob.size).toBe(4);
// seems to be a problem with jsdom + node. it works in
// node and it works in browsers
// expect(structuredClone(new Blob())).toBeInstanceOf(Blob);
// expect(deserialized.blob.size).toBe(4);

expect(deserialized._cached === undefined).toBeTruthy();
expect(deserialized.squared() === 100).toBeTruthy();
});
Expand All @@ -46,7 +57,7 @@ describe('web worker transfer', () => {
expect(!Klass.name).toBeTruthy();
register('Anon', Klass);
const x = new Klass();
const deserialized = deserialize(serialize(x));
const deserialized = mockTransfer(x);
expect(deserialized instanceof Klass).toBeTruthy();
});

Expand Down Expand Up @@ -75,9 +86,21 @@ describe('web worker transfer', () => {
const customSerialization = new CustomSerialization('a');
expect(!customSerialization._deserialized).toBeTruthy();

const deserialized = deserialize(serialize(customSerialization)) as CustomSerialization;
expect(deserialize(serialize(customSerialization)) instanceof CustomSerialization).toBeTruthy();
const deserialized = mockTransfer(customSerialization) as CustomSerialization;
expect(mockTransfer(customSerialization) instanceof CustomSerialization).toBeTruthy();
expect(deserialized.id).toBe(customSerialization.id);
expect(deserialized._deserialized).toBeTruthy();
});

test('AjaxError serialization', () => {
const status = 404;
const statusText = 'not found';
const url = 'https://example.com';

const ajaxError = new AJAXError(status, statusText, url, new Blob());
const deserialized = mockTransfer(ajaxError) as AJAXError;
expect(deserialized.status).toBe(404);
expect(deserialized.statusText).toBe(statusText);
expect(deserialized.url).toBe(url);
});
});
30 changes: 24 additions & 6 deletions src/util/web_worker_transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,20 @@ function isArrayBuffer(value: any): value is ArrayBuffer {
(value instanceof ArrayBuffer || (value.constructor && value.constructor.name === 'ArrayBuffer'));
}

function isRegistered(input: unknown) {
oberhamsi marked this conversation as resolved.
Show resolved Hide resolved
if (input === null || typeof input !== 'object') {
return false;
}
const klass = (input.constructor as any);
if (klass._classRegistryKey && klass._classRegistryKey !== 'Object') {
return true;
}
if ((<SerializedObject>input).$name) {
oberhamsi marked this conversation as resolved.
Show resolved Hide resolved
return true;
}
return false;
}

/**
* Serialize the given object for transfer to or from a web worker.
*
Expand All @@ -104,7 +118,8 @@ function isArrayBuffer(value: any): value is ArrayBuffer {
* this should happen in the client code, before using serialize().)
*/
export function serialize(input: unknown, transferables?: Array<Transferable> | null): Serialized {
if (input === null ||
if (!isRegistered(input) && (
HarelM marked this conversation as resolved.
Show resolved Hide resolved
input === null ||
input === undefined ||
typeof input === 'boolean' ||
typeof input === 'number' ||
Expand All @@ -115,7 +130,8 @@ export function serialize(input: unknown, transferables?: Array<Transferable> |
input instanceof Date ||
input instanceof RegExp ||
input instanceof Blob ||
input instanceof Error) {
input instanceof Error)
) {
return input;
}

Expand Down Expand Up @@ -206,7 +222,8 @@ export function serialize(input: unknown, transferables?: Array<Transferable> |
}

export function deserialize(input: Serialized): unknown {
if (input === null ||
if (!isRegistered(input) && (
input === null ||
input === undefined ||
typeof input === 'boolean' ||
typeof input === 'number' ||
Expand All @@ -217,11 +234,12 @@ export function deserialize(input: Serialized): unknown {
input instanceof Date ||
input instanceof RegExp ||
input instanceof Blob ||
input instanceof Error ||
isArrayBuffer(input) ||
isImageBitmap(input) ||
ArrayBuffer.isView(input) ||
input instanceof ImageData) {
input instanceof ImageData ||
input instanceof Error
)) {
return input;
}

Expand All @@ -230,7 +248,7 @@ export function deserialize(input: Serialized): unknown {
}

if (typeof input === 'object') {
const name = input.$name || 'Object';
const name = (<SerializedObject>input).$name || 'Object';
if (!registry[name]) {
throw new Error(`can't deserialize unregistered class ${name}`);
}
Expand Down