cf-workers-native 0.1.6 → 0.1.8-beta.pr75.sha9c39e2f0299f

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 CHANGED
@@ -66,7 +66,7 @@ Install the package in your web project, then follow the
66
66
  [complete mobile setup](docs/integration.md#workflow):
67
67
 
68
68
  ```sh
69
- npm install --save-exact cf-workers-native@0.1.5
69
+ npm install --save-exact cf-workers-native@0.1.7
70
70
  ```
71
71
 
72
72
  Installation alone does not generate a mobile project. The guide covers adding
@@ -84,7 +84,7 @@ Open your application repository and give your assistant this prompt:
84
84
 
85
85
  ```text
86
86
  Read https://github.com/rdlabo-dev/cf-workers-native/blob/main/docs/integration.md.
87
- Add an iOS and Android version of this Workers web app using cf-workers-native@0.1.5
87
+ Add an iOS and Android version of this Workers web app using cf-workers-native@0.1.7
88
88
  and Capacitor. Preserve the existing pages, backend and Cloudflare deployment configuration.
89
89
  Use an existing test Worker for cloud-backed requests; ask before any cloud deployment.
90
90
  Start by verifying one page, navigation and a form or data action on a simulator/emulator.
@@ -254,11 +254,12 @@ export function secretMaterial(value) {
254
254
  export function generateKeySync(type, options) {
255
255
  validateOneOf(type, "type", ["hmac", "aes"]);
256
256
  validateObject(options, "options");
257
+ const { length } = options;
257
258
  if (type === "hmac")
258
- validateInteger(options.length, "options.length", 8, 65536);
259
+ validateInteger(length, "options.length", 8, 65536);
259
260
  else
260
- validateOneOf(options.length, "options.length", [128, 192, 256]);
261
- const material = new Uint8Array(Math.floor(options.length / 8));
261
+ validateOneOf(length, "options.length", [128, 192, 256]);
262
+ const material = new Uint8Array(Math.floor(length / 8));
262
263
  globalThis.crypto.getRandomValues(material);
263
264
  return createSecretKey(material);
264
265
  }
@@ -47,6 +47,67 @@ var aliases = Object.assign(
47
47
  )
48
48
  );
49
49
 
50
+ // src/shared/buffer-source.ts
51
+ var typedArrayPrototype = Object.getPrototypeOf(Uint8Array.prototype);
52
+ var typedArrayBuffer = Object.getOwnPropertyDescriptor(
53
+ typedArrayPrototype,
54
+ "buffer"
55
+ ).get;
56
+ var typedArrayOffset = Object.getOwnPropertyDescriptor(
57
+ typedArrayPrototype,
58
+ "byteOffset"
59
+ ).get;
60
+ var typedArrayLength = Object.getOwnPropertyDescriptor(
61
+ typedArrayPrototype,
62
+ "byteLength"
63
+ ).get;
64
+ var dataViewBuffer = Object.getOwnPropertyDescriptor(
65
+ DataView.prototype,
66
+ "buffer"
67
+ ).get;
68
+ var dataViewOffset = Object.getOwnPropertyDescriptor(
69
+ DataView.prototype,
70
+ "byteOffset"
71
+ ).get;
72
+ var dataViewLength = Object.getOwnPropertyDescriptor(
73
+ DataView.prototype,
74
+ "byteLength"
75
+ ).get;
76
+ var sharedLength;
77
+ try {
78
+ const prototype = typeof SharedArrayBuffer === "function" ? SharedArrayBuffer.prototype : Object.getPrototypeOf(
79
+ new WebAssembly.Memory({ initial: 0, maximum: 1, shared: true }).buffer
80
+ );
81
+ sharedLength = Object.getOwnPropertyDescriptor(prototype, "byteLength")?.get;
82
+ } catch {
83
+ }
84
+ function viewBytes(value) {
85
+ let buffer;
86
+ try {
87
+ buffer = typedArrayBuffer.call(value);
88
+ } catch {
89
+ buffer = dataViewBuffer.call(value);
90
+ try {
91
+ return new Uint8Array(
92
+ buffer,
93
+ dataViewOffset.call(value),
94
+ dataViewLength.call(value)
95
+ );
96
+ } catch {
97
+ return new Uint8Array(0);
98
+ }
99
+ }
100
+ try {
101
+ return new Uint8Array(
102
+ buffer,
103
+ typedArrayOffset.call(value),
104
+ typedArrayLength.call(value)
105
+ );
106
+ } catch {
107
+ return new Uint8Array(0);
108
+ }
109
+ }
110
+
50
111
  // src/compat/crypto/native-crypto.ts
51
112
  var instance;
52
113
  var encoder = new TextEncoder();
@@ -469,12 +530,14 @@ var NativeECDH = class _NativeECDH {
469
530
  return result(api);
470
531
  }
471
532
  setPrivateKey(data) {
533
+ data = viewBytes(data);
472
534
  inputs([data], (api, [p]) => {
473
535
  if (!api.wk_ec_set_private(this.pointer, p, data.byteLength))
474
536
  throw new Error("Private key is not valid for specified curve.");
475
537
  });
476
538
  }
477
539
  computeSecret(data) {
540
+ data = viewBytes(data);
478
541
  return inputs([data], (api, [p]) => {
479
542
  const status = api.wk_ec_secret(this.pointer, p, data.byteLength);
480
543
  if (status !== 1)
@@ -485,6 +548,7 @@ var NativeECDH = class _NativeECDH {
485
548
  });
486
549
  }
487
550
  static convertKey(data, curve, format) {
551
+ data = viewBytes(data);
488
552
  if (!data.byteLength) return new Uint8Array();
489
553
  const key = new _NativeECDH(curve);
490
554
  try {
@@ -176,11 +176,17 @@ export class FileHandle extends EventEmitter {
176
176
  validateBoolean(autoClose, "options.autoClose");
177
177
  const handle = this;
178
178
  const read = this.read.bind(this);
179
- let controller, ended = false;
179
+ let controller, ended = false, failed = false, closingAtEof = false;
180
180
  const finish = () => {
181
+ if (failed)
182
+ throw new TypeError("This ReadableStream is closed.");
183
+ if (ended && !closingAtEof)
184
+ throw new TypeError("This ReadableStream is closed.");
181
185
  if (!ended) {
182
186
  ended = true;
187
+ const request = controller.byobRequest;
183
188
  controller.close();
189
+ request?.respond(0);
184
190
  }
185
191
  };
186
192
  const stream = new ReadableStream({
@@ -191,13 +197,30 @@ export class FileHandle extends EventEmitter {
191
197
  },
192
198
  async pull(c) {
193
199
  const request = c.byobRequest, view = request.view;
194
- const { bytesRead } = await read(view, view.byteOffset, view.byteLength);
200
+ let bytesRead;
201
+ try {
202
+ ({ bytesRead } = await read(view, view.byteOffset, view.byteLength));
203
+ }
204
+ catch (error) {
205
+ failed = true;
206
+ throw error;
207
+ }
208
+ if (ended)
209
+ return;
195
210
  if (bytesRead === 0) {
196
211
  finish();
197
- if (autoClose)
198
- await handle.close();
212
+ if (autoClose) {
213
+ closingAtEof = true;
214
+ try {
215
+ await handle.close();
216
+ }
217
+ finally {
218
+ closingAtEof = false;
219
+ }
220
+ }
199
221
  }
200
- request.respond(bytesRead);
222
+ if (!ended)
223
+ request.respond(bytesRead);
201
224
  },
202
225
  async cancel() {
203
226
  ended = true;
@@ -28,6 +28,20 @@ const reads = new Set([
28
28
  "existsSync",
29
29
  ]);
30
30
  const api = { ...temporary };
31
+ // Stats values are produced by the filesystem; Workers forbids public construction.
32
+ api.Stats = new Proxy(api.Stats, {
33
+ construct() {
34
+ throw new TypeError("Illegal constructor");
35
+ },
36
+ apply() {
37
+ throw new TypeError("Class constructor Stats cannot be invoked without 'new'");
38
+ },
39
+ });
40
+ Object.defineProperty(api.Stats.prototype, "constructor", {
41
+ value: api.Stats,
42
+ configurable: true,
43
+ writable: true,
44
+ });
31
45
  for (const name of Object.keys(api))
32
46
  if (typeof api[name] === "function" && /^[a-z]/.test(name))
33
47
  api[name] = (...args) => temporary[name](...args);
@@ -130,7 +130,8 @@ export function installIo(api, promises) {
130
130
  }, (fd, buffers, position) => {
131
131
  let count = 0;
132
132
  for (const buffer of buffers) {
133
- const size = api[name === "readv" ? "readSync" : "writeSync"](fd, buffer, 0, buffer.byteLength, position === null ? null : Number(position) + count);
133
+ const bytes = viewBytes(buffer);
134
+ const size = api[name === "readv" ? "readSync" : "writeSync"](fd, bytes, 0, bytes.byteLength, position === null ? null : Number(position) + count);
134
135
  count += size;
135
136
  }
136
137
  return count;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cf-workers-native",
3
- "version": "0.1.6",
3
+ "version": "0.1.8-beta.pr75.sha9c39e2f0299f",
4
4
  "license": "MIT",
5
5
  "description": "Bring Cloudflare Workers apps to iOS and Android.",
6
6
  "repository": {
@@ -23,7 +23,7 @@
23
23
  },
24
24
  "publishConfig": {
25
25
  "access": "public",
26
- "tag": "latest"
26
+ "tag": "beta"
27
27
  },
28
28
  "type": "module",
29
29
  "files": [