From e00e50834e7f0f5270ebc3a3221e9d87d7fcd108 Mon Sep 17 00:00:00 2001 From: ludamad Date: Fri, 1 Sep 2023 16:52:23 -0400 Subject: [PATCH 1/2] fix: polyfill by bundling fileURLToPath --- yarn-project/archiver/src/index.ts | 2 +- yarn-project/aztec-cli/src/client.ts | 2 +- yarn-project/aztec-cli/src/index.ts | 2 +- yarn-project/aztec-sandbox/src/index.ts | 2 +- .../canary/src/aztec_js_browser.test.ts | 2 +- .../circuits.js/src/cbind/circuits.in.ts | 3 +- .../circuits.js/src/cbind/constants.in.ts | 3 +- yarn-project/circuits.js/src/crs/index.ts | 3 +- .../circuits.js/src/wasm/circuits_wasm.ts | 2 +- .../src/e2e_aztec_js_browser.test.ts | 2 +- yarn-project/foundation/package.json | 1 + .../src/json-rpc/client/json_rpc_client.ts | 2 +- yarn-project/foundation/src/url/index.ts | 73 +++++++++++++++++++ .../foundation/src/wasm/wasm_module.test.ts | 2 +- yarn-project/noir-compiler/src/index.test.ts | 2 +- 15 files changed, 90 insertions(+), 13 deletions(-) create mode 100644 yarn-project/foundation/src/url/index.ts diff --git a/yarn-project/archiver/src/index.ts b/yarn-project/archiver/src/index.ts index 65cb31dc34d..1ab04d4c58e 100644 --- a/yarn-project/archiver/src/index.ts +++ b/yarn-project/archiver/src/index.ts @@ -1,6 +1,6 @@ import { createDebugLogger } from '@aztec/foundation/log'; +import { fileURLToPath } from '@aztec/foundation/url'; -import { fileURLToPath } from 'url'; import { createPublicClient, http } from 'viem'; import { localhost } from 'viem/chains'; diff --git a/yarn-project/aztec-cli/src/client.ts b/yarn-project/aztec-cli/src/client.ts index 7124e3682a7..7cb5d770aa0 100644 --- a/yarn-project/aztec-cli/src/client.ts +++ b/yarn-project/aztec-cli/src/client.ts @@ -1,11 +1,11 @@ import { AztecRPC, createAztecRpcClient } from '@aztec/aztec.js'; import { makeFetch } from '@aztec/foundation/json-rpc/client'; import { DebugLogger } from '@aztec/foundation/log'; +import { fileURLToPath } from '@aztec/foundation/url'; import { readFileSync } from 'fs'; import { dirname, resolve } from 'path'; import { gtr, ltr, satisfies, valid } from 'semver'; -import { fileURLToPath } from 'url'; const retries = [1, 1, 2]; diff --git a/yarn-project/aztec-cli/src/index.ts b/yarn-project/aztec-cli/src/index.ts index 18c849a9d49..a0b73b04f35 100644 --- a/yarn-project/aztec-cli/src/index.ts +++ b/yarn-project/aztec-cli/src/index.ts @@ -12,6 +12,7 @@ import { import { StructType } from '@aztec/foundation/abi'; import { JsonStringify } from '@aztec/foundation/json-rpc'; import { DebugLogger, LogFn } from '@aztec/foundation/log'; +import { fileURLToPath } from '@aztec/foundation/url'; import { compileContract } from '@aztec/noir-compiler/cli'; import { SchnorrAccountContractAbi } from '@aztec/noir-contracts/artifacts'; import { CompleteAddress, ContractData, L2BlockL2Logs, PrivateKey, TxHash } from '@aztec/types'; @@ -19,7 +20,6 @@ import { CompleteAddress, ContractData, L2BlockL2Logs, PrivateKey, TxHash } from import { Command } from 'commander'; import { readFileSync } from 'fs'; import { dirname, resolve } from 'path'; -import { fileURLToPath } from 'url'; import { mnemonicToAccount } from 'viem/accounts'; import { createCompatibleClient } from './client.js'; diff --git a/yarn-project/aztec-sandbox/src/index.ts b/yarn-project/aztec-sandbox/src/index.ts index 4a61b70ec52..093353350f2 100644 --- a/yarn-project/aztec-sandbox/src/index.ts +++ b/yarn-project/aztec-sandbox/src/index.ts @@ -6,10 +6,10 @@ import { PrivateKey } from '@aztec/circuits.js'; import { deployL1Contracts } from '@aztec/ethereum'; import { createDebugLogger } from '@aztec/foundation/log'; import { retryUntil } from '@aztec/foundation/retry'; +import { fileURLToPath } from '@aztec/foundation/url'; import { readFileSync } from 'fs'; import { dirname, resolve } from 'path'; -import { fileURLToPath } from 'url'; import { HDAccount, createPublicClient, http as httpViemTransport } from 'viem'; import { mnemonicToAccount } from 'viem/accounts'; import { foundry } from 'viem/chains'; diff --git a/yarn-project/canary/src/aztec_js_browser.test.ts b/yarn-project/canary/src/aztec_js_browser.test.ts index c9427ae15fd..11b7d4ebffc 100644 --- a/yarn-project/canary/src/aztec_js_browser.test.ts +++ b/yarn-project/canary/src/aztec_js_browser.test.ts @@ -2,6 +2,7 @@ import * as AztecJs from '@aztec/aztec.js'; import { AztecAddress, PrivateKey } from '@aztec/circuits.js'; import { DebugLogger, createDebugLogger } from '@aztec/foundation/log'; +import { fileURLToPath } from '@aztec/foundation/url'; import { PrivateTokenContractAbi } from '@aztec/noir-contracts/artifacts'; import { Server } from 'http'; @@ -9,7 +10,6 @@ import Koa from 'koa'; import serve from 'koa-static'; import path, { dirname } from 'path'; import { Browser, Page, launch } from 'puppeteer'; -import { fileURLToPath } from 'url'; declare global { interface Window { diff --git a/yarn-project/circuits.js/src/cbind/circuits.in.ts b/yarn-project/circuits.js/src/cbind/circuits.in.ts index a32d43a5a18..2d7acee6421 100644 --- a/yarn-project/circuits.js/src/cbind/circuits.in.ts +++ b/yarn-project/circuits.js/src/cbind/circuits.in.ts @@ -1,6 +1,7 @@ +import { fileURLToPath } from '@aztec/foundation/url'; + import { writeFileSync } from 'fs'; import { dirname } from 'path'; -import { fileURLToPath } from 'url'; import { CircuitsWasm } from '../wasm/circuits_wasm.js'; import { getCbindSchema } from './cbind.js'; diff --git a/yarn-project/circuits.js/src/cbind/constants.in.ts b/yarn-project/circuits.js/src/cbind/constants.in.ts index 7b198754c81..09a621cf895 100644 --- a/yarn-project/circuits.js/src/cbind/constants.in.ts +++ b/yarn-project/circuits.js/src/cbind/constants.in.ts @@ -1,6 +1,7 @@ +import { fileURLToPath } from '@aztec/foundation/url'; + import * as fs from 'fs'; import { dirname, join } from 'path'; -import { fileURLToPath } from 'url'; import { CircuitsWasm } from '../wasm/circuits_wasm.js'; import { callCbind } from './cbind.js'; diff --git a/yarn-project/circuits.js/src/crs/index.ts b/yarn-project/circuits.js/src/crs/index.ts index 41355990cc7..05a698d15e3 100644 --- a/yarn-project/circuits.js/src/crs/index.ts +++ b/yarn-project/circuits.js/src/crs/index.ts @@ -1,8 +1,9 @@ +import { fileURLToPath } from '@aztec/foundation/url'; + import isNode from 'detect-node'; import { existsSync } from 'fs'; import { open } from 'fs/promises'; import { dirname } from 'path'; -import { fileURLToPath } from 'url'; /** * Downloader for CRS from the web or local. diff --git a/yarn-project/circuits.js/src/wasm/circuits_wasm.ts b/yarn-project/circuits.js/src/wasm/circuits_wasm.ts index c7fd6d7328d..dd299c05aee 100644 --- a/yarn-project/circuits.js/src/wasm/circuits_wasm.ts +++ b/yarn-project/circuits.js/src/wasm/circuits_wasm.ts @@ -1,10 +1,10 @@ import { numToUInt32BE } from '@aztec/foundation/serialize'; +import { fileURLToPath } from '@aztec/foundation/url'; import { IWasmModule, WasmModule } from '@aztec/foundation/wasm'; import isNode from 'detect-node'; import { readFile } from 'fs/promises'; import { dirname, join } from 'path'; -import { fileURLToPath } from 'url'; import { Crs } from '../crs/index.js'; diff --git a/yarn-project/end-to-end/src/e2e_aztec_js_browser.test.ts b/yarn-project/end-to-end/src/e2e_aztec_js_browser.test.ts index 7f322c8a373..da3b23a91b1 100644 --- a/yarn-project/end-to-end/src/e2e_aztec_js_browser.test.ts +++ b/yarn-project/end-to-end/src/e2e_aztec_js_browser.test.ts @@ -2,6 +2,7 @@ import * as AztecJs from '@aztec/aztec.js'; import { AztecAddress, PrivateKey } from '@aztec/circuits.js'; import { DebugLogger, createDebugLogger } from '@aztec/foundation/log'; +import { fileURLToPath } from '@aztec/foundation/url'; import { PrivateTokenContractAbi } from '@aztec/noir-contracts/artifacts'; import { Server } from 'http'; @@ -9,7 +10,6 @@ import Koa from 'koa'; import serve from 'koa-static'; import path, { dirname } from 'path'; import { Browser, Page, launch } from 'puppeteer'; -import { fileURLToPath } from 'url'; declare global { interface Window { diff --git a/yarn-project/foundation/package.json b/yarn-project/foundation/package.json index 6a20846a05c..d1fdfc2f79f 100644 --- a/yarn-project/foundation/package.json +++ b/yarn-project/foundation/package.json @@ -31,6 +31,7 @@ "./worker": "./dest/worker/index.js", "./bigint-buffer": "./dest/bigint-buffer/index.js", "./types": "./dest/types/index.js", + "./url": "./dest/url/index.js", "./committable": "./dest/committable/index.js" }, "scripts": { diff --git a/yarn-project/foundation/src/json-rpc/client/json_rpc_client.ts b/yarn-project/foundation/src/json-rpc/client/json_rpc_client.ts index 876f3d0fed8..d90f9fad232 100644 --- a/yarn-project/foundation/src/json-rpc/client/json_rpc_client.ts +++ b/yarn-project/foundation/src/json-rpc/client/json_rpc_client.ts @@ -11,7 +11,7 @@ import { JsonStringify, convertFromJsonObj, convertToJsonObj } from '../convert. export { JsonStringify } from '../convert.js'; -const debug = createDebugLogger('json-rpc:json_rpc_client'); +const debug = console.log; // createDebugLogger('json-rpc:json_rpc_client'); /** * A normal fetch function that does not retry. * Alternatives are a fetch function with retries, or a mocked fetch. diff --git a/yarn-project/foundation/src/url/index.ts b/yarn-project/foundation/src/url/index.ts new file mode 100644 index 00000000000..d4b90a37fb0 --- /dev/null +++ b/yarn-project/foundation/src/url/index.ts @@ -0,0 +1,73 @@ +/* eslint-disable */ +// Copyright (c) 2014 Nathan Rajlich +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// 'Software'), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +import { sep } from 'path'; + +/** + * File URI to Path function. + * + * @param {String} uri + * @return {String} path + * @api public + */ + +export function fileURLToPath(uri: string): string { + if (typeof uri !== 'string' || uri.length <= 7 || uri.substring(0, 7) !== 'file://') { + throw new TypeError('must pass in a file:// URI to convert to a file path'); + } + + const rest = decodeURI(uri.substring(7)); + const firstSlash = rest.indexOf('/'); + let host = rest.substring(0, firstSlash); + let path = rest.substring(firstSlash + 1); + + // 2. Scheme Definition + // As a special case, can be the string "localhost" or the empty + // string; this is interpreted as "the machine from which the URL is + // being interpreted". + if (host === 'localhost') { + host = ''; + } + + if (host) { + host = sep + sep + host; + } + + // 3.2 Drives, drive letters, mount points, file system root + // Drive letters are mapped into the top of a file URI in various ways, + // depending on the implementation; some applications substitute + // vertical bar ("|") for the colon after the drive letter, yielding + // "file:///c|/tmp/test.txt". In some cases, the colon is left + // unchanged, as in "file:///c:/tmp/test.txt". In other cases, the + // colon is simply omitted, as in "file:///c/tmp/test.txt". + path = path.replace(/^(.+)\|/, '$1:'); + + // for Windows, we need to invert the path separators from what a URI uses + if (sep === '\\') { + path = path.replace(/\//g, '\\'); + } + + if (/^.+:/.test(path)) { + // has Windows drive at beginning of path + } else { + // unix path… + path = sep + path; + } + + return host + path; +} diff --git a/yarn-project/foundation/src/wasm/wasm_module.test.ts b/yarn-project/foundation/src/wasm/wasm_module.test.ts index 36e3f207189..32e293e1e9b 100644 --- a/yarn-project/foundation/src/wasm/wasm_module.test.ts +++ b/yarn-project/foundation/src/wasm/wasm_module.test.ts @@ -1,7 +1,7 @@ import { readFile } from 'fs/promises'; import { dirname } from 'path'; -import { fileURLToPath } from 'url'; +import { fileURLToPath } from '../url/index.js'; import { WasmModule } from './wasm_module.js'; /** diff --git a/yarn-project/noir-compiler/src/index.test.ts b/yarn-project/noir-compiler/src/index.test.ts index d68ac0961fd..9a09d1fe8f8 100644 --- a/yarn-project/noir-compiler/src/index.test.ts +++ b/yarn-project/noir-compiler/src/index.test.ts @@ -1,8 +1,8 @@ import { ContractAbi } from '@aztec/foundation/abi'; +import { fileURLToPath } from '@aztec/foundation/url'; import { execSync } from 'child_process'; import path from 'path'; -import { fileURLToPath } from 'url'; import { compileUsingNargo, generateNoirContractInterface, generateTypescriptContractInterface } from './index.js'; From ce3252c1dc88adc839331fd3afd42139552b26c4 Mon Sep 17 00:00:00 2001 From: ludamad Date: Sat, 2 Sep 2023 17:06:12 -0400 Subject: [PATCH 2/2] Revert a debug change --- yarn-project/foundation/src/json-rpc/client/json_rpc_client.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/yarn-project/foundation/src/json-rpc/client/json_rpc_client.ts b/yarn-project/foundation/src/json-rpc/client/json_rpc_client.ts index d90f9fad232..876f3d0fed8 100644 --- a/yarn-project/foundation/src/json-rpc/client/json_rpc_client.ts +++ b/yarn-project/foundation/src/json-rpc/client/json_rpc_client.ts @@ -11,7 +11,7 @@ import { JsonStringify, convertFromJsonObj, convertToJsonObj } from '../convert. export { JsonStringify } from '../convert.js'; -const debug = console.log; // createDebugLogger('json-rpc:json_rpc_client'); +const debug = createDebugLogger('json-rpc:json_rpc_client'); /** * A normal fetch function that does not retry. * Alternatives are a fetch function with retries, or a mocked fetch.