@mikrojs/quickjs 0.17.1-pr-300.20260731010039 → 0.18.0

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/apply-patches.js CHANGED
@@ -14,9 +14,7 @@
14
14
  * @mikrojs/native (which calls the patched module-management symbols).
15
15
  */
16
16
  import {execFileSync} from 'node:child_process'
17
- import {createHash} from 'node:crypto'
18
- import {existsSync, readdirSync, readFileSync, writeFileSync} from 'node:fs'
19
- import {tmpdir} from 'node:os'
17
+ import {existsSync, readdirSync} from 'node:fs'
20
18
  import {dirname, join} from 'node:path'
21
19
  import {fileURLToPath} from 'node:url'
22
20
 
@@ -24,116 +22,31 @@ const __dirname = dirname(fileURLToPath(import.meta.url))
24
22
  const qjsDir = join(__dirname, 'deps', 'quickjs')
25
23
  const patchesDir = join(__dirname, 'patches')
26
24
 
27
- function seriesHash(files) {
28
- const hash = createHash('sha256')
29
- for (const f of files) hash.update(readFileSync(join(patchesDir, f)))
30
- return hash.digest('hex')
31
- }
32
-
33
- function applyAll(files) {
34
- for (const f of files) {
35
- try {
36
- execFileSync('git', ['apply', join(patchesDir, f)], {cwd: qjsDir, stdio: 'inherit'})
37
- console.log(`@mikrojs/quickjs: applied patch ${f}`)
38
- } catch (err) {
39
- console.error(`@mikrojs/quickjs: failed to apply ${f}`, err.message)
40
- process.exit(1)
41
- }
42
- }
43
- }
44
-
45
25
  export function applyPatches() {
46
- // Published tarballs ship pre-patched sources and exclude patches/ from
47
- // the package files, so consumers return here and never need git.
48
26
  if (!existsSync(patchesDir)) return
49
27
  const files = readdirSync(patchesDir)
50
28
  .filter((f) => f.endsWith('.patch'))
51
29
  .sort()
52
- if (files.length === 0) return
53
-
54
- // Patches may stack (a later patch can modify code an earlier one added),
55
- // so they are only meaningful as a whole series applied in order to a
56
- // pristine tree. A stamp records which series the tree currently carries.
57
- const stampFile = join(qjsDir, '.patches-stamp')
58
- const hash = seriesHash(files)
59
- if (existsSync(stampFile) && readFileSync(stampFile, 'utf8').trim() === hash) {
60
- // The stamp can outlive the tree it describes (e.g. `git submodule
61
- // update` resets sources but leaves untracked files); trust it only if
62
- // the series bounds are actually present. First and last together catch
63
- // both a reset tree and a partially reverted one.
64
- try {
65
- for (const probe of [files[0], files[files.length - 1]]) {
66
- execFileSync('git', ['apply', '--reverse', '--check', join(patchesDir, probe)], {
67
- cwd: qjsDir,
68
- stdio: 'pipe',
69
- })
70
- }
71
- return // series already applied
72
- } catch {
73
- // stale stamp — fall through and re-apply
74
- }
75
- }
76
-
77
- let inRepo = true
78
- try {
79
- execFileSync('git', ['-C', qjsDir, 'rev-parse', 'HEAD'], {stdio: 'pipe'})
80
- } catch {
81
- inRepo = false
82
- }
83
-
84
- if (!inRepo) {
85
- // Sources without git metadata (should not happen: tarballs exclude
86
- // patches/ and return above). Refuse rather than guess.
87
- console.error('@mikrojs/quickjs: patches/ present but deps/quickjs is not a git checkout')
88
- process.exit(1)
89
- }
90
-
91
- // In-repo: reset to the pinned commit, then apply the whole series.
92
- // Only reset when the tree state is accounted for (a previous series
93
- // stamp, a clean tree, or a tree matching the pre-stamp-era series);
94
- // otherwise refuse rather than discard unknown local edits. Untracked
95
- // files (editor droppings, agent scratch dirs) are ignored: they cannot
96
- // conflict with `git apply` of tracked-file patches, and `checkout -- .`
97
- // could not remove them anyway.
98
- const dirty =
99
- execFileSync('git', ['-C', qjsDir, 'status', '--porcelain', '--untracked-files=no'], {
100
- encoding: 'utf8',
101
- })
102
- .split('\n')
103
- .filter((l) => l.trim()).length > 0
104
- if (dirty && !existsSync(stampFile)) {
105
- // Pre-stamp checkouts have some prefix of the series applied; the
106
- // first patch reverse-checking is strong evidence of that state.
30
+ for (const f of files) {
31
+ const patch = join(patchesDir, f)
32
+ // Probe: is the patch already applied?
107
33
  try {
108
- execFileSync('git', ['apply', '--reverse', '--check', join(patchesDir, files[0])], {
34
+ execFileSync('git', ['apply', '--reverse', '--check', patch], {
109
35
  cwd: qjsDir,
110
36
  stdio: 'pipe',
111
37
  })
38
+ continue // already applied
112
39
  } catch {
113
- console.error(
114
- '@mikrojs/quickjs: submodule tree has local changes that do not match the patch series.\n' +
115
- `Commit them as a patch or discard them: git -C ${qjsDir} checkout -- .`,
116
- )
117
- process.exit(1)
40
+ // not applied — fall through
118
41
  }
119
- }
120
- if (dirty) {
121
- // The tree is about to be discarded and re-derived from the patch
122
- // series. This also runs from CMake configure, i.e. mid-development
123
- // when someone is iterating on a patch — keep a backup of whatever
124
- // tracked changes are thrown away so an unregenerated edit is
125
- // recoverable instead of silently gone.
126
- const diff = execFileSync('git', ['-C', qjsDir, 'diff'], {encoding: 'utf8'})
127
- if (diff.length > 0) {
128
- const backup = join(tmpdir(), `quickjs-discarded-${Date.now()}.patch`)
129
- writeFileSync(backup, diff)
130
- console.log(`@mikrojs/quickjs: re-deriving submodule tree from the patch series`)
131
- console.log(`@mikrojs/quickjs: previous tracked changes saved to ${backup}`)
42
+ try {
43
+ execFileSync('git', ['apply', patch], {cwd: qjsDir, stdio: 'inherit'})
44
+ console.log(`@mikrojs/quickjs: applied patch ${f}`)
45
+ } catch (err) {
46
+ console.error(`@mikrojs/quickjs: failed to apply ${f}`, err.message)
47
+ process.exit(1)
132
48
  }
133
- execFileSync('git', ['-C', qjsDir, 'checkout', '--', '.'], {stdio: 'inherit'})
134
49
  }
135
- applyAll(files)
136
- writeFileSync(stampFile, hash + '\n')
137
50
  }
138
51
 
139
52
  // Run directly: sync the submodule, then apply patches.