bare-type-stripper 0.1.4 → 0.1.5

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/README.md +1 -26
  2. package/binding.c +72 -11
  3. package/index.d.ts +12 -0
  4. package/index.js +16 -10
  5. package/lib/strip.h +52 -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/README.md CHANGED
@@ -20,32 +20,7 @@ strip(`
20
20
 
21
21
  ## API
22
22
 
23
- #### `const output = strip(source[, encoding][, options])`
24
-
25
- Strip TypeScript-only syntax from `source` and return plain JavaScript as a `Buffer`. Stripped regions are replaced with spaces (newlines preserved) so the output has the same byte length as the input, keeping stack traces and source positions aligned.
26
-
27
- `source` may be a string or a `Buffer`. When a string, `encoding` selects the character encoding (defaults to `'utf8'`).
28
-
29
- Throws a `SyntaxError` when the source contains non-erasable TypeScript syntax (see below).
30
-
31
- Options are reserved.
32
-
33
- #### `const ranges = strip.lex(source[, encoding][, options])`
34
-
35
- Return the raw `[start, end, flags?]` ranges that `strip()` would erase, without applying them. The optional third element is a bitmap of flags from `strip.constants` that controls how a single byte in the range is rewritten (instead of being blanked to a space).
36
-
37
- ```js
38
- strip.lex('const x: number = 1')
39
- // [ [ 7, 16 ] ]
40
- ```
41
-
42
- #### `strip.constants`
43
-
44
- | Constant | Description |
45
- | :------- | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
46
- | `SEMI` | The first byte of the range becomes `;` instead of a space. Used on whole-statement strips (`type`, `interface`, `declare`, `import type`, `export type/interface`) and on the first stripped modifier of a class member to keep ASI from folding the preceding statement or field into what follows. |
47
- | `PAREN` | The first byte of the range becomes `)` instead of a space. Used together with a wider strip range to relocate an arrow function's closing `)` down to the line where `=>` lives, when the (now stripped) return-type annotation spanned newlines. |
48
- | `ERROR` | The range marks non-erasable TypeScript syntax that cannot be stripped to valid JavaScript. `strip()` throws a `SyntaxError` when a lexed range carries this flag; `lex()` returns the range so callers can implement their own handling. |
23
+ See the [`bare-type-stripper` reference](https://docs.pears.com/reference/bare/modules/bare-type-stripper).
49
24
 
50
25
  ## What gets stripped
51
26
 
package/binding.c CHANGED
@@ -1,31 +1,90 @@
1
1
  #include <assert.h>
2
2
  #include <bare.h>
3
3
  #include <js.h>
4
+ #include <stdbool.h>
5
+ #include <stdint.h>
4
6
  #include <stdlib.h>
5
7
  #include <string.h>
6
8
  #include <uv.h>
7
9
 
8
10
  #include "lib/strip.h"
9
11
 
12
+ static int
13
+ bare_type_stripper__get_int64(js_env_t *env, js_value_t *value, int64_t *result) {
14
+ int err;
15
+
16
+ bool is_number;
17
+ err = js_is_number(env, value, &is_number);
18
+ assert(err == 0);
19
+
20
+ if (!is_number) return -1;
21
+
22
+ err = js_get_value_int64(env, value, result);
23
+ assert(err == 0);
24
+
25
+ return 0;
26
+ }
27
+
10
28
  static js_value_t *
11
29
  bare_type_stripper_lex(js_env_t *env, js_callback_info_t *info) {
12
30
  int err;
13
31
 
14
- size_t argc = 1;
15
- js_value_t *argv[1];
32
+ size_t argc = 3;
33
+ js_value_t *argv[3];
16
34
 
17
35
  err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
18
36
  assert(err == 0);
19
37
 
20
- assert(argc == 1);
38
+ bool is_arraybuffer;
39
+ err = js_is_arraybuffer(env, argv[0], &is_arraybuffer);
40
+ assert(err == 0);
21
41
 
22
- utf8_t *input;
23
- size_t len;
24
- err = js_get_typedarray_info(env, argv[0], NULL, (void **) &input, (size_t *) &len, NULL, NULL);
42
+ if (!is_arraybuffer) {
43
+ err = js_throw_type_error(env, NULL, "Input must be a buffer");
44
+ assert(err == 0);
45
+
46
+ return NULL;
47
+ }
48
+
49
+ void *data;
50
+ size_t byte_len;
51
+ err = js_get_arraybuffer_info(env, argv[0], &data, &byte_len);
25
52
  assert(err == 0);
26
53
 
54
+ int64_t offset;
55
+ if (bare_type_stripper__get_int64(env, argv[1], &offset) < 0) {
56
+ err = js_throw_type_error(env, NULL, "Offset must be a number");
57
+ assert(err == 0);
58
+
59
+ return NULL;
60
+ }
61
+
62
+ int64_t len;
63
+ if (bare_type_stripper__get_int64(env, argv[2], &len) < 0) {
64
+ err = js_throw_type_error(env, NULL, "Length must be a number");
65
+ assert(err == 0);
66
+
67
+ return NULL;
68
+ }
69
+
70
+ if (offset < 0 || len < 0 || (uint64_t) offset + (uint64_t) len > byte_len) {
71
+ err = js_throw_range_error(env, NULL, "Offset and length are out of bounds");
72
+ assert(err == 0);
73
+
74
+ return NULL;
75
+ }
76
+
77
+ if ((uint64_t) len > UINT32_MAX) {
78
+ err = js_throw_range_error(env, NULL, "Length is out of bounds");
79
+ assert(err == 0);
80
+
81
+ return NULL;
82
+ }
83
+
84
+ utf8_t *input = (utf8_t *) data + offset;
85
+
27
86
  bare_type_stripper_t ctx;
28
- err = bare_type_stripper__lex(&ctx, input, len);
87
+ err = bare_type_stripper__lex(&ctx, input, (size_t) len);
29
88
 
30
89
  if (err < 0) {
31
90
  free(ctx.ranges);
@@ -38,17 +97,19 @@ bare_type_stripper_lex(js_env_t *env, js_callback_info_t *info) {
38
97
 
39
98
  // Hand the ranges to JS as a single Uint32Array of (start, end, flags)
40
99
  // triples.
100
+ size_t size = (size_t) ctx.len * 3;
101
+
41
102
  js_value_t *arraybuffer;
42
- uint32_t *data;
43
- err = js_create_arraybuffer(env, ctx.len * 3 * sizeof(uint32_t), (void **) &data, &arraybuffer);
103
+ uint32_t *data_out;
104
+ err = js_create_arraybuffer(env, size * sizeof(uint32_t), (void **) &data_out, &arraybuffer);
44
105
  assert(err == 0);
45
106
 
46
- if (ctx.len) memcpy(data, ctx.ranges, ctx.len * 3 * sizeof(uint32_t));
107
+ if (ctx.len) memcpy(data_out, ctx.ranges, size * sizeof(uint32_t));
47
108
 
48
109
  free(ctx.ranges);
49
110
 
50
111
  js_value_t *ranges;
51
- err = js_create_typedarray(env, js_uint32array, ctx.len * 3, arraybuffer, 0, &ranges);
112
+ err = js_create_typedarray(env, js_uint32array, size, arraybuffer, 0, &ranges);
52
113
  assert(err == 0);
53
114
 
54
115
  return ranges;
package/index.d.ts CHANGED
@@ -1,5 +1,17 @@
1
1
  import Buffer, { BufferEncoding } from 'bare-buffer'
2
2
 
3
+ /**
4
+ * Strip TypeScript-only syntax from `input` and return plain JavaScript as a `Buffer`. Stripped
5
+ * regions are replaced with spaces (newlines preserved) so the output has the same byte length as
6
+ * the input, keeping stack traces and source positions aligned.
7
+ * @param input - The TypeScript source to strip, as a string or a `Buffer`.
8
+ * @param encoding - Encoding used to decode `input` when it is a string (default `'utf8'`); ignored
9
+ * when `input` is already a `Buffer`.
10
+ * @param opts - An options object; currently unused.
11
+ * @throws {TypeError} `input` is neither a string nor a buffer.
12
+ * @throws {SyntaxError} the source contains non-erasable TypeScript syntax (`enum`/`const enum`,
13
+ * `namespace`/`module` with a body, parameter properties, or angle-bracket type assertions).
14
+ */
3
15
  declare function strip(input: string | Buffer, encoding?: BufferEncoding, opts?: object): Buffer
4
16
 
5
17
  declare namespace strip {
package/index.js CHANGED
@@ -6,13 +6,9 @@ module.exports = exports = function strip(input, encoding, opts = {}) {
6
6
  encoding = null
7
7
  }
8
8
 
9
- if (typeof input !== 'string' && !ArrayBuffer.isView(input)) {
10
- throw new TypeError(`Input must be a string or buffer. Received type ${typeof input}`)
11
- }
12
-
13
- const buffer = typeof input === 'string' ? Buffer.from(input, encoding) : input
9
+ const buffer = toBuffer(input, encoding)
14
10
 
15
- const ranges = binding.lex(buffer)
11
+ const ranges = binding.lex(buffer.buffer, buffer.byteOffset, buffer.byteLength)
16
12
 
17
13
  check(buffer, ranges)
18
14
 
@@ -25,11 +21,9 @@ exports.lex = function lex(input, encoding, opts = {}) {
25
21
  encoding = null
26
22
  }
27
23
 
28
- if (typeof input !== 'string' && !ArrayBuffer.isView(input)) {
29
- throw new TypeError(`Input must be a string or buffer. Received type ${typeof input}`)
30
- }
24
+ const buffer = toBuffer(input, encoding)
31
25
 
32
- const ranges = binding.lex(typeof input === 'string' ? Buffer.from(input, encoding) : input)
26
+ const ranges = binding.lex(buffer.buffer, buffer.byteOffset, buffer.byteLength)
33
27
 
34
28
  // The binding returns the ranges as a flat Uint32Array of (start, end,
35
29
  // flags) triples; reshape into the nested form.
@@ -68,6 +62,18 @@ exports.constants = {
68
62
  ERROR: binding.ERROR
69
63
  }
70
64
 
65
+ function toBuffer(input, encoding) {
66
+ if (typeof input === 'string') return Buffer.from(input, encoding)
67
+
68
+ if (!ArrayBuffer.isView(input)) {
69
+ throw new TypeError(`Input must be a string or buffer. Received type ${typeof input}`)
70
+ }
71
+
72
+ // Views with a wider element type, such as 'Float64Array' or 'DataView',
73
+ // are lexed as the bytes they span rather than as their elements.
74
+ return Buffer.from(input.buffer, input.byteOffset, input.byteLength)
75
+ }
76
+
71
77
  function check(buffer, ranges) {
72
78
  for (let r = 0; r < ranges.length; r += 3) {
73
79
  if ((ranges[r + 2] & binding.ERROR) === 0) continue
package/lib/strip.h CHANGED
@@ -20,6 +20,11 @@ enum {
20
20
  #define BARE_TYPE_STRIPPER__PAREN_CACHE_SIZE 512 // Power of two
21
21
  #define BARE_TYPE_STRIPPER__PAREN_STACK_SIZE 512
22
22
 
23
+ // Arrow-function lookahead budget, as a multiple of the input length. Cached
24
+ // matches keep well-formed input far below this; it only binds on input
25
+ // crafted to defeat the cache.
26
+ #define BARE_TYPE_STRIPPER__SCAN_BUDGET_FACTOR 64
27
+
23
28
  // Lexer context. Carries the input, the accumulated strip ranges, and a
24
29
  // small cache of paren match positions used by the arrow-function lookahead.
25
30
  typedef struct {
@@ -39,6 +44,13 @@ typedef struct {
39
44
  // position 2 or later, so 0 can't be a valid close.
40
45
  size_t paren_open[BARE_TYPE_STRIPPER__PAREN_CACHE_SIZE];
41
46
  size_t paren_close[BARE_TYPE_STRIPPER__PAREN_CACHE_SIZE];
47
+
48
+ // Bytes the arrow-function lookahead may still scan. The match cache keeps
49
+ // well-formed input linear, but a '(' that never closes can't be cached, so
50
+ // without a budget every '(' in an unclosed tail would rescan it. Running
51
+ // the budget down stops the lookahead from recognizing further arrow
52
+ // functions, degrading the same way exceeding MAX_DEPTH does.
53
+ size_t scan_budget;
42
54
  } bare_type_stripper_t;
43
55
 
44
56
  // Previous-token classification, used to decide how to interpret the next
@@ -99,16 +111,18 @@ bare_type_stripper__add_range_flags(bare_type_stripper_t *ctx, size_t start, siz
99
111
  assert(end > start);
100
112
 
101
113
  if (ctx->len == ctx->cap) {
114
+ if (ctx->cap > UINT32_MAX / 2) return -1;
115
+
102
116
  uint32_t cap = ctx->cap == 0 ? 64 : ctx->cap * 2;
103
117
 
104
- uint32_t *ranges = realloc(ctx->ranges, cap * 3 * sizeof(uint32_t));
118
+ uint32_t *ranges = realloc(ctx->ranges, (size_t) cap * 3 * sizeof(uint32_t));
105
119
  if (ranges == NULL) return -1;
106
120
 
107
121
  ctx->ranges = ranges;
108
122
  ctx->cap = cap;
109
123
  }
110
124
 
111
- uint32_t *entry = &ctx->ranges[ctx->len * 3];
125
+ uint32_t *entry = &ctx->ranges[(size_t) ctx->len * 3];
112
126
 
113
127
  entry[0] = (uint32_t) start;
114
128
  entry[1] = (uint32_t) end;
@@ -479,6 +493,10 @@ bare_type_stripper__scan_group(bare_type_stripper_t *ctx, size_t i) {
479
493
  size_t slot = i & (BARE_TYPE_STRIPPER__PAREN_CACHE_SIZE - 1);
480
494
  if (ctx->paren_close[slot] != 0 && ctx->paren_open[slot] == i) return ctx->paren_close[slot];
481
495
 
496
+ if (ctx->scan_budget == 0) return n;
497
+
498
+ size_t start = i;
499
+
482
500
  // Positions of the unclosed '(' at each depth, so every nested match
483
501
  // discovered along the way gets cached as well. Levels deeper than the
484
502
  // stack still balance, they just aren't recorded.
@@ -516,6 +534,25 @@ bare_type_stripper__scan_group(bare_type_stripper_t *ctx, size_t i) {
516
534
  i++;
517
535
  }
518
536
 
537
+ size_t scanned = i - start;
538
+
539
+ ctx->scan_budget = ctx->scan_budget > scanned ? ctx->scan_budget - scanned : 0;
540
+
541
+ // Groups still open at end of input never match. Record them as closing
542
+ // there so the next '(' in an unclosed tail answers from the cache instead
543
+ // of rescanning that tail.
544
+ if (depth > 0) {
545
+ int open_depth = depth < BARE_TYPE_STRIPPER__PAREN_STACK_SIZE ? depth : BARE_TYPE_STRIPPER__PAREN_STACK_SIZE;
546
+
547
+ for (int d = 0; d < open_depth; d++) {
548
+ size_t open = opens[d];
549
+ size_t unclosed_slot = open & (BARE_TYPE_STRIPPER__PAREN_CACHE_SIZE - 1);
550
+
551
+ ctx->paren_open[unclosed_slot] = open;
552
+ ctx->paren_close[unclosed_slot] = i;
553
+ }
554
+ }
555
+
519
556
  return i;
520
557
  }
521
558
 
@@ -1199,9 +1236,11 @@ bare_type_stripper__process_class_body(bare_type_stripper_t *ctx, size_t *result
1199
1236
  i = bare_type_stripper__skip_method_head_prefix(s, n, i, true);
1200
1237
 
1201
1238
  // Member name.
1202
- if (u(0) == '#') i++; // Private name
1239
+ if (i < n && u(0) == '#') i++; // Private name
1240
+
1241
+ if (i >= n) break;
1203
1242
 
1204
- if (i < n && ids(u(0))) {
1243
+ if (ids(u(0))) {
1205
1244
  while (i < n && id(u(0))) i++;
1206
1245
  } else if (u(0) == '\'' || u(0) == '"') {
1207
1246
  i = bare_type_stripper__skip_string(s, n, i);
@@ -1679,6 +1718,13 @@ bare_type_stripper__process_named_specifiers(bare_type_stripper_t *ctx, size_t *
1679
1718
  if (i >= n) break;
1680
1719
  if (u(0) == '}') break;
1681
1720
 
1721
+ // Nothing below consumes a byte that can't open a specifier name, so step
1722
+ // over it here to keep the loop advancing.
1723
+ if (!id(u(0)) && u(0) != '\'' && u(0) != '"' && !(allow_star && u(0) == '*')) {
1724
+ i++;
1725
+ continue;
1726
+ }
1727
+
1682
1728
  size_t spec_start = i;
1683
1729
  bool is_type = false;
1684
1730
 
@@ -3060,6 +3106,8 @@ bare_type_stripper__lex(bare_type_stripper_t *ctx, const utf8_t *s, size_t n) {
3060
3106
  ctx->len = 0;
3061
3107
  ctx->cap = 0;
3062
3108
 
3109
+ ctx->scan_budget = n * BARE_TYPE_STRIPPER__SCAN_BUDGET_FACTOR;
3110
+
3063
3111
  memset(ctx->paren_close, 0, sizeof(ctx->paren_close));
3064
3112
 
3065
3113
  return bare_type_stripper__walk(ctx, &i, 0, true, false, 0);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-type-stripper",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "description": "Heuristic lexer for stripping TypeScript type syntax to produce plain JavaScript",
5
5
  "exports": {
6
6
  "./package": "./package.json",