@crossworks/content-core 0.500.30 → 0.500.31

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crossworks/content-core",
3
- "version": "0.500.30",
3
+ "version": "0.500.31",
4
4
  "description": "Browser-safe content logic — markdown, blocks, pages, tables, formulas — shared by the server and any client. Zero server deps by design: nothing here may reach @mantle/db, node-only APIs, or the network (the jackdaw-repo-split P0 boundary).",
5
5
  "type": "module",
6
6
  "exports": {
@@ -38,7 +38,7 @@
38
38
  "./tree": "./src/tree.ts"
39
39
  },
40
40
  "dependencies": {
41
- "@mantle/client-types": "npm:@crossworks/client-types@0.500.30",
41
+ "@mantle/client-types": "npm:@crossworks/client-types@0.500.31",
42
42
  "marked": "^18.1.2",
43
43
  "mathjs": "15.2.0"
44
44
  },
@@ -3,6 +3,7 @@ import { canvasAssets, canvasLoadProblems } from './canvas';
3
3
  import {
4
4
  CANVAS_ASSETS_MAX,
5
5
  canvasAssetProblems,
6
+ canvasMediaBytesMatch,
6
7
  canvasMediaMime,
7
8
  readCanvasMediaMessage,
8
9
  } from './canvas-media';
@@ -95,6 +96,38 @@ describe('canvasMediaMime', () => {
95
96
  });
96
97
  });
97
98
 
