Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: polyfill by bundling fileURLToPath #1949

Merged
merged 2 commits into from
Sep 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion yarn-project/archiver/src/index.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
2 changes: 1 addition & 1 deletion yarn-project/aztec-cli/src/client.ts
Original file line number Diff line number Diff line change
@@ -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];

Expand Down
2 changes: 1 addition & 1 deletion yarn-project/aztec-cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ 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';

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';
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/aztec-sandbox/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/canary/src/aztec_js_browser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
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';
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 {
Expand Down
3 changes: 2 additions & 1 deletion yarn-project/circuits.js/src/cbind/circuits.in.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
3 changes: 2 additions & 1 deletion yarn-project/circuits.js/src/cbind/constants.in.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
3 changes: 2 additions & 1 deletion yarn-project/circuits.js/src/crs/index.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/circuits.js/src/wasm/circuits_wasm.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
2 changes: 1 addition & 1 deletion yarn-project/end-to-end/src/e2e_aztec_js_browser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
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';
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 {
Expand Down
1 change: 1 addition & 0 deletions yarn-project/foundation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
73 changes: 73 additions & 0 deletions yarn-project/foundation/src/url/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/* eslint-disable */
// Copyright (c) 2014 Nathan Rajlich <[email protected]>
// 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, <host> 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;
}
2 changes: 1 addition & 1 deletion yarn-project/foundation/src/wasm/wasm_module.test.ts
Original file line number Diff line number Diff line change
@@ -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';

/**
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/noir-compiler/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down