1- const shlex = require ( 'shlex' ) ;
2- const child_process = require ( 'child_process' ) ;
3- const path = require ( 'path' ) ;
4- const fs = require ( 'fs' ) . promises ;
5- const { createWriteStream } = require ( 'fs' ) ;
6- const os = require ( 'os' ) ;
7- const util = require ( 'util' ) ;
8- const crypto = require ( 'crypto' ) ;
9- const randomBytes = util . promisify ( crypto . randomBytes ) ;
10- const pty = require ( 'node-pty' ) ;
11-
12- const randomToken = ( len = 16 ) => randomBytes ( len ) . then ( x => x . toString ( 'hex' ) ) ;
1+ import shlex from 'shlex' ;
2+ import pty from 'node-pty' ;
3+
4+ import { promises as fs , createWriteStream } from 'node:fs' ;
5+ import { randomBytes , createHash } from 'node:crypto' ;
6+ import { fileURLToPath } from 'node:url' ;
7+
8+ import child_process from 'node:child_process' ;
9+ import path from 'node:path' ;
10+ import os from 'node:os' ;
11+
12+ const __dirname = fileURLToPath ( new URL ( '.' , import . meta. url ) ) ;
13+
14+ const randomToken = async ( len = 16 ) => randomBytes ( len ) . toString ( 'hex' ) ;
1315
1416const run = ( cmd , options = { } ) => new Promise ( ( resolve , reject ) => {
1517 if ( process . argv . includes ( '--serial' ) ) {
@@ -18,9 +20,16 @@ const run = (cmd, options = {}) => new Promise((resolve, reject) => {
1820 }
1921
2022 const args = [
21- path . join ( __dirname , '../dist /h1.js' ) ,
23+ path . join ( __dirname , '../bin /h1.js' ) ,
2224 ...shlex . split ( cmd ) . slice ( 1 ) ,
2325 ] ;
26+ options . env = {
27+ ...options . env ,
28+ //TODO remove when it becomes stable
29+ // "Importing JSON modules is an experimental feature" - https://nodejs.org/api/esm.html#json-modules
30+ // "The Fetch API is an experimental feature" - https://nodejs.org/api/globals.html#fetch
31+ NODE_NO_WARNINGS : '1'
32+ } ;
2433 const program = child_process . spawn ( process . argv [ 0 ] , args , options ) ;
2534 const chunks = [ ] ;
2635
@@ -57,7 +66,7 @@ const runJson = async (cmd, options = {}) => {
5766
5867const runPty = async ( cmd , inputs , options = { } ) => new Promise ( ( resolve , reject ) => {
5968 const ptyProcess = pty . spawn ( process . argv [ 0 ] , [
60- path . join ( __dirname , '../dist /h1.js' ) ,
69+ path . join ( __dirname , '../bin /h1.js' ) ,
6170 ...shlex . split ( cmd ) . slice ( 1 ) ,
6271 ] , options ) ;
6372 const chunks = [ ] ;
@@ -126,19 +135,18 @@ const getName = (...names) => [...names, Date.now().toString()]
126135 . replace ( / [ ^ a - z A - Z 0 - 9 ] / g, '-' ) ;
127136
128137const downloadCachedFile = url => new Promise ( ( resolve , reject ) => {
129- const suffix = crypto . createHash ( 'sha256' ) . update ( url ) . digest ( 'hex' ) ;
138+ const suffix = createHash ( 'sha256' ) . update ( url ) . digest ( 'hex' ) ;
130139 const filename = path . join ( os . tmpdir ( ) , `test-h1-cli-v2-${ suffix } ` ) ;
131- const stream = createWriteStream ( filename ) ;
132- stream . on ( 'finish' , ( ) => resolve ( filename ) ) ;
140+
133141 fetch ( url )
134- . then ( resp => resp . body
135- . pipe ( stream )
136- . on ( 'error' , reject )
137- )
138- . catch ( reject ) ;
142+ . then ( response => response . blob ( ) )
143+ . then ( blob => fs . writeFile ( filename , blob . stream ( ) ) )
144+ . catch ( reject )
145+ . then ( ( ) => resolve ( filename ) )
146+ ;
139147} ) ;
140148
141- module . exports = {
149+ export {
142150 run ,
143151 runJson ,
144152 runPty ,
0 commit comments