Skip to content

Commit

Permalink
Change isolated loadurl process script to ESM
Browse files Browse the repository at this point in the history
Signed-off-by: Steve Fan <[email protected]>
  • Loading branch information
stevefan1999-personal committed Jul 29, 2024
1 parent 61752b7 commit 688b5d8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
17 changes: 9 additions & 8 deletions src/_loadurl.js → src/_loadurl.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
// ctors). so we utilize `spawnSync` to spawn this program as a child process.
// alternatively we could have use `curl` but this is more portable.

const { http, https } = require('follow-redirects');
const { parse } = require('url');
const fs = require('fs');
import { http, https } from 'follow-redirects';
import { parse } from 'url';
import { lstatSync, createReadStream } from 'fs';
import process from 'node:process'

const url = process.argv[2];
if (!url) {
Expand All @@ -16,25 +17,25 @@ if (!url) {
}

try {
if (fs.lstatSync(url).isFile()) {
fs.createReadStream(url).pipe(process.stdout);
if (lstatSync(url).isFile()) {
createReadStream(url).pipe(process.stdout);
}
} catch (err) {
const purl = parse(url);

if (!purl.protocol) {
throw new Error(`unable to determine protocol from url: ${url}`);
}

const client = getHttpClient(purl.protocol);
const get = client.get(url, response => {
if (response.statusCode !== 200) {
throw new Error(`${response.statusCode} response from http get: ${response.statusMessage}`);
}

response.on('data', chunk => process.stdout.write(chunk));
});

get.once('error', err => {
throw new Error(`http error: ${err.message}`);
});
Expand Down
4 changes: 2 additions & 2 deletions src/yaml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class Yaml {
if (obj === undefined) { continue; }
if (obj === null) { continue; }
if (Array.isArray(obj) && obj.length === 0) { continue; }
if (typeof(obj) === 'object' && Object.keys(obj).length === 0) { continue; }
if (typeof (obj) === 'object' && Object.keys(obj).length === 0) { continue; }

result.push(obj);
}
Expand All @@ -103,7 +103,7 @@ export class Yaml {
* This method spawns a child process in order to perform an http call synchronously.
*/
function loadurl(url: string): string {
const script = path.join(__dirname, '_loadurl.js');
const script = path.join(__dirname, '_loadurl.mjs');
return execFileSync(process.execPath, [script, url], {
encoding: 'utf-8',
maxBuffer: MAX_DOWNLOAD_BUFFER,
Expand Down

0 comments on commit 688b5d8

Please sign in to comment.