@n-dream/builders-sdk 1.4.0 → 1.6.0
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/README.md +4 -3
- package/dist/auth.js +31 -0
- package/dist/local-db.js +2 -2
- package/dist/rules.d.ts +1 -0
- package/dist/rules.js +59 -3
- package/package.json +2 -3
package/README.md
CHANGED
|
@@ -74,9 +74,10 @@ await photos.remove(f.id);
|
|
|
74
74
|
```
|
|
75
75
|
|
|
76
76
|
- Types: JPEG, PNG, WebP, GIF, AVIF, HEIC; MP3, M4A, OGG, WAV, WebM
|
|
77
|
-
audio; MP4, WebM video; PDF. The platform reads the
|
|
78
|
-
file: the content must be the type of the file. No
|
|
79
|
-
scripts.
|
|
77
|
+
audio; MP4, MOV (iPhone), WebM video; PDF. The platform reads the
|
|
78
|
+
first bytes of the file: the content must be the type of the file. No
|
|
79
|
+
SVG, HTML or scripts. A `.mov` is served as `video/mp4` (Chrome on
|
|
80
|
+
Android plays it only so); its `type` stays `video/quicktime`.
|
|
80
81
|
- At most 10 MB a file, 1,000 files and 200 MB per user. Make photos
|
|
81
82
|
smaller in the browser before the upload (at most 2,048 pixels, JPEG
|
|
82
83
|
or WebP).
|
package/dist/auth.js
CHANGED
|
@@ -145,7 +145,17 @@ export function createAuth(env, track = !env) {
|
|
|
145
145
|
}
|
|
146
146
|
if (!res.ok)
|
|
147
147
|
throw new BuildersError(`Builders could not sign out (status ${res.status}). Try again.`, res.status);
|
|
148
|
+
const body = (await res.json().catch(() => null));
|
|
148
149
|
changed(null);
|
|
150
|
+
// A private app (docs/private-apps.md 6, D161): the files that the
|
|
151
|
+
// service worker saved go, and the worker too, so the next person on
|
|
152
|
+
// this device sees only the gate. The app's own localStorage and
|
|
153
|
+
// IndexedDB stay (Q3). Then the page loads again and shows the gate.
|
|
154
|
+
if (body?.private === true) {
|
|
155
|
+
await clearSavedFiles();
|
|
156
|
+
const { location } = get();
|
|
157
|
+
location.assign(`${location.pathname}${location.search}`);
|
|
158
|
+
}
|
|
149
159
|
},
|
|
150
160
|
accountPage() {
|
|
151
161
|
if (isLocal()) {
|
|
@@ -156,4 +166,25 @@ export function createAuth(env, track = !env) {
|
|
|
156
166
|
},
|
|
157
167
|
};
|
|
158
168
|
}
|
|
169
|
+
// The service worker caches and registrations of this origin. A failure
|
|
170
|
+
// does not stop the sign-out: the session has ended already.
|
|
171
|
+
async function clearSavedFiles() {
|
|
172
|
+
const g = globalThis;
|
|
173
|
+
try {
|
|
174
|
+
if (g.caches)
|
|
175
|
+
for (const k of await g.caches.keys())
|
|
176
|
+
await g.caches.delete(k);
|
|
177
|
+
}
|
|
178
|
+
catch {
|
|
179
|
+
/* no cache storage */
|
|
180
|
+
}
|
|
181
|
+
try {
|
|
182
|
+
const regs = (await g.navigator?.serviceWorker?.getRegistrations()) ?? [];
|
|
183
|
+
for (const r of regs)
|
|
184
|
+
await r.unregister();
|
|
185
|
+
}
|
|
186
|
+
catch {
|
|
187
|
+
/* no service worker */
|
|
188
|
+
}
|
|
189
|
+
}
|
|
159
190
|
export const auth = createAuth();
|
package/dist/local-db.js
CHANGED
|
@@ -8,7 +8,7 @@ import { BuildersError } from "./errors.js";
|
|
|
8
8
|
import { DataEngine, RuleFailure, randomItemId } from "./engine.js";
|
|
9
9
|
import { local } from "./local.js";
|
|
10
10
|
import { MemoryStorage } from "./memory-storage.js";
|
|
11
|
-
import { NO_USER, WATCH_LISTS_MAX, fileObjectOf, userGuard, parseDeclaration, spaceLimits } from "./rules.js";
|
|
11
|
+
import { NO_USER, WATCH_LISTS_MAX, fileObjectOf, servedTypeOf, userGuard, parseDeclaration, spaceLimits } from "./rules.js";
|
|
12
12
|
import { userChanged } from "./db.js";
|
|
13
13
|
import { believed } from "./session.js";
|
|
14
14
|
const APP_ID = "local0000001";
|
|
@@ -132,7 +132,7 @@ function localUrl(f, uid) {
|
|
|
132
132
|
let url = s.urls.get(name);
|
|
133
133
|
const blob = s.blobs.get(name);
|
|
134
134
|
if (!url && blob && typeof URL.createObjectURL === "function") {
|
|
135
|
-
url = URL.createObjectURL(new Blob([blob.bytes], { type: blob.type }));
|
|
135
|
+
url = URL.createObjectURL(new Blob([blob.bytes], { type: servedTypeOf(blob.type) }));
|
|
136
136
|
s.urls.set(name, url);
|
|
137
137
|
}
|
|
138
138
|
return url ? { ...f, url } : f;
|
package/dist/rules.d.ts
CHANGED
|
@@ -197,6 +197,7 @@ export declare const FILE_TYPES: Readonly<Record<string, {
|
|
|
197
197
|
ext: string;
|
|
198
198
|
label: string;
|
|
199
199
|
}>>;
|
|
200
|
+
export declare function servedTypeOf(type: string): string;
|
|
200
201
|
export declare const FILE_HEAD_BYTES = 64;
|
|
201
202
|
export declare function fileTypeOf(raw: unknown): string | null;
|
|
202
203
|
export declare function sniffFileTypes(b: Uint8Array): string[];
|
package/dist/rules.js
CHANGED
|
@@ -604,9 +604,10 @@ export const FILE_TYPES = {
|
|
|
604
604
|
"audio/webm": { kind: "audio", ext: "webm", label: "WebM audio" },
|
|
605
605
|
"video/mp4": { kind: "video", ext: "mp4", label: "MP4" },
|
|
606
606
|
"video/webm": { kind: "video", ext: "webm", label: "WebM video" },
|
|
607
|
+
"video/quicktime": { kind: "video", ext: "mov", label: "QuickTime (MOV)" },
|
|
607
608
|
"application/pdf": { kind: "pdf", ext: "pdf", label: "PDF" },
|
|
608
609
|
};
|
|
609
|
-
const FILE_TYPE_NAMES = "JPEG, PNG, WebP, GIF, AVIF or HEIC pictures, MP3, M4A, OGG, WAV or WebM audio, MP4 or WebM video, or PDF";
|
|
610
|
+
const FILE_TYPE_NAMES = "JPEG, PNG, WebP, GIF, AVIF or HEIC pictures, MP3, M4A, OGG, WAV or WebM audio, MP4, MOV or WebM video, or PDF";
|
|
610
611
|
// Other names that browsers give to the same types.
|
|
611
612
|
const TYPE_ALIASES = {
|
|
612
613
|
"image/jpg": "image/jpeg",
|
|
@@ -623,7 +624,19 @@ const TYPE_ALIASES = {
|
|
|
623
624
|
"audio/vnd.wave": "audio/wav",
|
|
624
625
|
"application/ogg": "audio/ogg",
|
|
625
626
|
"video/x-m4v": "video/mp4",
|
|
627
|
+
"video/mov": "video/quicktime",
|
|
628
|
+
"video/x-quicktime": "video/quicktime",
|
|
626
629
|
};
|
|
630
|
+
// The Content-Type of a download, when it is not the stored type.
|
|
631
|
+
// QuickTime is served as MP4: the containers are almost the same, Chrome
|
|
632
|
+
// on Android plays H.264 in a .mov only as "video/mp4", and Safari plays
|
|
633
|
+
// both. The saved name keeps ".mov" (downloadNameOf).
|
|
634
|
+
const SERVED_TYPES = {
|
|
635
|
+
"video/quicktime": "video/mp4",
|
|
636
|
+
};
|
|
637
|
+
export function servedTypeOf(type) {
|
|
638
|
+
return SERVED_TYPES[type] ?? type;
|
|
639
|
+
}
|
|
627
640
|
// The bytes that the type check reads.
|
|
628
641
|
export const FILE_HEAD_BYTES = 64;
|
|
629
642
|
// The declared type in its stored form. "": no type given (the type from
|
|
@@ -656,14 +669,46 @@ const HEIC_BRANDS = ["heic", "heix", "heim", "heis", "hevc", "hevx", "mif1", "ms
|
|
|
656
669
|
const M4A_BRANDS = ["M4A ", "M4B ", "M4P "];
|
|
657
670
|
const M4V_BRANDS = ["M4V ", "M4VH", "M4VP"];
|
|
658
671
|
const MP4_BRANDS = ["isom", "iso2", "iso3", "iso4", "iso5", "iso6", "mp41", "mp42", "avc1", "dash", "mmp4", "MSNV"];
|
|
672
|
+
const u32 = (b, at) => ((b[at] << 24) >>> 0) + (b[at + 1] << 16) + (b[at + 2] << 8) + b[at + 3];
|
|
673
|
+
// A box or atom size: under 16 MB (a file is at most 10 MB). The first
|
|
674
|
+
// byte is 0, so text (HTML, SVG, a script) never starts a box.
|
|
675
|
+
const BOX_MAX = 0x1000000;
|
|
659
676
|
function isoBrands(b) {
|
|
660
|
-
const size = (
|
|
677
|
+
const size = u32(b, 0);
|
|
661
678
|
const end = Math.min(b.length, Math.max(16, size));
|
|
662
679
|
const brands = [String.fromCharCode(...b.subarray(8, 12))];
|
|
663
680
|
for (let i = 16; i + 4 <= end; i += 4)
|
|
664
681
|
brands.push(String.fromCharCode(...b.subarray(i, i + 4)));
|
|
665
682
|
return brands;
|
|
666
683
|
}
|
|
684
|
+
// An atom size: 0 (to the end of the file), 1 (a 64-bit size follows)
|
|
685
|
+
// or at least the header.
|
|
686
|
+
const atomSize = (b, at, min) => {
|
|
687
|
+
const n = u32(b, at);
|
|
688
|
+
if (n === 1)
|
|
689
|
+
return b.length >= at + 16 && b[at + 8] === 0 && b[at + 9] === 0 && b[at + 10] === 0 && u32(b, at + 12) >= 16 ? n : -1;
|
|
690
|
+
return n === 0 || (n >= min && n < BOX_MAX) ? n : -1;
|
|
691
|
+
};
|
|
692
|
+
// An older QuickTime file without ftyp: it starts with a "moov" atom
|
|
693
|
+
// (its first child "mvhd", "cmov" or "prfl"), a "mdat" atom, or a
|
|
694
|
+
// "wide" atom of 8 bytes and then "mdat" or "moov".
|
|
695
|
+
function quickTimeAtoms(b) {
|
|
696
|
+
if (b.length < 16)
|
|
697
|
+
return null;
|
|
698
|
+
if (ascii(b, 4, "moov")) {
|
|
699
|
+
const n = u32(b, 0);
|
|
700
|
+
return n >= 16 && n < BOX_MAX && u32(b, 8) >= 8 && u32(b, 8) < BOX_MAX && ["mvhd", "cmov", "prfl"].some((x) => ascii(b, 12, x)) ? ["video/quicktime"] : null;
|
|
701
|
+
}
|
|
702
|
+
if (ascii(b, 4, "mdat"))
|
|
703
|
+
return atomSize(b, 0, 8) >= 0 ? ["video/quicktime"] : null;
|
|
704
|
+
if (ascii(b, 4, "wide") && u32(b, 0) === 8) {
|
|
705
|
+
if (ascii(b, 12, "mdat"))
|
|
706
|
+
return atomSize(b, 8, 8) >= 0 ? ["video/quicktime"] : null;
|
|
707
|
+
if (ascii(b, 12, "moov"))
|
|
708
|
+
return quickTimeAtoms(b.subarray(8));
|
|
709
|
+
}
|
|
710
|
+
return null;
|
|
711
|
+
}
|
|
667
712
|
// The types that the first bytes of a file can be (stored form), the most
|
|
668
713
|
// likely first. Empty: none of section 7 (for example SVG, HTML, XML or a
|
|
669
714
|
// script). The check reads only the start of the file.
|
|
@@ -686,7 +731,7 @@ export function sniffFileTypes(b) {
|
|
|
686
731
|
// EBML: only the doc type "webm" (not Matroska).
|
|
687
732
|
return indexOfAscii(b, "webm", 4) > 0 ? ["video/webm", "audio/webm"] : [];
|
|
688
733
|
}
|
|
689
|
-
if (b.length >= 12 && ascii(b, 4, "ftyp")) {
|
|
734
|
+
if (b.length >= 12 && ascii(b, 4, "ftyp") && u32(b, 0) >= 12 && u32(b, 0) < BOX_MAX) {
|
|
690
735
|
const brands = isoBrands(b);
|
|
691
736
|
const major = brands[0];
|
|
692
737
|
if (AVIF_BRANDS.includes(major) || (HEIC_BRANDS.includes(major) && brands.some((x) => AVIF_BRANDS.includes(x))))
|
|
@@ -699,8 +744,16 @@ export function sniffFileTypes(b) {
|
|
|
699
744
|
return ["video/mp4"];
|
|
700
745
|
if (MP4_BRANDS.includes(major))
|
|
701
746
|
return ["video/mp4", "audio/mp4"];
|
|
747
|
+
// QuickTime (the iPhone's video): only the major brand "qt ", in an
|
|
748
|
+
// ftyp box of a correct size.
|
|
749
|
+
const size = u32(b, 0);
|
|
750
|
+
if (major === "qt " && size >= 16 && size % 4 === 0)
|
|
751
|
+
return ["video/quicktime", "video/mp4"];
|
|
702
752
|
return [];
|
|
703
753
|
}
|
|
754
|
+
const qt = quickTimeAtoms(b);
|
|
755
|
+
if (qt)
|
|
756
|
+
return qt;
|
|
704
757
|
if (ascii(b, 0, "ID3"))
|
|
705
758
|
return ["audio/mpeg"];
|
|
706
759
|
// An MP3 frame: the sync bits, MPEG version not reserved, layer III, a
|
|
@@ -717,6 +770,9 @@ export function checkFileType(declared, head) {
|
|
|
717
770
|
return ruleError("invalid", declared ? MESSAGES.fileContent(declared) : MESSAGES.fileType());
|
|
718
771
|
if (!declared)
|
|
719
772
|
return can[0];
|
|
773
|
+
// A ".mov" that is a plain MP4 (some exports): stored as MP4.
|
|
774
|
+
if (declared === "video/quicktime" && !can.includes(declared) && can.includes("video/mp4"))
|
|
775
|
+
return "video/mp4";
|
|
720
776
|
return can.includes(declared) ? declared : ruleError("invalid", MESSAGES.fileContent(declared));
|
|
721
777
|
}
|
|
722
778
|
// The file set of a name, or the error.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@n-dream/builders-sdk",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.0",
|
|
4
4
|
"description": "Builders SDK for app code: app information, sign-in of app users and app data.",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"type": "module",
|
|
@@ -36,8 +36,7 @@
|
|
|
36
36
|
],
|
|
37
37
|
"sideEffects": false,
|
|
38
38
|
"publishConfig": {
|
|
39
|
-
"access": "public"
|
|
40
|
-
"tag": "next"
|
|
39
|
+
"access": "public"
|
|
41
40
|
},
|
|
42
41
|
"scripts": {
|
|
43
42
|
"build": "tsc -p tsconfig.build.json",
|