bare-type-stripper 0.1.5 → 0.1.6

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.
Files changed (29) hide show
  1. package/CMakeLists.txt +1 -0
  2. package/README.md +4 -0
  3. package/binding.c +33 -2
  4. package/index.d.ts +10 -1
  5. package/index.js +5 -4
  6. package/package.json +1 -1
  7. package/prebuilds/android-arm/bare-type-stripper.bare +0 -0
  8. package/prebuilds/android-arm/bare-type-stripper.node +0 -0
  9. package/prebuilds/android-arm64/bare-type-stripper.bare +0 -0
  10. package/prebuilds/android-arm64/bare-type-stripper.node +0 -0
  11. package/prebuilds/android-ia32/bare-type-stripper.bare +0 -0
  12. package/prebuilds/android-ia32/bare-type-stripper.node +0 -0
  13. package/prebuilds/android-x64/bare-type-stripper.bare +0 -0
  14. package/prebuilds/android-x64/bare-type-stripper.node +0 -0
  15. package/prebuilds/darwin-arm64/bare-type-stripper.bare +0 -0
  16. package/prebuilds/darwin-arm64/bare-type-stripper.node +0 -0
  17. package/prebuilds/darwin-x64/bare-type-stripper.bare +0 -0
  18. package/prebuilds/darwin-x64/bare-type-stripper.node +0 -0
  19. package/prebuilds/ios-arm64/bare-type-stripper.bare +0 -0
  20. package/prebuilds/ios-arm64-simulator/bare-type-stripper.bare +0 -0
  21. package/prebuilds/ios-x64-simulator/bare-type-stripper.bare +0 -0
  22. package/prebuilds/linux-arm64/bare-type-stripper.bare +0 -0
  23. package/prebuilds/linux-arm64/bare-type-stripper.node +0 -0
  24. package/prebuilds/linux-x64/bare-type-stripper.bare +0 -0
  25. package/prebuilds/linux-x64/bare-type-stripper.node +0 -0
  26. package/prebuilds/win32-arm64/bare-type-stripper.bare +0 -0
  27. package/prebuilds/win32-arm64/bare-type-stripper.node +0 -0
  28. package/prebuilds/win32-x64/bare-type-stripper.bare +0 -0
  29. package/prebuilds/win32-x64/bare-type-stripper.node +0 -0
package/CMakeLists.txt CHANGED
@@ -26,6 +26,7 @@ target_compile_definitions(
26
26
  ${bare_type_stripper_napi}
27
27
  PRIVATE
28
28
  NAPI_VERSION=9
29
+ NAPI_EXPERIMENTAL
29
30
  )
30
31
 
31
32
  resolve_node_module(bare-compat-napi compat)
package/README.md CHANGED
@@ -60,6 +60,10 @@ Constructs with runtime semantics that a purely lexical stripper cannot reproduc
60
60
 
61
61
  The stripper targets plain `.ts` sources; JSX (`.tsx`) is not supported and is reported as non-erasable syntax.
62
62
 