99
+ describe('canvasMediaBytesMatch', () => {
100
+ const b = (...parts: (string | number[])[]) =>
101
+ new Uint8Array(
102
+ parts.flatMap((p) => (typeof p === 'string' ? [...p].map((c) => c.charCodeAt(0)) : p)),
103
+ );
104
+ const png = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];
105
+
106
+ it('takes the bytes of each type', () => {
107
+ expect(canvasMediaBytesMatch('audio/mpeg', b('ID3', [4, 0]))).toBe(true);
108
+ expect(canvasMediaBytesMatch('audio/mpeg', b([0xff, 0xfb, 0x90]))).toBe(true);
109
+ // Zero padding before the first frame, as some encoders write.
110
+ expect(canvasMediaBytesMatch('audio/mpeg', b(new Array(600).fill(0), [0xff, 0xfb]))).toBe(true);
111
+ expect(canvasMediaBytesMatch('audio/mpeg', b(new Array(5000).fill(0), [0xff, 0xfb]))).toBe(
112
+ false,
113
+ );
114
+ expect(canvasMediaBytesMatch('audio/ogg', b('OggS', [0]))).toBe(true);
115
+ expect(canvasMediaBytesMatch('image/png', b(png, 'IHDR'))).toBe(true);
116
+ expect(canvasMediaBytesMatch('image/jpeg; x=1', b([0xff, 0xd8, 0xff, 0xe0]))).toBe(true);
117
+ expect(canvasMediaBytesMatch('image/webp', b('RIFF', [1, 2, 3, 4], 'WEBPVP8 '))).toBe(true);
118
+ });
119
+
120
+ it('refuses bytes of another type, too few bytes, and a type not handed out', () => {
121
+ expect(canvasMediaBytesMatch('image/png', b('ID3', [4, 0]))).toBe(false);
122
+ expect(canvasMediaBytesMatch('audio/mpeg', b('%PDF-1.7'))).toBe(false);
123
+ expect(canvasMediaBytesMatch('image/webp', b('RIFF', [1, 2, 3, 4], 'WAVE'))).toBe(false);
124
+ expect(canvasMediaBytesMatch('image/png', b(png.slice(0, 4)))).toBe(false);
125
+ expect(canvasMediaBytesMatch('audio/ogg', new Uint8Array())).toBe(false);
126
+ expect(canvasMediaBytesMatch('image/svg+xml', b('<svg'))).toBe(false);
127
+ expect(canvasMediaBytesMatch(null, b('ID3'))).toBe(false);
128
+ });
129
+ });
130
+
98
131
  describe('readCanvasMediaMessage', () => {
99
132
  it('takes a call id and a file id, lower case', () => {
100
133
  expect(readCanvasMediaMessage({ id: 3, file: A.toUpperCase() })).toEqual({
@@ -56,6 +56,48 @@ export function canvasMediaMime(contentType: string | null | undefined): string
56
56
  return (CANVAS_MEDIA_MIMES as readonly string[]).includes(base) ? base : null;
57
57
  }
58
58
 
59
+ /** How many zero bytes may come before an mp3's first frame or tag. */
60
+ export const CANVAS_MP3_PAD_MAX = 4096;
61
+
62
+ /** The first bytes of each type in CANVAS_MEDIA_MIMES. */
63
+ function startsWith(bytes: Uint8Array, at: number, sig: readonly number[]): boolean {
64
+ if (bytes.length < at + sig.length) return false;
65
+ return sig.every((b, i) => bytes[at + i] === b);
66
+ }
67
+ const ascii = (t: string) => [...t].map((c) => c.charCodeAt(0));
68
+
69
+ /**
70
+ * Whether `bytes` are the type `mime` names, by their first bytes. A file's
71
+ * type comes from the name it was given (a copy named `x.png` is stored as
72
+ * image/png), so an open link checks the bytes too before it hands a canvas
73
+ * a file: never a document under a picture's type. mp3: an ID3 tag or an
74
+ * MPEG frame sync, after up to CANVAS_MP3_PAD_MAX zero bytes (some encoders
75
+ * pad the start, and browsers play it); ogg: `OggS`; png, jpeg and webp:
76
+ * their signatures.
77
+ */
78
+ export function canvasMediaBytesMatch(mime: string | null | undefined, bytes: Uint8Array): boolean {
79
+ switch (canvasMediaMime(mime)) {
80
+ case 'audio/mpeg': {
81
+ let at = 0;
82
+ while (at < bytes.length && at < CANVAS_MP3_PAD_MAX && bytes[at] === 0) at++;
83
+ return (
84
+ startsWith(bytes, at, ascii('ID3')) ||
85
+ (bytes.length >= at + 2 && bytes[at] === 0xff && (bytes[at + 1]! & 0xe0) === 0xe0)
86
+ );
87
+ }
88
+ case 'audio/ogg':
89
+ return startsWith(bytes, 0, ascii('OggS'));
90
+ case 'image/png':
91
+ return startsWith(bytes, 0, [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
92
+ case 'image/jpeg':
93
+ return startsWith(bytes, 0, [0xff, 0xd8, 0xff]);
94
+ case 'image/webp':
95
+ return startsWith(bytes, 0, ascii('RIFF')) && startsWith(bytes, 8, ascii('WEBP'));
96
+ default:
97
+ return false;
98
+ }
99
+ }
100
+
59
101
  /** The words a frame hears for any file it may not have: one answer for
60
102
  * "not declared", "not readable" and "gone", so a canvas learns nothing
61
103
  * about a file it was not given. */
@@ -229,7 +271,7 @@ Rules:
229
271
 
230
272
  - Declare every file in the head: \`<meta name="${CANVAS_ASSETS_META}" content="<file id> <file id> ...">\`, ids from \`file_list\` or \`speech_file_create\`, at most ${CANVAS_ASSETS_MAX}. A call for a file not declared is refused. The save keeps only the files you can read, and its reply says which it left out and who cannot play them.
231
273
  - mp3 or ogg sound, png, jpeg or webp pictures, at most ${CANVAS_MEDIA_MAX_BYTES / (1024 * 1024)} MB each. Never a file URL in the source: it is refused.
232
- - Every call can reject: a viewer who cannot read the file, an open link whose media is off, a picture of the canvas (poster, export: no sound there). Always keep a way on without it: captions, \`speechSynthesis\`, or the next slide.
274
+ - Every call can reject: a viewer who cannot read the file, an open link (it serves only the files in the canvas's own workspace that the saver may edit), a picture of the canvas (poster, export: no sound there). Always keep a way on without it: captions, \`speechSynthesis\`, or the next slide.
233
275
  - Browsers play sound only from a click, and iPhone and iPad allow it per player. Use ONE shared player: ask for the first clip's URL before the click, set its \`src\` and call \`play()\` inside the click handler itself (no \`await\` before it), then change \`src\` and \`play()\` again on \`ended\` for each next clip. A new \`<audio>\` per clip stops after the first one there.
234
276
  - Ask for the next clip while one plays; release the one that finished.
235
277