express-tsx
Advanced tools
Comparing version 3.0.26 to 3.0.27
@@ -33,2 +33,3 @@ "use strict"; | ||
}; | ||
this.getScriptVersion = (file) => this.files[file].version; | ||
this.files = new Proxy({}, { | ||
@@ -64,3 +65,3 @@ get(target, filename) { | ||
getScriptFileNames: () => Object.keys(this.files), | ||
getScriptVersion: (file) => this.files[file].version, | ||
getScriptVersion: this.getScriptVersion, | ||
getScriptSnapshot: (file) => !fs.existsSync(file) ? undefined : ts.ScriptSnapshot.fromString(fs.readFileSync(file).toString()), | ||
@@ -67,0 +68,0 @@ getCurrentDirectory: () => rootDir, |
@@ -33,6 +33,7 @@ "use strict"; | ||
exports.basePath = '/express-tsx'; | ||
exports.MaxAge = 60 * 60 * 12; | ||
exports.middleware.use(exports.basePath, function (req, res) { | ||
res.type('js'); | ||
let module = req.param('filename', ''); | ||
module = module.replace(/\.js$/, ''); | ||
module = module.replace(/\.(js|tsx|ts|jsx)$/, ''); | ||
let moduleTry; | ||
@@ -46,2 +47,5 @@ switch (true) { | ||
case Reflect.has(Compile_1.compile.files, moduleTry = module + '.ts'): | ||
case Reflect.has(Compile_1.compile.files, moduleTry = module + '.jsx'): | ||
case Reflect.has(Compile_1.compile.files, moduleTry = module + '.js'): | ||
res.setHeader('cache-control', 'max-age=' + exports.MaxAge); | ||
res.send(Compile_1.compile.compile(moduleTry).outputFiles[0].text); | ||
@@ -48,0 +52,0 @@ break; |
@@ -6,2 +6,4 @@ "use strict"; | ||
const path_1 = require("path"); | ||
const lodash_1 = require("lodash"); | ||
const middleware_1 = require("./middleware"); | ||
exports.contentType = { | ||
@@ -13,7 +15,19 @@ request: { | ||
'content-type': 'application/javascript', | ||
'Cache-Control': 'max-age=' + middleware_1.MaxAge, | ||
} | ||
}; | ||
const middleware_1 = require("./middleware"); | ||
exports.dataContentType = { | ||
request: { | ||
accept: '*/*', | ||
}, | ||
response: { | ||
'content-type': 'application/javascript', | ||
'Cache-Control': 'no-cache', | ||
} | ||
}; | ||
const middleware_2 = require("./middleware"); | ||
const configExtend = require("config-extend"); | ||
const etag = require("etag"); | ||
exports.push = (file, data) => { | ||
data = new middleware_1.config(data); | ||
data = new middleware_2.config(data); | ||
let req = data.req; | ||
@@ -26,10 +40,15 @@ let res = data.res; | ||
let imports = Compile_1.compile.getImports(file); | ||
let relativePath = path_1.join(req.app.path(), middleware_1.basePath).replace(/\\/g, '/'); | ||
let imports_path = imports.map(module => `${relativePath}?filename=${module}`) | ||
res.setHeader('ETag', imports.join(';')); | ||
let preloaded = (req.header('if-none-match') || '').split(';'); | ||
imports = lodash_1.difference(imports, preloaded); | ||
// debugger | ||
let relativePath = path_1.join(req.app.path(), middleware_2.basePath).replace(/\\/g, '/'); | ||
let imports_path = imports.map(module => `${relativePath}?version=${Compile_1.compile.getScriptVersion(module)}&filename=${module}`) | ||
.map(m => m.replace(/\.(tsx|ts|js|jsx)$/, '')) | ||
.map(encodeURI); | ||
if (res.push) { | ||
res.push(dataurl, exports.contentType).end(`define(${JSON.stringify(res.ViewData)})`); | ||
res.push(dataurl, exports.dataContentType).end(`define(${JSON.stringify(res.ViewData)})`); | ||
imports.forEach((module, index) => { | ||
res.push(imports_path[index], exports.contentType).end(Compile_1.compile.compile(module).outputFiles[0].text); | ||
let body = Compile_1.compile.compile(module).outputFiles[0].text; | ||
res.push(imports_path[index], configExtend({ response: { ETag: etag(body, { weak: true }) } }, exports.contentType)).end(body); | ||
}); | ||
@@ -39,4 +58,5 @@ } | ||
data.imports = [dataurl].concat(imports); | ||
res.setHeader('Link', data.imports.map(path => `<${path}>; rel=preload; as=script`).join(',')); | ||
return data; | ||
}; | ||
//# sourceMappingURL=push.js.map |
{ | ||
"name": "express-tsx", | ||
"version": "3.0.26", | ||
"version": "3.0.27", | ||
"description": "express view tsx render", | ||
@@ -9,2 +9,4 @@ "main": "lib/index.js", | ||
"config-extend": "^0.1.1", | ||
"etag": "^1.8.0", | ||
"lodash": "^4.17.4", | ||
"mkdirp": "^0.5.1" | ||
@@ -21,2 +23,3 @@ }, | ||
"@types/express": "^4.0.35", | ||
"@types/lodash": "^4.14.64", | ||
"@types/node": "^7.0.12", | ||
@@ -23,0 +26,0 @@ "@types/react": "^15.0.24", |
@@ -36,3 +36,3 @@ import ts = require('typescript') | ||
getScriptFileNames:()=>Object.keys(this.files), | ||
getScriptVersion:(file)=>this.files[file].version, | ||
getScriptVersion:this.getScriptVersion, | ||
getScriptSnapshot:(file)=>!fs.existsSync(file)?undefined:ts.ScriptSnapshot.fromString(fs.readFileSync(file).toString()), | ||
@@ -57,2 +57,3 @@ getCurrentDirectory:()=>rootDir, | ||
static normalize = (f)=>f.replace(/\\/g,'/') | ||
getScriptVersion = (file:string)=>this.files[file].version | ||
files = new Proxy<{[key:string]:Shot}>({},{ | ||
@@ -59,0 +60,0 @@ get(target,filename:string){ |
@@ -42,6 +42,7 @@ import { Router,Response,Request } from "express"; | ||
export const basePath = '/express-tsx' | ||
export const MaxAge = 60*60*12 | ||
middleware.use(basePath,function(req,res){ | ||
res.type('js') | ||
let module = req.param('filename','') | ||
module = module.replace(/\.js$/,'') | ||
module = module.replace(/\.(js|tsx|ts|jsx)$/,'') | ||
let moduleTry:string | ||
@@ -55,2 +56,5 @@ switch(true){ | ||
case Reflect.has(compile.files,moduleTry=module+'.ts'): | ||
case Reflect.has(compile.files,moduleTry=module+'.jsx'): | ||
case Reflect.has(compile.files,moduleTry=module+'.js'): | ||
res.setHeader('cache-control','max-age='+MaxAge) | ||
res.send(compile.compile(moduleTry).outputFiles[0].text) | ||
@@ -57,0 +61,0 @@ break |
@@ -5,3 +5,5 @@ import { compile } from "./Compile"; | ||
import { relative,join } from 'path' | ||
export let contentType = { | ||
import { difference } from "lodash"; | ||
import { MaxAge } from "./middleware"; | ||
export const contentType = { | ||
request:{ | ||
@@ -11,6 +13,18 @@ accept:'*/*', | ||
response:{ | ||
'content-type':'application/javascript', | ||
'content-type' :'application/javascript', | ||
'Cache-Control' :'max-age='+MaxAge, | ||
} | ||
} | ||
export const dataContentType = { | ||
request:{ | ||
accept:'*/*', | ||
}, | ||
response:{ | ||
'content-type' :'application/javascript', | ||
'Cache-Control' :'no-cache', | ||
} | ||
} | ||
import { config,basePath } from "./middleware"; | ||
import configExtend = require('config-extend') | ||
import etag = require('etag') | ||
export let push = (file,data:config)=>{ | ||
@@ -25,11 +39,16 @@ data = new config(data) | ||
let imports = compile.getImports(file) | ||
res.setHeader('ETag',imports.join(';')) | ||
let preloaded = (req.header('if-none-match') || '').split(';') | ||
imports = difference(imports,preloaded) | ||
// debugger | ||
let relativePath = join(req.app.path(),basePath).replace(/\\/g,'/') | ||
let imports_path = | ||
imports.map(module=>`${relativePath}?filename=${module}`) | ||
imports.map(module=>`${relativePath}?version=${compile.getScriptVersion(module)}&filename=${module}`) | ||
.map(m=>m.replace(/\.(tsx|ts|js|jsx)$/,'')) | ||
.map(encodeURI) | ||
if(res.push){// http2 push | ||
res.push(dataurl,contentType).end(`define(${JSON.stringify(res.ViewData)})`) | ||
res.push(dataurl,dataContentType).end(`define(${JSON.stringify(res.ViewData)})`) | ||
imports.forEach((module,index)=>{ | ||
res.push(imports_path[index],contentType).end(compile.compile(module).outputFiles[0].text) | ||
let body = compile.compile(module).outputFiles[0].text | ||
res.push(imports_path[index],configExtend({ response:{ ETag:etag(body,{ weak:true }) } },contentType)).end(body) | ||
}) | ||
@@ -39,3 +58,4 @@ } | ||
data.imports = [dataurl].concat(imports) | ||
res.setHeader('Link',data.imports.map(path=>`<${path}>; rel=preload; as=script`).join(',')) | ||
return data | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
36512
615
9
17
+ Addedetag@^1.8.0
+ Addedlodash@^4.17.4
+ Addedlodash@4.17.21(transitive)