rajt 0.0.91 → 0.0.92

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "rajt",
3
3
  "description": "A serverless bundler layer, fully typed for AWS Lambda (Node.js and LLRT) and Cloudflare Workers.",
4
- "version": "0.0.91",
4
+ "version": "0.0.92",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
7
7
  "bin": {
@@ -2,7 +2,7 @@ import { defineCommand } from 'citty'
2
2
  import { spawn } from 'node:child_process'
3
3
  import { Migrator } from 'forj'
4
4
  import { gray } from '../../utils/colors'
5
- import { _root, getRuntime, cleanDir, d1Path, wait as WAIT } from '../utils'
5
+ import { _root, getRuntime, cleanDB } from '../utils'
6
6
  import { wait, info, event, rn, error, log } from '../../utils/log'
7
7
 
8
8
  export default defineCommand({
@@ -41,8 +41,7 @@ export default defineCommand({
41
41
  wait('Running migrations')
42
42
 
43
43
  if (action.includes('fresh')) {
44
- cleanDir(d1Path)
45
- await WAIT(2000)
44
+ cleanDB(database)
46
45
  }
47
46
 
48
47
  await Migrator.compile([...migrated, ...pending])
package/src/cli/utils.ts CHANGED
@@ -3,6 +3,7 @@ import { Miniflare } from 'miniflare'
3
3
  import { mkdirSync, existsSync, statSync, readdirSync, rmSync, unlinkSync, copyFileSync, writeFileSync } from 'node:fs'
4
4
  import { readFile, stat, writeFile } from 'node:fs/promises'
5
5
  import { basename, dirname, join, relative } from 'node:path'
6
+ import crypto, { createHash } from 'node:crypto'
6
7
 
7
8
  import { findWranglerConfig, parseWranglerConfig, WRANGLER_CONFIG_FILES } from 'localflare-core'
8
9
  import type { WranglerConfig, LocalflareManifest } from 'localflare-core'
@@ -197,6 +198,37 @@ export function cleanDir(path: string) {
197
198
  } catch {}
198
199
  }
199
200
 
201
+ function durableObjectNamespace(name: string, uniqueKey: string) {
202
+ const key = crypto.createHash('sha256').update(uniqueKey).digest()
203
+ const nameHmac = crypto.createHmac('sha256', key).update(name).digest().subarray(0, 16)
204
+ return Buffer.concat([
205
+ nameHmac,
206
+ crypto.createHmac('sha256', key).update(nameHmac).digest().subarray(0, 16)
207
+ ]).toString('hex')
208
+ }
209
+
210
+ export function cleanDB(key: string, config?: WranglerConfig) {
211
+ config ??= wranglerConfig()
212
+ if (!config?.d1_databases) return
213
+ for (const db of config.d1_databases) {
214
+ if (key == db.database_name || key == db.binding) {
215
+ const ns = 'miniflare-D1DatabaseObject'
216
+ const fileName = join(ns, durableObjectNamespace(db.preview_database_id || db.database_id || db.binding, ns) +'.sqlite')
217
+
218
+ for (const file of [
219
+ fileName,
220
+ fileName +'-shm',
221
+ fileName +'-wal',
222
+ ]) {
223
+ const filePath = join(d1Path, file)
224
+ console.log('filePath:', filePath)
225
+ if (!existsSync(filePath)) continue
226
+ unlinkSync(filePath)
227
+ }
228
+ }
229
+ }
230
+ }
231
+
200
232
  export function wranglerConfig(file: string | null = null) {
201
233
  file ??= findWranglerConfig(_root)
202
234
  if (!file) {
package/src/oas.ts CHANGED
@@ -8,7 +8,7 @@ import response from './response'
8
8
  import { getHandler } from './register'
9
9
  import type { Hono } from './types'
10
10
 
11
- // @ts-ignore
11
+ // @ts-ignore TODO: remove dependency of 'package.json'...
12
12
  import packageJson from '../../../package.json'
13
13
 
14
14
  export default class OAS {