Skip to content

Commit 78de882

Browse files
committed
take binary data and dates of other realms
1 parent 38bdbd7 commit 78de882

4 files changed

Lines changed: 39 additions & 5 deletions

File tree

‎src/image.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import type { ToneMapping } from './codecs/hdr.ts';
2020
import * as quantize from './codecs/quantize.ts';
2121
import * as textRenderer from './render/text.ts';
2222
import * as svgRenderer from './render/svg.ts';
23-
import { BinaryData, view } from './util/mem.ts';
23+
import { BinaryData, view, isBinaryData } from './util/mem.ts';
2424
import { cicpTransform, colorSpaceTransform, profileTransform } from './util/icc.ts';
2525
import { ImageType } from './image-type.ts';
2626
import { textLayout, TextLayoutOptions } from './text-layout.ts';
@@ -3107,7 +3107,7 @@ export class Image {
31073107
const given = new Map<string, BinaryData | Image>();
31083108
for (const href of Object.keys(givenImages)) {
31093109
const value = givenImages[href];
3110-
if (!(value instanceof Image) && !(value instanceof ArrayBuffer || ArrayBuffer.isView(value)))
3110+
if (!(value instanceof Image) && !isBinaryData(value))
31113111
throw new TypeError(`SVG image ${JSON.stringify(href)} must be BinaryData or an Image`);
31123112
given.set(href, value);
31133113
}

‎src/metadata.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ export function resolveMetadata(metadata: Metadata = {}): ResolvedMetadata {
8686
const resolved: ResolvedMetadata = {};
8787
if (creationTime !== undefined && creationTime !== null) {
8888
// (other types than these were turned into a date anyway: true was 1970)
89-
if (!(creationTime instanceof Date) && typeof creationTime !== 'number' && typeof creationTime !== 'string') {
89+
// (a Date by its tag, not instanceof: one of another realm, an iframe's or Jest's, isn't an instance here)
90+
if (Object.prototype.toString.call(creationTime) !== '[object Date]' && typeof creationTime !== 'number' && typeof creationTime !== 'string') {
9091
// eslint-disable-next-line no-restricted-syntax -- the one date in the options
9192
throw new TypeError(`creationTime must be a Date, a number or a string, not ${typeof creationTime === 'object' ? shown(creationTime) : typeof creationTime}`);
9293
}

‎src/util/mem.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,23 @@ import { shown } from './options.ts';
22
/** Data accepted wherever binary input is expected */
33
export type BinaryData = ArrayBuffer | ArrayBufferView;
44

5+
/** Whether a value is binary data (an ArrayBuffer, a SharedArrayBuffer or a view), also one of another realm */
6+
export function isBinaryData(value: unknown): value is BinaryData {
7+
if (ArrayBuffer.isView(value)) return true;
8+
const tag = Object.prototype.toString.call(value);
9+
return tag === '[object ArrayBuffer]' || tag === '[object SharedArrayBuffer]';
10+
}
11+
512
/**
613
* Returns a Uint8Array view of the given buffer without copying
714
*/
815
export function view(buffer: BinaryData): Uint8Array {
916
// subclasses like node's Buffer are unwrapped, their slice() doesn't copy
1017
if (buffer instanceof Uint8Array && buffer.constructor === Uint8Array) return buffer;
11-
if (buffer instanceof ArrayBuffer) return new Uint8Array(buffer);
1218
if (ArrayBuffer.isView(buffer)) return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
13-
if (typeof SharedArrayBuffer !== 'undefined' && (buffer as unknown) instanceof SharedArrayBuffer) return new Uint8Array(buffer);
19+
// (by its tag, not instanceof: a buffer of another realm, an iframe's or a vm context's, isn't an instance here)
20+
const tag = Object.prototype.toString.call(buffer);
21+
if (tag === '[object ArrayBuffer]' || tag === '[object SharedArrayBuffer]') return new Uint8Array(buffer);
1422

1523
throw new TypeError(`Binary data has to be an ArrayBuffer or a typed array like Uint8Array, not ${shown(buffer)}`);
1624
}

‎tests/other-realms.js‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const vm = require('vm');
2+
const {Image, ImageType} = require('..');
3+
4+
const panic = message => {
5+
console.error(message);
6+
process.exit(1);
7+
};
8+
9+
// values made in another realm (a vm context, an iframe, Jest's environment) are accepted like local ones: instanceof
10+
// is false for them
11+
(async () => {
12+
if (typeof Deno !== 'undefined' || process.versions.bun) return; // (node:vm contexts)
13+
const context = vm.createContext({});
14+
const png = await new Image(2, 2).fill(0xff0000ff).encodePNG();
15+
const buffer = vm.runInContext(`new ArrayBuffer(${png.length})`, context);
16+
new Uint8Array(buffer).set(png);
17+
for (const [name, data] of [['ArrayBuffer', buffer], ['Uint8Array', vm.runInContext('b => new Uint8Array(b)', context)(buffer)]]) {
18+
const image = await Image.decode(data).catch(err => err);
19+
if (!(image instanceof Image) || image.getPixelAt(0, 0) !== 0xff0000ff) panic(`${name} of another realm: ${image}`);
20+
if (ImageType.getType(data) !== 'png') panic(`ImageType.getType(${name} of another realm)`);
21+
}
22+
const date = vm.runInContext('new Date(Date.UTC(2020, 1, 1))', context);
23+
const file = await new Image(1, 1).encodePNG({metadata: {creationTime: date}}).catch(err => err);
24+
if (!(file instanceof Uint8Array) || !Buffer.from(file).toString('latin1').includes('2020')) panic(`a Date of another realm: ${file}`);
25+
})();

0 commit comments

Comments
 (0)