@simpleplatform/sdk 4.2.0 → 4.2.1

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,19 @@
1
+ /**
2
+ * @file Base64 for the JSON boundary, worked out by hand.
3
+ *
4
+ * A server action's runtime has neither `btoa` nor `Buffer`, so nothing here
5
+ * reaches for a global. It is arithmetic over the bytes, and it answers the same
6
+ * in every runtime the SDK runs in.
7
+ */
8
+ /**
9
+ * Encodes bytes as base64, in the standard alphabet with the standard padding
10
+ * and no line breaks, which is the form the platform accepts.
11
+ *
12
+ * The bytes are read in a loop, three at a time, and never spread into a call.
13
+ * A spread makes every byte an argument, and a call takes at most 65,535 of
14
+ * those in a server action and a stack's worth in a browser.
15
+ *
16
+ * @param bytes The bytes to encode.
17
+ * @returns The encoded text, which is empty when there are no bytes.
18
+ */
19
+ export declare function encodeBase64(bytes: Uint8Array): string;
@@ -0,0 +1,37 @@
1
+ /**
2
+ * @file Base64 for the JSON boundary, worked out by hand.
3
+ *
4
+ * A server action's runtime has neither `btoa` nor `Buffer`, so nothing here
5
+ * reaches for a global. It is arithmetic over the bytes, and it answers the same
6
+ * in every runtime the SDK runs in.
7
+ */
8
+ /** The standard alphabet of RFC 4648, which is the one the platform decodes. */
9
+ const ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
10
+ /** What takes the place of a symbol when the last group of bytes runs short. */
11
+ const PAD = '=';
12
+ /**
13
+ * Encodes bytes as base64, in the standard alphabet with the standard padding
14
+ * and no line breaks, which is the form the platform accepts.
15
+ *
16
+ * The bytes are read in a loop, three at a time, and never spread into a call.
17
+ * A spread makes every byte an argument, and a call takes at most 65,535 of
18
+ * those in a server action and a stack's worth in a browser.
19
+ *
20
+ * @param bytes The bytes to encode.
21
+ * @returns The encoded text, which is empty when there are no bytes.
22
+ */
23
+ export function encodeBase64(bytes) {
24
+ let encoded = '';
25
+ for (let start = 0; start < bytes.length; start += 3) {
26
+ const left = bytes.length - start;
27
+ // Left to right: the first byte takes the top eight of twenty-four bits. A
28
+ // read past the end answers nothing, so a group of one or two leaves the
29
+ // rest zero.
30
+ const group = ((bytes[start] ?? 0) << 16) | ((bytes[start + 1] ?? 0) << 8) | (bytes[start + 2] ?? 0);
31
+ encoded += ALPHABET.charAt(group >>> 18)
32
+ + ALPHABET.charAt((group >>> 12) & 0x3F)
33
+ + (left > 1 ? ALPHABET.charAt((group >>> 6) & 0x3F) : PAD)
34
+ + (left > 2 ? ALPHABET.charAt(group & 0x3F) : PAD);
35
+ }
36
+ return encoded;
37
+ }
package/dist/storage.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { execute as hostExecute, executeBytes as hostExecuteBytes } from './host';
2
+ import { encodeBase64 } from './internal/base64';
2
3
  /** The host action that answers a stored file's size. */
3
4
  const STAT = 'action:storage/stat';
4
5
  /** The host action that answers one range of a stored file as its bytes. */
@@ -47,9 +48,20 @@ export async function uploadBuffer(buffer, filename, mimeType, target, context)
47
48
  throw new Error('Target table_name is required and cannot be empty');
48
49
  if (!target.field_name || target.field_name.trim() === '')
49
50
  throw new Error('Target field_name is required and cannot be empty');
50
- const bytes = buffer instanceof ArrayBuffer ? new Uint8Array(buffer) : buffer;
51
+ // JavaScript callers are not held to the type, and `new Uint8Array` would
52
+ // read a number as a length and an array as values, wrapped into bytes. So
53
+ // anything but a buffer or a view is refused, from whichever realm it came.
54
+ if (!ArrayBuffer.isView(buffer) && !isArrayBuffer(buffer))
55
+ throw new Error('buffer must be an ArrayBuffer or a Uint8Array');
56
+ // A plain view over exactly the caller's bytes. A view that starts partway
57
+ // into a larger buffer sends only its own part.
58
+ const bytes = ArrayBuffer.isView(buffer)
59
+ ? new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength)
60
+ : new Uint8Array(buffer);
61
+ if (bytes.length === 0)
62
+ throw new Error('An upload needs at least one byte, and this buffer is empty.');
51
63
  // Base64-encode at the JSON boundary (JSON.stringify cannot carry raw binary)
52
- const base64 = btoa(String.fromCharCode(...bytes));
64
+ const base64 = encodeBase64(bytes);
53
65
  const source = { bytes: base64, filename, mime_type: mimeType };
54
66
  const response = await hostExecute('action:storage/upload-external', { source, target }, context);
55
67
  if (!response.ok)
@@ -243,6 +255,10 @@ async function readExactly(handle, offset, length, context) {
243
255
  throw new Error(`The file answered ${part.length} bytes for the ${length} at offset ${offset}, so it is not the size it was when the read began. Read it again.`);
244
256
  return part;
245
257
  }
258
+ /** Whether a value is an ArrayBuffer, from this realm or another. */
259
+ function isArrayBuffer(value) {
260
+ return Object.prototype.toString.call(value) === '[object ArrayBuffer]';
261
+ }
246
262
  /** The error for a call the host refused in its envelope, naming the call. */
247
263
  function refused(actionName, message) {
248
264
  if (message === undefined || message.trim() === '')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@simpleplatform/sdk",
3
- "version": "4.2.0",
3
+ "version": "4.2.1",
4
4
  "description": "Simple Platform Typescript SDK",
5
5
  "license": "Apache-2.0",
6
6
  "homepage": "https://docs.simple.dev",