Initial commit (by create-cloudflare CLI)

This commit is contained in:
Jonas_Jones 2023-09-12 00:20:38 +02:00
parent 8cb86120f1
commit fff961078a
1777 changed files with 1011798 additions and 0 deletions

View file

@ -0,0 +1,3 @@
require ('chai').should ()
window.path = require ('../../impl/path')
window.getSource = require ('../../get-source')

View file

@ -0,0 +1,9 @@
'use strict';
/* Dummy javascript file */
function hello() {
return 'hello world';
}
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIm9yaWdpbmFsLmpzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7O0FBQUE7O0FBRUEsU0FBUyxLQUFULEdBQWtCO0FBQ2pCLFFBQU8sYUFBUDtBQUFzQiIsImZpbGUiOiJvcmlnaW5hbC5iYWJlbGVkLndpdGguaW5saW5lLnNvdXJjZW1hcC5qcyIsInNvdXJjZXNDb250ZW50IjpbIi8qXHREdW1teSBqYXZhc2NyaXB0IGZpbGVcdCovXG5cbmZ1bmN0aW9uIGhlbGxvICgpIHtcblx0cmV0dXJuICdoZWxsbyB3b3JsZCcgfSJdfQ==

View file

@ -0,0 +1,4 @@
/* Dummy javascript file */
function hello () {
return 'hello world' }

View file

@ -0,0 +1,4 @@
function hello() {
return "hello world";
}
//# sourceMappingURL=original.uglified.beautified.js.map

View file

@ -0,0 +1 @@
{"version":3,"sources":["original.uglified.js"],"names":["hello"],"mappings":"AAAA,SAASA;IAAQ,OAAM"}

View file

@ -0,0 +1,2 @@
function hello(){return"hello world"}
//# sourceMappingURL=original.uglified.js.map

View file

@ -0,0 +1 @@
{"version":3,"sources":["original.js"],"names":["hello"],"mappings":"AAEA,QAASA,SACR,MAAO"}

View file

@ -0,0 +1,2 @@
function hello(){return"hello world"}
//# sourceMappingURL=original.uglified.with.sources.js.map

View file

@ -0,0 +1 @@
{"version":3,"sources":["## embedded ##"],"names":["hello"],"mappings":"AAEA,QAASA,SACR,MAAO","sourcesContent":["/*\tDummy javascript file\t*/\n\nfunction hello () {\n\treturn 'hello world' }"]}

View file

@ -0,0 +1 @@
<html></html>

View file

@ -0,0 +1,98 @@
/* TODO: make it work in Travis CI
------------------------------------------------------------------------ */
const selenium = require ('selenium-webdriver/testing')
/* ------------------------------------------------------------------------ */
selenium.describe ('Chrome test', (done) => {
const webdriver = require ('selenium-webdriver')
, path = require ('path')
, fs = require ('fs')
, memFS = new (require ('memory-fs')) ()
, it = selenium.it
, webpack = require ('webpack')
, logging = require ('selenium-webdriver/lib/logging')
let driver
/* Prepare ChromeDriver (with CORS disabled and log interception enabled) */
selenium.before (() => driver =
new webdriver
.Builder ()
.withCapabilities (
webdriver.Capabilities
.chrome ()
.setLoggingPrefs (new logging.Preferences ().setLevel (logging.Type.BROWSER, logging.Level.ALL))
.set ('chromeOptions', {
'args': ['--disable-web-security'] }))
.build ())
selenium.after (() => driver.quit ())
it ('works', async () => {
/* Compile get-source */
const compiledScript = await (new Promise (resolve => { Object.assign (webpack ({
entry: './test/files/get-source.webpack.entry.js',
output: { path: '/', filename: 'get-source.webpack.compiled.js' },
plugins: [ new webpack.IgnorePlugin(/^fs$/) ]
}), { outputFileSystem: memFS }).run ((err, stats) => {
if (err) throw err
resolve (memFS.readFileSync ('/get-source.webpack.compiled.js').toString ('utf-8'))
})
}))
/* Inject it into Chrome */
driver.get ('file://' + path.resolve ('./test/files/test.html'))
driver.executeScript (compiledScript)
/* Execute test */
const exec = fn => driver.executeScript (`(${fn.toString ()})()`)
try {
await exec (function () {
path.relativeToFile ('http://foo.com/scripts/bar.js', '../bar.js.map')
.should.equal ('http://foo.com/bar.js.map')
path.relativeToFile ('http://foo.com/scripts/bar.js', 'http://bar.js.map')
.should.equal ('http://bar.js.map')
path.relativeToFile ('http://foo.com/scripts/bar.js', '/bar.js.map')
.should.equal ('file:///bar.js.map')
path.relativeToFile ('http://foo.com/scripts/bar.js', '//bar.com/bar.js.map')
.should.equal ('http://bar.com/bar.js.map')
var loc = getSource ('../original.uglified.beautified.js').resolve ({ line: 2, column: 4 })
loc.line.should.equal (4)
loc.column.should.equal (2)
loc.sourceFile.path.should.contain ('test/files/original.js')
loc.sourceLine.should.equal ('\treturn \'hello world\' }')
})
} catch (e) { throw e } finally {
driver.manage ().logs ().get (logging.Type.BROWSER).then (entries => {
entries.forEach (entry => {
console.log('[BROWSER] [%s] %s', entry.level.name, entry.message);
})
})
}
})
})
/* ------------------------------------------------------------------------ */

