-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create unenv-preset package (#96)
* feat: create unenv-preset package * fix: update polyfill paths and change global polyfills in unenv-preset * fix: add crypto-browserify as a dependency in unenv-preset --------- Co-authored-by: José Filho <jc.bsfilho@gmail.com>
- Loading branch information
1 parent
8efe7ba
commit ddcf7fc
Showing
18 changed files
with
1,615 additions
and
236 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
dist |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** @type {import('ts-jest').JestConfigWithTsJest} */ | ||
module.exports = { | ||
displayName: 'Unenv Preset', | ||
preset: 'ts-jest', | ||
transform: { | ||
'^.+\\.(t|j)s?$': '@swc/jest', | ||
}, | ||
testPathIgnorePatterns: ['/node_modules/', '/dist/'], | ||
testEnvironment: 'node', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"name": "@lib/unenv-preset", | ||
"version": "1.0.0", | ||
"description": "Azion Unenv preset.", | ||
"main": "./dist/index.js", | ||
"module": "./dist/index.mjs", | ||
"types": "./dist/index.d.ts", | ||
"scripts": { | ||
"compile": "tsup --config ../../tsup.config.json", | ||
"lint": "eslint .", | ||
"lint:fix": "eslint --fix .", | ||
"prettier": "prettier --write .", | ||
"test": "jest --clearCache && jest -c jest.config.js .", | ||
"test:watch": "jest -c jest.config.js . --watch", | ||
"test:coverage": "jest --clearCache && jest -c jest.config.js . --coverage" | ||
}, | ||
"author": "aziontech", | ||
"license": "MIT", | ||
"files": [ | ||
"dist", | ||
"package.json", | ||
"src/polyfills/*" | ||
], | ||
"dependencies": { | ||
"crypto-browserify": "^3.12.1" | ||
} | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import path from 'path'; | ||
import { fileURLToPath } from 'url'; | ||
|
||
const __filename = fileURLToPath(import.meta.url); | ||
const __dirname = path.dirname(__filename); | ||
|
||
const getAbsolutePath = () => path.resolve(__dirname, '../', 'src'); | ||
|
||
const nextNodePresetPath = `${getAbsolutePath()}/polyfills/node/frameworks/next`; | ||
const polyfillsPath = `${getAbsolutePath()}/polyfills`; | ||
|
||
export default { | ||
inject: { | ||
__dirname: `${polyfillsPath}/node/globals/path-dirname.js`, | ||
__filename: `${polyfillsPath}/node/globals/path-filename.js`, | ||
process: `${polyfillsPath}/node/globals/process.cjs`, | ||
}, | ||
alias: { | ||
'azion/utils': 'azion/utils', | ||
'@fastly/http-compute-js': '@fastly/http-compute-js', | ||
'next/dist/compiled/etag': `${nextNodePresetPath}/custom-server/12.3.x/util/etag.js`, | ||
accepts: 'accepts', | ||
crypto: `${polyfillsPath}/node/crypto.js`, | ||
events: 'events/events.js', | ||
http: 'stream-http', | ||
module: `${polyfillsPath}/node/module.js`, | ||
stream: 'stream-browserify/', | ||
string_decoder: 'string_decoder/lib/string_decoder.js', | ||
url: 'url/url.js', | ||
util: 'util/util.js', | ||
timers: 'timers-browserify/', | ||
inherits: 'inherits/inherits_browser.js', | ||
vm: 'vm-browserify/', | ||
zlib: 'browserify-zlib', | ||
}, | ||
external: ['node:async_hooks', 'node:fs/promises'], | ||
polyfill: [ | ||
'aziondev:async_hooks:/async-hooks/async-hooks.polyfills.js', | ||
'aziondev:fs:/fs/fs.polyfills.js', | ||
'aziondev:fs/promises:/fs/promises/promises.polyfills.js', | ||
`azionprd:fs:/fs.js`, | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* eslint-disable */ | ||
import crypto from 'crypto-browserify'; | ||
|
||
crypto.webcrypto = globalThis.crypto; | ||
export default crypto; | ||
export var { Cipher } = crypto; | ||
export var { Cipheriv } = crypto; | ||
export var { Decipher } = crypto; | ||
export var { Decipheriv } = crypto; | ||
export var { DiffieHellman } = crypto; | ||
export var { DiffieHellmanGroup } = crypto; | ||
export var { Hash } = crypto; | ||
export var { Hmac } = crypto; | ||
export var { Sign } = crypto; | ||
export var { Verify } = crypto; | ||
export var { constants } = crypto; | ||
export var { createCipher } = crypto; | ||
export var { createCipheriv } = crypto; | ||
export var { createCredentials } = crypto; | ||
export var { createDecipher } = crypto; | ||
export var { createDecipheriv } = crypto; | ||
export var { createDiffieHellman } = crypto; | ||
export var { createDiffieHellmanGroup } = crypto; | ||
export var { createECDH } = crypto; | ||
export var { createHash } = crypto; | ||
export var { createHmac } = crypto; | ||
export var { createSign } = crypto; | ||
export var { createVerify } = crypto; | ||
export var { getCiphers } = crypto; | ||
export var { getDiffieHellman } = crypto; | ||
export var { getHashes } = crypto; | ||
export var { listCiphers } = crypto; | ||
export var { pbkdf2 } = crypto; | ||
export var { pbkdf2Sync } = crypto; | ||
export var { privateDecrypt } = crypto; | ||
export var { privateEncrypt } = crypto; | ||
export var { prng } = crypto; | ||
export var { pseudoRandomBytes } = crypto; | ||
export var { publicDecrypt } = crypto; | ||
export var { publicEncrypt } = crypto; | ||
export var { randomBytes } = crypto; | ||
export var { randomFill } = crypto; | ||
export var { randomFillSync } = crypto; | ||
export var { rng } = crypto; | ||
export var { webcrypto } = crypto; | ||
|
||
export var getRandomValues = function (abv) { | ||
let l = abv.length; | ||
while (l--) { | ||
const bytes = randomBytes(7); | ||
let randomFloat = (bytes[0] % 32) / 32; | ||
|
||
for (let i = 0; i < bytes.length; i++) { | ||
const byte = bytes[i]; | ||
randomFloat = (randomFloat + byte) / 256; | ||
} | ||
|
||
abv[l] = Math.floor(randomFloat * 256); | ||
} | ||
return abv; | ||
}; | ||
|
||
export var randomUUID = function () { | ||
return '10000000-1000-4000-8000-100000000000'.replace(/[018]/g, function (c) { | ||
return ( | ||
c ^ | ||
(getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4))) | ||
).toString(16); | ||
}); | ||
}; |
33 changes: 33 additions & 0 deletions
33
packages/unenv-preset/src/polyfills/node/frameworks/next/custom-server/12.3.x/util/etag.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright Azion | ||
* Licensed under the MIT license. See LICENSE file for details. | ||
* | ||
* Portions of this file Copyright Fastly, Inc, licensed under the MIT license. See LICENSE file for details. | ||
*/ | ||
|
||
import { Buffer } from 'buffer'; | ||
import crypto from 'crypto'; | ||
|
||
/** | ||
* Generates an etag string based on a payload. | ||
* (An adaptation for Compute@Edge of function in Next.js of the same name, | ||
* found at next/server/api-utils/web.ts) | ||
* @param {string} payload data to generate etag | ||
* @returns {string} the generated etag | ||
*/ | ||
export default function generateETag(payload) { | ||
if (payload.length === 0) { | ||
// fast-path empty | ||
return '"0-2jmj7l5rSw0yVb/vlWAYkK/YBwk"'; | ||
} | ||
|
||
// compute hash of entity | ||
// Do this without using crypto.subtle, as the crypto | ||
// polyfill doesn't support it. | ||
const hash = crypto.createHash('sha1').update(payload, 'utf8').digest('base64').substring(0, 27); | ||
|
||
// compute length of entity | ||
const len = Buffer.byteLength(payload); | ||
|
||
return `"${len.toString(16)}-${hash}"`; | ||
} |
Oops, something went wrong.