| 49 | return createSessionStorage({ |
| 50 | cookie, |
| 51 | async createData(data, expires) { |
| 52 | // eslint-disable-next-line no-constant-condition |
| 53 | while (true) { |
| 54 | const randomBytes = new Uint8Array(8); |
| 55 | crypto.getRandomValues(randomBytes); |
| 56 | // This storage manages an id space of 2^64 ids, which is far greater |
| 57 | // than the maximum number of files allowed on an NTFS or ext4 volume |
| 58 | // (2^32). However, the larger id space should help to avoid collisions |
| 59 | // with existing ids when creating new sessions, which speeds things up. |
| 60 | const id = [...randomBytes] |
| 61 | .map((x) => x.toString(16).padStart(2, "0")) |
| 62 | .join(""); |
| 63 | |
| 64 | if (await party.get(id)) { |
| 65 | continue; |
| 66 | } |
| 67 | |
| 68 | await party.put(id, JSON.stringify(data), { |
| 69 | expiration: expires ? Math.round(expires.getTime() / 1000) : undefined |
| 70 | }); |
| 71 | |
| 72 | return id; |
| 73 | } |
| 74 | }, |
| 75 | async readData(id) { |
| 76 | const session = await party.get(id); |
| 77 | |