View file

@ -0,0 +1,222 @@
"use strict";
/* NOTE: I've used supervisor to auto-restart mocha, because mocha --watch
didn't work for selenium tests (not reloading them)...
------------------------------------------------------------------ */
require ('chai').should ()
/* ------------------------------------------------------------------------ */
describe ('get-source', () => {
const getSource = require ('../get-source'),
fs = require ('fs'),
path = require ('path')
it ('cache sanity check', () => {
getSource ('./get-source.js').should.equal (getSource ('./get-source.js'))
getSource ('./get-source.js').should.not.equal (getSource ('./package.json'))
})
it ('reads sources (not sourcemapped)', () => {
const original = getSource ('./test/files/original.js')
original.path.should.equal (path.resolve ('./test/files/original.js')) // resolves input paths
original.text.should.equal (fs.readFileSync ('./test/files/original.js', { encoding: 'utf-8' }))
original.lines.should.deep.equal ([
'/*\tDummy javascript file\t*/',
'',
'function hello () {',
'\treturn \'hello world\' }'
])
const resolved = original.resolve ({ line: 4, column: 1 })
resolved.line.should.equal (4)
resolved.column.should.equal (1)
resolved.sourceFile.should.equal (original)
resolved.sourceLine.should.equal ('\treturn \'hello world\' }')
})
it ('reads sources (sourcemapped, with external links)', () => {
const uglified = getSource ('./test/files/original.uglified.js')
uglified.path.should.equal (path.resolve ('./test/files/original.uglified.js'))
uglified.lines.should.deep.equal ([
'function hello(){return"hello world"}',
'//# sourceMappingURL=original.uglified.js.map',
''
])
// uglified.sourceMap.should.not.equal (undefined)
// uglified.sourceMap.should.equal (uglified.sourceMap) // memoization should work
const resolved = uglified.resolve ({ line: 1, column: 18 }) // should be tolerant to column omission
resolved.line.should.equal (4)
resolved.column.should.equal (2)
resolved.sourceFile.should.equal (getSource ('./test/files/original.js'))
resolved.sourceLine.should.equal ('\treturn \'hello world\' }')
})
it ('reads sources (sourcemapped, with external links) — ASYNC', () => {
const uglified = getSource.async ('./test/files/original.uglified.js')
return uglified.then (uglified => {
uglified.path.should.equal (path.resolve ('./test/files/original.uglified.js'))
uglified.lines.should.deep.equal ([
'function hello(){return"hello world"}',
'//# sourceMappingURL=original.uglified.js.map',
''
])
// uglified.sourceMap.should.not.equal (undefined)
// uglified.sourceMap.should.equal (uglified.sourceMap) // memoization should work
return uglified.resolve ({ line: 1, column: 18 }).then (resolved => {
return getSource.async ('./test/files/original.js').then (originalFile => {
resolved.line.should.equal (4)
resolved.column.should.equal (2)
resolved.sourceFile.should.equal (originalFile)
resolved.sourceLine.should.equal ('\treturn \'hello world\' }')
})
})
})
})
it ('reads sources (sourcemapped, with embedded sources)', () => {
const uglified = getSource ('./test/files/original.uglified.with.sources.js')
uglified.path.should.equal (path.resolve ('./test/files/original.uglified.with.sources.js'))
uglified.lines.should.deep.equal ([
'function hello(){return"hello world"}',
'//# sourceMappingURL=original.uglified.with.sources.js.map',
''
])
// uglified.sourceMap.should.not.equal (undefined)
const resolved = uglified.resolve ({ line: 1, column: 18 })
resolved.line.should.equal (4)
resolved.column.should.equal (2)
resolved.sourceFile.path.should.equal (path.resolve ('./test/files') + '/## embedded ##') // I've changed the filename manually, by editing .map file
resolved.sourceLine.should.equal ('\treturn \'hello world\' }')
})
it ('reads sources (sourcemapped, with inline base64 sourcemaps)', () => {
const babeled = getSource ('./test/files/original.babeled.with.inline.sourcemap.js')
// babeled.sourceMap.should.not.equal (undefined)
// babeled.sourceMap.file.path.should.equal (babeled.path)
const resolved = babeled.resolve ({ line: 6, column: 1 })
resolved.line.should.equal (4)
resolved.column.should.equal (2)
resolved.sourceLine.should.equal ('\treturn \'hello world\' }')
})
it ('supports even CHAINED sourcemaps!', () => {
/* original.js → original.uglified.js → original.uglified.beautified.js */
const beautified = getSource ('./test/files/original.uglified.beautified.js')
beautified.path.should.equal (path.resolve ('./test/files/original.uglified.beautified.js'))
beautified.text.should.equal (fs.readFileSync ('./test/files/original.uglified.beautified.js', { encoding: 'utf-8' }))
// beautified.sourceMap.should.not.equal (undefined)
const resolved = beautified.resolve ({ line: 2, column: 4 })
resolved.line.should.equal (4)
resolved.column.should.equal (2)
resolved.sourceFile.path.should.equal (path.resolve ('./test/files/original.js'))
resolved.sourceLine.should.equal ('\treturn \'hello world\' }')
})
it ('adheres to async interface', () => {
return getSource.async ('./get-source.js').then (result => {
;(result.resolve ({ line: 1, column: 0 }) instanceof Promise).should.equal (true)
})
})
it ('supports even CHAINED sourcemaps! — ASYNC', () => {
/* original.js → original.uglified.js → original.uglified.beautified.js */
return getSource.async ('./test/files/original.uglified.beautified.js').then (beautified => {
beautified.text.should.equal (fs.readFileSync ('./test/files/original.uglified.beautified.js', { encoding: 'utf-8' }))
beautified.path.should.equal (path.resolve ('./test/files/original.uglified.beautified.js'))
return beautified.resolve ({ line: 2, column: 4 }).then (resolved => {
resolved.line.should.equal (4)
resolved.column.should.equal (2)
resolved.sourceFile.path.should.equal (path.resolve ('./test/files/original.js'))
resolved.sourceLine.should.equal ('\treturn \'hello world\' }')
})
})
})
it ('does some error handling', () => {
const nonsense = getSource ('abyrvalg')
nonsense.text.should.equal ('')
nonsense.error.should.be.an.instanceof (Error)
const resolved = nonsense.resolve ({ line: 5, column: 0 })
resolved.error.should.equal (nonsense.error)
resolved.sourceLine.should.equal ('')
resolved.sourceFile.path.should.equal ('abyrvalg')
})
it ('does some error handling - ASYNC', () => {
return getSource.async ('abyrvalg').then (x => { console.log (x) }).catch (error => {
error.should.be.an.instanceof (Error)
})
})
it ('allows absolute paths', () => {
getSource (require ('path').resolve ('./get-source.js')).should.equal (getSource ('./get-source.js'))
})
it ('caching works', () => {
const files =
[ './get-source.js',
'./package.json',
'./test/files/original.js',
'./test/files/original.uglified.js',
'./test/files/original.uglified.js.map',
'./test/files/original.uglified.with.sources.js',
'./test/files/original.uglified.with.sources.js.map',
'./test/files/original.babeled.with.inline.sourcemap.js',
'./test/files/original.uglified.beautified.js',
'./test/files/original.uglified.beautified.js.map',
'./abyrvalg' ]
Object.keys (getSource.getCache ()).should.deep.equal (files.map (x => path.resolve (x)))
getSource.resetCache ()
Object.keys (getSource.getCache ()).length.should.equal (0)
})
})

