@@ -33,6 +33,7 @@ var stream = require("stream");
33
33
var exec = require ( "child_process" ) . exec ;
34
34
var spawn = require ( "child_process" ) . spawn ;
35
35
var spawnSync = require ( "child_process" ) . spawnSync ;
36
+ var stripComments = require ( "gulp-strip-comments" ) ;
36
37
var streamqueue = require ( "streamqueue" ) ;
37
38
var merge = require ( "merge-stream" ) ;
38
39
var zip = require ( "gulp-zip" ) ;
@@ -105,6 +106,7 @@ const DEFINES = Object.freeze({
105
106
COMPONENTS : false ,
106
107
LIB : false ,
107
108
IMAGE_DECODERS : false ,
109
+ NO_SOURCE_MAP : false ,
108
110
} ) ;
109
111
110
112
function transform ( charEncoding , transformFunction ) {
@@ -182,7 +184,8 @@ function createWebpackConfig(defines, output) {
182
184
var enableSourceMaps =
183
185
! bundleDefines . MOZCENTRAL &&
184
186
! bundleDefines . CHROME &&
185
- ! bundleDefines . TESTING ;
187
+ ! bundleDefines . TESTING &&
188
+ ! bundleDefines . NO_SOURCE_MAP ;
186
189
var skipBabel = bundleDefines . SKIP_BABEL ;
187
190
188
191
// `core-js` (see https://github.com/zloirock/core-js/issues/514),
@@ -343,6 +346,53 @@ function createScriptingBundle(defines) {
343
346
. pipe ( replaceJSRootName ( scriptingAMDName , "pdfjsScripting" ) ) ;
344
347
}
345
348
349
+ function createSandboxBundle ( defines , code ) {
350
+ var sandboxAMDName = "pdfjs-dist/build/pdf.sandbox" ;
351
+ var sandboxOutputName = "pdf.sandbox.js" ;
352
+ var sandboxFileConfig = createWebpackConfig ( defines , {
353
+ filename : sandboxOutputName ,
354
+ library : sandboxAMDName ,
355
+ libraryTarget : "umd" ,
356
+ umdNamedDefine : true ,
357
+ } ) ;
358
+
359
+ // The code is the one from the bundle pdf.scripting.js
360
+ // so in order to have it in a string (which will be eval-ed
361
+ // in the sandbox) we must escape some chars.
362
+ // This way we've all the code (initialization+sandbox) in
363
+ // the same bundle.
364
+ code = code . replace ( / [ " \\ \n \t ] / g, match => {
365
+ if ( match === "\n" ) {
366
+ return "\\n" ;
367
+ }
368
+ if ( match === "\t" ) {
369
+ return "\\t" ;
370
+ }
371
+ return `\\${ match } ` ;
372
+ } ) ;
373
+ return (
374
+ gulp
375
+ . src ( "./src/scripting_api/quickjs-sandbox.js" )
376
+ . pipe ( webpack2Stream ( sandboxFileConfig ) )
377
+ . pipe ( replaceWebpackRequire ( ) )
378
+ . pipe ( replaceJSRootName ( sandboxAMDName , "pdfjsSandbox" ) )
379
+ // put the code in a string to be eval-ed in the sandbox
380
+ . pipe ( replace ( "/* INITIALIZATION_CODE */" , `${ code } ` ) )
381
+ ) ;
382
+ }
383
+
384
+ function buildSandbox ( defines , dir ) {
385
+ const scriptingDefines = builder . merge ( defines , { NO_SOURCE_MAP : true } ) ;
386
+ return createScriptingBundle ( scriptingDefines )
387
+ . pipe ( stripComments ( ) )
388
+ . pipe ( gulp . dest ( dir + "build" ) )
389
+ . on ( "data" , file => {
390
+ const content = file . contents . toString ( ) ;
391
+ createSandboxBundle ( defines , content ) . pipe ( gulp . dest ( dir + "build" ) ) ;
392
+ fs . unlinkSync ( dir + "build/pdf.scripting.js" ) ;
393
+ } ) ;
394
+ }
395
+
346
396
function createWorkerBundle ( defines ) {
347
397
var workerAMDName = "pdfjs-dist/build/pdf.worker" ;
348
398
var workerOutputName = "pdf.worker.js" ;
@@ -494,6 +544,25 @@ function makeRef(done, bot) {
494
544
} ) ;
495
545
}
496
546
547
+ gulp . task ( "sandbox" , function ( done ) {
548
+ const defines = builder . merge ( DEFINES , { GENERIC : true } ) ;
549
+ buildSandbox ( defines , GENERIC_DIR ) ;
550
+ done ( ) ;
551
+ } ) ;
552
+
553
+ gulp . task ( "watch-sandbox" , function ( done ) {
554
+ const defines = builder . merge ( DEFINES , { GENERIC : true } ) ;
555
+ buildSandbox ( defines , GENERIC_DIR ) ;
556
+ const watcher = gulp . watch ( [
557
+ "src/scripting_api/*.js" ,
558
+ "external/quickjs/*.js" ,
559
+ ] ) ;
560
+ watcher . on ( "change" , function ( ) {
561
+ buildSandbox ( defines , GENERIC_DIR ) ;
562
+ } ) ;
563
+ done ( ) ;
564
+ } ) ;
565
+
497
566
gulp . task ( "default" , function ( done ) {
498
567
console . log ( "Available tasks:" ) ;
499
568
var tasks = Object . keys ( gulp . registry ( ) . tasks ( ) ) ;
@@ -762,26 +831,47 @@ function buildGeneric(defines, dir) {
762
831
// HTML5 browsers, which implement modern ECMAScript features.
763
832
gulp . task (
764
833
"generic" ,
765
- gulp . series ( "buildnumber" , "default_preferences" , "locale" , function ( ) {
766
- console . log ( ) ;
767
- console . log ( "### Creating generic viewer" ) ;
768
- var defines = builder . merge ( DEFINES , { GENERIC : true } ) ;
834
+ gulp . series (
835
+ "buildnumber" ,
836
+ "default_preferences" ,
837
+ "locale" ,
838
+ function ( ) {
839
+ console . log ( ) ;
840
+ console . log ( "### Creating generic viewer" ) ;
841
+ var defines = builder . merge ( DEFINES , { GENERIC : true } ) ;
769
842
770
- return buildGeneric ( defines , GENERIC_DIR ) ;
771
- } )
843
+ return buildGeneric ( defines , GENERIC_DIR ) ;
844
+ } ,
845
+ "sandbox"
846
+ )
772
847
) ;
773
848
774
849
// Builds the generic production viewer that should be compatible with most
775
850
// older HTML5 browsers.
776
851
gulp . task (
777
852
"generic-es5" ,
778
- gulp . series ( "buildnumber" , "default_preferences" , "locale" , function ( ) {
779
- console . log ( ) ;
780
- console . log ( "### Creating generic (ES5) viewer" ) ;
781
- var defines = builder . merge ( DEFINES , { GENERIC : true , SKIP_BABEL : false } ) ;
853
+ gulp . series (
854
+ "buildnumber" ,
855
+ "default_preferences" ,
856
+ "locale" ,
857
+ function ( ) {
858
+ console . log ( ) ;
859
+ console . log ( "### Creating generic (ES5) viewer" ) ;
860
+ var defines = builder . merge ( DEFINES , {
861
+ GENERIC : true ,
862
+ SKIP_BABEL : false ,
863
+ } ) ;
782
864
783
- return buildGeneric ( defines , GENERIC_ES5_DIR ) ;
784
- } )
865
+ return buildGeneric ( defines , GENERIC_ES5_DIR ) ;
866
+ } ,
867
+ function ( ) {
868
+ const defines = builder . merge ( DEFINES , {
869
+ GENERIC : true ,
870
+ SKIP_BABEL : false ,
871
+ } ) ;
872
+ return buildSandbox ( defines , GENERIC_ES5_DIR ) ;
873
+ }
874
+ )
785
875
) ;
786
876
787
877
function buildComponents ( defines , dir ) {
@@ -908,33 +998,61 @@ function buildMinified(defines, dir) {
908
998
909
999
gulp . task (
910
1000
"minified-pre" ,
911
- gulp . series ( "buildnumber" , "default_preferences" , "locale" , function ( ) {
912
- console . log ( ) ;
913
- console . log ( "### Creating minified viewer" ) ;
914
- var defines = builder . merge ( DEFINES , { MINIFIED : true , GENERIC : true } ) ;
1001
+ gulp . series (
1002
+ "buildnumber" ,
1003
+ "default_preferences" ,
1004
+ "locale" ,
1005
+ function ( ) {
1006
+ console . log ( ) ;
1007
+ console . log ( "### Creating minified viewer" ) ;
1008
+ var defines = builder . merge ( DEFINES , { MINIFIED : true , GENERIC : true } ) ;
915
1009
916
- return buildMinified ( defines , MINIFIED_DIR ) ;
917
- } )
1010
+ return buildSandbox ( defines , MINIFIED_DIR ) ;
1011
+ } ,
1012
+ function ( ) {
1013
+ var defines = builder . merge ( DEFINES , { MINIFIED : true , GENERIC : true } ) ;
1014
+
1015
+ return buildMinified ( defines , MINIFIED_DIR ) ;
1016
+ }
1017
+ )
918
1018
) ;
919
1019
920
1020
gulp . task (
921
1021
"minified-es5-pre" ,
922
- gulp . series ( "buildnumber" , "default_preferences" , "locale" , function ( ) {
923
- console . log ( ) ;
924
- console . log ( "### Creating minified (ES5) viewer" ) ;
925
- var defines = builder . merge ( DEFINES , {
926
- MINIFIED : true ,
927
- GENERIC : true ,
928
- SKIP_BABEL : false ,
929
- } ) ;
1022
+ gulp . series (
1023
+ "buildnumber" ,
1024
+ "default_preferences" ,
1025
+ "locale" ,
1026
+ function ( ) {
1027
+ console . log ( ) ;
1028
+ console . log ( "### Creating minified (ES5) viewer" ) ;
1029
+ var defines = builder . merge ( DEFINES , {
1030
+ MINIFIED : true ,
1031
+ GENERIC : true ,
1032
+ SKIP_BABEL : false ,
1033
+ } ) ;
930
1034
931
- return buildMinified ( defines , MINIFIED_ES5_DIR ) ;
932
- } )
1035
+ return buildSandbox ( defines , MINIFIED_ES5_DIR ) ;
1036
+ } ,
1037
+
1038
+ function ( ) {
1039
+ var defines = builder . merge ( DEFINES , {
1040
+ MINIFIED : true ,
1041
+ GENERIC : true ,
1042
+ SKIP_BABEL : false ,
1043
+ } ) ;
1044
+
1045
+ return buildMinified ( defines , MINIFIED_ES5_DIR ) ;
1046
+ }
1047
+ )
933
1048
) ;
934
1049
935
1050
async function parseMinified ( dir ) {
936
1051
var pdfFile = fs . readFileSync ( dir + "/build/pdf.js" ) . toString ( ) ;
937
1052
var pdfWorkerFile = fs . readFileSync ( dir + "/build/pdf.worker.js" ) . toString ( ) ;
1053
+ var pdfSandboxFile = fs
1054
+ . readFileSync ( dir + "/build/pdf.sandbox.js" )
1055
+ . toString ( ) ;
938
1056
var pdfImageDecodersFile = fs
939
1057
. readFileSync ( dir + "/image_decoders/pdf.image_decoders.js" )
940
1058
. toString ( ) ;
@@ -968,6 +1086,10 @@ async function parseMinified(dir) {
968
1086
dir + "/build/pdf.worker.min.js" ,
969
1087
( await Terser . minify ( pdfWorkerFile , options ) ) . code
970
1088
) ;
1089
+ fs . writeFileSync (
1090
+ dir + "/build/pdf.sandbox.min.js" ,
1091
+ ( await Terser . minify ( pdfSandboxFile , options ) ) . code
1092
+ ) ;
971
1093
fs . writeFileSync (
972
1094
dir + "image_decoders/pdf.image_decoders.min.js" ,
973
1095
( await Terser . minify ( pdfImageDecodersFile , options ) ) . code
@@ -980,9 +1102,14 @@ async function parseMinified(dir) {
980
1102
fs . unlinkSync ( dir + "/web/debugger.js" ) ;
981
1103
fs . unlinkSync ( dir + "/build/pdf.js" ) ;
982
1104
fs . unlinkSync ( dir + "/build/pdf.worker.js" ) ;
1105
+ fs . unlinkSync ( dir + "/build/pdf.sandbox.js" ) ;
983
1106
984
1107
fs . renameSync ( dir + "/build/pdf.min.js" , dir + "/build/pdf.js" ) ;
985
1108
fs . renameSync ( dir + "/build/pdf.worker.min.js" , dir + "/build/pdf.worker.js" ) ;
1109
+ fs . renameSync (
1110
+ dir + "/build/pdf.sandbox.min.js" ,
1111
+ dir + "/build/pdf.sandbox.js"
1112
+ ) ;
986
1113
fs . renameSync (
987
1114
dir + "/image_decoders/pdf.image_decoders.min.js" ,
988
1115
dir + "/image_decoders/pdf.image_decoders.js"
@@ -1176,7 +1303,14 @@ gulp.task(
1176
1303
} )
1177
1304
) ;
1178
1305
1179
- gulp . task ( "chromium" , gulp . series ( "chromium-pre" ) ) ;
1306
+ gulp . task (
1307
+ "chromium" ,
1308
+ gulp . series ( "chromium-pre" , function ( ) {
1309
+ var defines = builder . merge ( DEFINES , { CHROME : true , SKIP_BABEL : false } ) ;
1310
+ var CHROME_BUILD_CONTENT_DIR = BUILD_DIR + "/chromium/content/" ;
1311
+ return buildSandbox ( defines , CHROME_BUILD_CONTENT_DIR ) ;
1312
+ } )
1313
+ ) ;
1180
1314
1181
1315
gulp . task ( "jsdoc" , function ( done ) {
1182
1316
console . log ( ) ;
@@ -1276,7 +1410,7 @@ function buildLib(defines, dir) {
1276
1410
return merge ( [
1277
1411
gulp . src (
1278
1412
[
1279
- "src/{core,display,scripting_api, shared}/*.js" ,
1413
+ "src/{core,display,shared}/*.js" ,
1280
1414
"!src/shared/{cffStandardStrings,fonts_utils}.js" ,
1281
1415
"src/{pdf,pdf.worker}.js" ,
1282
1416
] ,
@@ -1294,24 +1428,46 @@ function buildLib(defines, dir) {
1294
1428
1295
1429
gulp . task (
1296
1430
"lib" ,
1297
- gulp . series ( "buildnumber" , "default_preferences" , function ( ) {
1298
- var defines = builder . merge ( DEFINES , { GENERIC : true , LIB : true } ) ;
1431
+ gulp . series (
1432
+ "buildnumber" ,
1433
+ "default_preferences" ,
1434
+ function ( ) {
1435
+ var defines = builder . merge ( DEFINES , { GENERIC : true , LIB : true } ) ;
1299
1436
1300
- return buildLib ( defines , "build/lib/" ) ;
1301
- } )
1437
+ return buildLib ( defines , "build/lib/" ) ;
1438
+ } ,
1439
+ function ( ) {
1440
+ var defines = builder . merge ( DEFINES , { GENERIC : true , LIB : true } ) ;
1441
+
1442
+ return buildSandbox ( defines , "build/lib/" ) ;
1443
+ }
1444
+ )
1302
1445
) ;
1303
1446
1304
1447
gulp . task (
1305
1448
"lib-es5" ,
1306
- gulp . series ( "buildnumber" , "default_preferences" , function ( ) {
1307
- var defines = builder . merge ( DEFINES , {
1308
- GENERIC : true ,
1309
- LIB : true ,
1310
- SKIP_BABEL : false ,
1311
- } ) ;
1449
+ gulp . series (
1450
+ "buildnumber" ,
1451
+ "default_preferences" ,
1452
+ function ( ) {
1453
+ var defines = builder . merge ( DEFINES , {
1454
+ GENERIC : true ,
1455
+ LIB : true ,
1456
+ SKIP_BABEL : false ,
1457
+ } ) ;
1312
1458
1313
- return buildLib ( defines , "build/lib-es5/" ) ;
1314
- } )
1459
+ return buildLib ( defines , "build/lib-es5/" ) ;
1460
+ } ,
1461
+ function ( ) {
1462
+ var defines = builder . merge ( DEFINES , {
1463
+ GENERIC : true ,
1464
+ LIB : true ,
1465
+ SKIP_BABEL : false ,
1466
+ } ) ;
1467
+
1468
+ return buildSandbox ( defines , "build/lib-es5/" ) ;
1469
+ }
1470
+ )
1315
1471
) ;
1316
1472
1317
1473
function compressPublish ( targetName , dir ) {
0 commit comments