Skip to content
Merged
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
3 changes: 2 additions & 1 deletion packages/core/http/http-request-internal/index.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Headers, HttpResponse, HttpRequestOptions } from '../../http';
import { Screen } from '../../platform/screen';
import * as domainDebugger from '../../debugger';
import { isObject } from '../../utils';
import { runtimeCopiesTypedArraysToByteArrays } from '../../utils/android-runtime-version';
import { HttpResponseEncoding } from '../http-interfaces';
import { addHeader, getDebugRequestMethodAndBody } from './http-request-internal-common';
export { addHeader } from './http-request-internal-common';
Expand Down Expand Up @@ -163,7 +164,7 @@ function buildJavaOptions(options: HttpRequestOptions) {
javaOptions.content = nativeBuffer;
} else if (options.content instanceof ArrayBuffer) {
const typedArray = new Uint8Array(options.content as ArrayBuffer);
const nativeBuffer = java.nio.ByteBuffer.wrap(Array.from(typedArray));
const nativeBuffer = java.nio.ByteBuffer.wrap(runtimeCopiesTypedArraysToByteArrays() ? (typedArray as any) : Array.from(typedArray));
javaOptions.content = nativeBuffer;
}
if (typeof options.timeout === 'number') {
Expand Down
7 changes: 6 additions & 1 deletion packages/core/http/http-request/index.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import { getFilenameFromUrl, parseJSON } from './http-request-common';

const contentHandler: HttpContentHandler = {
toArrayBuffer(this: BaseHttpContent) {
return Uint8Array.from((this.raw as java.io.ByteArrayOutputStream).toByteArray()).buffer;
const bytes = (this.raw as java.io.ByteArrayOutputStream).toByteArray();
const buffer: ArrayBuffer = (ArrayBuffer as any).from(java.nio.ByteBuffer.wrap(bytes));
// Unlink the drained source ByteBuffer: it pins the byte[] and misleads FileSystemAccess.getBuffer.
delete (buffer as any).nativeObject;

return buffer;
},
toString(this: BaseHttpContent, encoding?: HttpResponseEncoding) {
let str: string;
Expand Down
34 changes: 34 additions & 0 deletions packages/core/utils/android-runtime-version.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { describe, it, expect } from 'vitest';
import { isVersionAtLeast } from './android-runtime-version';

describe('isVersionAtLeast', () => {
const atLeast912 = (version: unknown) => isVersionAtLeast(version, 9, 1, 2);

it('is false for values that are not version strings', () => {
expect(atLeast912(undefined)).toBe(false);
expect(atLeast912(null)).toBe(false);
expect(atLeast912(913)).toBe(false);
expect(atLeast912('')).toBe(false);
expect(atLeast912('garbage')).toBe(false);
expect(atLeast912('9.1')).toBe(false);
expect(atLeast912('v9.2.0')).toBe(false);
expect(atLeast912('9.2.0 ')).toBe(false);
expect(atLeast912('9.2.0-')).toBe(false);
});

it('is false for prereleases of the target release and older versions', () => {
expect(atLeast912('9.1.1')).toBe(false);
expect(atLeast912('9.1.2-next.2026-11-11-1')).toBe(false);
expect(atLeast912('9.0.10')).toBe(false);
expect(atLeast912('8.99.99')).toBe(false);
});

it('is true for the target release, newer versions and their prereleases', () => {
expect(atLeast912('9.1.2')).toBe(true);
expect(atLeast912('9.1.3')).toBe(true);
expect(atLeast912('9.1.3-alpha.1')).toBe(true);
expect(atLeast912('9.2.0')).toBe(true);
expect(atLeast912('9.10.0')).toBe(true);
expect(atLeast912('10.0.0')).toBe(true);
});
});
40 changes: 40 additions & 0 deletions packages/core/utils/android-runtime-version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const semverPattern = /^(\d+)\.(\d+)\.(\d+)(-[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/;

/**
* Whether a `major.minor.patch[-prerelease]` version string is at or above the given release.
* Prereleases of the release itself are lower than it, as in semver.
* @param version Version to test; anything that is not a parsable version string yields false.
* @returns True only for a parsable version at or above `major.minor.patch`.
*/
export function isVersionAtLeast(version: unknown, major: number, minor: number, patch: number): boolean {
if (typeof version !== 'string') {
return false;
}
const match = semverPattern.exec(version);
if (!match) {
return false;
}
const parts = [Number(match[1]), Number(match[2]), Number(match[3])];
const target = [major, minor, patch];
for (let i = 0; i < parts.length; i++) {
if (parts[i] !== target[i]) {
return parts[i] > target[i];
}
}

return match[4] === undefined;
}

let typedArrayToByteArray: boolean | undefined;

/**
* Whether the Android runtime copies a typed array passed to a Java `byte[]` parameter.
* Older runtimes mis-pass it, so this is gated on the version rather than probed.
*/
export function runtimeCopiesTypedArraysToByteArrays(): boolean {
if (typedArrayToByteArray === undefined) {
typedArrayToByteArray = isVersionAtLeast((global as any).__runtimeVersion, 9, 1, 2);
}

return typedArrayToByteArray;
}
Loading