View file

@ -0,0 +1,56 @@
"use strict";
/* ------------------------------------------------------------------------ */
require ('chai').should ()
/* ------------------------------------------------------------------------ */
describe ('path', () => {
const path = require ('../impl/path')
it ('resolves', () => {
path.resolve ('./foo/bar/../qux').should.equal (process.cwd () + '/foo/qux')
})
it ('normalizes', () => {
path.normalize ('./foo/./bar/.././.././qux.map./').should.equal ('qux.map./')
path.normalize ('/a/b').should.equal ('/a/b')
path.normalize ('http://foo/bar').should.equal ('http://foo/bar')
})
it ('computes relative location', () => {
path.relativeToFile ('/foo/bar.js', './qux.map')
.should.equal ('/foo/qux.map')
path.relativeToFile ('/foo/bar/baz.js', './../.././qux.map')
.should.equal ('/qux.map')
path.relativeToFile ('/foo/bar', 'webpack:something')
.should.equal ('webpack:something')
path.relativeToFile ('/foo/bar', 'web/pack:something')
.should.equal ('/foo/web/pack:something')
})
it ('works with data URIs', () => {
path.relativeToFile ('/foo/bar.js', 'data:application/json;charset=utf-8;base64,eyJ2ZXJza==')
.should.equal ( 'data:application/json;charset=utf-8;base64,eyJ2ZXJza==')
path.relativeToFile ('data:application/json;charset=utf-8;base64,eyJ2ZXJza==', 'foo.js')
.should.equal ( 'foo.js')
})
it ('implements isURL', () => {
path.isURL ('foo.js').should.equal (false)
path.isURL ('/foo/bar.js').should.equal (false)
path.isURL ('https://google.com').should.equal (true)
})
})