@protobase/cli 0.94.27 → 0.94.29
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/dist/cli/package.js +1 -1
- package/dist/mobile/android-backup.js +47 -0
- package/dist/mobile/api-origin.js +5 -2
- package/dist/mobile/generate.js +15 -5
- package/package.json +8 -8
- package/skills/protobase/references/features/files-and-images.md +1 -0
- package/skills/protobase/references/guides/mobile-app.md +1 -0
- package/skills/protobase/references/reference/files.md +31 -6
package/dist/cli/package.js
CHANGED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
//#region src/mobile/android-backup.ts
|
|
2
|
+
const rootTag = "<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\">";
|
|
3
|
+
const backupLine = " android:allowBackup=\"true\"\n";
|
|
4
|
+
/** Where the data extraction rules go, relative to the `android` folder. */
|
|
5
|
+
const dataExtractionRulesPath = [
|
|
6
|
+
"app",
|
|
7
|
+
"src",
|
|
8
|
+
"main",
|
|
9
|
+
"res",
|
|
10
|
+
"xml",
|
|
11
|
+
"data_extraction_rules.xml"
|
|
12
|
+
];
|
|
13
|
+
const excludeAll = [
|
|
14
|
+
"root",
|
|
15
|
+
"file",
|
|
16
|
+
"database",
|
|
17
|
+
"sharedpref",
|
|
18
|
+
"external",
|
|
19
|
+
"device_root",
|
|
20
|
+
"device_file",
|
|
21
|
+
"device_database",
|
|
22
|
+
"device_sharedpref"
|
|
23
|
+
].map((domain) => ` <exclude domain="${domain}" path="."/>\n`).join("");
|
|
24
|
+
/**
|
|
25
|
+
* The data extraction rules: on Android 12 and later, nothing goes into a cloud backup or a device-to-device transfer,
|
|
26
|
+
* so a signed-in session never moves to another phone. Every domain is named, as `root` is not documented to cover the others.
|
|
27
|
+
*/
|
|
28
|
+
const dataExtractionRules = `<?xml version="1.0" encoding="utf-8"?>
|
|
29
|
+
<!-- Nothing of the app's carries over to another phone: it starts there as a fresh install. -->
|
|
30
|
+
<data-extraction-rules>
|
|
31
|
+
<cloud-backup>
|
|
32
|
+
${excludeAll} </cloud-backup>
|
|
33
|
+
<device-transfer>
|
|
34
|
+
${excludeAll} </device-transfer>
|
|
35
|
+
</data-extraction-rules>
|
|
36
|
+
`;
|
|
37
|
+
/**
|
|
38
|
+
* Capacitor's AndroidManifest.xml opting out of backup and device transfer: `allowBackup="false"` for Android 11 and
|
|
39
|
+
* earlier and cloud backup, the data extraction rules for Android 12 and later, and `tools:replace` so a plugin's
|
|
40
|
+
* manifest cannot turn either back on in the merge. Throws on any other manifest rather than leave backup on.
|
|
41
|
+
*/
|
|
42
|
+
const withoutBackup = (manifest) => {
|
|
43
|
+
if (manifest.split(rootTag).length !== 2 || manifest.split(backupLine).length !== 2 || /dataExtractionRules|fullBackupContent|xmlns:tools/.test(manifest)) throw new Error("AndroidManifest.xml is not Capacitor's template: it needs its one root tag and android:allowBackup=\"true\" line, and no backup rules");
|
|
44
|
+
return manifest.replace(rootTag, `${rootTag.slice(0, -1)} xmlns:tools="http://schemas.android.com/tools">`).replace(backupLine, " android:allowBackup=\"false\"\n android:dataExtractionRules=\"@xml/data_extraction_rules\"\n tools:replace=\"android:allowBackup,android:dataExtractionRules\"\n");
|
|
45
|
+
};
|
|
46
|
+
//#endregion
|
|
47
|
+
export { dataExtractionRules, dataExtractionRulesPath, withoutBackup };
|
|
@@ -20,10 +20,13 @@ const apiUrl = (given, issuer, platform) => {
|
|
|
20
20
|
if (url.protocol === "http:" && !httpHosts[platform].includes(url.hostname)) throw new Error(`${what} uses plain http to ${url.hostname}: plain http is only for a server on this machine (${httpHosts[platform].join(", ")}); any other server needs https`);
|
|
21
21
|
return url;
|
|
22
22
|
};
|
|
23
|
-
/**
|
|
23
|
+
/**
|
|
24
|
+
* What the app must allow to reach `url`: plain http, and on Android mixed content for plain http to the emulator's
|
|
25
|
+
* host, which is not loopback, so the https page could not call it otherwise.
|
|
26
|
+
*/
|
|
24
27
|
const httpAllowances = (url) => ({
|
|
25
28
|
cleartext: url.protocol === "http:",
|
|
26
|
-
mixedContent: url.hostname === "10.0.2.2"
|
|
29
|
+
mixedContent: url.protocol === "http:" && url.hostname === "10.0.2.2"
|
|
27
30
|
});
|
|
28
31
|
/** The port `adb reverse` forwards for an Android app calling `url` on this machine, `undefined` for any other. */
|
|
29
32
|
const forwardedPort = (url, platform) => {
|
package/dist/mobile/generate.js
CHANGED
|
@@ -1,16 +1,19 @@
|
|
|
1
|
+
import { dataExtractionRules, dataExtractionRulesPath, withoutBackup } from "./android-backup.js";
|
|
1
2
|
import { copyGoogleServices } from "./google-services.js";
|
|
2
3
|
import { withLocalNetworking } from "./info-plist.js";
|
|
3
4
|
import { setUpIosPush } from "./ios-push.js";
|
|
4
5
|
import { withProtobaseIcon } from "./native-icon.js";
|
|
5
6
|
import path from "node:path";
|
|
6
|
-
import { readFile, rm, writeFile } from "node:fs/promises";
|
|
7
|
+
import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
7
8
|
//#region src/mobile/generate.ts
|
|
8
9
|
const infoPlist = (ws) => path.join(ws, "ios", "App", "App", "Info.plist");
|
|
10
|
+
const androidManifest = (ws) => path.join(ws, "android", "app", "src", "main", "AndroidManifest.xml");
|
|
9
11
|
/**
|
|
10
12
|
* Generates the platform's native project in the workspace from its capacitor.config.json and app files, replacing
|
|
11
13
|
* the last one: it is never edited by hand. Its home-screen icon is Protobase's, an Android app with push gets the
|
|
12
14
|
* project's google-services.json from `googleServicesFrom`, an iOS app with push its entitlement in `apnsEnvironment` and
|
|
13
|
-
* the token forwarding, and an iOS app calling the API over plain http may reach local addresses.
|
|
15
|
+
* the token forwarding, and an iOS app calling the API over plain http may reach local addresses. Android apps opt out
|
|
16
|
+
* of backup and device transfer, so nothing carries over to another phone.
|
|
14
17
|
*/
|
|
15
18
|
const generatePlatform = async (ws, platform, { cleartext, googleServicesFrom, apnsEnvironment }, cap) => {
|
|
16
19
|
await rm(path.join(ws, platform), {
|
|
@@ -19,9 +22,16 @@ const generatePlatform = async (ws, platform, { cleartext, googleServicesFrom, a
|
|
|
19
22
|
});
|
|
20
23
|
await cap(["add", platform]);
|
|
21
24
|
await withProtobaseIcon(ws, platform);
|
|
22
|
-
if (platform === "android"
|
|
23
|
-
|
|
24
|
-
|
|
25
|
+
if (platform === "android") {
|
|
26
|
+
if (googleServicesFrom !== void 0) await copyGoogleServices(googleServicesFrom, ws);
|
|
27
|
+
await writeFile(androidManifest(ws), withoutBackup(await readFile(androidManifest(ws), "utf8")));
|
|
28
|
+
const rules = path.join(ws, "android", ...dataExtractionRulesPath);
|
|
29
|
+
await mkdir(path.dirname(rules), { recursive: true });
|
|
30
|
+
await writeFile(rules, dataExtractionRules);
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
if (apnsEnvironment !== void 0) await setUpIosPush(ws, apnsEnvironment);
|
|
34
|
+
if (!cleartext) return;
|
|
25
35
|
await writeFile(infoPlist(ws), withLocalNetworking(await readFile(infoPlist(ws), "utf8")));
|
|
26
36
|
};
|
|
27
37
|
//#endregion
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@protobase/cli",
|
|
3
|
-
"version": "0.94.
|
|
3
|
+
"version": "0.94.29",
|
|
4
4
|
"description": "The protobase command: scaffold, doctor, dev, build and serve a Protobase project.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -43,13 +43,13 @@
|
|
|
43
43
|
"dependencies": {
|
|
44
44
|
"@better-auth/core": "^1.7.12",
|
|
45
45
|
"@hono/node-server": "^2.1.3",
|
|
46
|
-
"@protobase/config": "0.94.
|
|
47
|
-
"@protobase/layout": "0.94.
|
|
48
|
-
"@protobase/query": "0.94.
|
|
49
|
-
"@protobase/schema": "0.94.
|
|
50
|
-
"@protobase/server": "0.94.
|
|
51
|
-
"@protobase/ui": "0.94.
|
|
52
|
-
"@protobase/workflows": "0.94.
|
|
46
|
+
"@protobase/config": "0.94.29",
|
|
47
|
+
"@protobase/layout": "0.94.29",
|
|
48
|
+
"@protobase/query": "0.94.29",
|
|
49
|
+
"@protobase/schema": "0.94.29",
|
|
50
|
+
"@protobase/server": "0.94.29",
|
|
51
|
+
"@protobase/ui": "0.94.29",
|
|
52
|
+
"@protobase/workflows": "0.94.29",
|
|
53
53
|
"@react-email/render": "^2.1.0",
|
|
54
54
|
"@scalar/hono-api-reference": "^0.12.10",
|
|
55
55
|
"@sentry/vite-plugin": "^11.10.0",
|
|
@@ -13,6 +13,7 @@ Uploaded files and images, where they are kept and how they are served.
|
|
|
13
13
|
- [Your own types](../reference/files.md#your-own-types): An app names its own media types and their extensions, and files with them are stored and served as those types.
|
|
14
14
|
- [File viewers](../reference/files.md#viewers): A plugin registers a viewer for a media type, and a file of that type opens in it in the app instead of downloading.
|
|
15
15
|
- [Signed downloads](../reference/files.md#downloads): Private files open through short-lived links that stop working once the record no longer holds the file.
|
|
16
|
+
- [Type and size fields](../reference/files.md#type-and-size-fields): Each file's media type and size are kept in columns of their own, for lists and aggregate queries, and serving reads the type from there.
|
|
16
17
|
- [Derived fields](../reference/files.md#derived-fields): Width, height, size and type of each upload are written to other fields.
|
|
17
18
|
- [Image variants and placeholders](../reference/files.md#image-variants-and-placeholders): Resized copies of uploaded images, and a blurred placeholder while they load.
|
|
18
19
|
- [External URLs](../reference/files.md#external-urls): A file field can hold a link to a file on another site.
|
|
@@ -111,6 +111,7 @@ makes a signed release binary: an `.aab` to upload to Google Play, an `.ipa` to
|
|
|
111
111
|
- A release calls its server over https, at `--api` or the app's `url`: plain http, `localhost` and `10.0.2.2` are refused.
|
|
112
112
|
- `--app-version` is the version the stores show, one to three numbers such as `1.2.3`. `--build-number` is a whole number that must rise with every upload, such as your CI's run number.
|
|
113
113
|
- Extra plugins run at build time with access to your signing key, so add only plugins you trust.
|
|
114
|
+
- Android apps built by `protobase mobile run` and `protobase mobile build` opt out of Android's backup and device-to-device transfer, so no working session sits in a cloud backup or on another phone. On a new phone, or after restoring a backup, the app starts as a fresh install: your users sign in again, and unsent drafts are gone. <gap>An iOS app restored from a backup or moved to a new iPhone cannot start as a fresh install because opting the iOS app out of backup is not implemented.</gap>
|
|
114
115
|
|
|
115
116
|
**Android.** Make an upload key once, outside the project, and never commit it:
|
|
116
117
|
|
|
@@ -50,6 +50,8 @@ export const bikes = resource('bikes')
|
|
|
50
50
|
| `.maxSize('10 MB')` | The largest upload, as bytes or text (`512 KiB`, `1.5 GB`); default 50 MB. |
|
|
51
51
|
| `.public()` | Stored with the `public` provider, whose files anyone with the link may open. |
|
|
52
52
|
| `.storage('archive')` | Stored with another provider; default `private`. |
|
|
53
|
+
| `.typeField('attachment_type')` | Each file's media type in that text field, which responses and downloads read, see [Type and size fields](#type-and-size-fields). |
|
|
54
|
+
| `.sizeField('attachment_size')` | Each file's size in bytes in that integer or bigint field, see [Type and size fields](#type-and-size-fields). |
|
|
53
55
|
| `.derive({ field: processor })` | Values computed from each upload and written to other fields of the row, see [Derived fields](#derived-fields). |
|
|
54
56
|
| `.placeholder('photo_placeholder')` | Each image's ThumbHash in that text field, drawn while the image loads, see [Image variants and placeholders](#image-variants-and-placeholders). |
|
|
55
57
|
| `.variants({ card: { … } }, { original? })` | Resized copies of each image stored beside it, see [Image variants and placeholders](#image-variants-and-placeholders). |
|
|
@@ -66,7 +68,7 @@ private:1/2992db9c-776e-4d4e-9e0f-59d485fefaf9.jpg?name=bike+red.png&size=48211
|
|
|
66
68
|
- The **scheme is the provider's name**, so a provider can move from local disk to a bucket without touching any row. Name the role (`archive`), not the vendor: the name is part of every stored value.
|
|
67
69
|
- The **path** of an upload is `<tenant>/<random id>.<extension>`, with `_` for a resource without a tenant. Nothing in it comes from the row or from what the user typed, so renames and key changes need nothing.
|
|
68
70
|
- The **query** holds the uploader's file name and the size in bytes, and for an image with [variants](#image-variants-and-placeholders) their names and extensions: `&variants=card.webp,thumb.webp`. A plain `{provider}:{path}`, such as `public:images/test.jpg` written by an import, is valid too; its name is its last path segment.
|
|
69
|
-
- The **type is not stored
|
|
71
|
+
- The **type is not stored** in the value. It is the path's extension, looked up in the built-in table and the app's [own types](#your-own-types), which is also what downloads are served as, unless the field has a [type field](#type-and-size-fields).
|
|
70
72
|
- A field with `.externalUrls()` may hold an **https URL** instead, such as `https://images.example.com/listings/42.jpg`, see [External URLs](#external-urls).
|
|
71
73
|
|
|
72
74
|
Responses carry each file field as an object, and the stored value stays the URI:
|
|
@@ -151,10 +153,10 @@ PROTOBASE_FILES_ARCHIVE=s3://acme-archive?endpoint=https://s3.fr-par.scw.cloud&r
|
|
|
151
153
|
```
|
|
152
154
|
|
|
153
155
|
- **Credentials** never go in the URL. A provider set from the environment reads `PROTOBASE_FILES_<NAME>_ACCESS_KEY_ID` and `PROTOBASE_FILES_<NAME>_SECRET_ACCESS_KEY`, else `S3_ACCESS_KEY_ID` and `S3_SECRET_ACCESS_KEY`, else `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`, and the server does not start without a pair. In the config, pass `accessKeyId` and `secretAccessKey`, or leave them out for the `S3_` or `AWS_` pair. The key needs to read, write, list and delete objects in the bucket.
|
|
154
|
-
- **Uploads** still go through Protobase's upload route, so types are checked and processors run; the body streams on to the bucket in parts, and an upload that fails stores nothing. Each object is stored with the type and disposition
|
|
156
|
+
- **Uploads** still go through Protobase's upload route, so types are checked and processors run; the body streams on to the bucket in parts, and an upload that fails stores nothing. Each object is stored with the type its upload was decided to be, the app's [own types](#your-own-types) included, and the disposition that type is served with. A bucket keeps the parts of an upload that never finished until they are aborted: R2 aborts them after 7 days, on Scaleway and AWS add a lifecycle rule that aborts incomplete multipart uploads after a day.
|
|
155
157
|
- **Private files** keep their signed `url`. A download checks the link and the row as above, then redirects with `302` to a presigned link to the bucket that lasts as long as the app's link, served with the same type and disposition. The bytes never pass through the app.
|
|
156
158
|
- **Public files** are linked at `publicUrl` followed by the prefix and the path, such as `https://files.acme.com/public/_/2992db9c-….png`: the bucket's own public address, an R2 custom domain or a Scaleway public bucket. Without `publicUrl`, `/api/files/<provider>/<path>` redirects to a presigned link instead.
|
|
157
|
-
- The bucket serves its files itself, so the `nosniff` and sandbox headers of the app's own downloads do not apply. The type always
|
|
159
|
+
- The bucket serves its files itself, so the `nosniff` and sandbox headers of the app's own downloads do not apply. The type is always the one the upload was decided to be, never HTML, and the bucket is on another domain than the app. A page that `fetch`es file URLs, rather than showing them in `<img>` or linking them, needs CORS on the bucket.
|
|
158
160
|
- It runs on Bun, whose built-in S3 client it uses: `protobase-serve.js`, the `protobase` image, or the CLI started with Bun. On Node the first file operation fails.
|
|
159
161
|
|
|
160
162
|
## Uploading
|
|
@@ -211,6 +213,29 @@ A plugin, or the app's own `protobase.ui.tsx`, registers a viewer for a media ty
|
|
|
211
213
|
|
|
212
214
|
The viewer gets the file and its **siblings**: the record's other files, and the files of the records of the same resource that point at the same records through filterable relations, read in one list call of at most 100 records. For attachments that each point at a task, the siblings of one are the other attachments of that task, so a report can show the images it names by file name.
|
|
213
215
|
|
|
216
|
+
## Type and size fields
|
|
217
|
+
|
|
218
|
+
`.typeField()` and `.sizeField()` keep each file's media type and size in columns of their own, so a list can show them and SQL can count and sum by them:
|
|
219
|
+
|
|
220
|
+
```ts
|
|
221
|
+
attachment: f.file().optional().typeField('attachment_type').sizeField('attachment_size'),
|
|
222
|
+
attachment_type: f.text().readOnly().optional(),
|
|
223
|
+
attachment_size: f.bigint().readOnly().optional(),
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
```sql
|
|
227
|
+
select attachment_type, count(*), sum(attachment_size) from attachments group by attachment_type
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
- Both fields must be `readOnly()`, as [derived fields](#derived-fields) are: an upload writes them with the file in the same transaction, and clearing the file sets them to `null`. The type field is text; the size field an integer, or a bigint for files past 2 GB.
|
|
231
|
+
- An **upload** writes the type it was [decided](#how-the-type-is-decided) to be and its size in bytes.
|
|
232
|
+
- An **external URL** writes the type its extension names, the app's [own types](#your-own-types) included, and `null` when the table knows no such extension. Its size field stays `null`, since Protobase never fetches it.
|
|
233
|
+
- An **import** that writes rows itself may write both beside the value, such as the type of a file stored under a name with no extension. Leave a field `null` when the source does not say; never guess.
|
|
234
|
+
- **Responses** and [viewers](#viewers) take the file's `type` and `size` from these fields when the row holds them, else from the value.
|
|
235
|
+
- A **private download** is served as the type field's type when that is a type the app's table knows, so a file is never served as HTML; any other value is served by the path's extension. A public file has no row to read, so it is served by its path, and a bucket serves it with the type its upload was decided to be.
|
|
236
|
+
|
|
237
|
+
Files stored before a field declared them keep `null` until they are uploaded again or an import fills them.
|
|
238
|
+
|
|
214
239
|
## Derived fields
|
|
215
240
|
|
|
216
241
|
`.derive({ field: processor })` computes values from each upload into other fields of the same resource. Those fields must be `readOnly()`, so only the upload sets them.
|
|
@@ -271,10 +296,10 @@ curl -X PATCH https://app.example.com/api/v1/products/42 -H "authorization: Bear
|
|
|
271
296
|
```
|
|
272
297
|
|
|
273
298
|
- **What is taken**: an absolute `https://` URL with a host, without a user name or password, written without spaces or other characters a URL escapes. `http:`, `data:` and anything else is refused with `400`, as in a field without the option.
|
|
274
|
-
- **The type** is the URL path's extension, from the same table as stored files, and must pass `.accept()`: an image field takes `…/front.jpg` and refuses `…/front` and `…/scan.pdf`. The name is the path's last segment.
|
|
275
|
-
- **Stored as given**: the column holds the URL itself, and Protobase never fetches, uploads or copies it. It has no size, and responses link it as it is, as `url`, for whoever may read the row.
|
|
299
|
+
- **The type** is the URL path's extension, from the same table as stored files, the app's [own types](#your-own-types) included, and must pass `.accept()`: an image field takes `…/front.jpg` and refuses `…/front` and `…/scan.pdf`. The name is the path's last segment. A [type field](#type-and-size-fields) gets that type, and an import may write a better one there.
|
|
300
|
+
- **Stored as given**: the column holds the URL itself, and Protobase never fetches, uploads or copies it. It has no size unless an import wrote one in the [size field](#type-and-size-fields), and responses link it as it is, as `url`, for whoever may read the row.
|
|
276
301
|
- **No variants**: every place an image is drawn, cards, tables, thumbnails, forms and the record page, uses the URL itself, and `fileUrl(file, 'card')` gives it too. Size the image at its source when it should be small.
|
|
277
|
-
- **No ThumbHash**: a write through the API sets the field's derived fields and
|
|
302
|
+
- **No ThumbHash**: a write through the API sets the field's derived fields, placeholder and size field to `null`, since there are no bytes to compute them from; the image frame stays empty until the image loads. An import that writes the rows itself may write a placeholder beside the URL, and the admin app draws that one.
|
|
278
303
|
- **Nothing to delete**: replacing an upload with a URL schedules the upload's delete as any replacement does; a URL that is replaced or cleared schedules nothing.
|
|
279
304
|
- The other site sees each viewer's IP address when it serves the image, and decides when it changes or disappears. The admin app draws it without a referrer, see [Image referrers](#image-referrers).
|
|
280
305
|
|