Skip to content
Open
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
52 changes: 52 additions & 0 deletions benchmark/esm/data-url-processor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Benchmarks dataURLProcessor(), which decodes the body of `data:` URLs used
// by import() and Worker. `plain` and `base64` bodies contain no '%' and so
// take the unescaped path; `percent` bodies are percent-decoded, and
// `percent-tail` bodies have a single escape at the very end.
'use strict';

const assert = require('node:assert');
const common = require('../common.js');

const bench = common.createBenchmark(main, {
n: [1e4],
size: [64, 1024, 16384, 65536],
encoding: ['plain', 'percent', 'percent-tail', 'percent-mid', 'percent-at-64', 'base64'],
}, { flags: ['--expose-internals'] });

function main({ n, size, encoding }) {
const { dataURLProcessor } = require('internal/data_url');
let source = `export default '${'a'.repeat(size)}';`;
let body = source;
let mime = 'text/javascript';
if (encoding === 'percent') {
body = encodeURIComponent(source);
} else if (encoding === 'percent-tail') {
body = `${source}%0A`;
source += '\n';
} else if (encoding === 'percent-mid') {
const half = source.length >> 1;
body = `${source.slice(0, half)}%0A${source.slice(half)}`;
source = `${source.slice(0, half)}\n${source.slice(half)}`;
} else if (encoding === 'percent-at-64') {
body = `${'x'.repeat(64)}${encodeURIComponent(source)}`;
source = `${'x'.repeat(64)}${source}`;
} else if (encoding === 'base64') {
body = Buffer.from(source).toString('base64');
mime += ';base64';
}
const url = new URL(`data:${mime},${body}`);
let result;

for (let i = 0; i < 1000; i++) {
dataURLProcessor(url);
}

bench.start();
for (let i = 0; i < n; i++) {
result = dataURLProcessor(url);
}
bench.end(n);

assert.strictEqual(result.mimeType.essence, 'text/javascript');
assert.deepStrictEqual(result.body, new TextEncoder().encode(source));
}
35 changes: 26 additions & 9 deletions lib/internal/data_url.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {
StringPrototypeCharCodeAt,
StringPrototypeIndexOf,
StringPrototypeSlice,
TypedArrayPrototypeSet,
TypedArrayPrototypeSubarray,
Uint8Array,
} = primordials;
Expand Down Expand Up @@ -178,8 +179,13 @@ function stringPercentDecode(input) {
// 1. Let bytes be the UTF-8 encoding of input.
const bytes = lazyEncoder().encode(input);

const firstPercent = StringPrototypeIndexOf(input, '%');
if (firstPercent === -1) {
return bytes;
}

// 2. Return the percent-decoding of bytes.
return percentDecode(bytes);
return percentDecode(bytes, bytes.length === input.length ? firstPercent : 0);
}

/**
Expand Down Expand Up @@ -209,21 +215,31 @@ function hexByteToNumber(byte) {
// https://url.spec.whatwg.org/#percent-decode
/**
* @param {Uint8Array} input
* @param {number} [start] Index of the first 0x25 (%) byte, if known.
* @returns {Uint8Array}
*/
function percentDecode(input) {
function percentDecode(input, start = 0) {
const length = input.length;
// 1. Let output be an empty byte sequence.
/** @type {Uint8Array} */
const output = new Uint8Array(length);
let src = input;
let dst = output;
const offset = start > 128 ? start : 0;
if (offset > 0) {
TypedArrayPrototypeSet(output, TypedArrayPrototypeSubarray(input, 0, offset));
src = TypedArrayPrototypeSubarray(input, offset);
dst = TypedArrayPrototypeSubarray(output, offset);
}
const srcLen = src.length;
let j = 0;
// 2. For each byte byte in input:
for (let i = 0; i < length; ++i) {
const byte = input[i];
for (let i = 0; i < srcLen; ++i) {
const byte = src[i];

// 1. If byte is not 0x25 (%), then append byte to output.
if (byte !== 0x25) {
output[j++] = byte;
dst[j++] = byte;

// 2. Otherwise, if byte is 0x25 (%) and the next two bytes
// after byte in input are not in the ranges
Expand All @@ -232,24 +248,25 @@ function percentDecode(input) {
// to output.
} else if (
byte === 0x25 &&
!(isHexCharByte(input[i + 1]) && isHexCharByte(input[i + 2]))
!(isHexCharByte(src[i + 1]) && isHexCharByte(src[i + 2]))
) {
output[j++] = 0x25;
dst[j++] = 0x25;

// 3. Otherwise:
} else {
// 1. Let bytePoint be the two bytes after byte in input,
// decoded, and then interpreted as hexadecimal number.
// 2. Append a byte whose value is bytePoint to output.
output[j++] = (hexByteToNumber(input[i + 1]) << 4) | hexByteToNumber(input[i + 2]);
dst[j++] = (hexByteToNumber(src[i + 1]) << 4) | hexByteToNumber(src[i + 2]);

// 3. Skip the next two bytes in input.
i += 2;
}
}

// 3. Return output.
return length === j ? output : TypedArrayPrototypeSubarray(output, 0, j);
const written = offset + j;
return length === written ? output : TypedArrayPrototypeSubarray(output, 0, written);
}

// https://infra.spec.whatwg.org/#forgiving-base64-decode
Expand Down
Loading