@glassly/miniapp-cli 0.1.16 → 0.1.17-beta.2

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.
@@ -0,0 +1,406 @@
1
+ import {createHash} from "crypto"
2
+ import {closeSync, createWriteStream, existsSync, mkdirSync, openSync, readFileSync, readSync, renameSync, rmSync, statSync, writeFileSync, writeSync} from "fs"
3
+ import {join} from "path"
4
+ import {Readable} from "stream"
5
+ import type {ReadableStream as WebReadableStream} from "stream/web"
6
+ import {pipeline} from "stream/promises"
7
+ import {pathToFileURL} from "url"
8
+ import {MiniappErrorCode} from "@glassly/miniapp"
9
+
10
+ import {inferMime, urlBasename, withDiskExt} from "./blobs-paths.js"
11
+
12
+ // Ports the Glassly App's BlobStore (mobile/modules/engine/src/services/BlobStore.ts) to Node fs.
13
+ // Bytes live in `dir/{fileName}`; the phone's MMKV metadata keys live in `dir/meta.json`.
14
+
15
+ export const BLOB_QUOTA_BYTES = 1024 * 1024 * 1024
16
+ const MD5_MAX_BYTES = 50 * 1024 * 1024
17
+ const META_FILE = "meta.json"
18
+ export const OCTET = "application/octet-stream"
19
+
20
+ type Meta = Record<string, string | number | boolean>
21
+ type SendResult = (requestId: string | undefined, ok: boolean, data?: unknown, error?: {code: string; message: string}) => void
22
+
23
+ export interface StoredBlobMeta {
24
+ key: string
25
+ fileName: string
26
+ name?: string
27
+ mimeType: string
28
+ bytes: number
29
+ createdAt: number
30
+ updatedAt: number
31
+ md5?: string
32
+ meta?: Meta
33
+ }
34
+
35
+ interface ActiveUpload {
36
+ key: string
37
+ fileName: string
38
+ fd: number
39
+ bytes: number
40
+ priorBytes: number
41
+ name?: string
42
+ mimeType: string
43
+ meta?: Meta
44
+ }
45
+
46
+ interface ActiveReader {
47
+ fd: number
48
+ position: number
49
+ remaining: number
50
+ }
51
+
52
+ const quotaError = {code: MiniappErrorCode.BLOB_QUOTA_EXCEEDED, message: `Blob quota (${BLOB_QUOTA_BYTES} bytes) exceeded`}
53
+
54
+ export class SimBlobStore {
55
+ private readonly uploads = new Map<string, ActiveUpload>()
56
+ private readonly readers = new Map<string, ActiveReader>()
57
+ private readonly downloads = new Set<AbortController>()
58
+
59
+ constructor(
60
+ private readonly dir: string,
61
+ private readonly send: SendResult,
62
+ ) {}
63
+
64
+ private path(fileName: string): string {
65
+ mkdirSync(this.dir, {recursive: true})
66
+ return join(this.dir, fileName)
67
+ }
68
+
69
+ // No prototype, so any key (`__proto__`, `toString`) is an ordinary entry.
70
+ private loadAll(): Record<string, StoredBlobMeta> {
71
+ const file = join(this.dir, META_FILE)
72
+ const all: Record<string, StoredBlobMeta> = Object.create(null)
73
+ return existsSync(file) ? Object.assign(all, JSON.parse(readFileSync(file, "utf8"))) : all
74
+ }
75
+
76
+ // Written beside the index and renamed over it, so a write that fails partway leaves the last index whole.
77
+ private saveAll(all: Record<string, StoredBlobMeta>): void {
78
+ const next = this.path(`${META_FILE}.${process.pid}.tmp`)
79
+ try {
80
+ writeFileSync(next, JSON.stringify(all))
81
+ renameSync(next, this.path(META_FILE))
82
+ } catch (err) {
83
+ rmSync(next, {force: true})
84
+ throw err
85
+ }
86
+ }
87
+
88
+ readMeta(key: string): StoredBlobMeta | null {
89
+ return this.loadAll()[key] ?? null
90
+ }
91
+
92
+ private allMeta(): StoredBlobMeta[] {
93
+ return Object.values(this.loadAll())
94
+ }
95
+
96
+ exists(fileName: string): boolean {
97
+ return existsSync(join(this.dir, fileName))
98
+ }
99
+
100
+ filePath(fileName: string): string {
101
+ return join(this.dir, fileName)
102
+ }
103
+
104
+ private toBlobMeta(m: StoredBlobMeta) {
105
+ return {
106
+ key: m.key,
107
+ name: m.name,
108
+ mimeType: m.mimeType,
109
+ bytes: m.bytes,
110
+ createdAt: m.createdAt,
111
+ updatedAt: m.updatedAt,
112
+ md5: m.md5,
113
+ uri: pathToFileURL(join(this.dir, m.fileName)).href,
114
+ meta: m.meta,
115
+ }
116
+ }
117
+
118
+ private committedUsage(): number {
119
+ return this.allMeta().reduce((sum, m) => sum + (m.bytes || 0), 0)
120
+ }
121
+
122
+ private overQuota(key: string, incoming: number): boolean {
123
+ const priorBytes = this.readMeta(key)?.bytes ?? 0
124
+ return this.committedUsage() - priorBytes + incoming > BLOB_QUOTA_BYTES
125
+ }
126
+
127
+ private makeId(): string {
128
+ return `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 10)}${Math.random().toString(36).slice(2, 6)}`
129
+ }
130
+
131
+ // The new metadata is published before the old file is deleted, so a failed save keeps the last value.
132
+ private commitFile(key: string, fileName: string, mimeType: string, name: string | undefined, extraMeta: Meta | undefined) {
133
+ const named = withDiskExt(fileName, mimeType)
134
+ if (named !== fileName) {
135
+ try {
136
+ renameSync(this.path(fileName), this.path(named))
137
+ fileName = named
138
+ } catch {
139
+ /* keep the extension-less name */
140
+ }
141
+ }
142
+ const prior = this.readMeta(key)
143
+ const finalPath = this.path(fileName)
144
+ const bytes = statSync(finalPath).size
145
+ const now = Date.now()
146
+ const meta: StoredBlobMeta = {
147
+ key,
148
+ fileName,
149
+ name,
150
+ mimeType,
151
+ bytes,
152
+ createdAt: prior?.createdAt ?? now,
153
+ updatedAt: now,
154
+ md5: bytes > MD5_MAX_BYTES ? undefined : createHash("md5").update(readFileSync(finalPath)).digest("hex"),
155
+ meta: extraMeta,
156
+ }
157
+ const all = this.loadAll()
158
+ all[key] = meta
159
+ try {
160
+ this.saveAll(all)
161
+ } catch (err) {
162
+ this.deleteFileQuiet(fileName)
163
+ throw err
164
+ }
165
+ if (prior && prior.fileName !== fileName) this.deleteFileQuiet(prior.fileName)
166
+ return this.toBlobMeta(meta)
167
+ }
168
+
169
+ create(payload: Record<string, unknown>, requestId?: string): void {
170
+ try {
171
+ const key = (typeof payload.key === "string" && payload.key) || this.makeId()
172
+ const existing = this.uploads.get(key)
173
+ if (existing) this.discardUpload(existing)
174
+ const fileName = this.makeId()
175
+ const fd = openSync(this.path(fileName + ".part"), "w+")
176
+ this.uploads.set(key, {
177
+ key,
178
+ fileName,
179
+ fd,
180
+ bytes: 0,
181
+ priorBytes: this.readMeta(key)?.bytes ?? 0,
182
+ name: typeof payload.name === "string" ? payload.name : undefined,
183
+ mimeType: typeof payload.mimeType === "string" ? payload.mimeType : OCTET,
184
+ meta: (payload.meta as Meta) ?? undefined,
185
+ })
186
+ this.send(requestId, true, {key})
187
+ } catch (err) {
188
+ this.fail(requestId, MiniappErrorCode.BLOB_WRITE_FAILED, err)
189
+ }
190
+ }
191
+
192
+ write(payload: Record<string, unknown>, requestId?: string): void {
193
+ const key = payload.key as string
194
+ const up = this.uploads.get(key)
195
+ if (!up) return this.send(requestId, false, undefined, {code: MiniappErrorCode.BLOB_HANDLE_INVALID, message: `No open blob writer for key ${key}`})
196
+ try {
197
+ const bytes = Buffer.from((payload.base64 as string) ?? "", "base64")
198
+ if (typeof payload.offset === "number") {
199
+ if (payload.offset < 0 || payload.offset + bytes.length > up.bytes) {
200
+ return this.send(requestId, false, undefined, {
201
+ code: MiniappErrorCode.BLOB_WRITE_FAILED,
202
+ message: "writeAt offset out of range (must stay within written bytes)",
203
+ })
204
+ }
205
+ writeSync(up.fd, bytes, 0, bytes.length, payload.offset)
206
+ return this.send(requestId, true, {bytesWritten: up.bytes})
207
+ }
208
+ if (this.committedUsage() - up.priorBytes + up.bytes + bytes.length > BLOB_QUOTA_BYTES) {
209
+ return this.send(requestId, false, undefined, quotaError)
210
+ }
211
+ writeSync(up.fd, bytes, 0, bytes.length, up.bytes)
212
+ up.bytes += bytes.length
213
+ this.send(requestId, true, {bytesWritten: up.bytes})
214
+ } catch (err) {
215
+ this.fail(requestId, MiniappErrorCode.BLOB_WRITE_FAILED, err)
216
+ }
217
+ }
218
+
219
+ commit(payload: Record<string, unknown>, requestId?: string): void {
220
+ const key = payload.key as string
221
+ const up = this.uploads.get(key)
222
+ if (!up) return this.send(requestId, false, undefined, {code: MiniappErrorCode.BLOB_HANDLE_INVALID, message: `No open blob writer for key ${key}`})
223
+ try {
224
+ closeSync(up.fd)
225
+ renameSync(this.path(up.fileName + ".part"), this.path(up.fileName))
226
+ const extraMeta = {...(up.meta ?? {}), ...((payload.meta as Meta) ?? {})}
227
+ const meta = this.commitFile(key, up.fileName, up.mimeType, up.name, extraMeta)
228
+ this.uploads.delete(key)
229
+ this.send(requestId, true, meta)
230
+ } catch (err) {
231
+ this.discardUpload(up)
232
+ this.uploads.delete(key)
233
+ this.fail(requestId, MiniappErrorCode.BLOB_WRITE_FAILED, err)
234
+ }
235
+ }
236
+
237
+ abort(payload: Record<string, unknown>, requestId?: string): void {
238
+ const up = this.uploads.get(payload.key as string)
239
+ if (up) {
240
+ this.discardUpload(up)
241
+ this.uploads.delete(up.key)
242
+ }
243
+ this.send(requestId, true)
244
+ }
245
+
246
+ async setFromUrl(payload: Record<string, unknown>, requestId?: string): Promise<void> {
247
+ const key = payload.key as string
248
+ const url = payload.url as string
249
+ if (!key || !url) return this.fail(requestId, MiniappErrorCode.INTERNAL, new Error("setFromUrl requires key + url"))
250
+ if (url.startsWith("file:") || url.startsWith("javascript:")) {
251
+ return this.send(requestId, false, undefined, {code: MiniappErrorCode.BLOB_DOWNLOAD_FAILED, message: "Unsupported URL scheme"})
252
+ }
253
+ const headers = payload.headers as Record<string, string> | undefined
254
+ const fileName = this.makeId()
255
+ const partPath = this.path(fileName + ".part")
256
+ const download = new AbortController()
257
+ this.downloads.add(download)
258
+ try {
259
+ const response = await fetch(url, {signal: download.signal, ...(headers ? {headers} : {})})
260
+ if (!response.ok || !response.body) throw new Error(`Failed to download ${url}: HTTP ${response.status}`)
261
+ await pipeline(Readable.fromWeb(response.body as unknown as WebReadableStream), createWriteStream(partPath))
262
+ download.signal.throwIfAborted()
263
+ const size = statSync(partPath).size
264
+ if (this.overQuota(key, size)) {
265
+ rmSync(partPath, {force: true})
266
+ return this.send(requestId, false, undefined, quotaError)
267
+ }
268
+ renameSync(partPath, this.path(fileName))
269
+ const served = response.headers.get("content-type")?.split(";")[0].trim()
270
+ const mimeType = (payload.mimeType as string) || inferMime(urlBasename(url)) || served || OCTET
271
+ const name = (payload.name as string) || urlBasename(url) || key
272
+ this.send(requestId, true, this.commitFile(key, fileName, mimeType, name, payload.meta as Meta | undefined))
273
+ } catch (err) {
274
+ rmSync(partPath, {force: true})
275
+ this.fail(requestId, MiniappErrorCode.BLOB_DOWNLOAD_FAILED, err)
276
+ } finally {
277
+ this.downloads.delete(download)
278
+ }
279
+ }
280
+
281
+ get(payload: Record<string, unknown>, requestId?: string): void {
282
+ const meta = this.readMeta(payload.key as string)
283
+ if (!meta || !this.exists(meta.fileName)) {
284
+ if (meta) this.dropMeta(meta)
285
+ return this.send(requestId, true, null)
286
+ }
287
+ this.send(requestId, true, this.toBlobMeta(meta))
288
+ }
289
+
290
+ list(requestId?: string): void {
291
+ const blobs = this.allMeta()
292
+ .filter((m) => this.exists(m.fileName))
293
+ .sort((a, b) => b.createdAt - a.createdAt)
294
+ .map((m) => this.toBlobMeta(m))
295
+ this.send(requestId, true, {blobs})
296
+ }
297
+
298
+ usage(requestId?: string): void {
299
+ const metas = this.allMeta()
300
+ this.send(requestId, true, {bytes: metas.reduce((s, m) => s + (m.bytes || 0), 0), count: metas.length, quotaBytes: BLOB_QUOTA_BYTES})
301
+ }
302
+
303
+ delete(payload: Record<string, unknown>, requestId?: string): void {
304
+ const meta = this.readMeta(payload.key as string)
305
+ if (meta) this.dropMeta(meta)
306
+ this.send(requestId, true)
307
+ }
308
+
309
+ clear(requestId?: string): void {
310
+ try {
311
+ this.closeHandles()
312
+ rmSync(this.dir, {recursive: true, force: true})
313
+ this.send(requestId, true)
314
+ } catch (err) {
315
+ this.fail(requestId, MiniappErrorCode.INTERNAL, err)
316
+ }
317
+ }
318
+
319
+ openRead(payload: Record<string, unknown>, requestId?: string): void {
320
+ const key = payload.key as string
321
+ const meta = this.readMeta(key)
322
+ if (!meta || !this.exists(meta.fileName)) {
323
+ return this.send(requestId, false, undefined, {code: MiniappErrorCode.BLOB_NOT_FOUND, message: `Blob ${key} not found`})
324
+ }
325
+ try {
326
+ const fd = openSync(this.path(meta.fileName), "r")
327
+ const handle = `r_${this.makeId()}`
328
+ this.readers.set(handle, {fd, position: 0, remaining: meta.bytes})
329
+ this.send(requestId, true, {handle, meta: this.toBlobMeta(meta)})
330
+ } catch (err) {
331
+ this.fail(requestId, MiniappErrorCode.INTERNAL, err)
332
+ }
333
+ }
334
+
335
+ read(payload: Record<string, unknown>, requestId?: string): void {
336
+ const handle = payload.handle as string
337
+ const reader = this.readers.get(handle)
338
+ if (!reader) return this.send(requestId, false, undefined, {code: MiniappErrorCode.BLOB_HANDLE_INVALID, message: "Unknown blob read handle"})
339
+ try {
340
+ const want = Math.max(0, Math.min(Number(payload.maxBytes) || 1024 * 1024, reader.remaining))
341
+ const buffer = Buffer.alloc(want)
342
+ const read = want > 0 ? readSync(reader.fd, buffer, 0, want, reader.position) : 0
343
+ reader.position += read
344
+ reader.remaining -= read
345
+ const done = reader.remaining <= 0
346
+ if (done) {
347
+ closeQuiet(reader.fd)
348
+ this.readers.delete(handle)
349
+ }
350
+ this.send(requestId, true, {base64: buffer.subarray(0, read).toString("base64"), done})
351
+ } catch (err) {
352
+ this.fail(requestId, MiniappErrorCode.INTERNAL, err)
353
+ }
354
+ }
355
+
356
+ closeRead(payload: Record<string, unknown>, requestId?: string): void {
357
+ const handle = payload.handle as string
358
+ const reader = this.readers.get(handle)
359
+ if (reader) {
360
+ closeQuiet(reader.fd)
361
+ this.readers.delete(handle)
362
+ }
363
+ this.send(requestId, true)
364
+ }
365
+
366
+ closeHandles(): void {
367
+ for (const download of this.downloads) download.abort()
368
+ this.downloads.clear()
369
+ for (const up of this.uploads.values()) this.discardUpload(up)
370
+ this.uploads.clear()
371
+ for (const reader of this.readers.values()) closeQuiet(reader.fd)
372
+ this.readers.clear()
373
+ }
374
+
375
+ private discardUpload(up: ActiveUpload): void {
376
+ closeQuiet(up.fd)
377
+ rmSync(join(this.dir, up.fileName + ".part"), {force: true})
378
+ }
379
+
380
+ private dropMeta(meta: StoredBlobMeta): void {
381
+ this.deleteFileQuiet(meta.fileName)
382
+ const all = this.loadAll()
383
+ delete all[meta.key]
384
+ this.saveAll(all)
385
+ }
386
+
387
+ private deleteFileQuiet(fileName: string): void {
388
+ try {
389
+ rmSync(join(this.dir, fileName), {force: true})
390
+ } catch {
391
+ /* ignore */
392
+ }
393
+ }
394
+
395
+ fail(requestId: string | undefined, code: string, err: unknown): void {
396
+ this.send(requestId, false, undefined, {code, message: err instanceof Error ? err.message : "Blob error"})
397
+ }
398
+ }
399
+
400
+ function closeQuiet(fd: number): void {
401
+ try {
402
+ closeSync(fd)
403
+ } catch {
404
+ /* ignore */
405
+ }
406
+ }
@@ -0,0 +1,191 @@
1
+ /// <reference types="bun-types" />
2
+
3
+ import {afterAll, describe, expect, test} from "bun:test"
4
+ import {mkdtempSync, rmSync} from "fs"
5
+ import {tmpdir} from "os"
6
+ import {join} from "path"
7
+ import {MiniappSession, type Transport} from "@glassly/miniapp"
8
+
9
+ import {simDevice} from "../device.js"
10
+ import {SimHost, type SimManifest} from "../host.js"
11
+ import {createSimStorage} from "../storage.js"
12
+
13
+ const stateDirs: string[] = []
14
+ afterAll(() => stateDirs.forEach((dir) => rmSync(dir, {recursive: true, force: true})))
15
+
16
+ function newStateDir(): string {
17
+ const dir = mkdtempSync(join(tmpdir(), "glassly-sim-blobs-test-"))
18
+ stateDirs.push(dir)
19
+ return dir
20
+ }
21
+
22
+ async function connect(stateDir?: string, manifest: Partial<SimManifest> = {}) {
23
+ let deliver: (raw: string) => void = () => {}
24
+ const host = new SimHost({
25
+ manifest: {packageName: "com.example.sim", name: "Sim Test", ...manifest},
26
+ device: simDevice("simulated"),
27
+ storage: createSimStorage(),
28
+ stateDir,
29
+ toBackground: (raw) => queueMicrotask(() => deliver(raw)),
30
+ emit: () => {},
31
+ frameLatencyMs: 0,
32
+ speechMsPerChar: 0,
33
+ })
34
+ const transport: Transport = {
35
+ open: async () => {},
36
+ send: (raw) => queueMicrotask(() => host.fromBackground(raw)),
37
+ onMessage: (handler) => {
38
+ deliver = handler
39
+ },
40
+ onDisconnect: () => {},
41
+ close: () => {},
42
+ isOpen: () => true,
43
+ }
44
+ host.backgroundStarted()
45
+ const session = new MiniappSession({transport, packageName: "com.example.sim"})
46
+ await session.connect()
47
+ return {host, session}
48
+ }
49
+
50
+ const text = (bytes: Uint8Array | null) => (bytes ? new TextDecoder().decode(bytes) : null)
51
+
52
+ describe("blob storage", () => {
53
+ test("stores bytes in chunks and reads them back with the phone's metadata", async () => {
54
+ const {session} = await connect()
55
+ const writer = await session.blob.createWriteStream("note", {mimeType: "text/plain", name: "note.txt"})
56
+ await writer.write(new TextEncoder().encode("hel"))
57
+ await writer.write(new TextEncoder().encode("lo"))
58
+ const meta = await writer.close({lang: "en"})
59
+ expect(meta).toMatchObject({
60
+ key: "note",
61
+ name: "note.txt",
62
+ mimeType: "text/plain",
63
+ bytes: 5,
64
+ md5: "5d41402abc4b2a76b9719d911017c592",
65
+ meta: {lang: "en"},
66
+ })
67
+ expect(meta.uri).toMatch(/^file:\/\/.*\.txt$/)
68
+ expect(text(await session.blob.bytes("note"))).toBe("hello")
69
+ expect(await session.blob.usage()).toEqual({bytes: 5, count: 1, quotaBytes: 1073741824})
70
+ })
71
+
72
+ test("writeAt patches written bytes and refuses to write past them", async () => {
73
+ const {session} = await connect()
74
+ const writer = await session.blob.createWriteStream("clip")
75
+ await writer.write(new TextEncoder().encode("hello"))
76
+ await writer.writeAt(0, new TextEncoder().encode("J"))
77
+ await expect(writer.writeAt(4, new TextEncoder().encode("xy"))).rejects.toMatchObject({
78
+ code: "BLOB_WRITE_FAILED",
79
+ message: "writeAt offset out of range (must stay within written bytes)",
80
+ })
81
+ await writer.close()
82
+ expect(text(await session.blob.bytes("clip"))).toBe("Jello")
83
+ })
84
+
85
+ test("blobs survive a restart and are gone once the state folder's blobs are removed", async () => {
86
+ const stateDir = newStateDir()
87
+ await (await connect(stateDir)).session.blob.set("kept", new TextEncoder().encode("data"))
88
+ const restarted = await connect(stateDir)
89
+ expect(await restarted.session.blob.keys()).toEqual(["kept"])
90
+ rmSync(join(stateDir, "blobs"), {recursive: true, force: true})
91
+ expect(await (await connect(stateDir)).session.blob.get("kept")).toBeNull()
92
+ })
93
+
94
+ test("delete and clear remove blobs; reading a missing blob is BLOB_NOT_FOUND", async () => {
95
+ const {session} = await connect()
96
+ await session.blob.set("a", new TextEncoder().encode("1"))
97
+ await session.blob.set("b", new TextEncoder().encode("2"))
98
+ await session.blob.delete("a")
99
+ expect(await session.blob.keys()).toEqual(["b"])
100
+ await session.blob.clear()
101
+ expect(await session.blob.list()).toEqual([])
102
+ await expect(session.blob.createReadStream("b")).rejects.toMatchObject({code: "BLOB_NOT_FOUND", message: "Blob b not found"})
103
+ })
104
+
105
+ test("setFromUrl downloads the URL and names the blob after it", async () => {
106
+ const server = Bun.serve({port: 0, fetch: () => new Response("%PDF-")})
107
+ try {
108
+ const {session} = await connect()
109
+ const meta = await session.blob.setFromUrl("doc", `http://127.0.0.1:${server.port}/files/report%201.pdf?v=2`)
110
+ expect(meta).toMatchObject({key: "doc", name: "report 1.pdf", mimeType: "application/pdf", bytes: 5})
111
+ expect(text(await session.blob.bytes("doc"))).toBe("%PDF-")
112
+ } finally {
113
+ server.stop(true)
114
+ }
115
+ })
116
+
117
+ test("an overwrite cut off midway keeps the last saved value and index", async () => {
118
+ const stateDir = newStateDir()
119
+ const {session} = await connect(stateDir)
120
+ await session.blob.set("note", new TextEncoder().encode("original"))
121
+ await session.blob.set("other", new TextEncoder().encode("unrelated"))
122
+ const child = `
123
+ import {MiniappSession} from "@glassly/miniapp"
124
+ import {SimHost} from ${JSON.stringify(join(import.meta.dir, "../host.ts"))}
125
+ import {simDevice} from ${JSON.stringify(join(import.meta.dir, "../device.ts"))}
126
+ import {createSimStorage} from ${JSON.stringify(join(import.meta.dir, "../storage.ts"))}
127
+ let deliver = () => {}
128
+ const host = new SimHost({manifest: {packageName: "com.example.sim", name: "Sim"}, device: simDevice("simulated"), storage: createSimStorage(),
129
+ stateDir: ${JSON.stringify(stateDir)}, toBackground: (raw) => queueMicrotask(() => deliver(raw)), emit: () => {}, frameLatencyMs: 0})
130
+ host.backgroundStarted()
131
+ const session = new MiniappSession({packageName: "com.example.sim", transport: {open: async () => {}, send: (raw) => queueMicrotask(() => host.fromBackground(raw)),
132
+ onMessage: (h) => (deliver = h), onDisconnect: () => {}, close: () => {}, isOpen: () => true}})
133
+ await session.connect()
134
+ const error = await session.blob.set("note", new TextEncoder().encode("replacement"), {name: "n".repeat(2000)}).then(() => null, (e) => e.code)
135
+ console.log(error)
136
+ process.exit(0)
137
+ `
138
+ const run = Bun.spawnSync(["sh", "-c", `trap '' XFSZ; ulimit -f 2; exec "${process.execPath}" -e "$0"`, child], {cwd: join(import.meta.dir, "../../..")})
139
+ expect(run.stdout.toString().trim()).toBe("BLOB_WRITE_FAILED")
140
+ const restarted = await connect(stateDir)
141
+ expect((await restarted.session.blob.keys()).sort()).toEqual(["note", "other"])
142
+ expect(text(await restarted.session.blob.bytes("note"))).toBe("original")
143
+ expect(text(await restarted.session.blob.bytes("other"))).toBe("unrelated")
144
+ })
145
+
146
+ test("keys named like object properties are ordinary blobs", async () => {
147
+ const stateDir = newStateDir()
148
+ const {session} = await connect(stateDir)
149
+ expect(await session.blob.get("toString")).toBeNull()
150
+ await session.blob.set("__proto__", new TextEncoder().encode("keep me"))
151
+ const restarted = await connect(stateDir)
152
+ expect(await restarted.session.blob.keys()).toEqual(["__proto__"])
153
+ expect(text(await restarted.session.blob.bytes("__proto__"))).toBe("keep me")
154
+ expect(await restarted.session.blob.usage()).toEqual({bytes: 7, count: 1, quotaBytes: 1073741824})
155
+ })
156
+
157
+ test("a download still running at reset cannot change blobs saved after it", async () => {
158
+ let requested!: () => void
159
+ const arrived = new Promise<void>((resolve) => (requested = resolve))
160
+ let release!: (response: Response) => void
161
+ const held = new Promise<Response>((resolve) => (release = resolve))
162
+ const server = Bun.serve({
163
+ port: 0,
164
+ fetch: () => {
165
+ requested()
166
+ return held
167
+ },
168
+ })
169
+ try {
170
+ const {host, session} = await connect(newStateDir())
171
+ void session.blob.setFromUrl("doc", `http://127.0.0.1:${server.port}/old.txt`).catch(() => null)
172
+ await arrived
173
+ await host.reset()
174
+ await session.blob.set("doc", new TextEncoder().encode("saved after reset"))
175
+ release(new Response("old session payload"))
176
+ await new Promise((resolve) => setTimeout(resolve, 100))
177
+ expect(text(await session.blob.bytes("doc"))).toBe("saved after reset")
178
+ expect(await session.blob.keys()).toEqual(["doc"])
179
+ } finally {
180
+ server.stop(true)
181
+ }
182
+ })
183
+
184
+ test("share and import answer with marked stand-ins", async () => {
185
+ const {session, host} = await connect()
186
+ await session.blob.set("pic", new TextEncoder().encode("img"), {mimeType: "image/png"})
187
+ expect(await session.blob.share("pic")).toEqual({success: true, simulated: true} as never)
188
+ expect(await session.blob.importFile()).toBeNull()
189
+ expect(Object.keys(host.status().faked as object).sort()).toEqual(["miniapp_blob_import", "miniapp_blob_share"])
190
+ })
191
+ })