63
+ ## Threat model
64
+
65
+ `bare-type-stripper` is one of the addons Bare compiles into its binary, so it inherits [Bare's threat model](https://github.com/holepunchto/bare/blob/main/docs/threat-model.md). See [`docs/threat-model.md`](docs/threat-model.md) for where this addon sits in it.
66
+
63
67
  ## License
64
68
 
65
69
  Apache-2.0
package/binding.c CHANGED
@@ -39,7 +39,11 @@ bare_type_stripper_lex(js_env_t *env, js_callback_info_t *info) {
39
39
  err = js_is_arraybuffer(env, argv[0], &is_arraybuffer);
40
40
  assert(err == 0);
41
41
 
42
- if (!is_arraybuffer) {
42
+ bool is_sharedarraybuffer;
43
+ err = js_is_sharedarraybuffer(env, argv[0], &is_sharedarraybuffer);
44
+ assert(err == 0);
45
+
46
+ if (!is_arraybuffer && !is_sharedarraybuffer) {
43
47
  err = js_throw_type_error(env, NULL, "Input must be a buffer");
44
48
  assert(err == 0);
45
49
 
@@ -48,7 +52,13 @@ bare_type_stripper_lex(js_env_t *env, js_callback_info_t *info) {
48
52
 
49
53
  void *data;
50
54
  size_t byte_len;
51
- err = js_get_arraybuffer_info(env, argv[0], &data, &byte_len);
55
+
56
+ if (is_sharedarraybuffer) {
57
+ err = js_get_sharedarraybuffer_info(env, argv[0], &data, &byte_len);
58
+ } else {
59
+ err = js_get_arraybuffer_info(env, argv[0], &data, &byte_len);
60
+ }
61
+
52
62
  assert(err == 0);
53
63
 
54
64
  int64_t offset;
@@ -83,9 +93,30 @@ bare_type_stripper_lex(js_env_t *env, js_callback_info_t *info) {
83
93
 
84
94
  utf8_t *input = (utf8_t *) data + offset;
85
95
 
96
+ // Another thread may write shared memory while we read it, and the ranges we
97
+ // record must describe one input. Lex a private copy instead.
98
+ utf8_t *copy = NULL;
99
+
100
+ if (is_sharedarraybuffer && len > 0) {
101
+ copy = malloc((size_t) len);
102
+
103
+ if (copy == NULL) {
104
+ err = js_throw_error(env, NULL, "Out of memory");
105
+ assert(err == 0);
106
+
107
+ return NULL;
108
+ }
109
+
110
+ memcpy(copy, input, (size_t) len);
111
+
112
+ input = copy;
113
+ }
114
+
86
115
  bare_type_stripper_t ctx;
87
116
  err = bare_type_stripper__lex(&ctx, input, (size_t) len);
88
117
 
118
+ free(copy);
119
+
89
120
  if (err < 0) {
90
121
  free(ctx.ranges);
91
122
 
package/index.d.ts CHANGED
@@ -4,7 +4,8 @@ import Buffer, { BufferEncoding } from 'bare-buffer'
4
4
  * Strip TypeScript-only syntax from `input` and return plain JavaScript as a `Buffer`. Stripped
5
5
  * regions are replaced with spaces (newlines preserved) so the output has the same byte length as
6
6
  * the input, keeping stack traces and source positions aligned.
7
- * @param input - The TypeScript source to strip, as a string or a `Buffer`.
7
+ * @param input - The TypeScript source to strip, as a string or a `Buffer`. The input is copied
8
+ * before it is lexed, so a buffer backed by a `SharedArrayBuffer` cannot change under the strip.
8
9
  * @param encoding - Encoding used to decode `input` when it is a string (default `'utf8'`); ignored
9
10
  * when `input` is already a `Buffer`.
10
11
  * @param opts - An options object; currently unused.
@@ -15,6 +16,14 @@ import Buffer, { BufferEncoding } from 'bare-buffer'
15
16
  declare function strip(input: string | Buffer, encoding?: BufferEncoding, opts?: object): Buffer
16
17
 
17
18
  declare namespace strip {
19
+ /**
20
+ * Lex `input` and return the ranges `strip()` would replace, as `[start, end, flags?]` triples.
21
+ * @param input - The TypeScript source to lex, as a string or a `Buffer`. A buffer backed by a
22
+ * `SharedArrayBuffer` is copied before it is lexed; transfer a plain `ArrayBuffer` when that copy
23
+ * matters.
24
+ * @param encoding - Encoding used to decode `input` when it is a string (default `'utf8'`).
25
+ * @param opts - An options object; currently unused.
26
+ */
18
27
  export function lex(
19
28
  input: string | Buffer,
20
29
  encoding?: BufferEncoding,
package/index.js CHANGED
@@ -6,7 +6,10 @@ module.exports = exports = function strip(input, encoding, opts = {}) {
6
6
  encoding = null
7
7
  }
8
8
 
9
- const buffer = toBuffer(input, encoding)
9
+ // Strip a copy of the input: the ranges must be applied to the same bytes
10
+ // they were lexed from, which shared memory would otherwise let another
11
+ // thread change in between.
12
+ const buffer = Buffer.from(toBuffer(input, encoding))
10
13
 
11
14
  const ranges = binding.lex(buffer.buffer, buffer.byteOffset, buffer.byteLength)
12
15
 
@@ -101,9 +104,7 @@ function check(buffer, ranges) {
101
104
  }
102
105
  }
103
106
 
104
- function apply(buffer, ranges) {
105
- const out = Buffer.from(buffer)
106
-
107
+ function apply(out, ranges) {
107
108
  // The lexer emits ranges in scan order: A single-byte flag patch that
108
109
  // overlaps a wider strip range (PAREN relocates ')' into a stripped span)
109
110
  // always follows that range, so applying in array order keeps the patched
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-type-stripper",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
4
4
  "description": "Heuristic lexer for stripping TypeScript type syntax to produce plain JavaScript",
5
5
  "exports": {
6
6
  "./package": "./package.json",